Repository navigation
Description
Symptom
After the witness fix (#3603, commit 6e66e65), the first shielded transfer succeeds and debits the balance. A subsequent shielded transfer fails at broadcast:
shielded transfer failed: Shielded broadcast failed: Protocol error:
Anchor not found in the recorded anchors tree:
c1e787d4437c84a12a3d0f3343e5085e1723d337b4a21dd800688093ed61fa01
Reproduced live in SwiftExampleApp (regtest, iPhone 17 Pro sim): send #1 → "Shielded transfer complete"; send #2 → the error above.
Where it comes from
The Orchard spend proof references an anchor = the commitment-tree root the witness was generated against. Platform validates that anchor against its recorded anchor history (validate_anchor_exists); if the root isn't one Platform recorded at a block boundary, the transition is rejected.
Relevant code:
packages/rs-platform-wallet/src/wallet/shielded/operations.rs—extract_spends_and_anchor()derives the anchor fromMerklePath::root(cmx)at checkpoint-depth 0 (root of the most recent local checkpoint).packages/rs-platform-wallet/src/wallet/shielded/file_store.rs—witness(pos)callstree.witness(Position, 0)(depth 0).packages/rs-platform-wallet/src/wallet/shielded/sync.rs—sync_notes_across()checkpoints atcheckpoint_id = aligned_start + total_notes_scanned(the sync high-water mark).
Hypothesis
The wallet checkpoints the local tree at its sync high-water mark, which does not necessarily correspond to a Platform-recorded anchor (block boundary). Platform records an anchor per finalized block; the wallet's most-recent-checkpoint root reflects whatever it last synced — including commitments from the mutable partial "buffer" chunk that advance the local tree past the last finalized Platform anchor. After send #1 the local tree advances (the spend's own output commitments + ongoing sync), so the depth-0 root used for send #2 is a state Platform never recorded → rejection.
Prior context: commit 1911d736aa ("revert: drop spend-side anchor pre-flight, trust depth-0") shows earlier wrestling with exactly this anchor/checkpoint alignment.
Why it matters
Funds are effectively unspendable after the first shielded send until the local tree happens to re-align with a recorded Platform anchor. This is the next blocker after the witness fix.
Likely fix directions (need investigation)
- Witness/anchor against a root Platform actually recorded — e.g. fetch the latest valid anchor from Platform and witness at the matching checkpoint depth, rather than always depth 0.
- Only checkpoint the local tree at positions that correspond to Platform block-boundary anchors (don't checkpoint into the mutable buffer chunk).
- Reconcile
checkpoint_idsemantics so the depth-0 root is always a recorded anchor.
Surfaced while verifying #3603.
Activity
QuantumExplorer commentedon May 20, 2026
Root-caused — the local commitment tree is corrupted, producing per-position witness roots
Investigated against the live regtest wallet (SwiftExampleApp sim). Definitive finding:
witness(position, checkpoint_depth=0)returns a different root for different note positions — which is impossible for a consistent shardtree (the root is a property of the tree state, not the note). A spend requires every selected input to witness to one common anchor that Platform recorded; the corruption breaks that.Evidence
On-disk checkpoints (
commitment_tree_checkpoints):Witness root per unspent note (all at
checkpoint_depth=0):181225df…=tree_anchor()(correct, current root)c1e787d4…= the exact rejected anchore48a98fc…(a third, different root)Spend #2 selected the change note (position 6) → computed anchor
c1e787d4…→validate_anchor_existsrejects it (Platform never recorded that root).Why
The shared commitment tree's append + checkpoint is driven by per-subwallet watermarks and the SDK scan count, not by the tree's own state:
sync_notes_acrosscomputesalready_have = min(last_synced_note_index over subwallets)andcheckpoint_id = aligned_start + total_notes_scanned(packages/rs-platform-wallet/src/wallet/shielded/sync.rs).Reg+Wallet2) and lagging watermarks, the min-watermark re-fetch re-appends commitments already in the tree → duplicate/phantom leaves.checkpoint_idthen drifts from the true leaf count and collides across syncs; shardtree dedups colliding ids (checkpoint(id)returns false), freezing some marked positions' witnesses at stale checkpoint roots while the tree advances. That's the per-position root divergence above, and exactly the hazard the in-code comment insync_notes_acrossalready warned about.Note: this is a pre-existing corner that only became reachable after the witness-marking fix (#3603 / 6e66e65). Before that, witnesses failed outright ("Merkle witness unavailable") so execution never reached the anchor check. "First spend works, second fails" is because the first spend witnesses the original notes (which resolve to the live root); its outputs (positions 6/7) then get frozen witnesses, breaking the next spend. Anchor pruning is not involved — retention is 1000 blocks.
Fix direction
Drive the shared tree by its own state, decoupled from per-wallet watermarks:
ClientPersistentCommitmentTree::max_leaf_position()is available — use it as the append gate (already_have= current tree leaf count), so each commitment is appended exactly once, in order, no double-append.checkpoint_id.query_most_recent_shielded_anchorand witness all inputs at the matching checkpoint depth, rather than trusting depth-0.This is a careful change (the prior revert
1911d736aashows this area is sensitive) and needs a live regtest spend×2 cycle to validate.fix(platform-wallet): gate shielded tree appends by tree size, not wa…
fix(platform-wallet): address shielded review feedback (Rust)