Skip to content

feat(runtime): add promise_yield_create2 host function#15602

Draft
graphite wants to merge 5 commits intomasterfrom
anton-a/yield-create2
Draft

feat(runtime): add promise_yield_create2 host function#15602
graphite wants to merge 5 commits intomasterfrom
anton-a/yield-create2

Conversation

@graphite
Copy link
Copy Markdown
Contributor

Summary

  • Add new promise_yield_create2 host function that allows contracts to provide a custom 32-byte yield ID and timeout when creating yield/resume promises
  • Store bidirectional yield_id ↔ data_id mappings in the trie for duplicate detection and cleanup on resume/timeout
  • Gate behind YieldCreate2 nightly protocol feature (version 153)

Motivation

The current promise_yield_create returns a runtime-generated data_id that contracts must store in state to later call promise_yield_resume. For use cases like in-contract mempools (NEP-0616 wallet extensions, Aurora), this storage cost is prohibitive. promise_yield_create2 lets contracts provide their own deterministic yield ID, avoiding expensive storage.

Context: https://near.zulipchat.com/#narrow/channel/295558-core/topic/yield_create2

Host function signature

promise_yield_create2(
    method_name_len, method_name_ptr,
    arguments_len, arguments_ptr,
    gas, gas_weight,
    yield_id_len, yield_id_ptr,     // user-provided 32-byte yield ID
    yield_timeout_blocks,            // custom timeout (only 200 accepted for now)
    register_id                      // data_id written here
) -> u64  // promise index

Key design decisions

  • data_id generation: Normal generate_data_id(), not derived from yield_id
  • Trie mappings: YieldIdToDataId and DataIdToYieldId for duplicate detection and cleanup
  • Duplicate detection: Errors with YieldIdAlreadyExists if yield_id is already pending
  • Timeout: Only 200 blocks accepted for now; will be expanded later
  • Compatible with existing promise_yield_resume — data_id written to register

Test plan

  • Existing yield/resume integration tests pass (stable)
  • New create2_then_resume nightly test passes
  • New create2_and_resume_in_one_call nightly test passes
  • Parameter snapshot tests updated and passing
  • cargo check with nightly features passes

🤖 Generated with Claude Code

Introduce a new host function `promise_yield_create2` that allows smart
contracts to provide a custom 32-byte yield ID and timeout when creating
yield/resume promises. This avoids the storage cost of persisting
runtime-generated data IDs for use cases like in-contract mempools.

The runtime generates data_id normally and stores bidirectional
yield_id <-> data_id mappings in the trie for duplicate detection and
cleanup. The derived data_id is written to a register for use with the
existing `promise_yield_resume`. Mappings are cleaned up automatically
when yields are resumed or time out.

Currently only a timeout of 200 blocks is accepted; this will be
expanded in a future change.

Gated behind the `YieldCreate2` nightly protocol feature (version 153).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@graphite graphite requested a review from a team as a code owner April 19, 2026 15:13
@graphite graphite requested a review from darioush April 19, 2026 15:13
@graphite graphite marked this pull request as draft April 19, 2026 15:13
graphite and others added 2 commits April 19, 2026 17:26
Update auto-generated OpenAPI/OpenRPC specs and protocol schema to
reflect the new yield_create2_host_fns config field and new TrieKey
variants (YieldIdToDataId, DataIdToYieldId).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Update build_chain genesis hash snapshot (config change affects hash)
- Update runtime_config_view snapshots with yield_create2_host_fns field
- Regenerate 153.json snapshot after rebase onto latest master
- Add YieldIdToDataId and DataIdToYieldId intervals to resharding test

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 19, 2026

Codecov Report

❌ Patch coverage is 69.49153% with 108 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.40%. Comparing base (c21dadb) to head (8a4d808).

Files with missing lines Patch % Lines
runtime/near-vm-runner/src/logic/logic.rs 0.00% 68 Missing ⚠️
...untime/near-vm-runner/src/wasmtime_runner/logic.rs 85.71% 0 Missing and 14 partials ⚠️
core/store/src/utils/mod.rs 86.00% 7 Missing ⚠️
...me/near-vm-runner/src/logic/mocks/mock_external.rs 66.66% 5 Missing and 1 partial ⚠️
runtime/near-vm-runner/src/logic/errors.rs 0.00% 3 Missing ⚠️
runtime/runtime/src/ext.rs 92.50% 1 Missing and 2 partials ⚠️
core/primitives/src/trie_key.rs 91.66% 2 Missing ⚠️
core/primitives/src/types.rs 0.00% 2 Missing ⚠️
core/parameters/src/parameter_table.rs 0.00% 0 Missing and 1 partial ⚠️
core/parameters/src/view.rs 50.00% 1 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #15602      +/-   ##
==========================================
+ Coverage   69.38%   69.40%   +0.01%     
==========================================
  Files         940      940              
  Lines      212231   212583     +352     
  Branches   212231   212583     +352     
==========================================
+ Hits       147252   147536     +284     
- Misses      59125    59173      +48     
- Partials     5854     5874      +20     
Flag Coverage Δ
pytests-nightly 1.14% <0.00%> (-0.01%) ⬇️
unittests 68.69% <7.06%> (-0.08%) ⬇️
unittests-nightly 68.94% <69.20%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

graphite and others added 2 commits April 19, 2026 18:03
The previous generation had cargo build stderr mixed into the JSON.
Regenerated with stderr redirected.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add VMLogic-level unit tests for promise_yield_create,
promise_yield_resume, and promise_yield_create2 covering:
- successful create and resume flow
- empty method name validation
- malformed data_id rejection
- unknown data_id resume (returns false)
- yield_create2 with valid and invalid yield IDs
- yield_create2 timeout validation (only 200 accepted)
- view call prohibition

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant