From 7c9bfe4119d325684ac720039ee078c7ec62e2b5 Mon Sep 17 00:00:00 2001 From: romchornyi Date: Wed, 11 Nov 2026 10:59:27 +0200 Subject: [PATCH 1/8] feat: report a shielded identity create proven to have failed with a paid error An identity create from the shielded pool (Type 20) whose result wait failed, its identity not found by its derived id, was always reported unconfirmed. Platform's paid failure path for this transition (a key-state check failing in the block) spends the notes and credits the denomination minus a penalty to the fallback address, so the create can never execute, yet the wallet kept its notes reserved and its record for the redrive. The wallet now reads, with proofs, the transition's outcome (Sdk::state_transition_outcome) and then the identity. Every nullifier spent and the identity absent at or above the height they were spent by reports PlatformWalletError::ShieldedIdentityCreateChargedFailure: the notes are marked spent, which drops the recovery record, and the activity row reads failed. Anything else stays ShieldedBroadcastUnconfirmed. No node verdict is trusted. The same notes spent by another transition read the same, as documented. The redrive pass applied the same reads to an identity create whose re-broadcast is refused with NullifierAlreadySpent, which it read as executed before. FFI: ErrorShieldedIdentityCreateChargedFailure = 77 (registry updated), no consensus code. Swift: PlatformWalletError.shieldedIdentityCreateChargedFailure(String). Kotlin: PlatformWallet.ShieldedIdentityCreateChargedFailure. Co-Authored-By: Claude Opus 5.5 --- .../dashsdk/errors/DashSdkError.kt | 14 + .../dashsdk/errors/DashSdkErrorTest.kt | 10 + .../ERROR_CODE_REGISTRY.md | 6 +- packages/rs-platform-wallet-ffi/src/error.rs | 45 +- .../src/shielded_send.rs | 10 +- packages/rs-platform-wallet/README.md | 2 +- packages/rs-platform-wallet/src/error.rs | 25 + .../src/wallet/shielded/coordinator.rs | 2 + .../src/wallet/shielded/operations.rs | 671 ++++++++++++++++-- .../PlatformWallet/PlatformWalletResult.swift | 21 + .../ErrorHandlingTests.swift | 16 + 11 files changed, 770 insertions(+), 52 deletions(-) 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..18ea95853fc 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,19 @@ sealed class DashSdkError( class PublicContactRequestUnconfirmed(message: String, cause: Throwable? = null) : PlatformWallet(message, cause) + /** + * `ErrorShieldedIdentityCreateChargedFailure` (native code 77). An + * identity created 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 denomination minus a penalty went to the + * transition's fallback address; the wallet marked the notes spent. + * Not retryable: the notes are gone. The same notes spent by another + * transition (another device with this wallet's keys) read the same. + * ← Swift `PlatformWalletError.shieldedIdentityCreateChargedFailure`. + */ + class ShieldedIdentityCreateChargedFailure(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 +1017,7 @@ sealed class DashSdkError( 74 -> PlatformWallet.PrivateContactStoreNotRead(message, cause) // ErrorPrivateContactStoreNotRead 75 -> PlatformWallet.SdkPinnedBelowProtocolVersion(message, cause) // ErrorSdkPinnedBelowProtocolVersion 76 -> PlatformWallet.PublicContactRequestUnconfirmed(message, cause) // ErrorPublicContactRequestUnconfirmed + 77 -> PlatformWallet.ShieldedIdentityCreateChargedFailure(message, cause) // ErrorShieldedIdentityCreateChargedFailure 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..09bfd0a3658 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 chargedShieldedIdentityCreateCode77MapsTyped() { + val detail = "Shielded identity create failed with a paid error" + val native = DashSDKException(DashSdkError.PLATFORM_WALLET_CODE_OFFSET + 77, detail) + val mapped = DashSdkError.fromNative(native) + assertTrue(mapped is DashSdkError.PlatformWallet.ShieldedIdentityCreateChargedFailure) + assertEquals(detail, mapped.message) + assertFalse("its notes are spent", mapped.isRetryable) + } + @Test fun platformShieldCapacityCode41MapsTyped() { val message = diff --git a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md index a5655430abd..be006f08c46 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,8 @@ 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 charged; 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 +193,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 | `ErrorShieldedIdentityCreateChargedFailure` | Shielded identity create charged failure | Proposed. Maps `PlatformWalletError::ShieldedIdentityCreateChargedFailure`: an identity created from the shielded pool (Type 20) whose result wait failed, its identity not found, is proven by reads to have failed with a paid error: every nullifier it spends is spent, and the identity is absent at or above the height they were spent by. Platform's paid failure path for this transition credits the denomination minus a penalty to the transition's fallback address. The notes are marked spent and the recovery record dropped; nothing is to be re-submitted. The same notes spent by another transition read the same. `out_identity_id` is not written and no consensus code rides the result. Typed in Rust, Swift (`PlatformWalletError.shieldedIdentityCreateChargedFailure`) and Kotlin (`PlatformWallet.ShieldedIdentityCreateChargedFailure`) | **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..4d6eee1231a 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::ErrorShieldedIdentityCreateChargedFailure`]). 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,21 @@ pub enum PlatformWalletFFIResultCode { /// [`Self::ErrorPrivateContactRequestPending`] (71). ErrorPublicContactRequestUnconfirmed = 76, + /// Maps `PlatformWalletError::ShieldedIdentityCreateChargedFailure`: an + /// identity created from the shielded pool did not create its identity + /// and can never execute, as proven reads show: every note it spends is + /// spent, and the identity is absent at or above the height they were + /// spent by. Platform's paid failure path for this transition (a + /// key-state check failing in the block) credits the denomination minus + /// a penalty to the transition's fallback address + /// (`send_to_address_on_creation_failure`). The wallet marked the notes + /// spent and dropped the create's recovery record; nothing is to be + /// re-submitted. The same notes spent by another transition (another + /// device with this wallet's keys) read the same, and then nothing went to + /// the fallback address. `out_identity_id` is not written. No consensus + /// code rides the result: the proof is the reads, not a node's verdict. + ErrorShieldedIdentityCreateChargedFailure = 77, + /// The named thing does not exist. /// /// Originally (and still mostly) the code for every `Option` returned as an @@ -1080,6 +1096,9 @@ impl From for PlatformWalletFFIResult { PlatformWalletError::ShieldedBroadcastUnconfirmed { .. } => { PlatformWalletFFIResultCode::ErrorShieldedBroadcastUnconfirmed } + PlatformWalletError::ShieldedIdentityCreateChargedFailure { .. } => { + PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateChargedFailure + } // 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 +3139,30 @@ mod private_contact_request_error_tests { ); } + /// A shielded identity create proven to have failed with a paid error + /// 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_charged_shielded_identity_create_by_its_own_code() { + let charged = PlatformWalletError::ShieldedIdentityCreateChargedFailure { + identity_id: dpp::prelude::Identifier::from([7u8; 32]), + consumed_by_height: 12, + reason: "result wait failed".to_string(), + }; + let rendered = charged.to_string(); + let result = PlatformWalletFFIResult::from(charged); + assert_eq!( + result.code, + PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateChargedFailure + ); + 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/shielded_send.rs b/packages/rs-platform-wallet-ffi/src/shielded_send.rs index 00161411428..98b8e6f8267 100644 --- a/packages/rs-platform-wallet-ffi/src/shielded_send.rs +++ b/packages/rs-platform-wallet-ffi/src/shielded_send.rs @@ -957,7 +957,11 @@ 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 proven to have taken +/// that path (its notes spent, its identity absent) returns +/// [`ErrorShieldedIdentityCreateChargedFailure`]. +/// +/// [`ErrorShieldedIdentityCreateChargedFailure`]: crate::error::PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateChargedFailure /// /// `mnemonic_resolver_handle` supplies the per-operation Orchard spend authority (see /// `platform_wallet_manager_shielded_transfer`). @@ -1115,6 +1119,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 paid failure: the notes are spent and the identity is absent, so the + // denomination minus a penalty went to the fallback address. The notes were marked spent + // wallet-side; `out_identity_id` is not written, as the identity does not exist. + Err(e @ PlatformWalletError::ShieldedIdentityCreateChargedFailure { .. }) => e.into(), // Definitive failure: the transition was not executed and the spent notes were released. Err(e @ PlatformWalletError::ShieldedBroadcastFailed(_)) => PlatformWalletFFIResult::err( PlatformWalletFFIResultCode::ErrorShieldedBroadcastFailed, diff --git a/packages/rs-platform-wallet/README.md b/packages/rs-platform-wallet/README.md index 7e38a91f9f4..d09c9148a58 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. When its identity is not found, proven reads (`Sdk::state_transition_outcome`, then the identity) tell a paid failure apart: every nullifier spent and the identity absent at or above the height they were spent by reports `ShieldedIdentityCreateChargedFailure` (FFI code 77), its notes marked spent and its redrive record dropped, as Platform's paid failure path for a Type 20 credited the denomination minus a penalty to the transition's fallback address; the same notes spent by another transition read the same. A re-broadcast of an identity creation refused with `NullifierAlreadySpent` is settled the same way when the reads show it. 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/error.rs b/packages/rs-platform-wallet/src/error.rs index f9ed4a5b60f..f330c7bf9c8 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) can no longer execute and did not create its + /// identity, 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. + /// A Type-20 transition that reaches a block either creates the identity or, when a key-state + /// check fails (a public-key hash already registered, a contract bound, an expired key), + /// takes its paid failure path: the spend is final and the denomination minus a penalty is + /// credited to the transition's `send_to_address_on_creation_failure`. Its notes are gone, so + /// the wallet marks them spent and retires the create's recovery record; nothing is to be + /// re-submitted. + /// + /// The reads show which inputs are taken, not who took them: the same notes spent by another + /// transition (another device holding this wallet's keys) reads the same, and then nothing + /// was credited to the fallback address by this create. `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} failed with a paid error: its notes are spent \ + (by height {consumed_by_height}) and the identity does not exist, so the denomination \ + minus a penalty went to the transition's fallback address: {reason}" + )] + ShieldedIdentityCreateChargedFailure { + 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/wallet/shielded/coordinator.rs b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs index 3720a2deefe..f6426198ce0 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, diff --git a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs index c213918b251..2385a5ea3ad 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs @@ -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::{ @@ -2961,21 +2962,70 @@ 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 that ended with +/// `failure`, and return the error it reports. A create proven to have failed with a paid error +/// ([`PlatformWalletError::ShieldedIdentityCreateChargedFailure`]) has its `notes` marked spent, +/// its record dropped with them and its activity row failed +/// ([`settle_charged_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 matches!( + failure.error, + PlatformWalletError::ShieldedIdentityCreateChargedFailure { .. } + ) { + // 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_charged_identity_create( + store, + persister, + wallet_id, + id, + pending_entry.as_ref().map(|entry| &entry.id), + &nullifiers, + ) + .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 @@ -3050,10 +3100,16 @@ 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 a paid failure +/// ([`PlatformWalletError::ShieldedIdentityCreateChargedFailure`]), which can never +/// execute. Notes of this wallet 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. +/// 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, st: &StateTransition, @@ -3106,25 +3162,44 @@ 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); + let mut identity = match fetch_identity_with_retries(sdk, identity_id).await { + Some(identity) => identity, + // Not found: proven reads tell a paid failure from an unknown outcome. + None => match probe_identity_create(sdk, st, identity_id).await { + IdentityCreateProbe::Created(identity) => identity, + IdentityCreateProbe::ChargedFailure { consumed_by_height } => { + warn!( + derived_id = %identity_id, + consumed_by_height, + error = %wait_err, + "IdentityCreateFromShieldedPool: notes proven spent and identity \ + proven absent; the create failed with a paid error" + ); + return Err(SpendFailure::from( + PlatformWalletError::ShieldedIdentityCreateChargedFailure { + 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)); } }; @@ -3230,6 +3305,100 @@ async fn fetch_identity_with_retries( None } +/// 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. Platform's paid failure path for a Type 20 (a key-state + /// check that fails in the block) spends the notes and credits the denomination minus a + /// penalty to the transition's fallback address. The reads cannot tell that from the same + /// notes spent by another transition (another device with this wallet's keys). + ChargedFailure { 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, +} + +/// 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. +async fn probe_identity_create( + sdk: &Arc, + 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) +} + +/// 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::ChargedFailure { consumed_by_height } + } + _ => IdentityCreateProbe::Unknown, + } +} + +/// Settle an identity create proven to have failed with a paid error +/// ([`PlatformWalletError::ShieldedIdentityCreateChargedFailure`]): its notes are spent on +/// chain, so they are marked spent, which also clears their reservations and drops the create's +/// recovery record ([`ShieldedStore::mark_spent`]), and its activity row `activity_id` reads +/// failed. A store write that fails is logged: the next scan sees the nullifiers spent and +/// settles the notes and the record the same way. +async fn settle_charged_identity_create( + store: &Arc>, + persister: Option<&WalletPersister>, + wallet_id: WalletId, + id: SubwalletId, + activity_id: Option<&[u8; 32]>, + nullifiers: &[[u8; 32]], +) { + if let Err(e) = mark_nullifiers_spent(store, persister, wallet_id, id, nullifiers).await { + warn!( + error = %e, + "a charged identity create's notes were not marked spent; the next scan settles them" + ); + } + if let Some(activity_id) = activity_id { + record_activity_status_by_id( + store, + persister, + wallet_id, + id, + activity_id, + ShieldedActivityStatus::Failed, + ) + .await; + } +} + // ------------------------------------------------------------------------- // Internal helpers (free fns) // ------------------------------------------------------------------------- @@ -3468,16 +3637,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); } } } @@ -4425,7 +4607,10 @@ 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) whose +/// identity proven reads show absent once its notes are spent +/// ([`settle_spent_identity_create_redrive`]): a paid failure, whose notes +/// are marked spent, its record dropped and its activity row failed; /// - 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; @@ -4443,6 +4628,7 @@ pub(super) async fn redrive_pending_identity_shields( pub(super) async fn redrive_pending_spends( sdk: &Arc, store: &Arc>, + persister: Option<&WalletPersister>, id: SubwalletId, recorded: &std::collections::HashSet<[u8; 32]>, cancel: &CancellationToken, @@ -4506,6 +4692,13 @@ pub(super) async fn redrive_pending_spends( ); } RedriveBroadcastOutcome::AlreadyExecuted => { + // A Type 20 whose notes are proven spent without its identity is a paid + // failure, not an execution: settled here, its record dropped with the notes. + if settle_spent_identity_create_redrive(sdk, store, persister, id, &redrive, &st) + .await + { + continue; + } // 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 @@ -4533,6 +4726,49 @@ pub(super) async fn redrive_pending_spends( } } +/// For a redrive whose re-broadcast was refused with `NullifierAlreadySpent`: +/// when its transition `st` is an identity create (Type 20) that proven reads +/// show failed with a paid error ([`probe_identity_create`]), settle it as one +/// ([`settle_charged_identity_create`]) and return `true`. Any other +/// transition, or a create the reads do not settle, returns `false` and is +/// left to the usual handling of an executed spend. +async fn settle_spent_identity_create_redrive( + sdk: &Arc, + store: &Arc>, + persister: Option<&WalletPersister>, + id: SubwalletId, + redrive: &PendingRedrive, + st: &StateTransition, +) -> bool { + use dpp::state_transition::state_transitions::shielded::identity_create_from_shielded_pool_transition::accessors::IdentityCreateFromShieldedPoolTransitionAccessorsV0; + + let StateTransition::IdentityCreateFromShieldedPool(create) = st else { + return false; + }; + let identity_id = create.identity_id(); + let IdentityCreateProbe::ChargedFailure { consumed_by_height } = + probe_identity_create(sdk, st, identity_id).await + else { + return false; + }; + warn!( + derived_id = %identity_id, + consumed_by_height, + "redrive: identity create's notes proven spent and its identity proven absent; \ + it failed with a paid error" + ); + settle_charged_identity_create( + store, + persister, + id.wallet_id, + id, + Some(&redrive.activity_id), + &redrive.nullifiers, + ) + .await; + true +} + /// 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)] @@ -6211,7 +6447,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); @@ -6259,7 +6495,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(); @@ -6318,6 +6554,7 @@ mod redrive_tests { let mut pass = Box::pin(redrive_pending_spends( &sdk_handle, &store, + None, id, &recorded, &cancel, @@ -6491,7 +6728,7 @@ mod redrive_tests { store, reservation, entry, - .. + notes, } = spend; match self { Self::Shared => { @@ -6507,20 +6744,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 +6946,7 @@ mod redrive_tests { redrive_pending_spends( &sdk_handle, &restarted, + None, id, &recorded, &CancellationToken::new(), @@ -6927,6 +7165,7 @@ mod redrive_tests { redrive_pending_spends( &sdk_handle, &restarted, + None, id, &recorded, &CancellationToken::new(), @@ -7466,6 +7705,344 @@ mod redrive_tests { ShieldedActivityStatus::Failed ); } + + // ── Identity create: a paid failure, proven by reads ─────────────── + + /// 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 a paid failure 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_charged_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::ChargedFailure { + consumed_by_height: 10 + } + )); + assert!(matches!( + judge_identity_create_reads(spent_by(10), absent_at(12)), + IdentityCreateProbe::ChargedFailure { + 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 never took the paid path" + ); + 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 nullifiers of the tests' identity create, as its transition lists them. + fn identity_create_nullifiers() -> Vec<[u8; 32]> { + vec![spend_action().nullifier, spend_action().nullifier] + } + + /// A mock SDK whose proven reads show the tests' identity create's + /// anchor recorded, its nullifiers `spent` or not, and its identity + /// absent. Every answer of a mock SDK is at height 0. + async fn sdk_reading_identity_create(spent: 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"); + 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 statuses = identity_create_nullifiers() + .into_iter() + .map(|nullifier| ShieldedNullifierStatus { + nullifier, + is_spent: spent, + }) + .collect(); + sdk.mock() + .expect_fetch( + ShieldedNullifiersQuery(identity_create_nullifiers()), + Some(ShieldedNullifierStatuses(statuses)), + ) + .await + .expect("the nullifier statuses"); + sdk.mock() + .expect_fetch(Identifier::from(CREATED_IDENTITY), None::) + .await + .expect("the identity, absent"); + sdk + } + + /// The probe reads the create's outcome and its identity with proofs: its + /// nullifiers spent and its identity absent is a paid failure, its + /// nullifiers free is not, and reads that fail prove nothing. + #[tokio::test] + async fn should_probe_an_identity_create_by_its_proven_nullifiers_and_identity() { + let transition = SpendPath::IdentityCreate.transition(); + let identity_id = Identifier::from(CREATED_IDENTITY); + + let spent = Arc::new(sdk_reading_identity_create(true).await); + assert!(matches!( + probe_identity_create(&spent, &transition, identity_id).await, + IdentityCreateProbe::ChargedFailure { + consumed_by_height: 0 + } + )); + + let free = Arc::new(sdk_reading_identity_create(false).await); + assert!(matches!( + probe_identity_create(&free, &transition, identity_id).await, + IdentityCreateProbe::Unknown + )); + + let unanswered = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + assert!(matches!( + probe_identity_create(&unanswered, &transition, identity_id).await, + IdentityCreateProbe::Unknown + )); + } + + /// An identity create whose saved record is the only thing left of it + /// after a result wait failed: its note reserved, its record saved and + /// its activity row pending, in the store at `path`. + async fn saved_identity_create( + path: &std::path::Path, + id: SubwalletId, + activity_id: [u8; 32], + error: PlatformWalletError, + ) -> (ReservedSpend, SpendFailure) { + let mut spend = ReservedSpend::at(path, id, activity_id).await; + let transition = SpendPath::IdentityCreate.transition(); + let failure = save_spend_then_broadcast( + &spend.store, + id, + &spend.entry, + SPEND_ANCHOR, + SPEND_ANCHOR_HEIGHT, + &spend.notes, + &mut spend.reservation, + &transition, + IDENTITY_CREATE_OPERATION, + || async { Err::<(), _>(SpendFailure::from(error)) }, + ) + .await + .expect_err("the create failed"); + (spend, failure) + } + + /// An identity create proven to have failed with a paid error reports + /// it, and its notes are gone: they are marked spent, its record is + /// dropped, across a restart too, and its activity row reads failed. + #[tokio::test] + async fn should_settle_a_charged_identity_create_as_spent_and_failed() { + let wallet_id = [0x74; 32]; + let id = SubwalletId::new(wallet_id, 0); + let activity_id = [0x7D; 32]; + let dir = tempfile::tempdir().expect("a directory"); + let path = dir.path().join("tree.sqlite"); + let (spend, failure) = saved_identity_create( + &path, + id, + activity_id, + PlatformWalletError::ShieldedIdentityCreateChargedFailure { + identity_id: Identifier::from(CREATED_IDENTITY), + consumed_by_height: 9, + reason: "the result wait reported a key-state error".to_string(), + }, + ) + .await; + let store = Arc::clone(&spend.store); + + let settled = SpendPath::IdentityCreate + .settle(spend, wallet_id, id, failure) + .await; + assert!( + matches!( + settled, + PlatformWalletError::ShieldedIdentityCreateChargedFailure { + identity_id, + consumed_by_height: 9, + .. + } if identity_id == Identifier::from(CREATED_IDENTITY) + ), + "{settled:?}" + ); + assert_eq!( + spendable_and_status(&store, id, &activity_id).await, + (0, ShieldedActivityStatus::Failed) + ); + assert!( + store.read().await.get_all_notes(id).expect("notes")[0].is_spent, + "the note is spent" + ); + drop(store); + assert!( + FileBackedShieldedStore::open_path(&path, 100) + .expect("reopened") + .pending_redrives(id) + .expect("records") + .is_empty(), + "the record is dropped for good" + ); + } + + /// A redrive pass whose re-broadcast of an identity create was refused + /// with `NullifierAlreadySpent` settles the create as a paid failure + /// only when proven reads show its identity absent once its notes are + /// spent. Otherwise the record is left to the handling of a spend that + /// executed. + #[tokio::test] + async fn should_settle_a_redriven_identity_create_whose_notes_are_spent_without_its_identity() { + use dpp::serialization::PlatformDeserializableTrusted; + + let wallet_id = [0x75; 32]; + let id = SubwalletId::new(wallet_id, 0); + let activity_id = [0x7E; 32]; + for spent in [true, false] { + let dir = tempfile::tempdir().expect("a directory"); + let path = dir.path().join("tree.sqlite"); + let (spend, failure) = saved_identity_create( + &path, + id, + activity_id, + PlatformWalletError::ShieldedSpendUnconfirmed { + operation: IDENTITY_CREATE_OPERATION, + reason: "the result wait timed out".to_string(), + }, + ) + .await; + let store = Arc::clone(&spend.store); + let _ = SpendPath::IdentityCreate + .settle(spend, wallet_id, id, failure) + .await; + let redrive = store.read().await.pending_redrives(id).expect("records")[0].clone(); + let st = StateTransition::deserialize_from_bytes_trusted(&redrive.st_bytes) + .expect("the saved transition"); + + let sdk = Arc::new(sdk_reading_identity_create(spent).await); + let settled = + settle_spent_identity_create_redrive(&sdk, &store, None, id, &redrive, &st).await; + let (spendable, status) = spendable_and_status(&store, id, &activity_id).await; + let records = store.read().await.pending_redrives(id).expect("records"); + assert_eq!(settled, spent, "spent: {spent}"); + assert_eq!( + spendable, 0, + "spent: {spent}: the note stays out of selection" + ); + if spent { + assert!(records.is_empty(), "the record is dropped"); + assert_eq!(status, ShieldedActivityStatus::Failed); + assert!(store.read().await.get_all_notes(id).expect("notes")[0].is_spent); + } else { + assert_eq!(records.len(), 1, "the record stays"); + assert_eq!(status, ShieldedActivityStatus::Pending); + } + } + + // Any other transition is left alone, unread. + let dir = tempfile::tempdir().expect("a directory"); + let store = Arc::new(RwLock::new( + FileBackedShieldedStore::open_path(dir.path().join("tree.sqlite"), 100) + .expect("the store"), + )); + let transfer = SpendPath::Shared.transition(); + let redrive = PendingRedrive { + activity_id, + anchor: SPEND_ANCHOR, + nullifiers: vec![spend_note().nullifier], + st_bytes: transfer.serialize_to_bytes().expect("serializes"), + attempts: 0, + anchor_height: Some(SPEND_ANCHOR_HEIGHT), + identity_nonce_finalized: false, + identity_user_abandoned: false, + }; + let sdk = Arc::new(sdk_reading_identity_create(true).await); + assert!( + !settle_spent_identity_create_redrive(&sdk, &store, None, id, &redrive, &transfer) + .await + ); + } } #[cfg(test)] diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift index ee130585d2d..aedabb5954e 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 created from the shielded pool failed with a paid error, + /// as proven reads show: its notes are spent and the identity is absent. + /// The denomination minus a penalty went to the transition's fallback + /// address; the wallet marked the notes spent. Do not re-submit. + case errorShieldedIdentityCreateChargedFailure = 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_CHARGED_FAILURE: + self = .errorShieldedIdentityCreateChargedFailure case PLATFORM_WALLET_FFI_RESULT_CODE_NOT_FOUND: self = .notFound case PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_UNKNOWN: @@ -758,6 +765,17 @@ 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. Platform's paid failure path for this transition (a key-state + /// check failing in the block, such as a public-key hash already + /// registered) credits the denomination minus a penalty to the + /// `sendToAddressOnCreationFailure` address. The wallet marked the notes + /// spent and dropped the saved transition; do not re-submit. The same + /// notes spent by another transition (another device with this wallet's + /// keys) read the same, and then nothing went to the fallback address. + case shieldedIdentityCreateChargedFailure(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 +997,7 @@ public enum PlatformWalletError: LocalizedError { .publicContactRequestUnconfirmed(let m), .walletAlreadyExists(let m), .shieldedBroadcastFailed(let m), .shieldedBroadcastUnconfirmed(let m), .shieldedSpendUnconfirmed(let m), + .shieldedIdentityCreateChargedFailure(let m), .shieldedNoRecordedAnchor(let m), .shieldedInsufficientBalance(let m), .shieldedIdentityDebitPending(let m), .shieldedRecoveryCorrupted(let m), .shieldedRecoveryKeysRequired(let m), @@ -1117,6 +1136,8 @@ public enum PlatformWalletError: LocalizedError { case .errorWalletAlreadyExists: self = .walletAlreadyExists(detail) case .errorShieldedBroadcastFailed: self = .shieldedBroadcastFailed(detail) case .errorShieldedBroadcastUnconfirmed: self = .shieldedBroadcastUnconfirmed(detail) + case .errorShieldedIdentityCreateChargedFailure: + self = .shieldedIdentityCreateChargedFailure(detail) case .errorShieldedSpendUnconfirmed: self = .shieldedSpendUnconfirmed(detail) case .errorShieldedNoRecordedAnchor: self = .shieldedNoRecordedAnchor(detail) case .errorShieldedInsufficientBalance: self = .shieldedInsufficientBalance(detail) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift index b3bbb2a7d64..a66c85d9948 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift @@ -604,6 +604,22 @@ final class ErrorHandlingTests: XCTestCase { XCTAssertEqual(message, detail) } + /// A shielded identity create proven to have failed with a paid error + /// keeps its own code and typed case, apart from an unconfirmed create (17). + func testShouldReportAChargedShieldedIdentityCreateByItsOwnCode() { + let code = PlatformWalletResultCode( + ffi: PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_SHIELDED_IDENTITY_CREATE_CHARGED_FAILURE + ) + XCTAssertEqual(code, .errorShieldedIdentityCreateChargedFailure) + XCTAssertEqual(code.rawValue, 77) + let detail = "Shielded identity create failed with a paid error" + guard case .shieldedIdentityCreateChargedFailure(let message) = + PlatformWalletError(code: code, message: detail) else { + return XCTFail("lost typed charged identity create error") + } + XCTAssertEqual(message, detail) + } + func testShouldReadEveryFieldOfThePrivateContactReadiness() { var ffi = PrivateContactReadinessFFI() ffi.private_payments_restored = false From 3f6704126f89e7858021057ccf30fdbccd96cdca Mon Sep 17 00:00:00 2001 From: romchornyi Date: Wed, 11 Nov 2026 13:14:55 +0200 Subject: [PATCH 2/8] feat(sdk): scope an sdk to one dapi address Sdk::scoped_to_address returns a copy of an SDK that sends every DAPI request to one node, sharing everything else, without changing the original SDK's address list (DapiClient::with_address_list). It lets a client ask only the node that already holds a transition about it, as the privacy note of Sdk::state_transition_outcome asks. Co-Authored-By: Claude Opus 5.5 --- packages/rs-dapi-client/src/dapi_client.rs | 10 +++++ packages/rs-sdk/src/sdk.rs | 45 ++++++++++++++++++++++ 2 files changed, 55 insertions(+) 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-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; From c9979afd912f90e3a4a8db5a3af379c36763a585 Mon Sep 17 00:00:00 2001 From: romchornyi Date: Wed, 11 Nov 2026 13:14:55 +0200 Subject: [PATCH 3/8] fix: settle a shielded identity create by reads at its node, held across scans Rename the outcome to what the reads prove: ShieldedIdentityCreateInputsConsumed (FFI code 77, Swift shieldedIdentityCreateInputsConsumed(String), Kotlin PlatformWallet.ShieldedIdentityCreateInputsConsumed). Spent nullifiers and an absent identity show the create can never execute and created no identity, not what spent the notes, so nothing claims the fallback address was paid any more. Every read about the create goes only to the node a dispatch reached: each dispatch is sent through an SDK scoped to one node of the shared list (broadcast_tracking_endpoint), and the result wait, the identity read and the outcome reads use that node. A create with no such node is never probed. The node is kept with the recovery record (outcome_endpoint, a new nullable column), which holds it: mark_spent keeps a held record, the scan neither upgrades its activity row nor retires it, and the sync pass reads its outcome at the node after the scan (classify_spent_identity_creates): confirmed when the identity is found, failed at the height the notes were spent by when it is proven absent, overwriting a row the scan confirmed. A settled row keeps its height, so a later scan does not upgrade it. Re-broadcasts of a held create go to its node only. A create whose outcome cannot be read gives its hold up after MAX_REDRIVE_ATTEMPTS. Tests use real-input fixtures: the notes marked spent are exactly the transition's real inputs whose nullifiers the proof read. Co-Authored-By: Claude Opus 5.5 --- .../dashsdk/errors/DashSdkError.kt | 20 +- .../dashsdk/errors/DashSdkErrorTest.kt | 6 +- .../ERROR_CODE_REGISTRY.md | 5 +- packages/rs-platform-wallet-ffi/src/error.rs | 51 +- .../src/shielded_send.rs | 18 +- packages/rs-platform-wallet/README.md | 2 +- .../src/broadcast_outcome.rs | 141 ++- packages/rs-platform-wallet/src/error.rs | 31 +- .../rs-platform-wallet/src/manager/load.rs | 1 + .../src/wallet/platform_wallet.rs | 1 + .../src/wallet/shielded/balance_tests.rs | 1 + .../src/wallet/shielded/coordinator.rs | 530 +++++++++- .../src/wallet/shielded/file_store.rs | 85 +- .../src/wallet/shielded/operations.rs | 971 +++++++++++++----- .../src/wallet/shielded/store.rs | 67 +- .../wallet/shielded/viewing_key_bind_tests.rs | 2 + .../PlatformWallet/PlatformWalletResult.swift | 37 +- .../ErrorHandlingTests.swift | 17 +- 18 files changed, 1619 insertions(+), 367 deletions(-) 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 18ea95853fc..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 @@ -796,16 +796,18 @@ sealed class DashSdkError( PlatformWallet(message, cause) /** - * `ErrorShieldedIdentityCreateChargedFailure` (native code 77). An - * identity created from the shielded pool did not create its identity + * `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 denomination minus a penalty went to the - * transition's fallback address; the wallet marked the notes spent. - * Not retryable: the notes are gone. The same notes spent by another - * transition (another device with this wallet's keys) read the same. - * ← Swift `PlatformWalletError.shieldedIdentityCreateChargedFailure`. + * 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 ShieldedIdentityCreateChargedFailure(message: String, cause: Throwable? = null) : + class ShieldedIdentityCreateInputsConsumed(message: String, cause: Throwable? = null) : PlatformWallet(message, cause) /** @@ -1017,7 +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.ShieldedIdentityCreateChargedFailure(message, cause) // ErrorShieldedIdentityCreateChargedFailure + 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 09bfd0a3658..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 @@ -401,11 +401,11 @@ class DashSdkErrorTest { } @Test - fun chargedShieldedIdentityCreateCode77MapsTyped() { - val detail = "Shielded identity create failed with a paid error" + 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.ShieldedIdentityCreateChargedFailure) + assertTrue(mapped is DashSdkError.PlatformWallet.ShieldedIdentityCreateInputsConsumed) assertEquals(detail, mapped.message) assertFalse("its notes are spent", mapped.isRetryable) } diff --git a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md index be006f08c46..fb0227bf764 100644 --- a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md +++ b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md @@ -129,7 +129,8 @@ 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; 77 proposed for a -shielded identity create proven charged; 28, 30, +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 @@ -193,7 +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 | `ErrorShieldedIdentityCreateChargedFailure` | Shielded identity create charged failure | Proposed. Maps `PlatformWalletError::ShieldedIdentityCreateChargedFailure`: an identity created from the shielded pool (Type 20) whose result wait failed, its identity not found, is proven by reads to have failed with a paid error: every nullifier it spends is spent, and the identity is absent at or above the height they were spent by. Platform's paid failure path for this transition credits the denomination minus a penalty to the transition's fallback address. The notes are marked spent and the recovery record dropped; nothing is to be re-submitted. The same notes spent by another transition read the same. `out_identity_id` is not written and no consensus code rides the result. Typed in Rust, Swift (`PlatformWalletError.shieldedIdentityCreateChargedFailure`) and Kotlin (`PlatformWallet.ShieldedIdentityCreateChargedFailure`) | +| 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, the recovery record retired and the activity row failed; 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 4d6eee1231a..947c23a25d3 100644 --- a/packages/rs-platform-wallet-ffi/src/error.rs +++ b/packages/rs-platform-wallet-ffi/src/error.rs @@ -154,7 +154,7 @@ pub enum PlatformWalletFFIResultCode { /// 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, without proven reads showing a paid failure - /// ([`Self::ErrorShieldedIdentityCreateChargedFailure`]). The wallet keeps the notes reserved and the create's + /// ([`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, @@ -769,20 +769,21 @@ pub enum PlatformWalletFFIResultCode { /// [`Self::ErrorPrivateContactRequestPending`] (71). ErrorPublicContactRequestUnconfirmed = 76, - /// Maps `PlatformWalletError::ShieldedIdentityCreateChargedFailure`: an - /// identity created from the shielded pool did not create its identity - /// and can never execute, as proven reads show: every note it spends is - /// spent, and the identity is absent at or above the height they were - /// spent by. Platform's paid failure path for this transition (a - /// key-state check failing in the block) credits the denomination minus - /// a penalty to the transition's fallback address - /// (`send_to_address_on_creation_failure`). The wallet marked the notes - /// spent and dropped the create's recovery record; nothing is to be - /// re-submitted. The same notes spent by another transition (another - /// device with this wallet's keys) read the same, and then nothing went to - /// the fallback address. `out_identity_id` is not written. No consensus - /// code rides the result: the proof is the reads, not a node's verdict. - ErrorShieldedIdentityCreateChargedFailure = 77, + /// 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, + /// retired the create's recovery record and marked its activity row + /// failed; 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. /// @@ -1096,8 +1097,8 @@ impl From for PlatformWalletFFIResult { PlatformWalletError::ShieldedBroadcastUnconfirmed { .. } => { PlatformWalletFFIResultCode::ErrorShieldedBroadcastUnconfirmed } - PlatformWalletError::ShieldedIdentityCreateChargedFailure { .. } => { - PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateChargedFailure + 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 @@ -3139,21 +3140,21 @@ mod private_contact_request_error_tests { ); } - /// A shielded identity create proven to have failed with a paid error - /// reaches the host by its own code, with its message, and no consensus - /// code: the proof is the reads, not a node's verdict. + /// 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_charged_shielded_identity_create_by_its_own_code() { - let charged = PlatformWalletError::ShieldedIdentityCreateChargedFailure { + 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 = charged.to_string(); - let result = PlatformWalletFFIResult::from(charged); + let rendered = consumed.to_string(); + let result = PlatformWalletFFIResult::from(consumed); assert_eq!( result.code, - PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateChargedFailure + PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateInputsConsumed ); assert_eq!(result.code as u32, 77); assert_eq!(result.consensus_code, 0); diff --git a/packages/rs-platform-wallet-ffi/src/shielded_send.rs b/packages/rs-platform-wallet-ffi/src/shielded_send.rs index 98b8e6f8267..cba40465299 100644 --- a/packages/rs-platform-wallet-ffi/src/shielded_send.rs +++ b/packages/rs-platform-wallet-ffi/src/shielded_send.rs @@ -957,11 +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. A create proven to have taken -/// that path (its notes spent, its identity absent) returns -/// [`ErrorShieldedIdentityCreateChargedFailure`]. +/// 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. /// -/// [`ErrorShieldedIdentityCreateChargedFailure`]: crate::error::PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateChargedFailure +/// [`ErrorShieldedIdentityCreateInputsConsumed`]: crate::error::PlatformWalletFFIResultCode::ErrorShieldedIdentityCreateInputsConsumed /// /// `mnemonic_resolver_handle` supplies the per-operation Orchard spend authority (see /// `platform_wallet_manager_shielded_transfer`). @@ -1119,10 +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 paid failure: the notes are spent and the identity is absent, so the - // denomination minus a penalty went to the fallback address. The notes were marked spent - // wallet-side; `out_identity_id` is not written, as the identity does not exist. - Err(e @ PlatformWalletError::ShieldedIdentityCreateChargedFailure { .. }) => e.into(), + // 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, @@ -2495,6 +2496,7 @@ mod tests { .arm_redrive( SubwalletId::new([7; 32], 9), PendingRedrive { + outcome_endpoint: 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 d09c9148a58..ebbf532d289 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. When its identity is not found, proven reads (`Sdk::state_transition_outcome`, then the identity) tell a paid failure apart: every nullifier spent and the identity absent at or above the height they were spent by reports `ShieldedIdentityCreateChargedFailure` (FFI code 77), its notes marked spent and its redrive record dropped, as Platform's paid failure path for a Type 20 credited the denomination minus a penalty to the transition's fallback address; the same notes spent by another transition read the same. A re-broadcast of an identity creation refused with `NullifierAlreadySpent` is settled the same way when the reads show it. 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, its redrive record retired 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. The node is kept with the redrive record, which holds it for that read: a scan that sees its notes spent keeps the record and leaves its row as it is, and the sync pass reads the outcome at the node and settles it (confirmed when the identity is found, failed when it is proven absent), overwriting a row the scan confirmed. A creation with no node kept is never read about: it stays unconfirmed, and the scan settles it as before. 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..828bb34ad46 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. +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 f330c7bf9c8..1939868c9d0 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -1113,26 +1113,25 @@ pub enum PlatformWalletError { reason: String, }, - /// The shielded identity create (Type 20) can no longer execute and did not create its - /// identity, as proven reads show: every nullifier it spends is spent at or before + /// 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. - /// A Type-20 transition that reaches a block either creates the identity or, when a key-state - /// check fails (a public-key hash already registered, a contract bound, an expired key), - /// takes its paid failure path: the spend is final and the denomination minus a penalty is - /// credited to the transition's `send_to_address_on_creation_failure`. Its notes are gone, so - /// the wallet marks them spent and retires the create's recovery record; nothing is to be - /// re-submitted. + /// Its notes are gone, so the wallet marks them spent, retires the create's recovery record and + /// marks its activity row failed; nothing is to be re-submitted. /// - /// The reads show which inputs are taken, not who took them: the same notes spent by another - /// transition (another device holding this wallet's keys) reads the same, and then nothing - /// was credited to the fallback address by this create. `reason` carries the result-wait - /// failure that led to the reads, for diagnostics; it is not part of the proof. + /// 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} failed with a paid error: its notes are spent \ - (by height {consumed_by_height}) and the identity does not exist, so the denomination \ - minus a penalty went to the transition's fallback address: {reason}" + "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}" )] - ShieldedIdentityCreateChargedFailure { + ShieldedIdentityCreateInputsConsumed { identity_id: Identifier, consumed_by_height: u64, reason: String, diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 002186924f9..9c112e2d3fa 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -2115,6 +2115,7 @@ mod private_payment_hold_tests { .arm_private_contact_payment( SubwalletId::new(bound, 0), PendingRedrive { + outcome_endpoint: 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..06f5717c716 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -3500,6 +3500,7 @@ mod private_contact_request_redrive_tests { /// The unconfirmed spend that broadcast `st_bytes`. fn redrive(st_bytes: Vec) -> PendingRedrive { PendingRedrive { + outcome_endpoint: None, activity_id: [1; 32], anchor: [7; 32], nullifiers: vec![[1; 32]], 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..66ba2900748 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,7 @@ async fn local_balance_restores_funds_and_durable_pending_reservations_offline() .arm_redrive( id, super::store::PendingRedrive { + outcome_endpoint: 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 f6426198ce0..fd32154337b 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -1598,6 +1598,15 @@ impl NetworkShieldedCoordinator { tracing::warn!(error = %e, "Shielded activity derivation failed; skipping this pass"); } + // Identity creates held for their outcome read whose notes this scan + // (or an earlier one) saw spent: the scan kept their records and left + // their rows alone, as their notes spent do not show whether the + // identity was created. Each is read at the node it was delivered to. + if !cancelled { + self.classify_spent_identity_creates(&subwallets, 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 @@ -2347,6 +2356,14 @@ 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.outcome_endpoint.is_some()) + .map(|redrive| redrive.activity_id) + .collect(); for entry in derived.new_entries { let overlapped: std::collections::BTreeSet<[u8; 32]> = entry .note_cmxs @@ -2375,7 +2392,9 @@ impl NetworkShieldedCoordinator { })? .into_iter() .filter_map(|(owner, redrive)| { - (!redrive.nullifiers.is_empty() && redrive.activity_id == entry.id) + (!redrive.nullifiers.is_empty() + && redrive.outcome_endpoint.is_none() + && redrive.activity_id == entry.id) .then_some(owner) }) .collect(); @@ -2404,6 +2423,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 @@ -2466,6 +2488,31 @@ impl NetworkShieldedCoordinator { Ok(()) } + /// Settle the identity creates of `subwallets` held for their outcome + /// read whose notes the store holds spent + /// ([`super::operations::classify_spent_identity_creates`]), each with + /// its wallet's persister. + async fn classify_spent_identity_creates( + &self, + subwallets: &[(SubwalletId, AccountViewingKeys)], + cancel: &CancellationToken, + ) { + for (id, _) in subwallets { + if cancel.is_cancelled() { + return; + } + let persister = self.persisters.read().await.get(&id.wallet_id).cloned(); + super::operations::classify_spent_identity_creates( + &self.sdk, + &self.store, + persister.as_ref(), + *id, + cancel, + ) + .await; + } + } + /// 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. @@ -2927,6 +2974,7 @@ mod tests { .into(); ( PendingRedrive { + outcome_endpoint: None, activity_id: compute_activity_id(&cmxs), anchor: [1; 32], nullifiers: vec![], @@ -3357,6 +3405,480 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + // ── 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 + // `classify_spent_identity_creates`. + + /// 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() + }); + sdk.mock() + .expect_fetch(dpp::prelude::Identifier::from(CREATE_IDENTITY), identity) + .await + .unwrap(); + sdk + } + + /// [`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 { + 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(sdk), + dashcore::Network::Testnet, + db_path, + store, + ); + 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([0x11; 32], Arc::new(NoPlatformPersistence)); + coordinator + .register_wallet([0x11; 32], account_views, persister) + .await + .expect("register wallet"); + coordinator + } + + /// 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()), + }, + ) + .unwrap(); + } + + /// 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, + }; + + 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(); + } + + /// 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; + } + + /// 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(); + } + + /// 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)]; + coordinator + .derive_activity_into_changeset(&subwallets, &mut ShieldedChangeSet::default()) + .await + .unwrap(); + coordinator + .classify_spent_identity_creates(&subwallets, &CancellationToken::new()) + .await; + } + + /// 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::Failed, 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::Failed, 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::Failed, 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 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; + assert!(coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap() + .is_empty()); + + scan_sees_identity_create(&coordinator, id, 40).await; + post_scan_steps(&coordinator, id).await; + + assert_eq!( + create_row(&coordinator, id).await, + (ShieldedActivityStatus::Failed, Some(7)) + ); + 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::Failed + }; + 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); + } + } + /// 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() @@ -3501,6 +4023,7 @@ 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, activity_id: [0x31; 32], anchor: [0x32; 32], nullifiers: vec![], @@ -3878,6 +4401,7 @@ mod tests { .expect("old account") .clone(); let identity_guard = super::super::store::PendingRedrive { + outcome_endpoint: None, activity_id: [0x71; 32], anchor: [0x72; 32], nullifiers: vec![], @@ -3992,6 +4516,7 @@ mod tests { .arm_redrive( id, super::super::store::PendingRedrive { + outcome_endpoint: None, activity_id: [0x71; 32], anchor: [0x72; 32], nullifiers: vec![], @@ -4493,6 +5018,7 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, activity_id: *activity, anchor: *anchor, nullifiers: vec![*nullifier], @@ -4598,6 +5124,7 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, activity_id: activity, anchor, nullifiers: vec![nullifier], @@ -4697,6 +5224,7 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: 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..03fdaea42d7 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/file_store.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/file_store.rs @@ -191,8 +191,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) \ + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)", rusqlite::params![ id.wallet_id.as_slice(), id.account_index, @@ -209,6 +209,7 @@ impl FileBackedShieldedStore { redrive .anchor_height .and_then(|height| i64::try_from(height).ok()), + redrive.outcome_endpoint, ], ) .map_err(|e| FileShieldedStoreError::Storage(format!("persist redrive: {e}")))?; @@ -479,6 +480,21 @@ 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}")) + })?; + } // 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 +592,7 @@ 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 FROM shielded_pending_spends", ) .map_err(|e| FileShieldedStoreError::Storage(format!("prepare rehydrate: {e}")))?; let rows = stmt @@ -611,6 +627,9 @@ 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(); let decoded: rusqlite::Result<_> = (|| { Ok(( row.get::<_, Vec>(0)?, @@ -629,6 +648,7 @@ impl FileBackedShieldedStore { readable_wallet_id, private_contact_payment, anchor_height, + outcome_endpoint, decoded, )) }) @@ -639,6 +659,7 @@ impl FileBackedShieldedStore { readable_wallet_id, private_contact_payment, anchor_height, + outcome_endpoint, decoded, ) = row.map_err(|e| FileShieldedStoreError::RecoveryCorrupted { account_index: None, @@ -724,6 +745,7 @@ impl FileBackedShieldedStore { Ok(( SubwalletId::new(wallet_id, account_index), PendingRedrive { + outcome_endpoint, activity_id, anchor, nullifiers, @@ -1388,6 +1410,43 @@ 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 \ + 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, + ], + ) + .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 bump_redrive_attempts( &mut self, id: SubwalletId, @@ -1767,6 +1826,7 @@ mod tests { let path = temp_tree_path("redrive_roundtrip"); let id = SubwalletId::new([7u8; 32], 0); let redrive = PendingRedrive { + outcome_endpoint: None, activity_id: [1u8; 32], anchor: [2u8; 32], nullifiers: vec![[3u8; 32], [4u8; 32]], @@ -1829,6 +1889,7 @@ mod tests { let wallet_id = [0x5A; 32]; let id = SubwalletId::new(wallet_id, 0); let record = |activity: u8, nullifier: u8| PendingRedrive { + outcome_endpoint: None, activity_id: [activity; 32], anchor: [2u8; 32], nullifiers: vec![[nullifier; 32]], @@ -1902,6 +1963,7 @@ 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, activity_id: [activity; 32], anchor: [2u8; 32], nullifiers: vec![[nullifier; 32]], @@ -1982,6 +2044,7 @@ mod tests { let path = temp_tree_path("identity_debit_guard"); let id = SubwalletId::new([9u8; 32], 4); let redrive = PendingRedrive { + outcome_endpoint: None, activity_id: [1u8; 32], anchor: [2u8; 32], nullifiers: vec![], @@ -2080,6 +2143,7 @@ mod tests { .arm_redrive( SubwalletId::new([1; 32], 2), PendingRedrive { + outcome_endpoint: None, activity_id: [2; 32], anchor: [3; 32], nullifiers: vec![], @@ -2137,6 +2201,7 @@ mod tests { } .into(); let record = PendingRedrive { + outcome_endpoint: None, activity_id: [0x11; 32], anchor: [0x52; 32], nullifiers: vec![[0x13; 32]], @@ -2251,6 +2316,7 @@ mod tests { } .into(); let guard = PendingRedrive { + outcome_endpoint: None, activity_id: [0x61; 32], anchor: [0x71; 32], nullifiers: vec![], @@ -2266,6 +2332,7 @@ mod tests { .arm_redrive( note_id, PendingRedrive { + outcome_endpoint: None, activity_id: [0x21; 32], anchor: [0x51; 32], nullifiers: vec![[0x91; 32]], @@ -2332,6 +2399,7 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, activity_id: [0xD1; 32], anchor: [0xC1; 32], nullifiers: vec![], @@ -2386,6 +2454,7 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, activity_id: [0xD2; 32], anchor: [0xF1; 32], nullifiers: vec![[0xC2; 32]], @@ -2420,6 +2489,7 @@ mod tests { let path = temp_tree_path("redrive_park_failure"); let id = SubwalletId::new([0x91; 32], 2); let record = PendingRedrive { + outcome_endpoint: None, activity_id: [0x12; 32], anchor: [0x23; 32], nullifiers: vec![], @@ -2448,6 +2518,7 @@ mod tests { let path = temp_tree_path("clear_redrive_failure"); let id = SubwalletId::new([0x91; 32], 2); let redrive = PendingRedrive { + outcome_endpoint: None, activity_id: [0x12; 32], anchor: [0x23; 32], nullifiers: vec![], @@ -2498,6 +2569,7 @@ mod tests { let wallet_id = [0xA4; 32]; let id = SubwalletId::new(wallet_id, 8); let identity_guard = PendingRedrive { + outcome_endpoint: None, activity_id: [0x15; 32], anchor: [0x26; 32], nullifiers: vec![], @@ -2508,6 +2580,7 @@ mod tests { anchor_height: None, }; let note_spend = PendingRedrive { + outcome_endpoint: None, activity_id: [0x48; 32], anchor: [0x59; 32], nullifiers: vec![[0x6A; 32]], @@ -2554,6 +2627,7 @@ 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, activity_id: [activity; 32], anchor: [0x22; 32], nullifiers: vec![[nf; 32]], @@ -2632,6 +2706,7 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, activity_id: [0x7; 32], anchor: [0x22; 32], nullifiers: vec![nf], @@ -3056,6 +3131,7 @@ mod tests { account_reference: 7, }; let redrive = PendingRedrive { + outcome_endpoint: None, activity_id: [0x21; 32], anchor: [0x22; 32], nullifiers: vec![[0x23; 32], [0x24; 32]], @@ -3210,6 +3286,7 @@ mod tests { let wallet_id = [0x5C; 32]; let id = SubwalletId::new(wallet_id, 0); let redrive = PendingRedrive { + outcome_endpoint: None, activity_id: [0x31; 32], anchor: [0x32; 32], nullifiers: vec![[0x33; 32]], @@ -3311,6 +3388,7 @@ mod tests { .arm_private_contact_payment( id, PendingRedrive { + outcome_endpoint: None, activity_id: [0x31; 32], anchor: [0x32; 32], nullifiers: vec![[0x33; 32]], @@ -3376,6 +3454,7 @@ mod tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: 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 2385a5ea3ad..add7cb1438f 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs @@ -33,8 +33,8 @@ use super::store::{ 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, BroadcastDelivery, }; use crate::changeset::{PlatformWalletChangeSet, ShieldedChangeSet}; use crate::error::{preserve_signer_key_unavailable_or, PlatformWalletError}; @@ -1013,6 +1013,7 @@ async fn arm_identity_shield_redrive( .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, activity_id: entry.id, anchor: transition.anchor(), nullifiers: vec![], @@ -2951,6 +2952,10 @@ where update failed; will heal on next sync" ); } + // A create held for its outcome read keeps its record when its notes are marked + // spent (`PendingRedrive::outcome_endpoint`); its outcome is known now. + retire_identity_create_record(store, id, pending_entry.as_ref().map(|entry| &entry.id)) + .await; info!( account, denomination, @@ -2978,10 +2983,10 @@ where } /// 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 to have failed with a paid error -/// ([`PlatformWalletError::ShieldedIdentityCreateChargedFailure`]) has its `notes` marked spent, -/// its record dropped with them and its activity row failed -/// ([`settle_charged_identity_create`]); every other failure is settled as any spend's +/// `failure`, and return the error it reports. A create proven unable to execute without its +/// identity ([`PlatformWalletError::ShieldedIdentityCreateInputsConsumed`]) has its `notes` +/// marked spent, its record retired 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( @@ -2995,21 +3000,22 @@ async fn settle_failed_identity_create( failure: SpendFailure, identity_id: Option, ) -> PlatformWalletError { - if matches!( - failure.error, - PlatformWalletError::ShieldedIdentityCreateChargedFailure { .. } - ) { + 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_charged_identity_create( + 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; @@ -3075,7 +3081,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 } @@ -3102,16 +3118,25 @@ const IDENTITY_CREATE_OPERATION: &str = "identity create from shielded pool"; /// (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 a paid failure -/// ([`PlatformWalletError::ShieldedIdentityCreateChargedFailure`]), which can never -/// execute. Notes of this wallet spent by another transition (another device with the same -/// keys) read the same. +/// 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. +/// +/// 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( +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, @@ -3123,7 +3148,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 { @@ -3140,12 +3166,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, @@ -3162,21 +3198,24 @@ 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. - let mut identity = match fetch_identity_with_retries(sdk, identity_id).await { + let mut identity = match fetch_identity_with_retries(&reader, identity_id).await { Some(identity) => identity, - // Not found: proven reads tell a paid failure from an unknown outcome. - None => match probe_identity_create(sdk, st, identity_id).await { + // 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(&reader, st, identity_id).await { IdentityCreateProbe::Created(identity) => identity, - IdentityCreateProbe::ChargedFailure { consumed_by_height } => { + 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 failed with a paid error" + proven absent; the create can no longer execute" ); return Err(SpendFailure::from( - PlatformWalletError::ShieldedIdentityCreateChargedFailure { + PlatformWalletError::ShieldedIdentityCreateInputsConsumed { identity_id, consumed_by_height, reason: wait_err.to_string(), @@ -3242,7 +3281,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}" @@ -3273,7 +3312,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; @@ -3312,11 +3351,10 @@ enum IdentityCreateProbe { 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. Platform's paid failure path for a Type 20 (a key-state - /// check that fails in the block) spends the notes and credits the denomination minus a - /// penalty to the transition's fallback address. The reads cannot tell that from the same - /// notes spent by another transition (another device with this wallet's keys). - ChargedFailure { consumed_by_height: u64 }, + /// 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, @@ -3328,8 +3366,11 @@ enum IdentityCreateProbe { /// 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: &Arc, + sdk: &dash_sdk::Sdk, st: &StateTransition, identity_id: Identifier, ) -> IdentityCreateProbe { @@ -3360,42 +3401,303 @@ fn judge_identity_create_reads( Ok(TransitionOutcome::ExecutedOrSuperseded { consumed_by_height }), Ok((None, read_at)), ) if read_at >= consumed_by_height => { - IdentityCreateProbe::ChargedFailure { consumed_by_height } + IdentityCreateProbe::InputsConsumed { consumed_by_height } } _ => IdentityCreateProbe::Unknown, } } -/// Settle an identity create proven to have failed with a paid error -/// ([`PlatformWalletError::ShieldedIdentityCreateChargedFailure`]): its notes are spent on -/// chain, so they are marked spent, which also clears their reservations and drops the create's -/// recovery record ([`ShieldedStore::mark_spent`]), and its activity row `activity_id` reads -/// failed. A store write that fails is logged: the next scan sees the nullifiers spent and -/// settles the notes and the record the same way. -async fn settle_charged_identity_create( +/// Settle an identity create proven unable to execute without its identity +/// ([`PlatformWalletError::ShieldedIdentityCreateInputsConsumed`]): its notes are spent on +/// chain, so they are marked spent, which also clears their reservations; its recovery record +/// `activity_id` is retired; and its activity row reads failed at `consumed_by_height`, a height +/// by which its notes were spent. The height keeps the row failed: the scan upgrades only rows +/// that are pending or carry no height, and a row of this create the scan already confirmed is +/// overwritten ([`record_proven_activity_status`]), as its outputs seen on chain do not show +/// that the identity was created. A store write that fails is logged: the next pass reads the +/// outcome again while the record is kept. +#[allow(clippy::too_many_arguments)] +pub(super) async fn settle_inputs_consumed_identity_create( store: &Arc>, 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, - "a charged identity create's notes were not marked spent; the next scan settles them" + "an identity create's spent notes were not marked spent; the next scan marks them" ); } - if let Some(activity_id) = activity_id { - record_activity_status_by_id( - store, - persister, - wallet_id, - id, - activity_id, - ShieldedActivityStatus::Failed, - ) - .await; + let Some(activity_id) = activity_id else { + return; + }; + record_proven_activity_status( + store, + persister, + wallet_id, + id, + activity_id, + ShieldedActivityStatus::Failed, + Some(consumed_by_height), + ) + .await; + retire_identity_create_record(store, id, Some(activity_id)).await; +} + +/// Drop the recovery record `activity_id` of an identity create whose outcome is known. A +/// create held for its outcome read ([`PendingRedrive::outcome_endpoint`]) keeps its record when +/// its notes are marked spent, so the code that learns its outcome drops it. A failure is +/// logged: the record stays held, and the next pass reads the outcome again. +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" + ); + } +} + +/// 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`] did with a held identity create. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(super) enum HeldIdentityCreate { + /// Its outcome was proven and settled: its record is retired. + Settled, + /// Nothing was proven: the record stays. + 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 settle what the +/// reads prove ([`probe_identity_create`]): +/// +/// - its identity exists: the notes are marked spent, the activity row confirmed, the record +/// retired; +/// - its notes are spent and its identity absent: settled as +/// [`settle_inputs_consumed_identity_create`] settles it; +/// - anything else, a record with no node or a transition that is not an identity create +/// included: nothing changes. +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); + match probe_identity_create(&reader, &st, identity_id).await { + IdentityCreateProbe::Created(_) => { + info!(derived_id = %identity_id, "identity create proven executed"); + if let Err(e) = + mark_nullifiers_spent(store, persister, id.wallet_id, id, &redrive.nullifiers).await + { + warn!(error = %e, "an executed identity create's notes were not marked spent"); + } + record_proven_activity_status( + store, + persister, + id.wallet_id, + id, + &redrive.activity_id, + ShieldedActivityStatus::Confirmed, + None, + ) + .await; + retire_identity_create_record(store, id, Some(&redrive.activity_id)).await; + HeldIdentityCreate::Settled + } + 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" + ); + settle_inputs_consumed_identity_create( + store, + persister, + id.wallet_id, + id, + Some(&redrive.activity_id), + &redrive.nullifiers, + consumed_by_height, + ) + .await; + HeldIdentityCreate::Settled + } + IdentityCreateProbe::Unknown => HeldIdentityCreate::Unknown, + } +} + +/// The sync pass's settlement of `id`'s identity creates held for their outcome read +/// ([`PendingRedrive::outcome_endpoint`]) whose notes the scan saw spent: the scan keeps their +/// records and leaves their activity rows as they are, and each is read at the node it was +/// delivered to ([`classify_held_identity_create`]). A create the reads do not settle counts +/// an attempt; once [`MAX_REDRIVE_ATTEMPTS`] are spent its hold is given up and its record +/// retired, as a scan that saw its notes spent would have retired it, and its row is left to +/// the scan. +pub(super) async fn classify_spent_identity_creates( + sdk: &dash_sdk::Sdk, + store: &Arc>, + persister: Option<&WalletPersister>, + id: SubwalletId, + cancel: &CancellationToken, +) { + 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; + } + }; + 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; + } + }; + redrives + .into_iter() + .filter(|redrive| { + redrive.outcome_endpoint.is_some() + && !redrive.nullifiers.is_empty() + && redrive.nullifiers.iter().all(|n| spent.contains(n)) + }) + .collect() + }; + for redrive in held { + if cancel.is_cancelled() { + return; + } + if classify_held_identity_create(sdk, store, persister, id, &redrive).await + == HeldIdentityCreate::Settled + { + continue; + } + let attempts = match store + .write() + .await + .bump_redrive_attempts(id, &redrive.activity_id) + { + Ok(attempts) => attempts, + Err(e) => { + warn!(error = %e, "held identity create: attempt not counted; retried next pass"); + continue; + } + }; + if attempts >= MAX_REDRIVE_ATTEMPTS { + warn!( + activity_id = %hex::encode(redrive.activity_id), + attempts, + "held identity create: its outcome could not be read; giving up its hold" + ); + retire_identity_create_record(store, id, Some(&redrive.activity_id)).await; + } } } @@ -4260,6 +4562,7 @@ pub(crate) async fn arm_redrive_before_broadcast( None => None, }; let redrive = PendingRedrive { + outcome_endpoint: None, activity_id, anchor, nullifiers, @@ -4607,10 +4910,10 @@ 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. Except an identity create (Type 20) whose -/// identity proven reads show absent once its notes are spent -/// ([`settle_spent_identity_create_redrive`]): a paid failure, whose notes -/// are marked spent, its record dropped and its activity row failed; +/// confirms — touch nothing. Except an identity create (Type 20) held for +/// its outcome read ([`PendingRedrive::outcome_endpoint`]): its notes spent +/// do not show that its identity was created, so its outcome is read at +/// its node and settled ([`classify_held_identity_create`]); /// - 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; @@ -4622,9 +4925,13 @@ 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, and not at all once the scan +/// saw its notes spent: [`classify_spent_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>, @@ -4674,10 +4981,28 @@ pub(super) async fn redrive_pending_spends( continue; } }; + // 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"); + continue; + } + }, + None => None, + }; + if held_at.is_some() && notes_seen_spent(store, id, &redrive.nullifiers).await { + continue; + } + let broadcaster = match &held_at { + 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 { continue; }; - let broadcast_result = st.broadcast(sdk, None).await; + let broadcast_result = st.broadcast(&broadcaster, None).await; let err_display = broadcast_result .as_ref() .err() @@ -4692,10 +5017,11 @@ pub(super) async fn redrive_pending_spends( ); } RedriveBroadcastOutcome::AlreadyExecuted => { - // A Type 20 whose notes are proven spent without its identity is a paid - // failure, not an execution: settled here, its record dropped with the notes. - if settle_spent_identity_create_redrive(sdk, store, persister, id, &redrive, &st) - .await + // A held identity create's notes spent do not show its identity created: + // read its outcome at its node. + if held_at.is_some() + && classify_held_identity_create(sdk, store, persister, id, &redrive).await + == HeldIdentityCreate::Settled { continue; } @@ -4726,47 +5052,21 @@ pub(super) async fn redrive_pending_spends( } } -/// For a redrive whose re-broadcast was refused with `NullifierAlreadySpent`: -/// when its transition `st` is an identity create (Type 20) that proven reads -/// show failed with a paid error ([`probe_identity_create`]), settle it as one -/// ([`settle_charged_identity_create`]) and return `true`. Any other -/// transition, or a create the reads do not settle, returns `false` and is -/// left to the usual handling of an executed spend. -async fn settle_spent_identity_create_redrive( - sdk: &Arc, +/// Whether every one of `nullifiers` belongs to a note of `id` the store holds spent. +async fn notes_seen_spent( store: &Arc>, - persister: Option<&WalletPersister>, id: SubwalletId, - redrive: &PendingRedrive, - st: &StateTransition, + nullifiers: &[[u8; 32]], ) -> bool { - use dpp::state_transition::state_transitions::shielded::identity_create_from_shielded_pool_transition::accessors::IdentityCreateFromShieldedPoolTransitionAccessorsV0; - - let StateTransition::IdentityCreateFromShieldedPool(create) = st else { - return false; - }; - let identity_id = create.identity_id(); - let IdentityCreateProbe::ChargedFailure { consumed_by_height } = - probe_identity_create(sdk, st, identity_id).await - else { + let Ok(notes) = store.read().await.get_all_notes(id) else { return false; }; - warn!( - derived_id = %identity_id, - consumed_by_height, - "redrive: identity create's notes proven spent and its identity proven absent; \ - it failed with a paid error" - ); - settle_charged_identity_create( - store, - persister, - id.wallet_id, - id, - Some(&redrive.activity_id), - &redrive.nullifiers, - ) - .await; - true + 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 @@ -5109,6 +5409,7 @@ mod redrive_tests { let id = SubwalletId::new(wallet_id, 3); let mut store = InMemoryShieldedStore::new(); let archived = PendingRedrive { + outcome_endpoint: None, activity_id: [6; 32], anchor: [1; 32], nullifiers: vec![], @@ -5162,6 +5463,7 @@ mod redrive_tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, activity_id, anchor: [1; 32], nullifiers: vec![], @@ -5233,6 +5535,7 @@ mod redrive_tests { .arm_redrive( SubwalletId::new(wallet_id, 3), PendingRedrive { + outcome_endpoint: None, activity_id: [6; 32], anchor: [1; 32], nullifiers: vec![], @@ -5264,6 +5567,7 @@ mod redrive_tests { .arm_redrive( SubwalletId::new(wallet_id, 0), PendingRedrive { + outcome_endpoint: None, activity_id: [8; 32], anchor: [1; 32], nullifiers: vec![], @@ -5376,6 +5680,7 @@ mod redrive_tests { .arm_redrive( previous_account, PendingRedrive { + outcome_endpoint: None, activity_id, anchor: [0xAB; 32], nullifiers: vec![], @@ -5751,6 +6056,7 @@ mod redrive_tests { held.arm_private_contact_payment( id, PendingRedrive { + outcome_endpoint: None, activity_id: [0x79; 32], anchor: [7; 32], nullifiers: vec![note.nullifier], @@ -6432,6 +6738,7 @@ mod redrive_tests { .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, activity_id, anchor, nullifiers: vec![], @@ -6471,6 +6778,7 @@ mod redrive_tests { let id = SubwalletId::new(wallet_id, 0); let rec = |activity: u8, anchor: u8, attempts: u32| PendingRedrive { + outcome_endpoint: None, activity_id: [activity; 32], anchor: [anchor; 32], nullifiers: vec![[activity ^ 0xFF; 32]], @@ -6531,6 +6839,7 @@ 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, activity_id: [activity; 32], anchor, nullifiers: vec![[activity ^ 0xFF; 32]], @@ -7706,7 +8015,7 @@ mod redrive_tests { ); } - // ── Identity create: a paid failure, proven by reads ─────────────── + // ── Identity create: inputs consumed without the identity ────────── /// The identity a mock read returns for the tests' identity create. fn created_identity() -> Identity { @@ -7723,12 +8032,12 @@ mod redrive_tests { dash_sdk::Error::Generic("no proof".to_string()) } - /// The identity create is a paid failure 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. + /// 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_charged_only_when_its_notes_are_spent_and_its_identity_absent( + fn should_judge_an_identity_create_consumed_only_when_its_notes_are_spent_and_its_identity_absent( ) { let spent_by = |height| { Ok(TransitionOutcome::ExecutedOrSuperseded { @@ -7739,13 +8048,13 @@ mod redrive_tests { assert!(matches!( judge_identity_create_reads(spent_by(10), absent_at(10)), - IdentityCreateProbe::ChargedFailure { + IdentityCreateProbe::InputsConsumed { consumed_by_height: 10 } )); assert!(matches!( judge_identity_create_reads(spent_by(10), absent_at(12)), - IdentityCreateProbe::ChargedFailure { + IdentityCreateProbe::InputsConsumed { consumed_by_height: 10 } )); @@ -7785,7 +8094,7 @@ mod redrive_tests { ), IdentityCreateProbe::Unknown ), - "a note left free never took the paid path" + "a note left free: not this outcome" ); assert!( matches!( @@ -7804,15 +8113,59 @@ mod redrive_tests { )); } - /// The nullifiers of the tests' identity create, as its transition lists them. - fn identity_create_nullifiers() -> Vec<[u8; 32]> { - vec![spend_action().nullifier, spend_action().nullifier] + /// 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() } - /// A mock SDK whose proven reads show the tests' identity create's - /// anchor recorded, its nullifiers `spent` or not, and its identity - /// absent. Every answer of a mock SDK is at height 0. - async fn sdk_reading_identity_create(spent: bool) -> dash_sdk::Sdk { + /// 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, @@ -7822,6 +8175,8 @@ mod redrive_tests { .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 @@ -7833,67 +8188,203 @@ mod redrive_tests { ) .await .expect("the newest anchor"); - let statuses = identity_create_nullifiers() - .into_iter() + let nullifiers = transition_nullifiers(transition); + let statuses = nullifiers + .iter() .map(|nullifier| ShieldedNullifierStatus { - nullifier, + nullifier: *nullifier, is_spent: spent, }) .collect(); sdk.mock() .expect_fetch( - ShieldedNullifiersQuery(identity_create_nullifiers()), + ShieldedNullifiersQuery(nullifiers), Some(ShieldedNullifierStatuses(statuses)), ) .await .expect("the nullifier statuses"); sdk.mock() - .expect_fetch(Identifier::from(CREATED_IDENTITY), None::) + .expect_fetch( + Identifier::from(CREATED_IDENTITY), + created.then(created_identity), + ) .await - .expect("the identity, absent"); + .expect("the identity"); sdk } /// The probe reads the create's outcome and its identity with proofs: its - /// nullifiers spent and its identity absent is a paid failure, its - /// nullifiers free is not, and reads that fail prove nothing. + /// 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 = SpendPath::IdentityCreate.transition(); + let transition = identity_create_spending(&[spend_note()]); let identity_id = Identifier::from(CREATED_IDENTITY); - let spent = Arc::new(sdk_reading_identity_create(true).await); + let spent = sdk_reading(&transition, true, false).await; assert!(matches!( probe_identity_create(&spent, &transition, identity_id).await, - IdentityCreateProbe::ChargedFailure { + IdentityCreateProbe::InputsConsumed { consumed_by_height: 0 } )); - - let free = Arc::new(sdk_reading_identity_create(false).await); + 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 = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + 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 = [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, + &spend.entry, + SPEND_ANCHOR, + SPEND_ANCHOR_HEIGHT, + &spend.notes, + &mut spend.reservation, + &transition, + Identifier::from(CREATED_IDENTITY), + BTreeMap::new(), + ) + .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, + PlatformWalletError::ShieldedIdentityCreateInputsConsumed { + identity_id, + consumed_by_height: 0, + .. + } if identity_id == Identifier::from(CREATED_IDENTITY) + ), + "{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::Failed); + assert_eq!(row.block_height, Some(0)); + drop(store); + assert!( + FileBackedShieldedStore::open_path(&path, 100) + .expect("reopened") + .pending_redrives(id) + .expect("records") + .is_empty(), + "the record is retired for good" + ); + } + } + /// An identity create whose saved record is the only thing left of it - /// after a result wait failed: its note reserved, its record saved and - /// its activity row pending, in the store at `path`. + /// 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], - error: PlatformWalletError, - ) -> (ReservedSpend, SpendFailure) { + held: bool, + ) -> (Arc>, StateTransition) { let mut spend = ReservedSpend::at(path, id, activity_id).await; - let transition = SpendPath::IdentityCreate.transition(); + let transition = identity_create_spending(&spend.notes); let failure = save_spend_then_broadcast( &spend.store, id, @@ -7904,144 +8395,116 @@ mod redrive_tests { &mut spend.reservation, &transition, IDENTITY_CREATE_OPERATION, - || async { Err::<(), _>(SpendFailure::from(error)) }, - ) - .await - .expect_err("the create failed"); - (spend, failure) - } - - /// An identity create proven to have failed with a paid error reports - /// it, and its notes are gone: they are marked spent, its record is - /// dropped, across a restart too, and its activity row reads failed. - #[tokio::test] - async fn should_settle_a_charged_identity_create_as_spent_and_failed() { - let wallet_id = [0x74; 32]; - let id = SubwalletId::new(wallet_id, 0); - let activity_id = [0x7D; 32]; - let dir = tempfile::tempdir().expect("a directory"); - let path = dir.path().join("tree.sqlite"); - let (spend, failure) = saved_identity_create( - &path, - id, - activity_id, - PlatformWalletError::ShieldedIdentityCreateChargedFailure { - identity_id: Identifier::from(CREATED_IDENTITY), - consumed_by_height: 9, - reason: "the result wait reported a key-state error".to_string(), + || async { + Err::<(), _>(SpendFailure::from( + PlatformWalletError::ShieldedSpendUnconfirmed { + operation: IDENTITY_CREATE_OPERATION, + reason: "the result wait timed out".to_string(), + }, + )) }, ) - .await; + .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 settled = SpendPath::IdentityCreate - .settle(spend, wallet_id, id, failure) + let _ = SpendPath::IdentityCreate + .settle(spend, wallet_id_of(id), id, failure) .await; - assert!( - matches!( - settled, - PlatformWalletError::ShieldedIdentityCreateChargedFailure { - identity_id, - consumed_by_height: 9, - .. - } if identity_id == Identifier::from(CREATED_IDENTITY) - ), - "{settled:?}" - ); - assert_eq!( - spendable_and_status(&store, id, &activity_id).await, - (0, ShieldedActivityStatus::Failed) - ); - assert!( - store.read().await.get_all_notes(id).expect("notes")[0].is_spent, - "the note is spent" - ); - drop(store); - assert!( - FileBackedShieldedStore::open_path(&path, 100) - .expect("reopened") - .pending_redrives(id) - .expect("records") - .is_empty(), - "the record is dropped for good" - ); + (store, transition) } - /// A redrive pass whose re-broadcast of an identity create was refused - /// with `NullifierAlreadySpent` settles the create as a paid failure - /// only when proven reads show its identity absent once its notes are - /// spent. Otherwise the record is left to the handling of a spend that - /// executed. - #[tokio::test] - async fn should_settle_a_redriven_identity_create_whose_notes_are_spent_without_its_identity() { - use dpp::serialization::PlatformDeserializableTrusted; + 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 settles it as inputs consumed (exactly its real + /// inputs marked spent), its identity found settles it confirmed, and + /// its notes free leave it. 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 spent in [true, false] { + 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 path = dir.path().join("tree.sqlite"); - let (spend, failure) = saved_identity_create( - &path, - id, - activity_id, - PlatformWalletError::ShieldedSpendUnconfirmed { - operation: IDENTITY_CREATE_OPERATION, - reason: "the result wait timed out".to_string(), - }, - ) - .await; - let store = Arc::clone(&spend.store); - let _ = SpendPath::IdentityCreate - .settle(spend, wallet_id, id, failure) - .await; + 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(); - let st = StateTransition::deserialize_from_bytes_trusted(&redrive.st_bytes) - .expect("the saved transition"); + assert_eq!(redrive.outcome_endpoint.is_some(), held, "{case}"); + let sdk = sdk_reading(&transition, spent, created).await; - let sdk = Arc::new(sdk_reading_identity_create(spent).await); - let settled = - settle_spent_identity_create_redrive(&sdk, &store, None, id, &redrive, &st).await; - let (spendable, status) = spendable_and_status(&store, id, &activity_id).await; + let classified = classify_held_identity_create(&sdk, &store, None, id, &redrive).await; let records = store.read().await.pending_redrives(id).expect("records"); - assert_eq!(settled, spent, "spent: {spent}"); - assert_eq!( - spendable, 0, - "spent: {spent}: the note stays out of selection" - ); - if spent { - assert!(records.is_empty(), "the record is dropped"); - assert_eq!(status, ShieldedActivityStatus::Failed); - assert!(store.read().await.get_all_notes(id).expect("notes")[0].is_spent); - } else { - assert_eq!(records.len(), 1, "the record stays"); - assert_eq!(status, ShieldedActivityStatus::Pending); + let row = activity_row(&store, id, &activity_id).await; + match (held, spent, created) { + (true, true, false) => { + assert_eq!(classified, HeldIdentityCreate::Settled, "{case}"); + assert!(records.is_empty(), "{case}"); + assert_eq!(row.status, ShieldedActivityStatus::Failed, "{case}"); + assert_eq!(row.block_height, Some(0), "{case}"); + assert_eq!(spent_nullifiers(&store, id).await, redrive.nullifiers); + assert!(redrive + .nullifiers + .iter() + .all(|n| transition_nullifiers(&transition).contains(n))); + } + (true, true, true) => { + assert_eq!(classified, HeldIdentityCreate::Settled, "{case}"); + assert!(records.is_empty(), "{case}"); + assert_eq!(row.status, ShieldedActivityStatus::Confirmed, "{case}"); + assert_eq!(spent_nullifiers(&store, id).await, redrive.nullifiers); + } + _ => { + assert_eq!(classified, HeldIdentityCreate::Unknown, "{case}"); + assert_eq!(records.len(), 1, "{case}"); + assert_eq!(row.status, ShieldedActivityStatus::Pending, "{case}"); + } } } + } - // Any other transition is left alone, unread. - let dir = tempfile::tempdir().expect("a directory"); - let store = Arc::new(RwLock::new( - FileBackedShieldedStore::open_path(dir.path().join("tree.sqlite"), 100) - .expect("the store"), - )); - let transfer = SpendPath::Shared.transition(); - let redrive = PendingRedrive { - activity_id, - anchor: SPEND_ANCHOR, - nullifiers: vec![spend_note().nullifier], - st_bytes: transfer.serialize_to_bytes().expect("serializes"), - attempts: 0, - anchor_height: Some(SPEND_ANCHOR_HEIGHT), - identity_nonce_finalized: false, - identity_user_abandoned: false, - }; - let sdk = Arc::new(sdk_reading_identity_create(true).await); - assert!( - !settle_spent_identity_create_redrive(&sdk, &store, None, id, &redrive, &transfer) + /// A held identity create keeps its record when its notes are marked + /// spent, as a scan marks them: the notes spent do not show what it did. + /// Any other record is retired with its notes, as before. + #[tokio::test] + async fn should_keep_a_held_identity_create_s_record_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(), usize::from(held), "held {held}"); + if held { + assert_eq!(records[0].outcome_endpoint.as_deref(), Some(ENDPOINT)); + } + } } } diff --git a/packages/rs-platform-wallet/src/wallet/shielded/store.rs b/packages/rs-platform-wallet/src/wallet/shielded/store.rs index b2672e56d00..367e439ad64 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/store.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/store.rs @@ -185,6 +185,14 @@ 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. A + /// record that has one is held for that read: seeing its notes spent + /// ([`ShieldedStore::mark_spent`]) does not drop it, as its notes spent do + /// not show whether the identity was created. `None` for every other + /// spend, and for a create whose delivery no node was kept for. + pub outcome_endpoint: Option, } /// The DashPay private contact request a saved spend pays for: which identity @@ -720,6 +728,16 @@ 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`. 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; + /// Increment the attempt counter on `id`'s redrive keyed by /// `activity_id`, returning the new count (`0` when no such record /// exists). @@ -734,7 +752,8 @@ 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::outcome_endpoint`]). fn clear_redrive(&mut self, id: SubwalletId, activity_id: &[u8; 32]) -> Result<(), Self::Error>; @@ -1067,7 +1086,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 +1110,22 @@ impl SubwalletState { dropped } + /// [`Self::drop_redrives_containing`] for a spend seen landed: a record held + /// for its outcome read ([`PendingRedrive::outcome_endpoint`]) 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.outcome_endpoint.is_none() && 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 +1280,19 @@ impl SubwalletState { self.redrives.get(activity_id).map(|r| r.attempts) } + /// Set the record's [`PendingRedrive::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())) + .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 +1683,18 @@ 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 bump_redrive_attempts( &mut self, id: SubwalletId, @@ -2027,6 +2087,7 @@ mod tests { }; store.save_note(id, ¬e).unwrap(); let redrive = PendingRedrive { + outcome_endpoint: None, activity_id: [9u8; 32], anchor: [8u8; 32], nullifiers: vec![n1, n2], @@ -2424,6 +2485,7 @@ mod tests { let id = SubwalletId::new(wallet_id, 4); let mut store = InMemoryShieldedStore::new(); let identity_guard = PendingRedrive { + outcome_endpoint: None, activity_id: [0x11; 32], anchor: [0x22; 32], nullifiers: vec![], @@ -2434,6 +2496,7 @@ mod tests { anchor_height: None, }; let note_spend = PendingRedrive { + outcome_endpoint: 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..09c1c055e54 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,7 @@ async fn bind_persists_viewing_keys_and_restart_rebinds_seedlessly() { .arm_redrive( subwallet, crate::wallet::shielded::store::PendingRedrive { + outcome_endpoint: None, activity_id: [7; 32], anchor: [8; 32], nullifiers: vec![[2; 32]], @@ -1074,6 +1075,7 @@ async fn should_keep_wallet_and_coordinator_keys_when_guarded_registration_is_re .arm_redrive( id, PendingRedrive { + outcome_endpoint: None, activity_id: [1; 32], anchor: [2; 32], nullifiers: vec![], diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift index aedabb5954e..053c8d1c0dd 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift @@ -322,11 +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 created from the shielded pool failed with a paid error, - /// as proven reads show: its notes are spent and the identity is absent. - /// The denomination minus a penalty went to the transition's fallback - /// address; the wallet marked the notes spent. Do not re-submit. - case errorShieldedIdentityCreateChargedFailure = 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. 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. @@ -478,8 +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_CHARGED_FAILURE: - self = .errorShieldedIdentityCreateChargedFailure + 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: @@ -768,14 +768,15 @@ public enum PlatformWalletError: LocalizedError { /// 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. Platform's paid failure path for this transition (a key-state - /// check failing in the block, such as a public-key hash already - /// registered) credits the denomination minus a penalty to the - /// `sendToAddressOnCreationFailure` address. The wallet marked the notes - /// spent and dropped the saved transition; do not re-submit. The same - /// notes spent by another transition (another device with this wallet's - /// keys) read the same, and then nothing went to the fallback address. - case shieldedIdentityCreateChargedFailure(String) + /// 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 @@ -997,7 +998,7 @@ public enum PlatformWalletError: LocalizedError { .publicContactRequestUnconfirmed(let m), .walletAlreadyExists(let m), .shieldedBroadcastFailed(let m), .shieldedBroadcastUnconfirmed(let m), .shieldedSpendUnconfirmed(let m), - .shieldedIdentityCreateChargedFailure(let m), + .shieldedIdentityCreateInputsConsumed(let m), .shieldedNoRecordedAnchor(let m), .shieldedInsufficientBalance(let m), .shieldedIdentityDebitPending(let m), .shieldedRecoveryCorrupted(let m), .shieldedRecoveryKeysRequired(let m), @@ -1136,8 +1137,8 @@ public enum PlatformWalletError: LocalizedError { case .errorWalletAlreadyExists: self = .walletAlreadyExists(detail) case .errorShieldedBroadcastFailed: self = .shieldedBroadcastFailed(detail) case .errorShieldedBroadcastUnconfirmed: self = .shieldedBroadcastUnconfirmed(detail) - case .errorShieldedIdentityCreateChargedFailure: - self = .shieldedIdentityCreateChargedFailure(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/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift index a66c85d9948..9e343a05817 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/ErrorHandlingTests.swift @@ -604,18 +604,19 @@ final class ErrorHandlingTests: XCTestCase { XCTAssertEqual(message, detail) } - /// A shielded identity create proven to have failed with a paid error - /// keeps its own code and typed case, apart from an unconfirmed create (17). - func testShouldReportAChargedShieldedIdentityCreateByItsOwnCode() { + /// 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_CHARGED_FAILURE + ffi: PLATFORM_WALLET_FFI_RESULT_CODE_ERROR_SHIELDED_IDENTITY_CREATE_INPUTS_CONSUMED ) - XCTAssertEqual(code, .errorShieldedIdentityCreateChargedFailure) + XCTAssertEqual(code, .errorShieldedIdentityCreateInputsConsumed) XCTAssertEqual(code.rawValue, 77) - let detail = "Shielded identity create failed with a paid error" - guard case .shieldedIdentityCreateChargedFailure(let message) = + 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 charged identity create error") + return XCTFail("lost typed inputs-consumed identity create error") } XCTAssertEqual(message, detail) } From f1967248e4b1d64802f4bd24ed28bab80cadb15c Mon Sep 17 00:00:00 2001 From: romchornyi Date: Wed, 11 Nov 2026 17:14:05 +0200 Subject: [PATCH 4/8] fix: keep a held identity create's proven outcome until its row is durable A Type-20 create's recovery record is now held from its save (`PendingRedrive::identity_outcome`, `Awaiting`), before any dispatch, so a scan that sees its notes spent before its node is kept does not drop it. A proven outcome (`Created`, `InputsConsumed`) is kept durably with the record instead of retiring it. The sync pass writes the row with that outcome into its own changeset after the scan-derived rows, replacing any entry for the same id, and drops the record only once the host holds the changeset durably (inline store or successful flush); otherwise the next pass, after a restart too, writes it again from the kept outcome. A row the scan rebuilds for a held create is Pending, never Confirmed. A create whose reads prove nothing past `MAX_REDRIVE_ATTEMPTS` (shared with re-broadcasts, one read past them) is no longer released to the scan: it stays held and unconfirmed. `ShieldedStore` gains `set_redrive_identity_outcome`; its docs state what a custom store must keep for the hold. Co-Authored-By: Claude Opus 5.5 --- .../ERROR_CODE_REGISTRY.md | 2 +- packages/rs-platform-wallet-ffi/src/error.rs | 7 +- .../src/shielded_send.rs | 1 + packages/rs-platform-wallet/README.md | 2 +- packages/rs-platform-wallet/src/error.rs | 5 +- .../rs-platform-wallet/src/manager/load.rs | 1 + .../src/wallet/platform_wallet.rs | 1 + .../src/wallet/shielded/balance_tests.rs | 1 + .../src/wallet/shielded/coordinator.rs | 569 ++++++++++++++++-- .../src/wallet/shielded/file_store.rs | 143 ++++- .../src/wallet/shielded/operations.rs | 446 +++++++++----- .../src/wallet/shielded/store.rs | 128 +++- .../wallet/shielded/viewing_key_bind_tests.rs | 2 + 13 files changed, 1091 insertions(+), 217 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md index fb0227bf764..f895312401c 100644 --- a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md +++ b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md @@ -194,7 +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, the recovery record retired and the activity row failed; 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`) | +| 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 947c23a25d3..8b8bb34fb65 100644 --- a/packages/rs-platform-wallet-ffi/src/error.rs +++ b/packages/rs-platform-wallet-ffi/src/error.rs @@ -773,9 +773,10 @@ pub enum PlatformWalletFFIResultCode { /// 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, - /// retired the create's recovery record and marked its activity row - /// failed; nothing is to be re-submitted. The reads do not show what + /// 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 diff --git a/packages/rs-platform-wallet-ffi/src/shielded_send.rs b/packages/rs-platform-wallet-ffi/src/shielded_send.rs index cba40465299..69ed977138c 100644 --- a/packages/rs-platform-wallet-ffi/src/shielded_send.rs +++ b/packages/rs-platform-wallet-ffi/src/shielded_send.rs @@ -2497,6 +2497,7 @@ mod tests { 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 ebbf532d289..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. 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, its redrive record retired 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. The node is kept with the redrive record, which holds it for that read: a scan that sees its notes spent keeps the record and leaves its row as it is, and the sync pass reads the outcome at the node and settles it (confirmed when the identity is found, failed when it is proven absent), overwriting a row the scan confirmed. A creation with no node kept is never read about: it stays unconfirmed, and the scan settles it as before. 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). +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/error.rs b/packages/rs-platform-wallet/src/error.rs index 1939868c9d0..7b9b523ac22 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -1116,8 +1116,9 @@ pub enum PlatformWalletError { /// 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, retires the create's recovery record and - /// marks its activity row failed; nothing is to be re-submitted. + /// 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), diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 9c112e2d3fa..ccdc7e029a1 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -2116,6 +2116,7 @@ mod private_payment_hold_tests { 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 06f5717c716..ca5f5797bcf 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -3501,6 +3501,7 @@ mod private_contact_request_redrive_tests { 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/balance_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs index 66ba2900748..ba29d46a7c4 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/balance_tests.rs @@ -359,6 +359,7 @@ async fn local_balance_restores_funds_and_durable_pending_reservations_offline() 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 fd32154337b..91fcbdb1eeb 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -1598,14 +1598,18 @@ impl NetworkShieldedCoordinator { tracing::warn!(error = %e, "Shielded activity derivation failed; skipping this pass"); } - // Identity creates held for their outcome read whose notes this scan - // (or an earlier one) saw spent: the scan kept their records and left - // their rows alone, as their notes spent do not show whether the - // identity was created. Each is read at the node it was delivered to. - if !cancelled { - self.classify_spent_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 @@ -1631,34 +1635,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 @@ -2361,10 +2341,21 @@ impl NetworkShieldedCoordinator { // rows and records wait for that read. let held: HashSet<[u8; 32]> = redrives .iter() - .filter(|redrive| redrive.outcome_endpoint.is_some()) + .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() @@ -2393,7 +2384,7 @@ impl NetworkShieldedCoordinator { .into_iter() .filter_map(|(owner, redrive)| { (!redrive.nullifiers.is_empty() - && redrive.outcome_endpoint.is_none() + && !redrive.held_for_identity_outcome() && redrive.activity_id == entry.id) .then_some(owner) }) @@ -2488,29 +2479,109 @@ 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 wallet with no changeset this pass had no row to write. + if held_durably.get(&id.wallet_id) == Some(&false) { + continue; + } + super::operations::retire_settled_identity_creates(&self.store, id, &activity_ids) + .await; + } + } + /// Settle the identity creates of `subwallets` held for their outcome - /// read whose notes the store holds spent - /// ([`super::operations::classify_spent_identity_creates`]), each with - /// its wallet's persister. - async fn classify_spent_identity_creates( + /// 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() { - return; + break; } let persister = self.persisters.read().await.get(&id.wallet_id).cloned(); - super::operations::classify_spent_identity_creates( + 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 @@ -2877,7 +2948,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; @@ -2975,6 +3047,7 @@ mod tests { ( PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: compute_activity_id(&cmxs), anchor: [1; 32], nullifiers: vec![], @@ -3411,7 +3484,7 @@ mod tests { // 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 - // `classify_spent_identity_creates`. + // `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"; @@ -3514,6 +3587,15 @@ mod tests { 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"); @@ -3529,7 +3611,7 @@ mod tests { .viewing_keys(); let mut account_views = BTreeMap::new(); account_views.insert(0u32, views); - let persister = WalletPersister::new([0x11; 32], Arc::new(NoPlatformPersistence)); + let persister = WalletPersister::new([0x11; 32], host); coordinator .register_wallet([0x11; 32], account_views, persister) .await @@ -3562,6 +3644,7 @@ mod tests { identity_nonce_finalized: false, identity_user_abandoned: false, outcome_endpoint: held.then(|| HELD_ENDPOINT.to_string()), + identity_outcome: held.then_some(IdentityCreateOutcome::Awaiting), }, ) .unwrap(); @@ -3628,15 +3711,127 @@ mod tests { 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 ShieldedChangeSet::default()) + .derive_activity_into_changeset(&subwallets, &mut changeset) .await .unwrap(); + let settled = coordinator + .settle_held_identity_creates(&subwallets, &mut changeset, &CancellationToken::new()) + .await; coordinator - .classify_spent_identity_creates(&subwallets, &CancellationToken::new()) + .persist_pass_changeset(&changeset, settled) .await; } + /// 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. + struct RecordingHost { + mode: std::sync::Mutex, + buffered: std::sync::Mutex>, + durable: std::sync::Mutex< + BTreeMap<[u8; 32], crate::wallet::shielded::activity::ShieldedActivityEntry>, + >, + } + + impl RecordingHost { + fn new(mode: HostMode) -> Arc { + Arc::new(Self { + mode: std::sync::Mutex::new(mode), + buffered: Default::default(), + durable: Default::default(), + }) + } + + fn mode(&self) -> HostMode { + *self.mode.lock().unwrap() + } + + 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() + } + + /// A process exit: what was not flushed is gone. + fn crash(&self) { + self.buffered.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 rows: Vec<_> = changeset + .shielded + .into_iter() + .flat_map(|shielded| 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); + } + Ok(()) + } + HostMode::FlushFails => { + self.buffered.lock().unwrap().extend(rows); + 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); + } + Ok(()) + } + + fn load( + &self, + ) -> Result + { + Ok(Default::default()) + } + } + /// The create's activity row: its status and height. async fn create_row( coordinator: &NetworkShieldedCoordinator, @@ -3778,7 +3973,8 @@ mod tests { } /// A create settled before the scan reaches it (the direct path proved - /// its notes spent and its identity absent): its row stays failed when + /// 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() { @@ -3799,13 +3995,18 @@ mod tests { 7, ) .await; - assert!(coordinator + let records = coordinator .store() .read() .await .pending_redrives(id) - .unwrap() - .is_empty()); + .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; @@ -3814,6 +4015,13 @@ mod tests { create_row(&coordinator, id).await, (ShieldedActivityStatus::Failed, Some(7)) ); + assert!(coordinator + .store() + .read() + .await + .pending_redrives(id) + .unwrap() + .is_empty()); let _ = std::fs::remove_dir_all(&dir); } @@ -3879,6 +4087,253 @@ mod tests { } } + /// 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::Failed, 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::Failed + ); + 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::Failed, + "{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::Failed, 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); + } + } + + /// 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 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::Failed, 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() @@ -4024,6 +4479,7 @@ mod tests { 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![], @@ -4402,6 +4858,7 @@ mod tests { .clone(); let identity_guard = super::super::store::PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x71; 32], anchor: [0x72; 32], nullifiers: vec![], @@ -4517,6 +4974,7 @@ mod tests { id, super::super::store::PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x71; 32], anchor: [0x72; 32], nullifiers: vec![], @@ -5019,6 +5477,7 @@ mod tests { id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: *activity, anchor: *anchor, nullifiers: vec![*nullifier], @@ -5125,6 +5584,7 @@ mod tests { id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: activity, anchor, nullifiers: vec![nullifier], @@ -5225,6 +5685,7 @@ mod tests { 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 03fdaea42d7..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, outcome_endpoint) \ - VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)", + (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, @@ -210,6 +211,8 @@ impl FileBackedShieldedStore { .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}")))?; @@ -495,6 +498,23 @@ impl FileBackedShieldedStore { 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. @@ -592,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, outcome_endpoint 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 @@ -630,6 +651,15 @@ impl FileBackedShieldedStore { // 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)?, @@ -649,6 +679,7 @@ impl FileBackedShieldedStore { private_contact_payment, anchor_height, outcome_endpoint, + identity_outcome, decoded, )) }) @@ -660,6 +691,7 @@ impl FileBackedShieldedStore { private_contact_payment, anchor_height, outcome_endpoint, + identity_outcome, decoded, ) = row.map_err(|e| FileShieldedStoreError::RecoveryCorrupted { account_index: None, @@ -746,6 +778,7 @@ impl FileBackedShieldedStore { SubwalletId::new(wallet_id, account_index), PendingRedrive { outcome_endpoint, + identity_outcome, activity_id, anchor, nullifiers, @@ -945,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; @@ -1428,13 +1498,15 @@ impl ShieldedStore for FileBackedShieldedStore { { let conn = self.pending_conn.lock().expect("pending_conn mutex"); conn.execute( - "UPDATE shielded_pending_spends SET outcome_endpoint = ?4 \ + "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| { @@ -1447,6 +1519,45 @@ impl ShieldedStore for FileBackedShieldedStore { .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, @@ -1827,6 +1938,7 @@ mod tests { 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]], @@ -1890,6 +2002,7 @@ mod tests { 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]], @@ -1964,6 +2077,7 @@ mod tests { 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]], @@ -2045,6 +2159,7 @@ mod tests { 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![], @@ -2144,6 +2259,7 @@ mod tests { SubwalletId::new([1; 32], 2), PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [2; 32], anchor: [3; 32], nullifiers: vec![], @@ -2202,6 +2318,7 @@ mod tests { .into(); let record = PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x11; 32], anchor: [0x52; 32], nullifiers: vec![[0x13; 32]], @@ -2317,6 +2434,7 @@ mod tests { .into(); let guard = PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x61; 32], anchor: [0x71; 32], nullifiers: vec![], @@ -2333,6 +2451,7 @@ mod tests { note_id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x21; 32], anchor: [0x51; 32], nullifiers: vec![[0x91; 32]], @@ -2400,6 +2519,7 @@ mod tests { id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0xD1; 32], anchor: [0xC1; 32], nullifiers: vec![], @@ -2455,6 +2575,7 @@ mod tests { id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0xD2; 32], anchor: [0xF1; 32], nullifiers: vec![[0xC2; 32]], @@ -2490,6 +2611,7 @@ mod tests { 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![], @@ -2519,6 +2641,7 @@ mod tests { 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![], @@ -2570,6 +2693,7 @@ mod tests { 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![], @@ -2581,6 +2705,7 @@ mod tests { }; let note_spend = PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x48; 32], anchor: [0x59; 32], nullifiers: vec![[0x6A; 32]], @@ -2628,6 +2753,7 @@ mod tests { 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]], @@ -2707,6 +2833,7 @@ mod tests { id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x7; 32], anchor: [0x22; 32], nullifiers: vec![nf], @@ -3132,6 +3259,7 @@ mod tests { }; let redrive = PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x21; 32], anchor: [0x22; 32], nullifiers: vec![[0x23; 32], [0x24; 32]], @@ -3287,6 +3415,7 @@ mod tests { 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]], @@ -3389,6 +3518,7 @@ mod tests { id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x31; 32], anchor: [0x32; 32], nullifiers: vec![[0x33; 32]], @@ -3455,6 +3585,7 @@ mod tests { 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 add7cb1438f..0e6e4fdc279 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs @@ -29,8 +29,8 @@ 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_tracking_endpoint, broadcast_was_refused, @@ -1014,6 +1014,7 @@ async fn arm_identity_shield_redrive( id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: entry.id, anchor: transition.anchor(), nullifiers: vec![], @@ -2953,7 +2954,7 @@ where ); } // A create held for its outcome read keeps its record when its notes are marked - // spent (`PendingRedrive::outcome_endpoint`); its outcome is known now. + // spent (`PendingRedrive::identity_outcome`); its outcome is known now. retire_identity_create_record(store, id, pending_entry.as_ref().map(|entry| &entry.id)) .await; info!( @@ -3408,14 +3409,14 @@ fn judge_identity_create_reads( } /// Settle an identity create proven unable to execute without its identity -/// ([`PlatformWalletError::ShieldedIdentityCreateInputsConsumed`]): its notes are spent on -/// chain, so they are marked spent, which also clears their reservations; its recovery record -/// `activity_id` is retired; and its activity row reads failed at `consumed_by_height`, a height -/// by which its notes were spent. The height keeps the row failed: the scan upgrades only rows -/// that are pending or carry no height, and a row of this create the scan already confirmed is -/// overwritten ([`record_proven_activity_status`]), as its outputs seen on chain do not show -/// that the identity was created. A store write that fails is logged: the next pass reads the -/// outcome again while the record is kept. +/// ([`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>, @@ -3435,6 +3436,13 @@ pub(super) async fn settle_inputs_consumed_identity_create( 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, @@ -3445,13 +3453,12 @@ pub(super) async fn settle_inputs_consumed_identity_create( Some(consumed_by_height), ) .await; - retire_identity_create_record(store, id, Some(activity_id)).await; } /// Drop the recovery record `activity_id` of an identity create whose outcome is known. A -/// create held for its outcome read ([`PendingRedrive::outcome_endpoint`]) keeps its record when -/// its notes are marked spent, so the code that learns its outcome drops it. A failure is -/// logged: the record stays held, and the next pass reads the outcome again. +/// create held for its outcome read ([`PendingRedrive::held_for_identity_outcome`]) keeps its +/// record when its notes are marked spent, so the code that learns its outcome drops it. A +/// failure is logged: the record stays held, and the next pass reads the outcome again. async fn retire_identity_create_record( store: &Arc>, id: SubwalletId, @@ -3469,6 +3476,52 @@ async fn retire_identity_create_record( } } +/// 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; + } +} + +/// 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. @@ -3536,25 +3589,26 @@ async fn record_proven_activity_status( queue_shielded_changeset(persister, wallet_id, changeset_for_entry(id, next)); } -/// What [`classify_held_identity_create`] did with a held identity create. +/// 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 settled: its record is retired. - Settled, - /// Nothing was proven: the record stays. + /// 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 settle what the -/// reads prove ([`probe_identity_create`]): +/// 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`]. /// -/// - its identity exists: the notes are marked spent, the activity row confirmed, the record -/// retired; -/// - its notes are spent and its identity absent: settled as -/// [`settle_inputs_consumed_identity_create`] settles it; -/// - anything else, a record with no node or a transition that is not an identity create -/// included: nothing changes. +/// 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>, @@ -3583,26 +3637,10 @@ pub(super) async fn classify_held_identity_create( }; let identity_id = create.identity_id(); let reader = sdk.scoped_to_address(endpoint); - match probe_identity_create(&reader, &st, identity_id).await { + let outcome = match probe_identity_create(&reader, &st, identity_id).await { IdentityCreateProbe::Created(_) => { info!(derived_id = %identity_id, "identity create proven executed"); - if let Err(e) = - mark_nullifiers_spent(store, persister, id.wallet_id, id, &redrive.nullifiers).await - { - warn!(error = %e, "an executed identity create's notes were not marked spent"); - } - record_proven_activity_status( - store, - persister, - id.wallet_id, - id, - &redrive.activity_id, - ShieldedActivityStatus::Confirmed, - None, - ) - .await; - retire_identity_create_record(store, id, Some(&redrive.activity_id)).await; - HeldIdentityCreate::Settled + IdentityCreateOutcome::Created } IdentityCreateProbe::InputsConsumed { consumed_by_height } => { warn!( @@ -3611,43 +3649,55 @@ pub(super) async fn classify_held_identity_create( "identity create proven unable to execute: its notes are spent and its \ identity absent" ); - settle_inputs_consumed_identity_create( - store, - persister, - id.wallet_id, - id, - Some(&redrive.activity_id), - &redrive.nullifiers, - consumed_by_height, - ) - .await; - HeldIdentityCreate::Settled + IdentityCreateOutcome::InputsConsumed { consumed_by_height } } - IdentityCreateProbe::Unknown => HeldIdentityCreate::Unknown, + 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::outcome_endpoint`]) whose notes the scan saw spent: the scan keeps their -/// records and leaves their activity rows as they are, and each is read at the node it was -/// delivered to ([`classify_held_identity_create`]). A create the reads do not settle counts -/// an attempt; once [`MAX_REDRIVE_ATTEMPTS`] are spent its hold is given up and its record -/// retired, as a scan that saw its notes spent would have retired it, and its row is left to -/// the scan. -pub(super) async fn classify_spent_identity_creates( +/// ([`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. +/// +/// Returns the activity ids of the creates whose row is on `changeset` (or that have no row to +/// write). 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; + return Vec::new(); } }; let spent: HashSet<[u8; 32]> = match store.get_all_notes(id) { @@ -3658,47 +3708,113 @@ pub(super) async fn classify_spent_identity_creates( .collect(), Err(e) => { warn!(error = %e, "held identity creates: get_all_notes failed; skipped"); - return; + return Vec::new(); } }; redrives .into_iter() - .filter(|redrive| { - redrive.outcome_endpoint.is_some() - && !redrive.nullifiers.is_empty() - && redrive.nullifiers.iter().all(|n| spent.contains(n)) + .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 { - if cancel.is_cancelled() { - return; - } - if classify_held_identity_create(sdk, store, persister, id, &redrive).await - == HeldIdentityCreate::Settled - { - continue; - } - let attempts = match store - .write() - .await - .bump_redrive_attempts(id, &redrive.activity_id) - { - Ok(attempts) => attempts, - Err(e) => { - warn!(error = %e, "held identity create: attempt not counted; retried next pass"); - continue; + 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; + } + } } }; - if attempts >= MAX_REDRIVE_ATTEMPTS { + if write_proven_identity_create_row(store, id, &redrive.activity_id, outcome, changeset) + .await + { + 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`, or when there is no row to write; false, logged, when the row did not +/// read or save. +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::Failed, 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) => return true, + Err(e) => { warn!( - activity_id = %hex::encode(redrive.activity_id), - attempts, - "held identity create: its outcome could not be read; giving up its hold" + entry_id = %hex::encode(activity_id), + error = %e, + "held identity create: its row did not read; written next pass" ); - retire_identity_create_record(store, id, Some(&redrive.activity_id)).await; + 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 } // ------------------------------------------------------------------------- @@ -4561,8 +4677,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, @@ -4911,9 +5035,10 @@ 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. Except an identity create (Type 20) held for -/// its outcome read ([`PendingRedrive::outcome_endpoint`]): its notes spent -/// do not show that its identity was created, so its outcome is read at -/// its node and settled ([`classify_held_identity_create`]); +/// 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; @@ -4927,7 +5052,8 @@ pub(super) async fn redrive_pending_identity_shields( /// /// A held identity create is re-broadcast only to the node it was /// delivered to, through an SDK scoped to it, and not at all once the scan -/// saw its notes spent: [`classify_spent_identity_creates`] settles it then. +/// 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 @@ -4981,6 +5107,13 @@ pub(super) async fn redrive_pending_spends( continue; } }; + // 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) + { + continue; + } // 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::() { @@ -5020,8 +5153,10 @@ pub(super) async fn redrive_pending_spends( // A held identity create's notes spent do not show its identity created: // read its outcome at its node. if held_at.is_some() - && classify_held_identity_create(sdk, store, persister, id, &redrive).await - == HeldIdentityCreate::Settled + && matches!( + classify_held_identity_create(sdk, store, persister, id, &redrive).await, + HeldIdentityCreate::Proven(_) + ) { continue; } @@ -5410,6 +5545,7 @@ mod redrive_tests { let mut store = InMemoryShieldedStore::new(); let archived = PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [6; 32], anchor: [1; 32], nullifiers: vec![], @@ -5464,6 +5600,7 @@ mod redrive_tests { id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id, anchor: [1; 32], nullifiers: vec![], @@ -5536,6 +5673,7 @@ mod redrive_tests { SubwalletId::new(wallet_id, 3), PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [6; 32], anchor: [1; 32], nullifiers: vec![], @@ -5568,6 +5706,7 @@ mod redrive_tests { SubwalletId::new(wallet_id, 0), PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [8; 32], anchor: [1; 32], nullifiers: vec![], @@ -5681,6 +5820,7 @@ mod redrive_tests { previous_account, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id, anchor: [0xAB; 32], nullifiers: vec![], @@ -6057,6 +6197,7 @@ mod redrive_tests { id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x79; 32], anchor: [7; 32], nullifiers: vec![note.nullifier], @@ -6739,6 +6880,7 @@ mod redrive_tests { id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id, anchor, nullifiers: vec![], @@ -6779,6 +6921,7 @@ mod redrive_tests { 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]], @@ -6840,6 +6983,7 @@ mod redrive_tests { 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]], @@ -6945,7 +7089,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 @@ -7489,15 +7633,26 @@ mod redrive_tests { .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" - ); + 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" + ); + } } } @@ -8362,13 +8517,18 @@ mod redrive_tests { assert_eq!(row.status, ShieldedActivityStatus::Failed); assert_eq!(row.block_height, Some(0)); drop(store); - assert!( - FileBackedShieldedStore::open_path(&path, 100) - .expect("reopened") - .pending_redrives(id) - .expect("records") - .is_empty(), - "the record is retired for good" + // 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!( + records[0].identity_outcome, + Some(IdentityCreateOutcome::InputsConsumed { + consumed_by_height: 0 + }) ); } } @@ -8426,9 +8586,10 @@ mod redrive_tests { } /// A held identity create is read at its node: its notes spent and its - /// identity absent settles it as inputs consumed (exactly its real - /// inputs marked spent), its identity found settles it confirmed, and - /// its notes free leave it. One with no node is never read. + /// 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]; @@ -8453,10 +8614,12 @@ mod redrive_tests { let row = activity_row(&store, id, &activity_id).await; match (held, spent, created) { (true, true, false) => { - assert_eq!(classified, HeldIdentityCreate::Settled, "{case}"); - assert!(records.is_empty(), "{case}"); - assert_eq!(row.status, ShieldedActivityStatus::Failed, "{case}"); - assert_eq!(row.block_height, Some(0), "{case}"); + 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 @@ -8464,25 +8627,31 @@ mod redrive_tests { .all(|n| transition_nullifiers(&transition).contains(n))); } (true, true, true) => { - assert_eq!(classified, HeldIdentityCreate::Settled, "{case}"); - assert!(records.is_empty(), "{case}"); - assert_eq!(row.status, ShieldedActivityStatus::Confirmed, "{case}"); + 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.len(), 1, "{case}"); + assert_eq!( + records[0].identity_outcome, + Some(IdentityCreateOutcome::Awaiting), + "{case}" + ); assert_eq!(row.status, ShieldedActivityStatus::Pending, "{case}"); } } } } - /// A held identity create keeps its record when its notes are marked - /// spent, as a scan marks them: the notes spent do not show what it did. - /// Any other record is retired with its notes, as before. + /// 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_a_held_identity_create_s_record_when_its_notes_are_spent() { + 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]; @@ -8500,10 +8669,17 @@ mod redrive_tests { .expect("reopened") .pending_redrives(id) .expect("records"); - assert_eq!(records.len(), usize::from(held), "held {held}"); - if held { - assert_eq!(records[0].outcome_endpoint.as_deref(), Some(ENDPOINT)); - } + 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}" + ); } } } diff --git a/packages/rs-platform-wallet/src/wallet/shielded/store.rs b/packages/rs-platform-wallet/src/wallet/shielded/store.rs index 367e439ad64..db67312181c 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/store.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/store.rs @@ -187,12 +187,53 @@ pub struct PendingRedrive { 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. A - /// record that has one is held for that read: seeing its notes spent - /// ([`ShieldedStore::mark_spent`]) does not drop it, as its notes spent do - /// not show whether the identity was created. `None` for every other - /// spend, and for a create whose delivery no node was kept for. + /// 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 @@ -489,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; @@ -510,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 @@ -729,8 +781,10 @@ pub trait ShieldedStore: Send + Sync + 'static { ) -> Result, Self::Error>; /// Keep `endpoint` as the [`PendingRedrive::outcome_endpoint`] of `id`'s - /// redrive keyed by `activity_id`. Returns false, changing nothing, when no - /// such record exists. + /// 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, @@ -738,6 +792,16 @@ pub trait ShieldedStore: Send + Sync + 'static { 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). @@ -753,7 +817,8 @@ pub trait ShieldedStore: Send + Sync + 'static { /// 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; [`Self::mark_spent`] keeps a - /// record held for its outcome read ([`PendingRedrive::outcome_endpoint`]). + /// 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>; @@ -1111,13 +1176,13 @@ impl SubwalletState { } /// [`Self::drop_redrives_containing`] for a spend seen landed: a record held - /// for its outcome read ([`PendingRedrive::outcome_endpoint`]) is kept, as a - /// landed spend of its notes does not show what it did. + /// 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.outcome_endpoint.is_none() && r.nullifiers.contains(nullifier)) + .filter(|(_, r)| !r.held_for_identity_outcome() && r.nullifiers.contains(nullifier)) .map(|(id, _)| *id) .collect(); for id in &dropped { @@ -1280,8 +1345,9 @@ impl SubwalletState { self.redrives.get(activity_id).map(|r| r.attempts) } - /// Set the record's [`PendingRedrive::outcome_endpoint`]; false when no - /// such record exists. + /// 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], @@ -1289,7 +1355,24 @@ impl SubwalletState { ) -> bool { self.redrives .get_mut(activity_id) - .map(|r| r.outcome_endpoint = Some(endpoint.to_string())) + .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() } @@ -1695,6 +1778,18 @@ impl ShieldedStore for InMemoryShieldedStore { .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, @@ -2088,6 +2183,7 @@ 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], @@ -2486,6 +2582,7 @@ mod tests { let mut store = InMemoryShieldedStore::new(); let identity_guard = PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [0x11; 32], anchor: [0x22; 32], nullifiers: vec![], @@ -2497,6 +2594,7 @@ mod tests { }; 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 09c1c055e54..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 @@ -522,6 +522,7 @@ async fn bind_persists_viewing_keys_and_restart_rebinds_seedlessly() { subwallet, crate::wallet::shielded::store::PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [7; 32], anchor: [8; 32], nullifiers: vec![[2; 32]], @@ -1076,6 +1077,7 @@ async fn should_keep_wallet_and_coordinator_keys_when_guarded_registration_is_re id, PendingRedrive { outcome_endpoint: None, + identity_outcome: None, activity_id: [1; 32], anchor: [2; 32], nullifiers: vec![], From 4d08336a83465670bb8e42e007e7f01647e86c71 Mon Sep 17 00:00:00 2001 From: romchornyi Date: Wed, 11 Nov 2026 19:30:46 +0200 Subject: [PATCH 5/8] fix: replay a held identity create's spent notes with its kept outcome The pass that settles a held identity create puts every note it consumed onto its changeset as spent, with its terminal row, even when the store already has the note spent: an earlier spent-note delta may never have reached the host, and a scan emits none for a note already spent. A create with no row of its own id keeps its record, and a record goes only once its wallet's store of the pass is durable. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/shielded/coordinator.rs | 152 +++++++++++++++++- .../src/wallet/shielded/operations.rs | 78 +++++++-- 2 files changed, 206 insertions(+), 24 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs index 91fcbdb1eeb..b9e721a0611 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -2542,8 +2542,9 @@ impl NetworkShieldedCoordinator { } } for (id, activity_ids) in settled_identity_creates { - // A wallet with no changeset this pass had no row to write. - if held_durably.get(&id.wallet_id) == Some(&false) { + // 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) @@ -3736,13 +3737,16 @@ mod tests { } /// A host persister that keeps the activity rows it holds durably by id, - /// last write wins, as the Swift persistence callback does. + /// 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 { @@ -3751,6 +3755,8 @@ mod tests { mode: std::sync::Mutex::new(mode), buffered: Default::default(), durable: Default::default(), + buffered_spent: Default::default(), + durable_spent: Default::default(), }) } @@ -3770,9 +3776,15 @@ mod tests { 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(); } } @@ -3786,11 +3798,9 @@ mod tests { _wallet_id: WalletId, changeset: crate::changeset::PlatformWalletChangeSet, ) -> Result<(), crate::changeset::PersistenceError> { - let rows: Vec<_> = changeset - .shielded - .into_iter() - .flat_map(|shielded| shielded.activity_entries.into_values().flatten()) - .collect(); + 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, @@ -3801,10 +3811,12 @@ mod tests { 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(()) } } @@ -3821,6 +3833,10 @@ mod tests { 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(()) } @@ -4238,6 +4254,126 @@ mod tests { } } + /// 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::Failed, 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 diff --git a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs index 0e6e4fdc279..389cbcc7186 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs @@ -2986,9 +2986,9 @@ where /// 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 record retired 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`]). +/// 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>, @@ -3455,10 +3455,13 @@ pub(super) async fn settle_inputs_consumed_identity_create( .await; } -/// Drop the recovery record `activity_id` of an identity create whose outcome is known. A -/// create held for its outcome read ([`PendingRedrive::held_for_identity_outcome`]) keeps its -/// record when its notes are marked spent, so the code that learns its outcome drops it. A -/// failure is logged: the record stays held, and the next pass reads the outcome again. +/// Drop the recovery record `activity_id` of an identity create whose outcome is known. Two +/// callers: the result wait, for a create whose identity it proved created, whose row it +/// confirms itself; and, for a create held for its outcome read +/// ([`PendingRedrive::held_for_identity_outcome`]), the sync pass once the host holds that +/// create's row and spent notes durably ([`retire_settled_identity_creates`]). A create proven +/// unable to execute is never dropped where its outcome is learnt. 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, @@ -3677,10 +3680,11 @@ pub(super) async fn classify_held_identity_create( /// 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. +/// 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 is on `changeset` (or that have no row to -/// write). Their records are dropped only once the host holds `changeset` durably +/// 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( @@ -3742,9 +3746,11 @@ pub(super) async fn settle_held_identity_creates( } } }; - if write_proven_identity_create_row(store, id, &redrive.activity_id, outcome, changeset) - .await - { + 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); } } @@ -3773,8 +3779,9 @@ async fn count_unproven_identity_create_read( /// 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`, or when there is no row to write; false, logged, when the row did not -/// read or save. +/// 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, @@ -3792,7 +3799,13 @@ async fn write_proven_identity_create_row( let mut store = store.write().await; let stored = match store.get_activity_by_entry_id(id, activity_id) { Ok(Some(stored)) => stored, - Ok(None) => return true, + 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), @@ -3817,6 +3830,39 @@ async fn write_proven_identity_create_row( 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) // ------------------------------------------------------------------------- From 0c0fd741f73b340b5b49904260f5cb645ae7813d Mon Sep 17 00:00:00 2001 From: romchornyi Date: Wed, 11 Nov 2026 23:42:18 +0200 Subject: [PATCH 6/8] fix: keep the node a redrive of a held identity create reached A held identity create whose first dispatch's node was never kept (the process stopped, or keeping it failed, after its record was saved) was re-broadcast through the shared SDK, so no node was kept for it: the sync pass never read its outcome and its activity stayed pending. Such a record is now re-broadcast to one node, kept with its record once the dispatch may have reached it. The held-create settlement also returns before reading the note history when no record is held for an identity create's outcome. Co-Authored-By: Claude Opus 5.5 --- .../src/broadcast_outcome.rs | 2 +- .../src/wallet/shielded/operations.rs | 73 ++++++++++++++++++- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/packages/rs-platform-wallet/src/broadcast_outcome.rs b/packages/rs-platform-wallet/src/broadcast_outcome.rs index 828bb34ad46..9c521697d5d 100644 --- a/packages/rs-platform-wallet/src/broadcast_outcome.rs +++ b/packages/rs-platform-wallet/src/broadcast_outcome.rs @@ -317,7 +317,7 @@ 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. -fn refused_before_sending(error: &dash_sdk::Error) -> bool { +pub(crate) fn refused_before_sending(error: &dash_sdk::Error) -> bool { use dash_sdk::dapi_client::DapiClientError; matches!( diff --git a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs index 389cbcc7186..a8b94962c2c 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs @@ -34,7 +34,7 @@ use super::store::{ }; use crate::broadcast_outcome::{ broadcast_tracking_delivery, broadcast_tracking_endpoint, broadcast_was_refused, - carries_consensus_rejection, BroadcastDelivery, + carries_consensus_rejection, refused_before_sending, BroadcastDelivery, }; use crate::changeset::{PlatformWalletChangeSet, ShieldedChangeSet}; use crate::error::{preserve_signer_key_unavailable_or, PlatformWalletError}; @@ -3704,6 +3704,12 @@ pub(super) async fn settle_held_identity_creates( 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() @@ -5097,7 +5103,9 @@ pub(super) async fn redrive_pending_identity_shields( /// 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, and not at all once the scan +/// 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. /// @@ -5124,7 +5132,7 @@ pub(super) async fn redrive_pending_spends( return; } }; - for redrive in redrives { + for mut redrive in redrives { // A stop ends the pass between records: no new re-broadcast starts // once cancelled. One already submitted is left to finish and is // classified as usual — its outcome handling stays conservative. @@ -5174,7 +5182,13 @@ pub(super) async fn redrive_pending_spends( if held_at.is_some() && notes_seen_spent(store, id, &redrive.nullifiers).await { continue; } - let broadcaster = match &held_at { + // 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), }; @@ -5182,6 +5196,14 @@ pub(super) async fn redrive_pending_spends( continue; }; 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() @@ -8728,6 +8750,49 @@ mod redrive_tests { ); } } + + /// 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}" + ); + } + } } #[cfg(test)] From 863b700edeb1fd0ddb71b85ac278b93c8e6dc7b9 Mon Sep 17 00:00:00 2001 From: romchornyi Date: Thu, 12 Nov 2026 00:09:32 +0200 Subject: [PATCH 7/8] feat: report a shielded identity create proven unable to execute as InputsConsumed A held identity create proven by reads to have its nullifiers spent and its identity absent wrote its activity row as Failed, the status a transition that never left the wallet and the prune backstop's release also write. A host that showed the create as unconfirmed could not tell, from the row the sync pass settles later, that its inputs were spent. ShieldedActivityStatus gains InputsConsumed (FFI status 4), written only for that proven outcome, at the result wait and by the sync pass. Every other Failed write is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/persistence.rs | 5 ++++- .../src/shielded_persistence.rs | 2 +- .../src/wallet/shielded/activity.rs | 6 +++++ .../src/wallet/shielded/coordinator.rs | 22 +++++++++---------- .../src/wallet/shielded/operations.rs | 11 +++++----- .../Models/PersistentShieldedActivity.swift | 2 +- .../PlatformWalletManagerShieldedSync.swift | 5 +++++ 7 files changed, 34 insertions(+), 19 deletions(-) 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/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/coordinator.rs b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs index b9e721a0611..bb3771274e2 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -3941,14 +3941,14 @@ mod tests { .is_empty()); assert_eq!( create_row(&coordinator, id).await, - (ShieldedActivityStatus::Failed, Some(0)) + (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::Failed, Some(0)) + (ShieldedActivityStatus::InputsConsumed, Some(0)) ); let _ = std::fs::remove_dir_all(&dir); } @@ -3983,7 +3983,7 @@ mod tests { assert_eq!( create_row(&coordinator, id).await, - (ShieldedActivityStatus::Failed, Some(0)) + (ShieldedActivityStatus::InputsConsumed, Some(0)) ); let _ = std::fs::remove_dir_all(&dir); } @@ -4029,7 +4029,7 @@ mod tests { assert_eq!( create_row(&coordinator, id).await, - (ShieldedActivityStatus::Failed, Some(7)) + (ShieldedActivityStatus::InputsConsumed, Some(7)) ); assert!(coordinator .store() @@ -4082,7 +4082,7 @@ mod tests { let expected = if created { ShieldedActivityStatus::Confirmed } else { - ShieldedActivityStatus::Failed + ShieldedActivityStatus::InputsConsumed }; assert_eq!( create_row(&coordinator, id).await.0, @@ -4151,7 +4151,7 @@ mod tests { .expect("the host holds the row"); assert_eq!( (row.status, row.block_height), - (ShieldedActivityStatus::Failed, Some(0)) + (ShieldedActivityStatus::InputsConsumed, Some(0)) ); assert!(coordinator .store() @@ -4164,7 +4164,7 @@ mod tests { post_scan_steps(&coordinator, id).await; assert_eq!( host.durable_row(&rebuilt_id).unwrap().status, - ShieldedActivityStatus::Failed + ShieldedActivityStatus::InputsConsumed ); let _ = std::fs::remove_dir_all(&dir); } @@ -4203,7 +4203,7 @@ mod tests { assert_eq!( create_row(&coordinator, id).await.0, - ShieldedActivityStatus::Failed, + ShieldedActivityStatus::InputsConsumed, "{mode:?}: proven in memory" ); assert_eq!( @@ -4238,7 +4238,7 @@ mod tests { let row = host.durable_row(&CREATE_ROW).unwrap(); assert_eq!( (row.status, row.block_height), - (ShieldedActivityStatus::Failed, Some(0)), + (ShieldedActivityStatus::InputsConsumed, Some(0)), "{mode:?}: written from the kept outcome" ); drop(coordinator); @@ -4303,7 +4303,7 @@ mod tests { .expect("the host holds the row"); assert_eq!( (row.status, row.block_height), - (ShieldedActivityStatus::Failed, Some(0)) + (ShieldedActivityStatus::InputsConsumed, Some(0)) ); assert!( FileBackedShieldedStore::open_path(dir.join("tree.sqlite"), 100) @@ -4465,7 +4465,7 @@ mod tests { post_scan_steps(&coordinator, id).await; assert_eq!( create_row(&coordinator, id).await, - (ShieldedActivityStatus::Failed, Some(0)) + (ShieldedActivityStatus::InputsConsumed, Some(0)) ); let _ = std::fs::remove_dir_all(&dir); } diff --git a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs index a8b94962c2c..7986bc05d26 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs @@ -3449,7 +3449,7 @@ pub(super) async fn settle_inputs_consumed_identity_create( wallet_id, id, activity_id, - ShieldedActivityStatus::Failed, + ShieldedActivityStatus::InputsConsumed, Some(consumed_by_height), ) .await; @@ -3797,9 +3797,10 @@ async fn write_proven_identity_create_row( ) -> bool { let (status, block_height) = match outcome { IdentityCreateOutcome::Created => (ShieldedActivityStatus::Confirmed, None), - IdentityCreateOutcome::InputsConsumed { consumed_by_height } => { - (ShieldedActivityStatus::Failed, Some(consumed_by_height)) - } + IdentityCreateOutcome::InputsConsumed { consumed_by_height } => ( + ShieldedActivityStatus::InputsConsumed, + Some(consumed_by_height), + ), IdentityCreateOutcome::Awaiting => return false, }; let mut store = store.write().await; @@ -8582,7 +8583,7 @@ mod redrive_tests { .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::Failed); + 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 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. From 9e6f830536bfdd7b2aaf87fcd25b1e5d0c52eaff Mon Sep 17 00:00:00 2001 From: romchornyi Date: Thu, 12 Nov 2026 12:43:30 +0200 Subject: [PATCH 8/8] fix: make a held identity create's record the only owner of its settlement A held identity create's recovery record was dropped or left unscheduled by three paths outside the sync pass's durable settlement: - a result wait that proved the create executed retired the record before the host held its confirmed row and spent notes; it now keeps the record with IdentityCreateOutcome::Created, for the sync pass to replay both and retire it once the host acknowledges them; - the prune backstop released a reservation of a create whose outcome was proven, dropping its record with it; it now leaves such reservations to the settlement; - a held create with no node kept for it was re-broadcast only from the reservation-driven redrive, which no longer runs once a scan saw its notes spent; each pass now schedules it from the record journal (redrive_unkept_identity_creates), sharing redrive_one with the redrive pass. The result wait's fallback reads (identity fetch, nullifier probe) each use a fresh SDK scoped to the create's node, so a ban the failed wait placed on it does not stop them. The example apps show the InputsConsumed status. Co-Authored-By: Claude Opus 5.5 --- .../ui/shielded/ShieldedActivityScreen.kt | 1 + .../src/wallet/shielded/coordinator.rs | 150 +++++++ .../src/wallet/shielded/operations.rs | 397 ++++++++++++------ .../Core/Views/ShieldedActivityView.swift | 5 + 4 files changed, 424 insertions(+), 129 deletions(-) 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/rs-platform-wallet/src/wallet/shielded/coordinator.rs b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs index bb3771274e2..0ad9cad813a 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -1598,6 +1598,13 @@ 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 @@ -1907,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 @@ -2552,6 +2588,32 @@ impl NetworkShieldedCoordinator { } } + /// 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 @@ -3717,6 +3779,9 @@ mod tests { .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; @@ -4413,6 +4478,45 @@ mod tests { 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 @@ -5678,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; @@ -5692,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); } diff --git a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs index 7986bc05d26..aa1865e6a9f 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/operations.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/operations.rs @@ -2928,35 +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" - ); - } - // A create held for its outcome read keeps its record when its notes are marked - // spent (`PendingRedrive::identity_outcome`); its outcome is known now. - retire_identity_create_record(store, id, pending_entry.as_ref().map(|entry| &entry.id)) - .await; info!( account, denomination, @@ -2983,6 +2964,51 @@ where } } +/// 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` @@ -3199,13 +3225,21 @@ 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. - let mut identity = match fetch_identity_with_retries(&reader, identity_id).await { + // 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(&reader, st, identity_id).await { + None => match probe_identity_create(&fallback_reader(), st, identity_id).await { IdentityCreateProbe::Created(identity) => identity, IdentityCreateProbe::InputsConsumed { consumed_by_height } => { warn!( @@ -3455,13 +3489,12 @@ pub(super) async fn settle_inputs_consumed_identity_create( .await; } -/// Drop the recovery record `activity_id` of an identity create whose outcome is known. Two -/// callers: the result wait, for a create whose identity it proved created, whose row it -/// confirms itself; and, for a create held for its outcome read -/// ([`PendingRedrive::held_for_identity_outcome`]), the sync pass once the host holds that -/// create's row and spent notes durably ([`retire_settled_identity_creates`]). A create proven -/// unable to execute is never dropped where its outcome is learnt. A failure is logged: the -/// record stays, and the next pass settles it again from its kept outcome. +/// 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, @@ -5121,8 +5154,6 @@ pub(super) async fn redrive_pending_spends( 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) => { @@ -5133,7 +5164,7 @@ pub(super) async fn redrive_pending_spends( return; } }; - for mut redrive in redrives { + for redrive in redrives { // A stop ends the pass between records: no new re-broadcast starts // once cancelled. One already submitted is left to finish and is // classified as usual — its outcome handling stays conservative. @@ -5151,107 +5182,159 @@ 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; - } - }; - // 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) + 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; } - // 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"); - continue; - } - }, - None => None, - }; - if held_at.is_some() && notes_seen_spent(store, id, &redrive.nullifiers).await { - continue; + 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 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 { - continue; - }; - 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(_) - ) - { - continue; - } - // 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" - ); + }; + // 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" + ); } } } @@ -8602,6 +8685,62 @@ mod redrive_tests { } } + /// 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_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 = [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, + &spend.entry, + SPEND_ANCHOR, + SPEND_ANCHOR_HEIGHT, + &spend.notes, + &mut spend.reservation, + &transition, + IDENTITY_CREATE_OPERATION, + || async { Ok::<(), SpendFailure>(()) }, + ) + .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!( + 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) + ); + } + /// 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 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" } }