From 23a55c029e72ca3f64c6d1a446a56379cb9e2609 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 13 Nov 2026 13:07:40 +0700 Subject: [PATCH 01/14] fix(platform-wallet): keep contact request holds consistent after an unconfirmed send A public contact request whose broadcast may have been delivered is held until a proved read settles it. While it is held or being sent: - Its signed bytes are broadcast again only where a public request to the recipient would still go out by what the wallet tracks: not once a private request holds the pair or their requests came or went privately, not while the sender's read of the private store is rewound, not while private payments may be unrestored, and never from a private send. A public send does it only after its own store read succeeded. - One public send holds a pair at a time: a second to the same recipient is refused with PublicContactRequestUnconfirmed, with nothing signed. - A send that finds its unconfirmed request on Platform records it and returns it as the request sent, instead of building another past it. Also: - The lost-writes flag is read again after the flush, in note_read_back_once_durable and settled_requests_durable. - register_contact_account, register_external_contact_account and the accept path's adopted request act only on the handle's wallet generation. - A private request paid for whose read-back fails on the handle is reported pending. The README, the error's documentation and the FFI code registry describe what code 76 now also covers, when held bytes are broadcast again, and that a request which never lands and whose nonce is never used stays held for the life of the process. Co-Authored-By: Claude Fable 5.2 --- .../ERROR_CODE_REGISTRY.md | 2 +- packages/rs-platform-wallet/README.md | 8 +- packages/rs-platform-wallet/src/error.rs | 13 +- .../identity/network/contact_requests.rs | 202 ++++++--- .../src/wallet/identity/network/contacts.rs | 26 +- .../network/private_contact_requests.rs | 416 +++++++++++++++++- .../managed_identity/contact_requests.rs | 35 +- .../state/managed_identity/dashpay.rs | 23 +- .../src/wallet/persister.rs | 73 ++- .../src/wallet/platform_wallet.rs | 24 +- 10 files changed, 702 insertions(+), 120 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md index a5655430abd..d39224ebde6 100644 --- a/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md +++ b/packages/rs-platform-wallet-ffi/ERROR_CODE_REGISTRY.md @@ -191,7 +191,7 @@ Fork-era numbers remain in the collision history, which is immutable record. | 73 | `ErrorPrivateContactRecoveryUnsupported` | DashPay private contact requests | Proposed. Maps `PlatformWalletError::PrivateContactRecoveryUnsupported`: the persister does not declare `PRIVATE_CONTACT_REQUEST_SCAN_STATE` (bit 15) together with `ATOMIC_CHANGESETS` (bit 0, with the changeset begin and end callbacks wired), so the private send, the private accept and the in-kind accept's private reply are refused before anything is sealed or paid. The payment's recovery marker could not survive a restart, and a process reopening the wallet without the `shielded` feature, or before the shielded store is configured, could publish the pair while the payment may still execute. Not retryable as it is: a host that persists the private contact request scan state in changesets it commits whole declares both. The FFI constant is `PLATFORM_WALLET_PERSISTENCE_CAPABILITY_PRIVATE_CONTACT_REQUEST_SCAN_STATE` (`1 << 15`). Swift types it (`PlatformWalletError.privateContactRecoveryUnsupported`, `ErrorHandlingTests` pin), SwiftExampleApp shows it as a storage configuration error, and the SwiftData handler declares bit 0 and bit 15 with the begin and end callbacks wired; Kotlin types it (`DashSdkError.PlatformWallet.PrivateContactRecoveryUnsupported`, `DashSdkErrorTest` pin), KotlinExampleApp shows it as a storage configuration error, and the Room handler declares bit 0 and bit 15 with the begin and end callbacks wired | | 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 | +| 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 where a public request to that identity would still go out (not once the pair is held or tracked as private, and never from a private send). Also what a public send or accept gets while another public request to that identity is being sent: one public send holds a pair at a time, and the per-pair read below does not report that one. 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 | **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/README.md b/packages/rs-platform-wallet/README.md index 7e38a91f9f4..4363c45f06c 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -151,7 +151,13 @@ Every shielded spend that consumes notes (shielded transfers, unshields, withdra Every public `contactRequest` (a send, a public accept, the in-kind accept's public reply, a send from a QR code, an auto-accept and a rotation) is sent by one path, which signs the transition, keeps the signed bytes, and broadcasts them by the rule shielded spends use: only a refusal before anything was sent reports its error with nothing held. Once a dispatch may have reached an endpoint, the send waits for the result (not after an endpoint's verdict, which no proof backs), records the request when its document is confirmed, and otherwise reports the SDK's unconfirmed outcome (`StateTransitionResultUnconfirmed`, FFI code 20) and holds the pair as an unconfirmed public request. Before signing, the send reads the request it would make by DashPay's `ownerIdUserIdAndAccountRef` unique index (the sender, the recipient and the account reference); one found, sent by an earlier process that ended before recording it or by another device of the sender, is adopted as sent, with nothing signed. A read that fails sends on. -While an unconfirmed public request holds a pair, a public or private request to it, an accept included, is refused with `PublicContactRequestUnconfirmed` (FFI code 76), with nothing signed or paid, and the sender identity cannot be removed. `DashPayView::public_contact_request_unconfirmed` reads one pair's state, beside `DashPayView::private_pair_held`, so a host shows a public request awaiting its outcome apart from a private one pending. The public contact request sync settles it, as does each send or accept to the pair before it decides: `Sdk::state_transition_outcome` reads the transition's identity contract nonce, proved, and then the request is read by the unique index, proved. A request found is recorded as sent, as a confirmed send records it, and frees the pair. With none found, a nonce proved used at a height at or below the height of the read that missed the request shows the transition never wrote it and never will, and frees the pair; a new request may then be sent. Otherwise the transition may still execute: the same signed bytes are broadcast again and the pair stays held. No retry signs a second transition. +While an unconfirmed public request holds a pair, a public or private request to it, an accept included, is refused with `PublicContactRequestUnconfirmed` (FFI code 76), with nothing signed or paid, and the sender identity cannot be removed. `DashPayView::public_contact_request_unconfirmed` reads one pair's state, beside `DashPayView::private_pair_held`, so a host shows a public request awaiting its outcome apart from a private one pending. The public contact request sync settles it, as does each send or accept to the pair before it decides: `Sdk::state_transition_outcome` reads the transition's identity contract nonce, proved, and then the request is read by the unique index, proved. A request found is recorded as sent, as a confirmed send records it, and frees the pair; a send that finds it this way returns it as the request sent, with nothing signed, rather than building another past it. With none found, a nonce proved used at a height at or below the height of the read that missed the request shows the transition never wrote it and never will, and frees the pair; a new request may then be sent. Otherwise the transition may still execute, and the pair stays held. No retry signs a second transition. + +While it may still execute, the same signed bytes are broadcast again only where a public request to the recipient would still go out, by what the wallet tracks. The public sync does it, and so does a public send whose read of the private store succeeded; a private send never does, as it is about to take the pair privately. Nothing is broadcast while the wallet's private payments may be unrestored, while the sender's read of the private store is rewound (keys that changed, a sender unignored) and has not reached the store's end again, or once a private request holds the pair or their requests came or went privately: the request then stays held until a proved read settles it. This is decided from what the wallet has read, not from the store: a private request the store holds that no private sync has read in yet does not stop the broadcast, as one sealed after a send's own store read does not stop that send. + +One public send holds a pair at a time. While one is being sent, a second public request to the same recipient is refused with `PublicContactRequestUnconfirmed` too, with nothing signed: built from the same recorded requests, it would sign a request of its own while the first may still execute. `DashPayView::public_contact_request_unconfirmed` reports only a request held as unconfirmed, not one being sent. + +A request that never lands, and whose nonce is never used, stays held for the life of the process. That is the case for one every endpoint refuses (an identity balance too low, a signing key disabled since), as an endpoint's refusal proves nothing, and later DashPay transitions of the sender take later nonces, which leaves its nonce open as a missing revision for as long as it is within 24 of the latest one used. Until then each public sync broadcasts it again, every request to that recipient is refused with 76, and the sender identity cannot be removed. Ending the process drops the hold, as described next; so does removing the wallet. The hold is in memory, bound to the wallet generation that took it: a wallet removed and created again under its id starts with none, and a handle taken before settles nothing of the new wallet's. Nothing about it is persisted. A process that starts afresh reads the sender's DashPay identity contract nonce from Platform, so while the earlier transition has not executed, and no other DashPay transition of the sender has since, a new request takes the same nonce and at most one of the two executes. That alone does not cover a restart after another DashPay transition of the sender executed with a later nonce: Platform then keeps the earlier nonce open as a missing revision (up to 24 back), and the new request takes a later one. What keeps a second request off Platform then is the unique index: a request built from the same recorded requests carries the same account reference (derived from the sender's encryption key, the friendship key, the account index and the version past the last recorded request), so Platform writes at most one of the two, and the read before signing adopts one already written. A restart does not hold back a private request to the pair while the earlier public one may still execute: the private store is not under that index. diff --git a/packages/rs-platform-wallet/src/error.rs b/packages/rs-platform-wallet/src/error.rs index f9ed4a5b60f..eac4c960f44 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -705,11 +705,16 @@ pub enum PlatformWalletError { /// identity, public or private, until a proved read shows the request on /// Platform (it is then recorded as sent) or shows the transition can no /// longer write it. A public contact request sync settles it, broadcasting - /// the same transition again while it may still execute. Unconfirmed, not - /// failed: it is not submitted again. + /// the same transition again while it may still execute and a public + /// request to the identity would still go out. Unconfirmed, not failed: + /// it is not submitted again. + /// + /// Also what a public send gets while another public request to the + /// identity is being sent: that one's outcome is not known yet either, + /// and nothing is signed for the second. #[error( - "a public contact request to {0} was broadcast and its outcome is not proved yet; sync \ - contact requests to settle it" + "a public contact request to {0} is being sent or was broadcast, and its outcome is not \ + proved yet; sync contact requests to settle it" )] PublicContactRequestUnconfirmed(Identifier), diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs index e99725d64cb..8a0afefc276 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs @@ -700,6 +700,37 @@ fn release_public_claim( } } +/// Why a public request from `sender` to `recipient` may not go out, from +/// what the wallet tracks of the pair's private requests: one to the +/// recipient is reserved, or a private payment the shielded store's journal +/// keeps unresolved holds the pair +/// ([`PlatformWalletError::PrivateContactRequestPending`]), or their requests +/// came or went privately, as a `contactRequest` document would publish the +/// relationship +/// ([`PlatformWalletError::PrivateContactRequestNeedsPrivateReply`]). Read by +/// the claim of the pair and, before a held request's bytes are broadcast +/// again, by the settling of it. +fn private_pair_refusal( + managed: &ManagedIdentity, + persister: &WalletPersister, + sender: &Identifier, + recipient: &Identifier, +) -> Result<(), PlatformWalletError> { + if managed.dashpay().private_reply_pending(recipient) + || persister.private_pair_journaled(sender, recipient) + { + return Err(PlatformWalletError::PrivateContactRequestPending( + *recipient, + )); + } + if managed.dashpay().private_contact(recipient).is_some() { + return Err(PlatformWalletError::PrivateContactRequestNeedsPrivateReply( + *recipient, + )); + } + Ok(()) +} + /// What a contact request to send is built from, whether it is published as a /// `contactRequest` document or sealed privately: see /// `DashPayView::prepare_contact_request`. @@ -765,9 +796,11 @@ impl DashPayView<'_, B> { /// transition kept, until a proved read settles it /// (`DashPayView::settle_unconfirmed_public_request`): until then a request /// to the recipient is refused - /// ([`PlatformWalletError::PublicContactRequestUnconfirmed`]). A request - /// already on Platform that this one would repeat is adopted as sent, - /// with nothing signed. + /// ([`PlatformWalletError::PublicContactRequestUnconfirmed`]), as is a + /// second one while a public request to the recipient is being sent. A + /// request already on Platform that this one would repeat is adopted as + /// sent, with nothing signed; so is the unconfirmed one, when this send + /// finds it on Platform: it comes back as the request sent. /// /// A recipient whose requests with this identity came or went privately is /// refused ([`PlatformWalletError::PrivateContactRequestNeedsPrivateReply`]). @@ -857,13 +890,21 @@ impl DashPayView<'_, B> { Box::pin(self.read_private_store_before_deciding(sender_identity_id, crypto)).await; // A public request to the recipient whose outcome is unconfirmed is // settled first where proof allows; while it stands the claim refuses. - Box::pin( - self.settle_unconfirmed_public_request_logged( - sender_identity_id, - recipient_identity_id, - ), - ) + // Its bytes go out again only after a store read that succeeded: one + // that failed leaves what the wallet tracks of the pair unverified. + let settled = Box::pin(self.settle_unconfirmed_public_request_logged( + sender_identity_id, + recipient_identity_id, + read.is_ok(), + )) .await; + if let Some(request) = settled { + // The request this send would repeat is on Platform, recorded + // just now: it is the one sent. Building another would bump its + // version past the one recorded and sign a rotation the caller + // did not ask for. A reply adopts it as any tracked request. + return Ok((!adopt_reply).then_some(request)); + } let claim = { let _lifecycle = self.generation.payment_guard().await; if !self.is_current_generation().await { @@ -918,7 +959,11 @@ impl DashPayView<'_, B> { /// removed and added again since /// (`WalletPersister::private_pair_journaled`), or once their requests /// came or went privately, as a `contactRequest` document would publish - /// the relationship (replies and rotations go privately instead). The + /// the relationship (replies and rotations go privately instead) + /// ([`private_pair_refusal`]). Refused too while a public request to the + /// recipient is unconfirmed, or another is being sent + /// ([`PlatformWalletError::PublicContactRequestUnconfirmed`]): one public + /// send holds the pair at a time. The /// refusal reads what the wallet tracks, which the store read before the /// send brought up to date (`DashPayView::read_private_store_before_deciding`). /// When that read applied (`store_read`), the sender's read must still be @@ -957,19 +1002,13 @@ impl DashPayView<'_, B> { *recipient, )); } - if managed.dashpay().private_reply_pending(recipient) - || self.persister.private_pair_journaled(sender, recipient) - { - return Err(PlatformWalletError::PrivateContactRequestPending( - *recipient, - )); - } - if managed.dashpay().private_contact(recipient).is_some() { - return Err(PlatformWalletError::PrivateContactRequestNeedsPrivateReply( - *recipient, - )); - } - let attempt = managed.claim_public_contact_request(*recipient); + private_pair_refusal(managed, &self.persister, sender, recipient)?; + // One public send per pair: a second, built from the same tracked + // requests, would sign a request of its own while the first may + // still execute. + let attempt = managed.claim_public_contact_request(*recipient).ok_or( + PlatformWalletError::PublicContactRequestUnconfirmed(*recipient), + )?; Ok(Some(PublicContactClaim { wallet_manager: Arc::clone(&self.wallet_manager), wallet_id: self.wallet_id, @@ -1346,7 +1385,9 @@ impl DashPayView<'_, B> { /// ([`PlatformWalletError::PublicContactRequestUnconfirmed`]). A public /// contact request sync settles it. The per-contact read a host shows next /// to `DashPayView::private_pair_held`, so it tells a public request - /// awaiting its outcome from a private one pending. + /// awaiting its outcome from a private one pending. A public request + /// still being sent is not reported, though a second public send is + /// refused with the same error while it is. pub async fn public_contact_request_unconfirmed( &self, sender: &Identifier, @@ -1376,8 +1417,18 @@ impl DashPayView<'_, B> { /// at or above (`TransitionOutcome::ExecutedOrSuperseded`): it never /// wrote the document and never will, so the pair is released and a new /// request may be sent; - /// - otherwise it may still execute: the same signed bytes are broadcast - /// again, whatever that answers, and the pair stays held. + /// - otherwise it may still execute, and the pair stays held: when + /// `broadcast_again`, and a public request to the recipient would + /// still go out by what the wallet tracks + /// ([`Self::may_broadcast_held_public_request`]), the same signed bytes + /// are broadcast again, whatever that answers. + /// + /// Returns the request when this call recorded it as sent. + /// + /// `broadcast_again` is the caller's: a public send passes whether the + /// store read it just made succeeded, the public sync passes `true`, and + /// a private send passes `false`, as it is about to take the pair + /// privately. /// /// The nonce is read before the document: a nonce used at height `h` /// proves the transition cannot execute after `h`, so only a document @@ -1386,7 +1437,8 @@ impl DashPayView<'_, B> { &self, sender: &Identifier, recipient: &Identifier, - ) -> Result<(), PlatformWalletError> { + broadcast_again: bool, + ) -> Result, PlatformWalletError> { let held = { let wm = self.wallet_manager.read().await; wm.get_wallet_info(&self.wallet_id) @@ -1400,7 +1452,7 @@ impl DashPayView<'_, B> { }) }; let Some(held) = held else { - return Ok(()); + return Ok(None); }; let outcome = self .sdk @@ -1418,7 +1470,12 @@ impl DashPayView<'_, B> { TransitionOutcome::StillPossible { .. } => false, }; if found.is_none() && !settled { - if matches!(outcome, TransitionOutcome::StillPossible { .. }) { + if broadcast_again + && matches!(outcome, TransitionOutcome::StillPossible { .. }) + && self + .may_broadcast_held_public_request(sender, recipient, held.attempt) + .await + { // The same bytes, so never a second request; an endpoint's // answer proves nothing either way. let delivery = broadcast_tracking_delivery(&self.sdk, &held.transition).await; @@ -1429,15 +1486,15 @@ impl DashPayView<'_, B> { "unconfirmed public contact request broadcast again" ); } - return Ok(()); + return Ok(None); } let _lifecycle = self.generation.payment_guard().await; if !self.is_current_generation().await { - return Ok(()); + return Ok(None); } match found { - Some(request) => { - self.record_sent_contact_request( + Some(request) => self + .record_sent_contact_request( sender, recipient, request, @@ -1445,8 +1502,8 @@ impl DashPayView<'_, B> { held.account_index, held.contact_xpub, ) - .await?; - } + .await + .map(Some), None => { let mut wm = self.wallet_manager.write().await; if let Some(managed) = @@ -1455,9 +1512,47 @@ impl DashPayView<'_, B> { { managed.settle_unconfirmed_public_request(recipient, held.attempt); } + Ok(None) } } - Ok(()) + } + + /// Whether the bytes of the unconfirmed public request `attempt` holds + /// from `sender` to `recipient` may be broadcast again: only where a + /// public request to the recipient would still go out, by what this + /// handle's wallet generation tracks. Not while the wallet's private + /// payments may be unrestored, not while the sender's read of the private + /// store was rewound and has not reached its end again, and not once a + /// private request holds the pair or their requests came or went + /// privately ([`private_pair_refusal`]): the request stays held, and + /// only a proved read settles it. + /// + /// It reads what the wallet tracks, not the store: a private request the + /// store holds that no private sync has read in yet does not stop the + /// broadcast, as one sealed after a send's own store read does not stop + /// that send. + async fn may_broadcast_held_public_request( + &self, + sender: &Identifier, + recipient: &Identifier, + attempt: u64, + ) -> bool { + if self.persister.private_payments_unrestored() { + return false; + } + let wm = self.wallet_manager.read().await; + wm.get_wallet_info(&self.wallet_id) + .filter(|info| Arc::ptr_eq(&info.generation, &self.generation)) + .and_then(|info| info.identity_manager.managed_identity(sender)) + .is_some_and(|managed| { + let dashpay = managed.dashpay(); + dashpay + .unconfirmed_public_request(recipient) + .is_some_and(|held| held.attempt == attempt) + && !dashpay.private_payments_unrestored() + && !dashpay.private_store_read_rewound() + && private_pair_refusal(managed, &self.persister, sender, recipient).is_ok() + }) } /// [`Self::settle_unconfirmed_public_request`], logging a read that could @@ -1466,18 +1561,23 @@ impl DashPayView<'_, B> { &self, sender: &Identifier, recipient: &Identifier, - ) { + broadcast_again: bool, + ) -> Option { // Boxed: its reads and broadcast make a large future, which sends and // syncs nest in their own. - if let Err(error) = - Box::pin(self.settle_unconfirmed_public_request(sender, recipient)).await + match Box::pin(self.settle_unconfirmed_public_request(sender, recipient, broadcast_again)) + .await { - tracing::warn!( - sender = %sender, - recipient = %recipient, - %error, - "an unconfirmed public contact request could not be settled; it stays held" - ); + Ok(recorded) => recorded, + Err(error) => { + tracing::warn!( + sender = %sender, + recipient = %recipient, + %error, + "an unconfirmed public contact request could not be settled; it stays held" + ); + None + } } } @@ -1508,7 +1608,7 @@ impl DashPayView<'_, B> { .unwrap_or_default() }; for (sender, recipient) in pairs { - self.settle_unconfirmed_public_request_logged(&sender, &recipient) + self.settle_unconfirmed_public_request_logged(&sender, &recipient, true) .await; } } @@ -4714,9 +4814,13 @@ impl DashPayView<'_, B> { // preserves metadata on an already-established pair. { let mut wm = self.wallet_manager.write().await; - let managed = wm - .get_wallet_info_mut(&self.wallet_id) - .and_then(|info| info.identity_manager.managed_identity_mut(&our_identity_id)) + // Only on this handle's wallet generation: the reply's send + // ran without the lifecycle gate, and a wallet re-created + // under this id since is not given the contact. + let managed = self + .current_wallet_info_mut(&mut wm)? + .identity_manager + .managed_identity_mut(&our_identity_id) .ok_or(PlatformWalletError::IdentityNotFound(our_identity_id))?; // Read now: a reply adopted after `send_reply` may have // established the pair since step 1. diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs index 3e7012faf49..0a62522b495 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs @@ -220,11 +220,14 @@ impl DashPayView<'_, B> { // Early-exit if the account already exists — keeps the recurring // sweep's re-registration a true no-op (no duplicate persistence // round, no managed-state churn). + // + // Only on this handle's wallet generation, checked under the guard + // held to the end: a wallet removed, or re-created under this id, + // since the handle was taken is given no account, stored or in + // memory. { use key_wallet::account::account_collection::DashpayAccountKey; - let info = wm - .get_wallet_info(&self.wallet_id) - .ok_or_else(|| PlatformWalletError::WalletNotFound(hex::encode(self.wallet_id)))?; + let info = self.current_wallet_info_mut(&mut wm)?; let key = DashpayAccountKey { index: account_index, user_identity_id: our_identity_id.to_buffer(), @@ -273,9 +276,7 @@ impl DashPayView<'_, B> { )) })?; - let (wallet, info) = wm - .get_wallet_mut_and_info_mut(&self.wallet_id) - .ok_or_else(|| PlatformWalletError::WalletNotFound(hex::encode(self.wallet_id)))?; + let (wallet, info) = self.current_wallet_and_info_mut(&mut wm)?; // Mirror the restored shape: the immutable `wallet.accounts` // collection holds the Account (like `build_wallet_start_state` @@ -599,13 +600,12 @@ impl DashPayView<'_, B> { #[cfg(test)] registration_rendezvous::wait(&contact_identity_id).await; let mut wm = self.wallet_manager.write().await; - let (wallet, info) = wm - .get_wallet_mut_and_info_mut(&self.wallet_id) - .ok_or_else(|| { - Transient(PlatformWalletError::WalletNotFound(hex::encode( - self.wallet_id, - ))) - })?; + // Only on this handle's wallet generation, under the guard held to + // the end: the decrypt above ran with no lock, and a wallet + // re-created under this id since is given no account. + let (wallet, info) = self + .current_wallet_and_info_mut(&mut wm) + .map_err(Transient)?; // Re-check under the guard that serializes registrations: the early // check above ran under a read guard that was released for the diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs index 3425470a24a..89cf44666a5 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs @@ -1045,13 +1045,13 @@ impl DashPayView<'_, B> { self.release_discharged_private_requests().await?; // A public request to the recipient whose outcome is unconfirmed is // settled first where proof allows; while it stands nothing is - // reserved. - Box::pin( - self.settle_unconfirmed_public_request_logged( - sender_identity_id, - recipient_identity_id, - ), - ) + // reserved. Its bytes are not broadcast again from here: this send is + // about to take the pair privately. + Box::pin(self.settle_unconfirmed_public_request_logged( + sender_identity_id, + recipient_identity_id, + false, + )) .await; // Held beyond the sender identity: a journaled payment's pair outlives // the identity's removal, and holds for a sender loaded after it was @@ -1789,6 +1789,10 @@ impl DashPayView<'_, B> { "private contact request paid for; reading it back failed, a later sync reads it" ); } + // The payment went out, so nothing from here on reports a failure: + // a handle that can no longer read the request back (its wallet + // removed, or re-created under its id, or the identity removed, + // since) reports it pending, as one not read back yet. let recorded = self .with_managed(sender_identity_id, |managed| { managed @@ -1800,7 +1804,17 @@ impl DashPayView<'_, B> { }) .cloned() }) - .await?; + .await + .unwrap_or_else(|error| { + tracing::warn!( + sender = %sender_identity_id, + recipient = %recipient_identity_id, + %error, + "private contact request paid for; it cannot be read back on this handle, \ + so it is reported pending" + ); + None + }); let Some(contact_request) = recorded else { return Err(PlatformWalletError::PrivateContactRequestPending( *recipient_identity_id, @@ -6370,6 +6384,388 @@ mod tests { .expect("the read")); } + /// An unconfirmed public request from Alice to Bob on a fresh wallet of + /// hers, as a send whose broadcast may have been delivered leaves it: the + /// wallet, its transition and its account reference. + async fn unconfirmed_public_request( + alice: &Identity, + bob: &Identity, + signer: &impl Signer, + crypto: &SeedCryptoProvider, + ) -> ( + PlatformWalletManager, + Arc, + StateTransition, + u32, + ) { + let (manager, alice_wallet) = wallet(ALICE_PHRASE, alice, &[alice, bob], vec![]).await; + expect_dashpay_nonce(&alice_wallet, alice, 3).await; + let sent = alice_wallet + .identity() + .dashpay() + .send_contact_request_with_external_signer( + &alice.id(), + &bob.id(), + None, + AutoAcceptProofSource::None, + signer, + crypto, + ) + .await; + assert!( + matches!(&sent, Err(PlatformWalletError::Sdk(error)) + if error.is_state_transition_result_unconfirmed()), + "{sent:?}" + ); + let (transition, reference) = unconfirmed_to(&alice_wallet, ALICE, bob) + .await + .expect("held"); + (manager, alice_wallet, transition, reference) + } + + /// The bytes of an unconfirmed public request are broadcast again only + /// where a public request to its recipient would still go out, and never + /// by a private send: not once the recipient's request came privately, + /// and not while the sender's read of the private store is rewound. The + /// request stays held, and once that read reaches the store's end again + /// its bytes go out. + #[tokio::test] + async fn should_broadcast_an_unconfirmed_public_request_again_only_while_its_pair_is_clear() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let alice_signer = signer(ALICE_PHRASE, &alice); + + for came_privately in [true, false] { + let (_manager, alice_wallet, transition, reference) = + unconfirmed_public_request(&alice, &bob, &alice_signer, &alice_crypto).await; + // Not on Platform, and its nonce not used: it may still execute. + expect_sent_request(&alice_wallet, &alice, &bob, reference, None).await; + let view = alice_wallet.identity().dashpay(); + let sent_once = broadcasts_of(&alice_wallet, &transition).await; + assert!(sent_once > 0, "the send broadcast it"); + + // As a private send settles it: what proof allows, and no bytes. + view.settle_unconfirmed_public_request(&alice.id(), &bob.id(), false) + .await + .expect("the reads"); + assert_eq!( + broadcasts_of(&alice_wallet, &transition).await, + sent_once, + "a private send broadcasts nothing" + ); + + if came_privately { + // Bob's request, read from the private store since. + track_incoming( + &alice_wallet, + &ContactRequest::new( + bob.id(), + alice.id(), + ENCRYPTION_KEY_ID, + DECRYPTION_KEY_ID, + 0, + vec![7; 96], + 0, + BLOCK_TIME_MS, + ), + ) + .await; + } else { + // A sender unignored since: the store is read again from 0. + let iw = alice_wallet.identity(); + let mut wm = iw.wallet_manager.write().await; + let managed = wm + .get_wallet_info_mut(&alice_wallet.wallet_id()) + .expect("wallet info") + .identity_manager + .managed_identity_mut(&alice.id()) + .expect("Alice"); + let carol = Identifier::from([9; 32]); + managed.ignore_sender(&carol); + managed.unignore_sender(&carol); + assert!(managed.dashpay().private_store_read_rewound()); + } + let _ = view.sync_contact_requests_reporting().await; + assert_eq!( + broadcasts_of(&alice_wallet, &transition).await, + sent_once, + "not broadcast again ({came_privately})" + ); + assert_eq!( + unconfirmed_to(&alice_wallet, ALICE, &bob).await, + Some((transition.clone(), reference)), + "the request stays held ({came_privately})" + ); + + if !came_privately { + view.sync_private_contact_requests(&alice_crypto, 4) + .await + .expect("the store is read to its end again"); + let _ = view.sync_contact_requests_reporting().await; + assert!( + broadcasts_of(&alice_wallet, &transition).await > sent_once, + "broadcast again once the read is at the store's end" + ); + } + } + } + + /// One public send holds a pair at a time: while one is being sent, a + /// second to the same recipient is refused with nothing signed, and the + /// first's release frees the pair. + #[tokio::test] + async fn should_refuse_a_second_public_send_while_one_is_being_sent() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let signed = Arc::new(AtomicUsize::new(0)); + let alice_signer = CountingSigner { + inner: signer(ALICE_PHRASE, &alice), + signed: Arc::clone(&signed), + }; + let (_manager, alice_wallet) = wallet(ALICE_PHRASE, &alice, &[&alice, &bob], vec![]).await; + expect_dashpay_nonce(&alice_wallet, &alice, 3).await; + let view = alice_wallet.identity().dashpay(); + + let first = view + .claim_public_pair(&alice.id(), &bob.id(), false, false) + .await + .expect("claimed") + .expect("a claim"); + let second = view + .claim_public_pair(&alice.id(), &bob.id(), false, false) + .await; + assert!( + matches!( + &second, + Err(PlatformWalletError::PublicContactRequestUnconfirmed(recipient)) + if *recipient == bob.id() + ), + "{:?}", + second.err() + ); + let sent = view + .send_contact_request_with_external_signer( + &alice.id(), + &bob.id(), + None, + AutoAcceptProofSource::None, + &alice_signer, + &alice_crypto, + ) + .await; + assert!( + matches!( + &sent, + Err(PlatformWalletError::PublicContactRequestUnconfirmed(recipient)) + if *recipient == bob.id() + ), + "{sent:?}" + ); + assert_eq!(signed.load(Ordering::SeqCst), 0, "nothing is signed"); + managed(&alice_wallet, ALICE, |managed| { + assert!( + managed.dashpay().public_contact_request_claimed(&bob.id()), + "the first send's claim stays" + ); + }) + .await; + + first.release().await; + let third = view + .claim_public_pair(&alice.id(), &bob.id(), false, false) + .await; + assert!( + matches!(third, Ok(Some(_))), + "the pair is free once the first send ends: {:?}", + third.err() + ); + } + + /// A send retried after an unconfirmed outcome, once its request is on + /// Platform, records that request and returns it as the one sent, with + /// nothing signed or broadcast: it does not build a request past the one + /// it just recorded. + #[tokio::test] + async fn should_return_the_unconfirmed_request_a_retry_finds_on_platform() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let signed = Arc::new(AtomicUsize::new(0)); + let alice_signer = CountingSigner { + inner: signer(ALICE_PHRASE, &alice), + signed: Arc::clone(&signed), + }; + let (_manager, alice_wallet, transition, reference) = + unconfirmed_public_request(&alice, &bob, &alice_signer, &alice_crypto).await; + let view = alice_wallet.identity().dashpay(); + let signatures = signed.load(Ordering::SeqCst); + let broadcasts = broadcasts_of(&alice_wallet, &transition).await; + + expect_sent_request( + &alice_wallet, + &alice, + &bob, + reference, + Some(sent_request_document(&alice, &bob, reference)), + ) + .await; + let retried = view + .send_contact_request_with_external_signer( + &alice.id(), + &bob.id(), + None, + AutoAcceptProofSource::None, + &alice_signer, + &alice_crypto, + ) + .await + .expect("the request it sent"); + assert_eq!(retried.account_reference, reference); + assert_eq!( + signed.load(Ordering::SeqCst), + signatures, + "nothing is signed" + ); + assert_eq!( + broadcasts_of(&alice_wallet, &transition).await, + broadcasts, + "nothing is broadcast" + ); + assert!(unconfirmed_to(&alice_wallet, ALICE, &bob).await.is_none()); + managed(&alice_wallet, ALICE, |managed| { + assert_eq!( + managed + .dashpay() + .outgoing_request(&bob.id()) + .map(|request| request.account_reference), + Some(reference), + "recorded as sent" + ); + }) + .await; + } + + /// A handle taken before its wallet was removed and created again under + /// its id registers no contact account on the replacement, whose own + /// handle does. + #[tokio::test] + async fn should_register_no_contact_account_on_a_replacement_from_a_stale_handle() { + use key_wallet::account::account_collection::DashpayAccountKey; + + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let manager = manager_on(Arc::new(NoPersistence)).await; + let first = create_holding(&manager, ALICE_PHRASE, &alice).await; + let wallet_id = first.wallet_id(); + let stale = first.identity().dashpay(); + manager.remove_wallet(&wallet_id).await.expect("removed"); + let again = create_holding(&manager, ALICE_PHRASE, &alice).await; + assert_eq!(again.wallet_id(), wallet_id); + let xpub = alice_crypto + .receiving_xpub(&"m/9'/1'/15'/0'".parse::().expect("a path")) + .await + .expect("an xpub"); + let registered = |wallet: Arc| async move { + let iw = wallet.identity(); + let wm = iw.wallet_manager.read().await; + wm.get_wallet_info(&wallet.wallet_id()) + .expect("wallet info") + .core_wallet + .accounts + .dashpay_receival_accounts + .contains_key(&DashpayAccountKey { + index: 0, + user_identity_id: ALICE, + friend_identity_id: BOB, + }) + }; + + let refused = stale + .register_contact_account(&alice.id(), &bob.id(), 0, xpub) + .await; + assert!( + matches!(refused, Err(PlatformWalletError::WalletNotFound(_))), + "{refused:?}" + ); + assert!( + !registered(Arc::clone(&again)).await, + "the replacement is given no account" + ); + + again + .identity() + .dashpay() + .register_contact_account(&alice.id(), &bob.id(), 0, xpub) + .await + .expect("registered by its own handle"); + assert!(registered(again).await); + } + + /// A private request paid for on a wallet removed, and created again + /// under its id, before the send read it back is reported pending, not + /// failed: the payment went out. + #[tokio::test] + async fn should_report_a_paid_private_request_pending_once_its_wallet_is_replaced() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_signer = signer(ALICE_PHRASE, &alice); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let persistence = RecordingPersistence::attesting( + PersistenceCapabilities::PRIVATE_CONTACT_SCAN_STATE_RESTART, + ); + let manager = manager_on(Arc::clone(&persistence)).await; + #[cfg(feature = "shielded")] + let _store = { + let dir = tempfile::tempdir().expect("temporary shielded store"); + manager + .configure_shielded(dir.path().join("tree.sqlite")) + .await + .expect("the shielded store is configured"); + dir + }; + let first = create_holding(&manager, ALICE_PHRASE, &alice).await; + let wallet_id = first.wallet_id(); + // The payment goes out, and the wallet is replaced before it returns. + let replacing = |_entry: BinaryData, started: PaymentStarted| { + let (manager, alice) = (&manager, &alice); + async move { + started.mark(); + manager.remove_wallet(&wallet_id).await.expect("removed"); + let again = create_holding(manager, ALICE_PHRASE, alice).await; + assert_eq!(again.wallet_id(), wallet_id); + Ok(ResponseMetadata { + time_ms: BLOCK_TIME_MS, + ..Default::default() + }) + } + }; + let sent = first + .identity() + .dashpay() + .send_private_contact_request( + &alice.id(), + &bob.id(), + None, + AutoAcceptProofSource::None, + None, + &alice_signer, + &alice_crypto, + replacing, + ) + .await; + assert!( + matches!( + &sent, + Err(PlatformWalletError::PrivateContactRequestPending(recipient)) + if *recipient == bob.id() + ), + "{sent:?}" + ); + } + /// A handle taken before its wallet was removed and created again under /// its id settles nothing of the replacement's unconfirmed public request, /// whatever the proofs show, and claims nothing on its pair. @@ -6426,7 +6822,7 @@ mod tests { ) .await; stale - .settle_unconfirmed_public_request(&alice.id(), &bob.id()) + .settle_unconfirmed_public_request(&alice.id(), &bob.id(), true) .await .expect("nothing to settle"); assert_eq!( @@ -6454,7 +6850,7 @@ mod tests { again .identity() .dashpay() - .settle_unconfirmed_public_request(&alice.id(), &bob.id()) + .settle_unconfirmed_public_request(&alice.id(), &bob.id(), true) .await .expect("settled"); assert!(unconfirmed_to(&again, ALICE, &bob).await.is_none()); diff --git a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/contact_requests.rs index ac70cc4f35a..6d4590717d6 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/contact_requests.rs @@ -1096,32 +1096,37 @@ impl ManagedIdentity { Some(self.insert_private_reply(recipient, None)) } - /// Claim the pair with `recipient` for a public request about to be - /// sent, under a new attempt it returns: until that attempt releases it + /// Claim the pair with `recipient` for the one public request that may + /// be being sent to it, under a new attempt it returns; `None` when + /// another send holds the claim. Until that attempt releases it /// ([`Self::release_public_contact_request`]), no private request to the - /// recipient is reserved. Concurrent public sends each hold their own. - pub(crate) fn claim_public_contact_request(&mut self, recipient: Identifier) -> u64 { + /// recipient is reserved and no other public one is signed: two sends + /// built from the same tracked requests would each sign one. + pub(crate) fn claim_public_contact_request(&mut self, recipient: Identifier) -> Option { + if self + .dashpay + .public_contact_request_claims + .contains_key(&recipient) + { + return None; + } let attempt = next_contact_request_attempt(); self.dashpay .public_contact_request_claims - .entry(recipient) - .or_default() - .insert(attempt); - attempt + .insert(recipient, attempt); + Some(attempt) } /// Release the claim `attempt` took ([`Self::claim_public_contact_request`]); - /// another attempt's claim on the pair stays. + /// a claim another attempt took on the pair since stays. pub(crate) fn release_public_contact_request(&mut self, recipient: &Identifier, attempt: u64) { - if let Some(claims) = self + if self .dashpay .public_contact_request_claims - .get_mut(recipient) + .get(recipient) + .is_some_and(|claimed| *claimed == attempt) { - claims.remove(&attempt); - if claims.is_empty() { - self.dashpay.public_contact_request_claims.remove(recipient); - } + self.dashpay.public_contact_request_claims.remove(recipient); } } diff --git a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/dashpay.rs b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/dashpay.rs index a9b1fdeca06..a4493cca343 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/dashpay.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/dashpay.rs @@ -192,14 +192,15 @@ pub struct DashPayState { /// restored from the shielded store's unconfirmed spends. pub(super) pending_private_replies: BTreeMap, - /// The public requests to each recipient being sent, by attempt: each + /// The public request to each recipient being sent, by its attempt: it /// claims the pair from before its channel is decided until it is - /// submitted and recorded, and while one does no private request to the - /// recipient is reserved, as a reserved one refuses a public send. A - /// claim is released only by its own attempt, whose id is process-wide, - /// so a send that outlived its wallet's removal and re-creation under the - /// same id cannot release a claim of the new wallet. In memory only. - pub(super) public_contact_request_claims: BTreeMap>, + /// submitted and recorded, and while it does no private request to the + /// recipient is reserved, as a reserved one refuses a public send, and + /// no other public one is signed. A claim is released only by its own + /// attempt, whose id is process-wide, so a send that outlived its + /// wallet's removal and re-creation under the same id cannot release a + /// claim of the new wallet. In memory only. + pub(super) public_contact_request_claims: BTreeMap, /// The public request to each recipient whose broadcast may have been /// delivered while no proof settled its outcome: it may still execute, so @@ -480,6 +481,14 @@ impl DashPayState { self.private_store_read_height } + /// Whether this identity's read of the private store was rewound + /// (`ManagedIdentity::rewind_private_store_read`) and has not reached + /// the store's end again: until it does, what the wallet tracks may miss + /// a private request the store holds. + pub(crate) fn private_store_read_rewound(&self) -> bool { + !self.private_contact_requests_read && self.private_store_read_floor.is_some() + } + /// How this identity and `contact` exchanged private requests, from the /// tracked requests either way; `None` when neither went privately. pub fn private_contact(&self, contact: &Identifier) -> Option { diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index 4f30a4c2e4c..0b80f1a213b 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -299,15 +299,7 @@ impl WalletPersister { if pending.is_empty() { return; } - if self.buffered_writes_lost() { - tracing::warn!( - dropped = pending.len(), - "private requests read back after this wallet's records were lost are not \ - noted; their journal records stay for the next process" - ); - return; - } - if !self.store_commits_inline() { + if !self.buffered_writes_lost() && !self.store_commits_inline() { if let Err(error) = self.flush() { if error.is_transient() { tracing::warn!( @@ -342,6 +334,17 @@ impl WalletPersister { return; } } + // Read after the flush, not only before it: a flush that failed for + // good on another thread in between left nothing buffered, and the + // flush here then succeeded over records that never reached disk. + if self.buffered_writes_lost() { + tracing::warn!( + dropped = pending.len(), + "private requests read back after this wallet's records were lost are not \ + noted; their journal records stay for the next process" + ); + return; + } self.note_read_back_private_entries(pending); } @@ -820,4 +823,56 @@ mod tests { assert!(!noted_on_failure, "a failed flush notes nothing"); assert_eq!(noted, BTreeSet::from([[1; 32], [2; 32]]), "nothing lost"); } + + /// A read-back is not noted on the strength of a flush that succeeded + /// after another failed for good, also when that failure came while this + /// call's own flush ran, after it read that nothing was lost: the one + /// that failed dropped the buffer, so the one here succeeded over records + /// that never reached disk. + #[cfg(feature = "shielded")] + #[test] + fn should_note_no_read_back_when_a_flush_fails_for_good_during_its_own() { + struct Overtaken { + persister: OnceLock, + flushes: AtomicU64, + } + impl PlatformWalletPersistence for Overtaken { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + match self.flushes.fetch_add(1, Ordering::SeqCst) { + // The flush under test: before it ends, another, as from + // another thread, fails for good. + 0 => { + let overtaking = self.persister.get().expect("set").flush(); + assert!(overtaking.is_err_and(|error| !error.is_transient())); + Ok(()) + } + 1 => Err(PersistenceError::backend("the database is corrupt")), + _ => Ok(()), + } + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + let backend = Arc::new(Overtaken { + persister: OnceLock::new(), + flushes: AtomicU64::new(0), + }); + let persister = WalletPersister::new([0; 32], Arc::clone(&backend) as _); + let _ = backend.persister.set(persister.clone()); + persister.note_read_back_once_durable([[1; 32]]); + assert!(persister.buffered_writes_lost()); + assert!( + !persister.has_read_back_private_entries(), + "its flush succeeded over a buffer the other one dropped" + ); + } } diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index c67a5462f70..0c71031a2af 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -3127,23 +3127,28 @@ impl PrivatePaymentRestore { /// Whether the requests the wallet tracks are durable now: at once for a /// backend that commits inline, as each was stored before it was - /// tracked; after a flush that succeeds otherwise. + /// tracked; after a flush that succeeds otherwise. Never once a flush of + /// the wallet's records failed for good in this process + /// (`WalletPersister::buffered_writes_lost`), which is read after the + /// flush here: one that failed for good on another thread just before it + /// left nothing buffered, so this one succeeds over records that never + /// reached disk. fn settled_requests_durable(&self) -> bool { - if self.persister.store_commits_inline() { - return true; + if self.persister.buffered_writes_lost() { + return false; } - match self.persister.flush() { - Ok(()) => true, - Err(error) => { + if !self.persister.store_commits_inline() { + if let Err(error) = self.persister.flush() { tracing::warn!( wallet_id = %hex::encode(self.wallet_id), %error, "the requests settling private payments are not flushed; their journal \ records stay for a later restoration" ); - false + return false; } } + !self.persister.buffered_writes_lost() } fn restores_lock(&self) -> std::sync::MutexGuard<'_, PrivatePaymentRestores> { @@ -3266,10 +3271,7 @@ impl PrivatePaymentRestore { .into_iter() .filter(|commitment| !self.persister.read_back_awaiting_flush(commitment)) .collect(); - if !settled.is_empty() - && !self.persister.buffered_writes_lost() - && self.settled_requests_durable() - { + if !settled.is_empty() && self.settled_requests_durable() { let store = if wait { Some(self.coordinator.store().write().await) } else { From 2d7c3ee29731cbbd755aba2699b34c85c068c7d9 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 13 Nov 2026 18:05:11 +0700 Subject: [PATCH 02/14] fix(platform-wallet): close the review's gaps in the contact request holds - A request recorded as sent is returned whether or not its receiving account could be registered: a registration that fails is logged and left to the next sync. Its error used to send a retry on to sign a second request past the one just recorded. - The repeat broadcast of a held public request takes the wallet generation's lifecycle gate before its check and keeps it through the dispatch, so nothing goes out for a wallet that is being removed. - note_external_account_registered, store_contact_account_label and the external registration's early already-exists check act only on the handle's wallet generation. - Durability is certified in one place, WalletPersister::flush_durably. A flush is counted as in progress from before the backend is called until its outcome is recorded, and a flush is trusted only when, after it, none is in progress and none has failed for good. A flush that had failed in the backend but not yet recorded it could otherwise be missed. A read-back noted while another flush is in progress is kept for a later call. Tests cover each, and the restoration path's use of the durability check. Co-Authored-By: Claude Fable 5.2 --- packages/rs-platform-wallet/README.md | 4 +- .../identity/network/contact_requests.rs | 58 ++- .../src/wallet/identity/network/contacts.rs | 10 +- .../identity/network/identity_handle.rs | 11 + .../network/private_contact_requests.rs | 333 +++++++++++++++++- .../src/wallet/persister.rs | 264 +++++++++++--- .../src/wallet/platform_wallet.rs | 31 +- 7 files changed, 616 insertions(+), 95 deletions(-) diff --git a/packages/rs-platform-wallet/README.md b/packages/rs-platform-wallet/README.md index 4363c45f06c..e1a256fbfd7 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -151,9 +151,9 @@ Every shielded spend that consumes notes (shielded transfers, unshields, withdra Every public `contactRequest` (a send, a public accept, the in-kind accept's public reply, a send from a QR code, an auto-accept and a rotation) is sent by one path, which signs the transition, keeps the signed bytes, and broadcasts them by the rule shielded spends use: only a refusal before anything was sent reports its error with nothing held. Once a dispatch may have reached an endpoint, the send waits for the result (not after an endpoint's verdict, which no proof backs), records the request when its document is confirmed, and otherwise reports the SDK's unconfirmed outcome (`StateTransitionResultUnconfirmed`, FFI code 20) and holds the pair as an unconfirmed public request. Before signing, the send reads the request it would make by DashPay's `ownerIdUserIdAndAccountRef` unique index (the sender, the recipient and the account reference); one found, sent by an earlier process that ended before recording it or by another device of the sender, is adopted as sent, with nothing signed. A read that fails sends on. -While an unconfirmed public request holds a pair, a public or private request to it, an accept included, is refused with `PublicContactRequestUnconfirmed` (FFI code 76), with nothing signed or paid, and the sender identity cannot be removed. `DashPayView::public_contact_request_unconfirmed` reads one pair's state, beside `DashPayView::private_pair_held`, so a host shows a public request awaiting its outcome apart from a private one pending. The public contact request sync settles it, as does each send or accept to the pair before it decides: `Sdk::state_transition_outcome` reads the transition's identity contract nonce, proved, and then the request is read by the unique index, proved. A request found is recorded as sent, as a confirmed send records it, and frees the pair; a send that finds it this way returns it as the request sent, with nothing signed, rather than building another past it. With none found, a nonce proved used at a height at or below the height of the read that missed the request shows the transition never wrote it and never will, and frees the pair; a new request may then be sent. Otherwise the transition may still execute, and the pair stays held. No retry signs a second transition. +While an unconfirmed public request holds a pair, a public or private request to it, an accept included, is refused with `PublicContactRequestUnconfirmed` (FFI code 76), with nothing signed or paid, and the sender identity cannot be removed. `DashPayView::public_contact_request_unconfirmed` reads one pair's state, beside `DashPayView::private_pair_held`, so a host shows a public request awaiting its outcome apart from a private one pending. The public contact request sync settles it, as does each send or accept to the pair before it decides: `Sdk::state_transition_outcome` reads the transition's identity contract nonce, proved, and then the request is read by the unique index, proved. A request found is recorded as sent, as a confirmed send records it, and frees the pair; a send that finds it this way returns it as the request sent, with nothing signed, rather than building another past it. That holds whether or not the receiving account for the pair could be registered then: a registration that fails is logged and left to the next sync, and is never reported as a failed send. With none found, a nonce proved used at a height at or below the height of the read that missed the request shows the transition never wrote it and never will, and frees the pair; a new request may then be sent. Otherwise the transition may still execute, and the pair stays held. No retry signs a second transition. -While it may still execute, the same signed bytes are broadcast again only where a public request to the recipient would still go out, by what the wallet tracks. The public sync does it, and so does a public send whose read of the private store succeeded; a private send never does, as it is about to take the pair privately. Nothing is broadcast while the wallet's private payments may be unrestored, while the sender's read of the private store is rewound (keys that changed, a sender unignored) and has not reached the store's end again, or once a private request holds the pair or their requests came or went privately: the request then stays held until a proved read settles it. This is decided from what the wallet has read, not from the store: a private request the store holds that no private sync has read in yet does not stop the broadcast, as one sealed after a send's own store read does not stop that send. +While it may still execute, the same signed bytes are broadcast again only where a public request to the recipient would still go out, by what the wallet tracks. The public sync does it, and so does a public send whose read of the private store succeeded; a private send never does, as it is about to take the pair privately. The check and the dispatch run under the wallet generation's lifecycle gate, so nothing goes out for a wallet that is being removed. Nothing is broadcast while the wallet's private payments may be unrestored, while the sender's read of the private store is rewound (keys that changed, a sender unignored) and has not reached the store's end again, or once a private request holds the pair or their requests came or went privately: the request then stays held until a proved read settles it. This is decided from what the wallet has read, not from the store: a private request the store holds that no private sync has read in yet does not stop the broadcast, as one sealed after a send's own store read does not stop that send. One public send holds a pair at a time. While one is being sent, a second public request to the same recipient is refused with `PublicContactRequestUnconfirmed` too, with nothing signed: built from the same recorded requests, it would sign a request of its own while the first may still execute. `DashPayView::public_contact_request_unconfirmed` reports only a request held as unconfirmed, not one being sent. diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs index 8a0afefc276..a4c3ed2d0c7 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs @@ -1283,6 +1283,12 @@ impl DashPayView<'_, B> { /// the unconfirmed public request to the recipient this request settles, /// released in the same lock it is recorded in. Only on this handle's /// wallet generation: the caller holds the generation's lifecycle gate. + /// + /// Once the request is recorded it is the request sent, whatever becomes + /// of the account: a registration that fails is logged and left to the + /// next sync, which registers it. Reporting it as a failure would have + /// the caller send again, and a request built past the one just recorded + /// is a second request. async fn record_sent_contact_request( &self, sender: &Identifier, @@ -1310,8 +1316,18 @@ impl DashPayView<'_, B> { managed.settle_unconfirmed_public_request(recipient, attempt); } } - self.register_contact_account(sender, recipient, account_index, contact_xpub) - .await?; + if let Err(error) = self + .register_contact_account(sender, recipient, account_index, contact_xpub) + .await + { + tracing::warn!( + sender = %sender, + recipient = %recipient, + %error, + "contact request recorded as sent; its receiving account is not registered yet, \ + the next sync registers it" + ); + } Ok(request) } @@ -1470,21 +1486,25 @@ impl DashPayView<'_, B> { TransitionOutcome::StillPossible { .. } => false, }; if found.is_none() && !settled { - if broadcast_again - && matches!(outcome, TransitionOutcome::StillPossible { .. }) - && self + if broadcast_again && matches!(outcome, TransitionOutcome::StillPossible { .. }) { + // The lifecycle gate, from the check through the dispatch: a + // wallet removed in between has none of its requests pushed + // to the network after it is gone. + let _lifecycle = self.generation.payment_guard().await; + if self .may_broadcast_held_public_request(sender, recipient, held.attempt) .await - { - // The same bytes, so never a second request; an endpoint's - // answer proves nothing either way. - let delivery = broadcast_tracking_delivery(&self.sdk, &held.transition).await; - tracing::debug!( - sender = %sender, - recipient = %recipient, - ?delivery, - "unconfirmed public contact request broadcast again" - ); + { + // The same bytes, so never a second request; an + // endpoint's answer proves nothing either way. + let delivery = broadcast_tracking_delivery(&self.sdk, &held.transition).await; + tracing::debug!( + sender = %sender, + recipient = %recipient, + ?delivery, + "unconfirmed public contact request broadcast again" + ); + } } return Ok(None); } @@ -4474,7 +4494,10 @@ impl DashPayView<'_, B> { built_from_ciphertext: &[u8], ) { let mut wm = self.wallet_manager.write().await; - let Some(info) = wm.get_wallet_info_mut(&self.wallet_id) else { + // On this handle's wallet generation only: the registration this + // completes ran without the lock, and a wallet re-created under this + // id since has its own marker to keep. + let Ok(info) = self.current_wallet_info_mut(&mut wm) else { return; }; let Some(managed) = info.identity_manager.managed_identity_mut(identity_id) else { @@ -4566,7 +4589,8 @@ impl DashPayView<'_, B> { shared_key: &[u8; 32], ) { let mut wm = self.wallet_manager.write().await; - let Some(info) = wm.get_wallet_info_mut(&self.wallet_id) else { + // On this handle's wallet generation only, as the marker above. + let Ok(info) = self.current_wallet_info_mut(&mut wm) else { return; }; let Some(managed) = info.identity_manager.managed_identity_mut(identity_id) else { diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs index 0a62522b495..ac1b8d7221e 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs @@ -483,13 +483,13 @@ impl DashPayView<'_, B> { let contact_identity_id = contact_identity.id(); // --- 1. Early-exit if the external account already exists. --- + // + // On this handle's wallet generation only: a wallet re-created under + // this id since may hold an account of its own, which would answer + // for this registration and clear a caller's queued one. { let wm = self.wallet_manager.read().await; - let info = wm.get_wallet_info(&self.wallet_id).ok_or_else(|| { - Transient(PlatformWalletError::WalletNotFound(hex::encode( - self.wallet_id, - ))) - })?; + let info = self.current_wallet_info(&wm).map_err(Transient)?; use key_wallet::account::account_collection::DashpayAccountKey; let key = DashpayAccountKey { index: account_index, diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/identity_handle.rs b/packages/rs-platform-wallet/src/wallet/identity/network/identity_handle.rs index c8e7937b2e6..4a5357f3621 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/identity_handle.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/identity_handle.rs @@ -583,6 +583,17 @@ impl IdentityWallet { .ok_or_else(|| PlatformWalletError::WalletNotFound(hex::encode(self.wallet_id))) } + /// [`Self::current_wallet_info_mut`], for a read: under the manager's + /// read lock. + pub(crate) fn current_wallet_info<'w>( + &self, + wm: &'w WalletManager, + ) -> Result<&'w PlatformWalletInfo, PlatformWalletError> { + wm.get_wallet_info(&self.wallet_id) + .filter(|info| Arc::ptr_eq(&info.generation, &self.generation)) + .ok_or_else(|| PlatformWalletError::WalletNotFound(hex::encode(self.wallet_id))) + } + /// [`Self::current_wallet_info_mut`], with the wallet itself. pub(crate) fn current_wallet_and_info_mut<'w>( &self, diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs index 89cf44666a5..e7467ef9475 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs @@ -2739,6 +2739,7 @@ mod tests { PlatformWalletPersistence, }; use crate::events::{EventHandler, PlatformEventHandler}; + use crate::wallet::identity::network::contacts::RegisterExternalError; use crate::wallet::identity::network::identity_auth_derivation_path_for_type; use crate::wallet::identity::network::SeedCryptoProvider; use crate::wallet::platform_wallet::{PlatformWallet, WalletId}; @@ -2975,6 +2976,14 @@ mod tests { /// While set, a flush fails fatally: the backend drops what it /// buffered. flush_fails_fatally: AtomicBool, + /// While set, a store carrying an account registration fails and + /// keeps nothing. + accounts_fail: AtomicBool, + /// Once set, the next flush is overtaken: before it writes, a flush + /// through this persister fails fatally, as one from another thread + /// would, dropping what was buffered; the overtaken flush then + /// succeeds over nothing. + overtaken_by: Mutex>, } impl RecordingPersistence { @@ -2985,6 +2994,8 @@ mod tests { positions_fail: AtomicBool::new(false), flush_fails: AtomicBool::new(false), flush_fails_fatally: AtomicBool::new(false), + accounts_fail: AtomicBool::new(false), + overtaken_by: Mutex::new(None), }) } @@ -3013,11 +3024,25 @@ mod tests { { return Err(PersistenceError::backend("the disk is full")); } + if self.accounts_fail.load(Ordering::SeqCst) + && !changeset.account_registrations.is_empty() + { + return Err(PersistenceError::backend("the disk is full")); + } self.stored.lock().expect("stored").push(changeset); Ok(()) } fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + // Taken in its own statement: the flush below comes back here. + let overtaking = self.overtaken_by.lock().expect("overtaken").take(); + if let Some(persister) = overtaking { + self.flush_fails_fatally.store(true, Ordering::SeqCst); + let overtook = persister.flush(); + self.flush_fails_fatally.store(false, Ordering::SeqCst); + assert!(overtook.is_err_and(|error| !error.is_transient())); + return Ok(()); + } if self.flush_fails.load(Ordering::SeqCst) { return Err(PersistenceError::backend_with_kind( PersistenceErrorKind::Transient, @@ -3965,6 +3990,73 @@ mod tests { .expect("the hold")); } + /// A restoration does not discharge a record its sender's tracked request + /// settles on the strength of its own flush when another flush failed + /// for good while it ran: that one dropped what was buffered, the request + /// among it, and the restoration's flush then succeeded over nothing. + #[cfg(feature = "shielded")] + #[tokio::test] + async fn should_keep_a_settled_record_when_a_flush_fails_for_good_during_the_restoration() { + use platform_encryption::ACCOUNT_REFERENCE_VERSION_SHIFT; + + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let fixture = ExpiryFixture::new().await; + fixture + .pay_unconfirmed(ExpiryFixture::ANCHOR, ExpiryFixture::ANCHOR_SET_HEIGHT) + .await; + fixture + .wallet + .restore_private_contact_payments(&fixture.coordinator) + .await + .expect("restored"); + let reference = fixture.journal_snapshot().await[0] + .1 + .payment + .account_reference; + // A newer request to the pair, buffered: it settles the record. + { + let iw = fixture.wallet.identity(); + let mut wm = iw.wallet_manager.write().await; + wm.get_wallet_info_mut(&fixture.wallet.wallet_id()) + .expect("wallet info") + .identity_manager + .managed_identity_mut(&alice.id()) + .expect("Alice") + .add_sent_contact_request( + public_request( + &alice, + &bob, + reference + (1 << ACCOUNT_REFERENCE_VERSION_SHIFT), + ), + &iw.persister, + ) + .expect("stored, not flushed"); + } + *fixture.persistence.overtaken_by.lock().expect("overtaken") = + Some(fixture.wallet.persister().clone()); + fixture + .wallet + .restore_private_contact_payments(&fixture.coordinator) + .await + .expect("restored"); + assert!( + fixture + .persistence + .overtaken_by + .lock() + .expect("overtaken") + .is_none(), + "the restoration flushed" + ); + assert!(fixture.wallet.persister().buffered_writes_lost()); + assert_eq!( + fixture.journaled().await, + 1, + "kept: the request settling it never reached disk" + ); + } + /// A restoration discharges a record its sender's tracked request settles /// only once what records that request is durable. A newer request to the /// pair, stored but not flushed as a flush fails transiently, leaves the @@ -6398,7 +6490,24 @@ mod tests { StateTransition, u32, ) { - let (manager, alice_wallet) = wallet(ALICE_PHRASE, alice, &[alice, bob], vec![]).await; + unconfirmed_public_request_on(Arc::new(NoPersistence), alice, bob, signer, crypto).await + } + + /// [`unconfirmed_public_request`] on the backend `persistence`. + async fn unconfirmed_public_request_on( + persistence: Arc

, + alice: &Identity, + bob: &Identity, + signer: &impl Signer, + crypto: &SeedCryptoProvider, + ) -> ( + PlatformWalletManager

, + Arc, + StateTransition, + u32, + ) { + let (manager, alice_wallet) = + wallet_on(persistence, ALICE_PHRASE, alice, &[alice, bob], vec![]).await; expect_dashpay_nonce(&alice_wallet, alice, 3).await; let sent = alice_wallet .identity() @@ -6652,8 +6761,6 @@ mod tests { /// handle does. #[tokio::test] async fn should_register_no_contact_account_on_a_replacement_from_a_stale_handle() { - use key_wallet::account::account_collection::DashpayAccountKey; - let alice = identity(ALICE_PHRASE, ALICE); let bob = identity(BOB_PHRASE, BOB); let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); @@ -6766,6 +6873,226 @@ mod tests { ); } + /// A send retried after an unconfirmed outcome, whose request is on + /// Platform but whose receiving account cannot be registered, still + /// returns the request it recorded, with nothing signed or broadcast: the + /// account is left to the next sync, and is no reason to send again. + #[tokio::test] + async fn should_return_the_recorded_request_when_its_receiving_account_is_not_registered() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let signed = Arc::new(AtomicUsize::new(0)); + let alice_signer = CountingSigner { + inner: signer(ALICE_PHRASE, &alice), + signed: Arc::clone(&signed), + }; + let persistence = RecordingPersistence::attesting( + PersistenceCapabilities::PRIVATE_CONTACT_SCAN_STATE_RESTART, + ); + let (_manager, alice_wallet, transition, reference) = unconfirmed_public_request_on( + Arc::clone(&persistence), + &alice, + &bob, + &alice_signer, + &alice_crypto, + ) + .await; + let view = alice_wallet.identity().dashpay(); + let signatures = signed.load(Ordering::SeqCst); + let broadcasts = broadcasts_of(&alice_wallet, &transition).await; + + expect_sent_request( + &alice_wallet, + &alice, + &bob, + reference, + Some(sent_request_document(&alice, &bob, reference)), + ) + .await; + persistence.accounts_fail.store(true, Ordering::SeqCst); + let retried = view + .send_contact_request_with_external_signer( + &alice.id(), + &bob.id(), + None, + AutoAcceptProofSource::None, + &alice_signer, + &alice_crypto, + ) + .await + .expect("the request it recorded"); + assert_eq!(retried.account_reference, reference); + assert_eq!( + signed.load(Ordering::SeqCst), + signatures, + "nothing is signed" + ); + assert_eq!( + broadcasts_of(&alice_wallet, &transition).await, + broadcasts, + "nothing is broadcast" + ); + assert!(unconfirmed_to(&alice_wallet, ALICE, &bob).await.is_none()); + let iw = alice_wallet.identity(); + let wm = iw.wallet_manager.read().await; + assert!( + !wm.get_wallet_info(&alice_wallet.wallet_id()) + .expect("wallet info") + .core_wallet + .accounts + .dashpay_receival_accounts + .contains_key(&DashpayAccountKey { + index: 0, + user_identity_id: ALICE, + friend_identity_id: BOB, + }), + "its account waits for the next sync" + ); + } + + /// While its wallet is being removed, an unconfirmed public request is + /// not broadcast again: the settling takes the wallet generation's + /// lifecycle gate before it checks that the request may go out, and + /// keeps it through the dispatch. + #[tokio::test] + async fn should_not_broadcast_a_held_request_again_while_its_wallet_is_being_removed() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let alice_signer = signer(ALICE_PHRASE, &alice); + let (_manager, alice_wallet, transition, reference) = + unconfirmed_public_request(&alice, &bob, &alice_signer, &alice_crypto).await; + expect_sent_request(&alice_wallet, &alice, &bob, reference, None).await; + let view = alice_wallet.identity().dashpay(); + let sent_once = broadcasts_of(&alice_wallet, &transition).await; + + // As a removal holds it. + let removal = alice_wallet.generation().teardown_guard().await; + let (alice_id, bob_id) = (alice.id(), bob.id()); + let mut settling = + Box::pin(view.settle_unconfirmed_public_request(&alice_id, &bob_id, true)); + assert!( + tokio::time::timeout(Duration::from_millis(300), &mut settling) + .await + .is_err(), + "the settling waits for the removal" + ); + assert_eq!( + broadcasts_of(&alice_wallet, &transition).await, + sent_once, + "nothing is broadcast while the wallet is being removed" + ); + + drop(removal); + settling.await.expect("the reads"); + assert!( + broadcasts_of(&alice_wallet, &transition).await > sent_once, + "broadcast again once the gate is free" + ); + } + + /// A handle taken before its wallet was removed and created again under + /// its id completes no external account registration on the replacement: + /// it stamps no outbound-account marker, stores no label, and its + /// registration is refused as not found before anything is read from the + /// replacement's accounts. The replacement's own handle does all three. + #[tokio::test] + async fn should_complete_no_external_account_on_a_replacement_from_a_stale_handle() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let manager = manager_on(RecordingPersistence::attesting( + PersistenceCapabilities::PRIVATE_CONTACT_SCAN_STATE_RESTART, + )) + .await; + let first = create_holding(&manager, ALICE_PHRASE, &alice).await; + let wallet_id = first.wallet_id(); + let stale = first.identity().dashpay(); + manager.remove_wallet(&wallet_id).await.expect("removed"); + let again = create_holding(&manager, ALICE_PHRASE, &alice).await; + assert_eq!(again.wallet_id(), wallet_id); + + // Bob is a contact of the replacement, his request carrying a label. + let shared = [0x55u8; 32]; + let mut incoming = public_request(&bob, &alice, 0); + incoming.encrypted_account_label = Some(platform_encryption::encrypt_account_label( + &shared, + &[0x11u8; 16], + "Main wallet", + )); + let payload = incoming.encrypted_public_key.clone(); + { + let iw = again.identity(); + let mut wm = iw.wallet_manager.write().await; + let managed = wm + .get_wallet_info_mut(&wallet_id) + .expect("wallet info") + .identity_manager + .managed_identity_mut(&alice.id()) + .expect("Alice"); + managed + .add_sent_contact_request(public_request(&alice, &bob, 0), &iw.persister) + .expect("sent request"); + managed + .add_incoming_contact_request(incoming, &iw.persister) + .expect("incoming request"); + } + let completed = |wallet: Arc| async move { + managed(&wallet, ALICE, |managed| { + let contact = managed + .dashpay() + .established_contacts() + .get(&Identifier::from(BOB)) + .expect("established"); + ( + contact.external_account_reference, + contact.contact_account_label.clone(), + ) + }) + .await + }; + + stale + .note_external_account_registered(&alice.id(), &bob.id(), &payload) + .await; + stale + .store_contact_account_label(&alice.id(), &bob.id(), &shared) + .await; + assert_eq!( + completed(Arc::clone(&again)).await, + (None, None), + "the replacement keeps its own marker and label" + ); + let refused = stale + .register_external_contact_account( + &alice.id(), + &bob, + &payload, + zeroize::Zeroizing::new(shared), + ) + .await; + assert!( + matches!( + refused, + Err(RegisterExternalError::Transient( + PlatformWalletError::WalletNotFound(_) + )) + ), + "refused before the payload is opened" + ); + + let live = again.identity().dashpay(); + live.note_external_account_registered(&alice.id(), &bob.id(), &payload) + .await; + live.store_contact_account_label(&alice.id(), &bob.id(), &shared) + .await; + assert_eq!( + completed(again).await, + (Some(0), Some("Main wallet".to_string())), + "completed by its own handle" + ); + } + /// A handle taken before its wallet was removed and created again under /// its id settles nothing of the replacement's unconfirmed public request, /// whatever the proofs show, and claims nothing on its pair. diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index 0b80f1a213b..ec319942da3 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -5,6 +5,8 @@ //! a fixed `wallet_id` so callers don't need to pass the ID on every call. use std::collections::{BTreeMap, BTreeSet}; +#[cfg(feature = "shielded")] +use std::sync::atomic::AtomicUsize; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::Arc; @@ -106,6 +108,13 @@ pub struct WalletPersister { /// next process tracks what its load read from disk. #[cfg(feature = "shielded")] buffered_writes_lost: Arc, + /// How many flushes of this wallet's records are in progress: each is + /// counted from before it calls the backend until its outcome is + /// recorded in `buffered_writes_lost`, so while none is, that flag tells + /// of every flush that failed for good ([`Self::flush_durably`]). Shared + /// by the clones the wallet and the coordinator hold. + #[cfg(feature = "shielded")] + flushes_in_flight: Arc, /// The largest size of DashPay's private contact request store a page /// this wallet read proved, in this process /// ([`Self::private_store_size_proved`]). In memory only. @@ -159,6 +168,40 @@ struct DischargedPrivateEntries { journaled: BTreeMap<[u8; 32], (Identifier, Identifier)>, } +/// Why the records stored for a wallet are not known to be durable +/// ([`WalletPersister::flush_durably`]). +#[cfg(feature = "shielded")] +#[derive(Debug)] +pub(crate) enum NotDurable { + /// A flush failed for good in this process, the backend dropping what it + /// buffered ([`WalletPersister::buffered_writes_lost`]). + Lost, + /// The flush this call made failed. + Flush(PersistenceError), + /// Another flush is in progress, and its outcome is not recorded yet. + OtherFlushInFlight, +} + +/// One flush in progress, counted in [`WalletPersister`]'s +/// `flushes_in_flight` until dropped. +#[cfg(feature = "shielded")] +struct FlushInFlight<'a>(&'a AtomicUsize); + +#[cfg(feature = "shielded")] +impl<'a> FlushInFlight<'a> { + fn enter(in_flight: &'a AtomicUsize) -> Self { + in_flight.fetch_add(1, Ordering::SeqCst); + Self(in_flight) + } +} + +#[cfg(feature = "shielded")] +impl Drop for FlushInFlight<'_> { + fn drop(&mut self) { + self.0.fetch_sub(1, Ordering::SeqCst); + } +} + impl WalletPersister { pub fn new(wallet_id: WalletId, inner: Arc) -> Self { Self { @@ -174,6 +217,8 @@ impl WalletPersister { stored_private_markers: Arc::default(), #[cfg(feature = "shielded")] buffered_writes_lost: Arc::default(), + #[cfg(feature = "shielded")] + flushes_in_flight: Arc::default(), private_store_size_proved: Arc::default(), private_payment_restore_failure: Arc::default(), #[cfg(feature = "shielded")] @@ -270,14 +315,16 @@ impl WalletPersister { /// Note `commitments`, of private requests a private sync read back and /// recorded, for their journal records to be discharged /// ([`Self::note_read_back_private_entries`]), once what records them is - /// durable: at once for a backend that commits inline, after a flush - /// otherwise. Those a transient flush failure left are kept, with every - /// one kept before, and noted by the next call whose flush succeeds: an - /// empty one retries them. Those any other failure left are dropped, as the - /// backend dropped the records they rest on. Their journal records, and so - /// the pairs they hold, stay until noted. The host's flush runs with none - /// of the persister's locks held, so a call during it, from another thread - /// or the flush itself, neither waits nor loses what it adds. + /// durable ([`Self::flush_durably`]): at once for a backend that commits + /// inline, after a flush otherwise. Those a transient flush failure left + /// are kept, with every one kept before, and noted by the next call whose + /// flush succeeds: an empty one retries them. So are those of a call made + /// while another flush is in progress, whose outcome is not known yet. + /// Those any other failure left are dropped, as the backend dropped the + /// records they rest on. Their journal records, and so the pairs they + /// hold, stay until noted. The host's flush runs with none of the + /// persister's locks held, so a call during it, from another thread or + /// the flush itself, neither waits nor loses what it adds. /// /// None is noted once a flush of the wallet's records failed for good in /// this process ([`Self::buffered_writes_lost`]): a request read back @@ -299,53 +346,53 @@ impl WalletPersister { if pending.is_empty() { return; } - if !self.buffered_writes_lost() && !self.store_commits_inline() { - if let Err(error) = self.flush() { - if error.is_transient() { - tracing::warn!( - %error, - kept = pending.len(), - "private requests read back are not durably recorded; their journal \ - records stay until a later flush succeeds" - ); - // A transient failure keeps the buffer: with any added - // meanwhile. - self.unflushed_read_back_entries().extend(pending); - } else { - // Any other failure dropped the buffer, the records these - // notices rest on with it: the notices go, so their journal - // records, and the pairs they hold, stay. Their read is not - // rewound: a read again would find the requests already - // tracked, store nothing, and note them again over records - // that never reached disk. The markers stored into that - // buffer are forgotten, so the next comparison stores them - // again (`Self::private_marker_stored`). - tracing::error!( - %error, - dropped = pending.len(), - "private requests read back are not durably recorded, and the backend \ - dropped what it buffered; their journal records stay" - ); - self.stored_private_markers - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .clear(); - } - return; + match self.flush_durably() { + Ok(()) => self.note_read_back_private_entries(pending), + Err(NotDurable::Flush(error)) if error.is_transient() => { + tracing::warn!( + %error, + kept = pending.len(), + "private requests read back are not durably recorded; their journal \ + records stay until a later flush succeeds" + ); + // A transient failure keeps the buffer: with any added + // meanwhile. + self.unflushed_read_back_entries().extend(pending); } - } - // Read after the flush, not only before it: a flush that failed for - // good on another thread in between left nothing buffered, and the - // flush here then succeeded over records that never reached disk. - if self.buffered_writes_lost() { - tracing::warn!( + Err(NotDurable::OtherFlushInFlight) => { + tracing::warn!( + kept = pending.len(), + "private requests read back while another flush of this wallet's records \ + is in progress are not noted yet; a later call notes them" + ); + self.unflushed_read_back_entries().extend(pending); + } + Err(NotDurable::Flush(error)) => { + // Any other failure dropped the buffer, the records these + // notices rest on with it: the notices go, so their journal + // records, and the pairs they hold, stay. Their read is not + // rewound: a read again would find the requests already + // tracked, store nothing, and note them again over records + // that never reached disk. The markers stored into that + // buffer are forgotten, so the next comparison stores them + // again (`Self::private_marker_stored`). + tracing::error!( + %error, + dropped = pending.len(), + "private requests read back are not durably recorded, and the backend \ + dropped what it buffered; their journal records stay" + ); + self.stored_private_markers + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .clear(); + } + Err(NotDurable::Lost) => tracing::warn!( dropped = pending.len(), "private requests read back after this wallet's records were lost are not \ noted; their journal records stay for the next process" - ); - return; + ), } - self.note_read_back_private_entries(pending); } /// Whether the read-back of `commitment` waits for a flush that failed @@ -561,6 +608,10 @@ impl WalletPersister { } pub(crate) fn flush(&self) -> Result<(), PersistenceError> { + // In progress from before the backend is called until its outcome is + // recorded below (`Self::flush_durably`). + #[cfg(feature = "shielded")] + let _in_flight = FlushInFlight::enter(&self.flushes_in_flight); let flushed = self.inner.flush(self.wallet_id); #[cfg(feature = "shielded")] if flushed.as_ref().is_err_and(|error| !error.is_transient()) { @@ -569,6 +620,42 @@ impl WalletPersister { flushed } + /// Make every record stored for this wallet before the call durable, and + /// answer `Ok` only once it is: at once for a backend that commits + /// inline, after a flush that succeeds otherwise, and never once a flush + /// failed for good in this process ([`Self::buffered_writes_lost`]). + /// + /// A flush that succeeds proves nothing by itself: one that failed for + /// good just before it left nothing buffered, so it succeeds over records + /// that never reached disk. That failure is recorded only after the + /// backend returns, so between the two it shows nowhere but in the count + /// of flushes in progress. The flush here is therefore trusted only when, + /// after it, no flush is in progress and none has failed for good, read + /// in that order: every flush that began before then has recorded its + /// outcome. One that fails later drops only what was buffered after this + /// one wrote, so what this call answers for stays durable, and what the + /// caller does on the strength of it needs no lock held with the flush. + /// + /// While another flush is in progress the answer is + /// [`NotDurable::OtherFlushInFlight`], also for a call made from inside + /// a backend's own flush: the caller asks again later. + #[cfg(feature = "shielded")] + pub(crate) fn flush_durably(&self) -> Result<(), NotDurable> { + if self.buffered_writes_lost() { + return Err(NotDurable::Lost); + } + if !self.store_commits_inline() { + self.flush().map_err(NotDurable::Flush)?; + if self.flushes_in_flight.load(Ordering::SeqCst) != 0 { + return Err(NotDurable::OtherFlushInFlight); + } + } + if self.buffered_writes_lost() { + return Err(NotDurable::Lost); + } + Ok(()) + } + /// Whether a flush of this wallet's records failed for good in this /// process (anything but a transient failure, which keeps the buffer): /// what the wallet tracked before it may not be on disk, though it is in @@ -875,4 +962,81 @@ mod tests { "its flush succeeded over a buffer the other one dropped" ); } + + /// A read-back is not noted while another flush of the wallet's records + /// is still inside the backend: that one may have dropped what was + /// buffered already, and its failure is recorded only once it returns. + /// The notice is kept, and dropped once the failure shows. + #[cfg(feature = "shielded")] + #[test] + fn should_note_no_read_back_while_another_flush_has_not_returned() { + struct Stalled { + flushes: AtomicU64, + entered: std::sync::Mutex>, + release: std::sync::Mutex>, + } + impl PlatformWalletPersistence for Stalled { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + if self.flushes.fetch_add(1, Ordering::SeqCst) != 0 { + return Ok(()); + } + // The first flush dropped what was buffered, and has not + // returned yet. + self.entered + .lock() + .expect("entered") + .send(()) + .expect("the test waits"); + self.release + .lock() + .expect("release") + .recv_timeout(Duration::from_secs(10)) + .expect("released"); + Err(PersistenceError::backend("the database is corrupt")) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + let (entered, has_entered) = mpsc::channel(); + let (release, released) = mpsc::channel(); + let backend = Arc::new(Stalled { + flushes: AtomicU64::new(0), + entered: std::sync::Mutex::new(entered), + release: std::sync::Mutex::new(released), + }); + let persister = WalletPersister::new([0; 32], backend as _); + let failing = { + let persister = persister.clone(); + std::thread::spawn(move || persister.flush()) + }; + has_entered + .recv_timeout(Duration::from_secs(10)) + .expect("the first flush is inside the backend"); + + persister.note_read_back_once_durable([[1; 32]]); + let noted_meanwhile = persister.has_read_back_private_entries(); + let kept = persister.read_back_awaiting_flush(&[1; 32]); + release.send(()).expect("the first flush waits"); + let failed = failing.join().expect("the first flush returns"); + + assert!(failed.is_err_and(|error| !error.is_transient())); + assert!(!noted_meanwhile, "its own flush proves nothing yet"); + assert!(kept, "kept for a later call"); + persister.note_read_back_once_durable([]); + assert!(persister.buffered_writes_lost()); + assert!( + !persister.has_read_back_private_entries(), + "the buffer was lost" + ); + assert!(!persister.read_back_awaiting_flush(&[1; 32])); + } } diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index 0c71031a2af..eaa22e1af34 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -3125,30 +3125,25 @@ impl PrivatePaymentRestore { restored } - /// Whether the requests the wallet tracks are durable now: at once for a - /// backend that commits inline, as each was stored before it was - /// tracked; after a flush that succeeds otherwise. Never once a flush of - /// the wallet's records failed for good in this process - /// (`WalletPersister::buffered_writes_lost`), which is read after the - /// flush here: one that failed for good on another thread just before it - /// left nothing buffered, so this one succeeds over records that never - /// reached disk. + /// Whether the requests the wallet tracks are durable now + /// (`WalletPersister::flush_durably`): at once for a backend that commits + /// inline, as each was stored before it was tracked; after a flush that + /// succeeds otherwise, with no other flush in progress and none that + /// failed for good in this process. A flush that succeeds right after + /// one that failed for good wrote nothing of what that one dropped. fn settled_requests_durable(&self) -> bool { - if self.persister.buffered_writes_lost() { - return false; - } - if !self.persister.store_commits_inline() { - if let Err(error) = self.persister.flush() { + match self.persister.flush_durably() { + Ok(()) => true, + Err(reason) => { tracing::warn!( wallet_id = %hex::encode(self.wallet_id), - %error, - "the requests settling private payments are not flushed; their journal \ - records stay for a later restoration" + ?reason, + "the requests settling private payments are not known to be durable; \ + their journal records stay for a later restoration" ); - return false; + false } } - !self.persister.buffered_writes_lost() } fn restores_lock(&self) -> std::sync::MutexGuard<'_, PrivatePaymentRestores> { From 974c9da8ed5f698a6a3c7da1b032444a767a28ac Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 13 Nov 2026 21:51:16 +0700 Subject: [PATCH 03/14] fix(platform-wallet): settle a held request once, and count every flush of a wallet id together - A settle of an unconfirmed public request records the request it found on Platform only while its own hold still stands, checked in the lock it records in. Its reads run with no lock held, so another settle of the same hold can record the request and release the hold first, and a request sent after that is the one tracked: the later settle used to record the older request over it. It now records nothing, and answers the request only when that is still the one tracked. - The flushes of the records under a wallet id are counted in one ledger per id, kept with the manager's per-wallet state: how many are in progress and how many failed for good. A wallet's persister, the persister of a wallet removed or created under the same id, and the manager's own flush of a replaced backfill record all go through it. The count used to live in each wallet's persister, so a flush made anywhere else could drop the buffer without that wallet knowing, and a later flush of its own would vouch for records that were gone. A wallet made after such a failure starts clean, as it tracks what it read from disk. Tests cover each: a settle parked between its reads and its recording, the manager's flush, and a removed wallet's flush against its successor. Co-Authored-By: Claude Fable 5.2 --- packages/rs-platform-wallet/README.md | 4 +- .../src/changeset/core_bridge.rs | 6 + .../src/changeset/flush_ledger.rs | 93 ++++++++++++ .../rs-platform-wallet/src/changeset/mod.rs | 2 + .../rs-platform-wallet/src/manager/mod.rs | 100 +++++++++++-- .../identity/network/contact_requests.rs | 104 ++++++++++--- .../network/private_contact_requests.rs | 138 ++++++++++++++++++ .../src/wallet/persister.rs | 124 ++++++++-------- .../src/wallet/platform_wallet.rs | 6 +- 9 files changed, 476 insertions(+), 101 deletions(-) create mode 100644 packages/rs-platform-wallet/src/changeset/flush_ledger.rs diff --git a/packages/rs-platform-wallet/README.md b/packages/rs-platform-wallet/README.md index e1a256fbfd7..169e7aaea6e 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -123,7 +123,7 @@ Startup errors distinguish damaged recovery metadata or undecodable signed bytes ## Private contact request payments after a restart -Before a private contact request's payment can reach the network, the shielded store saves a record of it in its private payment journal: the sender and recipient identities, the request's account reference, the commitment of the entry it appends, the SHA-256 of the whole entry, the position the sender's read of DashPay's private store rested at when it paid, and the nullifiers its spend consumes. The file store writes the journal, and the payment's redrive record, in one transaction on a connection at `synchronous=FULL` with `fullfsync` (on Apple platforms a plain sync does not flush the drive's own cache), so the record survives a crash or a power loss; a record that cannot be saved refuses the payment before anything is broadcast. The journal is kept apart from the spend's notes and redrive record: no note release, redrive removal, undecodable transition, account rebind or purge removes it. A record is removed only when its entry is read back from DashPay's private store and recorded durably, or when the transition provably never left the wallet (refused before anything was sent: no address to send it to, or the SDK's own refusal). An endpoint's answer, a consensus verdict included, proves nothing: no proof or quorum signature backs it, and an endpoint that kept or forwarded the signed transition can answer with any verdict. A record is also discharged once its payment is proven expired, judged against Platform's recorded anchors fetched before the shielded sync pass and proved at a height H: the spend's anchor, which the record keeps with the proved height of the set it was read from, was read at or below H and is absent from the set at H, the pass's note scan covers H, and its entry is absent from DashPay's private store read from the lowest position the records being judged kept through its end, with every page proved at or above H. An entry counts as the record's only when its whole bytes hash to the hash the record keeps: one that only starts with the commitment, which anyone who saw the commitment can append, does not hold the record. A record journaled before the hash and the position were kept matches by its commitment and is read for from the store's start. Whether its inputs are spent does not matter: the transition appends its entry in the block that spends them. A payment that never lands therefore frees its pair once its anchor ages out (Platform keeps an anchor for `shielded_anchor_retention_blocks`, 1000 blocks, and prunes every `shielded_anchor_pruning_interval`, 100 blocks). A restoration that finds a record whose request the sender's tracked request already records or supersedes, as a private sync read it back in a process that ended before discharging it, discharges it. Once a flush of the wallet's records failed for good in a process, the backend dropping what it buffered, no record is discharged on the strength of a tracked request for the rest of that process: no read-back is noted, a read of the entry again included (it finds the request tracked in memory and stores nothing), and a restoration leaves the records it finds settled. A restoration that finds a record whose request its sender does not track rewinds the sender's read to the position the record kept (the store's start for a record that kept none), so the entry is read again, its request recorded, and the record discharged once that is durable. Nothing else, a relay's refusal or a missing redrive record included, discharges a record. +Before a private contact request's payment can reach the network, the shielded store saves a record of it in its private payment journal: the sender and recipient identities, the request's account reference, the commitment of the entry it appends, the SHA-256 of the whole entry, the position the sender's read of DashPay's private store rested at when it paid, and the nullifiers its spend consumes. The file store writes the journal, and the payment's redrive record, in one transaction on a connection at `synchronous=FULL` with `fullfsync` (on Apple platforms a plain sync does not flush the drive's own cache), so the record survives a crash or a power loss; a record that cannot be saved refuses the payment before anything is broadcast. The journal is kept apart from the spend's notes and redrive record: no note release, redrive removal, undecodable transition, account rebind or purge removes it. A record is removed only when its entry is read back from DashPay's private store and recorded durably, or when the transition provably never left the wallet (refused before anything was sent: no address to send it to, or the SDK's own refusal). An endpoint's answer, a consensus verdict included, proves nothing: no proof or quorum signature backs it, and an endpoint that kept or forwarded the signed transition can answer with any verdict. A record is also discharged once its payment is proven expired, judged against Platform's recorded anchors fetched before the shielded sync pass and proved at a height H: the spend's anchor, which the record keeps with the proved height of the set it was read from, was read at or below H and is absent from the set at H, the pass's note scan covers H, and its entry is absent from DashPay's private store read from the lowest position the records being judged kept through its end, with every page proved at or above H. An entry counts as the record's only when its whole bytes hash to the hash the record keeps: one that only starts with the commitment, which anyone who saw the commitment can append, does not hold the record. A record journaled before the hash and the position were kept matches by its commitment and is read for from the store's start. Whether its inputs are spent does not matter: the transition appends its entry in the block that spends them. A payment that never lands therefore frees its pair once its anchor ages out (Platform keeps an anchor for `shielded_anchor_retention_blocks`, 1000 blocks, and prunes every `shielded_anchor_pruning_interval`, 100 blocks). A restoration that finds a record whose request the sender's tracked request already records or supersedes, as a private sync read it back in a process that ended before discharging it, discharges it. Once a flush of the records under the wallet's id failed for good, the backend dropping what it buffered, no record is discharged on the strength of a tracked request for as long as that wallet stays loaded. Every flush of the id counts, whoever made it: the wallet's own, one through a handle of a wallet removed under the same id since, or the manager's before it publishes a wallet. A wallet loaded or created under the id after the failure tracks what it read from disk and is not held by it. While the hold stands, no read-back is noted, a read of the entry again included (it finds the request tracked in memory and stores nothing), and a restoration leaves the records it finds settled. A restoration that finds a record whose request its sender does not track rewinds the sender's read to the position the record kept (the store's start for a record that kept none), so the entry is read again, its request recorded, and the record discharged once that is durable. Nothing else, a relay's refusal or a missing redrive record included, discharges a record. Until a payment's reservation is restored from the journal, nothing shows that the pair is taken: the request's entry is not in DashPay's store until the payment executes. So a wallet sends and accepts no contact request, public or private, while such a payment may be unrestored, and returns `PrivateContactPaymentsNotRestored` (FFI code 72) with nothing read, signed or broadcast. The in-kind accept is held the same way. These hold it: @@ -151,7 +151,7 @@ Every shielded spend that consumes notes (shielded transfers, unshields, withdra Every public `contactRequest` (a send, a public accept, the in-kind accept's public reply, a send from a QR code, an auto-accept and a rotation) is sent by one path, which signs the transition, keeps the signed bytes, and broadcasts them by the rule shielded spends use: only a refusal before anything was sent reports its error with nothing held. Once a dispatch may have reached an endpoint, the send waits for the result (not after an endpoint's verdict, which no proof backs), records the request when its document is confirmed, and otherwise reports the SDK's unconfirmed outcome (`StateTransitionResultUnconfirmed`, FFI code 20) and holds the pair as an unconfirmed public request. Before signing, the send reads the request it would make by DashPay's `ownerIdUserIdAndAccountRef` unique index (the sender, the recipient and the account reference); one found, sent by an earlier process that ended before recording it or by another device of the sender, is adopted as sent, with nothing signed. A read that fails sends on. -While an unconfirmed public request holds a pair, a public or private request to it, an accept included, is refused with `PublicContactRequestUnconfirmed` (FFI code 76), with nothing signed or paid, and the sender identity cannot be removed. `DashPayView::public_contact_request_unconfirmed` reads one pair's state, beside `DashPayView::private_pair_held`, so a host shows a public request awaiting its outcome apart from a private one pending. The public contact request sync settles it, as does each send or accept to the pair before it decides: `Sdk::state_transition_outcome` reads the transition's identity contract nonce, proved, and then the request is read by the unique index, proved. A request found is recorded as sent, as a confirmed send records it, and frees the pair; a send that finds it this way returns it as the request sent, with nothing signed, rather than building another past it. That holds whether or not the receiving account for the pair could be registered then: a registration that fails is logged and left to the next sync, and is never reported as a failed send. With none found, a nonce proved used at a height at or below the height of the read that missed the request shows the transition never wrote it and never will, and frees the pair; a new request may then be sent. Otherwise the transition may still execute, and the pair stays held. No retry signs a second transition. +While an unconfirmed public request holds a pair, a public or private request to it, an accept included, is refused with `PublicContactRequestUnconfirmed` (FFI code 76), with nothing signed or paid, and the sender identity cannot be removed. `DashPayView::public_contact_request_unconfirmed` reads one pair's state, beside `DashPayView::private_pair_held`, so a host shows a public request awaiting its outcome apart from a private one pending. The public contact request sync settles it, as does each send or accept to the pair before it decides: `Sdk::state_transition_outcome` reads the transition's identity contract nonce, proved, and then the request is read by the unique index, proved. A request found is recorded as sent, as a confirmed send records it, and frees the pair; a send that finds it this way returns it as the request sent, with nothing signed, rather than building another past it. That holds whether or not the receiving account for the pair could be registered then: a registration that fails is logged and left to the next sync, and is never reported as a failed send. A read that finds the request records it only while its own hold still stands: when another read of the same hold recorded it first, the later one records nothing, so a request sent since is never replaced by the older one. With none found, a nonce proved used at a height at or below the height of the read that missed the request shows the transition never wrote it and never will, and frees the pair; a new request may then be sent. Otherwise the transition may still execute, and the pair stays held. No retry signs a second transition. While it may still execute, the same signed bytes are broadcast again only where a public request to the recipient would still go out, by what the wallet tracks. The public sync does it, and so does a public send whose read of the private store succeeded; a private send never does, as it is about to take the pair privately. The check and the dispatch run under the wallet generation's lifecycle gate, so nothing goes out for a wallet that is being removed. Nothing is broadcast while the wallet's private payments may be unrestored, while the sender's read of the private store is rewound (keys that changed, a sender unignored) and has not reached the store's end again, or once a private request holds the pair or their requests came or went privately: the request then stays held until a proved read settles it. This is decided from what the wallet has read, not from the store: a private request the store holds that no private sync has read in yet does not stop the broadcast, as one sealed after a send's own store read does not stop that send. diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index a0594b36402..f6a4a019175 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -64,6 +64,7 @@ use crate::changeset::changeset::{ fold_same_txid_records, merge_payment_overlays, AssetLockChangeSet, CoreChangeSet, HighestUsedIndexes, PaymentOverlay, PlatformWalletChangeSet, SweepBatch, UtxoCreditVerdict, }; +use crate::changeset::flush_ledger::FlushLedgers; use crate::changeset::merge::Merge; use crate::changeset::persistence_capabilities::PersistenceCapabilities; use crate::changeset::traits::PlatformWalletPersistence; @@ -321,6 +322,11 @@ pub struct DurableCursorState { /// wallet left (see [`RewindBarrier::retire`]). A leaf lock: taken under /// the manager lock on both sides, never held across an await. pub retired: std::sync::Mutex>, + /// Per wallet id, what is known of the flushes of its records: like a + /// cursor entry it outlives its wallet, so a removed wallet's handles, + /// its same-id successor's and the manager count every flush of the id + /// in one place ([`FlushLedger`](crate::changeset::flush_ledger::FlushLedger)). + pub flush_ledgers: FlushLedgers, } impl DurableCursorState { diff --git a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs new file mode 100644 index 00000000000..a50f1823331 --- /dev/null +++ b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs @@ -0,0 +1,93 @@ +//! What this process knows of the flushes of each wallet's records. + +use std::collections::BTreeMap; +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex, PoisonError}; + +use crate::changeset::traits::{PersistenceError, PlatformWalletPersistence}; +use crate::wallet::platform_wallet::WalletId; + +/// What this process knows of the flushes of one wallet's records. +/// +/// A backend buffers a wallet's records under its id, whoever stored them, +/// and a flush that fails for good drops that buffer, whoever flushed. So +/// what a flush did to the buffer is known only where every flush of the id +/// is counted: one ledger per wallet id, shared by the manager and by every +/// handle of a wallet under that id, a removed wallet's handles and its +/// same-id successor's alike ([`FlushLedgers`]). Every flush of the wallet's +/// records goes through [`Self::flush`]. +#[derive(Debug, Default)] +pub struct FlushLedger { + /// How many flushes are in progress: each is counted from before it calls + /// the backend until its outcome is recorded in `lost`, so while none is, + /// `lost` tells of every flush that failed for good. + in_flight: AtomicUsize, + /// How many flushes failed for good, the backend dropping what it + /// buffered. + lost: AtomicU64, +} + +/// One flush in progress, counted in its [`FlushLedger`] until dropped. +struct InFlight<'a>(&'a AtomicUsize); + +impl<'a> InFlight<'a> { + fn enter(in_flight: &'a AtomicUsize) -> Self { + in_flight.fetch_add(1, Ordering::SeqCst); + Self(in_flight) + } +} + +impl Drop for InFlight<'_> { + fn drop(&mut self) { + self.0.fetch_sub(1, Ordering::SeqCst); + } +} + +impl FlushLedger { + /// Flush `wallet_id`'s records on `backend`: in progress from before the + /// backend is called until its outcome is recorded, and counted as lost + /// when it fails for good (anything but a transient failure, which keeps + /// the buffer). + pub(crate) fn flush( + &self, + backend: &dyn PlatformWalletPersistence, + wallet_id: WalletId, + ) -> Result<(), PersistenceError> { + let _in_flight = InFlight::enter(&self.in_flight); + let flushed = backend.flush(wallet_id); + if flushed.as_ref().is_err_and(|error| !error.is_transient()) { + self.lost.fetch_add(1, Ordering::SeqCst); + } + flushed + } + + /// How many flushes are in progress. + #[cfg(feature = "shielded")] + pub(crate) fn in_flight(&self) -> usize { + self.in_flight.load(Ordering::SeqCst) + } + + /// How many flushes failed for good so far. + #[cfg(feature = "shielded")] + pub(crate) fn lost(&self) -> u64 { + self.lost.load(Ordering::SeqCst) + } +} + +/// The [`FlushLedger`] of each wallet id, for the wallets of one manager. An +/// entry outlives its wallet, as the backend's buffer under the id does. +#[derive(Debug, Default)] +pub struct FlushLedgers(Mutex>>); + +impl FlushLedgers { + /// The ledger of `wallet_id`'s records, made on first use. + pub fn of(&self, wallet_id: WalletId) -> Arc { + Arc::clone( + self.0 + .lock() + .unwrap_or_else(PoisonError::into_inner) + .entry(wallet_id) + .or_default(), + ) + } +} diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 266d2d6e2c8..8cd85d343e6 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -14,6 +14,7 @@ pub mod client_start_state; pub mod client_wallet_start_state; pub mod core_bridge; pub mod dashpay_backfill; +pub mod flush_ledger; pub mod identity_manager_start_state; pub mod identity_scan_state; pub mod merge; @@ -54,6 +55,7 @@ pub use core_bridge::{ pub use dashpay_backfill::{ persists_backfill_coverage, DashPayBackfillCoveredContact, DashPayBackfillRecord, }; +pub use flush_ledger::{FlushLedger, FlushLedgers}; pub use identity_manager_start_state::IdentityManagerStartState; pub use identity_scan_state::IdentityScanStateEntry; pub use merge::Merge; diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index b074deb07ed..76158801df6 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -837,15 +837,23 @@ impl PlatformWalletManager

{ PlatformWalletError::from_store_failure(&*self.persister, e) })?; if !inline { - self.persister.flush(wallet_id).map_err(|e| { - tracing::error!( - wallet_id = %hex::encode(wallet_id), - error = %e, - "failed to commit the replaced DashPay backfill record; \ - the wallet was not published" - ); - PlatformWalletError::from_flush_failure(e) - })?; + // Through the wallet id's flush ledger, as every flush of its + // records is: one that fails for good here drops what a handle of + // an earlier wallet under the id still had buffered, and that + // handle learns of it from the ledger. + self.durable_cursors + .flush_ledgers + .of(wallet_id) + .flush(&*self.persister, wallet_id) + .map_err(|e| { + tracing::error!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "failed to commit the replaced DashPay backfill record; \ + the wallet was not published" + ); + PlatformWalletError::from_flush_failure(e) + })?; } if let Some(cursor) = cursor { durable_cursors.insert(wallet_id, DurableCursor::at(cursor)); @@ -1554,6 +1562,80 @@ mod tests { impl EventHandler for NoopEventHandler {} impl PlatformEventHandler for NoopEventHandler {} + /// A buffering backend whose flush fails for good, dropping its buffer. + #[cfg(feature = "shielded")] + struct FatalFlushPersister; + + #[cfg(feature = "shielded")] + impl PlatformWalletPersistence for FatalFlushPersister { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Err(PersistenceError::backend("the database is corrupt")) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + /// The flush the manager makes itself, of the backfill record it replaces + /// before a wallet is published, is counted where the handles of that + /// wallet id count theirs. One that fails for good dropped what a handle + /// of an earlier wallet under the id still had buffered, and that handle + /// then vouches for nothing it stored: a flush of its own would succeed + /// over an empty buffer. + #[cfg(feature = "shielded")] + #[tokio::test] + async fn should_count_its_backfill_flush_in_the_wallet_ids_flush_ledger() { + use crate::wallet::persister::{NotDurable, WalletPersister}; + + let persister = Arc::new(FatalFlushPersister); + let mgr = PlatformWalletManager::new( + Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")), + Arc::clone(&persister), + Arc::new(NoopEventHandler) as Arc, + ); + let wallet_id: WalletId = [7; 32]; + // As `PlatformWallet::new` makes the handle of a wallet under the id. + let earlier = WalletPersister::with_flush_ledger( + wallet_id, + Arc::clone(&persister) as Arc, + mgr.durable_cursors.flush_ledgers.of(wallet_id), + ); + assert!(!earlier.buffered_writes_lost()); + + // Coverage the wallet about to be published does not hold was + // dropped, so the host's record is replaced and flushed. + let mut cursors = mgr.durable_cursors.lock().await; + let settled = mgr.settle_published_backfill( + &mut cursors, + wallet_id, + DashPayBackfillRecord::default(), + true, + 5, + 5, + ); + assert!(settled.is_err(), "the flush failed for good"); + + assert!( + earlier.buffered_writes_lost(), + "the earlier wallet's handle learns of the flush that dropped the buffer" + ); + assert!(matches!(earlier.flush_durably(), Err(NotDurable::Lost))); + // A handle made after it starts from what is on disk. + let later = WalletPersister::with_flush_ledger( + wallet_id, + Arc::clone(&persister) as Arc, + mgr.durable_cursors.flush_ledgers.of(wallet_id), + ); + assert!(!later.buffered_writes_lost()); + } + fn make_manager() -> Arc> { let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); Arc::new(PlatformWalletManager::new( diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs index a4c3ed2d0c7..dcff5517527 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs @@ -700,6 +700,19 @@ fn release_public_claim( } } +/// Track `request` as sent by `managed`, stored through `persister`. +fn track_sent_contact_request( + managed: &mut ManagedIdentity, + request: &ContactRequest, + persister: &WalletPersister, +) -> Result<(), PlatformWalletError> { + managed + .add_sent_contact_request(request.clone(), persister) + .map_err(|e| { + PlatformWalletError::Persistence(format!("sent contact request not persisted: {e}")) + }) +} + /// Why a public request from `sender` to `recipient` may not go out, from /// what the wallet tracks of the pair's private requests: one to the /// recipient is reserved, or a private payment the shielded store's journal @@ -1089,7 +1102,6 @@ impl DashPayView<'_, B> { sender_identity_id, recipient_identity_id, request, - None, account_index, contact_xpub_ext, ) @@ -1270,7 +1282,6 @@ impl DashPayView<'_, B> { sender_identity_id, recipient_identity_id, contact_request, - None, account_index, contact_xpub_ext, ) @@ -1279,9 +1290,7 @@ impl DashPayView<'_, B> { /// Record `request` as sent from `sender` to `recipient` and register our /// receiving (friendship) account for the pair from `contact_xpub`, as a - /// send that saw its document confirmed does. `settles` is the attempt of - /// the unconfirmed public request to the recipient this request settles, - /// released in the same lock it is recorded in. Only on this handle's + /// send that saw its document confirmed does. Only on this handle's /// wallet generation: the caller holds the generation's lifecycle gate. /// /// Once the request is recorded it is the request sent, whatever becomes @@ -1294,28 +1303,80 @@ impl DashPayView<'_, B> { sender: &Identifier, recipient: &Identifier, request: ContactRequest, - settles: Option, account_index: u32, contact_xpub: ExtendedPubKey, ) -> Result { { let mut wm = self.wallet_manager.write().await; - let managed = generation_info_mut(&mut wm, &self.wallet_id, &self.generation) - .ok_or_else(|| PlatformWalletError::WalletNotFound(hex::encode(self.wallet_id)))? + let managed = self + .current_wallet_info_mut(&mut wm)? .identity_manager .managed_identity_mut(sender) .ok_or(PlatformWalletError::IdentityNotFound(*sender))?; - managed - .add_sent_contact_request(request.clone(), &self.persister) - .map_err(|e| { - PlatformWalletError::Persistence(format!( - "sent contact request not persisted: {e}" - )) - })?; - if let Some(attempt) = settles { - managed.settle_unconfirmed_public_request(recipient, attempt); + track_sent_contact_request(managed, &request, &self.persister)?; + } + self.register_sent_request_account(sender, recipient, account_index, contact_xpub) + .await; + Ok(request) + } + + /// Record the request a settle found on Platform as sent + /// ([`Self::record_sent_contact_request`]) and release the hold it + /// settles, `attempt`'s, in one lock, and only while that attempt still + /// holds the pair there. `account_index` and `contact_xpub` are the + /// hold's. + /// + /// The settle read Platform with no lock held, so another settle of the + /// same hold may have recorded the request and released the hold by now, + /// and a request sent since may be the one tracked. Recording the older + /// one over it would have the next rotation build from a reference + /// already used. So a settle whose hold is gone records nothing: it + /// answers the request when that is the one tracked, as the other settle + /// recorded it, and `None` otherwise. + pub(super) async fn record_settled_public_request( + &self, + sender: &Identifier, + recipient: &Identifier, + request: ContactRequest, + attempt: u64, + account_index: u32, + contact_xpub: ExtendedPubKey, + ) -> Result, PlatformWalletError> { + { + let mut wm = self.wallet_manager.write().await; + let managed = self + .current_wallet_info_mut(&mut wm)? + .identity_manager + .managed_identity_mut(sender) + .ok_or(PlatformWalletError::IdentityNotFound(*sender))?; + let dashpay = managed.dashpay(); + if !dashpay + .unconfirmed_public_request(recipient) + .is_some_and(|held| held.attempt == attempt) + { + let tracked = dashpay + .outgoing_request(recipient) + .is_some_and(|tracked| tracked.account_reference == request.account_reference); + return Ok(tracked.then_some(request)); } + track_sent_contact_request(managed, &request, &self.persister)?; + managed.settle_unconfirmed_public_request(recipient, attempt); } + self.register_sent_request_account(sender, recipient, account_index, contact_xpub) + .await; + Ok(Some(request)) + } + + /// Register our receiving (friendship) account for a request recorded as + /// sent. A failure is logged and left to the next sync, which registers + /// the account ([`Self::record_sent_contact_request`]). + async fn register_sent_request_account( + &self, + sender: &Identifier, + recipient: &Identifier, + account_index: u32, + contact_xpub: ExtendedPubKey, + ) { if let Err(error) = self .register_contact_account(sender, recipient, account_index, contact_xpub) .await @@ -1328,7 +1389,6 @@ impl DashPayView<'_, B> { the next sync registers it" ); } - Ok(request) } /// Hold the pair `sender`, `recipient` for the public request @@ -1513,17 +1573,17 @@ impl DashPayView<'_, B> { return Ok(None); } match found { - Some(request) => self - .record_sent_contact_request( + Some(request) => { + self.record_settled_public_request( sender, recipient, request, - Some(held.attempt), + held.attempt, held.account_index, held.contact_xpub, ) .await - .map(Some), + } None => { let mut wm = self.wallet_manager.write().await; if let Some(managed) = diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs index e7467ef9475..a958de665f2 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs @@ -2742,6 +2742,8 @@ mod tests { use crate::wallet::identity::network::contacts::RegisterExternalError; use crate::wallet::identity::network::identity_auth_derivation_path_for_type; use crate::wallet::identity::network::SeedCryptoProvider; + #[cfg(feature = "shielded")] + use crate::wallet::persister::NotDurable; use crate::wallet::platform_wallet::{PlatformWallet, WalletId}; #[cfg(feature = "shielded")] use crate::wallet::shielded::coordinator::{ @@ -6756,6 +6758,142 @@ mod tests { .await; } + /// A settle that found its request on Platform records it only while its + /// own hold still stands. One whose reads came back after another settle + /// of the hold recorded the request and released it records nothing: it + /// answers that request while it is the one tracked, and nothing once a + /// request sent since is, which the older one is not recorded over. + #[tokio::test] + async fn should_record_nothing_from_a_settle_whose_hold_was_settled_meanwhile() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let alice_signer = signer(ALICE_PHRASE, &alice); + + for sent_since in [true, false] { + let (_manager, alice_wallet, _transition, reference) = + unconfirmed_public_request(&alice, &bob, &alice_signer, &alice_crypto).await; + expect_sent_request( + &alice_wallet, + &alice, + &bob, + reference, + Some(sent_request_document(&alice, &bob, reference)), + ) + .await; + let view = alice_wallet.identity().dashpay(); + + // Its reads done, the settle waits at the lifecycle gate, here + // held as a removal holds it: what follows happens between its + // reads and its recording. + let gate = alice_wallet.generation().teardown_guard().await; + let (alice_id, bob_id) = (alice.id(), bob.id()); + let mut settling = + Box::pin(view.settle_unconfirmed_public_request(&alice_id, &bob_id, true)); + assert!( + tokio::time::timeout(Duration::from_millis(300), &mut settling) + .await + .is_err(), + "the settle waits at the gate" + ); + { + let iw = alice_wallet.identity(); + let mut wm = iw.wallet_manager.write().await; + let managed = wm + .get_wallet_info_mut(&alice_wallet.wallet_id()) + .expect("wallet info") + .identity_manager + .managed_identity_mut(&alice_id) + .expect("the sender"); + let attempt = managed + .dashpay() + .unconfirmed_public_request(&bob_id) + .expect("held") + .attempt; + // As another settle of the hold leaves it. + managed + .add_sent_contact_request( + public_request(&alice, &bob, reference), + &iw.persister, + ) + .expect("recorded"); + managed.settle_unconfirmed_public_request(&bob_id, attempt); + if sent_since { + // As a send since leaves it: a rotation past that request. + managed + .add_sent_contact_request( + public_request(&alice, &bob, reference + 1), + &iw.persister, + ) + .expect("recorded"); + } + } + drop(gate); + + let settled = settling.await.expect("the reads"); + let tracked = if sent_since { reference + 1 } else { reference }; + assert_eq!( + settled.map(|request| request.account_reference), + (!sent_since).then_some(reference), + "sent since: {sent_since}" + ); + managed(&alice_wallet, ALICE, |managed| { + assert_eq!( + managed + .dashpay() + .outgoing_request(&bob_id) + .map(|request| request.account_reference), + Some(tracked), + "sent since: {sent_since}" + ); + }) + .await; + } + } + + /// Every flush of the records under a wallet id is counted in one place, + /// whichever handle makes it: one through the handle of a wallet removed + /// since, failing for good, drops what its same-id successor had + /// buffered, and the successor then vouches for nothing it stored. A + /// wallet created under the id after that starts from what is on disk. + #[cfg(feature = "shielded")] + #[tokio::test] + async fn should_count_a_removed_wallets_flush_against_its_successor() { + let alice = identity(ALICE_PHRASE, ALICE); + let persistence = RecordingPersistence::attesting( + PersistenceCapabilities::PRIVATE_CONTACT_SCAN_STATE_RESTART, + ); + let manager = manager_on(Arc::clone(&persistence)).await; + let first = create_holding(&manager, ALICE_PHRASE, &alice).await; + let wallet_id = first.wallet_id(); + manager.remove_wallet(&wallet_id).await.expect("removed"); + let successor = create_holding(&manager, ALICE_PHRASE, &alice).await; + assert_eq!(successor.wallet_id(), wallet_id); + assert!(!successor.persister().buffered_writes_lost()); + + persistence + .flush_fails_fatally + .store(true, Ordering::SeqCst); + assert!(first.flush_persist().is_err()); + persistence + .flush_fails_fatally + .store(false, Ordering::SeqCst); + + assert!( + successor.persister().buffered_writes_lost(), + "the flush dropped the one buffer the id's records share" + ); + assert!(matches!( + successor.persister().flush_durably(), + Err(NotDurable::Lost) + )); + + manager.remove_wallet(&wallet_id).await.expect("removed"); + let later = create_holding(&manager, ALICE_PHRASE, &alice).await; + assert!(!later.persister().buffered_writes_lost()); + assert!(later.persister().flush_durably().is_ok()); + } + /// A handle taken before its wallet was removed and created again under /// its id registers no contact account on the replacement, whose own /// handle does. diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index ec319942da3..de797e603a7 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -5,8 +5,6 @@ //! a fixed `wallet_id` so callers don't need to pass the ID on every call. use std::collections::{BTreeMap, BTreeSet}; -#[cfg(feature = "shielded")] -use std::sync::atomic::AtomicUsize; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::Arc; @@ -14,7 +12,7 @@ use dashcore::Txid; use key_wallet::managed_account::transaction_record::TransactionRecord; use crate::changeset::{ - ClientStartState, DpnsNameStateEntry, PersistenceCapabilities, PersistenceError, + ClientStartState, DpnsNameStateEntry, FlushLedger, PersistenceCapabilities, PersistenceError, PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::error::PlatformWalletError; @@ -100,21 +98,21 @@ pub struct WalletPersister { /// last store or its flush failed. Shared by the clones the wallet and /// the coordinator hold. In memory only. stored_private_markers: Arc>>, - /// Whether a flush of this wallet's records failed for good in this - /// process, the backend dropping what it buffered: what the wallet tracks - /// in memory since may then not be on disk, so nothing is discharged on - /// the strength of it alone ([`Self::buffered_writes_lost`]). Shared by - /// the clones the wallet and the coordinator hold. In memory only: the - /// next process tracks what its load read from disk. - #[cfg(feature = "shielded")] - buffered_writes_lost: Arc, - /// How many flushes of this wallet's records are in progress: each is - /// counted from before it calls the backend until its outcome is - /// recorded in `buffered_writes_lost`, so while none is, that flag tells - /// of every flush that failed for good ([`Self::flush_durably`]). Shared - /// by the clones the wallet and the coordinator hold. + /// What is known of the flushes of the records stored under this + /// wallet's id: how many are in progress, and how many failed for good, + /// the backend dropping what it buffered. Every flush of the id is + /// counted in it, this handle's, those of the handles of a wallet removed + /// or created under the id since, and the manager's own, as each acts on + /// the one buffer ([`FlushLedger`]). In memory only. + flush_ledger: Arc, + /// How many flushes the ledger had counted as failed for good when this + /// handle was made. One counted since may have dropped what this wallet + /// stored: what it tracks in memory may then not be on disk, so nothing + /// is discharged on the strength of it alone + /// ([`Self::buffered_writes_lost`]). One counted before dropped nothing + /// of this wallet's, which tracks what its load read from disk. #[cfg(feature = "shielded")] - flushes_in_flight: Arc, + flush_losses_at_start: u64, /// The largest size of DashPay's private contact request store a page /// this wallet read proved, in this process /// ([`Self::private_store_size_proved`]). In memory only. @@ -173,8 +171,9 @@ struct DischargedPrivateEntries { #[cfg(feature = "shielded")] #[derive(Debug)] pub(crate) enum NotDurable { - /// A flush failed for good in this process, the backend dropping what it - /// buffered ([`WalletPersister::buffered_writes_lost`]). + /// A flush of the wallet id's records failed for good since the handle + /// was made, the backend dropping what it buffered + /// ([`WalletPersister::buffered_writes_lost`]). Lost, /// The flush this call made failed. Flush(PersistenceError), @@ -182,28 +181,21 @@ pub(crate) enum NotDurable { OtherFlushInFlight, } -/// One flush in progress, counted in [`WalletPersister`]'s -/// `flushes_in_flight` until dropped. -#[cfg(feature = "shielded")] -struct FlushInFlight<'a>(&'a AtomicUsize); - -#[cfg(feature = "shielded")] -impl<'a> FlushInFlight<'a> { - fn enter(in_flight: &'a AtomicUsize) -> Self { - in_flight.fetch_add(1, Ordering::SeqCst); - Self(in_flight) - } -} - -#[cfg(feature = "shielded")] -impl Drop for FlushInFlight<'_> { - fn drop(&mut self) { - self.0.fetch_sub(1, Ordering::SeqCst); - } -} - impl WalletPersister { + /// A handle whose flushes no other handle or manager shares: for a + /// backend this handle alone flushes `wallet_id`'s records on. pub fn new(wallet_id: WalletId, inner: Arc) -> Self { + Self::with_flush_ledger(wallet_id, inner, Arc::default()) + } + + /// A handle that counts its flushes in `flush_ledger`, the one every + /// flush of `wallet_id`'s records on `inner` is counted in + /// ([`FlushLedgers`](crate::changeset::FlushLedgers)). + pub fn with_flush_ledger( + wallet_id: WalletId, + inner: Arc, + flush_ledger: Arc, + ) -> Self { Self { wallet_id, inner, @@ -216,9 +208,8 @@ impl WalletPersister { private_payments_unrestored: Arc::default(), stored_private_markers: Arc::default(), #[cfg(feature = "shielded")] - buffered_writes_lost: Arc::default(), - #[cfg(feature = "shielded")] - flushes_in_flight: Arc::default(), + flush_losses_at_start: flush_ledger.lost(), + flush_ledger, private_store_size_proved: Arc::default(), private_payment_restore_failure: Arc::default(), #[cfg(feature = "shielded")] @@ -326,11 +317,11 @@ impl WalletPersister { /// persister's locks held, so a call during it, from another thread or /// the flush itself, neither waits nor loses what it adds. /// - /// None is noted once a flush of the wallet's records failed for good in - /// this process ([`Self::buffered_writes_lost`]): a request read back - /// since may be one the wallet tracked before the loss, which a read of - /// its entry again finds tracked and stores nothing for, so its record - /// may be in memory only. Those journal records stay for the next + /// None is noted once a flush of the wallet id's records failed for good + /// since this handle was made ([`Self::buffered_writes_lost`]): a request + /// read back since may be one the wallet tracked before the loss, which a + /// read of its entry again finds tracked and stores nothing for, so its + /// record may be in memory only. Those journal records stay for the next /// process, whose restoration reads their entries again from what is on /// disk. #[cfg(feature = "shielded")] @@ -607,23 +598,17 @@ impl WalletPersister { PlatformWalletError::from_store_failure(self.inner.as_ref(), error) } + /// Flush this wallet's records, counted in the ledger of its id + /// ([`FlushLedger::flush`]). pub(crate) fn flush(&self) -> Result<(), PersistenceError> { - // In progress from before the backend is called until its outcome is - // recorded below (`Self::flush_durably`). - #[cfg(feature = "shielded")] - let _in_flight = FlushInFlight::enter(&self.flushes_in_flight); - let flushed = self.inner.flush(self.wallet_id); - #[cfg(feature = "shielded")] - if flushed.as_ref().is_err_and(|error| !error.is_transient()) { - self.buffered_writes_lost.store(true, Ordering::SeqCst); - } - flushed + self.flush_ledger.flush(self.inner.as_ref(), self.wallet_id) } /// Make every record stored for this wallet before the call durable, and /// answer `Ok` only once it is: at once for a backend that commits /// inline, after a flush that succeeds otherwise, and never once a flush - /// failed for good in this process ([`Self::buffered_writes_lost`]). + /// of the wallet id's records failed for good since this handle was made + /// ([`Self::buffered_writes_lost`]). /// /// A flush that succeeds proves nothing by itself: one that failed for /// good just before it left nothing buffered, so it succeeds over records @@ -632,7 +617,10 @@ impl WalletPersister { /// of flushes in progress. The flush here is therefore trusted only when, /// after it, no flush is in progress and none has failed for good, read /// in that order: every flush that began before then has recorded its - /// outcome. One that fails later drops only what was buffered after this + /// outcome. Both are read from the ledger of the wallet's id, which + /// counts every flush of its records, whoever made it: this handle, a + /// handle of a wallet removed or created under the id since, or the + /// manager. One that fails later drops only what was buffered after this /// one wrote, so what this call answers for stays durable, and what the /// caller does on the strength of it needs no lock held with the flush. /// @@ -646,7 +634,7 @@ impl WalletPersister { } if !self.store_commits_inline() { self.flush().map_err(NotDurable::Flush)?; - if self.flushes_in_flight.load(Ordering::SeqCst) != 0 { + if self.flush_ledger.in_flight() != 0 { return Err(NotDurable::OtherFlushInFlight); } } @@ -656,16 +644,18 @@ impl WalletPersister { Ok(()) } - /// Whether a flush of this wallet's records failed for good in this - /// process (anything but a transient failure, which keeps the buffer): - /// what the wallet tracked before it may not be on disk, though it is in - /// memory. For the rest of the process no journal record is discharged on - /// the strength of a tracked request: no read-back is noted + /// Whether a flush of the records under this wallet's id failed for good + /// since this handle was made (anything but a transient failure, which + /// keeps the buffer), whoever made the flush: what the wallet tracked + /// before it may not be on disk, though it is in memory. For the rest of + /// the handle's life no journal record is discharged on the strength of a + /// tracked request: no read-back is noted /// ([`Self::note_read_back_once_durable`]), and a restoration leaves the - /// records it finds settled for the next process. + /// records it finds settled for a later load, which tracks what it reads + /// from disk. #[cfg(feature = "shielded")] pub(crate) fn buffered_writes_lost(&self) -> bool { - self.buffered_writes_lost.load(Ordering::SeqCst) + self.flush_ledger.lost() != self.flush_losses_at_start } /// Note that a page this wallet read proved DashPay's private contact diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index eaa22e1af34..f0a6016c6a3 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -756,7 +756,11 @@ impl PlatformWallet { // Build the per-wallet persister handle once and share it with // the sub-wallets that need to queue their own changesets // (currently just `AssetLockManager` — see Item 8 sub-step 1a). - let wallet_persister = WalletPersister::new(wallet_id, persister); + let wallet_persister = WalletPersister::with_flush_ledger( + wallet_id, + persister, + durable_cursors.flush_ledgers.of(wallet_id), + ); let core = CoreWallet::new( Arc::clone(&sdk), From a7157ffb13781525d07cfce6d418b953d623f994 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 13 Nov 2026 23:59:15 +0700 Subject: [PATCH 04/14] fix(platform-wallet): pay for a private request only once its marker is known to be on disk Before a private contact request is paid for, the wallet stores a marker that the identity holds a payment that may be unresolved, and flushes it on a backend that does not commit a store inline. It trusted that flush as soon as it succeeded. A flush that succeeds proves nothing by itself: another flush of the wallet's records may take what was buffered and fail for good, dropping it, and the marker's own flush then succeeds over a buffer that no longer holds it. `WalletPersister::store_durably` stores a changeset and answers for it only when, after its flush, no other flush of the wallet id's records is in progress and none failed for good since before the store, read from the ledger that counts every flush of the id. The marker is stored through it, so a marker that may have been dropped refuses the payment with nothing paid. Unlike `flush_durably` it answers for the one changeset: a flush that failed for good before the store does not refuse it. `NotDurable::Flush` becomes `NotDurable::Backend`, as it now carries a failed store too. Co-Authored-By: Claude Fable 5.2 --- packages/rs-platform-wallet/README.md | 4 +- .../src/changeset/flush_ledger.rs | 2 - .../network/private_contact_requests.rs | 87 +++++-- .../src/wallet/persister.rs | 224 ++++++++++++++++-- 4 files changed, 282 insertions(+), 35 deletions(-) diff --git a/packages/rs-platform-wallet/README.md b/packages/rs-platform-wallet/README.md index 169e7aaea6e..5ba091dd1f3 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -130,10 +130,10 @@ Until a payment's reservation is restored from the journal, nothing shows that t - In a build with the `shielded` feature, a wallet published, registered or loaded, while no shielded store is configured is held when its persister attests the private scan state: only such a persister lets a private payment be paid, so only its wallet can have journal records that no marker shows, as after the wallet was removed and registered again from the same seed, in this process or a new one. A wallet whose persister does not attest is never held for want of a store. With a store configured, the journal is read as the wallet is published. - In a build with the `shielded` feature, `load_from_persistor` also holds a wallet that bound a shielded account before (its persisted viewing key shows it). A wallet with no persisted viewing key never bound one, since a bind persists the key before installing anything and refuses when it cannot. - In a build with the `shielded` feature, a wallet stays held while its last restoration failed, or waits for a busy store. -- In every build, an identity whose private scan state marks a private payment that may be unresolved (`PrivateContactRequestsPosition::private_payments_unresolved`) is held. The wallet stores the marker durably before such a payment may reach the network, flushing a backend that does not commit a store inline, and stores it cleared once the identity holds no private request it is paying for or has not read back. A loaded hold is kept even when the backend no longer attests the scan state. +- In every build, an identity whose private scan state marks a private payment that may be unresolved (`PrivateContactRequestsPosition::private_payments_unresolved`) is held. The wallet stores the marker durably before such a payment may reach the network, flushing a backend that does not commit a store inline. That flush is trusted only when, after it, no other flush of the wallet's records is in progress and none failed for good since before the marker was stored: one that did may have dropped the marker, so the payment is refused with nothing paid. The wallet stores the marker cleared once the identity holds no private request it is paying for or has not read back. A loaded hold is kept even when the backend no longer attests the scan state. - In every build, an identity added to a wallet registered or loaded under an id whose record the backend kept marked after removing the wallet (`ClientStartState::retained_private_contact_scan_states`) is held the same way, before it can send or accept, whether it is added from Platform or by discovery. Only the marker is taken: the record's read position covers requests that went with the wallet's other rows, so the store is read for the identity from its start. A backend declaring `PRIVATE_CONTACT_REQUEST_SCAN_STATE` keeps a marked record through the wallet's removal and any purge of its rows, until a record stored cleared for that wallet and owner replaces it; the wallet never asks for one to be deleted, as `remove_wallet` stores nothing and `remove_identity` refuses while the identity's record is marked. -A private contact request is therefore paid for only over a backend attesting `PRIVATE_CONTACT_REQUEST_SCAN_STATE` (bit 15) together with `ATOMIC_CHANGESETS` (bit 0), the two `PersistenceCapabilities::PRIVATE_CONTACT_SCAN_STATE_RESTART` names, as the scan state is written in changesets that must commit whole: any other refuses the private send and the private accept with `PrivateContactRecoveryUnsupported` (FFI code 73) before anything is sealed or paid, and a marker that cannot be stored or flushed refuses the payment too. A payment that may have reached the network keeps its pair held and its marker set whatever its spend's outcome, until its record is removed as above. A pair held so refuses another private payment (`PrivateContactRequestPending`, FFI code 71) and holds public sends and accepts. The wallet holds it, beyond the sender identity, for as long as the journal keeps the payment's record: removing the identity and loading it again does not release it. +A private contact request is therefore paid for only over a backend attesting `PRIVATE_CONTACT_REQUEST_SCAN_STATE` (bit 15) together with `ATOMIC_CHANGESETS` (bit 0), the two `PersistenceCapabilities::PRIVATE_CONTACT_SCAN_STATE_RESTART` names, as the scan state is written in changesets that must commit whole: any other refuses the private send and the private accept with `PrivateContactRecoveryUnsupported` (FFI code 73) before anything is sealed or paid, and a marker that is not known to be stored durably refuses the payment too. A payment that may have reached the network keeps its pair held and its marker set whatever its spend's outcome, until its record is removed as above. A pair held so refuses another private payment (`PrivateContactRequestPending`, FFI code 71) and holds public sends and accepts. The wallet holds it, beyond the sender identity, for as long as the journal keeps the payment's record: removing the identity and loading it again does not release it. The first `PlatformWalletManager::configure_shielded` restores from the journal, with no keys, the payments of every held wallet and of every wallet the journal may keep payments of; a wallet registered or loaded after it is restored as it is published. A repeat call, as a host makes on every bind, never waits for a store that a scan holds: it restores only the wallets still held, each deferred to a task while the store is busy. Binding a wallet's shielded account restores its own, deferring the same way, the wallet held until it succeeds. A contact request refused as not restored first runs one more restoration, which does not wait on the store. A private sync does not restore. A host whose persister attests the private scan state therefore configures the shielded store when it activates the manager, before loading or creating wallets: until then every such wallet's contact requests are refused with 72, and a configure that fails keeps them refused until one succeeds. A host that opens no shielded store this session keeps those wallets' and identities' contact requests refused, and a build without the `shielded` feature keeps the marked identities' refused: the private payment may still execute, and a public request would publish the pair the private one kept off chain. `DashPayView::private_contact_readiness` (FFI `platform_wallet_get_private_contact_readiness`) reads whether the payments are restored, why the last restoration failed if it did, whether the private store was read to its end, and whether a payment may be unresolved (the marker, or a journal record the wallet holds the pair for), so a host shows the wallet's state rather than inferring it from refusals. `DashPayView::private_pair_held` reads one pair's hold as a send decides it. diff --git a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs index a50f1823331..c6ee029a97e 100644 --- a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs +++ b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs @@ -62,13 +62,11 @@ impl FlushLedger { } /// How many flushes are in progress. - #[cfg(feature = "shielded")] pub(crate) fn in_flight(&self) -> usize { self.in_flight.load(Ordering::SeqCst) } /// How many flushes failed for good so far. - #[cfg(feature = "shielded")] pub(crate) fn lost(&self) -> u64 { self.lost.load(Ordering::SeqCst) } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs index a958de665f2..8b2de7985ca 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs @@ -71,7 +71,7 @@ use super::contact_requests::{ use super::signing_key::first_available; use super::{dashpay_contract, DashPayView, IdentityWallet}; use crate::broadcaster::TransactionBroadcaster; -use crate::changeset::{ContactChangeSet, PersistenceError, PrivateContactRequestsPosition}; +use crate::changeset::{ContactChangeSet, PrivateContactRequestsPosition}; use crate::error::PlatformWalletError; use crate::wallet::core::WalletGeneration; use crate::wallet::identity::types::dashpay::contact_request::ContactRequest; @@ -1364,12 +1364,14 @@ impl DashPayView<'_, B> { } /// Store `owner`'s position record, marking it as holding a private payment - /// that may be unresolved, durably, before anything is paid: flushed when - /// the backend does not commit a store inline. A backend that does not - /// keep the marker refuses the payment + /// that may be unresolved, durably, before anything is paid + /// (`WalletPersister::store_durably`): flushed when the backend does not + /// commit a store inline, and trusted only when no other flush of the + /// wallet's records can have dropped it. A backend that does not keep the + /// marker refuses the payment /// ([`PlatformWalletError::PrivateContactRecoveryUnsupported`]), and a - /// failed store or flush refuses it too, with nothing paid: a payment a - /// restart could not see would leave the pair unheld. + /// store that is not known to be durable refuses it too, with nothing + /// paid: a payment a restart could not see would leave the pair unheld. /// /// Clearing the marker once the payment resolves is not flushed: a clear /// that is lost only holds the identity after a restart until a @@ -1401,18 +1403,16 @@ impl DashPayView<'_, B> { changeset .private_contact_requests_positions .insert(*owner, marker); - let not_kept = |error: PersistenceError| { - PlatformWalletError::Persistence(format!( - "the private contact request's unresolved payment was not stored durably, so it \ - is not paid for: {error}" - )) - }; - // Not known to be stored until both succeed. + // Not known to be stored until it is durable. self.persister.note_private_marker_stored(*owner, None); - self.persister.store(changeset.into()).map_err(not_kept)?; - if !self.persister.store_commits_inline() { - self.persister.flush().map_err(not_kept)?; - } + self.persister + .store_durably(changeset.into()) + .map_err(|error| { + PlatformWalletError::Persistence(format!( + "the private contact request's unresolved payment was not stored durably, so \ + it is not paid for: {error}" + )) + })?; self.persister .note_private_marker_stored(*owner, Some(marker.private_payments_unresolved)); drop(wm); @@ -4875,6 +4875,59 @@ mod tests { assert!(!pending_to(&alice_wallet, ALICE, &bob).await); } + /// A private payment is refused, with nothing paid, when another flush of + /// the wallet's records fails for good while the marker's own flush runs: + /// that one dropped the buffered marker, so the flush that then succeeded + /// proves nothing, and a restart would not hold the pair. + #[tokio::test] + async fn should_not_pay_for_a_private_request_whose_marker_another_flush_dropped() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let alice_signer = signer(ALICE_PHRASE, &alice); + let persistence = RecordingPersistence::attesting( + PersistenceCapabilities::PRIVATE_CONTACT_SCAN_STATE_RESTART, + ); + let (_manager, alice_wallet) = wallet_on( + Arc::clone(&persistence), + ALICE_PHRASE, + &alice, + &[&alice, &bob], + vec![], + ) + .await; + *persistence.overtaken_by.lock().expect("overtaken") = + Some(alice_wallet.persister().clone()); + let refused = alice_wallet + .identity() + .dashpay() + .send_private_contact_request( + &alice.id(), + &bob.id(), + None, + AutoAcceptProofSource::None, + None, + &alice_signer, + &alice_crypto, + no_payment, + ) + .await; + assert!( + matches!(&refused, Err(PlatformWalletError::Persistence(message)) + if message.contains("not stored durably")), + "{refused:?}" + ); + assert!( + persistence + .overtaken_by + .lock() + .expect("overtaken") + .is_none(), + "a flush was overtaken" + ); + assert!(!pending_to(&alice_wallet, ALICE, &bob).await); + } + /// Over a backend that buffers its stores, a private payment's marker is /// flushed before it is paid: a flush that fails refuses the payment, with /// nothing paid, and one that succeeds leaves the marker durable before diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index de797e603a7..a8ccb1b2887 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -166,18 +166,27 @@ struct DischargedPrivateEntries { journaled: BTreeMap<[u8; 32], (Identifier, Identifier)>, } -/// Why the records stored for a wallet are not known to be durable -/// ([`WalletPersister::flush_durably`]). -#[cfg(feature = "shielded")] -#[derive(Debug)] +/// Why records stored for a wallet are not known to be durable +/// ([`WalletPersister::store_durably`], [`WalletPersister::flush_durably`]). +#[derive(Debug, thiserror::Error)] pub(crate) enum NotDurable { - /// A flush of the wallet id's records failed for good since the handle - /// was made, the backend dropping what it buffered - /// ([`WalletPersister::buffered_writes_lost`]). + /// A flush of the wallet id's records failed for good, the backend + /// dropping what it buffered: since before the store, for a store made + /// durable, and since the handle was made otherwise + /// (`WalletPersister::buffered_writes_lost`). + #[error( + "a flush of the wallet's records failed for good, and the backend dropped what it \ + had buffered" + )] Lost, - /// The flush this call made failed. - Flush(PersistenceError), + /// The store or the flush this call made failed. + #[error("{0}")] + Backend(PersistenceError), /// Another flush is in progress, and its outcome is not recorded yet. + #[error( + "another flush of the wallet's records is in progress, and its outcome is not known \ + yet" + )] OtherFlushInFlight, } @@ -339,7 +348,7 @@ impl WalletPersister { } match self.flush_durably() { Ok(()) => self.note_read_back_private_entries(pending), - Err(NotDurable::Flush(error)) if error.is_transient() => { + Err(NotDurable::Backend(error)) if error.is_transient() => { tracing::warn!( %error, kept = pending.len(), @@ -358,7 +367,7 @@ impl WalletPersister { ); self.unflushed_read_back_entries().extend(pending); } - Err(NotDurable::Flush(error)) => { + Err(NotDurable::Backend(error)) => { // Any other failure dropped the buffer, the records these // notices rest on with it: the notices go, so their journal // records, and the pairs they hold, stay. Their read is not @@ -604,6 +613,42 @@ impl WalletPersister { self.flush_ledger.flush(self.inner.as_ref(), self.wallet_id) } + /// Store `changeset` and answer `Ok` only once it is durable: at once for + /// a backend that commits a store inline, and otherwise after a flush + /// that succeeds with, after it, no flush of the wallet id's records in + /// progress and none failed for good since before the store + /// ([`FlushLedger`]), read in that order. + /// + /// The flush succeeding proves nothing by itself: another flush may take + /// what the store buffered and fail for good, dropping it, and the one + /// here then succeeds over a buffer that no longer holds it. That other + /// flush began before this one returned, and it records its failure only + /// after the backend returns: so after this flush it shows either as a + /// flush in progress, or as one more that failed for good than there + /// were before the store. + /// + /// It answers for `changeset` alone, which [`Self::flush_durably`] does + /// not: a flush that failed for good before the store dropped nothing of + /// it, and does not refuse it. + pub(crate) fn store_durably( + &self, + changeset: PlatformWalletChangeSet, + ) -> Result<(), NotDurable> { + let lost_before = self.flush_ledger.lost(); + self.store(changeset).map_err(NotDurable::Backend)?; + if self.store_commits_inline() { + return Ok(()); + } + self.flush().map_err(NotDurable::Backend)?; + if self.flush_ledger.in_flight() != 0 { + return Err(NotDurable::OtherFlushInFlight); + } + if self.flush_ledger.lost() != lost_before { + return Err(NotDurable::Lost); + } + Ok(()) + } + /// Make every record stored for this wallet before the call durable, and /// answer `Ok` only once it is: at once for a backend that commits /// inline, after a flush that succeeds otherwise, and never once a flush @@ -633,7 +678,7 @@ impl WalletPersister { return Err(NotDurable::Lost); } if !self.store_commits_inline() { - self.flush().map_err(NotDurable::Flush)?; + self.flush().map_err(NotDurable::Backend)?; if self.flush_ledger.in_flight() != 0 { return Err(NotDurable::OtherFlushInFlight); } @@ -806,9 +851,7 @@ mod tests { use super::*; #[cfg(feature = "shielded")] use crate::changeset::PersistenceErrorKind; - #[cfg(feature = "shielded")] use std::sync::{mpsc, OnceLock}; - #[cfg(feature = "shielded")] use std::time::Duration; /// `persists_durably` is a fail-closed security capability: an @@ -838,6 +881,159 @@ mod tests { assert!(!NoPlatformPersistence.persists_durably()); } + /// A store is not answered for on the strength of a flush that succeeded + /// after another took what the store buffered and failed for good, + /// whether that one ran between the store and its flush or during the + /// flush: the record never reached disk. A later store is answered for, + /// as the flush that failed dropped nothing of it. + #[test] + fn should_not_answer_for_a_store_another_flush_dropped() { + /// Buffers what is stored, and is overtaken once: a flush through the + /// same persister, as one from another thread, takes the buffer and + /// fails for good, right after the first store buffers + /// (`in_store`) or as the first flush begins. + struct Overtaken { + persister: OnceLock, + in_store: bool, + overtaken: AtomicBool, + fails_for_good: AtomicBool, + buffered: AtomicU64, + durable: AtomicU64, + } + impl Overtaken { + fn overtake(&self) { + if self.overtaken.swap(true, Ordering::SeqCst) { + return; + } + self.fails_for_good.store(true, Ordering::SeqCst); + let overtaking = self.persister.get().expect("set").flush(); + self.fails_for_good.store(false, Ordering::SeqCst); + assert!(overtaking.is_err_and(|error| !error.is_transient())); + } + } + impl PlatformWalletPersistence for Overtaken { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + self.buffered.fetch_add(1, Ordering::SeqCst); + if self.in_store { + self.overtake(); + } + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + if self.fails_for_good.load(Ordering::SeqCst) { + self.buffered.store(0, Ordering::SeqCst); + return Err(PersistenceError::backend("the database is corrupt")); + } + self.overtake(); + self.durable + .fetch_add(self.buffered.swap(0, Ordering::SeqCst), Ordering::SeqCst); + Ok(()) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + for in_store in [true, false] { + let backend = Arc::new(Overtaken { + persister: OnceLock::new(), + in_store, + overtaken: AtomicBool::new(false), + fails_for_good: AtomicBool::new(false), + buffered: AtomicU64::new(0), + durable: AtomicU64::new(0), + }); + let persister = WalletPersister::new([0; 32], Arc::clone(&backend) as _); + let _ = backend.persister.set(persister.clone()); + + let stored = persister.store_durably(PlatformWalletChangeSet::default()); + assert!( + matches!(stored, Err(NotDurable::Lost)), + "overtaken in its store: {in_store}, {stored:?}" + ); + assert_eq!(backend.durable.load(Ordering::SeqCst), 0, "never on disk"); + + let later = persister.store_durably(PlatformWalletChangeSet::default()); + assert!( + later.is_ok(), + "overtaken in its store: {in_store}, {later:?}" + ); + assert_eq!(backend.durable.load(Ordering::SeqCst), 1, "on disk"); + } + } + + /// A store is not answered for while another flush of the wallet's + /// records is still inside the backend: that one may have taken what the + /// store buffered, and its failure is recorded only once it returns. + #[test] + fn should_not_answer_for_a_store_while_another_flush_has_not_returned() { + struct Stalled { + flushes: AtomicU64, + entered: std::sync::Mutex>, + release: std::sync::Mutex>, + } + impl PlatformWalletPersistence for Stalled { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + if self.flushes.fetch_add(1, Ordering::SeqCst) != 0 { + return Ok(()); + } + // The first flush has not returned yet. + self.entered + .lock() + .expect("entered") + .send(()) + .expect("the test waits"); + self.release + .lock() + .expect("release") + .recv_timeout(Duration::from_secs(10)) + .expect("released"); + Ok(()) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + let (entered, has_entered) = mpsc::channel(); + let (release, released) = mpsc::channel(); + let backend = Arc::new(Stalled { + flushes: AtomicU64::new(0), + entered: std::sync::Mutex::new(entered), + release: std::sync::Mutex::new(released), + }); + let persister = WalletPersister::new([0; 32], backend as _); + let stalled = { + let persister = persister.clone(); + std::thread::spawn(move || persister.flush()) + }; + has_entered + .recv_timeout(Duration::from_secs(10)) + .expect("the first flush is inside the backend"); + + let meanwhile = persister.store_durably(PlatformWalletChangeSet::default()); + release.send(()).expect("the first flush waits"); + assert!(stalled.join().expect("the first flush returns").is_ok()); + + assert!( + matches!(meanwhile, Err(NotDurable::OtherFlushInFlight)), + "{meanwhile:?}" + ); + let after = persister.store_durably(PlatformWalletChangeSet::default()); + assert!(after.is_ok(), "{after:?}"); + } + /// The host's flush runs with no lock of the persister held: a flush that /// notes another read-back commitment through the same persister, as a /// reentrant backend or another thread would, completes, and nothing a From 143d4bdb7ebdc1c9ea3b70691b5b6907707c791d Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 14 Nov 2026 00:24:14 +0700 Subject: [PATCH 05/14] fix(platform-wallet): count transient flush failures, and keep a newer request when a held one settles Two gaps in the contact request holds. A flush that fails transiently takes the wallet's buffered records and puts them back when it fails. A flush made meanwhile succeeds over a buffer those records are not in, and once the other one had returned nothing showed it: no flush in progress, none failed for good. The flush ledger now counts every failed flush, and `store_durably` and `flush_durably` trust their own flush only when none failed while they ran. They answer `NotDurable::OtherFlushUnsettled` (was `OtherFlushInFlight`) for a flush in progress or one that put back what it took: the caller asks again, and that flush writes it. A settle that found its held request on Platform recorded it whenever its own hold still stood. A sweep may have read in a newer request by then, one that another device of the identity sent while the hold stood, and recording the older one over it had the next rotation build from a reference already used. `record_settled_public_request` now passes over a request older than the one tracked, as the sweep's ingest does (`predates`): the hold is released and nothing is recorded. Co-Authored-By: Claude Fable 5.2 --- packages/rs-platform-wallet/README.md | 4 +- .../src/changeset/flush_ledger.rs | 34 ++- .../identity/network/contact_requests.rs | 13 ++ .../network/private_contact_requests.rs | 66 ++++++ .../src/wallet/persister.rs | 194 +++++++++++++++--- 5 files changed, 264 insertions(+), 47 deletions(-) diff --git a/packages/rs-platform-wallet/README.md b/packages/rs-platform-wallet/README.md index 5ba091dd1f3..c79208c8253 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -130,7 +130,7 @@ Until a payment's reservation is restored from the journal, nothing shows that t - In a build with the `shielded` feature, a wallet published, registered or loaded, while no shielded store is configured is held when its persister attests the private scan state: only such a persister lets a private payment be paid, so only its wallet can have journal records that no marker shows, as after the wallet was removed and registered again from the same seed, in this process or a new one. A wallet whose persister does not attest is never held for want of a store. With a store configured, the journal is read as the wallet is published. - In a build with the `shielded` feature, `load_from_persistor` also holds a wallet that bound a shielded account before (its persisted viewing key shows it). A wallet with no persisted viewing key never bound one, since a bind persists the key before installing anything and refuses when it cannot. - In a build with the `shielded` feature, a wallet stays held while its last restoration failed, or waits for a busy store. -- In every build, an identity whose private scan state marks a private payment that may be unresolved (`PrivateContactRequestsPosition::private_payments_unresolved`) is held. The wallet stores the marker durably before such a payment may reach the network, flushing a backend that does not commit a store inline. That flush is trusted only when, after it, no other flush of the wallet's records is in progress and none failed for good since before the marker was stored: one that did may have dropped the marker, so the payment is refused with nothing paid. The wallet stores the marker cleared once the identity holds no private request it is paying for or has not read back. A loaded hold is kept even when the backend no longer attests the scan state. +- In every build, an identity whose private scan state marks a private payment that may be unresolved (`PrivateContactRequestsPosition::private_payments_unresolved`) is held. The wallet stores the marker durably before such a payment may reach the network, flushing a backend that does not commit a store inline. That flush is trusted only when, after it, no other flush of the wallet's records is in progress and none failed since before the marker was stored: one that failed for good dropped what it had taken from the buffer, and one that failed transiently put it back for a later flush, so either way the marker may not be on disk and the payment is refused with nothing paid. The wallet stores the marker cleared once the identity holds no private request it is paying for or has not read back. A loaded hold is kept even when the backend no longer attests the scan state. - In every build, an identity added to a wallet registered or loaded under an id whose record the backend kept marked after removing the wallet (`ClientStartState::retained_private_contact_scan_states`) is held the same way, before it can send or accept, whether it is added from Platform or by discovery. Only the marker is taken: the record's read position covers requests that went with the wallet's other rows, so the store is read for the identity from its start. A backend declaring `PRIVATE_CONTACT_REQUEST_SCAN_STATE` keeps a marked record through the wallet's removal and any purge of its rows, until a record stored cleared for that wallet and owner replaces it; the wallet never asks for one to be deleted, as `remove_wallet` stores nothing and `remove_identity` refuses while the identity's record is marked. A private contact request is therefore paid for only over a backend attesting `PRIVATE_CONTACT_REQUEST_SCAN_STATE` (bit 15) together with `ATOMIC_CHANGESETS` (bit 0), the two `PersistenceCapabilities::PRIVATE_CONTACT_SCAN_STATE_RESTART` names, as the scan state is written in changesets that must commit whole: any other refuses the private send and the private accept with `PrivateContactRecoveryUnsupported` (FFI code 73) before anything is sealed or paid, and a marker that is not known to be stored durably refuses the payment too. A payment that may have reached the network keeps its pair held and its marker set whatever its spend's outcome, until its record is removed as above. A pair held so refuses another private payment (`PrivateContactRequestPending`, FFI code 71) and holds public sends and accepts. The wallet holds it, beyond the sender identity, for as long as the journal keeps the payment's record: removing the identity and loading it again does not release it. @@ -151,7 +151,7 @@ Every shielded spend that consumes notes (shielded transfers, unshields, withdra Every public `contactRequest` (a send, a public accept, the in-kind accept's public reply, a send from a QR code, an auto-accept and a rotation) is sent by one path, which signs the transition, keeps the signed bytes, and broadcasts them by the rule shielded spends use: only a refusal before anything was sent reports its error with nothing held. Once a dispatch may have reached an endpoint, the send waits for the result (not after an endpoint's verdict, which no proof backs), records the request when its document is confirmed, and otherwise reports the SDK's unconfirmed outcome (`StateTransitionResultUnconfirmed`, FFI code 20) and holds the pair as an unconfirmed public request. Before signing, the send reads the request it would make by DashPay's `ownerIdUserIdAndAccountRef` unique index (the sender, the recipient and the account reference); one found, sent by an earlier process that ended before recording it or by another device of the sender, is adopted as sent, with nothing signed. A read that fails sends on. -While an unconfirmed public request holds a pair, a public or private request to it, an accept included, is refused with `PublicContactRequestUnconfirmed` (FFI code 76), with nothing signed or paid, and the sender identity cannot be removed. `DashPayView::public_contact_request_unconfirmed` reads one pair's state, beside `DashPayView::private_pair_held`, so a host shows a public request awaiting its outcome apart from a private one pending. The public contact request sync settles it, as does each send or accept to the pair before it decides: `Sdk::state_transition_outcome` reads the transition's identity contract nonce, proved, and then the request is read by the unique index, proved. A request found is recorded as sent, as a confirmed send records it, and frees the pair; a send that finds it this way returns it as the request sent, with nothing signed, rather than building another past it. That holds whether or not the receiving account for the pair could be registered then: a registration that fails is logged and left to the next sync, and is never reported as a failed send. A read that finds the request records it only while its own hold still stands: when another read of the same hold recorded it first, the later one records nothing, so a request sent since is never replaced by the older one. With none found, a nonce proved used at a height at or below the height of the read that missed the request shows the transition never wrote it and never will, and frees the pair; a new request may then be sent. Otherwise the transition may still execute, and the pair stays held. No retry signs a second transition. +While an unconfirmed public request holds a pair, a public or private request to it, an accept included, is refused with `PublicContactRequestUnconfirmed` (FFI code 76), with nothing signed or paid, and the sender identity cannot be removed. `DashPayView::public_contact_request_unconfirmed` reads one pair's state, beside `DashPayView::private_pair_held`, so a host shows a public request awaiting its outcome apart from a private one pending. The public contact request sync settles it, as does each send or accept to the pair before it decides: `Sdk::state_transition_outcome` reads the transition's identity contract nonce, proved, and then the request is read by the unique index, proved. A request found is recorded as sent, as a confirmed send records it, and frees the pair; a send that finds it this way returns it as the request sent, with nothing signed, rather than building another past it. That holds whether or not the receiving account for the pair could be registered then: a registration that fails is logged and left to the next sync, and is never reported as a failed send. A read that finds the request records it only while its own hold still stands: when another read of the same hold recorded it first, the later one records nothing, so a request sent since is never replaced by the older one. Nor is it recorded over a newer request the wallet tracks, as one that another device of the identity sent while the hold stood: the hold is released and the newer request stays. With none found, a nonce proved used at a height at or below the height of the read that missed the request shows the transition never wrote it and never will, and frees the pair; a new request may then be sent. Otherwise the transition may still execute, and the pair stays held. No retry signs a second transition. While it may still execute, the same signed bytes are broadcast again only where a public request to the recipient would still go out, by what the wallet tracks. The public sync does it, and so does a public send whose read of the private store succeeded; a private send never does, as it is about to take the pair privately. The check and the dispatch run under the wallet generation's lifecycle gate, so nothing goes out for a wallet that is being removed. Nothing is broadcast while the wallet's private payments may be unrestored, while the sender's read of the private store is rewound (keys that changed, a sender unignored) and has not reached the store's end again, or once a private request holds the pair or their requests came or went privately: the request then stays held until a proved read settles it. This is decided from what the wallet has read, not from the store: a private request the store holds that no private sync has read in yet does not stop the broadcast, as one sealed after a send's own store read does not stop that send. diff --git a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs index c6ee029a97e..798680124c5 100644 --- a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs +++ b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs @@ -10,19 +10,23 @@ use crate::wallet::platform_wallet::WalletId; /// What this process knows of the flushes of one wallet's records. /// /// A backend buffers a wallet's records under its id, whoever stored them, -/// and a flush that fails for good drops that buffer, whoever flushed. So -/// what a flush did to the buffer is known only where every flush of the id -/// is counted: one ledger per wallet id, shared by the manager and by every +/// and a flush takes that buffer, whoever flushed: one that fails for good +/// drops what it took, and one that fails transiently puts it back. So what +/// a flush did to the buffer is known only where every flush of the id is +/// counted: one ledger per wallet id, shared by the manager and by every /// handle of a wallet under that id, a removed wallet's handles and its /// same-id successor's alike ([`FlushLedgers`]). Every flush of the wallet's /// records goes through [`Self::flush`]. #[derive(Debug, Default)] pub struct FlushLedger { /// How many flushes are in progress: each is counted from before it calls - /// the backend until its outcome is recorded in `lost`, so while none is, - /// `lost` tells of every flush that failed for good. + /// the backend until its outcome is recorded in `failed` and `lost`, so + /// while none is, they tell of every flush that failed. in_flight: AtomicUsize, - /// How many flushes failed for good, the backend dropping what it + /// How many flushes failed, for good or not: none wrote what it had taken + /// from the buffer, which the backend dropped or put back. + failed: AtomicU64, + /// How many of them failed for good, the backend dropping what it /// buffered. lost: AtomicU64, } @@ -45,9 +49,9 @@ impl Drop for InFlight<'_> { impl FlushLedger { /// Flush `wallet_id`'s records on `backend`: in progress from before the - /// backend is called until its outcome is recorded, and counted as lost - /// when it fails for good (anything but a transient failure, which keeps - /// the buffer). + /// backend is called until its outcome is recorded, counted as failed + /// when it fails, and as lost too when it fails for good (anything but a + /// transient failure, which keeps the buffer). pub(crate) fn flush( &self, backend: &dyn PlatformWalletPersistence, @@ -55,8 +59,11 @@ impl FlushLedger { ) -> Result<(), PersistenceError> { let _in_flight = InFlight::enter(&self.in_flight); let flushed = backend.flush(wallet_id); - if flushed.as_ref().is_err_and(|error| !error.is_transient()) { - self.lost.fetch_add(1, Ordering::SeqCst); + if let Err(error) = &flushed { + if !error.is_transient() { + self.lost.fetch_add(1, Ordering::SeqCst); + } + self.failed.fetch_add(1, Ordering::SeqCst); } flushed } @@ -66,6 +73,11 @@ impl FlushLedger { self.in_flight.load(Ordering::SeqCst) } + /// How many flushes failed so far, for good or not. + pub(crate) fn failed(&self) -> u64 { + self.failed.load(Ordering::SeqCst) + } + /// How many flushes failed for good so far. pub(crate) fn lost(&self) -> u64 { self.lost.load(Ordering::SeqCst) diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs index dcff5517527..a5e9b16d649 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs @@ -1333,6 +1333,12 @@ impl DashPayView<'_, B> { /// already used. So a settle whose hold is gone records nothing: it /// answers the request when that is the one tracked, as the other settle /// recorded it, and `None` otherwise. + /// + /// A hold that still stands does not make its request the newest either: + /// a sweep may have read in a newer one, sent from another device of the + /// identity, while it stood. The older request is not recorded over that + /// one, as a sweep passes over a replay ([`predates`]): the hold is + /// released, as its request is settled, and nothing is recorded. pub(super) async fn record_settled_public_request( &self, sender: &Identifier, @@ -1359,6 +1365,13 @@ impl DashPayView<'_, B> { .is_some_and(|tracked| tracked.account_reference == request.account_reference); return Ok(tracked.then_some(request)); } + let superseded = dashpay + .outgoing_request(recipient) + .is_some_and(|tracked| predates(&request, tracked)); + if superseded { + managed.settle_unconfirmed_public_request(recipient, attempt); + return Ok(None); + } track_sent_contact_request(managed, &request, &self.persister)?; managed.settle_unconfirmed_public_request(recipient, attempt); } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs index 8b2de7985ca..bc42280c06c 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs @@ -6904,6 +6904,72 @@ mod tests { } } + /// A settle that finds its held request on Platform does not record it + /// over a newer request the wallet tracks, as a sweep read in one that + /// another device of the identity sent while the hold stood: the hold is + /// released, the newer request stays tracked, and nothing is answered as + /// recorded. + #[tokio::test] + async fn should_keep_a_newer_tracked_request_when_a_settle_finds_the_held_one() { + use platform_encryption::ACCOUNT_REFERENCE_VERSION_SHIFT; + + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let alice_signer = signer(ALICE_PHRASE, &alice); + let (_manager, alice_wallet, _transition, reference) = + unconfirmed_public_request(&alice, &bob, &alice_signer, &alice_crypto).await; + expect_sent_request( + &alice_wallet, + &alice, + &bob, + reference, + Some(sent_request_document(&alice, &bob, reference)), + ) + .await; + // As a sweep reads in a rotation past the held request. + let newer = reference + (1 << ACCOUNT_REFERENCE_VERSION_SHIFT); + { + let iw = alice_wallet.identity(); + let mut wm = iw.wallet_manager.write().await; + wm.get_wallet_info_mut(&alice_wallet.wallet_id()) + .expect("wallet info") + .identity_manager + .managed_identity_mut(&alice.id()) + .expect("the sender") + .add_sent_contact_request(public_request(&alice, &bob, newer), &iw.persister) + .expect("recorded"); + } + assert!( + unconfirmed_to(&alice_wallet, ALICE, &bob).await.is_some(), + "the older request is held still" + ); + + let settled = alice_wallet + .identity() + .dashpay() + .settle_unconfirmed_public_request(&alice.id(), &bob.id(), true) + .await + .expect("the reads"); + + assert_eq!(settled, None, "nothing recorded"); + assert!( + unconfirmed_to(&alice_wallet, ALICE, &bob).await.is_none(), + "the hold is released" + ); + managed(&alice_wallet, ALICE, |managed| { + assert_eq!( + managed + .dashpay() + .outgoing_request(&bob.id()) + .map(|request| request.account_reference), + Some(newer), + "the newer request stays tracked" + ); + }) + .await; + } + /// Every flush of the records under a wallet id is counted in one place, /// whichever handle makes it: one through the handle of a wallet removed /// since, failing for good, drops what its same-id successor had diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index a8ccb1b2887..82649a18303 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -182,12 +182,15 @@ pub(crate) enum NotDurable { /// The store or the flush this call made failed. #[error("{0}")] Backend(PersistenceError), - /// Another flush is in progress, and its outcome is not recorded yet. + /// Another flush of the wallet id's records has not returned, or failed + /// while the call ran with the backend keeping what it had taken from the + /// buffer: either may hold records the call answers for, which a later + /// flush writes. A later call answers. #[error( - "another flush of the wallet's records is in progress, and its outcome is not known \ - yet" + "another flush of the wallet's records is in progress, or failed keeping what it had \ + taken, so what was flushed may not be everything stored" )] - OtherFlushInFlight, + OtherFlushUnsettled, } impl WalletPersister { @@ -319,7 +322,8 @@ impl WalletPersister { /// inline, after a flush otherwise. Those a transient flush failure left /// are kept, with every one kept before, and noted by the next call whose /// flush succeeds: an empty one retries them. So are those of a call made - /// while another flush is in progress, whose outcome is not known yet. + /// while another flush is in progress, whose outcome is not known yet, or + /// during which another flush failed and put back what it had taken. /// Those any other failure left are dropped, as the backend dropped the /// records they rest on. Their journal records, and so the pairs they /// hold, stay until noted. The host's flush runs with none of the @@ -359,11 +363,12 @@ impl WalletPersister { // meanwhile. self.unflushed_read_back_entries().extend(pending); } - Err(NotDurable::OtherFlushInFlight) => { + Err(NotDurable::OtherFlushUnsettled) => { tracing::warn!( kept = pending.len(), "private requests read back while another flush of this wallet's records \ - is in progress are not noted yet; a later call notes them" + is in progress, or failed keeping what it took, are not noted yet; a later \ + call notes them" ); self.unflushed_read_back_entries().extend(pending); } @@ -616,16 +621,18 @@ impl WalletPersister { /// Store `changeset` and answer `Ok` only once it is durable: at once for /// a backend that commits a store inline, and otherwise after a flush /// that succeeds with, after it, no flush of the wallet id's records in - /// progress and none failed for good since before the store - /// ([`FlushLedger`]), read in that order. + /// progress and none failed since before the store ([`FlushLedger`]), + /// read in that order. /// /// The flush succeeding proves nothing by itself: another flush may take - /// what the store buffered and fail for good, dropping it, and the one - /// here then succeeds over a buffer that no longer holds it. That other - /// flush began before this one returned, and it records its failure only - /// after the backend returns: so after this flush it shows either as a - /// flush in progress, or as one more that failed for good than there - /// were before the store. + /// what the store buffered and fail, dropping it or putting it back, and + /// the one here then succeeds over a buffer that does not hold it. That + /// other flush began before this one returned, and it records its failure + /// only after the backend returns: so after this flush it shows either as + /// a flush in progress, or as one more that failed than there were before + /// the store. One that failed for good answers [`NotDurable::Lost`]; one + /// in progress, or that put back what it took, + /// [`NotDurable::OtherFlushUnsettled`], as a later flush writes it. /// /// It answers for `changeset` alone, which [`Self::flush_durably`] does /// not: a flush that failed for good before the store dropped nothing of @@ -634,18 +641,21 @@ impl WalletPersister { &self, changeset: PlatformWalletChangeSet, ) -> Result<(), NotDurable> { - let lost_before = self.flush_ledger.lost(); + let (lost_before, failed_before) = (self.flush_ledger.lost(), self.flush_ledger.failed()); self.store(changeset).map_err(NotDurable::Backend)?; if self.store_commits_inline() { return Ok(()); } self.flush().map_err(NotDurable::Backend)?; if self.flush_ledger.in_flight() != 0 { - return Err(NotDurable::OtherFlushInFlight); + return Err(NotDurable::OtherFlushUnsettled); } if self.flush_ledger.lost() != lost_before { return Err(NotDurable::Lost); } + if self.flush_ledger.failed() != failed_before { + return Err(NotDurable::OtherFlushUnsettled); + } Ok(()) } @@ -657,30 +667,42 @@ impl WalletPersister { /// /// A flush that succeeds proves nothing by itself: one that failed for /// good just before it left nothing buffered, so it succeeds over records - /// that never reached disk. That failure is recorded only after the - /// backend returns, so between the two it shows nowhere but in the count - /// of flushes in progress. The flush here is therefore trusted only when, - /// after it, no flush is in progress and none has failed for good, read - /// in that order: every flush that began before then has recorded its - /// outcome. Both are read from the ledger of the wallet's id, which - /// counts every flush of its records, whoever made it: this handle, a - /// handle of a wallet removed or created under the id since, or the - /// manager. One that fails later drops only what was buffered after this - /// one wrote, so what this call answers for stays durable, and what the - /// caller does on the strength of it needs no lock held with the flush. + /// that never reached disk, and one that fails transiently holds what it + /// took until it puts it back, so a flush made meanwhile succeeds over a + /// buffer those records are not in. A failure is recorded only after the + /// backend returns, so until then it shows nowhere but in the count of + /// flushes in progress. The flush here is therefore trusted only when, + /// after it, no flush is in progress, none has failed for good, and none + /// has failed at all since this call began, read in that order: every + /// flush that began before then has recorded its outcome. All are read + /// from the ledger of the wallet's id, which counts every flush of its + /// records, whoever made it: this handle, a handle of a wallet removed or + /// created under the id since, or the manager. One that fails later + /// takes only what was buffered after this one wrote, so what this call + /// answers for stays durable, and what the caller does on the strength of + /// it needs no lock held with the flush. /// - /// While another flush is in progress the answer is - /// [`NotDurable::OtherFlushInFlight`], also for a call made from inside - /// a backend's own flush: the caller asks again later. + /// While another flush is in progress, or when one failed during the call + /// and put back what it took, the answer is + /// [`NotDurable::OtherFlushUnsettled`], also for a call made from inside + /// a backend's own flush: the caller asks again later, and that call's + /// flush writes what was put back. #[cfg(feature = "shielded")] pub(crate) fn flush_durably(&self) -> Result<(), NotDurable> { if self.buffered_writes_lost() { return Err(NotDurable::Lost); } if !self.store_commits_inline() { + let failed_before = self.flush_ledger.failed(); self.flush().map_err(NotDurable::Backend)?; if self.flush_ledger.in_flight() != 0 { - return Err(NotDurable::OtherFlushInFlight); + return Err(NotDurable::OtherFlushUnsettled); + } + if self.buffered_writes_lost() { + return Err(NotDurable::Lost); + } + if self.flush_ledger.failed() != failed_before { + return Err(NotDurable::OtherFlushUnsettled); } } if self.buffered_writes_lost() { @@ -849,7 +871,6 @@ impl PlatformWalletPersistence for NoPlatformPersistence { #[cfg(test)] mod tests { use super::*; - #[cfg(feature = "shielded")] use crate::changeset::PersistenceErrorKind; use std::sync::{mpsc, OnceLock}; use std::time::Duration; @@ -966,6 +987,111 @@ mod tests { } } + /// Buffers what is stored. Its first flush finds the buffer empty: + /// another flush, made through the same persister as one from another + /// thread, took it before, and fails transiently while the first runs, + /// putting back what it took. Later flushes write what is buffered. + #[derive(Default)] + struct PutBack { + persister: OnceLock, + overlapped: AtomicBool, + other_fails: AtomicBool, + buffered: AtomicU64, + durable: AtomicU64, + } + + impl PutBack { + /// A persister over a new one, which its first flush overlaps + /// through. + fn persister() -> (Arc, WalletPersister) { + let backend = Arc::new(Self::default()); + let persister = WalletPersister::new([0; 32], Arc::clone(&backend) as _); + let _ = backend.persister.set(persister.clone()); + (backend, persister) + } + } + + impl PlatformWalletPersistence for PutBack { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + self.buffered.fetch_add(1, Ordering::SeqCst); + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + if self.other_fails.load(Ordering::SeqCst) { + // The other flush: what it took is buffered again. + return Err(PersistenceError::backend_with_kind( + PersistenceErrorKind::Transient, + "the disk is busy", + )); + } + if !self.overlapped.swap(true, Ordering::SeqCst) { + self.other_fails.store(true, Ordering::SeqCst); + let other = self.persister.get().expect("set").flush(); + self.other_fails.store(false, Ordering::SeqCst); + assert!(other.is_err_and(|error| error.is_transient())); + // The buffer was empty when this flush took it: nothing + // written. + return Ok(()); + } + self.durable + .fetch_add(self.buffered.swap(0, Ordering::SeqCst), Ordering::SeqCst); + Ok(()) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + /// A store is not answered for on the strength of a flush that succeeded + /// while another held what the store buffered, and then failed + /// transiently and put it back: the record is buffered still. The next + /// flush writes it, and a store made then is answered for. + #[test] + fn should_not_answer_for_a_store_another_flush_took_and_put_back() { + let (backend, persister) = PutBack::persister(); + + let stored = persister.store_durably(PlatformWalletChangeSet::default()); + assert!( + matches!(stored, Err(NotDurable::OtherFlushUnsettled)), + "{stored:?}" + ); + assert_eq!(backend.durable.load(Ordering::SeqCst), 0, "buffered still"); + + let later = persister.store_durably(PlatformWalletChangeSet::default()); + assert!(later.is_ok(), "{later:?}"); + assert_eq!(backend.durable.load(Ordering::SeqCst), 2, "both on disk"); + } + + /// A read-back is not noted on the strength of a flush that succeeded + /// while another held the record it rests on, and then failed transiently + /// and put it back: the record is buffered still. The notice is kept, + /// and noted by the next call, whose flush writes the record. + #[cfg(feature = "shielded")] + #[test] + fn should_note_no_read_back_when_a_flush_puts_back_what_it_took_during_its_own() { + let (backend, persister) = PutBack::persister(); + persister + .store(PlatformWalletChangeSet::default()) + .expect("buffered"); + + persister.note_read_back_once_durable([[1; 32]]); + assert!( + !persister.has_read_back_private_entries(), + "its flush wrote nothing of what the other put back" + ); + assert!(persister.read_back_awaiting_flush(&[1; 32]), "kept"); + assert!(!persister.buffered_writes_lost(), "nothing was dropped"); + assert_eq!(backend.durable.load(Ordering::SeqCst), 0, "buffered still"); + + persister.note_read_back_once_durable([]); + assert!(persister.has_read_back_private_entries(), "noted"); + assert_eq!(backend.durable.load(Ordering::SeqCst), 1, "on disk"); + } + /// A store is not answered for while another flush of the wallet's /// records is still inside the backend: that one may have taken what the /// store buffered, and its failure is recorded only once it returns. @@ -1027,7 +1153,7 @@ mod tests { assert!(stalled.join().expect("the first flush returns").is_ok()); assert!( - matches!(meanwhile, Err(NotDurable::OtherFlushInFlight)), + matches!(meanwhile, Err(NotDurable::OtherFlushUnsettled)), "{meanwhile:?}" ); let after = persister.store_durably(PlatformWalletChangeSet::default()); From 901eeec74204fb7d74150aa83613da99093ea908 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 14 Nov 2026 02:13:24 +0700 Subject: [PATCH 06/14] fix(platform-wallet): record a flush whose backend panics as lost The flush ledger counts a flush as in progress until its outcome is recorded, so that while none is in progress its counts tell of every flush that failed. A backend that panicked in its flush broke that: the flush stopped being counted with nothing recorded, and a caller that caught the panic and carried on could then trust a later flush over records the panicking one had taken. A flush that ends without its outcome recorded is now counted as failed for good before it stops being in progress, as what its backend did with what it took is not known. Also brings the FFI, Swift and Kotlin descriptions of result code 76 in line with the wallet's and the registry's: it is also what a public send or accept gets while another public request to the identity is being sent. Co-Authored-By: Claude Fable 5.2 --- .../dashsdk/errors/DashSdkError.kt | 5 +- packages/rs-platform-wallet-ffi/src/error.rs | 3 + .../src/changeset/flush_ledger.rs | 100 +++++++++++++++--- .../PlatformWallet/PlatformWalletResult.swift | 4 +- 4 files changed, 96 insertions(+), 16 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..b3f89f0eaf0 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 @@ -789,7 +789,10 @@ sealed class DashSdkError( * DashPay sync settles it (the request found: recorded as sent; its * nonce proved used: the pair freed), and * `Dashpay.publicContactRequestsUnconfirmed` reads whether it still - * stands. Not a private request pending ([PrivateContactRequestPending]). + * stands. Also thrown by a public send or accept made while another + * public request to that identity is being sent: one is sent at a + * time, and nothing is signed for the second. Not a private request + * pending ([PrivateContactRequestPending]). * ← Swift `PlatformWalletError.publicContactRequestUnconfirmed`. */ class PublicContactRequestUnconfirmed(message: String, cause: Throwable? = null) : diff --git a/packages/rs-platform-wallet-ffi/src/error.rs b/packages/rs-platform-wallet-ffi/src/error.rs index 58194bf7197..25b95a9fa4c 100644 --- a/packages/rs-platform-wallet-ffi/src/error.rs +++ b/packages/rs-platform-wallet-ffi/src/error.rs @@ -764,6 +764,9 @@ pub enum PlatformWalletFFIResultCode { /// failed: do not submit it again. A public contact request sync settles /// it, recording the request once its document is proved on Platform and /// freeing the pair once its transition is proved unable to write it. + /// Also what a public send or accept gets while another public request + /// to that identity is being sent: one public request to an identity is + /// sent at a time, and nothing is signed for the second. /// A private request pending to the identity is /// [`Self::ErrorPrivateContactRequestPending`] (71). ErrorPublicContactRequestUnconfirmed = 76, diff --git a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs index 798680124c5..6054a9be458 100644 --- a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs +++ b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs @@ -21,7 +21,9 @@ use crate::wallet::platform_wallet::WalletId; pub struct FlushLedger { /// How many flushes are in progress: each is counted from before it calls /// the backend until its outcome is recorded in `failed` and `lost`, so - /// while none is, they tell of every flush that failed. + /// while none is, they tell of every flush that failed. One whose backend + /// panicked is recorded too, as failed for good, before it stops being + /// counted. in_flight: AtomicUsize, /// How many flushes failed, for good or not: none wrote what it had taken /// from the buffer, which the backend dropped or put back. @@ -32,18 +34,41 @@ pub struct FlushLedger { } /// One flush in progress, counted in its [`FlushLedger`] until dropped. -struct InFlight<'a>(&'a AtomicUsize); +/// +/// Dropped before its outcome was recorded, its backend panicked: what that +/// did with what it took from the buffer is not known, so it is recorded as +/// failed for good before it stops being counted. A caller that catches the +/// panic and carries on then finds the loss, as it would any other. +struct InFlight<'a> { + ledger: &'a FlushLedger, + recorded: bool, +} impl<'a> InFlight<'a> { - fn enter(in_flight: &'a AtomicUsize) -> Self { - in_flight.fetch_add(1, Ordering::SeqCst); - Self(in_flight) + fn enter(ledger: &'a FlushLedger) -> Self { + ledger.in_flight.fetch_add(1, Ordering::SeqCst); + Self { + ledger, + recorded: false, + } + } + + /// Record the backend's answer: a failure is counted, and counted as + /// lost too when it is for good. + fn record(&mut self, flushed: &Result<(), PersistenceError>) { + if let Err(error) = flushed { + self.ledger.record_failure(!error.is_transient()); + } + self.recorded = true; } } impl Drop for InFlight<'_> { fn drop(&mut self) { - self.0.fetch_sub(1, Ordering::SeqCst); + if !self.recorded { + self.ledger.record_failure(true); + } + self.ledger.in_flight.fetch_sub(1, Ordering::SeqCst); } } @@ -51,23 +76,27 @@ impl FlushLedger { /// Flush `wallet_id`'s records on `backend`: in progress from before the /// backend is called until its outcome is recorded, counted as failed /// when it fails, and as lost too when it fails for good (anything but a - /// transient failure, which keeps the buffer). + /// transient failure, which keeps the buffer) or the backend panics. pub(crate) fn flush( &self, backend: &dyn PlatformWalletPersistence, wallet_id: WalletId, ) -> Result<(), PersistenceError> { - let _in_flight = InFlight::enter(&self.in_flight); + let mut in_flight = InFlight::enter(self); let flushed = backend.flush(wallet_id); - if let Err(error) = &flushed { - if !error.is_transient() { - self.lost.fetch_add(1, Ordering::SeqCst); - } - self.failed.fetch_add(1, Ordering::SeqCst); - } + in_flight.record(&flushed); flushed } + /// Count one flush that failed, `for_good` when the backend dropped what + /// it had taken. + fn record_failure(&self, for_good: bool) { + if for_good { + self.lost.fetch_add(1, Ordering::SeqCst); + } + self.failed.fetch_add(1, Ordering::SeqCst); + } + /// How many flushes are in progress. pub(crate) fn in_flight(&self) -> usize { self.in_flight.load(Ordering::SeqCst) @@ -101,3 +130,46 @@ impl FlushLedgers { ) } } + +#[cfg(test)] +mod tests { + use std::panic::{catch_unwind, AssertUnwindSafe}; + + use super::*; + use crate::changeset::{ClientStartState, PlatformWalletChangeSet}; + + /// A backend whose flush panics. + struct Panicking; + + impl PlatformWalletPersistence for Panicking { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + panic!("the backend panics in its flush") + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + /// A flush whose backend panics is recorded as failed for good before it + /// stops being counted as in progress: what it did with what it took from + /// the buffer is not known, so a caller that catches the panic and + /// carries on finds the loss rather than a ledger showing nothing. + #[test] + fn should_count_a_flush_that_panics_as_lost() { + let ledger = FlushLedger::default(); + + let flushed = catch_unwind(AssertUnwindSafe(|| ledger.flush(&Panicking, [0; 32]))); + + assert!(flushed.is_err(), "the panic reaches the caller"); + assert_eq!(ledger.in_flight(), 0, "no longer in progress"); + assert_eq!(ledger.lost(), 1, "counted as failed for good"); + assert_eq!(ledger.failed(), 1, "counted as failed"); + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift index ee130585d2d..c47a71a7ac6 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletResult.swift @@ -741,7 +741,9 @@ public enum PlatformWalletError: LocalizedError { /// settles it: the request proved on Platform is recorded as sent, and one /// proved unable to write it frees the pair. /// `ManagedPlatformWallet.publicContactRequestUnconfirmed(senderId:recipientId:)` - /// reads whether the pair is still held so. + /// reads whether the pair is still held so. Also thrown by a public send + /// or accept made while another public request to that identity is being + /// sent: one is sent at a time, and nothing is signed for the second. case publicContactRequestUnconfirmed(String) case walletAlreadyExists(String) /// Definitive shielded-broadcast failure: the shielded transition never From 3ad292db9f8b637bc1477dff57bd77e28bbf6f39 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 14 Nov 2026 04:20:37 +0700 Subject: [PATCH 07/14] fix(platform-wallet): certify the manager's backfill flush, and fence the broken-channel mark Two gaps a review of 901eeec742 found. The manager replaces the host's DashPay backfill record before it publishes a wallet, and on a backend that buffers it flushed that replacement itself and trusted the flush when it succeeded. The flush was counted in the wallet id's flush ledger, but nothing read the ledger afterwards: a flush by a handle of an earlier wallet under the id could take the replacement first and fail, and the manager's own flush then succeeded over a buffer that did not hold it. The replacement now goes through `WalletPersister::store_durably` on that ledger, as a wallet's handle stores a record it acts on, and the wallet is not published when the record is not known to be on the host. On such a backend the replacement is always an empty record with no cursor, so what was at stake is a record an earlier run left staying on the host. `NotDurable::Backend` is split into `Store` and `Flush`, as the stacked store-site change already has it, so the manager keeps reporting a failed store and a failed flush as it did. `WalletPersister:: classify_not_durable` maps the four answers to the wallet error: a record another flush dropped is `PersisterFlush` of kind `Fatal`, one still buffered is `PersisterFlush` of kind `Transient`. `mark_contact_channel_broken` looked the wallet up by id alone, where the two completions beside it already check the handle's generation. Work that found a permanent fault for a wallet since removed and created again under its id could mark the new wallet's contact broken and store that. It now checks the generation too. Tests: should_not_publish_over_a_backfill_record_another_flush_took, should_break_no_channel_on_a_replacement_from_a_stale_handle. Each fails with its fix taken out. Co-Authored-By: Claude Fable 5.2 --- .../rs-platform-wallet/src/manager/mod.rs | 160 +++++++++++++++--- .../identity/network/contact_requests.rs | 12 +- .../network/private_contact_requests.rs | 64 +++++++ .../src/wallet/persister.rs | 42 ++++- 4 files changed, 240 insertions(+), 38 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 76158801df6..c1b72255888 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -52,6 +52,7 @@ use crate::spv::SpvRuntime; use crate::wallet::asset_lock::LockNotifyHandler; use crate::wallet::core::{BalanceUpdateHandler, SpendObservationHandler}; use crate::wallet::identity::network::DashPayPaymentHandler; +use crate::wallet::persister::WalletPersister; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; #[cfg(feature = "shielded")] use crate::wallet::shielded::NetworkShieldedCoordinator; @@ -772,7 +773,11 @@ impl PlatformWalletManager

{ /// not inline, or not atomic) gets no record at all, the same as the /// rescan sweep gives it: only an empty one is written — committed with a /// `flush` where `store` only buffers — before the wallet is published, - /// so a record an earlier run left cannot outlive this one. + /// so a record an earlier run left cannot outlive this one. That flush is + /// trusted as a wallet's handle trusts its own + /// ([`WalletPersister::store_durably`]): not when another flush of the + /// wallet id's records may have taken the empty record and failed, in + /// which case the wallet is not published. #[allow(clippy::too_many_arguments)] pub(super) fn settle_published_backfill( &self, @@ -783,7 +788,6 @@ impl PlatformWalletManager

{ own_cursor: u32, snapshot_cursor: u32, ) -> Result { - let inline = self.persister.store_commits_inline(); let durable = persists_backfill_coverage(&*self.persister); let predecessor = durable_cursors.contains_key(&wallet_id); let held_on_host = !record.is_empty() || dropped; @@ -827,34 +831,28 @@ impl PlatformWalletManager

{ dashpay_backfill: Some(record.clone()), ..PlatformWalletChangeSet::default() }; - self.persister.store(wallet_id, changeset).map_err(|e| { + // Stored as a handle of the wallet stores a record it then acts on + // (`WalletPersister::store_durably`), in the wallet id's flush ledger, + // where every flush of its records is counted. A flush that fails for + // good here drops what a handle of an earlier wallet under the id + // still had buffered, and that handle learns of it from the ledger. + // And a flush that succeeds here is not trusted by itself: a flush + // by such a handle may have taken the replacement first and failed, + // which the ledger shows, and the wallet is then not published. + let persister = WalletPersister::with_flush_ledger( + wallet_id, + Arc::clone(&self.persister) as Arc, + self.durable_cursors.flush_ledgers.of(wallet_id), + ); + persister.store_durably(changeset).map_err(|not_durable| { tracing::error!( wallet_id = %hex::encode(wallet_id), - error = %e, - "failed to replace the host's DashPay backfill record; \ + error = %not_durable, + "the replaced DashPay backfill record is not known to be on the host; \ the wallet was not published" ); - PlatformWalletError::from_store_failure(&*self.persister, e) + persister.classify_not_durable(not_durable) })?; - if !inline { - // Through the wallet id's flush ledger, as every flush of its - // records is: one that fails for good here drops what a handle of - // an earlier wallet under the id still had buffered, and that - // handle learns of it from the ledger. - self.durable_cursors - .flush_ledgers - .of(wallet_id) - .flush(&*self.persister, wallet_id) - .map_err(|e| { - tracing::error!( - wallet_id = %hex::encode(wallet_id), - error = %e, - "failed to commit the replaced DashPay backfill record; \ - the wallet was not published" - ); - PlatformWalletError::from_flush_failure(e) - })?; - } if let Some(cursor) = cursor { durable_cursors.insert(wallet_id, DurableCursor::at(cursor)); } @@ -1507,10 +1505,13 @@ impl Drop for PlatformWalletManager

{ mod tests { use super::*; + use std::sync::OnceLock; + use dash_async::WorkerStatus; use crate::changeset::{ - ClientStartState, PersistenceError, PlatformWalletChangeSet, PlatformWalletPersistence, + ClientStartState, FlushLedger, PersistenceError, PersistenceErrorKind, + PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::events::{EventHandler, PlatformEventHandler}; @@ -1563,10 +1564,8 @@ mod tests { impl PlatformEventHandler for NoopEventHandler {} /// A buffering backend whose flush fails for good, dropping its buffer. - #[cfg(feature = "shielded")] struct FatalFlushPersister; - #[cfg(feature = "shielded")] impl PlatformWalletPersistence for FatalFlushPersister { fn store( &self, @@ -1636,6 +1635,111 @@ mod tests { assert!(!later.buffered_writes_lost()); } + /// A buffering backend whose every store is taken at once by a flush of + /// another handle of the wallet id, as one from another thread would + /// take it before the storer's own flush. That flush fails, for good or + /// keeping what it took, and the storer's own then succeeds over a buffer + /// that does not hold the record. + struct OvertakenPersister { + /// The ledger the other handle's flush is counted in, once known. + ledger: OnceLock>, + /// Whether the other flush fails for good. + for_good: bool, + } + + /// A buffering backend whose flush fails keeping what it took. + struct TransientFlushPersister; + + impl PlatformWalletPersistence for TransientFlushPersister { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Err(PersistenceError::backend_with_kind( + PersistenceErrorKind::Transient, + "the database is locked", + )) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + impl PlatformWalletPersistence for OvertakenPersister { + fn store( + &self, + wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + if let Some(ledger) = self.ledger.get() { + let overtaking = if self.for_good { + ledger.flush(&FatalFlushPersister, wallet_id) + } else { + ledger.flush(&TransientFlushPersister, wallet_id) + }; + assert!(overtaking.is_err(), "the other handle's flush fails"); + } + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Ok(()) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + /// The manager's own flush of the backfill record it replaces succeeding + /// is not proof the record is on the host: a flush by a handle of an + /// earlier wallet under the id may have taken it first and failed. The + /// wallet is then not published, and the error says whether the record + /// is gone (the other flush failed for good) or still buffered for a + /// later flush (it kept what it took). + #[tokio::test] + async fn should_not_publish_over_a_backfill_record_another_flush_took() { + for for_good in [true, false] { + let persister = Arc::new(OvertakenPersister { + ledger: OnceLock::new(), + for_good, + }); + let mgr = PlatformWalletManager::new( + Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")), + Arc::clone(&persister), + Arc::new(NoopEventHandler) as Arc, + ); + let wallet_id: WalletId = [7; 32]; + persister + .ledger + .set(mgr.durable_cursors.flush_ledgers.of(wallet_id)) + .expect("set once"); + + // Coverage the wallet about to be published does not hold was + // dropped, so the host's record is replaced and flushed. + let mut cursors = mgr.durable_cursors.lock().await; + let settled = mgr.settle_published_backfill( + &mut cursors, + wallet_id, + DashPayBackfillRecord::default(), + true, + 5, + 5, + ); + + match settled { + Err(PlatformWalletError::PersisterFlush(error)) => assert_eq!( + error.is_transient(), + !for_good, + "to be tried again only when the record is still buffered" + ), + other => panic!("the wallet is not published, got {other:?}"), + } + } + } + fn make_manager() -> Arc> { let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); Arc::new(PlatformWalletManager::new( diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs index a5e9b16d649..e92674f1715 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs @@ -4496,9 +4496,17 @@ impl DashPayView<'_, B> { /// Mark an established contact's payment channel as permanently broken /// and persist the transition through the changeset pipeline so /// it survives restarts and is FFI/UI-visible. Idempotent. - async fn mark_contact_channel_broken(&self, identity_id: &Identifier, contact_id: &Identifier) { + pub(super) async fn mark_contact_channel_broken( + &self, + identity_id: &Identifier, + contact_id: &Identifier, + ) { let mut wm = self.wallet_manager.write().await; - let Some(info) = wm.get_wallet_info_mut(&self.wallet_id) else { + // On this handle's wallet generation only, as the two completions + // below: the fault was found without the lock, for the wallet the + // handle was taken from, and a wallet re-created under this id since + // has its own channel to judge. + let Ok(info) = self.current_wallet_info_mut(&mut wm) else { return; }; let Some(managed) = info.identity_manager.managed_identity_mut(identity_id) else { diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs index bc42280c06c..02f3bb51cc5 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs @@ -7350,6 +7350,70 @@ mod tests { ); } + /// A handle taken before its wallet was removed and created again under + /// its id breaks no payment channel on the replacement: the fault its + /// work found was found for the wallet it was taken from. The + /// replacement's own handle does. + #[tokio::test] + async fn should_break_no_channel_on_a_replacement_from_a_stale_handle() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let manager = manager_on(RecordingPersistence::attesting( + PersistenceCapabilities::PRIVATE_CONTACT_SCAN_STATE_RESTART, + )) + .await; + let first = create_holding(&manager, ALICE_PHRASE, &alice).await; + let wallet_id = first.wallet_id(); + let stale = first.identity().dashpay(); + manager.remove_wallet(&wallet_id).await.expect("removed"); + let again = create_holding(&manager, ALICE_PHRASE, &alice).await; + assert_eq!(again.wallet_id(), wallet_id); + + // Bob is a contact of the replacement. + { + let iw = again.identity(); + let mut wm = iw.wallet_manager.write().await; + let managed = wm + .get_wallet_info_mut(&wallet_id) + .expect("wallet info") + .identity_manager + .managed_identity_mut(&alice.id()) + .expect("Alice"); + managed + .add_sent_contact_request(public_request(&alice, &bob, 0), &iw.persister) + .expect("sent request"); + managed + .add_incoming_contact_request(public_request(&bob, &alice, 0), &iw.persister) + .expect("incoming request"); + } + let broken = |wallet: Arc| async move { + managed(&wallet, ALICE, |managed| { + managed + .dashpay() + .established_contacts() + .get(&Identifier::from(BOB)) + .expect("established") + .payment_channel_broken + }) + .await + }; + + stale + .mark_contact_channel_broken(&alice.id(), &bob.id()) + .await; + assert!( + !broken(Arc::clone(&again)).await, + "the replacement's channel is its own to judge" + ); + + again + .identity() + .dashpay() + .mark_contact_channel_broken(&alice.id(), &bob.id()) + .await; + assert!(broken(again).await, "broken by its own handle"); + } + /// A handle taken before its wallet was removed and created again under /// its id settles nothing of the replacement's unconfirmed public request, /// whatever the proofs show, and claims nothing on its pair. diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index 82649a18303..d13558c6267 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -13,7 +13,7 @@ use key_wallet::managed_account::transaction_record::TransactionRecord; use crate::changeset::{ ClientStartState, DpnsNameStateEntry, FlushLedger, PersistenceCapabilities, PersistenceError, - PlatformWalletChangeSet, PlatformWalletPersistence, + PersistenceErrorKind, PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::error::PlatformWalletError; use crate::wallet::platform_wallet::WalletId; @@ -179,9 +179,13 @@ pub(crate) enum NotDurable { had buffered" )] Lost, - /// The store or the flush this call made failed. + /// The store this call made failed, so nothing was flushed. #[error("{0}")] - Backend(PersistenceError), + Store(PersistenceError), + /// The flush this call made failed: a transient failure keeps what the + /// backend buffered, any other drops it. + #[error("{0}")] + Flush(PersistenceError), /// Another flush of the wallet id's records has not returned, or failed /// while the call ran with the backend keeping what it had taken from the /// buffer: either may hold records the call answers for, which a later @@ -352,7 +356,7 @@ impl WalletPersister { } match self.flush_durably() { Ok(()) => self.note_read_back_private_entries(pending), - Err(NotDurable::Backend(error)) if error.is_transient() => { + Err(NotDurable::Flush(error)) if error.is_transient() => { tracing::warn!( %error, kept = pending.len(), @@ -372,7 +376,8 @@ impl WalletPersister { ); self.unflushed_read_back_entries().extend(pending); } - Err(NotDurable::Backend(error)) => { + // `flush_durably` stores nothing, so `Store` is never its answer. + Err(NotDurable::Flush(error) | NotDurable::Store(error)) => { // Any other failure dropped the buffer, the records these // notices rest on with it: the notices go, so their journal // records, and the pairs they hold, stay. Their read is not @@ -612,6 +617,27 @@ impl WalletPersister { PlatformWalletError::from_store_failure(self.inner.as_ref(), error) } + /// What a caller reports when [`Self::store_durably`] did not answer for + /// a record: the store's failure as [`Self::classify_store_failure`] + /// gives it, and anything else as the flush failure it is. A flush that + /// failed for good, the call's own or another that took the record, is + /// `PersisterFlush` of kind `Fatal`: the record is gone. Another flush + /// still in progress, or one that put back what it took, leaves the + /// record buffered for a later flush: `PersisterFlush` of kind + /// `Transient`, to be tried again. + pub(crate) fn classify_not_durable(&self, not_durable: NotDurable) -> PlatformWalletError { + match not_durable { + NotDurable::Store(error) => self.classify_store_failure(error), + NotDurable::Flush(error) => PlatformWalletError::from_flush_failure(error), + NotDurable::Lost => PlatformWalletError::from_flush_failure( + PersistenceError::backend_with_kind(PersistenceErrorKind::Fatal, not_durable), + ), + NotDurable::OtherFlushUnsettled => PlatformWalletError::from_flush_failure( + PersistenceError::backend_with_kind(PersistenceErrorKind::Transient, not_durable), + ), + } + } + /// Flush this wallet's records, counted in the ledger of its id /// ([`FlushLedger::flush`]). pub(crate) fn flush(&self) -> Result<(), PersistenceError> { @@ -642,11 +668,11 @@ impl WalletPersister { changeset: PlatformWalletChangeSet, ) -> Result<(), NotDurable> { let (lost_before, failed_before) = (self.flush_ledger.lost(), self.flush_ledger.failed()); - self.store(changeset).map_err(NotDurable::Backend)?; + self.store(changeset).map_err(NotDurable::Store)?; if self.store_commits_inline() { return Ok(()); } - self.flush().map_err(NotDurable::Backend)?; + self.flush().map_err(NotDurable::Flush)?; if self.flush_ledger.in_flight() != 0 { return Err(NotDurable::OtherFlushUnsettled); } @@ -694,7 +720,7 @@ impl WalletPersister { } if !self.store_commits_inline() { let failed_before = self.flush_ledger.failed(); - self.flush().map_err(NotDurable::Backend)?; + self.flush().map_err(NotDurable::Flush)?; if self.flush_ledger.in_flight() != 0 { return Err(NotDurable::OtherFlushUnsettled); } From 9aa9a1bf2ad842300cd259388b4b6ed1e3c26e33 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 14 Nov 2026 09:46:51 +0700 Subject: [PATCH 08/14] fix(platform-wallet): forget stored private markers once any flush of the wallet id fails for good The persister remembers which marker each identity's position record was last stored with, so a record is stored again only when its marker changed. A flush that fails for good drops what the backend buffered, and a store remembered that way may still have been in it. The remembered markers were cleared only where this handle's own flush failed for good while read-backs were being noted. A loss the shared flush ledger reported (another handle's flush, or the manager's) left them in place, so the next comparison found the marker "already stored" and did not store it again. The remembered markers now carry the ledger's count of flushes that failed for good, and are forgotten whenever that count has moved: whoever made the flush, and whether or not anything asked for a durable flush since. The explicit clear in the read-back path goes, as the ledger counts that flush too. Test: should_forget_stored_markers_once_any_flush_of_the_wallet_id_fails_for_good, which fails with the check taken out. Co-Authored-By: Claude Fable 5.2 --- .../src/wallet/persister.rs | 135 +++++++++++++++--- 1 file changed, 116 insertions(+), 19 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index d13558c6267..20fd27f5300 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -94,10 +94,12 @@ pub struct WalletPersister { /// The marker each identity's position record was last stored with in /// this session (`PrivateContactRequestsPosition::private_payments_unresolved`): /// a record whose marker, its identity's and the journal's together, - /// differs from it is stored again. Absent when none was stored, or the - /// last store or its flush failed. Shared by the clones the wallet and - /// the coordinator hold. In memory only. - stored_private_markers: Arc>>, + /// differs from it is stored again. Absent when none was stored, when the + /// last store or its flush failed, or once any flush of the wallet id's + /// records failed for good since it was noted + /// ([`StoredPrivateMarkers`]). Shared by the clones the wallet and the + /// coordinator hold. In memory only. + stored_private_markers: Arc>, /// What is known of the flushes of the records stored under this /// wallet's id: how many are in progress, and how many failed for good, /// the backend dropping what it buffered. Every flush of the id is @@ -166,6 +168,20 @@ struct DischargedPrivateEntries { journaled: BTreeMap<[u8; 32], (Identifier, Identifier)>, } +/// The marker each identity's position record was last stored with +/// ([`WalletPersister::note_private_marker_stored`]), with how many flushes of +/// the wallet id's records had failed for good when they were noted. One more +/// since dropped what the backend had buffered, whoever made that flush, and +/// a store noted here may still have been in it: so none is known stored any +/// longer, and all are forgotten ([`WalletPersister::stored_markers`]). +#[derive(Default)] +struct StoredPrivateMarkers { + markers: BTreeMap, + /// The ledger's count of flushes failed for good that `markers` were + /// noted under ([`FlushLedger::lost`]). + losses: u64, +} + /// Why records stored for a wallet are not known to be durable /// ([`WalletPersister::store_durably`], [`WalletPersister::flush_durably`]). #[derive(Debug, thiserror::Error)] @@ -287,25 +303,36 @@ impl WalletPersister { /// `marker`, or, with `None`, that whether it was is not known: its last /// store or flush failed ([`Self::private_marker_stored`]). pub(crate) fn note_private_marker_stored(&self, owner: Identifier, marker: Option) { + let mut stored = self.stored_markers(); + match marker { + Some(marker) => stored.markers.insert(owner, marker), + None => stored.markers.remove(&owner), + }; + } + + /// The markers noted as stored, forgotten first when a flush of the + /// wallet id's records failed for good since they were noted: this + /// handle's flush, another handle's or the manager's, as the ledger + /// counts them all. The next comparison then stores each marker again. + fn stored_markers(&self) -> std::sync::MutexGuard<'_, StoredPrivateMarkers> { let mut stored = self .stored_private_markers .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); - match marker { - Some(marker) => stored.insert(owner, marker), - None => stored.remove(&owner), - }; + let losses = self.flush_ledger.lost(); + if stored.losses != losses { + stored.markers.clear(); + stored.losses = losses; + } + stored } /// The marker `owner`'s position record was last stored with in this - /// session, if one was and its store did not fail since + /// session, if one was, its store did not fail since, and no flush of the + /// wallet id's records failed for good since /// ([`Self::note_private_marker_stored`]). pub(crate) fn private_marker_stored(&self, owner: &Identifier) -> Option { - self.stored_private_markers - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .get(owner) - .copied() + self.stored_markers().markers.get(owner).copied() } /// Note the commitments of private contact request entries a private sync @@ -384,7 +411,8 @@ impl WalletPersister { // rewound: a read again would find the requests already // tracked, store nothing, and note them again over records // that never reached disk. The markers stored into that - // buffer are forgotten, so the next comparison stores them + // buffer are forgotten, as after any flush the ledger counts + // as failed for good, so the next comparison stores them // again (`Self::private_marker_stored`). tracing::error!( %error, @@ -392,10 +420,6 @@ impl WalletPersister { "private requests read back are not durably recorded, and the backend \ dropped what it buffered; their journal records stay" ); - self.stored_private_markers - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .clear(); } Err(NotDurable::Lost) => tracing::warn!( dropped = pending.len(), @@ -1013,6 +1037,79 @@ mod tests { } } + /// A marker noted as stored is forgotten once a flush of the wallet id's + /// records fails for good, whoever made it: the store it was noted for + /// may still have been in the buffer that flush dropped, so the next + /// comparison stores the marker again. A flush that fails keeping what it + /// took forgets nothing, and a marker noted after the loss is kept. + #[test] + fn should_forget_stored_markers_once_any_flush_of_the_wallet_id_fails_for_good() { + /// A buffering backend whose flush fails, for good or not. + struct Failing { + for_good: AtomicBool, + } + impl PlatformWalletPersistence for Failing { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Err(if self.for_good.load(Ordering::SeqCst) { + PersistenceError::backend("the database is corrupt") + } else { + PersistenceError::backend_with_kind( + PersistenceErrorKind::Transient, + "the database is locked", + ) + }) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + let backend = Arc::new(Failing { + for_good: AtomicBool::new(false), + }); + let ledger = Arc::new(FlushLedger::default()); + let wallet_id: WalletId = [3; 32]; + let persister = WalletPersister::with_flush_ledger( + wallet_id, + Arc::clone(&backend) as _, + Arc::clone(&ledger), + ); + // A handle of a wallet removed, or created since, under the id. + let other = + WalletPersister::with_flush_ledger(wallet_id, Arc::clone(&backend) as _, ledger); + let owner = Identifier::from([9; 32]); + + persister.note_private_marker_stored(owner, Some(true)); + assert!(other.flush().is_err_and(|error| error.is_transient())); + assert_eq!( + persister.private_marker_stored(&owner), + Some(true), + "a flush that kept what it took dropped no store" + ); + + backend.for_good.store(true, Ordering::SeqCst); + assert!(other.flush().is_err_and(|error| !error.is_transient())); + assert_eq!( + persister.private_marker_stored(&owner), + None, + "not known stored once the buffer was dropped" + ); + + persister.note_private_marker_stored(owner, Some(false)); + assert_eq!( + persister.private_marker_stored(&owner), + Some(false), + "noted after the loss" + ); + } + /// Buffers what is stored. Its first flush finds the buffer empty: /// another flush, made through the same persister as one from another /// thread, took it before, and fails transiently while the first runs, From 76c5630e5f7b869ec10a3db1b0795d0ddf385fc3 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 14 Nov 2026 15:52:25 +0700 Subject: [PATCH 09/14] fix(platform-wallet): keep a newer tracked request when an older one is recorded as sent, and note a marker only when no flush dropped its record Two gaps a review of 9aa9a1bf2a found, and two smaller points. `record_sent_contact_request` recorded the request it was given whatever the wallet tracked for the pair. A send looks the request up on Platform, or broadcasts it, with no lock held, and a sweep may read in a newer request meanwhile, one another device of the identity sent. Recording the older one over it would have the next rotation build from a reference already used. An older request is now passed over (`predates`, as the settle and the sweep already do): the tracked request is answered as the request sent, and its account is left to the sweep that read it in. The previous commit made the persister forget the markers it remembers as stored once a flush of the wallet id's records fails for good. One window was left: a position record is stored and its marker is noted afterwards, so a flush that failed for good in between moved the ledger's count before the note and the marker looked stored although its record may have been dropped. `WalletPersister:: store_private_marker_record` reads the count before the store and notes the marker only when the count is still the same, compared under the lock the marker is noted in. `store_private_position` goes through it. `FlushLedger`, `FlushLedgers` and `WalletPersister::with_flush_ledger` are now visible inside the crate only: nothing outside uses them, and a ledger made elsewhere would count some of a wallet id's flushes apart from the rest. The Kotlin doc of `publicContactRequestUnconfirmed` said the held bytes are always broadcast again; it now says when. Tests: should_keep_a_newer_tracked_request_when_an_older_one_is_recorded_as_sent, should_not_note_a_marker_whose_store_another_flush_dropped. Each fails with its fix taken out. Co-Authored-By: Claude Fable 5.2 --- .../dashfoundation/dashsdk/tokens/Dashpay.kt | 4 +- .../src/changeset/core_bridge.rs | 2 +- .../src/changeset/flush_ledger.rs | 6 +- .../rs-platform-wallet/src/changeset/mod.rs | 4 +- .../identity/network/contact_requests.rs | 27 ++++- .../network/private_contact_requests.rs | 104 ++++++++++++++++-- .../src/wallet/persister.rs | 99 ++++++++++++++++- 7 files changed, 225 insertions(+), 21 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/tokens/Dashpay.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/tokens/Dashpay.kt index 4060881efa7..ba75d596a6b 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/tokens/Dashpay.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/tokens/Dashpay.kt @@ -523,7 +523,9 @@ class Dashpay internal constructor(private val walletHandle: Long, * a host shows the pair as unconfirmed and offers neither. A DashPay sync * settles it: a request found is recorded as sent, a nonce proved used * frees the pair, and otherwise the same signed bytes are broadcast - * again. Read it beside [privateRepliesPending]. Empty when the identity + * again only while the wallet's tracked state still allows a public + * request to the contact; a private send never broadcasts them again. + * Read it beside [privateRepliesPending]. Empty when the identity * isn't managed. * ← Swift `ManagedPlatformWallet.publicContactRequestUnconfirmed(senderId:recipientId:)` * (packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PrivateContactRequests.swift). diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index f6a4a019175..7102e2b83f4 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -326,7 +326,7 @@ pub struct DurableCursorState { /// cursor entry it outlives its wallet, so a removed wallet's handles, /// its same-id successor's and the manager count every flush of the id /// in one place ([`FlushLedger`](crate::changeset::flush_ledger::FlushLedger)). - pub flush_ledgers: FlushLedgers, + pub(crate) flush_ledgers: FlushLedgers, } impl DurableCursorState { diff --git a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs index 6054a9be458..7308c0bf71d 100644 --- a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs +++ b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs @@ -18,7 +18,7 @@ use crate::wallet::platform_wallet::WalletId; /// same-id successor's alike ([`FlushLedgers`]). Every flush of the wallet's /// records goes through [`Self::flush`]. #[derive(Debug, Default)] -pub struct FlushLedger { +pub(crate) struct FlushLedger { /// How many flushes are in progress: each is counted from before it calls /// the backend until its outcome is recorded in `failed` and `lost`, so /// while none is, they tell of every flush that failed. One whose backend @@ -116,11 +116,11 @@ impl FlushLedger { /// The [`FlushLedger`] of each wallet id, for the wallets of one manager. An /// entry outlives its wallet, as the backend's buffer under the id does. #[derive(Debug, Default)] -pub struct FlushLedgers(Mutex>>); +pub(crate) struct FlushLedgers(Mutex>>); impl FlushLedgers { /// The ledger of `wallet_id`'s records, made on first use. - pub fn of(&self, wallet_id: WalletId) -> Arc { + pub(crate) fn of(&self, wallet_id: WalletId) -> Arc { Arc::clone( self.0 .lock() diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 8cd85d343e6..b1a4ada2db1 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -14,7 +14,7 @@ pub mod client_start_state; pub mod client_wallet_start_state; pub mod core_bridge; pub mod dashpay_backfill; -pub mod flush_ledger; +pub(crate) mod flush_ledger; pub mod identity_manager_start_state; pub mod identity_scan_state; pub mod merge; @@ -55,7 +55,7 @@ pub use core_bridge::{ pub use dashpay_backfill::{ persists_backfill_coverage, DashPayBackfillCoveredContact, DashPayBackfillRecord, }; -pub use flush_ledger::{FlushLedger, FlushLedgers}; +pub(crate) use flush_ledger::FlushLedger; pub use identity_manager_start_state::IdentityManagerStartState; pub use identity_scan_state::IdentityScanStateEntry; pub use merge::Merge; diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs index e92674f1715..f4924f561e1 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs @@ -1298,7 +1298,16 @@ impl DashPayView<'_, B> { /// next sync, which registers it. Reporting it as a failure would have /// the caller send again, and a request built past the one just recorded /// is a second request. - async fn record_sent_contact_request( + /// + /// A request older than the one the wallet tracks for the pair is not + /// recorded over it ([`predates`]). The lookup that found it, or the + /// broadcast that confirmed it, ran with no lock held, and a sweep may + /// have read in a newer request meanwhile, one another device of the + /// identity sent. Recording the older one over it would have the next + /// rotation build from a reference already used. The tracked request is + /// then answered as the request sent, and its account is left to the + /// sweep that read it in. + pub(super) async fn record_sent_contact_request( &self, sender: &Identifier, recipient: &Identifier, @@ -1313,6 +1322,22 @@ impl DashPayView<'_, B> { .identity_manager .managed_identity_mut(sender) .ok_or(PlatformWalletError::IdentityNotFound(*sender))?; + let newer = managed + .dashpay() + .outgoing_request(recipient) + .filter(|tracked| predates(&request, tracked)) + .cloned(); + if let Some(newer) = newer { + tracing::info!( + sender = %sender, + recipient = %recipient, + account_reference = request.account_reference, + tracked_reference = newer.account_reference, + "a newer contact request is tracked for the pair; the older one is not \ + recorded over it" + ); + return Ok(newer); + } track_sent_contact_request(managed, &request, &self.persister)?; } self.register_sent_request_account(sender, recipient, account_index, contact_xpub) diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs index 02f3bb51cc5..6660b53ea83 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs @@ -769,17 +769,17 @@ fn store_private_position( changeset .private_contact_requests_positions .insert(owner, position); - match persister.store(changeset.into()) { - Ok(()) => { - persister.note_private_marker_stored(owner, Some(position.private_payments_unresolved)) - } - Err(error) => { - persister.note_private_marker_stored(owner, None); - tracing::warn!( - owner = %owner, position = position.position, %error, - "private read position not stored; the next launch reads from the last one stored" - ); - } + // Noted as stored only when no flush of the wallet's records failed for + // good since before the store: one that did may have dropped the record. + if let Err(error) = persister.store_private_marker_record( + owner, + position.private_payments_unresolved, + changeset.into(), + ) { + tracing::warn!( + owner = %owner, position = position.position, %error, + "private read position not stored; the next launch reads from the last one stored" + ); } } @@ -6970,6 +6970,88 @@ mod tests { .await; } + /// A request recorded as sent is not recorded over a newer one the wallet + /// tracks for the pair, as a sweep read in one that another device of the + /// identity sent while this one was being looked up or broadcast: the + /// newer request stays tracked and is answered as the request sent. One + /// that is not older is recorded. + #[tokio::test] + async fn should_keep_a_newer_tracked_request_when_an_older_one_is_recorded_as_sent() { + use platform_encryption::ACCOUNT_REFERENCE_VERSION_SHIFT; + + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let manager = manager_on(Arc::new(NoPersistence)).await; + let wallet = create_holding(&manager, ALICE_PHRASE, &alice).await; + let xpub = alice_crypto + .receiving_xpub(&"m/9'/1'/15'/0'".parse::().expect("a path")) + .await + .expect("an xpub"); + let older = 7; + let newer = older + (1 << ACCOUNT_REFERENCE_VERSION_SHIFT); + let newest = older + (2 << ACCOUNT_REFERENCE_VERSION_SHIFT); + // As a sweep reads in a rotation past the request being recorded. + { + let iw = wallet.identity(); + let mut wm = iw.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet.wallet_id()) + .expect("wallet info") + .identity_manager + .managed_identity_mut(&alice.id()) + .expect("the sender") + .add_sent_contact_request(public_request(&alice, &bob, newer), &iw.persister) + .expect("recorded"); + } + let tracked = |wallet: Arc| async move { + managed(&wallet, ALICE, |managed| { + managed + .dashpay() + .outgoing_request(&Identifier::from(BOB)) + .map(|request| request.account_reference) + }) + .await + }; + let view = wallet.identity().dashpay(); + + let answered = view + .record_sent_contact_request( + &alice.id(), + &bob.id(), + public_request(&alice, &bob, older), + 0, + xpub, + ) + .await + .expect("answered"); + assert_eq!( + answered.account_reference, newer, + "the tracked request is the request sent" + ); + assert_eq!( + tracked(Arc::clone(&wallet)).await, + Some(newer), + "the newer request stays tracked" + ); + + let answered = view + .record_sent_contact_request( + &alice.id(), + &bob.id(), + public_request(&alice, &bob, newest), + 0, + xpub, + ) + .await + .expect("recorded"); + assert_eq!(answered.account_reference, newest); + assert_eq!( + tracked(wallet).await, + Some(newest), + "one that is not older is recorded" + ); + } + /// Every flush of the records under a wallet id is counted in one place, /// whichever handle makes it: one through the handle of a wallet removed /// since, failing for good, drops what its same-id successor had diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index 20fd27f5300..e30ca11ce18 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -222,8 +222,10 @@ impl WalletPersister { /// A handle that counts its flushes in `flush_ledger`, the one every /// flush of `wallet_id`'s records on `inner` is counted in - /// ([`FlushLedgers`](crate::changeset::FlushLedgers)). - pub fn with_flush_ledger( + /// ([`FlushLedgers`](crate::changeset::flush_ledger::FlushLedgers)). Inside the crate + /// only: a ledger made elsewhere would count some of the id's flushes + /// apart from the rest. + pub(crate) fn with_flush_ledger( wallet_id: WalletId, inner: Arc, flush_ledger: Arc, @@ -310,6 +312,33 @@ impl WalletPersister { }; } + /// Store `changeset`, which carries `owner`'s position record with the + /// marker `marker`, and note the marker as stored + /// ([`Self::note_private_marker_stored`]): only when the store succeeded + /// and no flush of the wallet id's records failed for good since before + /// it. One that did may have dropped the record from the buffer, so the + /// marker is then not known stored and the next comparison stores the + /// record again. The count is compared under the lock the marker is + /// noted in, so a loss recorded later forgets it there + /// ([`Self::stored_markers`]). + pub(crate) fn store_private_marker_record( + &self, + owner: Identifier, + marker: bool, + changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + let losses_before = self.flush_ledger.lost(); + let outcome = self.store(changeset); + let mut stored = self.stored_markers(); + if outcome.is_ok() && stored.losses == losses_before { + stored.markers.insert(owner, marker); + } else { + stored.markers.remove(&owner); + } + drop(stored); + outcome + } + /// The markers noted as stored, forgotten first when a flush of the /// wallet id's records failed for good since they were noted: this /// handle's flush, another handle's or the manager's, as the ledger @@ -1110,6 +1139,72 @@ mod tests { ); } + /// A marker is not noted as stored when a flush of the wallet id's + /// records failed for good between its record's store and the note: the + /// record may have been in the buffer that flush dropped, so the next + /// comparison stores it again. One whose store nothing overtook is noted. + #[test] + fn should_not_note_a_marker_whose_store_another_flush_dropped() { + /// Buffers what is stored, and once `other` is set every store is + /// overtaken: a flush through that handle, as one from another + /// thread, takes the buffer and fails for good before the store + /// returns. + struct Overtaken { + other: OnceLock, + fails_for_good: AtomicBool, + } + impl PlatformWalletPersistence for Overtaken { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + if let Some(other) = self.other.get() { + self.fails_for_good.store(true, Ordering::SeqCst); + let overtaking = other.flush(); + self.fails_for_good.store(false, Ordering::SeqCst); + assert!(overtaking.is_err_and(|error| !error.is_transient())); + } + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + if self.fails_for_good.load(Ordering::SeqCst) { + return Err(PersistenceError::backend("the database is corrupt")); + } + Ok(()) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + let backend = Arc::new(Overtaken { + other: OnceLock::new(), + fails_for_good: AtomicBool::new(false), + }); + let persister = WalletPersister::new([0; 32], Arc::clone(&backend) as _); + let owner = Identifier::from([9; 32]); + + persister + .store_private_marker_record(owner, true, PlatformWalletChangeSet::default()) + .expect("stored"); + assert_eq!( + persister.private_marker_stored(&owner), + Some(true), + "nothing overtook its store" + ); + + let _ = backend.other.set(persister.clone()); + persister + .store_private_marker_record(owner, false, PlatformWalletChangeSet::default()) + .expect("the store itself succeeds"); + assert_eq!( + persister.private_marker_stored(&owner), + None, + "its record may have been in the buffer the other flush dropped" + ); + } + /// Buffers what is stored. Its first flush finds the buffer empty: /// another flush, made through the same persister as one from another /// thread, took it before, and fails transiently while the first runs, From 67945bb4b58d810d8ee1ac3039cfc7f43e9a6b6e Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 15 Nov 2026 14:46:47 +0700 Subject: [PATCH 10/14] refactor(platform-wallet): keep the order of a pair's sent requests in the identity state A review of 76c5630e5f noted that "an older request is not recorded over a newer one" was checked by each caller: the sweep's ingest, the settle of a held request, and the send's record. The mutation itself, `ManagedIdentity::add_sent_contact_request`, took any differing account reference as a rotation, so a caller without the check could still replace a newer tracked request. The rule now lives where the request is recorded. `ManagedIdentity::track_sent_contact_request` passes over a request older than the one tracked for its recipient (`predates`, moved here with `freshness`) and answers what became of the request: recorded, already tracked, or superseded. `add_sent_contact_request` goes through it and keeps its signature. The three callers read the answer and no longer compare for themselves. Also says, where the manager is made and where the flush ledgers live, that a storage backend serves one manager: the order of durable cursor writes and the count of flushes are kept per manager, so a second manager over the same backend instance, or a wallet handle of an earlier manager still storing through it, is not supported. The README says the same. Test: should_pass_over_a_sent_request_older_than_the_one_tracked, which fails with the check taken out, as the two caller-level tests do. Co-Authored-By: Claude Fable 5.2 --- packages/rs-platform-wallet/README.md | 2 +- .../src/changeset/core_bridge.rs | 5 + .../rs-platform-wallet/src/manager/mod.rs | 10 ++ .../identity/network/contact_requests.rs | 94 ++++-------- .../managed_identity/contact_requests.rs | 144 +++++++++++++++++- .../identity/state/managed_identity/mod.rs | 1 + 6 files changed, 187 insertions(+), 69 deletions(-) diff --git a/packages/rs-platform-wallet/README.md b/packages/rs-platform-wallet/README.md index c79208c8253..7981685cc5a 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -123,7 +123,7 @@ Startup errors distinguish damaged recovery metadata or undecodable signed bytes ## Private contact request payments after a restart -Before a private contact request's payment can reach the network, the shielded store saves a record of it in its private payment journal: the sender and recipient identities, the request's account reference, the commitment of the entry it appends, the SHA-256 of the whole entry, the position the sender's read of DashPay's private store rested at when it paid, and the nullifiers its spend consumes. The file store writes the journal, and the payment's redrive record, in one transaction on a connection at `synchronous=FULL` with `fullfsync` (on Apple platforms a plain sync does not flush the drive's own cache), so the record survives a crash or a power loss; a record that cannot be saved refuses the payment before anything is broadcast. The journal is kept apart from the spend's notes and redrive record: no note release, redrive removal, undecodable transition, account rebind or purge removes it. A record is removed only when its entry is read back from DashPay's private store and recorded durably, or when the transition provably never left the wallet (refused before anything was sent: no address to send it to, or the SDK's own refusal). An endpoint's answer, a consensus verdict included, proves nothing: no proof or quorum signature backs it, and an endpoint that kept or forwarded the signed transition can answer with any verdict. A record is also discharged once its payment is proven expired, judged against Platform's recorded anchors fetched before the shielded sync pass and proved at a height H: the spend's anchor, which the record keeps with the proved height of the set it was read from, was read at or below H and is absent from the set at H, the pass's note scan covers H, and its entry is absent from DashPay's private store read from the lowest position the records being judged kept through its end, with every page proved at or above H. An entry counts as the record's only when its whole bytes hash to the hash the record keeps: one that only starts with the commitment, which anyone who saw the commitment can append, does not hold the record. A record journaled before the hash and the position were kept matches by its commitment and is read for from the store's start. Whether its inputs are spent does not matter: the transition appends its entry in the block that spends them. A payment that never lands therefore frees its pair once its anchor ages out (Platform keeps an anchor for `shielded_anchor_retention_blocks`, 1000 blocks, and prunes every `shielded_anchor_pruning_interval`, 100 blocks). A restoration that finds a record whose request the sender's tracked request already records or supersedes, as a private sync read it back in a process that ended before discharging it, discharges it. Once a flush of the records under the wallet's id failed for good, the backend dropping what it buffered, no record is discharged on the strength of a tracked request for as long as that wallet stays loaded. Every flush of the id counts, whoever made it: the wallet's own, one through a handle of a wallet removed under the same id since, or the manager's before it publishes a wallet. A wallet loaded or created under the id after the failure tracks what it read from disk and is not held by it. While the hold stands, no read-back is noted, a read of the entry again included (it finds the request tracked in memory and stores nothing), and a restoration leaves the records it finds settled. A restoration that finds a record whose request its sender does not track rewinds the sender's read to the position the record kept (the store's start for a record that kept none), so the entry is read again, its request recorded, and the record discharged once that is durable. Nothing else, a relay's refusal or a missing redrive record included, discharges a record. +Before a private contact request's payment can reach the network, the shielded store saves a record of it in its private payment journal: the sender and recipient identities, the request's account reference, the commitment of the entry it appends, the SHA-256 of the whole entry, the position the sender's read of DashPay's private store rested at when it paid, and the nullifiers its spend consumes. The file store writes the journal, and the payment's redrive record, in one transaction on a connection at `synchronous=FULL` with `fullfsync` (on Apple platforms a plain sync does not flush the drive's own cache), so the record survives a crash or a power loss; a record that cannot be saved refuses the payment before anything is broadcast. The journal is kept apart from the spend's notes and redrive record: no note release, redrive removal, undecodable transition, account rebind or purge removes it. A record is removed only when its entry is read back from DashPay's private store and recorded durably, or when the transition provably never left the wallet (refused before anything was sent: no address to send it to, or the SDK's own refusal). An endpoint's answer, a consensus verdict included, proves nothing: no proof or quorum signature backs it, and an endpoint that kept or forwarded the signed transition can answer with any verdict. A record is also discharged once its payment is proven expired, judged against Platform's recorded anchors fetched before the shielded sync pass and proved at a height H: the spend's anchor, which the record keeps with the proved height of the set it was read from, was read at or below H and is absent from the set at H, the pass's note scan covers H, and its entry is absent from DashPay's private store read from the lowest position the records being judged kept through its end, with every page proved at or above H. An entry counts as the record's only when its whole bytes hash to the hash the record keeps: one that only starts with the commitment, which anyone who saw the commitment can append, does not hold the record. A record journaled before the hash and the position were kept matches by its commitment and is read for from the store's start. Whether its inputs are spent does not matter: the transition appends its entry in the block that spends them. A payment that never lands therefore frees its pair once its anchor ages out (Platform keeps an anchor for `shielded_anchor_retention_blocks`, 1000 blocks, and prunes every `shielded_anchor_pruning_interval`, 100 blocks). A restoration that finds a record whose request the sender's tracked request already records or supersedes, as a private sync read it back in a process that ended before discharging it, discharges it. Once a flush of the records under the wallet's id failed for good, the backend dropping what it buffered, no record is discharged on the strength of a tracked request for as long as that wallet stays loaded. Every flush of the id counts, whoever made it: the wallet's own, one through a handle of a wallet removed under the same id since, or the manager's before it publishes a wallet. The count is kept by the manager, so a storage backend serves one manager: a host that replaces its manager gives the new one its own backend instance, or stops using the earlier manager's wallet handles first. A wallet loaded or created under the id after the failure tracks what it read from disk and is not held by it. While the hold stands, no read-back is noted, a read of the entry again included (it finds the request tracked in memory and stores nothing), and a restoration leaves the records it finds settled. A restoration that finds a record whose request its sender does not track rewinds the sender's read to the position the record kept (the store's start for a record that kept none), so the entry is read again, its request recorded, and the record discharged once that is durable. Nothing else, a relay's refusal or a missing redrive record included, discharges a record. Until a payment's reservation is restored from the journal, nothing shows that the pair is taken: the request's entry is not in DashPay's store until the payment executes. So a wallet sends and accepts no contact request, public or private, while such a payment may be unrestored, and returns `PrivateContactPaymentsNotRestored` (FFI code 72) with nothing read, signed or broadcast. The in-kind accept is held the same way. These hold it: diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index 7102e2b83f4..e0421e52e0d 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -326,6 +326,11 @@ pub struct DurableCursorState { /// cursor entry it outlives its wallet, so a removed wallet's handles, /// its same-id successor's and the manager count every flush of the id /// in one place ([`FlushLedger`](crate::changeset::flush_ledger::FlushLedger)). + /// + /// One place per manager, as the cursors above are: a backend serves one + /// manager (see `PlatformWalletManager::new`). A second manager over the + /// same backend instance would order cursor writes, and count flushes, + /// apart from this one. pub(crate) flush_ledgers: FlushLedgers, } diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index c1b72255888..1d1f4170dde 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -573,6 +573,16 @@ impl PlatformWalletManager

{ /// `app_handler` receives all SPV and platform events by reference. /// Internally, a `LockNotifyHandler` is also registered to wake /// `AssetLockManager` async waiters on lock events. + /// + /// `persister` serves this manager alone, for as long as the manager or + /// a wallet handle it gave out is in use. What the manager knows of the + /// backend per wallet id (the order of durable cursor writes, and the + /// count of flushes that failed, [`DurableCursors`]) is kept per manager: + /// a second manager over the same backend instance, or a wallet handle + /// of an earlier manager still storing through it, would write and flush + /// the same buffered records without either seeing the other's. A host + /// that replaces its manager gives the new one its own backend instance, + /// as the FFI does, or stops using the earlier manager's handles first. pub fn new( sdk: Arc, persister: Arc

, diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs index f4924f561e1..e100fb1159f 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs @@ -19,6 +19,8 @@ use super::*; use crate::broadcaster::TransactionBroadcaster; use crate::error::PlatformWalletError; use crate::wallet::core::WalletGeneration; +pub(super) use crate::wallet::identity::state::managed_identity::predates; +use crate::wallet::identity::state::managed_identity::{freshness, SentContactRequestOutcome}; use crate::wallet::identity::types::dashpay::contact_request::ContactRequest; use crate::wallet::identity::types::dashpay::established_contact::EstablishedContact; use crate::wallet::identity::ManagedIdentity; @@ -700,14 +702,17 @@ fn release_public_claim( } } -/// Track `request` as sent by `managed`, stored through `persister`. +/// Track `request` as sent by `managed`, stored through `persister`, and +/// answer what became of it: `managed` passes over a request older than the +/// one it tracks for the recipient +/// (`ManagedIdentity::track_sent_contact_request`). fn track_sent_contact_request( managed: &mut ManagedIdentity, request: &ContactRequest, persister: &WalletPersister, -) -> Result<(), PlatformWalletError> { +) -> Result { managed - .add_sent_contact_request(request.clone(), persister) + .track_sent_contact_request(request.clone(), persister) .map_err(|e| { PlatformWalletError::Persistence(format!("sent contact request not persisted: {e}")) }) @@ -1300,7 +1305,7 @@ impl DashPayView<'_, B> { /// is a second request. /// /// A request older than the one the wallet tracks for the pair is not - /// recorded over it ([`predates`]). The lookup that found it, or the + /// recorded over it (the identity passes it over, [`predates`]). The lookup that found it, or the /// broadcast that confirmed it, ran with no lock held, and a sweep may /// have read in a newer request meanwhile, one another device of the /// identity sent. Recording the older one over it would have the next @@ -1322,11 +1327,10 @@ impl DashPayView<'_, B> { .identity_manager .managed_identity_mut(sender) .ok_or(PlatformWalletError::IdentityNotFound(*sender))?; - let newer = managed - .dashpay() - .outgoing_request(recipient) - .filter(|tracked| predates(&request, tracked)) - .cloned(); + let outcome = track_sent_contact_request(managed, &request, &self.persister)?; + let newer = (outcome == SentContactRequestOutcome::Superseded) + .then(|| managed.dashpay().outgoing_request(recipient).cloned()) + .flatten(); if let Some(newer) = newer { tracing::info!( sender = %sender, @@ -1338,7 +1342,6 @@ impl DashPayView<'_, B> { ); return Ok(newer); } - track_sent_contact_request(managed, &request, &self.persister)?; } self.register_sent_request_account(sender, recipient, account_index, contact_xpub) .await; @@ -1390,15 +1393,11 @@ impl DashPayView<'_, B> { .is_some_and(|tracked| tracked.account_reference == request.account_reference); return Ok(tracked.then_some(request)); } - let superseded = dashpay - .outgoing_request(recipient) - .is_some_and(|tracked| predates(&request, tracked)); - if superseded { - managed.settle_unconfirmed_public_request(recipient, attempt); + let outcome = track_sent_contact_request(managed, &request, &self.persister)?; + managed.settle_unconfirmed_public_request(recipient, attempt); + if outcome == SentContactRequestOutcome::Superseded { return Ok(None); } - track_sent_contact_request(managed, &request, &self.persister)?; - managed.settle_unconfirmed_public_request(recipient, attempt); } self.register_sent_request_account(sender, recipient, account_index, contact_xpub) .await; @@ -2182,14 +2181,6 @@ fn record_and_collapse_sent_requests( newest_sent_per_recipient(requests) } -/// Whether `candidate` is older than `tracked`, a request between the same -/// pair: by its account reference's version, which a rotation bumps, then by -/// its creation time. A replay of an older request appended again later has a -/// newer time but an older version. -pub(super) fn predates(candidate: &ContactRequest, tracked: &ContactRequest) -> bool { - freshness(candidate) < freshness(tracked) -} - /// Whether `tracked`, a request between one pair, is an unconfirmed request /// carrying `account_reference` or supersedes it in the order [`predates`] /// reads: the same reference, or a later version. The unconfirmed request is @@ -2201,19 +2192,6 @@ pub(super) fn records_or_supersedes(tracked: &ContactRequest, account_reference: < platform_encryption::account_reference_version(tracked.account_reference) } -/// How fresh a request between one pair is, for collapsing a sweep's requests -/// and for passing over older ones alike: by its account reference's version, -/// which a rotation bumps, then by its creation time, then by the reference -/// itself so that equal times order deterministically. A replay of an older -/// request appended again later has a newer time but an older version. -fn freshness(request: &ContactRequest) -> (u32, u64, u32) { - ( - platform_encryption::account_reference_version(request.account_reference), - request.created_at, - request.account_reference, - ) -} - /// Ingest one identity's collapsed **received** contact requests into local /// state, returning whether every write reached disk. /// @@ -2320,32 +2298,22 @@ pub(super) fn ingest_sent_requests( newest_by_recipient: std::collections::BTreeMap, recorded: &mut Vec, ) -> bool { - for (recipient_id, contact_request) in newest_by_recipient { + for contact_request in newest_by_recipient.into_values() { // An older request than the tracked one is a replay, as on the - // received side: superseding with it would re-derive the next rotation - // from a retired reference. - let (replay, tracked) = - managed - .dashpay() - .outgoing_request(&recipient_id) - .map_or((false, false), |tracked| { - ( - predates(&contact_request, tracked), - tracked.account_reference == contact_request.account_reference, - ) - }); - if replay { - continue; - } - if let Err(e) = managed.add_sent_contact_request(contact_request.clone(), persister) { - tracing::error!( - owner = %identity_id, error = %e, - "sent-request ingest persist failed; leaving sent cursor for retry" - ); - return false; - } - if !tracked { - recorded.push(contact_request); + // received side, and the identity passes it over: superseding with it + // would re-derive the next rotation from a retired reference. + match managed.track_sent_contact_request(contact_request.clone(), persister) { + Ok(SentContactRequestOutcome::Recorded) => recorded.push(contact_request), + Ok( + SentContactRequestOutcome::AlreadyTracked | SentContactRequestOutcome::Superseded, + ) => {} + Err(e) => { + tracing::error!( + owner = %identity_id, error = %e, + "sent-request ingest persist failed; leaving sent cursor for retry" + ); + return false; + } } } true diff --git a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/contact_requests.rs index 6d4590717d6..99f2e255ae5 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/contact_requests.rs @@ -35,6 +35,41 @@ fn next_contact_request_attempt() -> u64 { NEXT_CONTACT_REQUEST_ATTEMPT.fetch_add(1, Ordering::Relaxed) } +/// What became of a request handed to +/// [`ManagedIdentity::track_sent_contact_request`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum SentContactRequestOutcome { + /// Recorded: it is the request tracked for its recipient now. + Recorded, + /// Already the request tracked for its recipient, by its account + /// reference: nothing was written. + AlreadyTracked, + /// Older than the request tracked for its recipient ([`predates`]), + /// which stays the one tracked: nothing was written. + Superseded, +} + +/// Whether `candidate` is older than `tracked`, a request between the same +/// pair: by its account reference's version, which a rotation bumps, then by +/// its creation time. A replay of an older request appended again later has a +/// newer time but an older version. +pub(crate) fn predates(candidate: &ContactRequest, tracked: &ContactRequest) -> bool { + freshness(candidate) < freshness(tracked) +} + +/// How fresh a request between one pair is, for collapsing a sweep's requests +/// and for passing over older ones alike: by its account reference's version, +/// which a rotation bumps, then by its creation time, then by the reference +/// itself so that equal times order deterministically. A replay of an older +/// request appended again later has a newer time but an older version. +pub(crate) fn freshness(request: &ContactRequest) -> (u32, u64, u32) { + ( + account_reference_version(request.account_reference), + request.created_at, + request.account_reference, + ) +} + impl ManagedIdentity { /// The masked `accountReference` of the most recent request WE sent /// to `recipient`, or `None` if we've never sent one. @@ -97,16 +132,46 @@ impl ManagedIdentity { request: ContactRequest, persister: &WalletPersister, ) -> Result<(), crate::changeset::PersistenceError> { + self.track_sent_contact_request(request, persister) + .map(|_| ()) + } + + /// [`Self::add_sent_contact_request`], answering what became of the + /// request. + /// + /// **Freshness.** The order of a pair's sent requests is kept here, for + /// every caller: a request older than the one tracked for its recipient + /// ([`predates`]) is no rotation, whoever hands it in (a sweep replaying + /// an old document, a send that looked its request up before a newer one + /// was read in, a settle of a held request). It is passed over and the + /// tracked request stays: superseding with it would re-derive the next + /// rotation from a retired reference. + pub(crate) fn track_sent_contact_request( + &mut self, + request: ContactRequest, + persister: &WalletPersister, + ) -> Result { let owner_id = self.id(); let recipient_id = request.recipient_id; + if self + .dashpay + .outgoing_request(&recipient_id) + .is_some_and(|tracked| { + tracked.account_reference != request.account_reference + && predates(&request, tracked) + }) + { + return Ok(SentContactRequestOutcome::Superseded); + } + // Sent-side guard / rotation supersede: already established. if let Some(existing) = self.dashpay.established_contacts.get(&recipient_id) { // Same outgoing reference → a re-ingest of a doc we already // track carries no new information; re-establishing would wipe // user metadata, so it's a no-op. if existing.outgoing_request.account_reference == request.account_reference { - return Ok(()); + return Ok(SentContactRequestOutcome::AlreadyTracked); } // Different outgoing reference → a rotation re-send. Advance // `outgoing_request` so the next rotation reads the fresh @@ -135,7 +200,7 @@ impl ManagedIdentity { // guard. Incoming-side changes never touch the guard: the contact // must not be able to force rescans of our receiving account. self.dashpay.rescan_triggered.remove(&recipient_id); - return Ok(()); + return Ok(SentContactRequestOutcome::Recorded); } // Already tracked as a pending sent request. Same outgoing // reference → no-op (no phantom row, no redundant changeset @@ -151,7 +216,7 @@ impl ManagedIdentity { // the established branch and for the same retry reason. if let Some(existing) = self.dashpay.sent_contact_requests.get(&recipient_id) { if existing.account_reference == request.account_reference { - return Ok(()); + return Ok(SentContactRequestOutcome::AlreadyTracked); } let mut cs = ContactChangeSet::default(); cs.sent_requests.insert( @@ -168,7 +233,7 @@ impl ManagedIdentity { .sent_contact_requests .insert(recipient_id, request); self.dashpay.rescan_triggered.remove(&recipient_id); - return Ok(()); + return Ok(SentContactRequestOutcome::Recorded); } let mut cs = ContactChangeSet::default(); @@ -233,7 +298,7 @@ impl ManagedIdentity { .sent_contact_requests .insert(recipient_id, request); } - Ok(()) + Ok(SentContactRequestOutcome::Recorded) } /// Ignore `sender_id` (per-sender mute, = block, reversible). @@ -2200,6 +2265,75 @@ mod tests { ); } + /// The identity keeps the order of a pair's sent requests itself: a + /// request older than the one tracked for its recipient is passed over, + /// whoever hands it in, while the recipient is pending and once the + /// contact is established alike. The tracked request stays, nothing is + /// written, and the answer says so. A newer one is recorded, and the one + /// tracked answers as already tracked. + #[test] + fn should_pass_over_a_sent_request_older_than_the_one_tracked() { + let our_id = Identifier::from([1u8; 32]); + let contact_id = Identifier::from([2u8; 32]); + let p = noop_persister(); + let request = |timestamp: u64, reference: u32| { + let mut request = create_contact_request(our_id, contact_id, timestamp); + request.account_reference = reference; + request + }; + + for established in [false, true] { + let mut managed = create_test_identity([1u8; 32]); + if established { + managed + .add_incoming_contact_request(create_contact_request(contact_id, our_id, 1), &p) + .expect("setup persists"); + } + assert_eq!( + managed + .track_sent_contact_request(request(5, 101), &p) + .expect("no store fails"), + SentContactRequestOutcome::Recorded, + "established: {established}" + ); + + // Older by its creation time, under the same reference version. + assert_eq!( + managed + .track_sent_contact_request(request(4, 100), &p) + .expect("no store fails"), + SentContactRequestOutcome::Superseded, + "established: {established}" + ); + assert_eq!( + managed.prior_sent_account_reference(&contact_id), + Some(101), + "the newer request stays tracked, established: {established}" + ); + // The public entry point keeps the same order. + managed + .add_sent_contact_request(request(4, 100), &p) + .expect("passed over"); + assert_eq!(managed.prior_sent_account_reference(&contact_id), Some(101)); + + assert_eq!( + managed + .track_sent_contact_request(request(5, 101), &p) + .expect("no store fails"), + SentContactRequestOutcome::AlreadyTracked, + "established: {established}" + ); + assert_eq!( + managed + .track_sent_contact_request(request(6, 102), &p) + .expect("no store fails"), + SentContactRequestOutcome::Recorded, + "established: {established}" + ); + assert_eq!(managed.prior_sent_account_reference(&contact_id), Some(102)); + } + } + /// Pending-branch rotation supersede: re-sending to a recipient who /// has NOT yet reciprocated (still in the pending sent map) with a /// DIFFERENT outgoing `accountReference` must advance the pending diff --git a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/mod.rs b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/mod.rs index c11f1e91f8a..7565d41a00a 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/mod.rs @@ -9,6 +9,7 @@ mod dashpay; mod identity_ops; mod sync; +pub(crate) use contact_requests::{freshness, predates, SentContactRequestOutcome}; pub use dashpay::{DashPayState, PrivateContact}; // `block_time` + `key_storage` moved to `crate::wallet::identity::types`. From b2600478f2e549245b33e3c93f932fe438646d40 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 15 Nov 2026 17:34:47 +0700 Subject: [PATCH 11/14] fix(platform-wallet): keep a backend's flush ledgers with the backend instance The flush ledgers of a wallet id's records were made per manager. A backend buffers a wallet's records under its id whoever stored them, so a manager made over a backend instance that an earlier manager was made over, while a wallet handle of the earlier one was still in use, counted that backend's flushes apart from the handle. A flush through the handle that failed for good dropped records the later manager had buffered, and the later manager's own flush was then trusted over an empty buffer. A manager now takes the ledgers of the backend instance it is made over (`FlushLedgers::of_backend`, through `DurableCursorState::over_backend`). They are found by the backend's address in a list that holds the backend and the ledgers weakly: an entry goes once the backend or every holder of its ledgers is gone, and the weak reference keeps the allocation from being reused while the entry stands, so one backend's entry is never taken for another's. A manager made after every holder is gone starts with new ledgers, as before. The durable cursor entries and the lock that orders cursor writes stay per manager: two managers that both sync wallets over one backend instance are still not supported, and the docs say so. Test: should_count_a_backends_flushes_in_one_place_for_every_manager_over_it, which fails with the lookup taken out. Co-Authored-By: Claude Fable 5.2 --- packages/rs-platform-wallet/README.md | 2 +- .../src/changeset/core_bridge.rs | 21 ++++-- .../src/changeset/flush_ledger.rs | 43 ++++++++++- .../rs-platform-wallet/src/manager/mod.rs | 75 ++++++++++++++++--- 4 files changed, 120 insertions(+), 21 deletions(-) diff --git a/packages/rs-platform-wallet/README.md b/packages/rs-platform-wallet/README.md index 7981685cc5a..512c6d566a4 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -123,7 +123,7 @@ Startup errors distinguish damaged recovery metadata or undecodable signed bytes ## Private contact request payments after a restart -Before a private contact request's payment can reach the network, the shielded store saves a record of it in its private payment journal: the sender and recipient identities, the request's account reference, the commitment of the entry it appends, the SHA-256 of the whole entry, the position the sender's read of DashPay's private store rested at when it paid, and the nullifiers its spend consumes. The file store writes the journal, and the payment's redrive record, in one transaction on a connection at `synchronous=FULL` with `fullfsync` (on Apple platforms a plain sync does not flush the drive's own cache), so the record survives a crash or a power loss; a record that cannot be saved refuses the payment before anything is broadcast. The journal is kept apart from the spend's notes and redrive record: no note release, redrive removal, undecodable transition, account rebind or purge removes it. A record is removed only when its entry is read back from DashPay's private store and recorded durably, or when the transition provably never left the wallet (refused before anything was sent: no address to send it to, or the SDK's own refusal). An endpoint's answer, a consensus verdict included, proves nothing: no proof or quorum signature backs it, and an endpoint that kept or forwarded the signed transition can answer with any verdict. A record is also discharged once its payment is proven expired, judged against Platform's recorded anchors fetched before the shielded sync pass and proved at a height H: the spend's anchor, which the record keeps with the proved height of the set it was read from, was read at or below H and is absent from the set at H, the pass's note scan covers H, and its entry is absent from DashPay's private store read from the lowest position the records being judged kept through its end, with every page proved at or above H. An entry counts as the record's only when its whole bytes hash to the hash the record keeps: one that only starts with the commitment, which anyone who saw the commitment can append, does not hold the record. A record journaled before the hash and the position were kept matches by its commitment and is read for from the store's start. Whether its inputs are spent does not matter: the transition appends its entry in the block that spends them. A payment that never lands therefore frees its pair once its anchor ages out (Platform keeps an anchor for `shielded_anchor_retention_blocks`, 1000 blocks, and prunes every `shielded_anchor_pruning_interval`, 100 blocks). A restoration that finds a record whose request the sender's tracked request already records or supersedes, as a private sync read it back in a process that ended before discharging it, discharges it. Once a flush of the records under the wallet's id failed for good, the backend dropping what it buffered, no record is discharged on the strength of a tracked request for as long as that wallet stays loaded. Every flush of the id counts, whoever made it: the wallet's own, one through a handle of a wallet removed under the same id since, or the manager's before it publishes a wallet. The count is kept by the manager, so a storage backend serves one manager: a host that replaces its manager gives the new one its own backend instance, or stops using the earlier manager's wallet handles first. A wallet loaded or created under the id after the failure tracks what it read from disk and is not held by it. While the hold stands, no read-back is noted, a read of the entry again included (it finds the request tracked in memory and stores nothing), and a restoration leaves the records it finds settled. A restoration that finds a record whose request its sender does not track rewinds the sender's read to the position the record kept (the store's start for a record that kept none), so the entry is read again, its request recorded, and the record discharged once that is durable. Nothing else, a relay's refusal or a missing redrive record included, discharges a record. +Before a private contact request's payment can reach the network, the shielded store saves a record of it in its private payment journal: the sender and recipient identities, the request's account reference, the commitment of the entry it appends, the SHA-256 of the whole entry, the position the sender's read of DashPay's private store rested at when it paid, and the nullifiers its spend consumes. The file store writes the journal, and the payment's redrive record, in one transaction on a connection at `synchronous=FULL` with `fullfsync` (on Apple platforms a plain sync does not flush the drive's own cache), so the record survives a crash or a power loss; a record that cannot be saved refuses the payment before anything is broadcast. The journal is kept apart from the spend's notes and redrive record: no note release, redrive removal, undecodable transition, account rebind or purge removes it. A record is removed only when its entry is read back from DashPay's private store and recorded durably, or when the transition provably never left the wallet (refused before anything was sent: no address to send it to, or the SDK's own refusal). An endpoint's answer, a consensus verdict included, proves nothing: no proof or quorum signature backs it, and an endpoint that kept or forwarded the signed transition can answer with any verdict. A record is also discharged once its payment is proven expired, judged against Platform's recorded anchors fetched before the shielded sync pass and proved at a height H: the spend's anchor, which the record keeps with the proved height of the set it was read from, was read at or below H and is absent from the set at H, the pass's note scan covers H, and its entry is absent from DashPay's private store read from the lowest position the records being judged kept through its end, with every page proved at or above H. An entry counts as the record's only when its whole bytes hash to the hash the record keeps: one that only starts with the commitment, which anyone who saw the commitment can append, does not hold the record. A record journaled before the hash and the position were kept matches by its commitment and is read for from the store's start. Whether its inputs are spent does not matter: the transition appends its entry in the block that spends them. A payment that never lands therefore frees its pair once its anchor ages out (Platform keeps an anchor for `shielded_anchor_retention_blocks`, 1000 blocks, and prunes every `shielded_anchor_pruning_interval`, 100 blocks). A restoration that finds a record whose request the sender's tracked request already records or supersedes, as a private sync read it back in a process that ended before discharging it, discharges it. Once a flush of the records under the wallet's id failed for good, the backend dropping what it buffered, no record is discharged on the strength of a tracked request for as long as that wallet stays loaded. Every flush of the id counts, whoever made it: the wallet's own, one through a handle of a wallet removed under the same id since, or the manager's before it publishes a wallet. The count belongs to the storage backend instance, so a manager made over the same instance while an earlier manager's wallet handles are still in use counts with them. The order of sync cursor writes is still kept per manager: two managers that both sync wallets over one backend instance are not supported. A wallet loaded or created under the id after the failure tracks what it read from disk and is not held by it. While the hold stands, no read-back is noted, a read of the entry again included (it finds the request tracked in memory and stores nothing), and a restoration leaves the records it finds settled. A restoration that finds a record whose request its sender does not track rewinds the sender's read to the position the record kept (the store's start for a record that kept none), so the entry is read again, its request recorded, and the record discharged once that is durable. Nothing else, a relay's refusal or a missing redrive record included, discharges a record. Until a payment's reservation is restored from the journal, nothing shows that the pair is taken: the request's entry is not in DashPay's store until the payment executes. So a wallet sends and accepts no contact request, public or private, while such a payment may be unrestored, and returns `PrivateContactPaymentsNotRestored` (FFI code 72) with nothing read, signed or broadcast. The in-kind accept is held the same way. These hold it: diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index e0421e52e0d..928776d0ba1 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -327,14 +327,25 @@ pub struct DurableCursorState { /// its same-id successor's and the manager count every flush of the id /// in one place ([`FlushLedger`](crate::changeset::flush_ledger::FlushLedger)). /// - /// One place per manager, as the cursors above are: a backend serves one - /// manager (see `PlatformWalletManager::new`). A second manager over the - /// same backend instance would order cursor writes, and count flushes, - /// apart from this one. - pub(crate) flush_ledgers: FlushLedgers, + /// Unlike the cursors above, that place belongs to the backend, not to + /// one manager: a manager takes the ledgers of the backend instance it + /// is made over ([`Self::over_backend`]), so a manager made over the + /// same instance while an earlier one's handles are still in use counts + /// with them. + pub(crate) flush_ledgers: Arc, } impl DurableCursorState { + /// The state of a manager made over `backend`: its own cursors and + /// barriers, and the flush ledgers of that backend instance + /// ([`FlushLedgers::of_backend`]). + pub(crate) fn over_backend(backend: &Arc) -> DurableCursors { + Arc::new(Self { + flush_ledgers: FlushLedgers::of_backend(backend), + ..Self::default() + }) + } + /// Lock the cursor map for a short, non-blocking critical section. pub async fn lock(&self) -> tokio::sync::MutexGuard<'_, BTreeMap> { self.cursors.lock().await diff --git a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs index 7308c0bf71d..6e9cfb98817 100644 --- a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs +++ b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs @@ -2,7 +2,7 @@ use std::collections::BTreeMap; use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; -use std::sync::{Arc, Mutex, PoisonError}; +use std::sync::{Arc, Mutex, PoisonError, Weak}; use crate::changeset::traits::{PersistenceError, PlatformWalletPersistence}; use crate::wallet::platform_wallet::WalletId; @@ -113,12 +113,49 @@ impl FlushLedger { } } -/// The [`FlushLedger`] of each wallet id, for the wallets of one manager. An -/// entry outlives its wallet, as the backend's buffer under the id does. +/// The [`FlushLedger`] of each wallet id, for the records one backend +/// instance holds. An entry outlives its wallet, as the backend's buffer +/// under the id does. #[derive(Debug, Default)] pub(crate) struct FlushLedgers(Mutex>>); +/// The ledgers of each backend instance a manager was made over +/// ([`FlushLedgers::of_backend`]), both held weakly: an entry goes once its +/// backend, or every holder of its ledgers, is gone. The weak reference keeps +/// the backend's allocation from being reused while the entry stands, so an +/// entry is never taken for another backend's. +static BACKEND_LEDGERS: Mutex> = Mutex::new(Vec::new()); + +type BackendLedgers = (Weak, Weak); + impl FlushLedgers { + /// The ledgers of `backend`'s records: the same ones for every manager + /// made over this backend instance while a holder of them is left (a + /// manager, or a wallet handle one gave out), and new ones otherwise. + /// + /// A backend buffers a wallet's records under its id whoever stored + /// them, so the count of its flushes belongs with the backend, not with + /// one manager: a handle of an earlier manager that flushes the buffer + /// and fails is then counted where a later manager over the same backend + /// reads. + pub(crate) fn of_backend(backend: &Arc) -> Arc { + let mut known = BACKEND_LEDGERS + .lock() + .unwrap_or_else(PoisonError::into_inner); + known.retain(|(held, ledgers)| held.strong_count() > 0 && ledgers.strong_count() > 0); + let address = Arc::as_ptr(backend).cast::<()>(); + let shared = known + .iter() + .find(|(held, _)| held.as_ptr().cast::<()>() == address) + .and_then(|(_, ledgers)| ledgers.upgrade()); + if let Some(ledgers) = shared { + return ledgers; + } + let ledgers = Arc::new(Self::default()); + known.push((Arc::downgrade(backend), Arc::downgrade(&ledgers))); + ledgers + } + /// The ledger of `wallet_id`'s records, made on first use. pub(crate) fn of(&self, wallet_id: WalletId) -> Arc { Arc::clone( diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 1d1f4170dde..0cf89a5a81e 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -36,8 +36,8 @@ use key_wallet_manager::WalletManager; use crate::changeset::{ persists_backfill_coverage, spawn_wallet_event_adapter_with_durable_cursors, CoreChangeSet, - DashPayBackfillRecord, DurableCursor, DurableCursors, PlatformWalletChangeSet, - PlatformWalletPersistence, + DashPayBackfillRecord, DurableCursor, DurableCursorState, DurableCursors, + PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::error::PlatformWalletError; use crate::events::{PlatformEventHandler, PlatformEventManager}; @@ -574,15 +574,17 @@ impl PlatformWalletManager

{ /// Internally, a `LockNotifyHandler` is also registered to wake /// `AssetLockManager` async waiters on lock events. /// - /// `persister` serves this manager alone, for as long as the manager or - /// a wallet handle it gave out is in use. What the manager knows of the - /// backend per wallet id (the order of durable cursor writes, and the - /// count of flushes that failed, [`DurableCursors`]) is kept per manager: - /// a second manager over the same backend instance, or a wallet handle - /// of an earlier manager still storing through it, would write and flush - /// the same buffered records without either seeing the other's. A host - /// that replaces its manager gives the new one its own backend instance, - /// as the FFI does, or stops using the earlier manager's handles first. + /// The count of `persister`'s flushes per wallet id belongs to the + /// backend instance, not to this manager: a manager made over the same + /// instance while an earlier one's wallet handles are still in use counts + /// with them, so a flush through such a handle that drops the backend's + /// buffer is seen here ([`DurableCursorState::over_backend`]). + /// + /// The order of durable cursor writes ([`DurableCursors`]) is still kept + /// per manager, so two managers that both sync wallets over one backend + /// instance are not supported. A host that replaces its manager gives + /// the new one its own backend instance, as the FFI does, or stops the + /// earlier manager first. pub fn new( sdk: Arc, persister: Arc

, @@ -611,7 +613,11 @@ impl PlatformWalletManager

{ // Host-visible hard sync-fault latch. The // adapter raises it the first time it freezes a durable watermark. let sync_fault = Arc::new(std::sync::atomic::AtomicBool::new(false)); - let durable_cursors = DurableCursors::default(); + // With the flush ledgers of this backend instance, shared with any + // earlier manager over it whose handles are still in use. + let durable_cursors = DurableCursorState::over_backend( + &(Arc::clone(&persister) as Arc), + ); // Spawn the wallet-event adapter that translates upstream // `WalletEvent`s into `CoreChangeSet`s and forwards them to @@ -1645,6 +1651,51 @@ mod tests { assert!(!later.buffered_writes_lost()); } + /// The count of a backend's flushes belongs to the backend instance: a + /// manager made over one that an earlier manager was made over, while a + /// wallet handle of the earlier one is still in use, counts with it. A + /// flush through that handle that fails for good, dropping the buffer + /// both managers' records share, is then seen by the later manager. A + /// manager over another backend instance counts apart. + #[tokio::test] + async fn should_count_a_backends_flushes_in_one_place_for_every_manager_over_it() { + use crate::wallet::persister::WalletPersister; + + let manager_over = |persister: &Arc| { + PlatformWalletManager::new( + Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")), + Arc::clone(persister), + Arc::new(NoopEventHandler) as Arc, + ) + }; + let persister = Arc::new(FatalFlushPersister); + let wallet_id: WalletId = [7; 32]; + let earlier = manager_over(&persister); + // As `PlatformWallet::new` makes the handle of a wallet under the id. + let stale = WalletPersister::with_flush_ledger( + wallet_id, + Arc::clone(&persister) as Arc, + earlier.durable_cursors.flush_ledgers.of(wallet_id), + ); + + let later = manager_over(&persister); + let counted = later.durable_cursors.flush_ledgers.of(wallet_id); + assert_eq!(counted.lost(), 0); + assert!(stale.flush().is_err(), "the earlier handle's flush fails"); + assert_eq!( + counted.lost(), + 1, + "the later manager sees the flush that dropped the shared buffer" + ); + + let apart = manager_over(&Arc::new(FatalFlushPersister)); + assert_eq!( + apart.durable_cursors.flush_ledgers.of(wallet_id).lost(), + 0, + "another backend instance has its own count" + ); + } + /// A buffering backend whose every store is taken at once by a flush of /// another handle of the wallet id, as one from another thread would /// take it before the storer's own flush. That flush fails, for good or From 1e40bd8e2a8cf0c65a3d28ee5356de5c2c57236f Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 15 Nov 2026 19:44:29 +0700 Subject: [PATCH 12/14] fix(platform-wallet): let the backend own its flush ledgers, and never count an inline backend's flush as a loss Two gaps a review of b2600478f2 found. The flush ledgers of a backend instance were found through a list that held them weakly, and a wallet's handle kept only the ledger of its own wallet id. So once the manager was gone, the list could drop the entry while a handle that could still flush the backend was in use, and a later manager over the same backend counted apart from it. A handle made with `WalletPersister::new` never used the list at all. The ledgers now live as long as their backend instance: the list holds them strongly and the backend weakly, and drops an entry once nothing holds the backend. `WalletPersister::new` takes its ledger from there, so every handle over one backend instance counts in one place however it was made. The manager no longer carries ledgers: `DurableCursorState` is back to cursors and barriers, and `with_flush_ledger` is gone. A backend whose store is the commit buffers nothing, and its flush only notifies the host. A failed notification was still counted as a flush that dropped the buffer, after which the handle refused to note read-backs or settle restored requests for the rest of its life. Such a flush is now counted as failed and never as lost, and `flush_durably` answers for an inline backend before it looks at the ledger. Tests: should_keep_a_backends_ledgers_for_as_long_as_the_backend_lives, should_not_count_a_failed_flush_of_an_inline_backend_as_lost, should_answer_for_an_inline_backend_whose_flush_notification_fails. The first two fail with their fix taken out, the third with the second's. Co-Authored-By: Claude Fable 5.2 --- packages/rs-platform-wallet/README.md | 2 +- .../src/changeset/core_bridge.rs | 22 --- .../src/changeset/flush_ledger.rs | 133 ++++++++++++++---- .../rs-platform-wallet/src/manager/mod.rs | 81 +++-------- .../src/wallet/persister.rs | 99 ++++++++----- .../src/wallet/platform_wallet.rs | 6 +- 6 files changed, 193 insertions(+), 150 deletions(-) diff --git a/packages/rs-platform-wallet/README.md b/packages/rs-platform-wallet/README.md index 512c6d566a4..957ea35ee81 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -123,7 +123,7 @@ Startup errors distinguish damaged recovery metadata or undecodable signed bytes ## Private contact request payments after a restart -Before a private contact request's payment can reach the network, the shielded store saves a record of it in its private payment journal: the sender and recipient identities, the request's account reference, the commitment of the entry it appends, the SHA-256 of the whole entry, the position the sender's read of DashPay's private store rested at when it paid, and the nullifiers its spend consumes. The file store writes the journal, and the payment's redrive record, in one transaction on a connection at `synchronous=FULL` with `fullfsync` (on Apple platforms a plain sync does not flush the drive's own cache), so the record survives a crash or a power loss; a record that cannot be saved refuses the payment before anything is broadcast. The journal is kept apart from the spend's notes and redrive record: no note release, redrive removal, undecodable transition, account rebind or purge removes it. A record is removed only when its entry is read back from DashPay's private store and recorded durably, or when the transition provably never left the wallet (refused before anything was sent: no address to send it to, or the SDK's own refusal). An endpoint's answer, a consensus verdict included, proves nothing: no proof or quorum signature backs it, and an endpoint that kept or forwarded the signed transition can answer with any verdict. A record is also discharged once its payment is proven expired, judged against Platform's recorded anchors fetched before the shielded sync pass and proved at a height H: the spend's anchor, which the record keeps with the proved height of the set it was read from, was read at or below H and is absent from the set at H, the pass's note scan covers H, and its entry is absent from DashPay's private store read from the lowest position the records being judged kept through its end, with every page proved at or above H. An entry counts as the record's only when its whole bytes hash to the hash the record keeps: one that only starts with the commitment, which anyone who saw the commitment can append, does not hold the record. A record journaled before the hash and the position were kept matches by its commitment and is read for from the store's start. Whether its inputs are spent does not matter: the transition appends its entry in the block that spends them. A payment that never lands therefore frees its pair once its anchor ages out (Platform keeps an anchor for `shielded_anchor_retention_blocks`, 1000 blocks, and prunes every `shielded_anchor_pruning_interval`, 100 blocks). A restoration that finds a record whose request the sender's tracked request already records or supersedes, as a private sync read it back in a process that ended before discharging it, discharges it. Once a flush of the records under the wallet's id failed for good, the backend dropping what it buffered, no record is discharged on the strength of a tracked request for as long as that wallet stays loaded. Every flush of the id counts, whoever made it: the wallet's own, one through a handle of a wallet removed under the same id since, or the manager's before it publishes a wallet. The count belongs to the storage backend instance, so a manager made over the same instance while an earlier manager's wallet handles are still in use counts with them. The order of sync cursor writes is still kept per manager: two managers that both sync wallets over one backend instance are not supported. A wallet loaded or created under the id after the failure tracks what it read from disk and is not held by it. While the hold stands, no read-back is noted, a read of the entry again included (it finds the request tracked in memory and stores nothing), and a restoration leaves the records it finds settled. A restoration that finds a record whose request its sender does not track rewinds the sender's read to the position the record kept (the store's start for a record that kept none), so the entry is read again, its request recorded, and the record discharged once that is durable. Nothing else, a relay's refusal or a missing redrive record included, discharges a record. +Before a private contact request's payment can reach the network, the shielded store saves a record of it in its private payment journal: the sender and recipient identities, the request's account reference, the commitment of the entry it appends, the SHA-256 of the whole entry, the position the sender's read of DashPay's private store rested at when it paid, and the nullifiers its spend consumes. The file store writes the journal, and the payment's redrive record, in one transaction on a connection at `synchronous=FULL` with `fullfsync` (on Apple platforms a plain sync does not flush the drive's own cache), so the record survives a crash or a power loss; a record that cannot be saved refuses the payment before anything is broadcast. The journal is kept apart from the spend's notes and redrive record: no note release, redrive removal, undecodable transition, account rebind or purge removes it. A record is removed only when its entry is read back from DashPay's private store and recorded durably, or when the transition provably never left the wallet (refused before anything was sent: no address to send it to, or the SDK's own refusal). An endpoint's answer, a consensus verdict included, proves nothing: no proof or quorum signature backs it, and an endpoint that kept or forwarded the signed transition can answer with any verdict. A record is also discharged once its payment is proven expired, judged against Platform's recorded anchors fetched before the shielded sync pass and proved at a height H: the spend's anchor, which the record keeps with the proved height of the set it was read from, was read at or below H and is absent from the set at H, the pass's note scan covers H, and its entry is absent from DashPay's private store read from the lowest position the records being judged kept through its end, with every page proved at or above H. An entry counts as the record's only when its whole bytes hash to the hash the record keeps: one that only starts with the commitment, which anyone who saw the commitment can append, does not hold the record. A record journaled before the hash and the position were kept matches by its commitment and is read for from the store's start. Whether its inputs are spent does not matter: the transition appends its entry in the block that spends them. A payment that never lands therefore frees its pair once its anchor ages out (Platform keeps an anchor for `shielded_anchor_retention_blocks`, 1000 blocks, and prunes every `shielded_anchor_pruning_interval`, 100 blocks). A restoration that finds a record whose request the sender's tracked request already records or supersedes, as a private sync read it back in a process that ended before discharging it, discharges it. Once a flush of the records under the wallet's id failed for good, the backend dropping what it buffered, no record is discharged on the strength of a tracked request for as long as that wallet stays loaded. Every flush of the id counts, whoever made it: the wallet's own, one through a handle of a wallet removed under the same id since, or the manager's before it publishes a wallet. The count belongs to the storage backend instance and lives as long as it does: every handle made over the instance counts in it, whichever manager gave it out. A backend whose store is the commit is never counted as having lost records, as its flush only notifies the host. The order of sync cursor writes is still kept per manager: two managers that both sync wallets over one backend instance are not supported. A wallet loaded or created under the id after the failure tracks what it read from disk and is not held by it. While the hold stands, no read-back is noted, a read of the entry again included (it finds the request tracked in memory and stores nothing), and a restoration leaves the records it finds settled. A restoration that finds a record whose request its sender does not track rewinds the sender's read to the position the record kept (the store's start for a record that kept none), so the entry is read again, its request recorded, and the record discharged once that is durable. Nothing else, a relay's refusal or a missing redrive record included, discharges a record. Until a payment's reservation is restored from the journal, nothing shows that the pair is taken: the request's entry is not in DashPay's store until the payment executes. So a wallet sends and accepts no contact request, public or private, while such a payment may be unrestored, and returns `PrivateContactPaymentsNotRestored` (FFI code 72) with nothing read, signed or broadcast. The in-kind accept is held the same way. These hold it: diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index 928776d0ba1..a0594b36402 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -64,7 +64,6 @@ use crate::changeset::changeset::{ fold_same_txid_records, merge_payment_overlays, AssetLockChangeSet, CoreChangeSet, HighestUsedIndexes, PaymentOverlay, PlatformWalletChangeSet, SweepBatch, UtxoCreditVerdict, }; -use crate::changeset::flush_ledger::FlushLedgers; use crate::changeset::merge::Merge; use crate::changeset::persistence_capabilities::PersistenceCapabilities; use crate::changeset::traits::PlatformWalletPersistence; @@ -322,30 +321,9 @@ pub struct DurableCursorState { /// wallet left (see [`RewindBarrier::retire`]). A leaf lock: taken under /// the manager lock on both sides, never held across an await. pub retired: std::sync::Mutex>, - /// Per wallet id, what is known of the flushes of its records: like a - /// cursor entry it outlives its wallet, so a removed wallet's handles, - /// its same-id successor's and the manager count every flush of the id - /// in one place ([`FlushLedger`](crate::changeset::flush_ledger::FlushLedger)). - /// - /// Unlike the cursors above, that place belongs to the backend, not to - /// one manager: a manager takes the ledgers of the backend instance it - /// is made over ([`Self::over_backend`]), so a manager made over the - /// same instance while an earlier one's handles are still in use counts - /// with them. - pub(crate) flush_ledgers: Arc, } impl DurableCursorState { - /// The state of a manager made over `backend`: its own cursors and - /// barriers, and the flush ledgers of that backend instance - /// ([`FlushLedgers::of_backend`]). - pub(crate) fn over_backend(backend: &Arc) -> DurableCursors { - Arc::new(Self { - flush_ledgers: FlushLedgers::of_backend(backend), - ..Self::default() - }) - } - /// Lock the cursor map for a short, non-blocking critical section. pub async fn lock(&self) -> tokio::sync::MutexGuard<'_, BTreeMap> { self.cursors.lock().await diff --git a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs index 6e9cfb98817..fd2479ece38 100644 --- a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs +++ b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs @@ -13,10 +13,11 @@ use crate::wallet::platform_wallet::WalletId; /// and a flush takes that buffer, whoever flushed: one that fails for good /// drops what it took, and one that fails transiently puts it back. So what /// a flush did to the buffer is known only where every flush of the id is -/// counted: one ledger per wallet id, shared by the manager and by every -/// handle of a wallet under that id, a removed wallet's handles and its -/// same-id successor's alike ([`FlushLedgers`]). Every flush of the wallet's -/// records goes through [`Self::flush`]. +/// counted: one ledger per backend instance and wallet id, shared by every +/// manager made over the backend and by every handle of a wallet under that +/// id, a removed wallet's handles and its same-id successor's alike +/// ([`FlushLedgers::of_backend`]). Every flush of the wallet's records goes +/// through [`Self::flush`]. #[derive(Debug, Default)] pub(crate) struct FlushLedger { /// How many flushes are in progress: each is counted from before it calls @@ -39,25 +40,33 @@ pub(crate) struct FlushLedger { /// did with what it took from the buffer is not known, so it is recorded as /// failed for good before it stops being counted. A caller that catches the /// panic and carries on then finds the loss, as it would any other. +/// +/// A backend whose store is the commit buffers nothing, so no flush of it +/// drops anything, whatever the flush answers: it is counted as failed and +/// never as lost. struct InFlight<'a> { ledger: &'a FlushLedger, + /// Whether the backend buffers what is stored until a flush. + buffers: bool, recorded: bool, } impl<'a> InFlight<'a> { - fn enter(ledger: &'a FlushLedger) -> Self { + fn enter(ledger: &'a FlushLedger, buffers: bool) -> Self { ledger.in_flight.fetch_add(1, Ordering::SeqCst); Self { ledger, + buffers, recorded: false, } } /// Record the backend's answer: a failure is counted, and counted as - /// lost too when it is for good. + /// lost too when it is for good and the backend buffers. fn record(&mut self, flushed: &Result<(), PersistenceError>) { if let Err(error) = flushed { - self.ledger.record_failure(!error.is_transient()); + self.ledger + .record_failure(self.buffers && !error.is_transient()); } self.recorded = true; } @@ -66,7 +75,7 @@ impl<'a> InFlight<'a> { impl Drop for InFlight<'_> { fn drop(&mut self) { if !self.recorded { - self.ledger.record_failure(true); + self.ledger.record_failure(self.buffers); } self.ledger.in_flight.fetch_sub(1, Ordering::SeqCst); } @@ -77,12 +86,17 @@ impl FlushLedger { /// backend is called until its outcome is recorded, counted as failed /// when it fails, and as lost too when it fails for good (anything but a /// transient failure, which keeps the buffer) or the backend panics. + /// + /// Never as lost on a backend whose store is the commit + /// ([`PlatformWalletPersistence::store_commits_inline`]): a flush there + /// only notifies the host, and what was stored is already on disk + /// whatever the notification answers. pub(crate) fn flush( &self, backend: &dyn PlatformWalletPersistence, wallet_id: WalletId, ) -> Result<(), PersistenceError> { - let mut in_flight = InFlight::enter(self); + let mut in_flight = InFlight::enter(self, !backend.store_commits_inline()); let flushed = backend.flush(wallet_id); in_flight.record(&flushed); flushed @@ -119,40 +133,40 @@ impl FlushLedger { #[derive(Debug, Default)] pub(crate) struct FlushLedgers(Mutex>>); -/// The ledgers of each backend instance a manager was made over -/// ([`FlushLedgers::of_backend`]), both held weakly: an entry goes once its -/// backend, or every holder of its ledgers, is gone. The weak reference keeps -/// the backend's allocation from being reused while the entry stands, so an +/// The ledgers of each backend instance that is still alive +/// ([`FlushLedgers::of_backend`]): kept for as long as anything holds the +/// backend, and dropped once nothing does. The backend is held weakly, which +/// keeps its allocation from being reused while its entry stands, so an /// entry is never taken for another backend's. static BACKEND_LEDGERS: Mutex> = Mutex::new(Vec::new()); -type BackendLedgers = (Weak, Weak); +type BackendLedgers = (Weak, Arc); impl FlushLedgers { - /// The ledgers of `backend`'s records: the same ones for every manager - /// made over this backend instance while a holder of them is left (a - /// manager, or a wallet handle one gave out), and new ones otherwise. + /// The ledgers of `backend`'s records: the same ones for every caller + /// that passes this backend instance, for as long as the instance lives. /// /// A backend buffers a wallet's records under its id whoever stored - /// them, so the count of its flushes belongs with the backend, not with - /// one manager: a handle of an earlier manager that flushes the buffer - /// and fails is then counted where a later manager over the same backend - /// reads. + /// them, so the count of its flushes belongs with the backend and lives + /// as long as it does, not as long as one manager or one handle: whoever + /// can still flush the backend holds it, and so finds the same ledgers + /// here. Every handle of a wallet takes its ledger from here + /// (`WalletPersister::new`), however it was made. pub(crate) fn of_backend(backend: &Arc) -> Arc { let mut known = BACKEND_LEDGERS .lock() .unwrap_or_else(PoisonError::into_inner); - known.retain(|(held, ledgers)| held.strong_count() > 0 && ledgers.strong_count() > 0); + known.retain(|(held, _)| held.strong_count() > 0); let address = Arc::as_ptr(backend).cast::<()>(); let shared = known .iter() .find(|(held, _)| held.as_ptr().cast::<()>() == address) - .and_then(|(_, ledgers)| ledgers.upgrade()); + .map(|(_, ledgers)| Arc::clone(ledgers)); if let Some(ledgers) = shared { return ledgers; } let ledgers = Arc::new(Self::default()); - known.push((Arc::downgrade(backend), Arc::downgrade(&ledgers))); + known.push((Arc::downgrade(backend), Arc::clone(&ledgers))); ledgers } @@ -194,6 +208,77 @@ mod tests { } } + /// A buffering backend whose flush fails for good, and one whose store is + /// the commit, whose flush only notifies the host and fails the same way. + struct Failing { + inline: bool, + } + + impl PlatformWalletPersistence for Failing { + fn store_commits_inline(&self) -> bool { + self.inline + } + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Err(PersistenceError::backend("the host refused the flush")) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + /// A backend whose store is the commit buffers nothing, so a flush of it + /// that fails dropped nothing: it is counted as failed, never as lost. A + /// buffering backend's flush that fails the same way is counted as both. + #[test] + fn should_not_count_a_failed_flush_of_an_inline_backend_as_lost() { + for inline in [true, false] { + let ledger = FlushLedger::default(); + + let flushed = ledger.flush(&Failing { inline }, [0; 32]); + + assert!(flushed.is_err_and(|error| !error.is_transient())); + assert_eq!(ledger.failed(), 1, "inline: {inline}"); + assert_eq!(ledger.lost(), u64::from(!inline), "inline: {inline}"); + } + } + + /// The ledgers of a backend instance live as long as the instance does, + /// whoever asks for them and whatever was dropped in between: a handle + /// that outlived its manager keeps only its own wallet id's ledger, and + /// a handle made later over the same backend still counts in that one. + /// Another backend instance has its own. + #[test] + fn should_keep_a_backends_ledgers_for_as_long_as_the_backend_lives() { + let backend: Arc = Arc::new(Failing { inline: false }); + let wallet_id: WalletId = [5; 32]; + + // All an earlier handle keeps: the ledger of its own wallet id. + let earlier = FlushLedgers::of_backend(&backend).of(wallet_id); + let later = FlushLedgers::of_backend(&backend).of(wallet_id); + assert!(Arc::ptr_eq(&earlier, &later), "one ledger for the id"); + + assert!(earlier.flush(backend.as_ref(), wallet_id).is_err()); + assert_eq!( + later.lost(), + 1, + "the later handle sees the earlier one's loss" + ); + + let other: Arc = Arc::new(Failing { inline: false }); + assert_eq!( + FlushLedgers::of_backend(&other).of(wallet_id).lost(), + 0, + "another backend instance counts apart" + ); + } + /// A flush whose backend panics is recorded as failed for good before it /// stops being counted as in progress: what it did with what it took from /// the buffer is not known, so a caller that catches the panic and diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 0cf89a5a81e..0507b819b08 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -36,8 +36,8 @@ use key_wallet_manager::WalletManager; use crate::changeset::{ persists_backfill_coverage, spawn_wallet_event_adapter_with_durable_cursors, CoreChangeSet, - DashPayBackfillRecord, DurableCursor, DurableCursorState, DurableCursors, - PlatformWalletChangeSet, PlatformWalletPersistence, + DashPayBackfillRecord, DurableCursor, DurableCursors, PlatformWalletChangeSet, + PlatformWalletPersistence, }; use crate::error::PlatformWalletError; use crate::events::{PlatformEventHandler, PlatformEventManager}; @@ -575,10 +575,11 @@ impl PlatformWalletManager

{ /// `AssetLockManager` async waiters on lock events. /// /// The count of `persister`'s flushes per wallet id belongs to the - /// backend instance, not to this manager: a manager made over the same - /// instance while an earlier one's wallet handles are still in use counts - /// with them, so a flush through such a handle that drops the backend's - /// buffer is seen here ([`DurableCursorState::over_backend`]). + /// backend instance, not to this manager, and lives as long as the + /// instance does: every handle made over it counts there, so a flush + /// through a handle an earlier manager gave out that drops the backend's + /// buffer is seen by this manager and its wallets + /// (`WalletPersister::new`). /// /// The order of durable cursor writes ([`DurableCursors`]) is still kept /// per manager, so two managers that both sync wallets over one backend @@ -613,11 +614,7 @@ impl PlatformWalletManager

{ // Host-visible hard sync-fault latch. The // adapter raises it the first time it freezes a durable watermark. let sync_fault = Arc::new(std::sync::atomic::AtomicBool::new(false)); - // With the flush ledgers of this backend instance, shared with any - // earlier manager over it whose handles are still in use. - let durable_cursors = DurableCursorState::over_backend( - &(Arc::clone(&persister) as Arc), - ); + let durable_cursors = DurableCursors::default(); // Spawn the wallet-event adapter that translates upstream // `WalletEvent`s into `CoreChangeSet`s and forwards them to @@ -855,10 +852,9 @@ impl PlatformWalletManager

{ // And a flush that succeeds here is not trusted by itself: a flush // by such a handle may have taken the replacement first and failed, // which the ledger shows, and the wallet is then not published. - let persister = WalletPersister::with_flush_ledger( + let persister = WalletPersister::new( wallet_id, Arc::clone(&self.persister) as Arc, - self.durable_cursors.flush_ledgers.of(wallet_id), ); persister.store_durably(changeset).map_err(|not_durable| { tracing::error!( @@ -1525,6 +1521,7 @@ mod tests { use dash_async::WorkerStatus; + use crate::changeset::flush_ledger::FlushLedgers; use crate::changeset::{ ClientStartState, FlushLedger, PersistenceError, PersistenceErrorKind, PlatformWalletChangeSet, PlatformWalletPersistence, @@ -1617,10 +1614,9 @@ mod tests { ); let wallet_id: WalletId = [7; 32]; // As `PlatformWallet::new` makes the handle of a wallet under the id. - let earlier = WalletPersister::with_flush_ledger( + let earlier = WalletPersister::new( wallet_id, Arc::clone(&persister) as Arc, - mgr.durable_cursors.flush_ledgers.of(wallet_id), ); assert!(!earlier.buffered_writes_lost()); @@ -1643,59 +1639,13 @@ mod tests { ); assert!(matches!(earlier.flush_durably(), Err(NotDurable::Lost))); // A handle made after it starts from what is on disk. - let later = WalletPersister::with_flush_ledger( + let later = WalletPersister::new( wallet_id, Arc::clone(&persister) as Arc, - mgr.durable_cursors.flush_ledgers.of(wallet_id), ); assert!(!later.buffered_writes_lost()); } - /// The count of a backend's flushes belongs to the backend instance: a - /// manager made over one that an earlier manager was made over, while a - /// wallet handle of the earlier one is still in use, counts with it. A - /// flush through that handle that fails for good, dropping the buffer - /// both managers' records share, is then seen by the later manager. A - /// manager over another backend instance counts apart. - #[tokio::test] - async fn should_count_a_backends_flushes_in_one_place_for_every_manager_over_it() { - use crate::wallet::persister::WalletPersister; - - let manager_over = |persister: &Arc| { - PlatformWalletManager::new( - Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")), - Arc::clone(persister), - Arc::new(NoopEventHandler) as Arc, - ) - }; - let persister = Arc::new(FatalFlushPersister); - let wallet_id: WalletId = [7; 32]; - let earlier = manager_over(&persister); - // As `PlatformWallet::new` makes the handle of a wallet under the id. - let stale = WalletPersister::with_flush_ledger( - wallet_id, - Arc::clone(&persister) as Arc, - earlier.durable_cursors.flush_ledgers.of(wallet_id), - ); - - let later = manager_over(&persister); - let counted = later.durable_cursors.flush_ledgers.of(wallet_id); - assert_eq!(counted.lost(), 0); - assert!(stale.flush().is_err(), "the earlier handle's flush fails"); - assert_eq!( - counted.lost(), - 1, - "the later manager sees the flush that dropped the shared buffer" - ); - - let apart = manager_over(&Arc::new(FatalFlushPersister)); - assert_eq!( - apart.durable_cursors.flush_ledgers.of(wallet_id).lost(), - 0, - "another backend instance has its own count" - ); - } - /// A buffering backend whose every store is taken at once by a flush of /// another handle of the wallet id, as one from another thread would /// take it before the storer's own flush. That flush fails, for good or @@ -1775,7 +1725,12 @@ mod tests { let wallet_id: WalletId = [7; 32]; persister .ledger - .set(mgr.durable_cursors.flush_ledgers.of(wallet_id)) + .set( + FlushLedgers::of_backend( + &(Arc::clone(&persister) as Arc), + ) + .of(wallet_id), + ) .expect("set once"); // Coverage the wallet about to be published does not hold was diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index e30ca11ce18..25f050f0a84 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -11,6 +11,7 @@ use std::sync::Arc; use dashcore::Txid; use key_wallet::managed_account::transaction_record::TransactionRecord; +use crate::changeset::flush_ledger::FlushLedgers; use crate::changeset::{ ClientStartState, DpnsNameStateEntry, FlushLedger, PersistenceCapabilities, PersistenceError, PersistenceErrorKind, PlatformWalletChangeSet, PlatformWalletPersistence, @@ -214,22 +215,13 @@ pub(crate) enum NotDurable { } impl WalletPersister { - /// A handle whose flushes no other handle or manager shares: for a - /// backend this handle alone flushes `wallet_id`'s records on. + /// A handle for `wallet_id`'s records on `inner`. It counts its flushes + /// in the ledger of that backend instance and wallet id + /// ([`FlushLedgers::of_backend`]), the one every other handle made over + /// the same backend instance for the id counts in: whichever manager, or + /// no manager, made it, and for as long as the backend lives. pub fn new(wallet_id: WalletId, inner: Arc) -> Self { - Self::with_flush_ledger(wallet_id, inner, Arc::default()) - } - - /// A handle that counts its flushes in `flush_ledger`, the one every - /// flush of `wallet_id`'s records on `inner` is counted in - /// ([`FlushLedgers`](crate::changeset::flush_ledger::FlushLedgers)). Inside the crate - /// only: a ledger made elsewhere would count some of the id's flushes - /// apart from the rest. - pub(crate) fn with_flush_ledger( - wallet_id: WalletId, - inner: Arc, - flush_ledger: Arc, - ) -> Self { + let flush_ledger = FlushLedgers::of_backend(&inner).of(wallet_id); Self { wallet_id, inner, @@ -768,25 +760,26 @@ impl WalletPersister { /// flush writes what was put back. #[cfg(feature = "shielded")] pub(crate) fn flush_durably(&self) -> Result<(), NotDurable> { + // A store that is the commit left nothing to flush or to lose: a + // flush of such a backend only notifies the host, and its failing + // takes nothing back. + if self.store_commits_inline() { + return Ok(()); + } if self.buffered_writes_lost() { return Err(NotDurable::Lost); } - if !self.store_commits_inline() { - let failed_before = self.flush_ledger.failed(); - self.flush().map_err(NotDurable::Flush)?; - if self.flush_ledger.in_flight() != 0 { - return Err(NotDurable::OtherFlushUnsettled); - } - if self.buffered_writes_lost() { - return Err(NotDurable::Lost); - } - if self.flush_ledger.failed() != failed_before { - return Err(NotDurable::OtherFlushUnsettled); - } + let failed_before = self.flush_ledger.failed(); + self.flush().map_err(NotDurable::Flush)?; + if self.flush_ledger.in_flight() != 0 { + return Err(NotDurable::OtherFlushUnsettled); } if self.buffered_writes_lost() { return Err(NotDurable::Lost); } + if self.flush_ledger.failed() != failed_before { + return Err(NotDurable::OtherFlushUnsettled); + } Ok(()) } @@ -1103,16 +1096,10 @@ mod tests { let backend = Arc::new(Failing { for_good: AtomicBool::new(false), }); - let ledger = Arc::new(FlushLedger::default()); let wallet_id: WalletId = [3; 32]; - let persister = WalletPersister::with_flush_ledger( - wallet_id, - Arc::clone(&backend) as _, - Arc::clone(&ledger), - ); + let persister = WalletPersister::new(wallet_id, Arc::clone(&backend) as _); // A handle of a wallet removed, or created since, under the id. - let other = - WalletPersister::with_flush_ledger(wallet_id, Arc::clone(&backend) as _, ledger); + let other = WalletPersister::new(wallet_id, Arc::clone(&backend) as _); let owner = Identifier::from([9; 32]); persister.note_private_marker_stored(owner, Some(true)); @@ -1310,6 +1297,48 @@ mod tests { assert_eq!(backend.durable.load(Ordering::SeqCst), 1, "on disk"); } + /// A backend whose store is the commit answers for what was stored + /// whatever its flush notification answers: a flush of it that fails took + /// nothing back, so the handle does not count its records as lost, and a + /// read-back is still noted. + #[cfg(feature = "shielded")] + #[test] + fn should_answer_for_an_inline_backend_whose_flush_notification_fails() { + struct InlineFailing; + impl PlatformWalletPersistence for InlineFailing { + fn store_commits_inline(&self) -> bool { + true + } + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Err(PersistenceError::backend("the host refused the flush")) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + let persister = WalletPersister::new([0; 32], Arc::new(InlineFailing) as _); + assert!(persister.flush().is_err(), "the notification fails"); + + assert!( + !persister.buffered_writes_lost(), + "nothing was buffered to lose" + ); + assert!(persister.flush_durably().is_ok()); + persister.note_read_back_once_durable([[1; 32]]); + assert!( + persister.has_read_back_private_entries(), + "the read-back is noted" + ); + } + /// A store is not answered for while another flush of the wallet's /// records is still inside the backend: that one may have taken what the /// store buffered, and its failure is recorded only once it returns. diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index f0a6016c6a3..eaa22e1af34 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -756,11 +756,7 @@ impl PlatformWallet { // Build the per-wallet persister handle once and share it with // the sub-wallets that need to queue their own changesets // (currently just `AssetLockManager` — see Item 8 sub-step 1a). - let wallet_persister = WalletPersister::with_flush_ledger( - wallet_id, - persister, - durable_cursors.flush_ledgers.of(wallet_id), - ); + let wallet_persister = WalletPersister::new(wallet_id, persister); let core = CoreWallet::new( Arc::clone(&sdk), From 81d9151a33696bf5682be395ae33364668d52964 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 15 Nov 2026 21:51:09 +0700 Subject: [PATCH 13/14] test(platform-wallet): show that backends of zero-sized types have their own flush ledgers A review of 1e40bd8e2a took the address a backend instance is told apart by for one that zero-sized backend types could share. An `Arc` allocates its reference counts whatever the size of what it holds, so every live backend has its own address. The new test makes two backends of one zero-sized type and one of another, all alive at once, and checks that a failed flush of the first is counted against neither of the others. The comment on the list of backend ledgers now says how a backend is told apart, and what outlives a backend that is gone: its entry stays until the next lookup by any caller, so the leftovers are bounded and do not add up over repeated manager or backend lifetimes. `may_broadcast_held_public_request` read the wallet-wide hold on unrestored private payments before it waited for the wallet lock. It now reads it once the lock is held, with the rest of what decides, so a hold raised during that wait is seen. Test: should_tell_apart_live_backends_of_zero_sized_types. Co-Authored-By: Claude Fable 5.2 --- .../src/changeset/flush_ledger.rs | 57 +++++++++++++++++++ .../identity/network/contact_requests.rs | 7 ++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs index fd2479ece38..f9b7fc58b04 100644 --- a/packages/rs-platform-wallet/src/changeset/flush_ledger.rs +++ b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs @@ -138,6 +138,16 @@ pub(crate) struct FlushLedgers(Mutex>>); /// backend, and dropped once nothing does. The backend is held weakly, which /// keeps its allocation from being reused while its entry stands, so an /// entry is never taken for another backend's. +/// +/// A backend instance is told apart by the address of its `Arc` allocation. +/// Every `Arc` has one, a backend type of no size included, as the +/// allocation holds the reference counts: two live backends never share it. +/// +/// An entry whose backend is gone is dropped at the next lookup, by any +/// caller. So what outlives a backend is bounded: the entries of the +/// backends that went since the last lookup, each a few counters per wallet +/// id, until the next wallet handle is made anywhere in the process. They do +/// not add up over repeated manager or backend lifetimes. static BACKEND_LEDGERS: Mutex> = Mutex::new(Vec::new()); type BackendLedgers = (Weak, Arc); @@ -279,6 +289,53 @@ mod tests { ); } + /// A buffering backend of no size whose flush fails for good. `N` makes + /// distinct types of it. + struct Unit; + + impl PlatformWalletPersistence for Unit { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Err(PersistenceError::backend("the database is corrupt")) + } + fn load(&self) -> Result { + Ok(ClientStartState::default()) + } + } + + /// Backend instances are told apart by their `Arc` allocation, which a + /// backend type of no size has too: two live backends of such types, the + /// same type or not, have their own ledgers, and a flush of one that + /// fails is not counted against the others. The same instance, asked + /// again, has the same ledger. + #[test] + fn should_tell_apart_live_backends_of_zero_sized_types() { + assert_eq!(std::mem::size_of::>(), 0); + let first: Arc = Arc::new(Unit::<0>); + let second: Arc = Arc::new(Unit::<0>); + let third: Arc = Arc::new(Unit::<1>); + let wallet_id: WalletId = [6; 32]; + let ledger_of = |backend| FlushLedgers::of_backend(backend).of(wallet_id); + let (of_first, of_second, of_third) = + (ledger_of(&first), ledger_of(&second), ledger_of(&third)); + + assert!(of_first.flush(first.as_ref(), wallet_id).is_err()); + + assert_eq!(of_first.lost(), 1); + assert_eq!(of_second.lost(), 0, "another instance of the same type"); + assert_eq!(of_third.lost(), 0, "an instance of another type"); + assert!( + Arc::ptr_eq(&ledger_of(&first), &of_first), + "the same instance has the same ledger" + ); + } + /// A flush whose backend panics is recorded as failed for good before it /// stops being counted as in progress: what it did with what it took from /// the buffer is not known, so a caller that catches the panic and diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs index e100fb1159f..424610f8a5e 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs @@ -1654,10 +1654,15 @@ impl DashPayView<'_, B> { recipient: &Identifier, attempt: u64, ) -> bool { + let wm = self.wallet_manager.read().await; + // Read once the wallet is locked, with the rest of what decides, not + // before the wait for the lock: a hold raised during that wait is + // seen. One raised after this answer does not recall the broadcast + // that follows, as none recalls a first send between its claim and + // its broadcast. if self.persister.private_payments_unrestored() { return false; } - let wm = self.wallet_manager.read().await; wm.get_wallet_info(&self.wallet_id) .filter(|info| Arc::ptr_eq(&info.generation, &self.generation)) .and_then(|info| info.identity_manager.managed_identity(sender)) From 4116484cdeba2fc6a68644d6d985442f977f5dad Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Mon, 16 Nov 2026 01:06:24 +0700 Subject: [PATCH 14/14] fix(platform-wallet): raise the private payment hold only between repeat broadcasts A held public request's bytes are broadcast again only while the wallet's private payments are restored. The check read the hold, and the dispatch followed it; the hold could be raised in between, by a restoration that failed or could not run, and the bytes then went out with the hold standing. The persister now has a gate for that span. The repeat broadcast holds it shared from before it reads the hold until its bytes are dispatched, and raising the hold takes it alone. So a repeat broadcast is either decided after the hold stands, and not made, or wholly made before it. A hold that already stands is not raised again and waits for nothing, which keeps a contact request's own retry of a restoration from waiting on the gate. Nothing that raises the hold holds the wallet manager lock, which the repeat broadcast takes under the gate. `hold_until_private_payments_restored` is async for it; its three callers await it. Test: should_not_broadcast_a_held_request_again_once_a_hold_being_raised_stands, which fails with either side of the gate taken out. Co-Authored-By: Claude Fable 5.2 --- .../rs-platform-wallet/src/manager/load.rs | 3 +- .../rs-platform-wallet/src/manager/mod.rs | 5 +- .../identity/network/contact_requests.rs | 13 ++-- .../network/private_contact_requests.rs | 60 ++++++++++++++++++- .../src/wallet/persister.rs | 30 +++++++++- .../src/wallet/platform_wallet.rs | 4 +- 6 files changed, 104 insertions(+), 11 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index db5da2bd8ec..fa70818a696 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -597,7 +597,8 @@ impl PlatformWalletManager

{ { platform_wallet .persister() - .hold_until_private_payments_restored(); + .hold_until_private_payments_restored() + .await; } let platform_wallet = Arc::new(platform_wallet); diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 0507b819b08..a5c4c2df253 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -1111,7 +1111,10 @@ impl PlatformWalletManager

{ wallet.restore_private_payments_or_defer(&coordinator).await; } None if wallet.persister().persists_private_contact_scan_state() => { - wallet.persister().hold_until_private_payments_restored(); + wallet + .persister() + .hold_until_private_payments_restored() + .await; } None => {} } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs index 424610f8a5e..ea49cea92ed 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contact_requests.rs @@ -1588,6 +1588,10 @@ impl DashPayView<'_, B> { // wallet removed in between has none of its requests pushed // to the network after it is gone. let _lifecycle = self.generation.payment_guard().await; + // And the gate the hold on unrestored private payments is + // raised under, over the same span: the hold is not raised + // between the check that finds it down and the dispatch. + let _dispatching = self.persister.held_request_dispatch().await; if self .may_broadcast_held_public_request(sender, recipient, held.attempt) .await @@ -1655,11 +1659,10 @@ impl DashPayView<'_, B> { attempt: u64, ) -> bool { let wm = self.wallet_manager.read().await; - // Read once the wallet is locked, with the rest of what decides, not - // before the wait for the lock: a hold raised during that wait is - // seen. One raised after this answer does not recall the broadcast - // that follows, as none recalls a first send between its claim and - // its broadcast. + // Read once the wallet is locked, with the rest of what decides. The + // caller holds the gate the hold is raised under + // (`WalletPersister::held_request_dispatch`) from before this read + // through its dispatch, so the hold is not raised in between. if self.persister.private_payments_unrestored() { return false; } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs index 6660b53ea83..e4cde2ded4b 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/private_contact_requests.rs @@ -7331,6 +7331,61 @@ mod tests { ); } + /// A hold on unrestored private payments that is being raised while an + /// unconfirmed public request's repeat broadcast is in flight waits for + /// that broadcast, and a repeat broadcast that begins once the raise is + /// waiting waits for the hold and then finds it: its bytes do not go out. + /// So no repeat broadcast is decided while the hold is down and + /// dispatched once it is up. + #[cfg(feature = "shielded")] + #[tokio::test] + async fn should_not_broadcast_a_held_request_again_once_a_hold_being_raised_stands() { + let alice = identity(ALICE_PHRASE, ALICE); + let bob = identity(BOB_PHRASE, BOB); + let alice_crypto = SeedCryptoProvider::from_seed(seed(ALICE_PHRASE), Network::Testnet); + let alice_signer = signer(ALICE_PHRASE, &alice); + let (_manager, alice_wallet, transition, reference) = + unconfirmed_public_request(&alice, &bob, &alice_signer, &alice_crypto).await; + expect_sent_request(&alice_wallet, &alice, &bob, reference, None).await; + let view = alice_wallet.identity().dashpay(); + let persister = alice_wallet.persister().clone(); + persister.private_payments_restored(); + let sent_once = broadcasts_of(&alice_wallet, &transition).await; + + // As another held request's repeat broadcast, mid-dispatch. + let dispatching = persister.held_request_dispatch().await; + let mut raising = Box::pin(persister.hold_until_private_payments_restored()); + assert!( + tokio::time::timeout(Duration::from_millis(100), &mut raising) + .await + .is_err(), + "the raise waits for the dispatch in flight" + ); + assert!(!persister.private_payments_unrestored()); + + // A repeat broadcast that begins now is ordered after the raise. + let (alice_id, bob_id) = (alice.id(), bob.id()); + let mut settling = + Box::pin(view.settle_unconfirmed_public_request(&alice_id, &bob_id, true)); + assert!( + tokio::time::timeout(Duration::from_millis(300), &mut settling) + .await + .is_err(), + "the settling waits for the hold being raised" + ); + assert_eq!(broadcasts_of(&alice_wallet, &transition).await, sent_once); + + drop(dispatching); + raising.await; + assert!(persister.private_payments_unrestored(), "the hold stands"); + settling.await.expect("the reads"); + assert_eq!( + broadcasts_of(&alice_wallet, &transition).await, + sent_once, + "nothing is broadcast once the hold stands" + ); + } + /// A handle taken before its wallet was removed and created again under /// its id completes no external account registration on the replacement: /// it stamps no outbound-account marker, stores no label, and its @@ -12928,7 +12983,10 @@ mod tests { .expect("the shielded store is configured"); let wallet = create_holding(&manager, ALICE_PHRASE, &alice).await; // As the registration's check before the store was configured left it. - wallet.persister().hold_until_private_payments_restored(); + wallet + .persister() + .hold_until_private_payments_restored() + .await; manager .restore_private_payments_configured_since(&wallet) .await; diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index 25f050f0a84..bb9ee3abd15 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -60,6 +60,14 @@ pub struct WalletPersister { wallet_id: WalletId, inner: Arc, contact_payment_gate: Arc>, + /// Orders the raising of the hold on unrestored private payments + /// ([`Self::hold_until_private_payments_restored`]) against a held + /// public request's repeat broadcast ([`Self::held_request_dispatch`]): + /// the broadcast holds it shared from its check of the hold through its + /// dispatch, and the raise takes it alone. So a repeat broadcast is + /// either decided after the hold stands, and not made, or wholly made + /// before it. Shared by the clones the wallet and its views hold. + held_request_dispatch: Arc>, /// Commitments of private contact request entries a private sync read /// back and recorded durably, whose records in the shielded store's /// private payment journal are to be discharged: the wallet's view @@ -226,6 +234,7 @@ impl WalletPersister { wallet_id, inner, contact_payment_gate: Arc::new(tokio::sync::Mutex::new(())), + held_request_dispatch: Arc::default(), #[cfg(feature = "shielded")] read_back_private_entries: Arc::default(), #[cfg(feature = "shielded")] @@ -584,12 +593,31 @@ impl WalletPersister { /// [`Self::private_payments_restored`]: its shielded store may hold an /// unconfirmed private payment from an earlier process whose pair no /// reservation holds yet. + /// + /// Raising it waits for any held public request's repeat broadcast that + /// is between its check of this hold and its dispatch + /// ([`Self::held_request_dispatch`]), so none is decided while the hold + /// is down and dispatched once it is up. A hold that already stands is + /// not raised again and waits for nothing. The caller holds no wallet + /// manager lock: the repeat broadcast takes one under the gate. #[cfg(feature = "shielded")] - pub(crate) fn hold_until_private_payments_restored(&self) { + pub(crate) async fn hold_until_private_payments_restored(&self) { + if self.private_payments_unrestored() { + return; + } + let _no_repeat_broadcast = self.held_request_dispatch.write().await; self.private_payments_unrestored .store(true, Ordering::Release); } + /// Held by a held public request's repeat broadcast from before it + /// checks [`Self::private_payments_unrestored`] until its bytes are + /// dispatched: the hold is not raised meanwhile + /// ([`Self::hold_until_private_payments_restored`]). + pub(crate) async fn held_request_dispatch(&self) -> tokio::sync::RwLockReadGuard<'_, ()> { + self.held_request_dispatch.read().await + } + /// The reservations of this wallet's unconfirmed private payments are /// restored: release the hold [`Self::hold_until_private_payments_restored`] /// raised. Called after the restored reservations are in place, so a send diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index eaa22e1af34..6647ba890f7 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -3115,11 +3115,11 @@ impl PrivatePaymentRestore { self.persister.private_payments_restored(); } Ok(None) if self.restored_from_this_store() => {} - Ok(None) => self.persister.hold_until_private_payments_restored(), + Ok(None) => self.persister.hold_until_private_payments_restored().await, Err(error) => { self.persister .note_private_payment_restore(Some(error.to_string())); - self.persister.hold_until_private_payments_restored(); + self.persister.hold_until_private_payments_restored().await; } } restored