Repository navigation
fix(platform-wallet): stop reporting a shielded balance as known after a Clear that failed - #6186
Conversation
…r a Clear that failed A shielded Clear resets the commitment tree and then purges the ledger. When the reset landed and the purge failed, the notes stayed, and so did the evidence of a scan completed in this session. Hydration is dropped on a failed Clear, but a completed scan stands in for it, so such an account still read as `Ready` with its pre-Clear amount, against an emptied tree. The balance emitter saw no change and sent nothing. A failed Clear now forgets every subwallet's scan evidence (its scan coverage and balance source), under the same store guard as the reset. The wallet reads as `RestoreIncomplete` until a scan completes or a restore from the host covers it. Notes, watermarks and pending spends are kept. Closes #5988 Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 22 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
|
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
|
✅ Final review complete — no blockers (commit 931302b) |
thepastaclaw
left a comment
There was a problem hiding this comment.
Final validation — Phase 1 + Phase 2
A failed shielded Clear now invalidates scan evidence alongside hydration, so balance reads and callbacks report RestoreIncomplete instead of treating the retained pre-Clear amount as known. Linux wallet tests and Kotlin emulator checks passed; the Swift SDK build and tests are still pending, and no code checks have failed.
🟡 1 suggestion(s) | 💬 1 nitpick(s)
Approved: nothing here blocks the merge. The non-blocking findings are optional: fix them forward, in this PR or a follow-up, without waiting for another review round.
Review provenance
Source: reviewer 1: glm-5.3-flash (agent: phase1-reviewer, role: general); reviewer 2: glm-5.3-flash (agent: phase1-reviewer, role: rust-quality); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.2-sol (agent: phase2-reviewer, role: architecture-layering); reviewer 5: gpt-6.2-sol (agent: phase2-reviewer, role: rust-quality); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)
- Triage:
lowbygpt-6.2-sol(effort low) — The fix is confined to invalidating in-memory balance evidence under the existing write guard when Clear fails, with regression tests for the snapshot and emitted update. - Phase 1 reviewers:
glm-5.3-flash— general (completed, effort high); agentphase1-reviewer,glm-5.3-flash— rust-quality (completed, effort high); agentphase1-reviewer - Phase 1 model:
glm-5.3-flash— zai quota: 5h 94% left, weekly 77% left; passed overgemini-3.8-flash-high(lane failed) - Fresh verifier:
gpt-6.2-sol— final-verifier; agentsol-verifier - Phase 2 reviewers:
gpt-6.2-sol— general (completed, effort medium); agentphase2-reviewer,gpt-6.2-sol— architecture-layering (completed, effort medium); agentphase2-reviewer,gpt-6.2-sol— rust-quality (completed, effort medium); agentphase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.
In `packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs`:
- [SUGGESTION] packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs:1691-1693: The tests miss stale known balances when the tree reset itself fails
Impact: A regression limited to the tree-reset error path could pass the tests while a previously scanned wallet still reports its pre-Clear balance as known after a partial reset.
A regression that forgets scan evidence only on purge errors would pass both new tests. Both rename the pending-spends table to fail the purge, but the new guard also covers errors from the tree reset itself, which can fail after changing tree tables. The existing `clear_failure_still_invalidates_hydration_and_snapshots` test forces a reset error but checks only hydration and snapshot generation. Extend it to establish `ScannedThisSession` with `set_last_synced_note_index` before breaking the reopen, then assert that `local_balance_snapshot` returns `RestoreIncomplete` after Clear fails.
In `packages/rs-platform-wallet/src/wallet/shielded/balance_emitter_tests.rs`:
- [NITPICK] packages/rs-platform-wallet/src/wallet/shielded/balance_emitter_tests.rs:638-644: The same SQLite purge-failure setup is repeated in four tests
Impact: Maintainers changing how a purge failure is injected or checked must update four sites across two files to keep the tests exercising the same failure path.
The two new tests repeat the existing sequence of renaming the pending-spends table, calling Clear, and restoring the table. The databases are isolated, so this is not a cleanup safety issue. A shared test helper could own both schema changes, restore the table before checking the result, and verify that Clear failed in the purge rather than merely returning an error from any step.
Out-of-scope follow-up suggestions (2)
These are valid observations, but they are outside this PR's scope and should be handled in separate issues or author/maintainer-requested PRs rather than blocking this review.
- The next scan can skip commitments after a partial Clear — The scan starts at the retained watermark rounded down to a chunk boundary. If that boundary is past the end of the emptied tree, the scan can skip commitments needed to rebuild it and still mark the balance as known. This behavior predates the patch.
- Follow-up: Use #6184 to fix the scan start and verify both tree reconstruction and balance state through the real scan path.
- A token-pool purge error can leave durable spend guards deleted —
purge_all_subwalletsdeletes pending-spend rows before running the separate token-pool transaction. If the latter fails, the in-memory guards remain, but reopening the store cannot recover the deleted rows. This ordering predates the patch.- Follow-up: Include a failed token-pool purge followed by a store reopen in the durable-guard work tracked by #6011.
|
Bots are done — your move: post |
…e-failure setup A failed Clear forgets scan evidence whichever step failed, but the new tests only failed the purge. The existing test that breaks the tree reset now starts from a scanned account and checks the balance is not reported as known afterwards. The four tests that fail a Clear in the purge share one helper, which also checks that the purge is the step that failed. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post |
thepastaclaw
left a comment
There was a problem hiding this comment.
Re-review — Final validation — Phase 1 + Phase 2
New on this head: The latest commit changes only tests: it gives all four purge-failure tests a shared SQLite fixture and verifies that a failed tree reset leaves a previously scanned wallet’s balance unknown. The Kotlin SDK emulator build and tests and the policy checks passed; PR Hygiene and the review check are still pending, and no code checks are failing.
🔴 0 blocking | 🟡 0 suggestion(s) | 💬 0 nitpick(s)
Review provenance
Source: reviewer 1: glm-5.3-flash (agent: phase1-reviewer, role: general); reviewer 2: glm-5.3-flash (agent: phase1-reviewer, role: rust-quality); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.2-sol (agent: phase2-reviewer, role: architecture-layering); reviewer 5: gpt-6.2-sol (agent: phase2-reviewer, role: rust-quality); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)
- Triage:
lowbygpt-6.2-sol(effort low) — The fix is confined to invalidating session-only balance evidence after a failed Clear, with regression tests and no changes to durable state or funds movement. - Phase 1 reviewers:
glm-5.3-flash— general (completed, effort high); agentphase1-reviewer,glm-5.3-flash— rust-quality (completed, effort high); agentphase1-reviewer - Phase 1 model:
glm-5.3-flash— zai quota: 5h 91% left, weekly 76% left; passed overgemini-3.8-flash-high(lane failed) - Fresh verifier:
gpt-6.2-sol— final-verifier; agentsol-verifier - Phase 2 reviewers:
gpt-6.2-sol— general (completed, effort medium); agentphase2-reviewer,gpt-6.2-sol— architecture-layering (completed, effort medium); agentphase2-reviewer,gpt-6.2-sol— rust-quality (completed, effort medium); agentphase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify the current code and confirm that no unresolved issues remain.
No unresolved findings remain from the prior review on this head.
|
Bots are done — your move: post |
Basic explanation
What this does. A shielded "Clear" first empties the commitment tree and then deletes the wallet's notes and scan state. If the first step works and the second fails, the notes stay. Until now such a wallet could still report its old shielded balance as a known amount. After this change it reports "not known yet" until a scan completes again or the app restores it.
Value. After a Clear that failed half way, the app no longer shows the pre-Clear amount as if it were established, against a tree that was just emptied. The app is also told about the change: before, nothing was announced, so it kept showing the amount.
Risks. Small. Rust only, one crate, no FFI or host change. The only behaviour that changes is on the failure path of Clear, which needs a storage error.
Before and after
Wallet with 900 credits, its ledger established by a scan completed in this session (no host restore behind it). Clear resets the tree, then the purge fails.
local_balance_snapshotReady, account 0: 900 credits,ScannedThisSession,last_scanned_index: Some(1)RestoreIncompleteRestoreIncompleteScannedThisSession, establishedNoHistory,last_scanned_index: None, not established; still 900 credits of notesReady, 900,ScannedThisSessionIssue being fixed or feature implemented
Closes #5988.
Confirmed on
v5.1-dev(3b4117c64d): with the fix switched off, both new tests fail with exactly the state the issue describes:NetworkShieldedCoordinator::clearresets the tree, then callspurge_all_subwallets. The file store's purge deletes its SQL rows before it touches memory, so a failed purge leaves every subwallet as it was. Clear then drops hydration and returns the error.local_balance_snapshotreportsRestoreIncompletefor a wallet that is not hydrated, unless an account's source isScannedThisSession: a completed scan stands in for hydration. So an account scanned in this session stayedReady.What was done?
FileBackedShieldedStore::forget_scan_evidence()(crate-internal): for every subwallet,last_scanned_index = Noneandbalance_source = NoHistory. Notes,last_synced_indexand pending spends are not touched. Both fields are session-only state; nothing durable changes.clear()calls it when the Clear failed, under the same store write guard as the reset. It runs for any failure, not only a failed purge: a tree reset can also fail after it has changed part of its state, which is why hydration is already dropped "regardless of the outcome" there. The scan evidence now goes with the hydration.local_balance_snapshotthat lists who removes the source now names the failed Clear.After that, the ledger is established again the usual ways: a completed scan sets
ScannedThisSession, and a bind that restores from the host's rows setsRestoredand marks the wallet hydrated.Not changed, and worth knowing
A Clear that fails this way keeps the sync watermarks, as the issue asks. The next scan therefore resumes from the old watermark into the emptied tree. That is a separate problem with the tree's contents, not with what the balance reports, and is filed as #6184.
Public interface changes
None. No public Rust item is added or changed, and nothing changes in the FFI, JNI, Swift or Kotlin.
Behaviour a caller can observe, all on a Clear that returned an error:
NetworkShieldedCoordinator::local_balance_snapshotreturnsRestoreIncompletefor a wallet whose accounts were atScannedThisSession(before:Readywith the pre-Clear amounts). Wallets that were hydrated, not scanned, already returnedRestoreIncomplete.on_shielded_balance_changed, so Swift and Kotlin hosts show the balance as unknown instead of keeping the old amount.How Has This Been Tested?
cargo test -p platform-wallet --all-features --locked --lib -- wallet::shielded::balance wallet::shielded::coordinator::tests::clear: 35 passed.shielded_pending_spendstable renamed away):should_forget_scan_evidence_when_a_clear_resets_the_tree_but_fails_to_purge: the wallet readsRestoreIncomplete; the tree is empty; the note is kept; the account isNoHistory,last_scanned_index: None, not established, 900 credits; a scan completing afterwards makes itReadyagain.should_emit_restore_incomplete_after_a_failed_clear_of_a_scanned_wallet: the emitter deliversRestoreIncomplete.local_balance_recovers_after_failed_clear_and_completed_scanandshould_emit_restore_incomplete_after_a_clear_that_failsstill pass.clear_failure_still_invalidates_hydration_and_snapshots, which breaks the tree reset itself, now starts from a scanned account and requiresRestoreIncompleteafterwards.clear_failing_at_the_purge, which also checks that the purge is the step that failed.cargo clippy -p platform-wallet --all-targets --all-features --locked -- -D warnings: clean.cargo fmt --all -- --check: clean.Not yet verified
Breaking Changes
None.
Checklist:
structure.rs, regeneratedgrovedb-structure.json, and checked the structure viewer link posted on this pull requestFor repository code-owners and collaborators only
🤖 Generated with Claude Code
PR Hygiene ·
931302b/self-reviewedrs-platform-wallet(packages/rs-platform-wallet/src/wallet/shielded/balance_emitter_tests.rs,packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs,packages/rs-platform-wallet/src/wallet/shielded/coordinator.rsand 1 more) — HashEngineering or ZocoLini or llbartekll or romchornyiWhen every merge requirement is met, the
PR Hygienecheck passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.