Skip to content

platform-wallet: after a shielded Clear that resets the tree but fails to purge, the next scan resumes from the old watermark into the emptied tree #6184

Backlog

Description

@QuantumExplorer

Expected Behavior

After a shielded Clear that emptied the commitment tree but could not purge the ledger, the next note scan rebuilds the tree from position 0, or refuses to run until a Clear has gone through.

Current Behavior

NetworkShieldedCoordinator::clear (packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs) resets the commitment tree and then calls purge_all_subwallets. When the reset lands and the purge fails, each subwallet keeps its sync watermark (last_synced_index), while tree_size() is 0. The host is told the Clear failed and keeps its own rows, so a later bind restores the same watermark from them as well (restore_balance_metadata takes the maximum).

sync_notes_across (packages/rs-platform-wallet/src/wallet/shielded/sync.rs) starts from the smallest watermark, rounded down to a chunk, and appends every fetched commitment whose global position is not below the tree size it read at the start:

let global_pos = batch.start_index + i as u64;
if global_pos < tree_size {
    continue;
}
store.append_commitment(&cmx_bytes, true)

With a tree of 0 leaves and a watermark of N, the commitment at global position N is appended as leaf 0. The tree then holds leaves at the wrong positions, and a witness built for a kept note does not resolve to a root Platform recorded.

Found by reading the code on v5.1-dev (3b4117c64d) while fixing #5988. Not reproduced in a test: it needs a storage failure between the two steps of one Clear, and a pool with notes behind the watermark.

Possible Solution

  • In the scan, never start past the tree: clamp the start to tree_size (rounded down to a chunk), so a tree behind the watermark is filled from where it ends.
  • Or have the failed Clear rewind the watermarks it keeps, and have the restore from host rows not raise them again until a Clear has succeeded.
  • feat(platform-wallet)!: detect inconsistent shielded commitment trees and add explicit rebuild #5565 (v5.2-dev) detects a tree with more leaves than the chain reports and adds an explicit rebuild; this is the opposite shape (fewer leaves than the watermark claims) and is not detected by it.

Steps to Reproduce (for bugs)

  1. Bind a wallet and complete a shielded scan, so its watermark is N > 0.
  2. Run Clear so that reset_commitment_tree succeeds and purge_all_subwallets fails (for example the shielded_pending_spends delete fails).
  3. Run a shielded sync.
  4. The first commitment it appends is the one at global position N (rounded down to a chunk), at tree position 0.

Context

Left out of the fix for #5988, which only stops the balance from being reported as known after such a Clear. Clear returns the error to the host, and a Clear that then succeeds empties the tree and the watermarks together, so the state lasts until the user clears again.

🤖 Generated with Claude Code

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions