From 20f2680a40e560ba8d7791e849ffa1b28f0f8b90 Mon Sep 17 00:00:00 2001 From: pasta Date: Sun, 15 Nov 2026 08:14:42 +0000 Subject: [PATCH 1/4] fix(platform-wallet): own the shielded coordinator and wipe host rows inside Clear Shielded Clear spanned three owners with nothing tying them together. Wallet calls took a caller-supplied coordinator, so a wallet could stay bound to one Clear never reset. The host deleted its own shielded rows after Clear returned, so a bind or account add with a delayed host write, or a snapshot read before the Clear, could survive it. Each race had its own guard (clear_generation, snapshot_predates_clear). - The manager owns the coordinator; wallet shielded operations resolve it themselves and the coordinator parameter leaves the public API. - Clear calls the new persister method wipe_shielded_state(network), declared by PersistenceCapabilities::SHIELDED_STATE_WIPE, after the native reset and before returning. A persister without it makes Clear refuse before anything is reset. A failed wipe refuses binds, account adds and restores until a later Clear succeeds. - The FFI vtable extension gains on_wipe_shielded_state_fn, the JNI bridge onWipeShieldedState, and the Swift, Kotlin and SQLite persisters implement the network-scoped wipe; the app-side deletion after Clear is gone. - One admission lock (shared for every durable shielded mutation, exclusive for Clear across reset, host wipe and epoch bump) replaces clear_generation and snapshot_predates_clear. Lock order is wallet shield_guard, admission, lifecycle, store, so an operation queued behind a long shield never holds admission against a Clear. Co-Authored-By: Claude Opus 5.5 --- .../dashfoundation/dashsdk/FfiSmokeTest.kt | 46 ++ .../dashsdk/ffi/NativePersistenceBridge.kt | 25 + .../PlatformWalletPersistenceHandler.kt | 28 +- .../dashsdk/services/ShieldedService.kt | 101 +-- .../dashsdk/wallet/PlatformWalletManager.kt | 31 +- .../PlatformWalletPersistenceHandlerTest.kt | 40 +- .../dashsdk/services/ShieldedServiceTest.kt | 7 +- .../rs-platform-wallet-ffi/src/manager.rs | 55 +- .../rs-platform-wallet-ffi/src/persistence.rs | 95 +++ .../src/private_contact_request.rs | 33 +- .../src/shielded_send.rs | 103 +-- .../src/shielded_sync.rs | 239 ++++-- .../src/shielded_wallet_drain.rs | 9 +- .../src/sqlite/persister.rs | 39 +- .../sqlite/schema/shielded_viewing_keys.rs | 15 +- .../sqlite_shielded_viewing_keys.rs | 99 ++- .../examples/shielded_sync.rs | 4 +- .../examples/shielded_sync_paloma.rs | 4 +- .../src/changeset/persistence_capabilities.rs | 15 +- .../src/changeset/traits.rs | 24 + .../rs-platform-wallet/src/manager/load.rs | 2 + .../rs-platform-wallet/src/manager/mod.rs | 77 +- .../src/manager/wallet_lifecycle.rs | 2 + .../rs-platform-wallet/src/test_support.rs | 52 ++ .../src/wallet/core/broadcast.rs | 2 + .../network/private_contact_requests.rs | 109 +-- .../src/wallet/platform_wallet.rs | 254 ++++--- .../src/wallet/shielded/balance_tests.rs | 18 +- .../src/wallet/shielded/coordinator.rs | 468 ++++++++---- .../wallet/shielded/fund_from_asset_lock.rs | 15 +- .../shielded/lifecycle_boundary_tests.rs | 687 ++++++++++++++++++ .../src/wallet/shielded/mod.rs | 10 + .../src/wallet/shielded/seed_pool.rs | 2 - .../wallet/shielded/viewing_key_bind_tests.rs | 226 ++---- .../src/wallet/shielded/wallet_drain.rs | 2 + .../src/wallet/shielded/wallet_drain_tests.rs | 75 +- .../rs-unified-sdk-jni/src/persistence.rs | 59 ++ .../rs-unified-sdk-jni/src/wallet_manager.rs | 16 +- .../PlatformWalletManager.swift | 5 + .../PlatformWalletManagerShieldedSync.swift | 28 +- .../PlatformWalletPersistenceHandler.swift | 51 ++ .../Core/Services/ShieldedService.swift | 160 +--- .../Core/Views/CoreContentView.swift | 10 +- .../ShieldedServiceClearTests.swift | 122 +--- .../InvitationPersistenceTests.swift | 2 + .../ShieldedStateWipeTests.swift | 59 ++ 46 files changed, 2427 insertions(+), 1098 deletions(-) create mode 100644 packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs create mode 100644 packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ShieldedStateWipeTests.swift diff --git a/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/FfiSmokeTest.kt b/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/FfiSmokeTest.kt index 4895db7b6bd..f91540c14bf 100644 --- a/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/FfiSmokeTest.kt +++ b/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/FfiSmokeTest.kt @@ -230,6 +230,52 @@ class FfiSmokeTest { } } + /** + * Native Clear has the persistence bridge wipe its shielded rows, so + * `SHIELDED_STATE_WIPE` holds only for a bridge that overrides + * `onWipeShieldedState` AND declares the bit: JNI leaves the slot + * unwired for an inherited body, and Rust strips an undeclared bit. + */ + @Test + fun shieldedStateWipeCapabilityNeedsTheOverrideAndTheDeclaration() { + NativeLoader.ensureLoaded() + val bit = NativePersistenceBridge.CAPABILITY_SHIELDED_STATE_WIPE + fun effective(bridge: NativePersistenceBridge): Boolean = + WalletManagerNative.nativeEffectivePersistenceCapabilityBits(bridge) and bit == bit + + assertFalse( + "declared, inherited body", + effective(object : NativePersistenceBridge() { + override fun persistenceCapabilitiesVersion(): Int = 1 + override fun persistenceCapabilitiesBits(): Long = bit + }), + ) + assertFalse( + "overridden, not declared", + effective(object : NativePersistenceBridge() { + override fun persistenceCapabilitiesVersion(): Int = 1 + override fun onWipeShieldedState(network: Int): Int = 0 + }), + ) + assertTrue( + "overridden and declared", + effective(object : NativePersistenceBridge() { + override fun persistenceCapabilitiesVersion(): Int = 1 + override fun persistenceCapabilitiesBits(): Long = bit + override fun onWipeShieldedState(network: Int): Int = 0 + }), + ) + val db = DashDatabase.createInMemory(ApplicationProvider.getApplicationContext()) + try { + assertTrue( + "the Room handler", + effective(PlatformWalletPersistenceHandler(db, Dispatchers.Unconfined)), + ) + } finally { + db.close() + } + } + /** * Rust↔Kotlin persistence-descriptor lockstep. The trampolines resolve * each bridge method by (name, descriptor) only when its slot first diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt index ae4d7946395..de9efecc3c8 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt @@ -113,6 +113,15 @@ abstract class NativePersistenceBridge { */ const val CAPABILITY_PRIVATE_CONTACT_REQUEST_SCAN_STATE: Long = 1L shl 15 + /** + * `PersistenceCapabilities::SHIELDED_STATE_WIPE` (bit 16, + * `0x10000`): the store deletes its shielded rows for a network when + * native Clear asks ([onWipeShieldedState]). Native Clear refuses + * without it. The one Kotlin home of this bit, aliased by the public + * diagnostic mirror. + */ + const val CAPABILITY_SHIELDED_STATE_WIPE: Long = 1L shl 16 + /** * Packed row widths of [onPersistPrivateContactScanState] and * [onLoadPrivateContactScanState]: a read position is the identity @@ -916,6 +925,22 @@ abstract class NativePersistenceBridge { fvkBytes: ByteArray, ): Int = 0 + /** + * `on_wipe_shielded_state_fn`. Descriptor `(I)I`. Delete every shielded + * row kept for [network] (an `FFINetwork` discriminant, + * [org.dashfoundation.dashsdk.Network.ffiValue]): the viewing keys, + * notes, outgoing notes, sync watermarks and activity of every wallet on + * that network, loaded or not, in one transaction. Return 0 once they are + * gone; non-zero leaves them in place and fails the Clear. + * + * Native Clear calls it once, after its own reset and before it returns, + * with every other shielded operation held out. JNI wires the slot only + * for a subclass that overrides this method, and the store must also + * declare [CAPABILITY_SHIELDED_STATE_WIPE]; the inherited body is never + * called. + */ + open fun onWipeShieldedState(network: Int): Int = -1 + // ── Load callbacks ──────────────────────────────────────────────── // // These return objects rather than `Int`, so [PERSIST_RC_TRANSIENT] and diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt index b33f99a467d..e380b7bf8d6 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt @@ -157,7 +157,8 @@ class PlatformWalletPersistenceHandler( CAPABILITY_DPNS_NAME_STATES or CAPABILITY_TRACKED_ASSET_LOCKS or CAPABILITY_CORE_SWEEP_REMOVAL or - CAPABILITY_PRIVATE_CONTACT_REQUEST_SCAN_STATE + CAPABILITY_PRIVATE_CONTACT_REQUEST_SCAN_STATE or + CAPABILITY_SHIELDED_STATE_WIPE /** * The single-thread executor created when no [dispatcher] is injected. @@ -2683,6 +2684,28 @@ class PlatformWalletPersistenceHandler( 0 } + /** + * Native Clear's host half: drop the shielded rows of every wallet on + * [network] in one transaction. Scoped by the wallet rows' network rather + * than the loaded fleet, so a same-network wallet that failed restore + * loses its rows too. + */ + override fun onWipeShieldedState(network: Int): Int = guarded { + runBlockingCatching { + database.withTransaction { + val dao = database.shieldedDao() + for (wallet in database.walletDao().getByNetwork(network)) { + dao.deleteActivityByWallet(wallet.walletId) + dao.deleteOutgoingNotesByWallet(wallet.walletId) + dao.deleteNotesByWallet(wallet.walletId) + dao.deleteSyncStatesByWallet(wallet.walletId) + dao.deleteViewingKeysByWallet(wallet.walletId) + } + } + } + 0 + } + // ── Load callbacks ──────────────────────────────────────────────── /** @@ -4211,6 +4234,9 @@ class PlatformWalletPersistenceHandler( internal const val CAPABILITY_PRIVATE_CONTACT_REQUEST_SCAN_STATE: Long = NativePersistenceBridge.CAPABILITY_PRIVATE_CONTACT_REQUEST_SCAN_STATE + internal const val CAPABILITY_SHIELDED_STATE_WIPE: Long = + NativePersistenceBridge.CAPABILITY_SHIELDED_STATE_WIPE + private const val TAG = "DashPersistence" /** diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/services/ShieldedService.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/services/ShieldedService.kt index 1dfb441d1b0..1efd51fb0bb 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/services/ShieldedService.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/services/ShieldedService.kt @@ -1,6 +1,5 @@ package org.dashfoundation.dashsdk.services -import androidx.room.withTransaction import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob @@ -385,53 +384,28 @@ class ShieldedService(private val database: DashDatabase) { /** * Reset shielded state for the bound wallet — port of Swift - * `ShieldedService.clearLocalState(modelContext:)`. Two ordered halves: + * `ShieldedService.clearLocalState()`. * - * 1. **Rust-side reset FIRST** via - * [PlatformWalletManager.clearShieldedStorage] - * (`platform_wallet_manager_shielded_clear`): quiesce the background - * sync loop, drop every wallet registration from the network-scoped - * coordinator, empty the shared commitment tree, and reset the - * caught-up cooldown. This MUST run before the Room wipe — the - * coordinator keeps every bound wallet registered, so without it the - * next sync pass's persister callback immediately re-creates the very - * rows we're about to delete. It also resets the on-disk tree size, so - * the post-clear resync cold-rebuilds from index 0 instead of - * gate-skipping every re-downloaded position against a stale tree - * (the frozen-tree / zero-notes desync this Clear exists to fix). - * 2. **Room wipe — every wallet on THIS network** (INCLUDING - * `shielded_sync_states`, the per-subwallet watermark; leaving any - * would let [bind]'s `restore_for_wallet` re-seed a caught-up - * watermark and re-freeze the tree) in one transaction, and zero the - * published counters. Scoped to the active network's wallets - * (`walletDao().getByNetwork`), NOT global: [clearShieldedStorage] - * empties only this network's `shielded_tree_.sqlite`, so - * wiping other networks' rows would drop notes/activity/watermarks - * whose Rust trees are still populated → data loss + Room↔Rust - * divergence on the next network switch. Within this network the wipe - * must cover EVERY wallet (not just the loaded/bound fleet — an - * unloaded same-network wallet's surviving watermark would restore a - * position ahead of the now-empty shared tree on re-bind and - * gate-skip every note, the exact Room↔SQLite watermark-divergence - * freeze this Clear exists to fix). - * 3. **Re-bind + restart sync** — [clearShieldedStorage] dropped every - * coordinator registration and quiesced the sync loop, so nothing - * would resync until the next app relaunch otherwise. Re-run [bind] - * for the mirror wallet (re-registers the viewing keys on the - * now-empty coordinator, its `restore_for_wallet` finding no - * watermark → starts at index 0), then [bindEngine] every OTHER - * loaded wallet — mirror-bind failure does NOT skip the others - * ([bind] swallows its errors; the fleet pass runs regardless, the - * iOS best-effort lesson) — and restart the background loop, so the - * button alone triggers an in-session cold rebuild 0→N for the - * whole fleet. + * 1. **Native Clear** via [PlatformWalletManager.clearShieldedStorage] + * (`platform_wallet_manager_shielded_clear`). It resets the network's + * shielded state in Rust and then, before returning and with every + * other shielded operation held out, has the persistence handler + * delete this network's shielded Room rows + * ([org.dashfoundation.dashsdk.persistence.PlatformWalletPersistenceHandler.onWipeShieldedState]). + * Nothing is deleted here. A failure throws and the published + * counters stay as they were. + * 2. **Re-bind + restart sync** — Clear dropped every coordinator + * registration and quiesced the sync loop, so nothing would resync + * until the next app relaunch otherwise. Re-run [bind] for the mirror + * wallet, then [bindEngine] every OTHER loaded wallet — mirror-bind + * failure does NOT skip the others ([bind] swallows its errors; the + * fleet pass runs regardless, the iOS best-effort lesson) — and + * restart the background loop, so the button alone triggers an + * in-session cold rebuild 0→N for the whole fleet. * - * Fail-closed: if the Rust reset throws, the exception propagates and the - * Room rows are left intact — the FFI's contract is that the host must - * not drop its persistence while the shared tree may still be populated. * No-op when unbound. */ - suspend fun clearLocalState(db: DashDatabase = database) { + suspend fun clearLocalState() { val walletId = boundWalletId ?: return // Snapshot bind credentials up front so the re-bind at the end is // driven off a stable set (bind() retains credentials across its own @@ -441,48 +415,15 @@ class ShieldedService(private val database: DashDatabase) { val accounts = _boundAccounts.value.ifEmpty { listOf(0) } // A bound service must have a manager (both are set together in - // bind, cleared together in unbind). If it doesn't, the Rust-side - // reset can't run — fail LOUDLY rather than silently wiping only - // Room and leaving the on-disk tree at its full size (which would - // re-freeze the tree on the next cold resync). + // bind, cleared together in unbind). if (mgr == null) { error("Shielded clear: bound wallet has no manager — cannot reset the Rust store") } - // Rust-side reset FIRST (empties the on-disk commitment tree + - // watermarks, durably). This THROWS on failure (native FFI error → - // DashSDKException), and we deliberately do NOT catch it: the Room - // wipe below must not run unless the tree was truly reset, else the - // host drops its rows while the shared tree stays populated and the - // next resync gate-skips every position. Fail-closed by propagation. val walletIdHex = walletId.joinToString("") { "%02x".format(it) } - android.util.Log.i(TAG, "Shielded clear: resetting Rust store for $walletIdHex") + android.util.Log.i(TAG, "Shielded clear: resetting shielded state for $walletIdHex") mgr.clearShieldedStorage() - android.util.Log.i(TAG, "Shielded clear: Rust store reset OK; wiping Room rows") - - // Wipe every wallet on THIS network, but NOT other networks'. `mgr` - // is network-scoped and its shielded tree - // (`shielded_tree_.sqlite`) is per-network, so the Rust - // reset above emptied only the ACTIVE network's SHARED tree. A - // global deleteAll* would drop other networks' notes/activity/ - // watermarks while their Rust trees stay populated → Room/Rust - // divergence + data loss on the next network switch. Scope to the - // network via `walletDao().getByNetwork` — NOT `mgr.wallets`, which - // only holds the successfully-loaded fleet: a same-network wallet - // that failed restore still has Room rows, and a surviving watermark - // would restore a position ahead of the now-empty tree on re-bind - // and gate-skip every note (see doc, step 2). - val networkWalletIds = db.walletDao() - .getByNetwork(mgr.network.ffiValue) - .map { it.walletId } - db.withTransaction { - for (wid in networkWalletIds) { - db.shieldedDao().deleteActivityByWallet(wid) - db.shieldedDao().deleteOutgoingNotesByWallet(wid) - db.shieldedDao().deleteNotesByWallet(wid) - db.shieldedDao().deleteSyncStatesByWallet(wid) - } - } + android.util.Log.i(TAG, "Shielded clear: native and Room shielded state reset") _state.update { it.copy(shieldedBalance = 0, totalScanned = 0, totalNewNotes = 0) } // Re-bind + restart so the cold rebuild runs now, not just after a diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/wallet/PlatformWalletManager.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/wallet/PlatformWalletManager.kt index fd1a4d10ea9..258ade52f05 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/wallet/PlatformWalletManager.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/wallet/PlatformWalletManager.kt @@ -88,6 +88,13 @@ data class PlatformWalletPersistenceCapabilities( */ const val PRIVATE_CONTACT_REQUEST_SCAN_STATE: Long = NativePersistenceBridge.CAPABILITY_PRIVATE_CONTACT_REQUEST_SCAN_STATE + /** + * The store deletes its shielded rows for a network when native + * Clear asks, which [PlatformWalletManager.clearShieldedStorage] + * requires. Mirrors `PersistenceCapabilities::SHIELDED_STATE_WIPE`. + */ + const val SHIELDED_STATE_WIPE: Long = + NativePersistenceBridge.CAPABILITY_SHIELDED_STATE_WIPE } } @@ -1628,21 +1635,23 @@ class PlatformWalletManager( } /** - * Reset the Rust-side shielded state on this manager — port of Swift's + * Reset the shielded state of this manager's network — port of Swift's * `PlatformWalletManager.clearShielded()` * (`PlatformWalletManagerShieldedSync.swift`). Quiesces the background * sync loop, drops every wallet registration from the network-scoped - * coordinator, empties the shared commitment tree, and resets the - * caught-up cooldown, so the next [bindShielded] + sync cold-rebuilds - * from index 0. + * coordinator, empties the shared commitment tree, resets the caught-up + * cooldown, and has the persistence handler delete this network's + * shielded Room rows before returning, with every other shielded + * operation held out. The next [bindShielded] + sync cold-rebuilds from + * index 0. The caller deletes nothing itself. * - * The per-network SQLite file stays on disk (its contents are reset); - * the host must wipe its own Room rows AFTER this succeeds - * ([org.dashfoundation.dashsdk.services.ShieldedService.clearLocalState] - * orders the two). Throws on a store-reset failure so the caller can - * fail closed and keep its rows rather than orphan a still-populated - * tree. Only meaningful on a shielded build ([Sdk.hasShielded]); the - * native entry point is absent otherwise and this throws. + * Throws when the persistence handler does not declare + * [PlatformWalletPersistenceCapabilities.SHIELDED_STATE_WIPE] (nothing is + * reset), when the native reset fails, or when the Room wipe fails after + * the native reset; in the last case binds are refused until a retried + * Clear succeeds. Only meaningful on a shielded build + * ([Sdk.hasShielded]); the native entry point is absent otherwise and + * this throws. */ suspend fun clearShieldedStorage() = withContext(Dispatchers.IO) { mapNativeErrors { WalletManagerNative.shieldedClear(managerHandle) } diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt index 6a40c939b72..d6e68031766 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt @@ -29,6 +29,7 @@ import org.dashfoundation.dashsdk.persistence.entities.WalletEntity import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotEquals import org.junit.Assert.assertNotNull import org.junit.Assert.assertNull import org.junit.Assert.assertTrue @@ -220,8 +221,9 @@ class PlatformWalletPersistenceHandlerTest { assertEquals(0L, noOpBridge.persistenceCapabilitiesBits()) assertEquals(1, handler.persistenceCapabilitiesVersion()) - // The private contact request scan state is bit 15 (0x8000). - assertEquals(0x8bbfL, handler.persistenceCapabilitiesBits()) + // The private contact request scan state is bit 15 (0x8000), the + // shielded state wipe bit 16 (0x10000). + assertEquals(0x18bbfL, handler.persistenceCapabilitiesBits()) // Android has no pending-contact-crypto callback, so it must not // attest that semantic contract. assertEquals(0L, handler.persistenceCapabilitiesBits() and 0x40L) @@ -240,6 +242,7 @@ class PlatformWalletPersistenceHandlerTest { assertTrue( diagnostic.contains(PlatformWalletPersistenceCapabilities.PRIVATE_CONTACT_REQUEST_SCAN_STATE), ) + assertTrue(diagnostic.contains(PlatformWalletPersistenceCapabilities.SHIELDED_STATE_WIPE)) } @Test @@ -415,6 +418,39 @@ class PlatformWalletPersistenceHandlerTest { scoped.close() } + @Test + fun shieldedWipeDropsEveryShieldedRowOfTheNetworkAndKeepsOtherNetworks() = runTest { + val testnetWallet = ByteArray(32) { 0x61 } + val mainnetWallet = ByteArray(32) { 0x62 } + db.walletDao().upsert(WalletEntity(testnetWallet, networkRaw = Network.TESTNET.ffiValue)) + db.walletDao().upsert(WalletEntity(mainnetWallet, networkRaw = Network.MAINNET.ffiValue)) + for (wallet in listOf(testnetWallet, mainnetWallet)) { + db.shieldedDao().upsertViewingKey( + org.dashfoundation.dashsdk.persistence.entities.ShieldedViewingKeyEntity( + wallet, 0, ByteArray(96) { 1 }, + ), + ) + db.shieldedDao().upsertSyncState( + org.dashfoundation.dashsdk.persistence.entities.ShieldedSyncStateEntity( + wallet, 0, lastSyncedIndex = 42, + ), + ) + } + + assertEquals(0, handler.onWipeShieldedState(Network.TESTNET.ffiValue)) + + assertTrue(db.shieldedDao().getViewingKeysByWallet(testnetWallet).isEmpty()) + assertTrue(db.shieldedDao().getAllSyncStates().none { it.walletId.contentEquals(testnetWallet) }) + assertEquals(1, db.shieldedDao().getViewingKeysByWallet(mainnetWallet).size) + assertEquals(1, db.shieldedDao().getAllSyncStates().count { it.walletId.contentEquals(mainnetWallet) }) + } + + @Test + fun shieldedWipeIsNeverInheritedAsASuccess() { + val noOpBridge = object : NativePersistenceBridge() {} + assertNotEquals(0, noOpBridge.onWipeShieldedState(Network.TESTNET.ffiValue)) + } + @Test fun providerRestoreStagingIsPayloadOnlyWalletScopedAndOrderedByBlockPosition() = runTest { val siblingWalletId = ByteArray(32) { 0x22 } diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/services/ShieldedServiceTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/services/ShieldedServiceTest.kt index 539d024b9f6..6f96453d54d 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/services/ShieldedServiceTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/services/ShieldedServiceTest.kt @@ -159,14 +159,11 @@ class ShieldedServiceTest { } @Test - fun clearLocalStateDeletesBoundWalletRows() = runTest { - // Simulate bind by hand (native bind is unavailable under Robolectric): - // populate rows, drive reduce, then clear. + fun clearLocalStateIsANoOpWhileUnbound() = runTest { db.shieldedDao().upsertNote(note(1, 1_000, spent = false)) db.shieldedDao().upsertNote(note(2, 2_000, spent = false, wallet = otherWallet)) - // Clear is a no-op while unbound (nothing to scope to). - service.clearLocalState(db) + service.clearLocalState() assertEquals(2, db.shieldedDao().getAllNotes().size) } diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 51e38104709..5ec79812454 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -7,6 +7,8 @@ use crate::event_handler::{ FFIEventHandler, PLATFORM_WALLET_EVENT_CALLBACKS_EXTENSION_VERSION, }; use crate::handle::*; +#[cfg(all(test, feature = "shielded"))] +use crate::persistence::PLATFORM_WALLET_PERSISTENCE_CAPABILITY_SHIELDED_STATE_WIPE; use crate::persistence::{ FFIPersister, FreePrivateContactScanStateFn, FreePrivateContactScanStateWalletsFn, FreeTrackedMasternodesFn, LoadAssetLockOperationIdFn, LoadAssetLockShieldedRecipientFn, @@ -17,7 +19,7 @@ use crate::persistence::{ PersistTrackedMasternodesFn, PersistWalletChangesetChainLockHeightFn, PersistWalletChangesetSweepsFn, PersistWalletChangesetUtxoVerdictsFn, PersistWalletDashPayBackfillFn, PersistenceCallbacks, PersistenceCallbacksExtension, - PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks, + PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks, WipeShieldedStateFn, PLATFORM_WALLET_PERSISTENCE_CALLBACKS_EXTENSION_VERSION, }; use crate::runtime::runtime; @@ -408,6 +410,7 @@ unsafe fn persistence_extension_callbacks( on_load_private_contact_scan_state_wallets_free_fn, FreePrivateContactScanStateWalletsFn ), + wipe_shielded_state: slot!(on_wipe_shielded_state_fn, WipeShieldedStateFn), } } @@ -1693,6 +1696,56 @@ mod tests { assert!(read.wallet_dashpay_backfill.is_some()); } + /// The wipe slot is read only from a host whose `struct_size` covers it, + /// and `SHIELDED_STATE_WIPE` is attested only when the host both wires it + /// and declares the bit. + #[cfg(feature = "shielded")] + #[test] + fn should_size_gate_and_attest_the_shielded_wipe_slot() { + unsafe extern "C" fn wipe(_: *mut c_void, _: FFINetwork) -> i32 { + 0 + } + let wired = PersistenceCallbacksExtension { + on_wipe_shielded_state_fn: Some(wipe), + ..Default::default() + }; + assert!(unsafe { persistence_extension_callbacks(&wired) } + .wipe_shielded_state + .is_some()); + let short = PersistenceCallbacksExtension { + struct_size: std::mem::offset_of!( + PersistenceCallbacksExtension, + on_wipe_shielded_state_fn + ), + ..wired + }; + assert!(unsafe { persistence_extension_callbacks(&short) } + .wipe_shielded_state + .is_none()); + + let declared = |bits: PersistenceCapabilities| PersistenceCapabilitiesFFI { + version: PERSISTENCE_CAPABILITIES_VERSION, + reserved: 0, + bits: bits.bits(), + }; + let effective = |extension: &PersistenceCallbacksExtension, bits| unsafe { + effective_persistence_capabilities( + PersistenceCallbacks::default(), + &declared(bits), + extension, + ) + }; + let unwired = PersistenceCallbacksExtension::default(); + let wipe_bit = PersistenceCapabilities::SHIELDED_STATE_WIPE; + assert!(effective(&wired, wipe_bit).contains(wipe_bit)); + assert!(!effective(&unwired, wipe_bit).contains(wipe_bit)); + assert!(!effective(&wired, PersistenceCapabilities::NONE).contains(wipe_bit)); + assert_eq!( + wipe_bit.bits(), + PLATFORM_WALLET_PERSISTENCE_CAPABILITY_SHIELDED_STATE_WIPE + ); + } + #[test] fn should_size_gate_identity_balance_watermark_slots_independently() { unsafe extern "C" fn persist( diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 8495c4464ac..710a441c79a 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -198,6 +198,12 @@ pub const PLATFORM_WALLET_PERSISTENCE_CAPABILITY_ASSET_LOCK_SHIELDED_RECIPIENTS: /// un-ignore stores the sender's removal and the read-position rewind it /// brings in one changeset. pub const PLATFORM_WALLET_PERSISTENCE_CAPABILITY_PRIVATE_CONTACT_REQUEST_SCAN_STATE: u64 = 1 << 15; +/// The host deletes every shielded row it keeps for a network when asked +/// (`on_wipe_shielded_state_fn`). Required by a shielded Clear, which calls +/// the slot itself after the native reset and before it returns: a host +/// without it cannot be cleared. Attested when the host wires the slot and +/// declares the bit. +pub const PLATFORM_WALLET_PERSISTENCE_CAPABILITY_SHIELDED_STATE_WIPE: u64 = 1 << 16; /// Version of [`PersistenceCallbacksExtension`]. The extension is deliberately /// separate from [`PersistenceCallbacks`]: existing hosts pass the latter by @@ -456,6 +462,19 @@ pub type FreePrivateContactScanStateWalletsFn = unsafe extern "C" fn( wallet_ids_count: usize, ); +/// Delete every shielded row the host keeps for `network`: the viewing keys, +/// notes, outgoing notes, sync watermarks and activity of every wallet it +/// holds on that network, loaded or not, in one transaction. Returns 0 once +/// the rows are gone; non-zero leaves them in place. +/// +/// Called only by `platform_wallet_manager_shielded_clear`, on the calling +/// thread, after the native reset and before that call returns, with every +/// other shielded operation of the manager held out. The host deletes +/// nothing of its own around the Clear call. The vtable's reentrancy +/// contract applies: the callback must not call back into the manager. +pub type WipeShieldedStateFn = + unsafe extern "C" fn(context: *mut c_void, network: FFINetwork) -> i32; + /// Size- and version-tagged additive persistence callbacks. /// /// `context` is the context in the accompanying [`PersistenceCallbacks`] @@ -709,6 +728,12 @@ pub struct PersistenceCallbacksExtension { wallet_ids_count: usize, ), >, + /// Delete the host's shielded rows for a network (see + /// [`WipeShieldedStateFn`]). Appended under the same version: a host + /// whose `struct_size` predates it is never called and does not attest + /// `SHIELDED_STATE_WIPE`, so its Clear is refused. + pub on_wipe_shielded_state_fn: + Option i32>, } impl Default for PersistenceCallbacksExtension { @@ -736,6 +761,7 @@ impl Default for PersistenceCallbacksExtension { on_load_private_contact_scan_state_free_fn: None, on_load_private_contact_scan_state_wallets_fn: None, on_load_private_contact_scan_state_wallets_free_fn: None, + on_wipe_shielded_state_fn: None, } } } @@ -764,6 +790,7 @@ pub struct PersistenceExtensionCallbacks { pub load_private_contact_scan_state_free: Option, pub load_private_contact_scan_state_wallets: Option, pub load_private_contact_scan_state_wallets_free: Option, + pub wipe_shielded_state: Option, } /// Return value by which a persistence callback reports a **retryable** @@ -2019,6 +2046,14 @@ impl FFIPersister { { capabilities = capabilities.union(PersistenceCapabilities::SHIELDED_VIEWING_KEYS); } + #[cfg(feature = "shielded")] + if self + .tracked_masternodes_callbacks + .wipe_shielded_state + .is_some() + { + capabilities = capabilities.union(PersistenceCapabilities::SHIELDED_STATE_WIPE); + } // Deliberately absent: PersistenceCallbacks has no deferred contact // crypto add/clear callback pair, so FFI hosts cannot attest it. @@ -2096,6 +2131,36 @@ impl PlatformWalletPersistence for FFIPersister { .intersection(self.callback_capabilities()) } + #[cfg(feature = "shielded")] + fn wipe_shielded_state(&self, network: Network) -> Result<(), PersistenceError> { + let Some(wipe) = self.tracked_masternodes_callbacks.wipe_shielded_state else { + return Err(PersistenceError::backend( + "the host wired no on_wipe_shielded_state_fn", + )); + }; + // Serialized with store rounds: the host keeps one in-flight + // transaction state, which a round open on another thread would + // otherwise share with this deletion. A round left open by an + // unwound store still has rows staged there, which the wipe's + // commit would publish. + let round = self.round_lock.lock(); + if round.in_round { + return Err(PersistenceError::backend( + "FFIPersister: cannot wipe shielded state inside an unclosed changeset round", + )); + } + // SAFETY: `wipe` and `context` came together from the host's vtable, + // which stays valid for the persister's lifetime. + let rc = unsafe { wipe(self.callbacks.context, FFINetwork::from(network)) }; + if rc != 0 { + return Err(persist_callback_error( + rc, + format!("on_wipe_shielded_state_fn returned {rc}"), + )); + } + Ok(()) + } + fn persist_tracked_masternodes( &self, network: dashcore::Network, @@ -12102,6 +12167,11 @@ mod tests { PersistenceCallbacksExtension, on_load_private_contact_scan_state_wallets_free_fn ) + std::mem::size_of::>(), + std::mem::offset_of!(PersistenceCallbacksExtension, on_wipe_shielded_state_fn) + ); + assert_eq!( + std::mem::offset_of!(PersistenceCallbacksExtension, on_wipe_shielded_state_fn) + + std::mem::size_of::>(), std::mem::size_of::() ); assert_eq!( @@ -14542,6 +14612,31 @@ mod tests { assert!(persister.load().is_err()); } + #[cfg(feature = "shielded")] + #[test] + fn should_refuse_the_shielded_wipe_inside_an_unclosed_persistence_round() { + static WIPES: AtomicUsize = AtomicUsize::new(0); + unsafe extern "C" fn wipe(_: *mut c_void, _: FFINetwork) -> i32 { + WIPES.fetch_add(1, Ordering::SeqCst); + 0 + } + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks::default(), + PersistenceCapabilities::SHIELDED_STATE_WIPE, + PersistenceExtensionCallbacks { + wipe_shielded_state: Some(wipe), + ..Default::default() + }, + ); + persister.round_lock.lock().begin_round().unwrap(); + assert!(persister.wipe_shielded_state(Network::Testnet).is_err()); + assert_eq!(WIPES.load(Ordering::SeqCst), 0, "the host is never called"); + + persister.round_lock.lock().end_round().unwrap(); + persister.wipe_shielded_state(Network::Testnet).unwrap(); + assert_eq!(WIPES.load(Ordering::SeqCst), 1); + } + use std::os::raw::c_void as TestCVoid; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::Arc; diff --git a/packages/rs-platform-wallet-ffi/src/private_contact_request.rs b/packages/rs-platform-wallet-ffi/src/private_contact_request.rs index a9940b6ecc6..379cc9aee29 100644 --- a/packages/rs-platform-wallet-ffi/src/private_contact_request.rs +++ b/packages/rs-platform-wallet-ffi/src/private_contact_request.rs @@ -64,7 +64,7 @@ use std::ffi::CStr; use std::sync::atomic::{AtomicBool, Ordering}; #[cfg(feature = "shielded")] -use platform_wallet::wallet::shielded::{CachedOrchardProver, NetworkShieldedCoordinator}; +use platform_wallet::wallet::shielded::CachedOrchardProver; #[cfg(feature = "shielded")] use platform_wallet::{AutoAcceptProofSource, PrivateReplyPayment}; #[cfg(feature = "shielded")] @@ -77,7 +77,7 @@ use crate::identity_keys_from_mnemonic::resolve_seed_from_resolver; #[cfg(feature = "shielded")] use crate::shielded_send::{ catch_spend_panic, map_spend_result, poke_sync_on_unconfirmed, resolve_shielded_coordinator, - resolve_wallet_and_coordinator, + resolve_shielded_wallet, }; /// The bytes of an Orchard payment address: an 11-byte diversifier and a @@ -583,8 +583,8 @@ unsafe fn send_private_contact_request_inner( }; let shielded_address = read_shielded_address(shielded_address); - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { - Ok(pair) => pair, + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { + Ok(wallet) => wallet, Err(result) => return result, }; let seed = match resolve_seed_from_resolver(mnemonic_resolver_handle, &wallet_id) { @@ -609,7 +609,6 @@ unsafe fn send_private_contact_request_inner( let prover = CachedOrchardProver::new(); let result = wallet .send_private_contact_request( - &coordinator, seed.as_ref(), account, &sender, @@ -713,8 +712,8 @@ unsafe fn accept_private_contact_request_inner( let request = unwrap_option_or_return!(request); let shielded_address = read_shielded_address(shielded_address); - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { - Ok(pair) => pair, + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { + Ok(wallet) => wallet, Err(result) => return result, }; let seed = match resolve_seed_from_resolver(mnemonic_resolver_handle, &wallet_id) { @@ -734,7 +733,6 @@ unsafe fn accept_private_contact_request_inner( let prover = CachedOrchardProver::new(); let result = wallet .accept_private_contact_request( - &coordinator, seed.as_ref(), account, &request, @@ -881,9 +879,10 @@ fn resolve_manager_wallet( } /// What a private reply is paid with, looked up only when the wallet asks: -/// the shielded coordinator, then the seed read through the resolver at -/// `resolver_addr`. Runs on a blocking thread (the coordinator lookup blocks -/// on the runtime, and the resolver calls into the host). +/// the seed read through the resolver at `resolver_addr`, once the manager +/// is known to have shielded support (so an unconfigured manager never +/// prompts for the mnemonic). Runs on a blocking thread (the configuration +/// lookup blocks on the runtime, and the resolver calls into the host). /// /// # Safety /// `resolver_addr` must be the address of a valid `MnemonicResolverHandle` @@ -893,11 +892,10 @@ unsafe fn private_reply_payment_parts( handle: Handle, wallet_id: [u8; 32], resolver_addr: usize, -) -> Result<(Arc, Zeroizing<[u8; 64]>), PaymentFailure> { - let coordinator = resolve_shielded_coordinator(handle).map_err(payment_failure)?; - let seed = resolve_seed_from_resolver(resolver_addr as *mut MnemonicResolverHandle, &wallet_id) - .map_err(payment_failure)?; - Ok((coordinator, seed)) +) -> Result, PaymentFailure> { + resolve_shielded_coordinator(handle).map_err(payment_failure)?; + resolve_seed_from_resolver(resolver_addr as *mut MnemonicResolverHandle, &wallet_id) + .map_err(payment_failure) } /// Body of [`platform_wallet_manager_accept_contact_request_replying_in_kind`], @@ -976,8 +974,7 @@ unsafe fn accept_contact_request_replying_in_kind_inner( )) }); match parts { - Ok((coordinator, seed)) => Ok(PrivateReplyPayment { - coordinator, + Ok(seed) => Ok(PrivateReplyPayment { seed, account, prover: &prover, diff --git a/packages/rs-platform-wallet-ffi/src/shielded_send.rs b/packages/rs-platform-wallet-ffi/src/shielded_send.rs index 2c28e59d1ad..a6822c1b2dc 100644 --- a/packages/rs-platform-wallet-ffi/src/shielded_send.rs +++ b/packages/rs-platform-wallet-ffi/src/shielded_send.rs @@ -353,7 +353,7 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_transfer( Err(result) => return result, }; - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -376,15 +376,7 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_transfer( let result = block_on_worker(async move { let prover = CachedOrchardProver::new(); let r = wallet - .shielded_transfer_to( - &coordinator, - seed.as_ref(), - account, - &recipient, - amount, - memo, - &prover, - ) + .shielded_transfer_to(seed.as_ref(), account, &recipient, amount, memo, &prover) .await; poke_sync_on_unconfirmed(&r, handle); r @@ -438,7 +430,7 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_unshield( } }; - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -454,14 +446,7 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_unshield( let result = block_on_worker(async move { let prover = CachedOrchardProver::new(); let r = wallet - .shielded_unshield_to( - &coordinator, - seed.as_ref(), - account, - &to_addr_str, - amount, - &prover, - ) + .shielded_unshield_to(seed.as_ref(), account, &to_addr_str, amount, &prover) .await; poke_sync_on_unconfirmed(&r, handle); r @@ -512,7 +497,7 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_withdraw( } }; - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -529,7 +514,6 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_withdraw( let prover = CachedOrchardProver::new(); let r = wallet .shielded_withdraw_to( - &coordinator, seed.as_ref(), account, &to_core, @@ -987,7 +971,7 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_identity_create_from_p }) .collect(); - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -1020,7 +1004,6 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_identity_create_from_p let prover = CachedOrchardProver::new(); let r = wallet .shielded_identity_create_from_pool( - &coordinator, seed.as_ref(), account, identity_index, @@ -1172,10 +1155,9 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_shield( let mut wallet_id = [0u8; 32]; std::ptr::copy_nonoverlapping(wallet_id_bytes, wallet_id.as_mut_ptr(), 32); - // Shield writes its live activity entry to the coordinator's shared - // in-memory store, so resolve the coordinator alongside the wallet - // (same resolver the transfer / unshield / withdraw spends use). - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + // The wallet resolves the manager's coordinator itself; this refuses an + // unconfigured manager before the mnemonic is resolved. + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -1205,7 +1187,6 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_shield( let prover = CachedOrchardProver::new(); wallet .shielded_shield_from_account( - &coordinator, shielded_account, payment_account, amount, @@ -1264,7 +1245,7 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_shield_from_identity( std::ptr::copy_nonoverlapping(wallet_id_bytes, wallet_id.as_mut_ptr(), 32); let identity_id = unwrap_result_or_return!(read_identifier(identity_id)); - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -1280,7 +1261,6 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_shield_from_identity( let prover = CachedOrchardProver::new(); wallet .shielded_shield_from_identity( - &coordinator, shielded_account, &identity_id, amount, @@ -1360,7 +1340,7 @@ unsafe fn shielded_identity_top_up_from_pool_inner( std::ptr::copy_nonoverlapping(wallet_id_bytes, wallet_id.as_mut_ptr(), 32); let identity_id = unwrap_result_or_return!(read_identifier(identity_id)); - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -1377,7 +1357,6 @@ unsafe fn shielded_identity_top_up_from_pool_inner( let prover = CachedOrchardProver::new(); let r = wallet .shielded_identity_top_up_from_pool( - &coordinator, seed.as_ref(), account, &identity_id, @@ -1506,10 +1485,9 @@ unsafe fn shielded_shield_to_recipient_inner( Err(result) => return result, }; - // Shield writes its live activity entry to the coordinator's shared - // in-memory store, so resolve the coordinator alongside the wallet - // (same resolver the transfer / unshield / withdraw spends use). - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + // The wallet resolves the manager's coordinator itself; this refuses an + // unconfigured manager before the mnemonic is resolved. + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -1531,7 +1509,6 @@ unsafe fn shielded_shield_to_recipient_inner( let prover = CachedOrchardProver::new(); wallet .shielded_shield_from_account_to_recipient( - &coordinator, shielded_account, payment_account, &recipient, @@ -1635,10 +1612,9 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_fund_from_asset_lock( Err(result) => return result, }; - // The Type 18 live activity recorder writes to the coordinator's - // shared in-memory store, so resolve the coordinator alongside the - // wallet. - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + // The wallet resolves the manager's coordinator itself; this refuses an + // unconfigured manager before the mnemonic is resolved. + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -1669,7 +1645,6 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_fund_from_asset_lock( let on_built = built_callback.map(AssetLockBuiltCallback::into_observer); wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::FromWalletBalance { amount_duffs, account_index, @@ -1790,10 +1765,9 @@ unsafe fn shielded_fund_from_asset_lock_coinjoin_drain_inner( } }; - // The Type 18 live activity recorder writes to the coordinator's - // shared in-memory store, so resolve the coordinator alongside the - // wallet (same as the BIP44-funded sibling). - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + // The wallet resolves the manager's coordinator itself; this refuses an + // unconfigured manager before the mnemonic is resolved. + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -1821,7 +1795,6 @@ unsafe fn shielded_fund_from_asset_lock_coinjoin_drain_inner( let on_built = built_callback.map(AssetLockBuiltCallback::into_observer); wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::DrainAccountBalance { account: key_wallet::wallet::managed_wallet_info::asset_lock_builder::AssetLockFundingAccount::CoinJoin { @@ -1953,10 +1926,9 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_resume_fund_from_asset vout: out_point_ffi.vout, }; - // The Type 18 live activity recorder writes to the coordinator's - // shared in-memory store, so resolve the coordinator alongside the - // wallet. - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + // The wallet resolves the manager's coordinator itself; this refuses an + // unconfigured manager before the mnemonic is resolved. + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -1977,7 +1949,6 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_resume_fund_from_asset let prover = CachedOrchardProver::new(); wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::FromExistingAssetLock { out_point: resume_outpoint, consume_invitation_voucher: false, @@ -2066,10 +2037,9 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_seed_pool_notes( let mut wallet_id = [0u8; 32]; std::ptr::copy_nonoverlapping(wallet_id_bytes, wallet_id.as_mut_ptr(), 32); - // Each seeding batch's Type 18 live activity recorder writes to the - // coordinator's shared in-memory store, so resolve the coordinator - // alongside the wallet. - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + // The wallet resolves the manager's coordinator itself; this refuses an + // unconfigured manager before the mnemonic is resolved. + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(p) => p, Err(result) => return result, }; @@ -2120,7 +2090,6 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_seed_pool_notes( wallet .shielded_seed_pool_notes( - &coordinator, &wallet_id, account, target_total_notes, @@ -2233,14 +2202,13 @@ pub unsafe extern "C" fn platform_wallet_manager_abandon_shielded_identity_debit let mut activity_id = [0u8; 32]; std::ptr::copy_nonoverlapping(wallet_id_bytes, wallet_id.as_mut_ptr(), 32); std::ptr::copy_nonoverlapping(activity_id_bytes, activity_id.as_mut_ptr(), 32); - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(value) => value, Err(error) => return error, }; match block_on_worker(async move { wallet .abandon_shielded_identity_debit( - &coordinator, account_index, activity_id, acknowledge_possible_execution, @@ -2277,10 +2245,8 @@ fn resolve_wallet( } } -/// Resolve both the wallet `Arc` and the network-scoped shielded -/// coordinator `Arc` for the given manager handle. Shielded -/// spend operations need the coordinator's shared store, so this -/// is the right resolver for transfer/unshield/withdraw FFIs. +/// Resolve the wallet and the manager's shielded coordinator for `handle`, +/// failing if either is missing. pub(crate) fn resolve_wallet_and_coordinator( handle: Handle, wallet_id: &[u8; 32], @@ -2322,6 +2288,17 @@ pub(crate) fn resolve_wallet_and_coordinator( Ok((wallet, coordinator)) } +/// The wallet half of [`resolve_wallet_and_coordinator`], with the same +/// errors: the wallet's shielded operations resolve the manager's coordinator +/// themselves, and this only refuses a manager without one before a caller +/// resolves the mnemonic. +pub(crate) fn resolve_shielded_wallet( + handle: Handle, + wallet_id: &[u8; 32], +) -> Result, PlatformWalletFFIResult> { + resolve_wallet_and_coordinator(handle, wallet_id).map(|(wallet, _)| wallet) +} + /// The coordinator half of [`resolve_wallet_and_coordinator`], with the same /// errors. It blocks on the runtime, so it must not run on one of its async /// workers: call it from the export's thread or from `spawn_blocking`. diff --git a/packages/rs-platform-wallet-ffi/src/shielded_sync.rs b/packages/rs-platform-wallet-ffi/src/shielded_sync.rs index 3cdf55f5e1e..7fb9c658a5f 100644 --- a/packages/rs-platform-wallet-ffi/src/shielded_sync.rs +++ b/packages/rs-platform-wallet-ffi/src/shielded_sync.rs @@ -21,6 +21,7 @@ use platform_wallet::PlatformWalletError; use crate::error::*; use crate::handle::*; use crate::runtime::{block_on_worker, runtime}; +use crate::shielded_send::resolve_shielded_wallet; use crate::shielded_types::{ ShieldedBalanceSourceFFI, ShieldedLocalAccountBalanceFFI, ShieldedLocalBalanceSnapshotFFI, ShieldedLocalBalanceStatusFFI, ShieldedSyncWalletResultFFI, @@ -435,37 +436,12 @@ pub unsafe extern "C" fn platform_wallet_manager_bind_shielded( let mut wallet_id = [0u8; 32]; std::ptr::copy_nonoverlapping(wallet_id_bytes, wallet_id.as_mut_ptr(), 32); - // Look up the wallet + the network-scoped shielded coordinator - // on the manager. The coordinator owns the single SQLite handle - // *and* the per-network sync-coordination registry; we hand it - // to the bind so the wallet reuses the shared store and - // self-registers its viewing keys for the coordinator-driven - // sync loop. - let lookup = PLATFORM_WALLET_MANAGER_STORAGE.with_item(handle, |manager| { - runtime().block_on(async { - let wallet = manager.get_wallet(&wallet_id).await; - let coordinator = manager.shielded_coordinator().await; - (wallet, coordinator) - }) - }); - let (wallet_arc, coordinator) = unwrap_option_or_return!(lookup); - let wallet_arc = match wallet_arc { - Some(w) => w, - None => { - return PlatformWalletFFIResult::err( - PlatformWalletFFIResultCode::ErrorWalletOperation, - format!("wallet not found: {}", hex::encode(wallet_id)), - ); - } - }; - let coordinator = match coordinator { - Some(c) => c, - None => { - return PlatformWalletFFIResult::err( - PlatformWalletFFIResultCode::ErrorWalletOperation, - "shielded support not configured — call platform_wallet_manager_configure_shielded first", - ); - } + // The wallet binds onto its manager's coordinator itself; this lookup + // only refuses an unconfigured manager before the host is asked for the + // mnemonic. + let wallet_arc = match resolve_shielded_wallet(handle, &wallet_id) { + Ok(wallet) => wallet, + Err(result) => return result, }; // Seedless path first: rebind from viewing keys persisted by a @@ -473,9 +449,7 @@ pub unsafe extern "C" fn platform_wallet_manager_bind_shielded( // requested account has no persisted row — only then is the // mnemonic resolved. Persisted-key load/restore errors propagate without // resolving the seed; fallback must not mask a persistence failure. - match runtime() - .block_on(wallet_arc.bind_shielded_from_persisted(accounts.as_slice(), &coordinator)) - { + match runtime().block_on(wallet_arc.bind_shielded_from_persisted(accounts.as_slice())) { Ok(true) => return PlatformWalletFFIResult::ok(), Ok(false) => {} Err(e) => { @@ -494,11 +468,8 @@ pub unsafe extern "C" fn platform_wallet_manager_bind_shielded( Err(result) => return result, }; - if let Err(e) = runtime().block_on(wallet_arc.bind_shielded( - seed.as_ref(), - accounts.as_slice(), - &coordinator, - )) { + if let Err(e) = runtime().block_on(wallet_arc.bind_shielded(seed.as_ref(), accounts.as_slice())) + { return map_shielded_error(e, "bind_shielded"); } @@ -551,30 +522,30 @@ pub unsafe extern "C" fn platform_wallet_manager_configure_shielded( } // --------------------------------------------------------------------------- -// Clear shielded state (Rust side) +// Clear shielded state (native and host) // --------------------------------------------------------------------------- -/// Reset the Rust-side shielded state on this manager: stop the -/// background sync loop, drop every wallet registration on the -/// network-scoped coordinator, and reset the caught-up cooldown -/// stamp. +/// Clear this manager's shielded state, native and host, in one call: stop +/// the background sync loop, drop every wallet registration on the +/// network-scoped coordinator, reset the shared commitment tree, the +/// per-subwallet notes and watermarks and the caught-up cooldown, and then +/// call the persistence vtable's `on_wipe_shielded_state_fn` for the +/// manager's network, before returning. No bind, account add, spend or sync +/// pass of this manager overlaps the reset or the host wipe. The host must +/// not delete shielded rows of its own around this call. /// -/// The SQLite commitment-tree file stays on disk but its contents -/// are reset to empty — Clear semantics are "wipe my shielded -/// state and cold-resync from index 0 on the shared tree". The -/// host is responsible for wiping its own per-wallet persistence -/// layer (e.g. SwiftData rows) since Rust can't reach into iOS / -/// Android persistence; after that, the next -/// [`platform_wallet_manager_bind_shielded`] call repopulates the -/// coordinator's registries and the next sync pass re-saves notes -/// via the changeset path. +/// The SQLite commitment-tree file stays on disk but its contents are reset +/// to empty; the next [`platform_wallet_manager_bind_shielded`] call +/// repopulates the coordinator's registries and the next sync pass re-saves +/// notes via the changeset path. /// -/// Returns `ErrorWalletOperation` if the Rust-side store reset -/// fails. The host **must** check this before wiping its own -/// persistence: a silent failure would leave the shared tree -/// populated while the host drops its rows, and the next cold -/// resync would gate-skip every re-downloaded position against the -/// stale tree size. +/// Refused with nothing reset when the host did not wire and declare the +/// wipe slot (`PLATFORM_WALLET_PERSISTENCE_CAPABILITY_SHIELDED_STATE_WIPE`), +/// or when a shielded operation does not finish within the Clear budget +/// (`ErrorShutdownIncomplete`-class result; retry). A failed native reset +/// returns an error before the host is called. A failed host wipe after a +/// successful reset returns an error and refuses binds until a Clear +/// succeeds. /// /// Errors with `ErrorWalletOperation` when no shielded coordinator is /// installed on this manager (the sync-loop stop still runs unconditionally @@ -1363,9 +1334,8 @@ mod fresh_address_tests { .configure_shielded(&path) .await .expect("configure shielded"); - let coordinator = manager.shielded_coordinator().await.expect("coordinator"); wallet - .bind_shielded(&[42; 64], &[0], &coordinator) + .bind_shielded(&[42; 64], &[0]) .await .expect("bind shielded"); wallet.wallet_id() @@ -1565,3 +1535,150 @@ mod fresh_address_tests { } } } + +#[cfg(test)] +mod clear_tests { + use super::*; + use crate::persistence::{ + FFIPersister, PersistenceCallbacks, PersistenceExtensionCallbacks, WipeShieldedStateFn, + }; + use crate::test_fixtures::mock_manager_with; + use crate::types::FFINetwork; + use key_wallet::wallet::initialization::WalletAccountCreationOptions; + use platform_wallet::changeset::PersistenceCapabilities; + use std::ffi::c_void; + use std::sync::atomic::{AtomicI32, AtomicUsize, Ordering}; + use std::sync::Mutex; + + /// The wipe callback's observations. One static per test process, so the + /// tests below serialize on `SERIAL`. + static WIPES: AtomicUsize = AtomicUsize::new(0); + static WIPED_NETWORK: AtomicI32 = AtomicI32::new(-1); + static WIPE_RC: AtomicI32 = AtomicI32::new(0); + static SERIAL: Mutex<()> = Mutex::new(()); + + unsafe extern "C" fn wipe(_: *mut c_void, network: FFINetwork) -> i32 { + WIPES.fetch_add(1, Ordering::SeqCst); + WIPED_NETWORK.store(network as i32, Ordering::SeqCst); + WIPE_RC.load(Ordering::SeqCst) + } + + unsafe extern "C" fn begin(_: *mut c_void, _: *const u8) -> i32 { + 0 + } + + unsafe extern "C" fn end(_: *mut c_void, _: *const u8, _: bool) -> i32 { + 0 + } + + /// A configured manager over a persister declaring the wipe capability, + /// with the wipe slot wired when `wired`. + fn configured(wired: bool) -> (Handle, PathBuf) { + let extensions = PersistenceExtensionCallbacks { + wipe_shielded_state: wired.then_some(wipe as WipeShieldedStateFn), + ..Default::default() + }; + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + on_changeset_begin_fn: Some(begin), + on_changeset_end_fn: Some(end), + ..Default::default() + }, + PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::SHIELDED_STATE_WIPE), + extensions, + ); + let manager = mock_manager_with(persister); + let path = std::env::temp_dir().join(format!( + "ffi-shielded-clear-{}-{}.sqlite", + std::process::id(), + next_handle() + )); + runtime().block_on(async { + manager + .create_wallet_from_seed_bytes( + dashcore::Network::Testnet, + &[42; 64], + WalletAccountCreationOptions::Default, + Some(0), + ) + .await + .expect("create mock wallet"); + manager + .configure_shielded(&path) + .await + .expect("configure shielded"); + }); + (PLATFORM_WALLET_MANAGER_STORAGE.insert(manager), path) + } + + fn clear_epoch(handle: Handle) -> u64 { + PLATFORM_WALLET_MANAGER_STORAGE + .with_item(handle, |manager| { + runtime().block_on(async { + manager + .shielded_coordinator() + .await + .expect("configured") + .clear_epoch() + .await + }) + }) + .expect("live handle") + } + + fn finish(handle: Handle, path: PathBuf) { + drop(PLATFORM_WALLET_MANAGER_STORAGE.remove(handle)); + let _ = std::fs::remove_file(path); + } + + #[test] + fn should_call_the_wipe_once_with_the_managers_network_before_clear_returns() { + let _serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + WIPES.store(0, Ordering::SeqCst); + WIPE_RC.store(0, Ordering::SeqCst); + let (handle, path) = configured(true); + + let result = unsafe { platform_wallet_manager_shielded_clear(handle) }; + + assert_eq!(result.code, PlatformWalletFFIResultCode::Success); + assert_eq!(WIPES.load(Ordering::SeqCst), 1); + assert_eq!( + WIPED_NETWORK.load(Ordering::SeqCst), + FFINetwork::Testnet as i32 + ); + finish(handle, path); + } + + #[test] + fn should_refuse_clear_and_reset_nothing_without_the_wipe_slot() { + let _serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + WIPES.store(0, Ordering::SeqCst); + let (handle, path) = configured(false); + let epoch = clear_epoch(handle); + + let result = unsafe { platform_wallet_manager_shielded_clear(handle) }; + + assert_ne!(result.code, PlatformWalletFFIResultCode::Success); + assert_eq!(WIPES.load(Ordering::SeqCst), 0); + assert_eq!(clear_epoch(handle), epoch, "nothing was reset"); + finish(handle, path); + } + + #[test] + fn should_fail_clear_when_the_host_wipe_fails() { + let _serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner()); + WIPES.store(0, Ordering::SeqCst); + WIPE_RC.store(-1, Ordering::SeqCst); + let (handle, path) = configured(true); + + let result = unsafe { platform_wallet_manager_shielded_clear(handle) }; + + WIPE_RC.store(0, Ordering::SeqCst); + assert_ne!(result.code, PlatformWalletFFIResultCode::Success); + assert_eq!(WIPES.load(Ordering::SeqCst), 1); + let retried = unsafe { platform_wallet_manager_shielded_clear(handle) }; + assert_eq!(retried.code, PlatformWalletFFIResultCode::Success); + finish(handle, path); + } +} diff --git a/packages/rs-platform-wallet-ffi/src/shielded_wallet_drain.rs b/packages/rs-platform-wallet-ffi/src/shielded_wallet_drain.rs index c5c0bfb3ec9..efdb8032c0b 100644 --- a/packages/rs-platform-wallet-ffi/src/shielded_wallet_drain.rs +++ b/packages/rs-platform-wallet-ffi/src/shielded_wallet_drain.rs @@ -16,7 +16,7 @@ use crate::core_wallet_types::OutPointFFI; use crate::error::{catch_panic_to_code, PlatformWalletFFIResult, PlatformWalletFFIResultCode}; use crate::handle::Handle; use crate::runtime::block_on_worker; -use crate::shielded_send::{catch_funding_panic, resolve_wallet_and_coordinator}; +use crate::shielded_send::{catch_funding_panic, resolve_shielded_wallet}; use std::sync::Arc; @@ -141,7 +141,7 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_wallet_drain_preview( let mut wallet_id = [0; 32]; std::ptr::copy_nonoverlapping(wallet_id_bytes, wallet_id.as_mut_ptr(), 32); catch_wallet_drain_read_panic("shielded wallet drain preview", || { - let (wallet, _) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(value) => value, Err(error) => return error, }; @@ -206,7 +206,7 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_fund_from_wallet_drain ) } }; - let (wallet, coordinator) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(value) => value, Err(error) => return error, }; @@ -225,7 +225,6 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_fund_from_wallet_drain }; wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::DrainWalletBalance { account_index, minimum_lock_duffs: None, @@ -276,7 +275,7 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_wallet_drain_status( std::ptr::copy_nonoverlapping(wallet_id_bytes, wallet_id.as_mut_ptr(), 32); std::ptr::copy_nonoverlapping(operation_id_bytes, operation_id.as_mut_ptr(), 16); catch_wallet_drain_read_panic("shielded wallet drain status", || { - let (wallet, _) = match resolve_wallet_and_coordinator(handle, &wallet_id) { + let wallet = match resolve_shielded_wallet(handle, &wallet_id) { Ok(value) => value, Err(error) => return error, }; diff --git a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs index fb4080cc63c..000d37498cd 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs @@ -1311,7 +1311,8 @@ impl PlatformWalletPersistence for SqlitePersister { // Do NOT attest WALLET_RESTORE (and therefore not provider restore): // token balances and the DashPay overlay have no load readers, so a // full restore remains lossy. Shielded viewing keys are native when - // compiled in; notes, nullifiers, and sync state use ShieldedStore. + // compiled in, and so is their wipe; notes, nullifiers, and sync + // state use ShieldedStore, which Clear resets itself. // WALLET_DRAIN folds in WALLET_RESTORE, so it stays unattested too. let capabilities = PersistenceCapabilities::ATOMIC_CHANGESETS .union(PersistenceCapabilities::INVITATIONS) @@ -1327,7 +1328,9 @@ impl PlatformWalletPersistence for SqlitePersister { .union(PersistenceCapabilities::DASHPAY_PAYMENTS); #[cfg(feature = "shielded")] { - capabilities.union(PersistenceCapabilities::SHIELDED_VIEWING_KEYS) + capabilities + .union(PersistenceCapabilities::SHIELDED_VIEWING_KEYS) + .union(PersistenceCapabilities::SHIELDED_STATE_WIPE) } #[cfg(not(feature = "shielded"))] { @@ -1335,6 +1338,38 @@ impl PlatformWalletPersistence for SqlitePersister { } } + /// Flushes this network's buffered writes (and those of wallets with no + /// stored row yet) under the delete's connection guard, then deletes the + /// network's viewing keys in one transaction. A flush failure fails the + /// wipe with the rows in place. + #[cfg(feature = "shielded")] + fn wipe_shielded_state(&self, network: dashcore::Network) -> Result<(), PersistenceError> { + self.ensure_writable("wipe_shielded_state") + .map_err(PersistenceError::from)?; + let mut conn = self.conn().map_err(PersistenceError::from)?; + // Other networks' buffers stay put: a failing changeset there must + // not fail this network's Clear. + let label = schema::wallets::network_to_str(network); + for wallet_id in self + .buffer + .dirty_wallets() + .map_err(PersistenceError::from)? + { + let stored = + schema::wallets::fetch(&conn, &wallet_id).map_err(PersistenceError::from)?; + if !matches!(&stored, Some((stored_network, _)) if stored_network != label) { + self.flush_locked(&mut conn, &wallet_id)?; + } + } + let tx = conn + .transaction() + .map_err(|e| PersistenceError::from(WalletStorageError::from(e)))?; + schema::shielded_viewing_keys::delete_for_network(&tx, network) + .map_err(PersistenceError::from)?; + tx.commit() + .map_err(|e| PersistenceError::from(WalletStorageError::from(e))) + } + fn persist_tracked_masternodes( &self, network: dashcore::Network, diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/shielded_viewing_keys.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/shielded_viewing_keys.rs index 5f4c54b8cde..2f343e3b495 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/shielded_viewing_keys.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/shielded_viewing_keys.rs @@ -9,7 +9,7 @@ use rusqlite::{params, Connection, Transaction}; use crate::sqlite::error::WalletStorageError; use crate::sqlite::load_ctx::{LoadCtx, LoadSite, SiteCoords}; -use crate::sqlite::schema::{blob, id32}; +use crate::sqlite::schema::{blob, id32, wallets}; const VIEWING_KEY_WIDTH: usize = 96; @@ -68,6 +68,19 @@ pub(crate) fn apply( Ok(()) } +/// Delete the viewing keys of every wallet stored on `network`. +pub(crate) fn delete_for_network( + tx: &Transaction<'_>, + network: key_wallet::Network, +) -> Result<(), WalletStorageError> { + tx.execute( + "DELETE FROM shielded_viewing_keys WHERE wallet_id IN \ + (SELECT wallet_id FROM wallets WHERE network = ?1)", + params![wallets::network_to_str(network)], + )?; + Ok(()) +} + /// Read every persisted subwallet viewing key. /// /// # Errors diff --git a/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs b/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs index 29f3622b039..2f0670a0dac 100644 --- a/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs +++ b/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs @@ -8,7 +8,7 @@ use platform_wallet::changeset::{ ShieldedChangeSet, }; use platform_wallet::wallet::shielded::SubwalletId; -use platform_wallet_storage::WalletStorageError; +use platform_wallet_storage::{SqlitePersister, WalletStorageError}; fn changeset(id: SubwalletId, viewing_key: [u8; 96]) -> PlatformWalletChangeSet { let mut shielded = ShieldedChangeSet::default(); @@ -122,3 +122,100 @@ fn delete_wallet_cascades_shielded_viewing_keys() { .unwrap(); assert_eq!(remaining, 0); } + +fn viewing_key_count(path: &std::path::Path, wallet_id: &[u8; 32]) -> i64 { + common::ro_conn(path) + .query_row( + "SELECT COUNT(*) FROM shielded_viewing_keys WHERE wallet_id = ?1", + rusqlite::params![wallet_id.as_slice()], + |row| row.get(0), + ) + .unwrap() +} + +fn insert_mainnet_wallet(persister: &SqlitePersister, wallet_id: &[u8; 32]) { + persister + .lock_conn_for_test() + .execute( + "INSERT INTO wallets (wallet_id, network, birth_height) VALUES (?1, 'mainnet', 0)", + rusqlite::params![wallet_id.as_slice()], + ) + .unwrap(); +} + +#[test] +fn shielded_wipe_drops_the_networks_viewing_keys_and_keeps_other_networks() { + let (persister, _tmp, path) = fresh_persister(); + let testnet_wallet = wid(0x71); + let mainnet_wallet = wid(0x72); + ensure_wallet_meta(&persister, &testnet_wallet); + insert_mainnet_wallet(&persister, &mainnet_wallet); + for wallet in [testnet_wallet, mainnet_wallet] { + persister + .store(wallet, changeset(SubwalletId::new(wallet, 0), [0xE1; 96])) + .expect("store viewing key"); + } + + assert!(persister + .persistence_capabilities() + .contains(PersistenceCapabilities::SHIELDED_STATE_WIPE)); + persister + .wipe_shielded_state(dashcore::Network::Testnet) + .expect("wipe testnet"); + + assert_eq!(viewing_key_count(&path, &testnet_wallet), 0); + assert_eq!(viewing_key_count(&path, &mainnet_wallet), 1); +} + +#[test] +fn shielded_wipe_flushes_buffered_viewing_keys_before_deleting() { + let (persister, _tmp, path) = + common::fresh_persister_with_mode(platform_wallet_storage::FlushMode::Manual); + let wallet = wid(0x73); + ensure_wallet_meta(&persister, &wallet); + persister + .store(wallet, changeset(SubwalletId::new(wallet, 0), [0xE2; 96])) + .expect("buffer viewing key"); + + persister + .wipe_shielded_state(dashcore::Network::Testnet) + .expect("wipe testnet"); + persister.commit_writes().expect("flush after wipe"); + + assert_eq!(viewing_key_count(&path, &wallet), 0); +} + +#[test] +fn shielded_wipe_leaves_other_networks_buffered_writes_unflushed() { + let (persister, _tmp, path) = + common::fresh_persister_with_mode(platform_wallet_storage::FlushMode::Manual); + let testnet_wallet = wid(0x74); + let mainnet_wallet = wid(0x75); + ensure_wallet_meta(&persister, &testnet_wallet); + insert_mainnet_wallet(&persister, &mainnet_wallet); + persister + .store( + mainnet_wallet, + changeset(SubwalletId::new(mainnet_wallet, 0), [0xE3; 96]), + ) + .expect("buffer the mainnet viewing key"); + persister.force_next_flush_to_fail(WalletStorageError::Sqlite(rusqlite::Error::SqliteFailure( + rusqlite::ffi::Error { + code: rusqlite::ErrorCode::DatabaseBusy, + extended_code: rusqlite::ffi::SQLITE_BUSY, + }, + Some("database is busy".into()), + ))); + + persister + .wipe_shielded_state(dashcore::Network::Testnet) + .expect("a failing mainnet changeset does not fail the testnet wipe"); + + let report = persister.commit_writes().expect("commit report"); + assert_eq!( + report.failed.len(), + 1, + "the wipe never flushed the mainnet wallet, so the primed failure is still armed" + ); + assert_eq!(viewing_key_count(&path, &mainnet_wallet), 0); +} diff --git a/packages/rs-platform-wallet/examples/shielded_sync.rs b/packages/rs-platform-wallet/examples/shielded_sync.rs index 5abfc7d8706..f9c528a3aca 100644 --- a/packages/rs-platform-wallet/examples/shielded_sync.rs +++ b/packages/rs-platform-wallet/examples/shielded_sync.rs @@ -260,7 +260,7 @@ async fn run_wallet_sync_test(wallet: WalletIndex) { .await .expect("shielded_coordinator must exist after configure_shielded"); platform_wallet - .bind_shielded(&shielded_seed, &[0u32], &coordinator) + .bind_shielded(&shielded_seed, &[0u32]) .await .expect("bind_shielded"); @@ -303,7 +303,7 @@ async fn run_wallet_sync_test(wallet: WalletIndex) { // informationally. We bound account 0 only, so we expect exactly // one entry. --- let balances = platform_wallet - .shielded_balances(&coordinator) + .shielded_balances() .await .expect("shielded_balances"); let total_balance: u64 = balances.values().sum(); diff --git a/packages/rs-platform-wallet/examples/shielded_sync_paloma.rs b/packages/rs-platform-wallet/examples/shielded_sync_paloma.rs index 364569a58fa..37602b6a3a1 100644 --- a/packages/rs-platform-wallet/examples/shielded_sync_paloma.rs +++ b/packages/rs-platform-wallet/examples/shielded_sync_paloma.rs @@ -253,7 +253,7 @@ async fn main() { .await .expect("shielded_coordinator must exist after configure_shielded"); platform_wallet - .bind_shielded(&SEED_A, &[0u32], &coordinator) + .bind_shielded(&SEED_A, &[0u32]) .await .expect("bind_shielded"); @@ -288,7 +288,7 @@ async fn main() { ); let balances = platform_wallet - .shielded_balances(&coordinator) + .shielded_balances() .await .expect("shielded_balances"); let total_balance: u64 = balances.values().sum(); diff --git a/packages/rs-platform-wallet/src/changeset/persistence_capabilities.rs b/packages/rs-platform-wallet/src/changeset/persistence_capabilities.rs index 55c549c015b..37d93d7aa55 100644 --- a/packages/rs-platform-wallet/src/changeset/persistence_capabilities.rs +++ b/packages/rs-platform-wallet/src/changeset/persistence_capabilities.rs @@ -150,13 +150,19 @@ impl PersistenceCapabilities { /// is marked. pub const PRIVATE_CONTACT_REQUEST_SCAN_STATE: Self = Self(1 << 15); + /// The backend implements + /// [`wipe_shielded_state`](super::PlatformWalletPersistence::wipe_shielded_state). + /// A shielded Clear requires it and refuses before resetting anything + /// without it. + pub const SHIELDED_STATE_WIPE: Self = Self(1 << 16); + /// Index of the highest bit declared above. It lives here, beside the /// constants, so adding a bit and bumping this is one edit in one place /// — and `every_declared_bit_has_a_stable_name` walks up to it, so a new /// bit that never reaches `KNOWN` fails a test instead of gating /// behaviour invisibly. The same test asserts nothing above it is named, /// which is what catches a bit added without bumping this. - const HIGHEST_DECLARED_BIT: u32 = 15; + const HIGHEST_DECLARED_BIT: u32 = 16; /// Capabilities required before exporting and funding an invitation voucher. pub const INVITATION_CREATION: Self = Self( @@ -278,6 +284,10 @@ impl PersistenceCapabilities { PersistenceCapabilities::PRIVATE_CONTACT_REQUEST_SCAN_STATE, "private_contact_request_scan_state", ), + ( + PersistenceCapabilities::SHIELDED_STATE_WIPE, + "shielded_state_wipe", + ), ]; KNOWN @@ -295,7 +305,7 @@ impl PersistenceCapabilities { /// failure the test exists to catch. Written as a module-level `const _` so /// it is evaluated in every build, test or not. const _: () = assert!( - PersistenceCapabilities::PRIVATE_CONTACT_REQUEST_SCAN_STATE.bits() + PersistenceCapabilities::SHIELDED_STATE_WIPE.bits() == 1u64 << PersistenceCapabilities::HIGHEST_DECLARED_BIT, "HIGHEST_DECLARED_BIT must name the highest declared capability bit" ); @@ -335,6 +345,7 @@ mod tests { PersistenceCapabilities::PRIVATE_CONTACT_REQUEST_SCAN_STATE.bits(), 0x8000 ); + assert_eq!(PersistenceCapabilities::SHIELDED_STATE_WIPE.bits(), 0x10000); assert_eq!(PersistenceCapabilities::WALLET_DRAIN.bits(), 0x2285); assert_eq!( PersistenceCapabilities::ASSET_LOCK_RECONCILIATION.bits(), diff --git a/packages/rs-platform-wallet/src/changeset/traits.rs b/packages/rs-platform-wallet/src/changeset/traits.rs index 326820934c4..5048c454744 100644 --- a/packages/rs-platform-wallet/src/changeset/traits.rs +++ b/packages/rs-platform-wallet/src/changeset/traits.rs @@ -420,6 +420,30 @@ pub trait PlatformWalletPersistence: Send + Sync { Ok(()) } + /// Delete every shielded row this backend keeps for `network`: the + /// viewing keys, notes, outgoing notes, sync watermarks and activity of + /// every wallet it holds on that network, whether or not the wallet is + /// loaded. One atomic deletion: it completes, or it fails with the rows + /// in place. + /// + /// Called only by a shielded Clear, after the native reset and before + /// the Clear returns, while every other shielded operation on the + /// network is held out (see + /// [`PersistenceCapabilities::SHIELDED_STATE_WIPE`], which a backend + /// implementing this attests). A host deletes nothing of its own around + /// the Clear call. + /// + /// Default: fails, and the capability is not attested, so a Clear + /// refuses before it resets anything. The same reentrancy contract as + /// [`Self::store`] applies. + #[cfg(feature = "shielded")] + fn wipe_shielded_state(&self, network: dashcore::Network) -> Result<(), PersistenceError> { + let _ = network; + Err(PersistenceError::backend( + "this persistence backend cannot wipe shielded state", + )) + } + /// Load the persisted tracked-masternode set for `network`. Default: /// empty (see [`Self::persist_tracked_masternodes`]). fn load_tracked_masternodes( diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index db5da2bd8ec..81cf1b731bd 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -558,6 +558,8 @@ impl PlatformWalletManager

{ broadcaster, Arc::clone(&self.sync_fault), Arc::clone(&self.durable_cursors), + #[cfg(feature = "shielded")] + Arc::clone(&self.shielded_coordinator), ); // Initialize the platform-address provider. If the snapshot diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index b074deb07ed..41a8d9548d8 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -34,6 +34,8 @@ use dpp::prelude::Identifier; use key_wallet::wallet::ManagedWalletInfo; use key_wallet_manager::WalletManager; +#[cfg(feature = "shielded")] +use crate::changeset::PersistenceCapabilities; use crate::changeset::{ persists_backfill_coverage, spawn_wallet_event_adapter_with_durable_cursors, CoreChangeSet, DashPayBackfillRecord, DurableCursor, DurableCursors, PlatformWalletChangeSet, @@ -54,7 +56,7 @@ use crate::wallet::core::{BalanceUpdateHandler, SpendObservationHandler}; use crate::wallet::identity::network::DashPayPaymentHandler; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; #[cfg(feature = "shielded")] -use crate::wallet::shielded::NetworkShieldedCoordinator; +use crate::wallet::shielded::{NetworkShieldedCoordinator, ShieldedCoordinatorSlot}; use crate::wallet::PlatformWallet; /// Registry key identifying each background worker the manager joins at @@ -453,8 +455,7 @@ pub struct PlatformWalletManager { /// 4 deletes `ShieldedWallet` outright and the coordinator /// owns the spend surface too. #[cfg(feature = "shielded")] - pub(super) shielded_coordinator: - Arc>>>, + pub(super) shielded_coordinator: ShieldedCoordinatorSlot, /// Shared `PlatformEventManager` — held on the manager so /// `configure_shielded` can install a per-chunk progress handler /// onto the freshly-created `NetworkShieldedCoordinator` that @@ -677,9 +678,7 @@ impl PlatformWalletManager

{ Arc::clone(&event_manager), )); #[cfg(feature = "shielded")] - let shielded_coordinator: Arc< - RwLock>>, - > = Arc::new(RwLock::new(None)); + let shielded_coordinator: ShieldedCoordinatorSlot = Arc::new(RwLock::new(None)); #[cfg(feature = "shielded")] let shielded_sync = Arc::new(ShieldedSyncManager::new( Arc::clone(&event_manager), @@ -1035,7 +1034,7 @@ impl PlatformWalletManager

{ { continue; } - if let Err(error) = wallet.restore_private_contact_payments(coordinator).await { + if let Err(error) = wallet.restore_private_contact_payments().await { tracing::warn!( wallet_id = %hex::encode(wallet.wallet_id()), %error, @@ -1127,42 +1126,40 @@ impl PlatformWalletManager

{ .map(Arc::clone) } - /// Tear down shielded sync state for a Clear / wipe flow. - /// - /// Single library entry point so the FFI stays a one-call bridge: - /// first **quiesce** the sync manager (cancel the loop *and* drain - /// any in-flight pass, including its persister-callback fan-out, so - /// nothing can re-persist notes after this returns), then **clear** - /// the network coordinator's per-subwallet registries AND reset the - /// on-disk commitment-tree SQLite store. The per-network - /// commitment-tree SQLite file stays on disk but its contents are - /// reset to empty so the next bind cold-resyncs from index 0. - /// - /// # The missing-coordinator case is an ERROR, not a silent no-op + /// Clear this network's shielded state, native and host, in one call. /// - /// Returning `Ok(())` when `shielded_coordinator()` is `None` — treating - /// "no coordinator" as "nothing to clear" — masks a real on-device - /// failure: the host taps Clear on a manager whose coordinator is **not - /// installed on this instance** — e.g. an SDK rebuild handed the host a - /// fresh `PlatformWalletManager` whose `configure_shielded` never ran (or - /// ran on a different instance than the one currently syncing). The - /// quiesce runs (sync loop stops), the call returns `Ok`, and the host - /// then wipes its own Room/SwiftData rows — while the **on-disk - /// commitment tree is never touched** (file mtime unchanged on device, no - /// `reset_commitment_tree` call). The next bind reloads the still-full - /// tree + its persisted watermark and re-freezes everything. + /// Quiesces the sync manager (cancelling the loop and draining any + /// in-flight pass, persister fan-out included), resets the coordinator's + /// registries and its commitment-tree store, then wipes the host's + /// shielded rows for the network through this manager's persister + /// ([`PlatformWalletPersistence::wipe_shielded_state`]), all under the + /// coordinator's exclusive admission (see `NetworkShieldedCoordinator::clear`). + /// The host deletes nothing itself around this call. The tree file stays + /// on disk, emptied, so the next bind resyncs from index 0. /// - /// The FFI only exposes this call behind a bound, shielded-enabled host - /// surface (the "Clear" button), so reaching it with no coordinator is a - /// genuine wiring fault, not a benign "shielded was never used" case. - /// Returning an error makes the host fail closed (keep its rows) and - /// surfaces the real problem instead of a phantom success. + /// Refused, with nothing stopped or reset, when the persister does not + /// attest [`PersistenceCapabilities::SHIELDED_STATE_WIPE`]. /// - /// Returns an error if the coordinator is absent, or if the coordinator's - /// store reset (which resets the on-disk tree to 0 leaves and verifies it) - /// fails; the host must not commit its own persistence wipe in that case. + /// A missing coordinator is an error, not a no-op: reaching Clear on a + /// manager whose `configure_shielded` never ran is a wiring fault, and a + /// silent success would leave the on-disk tree and its watermark in + /// place for the next bind to reload. #[cfg(feature = "shielded")] pub async fn clear_shielded(&self) -> Result<(), crate::error::PlatformWalletError> { + // Fail closed before anything is stopped or reset: the host half of + // the Clear runs through this persister, and one that cannot wipe + // its shielded rows would keep the very state the reset drops. + let required = PersistenceCapabilities::SHIELDED_STATE_WIPE; + let capabilities = self.persister.persistence_capabilities(); + if !capabilities.contains(required) { + let missing = capabilities.missing(required); + return Err(PlatformWalletError::Persistence(format!( + "shielded clear requires persistence capabilities {:?} (missing mask 0x{:x}); \ + nothing was reset", + missing.names(), + missing.bits(), + ))); + } // Hold the registry's per-key clearing latch across the WHOLE // quiesce -> wipe. While it is up, `ShieldedSyncManager::start` // (and any registry (re)start) is a no-op, so no fresh pass can @@ -1188,7 +1185,7 @@ impl PlatformWalletManager

{ )); }; match self.shielded_coordinator().await { - Some(coord) => coord.clear().await, + Some(coord) => coord.clear(&*self.persister).await, None => { tracing::error!( "clear_shielded: no shielded coordinator installed on this manager — the \ @@ -1595,6 +1592,8 @@ mod tests { Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), crate::changeset::DurableCursors::default(), + #[cfg(feature = "shielded")] + Arc::clone(&mgr.shielded_coordinator), )); mgr.wallets.rcu(|wallets| { let mut next = std::collections::BTreeMap::clone(wallets); diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 466c6f5af35..ae9f5443dde 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -704,6 +704,8 @@ impl PlatformWalletManager

{ broadcaster, Arc::clone(&self.sync_fault), Arc::clone(&self.durable_cursors), + #[cfg(feature = "shielded")] + Arc::clone(&self.shielded_coordinator), ); // Restore the platform-address provider from the slice read above — diff --git a/packages/rs-platform-wallet/src/test_support.rs b/packages/rs-platform-wallet/src/test_support.rs index edcf03a0194..2a6e91d204b 100644 --- a/packages/rs-platform-wallet/src/test_support.rs +++ b/packages/rs-platform-wallet/src/test_support.rs @@ -5,6 +5,8 @@ //! and `wallet::asset_lock::build`. use std::collections::BTreeMap; +#[cfg(all(test, feature = "shielded"))] +use std::sync::atomic::AtomicUsize; #[cfg(test)] use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -43,6 +45,10 @@ use tokio::sync::Notify; use crate::broadcaster::SpvBroadcaster; #[cfg(test)] use crate::broadcaster::{BroadcastError, TransactionBroadcaster}; +#[cfg(all(test, feature = "shielded"))] +use crate::changeset::{ + ClientStartState, PersistenceCapabilities, PersistenceError, PlatformWalletChangeSet, +}; #[cfg(test)] use crate::changeset::{DurableCursors, PlatformWalletPersistence}; #[cfg(test)] @@ -958,6 +964,8 @@ pub(crate) async fn platform_wallet_in( Arc::new(SpvBroadcaster::new(spv)), Arc::new(AtomicBool::new(false)), DurableCursors::default(), + #[cfg(feature = "shielded")] + Default::default(), ) } @@ -1076,3 +1084,47 @@ pub(crate) mod tracing_capture { } } } + +/// A host that stores nothing and attests +/// [`PersistenceCapabilities::SHIELDED_STATE_WIPE`], counting the wipes a +/// shielded Clear asks of it. +#[cfg(all(test, feature = "shielded"))] +#[derive(Default)] +pub(crate) struct WipeCountingPersistence { + wipes: AtomicUsize, +} + +#[cfg(all(test, feature = "shielded"))] +impl WipeCountingPersistence { + pub(crate) fn wipe_count(&self) -> usize { + self.wipes.load(Ordering::SeqCst) + } +} + +#[cfg(all(test, feature = "shielded"))] +impl PlatformWalletPersistence for WipeCountingPersistence { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Ok(()) + } + + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + + fn persistence_capabilities(&self) -> PersistenceCapabilities { + PersistenceCapabilities::SHIELDED_STATE_WIPE + } + + fn wipe_shielded_state(&self, _network: Network) -> Result<(), PersistenceError> { + self.wipes.fetch_add(1, Ordering::SeqCst); + Ok(()) + } +} diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs index b4f4813facf..d88d3dd53db 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs @@ -821,6 +821,8 @@ mod tests { Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), crate::changeset::DurableCursors::default(), + #[cfg(feature = "shielded")] + Default::default(), )) } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs index 3425470a24a..0fe4219980e 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs @@ -3559,7 +3559,7 @@ mod tests { .await; fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); let persister = fixture.wallet.persister(); @@ -3641,7 +3641,7 @@ mod tests { .await; fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); let persister = fixture.wallet.persister(); @@ -3672,7 +3672,7 @@ mod tests { // request is tracked: in memory only, as its flush was lost. fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); assert_eq!(fixture.journaled().await, 1, "kept for the next process"); @@ -3709,7 +3709,7 @@ mod tests { .await; fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); publish(&fixture.wallet, 0, vec![entry.to_vec()]).await; @@ -3798,7 +3798,7 @@ mod tests { .await; let coordinator = manager.shielded_coordinator().await.expect("configured"); restarted - .restore_private_contact_payments(&coordinator) + .restore_private_contact_payments() .await .expect("restored again"); assert!( @@ -3916,7 +3916,7 @@ mod tests { .await; fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); publish(&fixture.wallet, 0, vec![entry.to_vec()]).await; @@ -3974,7 +3974,7 @@ mod tests { .await; fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); let journal = fixture.journal_snapshot().await; @@ -4004,7 +4004,7 @@ mod tests { } fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); assert_eq!( @@ -4018,7 +4018,7 @@ mod tests { .store(false, Ordering::SeqCst); fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); assert_eq!(fixture.journaled().await, 0, "discharged once flushed"); @@ -4030,7 +4030,7 @@ mod tests { .await; fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); publish(&fixture.wallet, 0, vec![entry.to_vec()]).await; @@ -4047,7 +4047,7 @@ mod tests { .expect("the entry is read back"); fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); assert_eq!( @@ -4061,7 +4061,7 @@ mod tests { .store(false, Ordering::SeqCst); fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); assert_eq!( @@ -4077,7 +4077,7 @@ mod tests { .await; fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); publish(&fixture.wallet, 0, vec![entry.to_vec()]).await; @@ -4098,7 +4098,7 @@ mod tests { .read_back_awaiting_flush(&journal_commitment(&fixture).await)); fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); assert_eq!( @@ -4116,7 +4116,7 @@ mod tests { .store(true, Ordering::SeqCst); fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); assert_eq!(fixture.journaled().await, 1, "the buffer was lost"); @@ -4162,7 +4162,7 @@ mod tests { assert_eq!(fixture.journaled().await, 1); fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); assert_eq!( @@ -5189,7 +5189,7 @@ mod tests { // record, then a second restoration. coordinator.sync(true, &CancellationToken::new()).await; restarted - .restore_private_contact_payments(&coordinator) + .restore_private_contact_payments() .await .expect("restored again from the journal"); } @@ -9606,15 +9606,13 @@ mod tests { let bob = identity(BOB_PHRASE, BOB); let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); let alice_signer = signer(ALICE_PHRASE, &alice); - let (_manager, alice_wallet) = wallet(ALICE_PHRASE, &alice, &[&alice, &bob], vec![]).await; + let (manager, alice_wallet) = wallet(ALICE_PHRASE, &alice, &[&alice, &bob], vec![]).await; let dir = tempfile::tempdir().expect("temporary shielded store"); - let db_path = dir.path().join("tree.sqlite"); - let coordinator = Arc::new(NetworkShieldedCoordinator::new( - Arc::new(dash_sdk::Sdk::new_mock()), - Network::Testnet, - db_path.clone(), - FileBackedShieldedStore::open_path(&db_path, 100).expect("the store"), - )); + manager + .configure_shielded(dir.path().join("tree.sqlite")) + .await + .expect("the shielded store"); + let coordinator = manager.shielded_coordinator().await.expect("configured"); let subwallet = SubwalletId::new(alice_wallet.wallet_id(), 0); let paid = Arc::new(Mutex::new(Vec::new())); let pay = { @@ -9675,7 +9673,7 @@ mod tests { let mut cx = Context::from_waker(Waker::noop()); let held_store = coordinator.store().write().await; - let mut restore = Box::pin(alice_wallet.restore_private_contact_payments(&coordinator)); + let mut restore = Box::pin(alice_wallet.restore_private_contact_payments()); assert!( restore.as_mut().poll(&mut cx).is_pending(), "waits for the store" @@ -9761,9 +9759,7 @@ mod tests { let mut cx = Context::from_waker(Waker::noop()); let held_store = fixture.coordinator.store().write().await; - let mut restore = Box::pin( - wallet.restore_pending_private_contact_requests(&fixture.coordinator, &alice_crypto), - ); + let mut restore = Box::pin(wallet.restore_pending_private_contact_requests(&alice_crypto)); assert!( restore.as_mut().poll(&mut cx).is_pending(), "waits for the store" @@ -9966,16 +9962,15 @@ mod tests { .await; let dir = tempfile::tempdir().expect("temporary shielded store"); let db_path = dir.path().join("tree.sqlite"); - let coordinator = Arc::new(NetworkShieldedCoordinator::new( - Arc::clone(&wallet.identity().sdk), - Network::Testnet, - db_path.clone(), - FileBackedShieldedStore::open_path(&db_path, 100).expect("the store"), - )); + manager + .configure_shielded(&db_path) + .await + .expect("the shielded store"); + let coordinator = manager.shielded_coordinator().await.expect("configured"); // Bound, the coordinator holds the wallet's persister, on which it // notes the discharge. wallet - .bind_shielded(&seed(ALICE_PHRASE), &[0], &coordinator) + .bind_shielded(&seed(ALICE_PHRASE), &[0]) .await .expect("bound"); let subwallet = SubwalletId::new(wallet.wallet_id(), 0); @@ -10482,7 +10477,7 @@ mod tests { // Discharged from that read-back by a restoration. fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); assert!(fixture @@ -11073,7 +11068,7 @@ mod tests { // submission or a restoration notes it. fixture .wallet - .restore_private_contact_payments(&fixture.coordinator) + .restore_private_contact_payments() .await .expect("restored"); let persister = fixture.wallet.persister(); @@ -11959,7 +11954,7 @@ mod tests { let bob = identity(BOB_PHRASE, BOB); let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); let alice_signer = signer(ALICE_PHRASE, &alice); - let (_manager, wallet) = wallet_on( + let (manager, wallet) = wallet_on( RecordingPersistence::attesting( PersistenceCapabilities::PRIVATE_CONTACT_SCAN_STATE_RESTART, ), @@ -11979,13 +11974,11 @@ mod tests { [], ) .expect("the record damaged"); - let coordinator = Arc::new(NetworkShieldedCoordinator::new( - Arc::clone(&wallet.identity().sdk), - Network::Testnet, - path.clone(), - FileBackedShieldedStore::open_path(&path, 100).expect("the store"), - )); - let failed = wallet.restore_private_contact_payments(&coordinator).await; + manager + .configure_shielded(&path) + .await + .expect("the shielded store"); + let failed = wallet.restore_private_contact_payments().await; assert!(failed.is_err(), "{failed:?}"); let view = wallet.identity().dashpay(); let readiness = view @@ -12364,13 +12357,16 @@ mod tests { assert!(exited.is_err(), "the process exits during the wait"); drop(before); - let after = coordinator_at(db_path); - let (_restarted_manager, restarted) = + let (restarted_manager, restarted) = wallet(ALICE_PHRASE, &alice, &[&alice, &bob], vec![]).await; assert_eq!(restarted.wallet_id(), alice_wallet.wallet_id()); assert!(!pending_to(&restarted, ALICE, &bob).await); + restarted_manager + .configure_shielded(&db_path) + .await + .expect("the shielded store"); let restored = restarted - .restore_pending_private_contact_requests(&after, &alice_crypto) + .restore_pending_private_contact_requests(&alice_crypto) .await .expect("restored from the saved record"); assert_eq!(restored, vec![(alice.id(), bob.id())]); @@ -12835,7 +12831,6 @@ mod tests { // The restart: the reply is not in the store and the host restores // nothing; only the bind can. - let after = coordinator_at(db_path); let restarted_manager = PlatformWalletManager::new( Arc::new(dashpay_sdk(&[&alice, &bob], vec![], PlatformVersion::latest(), None).await), kept, @@ -12890,8 +12885,16 @@ mod tests { "{public:?}" ); + restarted_manager + .configure_shielded(&db_path) + .await + .expect("the shielded store"); + let after = restarted_manager + .shielded_coordinator() + .await + .expect("configured"); restarted - .bind_shielded(&seed(BOB_PHRASE), &[0], &after) + .bind_shielded(&seed(BOB_PHRASE), &[0]) .await .expect("bound"); assert!( @@ -12906,7 +12909,7 @@ mod tests { let scan = after.store().write().await; tokio::time::timeout( Duration::from_secs(5), - restarted.bind_shielded(&seed(BOB_PHRASE), &[0], &after), + restarted.bind_shielded(&seed(BOB_PHRASE), &[0]), ) .await .expect("the rebind does not wait for the store") @@ -12978,7 +12981,7 @@ mod tests { // Read back and recorded durably, its journal record is discharged: // the next restoration finds none, and the pair stays free. restarted - .restore_private_contact_payments(&after) + .restore_private_contact_payments() .await .expect("restored"); assert!( diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index 8a7edc96b0b..77fd8b1f38b 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -49,6 +49,7 @@ use { super::identity::network::{ dashpay_contract, restore_journaled_private_payments, JournaledPrivatePayment, }, + super::shielded::coordinator::ShieldedAdmission, super::shielded::operations::{ discharge_read_back_private_payments, private_document_create_from_pool_with_metadata, }, @@ -56,7 +57,8 @@ use { PendingRedrive, PrivateContactPayment, PrivatePaymentEntry, PrivatePaymentRecord, }, super::shielded::{ - FileBackedShieldedStore, NetworkShieldedCoordinator, OrchardKeySet, ShieldedStore, + FileBackedShieldedStore, NetworkShieldedCoordinator, OrchardKeySet, + ShieldedCoordinatorSlot, ShieldedStore, }, crate::{ AutoAcceptProofSource, ContactCryptoProvider, ContactRequest, EstablishedContact, @@ -78,12 +80,11 @@ pub type WalletId = [u8; 32]; /// What a private contact request reply is paid with, which /// [`PlatformWallet::accept_contact_request_replying_in_kind`] asks for only -/// when the reply is private: the shielded coordinator, the seed the spend -/// authority is derived from (dropped once it is), the shielded account and -/// the prover. +/// when the reply is private: the seed the spend authority is derived from +/// (dropped once it is), the shielded account and the prover. The shielded +/// coordinator is the wallet's manager's. #[cfg(feature = "shielded")] pub struct PrivateReplyPayment { - pub coordinator: Arc, pub seed: Seed, pub account: u32, pub prover: P, @@ -453,6 +454,11 @@ pub struct PlatformWallet { /// handles share them. #[cfg(feature = "shielded")] private_payment_restores: Arc>, + /// The manager's shielded coordinator slot, the only source of the + /// coordinator this wallet's shielded operations run on + /// ([`Self::shielded_coordinator`]). + #[cfg(feature = "shielded")] + shielded_coordinator: ShieldedCoordinatorSlot, } impl PlatformWallet { @@ -752,6 +758,7 @@ impl PlatformWallet { broadcaster: Arc, sync_fault: Arc, durable_cursors: crate::changeset::DurableCursors, + #[cfg(feature = "shielded")] shielded_coordinator: ShieldedCoordinatorSlot, ) -> Self { // Build the per-wallet persister handle once and share it with // the sub-wallets that need to queue their own changesets @@ -837,6 +844,8 @@ impl PlatformWallet { shielded_detached: Arc::new(std::sync::atomic::AtomicBool::new(false)), #[cfg(feature = "shielded")] private_payment_restores: Arc::default(), + #[cfg(feature = "shielded")] + shielded_coordinator, } } @@ -871,7 +880,6 @@ impl PlatformWallet { &self, seed: &[u8], accounts: &[u32], - coordinator: &Arc, ) -> Result<(), PlatformWalletError> { use super::shielded::{AccountViewingKeys, OrchardKeySet, SubwalletId}; let required = crate::changeset::PersistenceCapabilities::SHIELDED_FVK_RESTART; @@ -894,9 +902,16 @@ impl PlatformWallet { // dropped must not persist viewing keys the host can no longer // delete (see `ensure_shielded_attached`). self.ensure_shielded_attached()?; - // Sampled before the load below so the install can tell that the - // snapshot predates a Clear. - let snapshot_generation = coordinator.clear_generation(); + // Admitted before the host snapshot is read and held through the + // viewing-key write and the install: a Clear either finished, host + // wipe included, before the load, or waits until this bind is done. + // `shield_guard` first (the lock order): registration is serialized + // with identity-shield proof construction, and a bind queued behind + // a long shield holds no admission that would stall a Clear. + let _shield_guard = self.shield_guard.lock().await; + let coordinator = self.shielded_coordinator().await?; + let admission = coordinator.admit().await; + admission.ensure_host_cleared()?; let network = self.sdk.network; let mut account_views: std::collections::BTreeMap = std::collections::BTreeMap::new(); @@ -966,7 +981,7 @@ impl PlatformWallet { // because the loop above proved the derived keys equal the // persisted ones: the rows the restore filters on are unchanged // by that write. - self.install_shielded_views(account_views, coordinator, start, snapshot_generation) + self.install_shielded_views(&coordinator, &admission, account_views, start) .await } @@ -987,7 +1002,6 @@ impl PlatformWallet { pub async fn bind_shielded_from_persisted( &self, accounts: &[u32], - coordinator: &Arc, ) -> Result { use super::shielded::{AccountViewingKeys, SubwalletId}; let required = crate::changeset::PersistenceCapabilities::SHIELDED_FVK_RESTART; @@ -1007,9 +1021,12 @@ impl PlatformWallet { )); } self.ensure_shielded_attached()?; - // Sampled before the load so the install can tell that the - // snapshot predates a Clear. - let snapshot_generation = coordinator.clear_generation(); + // Guarded and admitted before the host snapshot is read, as + // `bind_shielded` is. + let _shield_guard = self.shield_guard.lock().await; + let coordinator = self.shielded_coordinator().await?; + let admission = coordinator.admit().await; + admission.ensure_host_cleared()?; let start = self.persister.load().map_err(|e| { PlatformWalletError::ShieldedBuildError(format!( "persister load failed while rebinding shielded viewing keys: {e}" @@ -1032,7 +1049,7 @@ impl PlatformWallet { } // Hand the already-loaded snapshot to the install step so the // restore doesn't pay a second full persister load. - self.install_shielded_views(account_views, coordinator, start, snapshot_generation) + self.install_shielded_views(&coordinator, &admission, account_views, start) .await?; Ok(true) } @@ -1058,18 +1075,13 @@ impl PlatformWallet { #[cfg(feature = "shielded")] async fn install_shielded_views( &self, + coordinator: &Arc, + admission: &ShieldedAdmission<'_>, account_views: std::collections::BTreeMap, - coordinator: &Arc, start: crate::changeset::ClientStartState, - snapshot_generation: u64, ) -> Result<(), PlatformWalletError> { - self.install_shielded_views_in_one_transaction( - account_views, - coordinator, - start, - snapshot_generation, - ) - .await?; + self.install_shielded_views_in_one_transaction(admission, account_views, start) + .await?; // After the install transaction: the restore takes the store and // wallet locks on its own. self.restore_private_payments_or_defer(coordinator).await; @@ -1136,15 +1148,14 @@ impl PlatformWallet { #[cfg(feature = "shielded")] async fn install_shielded_views_in_one_transaction( &self, + admission: &ShieldedAdmission<'_>, account_views: std::collections::BTreeMap, - coordinator: &Arc, start: crate::changeset::ClientStartState, - snapshot_generation: u64, ) -> Result<(), PlatformWalletError> { - // Serialize registration with identity-shield proof construction. Otherwise - // an account could disappear before that operation arms its durable guard. - let _shield_guard = self.shield_guard.lock().await; - let install = coordinator.begin_install(self.wallet_id).await; + // The caller holds `shield_guard`, serializing registration with + // identity-shield proof construction. Otherwise an account could + // disappear before that operation arms its durable guard. + let install = admission.begin_install(self.wallet_id).await; // A wallet the manager has already removed must not be able to // re-register itself. Callers reach a bind through an @@ -1227,24 +1238,9 @@ impl PlatformWallet { // the host and leave hydration unset: an empty first-launch // snapshot succeeds, while an unreadable ledger must not look // like a successfully bound zero balance. A subsequent bind - // retries rather than fast-pathing over an unhydrated store. - // A Clear that completed after `start` was read wiped both the - // store and (once it returned) the host's own rows, so this - // snapshot describes state the user asked to be deleted. - // Restoring it would put the notes back and re-arm the pre-Clear - // watermark, which reports caught-up and suppresses the cold - // rebuild Clear promises. Register (done above) but restore - // nothing, and leave hydration unset so the next bind hydrates - // from the host's post-Clear rows. - if install.snapshot_predates_clear(snapshot_generation) { - install.mark_hydrated(false).await; - tracing::info!( - wallet_id = %hex::encode(self.wallet_id), - "Skipped shielded snapshot restore: shielded state was cleared while binding" - ); - return Ok(()); - } - + // retries rather than fast-pathing over an unhydrated store. The + // admission held since before `start` was read guarantees no Clear + // ran in between, so `start` describes the store's current epoch. match install.restore(&start.shielded).await { Ok(()) => install.mark_hydrated(true).await, Err(e) => { @@ -1289,6 +1285,13 @@ impl PlatformWallet { ))); } self.ensure_shielded_attached()?; + // Admitted for the whole read-check-write-install span, as a bind + // is: no Clear lands between the snapshot read and the key's + // installation, and none completes with this key's row still to be + // written. + let coordinator = self.shielded_coordinator().await?; + let admission = coordinator.admit().await; + admission.ensure_host_cleared()?; // Everything that calls into the host — the snapshot read below // and the viewing-key write further down — stays OUTSIDE the key // slot's lock. A host callback invoked while this write guard is @@ -1411,6 +1414,34 @@ impl PlatformWallet { .store(true, std::sync::atomic::Ordering::Release); } + /// The manager's shielded coordinator, the only one this wallet's + /// shielded operations run on. `ShieldedStoreError` until the manager's + /// `configure_shielded` has run. + #[cfg(feature = "shielded")] + pub(crate) async fn shielded_coordinator( + &self, + ) -> Result, PlatformWalletError> { + self.shielded_coordinator + .read() + .await + .clone() + .ok_or_else(|| { + PlatformWalletError::ShieldedStoreError( + "shielded support is not configured on this wallet manager".to_string(), + ) + }) + } + + /// Point a wallet built outside a manager at `coordinator`, as the + /// manager's `configure_shielded` does for the wallets it owns. + #[cfg(all(test, feature = "shielded"))] + pub(crate) async fn attach_shielded_coordinator_for_test( + &self, + coordinator: &Arc, + ) { + *self.shielded_coordinator.write().await = Some(Arc::clone(coordinator)); + } + /// Bound ZIP-32 account indices on the shielded sub-wallet, /// in ascending order. Empty if not bound. #[cfg(feature = "shielded")] @@ -1495,9 +1526,11 @@ impl PlatformWallet { #[cfg(feature = "shielded")] pub async fn shielded_balances( &self, - coordinator: &Arc, ) -> Result, PlatformWalletError> { use super::shielded::{AccountViewingKeys, SubwalletId}; + let Ok(coordinator) = self.shielded_coordinator().await else { + return Ok(std::collections::BTreeMap::new()); + }; let guard = self.shielded_keys.read().await; let Some(keys) = guard.as_ref() else { return Ok(std::collections::BTreeMap::new()); @@ -1561,8 +1594,10 @@ impl PlatformWallet { /// notes to `recipient_raw_43` (the recipient's Orchard payment /// address as the 43 raw bytes). /// - /// `coordinator` supplies the shared, network-scoped - /// commitment-tree store; `seed` supplies the spend authority — + /// The manager's coordinator supplies the shared, network-scoped + /// commitment-tree store, admitted for the whole spend so a Clear never + /// lands between note selection and the spend's writes; `seed` supplies + /// the spend authority — /// the full `OrchardKeySet` (with the `SpendAuthorizingKey`) is /// re-derived from it for this call only and dropped on return. /// Privilege separation: the ASK never crosses to the @@ -1579,7 +1614,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] pub async fn shielded_transfer_to( &self, - coordinator: &Arc, seed: &[u8], account: u32, recipient_raw_43: &[u8; 43], @@ -1587,6 +1621,8 @@ impl PlatformWallet { memo: [u8; 36], prover: P, ) -> Result<(), PlatformWalletError> { + let coordinator = self.shielded_coordinator().await?; + let _admission = coordinator.admit().await; let keyset = self.derive_spend_keyset(seed, account).await?; let recipient = Option::::from( grovedb_commitment_tree::PaymentAddress::from_raw_address_bytes(recipient_raw_43), @@ -1621,13 +1657,14 @@ impl PlatformWallet { #[cfg(feature = "shielded")] pub async fn shielded_unshield_to( &self, - coordinator: &Arc, seed: &[u8], account: u32, to_platform_addr_bech32m: &str, amount: u64, prover: P, ) -> Result<(), PlatformWalletError> { + let coordinator = self.shielded_coordinator().await?; + let _admission = coordinator.admit().await; let keyset = self.derive_spend_keyset(seed, account).await?; // The decoder is network-agnostic, so guard the recipient's HRP class // against the wallet's network before decoding. @@ -1659,7 +1696,6 @@ impl PlatformWallet { #[cfg(feature = "shielded")] pub async fn shielded_identity_top_up_from_pool( &self, - coordinator: &Arc, seed: &[u8], account: u32, identity_id: &Identifier, @@ -1674,8 +1710,10 @@ impl PlatformWallet { // two top-ups (or a top-up and a shield-from-identity debit) whose // waits completed out of execution order would let the older balance // overwrite the newer one. Held across build -> broadcast -> wait -> - // reconcile. + // reconcile, and taken before admission (the lock order). let _shield_guard = self.shield_guard.lock().await; + let coordinator = self.shielded_coordinator().await?; + let _admission = coordinator.admit().await; let keyset = self.derive_spend_keyset(seed, account).await?; let (mut proven_balance, metadata) = @@ -1726,7 +1764,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] pub async fn send_private_contact_request( &self, - coordinator: &Arc, seed: &[u8], account: u32, sender_identity_id: &Identifier, @@ -1756,7 +1793,6 @@ impl PlatformWallet { crypto, |entry, started| { self.pay_private_contact_request( - coordinator, keyset, account, sender_identity_id, @@ -1780,7 +1816,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] pub async fn accept_private_contact_request( &self, - coordinator: &Arc, seed: &[u8], account: u32, request: &ContactRequest, @@ -1804,7 +1839,6 @@ impl PlatformWallet { crypto, |entry, started| { self.pay_private_contact_request( - coordinator, keyset, account, &request.recipient_id, @@ -1821,10 +1855,10 @@ impl PlatformWallet { /// Accept a DashPay contact request replying the way it came: privately, /// paid from a shielded account, when it came privately and no reply /// exists yet, else with a `contactRequest` document signed by `signer`. - /// `payment` gives what a private reply is paid with (the shielded - /// coordinator, the seed for the spend authority, the account and the - /// prover) and is called only for a private reply, so a public reply or - /// an adopted one needs neither shielded support nor the seed. See + /// `payment` gives what a private reply is paid with (the seed for the + /// spend authority, the account and the prover) and is called only for + /// a private reply, so a public reply or an adopted one needs neither + /// shielded support nor the seed. See /// [`DashPayView::accept_contact_request_replying_in_kind`](crate::DashPayView::accept_contact_request_replying_in_kind). #[cfg(feature = "shielded")] pub async fn accept_contact_request_replying_in_kind( @@ -1856,7 +1890,6 @@ impl PlatformWallet { // first, before `started` is marked: a reply dropped // meanwhile releases its reservation. let PrivateReplyPayment { - coordinator, seed, account, prover, @@ -1864,7 +1897,6 @@ impl PlatformWallet { let keyset = self.derive_spend_keyset(seed.as_ref(), account).await?; drop(seed); self.pay_private_contact_request( - &coordinator, keyset, account, &request.recipient_id, @@ -1896,15 +1928,12 @@ impl PlatformWallet { #[cfg(feature = "shielded")] pub async fn restore_pending_private_contact_requests( &self, - coordinator: &Arc, crypto: &C, ) -> Result, PlatformWalletError> where C: ContactCryptoProvider + Sync, { - let entries = self - .unconfirmed_private_contact_requests(coordinator) - .await?; + let entries = self.unconfirmed_private_contact_requests().await?; self.identity .dashpay() .restore_pending_private_contact_requests(&entries, crypto) @@ -1951,9 +1980,9 @@ impl PlatformWallet { #[cfg(feature = "shielded")] pub async fn restore_private_contact_payments( &self, - coordinator: &Arc, ) -> Result, PlatformWalletError> { - self.private_payment_restore(coordinator) + let coordinator = self.shielded_coordinator().await?; + self.private_payment_restore(&coordinator) .restore_and_release(true) .await .map(Option::unwrap_or_default) @@ -2036,8 +2065,8 @@ impl PlatformWallet { #[cfg(feature = "shielded")] async fn unconfirmed_private_contact_requests( &self, - coordinator: &Arc, ) -> Result, PlatformWalletError> { + let coordinator = self.shielded_coordinator().await?; let dashpay_id = dashpay_contract()?.id(); let store = coordinator.store().read().await; let redrives = store @@ -2072,7 +2101,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] async fn pay_private_contact_request

( &self, - coordinator: &Arc, keyset: OrchardKeySet, account: u32, sender_identity_id: &Identifier, @@ -2084,6 +2112,11 @@ impl PlatformWallet { where P: OrchardProver, { + // Admitted for the spend, before the generation's gate (the shielded + // admission is the outermost lock), but only once the host has + // signed: a signing prompt never holds a Clear back. + let coordinator = self.shielded_coordinator().await?; + let _admission = coordinator.admit().await; // The generation's lifecycle gate, held across the liveness check, // the restore and the spend, before the wallet manager lock as the // teardown takes them: a removal cannot complete in between, so a @@ -2163,7 +2196,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] pub async fn shielded_withdraw_to( &self, - coordinator: &Arc, seed: &[u8], account: u32, to_core_address: &str, @@ -2171,6 +2203,8 @@ impl PlatformWallet { core_fee_per_byte: u32, prover: P, ) -> Result<(), PlatformWalletError> { + let coordinator = self.shielded_coordinator().await?; + let _admission = coordinator.admit().await; let keyset = self.derive_spend_keyset(seed, account).await?; let network = self.sdk.network; let parsed = to_core_address @@ -2221,7 +2255,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] pub async fn shielded_identity_create_from_pool( &self, - coordinator: &Arc, seed: &[u8], account: u32, identity_index: u32, @@ -2238,6 +2271,8 @@ impl PlatformWallet { P: dpp::shielded::builder::OrchardProver, IS: dpp::identity::signer::Signer + Send + Sync, { + let coordinator = self.shielded_coordinator().await?; + let _admission = coordinator.admit().await; let (identity_id, identity) = { // Scope the transient keyset so its spend authority is dropped before we take the // wallet-manager write lock below — it's only needed for the spend, not for the @@ -2407,7 +2442,6 @@ impl PlatformWallet { #[cfg(feature = "shielded")] pub async fn shielded_shield_from_account( &self, - coordinator: &Arc, shielded_account: u32, payment_account: u32, amount: u64, @@ -2419,7 +2453,6 @@ impl PlatformWallet { P: dpp::shielded::builder::OrchardProver, { self.shielded_shield_from_account_impl( - coordinator, shielded_account, payment_account, None, @@ -2456,7 +2489,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] pub async fn shielded_shield_from_account_to_recipient( &self, - coordinator: &Arc, shielded_account: u32, payment_account: u32, recipient_raw_43: &[u8; 43], @@ -2478,7 +2510,6 @@ impl PlatformWallet { ) })?; self.shielded_shield_from_account_impl( - coordinator, shielded_account, payment_account, Some(recipient), @@ -2496,7 +2527,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] async fn shielded_shield_from_account_impl( &self, - coordinator: &Arc, shielded_account: u32, payment_account: u32, recipient: Option, @@ -2520,8 +2550,11 @@ impl PlatformWallet { // Single-flight: serialize shield-class ops on this wallet so // two concurrent calls can't fetch + build with the same // address nonce (the second would be rejected as a replay after - // a ~30 s proof). Held across selection → build → broadcast. + // a ~30 s proof). Held across selection → build → broadcast, and + // taken before admission (the lock order). let _shield_guard = self.shield_guard.lock().await; + let coordinator = self.shielded_coordinator().await?; + let _admission = coordinator.admit().await; // Planning and amount selection are shared with the cached preflight. // The helper drops the wallet-manager read lock before the expensive @@ -2576,14 +2609,16 @@ impl PlatformWallet { #[cfg(feature = "shielded")] pub async fn abandon_shielded_identity_debit( &self, - coordinator: &Arc, account_index: u32, activity_id: [u8; 32], acknowledge_possible_execution: bool, ) -> Result<(), PlatformWalletError> { let _shield_guard = self.shield_guard.lock().await; + let coordinator = self.shielded_coordinator().await?; + let admission = coordinator.admit().await; coordinator - .abandon_identity_debit( + .abandon_identity_debit_admitted( + &admission, self.wallet_id(), account_index, activity_id, @@ -2613,7 +2648,6 @@ impl PlatformWallet { #[cfg(feature = "shielded")] pub async fn shielded_shield_from_identity( &self, - coordinator: &Arc, shielded_account: u32, identity_id: &Identifier, amount: u64, @@ -2625,7 +2659,6 @@ impl PlatformWallet { P: dpp::shielded::builder::OrchardProver, { self.shielded_shield_from_identity_impl( - coordinator, shielded_account, None, identity_id, @@ -2646,7 +2679,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] pub async fn shielded_shield_from_identity_to_recipient( &self, - coordinator: &Arc, shielded_account: u32, identity_id: &Identifier, recipient_raw_43: &[u8; 43], @@ -2668,7 +2700,6 @@ impl PlatformWallet { ) })?; self.shielded_shield_from_identity_impl( - coordinator, shielded_account, Some(recipient), identity_id, @@ -2684,7 +2715,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] async fn shielded_shield_from_identity_impl( &self, - coordinator: &Arc, shielded_account: u32, recipient: Option, identity_id: &Identifier, @@ -2705,7 +2735,10 @@ impl PlatformWallet { // Single-flight with the address-funded shields: the identity nonce is // fetched inside the operation, and two concurrent builds would race it. + // Taken before admission (the lock order). let _shield_guard = self.shield_guard.lock().await; + let coordinator = self.shielded_coordinator().await?; + let _admission = coordinator.admit().await; let identity = { let wm = self.wallet_manager.read().await; @@ -2909,6 +2942,8 @@ impl Clone for PlatformWallet { shielded_detached: self.shielded_detached.clone(), #[cfg(feature = "shielded")] private_payment_restores: self.private_payment_restores.clone(), + #[cfg(feature = "shielded")] + shielded_coordinator: self.shielded_coordinator.clone(), } } } @@ -3630,10 +3665,6 @@ mod private_contact_request_payment_tests { } } - fn coordinator_at(dir: &std::path::Path) -> Arc { - coordinator_on(dir, dashcore::Network::Testnet) - } - fn coordinator_on( dir: &std::path::Path, network: dashcore::Network, @@ -3675,16 +3706,25 @@ mod private_contact_request_payment_tests { .expect("wallet") } + /// Configure `manager`'s shielded store under `dir` and return its + /// coordinator. + async fn configure( + manager: &PlatformWalletManager, + dir: &std::path::Path, + ) -> Arc { + manager + .configure_shielded(dir.join("tree.sqlite")) + .await + .expect("the shielded store"); + manager.shielded_coordinator().await.expect("configured") + } + /// Pay for a private contact request from `wallet`'s account 0. - async fn pay( - wallet: &PlatformWallet, - coordinator: &Arc, - ) -> Result { + async fn pay(wallet: &PlatformWallet) -> Result { let keyset = OrchardKeySet::from_seed(&SEED, dashcore::Network::Testnet, 0).expect("keyset"); wallet .pay_private_contact_request( - coordinator, keyset, 0, &Identifier::from([0xA1; 32]), @@ -3702,11 +3742,11 @@ mod private_contact_request_payment_tests { #[tokio::test] async fn should_refuse_a_private_payment_from_a_wallet_no_longer_current() { let dir = tempfile::tempdir().expect("temporary shielded store"); - let coordinator = coordinator_at(dir.path()); let (manager, removed) = manager_with_wallet().await; + configure(&manager, dir.path()).await; let wallet_id = removed.wallet_id(); manager.remove_wallet(&wallet_id).await.expect("removed"); - let refused = pay(&removed, &coordinator).await; + let refused = pay(&removed).await; assert!( matches!(&refused, Err(PlatformWalletError::WalletRemovedBeforeSend(id)) if *id == hex::encode(wallet_id)), @@ -3715,7 +3755,7 @@ mod private_contact_request_payment_tests { let recreated = add_wallet(&manager).await; assert_eq!(recreated.wallet_id(), wallet_id); - let refused = pay(&removed, &coordinator).await; + let refused = pay(&removed).await; assert!( matches!( refused, @@ -3723,7 +3763,7 @@ mod private_contact_request_payment_tests { ), "{refused:?}" ); - let current = pay(&recreated, &coordinator).await; + let current = pay(&recreated).await; assert!( !matches!( current, @@ -3738,15 +3778,14 @@ mod private_contact_request_payment_tests { #[tokio::test] async fn should_hold_a_wallets_removal_until_its_private_payment_returns() { let dir = tempfile::tempdir().expect("temporary shielded store"); - let coordinator = coordinator_at(dir.path()); let (manager, wallet) = manager_with_wallet().await; + let coordinator = configure(&manager, dir.path()).await; let wallet_id = wallet.wallet_id(); let store = coordinator.store().write().await; let payment = { let wallet = Arc::clone(&wallet); - let coordinator = Arc::clone(&coordinator); - tokio::spawn(async move { pay(&wallet, &coordinator).await }) + tokio::spawn(async move { pay(&wallet).await }) }; tokio::time::sleep(Duration::from_millis(100)).await; let removal = { @@ -3780,14 +3819,13 @@ mod private_contact_request_payment_tests { #[tokio::test] async fn should_not_hold_a_removal_behind_a_restoration_s_store_wait() { let dir = tempfile::tempdir().expect("temporary shielded store"); - let (_manager, wallet) = manager_with_wallet().await; - let coordinator = coordinator_on(dir.path(), wallet.sdk.network); + let (manager, wallet) = manager_with_wallet().await; + let coordinator = configure(&manager, dir.path()).await; let store = coordinator.store().write().await; let restore = { let wallet = Arc::clone(&wallet); - let coordinator = Arc::clone(&coordinator); - tokio::spawn(async move { wallet.restore_private_contact_payments(&coordinator).await }) + tokio::spawn(async move { wallet.restore_private_contact_payments().await }) }; tokio::time::sleep(Duration::from_millis(100)).await; assert!(!restore.is_finished(), "the restoration waits at the store"); diff --git a/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs index b9700306b2e..71ab6f9add2 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs @@ -169,7 +169,10 @@ async fn local_balance_distinguishes_availability_and_explicit_empty_scan() { scanned.accounts[&2].source, ShieldedBalanceSource::ScannedThisSession ); - coordinator.clear().await.unwrap(); + coordinator + .clear(&crate::test_support::WipeCountingPersistence::default()) + .await + .unwrap(); assert_eq!( coordinator.local_balance_snapshot(wallet).await.unwrap(), ShieldedLocalBalanceState::Unbound @@ -291,7 +294,10 @@ async fn local_balance_recovers_after_failed_clear_and_completed_scan() { let conn = rusqlite::Connection::open(&path).unwrap(); conn.execute_batch("ALTER TABLE shielded_pending_spends RENAME TO unavailable_pending_spends;") .unwrap(); - assert!(coordinator.clear().await.is_err()); + assert!(coordinator + .clear(&crate::test_support::WipeCountingPersistence::default()) + .await + .is_err()); conn.execute_batch("ALTER TABLE unavailable_pending_spends RENAME TO shielded_pending_spends;") .unwrap(); drop(conn); @@ -335,7 +341,10 @@ async fn local_balance_unhydrated_requires_scan_coverage_for_every_account() { .set_last_synced_note_index(SubwalletId::new(wallet, 1), 0) .unwrap(); assert_eq!(ready(&coordinator, wallet).await.accounts.len(), 2); - coordinator.clear().await.unwrap(); + coordinator + .clear(&crate::test_support::WipeCountingPersistence::default()) + .await + .unwrap(); register(&coordinator, wallet, &[0, 1]).await; assert_eq!( coordinator.local_balance_snapshot(wallet).await.unwrap(), @@ -506,7 +515,8 @@ async fn local_balance_waits_for_atomic_store_and_lifecycle_updates() { ShieldedBalanceSource::ScannedThisSession ); - let install = coordinator.begin_install(wallet).await; + let admission = coordinator.admit().await; + let install = admission.begin_install(wallet).await; let mut read = Box::pin(coordinator.local_balance_snapshot(wallet)); assert!(matches!(futures::poll!(&mut read), Poll::Pending)); assert!( diff --git a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs index e224586420b..287261391d7 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -95,8 +95,10 @@ use super::store::{ StalePendingSpend, SubwalletId, }; use super::CAUGHT_UP_COOLDOWN; +use crate::changeset::PlatformWalletPersistence; use crate::error::PlatformWalletError; use crate::manager::shielded_sync::{ShieldedSyncPassSummary, WalletShieldedOutcome}; +use crate::manager::COORDINATOR_DRAIN_BUDGET; use crate::wallet::identity::network::dashpay_contract; use crate::wallet::persister::WalletPersister; use crate::wallet::platform_wallet::WalletId; @@ -216,7 +218,7 @@ pub struct NetworkShieldedCoordinator { /// Serializes every shielded lifecycle TRANSACTION on this /// coordinator: a wallet's whole bind install (see - /// [`begin_install`](Self::begin_install)), + /// [`ShieldedAdmission::begin_install`]), /// [`unregister_wallet`](Self::unregister_wallet), and /// [`clear`](Self::clear). /// @@ -266,28 +268,100 @@ pub struct NetworkShieldedCoordinator { /// account set. hydrated: RwLock>, - /// Counts completed [`clear`](Self::clear) calls, so a bind can tell - /// that the host snapshot it loaded predates a wipe. + /// The shielded admission: the one shared/exclusive lock every durable + /// shielded mutation and [`clear`](Self::clear) agree on. See + /// [`admit`](Self::admit) for the protocol. /// - /// A bind reads the host's persisted snapshot BEFORE it opens its - /// install transaction — it has to, because the seedless path - /// reconstructs the viewing keys from that snapshot and the - /// seed-backed path checks its derived keys against it, and neither - /// may run a host callback under the lifecycle mutex. So the mutex - /// alone cannot stop a Clear from landing in between: the bind would - /// then restore pre-Clear notes and the pre-Clear watermark into the - /// store the Clear just purged, and the host — which wipes its own - /// rows only after `clear()` returns — would see its shielded - /// history come back, with a watermark that reports caught-up so the - /// promised cold rebuild from index 0 never runs. Sampling this - /// counter before the load and re-reading it inside the transaction - /// makes that snapshot detectably stale. - clear_generation: std::sync::atomic::AtomicU64, + /// Shared ([`ShieldedAdmission`]): a bind (host snapshot load, + /// viewing-key persistence, install), an account add, a spend from its + /// note selection to its last store and host write, a sync pass, an + /// identity-debit abandonment. Exclusive: Clear, across the Rust reset, + /// the host wipe and the epoch bump. So a Clear cannot land inside any of + /// those spans, and none can start until Clear, host wipe included, has + /// finished: no snapshot loaded before a Clear and no viewing-key write + /// begun before it completes after the wipe. + /// + /// The guarded value is the Clear state: how many Clears completed (the + /// epoch) and whether the last one reset Rust but could not wipe the host. + /// + /// Lock order: wallet `shield_guard` → `admission` → `lifecycle` → + /// `store`. `shield_guard` comes first so an operation queued behind a + /// long shield-class operation (a funding's ChainLock wait) holds no + /// admission that would stall a Clear. tokio's `RwLock` is + /// write-preferring, so a holder must never take it again (a queued + /// Clear would deadlock the nested read). + admission: RwLock, +} + +/// What the [admission](NetworkShieldedCoordinator::admit) lock guards. +#[derive(Debug, Default, Clone, Copy)] +struct ClearState { + /// Completed Clears on this coordinator, successful or not: each one + /// reset Rust state the store had before it. + epoch: u64, + /// The last Clear reset the Rust store but the host wipe failed, so the + /// host may still hold the rows the reset dropped. Binds and account + /// adds are refused until a Clear completes both halves: restoring + /// those rows is exactly what Clear exists to prevent. + host_wipe_pending: bool, +} + +/// Shared admission to durable shielded state, from +/// [`NetworkShieldedCoordinator::admit`]. While one is alive, no Clear runs. +/// +/// Hold it across the whole span whose writes must not outlive a Clear, and +/// never take a second one while holding it (see the `admission` field doc). +pub(crate) struct ShieldedAdmission<'a> { + coordinator: &'a NetworkShieldedCoordinator, + state: tokio::sync::RwLockReadGuard<'a, ClearState>, +} + +impl ShieldedAdmission<'_> { + /// `Err` when the last Clear reset Rust but did not wipe the host, so a + /// host snapshot may still describe cleared state. Binds and account adds + /// check it before they read or write host rows. + pub(crate) fn ensure_host_cleared(&self) -> Result<(), PlatformWalletError> { + if self.state.host_wipe_pending { + return Err(PlatformWalletError::ShieldedStoreError( + "the last shielded Clear reset native state but did not wipe host storage; \ + retry Clear before binding" + .to_string(), + )); + } + Ok(()) + } + + /// Open an install transaction for `wallet_id`, taking the coordinator's + /// lifecycle mutex for the returned guard's lifetime. + /// + /// A bind's steps — compare the incoming registration, replace it, + /// restore the host snapshot, commit hydration — only compose into the + /// intended "replace this wallet's shielded state" operation if nothing + /// else mutates the registries in between; see the `lifecycle` field + /// doc for the interleavings this forbids. Reached only through an + /// admission, so an install transaction always runs inside one. + /// + /// Every other lifecycle entry point — the single-step methods + /// ([`register_wallet`](NetworkShieldedCoordinator::register_wallet), + /// [`is_hydrated`](NetworkShieldedCoordinator::is_hydrated), + /// [`mark_hydrated`](NetworkShieldedCoordinator::mark_hydrated), + /// [`wallet_registration_matches`](NetworkShieldedCoordinator::wallet_registration_matches), + /// [`restore_for_wallet`](NetworkShieldedCoordinator::restore_for_wallet)), + /// [`unregister_wallet`](NetworkShieldedCoordinator::unregister_wallet) + /// and Clear — takes this same non-reentrant mutex, so a caller holding a + /// guard must go through the guard instead of calling any of them. + pub(crate) async fn begin_install(&self, wallet_id: WalletId) -> ShieldedInstall<'_> { + ShieldedInstall { + coordinator: self.coordinator, + wallet_id, + _lifecycle: self.coordinator.lifecycle.lock().await, + } + } } /// Exclusive handle on one wallet's shielded install transaction, /// returned by -/// [`begin_install`](NetworkShieldedCoordinator::begin_install). +/// [`begin_install`](ShieldedAdmission::begin_install). /// /// Holds the coordinator's lifecycle mutex for as long as it lives, so /// every step taken through it — registration comparison, registration @@ -305,7 +379,7 @@ pub struct NetworkShieldedCoordinator { /// temporary scopes make that easy to write by accident (both operands /// of an `&&` share one scope, so a guard built in the left operand is /// still alive while the right one runs). Keeping the type and -/// [`begin_install`](NetworkShieldedCoordinator::begin_install) inside +/// [`begin_install`](ShieldedAdmission::begin_install) inside /// the crate confines that hazard to code reviewed alongside the /// coordinator. pub(crate) struct ShieldedInstall<'a> { @@ -349,17 +423,6 @@ impl ShieldedInstall<'_> { }) } - /// Whether a host snapshot taken at `generation` (from - /// [`clear_generation`](NetworkShieldedCoordinator::clear_generation), - /// sampled before the load) predates a completed Clear. - /// - /// Such a snapshot describes state the user asked to be wiped — - /// restoring it would put the notes and the watermark back while the - /// host's own rows are gone. See the `clear_generation` field doc. - pub fn snapshot_predates_clear(&self, generation: u64) -> bool { - self.coordinator.clear_generation() != generation - } - /// See [`register_wallet`](NetworkShieldedCoordinator::register_wallet). pub async fn register( &self, @@ -421,17 +484,41 @@ impl NetworkShieldedCoordinator { tree_progress_handler: std::sync::Mutex::new(None), lifecycle: tokio::sync::Mutex::new(()), hydrated: RwLock::new(std::collections::BTreeSet::new()), - clear_generation: std::sync::atomic::AtomicU64::new(0), + admission: RwLock::new(ClearState::default()), + } + } + + /// Shared admission to durable shielded state: the one protocol every + /// durable shielded mutation follows against [`clear`](Self::clear). + /// + /// An operation whose durable writes derive from state it read earlier — + /// a bind's host snapshot and viewing keys, a spend's selected notes, a + /// sync pass's scan — holds the returned admission from before that read + /// until its last write. Clear takes the same lock exclusively for the + /// Rust reset, the host wipe and the epoch bump, so such an operation + /// either completes before Clear starts, or starts after Clear (host wipe + /// included) has finished and sees only post-Clear state. + /// + /// Not reentrant: never call it, or any entry point that takes it, while + /// holding one. + pub(crate) async fn admit(&self) -> ShieldedAdmission<'_> { + ShieldedAdmission { + coordinator: self, + state: self.admission.read().await, } } - /// Snapshot of the clear counter, to be taken **before** reading the - /// host's persisted state and handed to - /// [`ShieldedInstall::snapshot_predates_clear`] inside the install - /// transaction. See the `clear_generation` field doc. - pub fn clear_generation(&self) -> u64 { - self.clear_generation - .load(std::sync::atomic::Ordering::Acquire) + /// Whether a Clear holds the admission or is queued for it: either way + /// no new admission is granted until it finishes. + #[cfg(test)] + pub(crate) fn admission_is_closed(&self) -> bool { + self.admission.try_read().is_err() + } + + /// Bumped by every Clear that reached the native reset, whether or not + /// the reset succeeded. + pub async fn clear_epoch(&self) -> u64 { + self.admission.read().await.epoch } /// Network this coordinator is pinned to. Used by hosts that @@ -552,10 +639,14 @@ impl NetworkShieldedCoordinator { account_views: BTreeMap, persister: WalletPersister, ) -> Result<(), PlatformWalletError> { - self.begin_install(wallet_id) + let admission = self.admit().await; + admission.ensure_host_cleared()?; + let result = admission + .begin_install(wallet_id) .await .register(account_views, persister) - .await + .await; + result } /// List active and explicitly abandoned identity debits, including those @@ -610,12 +701,34 @@ impl NetworkShieldedCoordinator { account_index: u32, activity_id: [u8; 32], acknowledge_possible_execution: bool, + ) -> Result<(), PlatformWalletError> { + let admission = self.admit().await; + self.abandon_identity_debit_admitted( + &admission, + wallet_id, + account_index, + activity_id, + acknowledge_possible_execution, + ) + .await + } + + /// [`abandon_identity_debit`](Self::abandon_identity_debit) inside an + /// admission the caller already holds, for a caller that must take its + /// own locks between the admission and this call. + pub(crate) async fn abandon_identity_debit_admitted( + &self, + admission: &ShieldedAdmission<'_>, + wallet_id: WalletId, + account_index: u32, + activity_id: [u8; 32], + acknowledge_possible_execution: bool, ) -> Result<(), PlatformWalletError> { if !acknowledge_possible_execution { return Err(PlatformWalletError::InvalidParameter( "recovery requires acknowledgement that the original payment may already have executed or could still execute".to_owned())); } - let _lifecycle = self.lifecycle.lock().await; + let _install = admission.begin_install(wallet_id).await; let persister = self.persisters.read().await.get(&wallet_id).cloned(); let id = SubwalletId::new(wallet_id, account_index); let mut store = self.store.write().await; @@ -648,37 +761,6 @@ impl NetworkShieldedCoordinator { Ok(()) } - /// Open an install transaction for `wallet_id`, taking the - /// coordinator's lifecycle mutex for the returned guard's lifetime. - /// - /// A bind's steps — compare the incoming registration, replace it, - /// restore the host snapshot, commit hydration — only compose into - /// the intended "replace this wallet's shielded state" operation if - /// nothing else mutates the registries in between; see the - /// `lifecycle` field doc for the interleavings this forbids. - /// - /// Every other lifecycle entry point — the single-step methods - /// ([`register_wallet`](Self::register_wallet), - /// [`is_hydrated`](Self::is_hydrated), - /// [`mark_hydrated`](Self::mark_hydrated), - /// [`wallet_registration_matches`](Self::wallet_registration_matches), - /// [`restore_for_wallet`](Self::restore_for_wallet)), - /// [`unregister_wallet`](Self::unregister_wallet) and - /// [`clear`](Self::clear) — takes this same non-reentrant mutex, so - /// a caller holding a guard must go through the guard instead of - /// calling any of them. - /// - /// Note that the single-step methods therefore cost a mutex - /// acquisition, not just the registry read they appear to be: an - /// install holds the mutex across a whole snapshot restore. - pub(crate) async fn begin_install(&self, wallet_id: WalletId) -> ShieldedInstall<'_> { - ShieldedInstall { - coordinator: self, - wallet_id, - _lifecycle: self.lifecycle.lock().await, - } - } - /// [`register_wallet`](Self::register_wallet)'s body, with the /// lifecycle mutex already held by the caller's transaction. async fn register_locked( @@ -820,7 +902,12 @@ impl NetworkShieldedCoordinator { /// successfully hydrated from the host snapshot this session /// (see [`mark_hydrated`](Self::mark_hydrated)). pub async fn is_hydrated(&self, wallet_id: WalletId) -> bool { - self.begin_install(wallet_id).await.is_hydrated().await + self.admit() + .await + .begin_install(wallet_id) + .await + .is_hydrated() + .await } async fn is_hydrated_locked(&self, wallet_id: WalletId) -> bool { @@ -835,7 +922,9 @@ impl NetworkShieldedCoordinator { /// re-bind retries the restore instead of taking the idempotent /// fast path on top of an unhydrated store. pub async fn mark_hydrated(&self, wallet_id: WalletId, hydrated: bool) { - self.begin_install(wallet_id) + self.admit() + .await + .begin_install(wallet_id) .await .mark_hydrated(hydrated) .await @@ -873,7 +962,9 @@ impl NetworkShieldedCoordinator { wallet_id: WalletId, account_views: &BTreeMap, ) -> bool { - self.begin_install(wallet_id) + self.admit() + .await + .begin_install(wallet_id) .await .registration_matches(account_views) .await @@ -949,7 +1040,8 @@ impl NetworkShieldedCoordinator { wallet_id: WalletId, ) -> Result { loop { - let install = self.begin_install(wallet_id).await; + let admission = self.admit().await; + let install = admission.begin_install(wallet_id).await; let account_ids: Vec = self .accounts .read() @@ -965,6 +1057,7 @@ impl NetworkShieldedCoordinator { // A network scan can hold the store writer for minutes. Do not // make store-free lifecycle operations wait behind this read. drop(install); + drop(admission); // Wait only for readiness, then drop the temporary store guard // BEFORE reacquiring lifecycle. Keeping it would invert the // lifecycle -> store order used by Clear/bind and deadlock. @@ -1037,7 +1130,14 @@ impl NetworkShieldedCoordinator { wallet_id: WalletId, snapshot: &crate::changeset::ShieldedSyncStartState, ) -> Result<(), crate::error::PlatformWalletError> { - self.begin_install(wallet_id).await.restore(snapshot).await + let admission = self.admit().await; + admission.ensure_host_cleared()?; + let result = admission + .begin_install(wallet_id) + .await + .restore(snapshot) + .await; + result } /// [`restore_for_wallet`](Self::restore_for_wallet)'s body, with the @@ -1172,76 +1272,79 @@ impl NetworkShieldedCoordinator { Ok(()) } - /// Drop every wallet registration, purge all per-subwallet - /// store state (notes, spent marks, sync watermarks, - /// nullifier checkpoints), empty the shared commitment tree, - /// and reset the cooldown stamp. The single SQLite handle stays - /// open — Clear semantics on the host side are "wipe my - /// persistence and cold-rebuild from index 0", not "blow away - /// the SQLite file". + /// Clear every wallet's shielded state on this network, Rust and host + /// together: drop every wallet registration, purge all per-subwallet + /// store state (notes, spent marks, sync watermarks, nullifier + /// checkpoints), empty the shared commitment tree, reset the cooldown + /// stamp, and then have `host` delete its own shielded rows for this + /// network ([`PlatformWalletPersistence::wipe_shielded_state`]). The + /// single SQLite handle stays open — Clear semantics are "wipe the + /// persistence and cold-rebuild from index 0", not "blow away the + /// SQLite file". /// /// Purging the in-memory `subwallets` store is what actually /// delivers the "re-sync from index 0" contract: the sync /// pass derives `already_have` from each subwallet's /// `last_synced_note_index`, so leaving stale watermarks /// behind would make a same-session re-bind report - /// caught-up and never re-emit notes to the host (it would - /// only work after a process restart that drops the - /// in-memory state). Clearing it here closes that gap. + /// caught-up and never re-emit notes to the host. /// /// Emptying the commitment tree is what keeps the two reset /// halves coherent. The watermark rewinds to 0 but the tree's /// append gate is `tree_size`, so a tree left at its full /// (~1M-leaf) size would gate-skip every re-downloaded position - /// (`global_pos < tree_size`) — nothing new appends, the - /// "Checked" progress bar stays pinned at the stale leaf count - /// while "Downloaded" climbs from 0, and the host pointlessly - /// re-downloads into an already-complete tree. Resetting the - /// tree alongside the watermarks makes Clear+resync a true cold - /// rebuild: `tree_size` returns to 0 and "Checked" climbs 0→N - /// trailing "Downloaded". + /// (`global_pos < tree_size`). Resetting the tree alongside the + /// watermarks makes Clear+resync a true cold rebuild. + /// + /// # Admission /// - /// Used by [`platform_wallet_manager_shielded_clear`] (the - /// host's Clear button). The host then wipes its own - /// per-wallet persistence (e.g. SwiftData rows) — Rust can't - /// reach that layer — and the next `bind_shielded` call - /// repopulates the registries and resyncs from scratch. + /// Runs under the exclusive [admission](Self::admit), taken within + /// [`COORDINATOR_DRAIN_BUDGET`]: every bind, account add, spend and sync + /// pass in flight finishes first, and none starts until the host wipe + /// has returned and the epoch is bumped. That is what makes the host + /// wipe final — no viewing-key write or snapshot restore begun before + /// it can land after it. An operation that does not release its + /// admission within the budget (a spend waiting on a host signer, say) + /// fails the Clear with [`PlatformWalletError::ShutdownIncomplete`], + /// with nothing reset. /// - /// Resets the cooldown to `None` so the first post-clear - /// background sync pass runs immediately rather than honoring - /// a stale "caught up" stamp from before the wipe. + /// # Failures /// - /// [`platform_wallet_manager_shielded_clear`]: - /// rs-platform-wallet-ffi's FFI entry point + /// A store reset failure returns its error before the host is called, + /// so the host keeps its rows while the tree may still be populated. + /// A host wipe failure after a successful reset returns + /// [`PlatformWalletError::Persistence`]; the Rust half stays reset, and + /// binds are refused ([`ShieldedAdmission::ensure_host_cleared`]) until + /// a later Clear wipes the host too, so the rows the host kept are + /// never restored. Either way the epoch advances. /// - /// Returns an error if either store reset (subwallet purge or - /// commitment-tree reset) fails. The caller **must** surface this: - /// the host only wipes its own per-wallet persistence (e.g. - /// SwiftData rows) after `clear()` succeeds. If a reset fails - /// silently the host could drop its rows while the shared tree - /// stays populated, and the next cold resync would gate-skip every - /// re-downloaded position against the stale `tree_size`. - pub async fn clear(&self) -> Result<(), crate::error::PlatformWalletError> { - // Held across the whole Clear, store reset included, so no bind - // install can be halfway through its transaction here: one that - // already registered would otherwise restore its pre-Clear - // snapshot into the store this just purged. A bind that has not - // yet opened its transaction is a separate problem — it may - // already hold a snapshot loaded before this Clear — and is - // handled by the generation bumped at the end of this function. - // Taken before the store lock to keep the `lifecycle` → `store` - // order. + /// Crate-internal: the manager's + /// [`clear_shielded`](crate::PlatformWalletManager::clear_shielded) + /// quiesces the sync loop first and supplies its own persister. + pub(crate) async fn clear( + &self, + host: &dyn PlatformWalletPersistence, + ) -> Result<(), crate::error::PlatformWalletError> { + let Ok(mut clear_state) = + tokio::time::timeout(COORDINATOR_DRAIN_BUDGET, self.admission.write()).await + else { + return Err(PlatformWalletError::ShutdownIncomplete( + "a shielded operation did not finish within the Clear budget; clear aborted \ + with nothing reset — retry once it completes" + .to_string(), + )); + }; + // Registry atomicity against `unregister_wallet`, which takes this + // mutex without an admission (see the `lifecycle` field doc). let _lifecycle = self.lifecycle.lock().await; // Reset the persistent store FIRST and bail before mutating any - // coordinator registries if it fails. Clearing `accounts` / `persisters` - // makes the coordinator forget every bound wallet (no syncs until - // the host rebinds), so doing that while the store reset failed — - // and the host therefore keeps its own local state — would leave - // the two halves inconsistent. Reset the tree before deleting - // subwallet state: a tree-reset failure must leave durable unresolved - // identity-debit guards intact, otherwise the still-bound wallet could - // build another valid debit after a failed Clear. + // coordinator registries or calling the host if it fails: the host + // must keep its rows while the tree may still be populated. Reset the + // tree before deleting subwallet state: a tree-reset failure must + // leave durable unresolved identity-debit guards intact, otherwise + // the still-bound wallet could build another valid debit after a + // failed Clear. let mut first_err: Option = None; { let mut store = self.store.write().await; @@ -1297,32 +1400,41 @@ impl NetworkShieldedCoordinator { } } } - // Hydration and snapshot validity go regardless of the outcome. - // Either reset may fail after changing part of its persistent state, - // so the next identical bind must not fast-path over a potentially - // half-reset store. The generation bump also prevents an in-flight - // bind from restoring a pre-Clear snapshot afterward. + // Hydration goes regardless of the outcome: either reset may fail + // after changing part of its persistent state, so the next identical + // bind must not fast-path over a potentially half-reset store. self.hydrated.write().await.clear(); - self.clear_generation - .fetch_add(1, std::sync::atomic::Ordering::AcqRel); + clear_state.epoch = clear_state.epoch.wrapping_add(1); if let Some(e) = first_err { return Err(e); } - // Store reset succeeded — now it is safe to drop the in-memory - // registries and reset the cooldown so the first post-clear - // background pass runs immediately rather than honoring a stale - // "caught up" stamp. The registries deliberately survive a failed - // clear: dropping them would make the coordinator forget every - // bound wallet (no syncs until the host rebinds) while the host, + // Store reset succeeded — drop the in-memory registries and reset the + // cooldown so the first post-clear background pass runs immediately. + // The registries survive a failed store reset: dropping them would + // make the coordinator forget every bound wallet while the host, // told the clear failed, keeps its own state. self.accounts.write().await.clear(); self.persisters.write().await.clear(); if let Ok(mut g) = self.last_caught_up_at.lock() { *g = None; } - Ok(()) + + // The host half, still under the exclusive admission. + match host.wipe_shielded_state(self.network) { + Ok(()) => { + clear_state.host_wipe_pending = false; + Ok(()) + } + Err(e) => { + tracing::error!(error = %e, "host shielded wipe failed after the native reset"); + clear_state.host_wipe_pending = true; + Err(PlatformWalletError::Persistence(format!( + "native shielded state was reset but the host wipe failed: {e}; retry Clear" + ))) + } + } } /// Discharge the private payment journal records of the private contact @@ -1384,16 +1496,22 @@ impl NetworkShieldedCoordinator { /// [`sync_notes_across`]: super::sync::sync_notes_across /// [`PlatformEventManager::on_shielded_sync_completed`]: /// crate::events::PlatformEventManager::on_shielded_sync_completed + /// + /// The whole pass runs under a shared [admission](Self::admit), so a + /// Clear never lands between its scan and the store and host writes the + /// scan produces. A pass that is stopped while it waits for the admission + /// returns the empty summary. pub async fn sync(&self, force: bool, cancel: &CancellationToken) -> ShieldedSyncPassSummary { // A stop that landed before any work: nothing to finalize, so // return the empty summary without touching Platform or the // store (the manager then skips the completion event). Checked // ahead of the cooldown gate, whose skip summary would otherwise // report one caught-up wallet each for a pass that never ran. - if cancel.is_cancelled() { + let admission = cancel.run_until_cancelled(self.admit()).await; + let (Some(_admission), false) = (admission, cancel.is_cancelled()) else { tracing::debug!("Coordinator sync cancelled before it started"); return ShieldedSyncPassSummary::default(); - } + }; // Every pass, cooldown or not: the private requests a private sync read // back discharge their journal records. @@ -3383,8 +3501,17 @@ mod tests { assert!(!coordinator.accounts.read().await.is_empty()); assert!(!coordinator.persisters.read().await.is_empty()); - coordinator.clear().await.expect("clear should succeed"); + let host = crate::test_support::WipeCountingPersistence::default(); + coordinator + .clear(&host) + .await + .expect("clear should succeed"); + assert_eq!( + host.wipe_count(), + 1, + "a successful clear wipes the host once" + ); assert_eq!( coordinator.store().read().await.tree_size().unwrap(), 0, @@ -3429,7 +3556,10 @@ mod tests { assert_eq!(store.last_synced_note_index(id).unwrap(), 8); } - coordinator.clear().await.expect("clear should succeed"); + coordinator + .clear(&crate::test_support::WipeCountingPersistence::default()) + .await + .expect("clear should succeed"); // Reopen the persisted file with a FRESH handle: this reads the // on-disk tables cold (no shared in-memory frontier), so a non-zero @@ -3443,7 +3573,7 @@ mod tests { a non-zero size means clear() only reset the in-memory tree" ); // The watermark is in-memory per store handle (rebuilt from the host's - // Room/SwiftData on bind, which the host wipes separately), so a fresh + // Room/SwiftData on bind, which Clear has the host wipe), so a fresh // handle starts it at 0 by construction; assert it to pin the contract // that a cold reopen is caught up to nothing. assert_eq!( @@ -3499,11 +3629,17 @@ mod tests { // inode, but a fresh open at the now-missing path errors. std::fs::remove_dir_all(&dir).expect("remove temp dir to break reopen"); - let result = coordinator.clear().await; + let host = crate::test_support::WipeCountingPersistence::default(); + let result = coordinator.clear(&host).await; assert!( result.is_err(), "clear must surface the store-reset failure rather than swallow it" ); + assert_eq!( + host.wipe_count(), + 0, + "the host keeps its rows when the native reset failed" + ); assert!( !coordinator.accounts.read().await.is_empty(), "accounts must survive a failed clear so sync does not become a no-op" @@ -3540,13 +3676,15 @@ mod tests { let wallet_id: WalletId = [0x11; 32]; coordinator.mark_hydrated(wallet_id, true).await; - let generation_before = coordinator.clear_generation(); + let epoch_before = coordinator.clear_epoch().await; // Break the tree reset's reopen. The subwallet purge must not run // after this failure. std::fs::remove_dir_all(&dir).expect("remove temp dir to break reopen"); - let result = coordinator.clear().await; + let result = coordinator + .clear(&crate::test_support::WipeCountingPersistence::default()) + .await; assert!(result.is_err(), "the store-reset failure must surface"); assert!( !coordinator.is_hydrated(wallet_id).await, @@ -3554,9 +3692,9 @@ mod tests { the next re-bind would otherwise fast-path over an emptied store" ); assert_ne!( - coordinator.clear_generation(), - generation_before, - "snapshots loaded before a partial clear must be invalidated too" + coordinator.clear_epoch().await, + epoch_before, + "admissions taken before a partial clear must see a new epoch" ); } @@ -4156,7 +4294,8 @@ mod tests { let coordinator = Arc::new(coordinator_with_one_wallet(&dir).await); let wallet_id: WalletId = [0x11; 32]; - let install = coordinator.begin_install(wallet_id).await; + let admission = coordinator.admit().await; + let install = admission.begin_install(wallet_id).await; let remover = { let coordinator = Arc::clone(&coordinator); tokio::spawn(async move { coordinator.unregister_wallet(wallet_id).await }) @@ -4180,8 +4319,8 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } - /// Clear must wait for an in-flight install transaction, purge - /// included. The mutex is taken before the store reset precisely so + /// Clear must wait for an in-flight install transaction, purge and + /// host wipe included. The admission is taken before the store reset so /// the purge cannot land between a bind's registration and its /// restore — the bind would then put its snapshot back into the /// store Clear had just emptied, which the host reads as "Clear did @@ -4201,10 +4340,13 @@ mod tests { .unwrap(); } - let install = coordinator.begin_install(wallet_id).await; + let admission = coordinator.admit().await; + let install = admission.begin_install(wallet_id).await; + let host = Arc::new(crate::test_support::WipeCountingPersistence::default()); let clearer = { let coordinator = Arc::clone(&coordinator); - tokio::spawn(async move { coordinator.clear().await }) + let host = Arc::clone(&host); + tokio::spawn(async move { coordinator.clear(&*host).await }) }; tokio::time::sleep(Duration::from_millis(200)).await; @@ -4225,8 +4367,16 @@ mod tests { is what lets the bind restore its snapshot back over a completed Clear" ); + assert_eq!( + host.wipe_count(), + 0, + "nor may the host wipe run mid-install" + ); + drop(install); + drop(admission); clearer.await.expect("clear task").expect("clear succeeds"); + assert_eq!(host.wipe_count(), 1); assert!( coordinator.registered_subwallets().await.is_empty(), "the queued clear runs as soon as the transaction commits" diff --git a/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs b/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs index e0b03b89c03..fcf4263dd4e 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs @@ -183,7 +183,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] pub async fn shielded_fund_from_asset_lock( &self, - coordinator: &std::sync::Arc, funding: AssetLockFunding, recipients: Vec<(OrchardAddress, Option)>, asset_lock_signer: &AS, @@ -222,7 +221,6 @@ impl PlatformWallet { }; let result = self .shielded_fund_from_asset_lock_inner( - coordinator, funding, recipients, asset_lock_signer, @@ -250,7 +248,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] async fn shielded_fund_from_asset_lock_inner( &self, - coordinator: &std::sync::Arc, funding: AssetLockFunding, recipients: Vec<(OrchardAddress, Option)>, asset_lock_signer: &AS, @@ -612,7 +609,7 @@ impl PlatformWallet { // by construction). Recorded Confirmed directly — `broadcast_and_ // _wait` already proved inclusion. Best-effort: a recording miss // (no bound keyset, no recoverable output) just omits the row. - self.record_shield_from_asset_lock_activity(coordinator, &landed.actions, shield_amount) + self.record_shield_from_asset_lock_activity(&landed.actions, shield_amount) .await; // Step 5: finalize the tracked asset lock. The shielded note itself @@ -743,7 +740,6 @@ impl PlatformWallet { #[cfg(feature = "shielded")] pub(super) async fn record_shield_from_asset_lock_activity( &self, - coordinator: &crate::wallet::shielded::NetworkShieldedCoordinator, actions: &[dpp::shielded::SerializedAction], shield_amount: Credits, ) { @@ -795,6 +791,15 @@ impl PlatformWallet { (account, pending) }; + // The only shielded write of a funding, admitted on its own: the + // asset lock's broadcast and ChainLock wait before it, which can take + // indefinitely long, write no shielded state and hold no Clear back. + // `shield_guard` is held here, which the lock order allows: it comes + // before admission everywhere. + let Ok(coordinator) = self.shielded_coordinator().await else { + return; + }; + let _admission = coordinator.admit().await; let confirmed = with_status(&pending, ShieldedActivityStatus::Confirmed, None); let id = crate::wallet::shielded::SubwalletId::new(self.wallet_id(), account); crate::wallet::shielded::operations::queue_shielded_activity( diff --git a/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs new file mode 100644 index 00000000000..5944f0e96a2 --- /dev/null +++ b/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs @@ -0,0 +1,687 @@ +//! The shielded lifecycle boundary: a manager-owned coordinator, one +//! admission protocol, and a Clear that runs the host wipe itself. +//! +//! Every durable shielded mutation holds the coordinator's admission +//! shared; Clear holds it exclusively across quiesce, native reset, host +//! wipe and epoch bump. These tests park one side at a host callback with +//! a gate and check the other side neither overtakes it nor leaves state +//! behind. + +use std::collections::BTreeMap; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::sync::{mpsc, Arc, Mutex}; +use std::time::Duration; + +use dashcore::Network; +use key_wallet::wallet::initialization::WalletAccountCreationOptions; + +use crate::changeset::{ + ClientStartState, PersistenceCapabilities, PersistenceError, PlatformWalletChangeSet, + PlatformWalletPersistence, ShieldedSubwalletStartState, +}; +use crate::error::PlatformWalletError; +use crate::events::{EventHandler, PlatformEventHandler}; +use crate::wallet::platform_wallet::{PlatformWallet, WalletId}; +use crate::wallet::shielded::store::ShieldedStore; +use crate::wallet::shielded::{NetworkShieldedCoordinator, ShieldedNote, SubwalletId}; +use crate::PlatformWalletManager; + +const SEED: [u8; 64] = [0x42; 64]; + +struct NoopEventHandler; +impl EventHandler for NoopEventHandler {} +impl PlatformEventHandler for NoopEventHandler {} + +type WipeHook = Box; + +/// A host that keeps shielded viewing keys and per-subwallet rows the way a +/// Swift or Kotlin store does, deletes them on `wipe_shielded_state`, and +/// records the order of its shielded callbacks. +struct BoundaryHost { + capabilities: PersistenceCapabilities, + viewing_keys: Mutex>>, + subwallets: Mutex>, + events: Mutex>, + wipes: AtomicUsize, + wiped_exclusively: AtomicBool, + fail_wipe: AtomicBool, + /// Parks the next viewing-key store until released. + store_gate: Mutex>>, + /// Parks the next load until released. + load_gate: Mutex>>, + /// Set by the test once the manager is configured, so a wipe can tell + /// whether Clear holds the admission exclusively. + coordinator: Mutex>>, + on_wipe: Mutex>, +} + +impl BoundaryHost { + fn new() -> Arc { + Self::with_capabilities( + PersistenceCapabilities::SHIELDED_FVK_RESTART + .union(PersistenceCapabilities::SHIELDED_STATE_WIPE), + ) + } + + fn with_capabilities(capabilities: PersistenceCapabilities) -> Arc { + Arc::new(Self { + capabilities, + viewing_keys: Mutex::default(), + subwallets: Mutex::default(), + events: Mutex::default(), + wipes: AtomicUsize::new(0), + wiped_exclusively: AtomicBool::new(true), + fail_wipe: AtomicBool::new(false), + store_gate: Mutex::default(), + load_gate: Mutex::default(), + coordinator: Mutex::default(), + on_wipe: Mutex::default(), + }) + } + + fn gate(slot: &Mutex>>) -> mpsc::Sender<()> { + let (tx, rx) = mpsc::channel(); + *slot.lock().unwrap() = Some(rx); + tx + } + + fn event(&self, event: &'static str) { + self.events.lock().unwrap().push(event); + } + + fn clear_events(&self) { + self.events.lock().unwrap().clear(); + } + + fn events(&self) -> Vec<&'static str> { + self.events.lock().unwrap().clone() + } + + fn wipe_count(&self) -> usize { + self.wipes.load(Ordering::SeqCst) + } + + fn viewing_key_rows(&self) -> usize { + self.viewing_keys.lock().unwrap().len() + } + + /// Wait until the host logged `event`, failing after a generous bound. + async fn reached(&self, event: &'static str) { + for _ in 0..1_000 { + if self.events().contains(&event) { + return; + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + panic!("the host never logged {event:?}: {:?}", self.events()); + } +} + +impl PlatformWalletPersistence for BoundaryHost { + fn persistence_capabilities(&self) -> PersistenceCapabilities { + self.capabilities + } + + fn store( + &self, + _wallet_id: WalletId, + changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + let Some(shielded) = changeset.shielded else { + return Ok(()); + }; + if shielded.viewing_keys.is_empty() { + return Ok(()); + } + self.event("store viewing keys"); + let gate = self.store_gate.lock().unwrap().take(); + if let Some(gate) = gate { + let _ = gate.recv(); + } + self.viewing_keys + .lock() + .unwrap() + .extend(shielded.viewing_keys); + self.event("stored viewing keys"); + Ok(()) + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Ok(()) + } + + fn load(&self) -> Result { + self.event("load"); + let gate = self.load_gate.lock().unwrap().take(); + if let Some(gate) = gate { + let _ = gate.recv(); + } + let mut start = ClientStartState::default(); + start.shielded.viewing_keys = self.viewing_keys.lock().unwrap().clone(); + start.shielded.per_subwallet = self.subwallets.lock().unwrap().clone(); + Ok(start) + } + + fn wipe_shielded_state(&self, _network: Network) -> Result<(), PersistenceError> { + self.event("wipe"); + self.wipes.fetch_add(1, Ordering::SeqCst); + if let Some(coordinator) = self.coordinator.lock().unwrap().as_ref() { + if !coordinator.admission_is_closed() { + self.wiped_exclusively.store(false, Ordering::SeqCst); + } + } + if let Some(hook) = self.on_wipe.lock().unwrap().as_mut() { + hook(); + } + if self.fail_wipe.load(Ordering::SeqCst) { + return Err(PersistenceError::backend("injected host wipe failure")); + } + self.viewing_keys.lock().unwrap().clear(); + self.subwallets.lock().unwrap().clear(); + self.event("wiped"); + Ok(()) + } +} + +/// A manager over `host` with one wallet, its shielded store configured +/// under `dir`. +async fn configured( + host: &Arc, + dir: &tempfile::TempDir, +) -> ( + Arc>, + Arc, + Arc, +) { + let (manager, wallet) = unconfigured(host).await; + manager + .configure_shielded(dir.path().join("tree.sqlite")) + .await + .expect("the shielded store is configured"); + let coordinator = manager + .shielded_coordinator() + .await + .expect("the manager's coordinator"); + *host.coordinator.lock().unwrap() = Some(Arc::clone(&coordinator)); + host.clear_events(); + (manager, wallet, coordinator) +} + +async fn unconfigured( + host: &Arc, +) -> ( + Arc>, + Arc, +) { + let manager = Arc::new(PlatformWalletManager::new( + Arc::new(dash_sdk::Sdk::new_mock()), + Arc::clone(host), + Arc::new(NoopEventHandler), + )); + let wallet = manager + .create_wallet_from_seed_bytes( + Network::Testnet, + &[0x17; 64], + WalletAccountCreationOptions::Default, + Some(0), + ) + .await + .expect("wallet"); + (manager, wallet) +} + +/// Host callbacks of a bind that finished before a Clear. +const BIND_THEN_CLEAR: [&str; 5] = [ + "load", + "store viewing keys", + "stored viewing keys", + "wipe", + "wiped", +]; +/// Host callbacks of a Clear followed by a bind (or account add). +const CLEAR_THEN_BIND: [&str; 5] = [ + "wipe", + "wiped", + "load", + "store viewing keys", + "stored viewing keys", +]; + +fn spawn_clear( + manager: &Arc>, +) -> tokio::task::JoinHandle> { + let manager = Arc::clone(manager); + tokio::spawn(async move { manager.clear_shielded().await }) +} + +/// Wait until a Clear holds or is queued on `coordinator`'s admission, so +/// what the test checks next happens after Clear reached it. +async fn until_admission_closed(coordinator: &NetworkShieldedCoordinator) { + for _ in 0..1_000 { + if coordinator.admission_is_closed() { + return; + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + panic!("Clear never reached the admission"); +} + +/// Host rows a pre-Clear session left: one note and a caught-up watermark. +fn pre_clear_rows(wallet_id: WalletId) -> BTreeMap { + BTreeMap::from([( + SubwalletId::new(wallet_id, 0), + ShieldedSubwalletStartState { + notes: vec![ShieldedNote { + position: 7, + cmx: [0x2A; 32], + nullifier: [0xE0; 32], + block_height: 100, + is_spent: false, + value: 1_000, + note_data: vec![0u8; 115], + }], + has_sync_state: true, + last_synced_index: 900_000, + ..Default::default() + }, + )]) +} + +/// Neither the pre-Clear note nor its watermark survives in the native +/// store, and no account is registered. +async fn assert_native_state_cleared(coordinator: &NetworkShieldedCoordinator, id: SubwalletId) { + let store = coordinator.store().read().await; + assert!(store.get_all_notes(id).unwrap().is_empty()); + assert_eq!(store.last_synced_note_index(id).unwrap(), 0); + drop(store); + assert!(coordinator.registered_subwallets().await.is_empty()); +} + +/// A seed bind parked in its viewing-key write holds Clear off until it is +/// done; Clear then resets it and wipes the row it wrote, so nothing of the +/// bind outlives the Clear. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_wipe_the_viewing_keys_of_a_bind_whose_host_write_was_delayed_across_clear() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + let id = SubwalletId::new(wallet.wallet_id(), 0); + + let release = BoundaryHost::gate(&host.store_gate); + let bind = { + let wallet = Arc::clone(&wallet); + tokio::spawn(async move { wallet.bind_shielded(&SEED, &[0]).await }) + }; + host.reached("store viewing keys").await; + let clear = spawn_clear(&manager); + until_admission_closed(&coordinator).await; + assert_eq!(host.wipe_count(), 0, "Clear waits for the admitted bind"); + assert!(!clear.is_finished()); + + release.send(()).unwrap(); + bind.await.unwrap().expect("the bind finishes first"); + clear.await.unwrap().expect("then Clear runs"); + + assert_eq!(host.wipe_count(), 1); + assert_eq!( + host.viewing_key_rows(), + 0, + "no viewing-key row outlives Clear" + ); + assert_eq!(host.events(), BIND_THEN_CLEAR); + assert_native_state_cleared(&coordinator, id).await; +} + +/// A seedless rebind parked in its host load holds Clear off; Clear then +/// purges what it restored, so the pre-Clear snapshot it read restores +/// nothing that survives. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_restore_nothing_that_survives_from_a_snapshot_read_before_clear() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + let id = SubwalletId::new(wallet.wallet_id(), 0); + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + *host.subwallets.lock().unwrap() = pre_clear_rows(wallet.wallet_id()); + host.clear_events(); + + let release = BoundaryHost::gate(&host.load_gate); + let rebind = { + let wallet = Arc::clone(&wallet); + tokio::spawn(async move { wallet.bind_shielded_from_persisted(&[0]).await }) + }; + host.reached("load").await; + let clear = spawn_clear(&manager); + until_admission_closed(&coordinator).await; + assert_eq!(host.wipe_count(), 0, "Clear waits for the admitted rebind"); + + release.send(()).unwrap(); + assert!(rebind.await.unwrap().expect("the rebind finishes first")); + clear.await.unwrap().expect("then Clear runs"); + + assert_eq!(host.events(), vec!["load", "wipe", "wiped"]); + assert_eq!(host.viewing_key_rows(), 0); + assert!(host.subwallets.lock().unwrap().is_empty()); + assert_native_state_cleared(&coordinator, id).await; +} + +/// A bind that starts while the host wipe runs waits for the whole Clear +/// and then reads the wiped host: the pre-Clear rows it would have read a +/// moment earlier are not restored. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_hold_a_bind_started_during_the_wipe_until_clear_finishes() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + let id = SubwalletId::new(wallet.wallet_id(), 0); + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + *host.subwallets.lock().unwrap() = pre_clear_rows(wallet.wallet_id()); + host.clear_events(); + + type Bind = tokio::task::JoinHandle>; + let started: Arc>> = Arc::default(); + let held_during_wipe = Arc::new(AtomicBool::new(false)); + { + let wallet = Arc::clone(&wallet); + let started = Arc::clone(&started); + let held_during_wipe = Arc::clone(&held_during_wipe); + let runtime = tokio::runtime::Handle::current(); + *host.on_wipe.lock().unwrap() = Some(Box::new(move || { + let wallet = Arc::clone(&wallet); + let bind = runtime.spawn(async move { wallet.bind_shielded(&SEED, &[0]).await }); + std::thread::sleep(Duration::from_millis(100)); + held_during_wipe.store(!bind.is_finished(), Ordering::SeqCst); + *started.lock().unwrap() = Some(bind); + })); + } + + manager.clear_shielded().await.expect("Clear succeeds"); + assert!(held_during_wipe.load(Ordering::SeqCst)); + let bind = started + .lock() + .unwrap() + .take() + .expect("the hook started a bind"); + bind.await.unwrap().expect("the bind runs after Clear"); + + assert_eq!( + host.events(), + CLEAR_THEN_BIND, + "the bind reads the host only after the wipe" + ); + let store = coordinator.store().read().await; + assert!(store.get_all_notes(id).unwrap().is_empty()); + assert_eq!(store.last_synced_note_index(id).unwrap(), 0); + drop(store); + assert_eq!(coordinator.registered_subwallets().await, vec![id]); +} + +/// Adding an account is admitted like a bind: Clear waits for one parked in +/// its host load, then removes the account it added. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_order_an_account_add_and_a_clear() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + host.clear_events(); + + let release = BoundaryHost::gate(&host.load_gate); + let add = { + let wallet = Arc::clone(&wallet); + tokio::spawn(async move { wallet.shielded_add_account(&SEED, 1).await }) + }; + host.reached("load").await; + let clear = spawn_clear(&manager); + until_admission_closed(&coordinator).await; + assert_eq!( + host.wipe_count(), + 0, + "Clear waits for the admitted account add" + ); + + release.send(()).unwrap(); + add.await.unwrap().expect("the add finishes first"); + clear.await.unwrap().expect("then Clear runs"); + + assert_eq!(host.events(), BIND_THEN_CLEAR); + assert_eq!(host.viewing_key_rows(), 0); + assert!(coordinator.registered_subwallets().await.is_empty()); + + // An add started while the wipe runs waits for the whole Clear, then + // reads the wiped host. + type Add = tokio::task::JoinHandle>; + let started: Arc>> = Arc::default(); + { + let wallet = Arc::clone(&wallet); + let started = Arc::clone(&started); + let runtime = tokio::runtime::Handle::current(); + *host.on_wipe.lock().unwrap() = Some(Box::new(move || { + let wallet = Arc::clone(&wallet); + *started.lock().unwrap() = + Some(runtime.spawn(async move { wallet.shielded_add_account(&SEED, 2).await })); + std::thread::sleep(Duration::from_millis(100)); + })); + } + host.clear_events(); + manager.clear_shielded().await.expect("the second Clear"); + let add = started + .lock() + .unwrap() + .take() + .expect("the hook started an add"); + add.await.unwrap().expect("the add runs after Clear"); + assert_eq!( + host.events(), + CLEAR_THEN_BIND, + "the add reads the host only after the wipe" + ); + assert_eq!( + host.viewing_key_rows(), + 1, + "only the post-Clear account row" + ); +} + +/// A bind queued behind a long shield-class operation on its wallet (a +/// funding waiting on its ChainLock holds `shield_guard`) holds no +/// admission, so it does not stall a Clear. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_clear_while_a_bind_waits_for_the_wallets_shield_guard() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + host.clear_events(); + + let long_operation = wallet.shield_guard.lock().await; + let bind = { + let wallet = Arc::clone(&wallet); + tokio::spawn(async move { wallet.bind_shielded(&SEED, &[0]).await }) + }; + tokio::time::sleep(Duration::from_millis(200)).await; + tokio::time::timeout(Duration::from_secs(5), manager.clear_shielded()) + .await + .expect("Clear does not wait for the queued bind") + .expect("Clear succeeds"); + assert!(!bind.is_finished(), "the bind still waits for the guard"); + + drop(long_operation); + bind.await.unwrap().expect("the bind runs after Clear"); + assert_eq!(host.events(), CLEAR_THEN_BIND); + assert_eq!( + coordinator.registered_subwallets().await, + vec![SubwalletId::new(wallet.wallet_id(), 0)] + ); +} + +/// A shielded operation that keeps its admission past the budget fails the +/// Clear with nothing reset and the host never called. +#[tokio::test(start_paused = true)] +async fn should_abort_clear_with_nothing_reset_when_admission_is_held_past_the_budget() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (_manager, wallet, coordinator) = configured(&host, &dir).await; + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + let epoch = coordinator.clear_epoch().await; + + let in_flight = coordinator.admit().await; + let error = coordinator + .clear(&*host) + .await + .expect_err("the admission is never released"); + drop(in_flight); + + assert!( + matches!(error, PlatformWalletError::ShutdownIncomplete(_)), + "{error:?}" + ); + assert_eq!(host.wipe_count(), 0); + assert_eq!(host.viewing_key_rows(), 1); + assert_eq!(coordinator.clear_epoch().await, epoch); + assert_eq!( + coordinator.registered_subwallets().await, + vec![SubwalletId::new(wallet.wallet_id(), 0)] + ); +} + +/// Clear calls the host wipe exactly once, while it holds the admission +/// exclusively, and returns only after it. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_wipe_once_under_exclusive_admission() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + let epoch = coordinator.clear_epoch().await; + + manager.clear_shielded().await.unwrap(); + + assert_eq!(host.wipe_count(), 1); + assert!(host.wiped_exclusively.load(Ordering::SeqCst)); + assert!(!coordinator.admission_is_closed(), "released on return"); + assert_eq!(coordinator.clear_epoch().await, epoch + 1); +} + +/// A host that cannot wipe makes Clear refuse before anything is reset. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_refuse_clear_without_the_wipe_capability() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::with_capabilities(PersistenceCapabilities::SHIELDED_FVK_RESTART); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + let epoch = coordinator.clear_epoch().await; + + let error = manager + .clear_shielded() + .await + .expect_err("Clear fails closed"); + + assert!( + matches!(&error, PlatformWalletError::Persistence(message) if message.contains("nothing was reset")), + "{error:?}" + ); + assert_eq!(host.wipe_count(), 0); + assert_eq!(host.viewing_key_rows(), 1); + assert_eq!(coordinator.clear_epoch().await, epoch); + assert_eq!( + coordinator.registered_subwallets().await, + vec![SubwalletId::new(wallet.wallet_id(), 0)] + ); +} + +/// A wipe that fails after the native reset leaves binds refused until a +/// retried Clear wipes the host. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_refuse_binds_until_a_failed_host_wipe_is_retried() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + + host.fail_wipe.store(true, Ordering::SeqCst); + manager + .clear_shielded() + .await + .expect_err("the host wipe failed"); + assert_eq!(host.viewing_key_rows(), 1, "the host kept its rows"); + let refused = wallet + .bind_shielded_from_persisted(&[0]) + .await + .expect_err("a bind would restore the unwiped rows"); + assert!( + matches!(refused, PlatformWalletError::ShieldedStoreError(_)), + "{refused:?}" + ); + let refused = wallet + .shielded_add_account(&SEED, 1) + .await + .expect_err("an account add is refused too"); + assert!( + matches!(refused, PlatformWalletError::ShieldedStoreError(_)), + "{refused:?}" + ); + let refused = coordinator + .restore_for_wallet(wallet.wallet_id(), &Default::default()) + .await + .expect_err("so is a direct coordinator restore"); + assert!( + matches!(refused, PlatformWalletError::ShieldedStoreError(_)), + "{refused:?}" + ); + + host.fail_wipe.store(false, Ordering::SeqCst); + manager.clear_shielded().await.expect("the retry wipes"); + assert_eq!(host.viewing_key_rows(), 0); + wallet + .bind_shielded(&SEED, &[0]) + .await + .expect("binds again"); +} + +/// The manager owns the coordinator: its wallets resolve exactly the +/// manager's, and none before the manager configured one. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_resolve_the_managers_coordinator_and_none_before_it_is_configured() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet) = unconfigured(&host).await; + + let error = wallet + .bind_shielded(&SEED, &[0]) + .await + .expect_err("no coordinator yet"); + assert!( + matches!(&error, PlatformWalletError::ShieldedStoreError(message) if message.contains("not configured")), + "{error:?}" + ); + assert_eq!(host.viewing_key_rows(), 0, "refused before any host write"); + + manager + .configure_shielded(dir.path().join("tree.sqlite")) + .await + .unwrap(); + let owned = manager.shielded_coordinator().await.unwrap(); + assert!(Arc::ptr_eq( + &wallet.shielded_coordinator().await.unwrap(), + &owned + )); + let later = manager + .create_wallet_from_seed_bytes( + Network::Testnet, + &[0x18; 64], + WalletAccountCreationOptions::Default, + Some(0), + ) + .await + .unwrap(); + assert!(Arc::ptr_eq( + &later.shielded_coordinator().await.unwrap(), + &owned + )); + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + assert_eq!( + owned.registered_subwallets().await, + vec![SubwalletId::new(wallet.wallet_id(), 0)] + ); +} diff --git a/packages/rs-platform-wallet/src/wallet/shielded/mod.rs b/packages/rs-platform-wallet/src/wallet/shielded/mod.rs index a535e52fff4..bf7861fbd78 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/mod.rs @@ -46,6 +46,8 @@ pub mod file_store; mod fresh_address_scan_tests; pub mod fund_from_asset_lock; pub mod keys; +#[cfg(test)] +mod lifecycle_boundary_tests; pub mod note_selection; pub mod operations; pub mod prover; @@ -67,6 +69,14 @@ pub use balance::{ ShieldedLocalBalanceState, }; pub use coordinator::NetworkShieldedCoordinator; + +/// The manager's slot for its network's [`NetworkShieldedCoordinator`]: +/// `None` until `PlatformWalletManager::configure_shielded` installs one. +/// The manager creates it and hands the same slot to its sync manager and +/// to every wallet it builds, so a wallet's shielded operations resolve +/// the manager's coordinator and no other. +pub(crate) type ShieldedCoordinatorSlot = + std::sync::Arc>>>; pub use file_store::{FileBackedShieldedStore, FileShieldedStoreError}; pub use keys::{AccountViewingKeys, OrchardKeySet}; pub use prover::CachedOrchardProver; diff --git a/packages/rs-platform-wallet/src/wallet/shielded/seed_pool.rs b/packages/rs-platform-wallet/src/wallet/shielded/seed_pool.rs index 2cfbf32e171..80e661b672d 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/seed_pool.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/seed_pool.rs @@ -139,7 +139,6 @@ impl PlatformWallet { #[allow(clippy::too_many_arguments)] pub async fn shielded_seed_pool_notes( &self, - coordinator: &std::sync::Arc, wallet_id: &[u8; 32], account: u32, target_total_notes: u64, @@ -277,7 +276,6 @@ impl PlatformWallet { attempt += 1; match self .shielded_fund_from_asset_lock( - coordinator, funding, vec![(recipient, None)], asset_lock_signer, diff --git a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs index 0f2b2cadada..2ce9853d34d 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs @@ -190,6 +190,7 @@ where Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), crate::changeset::DurableCursors::default(), + Default::default(), ) } @@ -232,12 +233,12 @@ mod zero_amount_tests { let dir = tempfile::tempdir().expect("temporary shielded store"); let coordinator = coordinator_at(dir.path()); let wallet = platform_wallet_with(Arc::new(CapturingPersistence::default())).await; + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; let network = wallet.network(); let seed = [0x42; 64]; - wallet - .bind_shielded(&seed, &[0], &coordinator) - .await - .unwrap(); + wallet.bind_shielded(&seed, &[0]).await.unwrap(); let recipient = OrchardKeySet::from_seed(&[0x43; 64], network, 0) .unwrap() .default_address @@ -268,56 +269,25 @@ mod zero_amount_tests { match spend { Spend::Transfer => { wallet - .shielded_transfer_to( - &coordinator, - &seed, - 0, - &recipient, - 0, - [0; 36], - PanicProver, - ) + .shielded_transfer_to(&seed, 0, &recipient, 0, [0; 36], PanicProver) .await } Spend::Unshield => { wallet - .shielded_unshield_to( - &coordinator, - &seed, - 0, - &platform_address, - 0, - PanicProver, - ) + .shielded_unshield_to(&seed, 0, &platform_address, 0, PanicProver) .await } Spend::Withdraw => { wallet - .shielded_withdraw_to( - &coordinator, - &seed, - 0, - &core_address, - 0, - 1, - PanicProver, - ) + .shielded_withdraw_to(&seed, 0, &core_address, 0, 1, PanicProver) .await } Spend::IdentityTopUp => wallet - .shielded_identity_top_up_from_pool( - &coordinator, - &seed, - 0, - &identity_id, - 0, - PanicProver, - ) + .shielded_identity_top_up_from_pool(&seed, 0, &identity_id, 0, PanicProver) .await .map(|_| ()), Spend::IdentityCreate => wallet .shielded_identity_create_from_pool( - &coordinator, &seed, 0, 0, @@ -383,9 +353,12 @@ async fn bind_fails_closed_without_fvk_restart_capabilities() { let persister = Arc::new(crate::wallet::persister::NoPlatformPersistence); let wallet = platform_wallet_with(persister).await; let coordinator = coordinator_at(&temp_dir("missing_capabilities")); + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; let err = wallet - .bind_shielded(&[0x42u8; 64], &[0], &coordinator) + .bind_shielded(&[0x42u8; 64], &[0]) .await .expect_err("missing FVK callbacks must fail closed"); assert!( @@ -400,9 +373,12 @@ async fn local_balance_bind_load_error_surfaces_and_retry_hydrates() { let persister = Arc::new(CapturingPersistence::default()); let wallet = platform_wallet_with(Arc::clone(&persister)).await; let coordinator = coordinator_at(&temp_dir("local_load_retry")); + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; *persister.fail_next_load.lock().unwrap() = true; let error = wallet - .bind_shielded(&[42; 64], &[0], &coordinator) + .bind_shielded(&[42; 64], &[0]) .await .expect_err("local storage failure must fail binding"); assert!(error @@ -418,7 +394,7 @@ async fn local_balance_bind_load_error_surfaces_and_retry_hydrates() { assert!(!wallet.is_shielded_bound().await); wallet - .bind_shielded(&[42; 64], &[0], &coordinator) + .bind_shielded(&[42; 64], &[0]) .await .expect("retry bind"); let ShieldedLocalBalanceState::Ready(snapshot) = coordinator @@ -453,7 +429,10 @@ async fn bind_persists_viewing_keys_and_restart_rebinds_seedlessly() { let wallet1 = platform_wallet_with(Arc::clone(&persister1)).await; let coordinator1 = coordinator_at(&temp_dir("session1")); wallet1 - .bind_shielded(&seed, &accounts, &coordinator1) + .attach_shielded_coordinator_for_test(&coordinator1) + .await; + wallet1 + .bind_shielded(&seed, &accounts) .await .expect("seed bind succeeds"); @@ -492,6 +471,9 @@ async fn bind_persists_viewing_keys_and_restart_rebinds_seedlessly() { persister2.serve_viewing_keys(restored_rows); let session2_dir = temp_dir("session2"); let coordinator2 = coordinator_at(&session2_dir); + wallet2 + .attach_shielded_coordinator_for_test(&coordinator2) + .await; let subwallet = SubwalletId::new(wallet2.wallet_id(), 0); persister2.serve_subwallets(BTreeMap::from([( subwallet, @@ -536,9 +518,12 @@ async fn bind_persists_viewing_keys_and_restart_rebinds_seedlessly() { // exactly as on launch, before the host's notes have been restored. drop(coordinator2); let coordinator2 = coordinator_at(&session2_dir); + wallet2 + .attach_shielded_coordinator_for_test(&coordinator2) + .await; let rebound = wallet2 - .bind_shielded_from_persisted(&accounts, &coordinator2) + .bind_shielded_from_persisted(&accounts) .await .expect("seedless rebind succeeds"); assert!(rebound, "persisted rows cover all accounts → rebound"); @@ -564,7 +549,7 @@ async fn bind_persists_viewing_keys_and_restart_rebinds_seedlessly() { // Local startup is complete before any network scan: notes from the // host snapshot are hydrated, and pending inputs remain unavailable. let balances = wallet2 - .shielded_balances(&coordinator2) + .shielded_balances() .await .expect("balance read over rebound viewing keys"); assert_eq!(balances.get(&0), Some(&100)); @@ -603,6 +588,9 @@ async fn bind_rejects_a_viewing_key_change_for_an_already_persisted_account() { let persister = Arc::new(CapturingPersistence::default()); let wallet = platform_wallet_with(Arc::clone(&persister)).await; let coordinator = coordinator_at(&temp_dir("rekey_refused")); + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; // Durable row from an earlier bind, under a different seed. Derived // on the wallet's own network so the SEED is the only difference — @@ -619,7 +607,7 @@ async fn bind_rejects_a_viewing_key_change_for_an_already_persisted_account() { persister.serve_viewing_keys(rows); let err = wallet - .bind_shielded(&[0x42u8; 64], &[0], &coordinator) + .bind_shielded(&[0x42u8; 64], &[0]) .await .expect_err("a re-keyed account must not bind over the old key's state"); assert!( @@ -649,9 +637,12 @@ async fn bind_after_wallet_removal_is_refused() { let persister = Arc::new(CapturingPersistence::default()); let wallet = platform_wallet_with(Arc::clone(&persister)).await; let coordinator = coordinator_at(&temp_dir("detached_bind")); + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; wallet - .bind_shielded(&[0x42u8; 64], &[0], &coordinator) + .bind_shielded(&[0x42u8; 64], &[0]) .await .expect("first bind succeeds"); assert_eq!(coordinator.registered_subwallets().await.len(), 1); @@ -662,7 +653,7 @@ async fn bind_after_wallet_removal_is_refused() { let persisted_before = persister.stored_count(); let err = wallet - .bind_shielded(&[0x42u8; 64], &[0], &coordinator) + .bind_shielded(&[0x42u8; 64], &[0]) .await .expect_err("a removed wallet must not re-register itself"); assert!( @@ -696,6 +687,9 @@ async fn add_account_rejects_a_viewing_key_change_for_a_persisted_account() { let persister = Arc::new(CapturingPersistence::default()); let wallet = platform_wallet_with(Arc::clone(&persister)).await; let coordinator = coordinator_at(&temp_dir("add_account_rekey")); + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; let seed = [0x42u8; 64]; // Durable rows: account 0 under this seed (so the bind agrees), @@ -721,7 +715,7 @@ async fn add_account_rejects_a_viewing_key_change_for_a_persisted_account() { persister.serve_viewing_keys(rows); wallet - .bind_shielded(&seed, &[0], &coordinator) + .bind_shielded(&seed, &[0]) .await .expect("bind of the matching account succeeds"); let persisted_before = persister.stored_count(); @@ -746,93 +740,6 @@ async fn add_account_rejects_a_viewing_key_change_for_a_persisted_account() { ); } -/// A bind that read the host snapshot before a Clear must not put that -/// snapshot back afterwards. -/// -/// The snapshot has to be read before the install transaction opens (the -/// seedless path reconstructs its keys from it, and a host callback must -/// not run under the coordinator's lifecycle mutex), so serializing the -/// transaction alone cannot cover this: Clear takes the mutex, finishes, -/// and the host then wipes its own rows — while a bind still holds the -/// pre-Clear notes and watermark in hand. Restoring them resurrects -/// history the user deleted and re-arms a watermark that reports -/// caught-up, so the cold rebuild from index 0 that Clear promises never -/// runs. -#[tokio::test(flavor = "multi_thread", worker_threads = 4)] -async fn bind_does_not_restore_a_snapshot_that_predates_a_clear() { - let persister = Arc::new(CapturingPersistence::default()); - let wallet = platform_wallet_with(Arc::clone(&persister)).await; - let coordinator = coordinator_at(&temp_dir("clear_during_bind")); - let id = SubwalletId::new(wallet.wallet_id(), 0); - - // Pre-Clear host state: one note and a high watermark. - let mut snapshot = BTreeMap::new(); - snapshot.insert( - id, - ShieldedSubwalletStartState { - notes: vec![crate::wallet::shielded::ShieldedNote { - position: 7, - cmx: [0x2A; 32], - nullifier: [0xE0; 32], - block_height: 100, - is_spent: false, - value: 1_000, - note_data: vec![0u8; 115], - }], - last_synced_index: 900_000, - ..Default::default() - }, - ); - persister.serve_subwallets(snapshot); - - // Park the bind inside load(): it has sampled the clear generation - // and is about to read the pre-Clear snapshot. - let release = persister.gate_next_load(); - let bind = { - let wallet = wallet.clone(); - let coordinator = Arc::clone(&coordinator); - tokio::spawn(async move { - wallet - .bind_shielded(&[0x42u8; 64], &[0], &coordinator) - .await - }) - }; - // Wait for the bind to actually REACH the gate. Without this - // handshake the Clear below could land before the bind samples the - // generation — a different ordering, which the fix is not about and - // which would make this test pass either way. - for _ in 0..400 { - if persister.load_entries() > 0 { - break; - } - tokio::time::sleep(std::time::Duration::from_millis(5)).await; - } - assert!( - persister.load_entries() > 0, - "bind never reached the persister load" - ); - - // The user taps Clear and it completes — store purged, registries - // dropped — then the host wipes its own rows. - coordinator.clear().await.expect("clear succeeds"); - - release.send(()).expect("release the parked load"); - bind.await.expect("bind task").expect("bind succeeds"); - - let store = coordinator.store().read().await; - assert!( - store.get_all_notes(id).unwrap().is_empty(), - "a snapshot read before the Clear must not be restored after it — those notes \ - are exactly what the user asked to delete" - ); - assert_eq!( - store.last_synced_note_index(id).unwrap(), - 0, - "the pre-Clear watermark must not come back; it would report caught-up and \ - suppress the cold rebuild from index 0" - ); -} - /// Two binds of one wallet must not interleave: neither may publish its /// registration while the other's transaction is open. That is what /// keeps the handle's key slot and the coordinator's registration from @@ -857,6 +764,9 @@ async fn a_second_bind_cannot_commit_inside_another_binds_transaction() { let persister = Arc::new(CapturingPersistence::default()); let wallet = platform_wallet_with(Arc::clone(&persister)).await; let coordinator = coordinator_at(&temp_dir("bind_interleave")); + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; let wallet_id = wallet.wallet_id(); // Establish the account registry without hydrating the wallet handle. @@ -890,12 +800,7 @@ async fn a_second_bind_cannot_commit_inside_another_binds_transaction() { let store_guard = coordinator.store().write().await; let bind_a = { let wallet = wallet.clone(); - let coordinator = Arc::clone(&coordinator); - tokio::spawn(async move { - wallet - .bind_shielded(&[0x42u8; 64], &[0], &coordinator) - .await - }) + tokio::spawn(async move { wallet.bind_shielded(&[0x42u8; 64], &[0]).await }) }; for _ in 0..200 { if !wallet.shielded_account_indices().await.is_empty() { @@ -913,12 +818,7 @@ async fn a_second_bind_cannot_commit_inside_another_binds_transaction() { // store lock: only the transaction can hold it back. let bind_b = { let wallet = wallet.clone(); - let coordinator = Arc::clone(&coordinator); - tokio::spawn(async move { - wallet - .bind_shielded(&[0x42u8; 64], &[0, 1], &coordinator) - .await - }) + tokio::spawn(async move { wallet.bind_shielded(&[0x42u8; 64], &[0, 1]).await }) }; tokio::time::sleep(std::time::Duration::from_millis(200)).await; assert_eq!( @@ -962,10 +862,13 @@ async fn add_account_does_not_hold_the_key_slot_across_host_persistence() { let persister = Arc::new(CapturingPersistence::default()); let wallet = platform_wallet_with(Arc::clone(&persister)).await; let coordinator = coordinator_at(&temp_dir("add_account_slot")); + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; let seed = [0x42u8; 64]; wallet - .bind_shielded(&seed, &[0], &coordinator) + .bind_shielded(&seed, &[0]) .await .expect("bind succeeds"); @@ -1014,9 +917,12 @@ async fn rebind_without_persisted_rows_reports_false_and_binds_nothing() { let persister = Arc::new(CapturingPersistence::default()); let wallet = platform_wallet_with(Arc::clone(&persister)).await; let coordinator = coordinator_at(&temp_dir("no_rows")); + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; let rebound = wallet - .bind_shielded_from_persisted(&[0], &coordinator) + .bind_shielded_from_persisted(&[0]) .await .expect("load succeeds with no rows"); assert!(!rebound, "no persisted rows → caller falls back to seed"); @@ -1035,7 +941,7 @@ async fn rebind_without_persisted_rows_reports_false_and_binds_nothing() { persister.serve_viewing_keys(rows); let rebound = wallet - .bind_shielded_from_persisted(&[0, 1], &coordinator) + .bind_shielded_from_persisted(&[0, 1]) .await .expect("load succeeds"); assert!( @@ -1049,10 +955,7 @@ async fn rebind_without_persisted_rows_reports_false_and_binds_nothing() { rows.insert(SubwalletId::new(wallet.wallet_id(), 0), vec![0u8; 5]); persister.serve_viewing_keys(rows); assert!( - wallet - .bind_shielded_from_persisted(&[0], &coordinator) - .await - .is_err(), + wallet.bind_shielded_from_persisted(&[0]).await.is_err(), "malformed persisted viewing key must surface as an error" ); } @@ -1063,9 +966,9 @@ async fn should_keep_wallet_and_coordinator_keys_when_guarded_registration_is_re let wallet = platform_wallet_with(Arc::clone(&persister)).await; let coordinator = coordinator_at(&temp_dir("bind_identity_guard")); wallet - .bind_shielded(&[0x42; 64], &[0], &coordinator) - .await - .unwrap(); + .attach_shielded_coordinator_for_test(&coordinator) + .await; + wallet.bind_shielded(&[0x42; 64], &[0]).await.unwrap(); let id = SubwalletId::new(wallet.wallet_id(), 0); coordinator .store() @@ -1086,7 +989,7 @@ async fn should_keep_wallet_and_coordinator_keys_when_guarded_registration_is_re ) .unwrap(); wallet - .bind_shielded(&[0x42; 64], &[1], &coordinator) + .bind_shielded(&[0x42; 64], &[1]) .await .expect_err("cannot remove the unresolved debit's owner"); assert_eq!(wallet.shielded_account_indices().await, vec![0]); @@ -1123,7 +1026,10 @@ async fn fresh_address_and_own_check_follow_the_bound_account() { let coordinator = coordinator_at(&temp_dir("fresh_address")); wallet - .bind_shielded(&[0x42u8; 64], &[0], &coordinator) + .attach_shielded_coordinator_for_test(&coordinator) + .await; + wallet + .bind_shielded(&[0x42u8; 64], &[0]) .await .expect("seed bind succeeds"); diff --git a/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain.rs b/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain.rs index c933429bb21..a53019f30ca 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain.rs @@ -206,6 +206,7 @@ mod tests { Arc::new(SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), crate::changeset::DurableCursors::default(), + Default::default(), ) } @@ -319,6 +320,7 @@ mod tests { Arc::new(SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), crate::changeset::DurableCursors::default(), + Default::default(), ); assert_eq!( wallet diff --git a/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain_tests.rs index b5ca65d2429..69c02248521 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain_tests.rs @@ -208,7 +208,7 @@ async fn wallet_fixture( ) -> ( PlatformWallet, Arc, - NetworkShieldedCoordinator, + Arc, TestDirectory, ) { let (wallet, persistence, coordinator, dir, _) = signed_wallet_fixture(lock, keys).await; @@ -223,7 +223,7 @@ async fn signed_wallet_fixture( ) -> ( PlatformWallet, Arc, - NetworkShieldedCoordinator, + Arc, TestDirectory, WalletSigner, ) { @@ -267,6 +267,7 @@ async fn signed_wallet_fixture( Arc::new(SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), crate::changeset::DurableCursors::default(), + Default::default(), ); manager .write() @@ -281,7 +282,15 @@ async fn signed_wallet_fixture( std::fs::create_dir_all(&dir.0).unwrap(); let path = dir.0.join("shielded.sqlite"); let store = FileBackedShieldedStore::open_path(&path, 100).unwrap(); - let coordinator = NetworkShieldedCoordinator::new(sdk, Network::Testnet, path, store); + let coordinator = Arc::new(NetworkShieldedCoordinator::new( + sdk, + Network::Testnet, + path, + store, + )); + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; (wallet, persistence, coordinator, dir, signer) } @@ -321,8 +330,14 @@ async fn restart_wallet( Arc::new(SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), crate::changeset::DurableCursors::default(), + Default::default(), ); *wallet.shielded_keys.write().await = old.shielded_keys.read().await.clone(); + if let Ok(coordinator) = old.shielded_coordinator().await { + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; + } wallet } @@ -341,8 +356,7 @@ async fn live_status(wallet: &PlatformWallet, out_point: &OutPoint) -> AssetLock async fn should_release_registration_after_returned_prebuild_load_failure() { let (mut lock, keys) = drain_lock_fixture(); lock.operation_id = None; - let (wallet, persistence, coordinator, _dir) = wallet_fixture(&lock, keys).await; - let coordinator = Arc::new(coordinator); + let (wallet, persistence, _, _dir) = wallet_fixture(&lock, keys).await; let (_, _, _, signer) = funded_wallet_manager(StandardAccountType::BIP44Account).await; let prover = super::prover::CachedOrchardProver::new(); let operation = Arc::new(AssetLockOperationContext::new([0x78; 16])); @@ -350,7 +364,6 @@ async fn should_release_registration_after_returned_prebuild_load_failure() { persistence.fail_load.store(failed_load, Ordering::SeqCst); let result = wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::DrainWalletBalance { account_index: 0, minimum_lock_duffs: None, @@ -393,14 +406,12 @@ async fn should_release_registration_after_returned_prebuild_load_failure() { async fn should_release_cancelled_drain_waiting_for_the_shield_guard() { let (mut lock, keys) = drain_lock_fixture(); lock.operation_id = None; - let (wallet, _, coordinator, _dir) = wallet_fixture(&lock, keys).await; - let coordinator = Arc::new(coordinator); + let (wallet, _, _, _dir) = wallet_fixture(&lock, keys).await; let (_, _, _, signer) = funded_wallet_manager(StandardAccountType::BIP44Account).await; let prover = super::prover::CachedOrchardProver::new(); let operation = Arc::new(AssetLockOperationContext::new([0x77; 16])); let blocker = wallet.shield_guard.lock().await; let mut future = Box::pin(wallet.shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::DrainWalletBalance { account_index: 0, minimum_lock_duffs: None, @@ -453,15 +464,13 @@ async fn should_refuse_drain_without_viewing_keys_before_core_funding() { let (mut lock, keys) = drain_lock_fixture(); lock.operation_id = None; let recipient = dpp::address_funds::OrchardAddress::from(keys[&0].default_address); - let (wallet, persistence, coordinator, _dir) = wallet_fixture(&lock, keys.clone()).await; + let (wallet, persistence, _, _dir) = wallet_fixture(&lock, keys.clone()).await; *wallet.shielded_keys.write().await = empty_binding.then(BTreeMap::new); - let coordinator = Arc::new(coordinator); let (_, _, _, signer) = funded_wallet_manager(StandardAccountType::BIP44Account).await; let prover = super::prover::CachedOrchardProver::new(); let operation = Arc::new(AssetLockOperationContext::new([0x97; 16])); let error = wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::DrainWalletBalance { account_index: 0, minimum_lock_duffs: None, @@ -838,16 +847,13 @@ async fn should_report_a_fresh_shield_lock_to_the_caller() { let (mut lock, keys) = drain_lock_fixture(); lock.operation_id = None; let recipient = dpp::address_funds::OrchardAddress::from(keys[&0].default_address); - let (wallet, _persistence, coordinator, _dir, signer) = - signed_wallet_fixture(&lock, keys).await; - let coordinator = Arc::new(coordinator); + let (wallet, _persistence, _, _dir, signer) = signed_wallet_fixture(&lock, keys).await; let prover = super::prover::CachedOrchardProver::new(); let seen = Mutex::new(Vec::new()); let observer = |out_point: OutPoint| seen.lock().unwrap().push(out_point); wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::FromWalletBalance { amount_duffs: 5_000_000, account_index: 0, @@ -883,9 +889,7 @@ async fn should_report_a_wallet_drain_lock_to_the_caller() { let (mut lock, keys) = drain_lock_fixture(); lock.operation_id = None; let recipient = dpp::address_funds::OrchardAddress::from(keys[&0].default_address); - let (wallet, _persistence, coordinator, _dir, signer) = - signed_wallet_fixture(&lock, keys).await; - let coordinator = Arc::new(coordinator); + let (wallet, _persistence, _, _dir, signer) = signed_wallet_fixture(&lock, keys).await; let prover = super::prover::CachedOrchardProver::new(); let operation = Arc::new(AssetLockOperationContext::new([0x5a; 16])); let seen = Mutex::new(Vec::new()); @@ -893,7 +897,6 @@ async fn should_report_a_wallet_drain_lock_to_the_caller() { wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::DrainWalletBalance { account_index: 0, minimum_lock_duffs: None, @@ -929,14 +932,12 @@ async fn should_refuse_a_generic_resume_to_a_recipient_other_than_the_recorded_o let recorded = crate::test_support::orchard_address_fixture(0x51); let other = crate::test_support::orchard_address_fixture(0x52); lock.shielded_recipient = Some(recorded.to_raw_bytes()); - let (wallet, persistence, coordinator, _dir) = wallet_fixture(&lock, keys).await; - let coordinator = Arc::new(coordinator); + let (wallet, persistence, _, _dir) = wallet_fixture(&lock, keys).await; let (_, _, _, signer) = funded_wallet_manager(StandardAccountType::BIP44Account).await; let prover = super::prover::CachedOrchardProver::new(); let error = wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::FromExistingAssetLock { out_point: lock.out_point, consume_invitation_voucher: false, @@ -984,15 +985,13 @@ async fn should_refuse_a_repeated_uuid_drain_to_a_recipient_other_than_the_recor let recorded = crate::test_support::orchard_address_fixture(0x53); let other = crate::test_support::orchard_address_fixture(0x54); lock.shielded_recipient = Some(recorded.to_raw_bytes()); - let (wallet, persistence, coordinator, _dir) = wallet_fixture(&lock, keys).await; - let coordinator = Arc::new(coordinator); + let (wallet, persistence, _, _dir) = wallet_fixture(&lock, keys).await; let (_, _, _, signer) = funded_wallet_manager(StandardAccountType::BIP44Account).await; let prover = super::prover::CachedOrchardProver::new(); let operation = Arc::new(AssetLockOperationContext::new(OPERATION_ID)); let error = wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::DrainWalletBalance { account_index: 0, minimum_lock_duffs: None, @@ -1043,14 +1042,12 @@ async fn should_accept_the_callers_recipient_for_a_legacy_row_without_one() { lock.operation_id = None; assert_eq!(lock.shielded_recipient, None); let recipient = crate::test_support::orchard_address_fixture(0x55); - let (wallet, _persistence, coordinator, _dir) = wallet_fixture(&lock, keys).await; - let coordinator = Arc::new(coordinator); + let (wallet, _persistence, _, _dir) = wallet_fixture(&lock, keys).await; let (_, _, _, signer) = funded_wallet_manager(StandardAccountType::BIP44Account).await; let prover = super::prover::CachedOrchardProver::new(); let error = wallet .shielded_fund_from_asset_lock( - &coordinator, AssetLockFunding::FromExistingAssetLock { out_point: lock.out_point, consume_invitation_voucher: false, @@ -1084,10 +1081,8 @@ async fn should_record_the_recipient_with_the_lock_before_dispatch() { let (mut lock, keys) = drain_lock_fixture(); lock.operation_id = None; let recipient = crate::test_support::orchard_address_fixture(0x56); - let (wallet, persistence, coordinator, _dir, signer) = - signed_wallet_fixture(&lock, keys).await; + let (wallet, persistence, _, _dir, signer) = signed_wallet_fixture(&lock, keys).await; persistence.commits_inline.store(drain, Ordering::SeqCst); - let coordinator = Arc::new(coordinator); let prover = super::prover::CachedOrchardProver::new(); let operation = Arc::new(AssetLockOperationContext::new([0x57; 16])); let seen = Mutex::new(Vec::new()); @@ -1106,7 +1101,6 @@ async fn should_record_the_recipient_with_the_lock_before_dispatch() { }; wallet .shielded_fund_from_asset_lock( - &coordinator, funding, vec![(recipient, None)], &signer, @@ -1141,7 +1135,6 @@ async fn should_record_the_recipient_with_the_lock_before_dispatch() { /// error and the outpoints the caller was told about. async fn fund_typed_amount( wallet: &PlatformWallet, - coordinator: &Arc, signer: &WalletSigner, recipient: dpp::address_funds::OrchardAddress, ) -> (PlatformWalletError, Vec) { @@ -1150,7 +1143,6 @@ async fn fund_typed_amount( let observer = |out_point: OutPoint| seen.lock().unwrap().push(out_point); let error = wallet .shielded_fund_from_asset_lock( - coordinator, AssetLockFunding::FromWalletBalance { amount_duffs: 5_000_000, account_index: 0, @@ -1181,9 +1173,7 @@ async fn should_abort_typed_amount_funding_when_the_recipient_cannot_be_made_dur let (mut lock, keys) = drain_lock_fixture(); lock.operation_id = None; let recipient = crate::test_support::orchard_address_fixture(0x58); - let (wallet, persistence, coordinator, _dir, signer) = - signed_wallet_fixture(&lock, keys).await; - let coordinator = Arc::new(coordinator); + let (wallet, persistence, _, _dir, signer) = signed_wallet_fixture(&lock, keys).await; persistence.commits_inline.store(false, Ordering::SeqCst); let switch = if fail_flush { &persistence.fail_flush @@ -1192,7 +1182,7 @@ async fn should_abort_typed_amount_funding_when_the_recipient_cannot_be_made_dur }; switch.store(true, Ordering::SeqCst); - let (error, seen) = fund_typed_amount(&wallet, &coordinator, &signer, recipient).await; + let (error, seen) = fund_typed_amount(&wallet, &signer, recipient).await; assert!( matches!(&error, PlatformWalletError::Persistence(message) if message.contains("shielded recipient was not made durable")), @@ -1210,7 +1200,7 @@ async fn should_abort_typed_amount_funding_when_the_recipient_cannot_be_made_dur // The inputs were handed back: the retry builds a lock again and gets // as far as the broadcaster. switch.store(false, Ordering::SeqCst); - let (error, retried) = fund_typed_amount(&wallet, &coordinator, &signer, recipient).await; + let (error, retried) = fund_typed_amount(&wallet, &signer, recipient).await; assert!( !matches!(error, PlatformWalletError::Persistence(_)), "fail_flush={fail_flush}: {error}" @@ -1235,14 +1225,13 @@ async fn should_keep_best_effort_dispatch_on_a_backend_without_recipient_support let (mut lock, keys) = drain_lock_fixture(); lock.operation_id = None; let recipient = crate::test_support::orchard_address_fixture(0x59); - let (wallet, persistence, coordinator, _dir, signer) = signed_wallet_fixture(&lock, keys).await; - let coordinator = Arc::new(coordinator); + let (wallet, persistence, _, _dir, signer) = signed_wallet_fixture(&lock, keys).await; persistence .records_recipients .store(false, Ordering::SeqCst); persistence.fail_store.store(true, Ordering::SeqCst); - let (error, seen) = fund_typed_amount(&wallet, &coordinator, &signer, recipient).await; + let (error, seen) = fund_typed_amount(&wallet, &signer, recipient).await; assert!( !matches!(error, PlatformWalletError::Persistence(_)), diff --git a/packages/rs-unified-sdk-jni/src/persistence.rs b/packages/rs-unified-sdk-jni/src/persistence.rs index 86a38c3329e..5c17fcd022a 100644 --- a/packages/rs-unified-sdk-jni/src/persistence.rs +++ b/packages/rs-unified-sdk-jni/src/persistence.rs @@ -265,6 +265,12 @@ pub(crate) fn build_extension(env: &mut JNIEnv, bridge: &JObject) -> Persistence "onPersistContactUpsert", CONTACT_UPSERT_DESCRIPTOR, ), + shielded_state_wipe: bridge_overrides( + env, + bridge, + "onWipeShieldedState", + WIPE_SHIELDED_STATE_DESCRIPTOR, + ), }) } @@ -328,6 +334,10 @@ struct BridgeOverrides { /// vouches for the contact requests read before it, which an inherited /// no-op contact upsert never keeps. private_contact_scan_state: bool, + /// `onWipeShieldedState`: an inherited body deletes nothing, so wiring + /// it would let Clear report a host wipe that never happened. + #[cfg_attr(not(feature = "shielded"), allow(dead_code))] + shielded_state_wipe: bool, } /// The extension slots wired for a bridge with `overrides` (see @@ -363,6 +373,10 @@ fn extension_for(overrides: BridgeOverrides) -> PersistenceCallbacksExtension { on_load_private_contact_scan_state_wallets_free_fn: overrides .private_contact_scan_state .then_some(tramp_load_private_contact_scan_state_wallets_free), + #[cfg(feature = "shielded")] + on_wipe_shielded_state_fn: overrides + .shielded_state_wipe + .then_some(tramp_wipe_shielded_state), ..Default::default() } } @@ -2496,6 +2510,28 @@ unsafe extern "C" fn tramp_persist_shielded_viewing_keys( }) } +/// Descriptor of `NativePersistenceBridge.onWipeShieldedState`: the +/// `FFINetwork` discriminant (`Network.ffiValue`). +const WIPE_SHIELDED_STATE_DESCRIPTOR: &str = "(I)I"; + +/// Delete the host's shielded rows for `network` (see +/// `WipeShieldedStateFn`); native Clear calls it once per successful reset. +#[cfg(feature = "shielded")] +unsafe extern "C" fn tramp_wipe_shielded_state( + context: *mut c_void, + network: platform_wallet_ffi::FFINetwork, +) -> i32 { + with_bridge(context, |env, bridge| { + env.call_method( + bridge, + "onWipeShieldedState", + WIPE_SHIELDED_STATE_DESCRIPTOR, + &[JValue::Int(network as i32)], + )? + .i() + }) +} + // ── Load: wallet list ───────────────────────────────────────────────── /// Staged wallet-list row: all JNI reads land in owned Rust memory, so a @@ -5288,6 +5324,8 @@ const BRIDGE_METHOD_TABLE: &[(&str, &str)] = &[ ), #[cfg(feature = "shielded")] ("onPersistShieldedViewingKey", "([B[BI[B)I"), + #[cfg(feature = "shielded")] + ("onWipeShieldedState", WIPE_SHIELDED_STATE_DESCRIPTOR), ( "onLoadWalletList", "()[Lorg/dashfoundation/dashsdk/ffi/WalletRestoreData;", @@ -5473,6 +5511,7 @@ mod tests { header, backfill, private_contact_scan_state: false, + shielded_state_wipe: false, }); assert_eq!( extension.on_persist_wallet_dashpay_backfill_fn.is_some(), @@ -5485,11 +5524,30 @@ mod tests { header: false, backfill: false, private_contact_scan_state: false, + shielded_state_wipe: false, }); assert!(sweeps_only.on_persist_wallet_changeset_sweeps_fn.is_some()); assert!(sweeps_only.on_persist_wallet_dashpay_backfill_fn.is_none()); } + /// The shielded wipe slot is wired only for a bridge that overrides + /// `onWipeShieldedState`; without it `FFIPersister` does not attest + /// `SHIELDED_STATE_WIPE` and native Clear refuses to reset anything. + #[cfg(feature = "shielded")] + #[test] + fn the_shielded_wipe_slot_is_wired_only_for_a_bridge_that_overrides_it() { + for wired in [false, true] { + let extension = extension_for(BridgeOverrides { + sweeps: false, + header: false, + backfill: false, + private_contact_scan_state: false, + shielded_state_wipe: wired, + }); + assert_eq!(extension.on_wipe_shielded_state_fn.is_some(), wired); + } + } + /// The scan-state trio is wired only for a bridge that implements both /// its store and its load; one that inherits either gets none of it, so /// `FFIPersister` does not attest the capability and each launch reads @@ -5502,6 +5560,7 @@ mod tests { header: false, backfill: false, private_contact_scan_state: wired, + shielded_state_wipe: false, }); assert_eq!( extension.on_persist_private_contact_scan_state_fn.is_some(), diff --git a/packages/rs-unified-sdk-jni/src/wallet_manager.rs b/packages/rs-unified-sdk-jni/src/wallet_manager.rs index bd7aa453525..e018071ae62 100644 --- a/packages/rs-unified-sdk-jni/src/wallet_manager.rs +++ b/packages/rs-unified-sdk-jni/src/wallet_manager.rs @@ -3314,15 +3314,13 @@ pub extern "system" fn Java_org_dashfoundation_dashsdk_ffi_WalletManagerNative_s }) } -/// Reset the Rust-side shielded state on this manager: quiesce the -/// background sync loop, drop every wallet registration on the -/// network-scoped coordinator, empty the shared commitment tree, and -/// reset the caught-up cooldown. The per-network SQLite file stays on -/// disk but its contents are reset so the next `shieldedBind` + -/// sync cold-rebuilds from index 0. Throws `DashSDKException` on a -/// store-reset failure — the host must NOT wipe its own persistence -/// unless this succeeds. Mirrors Swift's -/// `PlatformWalletManager.clearShieldedStorage()`. +/// Reset this manager's shielded state: quiesce the background sync loop, +/// drop every wallet registration on the network-scoped coordinator, empty +/// the shared commitment tree, reset the caught-up cooldown, and have the +/// persistence bridge delete the network's shielded rows +/// (`onWipeShieldedState`) before returning. Throws `DashSDKException` when +/// the bridge cannot wipe (nothing reset) or any step fails. Mirrors Swift's +/// `PlatformWalletManager.clearShielded()`. #[cfg(feature = "shielded")] #[no_mangle] pub extern "system" fn Java_org_dashfoundation_dashsdk_ffi_WalletManagerNative_shieldedClear( diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift index f512c31e473..2f5370964ad 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManager.swift @@ -126,6 +126,11 @@ public struct PlatformWalletPersistenceCapabilities: Equatable, Sendable { /// only honours the declaration when the extension carries the five /// scan-state callbacks. public static let privateContactRequestScanState: UInt64 = 1 << 15 + /// The store deletes its shielded rows for a network when native Clear + /// asks (`on_wipe_shielded_state_fn`), which `clearShielded()` requires. + /// Mirrors `PersistenceCapabilities::SHIELDED_STATE_WIPE`; Rust only + /// honours the declaration when the wipe callback is wired. + public static let shieldedStateWipe: UInt64 = 1 << 16 public let version: UInt32 public let bits: UInt64 diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedSync.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedSync.swift index 5befbfc41ec..56cda141abf 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedSync.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedSync.swift @@ -441,20 +441,18 @@ extension PlatformWalletManager { } } - /// Reset the Rust-side shielded state on this manager: - /// stops the background sync loop, drops every wallet - /// registration on the network-scoped coordinator, and - /// resets the caught-up cooldown stamp. + /// Reset this manager's shielded state: stops the background sync loop, + /// drops every wallet registration on the network-scoped coordinator, + /// empties the shared commitment tree, resets the caught-up cooldown + /// stamp, and has the persistence handler delete every shielded + /// SwiftData row of the network's wallets before returning, with every + /// other shielded operation held out. The host deletes nothing itself. + /// The per-network SQLite file stays open; the next [`bindShielded`] + /// repopulates the coordinator and the next sync rebuilds from index 0. /// - /// Use this from the host's "Clear" flow before wiping - /// host-side persistence (e.g. SwiftData rows). The single - /// per-network SQLite commitment-tree file stays open — - /// Clear semantics are "wipe my host persistence and - /// re-sync from index 0 on the shared tree", not "blow - /// away the chain-wide cache". After Clear, the next - /// [`bindShielded`] call repopulates the coordinator's - /// registries and the next sync pass re-saves notes via - /// the changeset path. + /// Throws when the handler cannot wipe (nothing is reset), when the + /// native reset fails, or when the SwiftData wipe fails after it; in the + /// last case binds are refused until a retried Clear succeeds. /// /// Throws `walletOperation` while an async [`stopShieldedSync()`] is in /// flight: Clear quiesces the same loop and would wait on the calling @@ -467,8 +465,8 @@ extension PlatformWalletManager { } // The Rust drain returned; bump the generation so any trailing // completion event the main actor delivers after Clear is dropped - // (it would otherwise briefly repopulate the mirror the host is - // about to wipe). + // (it would otherwise briefly repopulate the mirror Clear just + // wiped). shieldedSyncGeneration.bump() // The dropped completion would normally clear the per-pass // progress mirrors; do it here so a pass cleared mid-flight diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index 3a3568ec2f2..5543a04fb0e 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -3384,6 +3384,7 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { | PlatformWalletPersistenceCapabilities.assetLockOperationIds | PlatformWalletPersistenceCapabilities.assetLockShieldedRecipients | PlatformWalletPersistenceCapabilities.privateContactRequestScanState + | PlatformWalletPersistenceCapabilities.shieldedStateWipe ) } @@ -3435,6 +3436,7 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { loadPrivateContactScanStateWalletsCallback extensionCallbacks.on_load_private_contact_scan_state_wallets_free_fn = loadPrivateContactScanStateWalletsFreeCallback + extensionCallbacks.on_wipe_shielded_state_fn = wipeShieldedStateCallback return extensionCallbacks } @@ -6276,6 +6278,41 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { return Set(wallets.map { $0.walletId }) } + /// Native Clear's host half (`on_wipe_shielded_state_fn`): delete the + /// shielded rows of every wallet persisted on `network`, loaded or not, + /// in one save. A failed read or save rolls the context back and + /// throws, leaving the rows in place. + func wipeShieldedState(network: Network) throws { + try onQueue { + let networkRaw = network.rawValue + do { + let wallets = try backgroundContext.fetch(FetchDescriptor( + predicate: #Predicate { $0.networkRaw == networkRaw })) + for walletId in Set(wallets.map(\.walletId)) { + try backgroundContext.delete( + model: PersistentShieldedNote.self, + where: #Predicate { $0.walletId == walletId }) + try backgroundContext.delete( + model: PersistentShieldedOutgoingNote.self, + where: #Predicate { $0.walletId == walletId }) + try backgroundContext.delete( + model: PersistentShieldedSyncState.self, + where: #Predicate { $0.walletId == walletId }) + try backgroundContext.delete( + model: PersistentShieldedActivity.self, + where: #Predicate { $0.walletId == walletId }) + try backgroundContext.delete( + model: PersistentShieldedViewingKey.self, + where: #Predicate { $0.walletId == walletId }) + } + try backgroundContext.save() + } catch { + backgroundContext.rollback() + throw error + } + } + } + /// Build the host-allocated `ShieldedNoteRestoreFFI` array Rust /// reads at boot. The allocation is tracked in /// `shieldedLoadAllocations` and freed by @@ -12528,6 +12565,20 @@ private func loadPrivateContactScanStateWalletsFreeCallback( buffer.deallocate() } +/// C shim for `on_wipe_shielded_state_fn`: 0 once the network's shielded +/// rows are gone, non-zero with them in place. +private func wipeShieldedStateCallback( + context: UnsafeMutableRawPointer?, + network: FFINetwork +) -> Int32 { + guard let context else { return -1 } + let handler = Unmanaged.fromOpaque(context).takeUnretainedValue() + do { + try handler.wipeShieldedState(network: Network(ffiNetwork: network)) + return 0 + } catch { return -1 } +} + private func loadIdentityBalanceBlockTimeCallback( context: UnsafeMutableRawPointer?, walletId: UnsafePointer?, identityId: UnsafePointer?, outFound: UnsafeMutablePointer?, outBlockTime: UnsafeMutablePointer? diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Services/ShieldedService.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Services/ShieldedService.swift index d76f2fe1745..8cc3f9a786b 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Services/ShieldedService.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Services/ShieldedService.swift @@ -720,20 +720,21 @@ class ShieldedService: ObservableObject { syncTickTimer = nil } - /// Wipe every wallet's persisted shielded state and stop. The - /// service is left unbound, but the stashed bind credentials - /// (`walletManager` / `boundWalletId` / `network` / `resolver` - /// / `boundAccounts`) survive so [`manualSync`] can rebind on - /// demand without the user navigating away. The "Sync Now" - /// button on the Sync Status screen is the path back — - /// pressing it self-binds + syncs from a clean SQLite tree - /// and an empty SwiftData snapshot. + /// Wipe the network's shielded state and stop. The service is left + /// unbound, but the stashed bind credentials (`walletManager` / + /// `boundWalletId` / `network` / `resolver` / `boundAccounts`) + /// survive so [`manualSync`] can rebind on demand without the user + /// navigating away. The "Sync Now" button on the Sync Status screen + /// is the path back — pressing it self-binds + syncs from a clean + /// SQLite tree and an empty SwiftData snapshot. /// - /// The user reaches this through the Clear button on the - /// **global** Sync Status surface, not a per-wallet screen. - /// "Clear" therefore wipes every wallet's shielded rows and - /// empties the per-network commitment tree, so the rebind on - /// the next Sync Now walks the cmx stream from genesis. + /// The user reaches this through the Clear button on the **global** + /// Sync Status surface, not a per-wallet screen. `clearShielded()` + /// resets every wallet's shielded state on the manager's network and, + /// before returning, has the persistence handler delete every + /// shielded SwiftData row of that network's wallets + /// (`PlatformWalletPersistenceHandler.wipeShieldedState`). Nothing is + /// deleted here, so a sync or bind cannot land rows between the two. /// /// What survives the reset: /// * The per-network commitment-tree SQLite file at @@ -747,13 +748,7 @@ class ShieldedService: ObservableObject { /// inline soft-cleanup below only zeroes the published /// mirror; [`canResume`] therefore stays `true` and the /// Sync Now button stays usable. - /// - /// Per-wallet scoping was tried first and rejected because the - /// Clear button doesn't carry wallet context — other wallets' - /// `PersistentShieldedSyncState` rows would silently survive - /// (the symptom the user reported when "Clear" left a row - /// behind for a non-active wallet). - func clearLocalState(modelContext: ModelContext) async { + func clearLocalState() async { // Capture the manager before the soft-cleanup below // touches anything, so we can stop the background loop // first. (We used to capture `network` here too for the @@ -766,57 +761,14 @@ class ShieldedService: ObservableObject { fields: ["wallet_count": .integer(Int64(walletManager?.wallets.count ?? 0))] ) - // 1) Reset the Rust-side shielded state BEFORE touching - // state on disk. The Swift `ShieldedService` is - // per-wallet-at-a-time, but the Rust - // `PlatformWalletManager` keeps **every** wallet that - // ever ran `bind_shielded` registered on the - // network-scoped coordinator. Without this call the - // coordinator's next sync iterates the still-registered - // wallets and the persister callback immediately - // re-creates the `PersistentShieldedNote` / - // `PersistentShieldedSyncState` rows we're about to - // delete (the "Clear left a row behind / re-derived a - // fresh row" symptom from before). `clearShielded` - // does three things on the Rust side in one call: - // - stops the background sync loop - // - drops every wallet registration from the - // coordinator (`accounts` + `persisters` maps) - // - resets the network-wide caught-up cooldown - // The single SQLite commitment-tree file stays open; - // the next `bindShielded` call repopulates the - // registries and the next sync re-saves notes via - // the changeset path. This reset is load-bearing: if it - // cannot run, abort the host-row wipe so the tree file and - // SwiftData rows cannot diverge. - // - // Re-binding scope after Clear: `clearShielded` drops - // EVERY wallet (not just the mirror's `firstWallet`) - // from the coordinator. "Sync Now" (`manualSync()`) - // UNCONDITIONALLY re-registers EVERY loaded wallet on each - // tap: the mirror wallet via `bind(...)` (in the recovery - // branch, only when unbound), and every OTHER loaded wallet - // via a `engineBindOtherWallets` / `bindEngine` pass that - // runs on every Sync Now regardless of the recovery branch. - // That unconditional pass matters because a detail-view - // `switchTo` between Clear and Sync Now re-binds only the - // viewed wallet (flipping `isBound` true and skipping the - // recovery branch), which would otherwise leave the other - // wallets engine-unregistered. The pass is also best-effort - // across the mirror: a mirror-bind FAILURE (missing mnemonic - // / declined resolver) no longer bails Sync Now — the engine - // pass still runs so every OTHER wallet with an intact - // mnemonic re-registers, and Sync Now only bails when NOTHING - // registered (mirror + every other bind failed). So - // cross-wallet shielded flows (SH-14/15/16) come back - // immediately on the first post-Clear Sync Now, not only on - // the next `rebindWalletScopedServices()` fire. - // `rebindWalletScopedServices` remains the recovery path for - // wallets loaded LATER (a wallet added after the Clear isn't - // in the manager's set at Sync-Now time); it re-`bindEngine`s - // every wallet on any wallet-set change or network switch. We - // keep the WIPE scope global on purpose (see the class-level - // doc below) — this note is about the re-BIND scope. + // Re-binding scope after Clear: `clearShielded` drops EVERY + // wallet (not just the mirror's `firstWallet`) from the + // coordinator. "Sync Now" (`manualSync()`) re-registers every + // loaded wallet on each tap: the mirror wallet via `bind(...)` + // (in the recovery branch, only when unbound), and every OTHER + // loaded wallet via the `engineBindOtherWallets` / `bindEngine` + // pass, best-effort across the mirror. `rebindWalletScopedServices` + // remains the recovery path for wallets loaded LATER. prepareForShieldedRebind() guard let managerForStop else { lastError = "Failed to reset shielded state: no wallet manager is bound." @@ -830,52 +782,20 @@ class ShieldedService: ObservableObject { } do { - try Self.executeClearPersistenceSequence( - resetRustState: { - try managerForStop.clearShielded() - }, - clearHostState: { - // Delete every shielded SwiftData row across all - // wallets on this device. The Clear button is on the - // global Sync Status surface, so this is intentionally - // global rather than wallet-scoped. - try modelContext.delete(model: PersistentShieldedNote.self) - try modelContext.delete(model: PersistentShieldedOutgoingNote.self) - try modelContext.delete(model: PersistentShieldedSyncState.self) - try modelContext.delete(model: PersistentShieldedActivity.self) - // Viewing keys are included so a corrupted row cannot - // outlive Clear; the next bind re-persists them. - try modelContext.delete(model: PersistentShieldedViewingKey.self) - try modelContext.save() - } - ) + try managerForStop.clearShielded() } catch { - let phase: String - let underlyingError: Error - switch error { - case .rustReset(let error): - phase = "rust_state" - underlyingError = error - lastError = - "Failed to reset shielded state: \(error.localizedDescription)" - case .hostPersistence(let error): - phase = "host_persistence" - underlyingError = error - lastError = - "Failed to wipe persisted shielded state: " - + error.localizedDescription - } + lastError = "Failed to reset shielded state: \(error.localizedDescription)" SDKLogger.event( "shielded_reset_failed", category: .shielded, severity: .error, - fields: ["phase": .publicText(phase)], - error: underlyingError + fields: ["phase": .publicText("clear")], + error: error ) return } - // 3) Finish zeroing the published mirror. The subscriptions + // Finish zeroing the published mirror. The subscriptions // were cancelled before the reset attempt. Keep the bind credentials // (walletManager / boundWalletId / network / resolver // / boundAccounts) so [`manualSync`] can re-bind on @@ -900,30 +820,6 @@ class ShieldedService: ObservableObject { longestSyncDuration = nil } - enum ClearLocalStateFailure: Error { - case rustReset(Error) - case hostPersistence(Error) - } - - /// Enforces reset-before-delete ordering for the two persistence halves. - /// Kept separate from the user-facing operation so tests can exercise - /// failure ordering without exposing a reset bypass on `clearLocalState`. - static func executeClearPersistenceSequence( - resetRustState: () throws -> Void, - clearHostState: () throws -> Void - ) throws(ClearLocalStateFailure) { - do { - try resetRustState() - } catch { - throw ClearLocalStateFailure.rustReset(error) - } - do { - try clearHostState() - } catch { - throw ClearLocalStateFailure.hostPersistence(error) - } - } - /// A clear attempt quiesces Rust before it can report success or failure, /// and a store failure may occur after one reset half has already landed. /// Treat every attempt as requiring a fresh bind while retaining the diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/CoreContentView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/CoreContentView.swift index 4c7bfa7282a..cf7ec6d6ae0 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/CoreContentView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/CoreContentView.swift @@ -12,9 +12,9 @@ struct CoreContentView: View { @EnvironmentObject var appUIState: AppUIState @EnvironmentObject var platformBalanceSyncService: PlatformBalanceSyncService @EnvironmentObject var shieldedService: ShieldedService - /// Threaded into `ShieldedService.clearLocalState(modelContext:)` - /// so the Clear button can scope its delete-by-predicate to - /// the bound wallet's persisted rows. + /// Threaded into `PlatformBalanceSyncService.clearLocalState` so the + /// Clear button can scope its delete-by-predicate to the network's + /// persisted rows. @Environment(\.modelContext) private var modelContext @State private var showProofDetail = false @State private var masternodesEnabled: Bool = true @@ -743,9 +743,7 @@ var body: some View { // handle open via FileBackedShieldedStore; // see clearLocalState's doc). Task { - await shieldedService.clearLocalState( - modelContext: modelContext - ) + await shieldedService.clearLocalState() } } label: { Text("Clear") diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleAppTests/ShieldedServiceClearTests.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleAppTests/ShieldedServiceClearTests.swift index 97c58f2eaee..d5d9d30d834 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleAppTests/ShieldedServiceClearTests.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleAppTests/ShieldedServiceClearTests.swift @@ -6,26 +6,28 @@ import XCTest @MainActor final class ShieldedServiceClearTests: XCTestCase { - private struct ResetFailure: LocalizedError { - var errorDescription: String? { "injected Rust reset failure" } - } - - private struct HostPersistenceFailure: LocalizedError { - var errorDescription: String? { "injected host persistence failure" } - } - - /// Without a manager there is no way to clear the Rust coordinator/tree. - /// The host rows must survive so a failed reset cannot create split-brain - /// persistence where SwiftData is empty but the tree file is not. + /// Without a manager there is no native Clear to run, and the service + /// deletes nothing itself: the host rows survive and the error surfaces. func testClearLocalState_withoutWalletManager_preservesRows() async throws { let container = try DashModelContainer.createInMemory() - let context = try contextWithSyncState(in: container) + let context = ModelContext(container) + context.insert( + PersistentShieldedSyncState( + walletId: Data(repeating: 0x42, count: 32), + accountIndex: 0, + lastSyncedIndex: 123 + ) + ) + try context.save() let service = ShieldedService() service.isBound = true - await service.clearLocalState(modelContext: context) + await service.clearLocalState() - XCTAssertEqual(try fetchSyncStates(in: container).count, 1) + XCTAssertEqual( + try ModelContext(container).fetch(FetchDescriptor()).count, + 1 + ) XCTAssertFalse( service.isBound, "A failed clear must require a fresh Rust binding before the next sync" @@ -35,96 +37,4 @@ final class ShieldedServiceClearTests: XCTestCase { "Failed to reset shielded state: no wallet manager is bound." ) } - - /// A throwing `clearShielded()` equivalent is also load-bearing: surface - /// the error and leave every host row intact for a safe retry. - func testClearSequence_whenRustResetThrows_preservesRows() throws { - let container = try DashModelContainer.createInMemory() - let context = try contextWithSyncState(in: container) - var didClearHostState = false - - XCTAssertThrowsError( - try ShieldedService.executeClearPersistenceSequence( - resetRustState: { throw ResetFailure() }, - clearHostState: { - didClearHostState = true - try clearSyncStates(in: context) - } - ) - ) { thrownError in - guard case ShieldedService.ClearLocalStateFailure.rustReset(let error) = - thrownError - else { - return XCTFail("Expected the Rust reset failure to be preserved") - } - XCTAssertEqual(error.localizedDescription, "injected Rust reset failure") - } - - XCTAssertFalse(didClearHostState) - XCTAssertEqual(try fetchSyncStates(in: container).count, 1) - } - - /// Once the Rust reset succeeds, the host wipe proceeds normally. - func testClearSequence_afterRustResetSucceeds_deletesRows() throws { - let container = try DashModelContainer.createInMemory() - let context = try contextWithSyncState(in: container) - var didResetRustState = false - - try ShieldedService.executeClearPersistenceSequence( - resetRustState: { didResetRustState = true }, - clearHostState: { try clearSyncStates(in: context) } - ) - - XCTAssertTrue(didResetRustState) - XCTAssertTrue(try fetchSyncStates(in: container).isEmpty) - } - - /// A host failure after Rust reset must remain distinguishable so the - /// service can report it while requiring a fresh binding. - func testClearSequence_whenHostPersistenceThrows_reportsPostResetFailure() throws { - let container = try DashModelContainer.createInMemory() - _ = try contextWithSyncState(in: container) - var didResetRustState = false - - XCTAssertThrowsError( - try ShieldedService.executeClearPersistenceSequence( - resetRustState: { didResetRustState = true }, - clearHostState: { throw HostPersistenceFailure() } - ) - ) { thrownError in - guard case ShieldedService.ClearLocalStateFailure.hostPersistence(let error) = - thrownError - else { - return XCTFail("Expected the host persistence failure to be preserved") - } - XCTAssertEqual(error.localizedDescription, "injected host persistence failure") - } - - XCTAssertTrue(didResetRustState) - XCTAssertEqual(try fetchSyncStates(in: container).count, 1) - } - - private func contextWithSyncState(in container: ModelContainer) throws -> ModelContext { - let context = ModelContext(container) - context.insert( - PersistentShieldedSyncState( - walletId: Data(repeating: 0x42, count: 32), - accountIndex: 0, - lastSyncedIndex: 123 - ) - ) - try context.save() - return context - } - - private func fetchSyncStates( - in container: ModelContainer - ) throws -> [PersistentShieldedSyncState] { - try ModelContext(container).fetch(FetchDescriptor()) - } - - private func clearSyncStates(in context: ModelContext) throws { - try context.delete(model: PersistentShieldedSyncState.self) - try context.save() - } } diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/InvitationPersistenceTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/InvitationPersistenceTests.swift index 036693cab39..7b2e06cad47 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/InvitationPersistenceTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/InvitationPersistenceTests.swift @@ -75,6 +75,8 @@ final class InvitationPersistenceTests: XCTestCase { // persist/load/free trio onto `PersistentPrivateContactRequestsPosition` // and `PersistentPrivateShieldedAddress`. | PlatformWalletPersistenceCapabilities.privateContactRequestScanState + // Shielded state wipe: the handler wires `on_wipe_shielded_state_fn`. + | PlatformWalletPersistenceCapabilities.shieldedStateWipe XCTAssertEqual( capabilities.version, diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ShieldedStateWipeTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ShieldedStateWipeTests.swift new file mode 100644 index 00000000000..b73d4521b62 --- /dev/null +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ShieldedStateWipeTests.swift @@ -0,0 +1,59 @@ +import SwiftData +import XCTest +import DashSDKFFI + +@testable import SwiftDashSDK + +/// The host half of native shielded Clear: the wipe callback Rust calls +/// after its own reset. +final class ShieldedStateWipeTests: XCTestCase { + private let testnetWallet = Data(repeating: 0x61, count: 32) + private let mainnetWallet = Data(repeating: 0x62, count: 32) + + private func seed(_ container: ModelContainer) throws { + let context = ModelContext(container) + context.insert(PersistentWallet(walletId: testnetWallet, network: .testnet)) + context.insert(PersistentWallet(walletId: mainnetWallet, network: .mainnet)) + for walletId in [testnetWallet, mainnetWallet] { + context.insert(PersistentShieldedSyncState( + walletId: walletId, accountIndex: 0, lastSyncedIndex: 42)) + context.insert(PersistentShieldedViewingKey( + walletId: walletId, accountIndex: 0, fvkBytes: Data(repeating: 1, count: 96))) + } + try context.save() + } + + private func walletIds( + _ type: T.Type, _ container: ModelContainer, _ walletId: KeyPath + ) throws -> [Data] { + try ModelContext(container).fetch(FetchDescriptor()).map { $0[keyPath: walletId] } + } + + func testShouldDeclareTheCapabilityAndWireTheCallback() throws { + let container = try DashModelContainer.createInMemory() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + XCTAssertEqual(PlatformWalletPersistenceCapabilities.shieldedStateWipe, 1 << 16) + XCTAssertEqual( + PlatformWalletPersistenceCapabilities.shieldedStateWipe, + UInt64(PLATFORM_WALLET_PERSISTENCE_CAPABILITY_SHIELDED_STATE_WIPE)) + XCTAssertNotEqual( + handler.makePersistenceCapabilities().bits + & PlatformWalletPersistenceCapabilities.shieldedStateWipe, 0) + XCTAssertNotNil(handler.makePersistenceCallbacksExtension().on_wipe_shielded_state_fn) + } + + func testShouldDeleteEveryShieldedRowOfTheNetworkThroughTheShimAndKeepOtherNetworks() throws { + let container = try DashModelContainer.createInMemory() + try seed(container) + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + let wipe = try XCTUnwrap(handler.makePersistenceCallbacksExtension().on_wipe_shielded_state_fn) + + let rc = wipe(Unmanaged.passUnretained(handler).toOpaque(), Network.testnet.ffiValue) + + XCTAssertEqual(rc, 0) + XCTAssertEqual( + try walletIds(PersistentShieldedSyncState.self, container, \.walletId), [mainnetWallet]) + XCTAssertEqual( + try walletIds(PersistentShieldedViewingKey.self, container, \.walletId), [mainnetWallet]) + } +} From 1754d1aaf61eebd165abfe32635a91666688d520 Mon Sep 17 00:00:00 2001 From: pasta Date: Sun, 15 Nov 2026 10:19:55 +0000 Subject: [PATCH 2/4] fix(platform-wallet): admit private payment restorations and report an unconfigured balance - shielded_balances returns an error when the manager has no shielded coordinator, instead of an empty map that reads as a zero balance. - Private contact payment restoration runs under the coordinator's shielded admission from its journal read to its last store and persister write, so a Clear lands entirely before or after it, and a Clear whose host wipe failed refuses it as it refuses binds. A bind restores under its own admission; restorations that must not wait (retry before a send, load, register, repeat configure) take one without blocking and treat a Clear as a busy store. - SqlitePersister::wipe_shielded_state classifies a dirty wallet with no wallets row by the network of its buffered metadata, so another network's new wallet is not flushed by this network's wipe. Co-Authored-By: Claude Opus 5.5 --- .../src/sqlite/buffer.rs | 21 ++ .../src/sqlite/persister.rs | 22 ++- .../sqlite_shielded_viewing_keys.rs | 62 ++++++ .../rs-platform-wallet/src/manager/mod.rs | 12 +- .../network/private_contact_requests.rs | 2 +- .../src/wallet/platform_wallet.rs | 95 +++++++-- .../src/wallet/shielded/coordinator.rs | 36 +++- .../shielded/lifecycle_boundary_tests.rs | 181 ++++++++++++++++++ 8 files changed, 391 insertions(+), 40 deletions(-) diff --git a/packages/rs-platform-wallet-storage/src/sqlite/buffer.rs b/packages/rs-platform-wallet-storage/src/sqlite/buffer.rs index 918d484eac9..4b03e9041a3 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/buffer.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/buffer.rs @@ -206,6 +206,27 @@ impl Buffer { Ok(()) } + /// The network the last buffered wallet metadata of `wallet_id` names, + /// if any is buffered. + #[cfg(feature = "shielded")] + pub fn buffered_network( + &self, + wallet_id: &WalletId, + ) -> Result, WalletStorageError> { + let guard = self + .inner + .lock() + .map_err(|_| WalletStorageError::LockPoisoned)?; + Ok(guard.get(wallet_id).and_then(|pending| { + pending + .segments + .iter() + .rev() + .find_map(|segment| segment.wallet_metadata.as_ref()) + .map(|meta| meta.network) + })) + } + /// Every wallet currently holding buffered data, sorted by id for /// deterministic flush ordering. pub fn dirty_wallets(&self) -> Result, WalletStorageError> { diff --git a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs index 000d37498cd..b0507da16c8 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs @@ -1338,10 +1338,11 @@ impl PlatformWalletPersistence for SqlitePersister { } } - /// Flushes this network's buffered writes (and those of wallets with no - /// stored row yet) under the delete's connection guard, then deletes the - /// network's viewing keys in one transaction. A flush failure fails the - /// wipe with the rows in place. + /// Flushes this network's buffered writes under the delete's connection + /// guard, then deletes the network's viewing keys in one transaction. A + /// wallet with no stored row yet is classified by the network of its + /// buffered metadata, and flushed too when it has none. A flush failure + /// fails the wipe with the rows in place. #[cfg(feature = "shielded")] fn wipe_shielded_state(&self, network: dashcore::Network) -> Result<(), PersistenceError> { self.ensure_writable("wipe_shielded_state") @@ -1355,9 +1356,16 @@ impl PlatformWalletPersistence for SqlitePersister { .dirty_wallets() .map_err(PersistenceError::from)? { - let stored = - schema::wallets::fetch(&conn, &wallet_id).map_err(PersistenceError::from)?; - if !matches!(&stored, Some((stored_network, _)) if stored_network != label) { + let other_network = + match schema::wallets::fetch(&conn, &wallet_id).map_err(PersistenceError::from)? { + Some((stored_network, _)) => stored_network != label, + None => self + .buffer + .buffered_network(&wallet_id) + .map_err(PersistenceError::from)? + .is_some_and(|buffered| buffered != network), + }; + if !other_network { self.flush_locked(&mut conn, &wallet_id)?; } } diff --git a/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs b/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs index 2f0670a0dac..ca413a6f4e1 100644 --- a/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs +++ b/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs @@ -219,3 +219,65 @@ fn shielded_wipe_leaves_other_networks_buffered_writes_unflushed() { ); assert_eq!(viewing_key_count(&path, &mainnet_wallet), 0); } + +fn with_metadata( + mut cs: PlatformWalletChangeSet, + network: dashcore::Network, +) -> PlatformWalletChangeSet { + cs.wallet_metadata = Some(platform_wallet::changeset::WalletMetadataEntry { + network, + wallet_group_id: [0u8; 32], + birth_height: 0, + }); + cs +} + +#[test] +fn shielded_wipe_classifies_a_wallet_with_no_row_by_its_buffered_network() { + let (persister, _tmp, path) = + common::fresh_persister_with_mode(platform_wallet_storage::FlushMode::Manual); + let testnet_wallet = wid(0x76); + let mainnet_wallet = wid(0x77); + persister + .store( + testnet_wallet, + with_metadata( + changeset(SubwalletId::new(testnet_wallet, 0), [0xE4; 96]), + dashcore::Network::Testnet, + ), + ) + .expect("buffer the new testnet wallet"); + persister + .store( + mainnet_wallet, + with_metadata( + changeset(SubwalletId::new(mainnet_wallet, 0), [0xE5; 96]), + dashcore::Network::Mainnet, + ), + ) + .expect("buffer the new mainnet wallet"); + + persister + .wipe_shielded_state(dashcore::Network::Testnet) + .expect("wipe testnet"); + + let wallet_rows = |wallet_id: &[u8; 32]| -> i64 { + common::ro_conn(&path) + .query_row( + "SELECT COUNT(*) FROM wallets WHERE wallet_id = ?1", + rusqlite::params![wallet_id.as_slice()], + |row| row.get(0), + ) + .unwrap() + }; + assert_eq!(wallet_rows(&testnet_wallet), 1, "flushed by the wipe"); + assert_eq!(viewing_key_count(&path, &testnet_wallet), 0, "then wiped"); + assert_eq!( + wallet_rows(&mainnet_wallet), + 0, + "another network's new wallet stays buffered" + ); + + persister.commit_writes().expect("flush after wipe"); + assert_eq!(viewing_key_count(&path, &mainnet_wallet), 1); +} diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 41a8d9548d8..c997722cdbf 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -1066,7 +1066,9 @@ impl PlatformWalletManager

{ || (journaled_too && coordinator.may_journal_private_payments_of(wallet.wallet_id())) { - wallet.restore_private_payments_or_defer(coordinator).await; + wallet + .restore_private_payments_or_defer(coordinator, None) + .await; } } } @@ -1089,7 +1091,9 @@ impl PlatformWalletManager

{ pub(crate) async fn hold_or_restore_private_payments(&self, wallet: &PlatformWallet) { match self.shielded_coordinator().await { Some(coordinator) => { - wallet.restore_private_payments_or_defer(&coordinator).await; + wallet + .restore_private_payments_or_defer(&coordinator, None) + .await; } None if wallet.persister().persists_private_contact_scan_state() => { wallet.persister().hold_until_private_payments_restored(); @@ -1106,7 +1110,9 @@ impl PlatformWalletManager

{ pub(crate) async fn restore_private_payments_configured_since(&self, wallet: &PlatformWallet) { if let Some(coordinator) = self.shielded_coordinator().await { if wallet.private_payments_held().await { - wallet.restore_private_payments_or_defer(&coordinator).await; + wallet + .restore_private_payments_or_defer(&coordinator, None) + .await; } } } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs index 0fe4219980e..1bf72fbe806 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs @@ -11904,7 +11904,7 @@ mod tests { )); let scan = coordinator.store().write().await; let deferred = wallet - .restore_private_payments_or_defer(&coordinator) + .restore_private_payments_or_defer(&coordinator, None) .await .expect("deferred for the busy store"); deferred.abort(); diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index 77fd8b1f38b..ff608622094 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -1083,8 +1083,9 @@ impl PlatformWallet { self.install_shielded_views_in_one_transaction(admission, account_views, start) .await?; // After the install transaction: the restore takes the store and - // wallet locks on its own. - self.restore_private_payments_or_defer(coordinator).await; + // wallet locks on its own, under the bind's admission. + self.restore_private_payments_or_defer(coordinator, Some(admission)) + .await; Ok(()) } @@ -1100,13 +1101,19 @@ impl PlatformWallet { /// while one waits queues none. The queue frees however its task ends, /// a panic or an abort included (`DeferredFinish`). Returns the task of /// the deferred restoration when one was queued. + /// + /// `admission` is the caller's own, for a caller that holds one (a bind): + /// the restoration runs under it rather than taking a second. Without + /// one, the restoration takes its own, and a Clear holding or queued for + /// it counts as a busy store. #[cfg(feature = "shielded")] pub(crate) async fn restore_private_payments_or_defer( &self, coordinator: &Arc, + admission: Option<&ShieldedAdmission<'_>>, ) -> Option> { let restore = self.private_payment_restore(coordinator); - match restore.restore_and_release(false).await { + match restore.restore_and_release(admission, false).await { Ok(Some(_)) => None, Ok(None) => { if restore.restored_from_this_store() || !restore.queue_deferred() { @@ -1121,7 +1128,7 @@ impl PlatformWallet { // so the queue frees even for a task dropped unpolled. let restore = DeferredFinish(restore); Some(tokio::spawn(async move { - if let Err(error) = restore.0.restore_and_release(true).await { + if let Err(error) = restore.0.restore_and_release(None, true).await { tracing::warn!( wallet_id = %hex::encode(restore.0.wallet_id), %error, @@ -1135,7 +1142,7 @@ impl PlatformWallet { tracing::warn!( wallet_id = %hex::encode(self.wallet_id), %error, - "private contact request payments not restored at bind; the wallet's \ + "private contact request payments not restored; the wallet's \ contact requests stay held until a restoration succeeds" ); None @@ -1518,9 +1525,10 @@ impl PlatformWallet { /// Per-account unspent shielded balance. /// /// Reads against the coordinator's shared store (one SQLite - /// handle per network); returns an empty map if shielded - /// support hasn't been configured or this wallet isn't - /// bound. Folds the network-wide + /// handle per network); returns an empty map if this wallet isn't + /// bound, and [`PlatformWalletError::ShieldedStoreError`] if the + /// manager has no shielded support configured, so an unconfigured + /// manager never reads as a zero balance. Folds the network-wide /// [`balances_across`](super::shielded::sync::balances_across) /// result down to this wallet's per-account slice. #[cfg(feature = "shielded")] @@ -1528,9 +1536,7 @@ impl PlatformWallet { &self, ) -> Result, PlatformWalletError> { use super::shielded::{AccountViewingKeys, SubwalletId}; - let Ok(coordinator) = self.shielded_coordinator().await else { - return Ok(std::collections::BTreeMap::new()); - }; + let coordinator = self.shielded_coordinator().await?; let guard = self.shielded_keys.read().await; let Some(keys) = guard.as_ref() else { return Ok(std::collections::BTreeMap::new()); @@ -1949,8 +1955,12 @@ impl PlatformWallet { /// public and private send from then on. Returns the (sender, recipient) /// pairs restored. /// + /// Waits for the coordinator's admission and holds it to its last write, + /// so a shielded Clear lands entirely before or after it. + /// /// Fails closed, every hold staying and the wallet's raised if it was - /// not: for a coordinator of another network, a wallet removed or + /// not: for a coordinator of another network, a Clear that reset native + /// state but did not wipe the host, a wallet removed or /// re-created since this handle was taken, a journal record that does /// not read back, a saved spend whose transition does not decode and /// that no journal record accounts for, a private contact request @@ -1983,7 +1993,7 @@ impl PlatformWallet { ) -> Result, PlatformWalletError> { let coordinator = self.shielded_coordinator().await?; self.private_payment_restore(&coordinator) - .restore_and_release(true) + .restore_and_release(None, true) .await .map(Option::unwrap_or_default) } @@ -2023,7 +2033,7 @@ impl PlatformWallet { let Some(restore) = restore else { return; }; - if let Err(error) = restore.restore_and_release(false).await { + if let Err(error) = restore.restore_and_release(None, false).await { tracing::warn!( wallet_id = %hex::encode(wallet_id), %error, @@ -2060,6 +2070,16 @@ impl PlatformWallet { .await } + /// How many deferred restorations of this wallet's private payments were + /// queued ([`Self::restore_private_payments_or_defer`]). + #[cfg(all(test, feature = "shielded"))] + pub(crate) fn deferred_private_payment_restores(&self) -> usize { + self.private_payment_restores + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .queued + } + /// The entries of the private contact requests whose payment from this /// wallet's notes `coordinator`'s store holds as unconfirmed. #[cfg(feature = "shielded")] @@ -3139,11 +3159,35 @@ impl PrivatePaymentRestore { /// once a restoration succeeds, raised when it fails, and when it cannot /// run yet unless this generation already restored from this store. /// `Ok(None)` when the store is busy and `wait` is false. + /// + /// Runs under the coordinator's admission, held until the last store and + /// persister write, so a Clear lands entirely before or after it. A + /// caller that holds one passes it: taking a second would deadlock + /// behind a queued Clear. Otherwise one is taken here, and without + /// `wait` a Clear holding or queued for it counts as a busy store. Even + /// without `wait` the restoration waits for the wallet manager's lock, + /// admission held, so a Clear queued meanwhile waits for it too. async fn restore_and_release( &self, + admission: Option<&ShieldedAdmission<'_>>, wait: bool, ) -> Result>, PlatformWalletError> { - let restored = self.restore(wait).await; + let owned; + let admission = match admission { + Some(admission) => Some(admission), + None if wait => { + owned = Some(self.coordinator.admit().await); + owned.as_ref() + } + None => { + owned = self.coordinator.try_admit(); + owned.as_ref() + } + }; + let restored = match admission { + Some(admission) => self.restore(admission, wait).await, + None => Ok(None), + }; match &restored { Ok(Some(_)) => { self.restores_lock().restored_from = Arc::downgrade(self.coordinator.store()); @@ -3241,8 +3285,17 @@ impl PrivatePaymentRestore { /// holds it raises or releases are this generation's own. async fn restore( &self, + admission: &ShieldedAdmission<'_>, wait: bool, ) -> Result>, PlatformWalletError> { + if !admission.admits(&self.coordinator) { + return Err(PlatformWalletError::InvalidParameter( + "the shielded admission is not this restoration's coordinator's".to_string(), + )); + } + // A Clear that reset native state but not the host refuses + // restorations as it refuses binds. + admission.ensure_host_cleared()?; if self.coordinator.network() != self.network { return Err(PlatformWalletError::InvalidParameter(format!( "the shielded coordinator is for {:?}, this wallet for {:?}", @@ -3863,18 +3916,18 @@ mod private_contact_request_payment_tests { let store = coordinator.store().write().await; let task = wallet - .restore_private_payments_or_defer(&coordinator) + .restore_private_payments_or_defer(&coordinator, None) .await .expect("queued"); assert!(wallet - .restore_private_payments_or_defer(&coordinator) + .restore_private_payments_or_defer(&coordinator, None) .await .is_none()); task.abort(); assert!(task.await.is_err_and(|error| error.is_cancelled())); assert!( wallet - .restore_private_payments_or_defer(&coordinator) + .restore_private_payments_or_defer(&coordinator, None) .await .is_some(), "the aborted task freed the queue" @@ -3903,11 +3956,11 @@ mod private_contact_request_payment_tests { let store = coordinator.store().write().await; assert!(wallet - .restore_private_payments_or_defer(&coordinator) + .restore_private_payments_or_defer(&coordinator, None) .await .is_some()); assert!(wallet - .restore_private_payments_or_defer(&coordinator) + .restore_private_payments_or_defer(&coordinator, None) .await .is_none()); assert_eq!(queued(), 1, "one deferred restoration per store"); @@ -3923,7 +3976,7 @@ mod private_contact_request_payment_tests { let store = coordinator.store().write().await; assert!(wallet - .restore_private_payments_or_defer(&coordinator) + .restore_private_payments_or_defer(&coordinator, None) .await .is_none()); assert!( diff --git a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs index 287261391d7..5009200ee89 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -273,13 +273,15 @@ pub struct NetworkShieldedCoordinator { /// [`admit`](Self::admit) for the protocol. /// /// Shared ([`ShieldedAdmission`]): a bind (host snapshot load, - /// viewing-key persistence, install), an account add, a spend from its - /// note selection to its last store and host write, a sync pass, an - /// identity-debit abandonment. Exclusive: Clear, across the Rust reset, - /// the host wipe and the epoch bump. So a Clear cannot land inside any of - /// those spans, and none can start until Clear, host wipe included, has - /// finished: no snapshot loaded before a Clear and no viewing-key write - /// begun before it completes after the wipe. + /// viewing-key persistence, install, private payment restoration), an + /// account add, a spend from its note selection to its last store and + /// host write, a sync pass, an identity-debit abandonment, a private + /// payment restoration from its journal read to its last write. + /// Exclusive: Clear, across the Rust reset, the host wipe and the epoch + /// bump. So a Clear cannot land inside any of those spans, and none can + /// start until Clear, host wipe included, has finished: no snapshot + /// loaded before a Clear and no viewing-key write begun before it + /// completes after the wipe. /// /// The guarded value is the Clear state: how many Clears completed (the /// epoch) and whether the last one reset Rust but could not wipe the host. @@ -324,13 +326,18 @@ impl ShieldedAdmission<'_> { if self.state.host_wipe_pending { return Err(PlatformWalletError::ShieldedStoreError( "the last shielded Clear reset native state but did not wipe host storage; \ - retry Clear before binding" + retry Clear before binding or restoring" .to_string(), )); } Ok(()) } + /// Whether this admission was granted by `coordinator`. + pub(crate) fn admits(&self, coordinator: &NetworkShieldedCoordinator) -> bool { + std::ptr::eq(self.coordinator, coordinator) + } + /// Open an install transaction for `wallet_id`, taking the coordinator's /// lifecycle mutex for the returned guard's lifetime. /// @@ -508,6 +515,19 @@ impl NetworkShieldedCoordinator { } } + /// [`admit`](Self::admit) without waiting: `None` while a Clear holds the + /// admission or is queued for it. Never blocks, so it is safe where the + /// caller may already hold an admission. + pub(crate) fn try_admit(&self) -> Option> { + self.admission + .try_read() + .ok() + .map(|state| ShieldedAdmission { + coordinator: self, + state, + }) + } + /// Whether a Clear holds the admission or is queued for it: either way /// no new admission is granted until it finishes. #[cfg(test)] diff --git a/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs index 5944f0e96a2..a4657610193 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs @@ -685,3 +685,184 @@ async fn should_resolve_the_managers_coordinator_and_none_before_it_is_configure vec![SubwalletId::new(wallet.wallet_id(), 0)] ); } + +/// An unconfigured manager's wallet reports its shielded balance as an +/// error, not as an empty (zero) balance; once configured, an unbound wallet +/// reads empty. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_refuse_a_balance_read_before_the_manager_is_configured() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet) = unconfigured(&host).await; + + let error = wallet + .shielded_balances() + .await + .expect_err("no coordinator yet"); + assert!( + matches!(&error, PlatformWalletError::ShieldedStoreError(message) if message.contains("not configured")), + "{error:?}" + ); + + manager + .configure_shielded(dir.path().join("tree.sqlite")) + .await + .unwrap(); + assert!(wallet.shielded_balances().await.unwrap().is_empty()); +} + +/// A private payment restoration parked after its journal read, waiting for +/// the wallet, holds Clear off: Clear neither resets nor wipes until the +/// restoration's last write is done. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_hold_clear_until_a_private_payment_restoration_in_flight_finishes() { + use std::future::Future; + use std::task::{Context, Waker}; + + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + let mut cx = Context::from_waker(Waker::noop()); + + let held_store = coordinator.store().write().await; + let mut restore = Box::pin(wallet.restore_private_contact_payments()); + assert!( + restore.as_mut().poll(&mut cx).is_pending(), + "waits for the store" + ); + let held_wallet = wallet.wallet_manager().write().await; + drop(held_store); + assert!( + restore.as_mut().poll(&mut cx).is_pending(), + "read its journal, waits for the wallet" + ); + + let clear = spawn_clear(&manager); + until_admission_closed(&coordinator).await; + tokio::time::sleep(Duration::from_millis(100)).await; + assert_eq!(host.wipe_count(), 0, "Clear waits for the restoration"); + assert!(!clear.is_finished()); + assert_eq!(coordinator.store().read().await.tree_size().unwrap(), 0); + + drop(held_wallet); + restore.await.expect("the restoration finishes first"); + clear.await.unwrap().expect("then Clear runs"); + assert_eq!(host.wipe_count(), 1); + assert!(host.wiped_exclusively.load(Ordering::SeqCst)); +} + +/// A restoration that starts while the host wipe runs waits for the whole +/// Clear, as a bind does, and then succeeds against the cleared store. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_hold_a_restoration_started_during_the_wipe_until_clear_finishes() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + + type Restore = tokio::task::JoinHandle< + Result, PlatformWalletError>, + >; + let started: Arc>> = Arc::default(); + let held_during_wipe = Arc::new(AtomicBool::new(false)); + { + let wallet = Arc::clone(&wallet); + let started = Arc::clone(&started); + let held_during_wipe = Arc::clone(&held_during_wipe); + let runtime = tokio::runtime::Handle::current(); + *host.on_wipe.lock().unwrap() = Some(Box::new(move || { + let wallet = Arc::clone(&wallet); + let restore = + runtime.spawn(async move { wallet.restore_private_contact_payments().await }); + std::thread::sleep(Duration::from_millis(100)); + held_during_wipe.store(!restore.is_finished(), Ordering::SeqCst); + *started.lock().unwrap() = Some(restore); + })); + } + + manager.clear_shielded().await.expect("Clear succeeds"); + assert!(held_during_wipe.load(Ordering::SeqCst)); + let restore = started + .lock() + .unwrap() + .take() + .expect("the hook started a restoration"); + restore + .await + .unwrap() + .expect("the restoration runs after Clear"); + assert_eq!(coordinator.clear_epoch().await, 1); + assert!(!wallet.private_payments_held().await); +} + +/// A Clear that reset native state but failed to wipe the host refuses +/// restorations, holding the wallet, until a Clear completes both halves. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_refuse_restorations_until_a_failed_host_wipe_is_retried() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, _coordinator) = configured(&host, &dir).await; + + host.fail_wipe.store(true, Ordering::SeqCst); + manager + .clear_shielded() + .await + .expect_err("the host wipe fails"); + let error = wallet + .restore_private_contact_payments() + .await + .expect_err("refused while the host may hold cleared rows"); + assert!( + matches!(&error, PlatformWalletError::ShieldedStoreError(message) if message.contains("did not wipe host storage")), + "{error:?}" + ); + assert!( + wallet.private_payments_held().await, + "the wallet stays held" + ); + + host.fail_wipe.store(false, Ordering::SeqCst); + manager + .clear_shielded() + .await + .expect("the retry wipes the host"); + wallet + .restore_private_contact_payments() + .await + .expect("restored once the host is wiped"); + assert!(!wallet.private_payments_held().await); +} + +/// A bind parked inside its admission while a Clear queues for the lock runs +/// its private payment restoration under that same admission: it neither +/// deadlocks on a second admission behind the queued Clear nor defers the +/// restoration, and Clear runs once the bind is done. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_restore_a_binds_private_payments_under_its_admission_while_clear_is_queued() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + + let release = BoundaryHost::gate(&host.store_gate); + let bind = { + let wallet = Arc::clone(&wallet); + tokio::spawn(async move { wallet.bind_shielded(&SEED, &[0]).await }) + }; + host.reached("store viewing keys").await; + let clear = spawn_clear(&manager); + until_admission_closed(&coordinator).await; + + release.send(()).unwrap(); + tokio::time::timeout(Duration::from_secs(10), bind) + .await + .expect("the bind does not deadlock behind the queued Clear") + .unwrap() + .expect("the bind finishes first"); + assert_eq!( + wallet.deferred_private_payment_restores(), + 0, + "restored inline, not deferred" + ); + assert!(!wallet.private_payments_held().await); + clear.await.unwrap().expect("then Clear runs"); + assert_eq!(host.events(), BIND_THEN_CLEAR); +} From 2352c3c6705e0b824722d956a522d7826c9a834e Mon Sep 17 00:00:00 2001 From: pasta Date: Sun, 15 Nov 2026 13:42:12 +0000 Subject: [PATCH 3/4] fix(platform-wallet): persist a pending host wipe and refuse shielded work until it completes A Clear that reset native shielded state but could not wipe the host kept that fact in memory only, and only binds and restorations checked it. - The shielded store keeps a durable host-wipe-pending mark, set before the native reset and cleared only once the host wipe succeeds. A coordinator opened over the store starts from it, so a process that dies in between still refuses shielded work after a restart. - admit()/try_admit() refuse while the mark is set, so every spend, transfer, unshield, withdrawal, top-up, funding, abandonment, account add, sync pass and restoration is refused rather than run against the reset store next to the host's surviving rows. - shielded_balances and restore_pending_private_contact_requests take a shared admission from their store read through their last use of it. - shielded_add_account takes shield_guard before admission, as a bind does. - The Kotlin and Swift host wipes enumerate the shielded tables. Rows of a wallet whose network is unknown fail the wipe with nothing deleted; rows of a wallet with no wallet row are deleted with the network's own. Co-Authored-By: Claude Opus 5.5 --- .../PlatformWalletPersistenceHandler.kt | 35 ++- .../dashsdk/persistence/dao/ShieldedDao.kt | 10 + .../PlatformWalletPersistenceHandlerTest.kt | 46 ++++ .../src/wallet/platform_wallet.rs | 81 +++---- .../src/wallet/shielded/balance_tests.rs | 2 +- .../src/wallet/shielded/coordinator.rs | 212 +++++++++++------- .../src/wallet/shielded/file_store.rs | 69 ++++++ .../wallet/shielded/fund_from_asset_lock.rs | 14 +- .../shielded/lifecycle_boundary_tests.rs | 110 ++++++++- .../PlatformWalletPersistenceHandler.swift | 45 +++- .../ShieldedStateWipeTests.swift | 50 +++++ 11 files changed, 541 insertions(+), 133 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt index e380b7bf8d6..05d7e4880e3 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt @@ -2689,21 +2689,40 @@ class PlatformWalletPersistenceHandler( * [network] in one transaction. Scoped by the wallet rows' network rather * than the loaded fleet, so a same-network wallet that failed restore * loses its rows too. + * + * Shielded rows carry no wallet FK, so they are enumerated from the + * shielded tables. Rows of a wallet with no wallet row belong to no + * wallet on any network and go too, as a wallet deletion would have + * taken them. Rows of a wallet whose network is not yet known fail the + * wipe with nothing deleted: they may be [network]'s, and reporting + * success with them in place would let a later bind restore them. */ override fun onWipeShieldedState(network: Int): Int = guarded { - runBlockingCatching { + runBlockingResult { database.withTransaction { val dao = database.shieldedDao() - for (wallet in database.walletDao().getByNetwork(network)) { - dao.deleteActivityByWallet(wallet.walletId) - dao.deleteOutgoingNotesByWallet(wallet.walletId) - dao.deleteNotesByWallet(wallet.walletId) - dao.deleteSyncStatesByWallet(wallet.walletId) - dao.deleteViewingKeysByWallet(wallet.walletId) + val doomed = ArrayList() + for (walletId in dao.getShieldedWalletIds()) { + val wallet = database.walletDao().getByWalletId(walletId) + when { + wallet == null -> doomed += walletId + wallet.networkRaw == null -> { + Log.e(TAG, "shielded wipe refused: a wallet with shielded rows has no network") + return@withTransaction 1 + } + wallet.networkRaw == network -> doomed += walletId + } + } + for (walletId in doomed) { + dao.deleteActivityByWallet(walletId) + dao.deleteOutgoingNotesByWallet(walletId) + dao.deleteNotesByWallet(walletId) + dao.deleteSyncStatesByWallet(walletId) + dao.deleteViewingKeysByWallet(walletId) } + 0 } } - 0 } // ── Load callbacks ──────────────────────────────────────────────── diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/dao/ShieldedDao.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/dao/ShieldedDao.kt index 9e7ed9141e6..802dcefe38b 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/dao/ShieldedDao.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/dao/ShieldedDao.kt @@ -27,6 +27,16 @@ import org.dashfoundation.dashsdk.persistence.entities.ShieldedViewingKeyEntity @Dao interface ShieldedDao { + /** Every wallet any shielded table holds a row of. */ + @Query( + "SELECT walletId FROM shielded_notes " + + "UNION SELECT walletId FROM shielded_outgoing_notes " + + "UNION SELECT walletId FROM shielded_activities " + + "UNION SELECT walletId FROM shielded_sync_states " + + "UNION SELECT walletId FROM shielded_viewing_keys" + ) + suspend fun getShieldedWalletIds(): List + // MARK: Notes (received, spendable) @Query("SELECT * FROM shielded_notes WHERE nullifier = :nullifier") diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt index d6e68031766..869d3ae2fbd 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt @@ -445,6 +445,52 @@ class PlatformWalletPersistenceHandlerTest { assertEquals(1, db.shieldedDao().getAllSyncStates().count { it.walletId.contentEquals(mainnetWallet) }) } + private suspend fun seedShieldedRows(wallet: ByteArray) { + db.shieldedDao().upsertViewingKey( + org.dashfoundation.dashsdk.persistence.entities.ShieldedViewingKeyEntity( + wallet, 0, ByteArray(96) { 1 }, + ), + ) + db.shieldedDao().upsertSyncState( + org.dashfoundation.dashsdk.persistence.entities.ShieldedSyncStateEntity( + wallet, 0, lastSyncedIndex = 42, + ), + ) + } + + private suspend fun shieldedRowsOf(wallet: ByteArray): Int = + db.shieldedDao().getViewingKeysByWallet(wallet).size + + db.shieldedDao().getAllSyncStates().count { it.walletId.contentEquals(wallet) } + + @Test + fun shieldedWipeFailsWithNothingDeletedWhileAWalletWithShieldedRowsHasNoNetwork() = runTest { + val testnetWallet = ByteArray(32) { 0x63 } + val unknownNetworkWallet = ByteArray(32) { 0x64 } + db.walletDao().upsert(WalletEntity(testnetWallet, networkRaw = Network.TESTNET.ffiValue)) + db.walletDao().upsert(WalletEntity(unknownNetworkWallet, networkRaw = null)) + seedShieldedRows(testnetWallet) + seedShieldedRows(unknownNetworkWallet) + + assertNotEquals(0, handler.onWipeShieldedState(Network.TESTNET.ffiValue)) + + assertEquals(2, shieldedRowsOf(testnetWallet)) + assertEquals(2, shieldedRowsOf(unknownNetworkWallet)) + } + + @Test + fun shieldedWipeDropsTheRowsOfAWalletWithNoWalletRow() = runTest { + val mainnetWallet = ByteArray(32) { 0x65 } + val orphan = ByteArray(32) { 0x66 } + db.walletDao().upsert(WalletEntity(mainnetWallet, networkRaw = Network.MAINNET.ffiValue)) + seedShieldedRows(mainnetWallet) + seedShieldedRows(orphan) + + assertEquals(0, handler.onWipeShieldedState(Network.TESTNET.ffiValue)) + + assertEquals(0, shieldedRowsOf(orphan)) + assertEquals(2, shieldedRowsOf(mainnetWallet)) + } + @Test fun shieldedWipeIsNeverInheritedAsASuccess() { val noOpBridge = object : NativePersistenceBridge() {} diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index ff608622094..d338d6eb826 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -910,8 +910,7 @@ impl PlatformWallet { // a long shield holds no admission that would stall a Clear. let _shield_guard = self.shield_guard.lock().await; let coordinator = self.shielded_coordinator().await?; - let admission = coordinator.admit().await; - admission.ensure_host_cleared()?; + let admission = coordinator.admit().await?; let network = self.sdk.network; let mut account_views: std::collections::BTreeMap = std::collections::BTreeMap::new(); @@ -1025,8 +1024,7 @@ impl PlatformWallet { // `bind_shielded` is. let _shield_guard = self.shield_guard.lock().await; let coordinator = self.shielded_coordinator().await?; - let admission = coordinator.admit().await; - admission.ensure_host_cleared()?; + let admission = coordinator.admit().await?; let start = self.persister.load().map_err(|e| { PlatformWalletError::ShieldedBuildError(format!( "persister load failed while rebinding shielded viewing keys: {e}" @@ -1292,13 +1290,14 @@ impl PlatformWallet { ))); } self.ensure_shielded_attached()?; - // Admitted for the whole read-check-write-install span, as a bind - // is: no Clear lands between the snapshot read and the key's - // installation, and none completes with this key's row still to be - // written. + // Guarded and admitted for the whole read-check-write-install span, + // as a bind is: no Clear lands between the snapshot read and the + // key's installation, none completes with this key's row still to be + // written, and no bind replaces the key slot between this add's + // check and its insert. + let _shield_guard = self.shield_guard.lock().await; let coordinator = self.shielded_coordinator().await?; - let admission = coordinator.admit().await; - admission.ensure_host_cleared()?; + let _admission = coordinator.admit().await?; // Everything that calls into the host — the snapshot read below // and the viewing-key write further down — stays OUTSIDE the key // slot's lock. A host callback invoked while this write guard is @@ -1531,12 +1530,17 @@ impl PlatformWallet { /// manager never reads as a zero balance. Folds the network-wide /// [`balances_across`](super::shielded::sync::balances_across) /// result down to this wallet's per-account slice. + /// + /// Admitted from the key snapshot through the read, so it never + /// interleaves with a Clear, and refused rather than read empty while a + /// Clear's host wipe is pending. #[cfg(feature = "shielded")] pub async fn shielded_balances( &self, ) -> Result, PlatformWalletError> { use super::shielded::{AccountViewingKeys, SubwalletId}; let coordinator = self.shielded_coordinator().await?; + let _admission = coordinator.admit().await?; let guard = self.shielded_keys.read().await; let Some(keys) = guard.as_ref() else { return Ok(std::collections::BTreeMap::new()); @@ -1628,7 +1632,7 @@ impl PlatformWallet { prover: P, ) -> Result<(), PlatformWalletError> { let coordinator = self.shielded_coordinator().await?; - let _admission = coordinator.admit().await; + let _admission = coordinator.admit().await?; let keyset = self.derive_spend_keyset(seed, account).await?; let recipient = Option::::from( grovedb_commitment_tree::PaymentAddress::from_raw_address_bytes(recipient_raw_43), @@ -1670,7 +1674,7 @@ impl PlatformWallet { prover: P, ) -> Result<(), PlatformWalletError> { let coordinator = self.shielded_coordinator().await?; - let _admission = coordinator.admit().await; + let _admission = coordinator.admit().await?; let keyset = self.derive_spend_keyset(seed, account).await?; // The decoder is network-agnostic, so guard the recipient's HRP class // against the wallet's network before decoding. @@ -1719,7 +1723,7 @@ impl PlatformWallet { // reconcile, and taken before admission (the lock order). let _shield_guard = self.shield_guard.lock().await; let coordinator = self.shielded_coordinator().await?; - let _admission = coordinator.admit().await; + let _admission = coordinator.admit().await?; let keyset = self.derive_spend_keyset(seed, account).await?; let (mut proven_balance, metadata) = @@ -1931,6 +1935,10 @@ impl PlatformWallet { /// See /// [`DashPayView::restore_pending_private_contact_requests`](crate::DashPayView::restore_pending_private_contact_requests) /// for what it does not cover. + /// + /// Admitted from the store read through the last reservation, so a Clear + /// lands entirely before or after it, and refused while a Clear's host + /// wipe is pending. #[cfg(feature = "shielded")] pub async fn restore_pending_private_contact_requests( &self, @@ -1939,7 +1947,11 @@ impl PlatformWallet { where C: ContactCryptoProvider + Sync, { - let entries = self.unconfirmed_private_contact_requests().await?; + let coordinator = self.shielded_coordinator().await?; + let _admission = coordinator.admit().await?; + let entries = self + .unconfirmed_private_contact_requests(&coordinator) + .await?; self.identity .dashpay() .restore_pending_private_contact_requests(&entries, crypto) @@ -2085,8 +2097,8 @@ impl PlatformWallet { #[cfg(feature = "shielded")] async fn unconfirmed_private_contact_requests( &self, + coordinator: &NetworkShieldedCoordinator, ) -> Result, PlatformWalletError> { - let coordinator = self.shielded_coordinator().await?; let dashpay_id = dashpay_contract()?.id(); let store = coordinator.store().read().await; let redrives = store @@ -2136,7 +2148,7 @@ impl PlatformWallet { // admission is the outermost lock), but only once the host has // signed: a signing prompt never holds a Clear back. let coordinator = self.shielded_coordinator().await?; - let _admission = coordinator.admit().await; + let _admission = coordinator.admit().await?; // The generation's lifecycle gate, held across the liveness check, // the restore and the spend, before the wallet manager lock as the // teardown takes them: a removal cannot complete in between, so a @@ -2224,7 +2236,7 @@ impl PlatformWallet { prover: P, ) -> Result<(), PlatformWalletError> { let coordinator = self.shielded_coordinator().await?; - let _admission = coordinator.admit().await; + let _admission = coordinator.admit().await?; let keyset = self.derive_spend_keyset(seed, account).await?; let network = self.sdk.network; let parsed = to_core_address @@ -2292,7 +2304,7 @@ impl PlatformWallet { IS: dpp::identity::signer::Signer + Send + Sync, { let coordinator = self.shielded_coordinator().await?; - let _admission = coordinator.admit().await; + let _admission = coordinator.admit().await?; let (identity_id, identity) = { // Scope the transient keyset so its spend authority is dropped before we take the // wallet-manager write lock below — it's only needed for the spend, not for the @@ -2574,7 +2586,7 @@ impl PlatformWallet { // taken before admission (the lock order). let _shield_guard = self.shield_guard.lock().await; let coordinator = self.shielded_coordinator().await?; - let _admission = coordinator.admit().await; + let _admission = coordinator.admit().await?; // Planning and amount selection are shared with the cached preflight. // The helper drops the wallet-manager read lock before the expensive @@ -2635,7 +2647,7 @@ impl PlatformWallet { ) -> Result<(), PlatformWalletError> { let _shield_guard = self.shield_guard.lock().await; let coordinator = self.shielded_coordinator().await?; - let admission = coordinator.admit().await; + let admission = coordinator.admit().await?; coordinator .abandon_identity_debit_admitted( &admission, @@ -2758,7 +2770,7 @@ impl PlatformWallet { // Taken before admission (the lock order). let _shield_guard = self.shield_guard.lock().await; let coordinator = self.shielded_coordinator().await?; - let _admission = coordinator.admit().await; + let _admission = coordinator.admit().await?; let identity = { let wm = self.wallet_manager.read().await; @@ -3172,21 +3184,17 @@ impl PrivatePaymentRestore { admission: Option<&ShieldedAdmission<'_>>, wait: bool, ) -> Result>, PlatformWalletError> { - let owned; - let admission = match admission { - Some(admission) => Some(admission), - None if wait => { - owned = Some(self.coordinator.admit().await); - owned.as_ref() - } - None => { - owned = self.coordinator.try_admit(); - owned.as_ref() - } + let owned = match admission { + Some(_) => Ok(None), + None if wait => self.coordinator.admit().await.map(Some), + None => self.coordinator.try_admit(), }; - let restored = match admission { - Some(admission) => self.restore(admission, wait).await, - None => Ok(None), + let restored = match (admission, &owned) { + (Some(admission), _) | (None, Ok(Some(admission))) => { + self.restore(admission, wait).await + } + (None, Ok(None)) => Ok(None), + (None, Err(_)) => owned.map(|_| None), }; match &restored { Ok(Some(_)) => { @@ -3293,9 +3301,6 @@ impl PrivatePaymentRestore { "the shielded admission is not this restoration's coordinator's".to_string(), )); } - // A Clear that reset native state but not the host refuses - // restorations as it refuses binds. - admission.ensure_host_cleared()?; if self.coordinator.network() != self.network { return Err(PlatformWalletError::InvalidParameter(format!( "the shielded coordinator is for {:?}, this wallet for {:?}", diff --git a/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs index 71ab6f9add2..624ea9b2099 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs @@ -515,7 +515,7 @@ async fn local_balance_waits_for_atomic_store_and_lifecycle_updates() { ShieldedBalanceSource::ScannedThisSession ); - let admission = coordinator.admit().await; + let admission = coordinator.admit().await.unwrap(); let install = admission.begin_install(wallet).await; let mut read = Box::pin(coordinator.local_balance_snapshot(wallet)); assert!(matches!(futures::poll!(&mut read), Poll::Pending)); diff --git a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs index 5009200ee89..a7b93a1149a 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -301,38 +301,36 @@ struct ClearState { /// Completed Clears on this coordinator, successful or not: each one /// reset Rust state the store had before it. epoch: u64, - /// The last Clear reset the Rust store but the host wipe failed, so the - /// host may still hold the rows the reset dropped. Binds and account - /// adds are refused until a Clear completes both halves: restoring - /// those rows is exactly what Clear exists to prevent. + /// A Clear reset (or was about to reset) the Rust store but the host + /// wipe has not succeeded since, so the host may still hold the rows the + /// reset dropped. Seeded from the store's durable marker, so it survives + /// a restart. No admission is granted while it is set: restoring those + /// rows, or writing new state next to them, is exactly what Clear exists + /// to prevent. host_wipe_pending: bool, } +/// The refusal every admission returns while a Clear's host wipe is pending. +fn host_wipe_pending_error() -> PlatformWalletError { + PlatformWalletError::ShieldedStoreError( + "the last shielded Clear reset native state but did not wipe host storage; \ + retry Clear before any shielded operation" + .to_string(), + ) +} + /// Shared admission to durable shielded state, from -/// [`NetworkShieldedCoordinator::admit`]. While one is alive, no Clear runs. +/// [`NetworkShieldedCoordinator::admit`]. While one is alive, no Clear runs, +/// and none exists while a Clear's host wipe is pending. /// /// Hold it across the whole span whose writes must not outlive a Clear, and /// never take a second one while holding it (see the `admission` field doc). pub(crate) struct ShieldedAdmission<'a> { coordinator: &'a NetworkShieldedCoordinator, - state: tokio::sync::RwLockReadGuard<'a, ClearState>, + _state: tokio::sync::RwLockReadGuard<'a, ClearState>, } impl ShieldedAdmission<'_> { - /// `Err` when the last Clear reset Rust but did not wipe the host, so a - /// host snapshot may still describe cleared state. Binds and account adds - /// check it before they read or write host rows. - pub(crate) fn ensure_host_cleared(&self) -> Result<(), PlatformWalletError> { - if self.state.host_wipe_pending { - return Err(PlatformWalletError::ShieldedStoreError( - "the last shielded Clear reset native state but did not wipe host storage; \ - retry Clear before binding or restoring" - .to_string(), - )); - } - Ok(()) - } - /// Whether this admission was granted by `coordinator`. pub(crate) fn admits(&self, coordinator: &NetworkShieldedCoordinator) -> bool { std::ptr::eq(self.coordinator, coordinator) @@ -478,6 +476,10 @@ impl NetworkShieldedCoordinator { db_path: PathBuf, store: FileBackedShieldedStore, ) -> Self { + let clear_state = ClearState { + epoch: 0, + host_wipe_pending: store.host_wipe_pending(), + }; Self { sdk, network, @@ -491,7 +493,7 @@ impl NetworkShieldedCoordinator { tree_progress_handler: std::sync::Mutex::new(None), lifecycle: tokio::sync::Mutex::new(()), hydrated: RwLock::new(std::collections::BTreeSet::new()), - admission: RwLock::new(ClearState::default()), + admission: RwLock::new(clear_state), } } @@ -506,26 +508,38 @@ impl NetworkShieldedCoordinator { /// either completes before Clear starts, or starts after Clear (host wipe /// included) has finished and sees only post-Clear state. /// + /// Refused while a Clear's host wipe is pending (the store's durable + /// marker, set before the native reset and cleared only once the host + /// wiped): until a Clear completes both halves, no shielded operation + /// runs against the reset store or the host's surviving rows. + /// /// Not reentrant: never call it, or any entry point that takes it, while /// holding one. - pub(crate) async fn admit(&self) -> ShieldedAdmission<'_> { - ShieldedAdmission { - coordinator: self, - state: self.admission.read().await, + pub(crate) async fn admit(&self) -> Result, PlatformWalletError> { + Self::admission_from(self, self.admission.read().await) + } + + /// [`admit`](Self::admit) without waiting: `Ok(None)` while a Clear holds + /// the admission or is queued for it. Never blocks, so it is safe where + /// the caller may already hold an admission. + pub(crate) fn try_admit(&self) -> Result>, PlatformWalletError> { + match self.admission.try_read() { + Ok(state) => Self::admission_from(self, state).map(Some), + Err(_) => Ok(None), } } - /// [`admit`](Self::admit) without waiting: `None` while a Clear holds the - /// admission or is queued for it. Never blocks, so it is safe where the - /// caller may already hold an admission. - pub(crate) fn try_admit(&self) -> Option> { - self.admission - .try_read() - .ok() - .map(|state| ShieldedAdmission { - coordinator: self, - state, - }) + fn admission_from<'a>( + &'a self, + state: tokio::sync::RwLockReadGuard<'a, ClearState>, + ) -> Result, PlatformWalletError> { + if state.host_wipe_pending { + return Err(host_wipe_pending_error()); + } + Ok(ShieldedAdmission { + coordinator: self, + _state: state, + }) } /// Whether a Clear holds the admission or is queued for it: either way @@ -659,8 +673,7 @@ impl NetworkShieldedCoordinator { account_views: BTreeMap, persister: WalletPersister, ) -> Result<(), PlatformWalletError> { - let admission = self.admit().await; - admission.ensure_host_cleared()?; + let admission = self.admit().await?; let result = admission .begin_install(wallet_id) .await @@ -722,7 +735,7 @@ impl NetworkShieldedCoordinator { activity_id: [u8; 32], acknowledge_possible_execution: bool, ) -> Result<(), PlatformWalletError> { - let admission = self.admit().await; + let admission = self.admit().await?; self.abandon_identity_debit_admitted( &admission, wallet_id, @@ -921,13 +934,13 @@ impl NetworkShieldedCoordinator { /// Whether `wallet_id`'s per-subwallet store state has been /// successfully hydrated from the host snapshot this session /// (see [`mark_hydrated`](Self::mark_hydrated)). + /// `false` while a Clear's host wipe is pending. pub async fn is_hydrated(&self, wallet_id: WalletId) -> bool { - self.admit() - .await - .begin_install(wallet_id) - .await - .is_hydrated() - .await + let Ok(admission) = self.admit().await else { + return false; + }; + let hydrated = admission.begin_install(wallet_id).await.is_hydrated().await; + hydrated } async fn is_hydrated_locked(&self, wallet_id: WalletId) -> bool { @@ -940,14 +953,17 @@ impl NetworkShieldedCoordinator { /// sets `true` only after a successful load + restore; a /// transient persistence failure leaves it `false` so the next /// re-bind retries the restore instead of taking the idempotent - /// fast path on top of an unhydrated store. + /// fast path on top of an unhydrated store. Records nothing while a + /// Clear's host wipe is pending. pub async fn mark_hydrated(&self, wallet_id: WalletId, hydrated: bool) { - self.admit() - .await + let Ok(admission) = self.admit().await else { + return; + }; + admission .begin_install(wallet_id) .await .mark_hydrated(hydrated) - .await + .await; } async fn mark_hydrated_locked(&self, wallet_id: WalletId, hydrated: bool) { @@ -982,12 +998,15 @@ impl NetworkShieldedCoordinator { wallet_id: WalletId, account_views: &BTreeMap, ) -> bool { - self.admit() - .await + let Ok(admission) = self.admit().await else { + return false; + }; + let matches = admission .begin_install(wallet_id) .await .registration_matches(account_views) - .await + .await; + matches } async fn registration_matches_locked( @@ -1060,7 +1079,7 @@ impl NetworkShieldedCoordinator { wallet_id: WalletId, ) -> Result { loop { - let admission = self.admit().await; + let admission = self.admit().await?; let install = admission.begin_install(wallet_id).await; let account_ids: Vec = self .accounts @@ -1150,8 +1169,7 @@ impl NetworkShieldedCoordinator { wallet_id: WalletId, snapshot: &crate::changeset::ShieldedSyncStartState, ) -> Result<(), crate::error::PlatformWalletError> { - let admission = self.admit().await; - admission.ensure_host_cleared()?; + let admission = self.admit().await?; let result = admission .begin_install(wallet_id) .await @@ -1330,13 +1348,19 @@ impl NetworkShieldedCoordinator { /// /// # Failures /// + /// Before anything is reset, the store durably marks the host wipe + /// pending; only a host wipe that succeeds clears the mark. While it is + /// set no admission is granted ([`admit`](Self::admit)), in this process + /// or, if it dies before the wipe, the next one, so the rows the host + /// kept are never restored or written next to. A mark that cannot be + /// written aborts the Clear with nothing reset. + /// /// A store reset failure returns its error before the host is called, - /// so the host keeps its rows while the tree may still be populated. - /// A host wipe failure after a successful reset returns - /// [`PlatformWalletError::Persistence`]; the Rust half stays reset, and - /// binds are refused ([`ShieldedAdmission::ensure_host_cleared`]) until - /// a later Clear wipes the host too, so the rows the host kept are - /// never restored. Either way the epoch advances. + /// so the host keeps its rows while the tree may still be populated, + /// and the mark goes back to what it was. A host wipe failure after a + /// successful reset returns [`PlatformWalletError::Persistence`]; the + /// Rust half stays reset and the mark stays until a later Clear wipes the + /// host too. Either way the epoch advances. /// /// Crate-internal: the manager's /// [`clear_shielded`](crate::PlatformWalletManager::clear_shielded) @@ -1358,6 +1382,15 @@ impl NetworkShieldedCoordinator { // mutex without an admission (see the `lifecycle` field doc). let _lifecycle = self.lifecycle.lock().await; + let was_pending = clear_state.host_wipe_pending; + if let Err(e) = self.store.write().await.set_host_wipe_pending(true) { + return Err(PlatformWalletError::ShieldedStoreError(format!( + "the pending host wipe could not be recorded; clear aborted with nothing \ + reset: {e}" + ))); + } + clear_state.host_wipe_pending = true; + // Reset the persistent store FIRST and bail before mutating any // coordinator registries or calling the host if it fails: the host // must keep its rows while the tree may still be populated. Reset the @@ -1427,6 +1460,15 @@ impl NetworkShieldedCoordinator { clear_state.epoch = clear_state.epoch.wrapping_add(1); if let Some(e) = first_err { + // The host is not called and keeps its rows, which stay + // authoritative: the mark goes back to what it was, or stays set + // when that cannot be written, as the store then still reads it. + match self.store.write().await.set_host_wipe_pending(was_pending) { + Ok(()) => clear_state.host_wipe_pending = was_pending, + Err(marker) => { + tracing::warn!(error = %marker, "the host wipe mark was not restored"); + } + } return Err(e); } @@ -1442,19 +1484,25 @@ impl NetworkShieldedCoordinator { } // The host half, still under the exclusive admission. - match host.wipe_shielded_state(self.network) { - Ok(()) => { - clear_state.host_wipe_pending = false; - Ok(()) - } - Err(e) => { - tracing::error!(error = %e, "host shielded wipe failed after the native reset"); - clear_state.host_wipe_pending = true; - Err(PlatformWalletError::Persistence(format!( - "native shielded state was reset but the host wipe failed: {e}; retry Clear" - ))) - } + if let Err(e) = host.wipe_shielded_state(self.network) { + tracing::error!(error = %e, "host shielded wipe failed after the native reset"); + return Err(PlatformWalletError::Persistence(format!( + "native shielded state was reset but the host wipe failed: {e}; retry Clear" + ))); } + // Still pending, in memory as on disk, until the mark is cleared. + self.store + .write() + .await + .set_host_wipe_pending(false) + .map_err(|e| { + PlatformWalletError::ShieldedStoreError(format!( + "the host wiped its shielded rows but the pending mark was not cleared: \ + {e}; retry Clear" + )) + })?; + clear_state.host_wipe_pending = false; + Ok(()) } /// Discharge the private payment journal records of the private contact @@ -1527,10 +1575,16 @@ impl NetworkShieldedCoordinator { // store (the manager then skips the completion event). Checked // ahead of the cooldown gate, whose skip summary would otherwise // report one caught-up wallet each for a pass that never ran. - let admission = cancel.run_until_cancelled(self.admit()).await; - let (Some(_admission), false) = (admission, cancel.is_cancelled()) else { - tracing::debug!("Coordinator sync cancelled before it started"); - return ShieldedSyncPassSummary::default(); + let _admission = match cancel.run_until_cancelled(self.admit()).await { + Some(Ok(admission)) if !cancel.is_cancelled() => admission, + Some(Err(error)) => { + tracing::debug!(%error, "Coordinator sync refused"); + return ShieldedSyncPassSummary::default(); + } + _ => { + tracing::debug!("Coordinator sync cancelled before it started"); + return ShieldedSyncPassSummary::default(); + } }; // Every pass, cooldown or not: the private requests a private sync read @@ -4314,7 +4368,7 @@ mod tests { let coordinator = Arc::new(coordinator_with_one_wallet(&dir).await); let wallet_id: WalletId = [0x11; 32]; - let admission = coordinator.admit().await; + let admission = coordinator.admit().await.unwrap(); let install = admission.begin_install(wallet_id).await; let remover = { let coordinator = Arc::clone(&coordinator); @@ -4360,7 +4414,7 @@ mod tests { .unwrap(); } - let admission = coordinator.admit().await; + let admission = coordinator.admit().await.unwrap(); let install = admission.begin_install(wallet_id).await; let host = Arc::new(crate::test_support::WipeCountingPersistence::default()); let clearer = { diff --git a/packages/rs-platform-wallet/src/wallet/shielded/file_store.rs b/packages/rs-platform-wallet/src/wallet/shielded/file_store.rs index 1b22d0a32f7..70059c01074 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/file_store.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/file_store.rs @@ -20,6 +20,7 @@ use std::sync::Mutex; use dpp::serialization::{PlatformDeserializableTrusted, PlatformSerializable}; use dpp::state_transition::StateTransition; use grovedb_commitment_tree::{ClientPersistentCommitmentTree, Position, Retention}; +use rusqlite::OptionalExtension; use super::balance::{ShieldedBalanceSource, ShieldedLocalAccountBalance}; use super::store::{ @@ -144,6 +145,10 @@ pub struct FileBackedShieldedStore { /// The largest commitment tree the store had held when it was opened /// ([`ShieldedStore::legacy_anchor_tree_bound`]); `None` for none. legacy_anchor_tree_bound: Option, + /// Whether a Clear reset this store but has not yet seen the host wipe + /// its rows, as `shielded_store_meta` keeps it + /// ([`Self::set_host_wipe_pending`]). + host_wipe_pending: bool, } /// The private contact request association a saved record carries. @@ -520,6 +525,7 @@ impl FileBackedShieldedStore { private_payment_journals: BTreeSet::new(), unreadable_private_payment_journal: BTreeSet::new(), legacy_anchor_tree_bound: None, + host_wipe_pending: false, }; // Every record loaded below without an anchor height was saved by an // earlier process, from a tree no larger than the largest the store @@ -528,6 +534,7 @@ impl FileBackedShieldedStore { let size = store.tree_size()?; let largest = store.raise_largest_tree_size(size)?; store.legacy_anchor_tree_bound = (largest > 0).then_some(largest); + store.host_wipe_pending = store.read_host_wipe_pending()?; store.rehydrate_pending_spends()?; store.load_private_payment_journal()?; Ok(store) @@ -562,6 +569,47 @@ impl FileBackedShieldedStore { }) } + /// Whether a Clear reset this store and the host wipe that completes it + /// has not succeeded since, in this process or an earlier one. + pub fn host_wipe_pending(&self) -> bool { + self.host_wipe_pending + } + + /// Durably record whether a Clear is waiting on its host wipe. Set before + /// a Clear resets anything and cleared only once the host has wiped, so a + /// process that dies in between reopens with it still set. + pub fn set_host_wipe_pending(&mut self, pending: bool) -> Result<(), FileShieldedStoreError> { + let conn = self + .pending_conn + .lock() + .map_err(|e| FileShieldedStoreError::Storage(format!("pending_conn mutex: {e}")))?; + conn.execute( + "INSERT INTO shielded_store_meta (key, value) VALUES ('host_wipe_pending', ?1) \ + ON CONFLICT(key) DO UPDATE SET value = excluded.value", + rusqlite::params![i64::from(pending)], + ) + .map_err(|e| FileShieldedStoreError::Storage(format!("write host wipe marker: {e}")))?; + drop(conn); + self.host_wipe_pending = pending; + Ok(()) + } + + fn read_host_wipe_pending(&self) -> Result { + let conn = self + .pending_conn + .lock() + .map_err(|e| FileShieldedStoreError::Storage(format!("pending_conn mutex: {e}")))?; + let value: Option = conn + .query_row( + "SELECT value FROM shielded_store_meta WHERE key = 'host_wipe_pending'", + [], + |row| row.get(0), + ) + .optional() + .map_err(|e| FileShieldedStoreError::Storage(format!("read host wipe marker: {e}")))?; + Ok(value.is_some_and(|value| value != 0)) + } + /// Reload every persisted [`PendingRedrive`] into the in-memory /// per-subwallet state, re-arming both the redrive record and the /// note reservations its nullifiers carry — an unconfirmed @@ -1759,6 +1807,27 @@ mod tests { std::env::temp_dir().join(format!("shielded_tree_test_{tag}_{nanos}.sqlite")) } + /// The pending host wipe mark survives a reopen, and so does clearing it. + #[test] + fn should_keep_the_host_wipe_mark_across_a_reopen() { + let path = temp_tree_path("host_wipe_mark"); + let mut store = FileBackedShieldedStore::open_path(&path, 100).unwrap(); + assert!(!store.host_wipe_pending(), "a new store has none"); + store.set_host_wipe_pending(true).unwrap(); + drop(store); + let mut store = FileBackedShieldedStore::open_path(&path, 100).unwrap(); + assert!(store.host_wipe_pending()); + store.reset_commitment_tree().unwrap(); + store.purge_all_subwallets().unwrap(); + assert!(store.host_wipe_pending(), "a reset keeps the mark"); + store.set_host_wipe_pending(false).unwrap(); + drop(store); + let store = FileBackedShieldedStore::open_path(&path, 100).unwrap(); + assert!(!store.host_wipe_pending()); + drop(store); + let _ = std::fs::remove_file(&path); + } + /// A [`PendingRedrive`] survives a store reopen — record, attempt /// counter, AND the note reservations its nullifiers carry — and is /// deleted (durably) when one of its nullifiers resolves. diff --git a/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs b/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs index fcf4263dd4e..8171bc459e8 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs @@ -270,6 +270,12 @@ impl PlatformWallet { // an unfundable asset-lock tx (or paying for an Orchard proof // build, ~30s, only to reject downstream). validate_shielded_recipients(&recipients)?; + // Nothing is broadcast while a Clear's host wipe is pending, which + // refuses the activity write a funding ends with. Not held: the + // broadcast and ChainLock wait below must not hold a Clear back. + if let Ok(coordinator) = self.shielded_coordinator().await { + drop(coordinator.admit().await?); + } let (requested_recipient, _) = *recipients.first().expect("preflight enforces len() == 1"); // A resume pays the recipient its lock was built for. The row recorded @@ -799,7 +805,13 @@ impl PlatformWallet { let Ok(coordinator) = self.shielded_coordinator().await else { return; }; - let _admission = coordinator.admit().await; + let _admission = match coordinator.admit().await { + Ok(admission) => admission, + Err(error) => { + tracing::warn!(%error, "shield-from-asset-lock activity not recorded"); + return; + } + }; let confirmed = with_status(&pending, ShieldedActivityStatus::Confirmed, None); let id = crate::wallet::shielded::SubwalletId::new(self.wallet_id(), account); crate::wallet::shielded::operations::queue_shielded_activity( diff --git a/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs index a4657610193..24f45eeba1e 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/lifecycle_boundary_tests.rs @@ -525,7 +525,7 @@ async fn should_abort_clear_with_nothing_reset_when_admission_is_held_past_the_b wallet.bind_shielded(&SEED, &[0]).await.unwrap(); let epoch = coordinator.clear_epoch().await; - let in_flight = coordinator.admit().await; + let in_flight = coordinator.admit().await.unwrap(); let error = coordinator .clear(&*host) .await @@ -639,6 +639,114 @@ async fn should_refuse_binds_until_a_failed_host_wipe_is_retried() { .expect("binds again"); } +/// Whether `error` is the refusal of an admission while a Clear's host wipe +/// is pending. +fn refused_for_pending_wipe(error: &PlatformWalletError) -> bool { + matches!(error, PlatformWalletError::ShieldedStoreError(message) + if message.contains("did not wipe host storage")) +} + +/// While a failed Clear's host wipe is pending, no shielded operation runs: +/// a mutation, the balance read and a restoration from the journal are +/// refused, not served from the reset store. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_refuse_mutations_and_balance_reads_while_the_host_wipe_is_pending() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + let (manager, wallet, coordinator) = configured(&host, &dir).await; + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + + host.fail_wipe.store(true, Ordering::SeqCst); + manager + .clear_shielded() + .await + .expect_err("the host wipe failed"); + + let refused = wallet + .abandon_shielded_identity_debit(0, [0x33; 32], true) + .await + .expect_err("a mutation is refused"); + assert!(refused_for_pending_wipe(&refused), "{refused:?}"); + let refused = wallet + .shielded_balances() + .await + .expect_err("the balance read is refused, not reported empty"); + assert!(refused_for_pending_wipe(&refused), "{refused:?}"); + let refused = coordinator + .local_balance_snapshot(wallet.wallet_id()) + .await + .expect_err("so is the local balance snapshot"); + assert!(refused_for_pending_wipe(&refused), "{refused:?}"); + let crypto = crate::wallet::identity::network::SeedCryptoProvider::from_seed( + [0x17; 64], + Network::Testnet, + ); + let refused = wallet + .restore_pending_private_contact_requests(&crypto) + .await + .expect_err("a restoration from the shielded store is refused"); + assert!(refused_for_pending_wipe(&refused), "{refused:?}"); + + host.fail_wipe.store(false, Ordering::SeqCst); + manager.clear_shielded().await.expect("the retry wipes"); + let balances = wallet.shielded_balances().await.expect("readable again"); + assert!( + balances.values().all(|&balance| balance == 0), + "{balances:?}" + ); + wallet + .restore_pending_private_contact_requests(&crypto) + .await + .expect("restorations run again"); +} + +/// A host wipe that failed is still pending after a restart: the store keeps +/// the mark, so a reopened coordinator refuses binds and mutations, and the +/// host's surviving rows are never restored, until a later Clear wipes them. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_keep_a_failed_host_wipe_pending_across_a_reopen() { + let dir = tempfile::tempdir().unwrap(); + let host = BoundaryHost::new(); + { + let (manager, wallet, _coordinator) = configured(&host, &dir).await; + wallet.bind_shielded(&SEED, &[0]).await.unwrap(); + host.fail_wipe.store(true, Ordering::SeqCst); + manager + .clear_shielded() + .await + .expect_err("the host wipe failed"); + let _ = manager.shutdown().await; + } + *host.coordinator.lock().unwrap() = None; + host.fail_wipe.store(false, Ordering::SeqCst); + assert_eq!(host.viewing_key_rows(), 1, "the host kept its rows"); + + let (manager, wallet, coordinator) = configured(&host, &dir).await; + let refused = wallet + .bind_shielded_from_persisted(&[0]) + .await + .expect_err("a reopened coordinator still refuses the bind"); + assert!(refused_for_pending_wipe(&refused), "{refused:?}"); + let refused = wallet + .bind_shielded(&SEED, &[0]) + .await + .expect_err("a seed bind too"); + assert!(refused_for_pending_wipe(&refused), "{refused:?}"); + let refused = wallet + .abandon_shielded_identity_debit(0, [0x33; 32], true) + .await + .expect_err("and a mutation"); + assert!(refused_for_pending_wipe(&refused), "{refused:?}"); + assert!(coordinator.registered_subwallets().await.is_empty()); + + manager.clear_shielded().await.expect("the retry wipes"); + assert_eq!(host.viewing_key_rows(), 0); + wallet + .bind_shielded(&SEED, &[0]) + .await + .expect("binds again"); +} + /// The manager owns the coordinator: its wallets resolve exactly the /// manager's, and none before the manager configured one. #[tokio::test(flavor = "multi_thread", worker_threads = 4)] diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index 5543a04fb0e..f7f62325d3d 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -26,6 +26,12 @@ private enum AssetLockPersistenceError: Error { case requiresChangeset } +private enum ShieldedWipeError: Error { + /// A wallet with shielded rows has no network yet, so its rows may be + /// the wiped network's. + case walletNetworkUnknown +} + struct LiveModelFetcher: ModelFetching { func fetch( _ descriptor: FetchDescriptor, @@ -6280,15 +6286,44 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { /// Native Clear's host half (`on_wipe_shielded_state_fn`): delete the /// shielded rows of every wallet persisted on `network`, loaded or not, - /// in one save. A failed read or save rolls the context back and - /// throws, leaving the rows in place. + /// in one save. Shielded rows carry no wallet relationship, so they are + /// enumerated from the shielded models. Rows of a wallet with no wallet + /// row belong to no wallet on any network and go too, as a wallet + /// deletion would have taken them. Rows of a wallet whose network is not + /// yet known throw with nothing deleted: they may be `network`'s. A + /// failed read or save rolls the context back and throws, leaving the + /// rows in place. func wipeShieldedState(network: Network) throws { try onQueue { let networkRaw = network.rawValue do { - let wallets = try backgroundContext.fetch(FetchDescriptor( - predicate: #Predicate { $0.networkRaw == networkRaw })) - for walletId in Set(wallets.map(\.walletId)) { + var shieldedWalletIds = Set() + shieldedWalletIds.formUnion(try backgroundContext.fetch( + FetchDescriptor()).map(\.walletId)) + shieldedWalletIds.formUnion(try backgroundContext.fetch( + FetchDescriptor()).map(\.walletId)) + shieldedWalletIds.formUnion(try backgroundContext.fetch( + FetchDescriptor()).map(\.walletId)) + shieldedWalletIds.formUnion(try backgroundContext.fetch( + FetchDescriptor()).map(\.walletId)) + shieldedWalletIds.formUnion(try backgroundContext.fetch( + FetchDescriptor()).map(\.walletId)) + var doomed: [Data] = [] + for walletId in shieldedWalletIds { + let wallet = try backgroundContext.fetch(FetchDescriptor( + predicate: #Predicate { $0.walletId == walletId })).first + guard let wallet else { + doomed.append(walletId) + continue + } + guard let walletNetwork = wallet.networkRaw else { + throw ShieldedWipeError.walletNetworkUnknown + } + if walletNetwork == networkRaw { + doomed.append(walletId) + } + } + for walletId in doomed { try backgroundContext.delete( model: PersistentShieldedNote.self, where: #Predicate { $0.walletId == walletId }) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ShieldedStateWipeTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ShieldedStateWipeTests.swift index b73d4521b62..6980e5cab5c 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ShieldedStateWipeTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ShieldedStateWipeTests.swift @@ -56,4 +56,54 @@ final class ShieldedStateWipeTests: XCTestCase { XCTAssertEqual( try walletIds(PersistentShieldedViewingKey.self, container, \.walletId), [mainnetWallet]) } + + private func seedRows(_ context: ModelContext, _ walletId: Data) { + context.insert(PersistentShieldedSyncState( + walletId: walletId, accountIndex: 0, lastSyncedIndex: 42)) + context.insert(PersistentShieldedViewingKey( + walletId: walletId, accountIndex: 0, fvkBytes: Data(repeating: 1, count: 96))) + } + + func testShouldFailWithNothingDeletedWhileAWalletWithShieldedRowsHasNoNetwork() throws { + let container = try DashModelContainer.createInMemory() + let unknownNetworkWallet = Data(repeating: 0x63, count: 32) + let context = ModelContext(container) + context.insert(PersistentWallet(walletId: testnetWallet, network: .testnet)) + context.insert(PersistentWallet(walletId: unknownNetworkWallet)) + seedRows(context, testnetWallet) + seedRows(context, unknownNetworkWallet) + try context.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + let wipe = try XCTUnwrap(handler.makePersistenceCallbacksExtension().on_wipe_shielded_state_fn) + + let rc = wipe(Unmanaged.passUnretained(handler).toOpaque(), Network.testnet.ffiValue) + + XCTAssertNotEqual(rc, 0) + XCTAssertEqual( + Set(try walletIds(PersistentShieldedSyncState.self, container, \.walletId)), + [testnetWallet, unknownNetworkWallet]) + XCTAssertEqual( + Set(try walletIds(PersistentShieldedViewingKey.self, container, \.walletId)), + [testnetWallet, unknownNetworkWallet]) + } + + func testShouldDeleteTheRowsOfAWalletWithNoWalletRow() throws { + let container = try DashModelContainer.createInMemory() + let orphan = Data(repeating: 0x64, count: 32) + let context = ModelContext(container) + context.insert(PersistentWallet(walletId: mainnetWallet, network: .mainnet)) + seedRows(context, mainnetWallet) + seedRows(context, orphan) + try context.save() + let handler = PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet) + let wipe = try XCTUnwrap(handler.makePersistenceCallbacksExtension().on_wipe_shielded_state_fn) + + let rc = wipe(Unmanaged.passUnretained(handler).toOpaque(), Network.testnet.ffiValue) + + XCTAssertEqual(rc, 0) + XCTAssertEqual( + try walletIds(PersistentShieldedSyncState.self, container, \.walletId), [mainnetWallet]) + XCTAssertEqual( + try walletIds(PersistentShieldedViewingKey.self, container, \.walletId), [mainnetWallet]) + } } From 36e554933a72d152666c83c11f3a58716f6c81bc Mon Sep 17 00:00:00 2001 From: pasta Date: Sun, 15 Nov 2026 17:20:54 +0000 Subject: [PATCH 4/4] fix(platform-wallet): refuse unconfigured shielded funding and wipe rowless viewing keys Shielded funding now fails with the coordinator error before resolving any funding, so a manager without configure_shielded never builds or broadcasts an asset lock that has no shielded store behind it. The SQLite shielded wipe no longer flushes a wallet that has neither a wallets row nor buffered metadata: its viewing key could only fail the foreign key and block every Clear. Its buffered shielded writes are dropped instead, and the wipe also deletes stored viewing keys whose wallet has no row, matching the Kotlin and Swift wipes. Co-Authored-By: Claude Opus 5.5 --- .../src/sqlite/buffer.rs | 20 +++++ .../src/sqlite/persister.rs | 24 ++++-- .../sqlite/schema/shielded_viewing_keys.rs | 6 +- .../sqlite_shielded_viewing_keys.rs | 45 ++++++++++ .../wallet/shielded/fund_from_asset_lock.rs | 12 +-- .../src/wallet/shielded/wallet_drain_tests.rs | 83 +++++++++++++++---- 6 files changed, 159 insertions(+), 31 deletions(-) diff --git a/packages/rs-platform-wallet-storage/src/sqlite/buffer.rs b/packages/rs-platform-wallet-storage/src/sqlite/buffer.rs index 4b03e9041a3..45bbef4288c 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/buffer.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/buffer.rs @@ -227,6 +227,26 @@ impl Buffer { })) } + /// Drop the buffered shielded changesets of `wallet_id`, keeping the + /// rest of its buffered writes. + #[cfg(feature = "shielded")] + pub fn discard_shielded(&self, wallet_id: &WalletId) -> Result<(), WalletStorageError> { + let mut guard = self + .inner + .lock() + .map_err(|_| WalletStorageError::LockPoisoned)?; + if let Some(pending) = guard.get_mut(wallet_id) { + for segment in &mut pending.segments { + segment.shielded = None; + } + pending.segments.retain(|segment| !segment.is_empty()); + if pending.segments.is_empty() { + guard.remove(wallet_id); + } + } + Ok(()) + } + /// Every wallet currently holding buffered data, sorted by id for /// deterministic flush ordering. pub fn dirty_wallets(&self) -> Result, WalletStorageError> { diff --git a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs index b0507da16c8..9820fda505d 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs @@ -1341,8 +1341,10 @@ impl PlatformWalletPersistence for SqlitePersister { /// Flushes this network's buffered writes under the delete's connection /// guard, then deletes the network's viewing keys in one transaction. A /// wallet with no stored row yet is classified by the network of its - /// buffered metadata, and flushed too when it has none. A flush failure - /// fails the wipe with the rows in place. + /// buffered metadata. One with neither has an unknown network, like a + /// viewing-key row whose wallet has no row: its shielded writes are + /// dropped on every network's wipe. A flush failure fails the wipe with + /// the rows in place. #[cfg(feature = "shielded")] fn wipe_shielded_state(&self, network: dashcore::Network) -> Result<(), PersistenceError> { self.ensure_writable("wipe_shielded_state") @@ -1356,16 +1358,24 @@ impl PlatformWalletPersistence for SqlitePersister { .dirty_wallets() .map_err(PersistenceError::from)? { - let other_network = + let this_network = match schema::wallets::fetch(&conn, &wallet_id).map_err(PersistenceError::from)? { - Some((stored_network, _)) => stored_network != label, - None => self + Some((stored_network, _)) => stored_network == label, + None => match self .buffer .buffered_network(&wallet_id) .map_err(PersistenceError::from)? - .is_some_and(|buffered| buffered != network), + { + Some(buffered) => buffered == network, + None => { + self.buffer + .discard_shielded(&wallet_id) + .map_err(PersistenceError::from)?; + false + } + }, }; - if !other_network { + if this_network { self.flush_locked(&mut conn, &wallet_id)?; } } diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/shielded_viewing_keys.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/shielded_viewing_keys.rs index 2f343e3b495..39c821b65a8 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/shielded_viewing_keys.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/shielded_viewing_keys.rs @@ -68,14 +68,16 @@ pub(crate) fn apply( Ok(()) } -/// Delete the viewing keys of every wallet stored on `network`. +/// Delete the viewing keys of every wallet stored on `network`, and of every +/// wallet with no `wallets` row, whose network is unknown. pub(crate) fn delete_for_network( tx: &Transaction<'_>, network: key_wallet::Network, ) -> Result<(), WalletStorageError> { tx.execute( "DELETE FROM shielded_viewing_keys WHERE wallet_id IN \ - (SELECT wallet_id FROM wallets WHERE network = ?1)", + (SELECT wallet_id FROM wallets WHERE network = ?1) \ + OR wallet_id NOT IN (SELECT wallet_id FROM wallets)", params![wallets::network_to_str(network)], )?; Ok(()) diff --git a/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs b/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs index ca413a6f4e1..371f67d613a 100644 --- a/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs +++ b/packages/rs-platform-wallet-storage/tests/integration/sqlite_shielded_viewing_keys.rs @@ -281,3 +281,48 @@ fn shielded_wipe_classifies_a_wallet_with_no_row_by_its_buffered_network() { persister.commit_writes().expect("flush after wipe"); assert_eq!(viewing_key_count(&path, &mainnet_wallet), 1); } + +#[test] +fn shielded_wipe_drops_viewing_keys_of_wallets_with_no_row_or_metadata() { + let (persister, _tmp, path) = + common::fresh_persister_with_mode(platform_wallet_storage::FlushMode::Manual); + let orphan_wallet = wid(0x78); + let mainnet_wallet = wid(0x79); + let stored_orphan = wid(0x7A); + insert_mainnet_wallet(&persister, &mainnet_wallet); + { + // A row the foreign key would refuse today, as a database written + // without enforcement could still hold. + let conn = persister.lock_conn_for_test(); + conn.pragma_update(None, "foreign_keys", "OFF").unwrap(); + conn.execute( + "INSERT INTO shielded_viewing_keys (wallet_id, account_index, viewing_key) \ + VALUES (?1, 0, ?2)", + rusqlite::params![stored_orphan.as_slice(), [0xE8u8; 96].as_slice()], + ) + .unwrap(); + conn.pragma_update(None, "foreign_keys", "ON").unwrap(); + } + persister + .store( + mainnet_wallet, + changeset(SubwalletId::new(mainnet_wallet, 0), [0xE6; 96]), + ) + .expect("buffer the mainnet viewing key"); + persister.commit_writes().expect("store the mainnet key"); + persister + .store( + orphan_wallet, + changeset(SubwalletId::new(orphan_wallet, 0), [0xE7; 96]), + ) + .expect("buffer a viewing key with no wallet row or metadata"); + + persister + .wipe_shielded_state(dashcore::Network::Testnet) + .expect("wipe testnet"); + persister.commit_writes().expect("flush after wipe"); + + assert_eq!(viewing_key_count(&path, &orphan_wallet), 0); + assert_eq!(viewing_key_count(&path, &stored_orphan), 0); + assert_eq!(viewing_key_count(&path, &mainnet_wallet), 1); +} diff --git a/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs b/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs index 8171bc459e8..f14b9dbf463 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/fund_from_asset_lock.rs @@ -270,12 +270,12 @@ impl PlatformWallet { // an unfundable asset-lock tx (or paying for an Orchard proof // build, ~30s, only to reject downstream). validate_shielded_recipients(&recipients)?; - // Nothing is broadcast while a Clear's host wipe is pending, which - // refuses the activity write a funding ends with. Not held: the - // broadcast and ChainLock wait below must not hold a Clear back. - if let Ok(coordinator) = self.shielded_coordinator().await { - drop(coordinator.admit().await?); - } + // Nothing is broadcast without a shielded store to receive the + // funding, or while a Clear's host wipe is pending, which refuses the + // activity write a funding ends with. Not held: the broadcast and + // ChainLock wait below must not hold a Clear back. + let coordinator = self.shielded_coordinator().await?; + drop(coordinator.admit().await?); let (requested_recipient, _) = *recipients.first().expect("preflight enforces len() == 1"); // A resume pays the recipient its lock was built for. The row recorded diff --git a/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain_tests.rs index 69c02248521..ea67871df6d 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/wallet_drain_tests.rs @@ -227,6 +227,30 @@ async fn signed_wallet_fixture( TestDirectory, WalletSigner, ) { + let (wallet, persistence, signer) = unconfigured_wallet_fixture(lock, keys).await; + let dir = + TestDirectory(std::env::temp_dir().join(format!("wallet_drain_{}", rand::random::()))); + std::fs::create_dir_all(&dir.0).unwrap(); + let path = dir.0.join("shielded.sqlite"); + let store = FileBackedShieldedStore::open_path(&path, 100).unwrap(); + let coordinator = Arc::new(NetworkShieldedCoordinator::new( + wallet.sdk.clone(), + Network::Testnet, + path, + store, + )); + wallet + .attach_shielded_coordinator_for_test(&coordinator) + .await; + (wallet, persistence, coordinator, dir, signer) +} + +/// [`signed_wallet_fixture`] before any `configure_shielded`: the wallet has +/// no shielded coordinator. +async fn unconfigured_wallet_fixture( + lock: &TrackedAssetLock, + keys: BTreeMap, +) -> (PlatformWallet, Arc, WalletSigner) { let (manager, id, generation, signer) = funded_wallet_manager(StandardAccountType::BIP44Account).await; let (stored_wallet, info) = { @@ -258,7 +282,7 @@ async fn signed_wallet_fixture( Arc::new(PlatformEventManager::new(vec![])), )); let wallet = PlatformWallet::new( - sdk.clone(), + sdk, id, manager.clone(), generation, @@ -277,21 +301,7 @@ async fn signed_wallet_fixture( .tracked_asset_locks .insert(lock.out_point, lock.clone()); *wallet.shielded_keys.write().await = Some(keys); - let dir = - TestDirectory(std::env::temp_dir().join(format!("wallet_drain_{}", rand::random::()))); - std::fs::create_dir_all(&dir.0).unwrap(); - let path = dir.0.join("shielded.sqlite"); - let store = FileBackedShieldedStore::open_path(&path, 100).unwrap(); - let coordinator = Arc::new(NetworkShieldedCoordinator::new( - sdk, - Network::Testnet, - path, - store, - )); - wallet - .attach_shielded_coordinator_for_test(&coordinator) - .await; - (wallet, persistence, coordinator, dir, signer) + (wallet, persistence, signer) } /// A fresh wallet handle whose live tracked locks come from `persistence`'s @@ -880,6 +890,47 @@ async fn should_report_a_fresh_shield_lock_to_the_caller() { .any(|tracked| tracked.out_point == seen[0])); } +/// Funding a manager whose `configure_shielded` never ran fails before any +/// Core funding: no lock is built, tracked, persisted or broadcast, so no +/// transparent funds move without a shielded store behind them. +#[tokio::test] +async fn should_refuse_funding_before_any_lock_without_a_shielded_coordinator() { + let (mut lock, keys) = drain_lock_fixture(); + lock.operation_id = None; + let recipient = dpp::address_funds::OrchardAddress::from(keys[&0].default_address); + let (wallet, persistence, signer) = unconfigured_wallet_fixture(&lock, keys).await; + let prover = super::prover::CachedOrchardProver::new(); + let seen = Mutex::new(Vec::new()); + let observer = |out_point: OutPoint| seen.lock().unwrap().push(out_point); + + let error = wallet + .shielded_fund_from_asset_lock( + AssetLockFunding::FromWalletBalance { + amount_duffs: 5_000_000, + account_index: 0, + }, + vec![(recipient, None)], + &signer, + &prover, + None, + 0, + None, + None, + Some(&observer), + ) + .await + .unwrap_err(); + + assert!( + matches!(&error, PlatformWalletError::ShieldedStoreError(message) + if message.contains("not configured")), + "{error}" + ); + assert!(seen.into_inner().unwrap().is_empty()); + assert_eq!(wallet.asset_locks.list_tracked_locks().await.len(), 1); + assert!(persistence.staged.lock().unwrap().is_empty()); +} + /// The pooled wallet drain reaches the observer through /// `create_wallet_drain_asset_lock_proof`, before its row is stored: the /// caller hears the drained lock's outpoint even when the broadcast is then