Skip to content

Shielded: repeat spend fails with "Anchor not found in the recorded anchors tree" #3703

Closed

Description

@QuantumExplorer

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 from MerklePath::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) calls tree.witness(Position, 0) (depth 0).
  • packages/rs-platform-wallet/src/wallet/shielded/sync.rs — sync_notes_across() checkpoints at checkpoint_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_id semantics so the depth-0 root is always a recorded anchor.

Surfaced while verifying #3603.

Activity

  1. QuantumExplorer commented on May 20, 2026

    @QuantumExplorer
    MemberAuthor

    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):

    checkpoint_id position consistent?
    6 5 ✅ id == leaf count
    8 13 ❌ id 8, but 14 leaves

    Witness root per unspent note (all at checkpoint_depth=0):

    note position witness root
    1, 3 (original notes) 181225df… = tree_anchor() (correct, current root)
    6 (change from spend #1) c1e787d4… = the exact rejected anchor
    7 (recipient from spend #1) e48a98fc… (a third, different root)

    Spend #2 selected the change note (position 6) → computed anchor c1e787d4… → validate_anchor_exists rejects 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_across computes already_have = min(last_synced_note_index over subwallets) and checkpoint_id = aligned_start + total_notes_scanned (packages/rs-platform-wallet/src/wallet/shielded/sync.rs).
    • With multiple wallets on one network (here: Reg + Wallet2) and lagging watermarks, the min-watermark re-fetch re-appends commitments already in the tree → duplicate/phantom leaves.
    • checkpoint_id then 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 in sync_notes_across already 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.
    • Use the true post-append leaf count as a monotonic, collision-free checkpoint_id.
    • For the spend anchor, witness against an anchor Platform actually recorded — query query_most_recent_shielded_anchor and witness all inputs at the matching checkpoint depth, rather than trusting depth-0.

    This is a careful change (the prior revert 1911d736aa shows this area is sensitive) and needs a live regtest spend×2 cycle to validate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugplatform-wallet

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions