diff --git a/packages/kotlin-sdk/KotlinExampleApp/app/src/main/java/org/dashfoundation/example/ui/shielded/ShieldedActivityScreen.kt b/packages/kotlin-sdk/KotlinExampleApp/app/src/main/java/org/dashfoundation/example/ui/shielded/ShieldedActivityScreen.kt index cd53f9a43f5..66ac373deda 100644 --- a/packages/kotlin-sdk/KotlinExampleApp/app/src/main/java/org/dashfoundation/example/ui/shielded/ShieldedActivityScreen.kt +++ b/packages/kotlin-sdk/KotlinExampleApp/app/src/main/java/org/dashfoundation/example/ui/shielded/ShieldedActivityScreen.kt @@ -131,6 +131,7 @@ private fun statusLabel(status: Int): String = when (status) { 0 -> "Pending" 1 -> "Confirmed" 2 -> "Failed" + 4 -> "Failed, inputs spent" else -> "Unknown" } diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt index 91547fb3e1d..ea204109ab0 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/errors/DashSdkError.kt @@ -795,6 +795,21 @@ sealed class DashSdkError( class PublicContactRequestUnconfirmed(message: String, cause: Throwable? = null) : PlatformWallet(message, cause) + /** + * `ErrorShieldedIdentityCreateInputsConsumed` (native code 77). An + * identity create from the shielded pool did not create its identity + * and can never execute, as proven reads show: its notes are spent and + * the identity is absent. The wallet marked the notes spent. Not + * retryable: the notes are gone. The reads do not say what spent them: + * Platform's paid failure path for this transition (which credits the + * denomination minus a penalty to its fallback address) is one + * possible cause, the same notes spent by another transition (another + * device with this wallet's keys) another. + * ← Swift `PlatformWalletError.shieldedIdentityCreateInputsConsumed`. + */ + class ShieldedIdentityCreateInputsConsumed(message: String, cause: Throwable? = null) : + PlatformWallet(message, cause) + /** * Any other `PlatformWalletFFIResultCode` without a dedicated type. * Carries the platform-wallet [nativeCode] (already de-offset) and @@ -1004,6 +1019,7 @@ sealed class DashSdkError( 74 -> PlatformWallet.PrivateContactStoreNotRead(message, cause) // ErrorPrivateContactStoreNotRead 75 -> PlatformWallet.SdkPinnedBelowProtocolVersion(message, cause) // ErrorSdkPinnedBelowProtocolVersion 76 -> PlatformWallet.PublicContactRequestUnconfirmed(message, cause) // ErrorPublicContactRequestUnconfirmed + 77 -> PlatformWallet.ShieldedIdentityCreateInputsConsumed(message, cause) // ErrorShieldedIdentityCreateInputsConsumed else -> // @Deprecated fallback — see the code-6 arm; code 31 is the // real discriminator. diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt index f56f0736e5e..4e4f67ecb6d 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/errors/DashSdkErrorTest.kt @@ -400,6 +400,16 @@ class DashSdkErrorTest { assertFalse("a sync settles it, not a retry", mapped.isRetryable) } + @Test + fun consumedShieldedIdentityCreateCode77MapsTyped() { + val detail = "Shielded identity create did not create its identity" + val native = DashSDKException(DashSdkError.PLATFORM_WALLET_CODE_OFFSET + 77, detail) + val mapped = DashSdkError.fromNative(native) + assertTrue(mapped is DashSdkError.PlatformWallet.ShieldedIdentityCreateInputsConsumed) + assertEquals(detail, mapped.message) + assertFalse("its notes are spent", mapped.isRetryable) + } + @Test fun platformShieldCapacityCode41MapsTyped() { val message = diff --git a/packages/rs-dapi-client/src/dapi_client.rs b/packages/rs-dapi-client/src/dapi_client.rs index 2d0fe426245..6974d25e0ab 100644 --- a/packages/rs-dapi-client/src/dapi_client.rs +++ b/packages/rs-dapi-client/src/dapi_client.rs @@ -186,6 +186,16 @@ impl DapiClient { &self.address_list } + /// A copy of this client that selects its addresses from `address_list` + /// instead, with the same settings, certificate and proxy. The original + /// client and its address list are not changed. + pub fn with_address_list(&self, address_list: AddressList) -> Self { + Self { + address_list, + ..self.clone() + } + } + /// Get all non-banned addresses from the address list. /// /// Returns a vector of addresses that are not currently banned or whose ban period has expired. diff --git a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md index a5655430abd..f895312401c 100644 --- a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md +++ b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md @@ -114,7 +114,7 @@ These are shipped ABI. Do not renumber. | 98 | `NotFound` | Sentinel — `Option` returned as an error | | 99 | `ErrorUnknown` | Sentinel — unmapped/flattened errors | -**Next allocatable integer: 77**: 27–76 are all claimed (27, 29, 31, 34–42 +**Next allocatable integer: 78**: 27–77 are all claimed (27, 29, 31, 34–42 and 46 merged; 43–45 proposed by active #4313 at head `0302b188ab`; 47 and 48 proposed by active #4356 (47 renumbered from 42, 48 from 43 — see their rows below); 49–54 proposed by active #4586 (the persister @@ -128,7 +128,9 @@ renumbered from 59 when #5322 took that number on `v5.1-dev`); 61 proposed by by open branches with no row here yet (62 both `ErrorShieldedClaimBindingMismatch` and `ErrorIdentityRevisionMismatch`, 63–66 the invitation-claim preflight, 67 `ErrorShieldedTreeInconsistent`); 68 -to 76 proposed for DashPay private contact requests; 28, 30, +to 76 proposed for DashPay private contact requests; 77 proposed for a +shielded identity create proven to have its inputs consumed without its +identity; 28, 30, 32 and 33 reserved). **28, 30, 32 and 33 are RESERVED, not free**: 28 and 30 were vacated when the reservation trio moved to 34–36; 32 and 33 lapsed when their in-repo owners @@ -192,6 +194,7 @@ Fork-era numbers remain in the collision history, which is immutable record. | 74 | `ErrorPrivateContactStoreNotRead` | DashPay private contact requests | Proposed. Maps `PlatformWalletError::PrivateContactStoreNotRead`: DashPay's private contact request store was not read to its end for the identity, with the keys Platform proves it holds, before a contact request or acceptance, public or private, was decided (every one reads it from the identity's cursor first). Nothing was reserved, sealed, paid or broadcast; calling again continues the read from where it stopped. A node behind the store already seen, or keys that kept changing, stop it. It was reported as 71 before, which says not to submit again. Mapped by the blanket conversion and kept by the shielded spend contract (`map_spend_result`). Swift types it (`PlatformWalletError.privateContactStoreNotRead`, `ErrorHandlingTests` pin) and SwiftExampleApp offers a retry with no pending row; Kotlin types it (`DashSdkError.PlatformWallet.PrivateContactStoreNotRead`, `DashSdkErrorTest` pin) and KotlinExampleApp offers a retry with no pending row | | 75 | `ErrorSdkPinnedBelowProtocolVersion` | DashPay private contact requests | Proposed. The SDK refused a request without sending it because the SDK itself is pinned below the protocol version the request needs (`dash_sdk::Error::ProtocolVersionNotActive` with `pinned`), whatever the network runs: for example every contact request's private store read with the SDK pinned below protocol version 15 on a network at 15. A network upgrade does not lift it; only raising or removing the pin does. Before, it shared 70 with a network proven below the version, which lifts by itself. Mapped by the blanket conversion. Swift types it (`PlatformWalletError.sdkPinnedBelowProtocolVersion`, `ErrorHandlingTests` pin) and SwiftExampleApp shows a developer-facing message; Kotlin types it (`DashSdkError.PlatformWallet.SdkPinnedBelowProtocolVersion`, `DashSdkErrorTest` pin) and KotlinExampleApp shows a developer-facing message | | 76 | `ErrorPublicContactRequestUnconfirmed` | DashPay private contact requests | Proposed. Maps `PlatformWalletError::PublicContactRequestUnconfirmed`: a public contact request to that identity was broadcast and may have been delivered, and no proof settled its outcome yet, so the wallet holds the pair and signs nothing new to it, public or private. The send or accept that broadcast it reported 20; every later one is refused with 76 until a public contact request sync settles it (its document proved on Platform: recorded as sent; its transition proved unable to write it: the pair freed), broadcasting the same signed bytes again meanwhile. Not a private request pending (71). Mapped by the blanket conversion and by `map_spend_result`. `platform_wallet_get_public_contact_request_unconfirmed` reads the hold per pair (`DashPayView::public_contact_request_unconfirmed`; 7 for a sender the wallet does not hold). Swift types it (`PlatformWalletError.publicContactRequestUnconfirmed`, `ErrorHandlingTests` pin) and wraps the read (`ManagedPlatformWallet.publicContactRequestUnconfirmed`); SwiftExampleApp shows the pair as being checked, with no request or accept offered, until the read says settled, and its refreshes run the DashPay sync that settles it; Kotlin types it (`DashSdkError.PlatformWallet.PublicContactRequestUnconfirmed`, `DashSdkErrorTest` pin), bridges the read (`DashpayNative.publicContactRequestUnconfirmed`, `Dashpay.publicContactRequestsUnconfirmed`), and KotlinExampleApp shows the pair as SwiftExampleApp does | +| 77 | `ErrorShieldedIdentityCreateInputsConsumed` | Shielded identity create, inputs consumed | Proposed. Maps `PlatformWalletError::ShieldedIdentityCreateInputsConsumed`: an identity created from the shielded pool (Type 20) whose result wait failed, its identity not found, is proven by reads at the node it was delivered to to have its inputs consumed without its identity: every nullifier it spends is spent, and the identity is absent at or above the height they were spent by. It can never execute. The notes are marked spent and the activity row failed; the recovery record keeps the outcome until the sync pass has written that row durably; nothing is to be re-submitted. The reads do not say what spent the notes: Platform's paid failure path for this transition is one possible cause, the same notes spent by another transition another. `out_identity_id` is not written and no consensus code rides the result. Typed in Rust, Swift (`PlatformWalletError.shieldedIdentityCreateInputsConsumed`) and Kotlin (`PlatformWallet.ShieldedIdentityCreateInputsConsumed`) | **Code 31 left this table on 2026-08-04.** `ErrorSigningKeyUnavailable` sat here as #4183's proposal until #4183 merged (`189a3abb1c`); it is now in the merged diff --git a/packages/rs-platform-wallet-ffi/src/error.rs b/packages/rs-platform-wallet-ffi/src/error.rs index fbd1b5b2b37..8b8bb34fb65 100644 --- a/packages/rs-platform-wallet-ffi/src/error.rs +++ b/packages/rs-platform-wallet-ffi/src/error.rs @@ -153,7 +153,8 @@ pub enum PlatformWalletFFIResultCode { /// could not confirm its execution result (a broadcast attempt that failed /// in transport, an endpoint's refusal or verdict, a result-proof /// fetch/verify failure, …), and a direct fetch of the derived id also - /// came back empty. The wallet keeps the notes reserved and the create's + /// came back empty, without proven reads showing a paid failure + /// ([`Self::ErrorShieldedIdentityCreateInputsConsumed`]). The wallet keeps the notes reserved and the create's /// recovery record, which it saved before the broadcast. The identity may /// already exist on chain, so the caller must NOT treat /// it as unregistered or re-submit. UNLIKE every other error code, @@ -768,6 +769,23 @@ pub enum PlatformWalletFFIResultCode { /// [`Self::ErrorPrivateContactRequestPending`] (71). ErrorPublicContactRequestUnconfirmed = 76, + /// Maps `PlatformWalletError::ShieldedIdentityCreateInputsConsumed`: an + /// identity create from the shielded pool did not create its identity and + /// can never execute, as proven reads (at the node it was delivered to) + /// show: every note it spends is spent, and the identity is absent at or + /// above the height they were spent by. The wallet marked the notes spent + /// and its activity row failed; the create's recovery record keeps the + /// outcome until the sync pass has written that row durably. Nothing is + /// to be re-submitted. The reads do not show what + /// spent the notes: Platform's paid failure path for this transition (a + /// key-state check failing in the block, which credits the denomination + /// minus a penalty to `send_to_address_on_creation_failure`) is one + /// possible cause, the same notes spent by another transition (another + /// device with this wallet's keys) another. `out_identity_id` is not + /// written. No consensus code rides the result: the proof is the reads, + /// not a node's verdict. + ErrorShieldedIdentityCreateInputsConsumed = 77, + /// The named thing does not exist. /// /// Originally (and still mostly) the code for every `Option` returned as an @@ -1080,6 +1098,9 @@ impl From for PlatformWalletFFIResult { PlatformWalletError::ShieldedBroadcastUnconfirmed { .. } => { PlatformWalletFFIResultCode::ErrorShieldedBroadcastUnconfirmed } + PlatformWalletError::ShieldedIdentityCreateInputsConsumed { .. } => { + PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateInputsConsumed + } // A broadcast that may have been delivered may still execute: it // reads as unconfirmed, never as failed, so a host does not pay // again with other notes. @@ -3120,6 +3141,30 @@ mod private_contact_request_error_tests { ); } + /// A shielded identity create proven unable to execute without its + /// identity reaches the host by its own code, with its message, and no + /// consensus code: the proof is the reads, not a node's verdict. + #[test] + fn should_report_a_consumed_shielded_identity_create_by_its_own_code() { + let consumed = PlatformWalletError::ShieldedIdentityCreateInputsConsumed { + identity_id: dpp::prelude::Identifier::from([7u8; 32]), + consumed_by_height: 12, + reason: "result wait failed".to_string(), + }; + let rendered = consumed.to_string(); + let result = PlatformWalletFFIResult::from(consumed); + assert_eq!( + result.code, + PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateInputsConsumed + ); + assert_eq!(result.code as u32, 77); + assert_eq!(result.consensus_code, 0); + let msg = unsafe { std::ffi::CStr::from_ptr(result.message) } + .to_string_lossy() + .into_owned(); + assert_eq!(msg, rendered, "Display payload must survive verbatim"); + } + /// A public contact request a loaded wallet refuses until its private /// payments are restored reaches the host by its own code, not as a /// contact's pending request. diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 37bcecc9f82..ba3531990e4 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -4779,7 +4779,7 @@ fn activity_direction_tag(d: &platform_wallet::wallet::shielded::ShieldedDirecti } /// Discriminant byte for a `ShieldedActivityStatus` (FFI: 0 Pending, -/// 1 Confirmed, 2 Failed, 3 Unknown). +/// 1 Confirmed, 2 Failed, 3 Unknown, 4 InputsConsumed). #[cfg(feature = "shielded")] fn activity_status_tag(s: &ShieldedActivityStatus) -> u8 { match s { @@ -4787,6 +4787,7 @@ fn activity_status_tag(s: &ShieldedActivityStatus) -> u8 { ShieldedActivityStatus::Confirmed => 1, ShieldedActivityStatus::Failed => 2, ShieldedActivityStatus::Unknown => 3, + ShieldedActivityStatus::InputsConsumed => 4, } } @@ -4798,6 +4799,7 @@ fn activity_status_from_tag(tag: u8) -> ShieldedActivityStatus { 1 => ShieldedActivityStatus::Confirmed, 2 => ShieldedActivityStatus::Failed, 3 => ShieldedActivityStatus::Unknown, + 4 => ShieldedActivityStatus::InputsConsumed, other => { tracing::warn!( status = other, @@ -4820,6 +4822,7 @@ mod activity_status_tests { (ShieldedActivityStatus::Confirmed, 1), (ShieldedActivityStatus::Failed, 2), (ShieldedActivityStatus::Unknown, 3), + (ShieldedActivityStatus::InputsConsumed, 4), ] { assert_eq!(activity_status_tag(&status), tag); assert_eq!(activity_status_from_tag(tag), status); diff --git a/packages/rs-platform-wallet-ffi/src/shielded_persistence.rs b/packages/rs-platform-wallet-ffi/src/shielded_persistence.rs index db1a704ab31..de686317ff1 100644 --- a/packages/rs-platform-wallet-ffi/src/shielded_persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/shielded_persistence.rs @@ -130,7 +130,7 @@ pub struct ShieldedActivityFFI { pub kind_tag: u8, /// Direction: 0 In, 1 Out, 2 Self. pub direction: u8, - /// Status: 0 Pending, 1 Confirmed, 2 Failed, 3 Unknown. + /// Status: 0 Pending, 1 Confirmed, 2 Failed, 3 Unknown, 4 InputsConsumed. pub status: u8, /// Display amount in credits (principal; excludes self-change and /// zero-value fillers). diff --git a/packages/rs-platform-wallet-ffi/src/shielded_send.rs b/packages/rs-platform-wallet-ffi/src/shielded_send.rs index 00161411428..69ed977138c 100644 --- a/packages/rs-platform-wallet-ffi/src/shielded_send.rs +++ b/packages/rs-platform-wallet-ffi/src/shielded_send.rs @@ -957,7 +957,12 @@ fn map_asset_lock_funding_result( /// expose). If identity creation fails a stateful check (a public-key hash already registered to /// another identity) the spend is still finalized and the value is credited to this address minus a /// penalty, exactly like the asset-lock / address-funded identity-create penalties. It is bound into -/// the transition sighash, so it cannot be redirected after signing. +/// the transition sighash, so it cannot be redirected after signing. A create whose notes proven +/// reads show spent and whose identity they show absent returns +/// [`ErrorShieldedIdentityCreateInputsConsumed`]: that path is one possible cause, the same notes +/// spent by another transition another, and the result does not say which. +/// +/// [`ErrorShieldedIdentityCreateInputsConsumed`]: crate::error::PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateInputsConsumed /// /// `mnemonic_resolver_handle` supplies the per-operation Orchard spend authority (see /// `platform_wallet_manager_shielded_transfer`). @@ -1115,6 +1120,10 @@ pub unsafe extern "C" fn platform_wallet_manager_shielded_identity_create_from_p PlatformWalletFFIResultCode::ErrorShieldedNoRecordedAnchor, format!("Wallet is still syncing to a confirmed state — try again shortly. ({e})"), ), + // Proven unable to execute: its notes are spent and its identity is absent. The notes + // were marked spent wallet-side; `out_identity_id` is not written, as the identity does + // not exist. + Err(e @ PlatformWalletError::ShieldedIdentityCreateInputsConsumed { .. }) => e.into(), // Definitive failure: the transition was not executed and the spent notes were released. Err(e @ PlatformWalletError::ShieldedBroadcastFailed(_)) => PlatformWalletFFIResult::err( PlatformWalletFFIResultCode::ErrorShieldedBroadcastFailed, @@ -2487,6 +2496,8 @@ mod tests { .arm_redrive( SubwalletId::new([7; 32], 9), PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [8; 32], anchor: [9; 32], nullifiers: vec![[10; 32]], diff --git a/packages/rs-platform-wallet/README.md b/packages/rs-platform-wallet/README.md index 7e38a91f9f4..8a70a0ac6d9 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -145,7 +145,7 @@ A build without the `shielded` feature keeps no shielded store, journal or resto ### Shielded spends whose broadcast may have been delivered -Every shielded spend that consumes notes (shielded transfers, unshields, withdrawals, identity top-ups and creations from the pool, and private document creations) broadcasts in single dispatches and keeps what each one proved: the SDK's own retry loop returns only its last error, which cannot show that an earlier attempt was admitted with its response lost. Any dispatch that reached an endpoint, whatever it answered (a consensus verdict included, which is unauthenticated), or failed in a way that allows delivery, any transport error whatever its gRPC code, makes the whole broadcast possibly delivered. Such a spend waits for its result, and reports `ShieldedSpendUnconfirmed` (FFI code 18) when that wait fails or answers with a verdict, its notes and redrive record kept so the redrive pass resolves it: a host must not pay again with other notes. An identity creation reports `ShieldedBroadcastUnconfirmed` (FFI code 17) instead, carrying the identity's derived id. Earlier versions reported these verdicts and transport failures as `ShieldedBroadcastFailed` (FFI code 16). Only a spend refused before anything was sent reports `ShieldedBroadcastFailed` and releases its notes at once. The dispatches stay within the SDK's configured retry budget: one dispatch and its retries. A re-broadcast refused by a node does not prove that no other node holds the original, so the redrive pass keeps such a spend's notes reserved until it lands or its anchor is pruned. A shield from Platform addresses broadcasts the same way and reports `ShieldedSpendUnconfirmed` (FFI code 18), not `AddressNonceMismatch` (FFI code 21) or `ShieldedBroadcastFailed`, once a dispatch may have reached a node and its result does not read its inputs spent; it consumes no notes and keeps no record of its signed transition, so its activity row stays pending until the scan sees its note: a retry would sign a second shield with the inputs' next nonces. Only a shield from an identity keeps its own handling (see "Identity-funded shield recovery" above). +Every shielded spend that consumes notes (shielded transfers, unshields, withdrawals, identity top-ups and creations from the pool, and private document creations) broadcasts in single dispatches and keeps what each one proved: the SDK's own retry loop returns only its last error, which cannot show that an earlier attempt was admitted with its response lost. Any dispatch that reached an endpoint, whatever it answered (a consensus verdict included, which is unauthenticated), or failed in a way that allows delivery, any transport error whatever its gRPC code, makes the whole broadcast possibly delivered. Such a spend waits for its result, and reports `ShieldedSpendUnconfirmed` (FFI code 18) when that wait fails or answers with a verdict, its notes and redrive record kept so the redrive pass resolves it: a host must not pay again with other notes. An identity creation reports `ShieldedBroadcastUnconfirmed` (FFI code 17) instead, carrying the identity's derived id. An identity creation keeps the node one of its dispatches reached (each dispatch goes to one node, through an SDK scoped to it with `Sdk::scoped_to_address`, so the shared address list is not changed) and asks only that node about it afterwards: the result wait, the identity read and, when the identity is not found, the proven outcome reads (`Sdk::state_transition_outcome`, then the identity), as the outcome API's privacy note asks. Every nullifier spent and the identity absent at or above the height they were spent by reports `ShieldedIdentityCreateInputsConsumed` (FFI code 77): it can never execute and did not create its identity, its notes are marked spent and its activity row failed at that height. The reads do not say what spent the notes; Platform's paid failure path for a Type 20 is one possible cause, the same notes spent by another transition another. An identity creation's redrive record is held for that read from the moment it is saved, before any dispatch, and the node is kept with it once one is known: a scan that sees its notes spent keeps the record and never confirms its row from the outputs it sees. The sync pass reads the outcome at the node and keeps a proven one with the record (created when the identity is found, inputs consumed when it is proven absent); it then writes the row with that outcome into its own changeset after the scan's rows, overwriting a row the scan confirmed, and drops the record only once the host holds that changeset durably (a store that commits inline, or a flush that succeeded). Until then, across a restart too, the next pass writes the row again from the kept outcome. A creation with no node kept is never read about, and one whose reads prove nothing is not read again once its attempts, re-broadcasts included, pass `MAX_REDRIVE_ATTEMPTS` (one whose re-broadcasts spent them still gets one read): either stays held and unconfirmed. Re-broadcasts of a held creation go to its node only. Earlier versions reported these verdicts and transport failures as `ShieldedBroadcastFailed` (FFI code 16). Only a spend refused before anything was sent reports `ShieldedBroadcastFailed` and releases its notes at once. The dispatches stay within the SDK's configured retry budget: one dispatch and its retries. A re-broadcast refused by a node does not prove that no other node holds the original, so the redrive pass keeps such a spend's notes reserved until it lands or its anchor is pruned. A shield from Platform addresses broadcasts the same way and reports `ShieldedSpendUnconfirmed` (FFI code 18), not `AddressNonceMismatch` (FFI code 21) or `ShieldedBroadcastFailed`, once a dispatch may have reached a node and its result does not read its inputs spent; it consumes no notes and keeps no record of its signed transition, so its activity row stays pending until the scan sees its note: a retry would sign a second shield with the inputs' next nonces. Only a shield from an identity keeps its own handling (see "Identity-funded shield recovery" above). ### Public contact requests whose broadcast may have been delivered diff --git a/packages/rs-platform-wallet/src/broadcast_outcome.rs b/packages/rs-platform-wallet/src/broadcast_outcome.rs index 96b840e8742..9c521697d5d 100644 --- a/packages/rs-platform-wallet/src/broadcast_outcome.rs +++ b/packages/rs-platform-wallet/src/broadcast_outcome.rs @@ -198,6 +198,80 @@ pub(crate) async fn broadcast_tracking_delivery( .await } +/// [`broadcast_tracking_delivery`], with each dispatch sent to one node the +/// SDK's address list selects, through an SDK scoped to it +/// ([`dash_sdk::Sdk::scoped_to_address`]), so the node a dispatch reached is +/// known: the one that admitted it, or else the last one a dispatch may have +/// been delivered to. That node holds the transition, so it alone may be +/// asked about it afterwards (see the privacy note of +/// `Sdk::state_transition_outcome`). The shared SDK's address list is not +/// changed. With no live address to select, a dispatch goes through the +/// shared SDK, which reports why, and no node is returned; none is for a +/// broadcast never delivered either. +pub(crate) async fn broadcast_tracking_endpoint( + sdk: &dash_sdk::Sdk, + state_transition: &dpp::state_transition::StateTransition, +) -> (BroadcastDelivery, Option) { + use dash_sdk::dapi_client::RequestSettings; + use dash_sdk::platform::transition::broadcast::BroadcastStateTransition; + use dash_sdk::platform::transition::put_settings::PutSettings; + + let single_dispatch = PutSettings { + request_settings: RequestSettings { + retries: Some(0), + ..RequestSettings::default() + }, + ..PutSettings::default() + }; + let reached: std::sync::Mutex> = std::sync::Mutex::new(None); + let tried: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + let (reached_ref, tried_ref) = (&reached, &tried); + let delivery = track_delivery( + state_transition, + spend_broadcast_dispatches(sdk), + || async move { + // A later dispatch goes to a node no earlier one went to, while + // there is one: a node that failed is banned only in its scoped + // copy, so the shared rotation could select it again. + let address = { + let mut tried = tried_ref.lock().unwrap_or_else(|e| e.into_inner()); + let address = if tried.is_empty() { + sdk.address_list().get_live_address() + } else { + sdk.address_list() + .get_live_addresses() + .into_iter() + .find(|address| !tried.contains(address)) + .or_else(|| sdk.address_list().get_live_address()) + }; + if let Some(address) = &address { + tried.push(address.clone()); + } + address + }; + let Some(address) = address else { + return state_transition.broadcast(sdk, Some(single_dispatch)).await; + }; + let scoped = sdk.scoped_to_address(address.clone()); + let result = state_transition + .broadcast(&scoped, Some(single_dispatch)) + .await; + if !matches!(&result, Err(error) if refused_before_sending(error)) { + if let Ok(mut reached) = reached_ref.lock() { + *reached = Some(address); + } + } + result + }, + ) + .await; + let endpoint = match delivery { + BroadcastDelivery::NeverDelivered(_) => None, + _ => reached.into_inner().ok().flatten(), + }; + (delivery, endpoint) +} + /// The classification [`broadcast_tracking_delivery`] makes of /// `state_transition`'s broadcast, over the outcomes `dispatch` returns, one /// call per single dispatch, at most `dispatches` of them. @@ -210,8 +284,6 @@ where F: FnMut() -> Fut, Fut: std::future::Future>, { - use dash_sdk::dapi_client::DapiClientError; - let mut maybe_delivered: Option = None; for _ in 0..dispatches.max(1) { let error = match dispatch().await { @@ -219,21 +291,7 @@ where Err(error) if node_holds_transition(&error) => return BroadcastDelivery::Admitted, Err(error) => error, }; - // Refused before anything was sent: no address, or the SDK's own - // refusal of a kind the network does not serve yet. - let never_sent = matches!( - error, - dash_sdk::Error::DapiClientError(DapiClientError::NoAvailableAddresses) - | dash_sdk::Error::ProtocolVersionNotActive { .. } - ) || matches!( - &error, - dash_sdk::Error::NoAvailableAddressesToRetry(inner) - if matches!( - **inner, - dash_sdk::Error::DapiClientError(DapiClientError::NoAvailableAddresses) - ) - ); - if never_sent { + if refused_before_sending(&error) { return match maybe_delivered { Some(_) => BroadcastDelivery::MaybeDelivered(error), None => BroadcastDelivery::NeverDelivered(error), @@ -257,6 +315,25 @@ where } } +/// Whether `error` refused a dispatch before anything was sent: no address, +/// or the SDK's own refusal of a kind the network does not serve yet. +pub(crate) fn refused_before_sending(error: &dash_sdk::Error) -> bool { + use dash_sdk::dapi_client::DapiClientError; + + matches!( + error, + dash_sdk::Error::DapiClientError(DapiClientError::NoAvailableAddresses) + | dash_sdk::Error::ProtocolVersionNotActive { .. } + ) || matches!( + error, + dash_sdk::Error::NoAvailableAddressesToRetry(inner) + if matches!( + **inner, + dash_sdk::Error::DapiClientError(DapiClientError::NoAvailableAddresses) + ) + ) +} + /// Whether `error` says a node already holds the transition /// (`AlreadyExists`): bare, or under the SDK's unconfirmed-outcome envelope /// (`StateTransitionResultUnconfirmed`), which the SDK puts a duplicate @@ -680,6 +757,36 @@ mod tests { ); } + /// A broadcast that tracks its node keeps the node a dispatch may have + /// reached, and the shared SDK's list stays as it was; one never + /// delivered keeps none. + #[tokio::test] + async fn should_keep_the_node_a_dispatch_may_have_reached() { + let transition = withdrawal(); + + let (delivery, endpoint) = + broadcast_tracking_endpoint(&test_sdks::sdk_without_addresses(), &transition).await; + assert!( + matches!(delivery, BroadcastDelivery::NeverDelivered(_)), + "{delivery:?}" + ); + assert!(endpoint.is_none()); + + let (refusing, _port) = test_sdks::sdk_refusing_connections(); + let node = refusing.address_list().get_live_addresses(); + let (delivery, endpoint) = broadcast_tracking_endpoint(&refusing, &transition).await; + assert!( + matches!(delivery, BroadcastDelivery::MaybeDelivered(_)), + "{delivery:?}" + ); + assert_eq!(endpoint.as_ref(), node.first()); + assert_eq!( + refusing.address_list().get_live_addresses(), + node, + "the shared list neither loses nor bans the node" + ); + } + /// A node that reads an input address's nonce as ahead of its own state /// is behind the node the nonce was read from: the same signed /// transition goes to the next dispatch's node, as the SDK's own diff --git a/packages/rs-platform-wallet/src/error.rs b/packages/rs-platform-wallet/src/error.rs index f9ed4a5b60f..7b9b523ac22 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -1113,6 +1113,31 @@ pub enum PlatformWalletError { reason: String, }, + /// The shielded identity create (Type 20) did not create its identity and can never execute, + /// as proven reads show: every nullifier it spends is spent at or before + /// `consumed_by_height`, and the identity `identity_id` is absent at a height at or above it. + /// Its notes are gone, so the wallet marks them spent and marks its activity row failed; the + /// create's recovery record keeps the outcome until the sync pass has written that row + /// durably. Nothing is to be re-submitted. + /// + /// The reads show that the inputs are taken, not what took them. One possible cause is + /// Platform's paid failure path for this transition (a key-state check failing in the block), + /// which credits the denomination minus a penalty to the transition's + /// `send_to_address_on_creation_failure`; another is the same notes spent by another + /// transition (another device holding this wallet's keys), which credits nothing there. This + /// error does not say which. `reason` carries the result-wait failure that led to the reads, + /// for diagnostics; it is not part of the proof. + #[error( + "Shielded identity create {identity_id} did not create its identity and can no longer \ + execute: its notes are spent (by height {consumed_by_height}) and the identity does not \ + exist: {reason}" + )] + ShieldedIdentityCreateInputsConsumed { + identity_id: Identifier, + consumed_by_height: u64, + reason: String, + }, + /// A previous identity-funded shield is unresolved. This new call did not /// build or broadcast a transaction; wait for the original payment's sync. #[error("Identity {} has an unresolved shielded debit; this request was not started. Wait for shielded sync", hex::encode(identity_id))] diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 002186924f9..ccdc7e029a1 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -2115,6 +2115,8 @@ mod private_payment_hold_tests { .arm_private_contact_payment( SubwalletId::new(bound, 0), PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x21; 32], anchor: [0x22; 32], nullifiers: vec![[0x23; 32]], diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index 0797bb46ca1..ca5f5797bcf 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -3500,6 +3500,8 @@ mod private_contact_request_redrive_tests { /// The unconfirmed spend that broadcast `st_bytes`. fn redrive(st_bytes: Vec) -> PendingRedrive { PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [1; 32], anchor: [7; 32], nullifiers: vec![[1; 32]], diff --git a/packages/rs-platform-wallet/src/wallet/shielded/activity.rs b/packages/rs-platform-wallet/src/wallet/shielded/activity.rs index e0e8f3438c2..423da8eeee8 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/activity.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/activity.rs @@ -71,6 +71,12 @@ pub enum ShieldedActivityStatus { /// The user stopped automatic recovery without proof of success or failure. /// A later scan may still confirm the original payment. Unknown, + /// Proven by reads that it can never execute and that its inputs are + /// spent: written for an identity create from the pool (Type 20) whose + /// nullifiers are all spent and whose identity is absent at or above the + /// height they were spent by. Its paid failure path and the same notes + /// spent by another transition read the same. + InputsConsumed, } /// The classified kind of a shielded operation. 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..ba29d46a7c4 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs @@ -358,6 +358,8 @@ async fn local_balance_restores_funds_and_durable_pending_reservations_offline() .arm_redrive( id, super::store::PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [9; 32], anchor: [8; 32], nullifiers: vec![[2; 32]], diff --git a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs index 3720a2deefe..0ad9cad813a 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -1546,9 +1546,11 @@ impl NetworkShieldedCoordinator { if cancel.is_cancelled() { break; } + let persister = self.persisters.read().await.get(&id.wallet_id).cloned(); super::operations::redrive_pending_spends( &self.sdk, &self.store, + persister.as_ref(), *id, &recorded, cancel, @@ -1596,6 +1598,26 @@ impl NetworkShieldedCoordinator { tracing::warn!(error = %e, "Shielded activity derivation failed; skipping this pass"); } + // Identity creates held for their outcome read with no node kept for them: scheduled + // from the record journal, whatever reservations are left. + if !cancelled { + self.redrive_unkept_identity_creates(&subwallets, cancel) + .await; + } + + // Identity creates held for their outcome read: the scan kept their + // records and did not confirm their rows, as their notes spent do not + // show whether the identity was created. Each whose notes this scan + // (or an earlier one) saw spent is read at the node it was delivered + // to, and each proven outcome is written onto this pass's changeset + // after the scan's rows, so the host keeps it. + let settled_identity_creates = if cancelled { + Vec::new() + } else { + self.settle_held_identity_creates(&subwallets, &mut notes.changeset, cancel) + .await + }; + // Identity-funded shields have no input nullifiers and use an // outputs-only empty-tree anchor, so the ordinary stale-reservation // pass can never schedule them. After activity derivation has first @@ -1620,34 +1642,10 @@ impl NetworkShieldedCoordinator { } // The note-side changeset already carries saves, synced - // indices, AND the scan-detected spends, so split it per - // WalletId directly — each per-wallet `WalletPersister.store` - // only sees its own wallet's deltas. - let consolidated = notes.changeset.clone(); - if !Merge::is_empty(&consolidated) { - let per_wallet = consolidated.split_by_wallet_id(); - let persisters = self.persisters.read().await; - for (wallet_id, cs) in per_wallet { - let Some(persister) = persisters.get(&wallet_id) else { - tracing::warn!( - wallet_id = %hex::encode(wallet_id), - "Shielded sync changeset dropped: no persister registered (host inconsistency)" - ); - continue; - }; - let full = crate::changeset::PlatformWalletChangeSet { - shielded: Some(cs), - ..Default::default() - }; - if let Err(e) = persister.store(full) { - tracing::warn!( - wallet_id = %hex::encode(wallet_id), - error = %e, - "Failed to queue shielded changeset from coordinator" - ); - } - } - } + // indices, AND the scan-detected spends, so it goes to each + // wallet's persister as it is. + self.persist_pass_changeset(¬es.changeset, settled_identity_creates) + .await; // Cooldown decision based on aggregate activity across // every subwallet — any new commitment scanned or @@ -1916,6 +1914,35 @@ impl NetworkShieldedCoordinator { continue; } }; + // An identity create whose outcome is proven keeps its notes reserved and its + // record until the sync pass replays its spent notes and row and the host + // holds them: releasing them here would hand back spent notes. + let proven_held = match store.pending_redrives(id) { + Ok(records) => records.iter().any(|record| { + record + .identity_outcome + .is_some_and(super::store::IdentityCreateOutcome::is_proven) + && record.nullifiers.contains(&nullifier) + }), + Err(e) => { + tracing::warn!( + wallet_id = %hex::encode(id.wallet_id), + account = id.account_index, + error = %e, + "shielded reservation release: pending_redrives failed; skipping entry" + ); + continue; + } + }; + if proven_held { + tracing::debug!( + wallet_id = %hex::encode(id.wallet_id), + account = id.account_index, + nullifier = %hex::encode(nullifier), + "shielded reservation release: a proven identity create holds it; kept" + ); + continue; + } if !still_same { // Resolved (landed and reconciled, result-wait // confirmed) or re-armed by a retry while this pass @@ -2345,7 +2372,26 @@ impl NetworkShieldedCoordinator { .flat_map(|entry| entry.note_cmxs.into_iter().map(move |c| (c, entry.id))) .collect(); let confirmations = derived.confirmations; + // Identity creates held for their outcome read: their outputs seen + // on chain do not show whether the identity was created, so their + // rows and records wait for that read. + let held: HashSet<[u8; 32]> = redrives + .iter() + .filter(|redrive| redrive.held_for_identity_outcome()) + .map(|redrive| redrive.activity_id) + .collect(); for entry in derived.new_entries { + // A held create's row is rebuilt unconfirmed: its outputs seen + // on chain do not show that its identity was created. + let entry = if held.contains(&entry.id) { + super::activity_recorder::with_status( + &entry, + super::activity::ShieldedActivityStatus::Pending, + None, + ) + } else { + entry + }; let overlapped: std::collections::BTreeSet<[u8; 32]> = entry .note_cmxs .iter() @@ -2373,7 +2419,9 @@ impl NetworkShieldedCoordinator { })? .into_iter() .filter_map(|(owner, redrive)| { - (!redrive.nullifiers.is_empty() && redrive.activity_id == entry.id) + (!redrive.nullifiers.is_empty() + && !redrive.held_for_identity_outcome() + && redrive.activity_id == entry.id) .then_some(owner) }) .collect(); @@ -2402,6 +2450,9 @@ impl NetworkShieldedCoordinator { crate::error::PlatformWalletError::ShieldedStoreError(e.to_string()) })?; let Some(stored) = stored else { continue }; + if held.contains(&entry_id) { + continue; + } // Chain truth wins: a row marked Failed by a client-side // post-broadcast error whose outputs are later observed // on-chain was not actually a failure — upgrade it. (We @@ -2464,6 +2515,138 @@ impl NetworkShieldedCoordinator { Ok(()) } + /// Queue this pass's `changeset` on each wallet's persister, split per + /// `WalletId` directly — each per-wallet `WalletPersister.store` only + /// sees its own wallet's deltas — then drop the records of the + /// `settled_identity_creates` whose rows it carries once their wallet's + /// store is durable. + async fn persist_pass_changeset( + &self, + changeset: &crate::changeset::ShieldedChangeSet, + settled_identity_creates: Vec<(SubwalletId, Vec<[u8; 32]>)>, + ) { + // A wallet with settled identity creates is flushed after its store: + // their records go only once the host holds their rows durably. + let settling: HashSet = settled_identity_creates + .iter() + .map(|(id, _)| id.wallet_id) + .collect(); + let mut held_durably: BTreeMap = BTreeMap::new(); + if !Merge::is_empty(changeset) { + let per_wallet = changeset.clone().split_by_wallet_id(); + let persisters = self.persisters.read().await; + for (wallet_id, cs) in per_wallet { + let Some(persister) = persisters.get(&wallet_id) else { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + "Shielded sync changeset dropped: no persister registered (host inconsistency)" + ); + held_durably.insert(wallet_id, false); + continue; + }; + let full = crate::changeset::PlatformWalletChangeSet { + shielded: Some(cs), + ..Default::default() + }; + let stored = match persister.store(full) { + Ok(()) => true, + Err(e) => { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "Failed to queue shielded changeset from coordinator" + ); + false + } + }; + let durable = stored + && (!settling.contains(&wallet_id) + || persister.store_commits_inline() + || match persister.flush() { + Ok(()) => true, + Err(e) => { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "Shielded changeset not flushed; settled identity creates \ + keep their records for the next pass" + ); + false + } + }); + held_durably.insert(wallet_id, durable); + } + } + for (id, activity_ids) in settled_identity_creates { + // A settled create's row and spent notes are on the changeset: its + // record goes only once its wallet's store of it is durable. + if held_durably.get(&id.wallet_id) != Some(&true) { + continue; + } + super::operations::retire_settled_identity_creates(&self.store, id, &activity_ids) + .await; + } + } + + /// Re-broadcast each held identity create of `subwallets` that has no node kept for it to one + /// node, kept for its outcome read ([`super::operations::redrive_unkept_identity_creates`]). + /// Scheduled from the record journal on every completed pass, not from reservations or + /// recorded anchors: a scan that saw a create's notes spent cleared its reservations but kept + /// its record. + async fn redrive_unkept_identity_creates( + &self, + subwallets: &[(SubwalletId, AccountViewingKeys)], + cancel: &CancellationToken, + ) { + for (id, _) in subwallets { + if cancel.is_cancelled() { + break; + } + let persister = self.persisters.read().await.get(&id.wallet_id).cloned(); + super::operations::redrive_unkept_identity_creates( + &self.sdk, + &self.store, + persister.as_ref(), + *id, + cancel, + ) + .await; + } + } + + /// Settle the identity creates of `subwallets` held for their outcome + /// read onto `changeset` + /// ([`super::operations::settle_held_identity_creates`]), each with its + /// wallet's persister. Returns, per subwallet, the activity ids whose + /// records go once `changeset` is durable. + async fn settle_held_identity_creates( + &self, + subwallets: &[(SubwalletId, AccountViewingKeys)], + changeset: &mut crate::changeset::ShieldedChangeSet, + cancel: &CancellationToken, + ) -> Vec<(SubwalletId, Vec<[u8; 32]>)> { + let mut settled = Vec::new(); + for (id, _) in subwallets { + if cancel.is_cancelled() { + break; + } + let persister = self.persisters.read().await.get(&id.wallet_id).cloned(); + let activity_ids = super::operations::settle_held_identity_creates( + &self.sdk, + &self.store, + persister.as_ref(), + *id, + changeset, + cancel, + ) + .await; + if !activity_ids.is_empty() { + settled.push((*id, activity_ids)); + } + } + settled + } + /// Build a pass summary in which every registered wallet is /// reported as a cooldown skip. Used by [`sync`](Self::sync) /// when the network-wide cooldown is in effect. @@ -2828,7 +3011,8 @@ mod tests { use crate::changeset::ShieldedChangeSet; use crate::wallet::shielded::activity::compute_activity_id; use crate::wallet::shielded::store::{ - InMemoryShieldedStore, PendingRedrive, PrivateContactPayment, ShieldedNote, + IdentityCreateOutcome, InMemoryShieldedStore, PendingRedrive, PrivateContactPayment, + ShieldedNote, }; use dpp::platform_value::BinaryData; use dpp::serialization::PlatformSerializable; @@ -2925,6 +3109,8 @@ mod tests { .into(); ( PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: compute_activity_id(&cmxs), anchor: [1; 32], nullifiers: vec![], @@ -3355,116 +3541,1149 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } - /// Unique temp directory for a test's SQLite tree (no `tempfile` dev-dep). - fn temp_dir(tag: &str) -> PathBuf { - let nanos = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) + // ── Identity creates held for their outcome read, across scan passes ── + // + // The scan itself reads Platform; these tests stand in for it with the + // store writes it makes (`mark_spent` for the input it sees spent, the + // output note it receives), then run the pass's own post-scan steps in + // its order: `derive_activity_into_changeset`, then + // `settle_held_identity_creates`, then `persist_pass_changeset`. + + /// The node the held identity creates are delivered to. + const HELD_ENDPOINT: &str = "http://127.0.0.1:3011"; + /// The identity create's identity, real input, output and activity row. + const CREATE_IDENTITY: [u8; 32] = [0x44; 32]; + const CREATE_INPUT: [u8; 32] = [0x51; 32]; + const CREATE_OUTPUT: [u8; 32] = [0x52; 32]; + const CREATE_ROW: [u8; 32] = [0x53; 32]; + const CREATE_ANCHOR: [u8; 32] = [0x54; 32]; + + /// The identity create spending the wallet's note `CREATE_INPUT`, padded + /// with an explicit dummy spend to two actions. + fn held_identity_create_transition() -> StateTransition { + use dpp::address_funds::PlatformAddress; + use dpp::state_transition::state_transitions::shielded::identity_create_from_shielded_pool_transition::v0::IdentityCreateFromShieldedPoolTransitionV0; + + let action = |nullifier, cmx| SerializedAction { + nullifier, + rk: [2; 32], + cmx, + encrypted_note: vec![4; 216], + cv_net: [5; 32], + spend_auth_sig: [6; 64], + }; + IdentityCreateFromShieldedPoolTransitionV0 { + public_keys: vec![], + denomination: 1_000_000, + actions: vec![ + action(CREATE_INPUT, CREATE_OUTPUT), + action([0xD1; 32], [0xD2; 32]), + ], + anchor: CREATE_ANCHOR, + proof: vec![8; 64], + binding_signature: [9; 64], + send_to_address_on_creation_failure: PlatformAddress::P2pkh([7; 20]), + identity_id: CREATE_IDENTITY.into(), + } + .into() + } + + /// A mock SDK whose address list holds `HELD_ENDPOINT`, and whose proven + /// reads show the create's nullifiers spent and its identity absent, or + /// `created`. Every answer of a mock SDK is at height 0. + async fn sdk_reading_held_create(created: bool) -> dash_sdk::Sdk { + use dash_sdk::query_types::{ + MostRecentShieldedAnchor, NoParamQuery, ShieldedAnchors, ShieldedNullifierStatus, + ShieldedNullifierStatuses, ShieldedNullifiersQuery, + }; + use dpp::identity::Identity; + + let mut sdk = dash_sdk::SdkBuilder::new_mock() + .with_version(dpp::version::PlatformVersion::latest()) + .build() + .expect("mock sdk"); + let mut addresses = sdk.address_list().clone(); + addresses.add(HELD_ENDPOINT.parse().expect("an address")); + sdk.mock() + .expect_fetch(NoParamQuery {}, Some(ShieldedAnchors(vec![CREATE_ANCHOR]))) + .await + .unwrap(); + sdk.mock() + .expect_fetch( + NoParamQuery {}, + Some(MostRecentShieldedAnchor(CREATE_ANCHOR)), + ) + .await + .unwrap(); + let nullifiers = vec![CREATE_INPUT, [0xD1; 32]]; + let statuses = nullifiers + .iter() + .map(|nullifier| ShieldedNullifierStatus { + nullifier: *nullifier, + is_spent: true, + }) + .collect(); + sdk.mock() + .expect_fetch( + ShieldedNullifiersQuery(nullifiers), + Some(ShieldedNullifierStatuses(statuses)), + ) + .await + .unwrap(); + let identity = created.then(|| { + Identity::new_with_id_and_keys( + CREATE_IDENTITY.into(), + BTreeMap::new(), + dpp::version::PlatformVersion::latest(), + ) .unwrap() - .as_nanos(); - std::env::temp_dir().join(format!("shielded_coordinator_test_{tag}_{nanos}")) + }); + sdk.mock() + .expect_fetch(dpp::prelude::Identifier::from(CREATE_IDENTITY), identity) + .await + .unwrap(); + sdk } - /// Build a coordinator backed by a fresh file store under `dir`, with one - /// wallet registered so `accounts` / `persisters` are both non-empty. - async fn coordinator_with_one_wallet(dir: &std::path::Path) -> NetworkShieldedCoordinator { + /// [`coordinator_with_one_wallet`] on `sdk`, over the store under `dir` + /// (opened again when it exists). + async fn coordinator_on( + dir: &std::path::Path, + sdk: dash_sdk::Sdk, + ) -> NetworkShieldedCoordinator { + coordinator_on_host(dir, sdk, Arc::new(NoPlatformPersistence)).await + } + + /// [`coordinator_on`] with `host` as the wallet's persister. + async fn coordinator_on_host( + dir: &std::path::Path, + sdk: dash_sdk::Sdk, + host: Arc, + ) -> NetworkShieldedCoordinator { std::fs::create_dir_all(dir).expect("create temp dir"); let db_path = dir.join("tree.sqlite"); let store = FileBackedShieldedStore::open_path(&db_path, 100).expect("open file store"); let coordinator = NetworkShieldedCoordinator::new( - Arc::new(dash_sdk::Sdk::new_mock()), + Arc::new(sdk), dashcore::Network::Testnet, db_path, store, ); - - let wallet_id: WalletId = [0x11; 32]; let views = OrchardKeySet::from_seed(&[0x42u8; 64], dashcore::Network::Testnet, 0) .expect("derive viewing keys") .viewing_keys(); let mut account_views = BTreeMap::new(); account_views.insert(0u32, views); - let persister = WalletPersister::new(wallet_id, Arc::new(NoPlatformPersistence)); + let persister = WalletPersister::new([0x11; 32], host); coordinator - .register_wallet(wallet_id, account_views, persister) + .register_wallet([0x11; 32], account_views, persister) .await .expect("register wallet"); coordinator } - /// Success path: a healthy `clear()` empties the shared commitment tree - /// AND drops the in-memory account / persister registries, returning `Ok`. - #[tokio::test] - async fn clear_success_empties_tree_and_registries() { - let dir = temp_dir("clear_ok"); - let coordinator = coordinator_with_one_wallet(&dir).await; + /// The identity create, delivered and unconfirmed: its note, its + /// pending activity row and its recovery record, held for its outcome + /// read at `HELD_ENDPOINT` when `held`. + async fn arm_identity_create( + coordinator: &NetworkShieldedCoordinator, + id: SubwalletId, + held: bool, + ) { + arm_identity_create_row_and_input(coordinator, id).await; + let mut store = coordinator.store().write().await; + store + .arm_redrive( + id, + PendingRedrive { + activity_id: CREATE_ROW, + anchor: CREATE_ANCHOR, + nullifiers: vec![CREATE_INPUT], + st_bytes: held_identity_create_transition() + .serialize_to_bytes() + .unwrap(), + attempts: 0, + anchor_height: Some(5), + identity_nonce_finalized: false, + identity_user_abandoned: false, + outcome_endpoint: held.then(|| HELD_ENDPOINT.to_string()), + identity_outcome: held.then_some(IdentityCreateOutcome::Awaiting), + }, + ) + .unwrap(); + } - // Put a leaf in the tree so the reset is observable. - { - let mut store = coordinator.store().write().await; - store.append_commitment(&[7u8; 32], true).unwrap(); - assert_eq!(store.tree_size().unwrap(), 1); - } - assert!(!coordinator.accounts.read().await.is_empty()); - assert!(!coordinator.persisters.read().await.is_empty()); + /// The create's input note and its pending activity row. + async fn arm_identity_create_row_and_input( + coordinator: &NetworkShieldedCoordinator, + id: SubwalletId, + ) { + use crate::wallet::shielded::activity::{ + ShieldedActivityEntry, ShieldedActivityKind, ShieldedActivityStatus, ShieldedDirection, + }; - coordinator.clear().await.expect("clear should succeed"); + let mut store = coordinator.store().write().await; + let mut input = test_note(CREATE_INPUT, 0, false); + input.cmx = [0x56; 32]; + store.save_note(id, &input).unwrap(); + store + .save_activity( + id, + &ShieldedActivityEntry { + id: CREATE_ROW, + kind: ShieldedActivityKind::ShieldedSpend, + direction: ShieldedDirection::Out, + amount: 1_000_000, + fee: Some(0), + counterparty: None, + memo: None, + block_height: None, + status: ShieldedActivityStatus::Pending, + created_at_ms: 1, + min_note_position: None, + note_cmxs: vec![CREATE_OUTPUT], + spent_nullifiers: vec![CREATE_INPUT], + }, + ) + .unwrap(); + } - assert_eq!( - coordinator.store().read().await.tree_size().unwrap(), - 0, - "tree must be empty after a successful clear" - ); - assert!(coordinator.accounts.read().await.is_empty()); - assert!(coordinator.persisters.read().await.is_empty()); + /// What the host's snapshot restores after a restart for the create the + /// scan saw: its input spent, its output, its pending row. + async fn restore_host_snapshot(coordinator: &NetworkShieldedCoordinator, id: SubwalletId) { + arm_identity_create_row_and_input(coordinator, id).await; + scan_sees_identity_create(coordinator, id, 40).await; + } - let _ = std::fs::remove_dir_all(&dir); + /// What a scan that reaches the create's block writes: its input seen + /// spent, its output received at `height`. + async fn scan_sees_identity_create( + coordinator: &NetworkShieldedCoordinator, + id: SubwalletId, + height: u64, + ) { + let mut store = coordinator.store().write().await; + store.mark_spent(id, &CREATE_INPUT).unwrap(); + let mut output = test_note([0x55; 32], 1, false); + output.cmx = CREATE_OUTPUT; + output.block_height = height; + store.save_note(id, &output).unwrap(); } - /// ON-DISK durability of `clear()` — the regression guard for the - /// "Clear resets the in-memory tree but not the persisted SQLite" bug. - /// - /// The prior test asserts `tree_size() == 0` on the SAME store handle, - /// which passes even if `clear()` only reset an in-memory shardtree / - /// frontier and left the on-disk `commitment_tree_*` rows intact. This - /// test instead reopens the persisted file through a COMPLETELY FRESH - /// `FileBackedShieldedStore` (independent connection, cold frontier) after - /// `clear()` and asserts both the tree AND the per-subwallet watermark - /// read back empty — proving the reset reached disk. Without the on-disk - /// reset + WAL checkpoint, this fresh handle would reload the full tree - /// (the on-device "tap Clear, relaunch, still 867/867" symptom). - #[tokio::test] - async fn clear_resets_the_persisted_on_disk_store_not_just_memory() { - let dir = temp_dir("clear_ondisk"); - let db_path = dir.join("tree.sqlite"); - let coordinator = coordinator_with_one_wallet(&dir).await; - let wallet_id: WalletId = [0x11; 32]; - let id = SubwalletId::new(wallet_id, 0); + /// The pass's post-scan steps, in its order. + async fn post_scan_steps(coordinator: &NetworkShieldedCoordinator, id: SubwalletId) { + let keys = coordinator.accounts.read().await[&id].clone(); + let subwallets = vec![(id, keys)]; + let mut changeset = ShieldedChangeSet::default(); + coordinator + .derive_activity_into_changeset(&subwallets, &mut changeset) + .await + .unwrap(); + coordinator + .redrive_unkept_identity_creates(&subwallets, &CancellationToken::new()) + .await; + let settled = coordinator + .settle_held_identity_creates(&subwallets, &mut changeset, &CancellationToken::new()) + .await; + coordinator + .persist_pass_changeset(&changeset, settled) + .await; + } - // Build a non-trivial tree AND advance the watermark, then checkpoint - // — the exact durable state a real sync leaves behind. - { - let mut store = coordinator.store().write().await; - for i in 0..8u8 { - store.append_commitment(&[i + 1; 32], true).unwrap(); - } - store.checkpoint_tree(8).unwrap(); - store.set_last_synced_note_index(id, 8).unwrap(); - assert_eq!(store.tree_size().unwrap(), 8); - assert_eq!(store.last_synced_note_index(id).unwrap(), 8); + /// How [`RecordingHost`] takes a store. + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + enum HostMode { + /// Each store is durable at once. + Inline, + /// Stores are buffered; a flush fails transiently and keeps them. + FlushFails, + /// Every store fails. + StoreFails, + } + + /// A host persister that keeps the activity rows it holds durably by id, + /// last write wins, as the Swift persistence callback does, and the + /// nullifiers of the notes it holds spent. + struct RecordingHost { + mode: std::sync::Mutex, + buffered: std::sync::Mutex>, + durable: std::sync::Mutex< + BTreeMap<[u8; 32], crate::wallet::shielded::activity::ShieldedActivityEntry>, + >, + buffered_spent: std::sync::Mutex>, + durable_spent: std::sync::Mutex>, + } + + impl RecordingHost { + fn new(mode: HostMode) -> Arc { + Arc::new(Self { + mode: std::sync::Mutex::new(mode), + buffered: Default::default(), + durable: Default::default(), + buffered_spent: Default::default(), + durable_spent: Default::default(), + }) } - coordinator.clear().await.expect("clear should succeed"); + fn mode(&self) -> HostMode { + *self.mode.lock().unwrap() + } - // Reopen the persisted file with a FRESH handle: this reads the - // on-disk tables cold (no shared in-memory frontier), so a non-zero - // size here would mean clear() never wrote the disk. - let reopened = FileBackedShieldedStore::open_path(&db_path, 100) - .expect("reopen persisted store after clear"); - assert_eq!( - reopened.tree_size().unwrap(), - 0, - "on-disk commitment tree must read 0 through a fresh handle after clear — \ - a non-zero size means clear() only reset the in-memory tree" + fn set_mode(&self, mode: HostMode) { + *self.mode.lock().unwrap() = mode; + } + + /// The row `id` the host holds durably. + fn durable_row( + &self, + id: &[u8; 32], + ) -> Option { + self.durable.lock().unwrap().get(id).cloned() + } + + /// Whether the host holds the note of `nullifier` spent durably. + fn holds_spent(&self, nullifier: &[u8; 32]) -> bool { + self.durable_spent.lock().unwrap().contains(nullifier) + } + + /// A process exit: what was not flushed is gone. + fn crash(&self) { + self.buffered.lock().unwrap().clear(); + self.buffered_spent.lock().unwrap().clear(); + } + } + + impl crate::changeset::PlatformWalletPersistence for RecordingHost { + fn store_commits_inline(&self) -> bool { + self.mode() == HostMode::Inline + } + + fn store( + &self, + _wallet_id: WalletId, + changeset: crate::changeset::PlatformWalletChangeSet, + ) -> Result<(), crate::changeset::PersistenceError> { + let shielded = changeset.shielded.unwrap_or_default(); + let spent: Vec<[u8; 32]> = shielded.nullifiers_spent.into_values().flatten().collect(); + let rows: Vec<_> = shielded.activity_entries.into_values().flatten().collect(); + match self.mode() { + HostMode::StoreFails => Err(crate::changeset::PersistenceError::backend_with_kind( + crate::changeset::PersistenceErrorKind::Fatal, + "the host refused the write", + )), + HostMode::Inline => { + let mut durable = self.durable.lock().unwrap(); + for row in rows { + durable.insert(row.id, row); + } + self.durable_spent.lock().unwrap().extend(spent); + Ok(()) + } + HostMode::FlushFails => { + self.buffered.lock().unwrap().extend(rows); + self.buffered_spent.lock().unwrap().extend(spent); + Ok(()) + } + } + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), crate::changeset::PersistenceError> { + if self.mode() == HostMode::FlushFails { + return Err(crate::changeset::PersistenceError::backend_with_kind( + crate::changeset::PersistenceErrorKind::Transient, + "simulated SQLITE_BUSY", + )); + } + let mut durable = self.durable.lock().unwrap(); + for row in self.buffered.lock().unwrap().drain(..) { + durable.insert(row.id, row); + } + self.durable_spent + .lock() + .unwrap() + .extend(self.buffered_spent.lock().unwrap().drain(..)); + Ok(()) + } + + fn load( + &self, + ) -> Result + { + Ok(Default::default()) + } + } + + /// The create's activity row: its status and height. + async fn create_row( + coordinator: &NetworkShieldedCoordinator, + id: SubwalletId, + ) -> ( + crate::wallet::shielded::activity::ShieldedActivityStatus, + Option, + ) { + let row = coordinator + .store() + .read() + .await + .get_activity_by_entry_id(id, &CREATE_ROW) + .unwrap() + .expect("the row"); + (row.status, row.block_height) + } + + /// Without a node to read its outcome at, the create is what the scan + /// makes of it, as before: its record goes with its notes and its + /// outputs seen confirm its row. This is the sequence the held creates + /// below are protected from. + #[tokio::test] + async fn should_leave_an_unheld_identity_create_to_the_scan() { + use crate::wallet::shielded::activity::ShieldedActivityStatus; + + let dir = temp_dir("unheld_create"); + let coordinator = coordinator_on(&dir, sdk_reading_held_create(false).await).await; + let id = SubwalletId::new([0x11; 32], 0); + arm_identity_create(&coordinator, id, false).await; + + scan_sees_identity_create(&coordinator, id, 40).await; + post_scan_steps(&coordinator, id).await; + + assert!(coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap() + .is_empty()); + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::Confirmed, Some(40)) + ); + let _ = std::fs::remove_dir_all(&dir); + } + + /// The scan reaches a held create before anything settles it: the scan + /// keeps its record and its pending row, the pass reads its outcome at + /// its node, and the proven outcome stands through later scans. + #[tokio::test] + async fn should_settle_a_held_identity_create_the_scan_saw_first() { + use crate::wallet::shielded::activity::ShieldedActivityStatus; + + let dir = temp_dir("held_scan_first"); + let coordinator = coordinator_on(&dir, sdk_reading_held_create(false).await).await; + let id = SubwalletId::new([0x11; 32], 0); + arm_identity_create(&coordinator, id, true).await; + + scan_sees_identity_create(&coordinator, id, 40).await; + let keys = coordinator.accounts.read().await[&id].clone(); + coordinator + .derive_activity_into_changeset(&[(id, keys)], &mut ShieldedChangeSet::default()) + .await + .unwrap(); + assert_eq!( + coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap() + .len(), + 1, + "the scan keeps the held record" + ); + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::Pending, None), + "and leaves its row to the outcome read" + ); + + post_scan_steps(&coordinator, id).await; + assert!(coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap() + .is_empty()); + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::InputsConsumed, Some(0)) + ); + // A later pass sees the same outputs. + scan_sees_identity_create(&coordinator, id, 41).await; + post_scan_steps(&coordinator, id).await; + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::InputsConsumed, Some(0)) + ); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A row the scan already confirmed with a height (as it did before the + /// create was held) gives way to the proven outcome. + #[tokio::test] + async fn should_let_a_proven_outcome_win_over_a_scan_confirmed_row() { + use crate::wallet::shielded::activity::ShieldedActivityStatus; + use crate::wallet::shielded::activity_recorder::with_status; + + let dir = temp_dir("held_confirmed_row"); + let coordinator = coordinator_on(&dir, sdk_reading_held_create(false).await).await; + let id = SubwalletId::new([0x11; 32], 0); + arm_identity_create(&coordinator, id, true).await; + { + let mut store = coordinator.store().write().await; + let row = store + .get_activity_by_entry_id(id, &CREATE_ROW) + .unwrap() + .unwrap(); + store + .save_activity( + id, + &with_status(&row, ShieldedActivityStatus::Confirmed, Some(40)), + ) + .unwrap(); + } + + scan_sees_identity_create(&coordinator, id, 40).await; + post_scan_steps(&coordinator, id).await; + + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::InputsConsumed, Some(0)) + ); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A create settled before the scan reaches it (the direct path proved + /// its notes spent and its identity absent): its record keeps the + /// outcome until the pass writes its row, and its row stays failed when + /// the scan later sees its outputs. + #[tokio::test] + async fn should_keep_a_settled_identity_create_failed_when_the_scan_sees_its_outputs() { + use crate::wallet::shielded::activity::ShieldedActivityStatus; + + let dir = temp_dir("held_settled_first"); + let coordinator = coordinator_on(&dir, sdk_reading_held_create(false).await).await; + let id = SubwalletId::new([0x11; 32], 0); + arm_identity_create(&coordinator, id, true).await; + + super::super::operations::settle_inputs_consumed_identity_create( + coordinator.store(), + None, + id.wallet_id, + id, + Some(&CREATE_ROW), + &[CREATE_INPUT], + 7, + ) + .await; + let records = coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap(); + assert_eq!( + records[0].identity_outcome, + Some(IdentityCreateOutcome::InputsConsumed { + consumed_by_height: 7 + }) + ); + + scan_sees_identity_create(&coordinator, id, 40).await; + post_scan_steps(&coordinator, id).await; + + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::InputsConsumed, Some(7)) + ); + assert!(coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap() + .is_empty()); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A held create the scan saw before a restart: the reopened store keeps + /// its record and node, and the next pass settles it, for good. A create + /// whose identity the node proves is confirmed instead. + #[tokio::test] + async fn should_settle_a_held_identity_create_after_a_restart() { + use crate::wallet::shielded::activity::ShieldedActivityStatus; + + for created in [false, true] { + let dir = temp_dir(&format!("held_restart_{created}")); + let id = SubwalletId::new([0x11; 32], 0); + { + let coordinator = coordinator_on(&dir, dash_sdk::Sdk::new_mock()).await; + arm_identity_create(&coordinator, id, true).await; + scan_sees_identity_create(&coordinator, id, 40).await; + let keys = coordinator.accounts.read().await[&id].clone(); + coordinator + .derive_activity_into_changeset( + &[(id, keys)], + &mut ShieldedChangeSet::default(), + ) + .await + .unwrap(); + } + + let coordinator = coordinator_on(&dir, sdk_reading_held_create(created).await).await; + // The host's snapshot restores the notes and the pending row (the + // store keeps neither across a restart); the spent input is + // replayed through `mark_spent`, as the restore does. + restore_host_snapshot(&coordinator, id).await; + let records = coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap(); + assert_eq!(records.len(), 1, "created {created}"); + assert_eq!(records[0].outcome_endpoint.as_deref(), Some(HELD_ENDPOINT)); + post_scan_steps(&coordinator, id).await; + let expected = if created { + ShieldedActivityStatus::Confirmed + } else { + ShieldedActivityStatus::InputsConsumed + }; + assert_eq!( + create_row(&coordinator, id).await.0, + expected, + "created {created}" + ); + let path = coordinator.db_path.clone(); + drop(coordinator); + assert!( + FileBackedShieldedStore::open_path(&path, 100) + .unwrap() + .pending_redrives(id) + .unwrap() + .is_empty(), + "created {created}: settled for good" + ); + let _ = std::fs::remove_dir_all(&dir); + } + } + + /// A held create whose live row is gone: the scan rebuilds its row from + /// its outputs, under the create's own activity id, unconfirmed, and the + /// proven outcome is written after it in the same pass. The host ends on + /// the proven outcome, and so does a later pass. + #[tokio::test] + async fn should_leave_the_host_on_the_proven_outcome_when_the_scan_rebuilds_the_row() { + use crate::wallet::shielded::activity::ShieldedActivityStatus; + + let dir = temp_dir("held_rebuilt_row"); + let host = RecordingHost::new(HostMode::Inline); + let coordinator = + coordinator_on_host(&dir, sdk_reading_held_create(false).await, host.clone()).await; + let id = SubwalletId::new([0x11; 32], 0); + let rebuilt_id = compute_activity_id(&[CREATE_OUTPUT]); + { + let mut store = coordinator.store().write().await; + let mut input = test_note(CREATE_INPUT, 0, false); + input.cmx = [0x56; 32]; + store.save_note(id, &input).unwrap(); + store + .arm_redrive( + id, + PendingRedrive { + activity_id: rebuilt_id, + anchor: CREATE_ANCHOR, + nullifiers: vec![CREATE_INPUT], + st_bytes: held_identity_create_transition() + .serialize_to_bytes() + .unwrap(), + attempts: 0, + anchor_height: Some(5), + identity_nonce_finalized: false, + identity_user_abandoned: false, + outcome_endpoint: Some(HELD_ENDPOINT.to_string()), + identity_outcome: Some(IdentityCreateOutcome::Awaiting), + }, + ) + .unwrap(); + } + + scan_sees_identity_create(&coordinator, id, 40).await; + post_scan_steps(&coordinator, id).await; + + let row = host + .durable_row(&rebuilt_id) + .expect("the host holds the row"); + assert_eq!( + (row.status, row.block_height), + (ShieldedActivityStatus::InputsConsumed, Some(0)) + ); + assert!(coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap() + .is_empty()); + scan_sees_identity_create(&coordinator, id, 41).await; + post_scan_steps(&coordinator, id).await; + assert_eq!( + host.durable_row(&rebuilt_id).unwrap().status, + ShieldedActivityStatus::InputsConsumed + ); + let _ = std::fs::remove_dir_all(&dir); + } + + /// The proven outcome's write does not reach the host durably: a flush + /// that fails, then a process exit before any later flush, or a store + /// the host refuses. The record keeps the outcome across the restart, + /// and the next pass writes the row from it, without reading the node + /// again, and only then drops the record. + #[tokio::test] + async fn should_keep_a_proven_outcome_until_the_host_holds_its_row() { + use crate::wallet::shielded::activity::ShieldedActivityStatus; + + for mode in [HostMode::FlushFails, HostMode::StoreFails] { + let dir = temp_dir(&format!("held_undurable_{mode:?}")); + let id = SubwalletId::new([0x11; 32], 0); + let host = RecordingHost::new(HostMode::Inline); + { + let coordinator = + coordinator_on_host(&dir, sdk_reading_held_create(false).await, host.clone()) + .await; + arm_identity_create(&coordinator, id, true).await; + // The host holds the pending row. + let pending = coordinator + .store() + .read() + .await + .get_activity_by_entry_id(id, &CREATE_ROW) + .unwrap() + .unwrap(); + host.durable.lock().unwrap().insert(CREATE_ROW, pending); + host.set_mode(mode); + + scan_sees_identity_create(&coordinator, id, 40).await; + post_scan_steps(&coordinator, id).await; + + assert_eq!( + create_row(&coordinator, id).await.0, + ShieldedActivityStatus::InputsConsumed, + "{mode:?}: proven in memory" + ); + assert_eq!( + host.durable_row(&CREATE_ROW).unwrap().status, + ShieldedActivityStatus::Pending, + "{mode:?}: not on the host" + ); + host.crash(); + } + let path = dir.join("tree.sqlite"); + let records = FileBackedShieldedStore::open_path(&path, 100) + .unwrap() + .pending_redrives(id) + .unwrap(); + assert_eq!(records.len(), 1, "{mode:?}: the record stays"); + assert_eq!( + records[0].identity_outcome, + Some(IdentityCreateOutcome::InputsConsumed { + consumed_by_height: 0 + }), + "{mode:?}: with its outcome" + ); + + // The restart: a node that answers nothing, and the host's + // snapshot, still pending. + host.set_mode(HostMode::Inline); + let coordinator = + coordinator_on_host(&dir, dash_sdk::Sdk::new_mock(), host.clone()).await; + restore_host_snapshot(&coordinator, id).await; + post_scan_steps(&coordinator, id).await; + + let row = host.durable_row(&CREATE_ROW).unwrap(); + assert_eq!( + (row.status, row.block_height), + (ShieldedActivityStatus::InputsConsumed, Some(0)), + "{mode:?}: written from the kept outcome" + ); + drop(coordinator); + assert!( + FileBackedShieldedStore::open_path(&path, 100) + .unwrap() + .pending_redrives(id) + .unwrap() + .is_empty(), + "{mode:?}: dropped once durable" + ); + let _ = std::fs::remove_dir_all(&dir); + } + } + + /// The result wait proves the create's inputs consumed, but the host + /// refuses the store of its spent note and its failed row. The next pass, + /// with the host back, finds the note already spent in memory, so its scan + /// emits nothing for it: the settlement puts the spent note on the pass + /// changeset with the failed row, and the record goes only then. After a + /// restart the host holds both, and no record is left to correct them. + #[tokio::test] + async fn should_replay_the_spent_notes_with_the_kept_outcome() { + use crate::wallet::shielded::activity::ShieldedActivityStatus; + + let dir = temp_dir("held_replayed_spends"); + let id = SubwalletId::new([0x11; 32], 0); + let host = RecordingHost::new(HostMode::Inline); + let coordinator = coordinator_on_host(&dir, dash_sdk::Sdk::new_mock(), host.clone()).await; + arm_identity_create(&coordinator, id, true).await; + host.set_mode(HostMode::StoreFails); + let persister = coordinator + .persisters + .read() + .await + .get(&id.wallet_id) + .cloned(); + super::super::operations::settle_inputs_consumed_identity_create( + coordinator.store(), + persister.as_ref(), + id.wallet_id, + id, + Some(&CREATE_ROW), + &[CREATE_INPUT], + 0, + ) + .await; + assert!(!host.holds_spent(&CREATE_INPUT), "the store was refused"); + assert!(host.durable_row(&CREATE_ROW).is_none()); + + host.set_mode(HostMode::Inline); + scan_sees_identity_create(&coordinator, id, 40).await; + post_scan_steps(&coordinator, id).await; + drop(coordinator); + + assert!( + host.holds_spent(&CREATE_INPUT), + "the spent note is replayed" + ); + let row = host + .durable_row(&CREATE_ROW) + .expect("the host holds the row"); + assert_eq!( + (row.status, row.block_height), + (ShieldedActivityStatus::InputsConsumed, Some(0)) + ); + assert!( + FileBackedShieldedStore::open_path(dir.join("tree.sqlite"), 100) + .unwrap() + .pending_redrives(id) + .unwrap() + .is_empty() + ); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A held create whose outcome is proven but which has no row of its own + /// id (its pending row never reached the store, and the scan rebuilt its + /// outputs under another cluster's id): nothing is written for it, and its + /// record, the only evidence of its outcome, stays. + #[tokio::test] + async fn should_keep_a_proven_identity_create_without_its_row() { + let dir = temp_dir("held_rowless"); + let id = SubwalletId::new([0x11; 32], 0); + let host = RecordingHost::new(HostMode::Inline); + let coordinator = coordinator_on_host(&dir, dash_sdk::Sdk::new_mock(), host.clone()).await; + let rowless = [0x99; 32]; + { + let mut store = coordinator.store().write().await; + let mut input = test_note(CREATE_INPUT, 0, false); + input.cmx = [0x56; 32]; + store.save_note(id, &input).unwrap(); + store + .arm_redrive( + id, + PendingRedrive { + activity_id: rowless, + anchor: CREATE_ANCHOR, + nullifiers: vec![CREATE_INPUT], + st_bytes: held_identity_create_transition() + .serialize_to_bytes() + .unwrap(), + attempts: 0, + anchor_height: Some(5), + identity_nonce_finalized: false, + identity_user_abandoned: false, + outcome_endpoint: Some(HELD_ENDPOINT.to_string()), + identity_outcome: Some(IdentityCreateOutcome::InputsConsumed { + consumed_by_height: 7, + }), + }, + ) + .unwrap(); + } + + scan_sees_identity_create(&coordinator, id, 40).await; + post_scan_steps(&coordinator, id).await; + + assert!(host.durable_row(&rowless).is_none()); + let records = coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap(); + assert_eq!(records.len(), 1, "the record stays"); + assert_eq!( + records[0].identity_outcome, + Some(IdentityCreateOutcome::InputsConsumed { + consumed_by_height: 7 + }) + ); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A create held from its save whose node was not kept yet (its + /// broadcast still in flight, or a restart before it returned): the scan + /// that sees its notes spent keeps its record and does not confirm its + /// row, and nothing reads it. + #[tokio::test] + async fn should_hold_an_identity_create_before_its_node_is_kept() { + use crate::wallet::shielded::activity::ShieldedActivityStatus; + + let dir = temp_dir("held_no_node"); + let coordinator = coordinator_on(&dir, dash_sdk::Sdk::new_mock()).await; + let id = SubwalletId::new([0x11; 32], 0); + arm_identity_create(&coordinator, id, false).await; + { + let mut store = coordinator.store().write().await; + let mut record = store.pending_redrives(id).unwrap().remove(0); + record.identity_outcome = Some(IdentityCreateOutcome::Awaiting); + store.arm_redrive(id, record).unwrap(); + } + + for height in [40, 41] { + scan_sees_identity_create(&coordinator, id, height).await; + post_scan_steps(&coordinator, id).await; + } + + let records = coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap(); + assert_eq!(records.len(), 1); + assert_eq!(records[0].attempts, 0, "nothing was read"); + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::Pending, None) + ); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A held create whose node was never kept, after a scan saw its notes spent and so cleared + /// their reservations: no reservation or recorded anchor schedules it any more. The pass + /// schedules it from its record, sends it to one node, keeps that node, and proves its outcome + /// there. + #[tokio::test] + async fn should_settle_a_held_identity_create_with_no_node_once_its_reservations_are_gone() { + use crate::wallet::shielded::activity::ShieldedActivityStatus; + + let dir = temp_dir("held_no_node_spent"); + let coordinator = coordinator_on(&dir, sdk_reading_held_create(false).await).await; + let id = SubwalletId::new([0x11; 32], 0); + arm_identity_create(&coordinator, id, false).await; + { + let mut store = coordinator.store().write().await; + let mut record = store.pending_redrives(id).unwrap().remove(0); + record.identity_outcome = Some(IdentityCreateOutcome::Awaiting); + store.arm_redrive(id, record).unwrap(); + } + scan_sees_identity_create(&coordinator, id, 40).await; + assert!( + coordinator + .store() + .read() + .await + .stale_pending_spends(id) + .unwrap() + .is_empty(), + "no reservation is left to schedule it" + ); + + post_scan_steps(&coordinator, id).await; + + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::InputsConsumed, Some(0)) + ); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A held create whose node never proves its outcome: once its reads + /// are spent it is not read again, and it stays unconfirmed, held, for + /// every later scan that sees its outputs. One whose re-broadcasts spent + /// the shared attempts still gets one read, which settles it. + #[tokio::test] + async fn should_leave_a_held_identity_create_unconfirmed_once_its_reads_are_spent() { + use super::super::operations::MAX_REDRIVE_ATTEMPTS; + use crate::wallet::shielded::activity::ShieldedActivityStatus; + + let dir = temp_dir("held_budget"); + let coordinator = coordinator_on(&dir, dash_sdk::Sdk::new_mock()).await; + let id = SubwalletId::new([0x11; 32], 0); + arm_identity_create(&coordinator, id, true).await; + + for height in 40..40 + u64::from(MAX_REDRIVE_ATTEMPTS) + 2 { + scan_sees_identity_create(&coordinator, id, height).await; + post_scan_steps(&coordinator, id).await; + } + + let records = coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap(); + assert_eq!(records.len(), 1, "still held"); + assert_eq!( + records[0].attempts, + MAX_REDRIVE_ATTEMPTS + 1, + "read no more once its attempts are past the budget" + ); + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::Pending, None) + ); + let _ = std::fs::remove_dir_all(&dir); + + let dir = temp_dir("held_budget_redriven"); + let coordinator = coordinator_on(&dir, sdk_reading_held_create(false).await).await; + arm_identity_create(&coordinator, id, true).await; + for _ in 0..MAX_REDRIVE_ATTEMPTS { + coordinator + .store() + .write() + .await + .bump_redrive_attempts(id, &CREATE_ROW) + .unwrap(); + } + scan_sees_identity_create(&coordinator, id, 40).await; + post_scan_steps(&coordinator, id).await; + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::InputsConsumed, Some(0)) + ); + let _ = std::fs::remove_dir_all(&dir); + } + + /// Unique temp directory for a test's SQLite tree (no `tempfile` dev-dep). + fn temp_dir(tag: &str) -> PathBuf { + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos(); + std::env::temp_dir().join(format!("shielded_coordinator_test_{tag}_{nanos}")) + } + + /// Build a coordinator backed by a fresh file store under `dir`, with one + /// wallet registered so `accounts` / `persisters` are both non-empty. + async fn coordinator_with_one_wallet(dir: &std::path::Path) -> NetworkShieldedCoordinator { + std::fs::create_dir_all(dir).expect("create temp dir"); + let db_path = dir.join("tree.sqlite"); + let store = FileBackedShieldedStore::open_path(&db_path, 100).expect("open file store"); + let coordinator = NetworkShieldedCoordinator::new( + Arc::new(dash_sdk::Sdk::new_mock()), + dashcore::Network::Testnet, + db_path, + store, + ); + + let wallet_id: WalletId = [0x11; 32]; + let views = OrchardKeySet::from_seed(&[0x42u8; 64], dashcore::Network::Testnet, 0) + .expect("derive viewing keys") + .viewing_keys(); + let mut account_views = BTreeMap::new(); + account_views.insert(0u32, views); + let persister = WalletPersister::new(wallet_id, Arc::new(NoPlatformPersistence)); + coordinator + .register_wallet(wallet_id, account_views, persister) + .await + .expect("register wallet"); + coordinator + } + + /// Success path: a healthy `clear()` empties the shared commitment tree + /// AND drops the in-memory account / persister registries, returning `Ok`. + #[tokio::test] + async fn clear_success_empties_tree_and_registries() { + let dir = temp_dir("clear_ok"); + let coordinator = coordinator_with_one_wallet(&dir).await; + + // Put a leaf in the tree so the reset is observable. + { + let mut store = coordinator.store().write().await; + store.append_commitment(&[7u8; 32], true).unwrap(); + assert_eq!(store.tree_size().unwrap(), 1); + } + assert!(!coordinator.accounts.read().await.is_empty()); + assert!(!coordinator.persisters.read().await.is_empty()); + + coordinator.clear().await.expect("clear should succeed"); + + assert_eq!( + coordinator.store().read().await.tree_size().unwrap(), + 0, + "tree must be empty after a successful clear" + ); + assert!(coordinator.accounts.read().await.is_empty()); + assert!(coordinator.persisters.read().await.is_empty()); + + let _ = std::fs::remove_dir_all(&dir); + } + + /// ON-DISK durability of `clear()` — the regression guard for the + /// "Clear resets the in-memory tree but not the persisted SQLite" bug. + /// + /// The prior test asserts `tree_size() == 0` on the SAME store handle, + /// which passes even if `clear()` only reset an in-memory shardtree / + /// frontier and left the on-disk `commitment_tree_*` rows intact. This + /// test instead reopens the persisted file through a COMPLETELY FRESH + /// `FileBackedShieldedStore` (independent connection, cold frontier) after + /// `clear()` and asserts both the tree AND the per-subwallet watermark + /// read back empty — proving the reset reached disk. Without the on-disk + /// reset + WAL checkpoint, this fresh handle would reload the full tree + /// (the on-device "tap Clear, relaunch, still 867/867" symptom). + #[tokio::test] + async fn clear_resets_the_persisted_on_disk_store_not_just_memory() { + let dir = temp_dir("clear_ondisk"); + let db_path = dir.join("tree.sqlite"); + let coordinator = coordinator_with_one_wallet(&dir).await; + let wallet_id: WalletId = [0x11; 32]; + let id = SubwalletId::new(wallet_id, 0); + + // Build a non-trivial tree AND advance the watermark, then checkpoint + // — the exact durable state a real sync leaves behind. + { + let mut store = coordinator.store().write().await; + for i in 0..8u8 { + store.append_commitment(&[i + 1; 32], true).unwrap(); + } + store.checkpoint_tree(8).unwrap(); + store.set_last_synced_note_index(id, 8).unwrap(); + assert_eq!(store.tree_size().unwrap(), 8); + assert_eq!(store.last_synced_note_index(id).unwrap(), 8); + } + + coordinator.clear().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 + // size here would mean clear() never wrote the disk. + let reopened = FileBackedShieldedStore::open_path(&db_path, 100) + .expect("reopen persisted store after clear"); + assert_eq!( + reopened.tree_size().unwrap(), + 0, + "on-disk commitment tree must read 0 through a fresh handle after clear — \ + 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 @@ -3499,6 +4718,8 @@ mod tests { let coordinator = coordinator_with_one_wallet(&dir).await; let guard_id = SubwalletId::new([0x11; 32], 0); let identity_guard = super::super::store::PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x31; 32], anchor: [0x32; 32], nullifiers: vec![], @@ -3876,6 +5097,8 @@ mod tests { .expect("old account") .clone(); let identity_guard = super::super::store::PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x71; 32], anchor: [0x72; 32], nullifiers: vec![], @@ -3990,6 +5213,8 @@ mod tests { .arm_redrive( id, super::super::store::PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x71; 32], anchor: [0x72; 32], nullifiers: vec![], @@ -4491,6 +5716,8 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: *activity, anchor: *anchor, nullifiers: vec![*nullifier], @@ -4555,6 +5782,47 @@ mod tests { } } + // An identity create proven unable to execute, restored with its reservation and + // record but not its row, so its settlement waits: its anchor is pruned like the first + // spend's, in the same pass that would settle it. + let proven = ([0x41; 32], [0x42; 32], [0x43; 32], SET_HEIGHT - 5); + { + let mut store = coordinator.store().write().await; + let (nullifier, anchor, activity, anchor_height) = proven; + let note = ShieldedNote { + position: 2, + cmx: [0x4F; 32], + nullifier, + block_height: 1, + is_spent: false, + value: 1_000, + note_data: vec![0u8; 115], + }; + store.save_note(id, ¬e).expect("note"); + store.mark_pending(id, &nullifier).expect("reserved"); + store + .set_pending_spend(id, &nullifier, anchor, activity, Some(anchor_height)) + .expect("anchored"); + store + .arm_redrive( + id, + PendingRedrive { + outcome_endpoint: Some("http://127.0.0.1:3010".to_string()), + identity_outcome: Some(IdentityCreateOutcome::InputsConsumed { + consumed_by_height: 7, + }), + activity_id: activity, + anchor, + nullifiers: vec![nullifier], + st_bytes: vec![0xDE, 0xAD], + attempts: 0, + identity_nonce_finalized: false, + identity_user_abandoned: false, + anchor_height: Some(anchor_height), + }, + ) + .expect("record"); + } sdk.mock() .set_response_height_of::(SET_HEIGHT); coordinator.sync(true, &CancellationToken::new()).await; @@ -4569,6 +5837,11 @@ mod tests { held, "an anchor read above the set may be newer than it" ); + assert_eq!( + state(&store, proven.0, proven.2), + (true, true, None), + "a proven identity create keeps its record and spent note for its settlement" + ); drop(store); let _ = std::fs::remove_dir_all(&dir); } @@ -4596,6 +5869,8 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: activity, anchor, nullifiers: vec![nullifier], @@ -4695,6 +5970,8 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: activity, anchor, nullifiers: vec![nullifier], 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..5ce2cc98df3 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/file_store.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/file_store.rs @@ -23,8 +23,9 @@ use grovedb_commitment_tree::{ClientPersistentCommitmentTree, Position, Retentio use super::balance::{ShieldedBalanceSource, ShieldedLocalAccountBalance}; use super::store::{ - PendingRedrive, PrivateContactPayment, PrivatePaymentEntry, PrivatePaymentRecord, ShieldedNote, - ShieldedOutgoingNote, ShieldedStore, StalePendingSpend, SubwalletId, SubwalletState, + IdentityCreateOutcome, PendingRedrive, PrivateContactPayment, PrivatePaymentEntry, + PrivatePaymentRecord, ShieldedNote, ShieldedOutgoingNote, ShieldedStore, StalePendingSpend, + SubwalletId, SubwalletState, }; use crate::error::PlatformWalletError; use crate::wallet::platform_wallet::WalletId; @@ -191,8 +192,8 @@ impl FileBackedShieldedStore { let nullifier_blob: Vec = redrive.nullifiers.iter().flatten().copied().collect(); tx.execute( "INSERT OR REPLACE INTO shielded_pending_spends \ - (wallet_id, account_index, activity_id, anchor, nullifiers, st_bytes, attempts, identity_nonce_finalized, identity_user_abandoned, private_contact_payment, anchor_height) \ - VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)", + (wallet_id, account_index, activity_id, anchor, nullifiers, st_bytes, attempts, identity_nonce_finalized, identity_user_abandoned, private_contact_payment, anchor_height, outcome_endpoint, identity_outcome, identity_outcome_height) \ + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14)", rusqlite::params![ id.wallet_id.as_slice(), id.account_index, @@ -209,6 +210,9 @@ impl FileBackedShieldedStore { redrive .anchor_height .and_then(|height| i64::try_from(height).ok()), + redrive.outcome_endpoint, + identity_outcome_code(redrive.identity_outcome), + identity_outcome_height(redrive.identity_outcome), ], ) .map_err(|e| FileShieldedStoreError::Storage(format!("persist redrive: {e}")))?; @@ -479,6 +483,38 @@ impl FileBackedShieldedStore { FileShieldedStoreError::Storage(format!("upgrade pending schema: {e}")) })?; } + // The node an identity create was delivered to, when one was kept + // (`PendingRedrive::outcome_endpoint`): NULL for every other record. + if !columns + .iter() + .any(|existing| existing == "outcome_endpoint") + { + pending_conn + .execute( + "ALTER TABLE shielded_pending_spends ADD COLUMN outcome_endpoint TEXT", + [], + ) + .map_err(|e| { + FileShieldedStoreError::Storage(format!("upgrade pending schema: {e}")) + })?; + } + // Where a held identity create stands (`PendingRedrive::identity_outcome`): + // NULL for every other record. + for (column, ty) in [ + ("identity_outcome", "INTEGER"), + ("identity_outcome_height", "INTEGER"), + ] { + if !columns.iter().any(|existing| existing == column) { + pending_conn + .execute( + &format!("ALTER TABLE shielded_pending_spends ADD COLUMN {column} {ty}"), + [], + ) + .map_err(|e| { + FileShieldedStoreError::Storage(format!("upgrade pending schema: {e}")) + })?; + } + } // The private payment journal: one record per private contact request // payment that may have reached the network, by wallet and entry // commitment, and the wallets whose journal is open. Never purged. @@ -576,7 +612,8 @@ impl FileBackedShieldedStore { .prepare( "SELECT wallet_id, account_index, activity_id, anchor, nullifiers, st_bytes, \ attempts, identity_nonce_finalized, identity_user_abandoned, \ - private_contact_payment, anchor_height FROM shielded_pending_spends", + private_contact_payment, anchor_height, outcome_endpoint, identity_outcome, \ + identity_outcome_height FROM shielded_pending_spends", ) .map_err(|e| FileShieldedStoreError::Storage(format!("prepare rehydrate: {e}")))?; let rows = stmt @@ -611,6 +648,18 @@ impl FileBackedShieldedStore { .ok() .flatten() .and_then(|height| u64::try_from(height).ok()); + // A node that does not read is not known: the create stays + // unconfirmed rather than being read from another node. + let outcome_endpoint = row.get::<_, Option>(11).ok().flatten(); + // A record saved before creates were held from their save is + // held once it has a node, as that build held it. + let identity_outcome = read_identity_outcome( + row.get::<_, Option>(12).ok().flatten(), + row.get::<_, Option>(13).ok().flatten(), + ) + .or(outcome_endpoint + .is_some() + .then_some(IdentityCreateOutcome::Awaiting)); let decoded: rusqlite::Result<_> = (|| { Ok(( row.get::<_, Vec>(0)?, @@ -629,6 +678,8 @@ impl FileBackedShieldedStore { readable_wallet_id, private_contact_payment, anchor_height, + outcome_endpoint, + identity_outcome, decoded, )) }) @@ -639,6 +690,8 @@ impl FileBackedShieldedStore { readable_wallet_id, private_contact_payment, anchor_height, + outcome_endpoint, + identity_outcome, decoded, ) = row.map_err(|e| FileShieldedStoreError::RecoveryCorrupted { account_index: None, @@ -724,6 +777,8 @@ impl FileBackedShieldedStore { Ok(( SubwalletId::new(wallet_id, account_index), PendingRedrive { + outcome_endpoint, + identity_outcome, activity_id, anchor, nullifiers, @@ -923,6 +978,43 @@ impl FileBackedShieldedStore { } } +/// The `identity_outcome` column of a record's +/// [`PendingRedrive::identity_outcome`]: NULL for none, 0 awaiting, 1 created, +/// 2 inputs consumed (with its height in `identity_outcome_height`). +fn identity_outcome_code(outcome: Option) -> Option { + outcome.map(|outcome| match outcome { + IdentityCreateOutcome::Awaiting => 0, + IdentityCreateOutcome::Created => 1, + IdentityCreateOutcome::InputsConsumed { .. } => 2, + }) +} + +/// The `identity_outcome_height` column of a record's +/// [`PendingRedrive::identity_outcome`]. A height past SQLite's integer range +/// is not kept, which reads the outcome back as awaiting. +fn identity_outcome_height(outcome: Option) -> Option { + match outcome { + Some(IdentityCreateOutcome::InputsConsumed { consumed_by_height }) => { + i64::try_from(consumed_by_height).ok() + } + _ => None, + } +} + +/// The [`PendingRedrive::identity_outcome`] [`identity_outcome_code`] and +/// [`identity_outcome_height`] wrote. A value that does not read is a held +/// create whose outcome is not known: it is read again. +fn read_identity_outcome(code: Option, height: Option) -> Option { + let code = code?; + Some(match (code, height.and_then(|h| u64::try_from(h).ok())) { + (1, _) => IdentityCreateOutcome::Created, + (2, Some(consumed_by_height)) => { + IdentityCreateOutcome::InputsConsumed { consumed_by_height } + } + _ => IdentityCreateOutcome::Awaiting, + }) +} + impl ShieldedStore for FileBackedShieldedStore { type Error = FileShieldedStoreError; @@ -1388,6 +1480,84 @@ impl ShieldedStore for FileBackedShieldedStore { .collect()) } + fn set_redrive_outcome_endpoint( + &mut self, + id: SubwalletId, + activity_id: &[u8; 32], + endpoint: &str, + ) -> Result { + // Persist first, as `bump_redrive_attempts` does. + if self + .subwallets + .get(&id) + .and_then(|sw| sw.redrive_attempts(activity_id)) + .is_none() + { + return Ok(false); + } + { + let conn = self.pending_conn.lock().expect("pending_conn mutex"); + conn.execute( + "UPDATE shielded_pending_spends SET outcome_endpoint = ?4, \ + identity_outcome = COALESCE(identity_outcome, ?5) \ + WHERE wallet_id = ?1 AND account_index = ?2 AND activity_id = ?3", + rusqlite::params![ + id.wallet_id.as_slice(), + id.account_index, + activity_id.as_slice(), + endpoint, + identity_outcome_code(Some(IdentityCreateOutcome::Awaiting)), + ], + ) + .map_err(|e| { + FileShieldedStoreError::Storage(format!("keep redrive outcome endpoint: {e}")) + })?; + } + Ok(self + .subwallets + .get_mut(&id) + .is_some_and(|sw| sw.set_redrive_outcome_endpoint(activity_id, endpoint))) + } + + fn set_redrive_identity_outcome( + &mut self, + id: SubwalletId, + activity_id: &[u8; 32], + outcome: IdentityCreateOutcome, + ) -> Result { + // Persist first, as `bump_redrive_attempts` does. + if self + .subwallets + .get(&id) + .and_then(|sw| sw.redrive_attempts(activity_id)) + .is_none() + { + return Ok(false); + } + { + let conn = self.pending_conn.lock().expect("pending_conn mutex"); + conn.execute( + "UPDATE shielded_pending_spends SET identity_outcome = ?4, \ + identity_outcome_height = ?5 \ + WHERE wallet_id = ?1 AND account_index = ?2 AND activity_id = ?3", + rusqlite::params![ + id.wallet_id.as_slice(), + id.account_index, + activity_id.as_slice(), + identity_outcome_code(Some(outcome)), + identity_outcome_height(Some(outcome)), + ], + ) + .map_err(|e| { + FileShieldedStoreError::Storage(format!("keep redrive identity outcome: {e}")) + })?; + } + Ok(self + .subwallets + .get_mut(&id) + .is_some_and(|sw| sw.set_redrive_identity_outcome(activity_id, outcome))) + } + fn bump_redrive_attempts( &mut self, id: SubwalletId, @@ -1767,6 +1937,8 @@ mod tests { let path = temp_tree_path("redrive_roundtrip"); let id = SubwalletId::new([7u8; 32], 0); let redrive = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [1u8; 32], anchor: [2u8; 32], nullifiers: vec![[3u8; 32], [4u8; 32]], @@ -1829,6 +2001,8 @@ mod tests { let wallet_id = [0x5A; 32]; let id = SubwalletId::new(wallet_id, 0); let record = |activity: u8, nullifier: u8| PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [activity; 32], anchor: [2u8; 32], nullifiers: vec![[nullifier; 32]], @@ -1902,6 +2076,8 @@ mod tests { let id = SubwalletId::new(wallet_id, 0); let other = SubwalletId::new([0x5D; 32], 0); let record = |activity: u8, nullifier: u8| PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [activity; 32], anchor: [2u8; 32], nullifiers: vec![[nullifier; 32]], @@ -1982,6 +2158,8 @@ mod tests { let path = temp_tree_path("identity_debit_guard"); let id = SubwalletId::new([9u8; 32], 4); let redrive = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [1u8; 32], anchor: [2u8; 32], nullifiers: vec![], @@ -2080,6 +2258,8 @@ mod tests { .arm_redrive( SubwalletId::new([1; 32], 2), PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [2; 32], anchor: [3; 32], nullifiers: vec![], @@ -2137,6 +2317,8 @@ mod tests { } .into(); let record = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x11; 32], anchor: [0x52; 32], nullifiers: vec![[0x13; 32]], @@ -2251,6 +2433,8 @@ mod tests { } .into(); let guard = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x61; 32], anchor: [0x71; 32], nullifiers: vec![], @@ -2266,6 +2450,8 @@ mod tests { .arm_redrive( note_id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x21; 32], anchor: [0x51; 32], nullifiers: vec![[0x91; 32]], @@ -2332,6 +2518,8 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0xD1; 32], anchor: [0xC1; 32], nullifiers: vec![], @@ -2386,6 +2574,8 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0xD2; 32], anchor: [0xF1; 32], nullifiers: vec![[0xC2; 32]], @@ -2420,6 +2610,8 @@ mod tests { let path = temp_tree_path("redrive_park_failure"); let id = SubwalletId::new([0x91; 32], 2); let record = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x12; 32], anchor: [0x23; 32], nullifiers: vec![], @@ -2448,6 +2640,8 @@ mod tests { let path = temp_tree_path("clear_redrive_failure"); let id = SubwalletId::new([0x91; 32], 2); let redrive = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x12; 32], anchor: [0x23; 32], nullifiers: vec![], @@ -2498,6 +2692,8 @@ mod tests { let wallet_id = [0xA4; 32]; let id = SubwalletId::new(wallet_id, 8); let identity_guard = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x15; 32], anchor: [0x26; 32], nullifiers: vec![], @@ -2508,6 +2704,8 @@ mod tests { anchor_height: None, }; let note_spend = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x48; 32], anchor: [0x59; 32], nullifiers: vec![[0x6A; 32]], @@ -2554,6 +2752,8 @@ mod tests { let id_a = SubwalletId::new([0xA1; 32], 0); let id_b = SubwalletId::new([0xB2; 32], 0); let redrive = |activity: u8, nf: u8| PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [activity; 32], anchor: [0x22; 32], nullifiers: vec![[nf; 32]], @@ -2632,6 +2832,8 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x7; 32], anchor: [0x22; 32], nullifiers: vec![nf], @@ -3056,6 +3258,8 @@ mod tests { account_reference: 7, }; let redrive = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x21; 32], anchor: [0x22; 32], nullifiers: vec![[0x23; 32], [0x24; 32]], @@ -3210,6 +3414,8 @@ mod tests { let wallet_id = [0x5C; 32]; let id = SubwalletId::new(wallet_id, 0); let redrive = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x31; 32], anchor: [0x32; 32], nullifiers: vec![[0x33; 32]], @@ -3311,6 +3517,8 @@ mod tests { .arm_private_contact_payment( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x31; 32], anchor: [0x32; 32], nullifiers: vec![[0x33; 32]], @@ -3376,6 +3584,8 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x41; 32], anchor: [0x42; 32], nullifiers: vec![[0x43; 32]], diff --git a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs index c213918b251..aa1865e6a9f 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs @@ -29,12 +29,12 @@ use super::note_selection::{ select_notes_with_fee, ShieldedFeeKind, }; use super::store::{ - PendingRedrive, PrivateContactPayment, PrivatePaymentEntry, ShieldedNote, ShieldedStore, - SubwalletId, + IdentityCreateOutcome, PendingRedrive, PrivateContactPayment, PrivatePaymentEntry, + ShieldedNote, ShieldedStore, SubwalletId, }; use crate::broadcast_outcome::{ - broadcast_tracking_delivery, broadcast_was_refused, carries_consensus_rejection, - BroadcastDelivery, + broadcast_tracking_delivery, broadcast_tracking_endpoint, broadcast_was_refused, + carries_consensus_rejection, refused_before_sending, BroadcastDelivery, }; use crate::changeset::{PlatformWalletChangeSet, ShieldedChangeSet}; use crate::error::{preserve_signer_key_unavailable_or, PlatformWalletError}; @@ -52,6 +52,7 @@ use dash_sdk::dapi_grpc::platform::v0::ResponseMetadata; use dash_sdk::platform::fetch_current_no_parameters::FetchCurrent; use dash_sdk::platform::transition::broadcast::BroadcastStateTransition; use dash_sdk::platform::transition::identity_create_from_shielded_pool::IdentityCreateFromShieldedPool; +use dash_sdk::platform::transition::outcome::{OutcomeEvidence, TransitionOutcome}; use dash_sdk::platform::Fetch; use dash_sdk::query_types::IdentityNonceFetcher; use dpp::address_funds::{ @@ -1012,6 +1013,8 @@ async fn arm_identity_shield_redrive( .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: entry.id, anchor: transition.anchor(), nullifiers: vec![], @@ -2925,31 +2928,16 @@ where match result { Ok((identity_id, identity)) => { - // It executed: the outcome owns the notes, which it marks spent. - reservation.hand_over(); - record_activity_status( + settle_created_identity_create( store, persister, wallet_id, id, &pending_entry, - ShieldedActivityStatus::Confirmed, - None, + reservation, + &selected_notes, ) .await; - // Best-effort post-broadcast bookkeeping (see `unshield`): mark the spent notes so the - // local balance reflects the exit immediately; any drift heals on the next nullifier - // sync. The on-chain nullifier set — not this local mark — is the authoritative - // no-reuse guarantee. - if let Err(e) = finalize_pending(store, persister, wallet_id, id, &selected_notes).await - { - warn!( - account, - error = %e, - "IdentityCreateFromShieldedPool broadcast succeeded but local spent-state \ - update failed; will heal on next sync" - ); - } info!( account, denomination, @@ -2961,21 +2949,116 @@ where // A create that may have reached a node keeps its reservation, its saved record and its // `Pending` activity row (see `unshield`'s outer match), and reports the derived id: the // identity may already exist on chain, so the caller holds its registration slot. - Err(failure) => { - let error = settle_failed_spend( - store, - persister, - wallet_id, - id, - &pending_entry, - reservation, - IDENTITY_CREATE_OPERATION, - failure, - ) - .await; - Err(identity_create_failure(error, derived_identity_id)) - } + Err(failure) => Err(settle_failed_identity_create( + store, + persister, + wallet_id, + id, + &pending_entry, + reservation, + &selected_notes, + failure, + derived_identity_id, + ) + .await), + } +} + +/// Settle the notes, saved record and activity row of an identity create whose result wait +/// proved it executed: its `notes` are marked spent and its row confirmed, and its proven outcome +/// ([`IdentityCreateOutcome::Created`]) is kept with its record until the host holds both +/// durably: the sync pass replays them and retires the record then +/// ([`settle_held_identity_creates`]). +async fn settle_created_identity_create( + store: &Arc>, + persister: Option<&WalletPersister>, + wallet_id: WalletId, + id: SubwalletId, + pending_entry: &Option, + mut reservation: NoteReservation, + notes: &[ShieldedNote], +) { + // It executed: the outcome owns the notes, which it marks spent. + reservation.hand_over(); + record_activity_status( + store, + persister, + wallet_id, + id, + pending_entry, + ShieldedActivityStatus::Confirmed, + None, + ) + .await; + // Best-effort post-broadcast bookkeeping (see `unshield`): mark the spent notes so the + // local balance reflects the exit immediately; any drift heals on the next nullifier + // sync. The on-chain nullifier set — not this local mark — is the authoritative + // no-reuse guarantee. + if let Err(e) = finalize_pending(store, persister, wallet_id, id, notes).await { + warn!( + error = %e, + "IdentityCreateFromShieldedPool broadcast succeeded but local spent-state \ + update failed; will heal on next sync" + ); + } + // Its record is kept, with its outcome, until the host holds this row and these + // spent notes durably: the sync pass replays both and retires it then + // ([`settle_held_identity_creates`]). + if let Some(entry) = pending_entry.as_ref() { + keep_identity_outcome(store, id, &entry.id, IdentityCreateOutcome::Created).await; + } +} + +/// Settle the notes, saved record and activity row of an identity create that ended with +/// `failure`, and return the error it reports. A create proven unable to execute without its +/// identity ([`PlatformWalletError::ShieldedIdentityCreateInputsConsumed`]) has its `notes` +/// marked spent, its proven outcome kept with its record until durable settlement, and its +/// activity row failed ([`settle_inputs_consumed_identity_create`]); every other failure is +/// settled as any spend's ([`settle_failed_spend`], [`identity_create_failure`]). +#[allow(clippy::too_many_arguments)] +async fn settle_failed_identity_create( + store: &Arc>, + persister: Option<&WalletPersister>, + wallet_id: WalletId, + id: SubwalletId, + pending_entry: &Option, + mut reservation: NoteReservation, + notes: &[ShieldedNote], + failure: SpendFailure, + identity_id: Option, +) -> PlatformWalletError { + if let PlatformWalletError::ShieldedIdentityCreateInputsConsumed { + consumed_by_height, .. + } = failure.error + { + // The notes are spent on chain: marking them spent clears their reservations, so the + // guard must not release them. + reservation.hand_over(); + let nullifiers: Vec<[u8; 32]> = notes.iter().map(|note| note.nullifier).collect(); + settle_inputs_consumed_identity_create( + store, + persister, + wallet_id, + id, + pending_entry.as_ref().map(|entry| &entry.id), + &nullifiers, + consumed_by_height, + ) + .await; + return failure.error; } + let error = settle_failed_spend( + store, + persister, + wallet_id, + id, + pending_entry, + reservation, + IDENTITY_CREATE_OPERATION, + failure, + ) + .await; + identity_create_failure(error, identity_id) } /// The error an identity create reports for `error`, what [`settle_failed_spend`] settled its @@ -3025,7 +3108,17 @@ async fn broadcast_identity_create_with_redrive( reservation, st, IDENTITY_CREATE_OPERATION, - || broadcast_identity_create(sdk, st, identity_id, submitted_public_keys), + || { + broadcast_identity_create( + sdk, + store, + id, + pending_entry.as_ref().map(|entry| entry.id), + st, + identity_id, + submitted_public_keys, + ) + }, ) .await } @@ -3050,12 +3143,27 @@ const IDENTITY_CREATE_OPERATION: &str = "identity create from shielded pool"; /// signature backs it, and one that kept or forwarded the transition can answer with any /// verdict. It also includes cause-less ones, as DAPI encodes its own wait-side failures /// (timeouts, internal errors) that way. +/// - a wait that fails, with the identity not found, is judged by proven reads +/// ([`probe_identity_create`]): its nullifiers all spent and its identity absent at or +/// above the height they were spent by is +/// [`PlatformWalletError::ShieldedIdentityCreateInputsConsumed`]: it can never execute and +/// did not create its identity. Its paid failure path and this wallet's notes spent by +/// another transition (another device with the same keys) read the same. /// -/// Nothing here reports a failed create once a dispatch may have reached a node: a create -/// reported failed while it may still execute is the orphaned-identity hazard this staging -/// exists to avoid. -async fn broadcast_identity_create( +/// Every read after the broadcast names the transition (its nullifiers, its identity id), so +/// each goes only to the node one dispatch reached ([`broadcast_tracking_endpoint`]), through an +/// SDK scoped to it, and that node is kept with the create's record +/// ([`PendingRedrive::outcome_endpoint`]) for the sync pass to read the outcome from later. A +/// create with no such node is never probed: it stays unconfirmed. +/// +/// Nothing here reports a failed create once a dispatch may have reached a node, unless proven +/// reads show it can no longer execute: a create reported failed while it may still execute is +/// the orphaned-identity hazard this staging exists to avoid. +async fn broadcast_identity_create( sdk: &Arc, + store: &Arc>, + id: SubwalletId, + activity_id: Option<[u8; 32]>, st: &StateTransition, identity_id: Identifier, submitted_public_keys: BTreeMap, @@ -3067,7 +3175,8 @@ async fn broadcast_identity_create( }) }; - match broadcast_tracking_delivery(sdk, st).await { + let (delivery, endpoint) = broadcast_tracking_endpoint(sdk, st).await; + match delivery { BroadcastDelivery::Admitted => {} BroadcastDelivery::NeverDelivered(e) => { return Err(SpendFailure { @@ -3084,12 +3193,22 @@ async fn broadcast_identity_create( ); } } + // The node that holds the transition answers every read about it from here on. + let reader: Arc = match &endpoint { + Some(endpoint) => { + if let Some(activity_id) = activity_id { + keep_outcome_endpoint(store, id, &activity_id, endpoint).await; + } + Arc::new(sdk.scoped_to_address(endpoint.clone())) + } + None => Arc::clone(sdk), + }; // An identity-create-from-shielded-pool proof authenticates the spent // nullifiers and resulting identity as a snapshot; it cannot bind the // complete Orchard request, so accept the affected-state outcome. let proof_result = match st - .wait_for_affected_state::(sdk, None) + .wait_for_affected_state::(&reader, None) .await { Ok(result) => result, @@ -3106,25 +3225,55 @@ async fn broadcast_identity_create( // node we hit, so retry a few times with a short, fixed backoff before concluding // it's truly absent: long enough to ride out routine indexing/replica lag, short // enough not to wedge the caller's UI for minutes. - return match fetch_identity_with_retries(sdk, identity_id).await { - Some(mut identity) => { - info!( - derived_id = %identity_id, - "IdentityCreateFromShieldedPool: result confirmation failed but the \ - identity was found on chain by its derived id; treating as success" - ); - // Same defensive empty-`public_keys` fill as the proven-result path below, - // so downstream auth-key checks see the committed key set immediately. - if identity.public_keys().is_empty() { - identity.set_public_keys(submitted_public_keys); + // Each fallback read goes to the node through its own scoped SDK: a node the + // failed wait banned is banned only in the wait's copy, so it is asked again. + let fallback_reader = || match &endpoint { + Some(endpoint) => Arc::new(sdk.scoped_to_address(endpoint.clone())), + None => Arc::clone(&reader), + }; + let mut identity = match fetch_identity_with_retries(&fallback_reader(), identity_id) + .await + { + Some(identity) => identity, + // No node is known to hold the transition: nothing may be asked about it. + None if endpoint.is_none() => return Err(unconfirmed(wait_err.to_string())), + // Not found: proven reads tell a create that can no longer execute from an + // unknown outcome. + None => match probe_identity_create(&fallback_reader(), st, identity_id).await { + IdentityCreateProbe::Created(identity) => identity, + IdentityCreateProbe::InputsConsumed { consumed_by_height } => { + warn!( + derived_id = %identity_id, + consumed_by_height, + error = %wait_err, + "IdentityCreateFromShieldedPool: notes proven spent and identity \ + proven absent; the create can no longer execute" + ); + return Err(SpendFailure::from( + PlatformWalletError::ShieldedIdentityCreateInputsConsumed { + identity_id, + consumed_by_height, + reason: wait_err.to_string(), + }, + )); } - Ok((identity.id(), identity)) - } - // The saved record resolves it: the sync-time pass re-checks the nullifiers, - // then re-broadcasts this byte-identical transition up to - // MAX_REDRIVE_ATTEMPTS times. - None => Err(unconfirmed(wait_err.to_string())), + // The saved record resolves it: the sync-time pass re-checks the + // nullifiers, then re-broadcasts this byte-identical transition up to + // MAX_REDRIVE_ATTEMPTS times. + IdentityCreateProbe::Unknown => return Err(unconfirmed(wait_err.to_string())), + }, }; + info!( + derived_id = %identity_id, + "IdentityCreateFromShieldedPool: result confirmation failed but the \ + identity was found on chain by its derived id; treating as success" + ); + // Same defensive empty-`public_keys` fill as the proven-result path below, + // so downstream auth-key checks see the committed key set immediately. + if identity.public_keys().is_empty() { + identity.set_public_keys(submitted_public_keys); + } + return Ok((identity.id(), identity)); } }; @@ -3167,7 +3316,7 @@ async fn broadcast_identity_create( ); // The create executed. An identity that cannot be synthesized leaves it // unconfirmed, its notes and record kept for the next sync, never failed. - Identity::new_with_id_and_keys(identity_id, submitted_public_keys, sdk.version()) + Identity::new_with_id_and_keys(identity_id, submitted_public_keys, reader.version()) .map_err(|e| { unconfirmed(format!( "its result was proved, but the identity could not be read from it: {e}" @@ -3198,7 +3347,7 @@ const IDENTITY_CREATE_FETCH_RETRY_DELAY: std::time::Duration = std::time::Durati /// attempt comes back empty or errors (transport hiccup, not-yet-indexed, …) — the caller then /// surfaces `ShieldedBroadcastUnconfirmed` rather than a hard failure. async fn fetch_identity_with_retries( - sdk: &Arc, + sdk: &dash_sdk::Sdk, identity_id: Identifier, ) -> Option { use dash_sdk::platform::Fetch; @@ -3230,107 +3379,631 @@ async fn fetch_identity_with_retries( None } -// ------------------------------------------------------------------------- -// Internal helpers (free fns) -// ------------------------------------------------------------------------- +/// What proven reads show about a shielded identity create (Type 20) whose outcome is not known. +#[derive(Debug)] +enum IdentityCreateProbe { + /// The identity exists: the create executed. + Created(Identity), + /// Every nullifier the create spends is spent at or before `consumed_by_height`, and its + /// identity is proven absent at a height at or above it: the create can no longer execute, + /// and did not create the identity. The reads do not show what spent the notes: Platform's + /// paid failure path for a Type 20 (a key-state check that fails in the block) does, and so + /// does another transition spending the same notes (another device with this wallet's keys). + InputsConsumed { consumed_by_height: u64 }, + /// The reads prove neither: a read failed, a nullifier is still free, or the identity read + /// is older than the height the nullifiers were spent by. + Unknown, +} -/// Convert `keys`'s default `PaymentAddress` to an `OrchardAddress`. -fn default_orchard_address( - keys: &AccountViewingKeys, -) -> Result { - payment_address_to_orchard(&keys.default_address) +/// Read, with proofs, whether the identity create `st` can still execute +/// ([`dash_sdk::Sdk::state_transition_outcome`]), then whether its identity `identity_id` +/// exists, and judge the two ([`judge_identity_create_reads`]). The outcome is read first: a +/// nullifier spent at height `h` proves the create cannot execute after `h`, so only an identity +/// read at or above `h` that misses the identity shows it never created it. An SDK that does not +/// verify proofs gets no outcome, so the probe is [`IdentityCreateProbe::Unknown`] there. +/// +/// `sdk` must send its requests only to a node that already holds the transition +/// ([`dash_sdk::Sdk::scoped_to_address`]): the reads name its nullifiers and its identity. +async fn probe_identity_create( + sdk: &dash_sdk::Sdk, + st: &StateTransition, + identity_id: Identifier, +) -> IdentityCreateProbe { + let outcome = sdk + .state_transition_outcome(st, OutcomeEvidence::default()) + .await; + let identity = Identity::fetch_with_metadata(sdk, identity_id, None) + .await + .map(|(identity, metadata)| (identity, metadata.height)); + if let Err(e) = &outcome { + debug!(derived_id = %identity_id, error = %e, "identity create outcome read failed"); + } + if let Err(e) = &identity { + debug!(derived_id = %identity_id, error = %e, "identity create identity read failed"); + } + judge_identity_create_reads(outcome, identity) } -/// Checkpoint depths probed for a Platform-recorded anchor. Kept equal to the -/// commitment tree's `max_checkpoints` retention (the store is opened with -/// `100` — see `PlatformWalletManager`) so the probe reaches every checkpoint -/// the tree still holds and no further: deeper checkpoints are pruned, so -/// probing past this bound is wasted work. Coupled by convention — if that -/// retention changes, update this in lockstep. -const MAX_ANCHOR_PROBE_DEPTH: usize = 100; +/// Judge an identity create by its transition's proven `outcome` and the proven read of its +/// identity, taken after it: the identity and the height it was read at. +fn judge_identity_create_reads( + outcome: Result, + identity: Result<(Option, u64), dash_sdk::Error>, +) -> IdentityCreateProbe { + match (outcome, identity) { + (_, Ok((Some(identity), _))) => IdentityCreateProbe::Created(identity), + ( + Ok(TransitionOutcome::ExecutedOrSuperseded { consumed_by_height }), + Ok((None, read_at)), + ) if read_at >= consumed_by_height => { + IdentityCreateProbe::InputsConsumed { consumed_by_height } + } + _ => IdentityCreateProbe::Unknown, + } +} -/// Extract `SpendableNote` structs with Merkle witnesses and an anchor -/// Platform has recorded. -/// -/// A shielded spend's proof is accepted only if its anchor is a -/// commitment-tree root Platform recorded (`validate_anchor_exists`). -/// Platform records one anchor per block, but an index-chunk sync routinely -/// leaves the wallet's tree mid-block, so the depth-0 (current) root is -/// frequently a value Platform never recorded — building against it -/// unconditionally is what made such spends fail and never land. -/// -/// This fetches Platform's recorded anchor set (outside the store lock), then -/// selects the shallowest checkpoint depth whose root is in that set — depth 0 -/// being the fully-synced fast path — witnessing every note at that same depth -/// so the anchor and the authentication paths agree (the builder derives the -/// anchor from the witnesses via `MerklePath::root`, so a per-note disagreement -/// would surface downstream as `AnchorMismatch`). When no probed depth has a -/// recorded root it returns the retryable -/// [`PlatformWalletError::ShieldedNoRecordedAnchor`] rather than broadcasting a -/// spend Platform is guaranteed to reject. -/// -/// Returns the proved height of the recorded anchor set the anchor was chosen -/// from too: the anchor was in the set at that height, so a set proved at or -/// above it that lacks the anchor shows it pruned rather than newer. -async fn extract_spends_and_recorded_anchor( - sdk: &Arc, +/// Settle an identity create proven unable to execute without its identity +/// ([`PlatformWalletError::ShieldedIdentityCreateInputsConsumed`]) where its result wait learnt +/// it: its notes are spent on chain, so they are marked spent, which also clears their +/// reservations; the proven outcome is kept durably with its recovery record `activity_id` +/// ([`PendingRedrive::identity_outcome`]); and its activity row reads failed at +/// `consumed_by_height`, a height by which its notes were spent. The record stays: the sync pass +/// writes the failed row again after its scan's own rows and drops the record only once that +/// write is durable ([`settle_held_identity_creates`]). A store write that fails is logged: a +/// record whose outcome was not kept is read again by the next pass. +#[allow(clippy::too_many_arguments)] +pub(super) async fn settle_inputs_consumed_identity_create( store: &Arc>, - notes: &[ShieldedNote], -) -> Result<(Vec, Anchor, u64), PlatformWalletError> { - // Nothing selected — fail before the network round-trip. - if notes.is_empty() { - return Err(PlatformWalletError::ShieldedBuildError( - "no spendable notes selected — anchor undefined".to_string(), - )); + persister: Option<&WalletPersister>, + wallet_id: WalletId, + id: SubwalletId, + activity_id: Option<&[u8; 32]>, + nullifiers: &[[u8; 32]], + consumed_by_height: u64, +) { + if let Err(e) = mark_nullifiers_spent(store, persister, wallet_id, id, nullifiers).await { + warn!( + error = %e, + "an identity create's spent notes were not marked spent; the next scan marks them" + ); } + let Some(activity_id) = activity_id else { + return; + }; + keep_identity_outcome( + store, + id, + activity_id, + IdentityCreateOutcome::InputsConsumed { consumed_by_height }, + ) + .await; + record_proven_activity_status( + store, + persister, + wallet_id, + id, + activity_id, + ShieldedActivityStatus::InputsConsumed, + Some(consumed_by_height), + ) + .await; +} - // Fetch the recorded anchor set OUTSIDE the store lock so the network - // round-trip doesn't serialize with other store users, and so the lock is - // held only for the mutually-consistent depth/witness probe below. - let (dash_sdk::query_types::ShieldedAnchors(recorded_anchors), metadata) = - dash_sdk::query_types::ShieldedAnchors::fetch_current_with_metadata(sdk).await?; - let recorded: HashSet<[u8; 32]> = recorded_anchors.into_iter().collect(); - - // Hold a single read lock across the whole probe so the checkpoint depths - // and the per-note witnesses stay mutually consistent: a concurrent sync - // checkpointing mid-probe would otherwise shift the depth indices out from - // under us. - let store = store.read().await; - select_recorded_spends(&*store, notes, &recorded) - .map(|(spends, anchor)| (spends, anchor, metadata.height)) +/// Drop the recovery record `activity_id` of an identity create whose outcome is known. Its one +/// caller is the sync pass, once the host holds that create's row and spent notes durably +/// ([`retire_settled_identity_creates`]): a create's outcome, proven where it is learnt (the +/// result wait, a redrive, the sync pass), is kept with its record until then, whichever it is. +/// A failure is logged: the record stays, and the next pass settles it again from its kept +/// outcome. +async fn retire_identity_create_record( + store: &Arc>, + id: SubwalletId, + activity_id: Option<&[u8; 32]>, +) { + let Some(activity_id) = activity_id else { + return; + }; + if let Err(e) = store.write().await.clear_redrive(id, activity_id) { + warn!( + activity_id = %hex::encode(activity_id), + error = %e, + "an identity create's recovery record was not retired; the next pass retries" + ); + } } -/// Pick the shallowest checkpoint depth whose tree root is in `recorded`, -/// witnessing every note at that depth. Pure (no SDK, no async), so the depth -/// walk can be unit-tested against a real commitment tree. -/// -/// Depth 0 is the current tree state (the fully-synced fast path). Deeper -/// checkpoints are older and hold strictly fewer positions, so the probe stops -/// as soon as a selected note is no longer witnessable at a depth — no deeper -/// checkpoint could contain it. Returns -/// [`PlatformWalletError::ShieldedNoRecordedAnchor`] when no probed depth has a -/// recorded root (a clean, retryable outcome — nothing is broadcast). -fn select_recorded_spends( - store: &S, - notes: &[ShieldedNote], - recorded: &HashSet<[u8; 32]>, -) -> Result<(Vec, Anchor), PlatformWalletError> { - use grovedb_commitment_tree::ExtractedNoteCommitment; +/// Drop `id`'s recovery records `activity_ids` of identity creates whose proven outcome +/// [`settle_held_identity_creates`] wrote into a pass changeset, once the host holds that +/// changeset durably. +pub(super) async fn retire_settled_identity_creates( + store: &Arc>, + id: SubwalletId, + activity_ids: &[[u8; 32]], +) { + for activity_id in activity_ids { + retire_identity_create_record(store, id, Some(activity_id)).await; + } +} - // Deserialize each note and decode its commitment ONCE — both are - // independent of the checkpoint depth, so hoisting them out of the probe - // keeps the depth walk cheap (each probed depth only re-witnesses). - let prepared: Vec<(u64, grovedb_commitment_tree::Note, ExtractedNoteCommitment)> = notes - .iter() - .map(|note| { - let orchard_note = deserialize_note(¬e.note_data).ok_or_else(|| { - PlatformWalletError::ShieldedBuildError(format!( - "Failed to deserialize note at position {}", - note.position - )) - })?; - let cmx = ExtractedNoteCommitment::from_bytes(¬e.cmx) - .into_option() +/// Keep `outcome`, proven for identity create `activity_id`, durably with its recovery record +/// ([`PendingRedrive::identity_outcome`]). False, logged, when it was not kept. +async fn keep_identity_outcome( + store: &Arc>, + id: SubwalletId, + activity_id: &[u8; 32], + outcome: IdentityCreateOutcome, +) -> bool { + match store + .write() + .await + .set_redrive_identity_outcome(id, activity_id, outcome) + { + Ok(true) => true, + Ok(false) => { + warn!( + activity_id = %hex::encode(activity_id), + "an identity create's proven outcome was not kept: its record is gone" + ); + false + } + Err(e) => { + warn!( + activity_id = %hex::encode(activity_id), + error = %e, + "an identity create's proven outcome was not kept with its record; read again \ + next pass" + ); + false + } + } +} + +/// Keep `endpoint`, the node a dispatch of identity create `activity_id` reached, with its +/// recovery record, so the sync pass reads its outcome from that node only. A failure is +/// logged: the create then stays unconfirmed after a restart rather than being read elsewhere. +async fn keep_outcome_endpoint( + store: &Arc>, + id: SubwalletId, + activity_id: &[u8; 32], + endpoint: &dash_sdk::sdk::Address, +) { + match store + .write() + .await + .set_redrive_outcome_endpoint(id, activity_id, &endpoint.to_string()) + { + Ok(true) => {} + Ok(false) => warn!( + activity_id = %hex::encode(activity_id), + "an identity create's node was not kept: its record is gone" + ), + Err(e) => warn!( + activity_id = %hex::encode(activity_id), + error = %e, + "an identity create's node was not kept with its record" + ), + } +} + +/// Write `status` (and `block_height`, when given) onto the activity row `entry_id` whatever it +/// holds, for an outcome proven by reads: unlike [`record_activity_status`], a row the scan +/// confirmed with a height is overwritten, as its outputs seen on chain do not show what an +/// identity create did. No-op if no such row exists or it cannot be read. +async fn record_proven_activity_status( + store: &Arc>, + persister: Option<&WalletPersister>, + wallet_id: WalletId, + id: SubwalletId, + entry_id: &[u8; 32], + status: ShieldedActivityStatus, + block_height: Option, +) { + let next = { + let mut store = store.write().await; + let stored = match store.get_activity_by_entry_id(id, entry_id) { + Ok(Some(stored)) => stored, + Ok(None) => return, + Err(e) => { + warn!( + entry_id = %hex::encode(entry_id), + error = %e, + "proven activity status: the row did not read; left as it is" + ); + return; + } + }; + let next = with_status(&stored, status, block_height); + if let Err(e) = store.save_activity(id, &next) { + warn!( + entry_id = %hex::encode(entry_id), + error = %e, + "proven activity status: in-memory save_activity failed; persister still queued" + ); + } + next + }; + queue_shielded_changeset(persister, wallet_id, changeset_for_entry(id, next)); +} + +/// What [`classify_held_identity_create`] learnt of a held identity create. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(super) enum HeldIdentityCreate { + /// Its outcome was proven and kept with its record. + Proven(IdentityCreateOutcome), + /// Nothing was proven, or the proven outcome was not kept: the record stays as it was. + Unknown, +} + +/// Read, at the node it was delivered to, the outcome of the identity create whose recovery +/// record `redrive` is held for it ([`PendingRedrive::outcome_endpoint`]), and keep what the +/// reads prove ([`probe_identity_create`]) durably with the record +/// ([`PendingRedrive::identity_outcome`]): +/// +/// - its identity exists: [`IdentityCreateOutcome::Created`]; +/// - its notes are spent and its identity absent: [`IdentityCreateOutcome::InputsConsumed`]. +/// +/// Either way its notes are marked spent. Its activity row is not touched here: the sync pass +/// writes it after its scan's rows ([`settle_held_identity_creates`]). Anything else, a record +/// with no node or a transition that is not an identity create included, changes nothing. +pub(super) async fn classify_held_identity_create( + sdk: &dash_sdk::Sdk, + store: &Arc>, + persister: Option<&WalletPersister>, + id: SubwalletId, + redrive: &PendingRedrive, +) -> HeldIdentityCreate { + use dpp::serialization::PlatformDeserializableTrusted; + use dpp::state_transition::state_transitions::shielded::identity_create_from_shielded_pool_transition::accessors::IdentityCreateFromShieldedPoolTransitionAccessorsV0; + + let Some(endpoint) = redrive.outcome_endpoint.as_deref() else { + return HeldIdentityCreate::Unknown; + }; + let Ok(endpoint) = endpoint.parse::() else { + warn!( + endpoint, + "an identity create's kept node does not read; not probed" + ); + return HeldIdentityCreate::Unknown; + }; + let Ok(st) = StateTransition::deserialize_from_bytes_trusted(&redrive.st_bytes) else { + return HeldIdentityCreate::Unknown; + }; + let StateTransition::IdentityCreateFromShieldedPool(create) = &st else { + return HeldIdentityCreate::Unknown; + }; + let identity_id = create.identity_id(); + let reader = sdk.scoped_to_address(endpoint); + let outcome = match probe_identity_create(&reader, &st, identity_id).await { + IdentityCreateProbe::Created(_) => { + info!(derived_id = %identity_id, "identity create proven executed"); + IdentityCreateOutcome::Created + } + IdentityCreateProbe::InputsConsumed { consumed_by_height } => { + warn!( + derived_id = %identity_id, + consumed_by_height, + "identity create proven unable to execute: its notes are spent and its \ + identity absent" + ); + IdentityCreateOutcome::InputsConsumed { consumed_by_height } + } + IdentityCreateProbe::Unknown => return HeldIdentityCreate::Unknown, + }; + if let Err(e) = + mark_nullifiers_spent(store, persister, id.wallet_id, id, &redrive.nullifiers).await + { + warn!(error = %e, "a settled identity create's notes were not marked spent"); + } + if !keep_identity_outcome(store, id, &redrive.activity_id, outcome).await { + return HeldIdentityCreate::Unknown; + } + HeldIdentityCreate::Proven(outcome) +} + +/// The sync pass's settlement of `id`'s identity creates held for their outcome read +/// ([`PendingRedrive::held_for_identity_outcome`]). Runs after the scan's activity rows are on +/// `changeset`, so what it writes there comes last: +/// +/// - a create whose outcome is not proven yet, whose node is known and whose notes the scan saw +/// spent is read at that node ([`classify_held_identity_create`]). A read that proves nothing +/// counts an attempt. The attempts are shared with its re-broadcasts, so a create is read while +/// they are at most [`MAX_REDRIVE_ATTEMPTS`]: one whose re-broadcasts spent them still gets one +/// read. Past that it is not read again; its record stays held and its row is never confirmed +/// from its outputs alone: it stays unconfirmed. So does a create with no known node, which is +/// never read; +/// - a create whose outcome is proven, now or by an earlier pass or result wait, gets its +/// activity row written with that outcome (Confirmed, or Failed at `consumed_by_height`) in +/// place of any row `changeset` already carries for it, and every note it consumed put on +/// `changeset` as spent ([`replay_identity_create_spends`]). +/// +/// Returns the activity ids of the creates whose row and spent notes are on `changeset`. Their +/// records are dropped only once the host holds `changeset` durably +/// ([`retire_settled_identity_creates`]); until then the next pass, after a restart included, +/// writes the row again from the outcome kept with the record. +pub(super) async fn settle_held_identity_creates( + sdk: &dash_sdk::Sdk, + store: &Arc>, + persister: Option<&WalletPersister>, + id: SubwalletId, + changeset: &mut ShieldedChangeSet, + cancel: &CancellationToken, +) -> Vec<[u8; 32]> { + let held: Vec = { + let store = store.read().await; + let redrives = match store.pending_redrives(id) { + Ok(redrives) => redrives, + Err(e) => { + warn!(error = %e, "held identity creates: pending_redrives failed; skipped"); + return Vec::new(); + } + }; + if !redrives + .iter() + .any(PendingRedrive::held_for_identity_outcome) + { + return Vec::new(); + } + let spent: HashSet<[u8; 32]> = match store.get_all_notes(id) { + Ok(notes) => notes + .into_iter() + .filter(|note| note.is_spent) + .map(|note| note.nullifier) + .collect(), + Err(e) => { + warn!(error = %e, "held identity creates: get_all_notes failed; skipped"); + return Vec::new(); + } + }; + redrives + .into_iter() + .filter(|redrive| match redrive.identity_outcome { + Some(outcome) if outcome.is_proven() => true, + Some(IdentityCreateOutcome::Awaiting) => { + redrive.outcome_endpoint.is_some() + && redrive.attempts <= MAX_REDRIVE_ATTEMPTS + && !redrive.nullifiers.is_empty() + && redrive.nullifiers.iter().all(|n| spent.contains(n)) + } + _ => false, + }) + .collect() + }; + let mut settled = Vec::new(); + for redrive in held { + let outcome = match redrive.identity_outcome { + Some(outcome) if outcome.is_proven() => outcome, + _ => { + if cancel.is_cancelled() { + break; + } + match classify_held_identity_create(sdk, store, persister, id, &redrive).await { + HeldIdentityCreate::Proven(outcome) => outcome, + HeldIdentityCreate::Unknown => { + count_unproven_identity_create_read(store, id, &redrive.activity_id).await; + continue; + } + } + } + }; + let replayed = replay_identity_create_spends(store, id, &redrive, changeset).await; + let written = + write_proven_identity_create_row(store, id, &redrive.activity_id, outcome, changeset) + .await; + if replayed && written { + settled.push(redrive.activity_id); + } + } + settled +} + +/// Count a read of held identity create `activity_id` that proved nothing. The last read +/// ([`settle_held_identity_creates`]) leaves it held and unconfirmed. +async fn count_unproven_identity_create_read( + store: &Arc>, + id: SubwalletId, + activity_id: &[u8; 32], +) { + match store.write().await.bump_redrive_attempts(id, activity_id) { + Ok(attempts) if attempts > MAX_REDRIVE_ATTEMPTS => warn!( + activity_id = %hex::encode(activity_id), + attempts, + "held identity create: its outcome could not be read; it stays unconfirmed" + ), + Ok(_) => {} + Err(e) => { + warn!(error = %e, "held identity create: attempt not counted; retried next pass") + } + } +} + +/// Write identity create `activity_id`'s row with its proven `outcome` into the in-memory store +/// and onto `changeset`, in place of any row `changeset` already carries for it. True when the +/// row is on `changeset`; false, logged, when there is no row of that id, or it did not read or +/// save. A create without its row keeps its record: the record is the only evidence of its +/// outcome, and it never goes without a terminal row written for it. +async fn write_proven_identity_create_row( + store: &Arc>, + id: SubwalletId, + activity_id: &[u8; 32], + outcome: IdentityCreateOutcome, + changeset: &mut ShieldedChangeSet, +) -> bool { + let (status, block_height) = match outcome { + IdentityCreateOutcome::Created => (ShieldedActivityStatus::Confirmed, None), + IdentityCreateOutcome::InputsConsumed { consumed_by_height } => ( + ShieldedActivityStatus::InputsConsumed, + Some(consumed_by_height), + ), + IdentityCreateOutcome::Awaiting => return false, + }; + let mut store = store.write().await; + let stored = match store.get_activity_by_entry_id(id, activity_id) { + Ok(Some(stored)) => stored, + Ok(None) => { + warn!( + entry_id = %hex::encode(activity_id), + "held identity create: no row of its id to write its outcome on; its record stays" + ); + return false; + } + Err(e) => { + warn!( + entry_id = %hex::encode(activity_id), + error = %e, + "held identity create: its row did not read; written next pass" + ); + return false; + } + }; + let next = with_status(&stored, status, block_height); + if let Err(e) = store.save_activity(id, &next) { + warn!( + entry_id = %hex::encode(activity_id), + error = %e, + "held identity create: its row did not save; written next pass" + ); + return false; + } + let entries = changeset.activity_entries.entry(id).or_default(); + entries.retain(|entry| entry.id != next.id); + entries.push(next); + true +} + +/// Mark every note held identity create `redrive` consumed spent in the in-memory store, and put +/// each onto `changeset` as spent whether or not the store already had it so. A spent-note delta +/// queued earlier, by its result wait or its outcome read, may never have reached the host, and +/// a scan that finds a note already spent emits none: the pass that settles the create's row is +/// the last chance to write them. False, logged, when a note did not mark. +async fn replay_identity_create_spends( + store: &Arc>, + id: SubwalletId, + redrive: &PendingRedrive, + changeset: &mut ShieldedChangeSet, +) -> bool { + { + let mut store = store.write().await; + for nullifier in &redrive.nullifiers { + if let Err(e) = store.mark_spent(id, nullifier) { + warn!( + activity_id = %hex::encode(redrive.activity_id), + error = %e, + "held identity create: a spent note did not mark; settled next pass" + ); + return false; + } + } + } + let spent = changeset.nullifiers_spent.entry(id).or_default(); + for nullifier in &redrive.nullifiers { + if !spent.contains(nullifier) { + spent.push(*nullifier); + } + } + true +} + +// ------------------------------------------------------------------------- +// Internal helpers (free fns) +// ------------------------------------------------------------------------- + +/// Convert `keys`'s default `PaymentAddress` to an `OrchardAddress`. +fn default_orchard_address( + keys: &AccountViewingKeys, +) -> Result { + payment_address_to_orchard(&keys.default_address) +} + +/// Checkpoint depths probed for a Platform-recorded anchor. Kept equal to the +/// commitment tree's `max_checkpoints` retention (the store is opened with +/// `100` — see `PlatformWalletManager`) so the probe reaches every checkpoint +/// the tree still holds and no further: deeper checkpoints are pruned, so +/// probing past this bound is wasted work. Coupled by convention — if that +/// retention changes, update this in lockstep. +const MAX_ANCHOR_PROBE_DEPTH: usize = 100; + +/// Extract `SpendableNote` structs with Merkle witnesses and an anchor +/// Platform has recorded. +/// +/// A shielded spend's proof is accepted only if its anchor is a +/// commitment-tree root Platform recorded (`validate_anchor_exists`). +/// Platform records one anchor per block, but an index-chunk sync routinely +/// leaves the wallet's tree mid-block, so the depth-0 (current) root is +/// frequently a value Platform never recorded — building against it +/// unconditionally is what made such spends fail and never land. +/// +/// This fetches Platform's recorded anchor set (outside the store lock), then +/// selects the shallowest checkpoint depth whose root is in that set — depth 0 +/// being the fully-synced fast path — witnessing every note at that same depth +/// so the anchor and the authentication paths agree (the builder derives the +/// anchor from the witnesses via `MerklePath::root`, so a per-note disagreement +/// would surface downstream as `AnchorMismatch`). When no probed depth has a +/// recorded root it returns the retryable +/// [`PlatformWalletError::ShieldedNoRecordedAnchor`] rather than broadcasting a +/// spend Platform is guaranteed to reject. +/// +/// Returns the proved height of the recorded anchor set the anchor was chosen +/// from too: the anchor was in the set at that height, so a set proved at or +/// above it that lacks the anchor shows it pruned rather than newer. +async fn extract_spends_and_recorded_anchor( + sdk: &Arc, + store: &Arc>, + notes: &[ShieldedNote], +) -> Result<(Vec, Anchor, u64), PlatformWalletError> { + // Nothing selected — fail before the network round-trip. + if notes.is_empty() { + return Err(PlatformWalletError::ShieldedBuildError( + "no spendable notes selected — anchor undefined".to_string(), + )); + } + + // Fetch the recorded anchor set OUTSIDE the store lock so the network + // round-trip doesn't serialize with other store users, and so the lock is + // held only for the mutually-consistent depth/witness probe below. + let (dash_sdk::query_types::ShieldedAnchors(recorded_anchors), metadata) = + dash_sdk::query_types::ShieldedAnchors::fetch_current_with_metadata(sdk).await?; + let recorded: HashSet<[u8; 32]> = recorded_anchors.into_iter().collect(); + + // Hold a single read lock across the whole probe so the checkpoint depths + // and the per-note witnesses stay mutually consistent: a concurrent sync + // checkpointing mid-probe would otherwise shift the depth indices out from + // under us. + let store = store.read().await; + select_recorded_spends(&*store, notes, &recorded) + .map(|(spends, anchor)| (spends, anchor, metadata.height)) +} + +/// Pick the shallowest checkpoint depth whose tree root is in `recorded`, +/// witnessing every note at that depth. Pure (no SDK, no async), so the depth +/// walk can be unit-tested against a real commitment tree. +/// +/// Depth 0 is the current tree state (the fully-synced fast path). Deeper +/// checkpoints are older and hold strictly fewer positions, so the probe stops +/// as soon as a selected note is no longer witnessable at a depth — no deeper +/// checkpoint could contain it. Returns +/// [`PlatformWalletError::ShieldedNoRecordedAnchor`] when no probed depth has a +/// recorded root (a clean, retryable outcome — nothing is broadcast). +fn select_recorded_spends( + store: &S, + notes: &[ShieldedNote], + recorded: &HashSet<[u8; 32]>, +) -> Result<(Vec, Anchor), PlatformWalletError> { + use grovedb_commitment_tree::ExtractedNoteCommitment; + + // Deserialize each note and decode its commitment ONCE — both are + // independent of the checkpoint depth, so hoisting them out of the probe + // keeps the depth walk cheap (each probed depth only re-witnesses). + let prepared: Vec<(u64, grovedb_commitment_tree::Note, ExtractedNoteCommitment)> = notes + .iter() + .map(|note| { + let orchard_note = deserialize_note(¬e.note_data).ok_or_else(|| { + PlatformWalletError::ShieldedBuildError(format!( + "Failed to deserialize note at position {}", + note.position + )) + })?; + let cmx = ExtractedNoteCommitment::from_bytes(¬e.cmx) + .into_option() .ok_or_else(|| { PlatformWalletError::ShieldedBuildError(format!( "invalid stored cmx for note at position {}", @@ -3468,16 +4141,29 @@ async fn mark_notes_spent( wallet_id: WalletId, id: SubwalletId, notes: &[ShieldedNote], +) -> Result<(), PlatformWalletError> { + let nullifiers: Vec<[u8; 32]> = notes.iter().map(|note| note.nullifier).collect(); + mark_nullifiers_spent(store, persister, wallet_id, id, &nullifiers).await +} + +/// [`mark_notes_spent`] for notes known by their nullifiers alone, as a +/// recovery record keeps them. +async fn mark_nullifiers_spent( + store: &Arc>, + persister: Option<&WalletPersister>, + wallet_id: WalletId, + id: SubwalletId, + nullifiers: &[[u8; 32]], ) -> Result<(), PlatformWalletError> { let mut changeset = ShieldedChangeSet::default(); { let mut store = store.write().await; - for note in notes { + for nullifier in nullifiers { if store - .mark_spent(id, ¬e.nullifier) + .mark_spent(id, nullifier) .map_err(|e| PlatformWalletError::ShieldedStoreError(e.to_string()))? { - changeset.record_nullifier_spent(id, note.nullifier); + changeset.record_nullifier_spent(id, *nullifier); } } } @@ -4077,7 +4763,16 @@ pub(crate) async fn arm_redrive_before_broadcast( )), None => None, }; + // An identity create is held for its outcome read from its save, before any dispatch: a + // scan that sees its notes spent while its broadcast is still in flight must not drop it. + let identity_outcome = matches!( + state_transition, + StateTransition::IdentityCreateFromShieldedPool(_) + ) + .then_some(IdentityCreateOutcome::Awaiting); let redrive = PendingRedrive { + outcome_endpoint: None, + identity_outcome, activity_id, anchor, nullifiers, @@ -4425,7 +5120,11 @@ pub(super) async fn redrive_pending_identity_shields( /// /// - accepted / inconclusive → count the attempt; wait for the next scan; /// - `NullifierAlreadySpent` → the original executed; the next scan -/// confirms — touch nothing; +/// confirms — touch nothing. Except an identity create (Type 20) held for +/// its outcome read at a known node ([`PendingRedrive::outcome_endpoint`]): +/// its notes spent do not show that its identity was created, so its +/// outcome is read at its node and kept with its record +/// ([`classify_held_identity_create`]), for the sync pass to settle; /// - any other consensus verdict → inconclusive too: a node refusing the /// re-broadcast does not show that no other node holds the original, /// whose broadcast was ambiguous, so the notes stay reserved; @@ -4437,18 +5136,24 @@ pub(super) async fn redrive_pending_identity_shields( /// may be the only evidence of a payment that can still execute. Its notes /// stay reserved until the spend lands or the prune backstop releases them. /// +/// A held identity create is re-broadcast only to the node it was +/// delivered to, through an SDK scoped to it; one no node was kept for goes +/// to one node, kept with its record once the dispatch may have reached it; +/// and none is re-broadcast once the scan +/// saw its notes spent or its outcome is proven: +/// [`settle_held_identity_creates`] settles it then. +/// /// Runs after the scan's spent-note reconcile (a landed spend's record /// was already dropped by the `mark_spent` hook, so anything still -/// armed here is genuinely unresolved). +/// armed here is genuinely unresolved, or held for its outcome read). pub(super) async fn redrive_pending_spends( sdk: &Arc, store: &Arc>, + persister: Option<&WalletPersister>, id: SubwalletId, recorded: &std::collections::HashSet<[u8; 32]>, cancel: &CancellationToken, ) { - use dpp::serialization::PlatformDeserializableTrusted; - let redrives = match store.read().await.pending_redrives(id) { Ok(r) => r, Err(e) => { @@ -4477,62 +5182,180 @@ pub(super) async fn redrive_pending_spends( if !recorded.contains(&redrive.anchor) || redrive.attempts >= MAX_REDRIVE_ATTEMPTS { continue; } - let st = match StateTransition::deserialize_from_bytes_trusted(&redrive.st_bytes) { - Ok(st) => st, - Err(e) => { - warn!( - error = %e, - "redrive: stored transition failed to deserialize; keeping the record \ - without re-broadcasting it (the prune backstop still frees the notes)" - ); - continue; - } - }; - let Some(attempts) = claim_redrive_attempt(store, id, &redrive).await else { + redrive_one(sdk, store, persister, id, redrive).await; + } +} + +/// Sync-time recovery of `id`'s held identity creates (Type 20) no node was kept for: each is +/// re-broadcast to one node, kept with its record ([`redrive_one`]), so the sync pass can read its +/// outcome there ([`settle_held_identity_creates`]). Scheduled from the record journal on every +/// completed pass, whatever reservations or recorded anchors are left: a scan that saw a create's +/// notes spent cleared its reservations but kept its record. +pub(super) async fn redrive_unkept_identity_creates( + sdk: &Arc, + store: &Arc>, + persister: Option<&WalletPersister>, + id: SubwalletId, + cancel: &CancellationToken, +) { + let redrives = match store.read().await.pending_redrives(id) { + Ok(r) => r, + Err(e) => { + warn!(error = %e, "held identity creates: pending_redrives failed; skipping subwallet"); + return; + } + }; + // No node to send to: nothing is sent, and no attempt is spent. + if sdk.address_list().get_live_address().is_none() { + return; + } + for redrive in redrives { + if cancel.is_cancelled() { + return; + } + if redrive.identity_outcome != Some(IdentityCreateOutcome::Awaiting) + || redrive.outcome_endpoint.is_some() + || redrive.nullifiers.is_empty() + || redrive.attempts >= MAX_REDRIVE_ATTEMPTS + { continue; - }; - let broadcast_result = st.broadcast(sdk, None).await; - let err_display = broadcast_result - .as_ref() - .err() - .map(|e| e.to_string()) - .unwrap_or_default(); - match classify_redrive_broadcast(&broadcast_result) { - RedriveBroadcastOutcome::Accepted => { - info!( - attempts, - max = MAX_REDRIVE_ATTEMPTS, - "redrive: re-broadcast accepted; the next scan detects the landing" - ); - } - RedriveBroadcastOutcome::AlreadyExecuted => { - // Success signal, but the attempt still counts: the scan - // normally confirms the landing and drops the record, and - // if it lags, this arm must not re-broadcast unboundedly - // on every pass. The cap parks the record for the scan / - // prune passes to settle. - info!( - attempts, - max = MAX_REDRIVE_ATTEMPTS, - "redrive: transition already executed on-chain; the next scan confirms \ - the notes spent" - ); + } + redrive_one(sdk, store, persister, id, redrive).await; + } +} + +/// Re-broadcast `redrive`'s stored transition once and classify the answer, for +/// [`redrive_pending_spends`] and [`redrive_unkept_identity_creates`], which choose the records. +async fn redrive_one( + sdk: &Arc, + store: &Arc>, + persister: Option<&WalletPersister>, + id: SubwalletId, + mut redrive: PendingRedrive, +) { + use dpp::serialization::PlatformDeserializableTrusted; + + let st = match StateTransition::deserialize_from_bytes_trusted(&redrive.st_bytes) { + Ok(st) => st, + Err(e) => { + warn!( + error = %e, + "redrive: stored transition failed to deserialize; keeping the record \ + without re-broadcasting it (the prune backstop still frees the notes)" + ); + return; + } + }; + // A held identity create whose outcome is proven is the sync pass's to settle. + if redrive + .identity_outcome + .is_some_and(IdentityCreateOutcome::is_proven) + { + return; + } + // A held identity create goes only to the node that holds it. + let held_at = match redrive.outcome_endpoint.as_deref() { + Some(endpoint) => match endpoint.parse::() { + Ok(endpoint) => Some(endpoint), + Err(e) => { + warn!(error = %e, "redrive: a held record's node does not read; not re-broadcast"); + return; } - RedriveBroadcastOutcome::Inconclusive => { - // `AlreadyExists` (still in a mempool after a lost-ACK - // retry) or transport noise: inconclusive; counts toward - // the cap. - debug!( - attempts, - max = MAX_REDRIVE_ATTEMPTS, - error = %err_display, - "redrive: re-broadcast inconclusive" - ); + }, + None => None, + }; + if held_at.is_some() && notes_seen_spent(store, id, &redrive.nullifiers).await { + return; + } + // A held identity create no node was kept for goes to one node, kept with its record + // once the dispatch may have reached it, so its outcome can be read there. + let unkept_node = match &held_at { + None if redrive.held_for_identity_outcome() => sdk.address_list().get_live_address(), + _ => None, + }; + let broadcaster = match held_at.as_ref().or(unkept_node.as_ref()) { + Some(endpoint) => Arc::new(sdk.scoped_to_address(endpoint.clone())), + None => Arc::clone(sdk), + }; + let Some(attempts) = claim_redrive_attempt(store, id, &redrive).await else { + return; + }; + let broadcast_result = st.broadcast(&broadcaster, None).await; + let held_at = match unkept_node { + Some(endpoint) if !matches!(&broadcast_result, Err(e) if refused_before_sending(e)) => { + keep_outcome_endpoint(store, id, &redrive.activity_id, &endpoint).await; + redrive.outcome_endpoint = Some(endpoint.to_string()); + Some(endpoint) + } + _ => held_at, + }; + let err_display = broadcast_result + .as_ref() + .err() + .map(|e| e.to_string()) + .unwrap_or_default(); + match classify_redrive_broadcast(&broadcast_result) { + RedriveBroadcastOutcome::Accepted => { + info!( + attempts, + max = MAX_REDRIVE_ATTEMPTS, + "redrive: re-broadcast accepted; the next scan detects the landing" + ); + } + RedriveBroadcastOutcome::AlreadyExecuted => { + // A held identity create's notes spent do not show its identity created: + // read its outcome at its node. + if held_at.is_some() + && matches!( + classify_held_identity_create(sdk, store, persister, id, &redrive).await, + HeldIdentityCreate::Proven(_) + ) + { + return; } + // Success signal, but the attempt still counts: the scan + // normally confirms the landing and drops the record, and + // if it lags, this arm must not re-broadcast unboundedly + // on every pass. The cap parks the record for the scan / + // prune passes to settle. + info!( + attempts, + max = MAX_REDRIVE_ATTEMPTS, + "redrive: transition already executed on-chain; the next scan confirms \ + the notes spent" + ); + } + RedriveBroadcastOutcome::Inconclusive => { + // `AlreadyExists` (still in a mempool after a lost-ACK + // retry) or transport noise: inconclusive; counts toward + // the cap. + debug!( + attempts, + max = MAX_REDRIVE_ATTEMPTS, + error = %err_display, + "redrive: re-broadcast inconclusive" + ); } } } +/// Whether every one of `nullifiers` belongs to a note of `id` the store holds spent. +async fn notes_seen_spent( + store: &Arc>, + id: SubwalletId, + nullifiers: &[[u8; 32]], +) -> bool { + let Ok(notes) = store.read().await.get_all_notes(id) else { + return false; + }; + let spent: HashSet<[u8; 32]> = notes + .into_iter() + .filter(|note| note.is_spent) + .map(|note| note.nullifier) + .collect(); + !nullifiers.is_empty() && nullifiers.iter().all(|nullifier| spent.contains(nullifier)) +} + /// How [`broadcast_shielded_spend_classified`] waits for an already-broadcast /// spend's result, mirroring the verifier's classification of the family. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -4873,6 +5696,8 @@ mod redrive_tests { let id = SubwalletId::new(wallet_id, 3); let mut store = InMemoryShieldedStore::new(); let archived = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [6; 32], anchor: [1; 32], nullifiers: vec![], @@ -4926,6 +5751,8 @@ mod redrive_tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id, anchor: [1; 32], nullifiers: vec![], @@ -4997,6 +5824,8 @@ mod redrive_tests { .arm_redrive( SubwalletId::new(wallet_id, 3), PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [6; 32], anchor: [1; 32], nullifiers: vec![], @@ -5028,6 +5857,8 @@ mod redrive_tests { .arm_redrive( SubwalletId::new(wallet_id, 0), PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [8; 32], anchor: [1; 32], nullifiers: vec![], @@ -5140,6 +5971,8 @@ mod redrive_tests { .arm_redrive( previous_account, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id, anchor: [0xAB; 32], nullifiers: vec![], @@ -5515,6 +6348,8 @@ mod redrive_tests { held.arm_private_contact_payment( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x79; 32], anchor: [7; 32], nullifiers: vec![note.nullifier], @@ -6196,6 +7031,8 @@ mod redrive_tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id, anchor, nullifiers: vec![], @@ -6211,7 +7048,7 @@ mod redrive_tests { .expect("arm identity guard"); let recorded = [anchor].into_iter().collect(); - redrive_pending_spends(&sdk, &store, id, &recorded, &CancellationToken::new()).await; + redrive_pending_spends(&sdk, &store, None, id, &recorded, &CancellationToken::new()).await; let retained = store.read().await.pending_redrives(id).expect("guard"); assert_eq!(retained.len(), 1); @@ -6235,6 +7072,8 @@ mod redrive_tests { let id = SubwalletId::new(wallet_id, 0); let rec = |activity: u8, anchor: u8, attempts: u32| PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [activity; 32], anchor: [anchor; 32], nullifiers: vec![[activity ^ 0xFF; 32]], @@ -6259,7 +7098,7 @@ mod redrive_tests { let recorded: std::collections::HashSet<[u8; 32]> = [[11u8; 32], [12u8; 32]].into_iter().collect(); - redrive_pending_spends(&sdk, &store, id, &recorded, &CancellationToken::new()).await; + redrive_pending_spends(&sdk, &store, None, id, &recorded, &CancellationToken::new()).await; let left = store.read().await.pending_redrives(id).unwrap(); let ids: Vec<[u8; 32]> = left.iter().map(|r| r.activity_id).collect(); @@ -6295,6 +7134,8 @@ mod redrive_tests { let earlier = identity_shield_transition([0xA1; 32], 3); let retired = identity_shield_transition([0xA2; 32], 4); let record = |activity: u8, transition: &StateTransition| PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [activity; 32], anchor, nullifiers: vec![[activity ^ 0xFF; 32]], @@ -6318,6 +7159,7 @@ mod redrive_tests { let mut pass = Box::pin(redrive_pending_spends( &sdk_handle, &store, + None, id, &recorded, &cancel, @@ -6399,7 +7241,7 @@ mod redrive_tests { /// The note-consuming spends, by the code that saves, broadcasts and /// waits for them. - #[derive(Clone, Copy, Debug)] + #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum SpendPath { /// Transfer, unshield, withdrawal and identity top-up /// ([`broadcast_shielded_spend_with_redrive_with_metadata`]): here a @@ -6491,7 +7333,7 @@ mod redrive_tests { store, reservation, entry, - .. + notes, } = spend; match self { Self::Shared => { @@ -6507,20 +7349,20 @@ mod redrive_tests { ) .await } - Self::IdentityCreate => identity_create_failure( - settle_failed_spend( + Self::IdentityCreate => { + settle_failed_identity_create( &store, None, wallet_id, id, &entry, reservation, - IDENTITY_CREATE_OPERATION, + ¬es, failure, + Some(Identifier::from(CREATED_IDENTITY)), ) - .await, - Some(Identifier::from(CREATED_IDENTITY)), - ), + .await + } } } @@ -6709,6 +7551,7 @@ mod redrive_tests { redrive_pending_spends( &sdk_handle, &restarted, + None, id, &recorded, &CancellationToken::new(), @@ -6927,6 +7770,7 @@ mod redrive_tests { redrive_pending_spends( &sdk_handle, &restarted, + None, id, &recorded, &CancellationToken::new(), @@ -6939,17 +7783,28 @@ mod redrive_tests { assert!(restarted .write() .await - .mark_spent(id, &spend_note().nullifier) - .expect("marked")); - assert!( - restarted - .read() - .await - .pending_redrives(id) - .expect("records") - .is_empty(), - "{spend_path:?}: resolved once its nullifier is seen spent" - ); + .mark_spent(id, &spend_note().nullifier) + .expect("marked")); + let records = restarted + .read() + .await + .pending_redrives(id) + .expect("records"); + if spend_path == SpendPath::IdentityCreate { + // Its notes spent do not show whether its identity was created: held for the + // outcome read from its save. + assert_eq!(records.len(), 1, "{spend_path:?}: held"); + assert_eq!( + records[0].identity_outcome, + Some(IdentityCreateOutcome::Awaiting), + "{spend_path:?}" + ); + } else { + assert!( + records.is_empty(), + "{spend_path:?}: resolved once its nullifier is seen spent" + ); + } } } @@ -7321,150 +8176,762 @@ mod redrive_tests { } } - /// A verified result that does not read the shield's inputs spent shows - /// the shield had not executed at its block, whatever the endpoint that - /// returned it claims: a result of another kind, one that leaves an input - /// out or proves no state for it, and an input still below the nonce the - /// shield signed leave it unconfirmed, its row pending. - #[tokio::test] - async fn should_report_a_shield_unconfirmed_when_its_result_does_not_read_its_inputs_spent() { - let wallet_id = [0x7A; 32]; + /// A verified result that does not read the shield's inputs spent shows + /// the shield had not executed at its block, whatever the endpoint that + /// returned it claims: a result of another kind, one that leaves an input + /// out or proves no state for it, and an input still below the nonce the + /// shield signed leave it unconfirmed, its row pending. + #[tokio::test] + async fn should_report_a_shield_unconfirmed_when_its_result_does_not_read_its_inputs_spent() { + let wallet_id = [0x7A; 32]; + let id = SubwalletId::new(wallet_id, 0); + let activity_id = [0x83; 32]; + let cases = [ + ( + "a result of another kind", + StateTransitionProofResult::VerifiedTokenBalanceAbsence(Identifier::from([1; 32])), + ), + ( + "an input left out", + StateTransitionProofResult::VerifiedAddressInfos(BTreeMap::from([( + PlatformAddress::P2pkh([8; 20]), + Some((SHIELD_NONCE, 1_000_000)), + )])), + ), + ("an input with no state", input_snapshot(None)), + ( + "an input below its signed nonce", + input_snapshot(Some(SHIELD_NONCE - 1)), + ), + ]; + for (case, snapshot) in cases { + let (store, entry) = recorded_shield(id, activity_id).await; + let settled = settle_shield( + &store, + None, + wallet_id, + id, + &entry, + &shield_inputs(), + BroadcastDelivery::Admitted, + |e: &dash_sdk::Error| e.to_string(), + || std::future::ready(Ok(snapshot)), + ) + .await; + assert!( + matches!( + settled, + Err(PlatformWalletError::ShieldedSpendUnconfirmed { + operation: "shield", + .. + }) + ), + "{case}: {settled:?}" + ); + assert_eq!( + shield_status(&store, id, &activity_id).await, + ShieldedActivityStatus::Pending, + "{case}" + ); + } + + // A transition that names no input shows nothing spent. + let (store, entry) = recorded_shield(id, activity_id).await; + let settled = settle_shield( + &store, + None, + wallet_id, + id, + &entry, + &BTreeMap::new(), + BroadcastDelivery::Admitted, + |e: &dash_sdk::Error| e.to_string(), + || std::future::ready(Ok(input_snapshot(Some(SHIELD_NONCE)))), + ) + .await; + assert!( + matches!( + settled, + Err(PlatformWalletError::ShieldedSpendUnconfirmed { .. }) + ), + "no input: {settled:?}" + ); + } + + /// The broadcast of a signed shield through a network SDK: a node that + /// refuses the connection leaves it unconfirmed and its row pending, as a + /// transport failure cannot show that nothing was sent, and an SDK with + /// no address to send to leaves it failed. + #[tokio::test] + async fn should_report_a_shield_unconfirmed_when_its_node_refuses_the_connection() { + let wallet_id = [0x79; 32]; + let id = SubwalletId::new(wallet_id, 0); + let activity_id = [0x82; 32]; + let transition = shield_transition(); + + let (store, entry) = recorded_shield(id, activity_id).await; + let (refusing, _port) = sdk_refusing_connections(); + let refused = broadcast_shield( + &Arc::new(refusing), + &store, + None, + wallet_id, + id, + &entry, + &transition, + |e: &dash_sdk::Error| e.to_string(), + ) + .await; + assert!( + matches!( + refused, + Err(PlatformWalletError::ShieldedSpendUnconfirmed { + operation: "shield", + .. + }) + ), + "{refused:?}" + ); + assert_eq!( + shield_status(&store, id, &activity_id).await, + ShieldedActivityStatus::Pending + ); + + let (store, entry) = recorded_shield(id, activity_id).await; + let never_sent = broadcast_shield( + &Arc::new(sdk_without_addresses()), + &store, + None, + wallet_id, + id, + &entry, + &transition, + |e: &dash_sdk::Error| e.to_string(), + ) + .await; + assert!( + matches!( + never_sent, + Err(PlatformWalletError::ShieldedBroadcastFailed(_)) + ), + "{never_sent:?}" + ); + assert_eq!( + shield_status(&store, id, &activity_id).await, + ShieldedActivityStatus::Failed + ); + } + + // ── Identity create: inputs consumed without the identity ────────── + + /// The identity a mock read returns for the tests' identity create. + fn created_identity() -> Identity { + Identity::new_with_id_and_keys( + Identifier::from(CREATED_IDENTITY), + BTreeMap::new(), + PlatformVersion::latest(), + ) + .expect("an identity") + } + + /// An outcome read the SDK could not prove. + fn unproved() -> dash_sdk::Error { + dash_sdk::Error::Generic("no proof".to_string()) + } + + /// The identity create is settled as unable to execute only when every + /// nullifier is spent and the identity is proven absent at or above the + /// height they were spent by. An identity found means it executed, + /// whatever the outcome read says; anything else proves nothing. + #[test] + fn should_judge_an_identity_create_consumed_only_when_its_notes_are_spent_and_its_identity_absent( + ) { + let spent_by = |height| { + Ok(TransitionOutcome::ExecutedOrSuperseded { + consumed_by_height: height, + }) + }; + let absent_at = |height| Ok((None, height)); + + assert!(matches!( + judge_identity_create_reads(spent_by(10), absent_at(10)), + IdentityCreateProbe::InputsConsumed { + consumed_by_height: 10 + } + )); + assert!(matches!( + judge_identity_create_reads(spent_by(10), absent_at(12)), + IdentityCreateProbe::InputsConsumed { + consumed_by_height: 10 + } + )); + assert!( + matches!( + judge_identity_create_reads(spent_by(10), absent_at(9)), + IdentityCreateProbe::Unknown + ), + "an identity read older than the spend proves nothing" + ); + assert!( + matches!( + judge_identity_create_reads(spent_by(10), Err(unproved())), + IdentityCreateProbe::Unknown + ), + "an identity read that failed proves nothing" + ); + let still_possible = Ok(TransitionOutcome::StillPossible { + checked_at_height: 10, + anchors_recorded_at: None, + until_height: None, + }); + assert!( + matches!( + judge_identity_create_reads(still_possible, absent_at(10)), + IdentityCreateProbe::Unknown + ), + "unspent notes: it may still execute" + ); + assert!( + matches!( + judge_identity_create_reads( + Ok(TransitionOutcome::CannotExecute( + dash_sdk::platform::transition::outcome::CannotExecuteReason::InputUsed + )), + absent_at(10) + ), + IdentityCreateProbe::Unknown + ), + "a note left free: not this outcome" + ); + assert!( + matches!( + judge_identity_create_reads(Err(unproved()), absent_at(10)), + IdentityCreateProbe::Unknown + ), + "no proven outcome" + ); + assert!(matches!( + judge_identity_create_reads(spent_by(10), Ok((Some(created_identity()), 10))), + IdentityCreateProbe::Created(_) + )); + assert!(matches!( + judge_identity_create_reads(Err(unproved()), Ok((Some(created_identity()), 3))), + IdentityCreateProbe::Created(_) + )); + } + + /// The nullifier of the explicit dummy spend that pads the tests' + /// identity create to the two actions a bundle carries at least. + const DUMMY_NULLIFIER: [u8; 32] = [0xD0; 32]; + + /// The node the tests' identity creates are delivered to. + const ENDPOINT: &str = "http://127.0.0.1:3010"; + + /// An identity create spending exactly `notes`: one action per note, + /// carrying its nullifier, padded with one dummy spend to two actions. + fn identity_create_spending(notes: &[ShieldedNote]) -> StateTransition { + let mut actions: Vec = notes + .iter() + .map(|note| SerializedAction { + nullifier: note.nullifier, + ..spend_action() + }) + .collect(); + while actions.len() < 2 { + actions.push(SerializedAction { + nullifier: DUMMY_NULLIFIER, + ..spend_action() + }); + } + IdentityCreateFromShieldedPoolTransitionV0 { + public_keys: vec![], + denomination: 1_000_000, + actions, + anchor: SPEND_ANCHOR, + proof: vec![8; 64], + binding_signature: [9; 64], + send_to_address_on_creation_failure: PlatformAddress::P2pkh([7; 20]), + identity_id: Identifier::from(CREATED_IDENTITY), + } + .into() + } + + /// The nullifiers `transition`'s actions carry, as its outcome read names them. + fn transition_nullifiers(transition: &StateTransition) -> Vec<[u8; 32]> { + shielded_actions(transition) + .iter() + .map(|action| action.nullifier) + .collect() + } + + /// A mock SDK whose node `ENDPOINT` is in its address list, and whose + /// proven reads show `transition`'s anchor recorded, its nullifiers + /// `spent` or not, and its identity absent, or `created`. Every answer of + /// a mock SDK is at height 0. + async fn sdk_reading( + transition: &StateTransition, + spent: bool, + created: bool, + ) -> dash_sdk::Sdk { + use dash_sdk::query_types::{ + MostRecentShieldedAnchor, NoParamQuery, ShieldedAnchors, ShieldedNullifierStatus, + ShieldedNullifierStatuses, ShieldedNullifiersQuery, + }; + + let mut sdk = dash_sdk::SdkBuilder::new_mock() + .with_version(PlatformVersion::latest()) + .build() + .expect("mock sdk"); + let mut addresses = sdk.address_list().clone(); + addresses.add(ENDPOINT.parse().expect("an address")); + sdk.mock() + .expect_fetch(NoParamQuery {}, Some(ShieldedAnchors(vec![SPEND_ANCHOR]))) + .await + .expect("the anchor set"); + sdk.mock() + .expect_fetch( + NoParamQuery {}, + Some(MostRecentShieldedAnchor(SPEND_ANCHOR)), + ) + .await + .expect("the newest anchor"); + let nullifiers = transition_nullifiers(transition); + let statuses = nullifiers + .iter() + .map(|nullifier| ShieldedNullifierStatus { + nullifier: *nullifier, + is_spent: spent, + }) + .collect(); + sdk.mock() + .expect_fetch( + ShieldedNullifiersQuery(nullifiers), + Some(ShieldedNullifierStatuses(statuses)), + ) + .await + .expect("the nullifier statuses"); + sdk.mock() + .expect_fetch( + Identifier::from(CREATED_IDENTITY), + created.then(created_identity), + ) + .await + .expect("the identity"); + sdk + } + + /// The probe reads the create's outcome and its identity with proofs: its + /// nullifiers spent and its identity absent is inputs consumed, its + /// identity found is created, its nullifiers free is not settled, and + /// reads that fail prove nothing. + #[tokio::test] + async fn should_probe_an_identity_create_by_its_proven_nullifiers_and_identity() { + let transition = identity_create_spending(&[spend_note()]); + let identity_id = Identifier::from(CREATED_IDENTITY); + + let spent = sdk_reading(&transition, true, false).await; + assert!(matches!( + probe_identity_create(&spent, &transition, identity_id).await, + IdentityCreateProbe::InputsConsumed { + consumed_by_height: 0 + } + )); + let created = sdk_reading(&transition, true, true).await; + assert!(matches!( + probe_identity_create(&created, &transition, identity_id).await, + IdentityCreateProbe::Created(_) + )); + let free = sdk_reading(&transition, false, false).await; + assert!(matches!( + probe_identity_create(&free, &transition, identity_id).await, + IdentityCreateProbe::Unknown + )); + let unanswered = dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk"); + assert!(matches!( + probe_identity_create(&unanswered, &transition, identity_id).await, + IdentityCreateProbe::Unknown + )); + } + + /// The notes of `id` the store at `store` holds spent, by nullifier. + async fn spent_nullifiers( + store: &Arc>, + id: SubwalletId, + ) -> Vec<[u8; 32]> { + store + .read() + .await + .get_all_notes(id) + .expect("notes") + .into_iter() + .filter(|note| note.is_spent) + .map(|note| note.nullifier) + .collect() + } + + /// The activity row `activity_id` of `id`. + async fn activity_row( + store: &Arc>, + id: SubwalletId, + activity_id: &[u8; 32], + ) -> ShieldedActivityEntry { + store + .read() + .await + .get_activity_by_entry_id(id, activity_id) + .expect("the row reads") + .expect("the row") + } + + /// The identity create, delivered to a node whose result wait fails, with + /// its identity not found: the node is kept with its record, and the + /// outcome is read there. Its notes spent and its identity absent report + /// inputs consumed: the notes it spends, exactly the real inputs the + /// proof read, are marked spent, the record is retired for good and the + /// row reads failed at the height the notes were spent by. With no node + /// to read at (no address to pin), nothing is read: it stays unconfirmed. + #[tokio::test(start_paused = true)] + async fn should_settle_a_delivered_identity_create_whose_notes_are_spent_without_its_identity() + { + use dash_sdk::query_types::ShieldedNullifiersQuery; + + let wallet_id = [0x74; 32]; let id = SubwalletId::new(wallet_id, 0); - let activity_id = [0x83; 32]; - let cases = [ - ( - "a result of another kind", - StateTransitionProofResult::VerifiedTokenBalanceAbsence(Identifier::from([1; 32])), - ), - ( - "an input left out", - StateTransitionProofResult::VerifiedAddressInfos(BTreeMap::from([( - PlatformAddress::P2pkh([8; 20]), - Some((SHIELD_NONCE, 1_000_000)), - )])), - ), - ("an input with no state", input_snapshot(None)), - ( - "an input below its signed nonce", - input_snapshot(Some(SHIELD_NONCE - 1)), - ), - ]; - for (case, snapshot) in cases { - let (store, entry) = recorded_shield(id, activity_id).await; - let settled = settle_shield( - &store, - None, - wallet_id, + let activity_id = [0x7D; 32]; + for pinned in [true, false] { + let dir = tempfile::tempdir().expect("a directory"); + let path = dir.path().join("tree.sqlite"); + let mut spend = ReservedSpend::at(&path, id, activity_id).await; + let transition = identity_create_spending(&spend.notes); + let mut sdk = if pinned { + sdk_reading(&transition, true, false).await + } else { + dash_sdk::SdkBuilder::new_mock() + .with_version(PlatformVersion::latest()) + .build() + .expect("mock sdk") + }; + let sdk_handle = Arc::new(sdk.clone()); + let failure = broadcast_identity_create_with_redrive( + &sdk_handle, + &spend.store, id, - &entry, - &shield_inputs(), - BroadcastDelivery::Admitted, - |e: &dash_sdk::Error| e.to_string(), - || std::future::ready(Ok(snapshot)), + &spend.entry, + SPEND_ANCHOR, + SPEND_ANCHOR_HEIGHT, + &spend.notes, + &mut spend.reservation, + &transition, + Identifier::from(CREATED_IDENTITY), + BTreeMap::new(), ) - .await; + .await + .expect_err("the create did not execute"); + let outcome_read: dash_sdk::dapi_grpc::platform::v0::GetShieldedNullifiersRequest = + dash_sdk::platform::Query::query( + &ShieldedNullifiersQuery(transition_nullifiers(&transition)), + &sdk.query_settings(), + ) + .expect("a request"); + let reads = sdk.mock().request_count(&outcome_read).await; + let notes = spend.notes.clone(); + let store = Arc::clone(&spend.store); + let settled = SpendPath::IdentityCreate + .settle(spend, wallet_id, id, failure) + .await; + if !pinned { + assert_eq!(reads, 0, "no node holds it, so nothing is read"); + assert!( + SpendPath::IdentityCreate.reads_unconfirmed(&settled), + "{settled:?}" + ); + continue; + } + assert!(reads > 0, "the outcome is read at the node"); assert!( matches!( settled, - Err(PlatformWalletError::ShieldedSpendUnconfirmed { - operation: "shield", + PlatformWalletError::ShieldedIdentityCreateInputsConsumed { + identity_id, + consumed_by_height: 0, .. - }) + } if identity_id == Identifier::from(CREATED_IDENTITY) ), - "{case}: {settled:?}" + "{settled:?}" ); + let real_inputs: Vec<[u8; 32]> = notes.iter().map(|note| note.nullifier).collect(); + let queried = transition_nullifiers(&transition); + assert!(real_inputs + .iter() + .all(|nullifier| queried.contains(nullifier))); + assert_eq!(spent_nullifiers(&store, id).await, real_inputs); + let row = activity_row(&store, id, &activity_id).await; + assert_eq!(row.status, ShieldedActivityStatus::InputsConsumed); + assert_eq!(row.block_height, Some(0)); + drop(store); + // The record stays, with the outcome, until the sync pass's write + // of the row is durable. + let records = FileBackedShieldedStore::open_path(&path, 100) + .expect("reopened") + .pending_redrives(id) + .expect("records"); + assert_eq!(records.len(), 1); assert_eq!( - shield_status(&store, id, &activity_id).await, - ShieldedActivityStatus::Pending, - "{case}" + records[0].identity_outcome, + Some(IdentityCreateOutcome::InputsConsumed { + consumed_by_height: 0 + }) ); } - - // A transition that names no input shows nothing spent. - let (store, entry) = recorded_shield(id, activity_id).await; - let settled = settle_shield( - &store, - None, - wallet_id, - id, - &entry, - &BTreeMap::new(), - BroadcastDelivery::Admitted, - |e: &dash_sdk::Error| e.to_string(), - || std::future::ready(Ok(input_snapshot(Some(SHIELD_NONCE)))), - ) - .await; - assert!( - matches!( - settled, - Err(PlatformWalletError::ShieldedSpendUnconfirmed { .. }) - ), - "no input: {settled:?}" - ); } - /// The broadcast of a signed shield through a network SDK: a node that - /// refuses the connection leaves it unconfirmed and its row pending, as a - /// transport failure cannot show that nothing was sent, and an SDK with - /// no address to send to leaves it failed. + /// The result wait proves an identity create executed: its note is marked spent and its row + /// confirmed, but its record is kept, with its outcome, across a restart too, until the sync + /// pass has the host hold both durably. #[tokio::test] - async fn should_report_a_shield_unconfirmed_when_its_node_refuses_the_connection() { - let wallet_id = [0x79; 32]; + async fn should_keep_a_created_identity_create_s_record_until_its_settlement_is_durable() { + let wallet_id = [0x78; 32]; let id = SubwalletId::new(wallet_id, 0); - let activity_id = [0x82; 32]; - let transition = shield_transition(); - - let (store, entry) = recorded_shield(id, activity_id).await; - let (refusing, _port) = sdk_refusing_connections(); - let refused = broadcast_shield( - &Arc::new(refusing), - &store, - None, - wallet_id, + let activity_id = [0x81; 32]; + let dir = tempfile::tempdir().expect("a directory"); + let path = dir.path().join("tree.sqlite"); + let mut spend = ReservedSpend::at(&path, id, activity_id).await; + let transition = identity_create_spending(&spend.notes); + save_spend_then_broadcast( + &spend.store, id, - &entry, + &spend.entry, + SPEND_ANCHOR, + SPEND_ANCHOR_HEIGHT, + &spend.notes, + &mut spend.reservation, &transition, - |e: &dash_sdk::Error| e.to_string(), + IDENTITY_CREATE_OPERATION, + || async { Ok::<(), SpendFailure>(()) }, ) - .await; - assert!( - matches!( - refused, - Err(PlatformWalletError::ShieldedSpendUnconfirmed { - operation: "shield", - .. - }) - ), - "{refused:?}" + .await + .unwrap_or_else(|_| panic!("dispatched")); + let ReservedSpend { + store, + reservation, + entry, + notes, + } = spend; + + settle_created_identity_create(&store, None, wallet_id, id, &entry, reservation, ¬es) + .await; + + assert_eq!( + spent_nullifiers(&store, id).await, + vec![spend_note().nullifier] ); assert_eq!( - shield_status(&store, id, &activity_id).await, - ShieldedActivityStatus::Pending + activity_row(&store, id, &activity_id).await.status, + ShieldedActivityStatus::Confirmed + ); + drop(store); + let records = FileBackedShieldedStore::open_path(&path, 100) + .expect("reopened") + .pending_redrives(id) + .expect("records"); + assert_eq!(records.len(), 1, "kept for the sync pass to settle"); + assert_eq!( + records[0].identity_outcome, + Some(IdentityCreateOutcome::Created) ); + } - let (store, entry) = recorded_shield(id, activity_id).await; - let never_sent = broadcast_shield( - &Arc::new(sdk_without_addresses()), - &store, - None, - wallet_id, + /// An identity create whose saved record is the only thing left of it + /// after a result wait failed, held for its outcome read at `ENDPOINT` + /// when `held`: its note reserved and its activity row pending, in the + /// store at `path`, with the transition it spends its note in. + async fn saved_identity_create( + path: &std::path::Path, + id: SubwalletId, + activity_id: [u8; 32], + held: bool, + ) -> (Arc>, StateTransition) { + let mut spend = ReservedSpend::at(path, id, activity_id).await; + let transition = identity_create_spending(&spend.notes); + let failure = save_spend_then_broadcast( + &spend.store, id, - &entry, + &spend.entry, + SPEND_ANCHOR, + SPEND_ANCHOR_HEIGHT, + &spend.notes, + &mut spend.reservation, &transition, - |e: &dash_sdk::Error| e.to_string(), + IDENTITY_CREATE_OPERATION, + || async { + Err::<(), _>(SpendFailure::from( + PlatformWalletError::ShieldedSpendUnconfirmed { + operation: IDENTITY_CREATE_OPERATION, + reason: "the result wait timed out".to_string(), + }, + )) + }, ) - .await; - assert!( - matches!( - never_sent, - Err(PlatformWalletError::ShieldedBroadcastFailed(_)) - ), - "{never_sent:?}" - ); - assert_eq!( - shield_status(&store, id, &activity_id).await, - ShieldedActivityStatus::Failed - ); + .await + .expect_err("the create is unconfirmed"); + if held { + assert!(spend + .store + .write() + .await + .set_redrive_outcome_endpoint(id, &activity_id, ENDPOINT) + .expect("kept")); + } + let store = Arc::clone(&spend.store); + let _ = SpendPath::IdentityCreate + .settle(spend, wallet_id_of(id), id, failure) + .await; + (store, transition) + } + + fn wallet_id_of(id: SubwalletId) -> WalletId { + id.wallet_id + } + + /// A held identity create is read at its node: its notes spent and its + /// identity absent keeps inputs consumed with its record (exactly its + /// real inputs marked spent), its identity found keeps created, and its + /// notes free leave it. Its row is the sync pass's to write. One with no + /// node is never read. + #[tokio::test] + async fn should_classify_a_held_identity_create_only_at_its_node() { + let wallet_id = [0x75; 32]; + let id = SubwalletId::new(wallet_id, 0); + let activity_id = [0x7E; 32]; + for (held, spent, created) in [ + (true, true, false), + (true, true, true), + (true, false, false), + (false, true, false), + ] { + let case = format!("held {held}, spent {spent}, created {created}"); + let dir = tempfile::tempdir().expect("a directory"); + let (store, transition) = + saved_identity_create(&dir.path().join("tree.sqlite"), id, activity_id, held).await; + let redrive = store.read().await.pending_redrives(id).expect("records")[0].clone(); + assert_eq!(redrive.outcome_endpoint.is_some(), held, "{case}"); + let sdk = sdk_reading(&transition, spent, created).await; + + let classified = classify_held_identity_create(&sdk, &store, None, id, &redrive).await; + let records = store.read().await.pending_redrives(id).expect("records"); + let row = activity_row(&store, id, &activity_id).await; + match (held, spent, created) { + (true, true, false) => { + let outcome = IdentityCreateOutcome::InputsConsumed { + consumed_by_height: 0, + }; + assert_eq!(classified, HeldIdentityCreate::Proven(outcome), "{case}"); + assert_eq!(records[0].identity_outcome, Some(outcome), "{case}"); + assert_eq!(row.status, ShieldedActivityStatus::Pending, "{case}"); + assert_eq!(spent_nullifiers(&store, id).await, redrive.nullifiers); + assert!(redrive + .nullifiers + .iter() + .all(|n| transition_nullifiers(&transition).contains(n))); + } + (true, true, true) => { + let outcome = IdentityCreateOutcome::Created; + assert_eq!(classified, HeldIdentityCreate::Proven(outcome), "{case}"); + assert_eq!(records[0].identity_outcome, Some(outcome), "{case}"); + assert_eq!(row.status, ShieldedActivityStatus::Pending, "{case}"); + assert_eq!(spent_nullifiers(&store, id).await, redrive.nullifiers); + } + _ => { + assert_eq!(classified, HeldIdentityCreate::Unknown, "{case}"); + assert_eq!( + records[0].identity_outcome, + Some(IdentityCreateOutcome::Awaiting), + "{case}" + ); + assert_eq!(row.status, ShieldedActivityStatus::Pending, "{case}"); + } + } + } + } + + /// An identity create is held from its save, before any node is known + /// for it: it keeps its record when its notes are marked spent, as a + /// scan marks them, whether or not its node was kept by then, as its + /// notes spent do not show what it did. Both survive a restart. + #[tokio::test] + async fn should_keep_an_identity_create_s_record_from_its_save_when_its_notes_are_spent() { + let wallet_id = [0x76; 32]; + let id = SubwalletId::new(wallet_id, 0); + let activity_id = [0x7F; 32]; + for held in [true, false] { + let dir = tempfile::tempdir().expect("a directory"); + let path = dir.path().join("tree.sqlite"); + let (store, _) = saved_identity_create(&path, id, activity_id, held).await; + assert!(store + .write() + .await + .mark_spent(id, &spend_note().nullifier) + .expect("marked")); + drop(store); + let records = FileBackedShieldedStore::open_path(&path, 100) + .expect("reopened") + .pending_redrives(id) + .expect("records"); + assert_eq!(records.len(), 1, "held {held}"); + assert_eq!( + records[0].identity_outcome, + Some(IdentityCreateOutcome::Awaiting), + "held {held}" + ); + assert_eq!( + records[0].outcome_endpoint.as_deref(), + held.then_some(ENDPOINT), + "held {held}" + ); + } + } + + /// A held identity create whose node was never kept (its process stopped before its first + /// dispatch's node was kept) is re-broadcast to one node, kept with its record for its + /// outcome read, across a restart too. With no node to send to, nothing is sent and no node + /// is kept. + #[tokio::test(start_paused = true)] + async fn should_keep_the_node_a_redrive_of_a_create_with_no_kept_node_reached() { + let wallet_id = [0x77; 32]; + let id = SubwalletId::new(wallet_id, 0); + let activity_id = [0x80; 32]; + for reachable in [true, false] { + let sdk = Arc::new(if reachable { + let sdk = dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk"); + let mut addresses = sdk.address_list().clone(); + addresses.add(ENDPOINT.parse().expect("an address")); + sdk + } else { + sdk_without_addresses() + }); + let dir = tempfile::tempdir().expect("a directory"); + let path = dir.path().join("tree.sqlite"); + let (store, _) = saved_identity_create(&path, id, activity_id, false).await; + let recorded = [SPEND_ANCHOR].into_iter().collect(); + redrive_pending_spends(&sdk, &store, None, id, &recorded, &CancellationToken::new()) + .await; + drop(store); + let records = FileBackedShieldedStore::open_path(&path, 100) + .expect("reopened") + .pending_redrives(id) + .expect("records"); + assert_eq!(records.len(), 1, "reachable {reachable}"); + assert_eq!( + records[0].identity_outcome, + Some(IdentityCreateOutcome::Awaiting), + "reachable {reachable}" + ); + assert_eq!( + records[0].outcome_endpoint.is_some(), + reachable, + "reachable {reachable}" + ); + } } } diff --git a/packages/rs-platform-wallet/src/wallet/shielded/store.rs b/packages/rs-platform-wallet/src/wallet/shielded/store.rs index b2672e56d00..db67312181c 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/store.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/store.rs @@ -185,6 +185,55 @@ pub struct PendingRedrive { /// automatic retries and permit separately authorized new payments. /// This is never a proof of failure and is always false for note spends. pub identity_user_abandoned: bool, + /// The DAPI node an identity create from the pool (Type 20) was + /// delivered to, when one dispatch reached a node: the only node its + /// outcome may be read from, since the reads name its nullifiers. + /// Keeping one also holds the record + /// ([`ShieldedStore::set_redrive_outcome_endpoint`]). `None` for every + /// other spend, and for a create whose delivery no node was kept for. + pub outcome_endpoint: Option, + /// Where an identity create from the pool (Type 20) stands in its hold + /// for the outcome read. A record that has one is held: seeing its notes + /// spent ([`ShieldedStore::mark_spent`]) does not drop it, as its notes + /// spent do not show whether the identity was created. A create's record + /// is held from the moment it is saved, before any dispatch, as + /// [`IdentityCreateOutcome::Awaiting`]; a proven outcome is kept here + /// durably until the activity row that reports it is durable too, then + /// the record is dropped. `None` for every other spend, and for a create + /// saved by a build that did not hold creates. + pub identity_outcome: Option, +} + +impl PendingRedrive { + /// Whether the record is held for an identity create's outcome read + /// ([`Self::identity_outcome`]). + pub fn held_for_identity_outcome(&self) -> bool { + self.identity_outcome.is_some() + } +} + +/// Where a held identity create from the pool (Type 20) stands +/// ([`PendingRedrive::identity_outcome`]). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum IdentityCreateOutcome { + /// No outcome is proven yet. + Awaiting, + /// Proven executed: its identity exists. + Created, + /// Proven unable to execute: every nullifier is spent by + /// `consumed_by_height` and its identity is absent at a height at or above + /// it. + InputsConsumed { + /// A height by which its notes were spent. + consumed_by_height: u64, + }, +} + +impl IdentityCreateOutcome { + /// Whether an outcome is proven. + pub fn is_proven(self) -> bool { + !matches!(self, Self::Awaiting) + } } /// The DashPay private contact request a saved spend pays for: which identity @@ -481,6 +530,15 @@ pub(super) struct MarkSpentOutcome { /// /// A store is `'static`: a spend dropped before it hands its notes over /// releases their reservation on a spawned task when the store is locked. +/// +/// An implementation outside this crate must also keep the identity-create +/// hold: persist [`PendingRedrive::outcome_endpoint`] and +/// [`PendingRedrive::identity_outcome`] with the record and restore both, +/// implement [`Self::set_redrive_outcome_endpoint`] and +/// [`Self::set_redrive_identity_outcome`] durably, and keep a held record +/// ([`PendingRedrive::held_for_identity_outcome`]) when [`Self::mark_spent`] +/// sees one of its notes spent. Dropping it there lets a scan confirm a +/// failed create from its change outputs. pub trait ShieldedStore: Send + Sync + 'static { /// The error type returned by storage operations. type Error: StdError + Send + Sync + 'static; @@ -502,7 +560,9 @@ pub trait ShieldedStore: Send + Sync + 'static { fn get_all_notes(&self, id: SubwalletId) -> Result, Self::Error>; /// Mark `id`'s note with `nullifier` as spent. Returns `true` - /// if a matching unspent note was found. + /// if a matching unspent note was found. Drops every redrive record + /// that carries `nullifier` except one held for its identity create's + /// outcome read ([`PendingRedrive::held_for_identity_outcome`]). fn mark_spent(&mut self, id: SubwalletId, nullifier: &[u8; 32]) -> Result; /// Reserve `id`'s note with `nullifier` against an in-flight @@ -720,6 +780,28 @@ pub trait ShieldedStore: Send + Sync + 'static { wallet_id: WalletId, ) -> Result, Self::Error>; + /// Keep `endpoint` as the [`PendingRedrive::outcome_endpoint`] of `id`'s + /// redrive keyed by `activity_id`, durably, and hold the record: one with + /// no [`PendingRedrive::identity_outcome`] gets + /// [`IdentityCreateOutcome::Awaiting`]. Returns false, changing nothing, + /// when no such record exists. + fn set_redrive_outcome_endpoint( + &mut self, + id: SubwalletId, + activity_id: &[u8; 32], + endpoint: &str, + ) -> Result; + + /// Keep `outcome` as the [`PendingRedrive::identity_outcome`] of `id`'s + /// redrive keyed by `activity_id`, durably. Returns false, changing + /// nothing, when no such record exists. + fn set_redrive_identity_outcome( + &mut self, + id: SubwalletId, + activity_id: &[u8; 32], + outcome: IdentityCreateOutcome, + ) -> Result; + /// Increment the attempt counter on `id`'s redrive keyed by /// `activity_id`, returning the new count (`0` when no such record /// exists). @@ -734,7 +816,9 @@ pub trait ShieldedStore: Send + Sync + 'static { /// anchor-prune backstop). Implementations also drop the record /// implicitly when [`Self::mark_spent`] or [`Self::clear_pending`] /// resolves one of its nullifiers, since a transition lands or dies - /// atomically for all of its nullifiers. + /// atomically for all of its nullifiers; [`Self::mark_spent`] keeps a + /// record held for its outcome read + /// ([`PendingRedrive::held_for_identity_outcome`]). fn clear_redrive(&mut self, id: SubwalletId, activity_id: &[u8; 32]) -> Result<(), Self::Error>; @@ -1067,7 +1151,7 @@ impl SubwalletState { // when `newly_spent` is false (the in-memory drop still // happened here). self.pending_nullifiers.remove(nullifier); - let dropped = self.drop_redrives_containing(nullifier); + let dropped = self.drop_unheld_redrives_containing(nullifier); MarkSpentOutcome { newly_spent, dropped_redrives: dropped, @@ -1091,6 +1175,22 @@ impl SubwalletState { dropped } + /// [`Self::drop_redrives_containing`] for a spend seen landed: a record held + /// for its outcome read ([`PendingRedrive::held_for_identity_outcome`]) is + /// kept, as a landed spend of its notes does not show what it did. + fn drop_unheld_redrives_containing(&mut self, nullifier: &[u8; 32]) -> Vec<[u8; 32]> { + let dropped: Vec<[u8; 32]> = self + .redrives + .iter() + .filter(|(_, r)| !r.held_for_identity_outcome() && r.nullifiers.contains(nullifier)) + .map(|(id, _)| *id) + .collect(); + for id in &dropped { + self.redrives.remove(id); + } + dropped + } + /// Reserve `nullifier` against an in-flight spend. Returns /// `true` if newly added, `false` if it was already reserved. /// Re-reserving is a true no-op: an already-armed entry keeps its @@ -1245,6 +1345,37 @@ impl SubwalletState { self.redrives.get(activity_id).map(|r| r.attempts) } + /// Set the record's [`PendingRedrive::outcome_endpoint`], holding it + /// ([`ShieldedStore::set_redrive_outcome_endpoint`]); false when no such + /// record exists. + pub(super) fn set_redrive_outcome_endpoint( + &mut self, + activity_id: &[u8; 32], + endpoint: &str, + ) -> bool { + self.redrives + .get_mut(activity_id) + .map(|r| { + r.outcome_endpoint = Some(endpoint.to_string()); + r.identity_outcome + .get_or_insert(IdentityCreateOutcome::Awaiting); + }) + .is_some() + } + + /// Set the record's [`PendingRedrive::identity_outcome`]; false when no + /// such record exists. + pub(super) fn set_redrive_identity_outcome( + &mut self, + activity_id: &[u8; 32], + outcome: IdentityCreateOutcome, + ) -> bool { + self.redrives + .get_mut(activity_id) + .map(|r| r.identity_outcome = Some(outcome)) + .is_some() + } + /// Bump the attempt counter; `0` when no such record exists. pub(super) fn bump_redrive_attempts(&mut self, activity_id: &[u8; 32]) -> u32 { self.redrives @@ -1635,6 +1766,30 @@ impl ShieldedStore for InMemoryShieldedStore { .collect()) } + fn set_redrive_outcome_endpoint( + &mut self, + id: SubwalletId, + activity_id: &[u8; 32], + endpoint: &str, + ) -> Result { + Ok(self + .subwallets + .get_mut(&id) + .is_some_and(|sw| sw.set_redrive_outcome_endpoint(activity_id, endpoint))) + } + + fn set_redrive_identity_outcome( + &mut self, + id: SubwalletId, + activity_id: &[u8; 32], + outcome: IdentityCreateOutcome, + ) -> Result { + Ok(self + .subwallets + .get_mut(&id) + .is_some_and(|sw| sw.set_redrive_identity_outcome(activity_id, outcome))) + } + fn bump_redrive_attempts( &mut self, id: SubwalletId, @@ -2027,6 +2182,8 @@ mod tests { }; store.save_note(id, ¬e).unwrap(); let redrive = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [9u8; 32], anchor: [8u8; 32], nullifiers: vec![n1, n2], @@ -2424,6 +2581,8 @@ mod tests { let id = SubwalletId::new(wallet_id, 4); let mut store = InMemoryShieldedStore::new(); let identity_guard = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x11; 32], anchor: [0x22; 32], nullifiers: vec![], @@ -2434,6 +2593,8 @@ mod tests { anchor_height: None, }; let note_spend = PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [0x44; 32], anchor: [0x55; 32], nullifiers: vec![[0x66; 32]], 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 dd0c190a281..48538803684 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 @@ -521,6 +521,8 @@ async fn bind_persists_viewing_keys_and_restart_rebinds_seedlessly() { .arm_redrive( subwallet, crate::wallet::shielded::store::PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [7; 32], anchor: [8; 32], nullifiers: vec![[2; 32]], @@ -1074,6 +1076,8 @@ async fn should_keep_wallet_and_coordinator_keys_when_guarded_registration_is_re .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, + identity_outcome: None, activity_id: [1; 32], anchor: [2; 32], nullifiers: vec![], diff --git a/packages/rs-sdk/src/sdk.rs b/packages/rs-sdk/src/sdk.rs index 95281eacaa3..f15046a0a2e 100644 --- a/packages/rs-sdk/src/sdk.rs +++ b/packages/rs-sdk/src/sdk.rs @@ -1068,6 +1068,31 @@ impl Sdk { } } + /// A copy of this SDK that sends every DAPI request to `address` alone. + /// + /// The copy shares everything else with this SDK (settings, proofs, context provider, + /// caches); this SDK's address list is not changed, so its own requests keep rotating over + /// its nodes. A request the node fails bans it in the copy's list only. Use it to ask one + /// node that already holds a transition about that transition, as the privacy note of + /// [`Sdk::state_transition_outcome`](crate::Sdk::state_transition_outcome) asks. + pub fn scoped_to_address(&self, address: Address) -> Sdk { + let mut addresses = AddressList::new(); + addresses.add(address); + let mut scoped = self.clone(); + scoped.inner = match &self.inner { + SdkInstance::Dapi { dapi } => SdkInstance::Dapi { + dapi: dapi.with_address_list(addresses), + }, + #[cfg(feature = "mocks")] + SdkInstance::Mock { dapi, mock, .. } => SdkInstance::Mock { + dapi: Arc::clone(dapi), + mock: Arc::clone(mock), + address_list: addresses, + }, + }; + scoped + } + /// Return an owned snapshot of every DAPI address' ban state, /// including the reason the address was banned (when recorded). /// @@ -1831,6 +1856,26 @@ pub(crate) mod test { use crate::SdkBuilder; + /// A scoped SDK selects only its one address, and leaves the SDK it was + /// made from with its own list. + #[test] + fn should_scope_requests_to_one_address_without_changing_the_shared_list() { + let shared: AddressList = "http://127.0.0.1:3000,http://127.0.0.1:3001" + .parse() + .expect("addresses"); + let sdk = SdkBuilder::new(shared.clone()) + .with_core("127.0.0.1", 1, "user", "password") + .build() + .expect("an sdk"); + let address: rs_dapi_client::Address = "http://127.0.0.1:3001".parse().expect("address"); + + let scoped = sdk.scoped_to_address(address.clone()); + + assert_eq!(scoped.address_list().get_live_addresses(), vec![address]); + assert_eq!(sdk.address_list().len(), 2); + assert_eq!(shared.len(), 2); + } + use super::{min_protocol_version, Network}; use dash_context_provider::{ContextProvider, ContextProviderError}; use dpp::data_contract::TokenConfiguration; diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentShieldedActivity.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentShieldedActivity.swift index 39ae9835138..516053fd0bb 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentShieldedActivity.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentShieldedActivity.swift @@ -51,7 +51,7 @@ public final class PersistentShieldedActivity { public var kindTag: Int /// Direction: 0 In, 1 Out, 2 Self. public var direction: Int - /// Status: 0 Pending, 1 Confirmed, 2 Failed, 3 Unknown. + /// Status: 0 Pending, 1 Confirmed, 2 Failed, 3 Unknown, 4 InputsConsumed. public var status: Int /// Display amount in credits (principal; excludes self-change / diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedSync.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedSync.swift index a6feb022840..9ab6529c20b 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedSync.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletManagerShieldedSync.swift @@ -24,6 +24,11 @@ public struct ShieldedIdentityCreateUnconfirmedError: LocalizedError { public let identityId: Data public let message: String public var errorDescription: String? { message } + + public init(identityId: Data, message: String) { + self.identityId = identityId + self.message = message + } } /// Per-wallet outcome from a completed shielded sync pass. diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift index ee130585d2d..053c8d1c0dd 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift @@ -322,6 +322,11 @@ public enum PlatformWalletResultCode: Int32, Sendable { /// outcome yet. Nothing new was signed. Unconfirmed, not failed: do not /// submit it again; a public contact request sync settles it. case errorPublicContactRequestUnconfirmed = 76 + /// An identity create from the shielded pool did not create its identity + /// and can never execute, as proven reads show: its notes are spent and + /// the identity is absent. The wallet marked the notes spent. Do not + /// re-submit. + case errorShieldedIdentityCreateInputsConsumed = 77 /// The named thing does not exist. Besides the handle/lookup failures this /// has always covered, BOTH deferred-send paths report the /// wallet-was-REMOVED case here. @@ -473,6 +478,8 @@ public enum PlatformWalletResultCode: Int32, Sendable { self = .errorSdkPinnedBelowProtocolVersion case PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_PUBLIC_CONTACT_REQUEST_UNCONFIRMED: self = .errorPublicContactRequestUnconfirmed + case PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_SHIELDED_IDENTITY_CREATE_INPUTS_CONSUMED: + self = .errorShieldedIdentityCreateInputsConsumed case PLATFORM_WALLET_FFI_RESULT_CODE_NOT_FOUND: self = .notFound case PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_UNKNOWN: @@ -758,6 +765,18 @@ public enum PlatformWalletError: LocalizedError { /// `outIdentityId`) before falling back to this error: see /// `ShieldedIdentityCreateUnconfirmedError`. case shieldedBroadcastUnconfirmed(String) + /// An identity create from the pool did not create its identity and can + /// never execute, as proven reads show (code 77): every note it spends is + /// spent and the identity is absent at or above the height they were + /// spent by. The wallet marked the notes spent, dropped the saved + /// transition and marked the activity row failed; do not re-submit. The + /// reads do not say what spent the notes: Platform's paid failure path for + /// this transition (a key-state check failing in the block, such as a + /// public-key hash already registered, which credits the denomination + /// minus a penalty to the `sendToAddressOnCreationFailure` address) is one + /// possible cause, the same notes spent by another transition (another + /// device with this wallet's keys) another. + case shieldedIdentityCreateInputsConsumed(String) /// A shielded operation (shield from Platform addresses, unshield / /// transfer / withdrawal) may have reached a node and its result is not /// proved (a node's refusal or consensus verdict included). It may have @@ -979,6 +998,7 @@ public enum PlatformWalletError: LocalizedError { .publicContactRequestUnconfirmed(let m), .walletAlreadyExists(let m), .shieldedBroadcastFailed(let m), .shieldedBroadcastUnconfirmed(let m), .shieldedSpendUnconfirmed(let m), + .shieldedIdentityCreateInputsConsumed(let m), .shieldedNoRecordedAnchor(let m), .shieldedInsufficientBalance(let m), .shieldedIdentityDebitPending(let m), .shieldedRecoveryCorrupted(let m), .shieldedRecoveryKeysRequired(let m), @@ -1117,6 +1137,8 @@ public enum PlatformWalletError: LocalizedError { case .errorWalletAlreadyExists: self = .walletAlreadyExists(detail) case .errorShieldedBroadcastFailed: self = .shieldedBroadcastFailed(detail) case .errorShieldedBroadcastUnconfirmed: self = .shieldedBroadcastUnconfirmed(detail) + case .errorShieldedIdentityCreateInputsConsumed: + self = .shieldedIdentityCreateInputsConsumed(detail) case .errorShieldedSpendUnconfirmed: self = .shieldedSpendUnconfirmed(detail) case .errorShieldedNoRecordedAnchor: self = .shieldedNoRecordedAnchor(detail) case .errorShieldedInsufficientBalance: self = .shieldedInsufficientBalance(detail) diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/ShieldedActivityView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/ShieldedActivityView.swift index e2903a3b784..6e6d63defeb 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/ShieldedActivityView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/ShieldedActivityView.swift @@ -232,6 +232,10 @@ struct ShieldedActivityListView: View { Text("Failed") .font(.caption2) .foregroundColor(.red) + } else if entry.status == 4 { + Text("Failed, inputs spent") + .font(.caption2) + .foregroundColor(.red) } } } @@ -256,6 +260,7 @@ struct ShieldedActivityDetailView: View { case 0: return "Pending" case 1: return "Confirmed" case 2: return "Failed" + case 4: return "Failed, inputs spent" default: return "Unknown" } } diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift index b3bbb2a7d64..9e343a05817 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift @@ -604,6 +604,23 @@ final class ErrorHandlingTests: XCTestCase { XCTAssertEqual(message, detail) } + /// A shielded identity create proven unable to execute without its + /// identity keeps its own code and typed case, apart from an unconfirmed + /// create (17). + func testShouldReportAConsumedShieldedIdentityCreateByItsOwnCode() { + let code = PlatformWalletResultCode( + ffi: PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_SHIELDED_IDENTITY_CREATE_INPUTS_CONSUMED + ) + XCTAssertEqual(code, .errorShieldedIdentityCreateInputsConsumed) + XCTAssertEqual(code.rawValue, 77) + let detail = "Shielded identity create did not create its identity" + guard case .shieldedIdentityCreateInputsConsumed(let message) = + PlatformWalletError(code: code, message: detail) else { + return XCTFail("lost typed inputs-consumed identity create error") + } + XCTAssertEqual(message, detail) + } + func testShouldReadEveryFieldOfThePrivateContactReadiness() { var ffi = PrivateContactReadinessFFI() ffi.private_payments_restored = false