You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Shielded send started while a sync pass is running: it waited for the rest of the pass before it could reserve notes and start proving. Worst case (send at the start of the pass): 0.30 s median (0.23–1.6 s) behind an incremental pass, 1.2 s behind a full testnet catch-up, and 226 s behind one catch-up that stalled. Now ≤ 15 ms, and ≤ 0.1 ms during an incremental pass.
How often: passes are short today, so a send overlaps one in roughly 1% of sends (the 60 s loop's ~0.3 s pass), more for a second send right after the first (forced readback passes at 0, +4, +12 and +24 s) and right after foregrounding. The tail is unbounded: a slow or stalled pass held every send for its whole length.
Measured on an Apple M4 Max desktop, release build, against testnet (4,879-note pool), host load average 44–320 from other builds. 5 interleaved runs per side (base, PR, base, PR, ...), each one cold catch-up plus 4 incremental passes through NetworkShieldedCoordinator::sync(true). The "send" is a probe that takes the store write lock (as reserve_notes does) every 20 ms; "before" numbers are its wait when it starts at the beginning of the pass, i.e. the worst case. Desktop network only; phone numbers not measured.
Issue being fixed or feature implemented
A shielded sync pass held the shielded store's write lock for the entire note download:
every stream.next().await (network round trips and server proof generation);
trial decryption;
tree appends.
A shielded send needs that same lock up to five times: reserve notes, the anchor/witness probe, pending activity, release arming, and post-result. tokio's RwLock is fair, so every send queued behind the whole in-flight pass.
Passes run often:
the Rust loop runs every 60 s;
the iOS app adds a 15 s watchdog / 90 s staleness pass;
the app also forces passes on foreground (if 30 s have passed since the last scan), every 5 s on the Receive screen while it waits for a payment, and after each send (plus reconciliation passes at +4, +12 and +24 s).
So a send could wait anywhere from 0 s to a full sync pass. That wait appeared to the user as "generating proof".
No lock across network I/O. Each streamed batch is parsed, trial-decrypted and OVK-recovered with no lock held. New commitments are appended in 256-leaf write-lock holds.
Same per-pass atomicity for readers. After the stream ends, the checkpoint, saved notes, outgoing notes, scan-detected spends and watermarks are still committed together in one write-lock hold.
Spends are unaffected by mid-pass appends. The spend path witnesses only at checkpoints, and shardtree (0.6.2, checked) computes a checkpoint-depth witness as of that checkpoint's position. Leaves appended between holds change no witness or anchor until the pass checkpoints them together with their notes. Sends also copy their witnesses out under the read lock.
Concurrent lifecycle changes are detected (StoreEpoch).
Unregister, account-dropping re-bind and host snapshot restore bump their wallet's generation. Clear bumps the tree generation. All bumps happen under the store write guard.
The pass records the generations before it snapshots the registry. Every write it makes first re-checks them, plus the tree's exact leaf count.
If anything moved, the pass is Superseded. It has written only uncheckpointed leaves, and sync() retries up to twice from a fresh registry snapshot.
Changes to wallets the pass isn't scanning don't supersede it.
One pass at a time. A per-coordinator sync_pass mutex serializes sync() passes, keeping the pass the tree's only appender. Clear holds it too.
Stops stay prompt. The wait for sync_pass, the pre-scan read and every append slice observe the cancellation token. A stopped pass checkpoints its appended leaves only if the store is free and its snapshot is current, publishes nothing else, and reports Cancelled.
Spends confirmed mid-pass stay spent. The store, and now the Swift and Kotlin persisters, keep a note's spent flag across a re-save. A confirmed spend's round saves the store's copy of the note marked spent and is queued before its guard drops. After delivering, the pass re-checks every note it sent unspent and queues a correction round for any the store now has spent. A fan_out mutex keeps a pass's results from reaching a persister for state purged after its commit.
Checkpoint fix. The commit now checkpoints whenever the tree is non-empty, not only when the pass appended. Notes in leaves left by an interrupted pass (superseded, stream error, crash) were otherwise unwitnessable until the chain grew. A checkpoint at an unchanged size is a no-op; this is now documented on ShieldedStore::checkpoint_tree and followed by InMemoryShieldedStore.
Testability and side effects.
The sync consumer takes its batch stream as a parameter (sync_notes_from_stream), so tests can pause a pass mid-download.
The tree-progress callback no longer runs under the store lock.
The FFI local-balance snapshot (100 ms budget) now succeeds during a sync instead of timing out.
How Has This Been Tested?
New sync/lock_contention_tests.rs drives a pass with a hand-fed stream:
Scenario
Before
After
Send write-lock wait while the pass is stalled on the network (regression test)
1.514 s (whole stall; test fails on the old code)
0.5–3.9 µs
Send read-lock wait (anchor/witness probe), same situation
whole stall
0.25–1.3 µs
Lock waits while the pass appends an 8,192-leaf batch
entire pass
median 4–10 ms, max 38–155 ms (debug build, shared host)
Plus:
spend witnesses are stable mid-pass;
supersession on wallet removal, on Clear right before commit, and on a foreign tree write;
no supersession for another wallet's change;
the retry checkpoints leaves an abandoned pass left behind (this test fails with the old checkpoint condition);
coordinator-level tests over the mock SDK that inject lifecycle changes mid-pass: retry without bringing back a removed wallet, ShieldedSyncFailed after repeated supersession, and a mid-pass bind of another wallet does not interrupt the scan;
which lifecycle operations bump the generations.
Testnet measurement (see "Observable value"), store write-lock wait seen by the probe, per pass:
Pass
Pass length (median, range)
Before
After
Cold catch-up, 4,879 notes (5 per side)
1.2 s (1.2–226 s base, 1.2–300 s PR)
1.22 s (1.17–225.9 s)
4.0 ms (2.8–15.2 ms)
Incremental (20 per side)
0.31 s base / 0.38 s PR (0.24–1.8 s)
0.30 s (0.23–1.62 s)
0.001 ms (≤ 0.07 ms)
The two slow catch-ups (226 s, 300 s) coincided with a load spike on the host; pass length is otherwise unchanged by this PR.
Commands:
cargo test -p platform-wallet -p platform-wallet-ffi --all-features: 2587 pass.
cargo clippy ... --all-targets -D warnings (both crates) and cargo fmt --all --check are clean.
Two review rounds and a simplification pass were applied before opening.
Breaking Changes
None. A pass superseded three times in a row reports ShieldedSyncFailed for the wallets it was scanning; the next scheduled pass runs normally.
Known trade-offs:
An abandoned pass discards its download; the retry refetches from the old watermark, so the host's "Downloaded" progress can jump back.
The final commit still replays scanned nullifiers under the lock. That takes microseconds normally, and up to about a second at the end of a cold full catch-up, as before.
Checklist:
I have performed a self-review of my own code
I have commented my code, particularly in hard-to-understand areas
I have added or updated relevant unit/integration/functional/e2e tests
I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
I have made corresponding changes to the documentation if needed
If I added or changed GroveDB structure, I described it in the area's structure.rs, regenerated grovedb-structure.json, and checked the structure viewer link posted on this pull request
Shielded sync no longer holds the store’s write lock while waiting for network data, helping balance reads and other store operations stay responsive. Brief delays may still occur during tree updates, final commits, restore, or Clear.
Reliability
Sync detects wallet or tree changes during a scan and retries when possible, avoiding commits based on outdated state.
Results for removed wallets are excluded, while unrelated wallet changes do not unnecessarily invalidate a scan.
Checkpoints are recorded only when their identifiers advance, and interrupted sync progress can be checkpointed on a later pass.
Rescanned notes retain their recorded spent status, and shielded stores can report whether a note is spent.
rs-platform-wallet-ffi (packages/rs-platform-wallet-ffi/src/persistence.rs, packages/rs-platform-wallet-ffi/src/shielded_persistence.rs, packages/rs-platform-wallet-ffi/src/shielded_sync.rs) — HashEngineering or ZocoLini or llbartekll or romchornyi
rs-platform-wallet (packages/rs-platform-wallet/src/changeset/shielded_changeset.rs, packages/rs-platform-wallet/src/manager/mod.rs, packages/rs-platform-wallet/src/wallet/platform_wallet.rs and 15 more) — HashEngineering or ZocoLini or llbartekll or romchornyi
swift-sdk (packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentShieldedNote.swift, packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedLocalBalance.swift, packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift and 1 more) — llbartekll or romchornyi
When every merge requirement is met, the PR Hygiene check passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.
Waiting for bot review — coderabbitai ✓ · thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.
Auto reviews are disabled on base/target branches other than the default branch.
Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.
You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.
Use the checkbox below for a quick retry:
Trigger review
Note
Reviews paused
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.
No actionable comments were generated in the recent review.
Walkthrough
Shielded sync now releases the store write lock during network streaming, appends commitments in slices, and checks store epochs before committing. The coordinator retries superseded passes. Store changes preserve spent-note status and prevent non-increasing checkpoint IDs.
Changes: Shielded sync consistency and lock contention
Layer / File(s)
Summary
Lifecycle epochs and retry orchestration packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs, packages/rs-platform-wallet/src/wallet/shielded/sync.rs, packages/rs-platform-wallet/src/wallet/platform_wallet.rs, packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs, packages/rs-platform-wallet/src/changeset/shielded_changeset.rs
The coordinator tracks relevant wallet and tree changes with epochs. It snapshots registered subwallets and retries superseded sync passes up to two times. Changesets exclude subwallet state that was purged. Tests cover lifecycle changes during a pass.
Streamed appends and final commit packages/rs-platform-wallet/src/wallet/shielded/sync.rs, packages/rs-platform-wallet/src/wallet/shielded/store.rs, packages/rs-platform-wallet/src/wallet/shielded/file_store.rs, packages/rs-platform-wallet-ffi/src/shielded_sync.rs, packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedLocalBalance.swift
Sync parses batches before appending commitments in slices of up to 256. It checks the epoch and expected tree size before writes, then validates the snapshot before committing sync results. Store lookups report spent status, and checkpoint IDs must increase strictly. Documentation describes the lock periods during sync and lifecycle operations.
Concurrency and lifecycle validation packages/rs-platform-wallet/src/wallet/shielded/sync/lock_contention_tests.rs
Tests cover store-lock access during stalled streams, incremental appends, supersession and retries, witness roots, unrelated wallet changes, and preservation of spent status during rescans.
sequenceDiagram
participant Coordinator
participant SyncPass
participant SDK
participant ShieldedStore
Coordinator->>SyncPass: Start with epoch watch and subwallet snapshot
SyncPass->>SDK: Open note stream
SDK-->>SyncPass: Stream note batches
SyncPass->>ShieldedStore: Append commitments in slices
SyncPass->>ShieldedStore: Validate snapshot and commit sync results
Coordinator->>SyncPass: Lifecycle change bumps relevant epoch
SyncPass-->>Coordinator: Return superseded outcome
Coordinator->>SyncPass: Retry with refreshed snapshot
Loading
Merge Risk: Low · previous review fc90f
Mergeable with awareness that a wallet change during sync can delay other wallets’ results until a retry or later pass.
Security architecture risk: Low · up to 66d1c
The synchronization change retains existing validation controls and limits conflicting wallet updates. No new security exposure was established, but persistence and failure recovery were not fully verified.
Retained concerns
No architecture-level concerns identified.
Security review details
Security Blast Radius
inferred — A commitment-tree consistency failure could affect spending availability across wallets registered on the same network coordinator. Wallet note ownership remains separately scoped; the inspected change does not establish new remote lifecycle authority, cross-environment access, or access to spending keys.
Trust Boundaries and Controls
observed — Production batches use the existing SDK proof-processing path: shielded-note queries reject unproved requests, fetched responses pass through proof parsing and verification, and malformed fixed-width nullifiers or new commitments fail the consumer rather than being silently accepted.
observed — Spend construction holds one read guard while selecting mutually consistent checkpoint-depth witnesses and requires their root to belong to Platform's recorded anchor set. The added real-tree test asserts that uncheckpointed mid-pass appends leave the previous checkpoint root unchanged; this test was inspected but not run.
inferred — Generation validation ends at the protected store commit, while subsequent changeset publication looks up the current persister by WalletId. A concurrent local rebind can therefore create a stale-publication window. The same post-commit ordering and routing existed at the PR base; no worsened exposure was demonstrated, and the host backend's treatment of old-key account data remains unverified.
Resilience and Maintainability Implications
inferred — Supersession, stream errors, or cancellation before the final commit can leave appended leaves without new wallet notes or watermark advances. A later successful pass skips already-present positions and checkpoints the nonempty tree even when it appends nothing. The added retry test checks leaf deduplication, restored witness availability, and repeated-checkpoint idempotency.
Hardening Proposals
proposed — Consider binding queued changesets and completion results to the wallet registration generation, or coordinating publication with lifecycle cleanup. This would strengthen the pre-existing post-commit ownership boundary; it is a hardening proposal, not a finding introduced by this PR.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title clearly and concisely describes the primary change: avoiding the shielded store lock during sync network I/O.
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Finishing Touches
Fix failing CI checks
Commit to this branch
Create a new PR
🧪 Generate unit tests (beta)
Commit to this branch
Create a new PR
Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Final validation — Phase 1 + Phase 2
The unlocked scan can overwrite a spend confirmed mid-pass when an existing note is re-scanned, and the append-contention test can pass without an overlapping lock probe. Both are in-scope, non-consensus suggestions under the supplied severity policy. Verification was static; the supplied exact-head CI snapshot reports successful Rust wallet, Swift, and Kotlin checks, with Rust workspace tests skipped and PR Hygiene pending.
Triage: critical by gpt-6.1-sol (effort low) — The intricate changes in sync_notes_from_source in packages/rs-platform-wallet/src/wallet/shielded/sync.rs alter commitment-tree checkpointing and the atomic visibility of spendable notes and detected spends, directly affecting coin selection and cryptographic witness availability.
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.
In `packages/rs-platform-wallet/src/wallet/shielded/sync.rs`:
- [SUGGESTION] packages/rs-platform-wallet/src/wallet/shielded/sync.rs:757-759: Preserve spend confirmations made during the unlocked scan
The final validation checks lifecycle generations and tree size, but ordinary spend finalization changes neither. Below this guard, rediscovered receipts are constructed with `is_spent: false` and passed to `save_note`; both store implementations delegate to `SubwalletState::save_note`, which replaces an existing note wholesale.
This can undo a confirmation that lands during the newly unlocked download. For example, `restore_locked` accepts a notes-only snapshot with watermark 0, so an existing unspent note can be re-scanned. If the cached tree already has a usable checkpoint covering that note, a concurrent send can spend it and call `finalize_pending` → `mark_notes_spent` while the pass is downloading. When the downloaded scan data predates that spend, its nullifiers cannot repair the subsequent receipt overwrite. The epoch check succeeds, the confirmed spent flag becomes false, and `mark_spent` has already removed the reservation, making the consumed note selectable again and inflating the balance. Previously, the pass-wide write lock ordered a confirmation occurring during download after receipt saving.
Merge rediscovered receipts with the current stored spent state under the final write guard, or skip already-known receipts, and ensure the emitted changeset also preserves that state. Add a hand-fed-stream regression that confirms an existing note at or above the snapshot watermark during the stall, then verifies that committing older scan data preserves its spent status, excludes it from selection, and does not emit an unspent replacement.
In `packages/rs-platform-wallet/src/wallet/shielded/sync/lock_contention_tests.rs`:
- [SUGGESTION] packages/rs-platform-wallet/src/wallet/shielded/sync/lock_contention_tests.rs:241-245: Require lock probes to overlap the append batch
The spawned prober has no readiness handshake, and the test never checks that a lock acquisition occurred during ingestion. If the prober first runs after `done` becomes true, it returns an empty vector; `unwrap_or_default()` then makes `max_wait` zero and the regression assertion passes. Samples taken only before or after ingestion can also pass without exercising contention, so this test does not reliably distinguish slice-sized lock holds from a whole-batch hold.
Coordinate the prober with ingestion and assert that it acquires the store at an intermediate tree size strictly between 0 and 8192. A test-store hook or explicit channels can establish that interleaving deterministically. Keep the timing measurements as supplementary evidence rather than the sole regression invariant.
Waiting for bot review — coderabbitai not yet · thepastaclaw ✓. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.
The reason will be displayed to describe this comment to others. Learn more.
Re-review — Final validation — Phase 1 + Phase 2
Verified the complete diff at 1828d4b and confirmed that both prior findings are fixed. Two non-blocking suggestions remain: direct mutations through the public store handle bypass lifecycle invalidation, and several stalled-stream tests can deadlock on a lock-scope regression. Validation was static; current CI shows Rust wallet, Swift SDK, and Kotlin SDK checks passing, with PR Hygiene pending.
Triage: critical by gpt-6.1-sol (effort low) — The intricate concurrency rewrite in sync_notes_from_source changes shielded commitment-tree checkpointing and spent-note reconciliation, directly affecting spend witnesses and which notes remain eligible for spending.
Model comparison: every Phase-2 reviewer also ran on gpt-6-astra; the verifier weighed both sets without knowing which model wrote which
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.
In `packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs`:
- [SUGGESTION] packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs:268-272: Enforce invalidation at the exposed store mutation boundary
The coordinator's lifecycle methods bump this epoch correctly, but the public `store()` accessor still exposes `Arc<RwLock<FileBackedShieldedStore>>`. A native caller can acquire that handle during a stalled download and invoke `ShieldedStore::purge_wallet` or `purge_subwallet` without invalidating the pass. Those methods leave the shared tree unchanged, so the leaf-count check also succeeds. The final commit can then recreate the purged watermark and save receipts accumulated before the purge. Previously, the pass-wide write lock ordered a purge requested during download after receipt saving.
The accessor is documented as supporting tests and migration scaffolding, and existing in-tree lifecycle paths are protected, so this is a non-blocking public-boundary concern rather than a defect in those paths. Make conflicting mutations participate in invalidation at the store boundary, or prevent them through the exposed handle and provide coordinator-controlled operations. Add coverage through that public boundary without manually bumping the epoch in the test.
In `packages/rs-platform-wallet/src/wallet/shielded/sync/lock_contention_tests.rs`:
- [SUGGESTION] packages/rs-platform-wallet/src/wallet/shielded/sync/lock_contention_tests.rs:363: Bound lock acquisition in the stalled-stream tests
After receiving the first progress notification, this test waits for the store writer before sending another batch or dropping the stream sender. If sync regresses to holding its write guard across `stream.next().await`, the pass waits for the sender while the test waits for the pass's guard. Neither can advance, so the test hangs instead of reporting the regression. The same circular wait exists in the mid-download witness, reset, foreign-writer, unrelated-wallet, retry, and spend-confirmation tests.
Bound these mid-pass lock acquisitions with `tokio::time::timeout`, or bound the entire interaction. Unlike the independently scheduled stream release in `should_not_hold_store_lock_while_sync_waits_for_network`, these tests currently have no escape from that circular wait.
Waiting for bot review — coderabbitai not yet · thepastaclaw ✓. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.
The reason will be displayed to describe this comment to others. Learn more.
Re-review — Final validation — Phase 1 + Phase 2
At exact head 3b47cd0, the complete diff removes store guards from network waits and decryption while retaining guarded snapshot validation and a serialized final commit; no blocking defects were verified. Two non-blocking suggestions remain, three prior findings are fixed, and the public-store invalidation concern remains intentionally deferred. This was static verification only: the supplied head-matched CI snapshot reports successful wallet, Swift, and Kotlin checks, with Rust workspace tests skipped and PR Hygiene pending.
Triage: critical by gpt-6.1-sol (effort low) — The intricate refactor of shielded/sync.rs::sync_notes_across changes spend-detection and note-state commit semantics while allowing concurrent sends and lifecycle mutations, directly affecting funds availability and spend correctness.
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.
In `packages/rs-platform-wallet/src/wallet/shielded/sync/lock_contention_tests.rs`:
- [SUGGESTION] packages/rs-platform-wallet/src/wallet/shielded/sync/lock_contention_tests.rs:263: Bound the prober readiness handshake
The pass is spawned before the prober, and the first batch is sent only after this unbounded readiness wait. If sync regresses to holding the store writer across its initial stream.next().await and acquires the writer first, the prober blocks in blocking_write(), this recv() waits for the prober, and sync waits for the batch that the test has not sent. That scheduling order hangs the regression test instead of reporting a failure. Use recv_timeout with MID_PASS_LOCK_BOUND; unwinding then drops the sender and StopOnDrop so the stalled interaction can terminate.
In `packages/rs-platform-wallet/src/wallet/shielded/sync.rs`:
- [SUGGESTION] packages/rs-platform-wallet/src/wallet/shielded/sync.rs:819-827: Avoid cloning complete note history to preserve spent flags
Both store implementations delegate get_all_notes to SubwalletState::all_notes, which clones the complete notes vector, including each note's owned note_data buffer. This condition also matches an ordinary new receipt, so saving even one new note deep-copies the subwallet's entire history and builds a spent-nullifier set under the final exclusive store guard. The spent-state reconciliation is necessary, but this implementation adds history-proportional allocations to the lock hold even when only a small number of receipts need reconciliation. Expose a per-nullifier spent-state lookup backed by the existing nullifier_index, or a store-level receipt merge that returns the merged note for the changeset, while keeping reconciliation under the final guard.
Out-of-scope follow-up suggestions (1)
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.
Store-owned invalidation for public native mutations — The public coordinator.store() handle still permits native callers to purge notes and watermarks without changing the coordinator-private generations. Because those purges preserve tree size, an unlocked scan can subsequently recreate the purged state. Current in-tree production purges use epoch-bumping coordinator lifecycle methods, and the cross-crate FFI raw-lock accesses are tests; the author explicitly deferred this concrete public-API ownership gap to a separate follow-up.
Follow-up: Track store-owned invalidation or a restricted production mutation interface separately, with a stalled-stream purge regression through the public boundary that does not manually bump StoreEpoch.
Waiting for bot review — coderabbitai not yet · thepastaclaw ✓. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.
The reason will be displayed to describe this comment to others. Learn more.
Re-review — Final validation — Phase 1 + Phase 2
Static verification of the complete diff at 9b7b561 found no remaining in-scope findings requiring changes. Five prior findings are fixed; the public native store-invalidation boundary remains a concrete, non-blocking follow-up rather than a completed fix. No builds or tests were run: the supplied CI snapshot from 2026-10-06T20:29:51Z shows wallet tests queued, Swift and Kotlin checks running, and PR Hygiene pending.
Triage: critical by gpt-6.1-sol (effort low) — The intricate concurrency refactor in shielded/sync.rs changes spend-state preservation and commitment-tree checkpoint handling used for spend witnesses, directly affecting funds movement correctness.
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.
Out-of-scope follow-up suggestions (1)
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.
Make conflicting mutations invalidate sync through the public native store boundary — The public manager API exposes the coordinator, whose store() accessor returns a writable FileBackedShieldedStore. Its purge methods remove subwallet history without changing the coordinator-owned epoch or shared tree size, so a native caller purging during a download can let the old pass restore an advanced watermark while retaining only its old scan range, causing subsequent scans to skip deleted history. The in-tree lifecycle purge paths participate in invalidation, and the external FFI raw-lock uses are tests. Centralizing invalidation remains an explicitly deferred ownership change, recorded separately rather than requested again in this PR.
Follow-up: In a separate maintainer-requested change, move conflicting-mutation generations to the store boundary or restrict raw mutation access. Cover a purge through the public handle during a scan starting from a positive watermark, without manually bumping StoreEpoch, and verify complete historical recovery.
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.
The reason will be displayed to describe this comment to others. Learn more.
Re-review — Final validation — Phase 1 + Phase 2
New on this head: This head prevents an already-committed shielded sync pass from delivering the old key’s receipts and watermark to a replacement persister when a re-key purge fails. It preserves delivery for dropped accounts whose state was retained. Rust workspace tests, Kotlin and Swift SDK builds and tests, and the Swift frozen-schema check passed; review checks are still pending.
🟡 1 suggestion(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.
Triage: critical by gpt-6.2-sol (effort low) — The intricate changes to mark_notes_spent in operations.rs and sync reconciliation in sync.rs change how confirmed spends are recorded during concurrent sync, directly affecting whether spent notes can re-enter the spendable set.
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:6757-6758: The failed re-key regression can pass without exercising a failed purge
Impact: If a store change makes the table rename stop triggering a purge error, CI can still pass this test without covering the new error branch.
The test can silently stop covering the behavior added in this commit. Registration returns `Ok(())` whether the purge succeeds or fails, and both paths now suppress delivery to the replacement persister. The table rename does trigger the error today: the store deletes from `shielded_pending_spends` before removing the in-memory subwallet. Assert that the seeded watermark is still present before testing delivery, as the adjacent dropped-account test does, so a successful purge cannot satisfy this regression.
Out-of-scope follow-up suggestions (1)
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.
A failed direct re-key can leave the replacement account scanning from the old watermark — Direct register_wallet calls already installed the replacement key before cleanup and returned success on purge failure at the base revision and the prior reviewed head. A fresh scan can therefore still read the previous key’s cursor for the same SubwalletId and skip receipts belonging to the replacement key. The ordinary wallet bind refuses in-place re-keying.
Follow-up: Use a separate Rust wallet PR to reject in-place re-keying at the public coordinator boundary or require successful cleanup before replacing the registration. Cover a fresh scan after the failure, separately from delivery of a previously committed pass.
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.
The reason will be displayed to describe this comment to others. Learn more.
Re-review — Final validation — Phase 1 + Phase 2
New on this head: This head changes only the failed re-key regression test: it now checks that account 1 still has its seeded watermark of 4096 before verifying that neither the old receipt nor the watermark reaches the replacement persister. The Kotlin SDK build and tests and the Swift frozen-schema check passed; Rust workspace tests are still pending, and no code checks have failed.
Triage: critical by gpt-6.2-sol (effort low) — The intricate sync and persistence refactor directly changes shielded spend finalization in operations.rs::mark_notes_spent and how concurrent saves determine whether a spent note remains ineligible for future coin selection.
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.
A sync pass that no longer holds the store lock across its download can
deliver a note unspent after a concurrent send's round already marked it
spent. Hosts that assign the saved flag then revert the spend durably:
the Rust store has the note spent, the host row does not, and after a
restart the note is offered again.
The Rust store never un-spends a note, so make the hosts match: a
`notes_saved` row keeps `is_spent` once set, whatever order the rounds
arrive in.
- Swift `persistShieldedNotes` and Kotlin `onPersistShieldedNote` OR the
stored flag into the incoming one.
- Swift read the existing row with `try?`, so a failed read took the
insert branch and wrote the incoming copy over a row it couldn't see.
The read now goes through the handler's `ModelFetching` seam and a
failure fails the round.
- The rule is stated where a custom host reads it: `ShieldedNoteFFI`,
`on_persist_shielded_notes_fn` and Kotlin's
`NativePersistenceBridge.onPersistShieldedNote`.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… network I/O
A shielded sync pass took the store write lock before opening the SDK
note stream and held it until the pass committed, across every
`stream.next().await` (network round trips, server proof generation),
trial decryption and tree appends. A send needs that lock up to five
times (reserve notes, anchor/witness probe, pending activity, release
arming, post-result), and tokio's RwLock is fair, so every send waited
for the whole in-flight pass: up to minutes during catch-up.
The pass now never holds the lock across an await on the network:
- Each batch is parsed, trial-decrypted and OVK-recovered with no lock
held. Its new commitments are appended in 256-leaf write-lock holds.
- The checkpoint, notes, outgoing notes, scan-detected spends and
watermarks are still committed together in one write-lock hold after
the stream ends, so readers see them atomically per pass, as before.
- Spends can't observe the uncheckpointed leaves appended between
holds: they witness only at checkpoints, and shardtree computes a
checkpoint-depth witness as of that checkpoint's position.
Because lifecycle operations can now land mid-pass, every write the
pass makes first re-validates its snapshot:
- `StoreEpoch` keeps one generation per wallet and subwallet and one
for the tree. Unregister, account-dropping re-binds and snapshot
restores bump their wallet's generation; Clear bumps the tree's and
drops the per-wallet counters. Each bump happens under the store
write guard.
- The pass watches the generations before it snapshots the registry.
- The tree's leaf count must be exactly the snapshot size plus the
pass's own appends.
If anything moved, the pass is abandoned (`Superseded`) having written
only uncheckpointed leaves, the state an interrupted pass already
leaves. `sync()` then retries up to twice from a fresh registry
snapshot. Changes to a wallet the pass isn't scanning don't supersede
it, so binding a second wallet mid-pass keeps the scan.
A stop (`CancellationToken`) still ends the pass promptly: the pre-scan
read, every append slice and the wait for `sync_pass` observe the
token, and a cancelled pass checkpoints its appended leaves only if the
store is free (`try_write`) and its snapshot is current, publishes
nothing else, and reports `Cancelled`, which takes precedence over
`Superseded`.
Passes and lifecycle operations are fenced against each other:
- Passes on a coordinator are serialized by a `sync_pass` mutex, which
keeps the pass the tree's only appender. Clear holds it too. Lock
order is lifecycle -> sync_pass -> fan_out -> store. A non-forced pass
that waited behind another re-checks the caught-up cooldown.
- A pass queues its changeset after it releases the commit guard. A
`fan_out` mutex makes that delivery atomic with purging lifecycle
operations, and the fan-out drops every subwallet purged since the
pass watched the epoch, so a replacement persister never receives
stale notes and watermarks.
Spends confirmed mid-pass stay spent:
- The store keeps a note's spent flag across a re-save, so a pass that
re-scans a note from data older than the spend doesn't make it
selectable again. The flag is looked up by nullifier
(`ShieldedStore::is_note_spent`, `get_note`), not by copying the
subwallet's note history under the commit guard.
- A confirmed spend's round saves the store's copy of the note, marked
spent, alongside its nullifier, and is queued before the guard that
marked it drops, so a pass's later round can't roll the host row's
metadata back.
- After delivering, the fan-out re-checks every note it sent unspent
and queues a `nullifiers_spent` correction round for any the store
now has spent, skipping notes the same round already marks spent.
Also:
- The commit checkpoints whenever the tree is non-empty, not only when
the pass appended. Otherwise notes saved in leaves an interrupted
pass left behind had no checkpoint and were unspendable until the
chain grew. A checkpoint at an unchanged size is a no-op; this rule
is now documented on `ShieldedStore::checkpoint_tree` and followed by
the in-memory store.
- The tree-progress callback no longer runs under the store lock, and
the FFI local-balance snapshot no longer times out during a pass.
- The sync consumer takes its batch stream as a parameter
(`sync_notes_from_stream`), so tests can pause a pass mid-download.
In the regression test, a write-lock wait behind a pass stalled on the
network dropped from 1.51 s (the full stall) to about 2 µs. While the
pass appends an 8192-leaf batch, concurrent waits are 5-10 ms median
and under 60 ms max.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…end's host round
`mark_notes_spent` queued the spend's changeset while still holding the
store write guard. The FFI persister runs the whole begin/persist/end host
round synchronously behind its blocking round lock, so a confirming send
held the store across host DB writes, and across a wait for a sync pass's
delivery of thousands of notes. Every other send's reservation and witness
probe, every local-balance read and the pass's own correction read then
waited behind host I/O: the stall this branch removes from the sync pass.
The store guard now covers only `mark_spent` and the copy read. The round
is queued after it drops, then the store's copies are read again and any
that a sync pass re-saved in between go out in a follow-up round, so the
host still ends on the store's copy whichever round lands first. A failed
copy read is logged and skipped instead of returning before the round is
queued, which had left the host with the note unspent.
`ShieldedNote` derives `PartialEq, Eq` for that comparison.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hielded sync change
- Name the tests this branch adds "should ..." (Rust, Kotlin, Swift).
- Import `ShieldedChangeSet`, `PlatformWalletChangeSet` and the test
persister types instead of writing inline `crate::` paths.
- Say in `sync_notes_across`'s docs that the commit's wait for the write
lock doesn't observe a stop: once the stream has ended, a complete scan
always commits.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ded Clear
Since 4144db1, `mark_notes_spent` released the store before its host
round. Clear could then purge the store and return while that round was
still waiting on the persister, the host deleted its shielded rows, and
the round, which carries the whole note, put the deleted receipt back.
The round is queued under the store guard again, as before 4144db1,
but downgraded to a read guard first: Clear and sync-pass commits wait
for it, balance reads do not. With no pass able to commit in between,
the follow-up round and `ShieldedNote`'s `PartialEq` derive go away.
A regression test parks the spend's round in the host while Clear runs,
and fails on the previous code with the note recreated after the wipe.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…stream ends
After the stream ends, cancellation no longer interrupts the commit's
lock wait, but the snapshot check after it still runs, so a complete
scan can return `Superseded` rather than commit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nload lock wording
- Cover a pending spend saved while a sync pass is paused mid-download:
the pass matches saved spends under its commit guard, so the competing
action it already read still fails the spend.
- Say a gap stop as a gap in the coordinator log: the cut-short summary
now takes its reason.
- Update comments and a test name that still described a scan holding
the store for its whole download.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… as purged
A re-bind that drops an account keeps the account's store state when a
spend saved its record from it after the bind's check. It still bumped
the account's purge generation, so a pass that committed a receipt for
the account before the re-bind dropped that slice at fan-out: the host
never got the note while the store's watermark moved past it. Bump only
the accounts actually purged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ge succeeds
Bump the purge generations before the wallet generation, so a sync pass
that watched the epoch during the purge loop retries instead of
committing against a purge count it never saw.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.
The reason will be displayed to describe this comment to others. Learn more.
Re-review — Final validation — Phase 1 + Phase 2
New on this head: This rebase onto v5.2-dev preserves the shielded sync lock changes, adopts #6276’s checkpoint contract from the base, and supplies epoch watches to the rebased checkpoint tests. The Kotlin SDK build and tests and the Swift frozen-schema check passed; Rust workspace tests are still running.
🟡 1 suggestion(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.
1 finding(s) not shown inline (the lines are not part of this PR's diff)
🟡 Suggestion: The idle file-store checkpoint test passes even if the scan stops before commit Impact: CI can report that an idle file-backed scan preserved both checkpoints even when the scan returned Superseded or StoppedAtGap instead of completing. packages/rs-platform-wallet/src/wallet/shielded/sync/cancel_tests.rs:726
The final witness comparison can pass without exercising the checkpoint call: if the idle scan stops early without appending anything, both witnesses simply stay as they were. In #6276, this call relied on run() calling .finished() before returning. After the rebase, it uses this PR’s helper, which returns any successful NoteSyncOutcome, and discards that outcome. Require Completed before comparing the witnesses, as the adjacent in-memory test does.
.await
.finished();
source: gpt-6.2-astra (phase2-reviewer: general, architecture-layering, ffi-engineer, rust-quality, security-auditor)
Triage: critical by gpt-6.2-sol (effort low) — The intricate sync and persistence changes directly affect which notes remain eligible for coin selection: operations.rs::mark_notes_spent now coordinates spent-note persistence with concurrent sync commits and store clears.
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/sync/cancel_tests.rs`:
- [SUGGESTION] packages/rs-platform-wallet/src/wallet/shielded/sync/cancel_tests.rs:726: The idle file-store checkpoint test passes even if the scan stops before commit
Impact: CI can report that an idle file-backed scan preserved both checkpoints even when the scan returned `Superseded` or `StoppedAtGap` instead of completing.
The final witness comparison can pass without exercising the checkpoint call: if the idle scan stops early without appending anything, both witnesses simply stay as they were. In #6276, this call relied on `run()` calling `.finished()` before returning. After the rebase, it uses this PR’s helper, which returns any successful `NoteSyncOutcome`, and discards that outcome. Require `Completed` before comparing the witnesses, as the adjacent in-memory test does.
Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.
The reason will be displayed to describe this comment to others. Learn more.
Re-review — Final validation — Phase 1 + Phase 2
New on this head: This head changes only two file-backed shielded checkpoint tests: every pass expected to commit must now return Completed, while the recovery test still checks the injected transport failure separately. Rust workspace tests are pending; the Kotlin SDK build and tests and the Swift frozen-schema check passed.
Triage: critical by gpt-6.2-sol (effort low) — The intricate concurrency changes in shielded/sync.rs and operations.rs::mark_notes_spent alter how confirmed spends are recorded and persisted, which determines whether an already-spent note can become eligible for selection after restart.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Observable value
Shielded send started while a sync pass is running: it waited for the rest of the pass before it could reserve notes and start proving. Worst case (send at the start of the pass): 0.30 s median (0.23–1.6 s) behind an incremental pass, 1.2 s behind a full testnet catch-up, and 226 s behind one catch-up that stalled. Now ≤ 15 ms, and ≤ 0.1 ms during an incremental pass.
How often: passes are short today, so a send overlaps one in roughly 1% of sends (the 60 s loop's ~0.3 s pass), more for a second send right after the first (forced readback passes at 0, +4, +12 and +24 s) and right after foregrounding. The tail is unbounded: a slow or stalled pass held every send for its whole length.
Measured on an Apple M4 Max desktop, release build, against testnet (4,879-note pool), host load average 44–320 from other builds. 5 interleaved runs per side (base, PR, base, PR, ...), each one cold catch-up plus 4 incremental passes through
NetworkShieldedCoordinator::sync(true). The "send" is a probe that takes the store write lock (asreserve_notesdoes) every 20 ms; "before" numbers are its wait when it starts at the beginning of the pass, i.e. the worst case. Desktop network only; phone numbers not measured.Issue being fixed or feature implemented
A shielded sync pass held the shielded store's write lock for the entire note download:
stream.next().await(network round trips and server proof generation);A shielded send needs that same lock up to five times: reserve notes, the anchor/witness probe, pending activity, release arming, and post-result. tokio's
RwLockis fair, so every send queued behind the whole in-flight pass.Passes run often:
So a send could wait anywhere from 0 s to a full sync pass. That wait appeared to the user as "generating proof".
Part of a series cutting Dash shielded send latency. Related: #5280, #5282, dashpay/halo2#1–#3 and dashpay/orchard#12–#13 (prover), dashpay/dashwallet-ios#1183 (prover warm-up).
What was done?
shardtree(0.6.2, checked) computes a checkpoint-depth witness as of that checkpoint's position. Leaves appended between holds change no witness or anchor until the pass checkpoints them together with their notes. Sends also copy their witnesses out under the read lock.StoreEpoch).Superseded. It has written only uncheckpointed leaves, andsync()retries up to twice from a fresh registry snapshot.sync_passmutex serializessync()passes, keeping the pass the tree's only appender. Clear holds it too.sync_pass, the pre-scan read and every append slice observe the cancellation token. A stopped pass checkpoints its appended leaves only if the store is free and its snapshot is current, publishes nothing else, and reportsCancelled.fan_outmutex keeps a pass's results from reaching a persister for state purged after its commit.ShieldedStore::checkpoint_treeand followed byInMemoryShieldedStore.sync_notes_from_stream), so tests can pause a pass mid-download.How Has This Been Tested?
New
sync/lock_contention_tests.rsdrives a pass with a hand-fed stream:Plus:
ShieldedSyncFailedafter repeated supersession, and a mid-pass bind of another wallet does not interrupt the scan;Testnet measurement (see "Observable value"), store write-lock wait seen by the probe, per pass:
The two slow catch-ups (226 s, 300 s) coincided with a load spike on the host; pass length is otherwise unchanged by this PR.
Commands:
cargo test -p platform-wallet -p platform-wallet-ffi --all-features: 2587 pass.cargo clippy ... --all-targets -D warnings(both crates) andcargo fmt --all --checkare clean.Two review rounds and a simplification pass were applied before opening.
Breaking Changes
None. A pass superseded three times in a row reports
ShieldedSyncFailedfor the wallets it was scanning; the next scheduled pass runs normally.Known trade-offs:
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
Summary by CodeRabbit
PR Hygiene ·
b0ef19f/self-reviewedkotlin-sdk(packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt,packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt,packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt) — HashEngineeringrs-platform-wallet-ffi(packages/rs-platform-wallet-ffi/src/persistence.rs,packages/rs-platform-wallet-ffi/src/shielded_persistence.rs,packages/rs-platform-wallet-ffi/src/shielded_sync.rs) — HashEngineering or ZocoLini or llbartekll or romchornyirs-platform-wallet(packages/rs-platform-wallet/src/changeset/shielded_changeset.rs,packages/rs-platform-wallet/src/manager/mod.rs,packages/rs-platform-wallet/src/wallet/platform_wallet.rsand 15 more) — HashEngineering or ZocoLini or llbartekll or romchornyiswift-sdk(packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentShieldedNote.swift,packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedLocalBalance.swift,packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swiftand 1 more) — 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.