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/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-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-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/README.md b/packages/rs-platform-wallet/README.md index 7e38a91f9f4..957ea35ee81 100644 --- a/packages/rs-platform-wallet/README.md +++ b/packages/rs-platform-wallet/README.md @@ -123,17 +123,17 @@ 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. 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: - 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 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 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. @@ -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. 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. + +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/changeset/flush_ledger.rs b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs new file mode 100644 index 00000000000..f9b7fc58b04 --- /dev/null +++ b/packages/rs-platform-wallet/src/changeset/flush_ledger.rs @@ -0,0 +1,354 @@ +//! 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, Weak}; + +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 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 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 + /// 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 + /// 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. + failed: AtomicU64, + /// How many of them failed for good, the backend dropping what it + /// buffered. + lost: AtomicU64, +} + +/// One flush in progress, counted in its [`FlushLedger`] until dropped. +/// +/// 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. +/// +/// 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, 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 and the backend buffers. + fn record(&mut self, flushed: &Result<(), PersistenceError>) { + if let Err(error) = flushed { + self.ledger + .record_failure(self.buffers && !error.is_transient()); + } + self.recorded = true; + } +} + +impl Drop for InFlight<'_> { + fn drop(&mut self) { + if !self.recorded { + self.ledger.record_failure(self.buffers); + } + self.ledger.in_flight.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, 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, !backend.store_commits_inline()); + let flushed = backend.flush(wallet_id); + 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) + } + + /// 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) + } +} + +/// 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 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. +/// +/// 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); + +impl FlushLedgers { + /// 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 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, _)| held.strong_count() > 0); + let address = Arc::as_ptr(backend).cast::<()>(); + let shared = known + .iter() + .find(|(held, _)| held.as_ptr().cast::<()>() == address) + .map(|(_, ledgers)| Arc::clone(ledgers)); + if let Some(ledgers) = shared { + return ledgers; + } + let ledgers = Arc::new(Self::default()); + known.push((Arc::downgrade(backend), Arc::clone(&ledgers))); + ledgers + } + + /// The ledger of `wallet_id`'s records, made on first use. + pub(crate) fn of(&self, wallet_id: WalletId) -> Arc { + Arc::clone( + self.0 + .lock() + .unwrap_or_else(PoisonError::into_inner) + .entry(wallet_id) + .or_default(), + ) + } +} + +#[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 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 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 + /// 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/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 266d2d6e2c8..b1a4ada2db1 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(crate) 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(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/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/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 b074deb07ed..a5c4c2df253 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; @@ -572,6 +573,19 @@ 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. + /// + /// The count of `persister`'s flushes per wallet id belongs to the + /// 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 + /// 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

, @@ -772,7 +786,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 +801,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,26 +844,27 @@ 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::new( + wallet_id, + Arc::clone(&self.persister) as Arc, + ); + 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 { - 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) - })?; - } if let Some(cursor) = cursor { durable_cursors.insert(wallet_id, DurableCursor::at(cursor)); } @@ -1093,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 => {} } @@ -1499,10 +1520,14 @@ impl Drop for PlatformWalletManager

{ mod tests { use super::*; + use std::sync::OnceLock; + use dash_async::WorkerStatus; + use crate::changeset::flush_ledger::FlushLedgers; use crate::changeset::{ - ClientStartState, PersistenceError, PlatformWalletChangeSet, PlatformWalletPersistence, + ClientStartState, FlushLedger, PersistenceError, PersistenceErrorKind, + PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::events::{EventHandler, PlatformEventHandler}; @@ -1554,6 +1579,186 @@ mod tests { impl EventHandler for NoopEventHandler {} impl PlatformEventHandler for NoopEventHandler {} + /// A buffering backend whose flush fails for good, dropping its buffer. + struct FatalFlushPersister; + + 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::new( + wallet_id, + Arc::clone(&persister) as Arc, + ); + 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::new( + wallet_id, + Arc::clone(&persister) as Arc, + ); + 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( + 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 + // 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 e99725d64cb..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 @@ -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,6 +702,53 @@ fn release_public_claim( } } +/// 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 { + managed + .track_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 +/// 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 +814,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 +908,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 +977,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 +1020,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, @@ -1050,7 +1107,6 @@ impl DashPayView<'_, B> { sender_identity_id, recipient_identity_id, request, - None, account_index, contact_xpub_ext, ) @@ -1231,7 +1287,6 @@ impl DashPayView<'_, B> { sender_identity_id, recipient_identity_id, contact_request, - None, account_index, contact_xpub_ext, ) @@ -1240,42 +1295,139 @@ 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. - async fn record_sent_contact_request( + /// + /// 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. + /// + /// A request older than the one the wallet tracks for the pair is not + /// 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 + /// 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, 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); + 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, + 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); } } - self.register_contact_account(sender, recipient, account_index, contact_xpub) - .await?; + 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. + /// + /// 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, + 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)); + } + let outcome = track_sent_contact_request(managed, &request, &self.persister)?; + managed.settle_unconfirmed_public_request(recipient, attempt); + if outcome == SentContactRequestOutcome::Superseded { + return Ok(None); + } + } + 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 + { + tracing::warn!( + sender = %sender, + recipient = %recipient, + %error, + "contact request recorded as sent; its receiving account is not registered yet, \ + the next sync registers it" + ); + } + } + /// Hold the pair `sender`, `recipient` for the public request /// `transition` signs, whose broadcast may have been delivered /// (`ManagedIdentity::hold_unconfirmed_public_request`). Only on this @@ -1346,7 +1498,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 +1530,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 +1550,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 +1565,7 @@ impl DashPayView<'_, B> { }) }; let Some(held) = held else { - return Ok(()); + return Ok(None); }; let outcome = self .sdk @@ -1418,34 +1583,47 @@ impl DashPayView<'_, B> { TransitionOutcome::StillPossible { .. } => false, }; if found.is_none() && !settled { - if matches!(outcome, TransitionOutcome::StillPossible { .. }) { - // 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" - ); + 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; + // 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 + { + // 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(()); + 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( + self.record_settled_public_request( sender, recipient, request, - Some(held.attempt), + held.attempt, held.account_index, held.contact_xpub, ) - .await?; + .await } None => { let mut wm = self.wallet_manager.write().await; @@ -1455,9 +1633,51 @@ 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 { + let wm = self.wallet_manager.read().await; + // 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; + } + 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 +1686,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 +1733,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; } } @@ -1964,14 +2189,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 @@ -1983,19 +2200,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. /// @@ -2102,32 +2306,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 @@ -4303,9 +4497,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 { @@ -4374,7 +4576,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 { @@ -4466,7 +4671,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 { @@ -4714,9 +4920,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..ac1b8d7221e 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` @@ -482,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, @@ -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/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 3425470a24a..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 @@ -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; @@ -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" + ); } } @@ -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 @@ -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); @@ -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, @@ -2725,8 +2739,11 @@ 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; + #[cfg(feature = "shielded")] + use crate::wallet::persister::NotDurable; use crate::wallet::platform_wallet::{PlatformWallet, WalletId}; #[cfg(feature = "shielded")] use crate::wallet::shielded::coordinator::{ @@ -2961,6 +2978,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 { @@ -2971,6 +2996,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), }) } @@ -2999,11 +3026,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, @@ -3951,6 +3992,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 @@ -4767,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 @@ -6370,33 +6531,40 @@ mod tests { .expect("the read")); } - /// 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. - #[tokio::test] - async fn should_leave_a_replacement_s_unconfirmed_public_request_to_it() { - 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 = manager_on(Arc::new(NoPersistence)).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(); - 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); - expect_dashpay_nonce(&again, &alice, 3).await; - let sent = again + /// 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, + ) { + 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() .dashpay() .send_contact_request_with_external_signer( @@ -6404,8 +6572,8 @@ mod tests { &bob.id(), None, AutoAcceptProofSource::None, - &alice_signer, - &alice_crypto, + signer, + crypto, ) .await; assert!( @@ -6413,59 +6581,105 @@ mod tests { if error.is_state_transition_result_unconfirmed()), "{sent:?}" ); - let (transition, reference) = unconfirmed_to(&again, ALICE, &bob).await.expect("held"); - - // Proofs that would settle it, as recorded: the stale handle acts on - // none of it. - expect_sent_request( - &again, - &alice, - &bob, - reference, - Some(sent_request_document(&alice, &bob, reference)), - ) - .await; - stale - .settle_unconfirmed_public_request(&alice.id(), &bob.id()) + let (transition, reference) = unconfirmed_to(&alice_wallet, ALICE, bob) .await - .expect("nothing to settle"); - assert_eq!( - unconfirmed_to(&again, ALICE, &bob).await, - Some((transition, reference)), - "the replacement's request stays held" - ); - managed(&again, ALICE, |managed| { - assert!(managed.dashpay().outgoing_request(&bob.id()).is_none()); - }) - .await; - let claimed = stale - .claim_public_pair(&alice.id(), &bob.id(), false, false) - .await; - assert!( - matches!( - claimed, - Err(PlatformWalletError::WalletRemovedBeforeSend(_)) - ), - "{:?}", - claimed.err() - ); + .expect("held"); + (manager, alice_wallet, transition, reference) + } - // The replacement's own handle settles it. - again - .identity() - .dashpay() - .settle_unconfirmed_public_request(&alice.id(), &bob.id()) - .await - .expect("settled"); - assert!(unconfirmed_to(&again, ALICE, &bob).await.is_none()); + /// 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" + ); + } + } } - /// A process that starts afresh holds no unconfirmed public request: a - /// request the earlier process broadcast, and that executed after it - /// ended, is found by the proved read before a new one is signed, and - /// adopted as sent with nothing signed or broadcast. + /// 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_adopt_a_request_already_on_platform_instead_of_sending_another() { + 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); @@ -6474,15 +6688,982 @@ mod tests { inner: signer(ALICE_PHRASE, &alice), signed: Arc::clone(&signed), }; - // The earlier process: its request is unconfirmed when it ends. - let reference = { - let (_manager, earlier) = wallet(ALICE_PHRASE, &alice, &[&alice, &bob], vec![]).await; - expect_dashpay_nonce(&earlier, &alice, 3).await; - let _ = earlier - .identity() - .dashpay() - .send_contact_request_with_external_signer( - &alice.id(), + 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 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; + } + } + + /// 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; + } + + /// 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 + /// 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. + #[tokio::test] + async fn should_register_no_contact_account_on_a_replacement_from_a_stale_handle() { + 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 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 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 + /// 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 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. + #[tokio::test] + async fn should_leave_a_replacement_s_unconfirmed_public_request_to_it() { + 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 = manager_on(Arc::new(NoPersistence)).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(); + 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); + expect_dashpay_nonce(&again, &alice, 3).await; + let sent = again + .identity() + .dashpay() + .send_contact_request_with_external_signer( + &alice.id(), + &bob.id(), + None, + AutoAcceptProofSource::None, + &alice_signer, + &alice_crypto, + ) + .await; + assert!( + matches!(&sent, Err(PlatformWalletError::Sdk(error)) + if error.is_state_transition_result_unconfirmed()), + "{sent:?}" + ); + let (transition, reference) = unconfirmed_to(&again, ALICE, &bob).await.expect("held"); + + // Proofs that would settle it, as recorded: the stale handle acts on + // none of it. + expect_sent_request( + &again, + &alice, + &bob, + reference, + Some(sent_request_document(&alice, &bob, reference)), + ) + .await; + stale + .settle_unconfirmed_public_request(&alice.id(), &bob.id(), true) + .await + .expect("nothing to settle"); + assert_eq!( + unconfirmed_to(&again, ALICE, &bob).await, + Some((transition, reference)), + "the replacement's request stays held" + ); + managed(&again, ALICE, |managed| { + assert!(managed.dashpay().outgoing_request(&bob.id()).is_none()); + }) + .await; + let claimed = stale + .claim_public_pair(&alice.id(), &bob.id(), false, false) + .await; + assert!( + matches!( + claimed, + Err(PlatformWalletError::WalletRemovedBeforeSend(_)) + ), + "{:?}", + claimed.err() + ); + + // The replacement's own handle settles it. + again + .identity() + .dashpay() + .settle_unconfirmed_public_request(&alice.id(), &bob.id(), true) + .await + .expect("settled"); + assert!(unconfirmed_to(&again, ALICE, &bob).await.is_none()); + } + + /// A process that starts afresh holds no unconfirmed public request: a + /// request the earlier process broadcast, and that executed after it + /// ended, is found by the proved read before a new one is signed, and + /// adopted as sent with nothing signed or broadcast. + #[tokio::test] + async fn should_adopt_a_request_already_on_platform_instead_of_sending_another() { + 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), + }; + // The earlier process: its request is unconfirmed when it ends. + let reference = { + let (_manager, earlier) = wallet(ALICE_PHRASE, &alice, &[&alice, &bob], vec![]).await; + expect_dashpay_nonce(&earlier, &alice, 3).await; + let _ = earlier + .identity() + .dashpay() + .send_contact_request_with_external_signer( + &alice.id(), &bob.id(), None, AutoAcceptProofSource::None, @@ -11802,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/identity/state/managed_identity/contact_requests.rs b/packages/rs-platform-wallet/src/wallet/identity/state/managed_identity/contact_requests.rs index ac70cc4f35a..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). @@ -1096,32 +1161,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); } } @@ -2195,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/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/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`. diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index 4f30a4c2e4c..bb9ee3abd15 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -11,9 +11,10 @@ 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, PersistenceCapabilities, PersistenceError, - PlatformWalletChangeSet, PlatformWalletPersistence, + ClientStartState, DpnsNameStateEntry, FlushLedger, PersistenceCapabilities, PersistenceError, + PersistenceErrorKind, PlatformWalletChangeSet, PlatformWalletPersistence, }; use crate::error::PlatformWalletError; use crate::wallet::platform_wallet::WalletId; @@ -59,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 @@ -94,18 +103,27 @@ 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>>, - /// 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. + /// 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 + /// 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")] - buffered_writes_lost: 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. @@ -159,12 +177,64 @@ 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)] +pub(crate) enum NotDurable { + /// 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 store this call made failed, so nothing was flushed. + #[error("{0}")] + 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 + /// flush writes. A later call answers. + #[error( + "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" + )] + OtherFlushUnsettled, +} + impl WalletPersister { + /// 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 { + let flush_ledger = FlushLedgers::of_backend(&inner).of(wallet_id); Self { 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")] @@ -173,7 +243,8 @@ impl WalletPersister { private_payments_unrestored: Arc::default(), stored_private_markers: Arc::default(), #[cfg(feature = "shielded")] - buffered_writes_lost: 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")] @@ -235,25 +306,63 @@ 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), + }; + } + + /// 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 + /// 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 @@ -270,20 +379,23 @@ 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, 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 + /// 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")] @@ -299,50 +411,52 @@ impl WalletPersister { if pending.is_empty() { return; } - if self.buffered_writes_lost() { - tracing::warn!( + 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); + } + Err(NotDurable::OtherFlushUnsettled) => { + tracing::warn!( + kept = pending.len(), + "private requests read back while another flush of this wallet's records \ + 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); + } + // `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 + // 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, 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, + dropped = pending.len(), + "private requests read back are not durably recorded, and the backend \ + dropped what it buffered; their journal records stay" + ); + } + 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; - } - if !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; - } + ), } - self.note_read_back_private_entries(pending); } /// Whether the read-back of `commitment` waits for a flush that failed @@ -479,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 @@ -557,25 +690,139 @@ 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> { - 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); + 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 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, 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 + /// it, and does not refuse it. + pub(crate) fn store_durably( + &self, + changeset: PlatformWalletChangeSet, + ) -> Result<(), NotDurable> { + let (lost_before, failed_before) = (self.flush_ledger.lost(), self.flush_ledger.failed()); + self.store(changeset).map_err(NotDurable::Store)?; + if self.store_commits_inline() { + return Ok(()); + } + self.flush().map_err(NotDurable::Flush)?; + if self.flush_ledger.in_flight() != 0 { + return Err(NotDurable::OtherFlushUnsettled); + } + if self.flush_ledger.lost() != lost_before { + return Err(NotDurable::Lost); } - flushed + if self.flush_ledger.failed() != failed_before { + return Err(NotDurable::OtherFlushUnsettled); + } + 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 + /// 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 + /// 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 + /// 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, 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> { + // 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); + } + 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(()) + } + + /// 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 @@ -724,11 +971,8 @@ impl PlatformWalletPersistence for NoPlatformPersistence { #[cfg(test)] 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 @@ -758,6 +1002,439 @@ 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 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 wallet_id: WalletId = [3; 32]; + 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::new(wallet_id, Arc::clone(&backend) as _); + 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" + ); + } + + /// 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, + /// 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 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. + #[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::OtherFlushUnsettled)), + "{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 @@ -820,4 +1497,133 @@ 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" + ); + } + + /// 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 c67a5462f70..6647ba890f7 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -3115,31 +3115,31 @@ 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 } - /// 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. + /// 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.store_commits_inline() { - return true; - } - match self.persister.flush() { + match self.persister.flush_durably() { Ok(()) => true, - Err(error) => { + 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" ); false } @@ -3266,10 +3266,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 { 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