Skip to content

fix: address rust tutorial issues from #191#192

Open
Keinberger wants to merge 4 commits intomainfrom
kbg/fix/rust-tutorial-issues-191
Open

fix: address rust tutorial issues from #191#192
Keinberger wants to merge 4 commits intomainfrom
kbg/fix/rust-tutorial-issues-191

Conversation

@Keinberger
Copy link
Copy Markdown
Collaborator

@Keinberger Keinberger commented Apr 28, 2026

Summary

  • removes the per-bin create_library helper and the cwd-dependent Path::new("../masm/...") pattern; rust-client tutorials now compile MASM via client.code_builder().with_linked_module(...) and load source via include_str!
  • migrates the three tutorial note scripts (hash_preimage_note, iterative_output_note, network_increment_note) to the v0.14.5 library form (@note_script pub proc main); both lockfiles bumped to miden-{client,client-sqlite-store,protocol,standards}@0.14.5
  • restructures oracle_data_query to mirror astraly-labs/pragma-miden/examples/consume-price: parameterized MASM template, explicit pair_word argument, AccountId::parse for hex-or-bech32 input, and a strict template-substitution gate

Details

create_library removal

  • the per-binary helper allocated a fresh DefaultSourceManager, parsed a Module, and assembled a Library. The resulting library was then linked into a client.code_builder() chain whose assembler used a different source manager — the cross-manager handoff is what triggers invalid source span: starting byte is out of bounds (miden-vm#2778)
  • replacement: client.code_builder().with_linked_module(path, source).compile_tx_script(script) — single chain, single manager, no intermediate Library
  • same change applied to component compilation: CodeBuilder::new().compile_component_code(...)client.code_builder().compile_component_code(...)
  • affected bins: counter_contract_deploy, counter_contract_increment, counter_contract_fpi, oracle_data_query, network_notes_counter_contract, mapping_example
  • affected docs: counter_contract_tutorial, foreign_procedure_invocation_tutorial, mappings_in_masm_how_to, network_transactions_tutorial, oracle_tutorial, public_account_interaction_tutorial

path handling

  • relative Path::new("../masm/...") resolved against cwd, so users following the docs literally (with masm/ inside their own project root) hit Os { code: 2, NotFound }
  • replaced with include_str!("../masm/...") (docs) and include_str!("../../../masm/...") (repo bins) — both resolve at compile time relative to the source file, regardless of where cargo run is invoked
  • repo bins and doc snippets diverge only in .. count because the repo's masm/ is shared with web-client and lives at repo root, while user projects have masm/ at project root

@note_script migration (v0.14.5)

  • starting with miden-protocol@0.14.5, CodeBuilder::compile_note_script calls Assembler::assemble_library (not assemble_program), so the legacy begin..end form is rejected with invalid module: unexpected entrypoint definition and a single pub proc carrying the @note_script attribute is required (per 0xMiden/protocol#2822)
  • both lockfiles bumped: rust-client/Cargo.lock and docs/Cargo.lock now pin miden-{client,client-sqlite-store,protocol,standards}@0.14.5 (verified via crate-by-crate sweep in the verification script)
  • three tutorial note scripts converted to the new form with pub proc main as the entrypoint procedure, matching upstream P2ID/P2IDE/SWAP/MINT/BURN convention; the doc snippets that mirror them are updated alongside, with a one-line prose note explaining the convention
  • the bins that load these note scripts (hash_preimage_note.rs, note_creation_in_masm.rs, network_notes_counter_contract.rs) chain client.code_builder().with_linked_module(...).compile_note_script(...) for the network-note-+-counter case, or compile_note_script directly for the standalone notes

oracle tutorial

  • oracle_data_query.rs accepts AccountId::parse so both Pragma README hex (0x...) and bech32 (mtst1...) forms work; the panic message points at the astraly-labs/pragma-miden#deployments README as the source of truth for the live testnet ID
  • masm/accounts/oracle_reader.masm is now a template with placeholders {pair_prefix}, {pair_suffix}, {get_median_proc_hash}, {oracle_id_prefix}, and {oracle_id_suffix}. The Rust binary fills these via oracle_reader_source(oracle_id) before MASM compilation. A defensive runtime gate panics with a clear message if any placeholder leaks through (catches typos before the assembler emits an opaque error)
  • get_oracle_foreign_accounts now mirrors astraly-labs/pragma-miden/examples/consume-price/src/main.rs verbatim: reads pragma::oracle::next_publisher_index, walks the pragma::oracle::publishers map for i in 2..publisher_count (Pragma reserves 0/1 as sentinels), then for each publisher requests its pragma::publisher::entries map gated on the supplied pair_word. The function takes pair_word: Word as an explicit argument, so the BTC/USD assumption is in the caller, not a hidden global
  • the deployment-specific get_median procedure root and the parsed oracle account ID are threaded through the template via Rust constants rather than hardcoded into MASM, so changing oracle deployments requires updating one constant in oracle_data_query.rs and re-running rather than hand-patching .masm
  • the bin reorders work so that the local template fill, MASM component compile, and tx-script compile happen before any live RPC walk into Pragma's storage. Each stage logs its own progress line (Parsed oracle ID:, oracle_reader template substituted (no leftover placeholders), Compiled oracle_reader component locally, Compiled tx_script with linked oracle_reader module), so the verification harness can confirm the full local path is healthy independent of Pragma's live deployment status

Pragma blocker

The live oracle path is currently blocked pending a v0.14-compatible Pragma deployment. Pragma's source repo is on miden-protocol@0.13; the migration is tracked in astraly-labs/pragma-miden#40 (open PR). After Pragma migrates, the oracle account ID and the get_median procedure hash hardcoded in oracle_data_query.rs (constant GET_MEDIAN_PROC_HASH) must be re-verified against the new deployment — the doc and the constant comment both call this out explicitly.

cleanup

  • rust-client/Cargo.toml: dropped serde, serde_json, rand_chacha (none referenced from rust-client/src/)
  • docs/Cargo.toml: same three deps dropped after confirming doctests still pass without them (17 passed / 16 ignored / 0 failed)
  • six tutorial setup-section snippets (network_transactions, creating_notes_in_masm, public_account_interaction, delegated_proving, counter_contract, create_deploy) had stale [dependencies] blocks repeating the dropped deps; removed
  • four tutorial code blocks (mint_consume_create, unauthenticated_note_how_to, delegated_proving, create_deploy) used inline std::path::PathBuf::from(...) despite PathBuf being importable; consolidated to PathBuf::from(...) with PathBuf added to the existing std::{...} group

end-to-end verification

The verification script /tmp/claude/verify-pr-192-rev6.sh runs (in order): lockfile pinning checks, cargo build --release in rust-client, cargo test --doc --release --locked in docs, npx prettier --check, cargo check per changed binary, end-to-end runs of all 10 non-oracle tutorials in fresh mktemp cwds, oracle runs against both Pragma README IDs with the strict TEMP-PASS classifier, and the full sweep set. Every "expect 0" sweep is wrapped to be set -e-safe; every "expect ≥ N" forces a 0 on no-match. Replace transcript-paste with the standalone script.

The oracle TEMP PASS gate requires all of these to hold (any other failure mode is NOT PASS): build clean, Latest block: printed, Parsed oracle ID: printed, oracle_reader template substituted (no leftover placeholders) printed, Compiled oracle_reader component locally printed, Compiled tx_script with linked oracle_reader module printed, no template tokens leaked, no MASM assembly error, and the residual error is AccountNotFoundOnChain against Pragma's documented testnet ID. The three local-compile log lines together prove that the template fill, the component compile, and the tx-script compile all succeeded before the live storage walk — so a Pragma-side failure is the only thing that can cause the residual error, and the local Rust + MASM path is verified end-to-end.

Final commit shape: three signed local commits stacked on top of the original PR head 659b4a0043be43 (initial revision), 12bc332 (oracle local-compile path + Pragma re-verify wording), and 16c9298 (oracle tutorial doc snippet synced to the runnable source order). No --amend, no force-push.

… wording

Reorder oracle_data_query.rs to compile the oracle reader contract and
tx script BEFORE the live Pragma storage walk, so a dead Pragma deployment
no longer blocks local verification of the template substitution and MASM
assembly path. Add three log lines (template substituted, component
compiled, tx_script compiled) so the verification script can classify
oracle import failure as TEMP PASS when the local compile path is healthy.

Add oracle_tutorial.md to docs/src/lib.rs for doctesting alongside the
other tutorial pages.

Strengthen the Pragma callout in oracle_tutorial.md: after Pragma
redeploys, the oracle account ID and GET_MEDIAN_PROC_HASH constant must
be re-verified; do not assume either survives a redeploy unchanged.

Apply prettier-formatting fixes to oracle_tutorial.md (italics syntax
and three fenced-block language hints).
Reorder the doc snippet in oracle_tutorial.md to match the runnable
oracle_data_query.rs: build and locally compile the oracle reader
contract and tx script BEFORE walking Pragma's storage, with the same
three progress println lines (template substituted, component compiled,
tx_script compiled). The doc and bin now read identically through the
local-compile path, so the tutorial accurately reflects how a dead
Pragma deployment surfaces (only the storage walk fails).

Replace stale 'trading_pair requirement' wording with 'pair_word' to
match the actual function-signature argument.
Copy link
Copy Markdown
Collaborator

@partylikeits1983 partylikeits1983 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for doing this.

Leaving a conditional approval, I'd remove all the comment references to that VM issue/PR, its irrelevant context for a future reader / consumer of the tutorials & examples. There are more references to vm #2778 than what I listed below

// Compile the account code into `AccountComponent` with one storage slot.
// Using `client.code_builder()` makes the assembler share the client's
// persisted source manager, which keeps debug spans coherent for any
// libraries that link against this code later (see miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you refactor this comment to not reference the VM PR?


// Compile the script with the counter contract code linked as a dynamic
// module on the same `CodeBuilder`. This shares the client's source
// manager between parsing and assembly, which is what miden-vm#2778
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

// Compile the transaction script with the library.
// Compile the transaction script with the account code linked as a
// module on the same `CodeBuilder` chain (avoids the source-span
// mismatch from miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:


// Link the counter contract code into the same `CodeBuilder` chain that
// compiles the script, so the assembler shares the client's persisted
// source manager (avoids the source-span mismatch from miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit


// Compile the note script with the counter contract library
// Compile the note script with the counter contract code linked as a
// module on the same `CodeBuilder` chain (avoids miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit


// Link the counter contract code into the same `CodeBuilder` chain that
// compiles the script, so the assembler shares the client's persisted
// source manager (avoids the source-span mismatch from miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit


// Compile the note script with the counter contract library
// Compile the note script with the counter contract code linked as a
// module on the same `CodeBuilder` chain (avoids miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit


// Link the counter contract code into the same `CodeBuilder` chain that
// compiles the script, so the assembler shares the client's persisted
// source manager (avoids the source-span mismatch from miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit


// Compile the note script with the counter contract library
// Compile the note script with the counter contract code linked as a
// module on the same `CodeBuilder` chain (avoids miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit


// Compile the script against the counter contract code via a single
// `CodeBuilder` chain so the assembler shares the client's persisted
// source manager (avoids the source-span mismatch from miden-vm#2778).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

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.

2 participants