From 83a94984cb7629809ef7fe0c7ddbc35f2c865b68 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Fri, 25 Sep 2026 13:45:08 -0700 Subject: [PATCH 01/43] fix(platform-wallet): persist DashPay backfill coverage so a relaunch resumes instead of rewinding again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `reconcile_dashpay_rescan` lowers the SPV `synced_height` to the earliest contact-request height so payments that landed before a receival account's addresses were watched get re-matched. Its guard against re-lowering the cursor every pass, `DashPayState::rescan_triggered`, was in memory only, while the cursor it lowers is durable: the host persists every `SyncHeightAdvanced` the rescan climbs through, so a fresh process restored a cursor inside the climb, saw no guard, and rewound again — one re-walk of every filter from the earliest contact's core height per launch, forever (dashpay/platform#4302; 377,000 filters per launch on the mainnet reference wallet). Add `DashPayBackfillRecord`, a per-wallet durable record of which receival contacts the backfill covers and from which checkpoint each, plus the rescan's extent (`floor`, `rewound_from`). The reconcile writes it on the same persistence round as the lowered cursor, restores it through `ClientWalletStartState::dashpay_backfill` into `PlatformWalletInfo`, and treats a recorded contact as covered when its checkpoint has not dropped below the height it was recorded at — the scan then resumes from the restored cursor with the contact watched. A contact below its recorded height, or one the record never listed, is re-armed exactly as before. Completion is read off the cursor (`is_complete`), never marked at trigger, so an interrupted backfill resumes rather than reads as finished. The pinned engine's commit-time contiguity guard refuses to certify a batch scanned at the old checkpoint once the cursor sits below it, so an in-flight batch cannot lift the lowered cursor back over the rescanned range. The SQLite storage backend does not persist the record yet and keeps its pre-record behaviour (a rewind per launch, never lossy). Co-Authored-By: Claude Fable 5.1 --- .../src/sqlite/persister.rs | 4 + .../src/sqlite/schema/versions.rs | 4 + .../src/changeset/changeset.rs | 21 + .../changeset/client_wallet_start_state.rs | 8 + .../src/changeset/core_bridge.rs | 3 + .../src/changeset/dashpay_backfill.rs | 375 ++++++++++ .../rs-platform-wallet/src/changeset/mod.rs | 2 + .../src/manager/accessors.rs | 12 +- .../rs-platform-wallet/src/manager/load.rs | 5 + .../rs-platform-wallet/src/manager/startup.rs | 1 + .../src/manager/wallet_lifecycle.rs | 1 + .../rs-platform-wallet/src/test_support.rs | 5 + .../rs-platform-wallet/src/wallet/apply.rs | 5 + .../wallet/asset_lock/sync/reconstruction.rs | 1 + .../src/wallet/asset_lock/sync/recovery.rs | 1 + .../identity/network/contact_requests.rs | 1 + .../src/wallet/identity/network/payments.rs | 667 +++++++++++++++++- .../state/managed_identity/dashpay.rs | 20 +- .../identity/state/managed_identity/mod.rs | 3 +- .../src/wallet/platform_wallet.rs | 9 + .../src/wallet/platform_wallet_traits.rs | 2 + 21 files changed, 1113 insertions(+), 37 deletions(-) create mode 100644 packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs diff --git a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs index fd865405121..1dce2913e8a 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs @@ -1859,6 +1859,10 @@ fn load_one_wallet( // replay inert here, which is the behaviour this path had before the // field existed. unconfirmed_outgoing_txs: Vec::new(), + // Not persisted by this backend yet: the backfill record rides the + // FFI hosts' wallet row only. Empty keeps the rescan sweep on its + // pre-record behaviour here (a rewind per launch, never lossy). + dashpay_backfill: Default::default(), }) } diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/versions.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/versions.rs index bfbb9af6486..de2b418cae1 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/versions.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/versions.rs @@ -144,6 +144,10 @@ pub fn touched_domains(cs: &PlatformWalletChangeSet) -> Vec { invitations, dpns_name_states, identity_scan_state, + // Not persisted by this backend yet (see `ClientWalletStartState:: + // dashpay_backfill` on the load side): the record rides the FFI + // hosts' wallet row only, so it touches no domain here. + dashpay_backfill: _, #[cfg(feature = "shielded")] shielded, #[cfg(not(feature = "shielded"))] diff --git a/packages/rs-platform-wallet/src/changeset/changeset.rs b/packages/rs-platform-wallet/src/changeset/changeset.rs index 1fe4d1eb514..87d195d4bf1 100644 --- a/packages/rs-platform-wallet/src/changeset/changeset.rs +++ b/packages/rs-platform-wallet/src/changeset/changeset.rs @@ -34,6 +34,7 @@ use key_wallet::managed_account::address_pool::{AddressPool, AddressPoolType}; use key_wallet::managed_account::transaction_record::TransactionRecord; use key_wallet::{AddressInfo, Network, PlatformP2PKHAddress, Utxo}; +use crate::changeset::dashpay_backfill::DashPayBackfillRecord; use crate::changeset::identity_scan_state::IdentityScanStateEntry; use crate::wallet::platform_wallet::WalletId; @@ -2153,6 +2154,20 @@ pub struct PlatformWalletChangeSet { /// scan is retried inside its own launch — but honouring the verdict /// across launches needs the host slot. pub identity_scan_state: Option, + /// Durable record of the DashPay coreHeight backfill — which receival + /// contacts the historical rescan covers, from which height each, and + /// the extent of the rescan. Written by `reconcile_dashpay_rescan` on the + /// same round as the lowered `core.synced_height` it belongs with, and + /// read back through + /// [`ClientWalletStartState::dashpay_backfill`](crate::changeset::ClientWalletStartState::dashpay_backfill) + /// so a fresh process does not rewind for a contact the previous one + /// already rewound for (dashpay/platform#4302). Every write carries the + /// whole record, so merge is last-write-wins. See [`DashPayBackfillRecord`]. + /// + /// Durability caveat, the same one `identity_scan_state` carries: a host + /// that has not adopted the slot keeps today's behaviour — the backfill + /// re-fires on every launch — which is slow but never lossy. + pub dashpay_backfill: Option, /// Per-account registration entries emitted at registration / on /// later `add_account` calls. See [`AccountRegistrationEntry`] for /// the merge policy (plain `Vec::extend`, dedup is the apply-side @@ -2304,6 +2319,11 @@ impl Merge for PlatformWalletChangeSet { None => scan, }); } + // Backfill record: every write is a whole snapshot, so the later + // one wins outright. + if let Some(record) = other.dashpay_backfill { + self.dashpay_backfill = Some(record); + } // Per-account specs and address-pool snapshots: append-only. // See the type docstrings for the rationale (registration // round emits each key once; snapshots are whole-pool, so @@ -2340,6 +2360,7 @@ impl Merge for PlatformWalletChangeSet { .is_none_or(|m| m.is_empty()) && self.wallet_metadata.is_none() && self.identity_scan_state.is_none() + && self.dashpay_backfill.is_none() && self.account_registrations.is_empty() && self.provider_key_account_registrations.is_empty() && self.account_address_pools.is_empty() diff --git a/packages/rs-platform-wallet/src/changeset/client_wallet_start_state.rs b/packages/rs-platform-wallet/src/changeset/client_wallet_start_state.rs index 6eab3d88780..90f18b08e5a 100644 --- a/packages/rs-platform-wallet/src/changeset/client_wallet_start_state.rs +++ b/packages/rs-platform-wallet/src/changeset/client_wallet_start_state.rs @@ -7,6 +7,7 @@ use std::collections::BTreeMap; +use crate::changeset::dashpay_backfill::DashPayBackfillRecord; use crate::changeset::identity_manager_start_state::IdentityManagerStartState; use crate::wallet::asset_lock::tracked::TrackedAssetLock; use dashcore::{OutPoint, Transaction}; @@ -60,4 +61,11 @@ pub struct ClientWalletStartState { /// permanently. Replaying the record at load restores exactly the /// state the live process held. pub unconfirmed_outgoing_txs: Vec, + /// Durable record of the DashPay coreHeight backfill, as the host last + /// stored it — which receival contacts the historical rescan already + /// covers, from which height each. Empty (the default) when the host + /// never stored one, or does not persist the slot at all; the first + /// rescan sweep then behaves exactly as before and writes one. See + /// [`DashPayBackfillRecord`]. + pub dashpay_backfill: DashPayBackfillRecord, } diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index 574715eed6b..de8c7839e9f 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -3803,6 +3803,7 @@ mod contact_watch_only_projection_tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); @@ -7203,6 +7204,7 @@ mod utxo_credit_verdict_tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); @@ -7297,6 +7299,7 @@ mod utxo_credit_verdict_tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs new file mode 100644 index 00000000000..7aa615885f7 --- /dev/null +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -0,0 +1,375 @@ +//! Durable record of the DashPay coreHeight backfill (DIP-15 §12.6). +//! +//! `reconcile_dashpay_rescan` lowers the wallet's SPV `synced_height` to the +//! earliest contact-request height among receival contacts whose addresses +//! were not yet watched when those blocks were first scanned, so the filter +//! scanner re-matches that range with the contact's addresses in the set. +//! The guard that stops the recurring sweep from re-lowering the cursor every +//! pass used to live only in memory (`DashPayState::rescan_triggered`), while +//! the cursor it lowers is durable: the host persists every +//! `SyncHeightAdvanced` the engine emits as the rescan climbs. A fresh process +//! therefore restored a cursor *inside* the climb, saw no guard, and rewound +//! again — one full re-walk per launch, forever (dashpay/platform#4302). +//! +//! [`DashPayBackfillRecord`] is the guard's durable half. It travels with the +//! wallet's own changeset round (the one that also carries the lowered +//! cursor), crosses the FFI as its own size-negotiated persistence slot and a +//! wallet-restore field, and comes back through +//! [`ClientWalletStartState::dashpay_backfill`]. +//! +//! [`ClientWalletStartState::dashpay_backfill`]: crate::changeset::ClientWalletStartState::dashpay_backfill + +use dpp::prelude::Identifier; + +/// One receival contact the backfill covers, and the height it covers it +/// from. +/// +/// `covered_from` is the contact's scan checkpoint at the moment its coverage +/// was recorded — the earliest height whose block the scanner is guaranteed to +/// test against this contact's addresses. It is stored per contact rather than +/// as one wallet-wide floor because a contact's checkpoint can *drop* after it +/// was covered (the reciprocal request turns out to be older than ours, or a +/// rotation falls back to wallet birth): a wallet-wide floor would then read +/// as "still covered" for blocks the scan never tested with that contact. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct DashPayBackfillCoveredContact { + /// Our identity — the owner of the receival account. + pub owner: Identifier, + /// The contact whose payments the receival account collects. + pub contact: Identifier, + /// Lowest height the scan is guaranteed to have tested (or to test, as it + /// climbs) with this contact's addresses watched. + pub covered_from: u32, +} + +/// Per-wallet durable record of the DashPay coreHeight backfill. +/// +/// # What a record claims +/// +/// For every entry in [`covered`](Self::covered): from the moment the record +/// was written, the contact's receival addresses are in the filter match set, +/// and the durable `synced_height` was lowered to at most that contact's +/// `covered_from` in the very same persistence round. From then on the +/// persisted cursor tracks the scanner's real progress *with the contact +/// watched* — the pinned engine refuses to certify a batch scanned at the old +/// checkpoint once the cursor sits below it (contiguity guard, +/// dashpay/rust-dashcore#649) — so on any later launch every block in +/// `[covered_from, synced_height]` has been tested against that contact, and +/// everything above the cursor is about to be. Such a contact needs no second +/// rewind unless its checkpoint has since dropped *below* `covered_from`. +/// +/// # What it does not claim +/// +/// Nothing about a receival account the record does not list. A contact whose +/// account was registered after the record was written (or on this device by +/// a path that never recorded it) is re-armed exactly as before, because the +/// scan that produced this record never watched it. Any ambiguity falls back +/// to re-running the backfill: slow, never lossy. +/// +/// # Completion +/// +/// [`floor`](Self::floor) and [`rewound_from`](Self::rewound_from) describe +/// the backfill's extent: the lowest height it rewound to and the cursor it +/// rewound from. The backfill is complete once `synced_height` has climbed +/// back past `rewound_from` ([`is_complete`](Self::is_complete)); while it is +/// still climbing ([`is_pending`](Self::is_pending)) a restart resumes the +/// climb from the persisted cursor rather than restarting it from the floor. +/// Completion is never *marked* — it is read off the cursor, which is the +/// only thing that actually moved. Marking it at trigger time would have made +/// an interrupted backfill look finished; not marking it costs nothing, +/// because the per-contact `covered_from` entries, not the completion state, +/// are what suppress a second rewind. +/// +/// A later rewind folds into the existing record ([`record_pass`](Self::record_pass)): +/// the floor can only go down, `rewound_from` can only go up, and a contact's +/// entry is replaced by the new checkpoint it was covered from. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct DashPayBackfillRecord { + /// Lowest height a backfill rewound the cursor to. Meaningless while + /// `covered` is empty. + pub floor: u32, + /// Highest cursor a backfill rewound from — the height the scan has to + /// climb back to for the backfill to be complete. Meaningless while + /// `covered` is empty. + pub rewound_from: u32, + /// Receival contacts the backfill covers, sorted by `(owner, contact)`, + /// at most one entry per pair. + pub covered: Vec, +} + +impl DashPayBackfillRecord { + /// Whether no backfill has ever been recorded for this wallet. + pub fn is_empty(&self) -> bool { + self.covered.is_empty() + } + + /// The height this record covers `(owner, contact)` from, if it covers + /// it at all. + pub fn covered_from(&self, owner: &Identifier, contact: &Identifier) -> Option { + self.covered + .binary_search_by(|entry| (entry.owner, entry.contact).cmp(&(*owner, *contact))) + .ok() + .map(|index| self.covered[index].covered_from) + } + + /// Whether `(owner, contact)` is covered at `checkpoint`: the record lists + /// it and its checkpoint has not dropped below the height it was covered + /// from. + pub fn covers(&self, owner: &Identifier, contact: &Identifier, checkpoint: u32) -> bool { + self.covered_from(owner, contact) + .is_some_and(|covered_from| checkpoint >= covered_from) + } + + /// The backfill is still climbing back to the cursor it rewound from. + pub fn is_pending(&self, synced_height: u32) -> bool { + !self.is_empty() && synced_height < self.rewound_from + } + + /// The scan has climbed back past the cursor the backfill rewound from, + /// so every block in `[floor, rewound_from]` has been re-matched. + pub fn is_complete(&self, synced_height: u32) -> bool { + !self.is_empty() && synced_height >= self.rewound_from + } + + /// Fold one reconcile pass into the record. + /// + /// `synced_height_before` is the cursor before the pass rewound it (the + /// height the scan must climb back to); `floor` is where the pass rewound + /// to, or `None` when every contact it handled was already covered by the + /// forward scan. Each `(owner, contact, covered_from)` entry replaces any + /// existing entry for the pair. + /// + /// Returns `true` when the record changed. + pub fn record_pass( + &mut self, + synced_height_before: u32, + floor: Option, + entries: impl IntoIterator, + ) -> bool { + let was_empty = self.covered.is_empty(); + let mut changed = false; + for (owner, contact, covered_from) in entries { + let entry = DashPayBackfillCoveredContact { + owner, + contact, + covered_from, + }; + match self.covered.binary_search_by(|existing| { + (existing.owner, existing.contact).cmp(&(owner, contact)) + }) { + Ok(index) => { + if self.covered[index] != entry { + self.covered[index] = entry; + changed = true; + } + } + Err(index) => { + self.covered.insert(index, entry); + changed = true; + } + } + } + if self.covered.is_empty() { + return changed; + } + // A first record takes the pass's extent as-is; a later pass can only + // widen it — deeper floor, higher climb target. + let pass_floor = floor.unwrap_or(synced_height_before); + let (new_floor, new_rewound_from) = if was_empty { + (pass_floor, synced_height_before) + } else { + ( + self.floor.min(pass_floor), + self.rewound_from.max(synced_height_before), + ) + }; + if new_floor != self.floor || new_rewound_from != self.rewound_from { + self.floor = new_floor; + self.rewound_from = new_rewound_from; + changed = true; + } + changed + } + + /// Drop every entry for which `keep` is false — used to forget contacts + /// whose receival account no longer exists. Returns `true` when the record + /// changed. + pub fn retain(&mut self, mut keep: impl FnMut(&Identifier, &Identifier) -> bool) -> bool { + let before = self.covered.len(); + self.covered + .retain(|entry| keep(&entry.owner, &entry.contact)); + before != self.covered.len() + } + + /// Wire size of one [`DashPayBackfillCoveredContact`] in + /// [`covered_bytes`](Self::covered_bytes): owner ‖ contact ‖ `covered_from` + /// (little-endian). + pub const COVERED_ENTRY_LEN: usize = 32 + 32 + 4; + + /// Flat encoding of [`covered`](Self::covered) for hosts that store the + /// list as one opaque blob: [`COVERED_ENTRY_LEN`](Self::COVERED_ENTRY_LEN) + /// bytes per entry, in record order. + pub fn covered_bytes(&self) -> Vec { + let mut out = Vec::with_capacity(self.covered.len() * Self::COVERED_ENTRY_LEN); + for entry in &self.covered { + out.extend_from_slice(&entry.owner.to_buffer()); + out.extend_from_slice(&entry.contact.to_buffer()); + out.extend_from_slice(&entry.covered_from.to_le_bytes()); + } + out + } + + /// Inverse of [`covered_bytes`](Self::covered_bytes). + /// + /// Returns `None` when `bytes` is not a whole number of entries: a + /// truncated or foreign blob must not be read as a shorter cover set, + /// because a contact missing from the set is simply re-armed while a + /// contact wrongly *present* in it is never backfilled again. The caller + /// treats `None` as "no record" — the safe direction. + pub fn from_parts(floor: u32, rewound_from: u32, covered_bytes: &[u8]) -> Option { + if !covered_bytes.len().is_multiple_of(Self::COVERED_ENTRY_LEN) { + return None; + } + let (chunks, _) = covered_bytes.as_chunks::<{ Self::COVERED_ENTRY_LEN }>(); + let mut covered: Vec = chunks + .iter() + .map(|chunk| { + let mut owner = [0u8; 32]; + let mut contact = [0u8; 32]; + let mut height = [0u8; 4]; + owner.copy_from_slice(&chunk[..32]); + contact.copy_from_slice(&chunk[32..64]); + height.copy_from_slice(&chunk[64..68]); + DashPayBackfillCoveredContact { + owner: Identifier::from(owner), + contact: Identifier::from(contact), + covered_from: u32::from_le_bytes(height), + } + }) + .collect(); + // Canonical order and one entry per pair, whatever the host stored. + covered.sort_by_key(|entry| (entry.owner, entry.contact)); + covered.dedup_by(|a, b| (a.owner, a.contact) == (b.owner, b.contact)); + Some(Self { + floor, + rewound_from, + covered, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn id(byte: u8) -> Identifier { + Identifier::from([byte; 32]) + } + + #[test] + fn a_first_pass_sets_the_extent_and_a_later_one_only_widens_it() { + let mut record = DashPayBackfillRecord::default(); + assert!(record.is_empty()); + assert!(!record.is_pending(0)); + assert!(!record.is_complete(u32::MAX)); + + assert!(record.record_pass(1_000, Some(100), [(id(1), id(2), 100)])); + assert_eq!((record.floor, record.rewound_from), (100, 1_000)); + assert!(record.is_pending(999)); + assert!(record.is_complete(1_000)); + + // A shallower rewind while climbing neither raises the floor nor + // lowers the climb target. + assert!(record.record_pass(400, Some(300), [(id(1), id(3), 300)])); + assert_eq!((record.floor, record.rewound_from), (100, 1_000)); + + // A deeper rewind after completion lowers the floor and raises the + // climb target to the cursor it rewound from. + assert!(record.record_pass(2_000, Some(50), [(id(1), id(4), 50)])); + assert_eq!((record.floor, record.rewound_from), (50, 2_000)); + + // A forward-covered pass (no rewind) records the contact without + // touching the extent it cannot widen. + assert!(record.record_pass(1_500, None, [(id(1), id(5), 1_700)])); + assert_eq!((record.floor, record.rewound_from), (50, 2_000)); + assert_eq!(record.covered_from(&id(1), &id(5)), Some(1_700)); + + // An unchanged replay is reported as such. + assert!(!record.record_pass(1_500, None, [(id(1), id(5), 1_700)])); + } + + #[test] + fn coverage_is_per_contact_and_keyed_on_the_recorded_checkpoint() { + let mut record = DashPayBackfillRecord::default(); + record.record_pass(1_000, Some(100), [(id(1), id(2), 100), (id(1), id(3), 300)]); + + assert!(record.covers(&id(1), &id(2), 100)); + assert!(record.covers(&id(1), &id(2), 250)); + assert!( + !record.covers(&id(1), &id(2), 99), + "a checkpoint below the covered height re-arms" + ); + assert!( + !record.covers(&id(1), &id(9), 500), + "an unlisted contact is never covered" + ); + assert!( + !record.covers(&id(7), &id(2), 500), + "coverage is per owner, too" + ); + + // Re-recording a pair replaces its entry. + assert!(record.record_pass(500, Some(40), [(id(1), id(2), 40)])); + assert_eq!(record.covered_from(&id(1), &id(2)), Some(40)); + assert_eq!(record.covered.len(), 2); + } + + #[test] + fn the_flat_encoding_round_trips_and_refuses_a_torn_blob() { + let mut record = DashPayBackfillRecord::default(); + record.record_pass(1_000, Some(100), [(id(9), id(2), 100), (id(1), id(3), 300)]); + let bytes = record.covered_bytes(); + assert_eq!(bytes.len(), 2 * DashPayBackfillRecord::COVERED_ENTRY_LEN); + + let restored = DashPayBackfillRecord::from_parts(record.floor, record.rewound_from, &bytes) + .expect("a whole blob decodes"); + assert_eq!(restored, record); + + assert!( + DashPayBackfillRecord::from_parts(100, 1_000, &bytes[..bytes.len() - 1]).is_none(), + "a torn blob must read as no record, never as a shorter cover set" + ); + let empty = DashPayBackfillRecord::from_parts(0, 0, &[]).expect("empty decodes"); + assert!(empty.is_empty()); + } + + #[test] + fn a_host_stored_blob_is_canonicalised_on_the_way_in() { + let entry = |owner: u8, contact: u8, from: u32| { + let mut bytes = Vec::new(); + bytes.extend_from_slice(&[owner; 32]); + bytes.extend_from_slice(&[contact; 32]); + bytes.extend_from_slice(&from.to_le_bytes()); + bytes + }; + let mut bytes = entry(5, 6, 700); + bytes.extend(entry(1, 2, 100)); + bytes.extend(entry(5, 6, 900)); // duplicate pair + let record = DashPayBackfillRecord::from_parts(100, 1_000, &bytes).expect("decodes"); + assert_eq!(record.covered.len(), 2); + assert_eq!(record.covered[0].owner, id(1)); + assert_eq!(record.covered_from(&id(5), &id(6)), Some(700)); + } + + #[test] + fn retain_forgets_contacts_and_reports_whether_anything_went() { + let mut record = DashPayBackfillRecord::default(); + record.record_pass(1_000, Some(100), [(id(1), id(2), 100), (id(1), id(3), 300)]); + assert!(!record.retain(|_, _| true)); + assert!(record.retain(|_, contact| *contact != id(3))); + assert_eq!(record.covered_from(&id(1), &id(3)), None); + assert_eq!(record.covered_from(&id(1), &id(2)), Some(100)); + } +} diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 4125b8df568..5234052c1ad 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -13,6 +13,7 @@ pub mod changeset; pub mod client_start_state; pub mod client_wallet_start_state; pub mod core_bridge; +pub mod dashpay_backfill; pub mod identity_manager_start_state; pub mod identity_scan_state; pub mod merge; @@ -46,6 +47,7 @@ pub use changeset::{ pub use client_start_state::ClientStartState; pub use client_wallet_start_state::ClientWalletStartState; pub use core_bridge::spawn_wallet_event_adapter; +pub use dashpay_backfill::{DashPayBackfillCoveredContact, DashPayBackfillRecord}; pub use identity_manager_start_state::IdentityManagerStartState; pub use identity_scan_state::IdentityScanStateEntry; pub use merge::Merge; diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index 1f9ed240c84..b94b76273cd 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -836,13 +836,14 @@ impl PlatformWalletManager

{ /// /// `synced_height` may regress here: that is safe because it is the /// filter-scan checkpoint, decoupled from the monotonic - /// `last_processed_height`, and every persisted sync cursor is - /// monotonic-max guarded (see `reconcile_dashpay_rescan`'s note), so a - /// transient rewind cannot corrupt state or persist a lower cursor. + /// `last_processed_height`, so a transient rewind cannot corrupt state. /// /// The rewound checkpoint lives only in the in-memory `WalletManager`; this - /// call does not persist it. If the process dies before the rescan finishes, - /// the host must issue this request again after restart. Requires SPV + /// call does not persist it — though the host will persist every height + /// the rescan then climbs through, so a relaunch resumes the climb from + /// wherever it got to rather than from the high-water. If the process dies + /// before the rescan finishes and the host wants it restarted from + /// `from_height`, it must issue this request again after restart. Requires SPV /// running for an immediate effect; otherwise it takes effect when SPV next /// starts in the same process and its filter loop first ticks. /// @@ -1946,6 +1947,7 @@ mod txo_inventory_tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 1bbd0330172..64688a1f12d 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -129,6 +129,7 @@ impl PlatformWalletManager

{ identity_manager, unused_asset_locks, unconfirmed_outgoing_txs, + dashpay_backfill, } = wallet_state; // Replay the sends the host still holds as unconfirmed, before @@ -229,6 +230,7 @@ impl PlatformWalletManager

{ identity_manager: IdentityManager::from(identity_manager), tracked_asset_locks, dpns_name_states: std::collections::BTreeMap::new(), + dashpay_backfill, }; // Seed the double-spend screen's session memory from the // freshly restored state: it closes the race where SPV's @@ -718,6 +720,7 @@ mod idempotent_load_tests { identity_manager: IdentityManagerStartState::default(), unused_asset_locks: BTreeMap::new(), unconfirmed_outgoing_txs: self.pending.clone(), + dashpay_backfill: Default::default(), }, ); Ok(ClientStartState { @@ -784,6 +787,7 @@ mod idempotent_load_tests { identity_manager: IdentityManagerStartState::default(), unused_asset_locks: BTreeMap::new(), unconfirmed_outgoing_txs: Vec::new(), + dashpay_backfill: Default::default(), }, ); Ok(ClientStartState { @@ -823,6 +827,7 @@ mod idempotent_load_tests { identity_manager: IdentityManagerStartState::default(), unused_asset_locks: BTreeMap::new(), unconfirmed_outgoing_txs: Vec::new(), + dashpay_backfill: Default::default(), }; let mut wallets = BTreeMap::new(); wallets.insert(self.wallet.compute_wallet_id(), entry()); diff --git a/packages/rs-platform-wallet/src/manager/startup.rs b/packages/rs-platform-wallet/src/manager/startup.rs index 04c07d50832..27a1e76e5f3 100644 --- a/packages/rs-platform-wallet/src/manager/startup.rs +++ b/packages/rs-platform-wallet/src/manager/startup.rs @@ -1842,6 +1842,7 @@ mod tests { identity_manager: crate::changeset::IdentityManagerStartState::default(), unused_asset_locks: std::collections::BTreeMap::new(), unconfirmed_outgoing_txs: Vec::new(), + dashpay_backfill: Default::default(), }, ); Ok(crate::changeset::ClientStartState { diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index bc211ce76cc..2dd8519c15f 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -370,6 +370,7 @@ impl PlatformWalletManager

{ identity_manager: crate::wallet::identity::IdentityManager::new(), tracked_asset_locks: std::collections::BTreeMap::new(), dpns_name_states: std::collections::BTreeMap::new(), + dashpay_backfill: Default::default(), }; wallet.downgrade_to_external_signable(); diff --git a/packages/rs-platform-wallet/src/test_support.rs b/packages/rs-platform-wallet/src/test_support.rs index b9446432ee0..a41e03967c4 100644 --- a/packages/rs-platform-wallet/src/test_support.rs +++ b/packages/rs-platform-wallet/src/test_support.rs @@ -256,6 +256,7 @@ pub async fn funded_wallet_manager_with_outputs( identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); @@ -385,6 +386,7 @@ pub(crate) async fn funded_wallet_manager_dual_standard( identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); let wallet_id = wm.insert_wallet(ctx.wallet, info).expect("insert wallet"); @@ -488,6 +490,7 @@ pub(crate) async fn funded_wallet_manager_with_contact( identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); let wallet_id = wm.insert_wallet(ctx.wallet, info).expect("insert wallet"); @@ -565,6 +568,7 @@ pub(crate) async fn funded_coinjoin_wallet_manager() -> ( identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); @@ -770,6 +774,7 @@ pub(crate) async fn mnemonic_wallet_manager( identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); diff --git a/packages/rs-platform-wallet/src/wallet/apply.rs b/packages/rs-platform-wallet/src/wallet/apply.rs index 10780172b4a..24b70da93cd 100644 --- a/packages/rs-platform-wallet/src/wallet/apply.rs +++ b/packages/rs-platform-wallet/src/wallet/apply.rs @@ -107,6 +107,10 @@ impl PlatformWalletInfo { invitations: _, dpns_name_states, identity_scan_state, + // The backfill record is persistence-only here: the reconcile + // sweep mutates `PlatformWalletInfo::dashpay_backfill` directly + // and the start-state path restores it at load. No replay hook. + dashpay_backfill: _, // Registration-round metadata / per-account specs / // per-pool snapshots are persistence-only — the // canonical in-memory wallet state is built up at @@ -440,6 +444,7 @@ mod tests { tracked_asset_locks: BTreeMap::new(), observed_input_conflicts: Default::default(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), } } diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs index a14417dba53..06a5f44c4c9 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs @@ -664,6 +664,7 @@ mod tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), }; assert_eq!( funding_account_index(&info, &txid), diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs index e4fc8302234..b1741b2a6ec 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs @@ -2524,6 +2524,7 @@ mod tests { identity_manager: IdentityManager::new(), tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), }; let out_point = OutPoint::new(tx.txid(), 0); let lock = TrackedAssetLock { 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 8f95fed9279..045e5a3b0db 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 @@ -4179,6 +4179,7 @@ mod sweep_tests { tracked_asset_locks: BTreeMap::new(), observed_input_conflicts: Default::default(), dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), } } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 940b8da96ec..287efdbaa9d 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -86,21 +86,54 @@ impl DashPayView<'_, B> { /// `$coreHeightCreatedAt` across established receival contacts that haven't /// been rescanned yet — the filter manager (`dash-spv`) then re-downloads /// nothing it already has, re-matches the now-larger script set, and - /// re-requests the matching blocks. Each contact is recorded in - /// [`DashPayState::rescan_triggered`](crate::wallet::identity::DashPayState) so the recurring sweep does - /// not re-lower the height every pass (which would reset the in-flight - /// backfill and keep it from ever completing). The guard is in-memory, so a - /// relaunch — where `synced_height` is restored at its high-water — safely - /// re-triggers an interrupted backfill. + /// re-requests the matching blocks. + /// + /// # The guard, in memory and on disk + /// + /// Each contact the pass handles is recorded twice. In memory, in + /// [`DashPayState::rescan_triggered`](crate::wallet::identity::DashPayState), + /// so the recurring sweep does not re-lower the height every pass (which + /// would reset the in-flight backfill and keep it from ever completing); + /// the contact-request state transitions clear that mark when a request + /// changes, so the next pass re-evaluates the contact. And durably, in the + /// wallet's [`DashPayBackfillRecord`], keyed per contact on the checkpoint + /// it was covered from, written on the same persistence round as the + /// lowered cursor. + /// + /// The durable half exists because the cursor this pass lowers is itself + /// durable: the host persists every `SyncHeightAdvanced` the engine emits + /// as the rescan climbs, so a relaunch restores `synced_height` *inside* + /// the climb, not at its high-water. With only the in-memory guard, every + /// fresh process rewound again — on a contact-heavy mainnet wallet a + /// re-walk of hundreds of thousands of filters per launch, never finishing + /// (dashpay/platform#4302). Now a contact the record covers at a + /// checkpoint no lower than the one it was covered from is treated as + /// covered: the scan resumes from the restored cursor with the contact's + /// addresses watched, which is exactly where the previous process left it. + /// A contact whose checkpoint has dropped below its recorded height, or + /// one the record never listed (registered since, or by a path that did + /// not record it), is re-armed as before — the record only ever suppresses + /// a rewind it can vouch for. + /// + /// Nothing is marked "complete" at trigger time: the record's extent + /// (`floor`, `rewound_from`) is descriptive, and completion is read off the + /// cursor ([`DashPayBackfillRecord::is_complete`]). An interrupted + /// backfill therefore resumes rather than restarts, and never reads as + /// finished while the cursor still sits below `rewound_from`. /// /// `synced_height` may regress here: that is safe because it is the /// filter-scan checkpoint, decoupled from the monotonic - /// `last_processed_height`, and every persisted sync cursor is monotonic-max - /// guarded, so a transient rewind cannot corrupt state or persist a lower - /// cursor. Only the **receival** account matters — `DashpayExternalAccount` - /// is outbound and never receives. Returns the floor the height was lowered - /// to, or `None`. + /// `last_processed_height`. The pinned engine refuses to certify a batch + /// scanned at the old checkpoint once the cursor sits below it, so an + /// in-flight batch cannot lift the rewound cursor back over the range it + /// never tested with the new addresses. Only the **receival** account + /// matters — `DashpayExternalAccount` is outbound and never receives. + /// Returns the floor the height was lowered to, or `None`. + /// + /// [`DashPayBackfillRecord`]: crate::changeset::DashPayBackfillRecord + /// [`DashPayBackfillRecord::is_complete`]: crate::changeset::DashPayBackfillRecord::is_complete pub async fn reconcile_dashpay_rescan(&self) -> Result, PlatformWalletError> { + use crate::changeset::{CoreChangeSet, PlatformWalletChangeSet}; use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; let mut wm = self.wallet_manager.write().await; @@ -138,21 +171,42 @@ impl DashPayView<'_, B> { // is payable only once both requests exist, so the earlier of the two is // the conservative-correct lower bound. let mut floor: Option = None; - let mut to_mark: Vec<(Identifier, Identifier)> = Vec::new(); - for (owner, contact) in receival_pairs { + // Contacts this pass handles, with the checkpoint each is covered from. + let mut to_mark: Vec<(Identifier, Identifier, u32)> = Vec::new(); + // Contacts already marked in memory whose durable entry is missing or + // stale (a pass whose record write failed, or a mark that predates the + // record), so the next launch does not rewind for them again. + let mut to_record: Vec<(Identifier, Identifier, u32)> = Vec::new(); + // Contacts the durable record vouched for this pass. + let mut restored: Vec<(Identifier, Identifier)> = Vec::new(); + for (owner, contact) in receival_pairs.iter().copied() { let Some(managed) = info.identity_manager.managed_identity(&owner) else { continue; }; - if managed.dashpay().rescan_triggered.contains(&contact) { - continue; - } let Some(established) = managed.dashpay().established_contacts().get(&contact) else { continue; }; - let funding = established + let checkpoint = established .outgoing_request .core_height_created_at .min(established.incoming_request.core_height_created_at); + if managed.dashpay().rescan_triggered.contains(&contact) { + if info + .dashpay_backfill + .covered_from(&owner, &contact) + .is_none_or(|covered_from| covered_from > checkpoint) + { + to_record.push((owner, contact, checkpoint)); + } + continue; + } + // The previous process already rewound for this contact and the + // persisted cursor tracked that scan with its addresses watched, + // so nothing below the cursor is missing: resume, don't restart. + if info.dashpay_backfill.covers(&owner, &contact, checkpoint) { + restored.push((owner, contact)); + continue; + } // Contacts funded below the tip need a backfill — their addresses // weren't watched when those blocks were first scanned. Contacts // funded at or after the tip are already covered by the ongoing @@ -161,13 +215,32 @@ impl DashPayView<'_, B> { // forward pointer later climbs past a still-forward-covered // contact's funding height, the recurring sweep must NOT then // rewind to it and redundantly re-scan an already-scanned range. - if funding < synced_height { - floor = Some(floor.map_or(funding, |cur| cur.min(funding))); + if checkpoint < synced_height { + floor = Some(floor.map_or(checkpoint, |cur| cur.min(checkpoint))); + } + to_mark.push((owner, contact, checkpoint)); + } + + let restored_count = restored.len(); + for (owner, contact) in restored { + if let Some(managed) = info.identity_manager.managed_identity_mut(&owner) { + managed.dashpay_rescan_triggered_mut().insert(contact); } - to_mark.push((owner, contact)); + } + if restored_count > 0 { + tracing::info!( + wallet_id = %hex::encode(self.wallet_id), + contacts = restored_count, + synced_height, + floor = info.dashpay_backfill.floor, + rewound_from = info.dashpay_backfill.rewound_from, + pending = info.dashpay_backfill.is_pending(synced_height), + "DashPay rescan: persisted backfill record covers these contacts; resuming from the \ + restored cursor instead of rewinding again" + ); } - if to_mark.is_empty() { + if to_mark.is_empty() && to_record.is_empty() { return Ok(None); } @@ -179,19 +252,58 @@ impl DashPayView<'_, B> { info.core_wallet.update_synced_height(floor); } let triggered = to_mark.len(); - for (owner, contact) in to_mark { - if let Some(managed) = info.identity_manager.managed_identity_mut(&owner) { - managed.dashpay_rescan_triggered_mut().insert(contact); + for (owner, contact, _) in &to_mark { + if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { + managed.dashpay_rescan_triggered_mut().insert(*contact); } } if let Some(floor) = floor { tracing::info!( wallet_id = %hex::encode(self.wallet_id), floor, + rewound_from = synced_height, contacts = triggered, "DashPay rescan: lowered SPV synced_height to backfill historical contact payments" ); } + + // Durable half of the guard. Every contact handled this pass is covered + // from its own checkpoint: a rewound one because the scan restarts at + // `floor <= checkpoint`, a forward-covered one because its addresses + // are watched from `synced_height <= checkpoint` on. Entries for + // accounts that no longer exist are dropped so the record tracks the + // live receival set. The record and the lowered cursor ride ONE round, + // so a host that stores the record has necessarily stored the cursor + // it vouches for — never a record over a cursor still at its + // high-water. + let record = &mut info.dashpay_backfill; + let mut changed = record.retain(|owner, contact| { + receival_pairs + .iter() + .any(|(o, c)| (o, c) == (owner, contact)) + }); + changed |= record.record_pass(synced_height, floor, to_mark.into_iter().chain(to_record)); + if changed { + let changeset = PlatformWalletChangeSet { + core: floor.map(|floor| CoreChangeSet { + synced_height: Some(floor), + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(record.clone()), + ..PlatformWalletChangeSet::default() + }; + if let Err(e) = self.persister.store(changeset) { + // The in-memory guard still holds for this process; the next + // launch re-runs the backfill for these contacts, which is the + // pre-record behaviour — slow, never lossy. + tracing::warn!( + wallet_id = %hex::encode(self.wallet_id), + error = %e, + "DashPay rescan: failed to persist the backfill record; the next launch \ + will re-run this backfill" + ); + } + } Ok(floor) } @@ -2965,6 +3077,513 @@ mod tests { assert_eq!(synced_height(&manager, wallet_id).await, 0); } + /// The persisted half of the rescan guard, captured from a live manager + /// the way a host stores it: the wallet, its managed info (accounts and + /// cursor), its identities with the in-memory marks cleared, and the + /// backfill record from the last round the manager stored. + struct ReloadPersister { + wallet: key_wallet::Wallet, + managed: key_wallet::wallet::ManagedWalletInfo, + identities: std::collections::BTreeMap< + crate::wallet::identity::RegistrationIndex, + crate::wallet::identity::ManagedIdentity, + >, + backfill: crate::changeset::DashPayBackfillRecord, + stores: Mutex>, + } + + impl PlatformWalletPersistence for ReloadPersister { + fn store( + &self, + wallet_id: WalletId, + changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + self.stores.lock().unwrap().push((wallet_id, changeset)); + Ok(()) + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Ok(()) + } + + fn load(&self) -> Result { + let wallet_id = self.wallet.compute_wallet_id(); + let mut wallet_identities = std::collections::BTreeMap::new(); + wallet_identities.insert(wallet_id, self.identities.clone()); + let mut wallets = std::collections::BTreeMap::new(); + wallets.insert( + wallet_id, + crate::changeset::ClientWalletStartState { + wallet: self.wallet.clone(), + wallet_info: self.managed.clone(), + identity_manager: crate::changeset::IdentityManagerStartState { + wallet_identities, + ..Default::default() + }, + unused_asset_locks: std::collections::BTreeMap::new(), + unconfirmed_outgoing_txs: Vec::new(), + dashpay_backfill: self.backfill.clone(), + }, + ); + Ok(ClientStartState { + wallets, + ..Default::default() + }) + } + } + + /// Snapshot `manager`'s wallet the way a relaunch would restore it: the + /// persisted cursor at `persisted_synced_height`, every in-memory + /// rescan mark gone, and the backfill record as the host last stored it + /// (`backfill`, or the record the manager holds when `None`). + async fn reload_snapshot( + manager: &Arc>, + wallet_id: WalletId, + owner: Identifier, + persisted_synced_height: u32, + backfill: Option, + ) -> ReloadPersister { + let wm = manager.wallet_manager.read().await; + let wallet = wm.get_wallet(&wallet_id).expect("wallet").clone(); + let info = wm.get_wallet_info(&wallet_id).expect("info"); + let mut managed = info.core_wallet.clone(); + managed.metadata.synced_height = persisted_synced_height; + let mut identity = info + .identity_manager + .managed_identity(&owner) + .expect("owner identity") + .clone(); + identity.dashpay_rescan_triggered_mut().clear(); + let mut identities = std::collections::BTreeMap::new(); + identities.insert(0, identity); + ReloadPersister { + wallet, + managed, + identities, + backfill: backfill.unwrap_or_else(|| info.dashpay_backfill.clone()), + stores: Mutex::default(), + } + } + + /// Bring a manager up from `persister` and hand back the restored wallet. + async fn reload( + persister: ReloadPersister, + ) -> ( + Arc>, + Arc, + WalletId, + ) { + let wallet_id = persister.wallet.compute_wallet_id(); + let persister = Arc::new(persister); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new( + sdk, + Arc::clone(&persister), + handler, + )); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + (manager, persister, wallet_id) + } + + /// The backfill record the manager last stored, from the newest round + /// that carried one. + fn last_stored_record( + stores: &[(WalletId, PlatformWalletChangeSet)], + ) -> Option<&PlatformWalletChangeSet> { + stores + .iter() + .rev() + .map(|(_, cs)| cs) + .find(|cs| cs.dashpay_backfill.is_some()) + } + + /// dashpay/platform#4302. The cursor a rewind lowers is durable — the + /// host persists every height the rescan climbs through — so a relaunch + /// restores it *inside* the climb. With only the in-memory guard every + /// fresh process rewound again, forever. The record the rewind stored + /// on its own round must make the next process resume from the restored + /// cursor instead; and it is the record doing that, not anything the + /// reload happens to carry: the same snapshot without it rewinds again. + #[tokio::test] + async fn rescan_resumes_from_the_persisted_cursor_after_a_reload_instead_of_rewinding_again() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + persister.stores.lock().unwrap().clear(); + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("first pass"), + Some(100) + ); + + // The record and the lowered cursor ride ONE round: a host that + // stores the record has stored the cursor it vouches for. + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the rewind must persist its record"); + assert_eq!( + round.core.as_ref().and_then(|core| core.synced_height), + Some(100), + "the lowered cursor must be on the same round as the record" + ); + let record = round.dashpay_backfill.as_ref().unwrap(); + assert_eq!((record.floor, record.rewound_from), (100, 1_000)); + assert_eq!(record.covered_from(&owner, &contact), Some(100)); + assert!(record.is_pending(100)); + assert!( + !record.is_complete(999), + "not complete until the scan climbs past 1000" + ); + assert!(record.is_complete(1_000)); + } + + // The process dies while the rescan is climbing: the host persisted + // the cursor at 400. + let snapshot = reload_snapshot(&manager, wallet_id, owner, 400, None).await; + let (restarted, restarted_persister, _) = reload(snapshot).await; + let restored = restarted + .get_wallet(&wallet_id) + .await + .expect("restored wallet"); + assert_eq!( + restored + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass after reload"), + None, + "a contact the persisted record covers must not be rewound for again" + ); + { + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!( + info.core_wallet.synced_height(), + 400, + "the scan resumes from the restored cursor, mid-climb" + ); + assert!( + info.identity_manager + .managed_identity(&owner) + .unwrap() + .dashpay() + .rescan_triggered + .contains(&contact), + "the record re-seeds the in-memory guard for this process" + ); + assert!(info.dashpay_backfill.is_pending(400)); + } + assert!( + restarted_persister + .stores + .lock() + .unwrap() + .iter() + .all(|(_, cs)| cs.dashpay_backfill.is_none()), + "nothing changed, so nothing is re-stored" + ); + + // The climb completes: still no rewind, and the record reads complete. + { + let mut wm = restarted.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.core_wallet.update_synced_height(1_000); + } + assert_eq!( + restored + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass at completion"), + None + ); + { + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert!(info.dashpay_backfill.is_complete(1_000)); + assert_eq!(info.core_wallet.synced_height(), 1_000); + } + + // Mutation check: the identical snapshot with the record withheld — + // the in-memory-only behaviour — rewinds again. Restoring that + // behaviour in the reconcile fails the assertions above, not this one. + let without = reload_snapshot( + &manager, + wallet_id, + owner, + 400, + Some(crate::changeset::DashPayBackfillRecord::default()), + ) + .await; + let (bare, _, _) = reload(without).await; + assert_eq!( + bare.get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass without a record"), + Some(100), + "without the persisted record the reload rewinds again — the defect" + ); + } + + /// A contact's checkpoint can drop after it was covered: the reciprocal + /// request turns out to be older than ours. The record covers it only + /// from the height it was recorded at, so the reload must still rewind + /// to the new, lower checkpoint — and the record's floor follows it + /// while its climb target stays. + #[tokio::test] + async fn rescan_reload_rewinds_for_a_contact_whose_checkpoint_dropped_below_its_covered_height() + { + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("first pass"), + Some(100) + ); + + // Before the relaunch the persisted contact state learned an older + // incoming request (50), while the record still says "covered from + // 100". + let mut snapshot = reload_snapshot(&manager, wallet_id, owner, 400, None).await; + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 50, 0); + snapshot + .identities + .get_mut(&0) + .unwrap() + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + assert_eq!(snapshot.backfill.covered_from(&owner, &contact), Some(100)); + + let (restarted, restarted_persister, _) = reload(snapshot).await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass after reload"), + Some(50), + "a checkpoint below the recorded height re-arms the contact" + ); + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.synced_height(), 50); + assert_eq!( + info.dashpay_backfill.covered_from(&owner, &contact), + Some(50) + ); + assert_eq!( + ( + info.dashpay_backfill.floor, + info.dashpay_backfill.rewound_from + ), + (50, 1_000), + "the floor lowers, the climb target stays" + ); + let stores = restarted_persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the deeper rewind persists its record"); + assert_eq!( + round.core.as_ref().and_then(|core| core.synced_height), + Some(50) + ); + } + + /// The record covers exactly the contacts whose rewind it was written + /// for. A receival account it never listed — registered after the record + /// was stored, or established on another device and only now built here + /// — is re-armed on reload regardless of how far the scan has climbed: + /// the scan the record vouches for never watched that contact. + #[tokio::test] + async fn rescan_reload_rearms_a_receival_contact_the_record_never_listed() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let covered = Identifier::from([0xBB; 32]); + let newcomer = Identifier::from([0xCC; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, covered, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("first pass"), + Some(100) + ); + let record = { + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .unwrap() + .dashpay_backfill + .clone() + }; + assert_eq!(record.covered.len(), 1); + + // The newcomer's account exists on this device by the time of the + // relaunch, but the record on disk predates it. + establish_receival_contact(&manager, &persister, wallet_id, owner, newcomer, 300, 300) + .await; + let snapshot = reload_snapshot(&manager, wallet_id, owner, 400, Some(record)).await; + let (restarted, _, _) = reload(snapshot).await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass after reload"), + Some(300), + "an unlisted receival contact is never treated as covered" + ); + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.synced_height(), 300); + assert_eq!( + info.dashpay_backfill.covered_from(&owner, &covered), + Some(100) + ); + assert_eq!( + info.dashpay_backfill.covered_from(&owner, &newcomer), + Some(300) + ); + assert_eq!( + ( + info.dashpay_backfill.floor, + info.dashpay_backfill.rewound_from + ), + (100, 1_000) + ); + } + + /// Registering the account of an already-established contact marks it in + /// memory without a reconcile pass ever seeing it as a candidate. The + /// next pass must still write it into the record — with no rewind, since + /// registration already lowered the cursor for it — or the following + /// launch rewinds for it once more. + #[tokio::test] + async fn rescan_records_a_contact_registration_marked_in_memory_so_a_reload_does_not_rewind_for_it( + ) { + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + { + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 100, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + info.core_wallet.update_synced_height(1_000); + } + // Established first, registered second: registration lowers the + // cursor to the checkpoint and marks the contact in memory. + iw.dashpay() + .register_contact_account(&owner, &contact, 0, test_receiving_xpub(&owner, &contact)) + .await + .expect("register receival account"); + assert_eq!(synced_height(&manager, wallet_id).await, 100); + persister.stores.lock().unwrap().clear(); + + assert_eq!( + iw.dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass after registration"), + None, + "registration already lowered the cursor; the pass must not rewind again" + ); + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the pass must record the contact"); + assert!( + round.core.is_none(), + "no rewind fired, so the round carries no cursor" + ); + assert_eq!( + round + .dashpay_backfill + .as_ref() + .unwrap() + .covered_from(&owner, &contact), + Some(100) + ); + } + + let snapshot = reload_snapshot(&manager, wallet_id, owner, 400, None).await; + let (restarted, _, _) = reload(snapshot).await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass after reload"), + None + ); + assert_eq!(synced_height_of(&restarted, wallet_id).await, 400); + } + + async fn synced_height_of( + manager: &Arc>, + wallet_id: WalletId, + ) -> u32 { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .synced_height() + } + /// A `Sent` payment must advance `Pending → Confirmed` once its /// transaction confirms on-chain. `send_payment` records it `Pending` /// and nothing else moved it, so before the confirm path was wired the 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 fb981b0ebd4..573e84aed6c 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 @@ -134,13 +134,19 @@ pub struct DashPayState { /// recurring sweep does not re-lower the height every pass — which would /// reset the in-flight backfill and prevent it from ever completing. /// - /// In-memory only (never persisted): a relaunch clears it, and because - /// `synced_height` is restored at its monotonic high-water, an interrupted - /// backfill is re-triggered on the next launch — self-healing. The cost of - /// that reset is one historical re-match per launch while any contact is - /// funded below the tip; the compact filters are reused from disk (not - /// re-downloaded), so it is cheap. A persisted breadcrumb could make the - /// backfill durable across a crash if that ever becomes necessary. + /// This set is the in-memory half of the guard and is never persisted: a + /// relaunch clears it. Its durable half is the wallet's + /// [`DashPayBackfillRecord`](crate::changeset::DashPayBackfillRecord), + /// which the rescan reconcile writes on the same persistence round as the + /// lowered cursor and consults on the next launch — the cursor it lowers + /// is durable too (the host persists every height the rescan climbs + /// through), so without the record every fresh process restored a cursor + /// inside the climb and rewound again (dashpay/platform#4302). The two + /// compose per contact: a mark here says "handled this process", an entry + /// there says "covered from this height"; the request-state transitions + /// that clear a mark here leave the record alone, and the reconcile + /// re-arms a recorded contact only when its checkpoint has dropped below + /// the height it was recorded at. pub rescan_triggered: BTreeSet, /// DashPay contact-crypto ops the unattended background sweep enqueued for 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 6038779d572..208f3cf14bd 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 @@ -156,7 +156,8 @@ impl ManagedIdentity { /// Mutable access to the per-session contact-rescan guard set. /// /// In-memory only — never persisted; see the field docs on - /// [`DashPayState::rescan_triggered`] for the self-healing contract. + /// [`DashPayState::rescan_triggered`] for how it composes with the + /// wallet's durable backfill record. pub fn dashpay_rescan_triggered_mut( &mut self, ) -> &mut std::collections::BTreeSet { diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index 7cbe111776d..e25ffa798c6 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -296,6 +296,15 @@ pub struct PlatformWalletInfo { /// host-side persister mirror fed by /// [`DpnsNameStateChangeSet`](crate::changeset::DpnsNameStateChangeSet). pub dpns_name_states: BTreeMap, + /// Durable half of the DashPay coreHeight backfill guard: which receival + /// contacts the historical rescan already covers, from which height each, + /// and how far the rescan reached. Restored at load from + /// [`ClientWalletStartState::dashpay_backfill`](crate::changeset::ClientWalletStartState::dashpay_backfill) + /// and written by `reconcile_dashpay_rescan` on the round that lowers the + /// cursor. The in-memory half is `DashPayState::rescan_triggered`; see + /// [`DashPayBackfillRecord`](crate::changeset::DashPayBackfillRecord) + /// for how the two compose. + pub dashpay_backfill: crate::changeset::DashPayBackfillRecord, } /// A platform wallet that combines core UTXO functionality with identity management. diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs index 02d052359de..7b4ba75e2e5 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs @@ -42,6 +42,7 @@ impl WalletInfoInterface for PlatformWalletInfo { tracked_asset_locks: std::collections::BTreeMap::new(), observed_input_conflicts: Default::default(), dpns_name_states: std::collections::BTreeMap::new(), + dashpay_backfill: Default::default(), } } @@ -56,6 +57,7 @@ impl WalletInfoInterface for PlatformWalletInfo { tracked_asset_locks: std::collections::BTreeMap::new(), observed_input_conflicts: Default::default(), dpns_name_states: std::collections::BTreeMap::new(), + dashpay_backfill: Default::default(), } } From 3fd3f544317e4282548870f0f3942d6c6c7e9648 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Fri, 25 Sep 2026 13:45:09 -0700 Subject: [PATCH 02/43] feat(platform-wallet-ffi): carry the DashPay backfill record across the FFI and JNI Deliver `DashPayBackfillRecord` to hosts through a new size-negotiated `PersistenceCallbacksExtension` slot, `on_persist_wallet_dashpay_backfill_fn` (floor, rewound_from, and a `DashPayBackfillCoveredContactFFI` array), fired inside the round after the core changeset callback so the lowered cursor is staged before the record that vouches for it. `WalletChangeSetFFI` stays frozen. Hand it back on `WalletRestoreEntryFFI` via appended `has_dashpay_backfill` / `dashpay_backfill_*` fields, decoded into `ClientWalletStartState::dashpay_backfill`; the zero-init default reads as no record, so a host that has not adopted the slot keeps today's behaviour. The JNI layer wires the slot to `NativePersistenceBridge.onWalletChangesetDashPayBackfill` with the cover set packed as one flat 68-byte-per-contact array, and reads the record back from `WalletRestoreData.dashPayBackfill*`; a blob that is not a whole number of entries is read as no record, never as a shorter cover set. Co-Authored-By: Claude Fable 5.1 --- .../src/core_wallet_types.rs | 39 ++ .../rs-platform-wallet-ffi/src/manager.rs | 51 ++- .../rs-platform-wallet-ffi/src/persistence.rs | 391 +++++++++++++++++- .../src/wallet_restore_types.rs | 24 ++ .../rs-unified-sdk-jni/src/persistence.rs | 161 +++++++- 5 files changed, 655 insertions(+), 11 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs index 68e3dd8ff8a..74f3f12d8ae 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs @@ -108,6 +108,45 @@ pub struct UtxoCreditVerdictFFI { pub spent_at_height: u32, } +/// One receival contact the DashPay coreHeight backfill covers, and the +/// height it covers it from — the C mirror of +/// [`DashPayBackfillCoveredContact`](platform_wallet::changeset::DashPayBackfillCoveredContact). +/// +/// Delivered through the persistence extension's +/// `on_persist_wallet_dashpay_backfill_fn` slot alongside the record's +/// `floor` / `rewound_from`, and handed back at restore on +/// `WalletRestoreEntryFFI::dashpay_backfill_covered`. Rides the extension +/// rather than `WalletChangeSetFFI` for the layout reason documented there: +/// that struct crosses by bare pointer and cannot prove a new field present, +/// while the extension's `struct_size` can. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct DashPayBackfillCoveredContactFFI { + /// Our identity — the owner of the receival account. + pub owner_identity_id: [u8; 32], + /// The contact whose payments the receival account collects. + pub contact_identity_id: [u8; 32], + /// Lowest height the scan is guaranteed to test with this contact's + /// addresses watched. + pub covered_from: u32, +} + +/// Project a backfill record's cover set into its C mirrors for the +/// extension slot, in record order (sorted by `(owner, contact)`). +pub fn build_dashpay_backfill_covered_for_callback( + record: &platform_wallet::changeset::DashPayBackfillRecord, +) -> Vec { + record + .covered + .iter() + .map(|entry| DashPayBackfillCoveredContactFFI { + owner_identity_id: entry.owner.to_buffer(), + contact_identity_id: entry.contact.to_buffer(), + covered_from: entry.covered_from, + }) + .collect() +} + /// Project a changeset's credit verdicts into their C mirrors for the /// extension slot, in outpoint order (the map's own ordering — stable, /// so a host log of a round is reproducible). diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 20f0c421b20..e1d4552733c 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -11,8 +11,9 @@ use crate::persistence::{ FFIPersister, FreeTrackedMasternodesFn, LoadIdentityBalanceBlockTimeFn, LoadTrackedMasternodesFn, PersistDpnsNameStatesFn, PersistIdentityBalanceBlockTimeFn, PersistTrackedMasternodesFn, PersistWalletChangesetChainLockHeightFn, - PersistWalletChangesetSweepsFn, PersistWalletChangesetUtxoVerdictsFn, PersistenceCallbacks, - PersistenceCallbacksExtension, PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks, + PersistWalletChangesetSweepsFn, PersistWalletChangesetUtxoVerdictsFn, + PersistWalletDashPayBackfillFn, PersistenceCallbacks, PersistenceCallbacksExtension, + PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks, PLATFORM_WALLET_PERSISTENCE_CALLBACKS_EXTENSION_VERSION, }; use crate::runtime::runtime; @@ -277,6 +278,10 @@ unsafe fn persistence_extension_callbacks( on_load_identity_balance_block_time_fn, LoadIdentityBalanceBlockTimeFn ), + wallet_dashpay_backfill: slot!( + on_persist_wallet_dashpay_backfill_fn, + PersistWalletDashPayBackfillFn + ), } } @@ -903,6 +908,17 @@ mod tests { 0 } + unsafe extern "C" fn persist_wallet_dashpay_backfill( + _context: *mut c_void, + _wallet_id: *const u8, + _floor: u32, + _rewound_from: u32, + _covered: *const crate::core_wallet_types::DashPayBackfillCoveredContactFFI, + _covered_count: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn persist_tracked_masternodes( _context: *mut c_void, _network: *const std::os::raw::c_char, @@ -1255,12 +1271,43 @@ mod tests { assert!(read_short.wallet_changeset_sweeps.is_none()); assert!(read_short.wallet_changeset_chain_lock_height.is_none()); assert!(read_short.wallet_changeset_utxo_verdicts.is_none()); + assert!(read_short.wallet_dashpay_backfill.is_none()); let read_unknown = unsafe { persistence_extension_callbacks(&unknown) }; assert!(read_unknown.dpns_name_states.is_none()); assert!(read_unknown.load_tracked_masternodes.is_none()); assert!(read_unknown.wallet_changeset_sweeps.is_none()); assert!(read_unknown.wallet_changeset_chain_lock_height.is_none()); assert!(read_unknown.wallet_changeset_utxo_verdicts.is_none()); + assert!(read_unknown.wallet_dashpay_backfill.is_none()); + } + + /// A host whose `struct_size` stops right after the identity + /// balance-block-time slots (built before the DashPay backfill slot + /// existed) keeps every earlier slot and simply never has the backfill + /// slot read; a host declaring the full size yields it. + #[test] + fn dashpay_backfill_slot_is_gated_by_struct_size() { + let without = PersistenceCallbacksExtension { + struct_size: std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_wallet_dashpay_backfill_fn + ), + on_persist_wallet_changeset_utxo_verdicts_fn: Some( + persist_wallet_changeset_utxo_verdicts, + ), + on_persist_wallet_dashpay_backfill_fn: Some(persist_wallet_dashpay_backfill), + ..Default::default() + }; + let read = unsafe { persistence_extension_callbacks(&without) }; + assert!(read.wallet_changeset_utxo_verdicts.is_some()); + assert!(read.wallet_dashpay_backfill.is_none()); + + let with = PersistenceCallbacksExtension { + on_persist_wallet_dashpay_backfill_fn: Some(persist_wallet_dashpay_backfill), + ..Default::default() + }; + let read = unsafe { persistence_extension_callbacks(&with) }; + assert!(read.wallet_dashpay_backfill.is_some()); } #[test] diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index d95a1f31db8..cb57ce92997 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -54,8 +54,9 @@ use crate::contact_persistence::{ }; use crate::core_address_types::{AddressPoolTypeTagFFI, CoreAddressEntryFFI, KeyTypeTagFFI}; use crate::core_wallet_types::{ - build_sweep_batches_for_callback, build_utxo_credit_verdicts_for_callback, - free_wallet_changeset_ffi, SweepBatchFFI, UtxoCreditVerdictFFI, WalletChangeSetFFI, + build_dashpay_backfill_covered_for_callback, build_sweep_batches_for_callback, + build_utxo_credit_verdicts_for_callback, free_wallet_changeset_ffi, + DashPayBackfillCoveredContactFFI, SweepBatchFFI, UtxoCreditVerdictFFI, WalletChangeSetFFI, }; use crate::dashpay_payment::{build_payment_persist_entries, DashpayPaymentPersistEntryFFI}; use crate::dpns_name_state_persistence::{ @@ -276,6 +277,25 @@ pub type LoadIdentityBalanceBlockTimeFn = unsafe extern "C" fn( out_block_time: *mut crate::types::BlockTime, ) -> i32; +/// Carries the wallet's DashPay coreHeight backfill record +/// ([`DashPayBackfillRecord`](platform_wallet::changeset::DashPayBackfillRecord)) +/// whenever a round writes one: the rescan sweep stores it on the same round +/// as the lowered `synced_height` it belongs with, so a host that stores +/// both has a record that vouches only for a cursor it also stored. Fired +/// inside the round's begin/end bracket, after the core changeset callback. +/// Every write is the whole record — `floor`, `rewound_from`, and every +/// covered contact — so the host replaces, never merges. A host without +/// the slot keeps today's behaviour: the backfill re-fires on every launch, +/// slow but never lossy (dashpay/platform#4302). +pub type PersistWalletDashPayBackfillFn = unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + floor: u32, + rewound_from: u32, + covered: *const DashPayBackfillCoveredContactFFI, + covered_count: usize, +) -> i32; + /// Size- and version-tagged additive persistence callbacks. /// /// `context` is the context in the accompanying [`PersistenceCallbacks`] @@ -406,6 +426,22 @@ pub struct PersistenceCallbacksExtension { out_block_time: *mut crate::types::BlockTime, ) -> i32, >, + /// The wallet's DashPay backfill record (see + /// [`PersistWalletDashPayBackfillFn`]). Appended under the same version + /// for the same reason as the slots above: `struct_size` proves whether + /// a host allocated it, and a host that did not simply never has it read + /// — which is exactly today's behaviour, a backfill that re-fires on + /// every launch. + pub on_persist_wallet_dashpay_backfill_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + floor: u32, + rewound_from: u32, + covered: *const DashPayBackfillCoveredContactFFI, + covered_count: usize, + ) -> i32, + >, } impl Default for PersistenceCallbacksExtension { @@ -423,6 +459,7 @@ impl Default for PersistenceCallbacksExtension { on_persist_wallet_changeset_utxo_verdicts_fn: None, on_persist_identity_balance_block_time_fn: None, on_load_identity_balance_block_time_fn: None, + on_persist_wallet_dashpay_backfill_fn: None, } } } @@ -441,6 +478,7 @@ pub struct PersistenceExtensionCallbacks { pub wallet_changeset_utxo_verdicts: Option, pub persist_identity_balance_block_time: Option, pub load_identity_balance_block_time: Option, + pub wallet_dashpay_backfill: Option, } /// Return value by which a persistence callback reports a **retryable** @@ -1372,6 +1410,11 @@ pub struct FFIPersister { /// materialises those rows unspent, as every host did before the slot /// existed. wallet_changeset_utxo_verdicts_callback: Option, + /// `Some` only when the host's extension `struct_size` proved the slot + /// was allocated. Carries the DashPay backfill record a round writes; a + /// host without it re-runs the backfill on every launch, as every host + /// did before the slot existed (dashpay/platform#4302). + wallet_dashpay_backfill_callback: Option, /// Additive tracked-masternode persistence trio (persist / load / /// free), likewise extension-negotiated. tracked_masternodes_callbacks: PersistenceExtensionCallbacks, @@ -1496,6 +1539,7 @@ impl FFIPersister { wallet_changeset_chain_lock_height_callback: extensions .wallet_changeset_chain_lock_height, wallet_changeset_utxo_verdicts_callback: extensions.wallet_changeset_utxo_verdicts, + wallet_dashpay_backfill_callback: extensions.wallet_dashpay_backfill, tracked_masternodes_callbacks: extensions, persist_identity_balance_block_time_callback: extensions .persist_identity_balance_block_time, @@ -2178,6 +2222,37 @@ impl PlatformWalletPersistence for FFIPersister { } } + // The DashPay backfill record rides its own size-negotiated slot, + // after the core changeset callback so the lowered cursor a rescan + // round carries is staged before the record that vouches for it. + // Whole-record semantics: the host replaces what it holds. + if let Some(record) = changeset.dashpay_backfill.as_ref() { + if let Some(cb) = self.wallet_dashpay_backfill_callback { + let covered = build_dashpay_backfill_covered_for_callback(record); + let result = unsafe { + cb( + self.callbacks.context, + wallet_id.as_ptr(), + record.floor, + record.rewound_from, + if covered.is_empty() { + std::ptr::null() + } else { + covered.as_ptr() + }, + covered.len(), + ) + }; + if result != 0 { + eprintln!( + "Wallet DashPay backfill persistence callback returned error code {}", + result + ); + outcome.record(result); + } + } + } + // Send identity scalar changeset — upserts and removals. // Swift handler maps these onto `PersistentIdentity` row // upserts / tombstones. @@ -5213,6 +5288,53 @@ fn provider_rebuild_error( /// the configured signer surface (see /// `Wallet::new_external_signable`). Earlier revisions of this code /// path produced a `WatchOnly` wallet — that has been replaced. +/// The persisted DashPay backfill record on a wallet-restore entry, or the +/// empty record when the host holds none. +/// +/// Empty is the fail-safe reading: a contact absent from the record is +/// re-armed by the next rescan sweep (one redundant rewind), whereas a +/// contact wrongly present would never be backfilled again. So a host that +/// reports the flag with a null cover set gets a record that covers no one, +/// and every entry is taken as the host stored it. +fn decode_dashpay_backfill( + entry: &WalletRestoreEntryFFI, +) -> platform_wallet::changeset::DashPayBackfillRecord { + use platform_wallet::changeset::{DashPayBackfillCoveredContact, DashPayBackfillRecord}; + + if !entry.has_dashpay_backfill { + return DashPayBackfillRecord::default(); + } + let covered: &[DashPayBackfillCoveredContactFFI] = + if entry.dashpay_backfill_covered.is_null() || entry.dashpay_backfill_covered_count == 0 { + &[] + } else { + // SAFETY: host-owned array of `count` entries, valid for the load + // callback window (the same contract as every sibling array). + unsafe { + slice::from_raw_parts( + entry.dashpay_backfill_covered, + entry.dashpay_backfill_covered_count, + ) + } + }; + let mut covered: Vec = covered + .iter() + .map(|c| DashPayBackfillCoveredContact { + owner: Identifier::from(c.owner_identity_id), + contact: Identifier::from(c.contact_identity_id), + covered_from: c.covered_from, + }) + .collect(); + // Canonical order and one entry per pair, whatever the host stored. + covered.sort_by_key(|entry| (entry.owner, entry.contact)); + covered.dedup_by(|a, b| (a.owner, a.contact) == (b.owner, b.contact)); + DashPayBackfillRecord { + floor: entry.dashpay_backfill_floor, + rewound_from: entry.dashpay_backfill_rewound_from, + covered, + } +} + fn build_wallet_start_state( entry: &WalletRestoreEntryFFI, ) -> Result< @@ -5850,12 +5972,15 @@ fn build_wallet_start_state( // it. let unconfirmed_outgoing_txs = decode_unconfirmed_outgoing(entry); + let dashpay_backfill = decode_dashpay_backfill(entry); + let wallet_state = ClientWalletStartState { wallet, wallet_info, identity_manager, unused_asset_locks, unconfirmed_outgoing_txs, + dashpay_backfill, }; let platform_address_state = if per_account.is_empty() @@ -8210,6 +8335,258 @@ mod tests { drop(persister); } + /// The DashPay backfill record reaches the host through its own + /// size-negotiated extension slot (dashpay/platform#4302): a round that + /// carries one fires it after the changeset callback with the whole + /// record — scalars and every covered contact — a round without one + /// never fires it, and a host without the slot still succeeds, keeping + /// the pre-record behaviour of a rewind per launch. + #[test] + fn store_delivers_the_dashpay_backfill_record_through_the_extension_slot() { + use platform_wallet::changeset::{CoreChangeSet, DashPayBackfillRecord}; + + #[derive(Default)] + struct Sink { + events: std::sync::Mutex>, + } + unsafe extern "C" fn record_changeset( + ctx: *mut c_void, + _wallet_id: *const u8, + _changeset: *const WalletChangeSetFFI, + ) -> i32 { + let sink = &*(ctx as *const Sink); + sink.events.lock().unwrap().push("changeset".into()); + 0 + } + unsafe extern "C" fn record_backfill( + ctx: *mut c_void, + wallet_id: *const u8, + floor: u32, + rewound_from: u32, + covered: *const DashPayBackfillCoveredContactFFI, + covered_count: usize, + ) -> i32 { + let sink = &*(ctx as *const Sink); + let wallet_id = std::slice::from_raw_parts(wallet_id, 32); + let covered: Vec = if covered.is_null() { + assert_eq!(covered_count, 0, "null cover set must carry count 0"); + Vec::new() + } else { + std::slice::from_raw_parts(covered, covered_count) + .iter() + .map(|c| { + format!( + "{}/{}@{}", + c.owner_identity_id[0], c.contact_identity_id[0], c.covered_from + ) + }) + .collect() + }; + sink.events.lock().unwrap().push(format!( + "backfill wallet={} floor={floor} rewound_from={rewound_from} covered=[{}]", + wallet_id[0], + covered.join(",") + )); + 0 + } + + let sink = Sink::default(); + let callbacks = PersistenceCallbacks { + context: &sink as *const Sink as *mut c_void, + on_persist_wallet_changeset_fn: Some(record_changeset), + ..PersistenceCallbacks::default() + }; + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + callbacks, + PersistenceCapabilities::NONE, + PersistenceExtensionCallbacks { + wallet_dashpay_backfill: Some(record_backfill), + ..Default::default() + }, + ); + + // A round with no record: the slot stays silent. + persister + .store( + [1u8; 32], + PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(500), + ..CoreChangeSet::default() + }), + ..PlatformWalletChangeSet::default() + }, + ) + .expect("round without a record"); + assert_eq!( + sink.events.lock().unwrap().clone(), + vec!["changeset".to_string()] + ); + + // The rescan's round: lowered cursor first, then the whole record. + let mut record = DashPayBackfillRecord::default(); + record.record_pass( + 2_537_092, + Some(2_167_092), + [ + ( + dpp::prelude::Identifier::from([9u8; 32]), + dpp::prelude::Identifier::from([2u8; 32]), + 2_300_000, + ), + ( + dpp::prelude::Identifier::from([1u8; 32]), + dpp::prelude::Identifier::from([3u8; 32]), + 2_167_092, + ), + ], + ); + sink.events.lock().unwrap().clear(); + persister + .store( + [7u8; 32], + PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(2_167_092), + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(record), + ..PlatformWalletChangeSet::default() + }, + ) + .expect("rescan round"); + assert_eq!( + sink.events.lock().unwrap().clone(), + vec![ + "changeset".to_string(), + "backfill wallet=7 floor=2167092 rewound_from=2537092 \ + covered=[1/3@2167092,9/2@2300000]" + .to_string(), + ], + "the record follows the changeset callback, in record order" + ); + + // An empty cover set crosses as a null pointer with count 0. + sink.events.lock().unwrap().clear(); + persister + .store( + [7u8; 32], + PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }, + ) + .expect("empty record round"); + assert_eq!( + sink.events.lock().unwrap().clone(), + vec!["backfill wallet=7 floor=0 rewound_from=0 covered=[]".to_string()] + ); + + // A host without the slot: the round still succeeds. + let sink = Sink::default(); + let callbacks = PersistenceCallbacks { + context: &sink as *const Sink as *mut c_void, + on_persist_wallet_changeset_fn: Some(record_changeset), + ..PersistenceCallbacks::default() + }; + let legacy = FFIPersister::new_with_persistence_capabilities( + callbacks, + PersistenceCapabilities::NONE, + ); + legacy + .store( + [7u8; 32], + PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }, + ) + .expect("a host without the slot still stores the round"); + assert!(sink.events.lock().unwrap().is_empty()); + drop(persister); + } + + /// The restore side of dashpay/platform#4302: a wallet-restore entry + /// carrying the record decodes into the start state's + /// `dashpay_backfill` in canonical form, and an entry without it — the + /// zero-init default every pre-record host produces — decodes as the + /// empty record, so the rescan sweep rewinds once as it always did. + #[test] + fn wallet_restore_decodes_the_dashpay_backfill_record() { + use platform_wallet::changeset::DashPayBackfillRecord; + + let none = WalletRestoreEntryFFI::default(); + assert!(!none.has_dashpay_backfill); + assert!(super::decode_dashpay_backfill(&none).is_empty()); + + // The flag alone, with a null cover set: a record that covers no + // one, never a torn read. + let bare = WalletRestoreEntryFFI { + has_dashpay_backfill: true, + dashpay_backfill_floor: 10, + dashpay_backfill_rewound_from: 20, + ..Default::default() + }; + let decoded = super::decode_dashpay_backfill(&bare); + assert!(decoded.is_empty()); + assert_eq!((decoded.floor, decoded.rewound_from), (10, 20)); + + // Out of order and duplicated, as a host might hand it back: the + // decoded record is sorted with one entry per pair. + let covered = [ + DashPayBackfillCoveredContactFFI { + owner_identity_id: [9u8; 32], + contact_identity_id: [2u8; 32], + covered_from: 2_300_000, + }, + DashPayBackfillCoveredContactFFI { + owner_identity_id: [1u8; 32], + contact_identity_id: [3u8; 32], + covered_from: 2_167_092, + }, + DashPayBackfillCoveredContactFFI { + owner_identity_id: [9u8; 32], + contact_identity_id: [2u8; 32], + covered_from: 2_999_999, + }, + ]; + let entry = WalletRestoreEntryFFI { + has_dashpay_backfill: true, + dashpay_backfill_floor: 2_167_092, + dashpay_backfill_rewound_from: 2_537_092, + dashpay_backfill_covered: covered.as_ptr(), + dashpay_backfill_covered_count: covered.len(), + ..Default::default() + }; + let decoded = super::decode_dashpay_backfill(&entry); + let mut expected = DashPayBackfillRecord::default(); + expected.record_pass( + 2_537_092, + Some(2_167_092), + [ + ( + dpp::prelude::Identifier::from([1u8; 32]), + dpp::prelude::Identifier::from([3u8; 32]), + 2_167_092, + ), + ( + dpp::prelude::Identifier::from([9u8; 32]), + dpp::prelude::Identifier::from([2u8; 32]), + 2_300_000, + ), + ], + ); + assert_eq!(decoded, expected); + assert_eq!( + decoded.covered_from( + &dpp::prelude::Identifier::from([9u8; 32]), + &dpp::prelude::Identifier::from([2u8; 32]) + ), + Some(2_300_000), + "the first of a duplicated pair wins" + ); + } + /// The numeric chainlock height reaches the host through its own /// size-negotiated extension slot: a chainlock-advancing round fires it /// after the changeset callback with the height a non-Rust host cannot @@ -8705,6 +9082,16 @@ mod tests { PersistenceCallbacksExtension, on_load_identity_balance_block_time_fn ) + std::mem::size_of::>(), + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_wallet_dashpay_backfill_fn + ) + ); + assert_eq!( + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_wallet_dashpay_backfill_fn + ) + std::mem::size_of::>(), std::mem::size_of::() ); assert_eq!( diff --git a/packages/rs-platform-wallet-ffi/src/wallet_restore_types.rs b/packages/rs-platform-wallet-ffi/src/wallet_restore_types.rs index 22a71ade25b..a835b46b6f0 100644 --- a/packages/rs-platform-wallet-ffi/src/wallet_restore_types.rs +++ b/packages/rs-platform-wallet-ffi/src/wallet_restore_types.rs @@ -718,6 +718,25 @@ pub struct WalletRestoreEntryFFI { /// leaves every existing field where it was. pub unconfirmed_outgoing_tx_records: *const UnconfirmedOutgoingTxRecordFFI, pub unconfirmed_outgoing_tx_records_count: usize, + /// Whether the host holds a persisted DashPay backfill record for this + /// wallet (dashpay/platform#4302). `false` — the zero-init default — for + /// a wallet that never rewound for a contact, a row persisted before the + /// record existed, or a host that does not store the slot; the rescan + /// sweep then behaves as it always did and writes one. The three fields + /// below are read only when this is `true`. + pub has_dashpay_backfill: bool, + /// Lowest height the backfill rewound the cursor to. + pub dashpay_backfill_floor: u32, + /// Highest cursor the backfill rewound from — the height the scan climbs + /// back to for the backfill to be complete. + pub dashpay_backfill_rewound_from: u32, + /// Receival contacts the backfill covers, each with the height it is + /// covered from. Host-owned for the callback window, freed by + /// `LoadWalletListFreeFn`; `null` / `0` when the record covers no one. + /// Appended at the end, like every addition to this `#[repr(C)]` struct, + /// so no existing offset moves. + pub dashpay_backfill_covered: *const crate::core_wallet_types::DashPayBackfillCoveredContactFFI, + pub dashpay_backfill_covered_count: usize, } /// Every field named explicitly so that adding a field to this ABI struct @@ -758,6 +777,11 @@ impl Default for WalletRestoreEntryFFI { last_applied_chain_lock_bytes_len: 0, unconfirmed_outgoing_tx_records: std::ptr::null(), unconfirmed_outgoing_tx_records_count: 0, + has_dashpay_backfill: false, + dashpay_backfill_floor: 0, + dashpay_backfill_rewound_from: 0, + dashpay_backfill_covered: std::ptr::null(), + dashpay_backfill_covered_count: 0, } } } diff --git a/packages/rs-unified-sdk-jni/src/persistence.rs b/packages/rs-unified-sdk-jni/src/persistence.rs index fea73af90dc..f0f1492402c 100644 --- a/packages/rs-unified-sdk-jni/src/persistence.rs +++ b/packages/rs-unified-sdk-jni/src/persistence.rs @@ -54,13 +54,14 @@ use jni::JNIEnv; use platform_wallet_ffi::{ AccountAddressPoolFFI, AccountChangeSetFFI, AccountSpecFFI, AddressBalanceEntryFFI, AssetLockEntryFFI, ContactIgnoredSenderFFI, ContactProfileRestoreEntryFFI, ContactRequestFFI, - ContactRequestRemovalFFI, CoreAddressEntryFFI, DpnsNameStateFFI, IdentityEntryFFI, - IdentityKeyEntryFFI, IdentityKeyRemovalFFI, IdentityKeyRestoreFFI, IdentityRestoreEntryFFI, - InvitationEntryFFI, OutPointFFI, PaymentRestoreEntryFFI, PersistenceCallbacks, - PersistenceCallbacksExtension, PlatformAddressFFI, ProviderSpecialTxRestoreEntryFFI, - SpentOutPointFFI, SweepBatchFFI, TokenBalanceRemovalFFI, TokenBalanceUpsertFFI, - TransactionRecordFFI, UnresolvedAssetLockTxRecordFFI, UtxoEntryFFI, UtxoRestoreEntryFFI, - WalletChangeSetFFI, WalletRestoreEntryFFI, + ContactRequestRemovalFFI, CoreAddressEntryFFI, DashPayBackfillCoveredContactFFI, + DpnsNameStateFFI, IdentityEntryFFI, IdentityKeyEntryFFI, IdentityKeyRemovalFFI, + IdentityKeyRestoreFFI, IdentityRestoreEntryFFI, InvitationEntryFFI, OutPointFFI, + PaymentRestoreEntryFFI, PersistenceCallbacks, PersistenceCallbacksExtension, + PlatformAddressFFI, ProviderSpecialTxRestoreEntryFFI, SpentOutPointFFI, SweepBatchFFI, + TokenBalanceRemovalFFI, TokenBalanceUpsertFFI, TransactionRecordFFI, + UnresolvedAssetLockTxRecordFFI, UtxoEntryFFI, UtxoRestoreEntryFFI, WalletChangeSetFFI, + WalletRestoreEntryFFI, }; use std::ffi::{c_void, CStr, CString}; use std::os::raw::c_char; @@ -217,6 +218,7 @@ pub(crate) fn build_extension(env: &mut JNIEnv, bridge: &JObject) -> Persistence on_persist_wallet_changeset_chain_lock_height_fn: Some( tramp_persist_wallet_changeset_chain_lock_height, ), + on_persist_wallet_dashpay_backfill_fn: Some(tramp_persist_wallet_dashpay_backfill), ..Default::default() } } @@ -850,6 +852,61 @@ unsafe extern "C" fn tramp_persist_wallet_changeset_chain_lock_height( }) } +/// Wire size of one covered contact in the flat `covered` array handed to +/// `onWalletChangesetDashPayBackfill` and read back from +/// `WalletRestoreData.dashPayBackfillCovered`: owner (32) ‖ contact (32) ‖ +/// `coveredFrom` (u32, little-endian). The same layout as +/// `DashPayBackfillRecord::covered_bytes`, so the handler can store the array +/// as one opaque blob and hand it straight back at load. +const DASHPAY_BACKFILL_COVERED_ENTRY_LEN: usize = 32 + 32 + 4; + +/// Descriptor of `NativePersistenceBridge.onWalletChangesetDashPayBackfill`: +/// `(walletId, floor, rewoundFrom, covered, coveredCount)`. The cover set is +/// shipped as ONE flat `byte[]` of `68·N` bytes plus a count, the same packing +/// the sweep slot uses for its txids, rather than one JVM allocation per +/// contact — a contact-heavy wallet records hundreds. +const WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR: &str = "([BII[BI)I"; + +/// Deliver the wallet's DashPay backfill record (see +/// `PersistWalletDashPayBackfillFn`) — the durable half of the coreHeight +/// rescan guard (dashpay/platform#4302). Whole-record semantics: the handler +/// replaces what it holds on the wallet row. +unsafe extern "C" fn tramp_persist_wallet_dashpay_backfill( + context: *mut c_void, + wallet_id: *const u8, + floor: u32, + rewound_from: u32, + covered: *const DashPayBackfillCoveredContactFFI, + covered_count: usize, +) -> i32 { + with_bridge(context, |env, bridge| { + let wid = id32(env, wallet_id)?; + let entries = slice_or_empty(covered, covered_count); + let mut packed = Vec::with_capacity(entries.len() * DASHPAY_BACKFILL_COVERED_ENTRY_LEN); + for entry in entries { + packed.extend_from_slice(&entry.owner_identity_id); + packed.extend_from_slice(&entry.contact_identity_id); + packed.extend_from_slice(&entry.covered_from.to_le_bytes()); + } + let packed_arr = env.byte_array_from_slice(&packed)?; + let count = i32::try_from(entries.len()) + .map_err(|_| jni::errors::Error::JniCall(jni::errors::JniError::InvalidArguments))?; + env.call_method( + bridge, + "onWalletChangesetDashPayBackfill", + WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR, + &[ + (&wid).into(), + JValue::Int(jint_height(floor)?), + JValue::Int(jint_height(rewound_from)?), + (&packed_arr).into(), + JValue::Int(count), + ], + )? + .i() + }) +} + unsafe fn persist_changeset_account( env: &mut JNIEnv, bridge: &JObject, @@ -1973,6 +2030,11 @@ struct WalletRestoreStaged { /// minted null / 0 at seal (no chainlock persisted). A single flat /// buffer, freed with one `free_raw_bytes`. last_applied_chain_lock: Vec, + /// The DashPay backfill record's cover set. `Copy` POD like the + /// platform-address balances: minted in one shot at seal, freed with a + /// single `free_raw_slice`. The record's scalars and presence flag ride + /// on `entry` directly. + dashpay_backfill_covered: Vec, } /// Staged account spec: FFI struct with a null xpub pointer plus the @@ -2169,6 +2231,7 @@ fn seal_wallet_entries(staged: Vec) -> Vec` mints in one shot and @@ -2291,6 +2354,13 @@ fn seal_wallet_entries(staged: Vec) -> Vec Result<(bool, u32, u32, Vec), jni::errors::Error> { + let present = env.get_field(holder, "hasDashPayBackfill", "Z")?.z()?; + if !present { + return Ok((false, 0, 0, Vec::new())); + } + let floor = env.get_field(holder, "dashPayBackfillFloor", "I")?.i()?; + let rewound_from = env + .get_field(holder, "dashPayBackfillRewoundFrom", "I")? + .i()?; + let blob = read_bytes_field_vec(env, holder, "dashPayBackfillCovered")?; + if floor < 0 + || rewound_from < 0 + || !blob + .len() + .is_multiple_of(DASHPAY_BACKFILL_COVERED_ENTRY_LEN) + { + log::warn!( + "load: malformed DashPay backfill record on the wallet row (floor={floor}, \ + rewound_from={rewound_from}, blob_len={}); reading as no record", + blob.len() + ); + return Ok((false, 0, 0, Vec::new())); + } + let (chunks, _) = blob.as_chunks::(); + let covered = chunks + .iter() + .map(|chunk| { + let mut owner_identity_id = [0u8; 32]; + let mut contact_identity_id = [0u8; 32]; + let mut height = [0u8; 4]; + owner_identity_id.copy_from_slice(&chunk[..32]); + contact_identity_id.copy_from_slice(&chunk[32..64]); + height.copy_from_slice(&chunk[64..68]); + DashPayBackfillCoveredContactFFI { + owner_identity_id, + contact_identity_id, + covered_from: u32::from_le_bytes(height), + } + }) + .collect(); + Ok((true, floor as u32, rewound_from as u32, covered)) +} + /// Read the Kotlin `WalletRestoreData.utxos` array into staged /// [`UtxoRestoreStaged`] rows (CORE-06 restore path). Empty / null field /// → empty vec (staged as `(null, 0)` at seal). @@ -3613,6 +3753,9 @@ unsafe extern "C" fn tramp_load_wallet_list_free( e.last_applied_chain_lock_bytes_len, ); + // DashPay backfill cover set — flat POD slice, one mint at seal. + free_raw_slice(e.dashpay_backfill_covered, e.dashpay_backfill_covered_count); + // identities + nested key / contact / ignored-sender / // payment / contact-profile arrays (each key's `data` buffer + // contract-bounds doc-type C-string; each contact's three byte @@ -4548,6 +4691,10 @@ const BRIDGE_METHOD_TABLE: &[(&str, &str)] = &[ // literal at the `call_method` site in // `tramp_persist_wallet_changeset_chain_lock_height`. ("onWalletChangesetChainLockHeight", "([BI)I"), + ( + "onWalletChangesetDashPayBackfill", + WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR, + ), ( "onPersistIdentityUpsert", "([B[BJJZIBZ[B[Ljava/lang/String;[JZLjava/lang/String;Ljava/lang/String;\ From 7618785b84159b5e119d2cad1ac50c2f80c8834b Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Fri, 25 Sep 2026 13:45:09 -0700 Subject: [PATCH 03/43] feat(kotlin-sdk): persist the DashPay backfill record on the wallet row (Room v15) Store the record native delivers through `onWalletChangesetDashPayBackfill` in three new nullable `wallets` columns (`dashPayBackfillFloor`, `dashPayBackfillRewoundFrom`, `dashPayBackfillCovered`), staged into the round so it commits or rolls back with the lowered `syncedHeight` the header slot wrote, and hand it back unchanged on `loadWalletList`. Room migration 14 -> 15 is additive; every pre-migration row reads as no record, so native rewinds once more for it and then never again for the contacts it covers (dashpay/platform#4302). Co-Authored-By: Claude Fable 5.1 --- .../persistence/DashDatabaseMigrationTest.kt | 57 ++++++- .../dashsdk/ffi/NativePersistenceBridge.kt | 52 ++++++ .../dashsdk/persistence/DashDatabase.kt | 29 +++- .../PlatformWalletPersistenceHandler.kt | 53 +++++++ .../persistence/entities/WalletEntity.kt | 14 ++ .../dashsdk/persistence/DashDatabaseTest.kt | 41 ++++- .../PlatformWalletPersistenceHandlerTest.kt | 150 ++++++++++++++++++ 7 files changed, 388 insertions(+), 8 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt b/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt index 49398a2d13a..ff2cfc31d11 100644 --- a/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt +++ b/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt @@ -613,13 +613,62 @@ class DashDatabaseMigrationTest { db.close() } + /** + * v14 -> v15 adds the DashPay backfill record columns on `wallets` + * (`dashPayBackfillFloor`, `dashPayBackfillRewoundFrom`, + * `dashPayBackfillCovered`), all nullable and additive. A pre-migration + * wallet reads back with no record — native then rewinds once, as it did + * before the record existed — and a row written afterwards keeps all + * three, so a restored wallet resumes its backfill instead of restarting + * it (dashpay/platform#4302). + */ + @Test + fun migrate14To15AddsDashPayBackfillColumns() { + helper.createDatabase(dbName, 14).apply { + execSQL( + "INSERT INTO wallets (walletId, walletGroupId, networkRaw, name, birthHeight, " + + "syncedHeight, lastSynced, isImported, createdAt, lastUpdated) " + + "VALUES (x'01', x'02', 1, 'w', 0, 2309809, 0, 0, 0, 0)", + ) + close() + } + + val db = helper.runMigrationsAndValidate(dbName, 15, true, DashDatabase.MIGRATION_14_15) + + db.query( + "SELECT dashPayBackfillFloor, dashPayBackfillRewoundFrom, dashPayBackfillCovered, " + + "syncedHeight FROM wallets WHERE walletId = x'01'", + ).use { c -> + assertTrue(c.moveToFirst()) + assertTrue("pre-migration wallets have no backfill record", c.isNull(0)) + assertTrue(c.isNull(1)) + assertTrue(c.isNull(2)) + assertEquals("the cursor itself is untouched", 2_309_809, c.getInt(3)) + } + db.execSQL( + "UPDATE wallets SET dashPayBackfillFloor = 2167092, " + + "dashPayBackfillRewoundFrom = 2537092, dashPayBackfillCovered = x'AABB' " + + "WHERE walletId = x'01'", + ) + db.query( + "SELECT dashPayBackfillFloor, dashPayBackfillRewoundFrom, dashPayBackfillCovered " + + "FROM wallets WHERE walletId = x'01'", + ).use { c -> + assertTrue(c.moveToFirst()) + assertEquals(2_167_092, c.getInt(0)) + assertEquals(2_537_092, c.getInt(1)) + assertTrue(byteArrayOf(0xAA.toByte(), 0xBB.toByte()).contentEquals(c.getBlob(2))) + } + db.close() + } + /** The requested contiguous path from the pre-u64 v4 schema to latest. */ @Test fun migrate4ToLatest() { helper.createDatabase(dbName, 4).close() helper.runMigrationsAndValidate( dbName, - 14, + 15, true, DashDatabase.MIGRATION_4_5, DashDatabase.MIGRATION_5_6, @@ -631,16 +680,17 @@ class DashDatabaseMigrationTest { DashDatabase.MIGRATION_11_12, DashDatabase.MIGRATION_12_13, DashDatabase.MIGRATION_13_14, + DashDatabase.MIGRATION_14_15, ).close() } - /** The full chain from v1 must also land on a valid v14 schema. */ + /** The full chain from v1 must also land on a valid v15 schema. */ @Test fun migrateAllTheWayFrom1() { helper.createDatabase(dbName, 1).close() helper.runMigrationsAndValidate( dbName, - 14, + 15, true, DashDatabase.MIGRATION_1_2, DashDatabase.MIGRATION_2_3, @@ -655,6 +705,7 @@ class DashDatabaseMigrationTest { DashDatabase.MIGRATION_11_12, DashDatabase.MIGRATION_12_13, DashDatabase.MIGRATION_13_14, + DashDatabase.MIGRATION_14_15, ).close() } } diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt index 76759d63ed3..014208f4ec0 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt @@ -437,6 +437,35 @@ abstract class NativePersistenceBridge { */ open fun onWalletChangesetChainLockHeight(walletId: ByteArray, height: Int): Int = 0 + /** + * The wallet's DashPay coreHeight backfill record — the durable half of + * the contact rescan guard (dashpay/platform#4302). Fired inside the + * round, after the wallet changeset header, on every round whose + * changeset carries a record; the rescan sweep writes it on the SAME + * round as the lowered `syncedHeight` it belongs with, so a handler that + * stores both holds a record that only ever vouches for a cursor it also + * stored. Descriptor `([BII[BI)I`. + * + * Whole-record semantics: replace what the wallet row holds, never + * merge. [covered] is ONE flat `byte[]` of `68 * coveredCount` bytes — + * per contact the owner identity id (32), the contact identity id (32) + * and the height it is covered from (u32, little-endian) — the same + * packing the sweep slot uses for its txids. Store it as an opaque blob + * and hand it back unchanged on [WalletRestoreData.dashPayBackfillCovered]; + * only native reads it. + * + * Purely additive: an implementation that ignores it keeps today's + * behaviour — the backfill re-fires on every launch, re-walking every + * filter from the earliest contact's core height. Slow, never lossy. + */ + open fun onWalletChangesetDashPayBackfill( + walletId: ByteArray, + floor: Int, + rewoundFrom: Int, + covered: ByteArray, + coveredCount: Int, + ): Int = 0 + // ── Identities ──────────────────────────────────────────────────── /** @@ -968,6 +997,29 @@ class WalletRestoreData( * `loadWalletList`. */ @JvmField val lastAppliedChainLockBytes: ByteArray, + /** + * Whether the wallet row holds a DashPay coreHeight backfill record + * (dashpay/platform#4302). `false` for a wallet that never rewound for a + * contact or a row persisted before the record existed; native then + * ignores the three fields below, and the first rescan sweep behaves as + * it always did and writes one. Mirror of + * `WalletRestoreEntryFFI.has_dashpay_backfill`. + */ + @JvmField val hasDashPayBackfill: Boolean = false, + /** Lowest height the backfill rewound the cursor to. */ + @JvmField val dashPayBackfillFloor: Int = 0, + /** + * Highest cursor the backfill rewound from — the height the scan climbs + * back to for the backfill to be complete. + */ + @JvmField val dashPayBackfillRewoundFrom: Int = 0, + /** + * The record's cover set, exactly as [NativePersistenceBridge.onWalletChangesetDashPayBackfill] + * delivered it: `68` bytes per covered contact. Native re-packs it into + * a `DashPayBackfillCoveredContactFFI` array; a blob that is not a whole + * number of entries is read as no record. + */ + @JvmField val dashPayBackfillCovered: ByteArray = ByteArray(0), ) /** diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt index 94ebbb0267f..1603d26f164 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt @@ -161,9 +161,20 @@ import org.dashfoundation.dashsdk.persistence.entities.WalletManagerMetadataEnti * contract's `oncePerIdentityDistribution` block as JSON, so the claim * screen can offer the third distribution kind. NULL for every pre-existing * row; the next contract materialization fills it in. + * + * Version 15 (durable DashPay backfill, dashpay/platform#4302): adds the + * nullable `wallets.dashPayBackfillFloor`, `wallets.dashPayBackfillRewoundFrom` + * and `wallets.dashPayBackfillCovered` columns — the record native writes + * through `onWalletChangesetDashPayBackfill` on the same round as the lowered + * `syncedHeight` it belongs with, and reads back on `loadWalletList`. The + * contact rescan used to guard against re-lowering the cursor in memory only, + * while the cursor it lowers is durable, so every fresh process rewound again + * and re-walked every filter from the earliest contact's core height. All + * three NULL (every pre-migration row) means no record: native rewinds once + * more, writes one, and never again for the contacts it covers. */ @Database( - version = 14, + version = 15, exportSchema = true, entities = [ WalletEntity::class, @@ -679,6 +690,21 @@ abstract class DashDatabase : RoomDatabase() { } } + /** + * v14 -> v15: additive nullable `wallets.dashPayBackfillFloor`, + * `wallets.dashPayBackfillRewoundFrom` and + * `wallets.dashPayBackfillCovered`, see the version-15 class doc + * above. NULL for every pre-existing row — no record on file, so the + * next rescan sweep behaves exactly as before and writes one. + */ + val MIGRATION_14_15: Migration = object : Migration(14, 15) { + override fun migrate(db: SupportSQLiteDatabase) { + db.execSQL("ALTER TABLE `wallets` ADD COLUMN `dashPayBackfillFloor` INTEGER") + db.execSQL("ALTER TABLE `wallets` ADD COLUMN `dashPayBackfillRewoundFrom` INTEGER") + db.execSQL("ALTER TABLE `wallets` ADD COLUMN `dashPayBackfillCovered` BLOB") + } + } + /** * Build the on-disk database. WAL is Room's default journal mode on * API 16+; writes go through the persistence handler inside @@ -701,6 +727,7 @@ abstract class DashDatabase : RoomDatabase() { MIGRATION_11_12, MIGRATION_12_13, MIGRATION_13_14, + MIGRATION_14_15, ) .build() diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt index 2f4ad4d12d5..8c087f53763 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt @@ -775,6 +775,39 @@ class PlatformWalletPersistenceHandler( * height, no collection), so a chainlock-advancing round collects too, * not only a header round. */ + /** + * The wallet's DashPay coreHeight backfill record (dashpay/platform#4302): + * whole-record replace of the three `wallets.dashPayBackfill*` columns, + * staged into the round so it commits with — or rolls back with — the + * lowered `syncedHeight` the header slot wrote moments earlier in the + * same round. A record that vouches for a cursor the round never + * committed must not survive on its own. + */ + override fun onWalletChangesetDashPayBackfill( + walletId: ByteArray, + floor: Int, + rewoundFrom: Int, + covered: ByteArray, + coveredCount: Int, + ): Int = guarded { + require(covered.size == coveredCount * DASHPAY_BACKFILL_COVERED_ENTRY_SIZE) { + "DashPay backfill cover set must be $DASHPAY_BACKFILL_COVERED_ENTRY_SIZE bytes per contact" + } + stage(walletId) { db -> + // Drop stale post-deletion callbacks (can't resurrect a wallet). + val wallet = db.walletDao().getByWalletId(walletId) ?: return@stage + db.walletDao().upsert( + wallet.copy( + dashPayBackfillFloor = floor, + dashPayBackfillRewoundFrom = rewoundFrom, + dashPayBackfillCovered = covered.copyOf(), + lastUpdated = now(), + ), + ) + } + 0 + } + override fun onWalletChangesetChainLockHeight(walletId: ByteArray, height: Int): Int = guarded { val round = openRound(walletId) round?.finalityAdvanced = true @@ -2673,6 +2706,15 @@ class PlatformWalletPersistenceHandler( // slice. val lastAppliedChainLockBytes = w.lastAppliedChainLockBytes ?: ByteArray(0) + // Persisted DashPay backfill record — the durable half of the + // contact rescan guard. Present only when every column is set + // (one round writes all three); otherwise native treats the + // wallet as never backfilled and rewinds once, as before. + val backfillFloor = w.dashPayBackfillFloor + val backfillRewoundFrom = w.dashPayBackfillRewoundFrom + val backfillCovered = w.dashPayBackfillCovered + val hasBackfill = + backfillFloor != null && backfillRewoundFrom != null && backfillCovered != null out.add( WalletRestoreData( walletId = w.walletId, @@ -2694,6 +2736,10 @@ class PlatformWalletPersistenceHandler( unresolvedAssetLockTxRecords = unresolvedAssetLockTxRecords, providerSpecialTxs = providerSpecialTxs, lastAppliedChainLockBytes = lastAppliedChainLockBytes, + hasDashPayBackfill = hasBackfill, + dashPayBackfillFloor = if (hasBackfill) backfillFloor!! else 0, + dashPayBackfillRewoundFrom = if (hasBackfill) backfillRewoundFrom!! else 0, + dashPayBackfillCovered = if (hasBackfill) backfillCovered!! else ByteArray(0), ), ) } @@ -4024,6 +4070,13 @@ class PlatformWalletPersistenceHandler( internal const val CAPABILITY_WALLET_RESTORE: Long = 0x80 internal const val CAPABILITY_DPNS_NAME_STATES: Long = 0x100 internal const val CAPABILITY_TRACKED_ASSET_LOCKS: Long = 0x200 + /** + * Bytes per contact in the opaque cover set + * [onWalletChangesetDashPayBackfill] delivers: owner id (32), + * contact id (32), covered-from height (4). + */ + internal const val DASHPAY_BACKFILL_COVERED_ENTRY_SIZE: Int = 32 + 32 + 4 + internal const val CAPABILITY_CORE_SWEEP_REMOVAL: Long = NativePersistenceBridge.CAPABILITY_CORE_SWEEP_REMOVAL diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt index 200edbe1947..2e0873d63ba 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt @@ -68,6 +68,20 @@ data class WalletEntity( * has been persisted". */ val lastAppliedChainLockHeight: Int? = null, + /** + * DashPay coreHeight backfill record (dashpay/platform#4302), delivered + * whole by `onWalletChangesetDashPayBackfill` on the same round as the + * lowered `syncedHeight` it belongs with, and handed back unchanged on + * `loadWalletList`. All three NULL (the pre-migration reading) means no + * record: native re-runs the backfill once and writes one. `floor` is the + * lowest height the backfill rewound to, `rewoundFrom` the cursor it + * rewound from — the scan is complete once `syncedHeight` climbs back + * past it — and `covered` the opaque cover set (68 bytes per contact: + * owner id, contact id, covered-from height) only native reads. + */ + val dashPayBackfillFloor: Int? = null, + val dashPayBackfillRewoundFrom: Int? = null, + val dashPayBackfillCovered: ByteArray? = null, val isImported: Boolean = false, val createdAt: Date = Date(), val lastUpdated: Date = Date(), diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt index fb2ba7896b7..8a114035a43 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt @@ -233,15 +233,16 @@ class DashDatabaseTest { } @Test - fun schemaIsAtVersion14WithTheSweepHoldIndexes() = runTest { + fun schemaIsAtVersion15WithTheSweepHoldIndexes() = runTest { // The sweep-hold columns land in ONE migration (10 → 11), with the // two `pending_inputs` indexes the sweep's claimed-row lookup // (`spendingTxid`) and the end-of-round collector // (`walletId, isSweptTombstone, winnerMinedHeight`) rely on. // 11 → 12 adds the identity key usage limits columns on top, - // 12 → 13 the contract bounds kind, and 13 → 14 the token - // once-per-identity distribution block. - assertEquals(14, db.openHelper.readableDatabase.version) + // 12 → 13 the contract bounds kind, 13 → 14 the token + // once-per-identity distribution block, and 14 → 15 the DashPay + // backfill record columns on `wallets`. + assertEquals(15, db.openHelper.readableDatabase.version) val indexes = mutableSetOf() db.openHelper.readableDatabase.query("PRAGMA index_list('pending_inputs')").use { c -> val nameColumn = c.getColumnIndexOrThrow("name") @@ -293,6 +294,38 @@ class DashDatabaseTest { assertTrue(found) } + @Test + fun shouldHaveNullableDashPayBackfillColumnsOnWallets() = runTest { + // Version 15 (14 → 15): all three nullable with no default, so a + // legacy row reads back as "no record" and native rewinds once. + val expected = mapOf( + "dashPayBackfillFloor" to "INTEGER", + "dashPayBackfillRewoundFrom" to "INTEGER", + "dashPayBackfillCovered" to "BLOB", + ) + val found = mutableMapOf() + db.openHelper.readableDatabase.query("PRAGMA table_info('wallets')").use { c -> + val name = c.getColumnIndexOrThrow("name") + val type = c.getColumnIndexOrThrow("type") + val notNull = c.getColumnIndexOrThrow("notnull") + val default = c.getColumnIndexOrThrow("dflt_value") + while (c.moveToNext()) { + val column = c.getString(name) + if (column !in expected) continue + found[column] = c.getString(type) + assertEquals(column, 0, c.getInt(notNull)) + assertTrue(column, c.isNull(default)) + } + } + assertEquals(expected, found) + + db.walletDao().upsert(WalletEntity(walletId = walletId, networkRaw = 1)) + val legacy = db.walletDao().getByWalletId(walletId)!! + assertNull(legacy.dashPayBackfillFloor) + assertNull(legacy.dashPayBackfillRewoundFrom) + assertNull(legacy.dashPayBackfillCovered) + } + @Test fun advanceChainLockHeightIsANarrowMonotonicMaxWrite() = runTest { db.walletDao().upsert(WalletEntity(walletId = walletId, networkRaw = 1, name = "w", syncedHeight = 7)) diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt index b7b1bfd63e9..85b3748f709 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt @@ -6727,6 +6727,156 @@ class PlatformWalletPersistenceHandlerTest { chainLockHeightRound(handler, 600) assertEquals(600, db.walletDao().getByWalletId(walletId)!!.lastAppliedChainLockHeight) } + /** One packed cover-set entry: owner id ‖ contact id ‖ covered-from (LE u32). */ + private fun backfillEntry(owner: Byte, contact: Byte, coveredFrom: Int): ByteArray = + ByteArray(32) { owner } + ByteArray(32) { contact } + byteArrayOf( + (coveredFrom and 0xFF).toByte(), + ((coveredFrom shr 8) and 0xFF).toByte(), + ((coveredFrom shr 16) and 0xFF).toByte(), + ((coveredFrom shr 24) and 0xFF).toByte(), + ) + + /** + * dashpay/platform#4302: the DashPay backfill record is a whole-record + * replace of the three `wallets.dashPayBackfill*` columns, written on + * the same round as the lowered `syncedHeight` the rescan carries. A + * later record replaces the earlier one outright — a contact the new + * record no longer lists is gone, never merged back — and the cover set + * is stored byte-for-byte so native reads back exactly what it wrote. + */ + @Test + fun onWalletChangesetDashPayBackfillReplacesTheRecordOnTheWalletRow() = runTest { + handler.onPersistWalletMetadata(walletId, testnet, groupId, 0) + val row = db.walletDao().getByWalletId(walletId)!! + assertNull("no record until the slot fires", row.dashPayBackfillFloor) + assertNull(row.dashPayBackfillRewoundFrom) + assertNull(row.dashPayBackfillCovered) + + val first = backfillEntry(1, 2, 2_167_092) + backfillEntry(1, 3, 2_300_000) + handler.onChangesetBegin(walletId) + handler.onWalletChangesetHeader( + walletId = walletId, + hasSyncedHeight = true, + syncedHeight = 2_167_092, + hasBalance = false, + confirmedDelta = 0, + unconfirmedDelta = 0, + immatureDelta = 0, + lockedDelta = 0, + lastAppliedChainLockBytes = ByteArray(0), + ) + assertEquals( + 0, + handler.onWalletChangesetDashPayBackfill(walletId, 2_167_092, 2_537_092, first, 2), + ) + handler.onChangesetEnd(walletId, success = true) + + val stored = db.walletDao().getByWalletId(walletId)!! + assertEquals("the lowered cursor lands with the record", 2_167_092, stored.syncedHeight) + assertEquals(2_167_092, stored.dashPayBackfillFloor) + assertEquals(2_537_092, stored.dashPayBackfillRewoundFrom) + assertTrue(first.contentEquals(stored.dashPayBackfillCovered)) + + // A later record replaces the whole cover set. + val second = backfillEntry(1, 2, 2_167_092) + handler.onChangesetBegin(walletId) + handler.onWalletChangesetDashPayBackfill(walletId, 2_100_000, 2_600_000, second, 1) + handler.onChangesetEnd(walletId, success = true) + val replaced = db.walletDao().getByWalletId(walletId)!! + assertEquals(2_100_000, replaced.dashPayBackfillFloor) + assertEquals(2_600_000, replaced.dashPayBackfillRewoundFrom) + assertTrue(second.contentEquals(replaced.dashPayBackfillCovered)) + assertEquals("sibling columns are untouched", 2_167_092, replaced.syncedHeight) + } + + /** + * The record vouches for the cursor written in its round, so a round + * that rolls back must take the record with it: a persisted record over + * a cursor still at its high-water would tell native the backfill + * resumed when it never started — the lossy direction. A malformed cover + * set (not a whole number of 68-byte entries) is refused outright. + */ + @Test + fun onWalletChangesetDashPayBackfillRollsBackWithItsRoundAndRefusesATornCoverSet() = runTest { + handler.onPersistWalletMetadata(walletId, testnet, groupId, 0) + handler.onChangesetBegin(walletId) + handler.onWalletChangesetHeader( + walletId = walletId, + hasSyncedHeight = true, + syncedHeight = 2_537_092, + hasBalance = false, + confirmedDelta = 0, + unconfirmedDelta = 0, + immatureDelta = 0, + lockedDelta = 0, + lastAppliedChainLockBytes = ByteArray(0), + ) + handler.onChangesetEnd(walletId, success = true) + + handler.onChangesetBegin(walletId) + handler.onWalletChangesetHeader( + walletId = walletId, + hasSyncedHeight = true, + syncedHeight = 2_167_092, + hasBalance = false, + confirmedDelta = 0, + unconfirmedDelta = 0, + immatureDelta = 0, + lockedDelta = 0, + lastAppliedChainLockBytes = ByteArray(0), + ) + handler.onWalletChangesetDashPayBackfill( + walletId, 2_167_092, 2_537_092, backfillEntry(1, 2, 2_167_092), 1, + ) + handler.onChangesetEnd(walletId, success = false) + + val row = db.walletDao().getByWalletId(walletId)!! + assertEquals("the rolled-back cursor stays at its committed value", 2_537_092, row.syncedHeight) + assertNull("the rolled-back record never lands", row.dashPayBackfillFloor) + assertNull(row.dashPayBackfillRewoundFrom) + assertNull(row.dashPayBackfillCovered) + + handler.onChangesetBegin(walletId) + val rc = handler.onWalletChangesetDashPayBackfill( + walletId, 2_167_092, 2_537_092, backfillEntry(1, 2, 2_167_092).copyOf(67), 1, + ) + handler.onChangesetEnd(walletId, success = rc == 0) + assertTrue("a torn cover set is refused", rc != 0) + assertNull(db.walletDao().getByWalletId(walletId)!!.dashPayBackfillFloor) + } + + /** + * The restore side of dashpay/platform#4302: a stored record comes back + * on `loadWalletList` exactly as written, and a wallet with no record + * (every pre-migration row) reports none, so native rewinds once for it + * as it always did. + */ + @Test + fun loadWalletListRoundTripsTheDashPayBackfillRecord() = runTest { + handler.onPersistWalletMetadata(walletId, testnet, groupId, 0) + val xpub = ByteArray(78) { 30 } + handler.onPersistAccountRegistration( + walletId, 0, 0, 0, 0, 0, ByteArray(0), ByteArray(0), xpub, + ) + + val before = handler.onLoadWalletList().single() + assertFalse("no record on a fresh wallet", before.hasDashPayBackfill) + assertEquals(0, before.dashPayBackfillFloor) + assertEquals(0, before.dashPayBackfillRewoundFrom) + assertEquals(0, before.dashPayBackfillCovered.size) + + val covered = backfillEntry(1, 2, 2_167_092) + backfillEntry(1, 3, 2_300_000) + handler.onChangesetBegin(walletId) + handler.onWalletChangesetDashPayBackfill(walletId, 2_167_092, 2_537_092, covered, 2) + handler.onChangesetEnd(walletId, success = true) + + val after = handler.onLoadWalletList().single() + assertTrue(after.hasDashPayBackfill) + assertEquals(2_167_092, after.dashPayBackfillFloor) + assertEquals(2_537_092, after.dashPayBackfillRewoundFrom) + assertTrue(covered.contentEquals(after.dashPayBackfillCovered)) + } + } /** From 48b1eb0cfa42b296284a8175a36a18ff4361ac79 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Fri, 25 Sep 2026 23:27:44 -0700 Subject: [PATCH 04/43] fix(platform-wallet): keep the backfill record's extent honest and persist the outbound-account marker Two follow-ups to the record, found on device on the integration line (dashpay/platform#4302, topple relaunch): - `DashPayBackfillRecord::record_pass` with no rewind no longer invents an extent from the cursor it ran at: a pass whose contacts were all forward-covered produced `floor == rewound_from == `, which read complete at any later cursor and measured nothing. The extent is now set by real rewinds only; `has_extent` gates `is_pending` / `is_complete`. - `EstablishedContact::external_account_reference` is documented as persisted (the FFI / Kotlin commits carry it): every cold start read it as `None`, `external_account_needs_rebuild` treated that as a rotation, and the contact pass tore the outbound `DashpayExternalAccount` down and rebuilt it on every launch. On this tree the rebuild costs an account teardown, re-registration and a persisted row per contact per launch; on a tree where registration rewinds the scan cursor (the integration line, and #4740 once it lands) it was the per-launch re-walk itself. Tests pin that an outbound registration never moves the cursor, that a cold start with both accounts persisted and the marker restored has nothing to rebuild and leaves the cursor at the tip (with the marker stripped the rebuild still leaves it there), and that a forward-only pass on an empty record leaves no extent. `collect_account_build_candidates` and `external_account_needs_rebuild` become `pub(super)` for those tests. Co-Authored-By: Claude Fable 5.1 --- .../src/changeset/dashpay_backfill.rs | 53 +++- .../identity/network/contact_requests.rs | 19 +- .../src/wallet/identity/network/payments.rs | 295 +++++++++++++++--- .../types/dashpay/established_contact.rs | 11 +- 4 files changed, 319 insertions(+), 59 deletions(-) diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 7aa615885f7..2b8a716403f 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -88,11 +88,11 @@ pub struct DashPayBackfillCoveredContact { #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct DashPayBackfillRecord { /// Lowest height a backfill rewound the cursor to. Meaningless while - /// `covered` is empty. + /// [`has_extent`](Self::has_extent) is false. pub floor: u32, /// Highest cursor a backfill rewound from — the height the scan has to - /// climb back to for the backfill to be complete. Meaningless while - /// `covered` is empty. + /// climb back to for the backfill to be complete. `0` while no pass has + /// rewound anything ([`has_extent`](Self::has_extent)). pub rewound_from: u32, /// Receival contacts the backfill covers, sorted by `(owner, contact)`, /// at most one entry per pair. @@ -122,15 +122,23 @@ impl DashPayBackfillRecord { .is_some_and(|covered_from| checkpoint >= covered_from) } + /// Whether a backfill ever rewound the cursor, i.e. whether `floor` / + /// `rewound_from` describe anything. A record whose every contact was + /// forward-covered has entries but no extent, and `rewound_from == 0` + /// cannot come from a real rewind (a checkpoint is never below 0). + pub fn has_extent(&self) -> bool { + self.rewound_from > 0 + } + /// The backfill is still climbing back to the cursor it rewound from. pub fn is_pending(&self, synced_height: u32) -> bool { - !self.is_empty() && synced_height < self.rewound_from + self.has_extent() && synced_height < self.rewound_from } /// The scan has climbed back past the cursor the backfill rewound from, /// so every block in `[floor, rewound_from]` has been re-matched. pub fn is_complete(&self, synced_height: u32) -> bool { - !self.is_empty() && synced_height >= self.rewound_from + self.has_extent() && synced_height >= self.rewound_from } /// Fold one reconcile pass into the record. @@ -171,13 +179,18 @@ impl DashPayBackfillRecord { } } } - if self.covered.is_empty() { + // The extent describes real rewinds only. A pass that rewound nothing + // (every contact forward-covered, or already covered) says nothing + // about how far a backfill reached, so it must not invent one from + // the cursor it happened to run at — on a record that had no extent + // yet that would make `floor == rewound_from == cursor`, which reads + // complete at any later cursor and measures nothing. + let Some(pass_floor) = floor else { return changed; - } - // A first record takes the pass's extent as-is; a later pass can only + }; + // A first rewind takes the pass's extent as-is; a later one can only // widen it — deeper floor, higher climb target. - let pass_floor = floor.unwrap_or(synced_height_before); - let (new_floor, new_rewound_from) = if was_empty { + let (new_floor, new_rewound_from) = if was_empty || !self.has_extent() { (pass_floor, synced_height_before) } else { ( @@ -300,6 +313,26 @@ mod tests { assert!(!record.record_pass(1_500, None, [(id(1), id(5), 1_700)])); } + /// A pass that rewound nothing must not invent an extent from the cursor + /// it ran at: on the device that produced `floor == rewound_from == + /// 1251329` — the cursor of a later pass — which read complete at any + /// later cursor. The first real rewind then sets the extent. + #[test] + fn a_forward_only_pass_on_an_empty_record_leaves_no_extent() { + let mut record = DashPayBackfillRecord::default(); + assert!(record.record_pass(1_251_329, None, [(id(1), id(2), 1_300_000)])); + assert!(!record.has_extent()); + assert!(!record.is_pending(1_000)); + assert!(!record.is_complete(2_000_000)); + assert!(record.covers(&id(1), &id(2), 1_300_000)); + + assert!(record.record_pass(1_560_731, Some(1_226_329), [(id(1), id(3), 1_226_329)])); + assert!(record.has_extent()); + assert_eq!((record.floor, record.rewound_from), (1_226_329, 1_560_731)); + assert!(record.is_pending(1_251_329)); + assert!(record.is_complete(1_560_731)); + } + #[test] fn coverage_is_per_contact_and_keyed_on_the_recorded_checkpoint() { let mut record = DashPayBackfillRecord::default(); 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 045e5a3b0db..132a5c6db3a 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 @@ -1075,11 +1075,16 @@ pub(crate) fn budget_spent(deadline: Option) -> bool { /// A permanently-broken channel is left alone (the sweep never rebuilds /// broken contacts; they heal on a superseding request). The staleness test /// is `external_account_reference != Some(incoming_request.account_reference)`: -/// a mismatch (or a `None` marker from a cold restart that did not carry it) -/// means the persisted, tombstone-less account row rebuilt the rotated-away -/// xpub while the contact already tracks the new reference — so `send_payment` -/// would derive addresses the contact no longer watches until it is rebuilt. -fn external_account_needs_rebuild(contact: &EstablishedContact, has_external: bool) -> bool { +/// a mismatch (or a `None` marker from a cold restart on a host that did not +/// carry it — the in-tree hosts persist it on the established rows, so a +/// healthy account survives a restart without a rebuild) means the persisted, +/// tombstone-less account row rebuilt the rotated-away xpub while the contact +/// already tracks the new reference — so `send_payment` would derive addresses +/// the contact no longer watches until it is rebuilt. +pub(super) fn external_account_needs_rebuild( + contact: &EstablishedContact, + has_external: bool, +) -> bool { has_external && !contact.payment_channel_broken && contact.external_account_reference != Some(contact.incoming_request.account_reference) @@ -1842,7 +1847,7 @@ impl DashPayView<'_, B> { /// permanently broken — the account-building candidates for this /// sweep. Runs under the caller's write guard; performs no /// awaits and no lock re-acquisition. - fn collect_account_build_candidates( + pub(super) fn collect_account_build_candidates( info: &crate::wallet::platform_wallet::PlatformWalletInfo, identity_id: &Identifier, ) -> Vec { @@ -3362,7 +3367,7 @@ impl DashPayView<'_, B> { /// One established contact that needs its DashPay accounts (re)built /// during a sync sweep. Collected under the write guard, consumed /// after it is dropped. -struct AccountBuildCandidate { +pub(super) struct AccountBuildCandidate { /// The counterparty identity. contact_id: Identifier, /// The counterparty's 96-byte encrypted xpub (from their incoming diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 287efdbaa9d..c278bfaa0eb 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -2809,6 +2809,9 @@ mod tests { .register_contact_account(&owner, &contact, 0, test_receiving_xpub(&owner, &contact)) .await .expect("register receival account"); + // An account on file that the record does not know (see + // `forget_recorded_coverage`). + forget_recorded_coverage(&manager, wallet_id, owner, contact).await; { let mut wm = iw.wallet_manager.write().await; @@ -2901,6 +2904,29 @@ mod tests { .managed_identity_mut(&owner) .expect("managed") .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + // Registration records its own coverage now. These fixtures exercise + // the reconcile's rewind path, which serves accounts persisted before + // the record existed (and hosts that do not store it), so model that: + // the account is on file, the record does not know it. + info.dashpay_backfill + .retain(|o, c| (o, c) != (&owner, &contact)); + } + + /// Drop `(owner, contact)` from the wallet's backfill record — the shape + /// of an account registered before the record existed, or by a host + /// that does not persist it — so the reconcile's own rewind path is what + /// a test exercises. + async fn forget_recorded_coverage( + manager: &Arc>, + wallet_id: WalletId, + owner: Identifier, + contact: Identifier, + ) { + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .dashpay_backfill + .retain(|o, c| (o, c) != (&owner, &contact)); } async fn set_synced_height( @@ -3491,14 +3517,46 @@ mod tests { ); } - /// Registering the account of an already-established contact marks it in - /// memory without a reconcile pass ever seeing it as a candidate. The - /// next pass must still write it into the record — with no rewind, since - /// registration already lowered the cursor for it — or the following - /// launch rewinds for it once more. + /// A contact's compact receiving xpub encrypted under `shared_key` — the + /// wire shape a contact sends us — so `register_external_contact_account` + /// can be driven without a signer. + fn encrypted_contact_xpub( + owner: &Identifier, + contact: &Identifier, + shared_key: &[u8; 32], + ) -> Vec { + let seed = Mnemonic::from_phrase(TEST_MNEMONIC) + .expect("mnemonic") + .to_seed(""); + let w = key_wallet::wallet::Wallet::from_seed_bytes( + seed, + Network::Testnet, + WalletAccountCreationOptions::None, + ) + .expect("seed wallet"); + let compact = crate::wallet::identity::crypto::dip14::derive_contact_xpub( + &w, + Network::Testnet, + 0, + owner, + contact, + ) + .expect("derive a valid compact xpub") + .compact + .to_bytes(); + platform_encryption::encrypt_extended_public_key(shared_key, &[0x11u8; 16], &compact) + } + + /// The outbound `DashpayExternalAccount` is watch-only and never + /// receives, so registering (or rebuilding) it must not move the + /// filter-scan cursor. Every cold start whose host does not persist + /// `external_account_reference` tears the account down and rebuilds it; + /// on a tree where that rebuild also rewound the cursor it re-walked + /// every filter from the earliest contact's request height per launch + /// (dashpay/platform#4302, the topple relaunch). Pin the invariant so a + /// registration-time rewind (#4740) cannot pick the outbound account up. #[tokio::test] - async fn rescan_records_a_contact_registration_marked_in_memory_so_a_reload_does_not_rewind_for_it( - ) { + async fn external_account_registration_leaves_the_scan_cursor_untouched() { use crate::wallet::identity::{ContactRequest, EstablishedContact}; let (manager, persister, wallet_id) = make_wallet().await; @@ -3513,63 +3571,222 @@ mod tests { info.identity_manager .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) .expect("add owner"); - let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); - let incoming = ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 100, 0); + let outgoing = + ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 1_226_329, 0); + let incoming = + ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 1_226_329, 0); info.identity_manager .managed_identity_mut(&owner) .expect("managed") .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); - info.core_wallet.update_synced_height(1_000); + info.core_wallet.update_synced_height(1_560_740); } - // Established first, registered second: registration lowers the - // cursor to the checkpoint and marks the contact in memory. - iw.dashpay() - .register_contact_account(&owner, &contact, 0, test_receiving_xpub(&owner, &contact)) + + let shared_key = [0x55u8; 32]; + let encrypted = encrypted_contact_xpub(&owner, &contact, &shared_key); + let registration = iw + .dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &encrypted, + zeroize::Zeroizing::new(shared_key), + ) .await - .expect("register receival account"); - assert_eq!(synced_height(&manager, wallet_id).await, 100); - persister.stores.lock().unwrap().clear(); + .expect("register the outbound account"); + assert_eq!( + registration, + crate::wallet::identity::network::contacts::ExternalAccountRegistration::Built + ); + assert_eq!( + synced_height(&manager, wallet_id).await, + 1_560_740, + "an outbound account never receives: building it must not rewind the scan" + ); + } + + /// The observed relaunch, end to end (dashpay/platform#4302, topple on + /// int23): a restore with established contacts, both accounts persisted, + /// the cursor at the tip and a record covering every contact. With the + /// outbound-account marker restored, the contact pass finds nothing to + /// rebuild; without it (a host that does not carry the marker) the + /// rebuild still leaves the cursor alone; and the reconcile pass after + /// either leaves the cursor at the tip. + #[tokio::test] + async fn a_cold_start_with_persisted_accounts_and_record_leaves_the_cursor_at_the_tip() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use key_wallet::account::account_collection::DashpayAccountKey; + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contacts: Vec = (1..=8u8).map(|b| Identifier::from([b; 32])).collect(); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + { + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let managed = info + .identity_manager + .managed_identity_mut(&owner) + .expect("managed"); + for (i, contact) in contacts.iter().enumerate() { + let height = 1_226_329 + (i as u32) * 5_000; + let outgoing = + ContactRequest::new(owner, *contact, 0, 0, 0, vec![0u8; 96], height, 0); + // The incoming request carries the ciphertext the outbound + // account is built from, so the marker stamp accepts it. + let ciphertext = encrypted_contact_xpub(&owner, contact, &[0x55u8; 32]); + let incoming = ContactRequest::new(*contact, owner, 0, 0, 7, ciphertext, height, 0); + managed.apply_established_contact(EstablishedContact::new( + *contact, outgoing, incoming, + )); + } + } + // First session: the scan is at the tip when the contacts arrive. + // Registration never moves the cursor here; the reconcile pass then + // rewinds once to the earliest request height and records every + // contact, every outbound registration stamps the marker, and the scan + // climbs back to the tip. + set_synced_height(&manager, wallet_id, 1_560_740).await; + let shared_key = [0x55u8; 32]; + for contact in &contacts { + iw.dashpay() + .register_contact_account(&owner, contact, 0, test_receiving_xpub(&owner, contact)) + .await + .expect("register receival"); + let encrypted = encrypted_contact_xpub(&owner, contact, &shared_key); + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &encrypted, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register outbound"); + iw.dashpay() + .note_external_account_registered(&owner, contact, &encrypted) + .await; + } + assert_eq!( + synced_height(&manager, wallet_id).await, + 1_560_740, + "registration alone never moves the cursor" + ); assert_eq!( iw.dashpay() .reconcile_dashpay_rescan() .await - .expect("pass after registration"), - None, - "registration already lowered the cursor; the pass must not rewind again" + .expect("first reconcile"), + Some(1_226_329) ); + set_synced_height(&manager, wallet_id, 1_560_740).await; + + // The host persisted everything: accounts, contacts with the marker, + // the record, the cursor at the tip. Cold start. + let snapshot = reload_snapshot(&manager, wallet_id, owner, 1_560_740, None).await; + let (restarted, _, _) = reload(snapshot).await; { - let stores = persister.stores.lock().unwrap(); - let round = last_stored_record(&stores).expect("the pass must record the contact"); + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + let managed = info.identity_manager.managed_identity(&owner).unwrap(); + for contact in &contacts { + let established = managed.dashpay().established_contacts()[contact].clone(); + assert_eq!(established.external_account_reference, Some(7)); + assert!( + !external_account_needs_rebuild(&established, true), + "a restored marker must not read as a rotation" + ); + } assert!( - round.core.is_none(), - "no rewind fired, so the round carries no cursor" + crate::wallet::identity::network::dashpay_view::DashPayView::< + crate::broadcaster::SpvBroadcaster, + >::collect_account_build_candidates(info, &owner) + .is_empty(), + "both accounts persisted: the contact pass has nothing to build" ); + } + let restored = restarted.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + restored + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None + ); + assert_eq!(synced_height_of(&restarted, wallet_id).await, 1_560_740); + + // A host that did not carry the marker: the pass tears the outbound + // accounts down and the drain rebuilds them — and even that must not + // move the cursor any more. + let mut bare = reload_snapshot(&manager, wallet_id, owner, 1_560_740, None).await; + for contact in &contacts { + let established = bare.identities[&0].dashpay().established_contacts()[contact].clone(); + let mut stripped = established.clone(); + stripped.external_account_reference = None; + bare.identities + .get_mut(&0) + .unwrap() + .apply_established_contact(stripped); + assert!(external_account_needs_rebuild( + &bare.identities[&0].dashpay().established_contacts()[contact], + true + )); + } + let (restarted, _, _) = reload(bare).await; + let restored = restarted.get_wallet(&wallet_id).await.expect("wallet"); + for contact in &contacts { + { + let mut wm = restarted.wallet_manager.write().await; + let (wallet, info) = wm.get_wallet_mut_and_info_mut(&wallet_id).unwrap(); + let key = DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }; + wallet.accounts.dashpay_external_accounts.remove(&key); + info.core_wallet + .accounts + .dashpay_external_accounts + .remove(&key); + } + let encrypted = encrypted_contact_xpub(&owner, contact, &shared_key); assert_eq!( - round - .dashpay_backfill - .as_ref() - .unwrap() - .covered_from(&owner, &contact), - Some(100) + restored + .identity() + .dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &encrypted, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("rebuild outbound"), + crate::wallet::identity::network::contacts::ExternalAccountRegistration::Built ); } - - let snapshot = reload_snapshot(&manager, wallet_id, owner, 400, None).await; - let (restarted, _, _) = reload(snapshot).await; assert_eq!( - restarted - .get_wallet(&wallet_id) - .await - .expect("wallet") + restored .identity() .dashpay() .reconcile_dashpay_rescan() .await - .expect("pass after reload"), + .expect("reconcile"), None ); - assert_eq!(synced_height_of(&restarted, wallet_id).await, 400); + assert_eq!( + synced_height_of(&restarted, wallet_id).await, + 1_560_740, + "rebuilding every outbound account on a cold start must leave the cursor at the tip" + ); } async fn synced_height_of( diff --git a/packages/rs-platform-wallet/src/wallet/identity/types/dashpay/established_contact.rs b/packages/rs-platform-wallet/src/wallet/identity/types/dashpay/established_contact.rs index 62c74084b8a..114265436f9 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/types/dashpay/established_contact.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/types/dashpay/established_contact.rs @@ -82,9 +82,14 @@ pub struct EstablishedContact { /// lets the sweep detect a stale account and tear it down + rebuild. /// /// Reset to `None` on (re-)establish and on rotation (the account must be - /// rebuilt from the new key material). A cold restart that does not - /// restore this field leaves it `None`, which conservatively forces one - /// rebuild on the next sweep — self-healing either way. + /// rebuilt from the new key material). Persisted on the contact's + /// established rows (`ContactRequestFFI::external_account_reference`, the + /// Kotlin `dashpay_contact_requests.externalAccountReference` column) so + /// a cold restart restores it and a healthy account is NOT rebuilt. A + /// host that does not carry it leaves it `None`, which conservatively + /// forces one rebuild on the next sweep — self-healing, but on every + /// launch; the rebuild used to also rewind the filter-scan cursor, which + /// is the per-launch re-walk of dashpay/platform#4302. #[cfg_attr(feature = "serde", serde(default))] pub external_account_reference: Option, } From d3b1298bfebe7702edd526bb85360eaca938babd Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Fri, 25 Sep 2026 23:27:45 -0700 Subject: [PATCH 05/43] feat(platform-wallet-ffi): persist the outbound contact-account marker on the contact rows Append `has_external_account_reference` / `external_account_reference` to `ContactRequestFFI` (208 bytes, layout table updated), replicated onto both established rows like the broken-channel flag and absent on pending rows, and restore it into `EstablishedContact::external_account_reference` at load. The JNI `onPersistContactUpsert` descriptor gains the trailing `(Z, I)` pair and the restore reader takes it from `ContactRequestRestoreData`. Without it every cold start rebuilt every outbound account (dashpay/platform#4302). Co-Authored-By: Claude Fable 5.1 --- .../src/contact_persistence.rs | 103 +++++++++++++++++- .../rs-platform-wallet-ffi/src/persistence.rs | 17 +++ .../rs-unified-sdk-jni/src/persistence.rs | 32 +++++- 3 files changed, 145 insertions(+), 7 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/contact_persistence.rs b/packages/rs-platform-wallet-ffi/src/contact_persistence.rs index d0b79103821..416e82870dc 100644 --- a/packages/rs-platform-wallet-ffi/src/contact_persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/contact_persistence.rs @@ -149,6 +149,23 @@ pub struct ContactRequestFFI { /// Number of `u32` entries in [`Self::accepted_accounts`]; `0` when the /// pointer is null. pub accepted_accounts_len: usize, + /// Whether [`Self::external_account_reference`] is set — the mirror of + /// `EstablishedContact::external_account_reference` being `Some`. + /// Relationship-level, replicated onto BOTH established rows like + /// [`Self::payment_channel_broken`]; always `false` on pending rows. + /// + /// Without this the marker died with the process: every cold start saw + /// `None`, treated it as a rotation, tore the outbound + /// `DashpayExternalAccount` down and rebuilt it — and the rebuild rewound + /// the filter-scan cursor to the earliest contact's request height on + /// every launch (dashpay/platform#4302). A host that leaves it `false` + /// keeps that rebuild (the cursor no longer moves for it, but the + /// account churn remains). + pub has_external_account_reference: bool, + /// The `incoming_request.account_reference` the registered outbound + /// account was built from; meaningful only when + /// [`Self::has_external_account_reference`]. + pub external_account_reference: u32, } /// Composite identifier for [`ContactChangeSet::removed_sent`] and @@ -228,9 +245,12 @@ pub struct ContactIgnoredSenderFFI { // 176..=183 contact_account_label *const c_char // 184..=191 accepted_accounts *const u32 // 192..=199 accepted_accounts_len usize +// 200 has_external_account_reference bool +// 201..=203 (padding to 4) +// 204..=207 external_account_reference u32 // -// Total size = 200, alignment = 8 (from u64 / pointer fields). -const _: [u8; 200] = [0u8; std::mem::size_of::()]; +// Total size = 208, alignment = 8 (from u64 / pointer fields). +const _: [u8; 208] = [0u8; std::mem::size_of::()]; const _: [u8; 8] = [0u8; std::mem::align_of::()]; // Expected `ContactRequestRemovalFFI` layout: 64 bytes, alignment 1. @@ -297,6 +317,7 @@ impl ContactRequestFFI { false, None, &[], + None, ) } @@ -318,6 +339,7 @@ impl ContactRequestFFI { false, None, &[], + None, ) } @@ -339,6 +361,7 @@ impl ContactRequestFFI { note: Option<&str>, is_hidden: bool, accepted_accounts: &[u32], + external_account_reference: Option, ) -> Self { Self::from_parts( owner_id, @@ -353,6 +376,7 @@ impl ContactRequestFFI { // it is direction-specific (incoming-only). None, accepted_accounts, + external_account_reference, ) } @@ -371,6 +395,7 @@ impl ContactRequestFFI { is_hidden: bool, contact_account_label: Option<&str>, accepted_accounts: &[u32], + external_account_reference: Option, ) -> Self { Self::from_parts( owner_id, @@ -383,6 +408,7 @@ impl ContactRequestFFI { is_hidden, contact_account_label, accepted_accounts, + external_account_reference, ) } @@ -398,6 +424,7 @@ impl ContactRequestFFI { is_hidden: bool, contact_account_label: Option<&str>, accepted_accounts: &[u32], + external_account_reference: Option, ) -> Self { let (encrypted_public_key, encrypted_public_key_len) = allocate_byte_buffer(&request.encrypted_public_key); @@ -434,6 +461,8 @@ impl ContactRequestFFI { contact_account_label: allocate_c_string(contact_account_label), accepted_accounts, accepted_accounts_len, + has_external_account_reference: external_account_reference.is_some(), + external_account_reference: external_account_reference.unwrap_or(0), } } } @@ -692,6 +721,7 @@ mod tests { Some("a note"), true, &[], + None, ); let mut inc = ContactRequestFFI::from_established_incoming( owner, @@ -703,6 +733,7 @@ mod tests { true, None, &[], + None, ); assert!(out.is_outgoing); assert!(!inc.is_outgoing); @@ -726,6 +757,7 @@ mod tests { None, false, &[], + None, ); assert!(!healthy.payment_channel_broken); assert!(healthy.alias.is_null()); @@ -762,6 +794,7 @@ mod tests { None, false, &[], + None, ); let mut inc = ContactRequestFFI::from_established_incoming( owner, @@ -773,6 +806,7 @@ mod tests { false, Some("Main wallet"), &[], + None, ); assert!( @@ -796,6 +830,71 @@ mod tests { ); } + /// `EstablishedContact::external_account_reference` crosses as a + /// `(has, value)` pair on BOTH established rows (it is relationship-level, + /// like the broken-channel flag) and is absent on pending rows. Losing it + /// across a restart is what rebuilt every outbound account per launch + /// (dashpay/platform#4302). + #[test] + fn established_rows_carry_the_external_account_reference_pending_rows_do_not() { + let request = sample_request(); + let owner = [5u8; 32]; + let contact = [6u8; 32]; + + let mut out = ContactRequestFFI::from_established_outgoing( + owner, + contact, + &request, + false, + None, + None, + false, + &[], + Some(4), + ); + let mut inc = ContactRequestFFI::from_established_incoming( + owner, + contact, + &request, + false, + None, + None, + false, + None, + &[], + Some(4), + ); + for row in [&out, &inc] { + assert!(row.has_external_account_reference); + assert_eq!(row.external_account_reference, 4); + } + + let mut unstamped = ContactRequestFFI::from_established_incoming( + owner, + contact, + &request, + false, + None, + None, + false, + None, + &[], + None, + ); + assert!(!unstamped.has_external_account_reference); + assert_eq!(unstamped.external_account_reference, 0); + + let mut pending = ContactRequestFFI::from_incoming(owner, contact, &request); + assert!(!pending.has_external_account_reference); + + unsafe { + free_contact_requests_ffi(&mut out as *mut ContactRequestFFI, 1); + free_contact_requests_ffi(&mut inc as *mut ContactRequestFFI, 1); + free_contact_requests_ffi(&mut unstamped as *mut ContactRequestFFI, 1); + free_contact_requests_ffi(&mut pending as *mut ContactRequestFFI, 1); + } + } + /// `ContactIgnoredSenderFFI::new` must carry the `(owner, sender)` /// suppression key and the insert/remove `is_ignored` bit, so the /// Swift handler can persist an ignore (`true`) or delete an diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index cb57ce92997..9e0078ea5b0 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -2668,6 +2668,7 @@ impl PlatformWalletPersistence for FFIPersister { established.note.as_deref(), established.is_hidden, &established.accepted_accounts, + established.external_account_reference, )); upserts.push(ContactRequestFFI::from_established_incoming( key.owner_id.to_buffer(), @@ -2681,6 +2682,7 @@ impl PlatformWalletPersistence for FFIPersister { // rides only the incoming row. established.contact_account_label.as_deref(), &established.accepted_accounts, + established.external_account_reference, )); } let removed_sent: Vec = contacts_cs @@ -6501,6 +6503,7 @@ unsafe fn apply_contact_rows( is_hidden: bool, contact_account_label: Option, accepted_accounts: Vec, + external_account_reference: Option, } let opt_string = |ptr: *const std::os::raw::c_char| -> Option { @@ -6573,6 +6576,12 @@ unsafe fn apply_contact_rows( if acc.accepted_accounts.is_empty() { acc.accepted_accounts = u32s(row.accepted_accounts, row.accepted_accounts_len); } + // The outbound-account marker is relationship-level too; a host + // that never stored it hands back `false`, which restores `None` + // and keeps the conservative one-rebuild behaviour. + if acc.external_account_reference.is_none() && row.has_external_account_reference { + acc.external_account_reference = Some(row.external_account_reference); + } } for (contact_id_bytes, acc) in by_contact { @@ -6586,6 +6595,7 @@ unsafe fn apply_contact_rows( contact.payment_channel_broken = acc.payment_channel_broken; contact.contact_account_label = acc.contact_account_label; contact.accepted_accounts = acc.accepted_accounts; + contact.external_account_reference = acc.external_account_reference; managed.apply_established_contact(contact); } (Some(outgoing), None) => { @@ -10524,6 +10534,7 @@ mod tests { Some("a note"), true, &[7, 42], + Some(4), ), ContactRequestFFI::from_established_incoming( owner.to_buffer(), @@ -10535,6 +10546,7 @@ mod tests { true, Some("Main wallet"), &[7, 42], + Some(4), ), ]; @@ -10591,6 +10603,11 @@ mod tests { assert_eq!(e.note.as_deref(), Some("a note")); assert!(e.is_hidden); assert!(e.payment_channel_broken); + assert_eq!( + e.external_account_reference, + Some(4), + "the outbound-account marker must survive the restore (dashpay/platform#4302)" + ); assert_eq!( e.contact_account_label.as_deref(), Some("Main wallet"), diff --git a/packages/rs-unified-sdk-jni/src/persistence.rs b/packages/rs-unified-sdk-jni/src/persistence.rs index f0f1492402c..40ef98b15c7 100644 --- a/packages/rs-unified-sdk-jni/src/persistence.rs +++ b/packages/rs-unified-sdk-jni/src/persistence.rs @@ -1557,7 +1557,7 @@ unsafe fn persist_contact_upsert( env.call_method( bridge, "onPersistContactUpsert", - "([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[I)I", + CONTACT_UPSERT_DESCRIPTOR, &[ wid.into(), (&owner).into(), @@ -1577,11 +1577,25 @@ unsafe fn persist_contact_upsert( JValue::Bool(c.is_hidden as u8), (&contact_account_label).into(), (&accepted).into(), + JValue::Bool(c.has_external_account_reference as u8), + JValue::Int(if c.has_external_account_reference { + c.external_account_reference as i32 + } else { + 0 + }), ], )? .i() } +/// Descriptor of `NativePersistenceBridge.onPersistContactUpsert`. The +/// trailing `(Z, I)` pair is `EstablishedContact::external_account_reference` +/// as `(hasExternalAccountReference, externalAccountReference)` — the marker +/// whose loss across restart rebuilt every outbound contact account on every +/// launch (dashpay/platform#4302). +const CONTACT_UPSERT_DESCRIPTOR: &str = + "([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[IZI)I"; + /// Copy `len` `u32`s from `ptr` (or 0 when null) into a JVM `int[]` /// (bit-pattern cast — DIP-15 account indices never exceed `i32::MAX` /// in practice, and the Kotlin side reads them back with the same cast). @@ -3463,6 +3477,15 @@ fn build_contact_restore( let is_hidden = env.get_field(holder, "isHidden", "Z")?.z()?; let contact_account_label = read_opt_cstring_field(env, holder, "contactAccountLabel")?; let accepted_accounts = read_u32_array_field(env, holder, "acceptedAccounts")?; + let has_external_account_reference = env + .get_field(holder, "hasExternalAccountReference", "Z")? + .z()?; + let external_account_reference = if has_external_account_reference { + env.get_field(holder, "externalAccountReference", "I")? + .i()? as u32 + } else { + 0 + }; let row = ContactRequestFFI { owner_id, @@ -3486,6 +3509,8 @@ fn build_contact_restore( contact_account_label: ptr::null(), accepted_accounts: ptr::null(), accepted_accounts_len: 0, + has_external_account_reference, + external_account_reference, }; Ok(ContactRestoreStaged { row, @@ -4711,10 +4736,7 @@ const BRIDGE_METHOD_TABLE: &[(&str, &str)] = &[ ("onPersistTokenBalanceUpsert", "([B[B[BJ)I"), ("onPersistTokenBalanceRemoval", "([B[B[B)I"), ("onPersistContactIgnored", "([B[B[BZ)I"), - ( - "onPersistContactUpsert", - "([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[I)I", - ), + ("onPersistContactUpsert", CONTACT_UPSERT_DESCRIPTOR), ("onPersistContactRemovalSent", "([B[B[B)I"), ("onPersistContactRemovalIncoming", "([B[B[B)I"), ("onPersistAssetLockUpsert", "([B[B[BIBIJB[B)I"), From ee8fcc1d821e3e3fde3600ce198be7d27e14e00e Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Fri, 25 Sep 2026 23:27:45 -0700 Subject: [PATCH 06/43] feat(kotlin-sdk): store the outbound contact-account marker on the contact row `dashpay_contact_requests.externalAccountReference` (nullable, added to the unreleased 13 -> 14 migration alongside the backfill columns) round-trips `EstablishedContact::external_account_reference` through `onPersistContactUpsert`'s new trailing `(hasExternalAccountReference, externalAccountReference)` pair and `ContactRequestRestoreData`. NULL for pre-migration rows: native rebuilds the outbound account once, stamps it, and never again (dashpay/platform#4302). Co-Authored-By: Claude Fable 5.1 --- .../persistence/DashDatabaseMigrationTest.kt | 28 ++++++++++ .../dashsdk/ffi/NativePersistenceBridge.kt | 21 ++++++- .../dashsdk/persistence/DashDatabase.kt | 21 +++++-- .../PlatformWalletPersistenceHandler.kt | 6 ++ .../entities/DashpayContactRequestEntity.kt | 10 ++++ .../dashsdk/persistence/DashDatabaseTest.kt | 15 +++++ .../PlatformWalletPersistenceHandlerTest.kt | 55 +++++++++++++++++++ 7 files changed, 150 insertions(+), 6 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt b/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt index ff2cfc31d11..9a98201510b 100644 --- a/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt +++ b/packages/kotlin-sdk/sdk/src/androidTest/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseMigrationTest.kt @@ -630,11 +630,39 @@ class DashDatabaseMigrationTest { "syncedHeight, lastSynced, isImported, createdAt, lastUpdated) " + "VALUES (x'01', x'02', 1, 'w', 0, 2309809, 0, 0, 0, 0)", ) + execSQL( + "INSERT INTO identities (identityId, balance, revision, isLocal, identityType, " + + "createdAt, lastUpdated, networkRaw, identityIndex, walletId) " + + "VALUES (x'0A', 0, 0, 1, 'User', 0, 0, 1, 0, x'01')", + ) + execSQL( + "INSERT INTO dashpay_contact_requests (networkRaw, ownerIdentityId, " + + "contactIdentityId, isOutgoing, senderKeyIndex, recipientKeyIndex, " + + "accountReference, encryptedPublicKey, coreHeightCreatedAt, createdAtMillis, " + + "paymentChannelBroken, contactHidden, createdAt, lastUpdated) " + + "VALUES (1, x'0A', x'0B', 0, 2, 3, 4, x'05', 1226329, 0, 0, 0, 0, 0)", + ) close() } val db = helper.runMigrationsAndValidate(dbName, 15, true, DashDatabase.MIGRATION_14_15) + db.query( + "SELECT externalAccountReference FROM dashpay_contact_requests WHERE ownerIdentityId = x'0A'", + ).use { c -> + assertTrue(c.moveToFirst()) + assertTrue("pre-migration contact rows carry no outbound-account marker", c.isNull(0)) + } + db.execSQL( + "UPDATE dashpay_contact_requests SET externalAccountReference = 4 WHERE ownerIdentityId = x'0A'", + ) + db.query( + "SELECT externalAccountReference FROM dashpay_contact_requests WHERE ownerIdentityId = x'0A'", + ).use { c -> + assertTrue(c.moveToFirst()) + assertEquals(4, c.getInt(0)) + } + db.query( "SELECT dashPayBackfillFloor, dashPayBackfillRewoundFrom, dashPayBackfillCovered, " + "syncedHeight FROM wallets WHERE walletId = x'01'", diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt index 014208f4ec0..c1b747c8b85 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt @@ -592,13 +592,22 @@ abstract class NativePersistenceBridge { /** * One `ContactRequestFFI` upsert. Descriptor - * `([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[I)I`. + * `([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[IZI)I`. * * The tail block ([paymentChannelBroken] / [alias] / [note] / * [isHidden] / [contactAccountLabel] / [acceptedAccounts]) is * established-row relationship metadata (contactInfo + DIP-15 * accepted accounts) — null / false / empty on pending rows. * [acceptedAccounts] is never null (empty when absent). + * + * [hasExternalAccountReference] / [externalAccountReference] mirror + * `EstablishedContact::external_account_reference`: the incoming + * `accountReference` the registered outbound (sending) account was + * built from. Store it and hand it back on + * [ContactRequestRestoreData]; a cold start that restores `None` + * treats the account as rotated and rebuilds it on every launch + * (dashpay/platform#4302). Replicated onto both established rows; + * `false` / 0 on pending rows. */ @Suppress("LongParameterList") open fun onPersistContactUpsert( @@ -620,6 +629,8 @@ abstract class NativePersistenceBridge { isHidden: Boolean, contactAccountLabel: String?, acceptedAccounts: IntArray, + hasExternalAccountReference: Boolean = false, + externalAccountReference: Int = 0, ): Int = 0 /** One sent-side `ContactRequestRemovalFFI`. Descriptor `([B[B[B)I`. */ @@ -1310,6 +1321,14 @@ class ContactRequestRestoreData( @JvmField val isHidden: Boolean, @JvmField val contactAccountLabel: String?, @JvmField val acceptedAccounts: IntArray, + /** + * `EstablishedContact::external_account_reference` as a `(present, + * value)` pair, exactly as [NativePersistenceBridge.onPersistContactUpsert] + * delivered it. `false` restores `None`, which makes native rebuild the + * outbound account once on the next sweep. + */ + @JvmField val hasExternalAccountReference: Boolean = false, + @JvmField val externalAccountReference: Int = 0, ) /** diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt index 1603d26f164..fc639991c85 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabase.kt @@ -171,7 +171,14 @@ import org.dashfoundation.dashsdk.persistence.entities.WalletManagerMetadataEnti * while the cursor it lowers is durable, so every fresh process rewound again * and re-walked every filter from the earliest contact's core height. All * three NULL (every pre-migration row) means no record: native rewinds once - * more, writes one, and never again for the contacts it covers. + * more, writes one, and never again for the contacts it covers. The same + * version adds the nullable `dashpay_contact_requests.externalAccountReference` + * column — `EstablishedContact::external_account_reference`, the marker that + * says which incoming `accountReference` the outbound sending account was + * built from. It was never persisted, so every cold start read it as absent, + * treated the account as rotated, tore it down and rebuilt it — and that + * rebuild was the per-launch cursor rewind. NULL for pre-migration rows: + * native rebuilds once, stamps it, and never again. */ @Database( version = 15, @@ -692,16 +699,20 @@ abstract class DashDatabase : RoomDatabase() { /** * v14 -> v15: additive nullable `wallets.dashPayBackfillFloor`, - * `wallets.dashPayBackfillRewoundFrom` and - * `wallets.dashPayBackfillCovered`, see the version-15 class doc - * above. NULL for every pre-existing row — no record on file, so the - * next rescan sweep behaves exactly as before and writes one. + * `wallets.dashPayBackfillRewoundFrom`, `wallets.dashPayBackfillCovered` + * and `dashpay_contact_requests.externalAccountReference`, see the + * version-15 class doc above. NULL for every pre-existing row — no + * record on file and no marker, so the next sweep rebuilds the + * outbound account once, rescans once, and writes both. */ val MIGRATION_14_15: Migration = object : Migration(14, 15) { override fun migrate(db: SupportSQLiteDatabase) { db.execSQL("ALTER TABLE `wallets` ADD COLUMN `dashPayBackfillFloor` INTEGER") db.execSQL("ALTER TABLE `wallets` ADD COLUMN `dashPayBackfillRewoundFrom` INTEGER") db.execSQL("ALTER TABLE `wallets` ADD COLUMN `dashPayBackfillCovered` BLOB") + db.execSQL( + "ALTER TABLE `dashpay_contact_requests` ADD COLUMN `externalAccountReference` INTEGER", + ) } } diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt index 8c087f53763..5c94e271efe 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt @@ -2147,6 +2147,8 @@ class PlatformWalletPersistenceHandler( isHidden: Boolean, contactAccountLabel: String?, acceptedAccounts: IntArray, + hasExternalAccountReference: Boolean, + externalAccountReference: Int, ): Int = guarded { stage(walletId) { db -> // Owner identity must exist; skip silently otherwise (replayed @@ -2172,6 +2174,8 @@ class PlatformWalletPersistenceHandler( contactHidden = isHidden, contactAccountLabel = contactAccountLabel, contactAcceptedAccounts = encodeAcceptedAccounts(acceptedAccounts), + externalAccountReference = + if (hasExternalAccountReference) externalAccountReference else null, lastUpdated = now(), ), ) @@ -2981,6 +2985,8 @@ class PlatformWalletPersistenceHandler( isHidden = c.contactHidden, contactAccountLabel = c.contactAccountLabel, acceptedAccounts = decodeAcceptedAccounts(c.contactAcceptedAccounts), + hasExternalAccountReference = c.externalAccountReference != null, + externalAccountReference = c.externalAccountReference ?: 0, ) }.toTypedArray() // Ignored senders (per-sender mute) — restores the Rust diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/DashpayContactRequestEntity.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/DashpayContactRequestEntity.kt index aae74e5a845..61254e1c234 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/DashpayContactRequestEntity.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/DashpayContactRequestEntity.kt @@ -81,6 +81,16 @@ data class DashpayContactRequestEntity( * `contactAcceptedAccounts` (`[UInt32]`). */ val contactAcceptedAccounts: ByteArray? = null, + /** + * `EstablishedContact::external_account_reference` — the incoming + * `accountReference` the registered outbound (sending) account was + * built from; null when no account is known to be built for the + * current reference. Relationship-level (both established rows), null + * on pending rows. Restoring it is what stops native from tearing the + * outbound account down and rebuilding it on every cold start + * (dashpay/platform#4302). + */ + val externalAccountReference: Int? = null, val createdAt: Date = Date(), val lastUpdated: Date = Date(), ) diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt index 8a114035a43..41326e84f92 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt @@ -324,6 +324,21 @@ class DashDatabaseTest { assertNull(legacy.dashPayBackfillFloor) assertNull(legacy.dashPayBackfillRewoundFrom) assertNull(legacy.dashPayBackfillCovered) + + // Same version: the nullable outbound-account marker on contact rows. + var markerFound = false + db.openHelper.readableDatabase.query("PRAGMA table_info('dashpay_contact_requests')").use { c -> + val name = c.getColumnIndexOrThrow("name") + val type = c.getColumnIndexOrThrow("type") + val notNull = c.getColumnIndexOrThrow("notnull") + while (c.moveToNext()) { + if (c.getString(name) != "externalAccountReference") continue + markerFound = true + assertEquals("INTEGER", c.getString(type)) + assertEquals(0, c.getInt(notNull)) + } + } + assertTrue(markerFound) } @Test diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt index 85b3748f709..44281b16fca 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt @@ -4833,6 +4833,61 @@ class PlatformWalletPersistenceHandlerTest { assertTrue(mutedId.contentEquals(identity.ignoredSenders[0])) } + /** + * dashpay/platform#4302: the outbound-account marker + * (`EstablishedContact::external_account_reference`) must round-trip, + * or every cold start rebuilds the outbound account. `false` stores + * NULL and restores as absent; `true` stores the value and restores it. + */ + @Test + fun contactUpsertRoundTripsTheExternalAccountReference() = runTest { + handler.onPersistWalletMetadata(walletId, testnet, groupId, 0) + val xpub = ByteArray(78) { 30 } + handler.onPersistAccountRegistration( + walletId, 0, 0, 0, 0, 0, ByteArray(0), ByteArray(0), xpub, + ) + val ownerId = ByteArray(32) { 23 } + val contactId = ByteArray(32) { 24 } + seedIdentity(ownerId) + + // Absent marker (the default trailing arguments). + persistIncomingContact(ownerId, contactId) + val before = handler.onLoadWalletList().single().identities.single().contacts.single() + assertFalse(before.hasExternalAccountReference) + assertEquals(0, before.externalAccountReference) + assertNull(db.dashpayDao().getContactRequestsByOwner(ownerId).single().externalAccountReference) + + // Stamped marker. + handler.onChangesetBegin(walletId) + handler.onPersistContactUpsert( + walletId = walletId, + ownerId = ownerId, + contactId = contactId, + isOutgoing = false, + senderKeyIndex = 2, + recipientKeyIndex = 3, + accountReference = 4, + encryptedPublicKey = ByteArray(96) { 5 }, + encryptedAccountLabel = null, + autoAcceptProof = null, + coreHeightCreatedAt = 100_000, + createdAt = 1_700_000_000_000, + paymentChannelBroken = false, + alias = null, + note = null, + isHidden = false, + contactAccountLabel = null, + acceptedAccounts = IntArray(0), + hasExternalAccountReference = true, + externalAccountReference = 4, + ) + handler.onChangesetEnd(walletId, success = true) + val after = handler.onLoadWalletList().single().identities.single().contacts.single() + assertTrue(after.hasExternalAccountReference) + assertEquals(4, after.externalAccountReference) + assertEquals(4, db.dashpayDao().getContactRequestsByOwner(ownerId).single().externalAccountReference) + } + @Test fun loadWalletListScopesToTheHandlerNetwork() = runTest { // A network-scoped handler must never hand the Rust loader a From 74898f585ddc4f52470bffbdd0fa6cc22303c442 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Sat, 26 Sep 2026 08:20:51 -0700 Subject: [PATCH 07/43] test(platform-wallet): model late-discovered contacts rewinding once and a mid-climb kill resuming The int24 device pass restored with Platform reachable, so every contact account was registered before the scan started and the rewind branch never ran. Pin the other ordering: contacts discovered after the scan passed their request heights make exactly one receival registration lower the cursor, on a round that carries the record with the real (floor, rewound_from) pair; the rest are forward-covered by that rewind. A cold start mid-climb finds nothing to rebuild, re-registers idempotently, does not rewind, and resumes from the persisted cursor with the record still pending (dashpay/platform#4302). Co-Authored-By: Claude Fable 5.1 --- .../15.json | 4202 +++++++++++++++++ .../src/wallet/identity/network/payments.rs | 194 + 2 files changed, 4396 insertions(+) create mode 100644 packages/kotlin-sdk/sdk/schemas/org.dashfoundation.dashsdk.persistence.DashDatabase/15.json diff --git a/packages/kotlin-sdk/sdk/schemas/org.dashfoundation.dashsdk.persistence.DashDatabase/15.json b/packages/kotlin-sdk/sdk/schemas/org.dashfoundation.dashsdk.persistence.DashDatabase/15.json new file mode 100644 index 00000000000..ed27781b3eb --- /dev/null +++ b/packages/kotlin-sdk/sdk/schemas/org.dashfoundation.dashsdk.persistence.DashDatabase/15.json @@ -0,0 +1,4202 @@ +{ + "formatVersion": 1, + "database": { + "version": 15, + "identityHash": "4cdbf8acfcfa07b5745b6aacd7f9d5bb", + "entities": [ + { + "tableName": "wallets", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `walletGroupId` BLOB NOT NULL, `networkRaw` INTEGER, `name` TEXT, `walletDescription` TEXT, `birthHeight` INTEGER NOT NULL, `syncedHeight` INTEGER NOT NULL, `lastSynced` INTEGER NOT NULL, `lastAppliedChainLockBytes` BLOB, `lastAppliedChainLockHeight` INTEGER, `dashPayBackfillFloor` INTEGER, `dashPayBackfillRewoundFrom` INTEGER, `dashPayBackfillCovered` BLOB, `isImported` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "walletGroupId", + "columnName": "walletGroupId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER" + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT" + }, + { + "fieldPath": "walletDescription", + "columnName": "walletDescription", + "affinity": "TEXT" + }, + { + "fieldPath": "birthHeight", + "columnName": "birthHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "syncedHeight", + "columnName": "syncedHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSynced", + "columnName": "lastSynced", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastAppliedChainLockBytes", + "columnName": "lastAppliedChainLockBytes", + "affinity": "BLOB" + }, + { + "fieldPath": "lastAppliedChainLockHeight", + "columnName": "lastAppliedChainLockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "dashPayBackfillFloor", + "columnName": "dashPayBackfillFloor", + "affinity": "INTEGER" + }, + { + "fieldPath": "dashPayBackfillRewoundFrom", + "columnName": "dashPayBackfillRewoundFrom", + "affinity": "INTEGER" + }, + { + "fieldPath": "dashPayBackfillCovered", + "columnName": "dashPayBackfillCovered", + "affinity": "BLOB" + }, + { + "fieldPath": "isImported", + "columnName": "isImported", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId" + ] + }, + "indices": [ + { + "name": "index_wallets_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_wallets_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + }, + { + "name": "index_wallets_walletGroupId", + "unique": false, + "columnNames": [ + "walletGroupId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_wallets_walletGroupId` ON `${TABLE_NAME}` (`walletGroupId`)" + } + ] + }, + { + "tableName": "accounts", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `walletId` BLOB NOT NULL, `accountType` INTEGER NOT NULL, `accountIndex` INTEGER NOT NULL, `accountTypeName` TEXT NOT NULL, `balanceConfirmed` INTEGER NOT NULL, `balanceUnconfirmed` INTEGER NOT NULL, `externalHighestUsed` INTEGER NOT NULL, `internalHighestUsed` INTEGER NOT NULL, `standardTag` INTEGER NOT NULL, `registrationIndex` INTEGER NOT NULL, `keyClass` INTEGER NOT NULL, `userIdentityId` BLOB NOT NULL, `friendIdentityId` BLOB NOT NULL, `accountExtendedPubKeyBytes` BLOB, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, FOREIGN KEY(`walletId`) REFERENCES `wallets`(`walletId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountType", + "columnName": "accountType", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountTypeName", + "columnName": "accountTypeName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "balanceConfirmed", + "columnName": "balanceConfirmed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "balanceUnconfirmed", + "columnName": "balanceUnconfirmed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "externalHighestUsed", + "columnName": "externalHighestUsed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "internalHighestUsed", + "columnName": "internalHighestUsed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "standardTag", + "columnName": "standardTag", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "registrationIndex", + "columnName": "registrationIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keyClass", + "columnName": "keyClass", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "userIdentityId", + "columnName": "userIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "friendIdentityId", + "columnName": "friendIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountExtendedPubKeyBytes", + "columnName": "accountExtendedPubKeyBytes", + "affinity": "BLOB" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_accounts_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_accounts_walletId` ON `${TABLE_NAME}` (`walletId`)" + }, + { + "name": "index_accounts_walletId_accountType_accountIndex_standardTag_registrationIndex_keyClass_userIdentityId_friendIdentityId", + "unique": true, + "columnNames": [ + "walletId", + "accountType", + "accountIndex", + "standardTag", + "registrationIndex", + "keyClass", + "userIdentityId", + "friendIdentityId" + ], + "orders": [], + "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_accounts_walletId_accountType_accountIndex_standardTag_registrationIndex_keyClass_userIdentityId_friendIdentityId` ON `${TABLE_NAME}` (`walletId`, `accountType`, `accountIndex`, `standardTag`, `registrationIndex`, `keyClass`, `userIdentityId`, `friendIdentityId`)" + }, + { + "name": "index_accounts_accountExtendedPubKeyBytes", + "unique": true, + "columnNames": [ + "accountExtendedPubKeyBytes" + ], + "orders": [], + "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_accounts_accountExtendedPubKeyBytes` ON `${TABLE_NAME}` (`accountExtendedPubKeyBytes`)" + } + ], + "foreignKeys": [ + { + "table": "wallets", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "walletId" + ], + "referencedColumns": [ + "walletId" + ] + } + ] + }, + { + "tableName": "transactions", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`txid` BLOB NOT NULL, `transactionData` BLOB NOT NULL, `context` INTEGER NOT NULL, `blockHeight` INTEGER NOT NULL, `blockHash` BLOB, `blockTimestamp` INTEGER NOT NULL, `blockPosition` INTEGER NOT NULL, `hasBlockPosition` INTEGER NOT NULL, `direction` INTEGER NOT NULL, `transactionType` TEXT NOT NULL, `transactionTypeKind` INTEGER NOT NULL, `netAmount` INTEGER NOT NULL, `fee` INTEGER, `label` TEXT NOT NULL, `firstSeen` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`txid`))", + "fields": [ + { + "fieldPath": "txid", + "columnName": "txid", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "transactionData", + "columnName": "transactionData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "context", + "columnName": "context", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "blockHeight", + "columnName": "blockHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "blockHash", + "columnName": "blockHash", + "affinity": "BLOB" + }, + { + "fieldPath": "blockTimestamp", + "columnName": "blockTimestamp", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "blockPosition", + "columnName": "blockPosition", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasBlockPosition", + "columnName": "hasBlockPosition", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "direction", + "columnName": "direction", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "transactionType", + "columnName": "transactionType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "transactionTypeKind", + "columnName": "transactionTypeKind", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "netAmount", + "columnName": "netAmount", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "fee", + "columnName": "fee", + "affinity": "INTEGER" + }, + { + "fieldPath": "label", + "columnName": "label", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "firstSeen", + "columnName": "firstSeen", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "txid" + ] + }, + "indices": [ + { + "name": "index_transactions_firstSeen", + "unique": false, + "columnNames": [ + "firstSeen" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_transactions_firstSeen` ON `${TABLE_NAME}` (`firstSeen`)" + } + ] + }, + { + "tableName": "transaction_account_involvements", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`transactionTxid` BLOB NOT NULL, `accountId` INTEGER NOT NULL, PRIMARY KEY(`transactionTxid`, `accountId`), FOREIGN KEY(`transactionTxid`) REFERENCES `transactions`(`txid`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`accountId`) REFERENCES `accounts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "transactionTxid", + "columnName": "transactionTxid", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "transactionTxid", + "accountId" + ] + }, + "indices": [ + { + "name": "index_transaction_account_involvements_accountId", + "unique": false, + "columnNames": [ + "accountId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_transaction_account_involvements_accountId` ON `${TABLE_NAME}` (`accountId`)" + } + ], + "foreignKeys": [ + { + "table": "transactions", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "transactionTxid" + ], + "referencedColumns": [ + "txid" + ] + }, + { + "table": "accounts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "accountId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "txos", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`outpoint` BLOB NOT NULL, `vout` INTEGER NOT NULL, `amount` INTEGER NOT NULL, `address` TEXT NOT NULL, `scriptPubKey` BLOB NOT NULL, `height` INTEGER NOT NULL, `isCoinbase` INTEGER NOT NULL, `isConfirmed` INTEGER NOT NULL, `isInstantLocked` INTEGER NOT NULL, `isLocked` INTEGER NOT NULL, `isSpent` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `walletId` BLOB NOT NULL, `txid` BLOB, `spendingTxid` BLOB, `spendingInputIndex` INTEGER, `accountId` INTEGER, `coreAddressId` TEXT, `supersededByTxid` BLOB, PRIMARY KEY(`outpoint`), FOREIGN KEY(`txid`) REFERENCES `transactions`(`txid`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`spendingTxid`) REFERENCES `transactions`(`txid`) ON UPDATE NO ACTION ON DELETE SET NULL , FOREIGN KEY(`accountId`) REFERENCES `accounts`(`id`) ON UPDATE NO ACTION ON DELETE SET NULL , FOREIGN KEY(`coreAddressId`) REFERENCES `core_addresses`(`address`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "outpoint", + "columnName": "outpoint", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "vout", + "columnName": "vout", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "amount", + "columnName": "amount", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "address", + "columnName": "address", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "scriptPubKey", + "columnName": "scriptPubKey", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "height", + "columnName": "height", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isCoinbase", + "columnName": "isCoinbase", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isConfirmed", + "columnName": "isConfirmed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isInstantLocked", + "columnName": "isInstantLocked", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isLocked", + "columnName": "isLocked", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isSpent", + "columnName": "isSpent", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "txid", + "columnName": "txid", + "affinity": "BLOB" + }, + { + "fieldPath": "spendingTxid", + "columnName": "spendingTxid", + "affinity": "BLOB" + }, + { + "fieldPath": "spendingInputIndex", + "columnName": "spendingInputIndex", + "affinity": "INTEGER" + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER" + }, + { + "fieldPath": "coreAddressId", + "columnName": "coreAddressId", + "affinity": "TEXT" + }, + { + "fieldPath": "supersededByTxid", + "columnName": "supersededByTxid", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "outpoint" + ] + }, + "indices": [ + { + "name": "index_txos_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_txos_walletId` ON `${TABLE_NAME}` (`walletId`)" + }, + { + "name": "index_txos_txid", + "unique": false, + "columnNames": [ + "txid" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_txos_txid` ON `${TABLE_NAME}` (`txid`)" + }, + { + "name": "index_txos_spendingTxid", + "unique": false, + "columnNames": [ + "spendingTxid" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_txos_spendingTxid` ON `${TABLE_NAME}` (`spendingTxid`)" + }, + { + "name": "index_txos_accountId", + "unique": false, + "columnNames": [ + "accountId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_txos_accountId` ON `${TABLE_NAME}` (`accountId`)" + }, + { + "name": "index_txos_coreAddressId", + "unique": false, + "columnNames": [ + "coreAddressId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_txos_coreAddressId` ON `${TABLE_NAME}` (`coreAddressId`)" + } + ], + "foreignKeys": [ + { + "table": "transactions", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "txid" + ], + "referencedColumns": [ + "txid" + ] + }, + { + "table": "transactions", + "onDelete": "SET NULL", + "onUpdate": "NO ACTION", + "columns": [ + "spendingTxid" + ], + "referencedColumns": [ + "txid" + ] + }, + { + "table": "accounts", + "onDelete": "SET NULL", + "onUpdate": "NO ACTION", + "columns": [ + "accountId" + ], + "referencedColumns": [ + "id" + ] + }, + { + "table": "core_addresses", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "coreAddressId" + ], + "referencedColumns": [ + "address" + ] + } + ] + }, + { + "tableName": "core_addresses", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`address` TEXT NOT NULL, `publicKey` BLOB NOT NULL, `poolTypeTag` INTEGER NOT NULL, `addressIndex` INTEGER NOT NULL, `derivationPath` TEXT NOT NULL, `isUsed` INTEGER NOT NULL, `firstSeenHeight` INTEGER NOT NULL, `lastSeenHeight` INTEGER NOT NULL, `balance` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `accountId` INTEGER, PRIMARY KEY(`address`), FOREIGN KEY(`accountId`) REFERENCES `accounts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "address", + "columnName": "address", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "publicKey", + "columnName": "publicKey", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "poolTypeTag", + "columnName": "poolTypeTag", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "addressIndex", + "columnName": "addressIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "derivationPath", + "columnName": "derivationPath", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "isUsed", + "columnName": "isUsed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "firstSeenHeight", + "columnName": "firstSeenHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSeenHeight", + "columnName": "lastSeenHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "balance", + "columnName": "balance", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "address" + ] + }, + "indices": [ + { + "name": "index_core_addresses_accountId", + "unique": false, + "columnNames": [ + "accountId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_core_addresses_accountId` ON `${TABLE_NAME}` (`accountId`)" + } + ], + "foreignKeys": [ + { + "table": "accounts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "accountId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "asset_locks", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`outPointHex` TEXT NOT NULL, `walletId` BLOB NOT NULL, `transactionBytes` BLOB NOT NULL, `fundingTypeRaw` INTEGER NOT NULL, `identityIndexRaw` INTEGER NOT NULL, `accountIndexRaw` INTEGER NOT NULL, `amountDuffs` INTEGER NOT NULL, `statusRaw` INTEGER NOT NULL, `proofBytes` BLOB, `recipientPlatformAddressHash` BLOB, `recipientPlatformAddressType` INTEGER, `createdAt` INTEGER NOT NULL, `updatedAt` INTEGER NOT NULL, PRIMARY KEY(`outPointHex`))", + "fields": [ + { + "fieldPath": "outPointHex", + "columnName": "outPointHex", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "transactionBytes", + "columnName": "transactionBytes", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "fundingTypeRaw", + "columnName": "fundingTypeRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "identityIndexRaw", + "columnName": "identityIndexRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountIndexRaw", + "columnName": "accountIndexRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "amountDuffs", + "columnName": "amountDuffs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "statusRaw", + "columnName": "statusRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "proofBytes", + "columnName": "proofBytes", + "affinity": "BLOB" + }, + { + "fieldPath": "recipientPlatformAddressHash", + "columnName": "recipientPlatformAddressHash", + "affinity": "BLOB" + }, + { + "fieldPath": "recipientPlatformAddressType", + "columnName": "recipientPlatformAddressType", + "affinity": "INTEGER" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "updatedAt", + "columnName": "updatedAt", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "outPointHex" + ] + }, + "indices": [ + { + "name": "index_asset_locks_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_asset_locks_walletId` ON `${TABLE_NAME}` (`walletId`)" + } + ] + }, + { + "tableName": "invitations", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`outPointHex` TEXT NOT NULL, `rawOutPoint` BLOB NOT NULL, `walletId` BLOB NOT NULL, `fundingIndexRaw` INTEGER NOT NULL, `amountDuffs` INTEGER NOT NULL, `expiryUnix` INTEGER NOT NULL, `createdAtSecs` INTEGER NOT NULL, `hasInviter` INTEGER NOT NULL, `statusRaw` INTEGER NOT NULL, `reclaimInFlight` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `updatedAt` INTEGER NOT NULL, PRIMARY KEY(`outPointHex`))", + "fields": [ + { + "fieldPath": "outPointHex", + "columnName": "outPointHex", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "rawOutPoint", + "columnName": "rawOutPoint", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "fundingIndexRaw", + "columnName": "fundingIndexRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "amountDuffs", + "columnName": "amountDuffs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "expiryUnix", + "columnName": "expiryUnix", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAtSecs", + "columnName": "createdAtSecs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasInviter", + "columnName": "hasInviter", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "statusRaw", + "columnName": "statusRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "reclaimInFlight", + "columnName": "reclaimInFlight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "updatedAt", + "columnName": "updatedAt", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "outPointHex" + ] + }, + "indices": [ + { + "name": "index_invitations_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_invitations_walletId` ON `${TABLE_NAME}` (`walletId`)" + } + ] + }, + { + "tableName": "identities", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`identityId` BLOB NOT NULL, `balance` INTEGER NOT NULL, `revision` INTEGER NOT NULL, `isLocal` INTEGER NOT NULL, `alias` TEXT, `dpnsName` TEXT, `mainDpnsName` TEXT, `identityType` TEXT NOT NULL, `votingPrivateKeyIdentifier` TEXT, `ownerPrivateKeyIdentifier` TEXT, `payoutPrivateKeyIdentifier` TEXT, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `lastSyncedAt` INTEGER, `networkRaw` INTEGER NOT NULL, `walletId` BLOB, `identityIndex` INTEGER NOT NULL, PRIMARY KEY(`identityId`), FOREIGN KEY(`walletId`) REFERENCES `wallets`(`walletId`) ON UPDATE NO ACTION ON DELETE SET NULL )", + "fields": [ + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "balance", + "columnName": "balance", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "revision", + "columnName": "revision", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isLocal", + "columnName": "isLocal", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "alias", + "columnName": "alias", + "affinity": "TEXT" + }, + { + "fieldPath": "dpnsName", + "columnName": "dpnsName", + "affinity": "TEXT" + }, + { + "fieldPath": "mainDpnsName", + "columnName": "mainDpnsName", + "affinity": "TEXT" + }, + { + "fieldPath": "identityType", + "columnName": "identityType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "votingPrivateKeyIdentifier", + "columnName": "votingPrivateKeyIdentifier", + "affinity": "TEXT" + }, + { + "fieldPath": "ownerPrivateKeyIdentifier", + "columnName": "ownerPrivateKeyIdentifier", + "affinity": "TEXT" + }, + { + "fieldPath": "payoutPrivateKeyIdentifier", + "columnName": "payoutPrivateKeyIdentifier", + "affinity": "TEXT" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSyncedAt", + "columnName": "lastSyncedAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB" + }, + { + "fieldPath": "identityIndex", + "columnName": "identityIndex", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "identityId" + ] + }, + "indices": [ + { + "name": "index_identities_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_identities_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + }, + { + "name": "index_identities_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_identities_walletId` ON `${TABLE_NAME}` (`walletId`)" + } + ], + "foreignKeys": [ + { + "table": "wallets", + "onDelete": "SET NULL", + "onUpdate": "NO ACTION", + "columns": [ + "walletId" + ], + "referencedColumns": [ + "walletId" + ] + } + ] + }, + { + "tableName": "public_keys", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `keyId` INTEGER NOT NULL, `purpose` TEXT NOT NULL, `securityLevel` TEXT NOT NULL, `keyType` TEXT NOT NULL, `readOnly` INTEGER NOT NULL, `disabledAt` INTEGER, `totalBudget` INTEGER, `expiresAt` INTEGER, `publicKeyData` BLOB NOT NULL, `contractBoundsData` BLOB, `contractBoundsDocumentTypeName` TEXT, `contractBoundsKind` INTEGER, `privateKeyKeychainIdentifier` TEXT, `derivationIdentityIndex` INTEGER, `derivationKeyIndex` INTEGER, `identityId` TEXT NOT NULL, `createdAt` INTEGER NOT NULL, `lastAccessed` INTEGER, `identityIdData` BLOB, FOREIGN KEY(`identityIdData`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keyId", + "columnName": "keyId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "purpose", + "columnName": "purpose", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "securityLevel", + "columnName": "securityLevel", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "keyType", + "columnName": "keyType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "readOnly", + "columnName": "readOnly", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "disabledAt", + "columnName": "disabledAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "totalBudget", + "columnName": "totalBudget", + "affinity": "INTEGER" + }, + { + "fieldPath": "expiresAt", + "columnName": "expiresAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "publicKeyData", + "columnName": "publicKeyData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractBoundsData", + "columnName": "contractBoundsData", + "affinity": "BLOB" + }, + { + "fieldPath": "contractBoundsDocumentTypeName", + "columnName": "contractBoundsDocumentTypeName", + "affinity": "TEXT" + }, + { + "fieldPath": "contractBoundsKind", + "columnName": "contractBoundsKind", + "affinity": "INTEGER" + }, + { + "fieldPath": "privateKeyKeychainIdentifier", + "columnName": "privateKeyKeychainIdentifier", + "affinity": "TEXT" + }, + { + "fieldPath": "derivationIdentityIndex", + "columnName": "derivationIdentityIndex", + "affinity": "INTEGER" + }, + { + "fieldPath": "derivationKeyIndex", + "columnName": "derivationKeyIndex", + "affinity": "INTEGER" + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastAccessed", + "columnName": "lastAccessed", + "affinity": "INTEGER" + }, + { + "fieldPath": "identityIdData", + "columnName": "identityIdData", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_public_keys_identityId_keyId", + "unique": false, + "columnNames": [ + "identityId", + "keyId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_public_keys_identityId_keyId` ON `${TABLE_NAME}` (`identityId`, `keyId`)" + }, + { + "name": "index_public_keys_identityIdData", + "unique": false, + "columnNames": [ + "identityIdData" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_public_keys_identityIdData` ON `${TABLE_NAME}` (`identityIdData`)" + }, + { + "name": "index_public_keys_publicKeyData", + "unique": false, + "columnNames": [ + "publicKeyData" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_public_keys_publicKeyData` ON `${TABLE_NAME}` (`publicKeyData`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "identityIdData" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dpns_names", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `label` TEXT NOT NULL, `normalizedLabel` TEXT NOT NULL, `parentDomainName` TEXT NOT NULL, `normalizedParentDomainName` TEXT NOT NULL, `acquiredAt` INTEGER NOT NULL, `identityId` BLOB NOT NULL, `documentId` BLOB, `isOwned` INTEGER NOT NULL, `priceCredits` INTEGER, `saleStatusRaw` INTEGER NOT NULL, `counterpartyIdentityId` BLOB, `documentCreatedAtMs` INTEGER NOT NULL, `documentUpdatedAtMs` INTEGER NOT NULL, `documentTransferredAtMs` INTEGER NOT NULL, `marketplaceUpdatedAt` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `normalizedParentDomainName`, `normalizedLabel`), FOREIGN KEY(`identityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "label", + "columnName": "label", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "normalizedLabel", + "columnName": "normalizedLabel", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "parentDomainName", + "columnName": "parentDomainName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "normalizedParentDomainName", + "columnName": "normalizedParentDomainName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "acquiredAt", + "columnName": "acquiredAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "documentId", + "columnName": "documentId", + "affinity": "BLOB" + }, + { + "fieldPath": "isOwned", + "columnName": "isOwned", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "priceCredits", + "columnName": "priceCredits", + "affinity": "INTEGER" + }, + { + "fieldPath": "saleStatusRaw", + "columnName": "saleStatusRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "counterpartyIdentityId", + "columnName": "counterpartyIdentityId", + "affinity": "BLOB" + }, + { + "fieldPath": "documentCreatedAtMs", + "columnName": "documentCreatedAtMs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentUpdatedAtMs", + "columnName": "documentUpdatedAtMs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentTransferredAtMs", + "columnName": "documentTransferredAtMs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "marketplaceUpdatedAt", + "columnName": "marketplaceUpdatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "normalizedParentDomainName", + "normalizedLabel" + ] + }, + "indices": [ + { + "name": "index_dpns_names_identityId", + "unique": false, + "columnNames": [ + "identityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dpns_names_identityId` ON `${TABLE_NAME}` (`identityId`)" + }, + { + "name": "index_dpns_names_documentId", + "unique": false, + "columnNames": [ + "documentId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dpns_names_documentId` ON `${TABLE_NAME}` (`documentId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "identityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dashpay_profiles", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `identityId` BLOB NOT NULL, `displayName` TEXT, `publicMessage` TEXT, `bio` TEXT, `avatarUrl` TEXT, `avatarHash` BLOB, `avatarFingerprint` BLOB, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `identityId`), FOREIGN KEY(`identityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "displayName", + "columnName": "displayName", + "affinity": "TEXT" + }, + { + "fieldPath": "publicMessage", + "columnName": "publicMessage", + "affinity": "TEXT" + }, + { + "fieldPath": "bio", + "columnName": "bio", + "affinity": "TEXT" + }, + { + "fieldPath": "avatarUrl", + "columnName": "avatarUrl", + "affinity": "TEXT" + }, + { + "fieldPath": "avatarHash", + "columnName": "avatarHash", + "affinity": "BLOB" + }, + { + "fieldPath": "avatarFingerprint", + "columnName": "avatarFingerprint", + "affinity": "BLOB" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "identityId" + ] + }, + "indices": [ + { + "name": "index_dashpay_profiles_identityId", + "unique": false, + "columnNames": [ + "identityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dashpay_profiles_identityId` ON `${TABLE_NAME}` (`identityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "identityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dashpay_contact_requests", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `ownerIdentityId` BLOB NOT NULL, `contactIdentityId` BLOB NOT NULL, `isOutgoing` INTEGER NOT NULL, `senderKeyIndex` INTEGER NOT NULL, `recipientKeyIndex` INTEGER NOT NULL, `accountReference` INTEGER NOT NULL, `encryptedPublicKey` BLOB NOT NULL, `encryptedAccountLabel` BLOB, `autoAcceptProof` BLOB, `coreHeightCreatedAt` INTEGER NOT NULL, `createdAtMillis` INTEGER NOT NULL, `paymentChannelBroken` INTEGER NOT NULL DEFAULT 0, `contactAlias` TEXT, `contactNote` TEXT, `contactHidden` INTEGER NOT NULL DEFAULT 0, `contactAccountLabel` TEXT, `contactAcceptedAccounts` BLOB, `externalAccountReference` INTEGER, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `ownerIdentityId`, `contactIdentityId`, `isOutgoing`), FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contactIdentityId", + "columnName": "contactIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "isOutgoing", + "columnName": "isOutgoing", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "senderKeyIndex", + "columnName": "senderKeyIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "recipientKeyIndex", + "columnName": "recipientKeyIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountReference", + "columnName": "accountReference", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "encryptedPublicKey", + "columnName": "encryptedPublicKey", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "encryptedAccountLabel", + "columnName": "encryptedAccountLabel", + "affinity": "BLOB" + }, + { + "fieldPath": "autoAcceptProof", + "columnName": "autoAcceptProof", + "affinity": "BLOB" + }, + { + "fieldPath": "coreHeightCreatedAt", + "columnName": "coreHeightCreatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAtMillis", + "columnName": "createdAtMillis", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "paymentChannelBroken", + "columnName": "paymentChannelBroken", + "affinity": "INTEGER", + "notNull": true, + "defaultValue": "0" + }, + { + "fieldPath": "contactAlias", + "columnName": "contactAlias", + "affinity": "TEXT" + }, + { + "fieldPath": "contactNote", + "columnName": "contactNote", + "affinity": "TEXT" + }, + { + "fieldPath": "contactHidden", + "columnName": "contactHidden", + "affinity": "INTEGER", + "notNull": true, + "defaultValue": "0" + }, + { + "fieldPath": "contactAccountLabel", + "columnName": "contactAccountLabel", + "affinity": "TEXT" + }, + { + "fieldPath": "contactAcceptedAccounts", + "columnName": "contactAcceptedAccounts", + "affinity": "BLOB" + }, + { + "fieldPath": "externalAccountReference", + "columnName": "externalAccountReference", + "affinity": "INTEGER" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "ownerIdentityId", + "contactIdentityId", + "isOutgoing" + ] + }, + "indices": [ + { + "name": "index_dashpay_contact_requests_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dashpay_contact_requests_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dashpay_ignored_senders", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `ownerIdentityId` BLOB NOT NULL, `ignoredSenderId` BLOB NOT NULL, `ignoredAt` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `ownerIdentityId`, `ignoredSenderId`), FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "ignoredSenderId", + "columnName": "ignoredSenderId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "ignoredAt", + "columnName": "ignoredAt", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "ownerIdentityId", + "ignoredSenderId" + ] + }, + "indices": [ + { + "name": "index_dashpay_ignored_senders_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dashpay_ignored_senders_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dashpay_contact_profiles", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `ownerIdentityId` BLOB NOT NULL, `contactIdentityId` BLOB NOT NULL, `displayName` TEXT, `publicMessage` TEXT, `bio` TEXT, `avatarUrl` TEXT, `avatarHash` BLOB, `avatarFingerprint` BLOB, `checkedAtMs` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `ownerIdentityId`, `contactIdentityId`), FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contactIdentityId", + "columnName": "contactIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "displayName", + "columnName": "displayName", + "affinity": "TEXT" + }, + { + "fieldPath": "publicMessage", + "columnName": "publicMessage", + "affinity": "TEXT" + }, + { + "fieldPath": "bio", + "columnName": "bio", + "affinity": "TEXT" + }, + { + "fieldPath": "avatarUrl", + "columnName": "avatarUrl", + "affinity": "TEXT" + }, + { + "fieldPath": "avatarHash", + "columnName": "avatarHash", + "affinity": "BLOB" + }, + { + "fieldPath": "avatarFingerprint", + "columnName": "avatarFingerprint", + "affinity": "BLOB" + }, + { + "fieldPath": "checkedAtMs", + "columnName": "checkedAtMs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "ownerIdentityId", + "contactIdentityId" + ] + }, + "indices": [ + { + "name": "index_dashpay_contact_profiles_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dashpay_contact_profiles_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "dashpay_payments", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `ownerIdentityId` BLOB NOT NULL, `counterpartyIdentityId` BLOB NOT NULL, `amountDuffs` INTEGER NOT NULL, `directionRaw` INTEGER NOT NULL, `statusRaw` INTEGER NOT NULL, `txid` TEXT NOT NULL, `memo` TEXT, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`, `ownerIdentityId`, `txid`), FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "counterpartyIdentityId", + "columnName": "counterpartyIdentityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "amountDuffs", + "columnName": "amountDuffs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "directionRaw", + "columnName": "directionRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "statusRaw", + "columnName": "statusRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "txid", + "columnName": "txid", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "memo", + "columnName": "memo", + "affinity": "TEXT" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw", + "ownerIdentityId", + "txid" + ] + }, + "indices": [ + { + "name": "index_dashpay_payments_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_dashpay_payments_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "data_contracts", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` BLOB NOT NULL, `name` TEXT NOT NULL, `serializedContract` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `lastAccessedAt` INTEGER NOT NULL, `binarySerialization` BLOB, `version` INTEGER, `ownerId` BLOB, `contractDescription` TEXT, `schemaData` BLOB NOT NULL, `documentTypesData` BLOB NOT NULL, `groupsData` BLOB, `networkRaw` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `lastSyncedAt` INTEGER, `canBeDeleted` INTEGER NOT NULL, `readonly` INTEGER NOT NULL, `keepsHistory` INTEGER NOT NULL, `schemaDefs` INTEGER, `documentsKeepHistoryContractDefault` INTEGER NOT NULL, `documentsMutableContractDefault` INTEGER NOT NULL, `documentsCanBeDeletedContractDefault` INTEGER NOT NULL, `hasTokens` INTEGER NOT NULL, `tokensData` BLOB, `ownerIdentityId` BLOB, PRIMARY KEY(`id`), FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE SET NULL )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "serializedContract", + "columnName": "serializedContract", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastAccessedAt", + "columnName": "lastAccessedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "binarySerialization", + "columnName": "binarySerialization", + "affinity": "BLOB" + }, + { + "fieldPath": "version", + "columnName": "version", + "affinity": "INTEGER" + }, + { + "fieldPath": "ownerId", + "columnName": "ownerId", + "affinity": "BLOB" + }, + { + "fieldPath": "contractDescription", + "columnName": "contractDescription", + "affinity": "TEXT" + }, + { + "fieldPath": "schemaData", + "columnName": "schemaData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "documentTypesData", + "columnName": "documentTypesData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "groupsData", + "columnName": "groupsData", + "affinity": "BLOB" + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSyncedAt", + "columnName": "lastSyncedAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "canBeDeleted", + "columnName": "canBeDeleted", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "readonly", + "columnName": "readonly", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsHistory", + "columnName": "keepsHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "schemaDefs", + "columnName": "schemaDefs", + "affinity": "INTEGER" + }, + { + "fieldPath": "documentsKeepHistoryContractDefault", + "columnName": "documentsKeepHistoryContractDefault", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentsMutableContractDefault", + "columnName": "documentsMutableContractDefault", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentsCanBeDeletedContractDefault", + "columnName": "documentsCanBeDeletedContractDefault", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasTokens", + "columnName": "hasTokens", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "tokensData", + "columnName": "tokensData", + "affinity": "BLOB" + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_data_contracts_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_data_contracts_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + }, + { + "name": "index_data_contracts_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_data_contracts_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "SET NULL", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "document_types", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` BLOB NOT NULL, `contractId` BLOB NOT NULL, `name` TEXT NOT NULL, `schemaJSON` BLOB NOT NULL, `propertiesJSON` BLOB NOT NULL, `documentsKeepHistory` INTEGER NOT NULL, `documentsMutable` INTEGER NOT NULL, `documentsCanBeDeleted` INTEGER NOT NULL, `documentsTransferable` INTEGER NOT NULL, `requiredFieldsJSON` BLOB, `securityLevel` INTEGER NOT NULL, `tradeMode` INTEGER NOT NULL, `creationRestrictionMode` INTEGER NOT NULL, `requiresIdentityEncryptionBoundedKey` INTEGER NOT NULL, `requiresIdentityDecryptionBoundedKey` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastAccessedAt` INTEGER NOT NULL, PRIMARY KEY(`id`), FOREIGN KEY(`contractId`) REFERENCES `data_contracts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "schemaJSON", + "columnName": "schemaJSON", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "propertiesJSON", + "columnName": "propertiesJSON", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "documentsKeepHistory", + "columnName": "documentsKeepHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentsMutable", + "columnName": "documentsMutable", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentsCanBeDeleted", + "columnName": "documentsCanBeDeleted", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentsTransferable", + "columnName": "documentsTransferable", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "requiredFieldsJSON", + "columnName": "requiredFieldsJSON", + "affinity": "BLOB" + }, + { + "fieldPath": "securityLevel", + "columnName": "securityLevel", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "tradeMode", + "columnName": "tradeMode", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "creationRestrictionMode", + "columnName": "creationRestrictionMode", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "requiresIdentityEncryptionBoundedKey", + "columnName": "requiresIdentityEncryptionBoundedKey", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "requiresIdentityDecryptionBoundedKey", + "columnName": "requiresIdentityDecryptionBoundedKey", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastAccessedAt", + "columnName": "lastAccessedAt", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_document_types_contractId", + "unique": false, + "columnNames": [ + "contractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_document_types_contractId` ON `${TABLE_NAME}` (`contractId`)" + } + ], + "foreignKeys": [ + { + "table": "data_contracts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "contractId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "documents", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`documentId` TEXT NOT NULL, `documentType` TEXT NOT NULL, `revision` INTEGER NOT NULL, `data` BLOB NOT NULL, `contractId` TEXT NOT NULL, `ownerId` TEXT NOT NULL, `contractIdData` BLOB NOT NULL, `ownerIdData` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `updatedAt` INTEGER NOT NULL, `transferredAt` INTEGER, `createdAtBlockHeight` INTEGER, `updatedAtBlockHeight` INTEGER, `transferredAtBlockHeight` INTEGER, `createdAtCoreBlockHeight` INTEGER, `updatedAtCoreBlockHeight` INTEGER, `transferredAtCoreBlockHeight` INTEGER, `networkRaw` INTEGER NOT NULL, `isDeleted` INTEGER NOT NULL, `localCreatedAt` INTEGER NOT NULL, `localUpdatedAt` INTEGER NOT NULL, `documentTypeRelationId` BLOB, `dataContractId` BLOB, `ownerIdentityId` BLOB, PRIMARY KEY(`documentId`), FOREIGN KEY(`documentTypeRelationId`) REFERENCES `document_types`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`dataContractId`) REFERENCES `data_contracts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE , FOREIGN KEY(`ownerIdentityId`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "documentId", + "columnName": "documentId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "documentType", + "columnName": "documentType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "revision", + "columnName": "revision", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "data", + "columnName": "data", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "ownerId", + "columnName": "ownerId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "contractIdData", + "columnName": "contractIdData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "ownerIdData", + "columnName": "ownerIdData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "updatedAt", + "columnName": "updatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "transferredAt", + "columnName": "transferredAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "createdAtBlockHeight", + "columnName": "createdAtBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "updatedAtBlockHeight", + "columnName": "updatedAtBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "transferredAtBlockHeight", + "columnName": "transferredAtBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "createdAtCoreBlockHeight", + "columnName": "createdAtCoreBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "updatedAtCoreBlockHeight", + "columnName": "updatedAtCoreBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "transferredAtCoreBlockHeight", + "columnName": "transferredAtCoreBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isDeleted", + "columnName": "isDeleted", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "localCreatedAt", + "columnName": "localCreatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "localUpdatedAt", + "columnName": "localUpdatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentTypeRelationId", + "columnName": "documentTypeRelationId", + "affinity": "BLOB" + }, + { + "fieldPath": "dataContractId", + "columnName": "dataContractId", + "affinity": "BLOB" + }, + { + "fieldPath": "ownerIdentityId", + "columnName": "ownerIdentityId", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "documentId" + ] + }, + "indices": [ + { + "name": "index_documents_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + }, + { + "name": "index_documents_contractId", + "unique": false, + "columnNames": [ + "contractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_contractId` ON `${TABLE_NAME}` (`contractId`)" + }, + { + "name": "index_documents_ownerId", + "unique": false, + "columnNames": [ + "ownerId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_ownerId` ON `${TABLE_NAME}` (`ownerId`)" + }, + { + "name": "index_documents_documentTypeRelationId", + "unique": false, + "columnNames": [ + "documentTypeRelationId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_documentTypeRelationId` ON `${TABLE_NAME}` (`documentTypeRelationId`)" + }, + { + "name": "index_documents_dataContractId", + "unique": false, + "columnNames": [ + "dataContractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_dataContractId` ON `${TABLE_NAME}` (`dataContractId`)" + }, + { + "name": "index_documents_ownerIdentityId", + "unique": false, + "columnNames": [ + "ownerIdentityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_documents_ownerIdentityId` ON `${TABLE_NAME}` (`ownerIdentityId`)" + } + ], + "foreignKeys": [ + { + "table": "document_types", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "documentTypeRelationId" + ], + "referencedColumns": [ + "id" + ] + }, + { + "table": "data_contracts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "dataContractId" + ], + "referencedColumns": [ + "id" + ] + }, + { + "table": "identities", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "ownerIdentityId" + ], + "referencedColumns": [ + "identityId" + ] + } + ] + }, + { + "tableName": "indices", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` BLOB NOT NULL, `contractId` BLOB NOT NULL, `documentTypeName` TEXT NOT NULL, `name` TEXT NOT NULL, `unique` INTEGER NOT NULL, `nullSearchable` INTEGER NOT NULL, `contested` INTEGER NOT NULL, `propertiesJSON` BLOB NOT NULL, `contestedDetailsJSON` BLOB, `createdAt` INTEGER NOT NULL, `documentTypeId` BLOB, PRIMARY KEY(`id`), FOREIGN KEY(`documentTypeId`) REFERENCES `document_types`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "documentTypeName", + "columnName": "documentTypeName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "unique", + "columnName": "unique", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "nullSearchable", + "columnName": "nullSearchable", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "contested", + "columnName": "contested", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "propertiesJSON", + "columnName": "propertiesJSON", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contestedDetailsJSON", + "columnName": "contestedDetailsJSON", + "affinity": "BLOB" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentTypeId", + "columnName": "documentTypeId", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_indices_documentTypeId", + "unique": false, + "columnNames": [ + "documentTypeId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_indices_documentTypeId` ON `${TABLE_NAME}` (`documentTypeId`)" + } + ], + "foreignKeys": [ + { + "table": "document_types", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "documentTypeId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "keywords", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `keyword` TEXT NOT NULL, `contractId` TEXT NOT NULL, `dataContractId` BLOB, PRIMARY KEY(`id`), FOREIGN KEY(`dataContractId`) REFERENCES `data_contracts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "keyword", + "columnName": "keyword", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "dataContractId", + "columnName": "dataContractId", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_keywords_contractId", + "unique": false, + "columnNames": [ + "contractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_keywords_contractId` ON `${TABLE_NAME}` (`contractId`)" + }, + { + "name": "index_keywords_dataContractId", + "unique": false, + "columnNames": [ + "dataContractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_keywords_dataContractId` ON `${TABLE_NAME}` (`dataContractId`)" + } + ], + "foreignKeys": [ + { + "table": "data_contracts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "dataContractId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "properties", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` BLOB NOT NULL, `contractId` BLOB NOT NULL, `documentTypeName` TEXT NOT NULL, `name` TEXT NOT NULL, `type` TEXT NOT NULL, `format` TEXT, `contentMediaType` TEXT, `byteArray` INTEGER NOT NULL, `minItems` INTEGER, `maxItems` INTEGER, `pattern` TEXT, `minLength` INTEGER, `maxLength` INTEGER, `minValue` INTEGER, `maxValue` INTEGER, `fieldDescription` TEXT, `transient` INTEGER NOT NULL, `isRequired` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `documentTypeId` BLOB, PRIMARY KEY(`id`), FOREIGN KEY(`documentTypeId`) REFERENCES `document_types`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "documentTypeName", + "columnName": "documentTypeName", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "type", + "columnName": "type", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "format", + "columnName": "format", + "affinity": "TEXT" + }, + { + "fieldPath": "contentMediaType", + "columnName": "contentMediaType", + "affinity": "TEXT" + }, + { + "fieldPath": "byteArray", + "columnName": "byteArray", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "minItems", + "columnName": "minItems", + "affinity": "INTEGER" + }, + { + "fieldPath": "maxItems", + "columnName": "maxItems", + "affinity": "INTEGER" + }, + { + "fieldPath": "pattern", + "columnName": "pattern", + "affinity": "TEXT" + }, + { + "fieldPath": "minLength", + "columnName": "minLength", + "affinity": "INTEGER" + }, + { + "fieldPath": "maxLength", + "columnName": "maxLength", + "affinity": "INTEGER" + }, + { + "fieldPath": "minValue", + "columnName": "minValue", + "affinity": "INTEGER" + }, + { + "fieldPath": "maxValue", + "columnName": "maxValue", + "affinity": "INTEGER" + }, + { + "fieldPath": "fieldDescription", + "columnName": "fieldDescription", + "affinity": "TEXT" + }, + { + "fieldPath": "transient", + "columnName": "transient", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isRequired", + "columnName": "isRequired", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "documentTypeId", + "columnName": "documentTypeId", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_properties_documentTypeId", + "unique": false, + "columnNames": [ + "documentTypeId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_properties_documentTypeId` ON `${TABLE_NAME}` (`documentTypeId`)" + } + ], + "foreignKeys": [ + { + "table": "document_types", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "documentTypeId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "pending_inputs", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `outpoint` BLOB NOT NULL, `inputIndex` INTEGER NOT NULL, `spendingTxid` BLOB NOT NULL, `spendingTransactionTxid` BLOB, `walletId` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `isSweptTombstone` INTEGER NOT NULL DEFAULT 0, `winnerMinedHeight` INTEGER, FOREIGN KEY(`spendingTransactionTxid`) REFERENCES `transactions`(`txid`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "outpoint", + "columnName": "outpoint", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "inputIndex", + "columnName": "inputIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "spendingTxid", + "columnName": "spendingTxid", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "spendingTransactionTxid", + "columnName": "spendingTransactionTxid", + "affinity": "BLOB" + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isSweptTombstone", + "columnName": "isSweptTombstone", + "affinity": "INTEGER", + "notNull": true, + "defaultValue": "0" + }, + { + "fieldPath": "winnerMinedHeight", + "columnName": "winnerMinedHeight", + "affinity": "INTEGER" + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_pending_inputs_outpoint", + "unique": false, + "columnNames": [ + "outpoint" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_pending_inputs_outpoint` ON `${TABLE_NAME}` (`outpoint`)" + }, + { + "name": "index_pending_inputs_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_pending_inputs_walletId` ON `${TABLE_NAME}` (`walletId`)" + }, + { + "name": "index_pending_inputs_spendingTransactionTxid", + "unique": false, + "columnNames": [ + "spendingTransactionTxid" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_pending_inputs_spendingTransactionTxid` ON `${TABLE_NAME}` (`spendingTransactionTxid`)" + }, + { + "name": "index_pending_inputs_spendingTxid", + "unique": false, + "columnNames": [ + "spendingTxid" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_pending_inputs_spendingTxid` ON `${TABLE_NAME}` (`spendingTxid`)" + }, + { + "name": "index_pending_inputs_walletId_isSweptTombstone_winnerMinedHeight", + "unique": false, + "columnNames": [ + "walletId", + "isSweptTombstone", + "winnerMinedHeight" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_pending_inputs_walletId_isSweptTombstone_winnerMinedHeight` ON `${TABLE_NAME}` (`walletId`, `isSweptTombstone`, `winnerMinedHeight`)" + } + ], + "foreignKeys": [ + { + "table": "transactions", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "spendingTransactionTxid" + ], + "referencedColumns": [ + "txid" + ] + } + ] + }, + { + "tableName": "tokens", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` BLOB NOT NULL, `contractId` BLOB NOT NULL, `position` INTEGER NOT NULL, `name` TEXT NOT NULL, `baseSupply` TEXT NOT NULL, `maxSupply` TEXT, `decimals` INTEGER NOT NULL, `localizations` TEXT, `isPaused` INTEGER NOT NULL, `allowTransferToFrozenBalance` INTEGER NOT NULL, `keepsTransferHistory` INTEGER NOT NULL, `keepsFreezingHistory` INTEGER NOT NULL, `keepsMintingHistory` INTEGER NOT NULL, `keepsBurningHistory` INTEGER NOT NULL, `keepsDirectPricingHistory` INTEGER NOT NULL, `keepsDirectPurchaseHistory` INTEGER NOT NULL, `conventionsChangeRules` TEXT, `maxSupplyChangeRules` TEXT, `manualMintingRules` TEXT, `manualBurningRules` TEXT, `freezeRules` TEXT, `unfreezeRules` TEXT, `destroyFrozenFundsRules` TEXT, `emergencyActionRules` TEXT, `perpetualDistribution` TEXT, `preProgrammedDistribution` TEXT, `oncePerIdentityDistribution` TEXT, `newTokensDestinationIdentity` BLOB, `mintingAllowChoosingDestination` INTEGER NOT NULL, `distributionChangeRules` TEXT, `tradeMode` TEXT NOT NULL, `tradeModeChangeRules` TEXT, `mainControlGroupPosition` INTEGER, `mainControlGroupCanBeModified` TEXT, `tokenDescription` TEXT, `createdAt` INTEGER NOT NULL, `lastUpdatedAt` INTEGER NOT NULL, `canManuallyMint` INTEGER NOT NULL, `canManuallyBurn` INTEGER NOT NULL, `canFreeze` INTEGER NOT NULL, `canUnfreeze` INTEGER NOT NULL, `canDestroyFrozenFunds` INTEGER NOT NULL, `hasEmergencyActions` INTEGER NOT NULL, `canChangeMaxSupply` INTEGER NOT NULL, `canChangeConventions` INTEGER NOT NULL, `canChangeTradeMode` INTEGER NOT NULL, `hasDistribution` INTEGER NOT NULL, PRIMARY KEY(`id`), FOREIGN KEY(`contractId`) REFERENCES `data_contracts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "contractId", + "columnName": "contractId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "position", + "columnName": "position", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "baseSupply", + "columnName": "baseSupply", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "maxSupply", + "columnName": "maxSupply", + "affinity": "TEXT" + }, + { + "fieldPath": "decimals", + "columnName": "decimals", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "localizations", + "columnName": "localizations", + "affinity": "TEXT" + }, + { + "fieldPath": "isPaused", + "columnName": "isPaused", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "allowTransferToFrozenBalance", + "columnName": "allowTransferToFrozenBalance", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsTransferHistory", + "columnName": "keepsTransferHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsFreezingHistory", + "columnName": "keepsFreezingHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsMintingHistory", + "columnName": "keepsMintingHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsBurningHistory", + "columnName": "keepsBurningHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsDirectPricingHistory", + "columnName": "keepsDirectPricingHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "keepsDirectPurchaseHistory", + "columnName": "keepsDirectPurchaseHistory", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "conventionsChangeRules", + "columnName": "conventionsChangeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "maxSupplyChangeRules", + "columnName": "maxSupplyChangeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "manualMintingRules", + "columnName": "manualMintingRules", + "affinity": "TEXT" + }, + { + "fieldPath": "manualBurningRules", + "columnName": "manualBurningRules", + "affinity": "TEXT" + }, + { + "fieldPath": "freezeRules", + "columnName": "freezeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "unfreezeRules", + "columnName": "unfreezeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "destroyFrozenFundsRules", + "columnName": "destroyFrozenFundsRules", + "affinity": "TEXT" + }, + { + "fieldPath": "emergencyActionRules", + "columnName": "emergencyActionRules", + "affinity": "TEXT" + }, + { + "fieldPath": "perpetualDistribution", + "columnName": "perpetualDistribution", + "affinity": "TEXT" + }, + { + "fieldPath": "preProgrammedDistribution", + "columnName": "preProgrammedDistribution", + "affinity": "TEXT" + }, + { + "fieldPath": "oncePerIdentityDistribution", + "columnName": "oncePerIdentityDistribution", + "affinity": "TEXT" + }, + { + "fieldPath": "newTokensDestinationIdentity", + "columnName": "newTokensDestinationIdentity", + "affinity": "BLOB" + }, + { + "fieldPath": "mintingAllowChoosingDestination", + "columnName": "mintingAllowChoosingDestination", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "distributionChangeRules", + "columnName": "distributionChangeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "tradeMode", + "columnName": "tradeMode", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "tradeModeChangeRules", + "columnName": "tradeModeChangeRules", + "affinity": "TEXT" + }, + { + "fieldPath": "mainControlGroupPosition", + "columnName": "mainControlGroupPosition", + "affinity": "INTEGER" + }, + { + "fieldPath": "mainControlGroupCanBeModified", + "columnName": "mainControlGroupCanBeModified", + "affinity": "TEXT" + }, + { + "fieldPath": "tokenDescription", + "columnName": "tokenDescription", + "affinity": "TEXT" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdatedAt", + "columnName": "lastUpdatedAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canManuallyMint", + "columnName": "canManuallyMint", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canManuallyBurn", + "columnName": "canManuallyBurn", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canFreeze", + "columnName": "canFreeze", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canUnfreeze", + "columnName": "canUnfreeze", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canDestroyFrozenFunds", + "columnName": "canDestroyFrozenFunds", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasEmergencyActions", + "columnName": "hasEmergencyActions", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canChangeMaxSupply", + "columnName": "canChangeMaxSupply", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canChangeConventions", + "columnName": "canChangeConventions", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "canChangeTradeMode", + "columnName": "canChangeTradeMode", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasDistribution", + "columnName": "hasDistribution", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_tokens_contractId", + "unique": false, + "columnNames": [ + "contractId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_tokens_contractId` ON `${TABLE_NAME}` (`contractId`)" + } + ], + "foreignKeys": [ + { + "table": "data_contracts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "contractId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "token_balances", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `tokenId` TEXT NOT NULL, `identityId` BLOB NOT NULL, `balance` BLOB NOT NULL, `frozen` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `lastSyncedAt` INTEGER, `tokenName` TEXT, `tokenSymbol` TEXT, `tokenDecimals` INTEGER, `networkRaw` INTEGER NOT NULL, `identityRef` BLOB, `tokenRef` BLOB, FOREIGN KEY(`identityRef`) REFERENCES `identities`(`identityId`) ON UPDATE NO ACTION ON DELETE SET NULL , FOREIGN KEY(`tokenRef`) REFERENCES `tokens`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "tokenId", + "columnName": "tokenId", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "balance", + "columnName": "balance", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "frozen", + "columnName": "frozen", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSyncedAt", + "columnName": "lastSyncedAt", + "affinity": "INTEGER" + }, + { + "fieldPath": "tokenName", + "columnName": "tokenName", + "affinity": "TEXT" + }, + { + "fieldPath": "tokenSymbol", + "columnName": "tokenSymbol", + "affinity": "TEXT" + }, + { + "fieldPath": "tokenDecimals", + "columnName": "tokenDecimals", + "affinity": "INTEGER" + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "identityRef", + "columnName": "identityRef", + "affinity": "BLOB" + }, + { + "fieldPath": "tokenRef", + "columnName": "tokenRef", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_token_balances_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_balances_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + }, + { + "name": "index_token_balances_tokenId_identityId", + "unique": false, + "columnNames": [ + "tokenId", + "identityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_balances_tokenId_identityId` ON `${TABLE_NAME}` (`tokenId`, `identityId`)" + }, + { + "name": "index_token_balances_identityId", + "unique": false, + "columnNames": [ + "identityId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_balances_identityId` ON `${TABLE_NAME}` (`identityId`)" + }, + { + "name": "index_token_balances_identityRef", + "unique": false, + "columnNames": [ + "identityRef" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_balances_identityRef` ON `${TABLE_NAME}` (`identityRef`)" + }, + { + "name": "index_token_balances_tokenRef", + "unique": false, + "columnNames": [ + "tokenRef" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_balances_tokenRef` ON `${TABLE_NAME}` (`tokenRef`)" + } + ], + "foreignKeys": [ + { + "table": "identities", + "onDelete": "SET NULL", + "onUpdate": "NO ACTION", + "columns": [ + "identityRef" + ], + "referencedColumns": [ + "identityId" + ] + }, + { + "table": "tokens", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "tokenRef" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "token_history_events", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `eventType` TEXT NOT NULL, `transactionId` BLOB, `blockHeight` INTEGER, `coreBlockHeight` INTEGER, `fromIdentity` BLOB, `toIdentity` BLOB, `performedByIdentity` BLOB NOT NULL, `amount` TEXT, `balanceBefore` TEXT, `balanceAfter` TEXT, `additionalDataJSON` BLOB, `eventDescription` TEXT, `createdAt` INTEGER NOT NULL, `eventTimestamp` INTEGER NOT NULL, `tokenRef` BLOB, PRIMARY KEY(`id`), FOREIGN KEY(`tokenRef`) REFERENCES `tokens`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "eventType", + "columnName": "eventType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "transactionId", + "columnName": "transactionId", + "affinity": "BLOB" + }, + { + "fieldPath": "blockHeight", + "columnName": "blockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "coreBlockHeight", + "columnName": "coreBlockHeight", + "affinity": "INTEGER" + }, + { + "fieldPath": "fromIdentity", + "columnName": "fromIdentity", + "affinity": "BLOB" + }, + { + "fieldPath": "toIdentity", + "columnName": "toIdentity", + "affinity": "BLOB" + }, + { + "fieldPath": "performedByIdentity", + "columnName": "performedByIdentity", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "amount", + "columnName": "amount", + "affinity": "TEXT" + }, + { + "fieldPath": "balanceBefore", + "columnName": "balanceBefore", + "affinity": "TEXT" + }, + { + "fieldPath": "balanceAfter", + "columnName": "balanceAfter", + "affinity": "TEXT" + }, + { + "fieldPath": "additionalDataJSON", + "columnName": "additionalDataJSON", + "affinity": "BLOB" + }, + { + "fieldPath": "eventDescription", + "columnName": "eventDescription", + "affinity": "TEXT" + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "eventTimestamp", + "columnName": "eventTimestamp", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "tokenRef", + "columnName": "tokenRef", + "affinity": "BLOB" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_token_history_events_tokenRef", + "unique": false, + "columnNames": [ + "tokenRef" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_token_history_events_tokenRef` ON `${TABLE_NAME}` (`tokenRef`)" + } + ], + "foreignKeys": [ + { + "table": "tokens", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "tokenRef" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "platform_addresses", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`address` TEXT NOT NULL, `addressType` INTEGER NOT NULL, `addressHash` BLOB NOT NULL, `publicKey` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `addressIndex` INTEGER NOT NULL, `derivationPath` TEXT NOT NULL, `isUsed` INTEGER NOT NULL, `balance` INTEGER NOT NULL, `nonce` INTEGER NOT NULL, `firstSeenHeight` INTEGER NOT NULL, `lastSeenHeight` INTEGER NOT NULL, `walletId` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `accountId` INTEGER, PRIMARY KEY(`walletId`, `address`), FOREIGN KEY(`accountId`) REFERENCES `accounts`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "address", + "columnName": "address", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "addressType", + "columnName": "addressType", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "addressHash", + "columnName": "addressHash", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "publicKey", + "columnName": "publicKey", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "addressIndex", + "columnName": "addressIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "derivationPath", + "columnName": "derivationPath", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "isUsed", + "columnName": "isUsed", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "balance", + "columnName": "balance", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "nonce", + "columnName": "nonce", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "firstSeenHeight", + "columnName": "firstSeenHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSeenHeight", + "columnName": "lastSeenHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER" + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId", + "address" + ] + }, + "indices": [ + { + "name": "index_platform_addresses_walletId_addressHash", + "unique": true, + "columnNames": [ + "walletId", + "addressHash" + ], + "orders": [], + "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_platform_addresses_walletId_addressHash` ON `${TABLE_NAME}` (`walletId`, `addressHash`)" + }, + { + "name": "index_platform_addresses_accountId", + "unique": false, + "columnNames": [ + "accountId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_platform_addresses_accountId` ON `${TABLE_NAME}` (`accountId`)" + } + ], + "foreignKeys": [ + { + "table": "accounts", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "accountId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "platform_addresses_sync_states", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `networkRaw` INTEGER NOT NULL, `syncHeight` INTEGER NOT NULL, `syncTimestamp` INTEGER NOT NULL, `lastKnownRecentBlock` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "syncHeight", + "columnName": "syncHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "syncTimestamp", + "columnName": "syncTimestamp", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastKnownRecentBlock", + "columnName": "lastKnownRecentBlock", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId" + ] + }, + "indices": [ + { + "name": "index_platform_addresses_sync_states_networkRaw", + "unique": false, + "columnNames": [ + "networkRaw" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_platform_addresses_sync_states_networkRaw` ON `${TABLE_NAME}` (`networkRaw`)" + } + ] + }, + { + "tableName": "shielded_notes", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`nullifier` BLOB NOT NULL, `walletId` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `position` INTEGER NOT NULL, `cmx` BLOB NOT NULL, `blockHeight` INTEGER NOT NULL, `isSpent` INTEGER NOT NULL, `value` INTEGER NOT NULL, `noteData` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`nullifier`))", + "fields": [ + { + "fieldPath": "nullifier", + "columnName": "nullifier", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "position", + "columnName": "position", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "cmx", + "columnName": "cmx", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "blockHeight", + "columnName": "blockHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "isSpent", + "columnName": "isSpent", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "value", + "columnName": "value", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "noteData", + "columnName": "noteData", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "nullifier" + ] + }, + "indices": [ + { + "name": "index_shielded_notes_walletId_accountIndex", + "unique": false, + "columnNames": [ + "walletId", + "accountIndex" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_shielded_notes_walletId_accountIndex` ON `${TABLE_NAME}` (`walletId`, `accountIndex`)" + } + ] + }, + { + "tableName": "shielded_outgoing_notes", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `cmx` BLOB NOT NULL, `recipient` BLOB NOT NULL, `value` INTEGER NOT NULL, `memo` BLOB NOT NULL, `blockHeight` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`, `accountIndex`, `cmx`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "cmx", + "columnName": "cmx", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "recipient", + "columnName": "recipient", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "value", + "columnName": "value", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "memo", + "columnName": "memo", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "blockHeight", + "columnName": "blockHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId", + "accountIndex", + "cmx" + ] + }, + "indices": [ + { + "name": "index_shielded_outgoing_notes_walletId_accountIndex", + "unique": false, + "columnNames": [ + "walletId", + "accountIndex" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_shielded_outgoing_notes_walletId_accountIndex` ON `${TABLE_NAME}` (`walletId`, `accountIndex`)" + } + ] + }, + { + "tableName": "shielded_activities", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `entryId` BLOB NOT NULL, `kindTag` INTEGER NOT NULL, `direction` INTEGER NOT NULL, `status` INTEGER NOT NULL, `amount` INTEGER NOT NULL, `fee` INTEGER NOT NULL, `hasFee` INTEGER NOT NULL, `blockHeight` INTEGER NOT NULL, `hasBlockHeight` INTEGER NOT NULL, `createdAtMs` INTEGER NOT NULL, `identityId` BLOB NOT NULL, `counterparty` BLOB NOT NULL, `memo` BLOB NOT NULL, `noteCmxs` BLOB NOT NULL, `spentNullifiers` BLOB NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`, `accountIndex`, `entryId`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "entryId", + "columnName": "entryId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "kindTag", + "columnName": "kindTag", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "direction", + "columnName": "direction", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "status", + "columnName": "status", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "amount", + "columnName": "amount", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "fee", + "columnName": "fee", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasFee", + "columnName": "hasFee", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "blockHeight", + "columnName": "blockHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "hasBlockHeight", + "columnName": "hasBlockHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAtMs", + "columnName": "createdAtMs", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "counterparty", + "columnName": "counterparty", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "memo", + "columnName": "memo", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "noteCmxs", + "columnName": "noteCmxs", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "spentNullifiers", + "columnName": "spentNullifiers", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId", + "accountIndex", + "entryId" + ] + }, + "indices": [ + { + "name": "index_shielded_activities_walletId_accountIndex", + "unique": false, + "columnNames": [ + "walletId", + "accountIndex" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_shielded_activities_walletId_accountIndex` ON `${TABLE_NAME}` (`walletId`, `accountIndex`)" + } + ] + }, + { + "tableName": "shielded_sync_states", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `lastSyncedIndex` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`, `accountIndex`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastSyncedIndex", + "columnName": "lastSyncedIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId", + "accountIndex" + ] + }, + "indices": [ + { + "name": "index_shielded_sync_states_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_shielded_sync_states_walletId` ON `${TABLE_NAME}` (`walletId`)" + } + ] + }, + { + "tableName": "shielded_viewing_keys", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`walletId` BLOB NOT NULL, `accountIndex` INTEGER NOT NULL, `fvkBytes` BLOB NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`walletId`, `accountIndex`))", + "fields": [ + { + "fieldPath": "walletId", + "columnName": "walletId", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "accountIndex", + "columnName": "accountIndex", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "fvkBytes", + "columnName": "fvkBytes", + "affinity": "BLOB", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "walletId", + "accountIndex" + ] + }, + "indices": [ + { + "name": "index_shielded_viewing_keys_walletId", + "unique": false, + "columnNames": [ + "walletId" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_shielded_viewing_keys_walletId` ON `${TABLE_NAME}` (`walletId`)" + } + ] + }, + { + "tableName": "wallet_manager_metadata", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`networkRaw` INTEGER NOT NULL, `combinedSyncHeight` INTEGER NOT NULL, `combinedSyncBlockHash` BLOB, `walletCount` INTEGER NOT NULL, `createdAt` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, PRIMARY KEY(`networkRaw`))", + "fields": [ + { + "fieldPath": "networkRaw", + "columnName": "networkRaw", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "combinedSyncHeight", + "columnName": "combinedSyncHeight", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "combinedSyncBlockHash", + "columnName": "combinedSyncBlockHash", + "affinity": "BLOB" + }, + { + "fieldPath": "walletCount", + "columnName": "walletCount", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "createdAt", + "columnName": "createdAt", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUpdated", + "columnName": "lastUpdated", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "networkRaw" + ] + } + } + ], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4cdbf8acfcfa07b5745b6aacd7f9d5bb')" + ] + } +} \ No newline at end of file diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index c278bfaa0eb..d092bad92fe 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -1753,6 +1753,7 @@ mod tests { //! 3. Reconcile must be idempotent and never clobber an existing //! entry for the same txid. + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use std::collections::BTreeMap; use std::sync::{Arc, Mutex}; @@ -3789,6 +3790,199 @@ mod tests { ); } + /// Contacts discovered only after the scan has passed their request + /// heights. Registration never moves the cursor on this tree; the + /// reconcile pass then rewinds exactly once and writes the record with + /// the real pair — the floor and the cursor it rewound from — on the + /// round that lowers the cursor. A kill mid-climb then resumes from the + /// persisted cursor: the drain's re-registrations are no-ops, the pass + /// has nothing to build, the reconcile does not rewind, and the record + /// still reads pending with its pair intact. + #[tokio::test] + async fn late_discovered_contacts_rewind_once_and_a_mid_climb_kill_resumes_from_the_cursor() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contacts: Vec = (1..=8u8).map(|b| Identifier::from([b; 32])).collect(); + let shared_key = [0x55u8; 32]; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + { + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let managed = info + .identity_manager + .managed_identity_mut(&owner) + .expect("managed"); + for (i, contact) in contacts.iter().enumerate() { + let height = 1_226_329 + (i as u32) * 5_000; + let outgoing = + ContactRequest::new(owner, *contact, 0, 0, 0, vec![0u8; 96], height, 0); + let ciphertext = encrypted_contact_xpub(&owner, contact, &shared_key); + let incoming = ContactRequest::new(*contact, owner, 0, 0, 7, ciphertext, height, 0); + managed.apply_established_contact(EstablishedContact::new( + *contact, outgoing, incoming, + )); + } + } + // The scan is already at the tip when the contacts are discovered. + set_synced_height(&manager, wallet_id, 1_560_731).await; + persister.stores.lock().unwrap().clear(); + + let mut lowering_rounds = 0usize; + for contact in &contacts { + let before = persister.stores.lock().unwrap().len(); + iw.dashpay() + .register_contact_account(&owner, contact, 0, test_receiving_xpub(&owner, contact)) + .await + .expect("register receival"); + let encrypted = encrypted_contact_xpub(&owner, contact, &shared_key); + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &encrypted, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register outbound"); + iw.dashpay() + .note_external_account_registered(&owner, contact, &encrypted) + .await; + let stores = persister.stores.lock().unwrap(); + lowering_rounds += stores[before..] + .iter() + .filter(|(_, cs)| { + cs.core + .as_ref() + .and_then(|core| core.synced_height) + .is_some() + }) + .count(); + } + assert_eq!( + lowering_rounds, 0, + "registration never moves the cursor on this tree" + ); + assert_eq!(synced_height(&manager, wallet_id).await, 1_560_731); + // The reconcile pass is what rewinds: once, to the earliest request + // height, recording every contact on the round that lowers the cursor. + assert_eq!( + iw.dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + Some(1_226_329) + ); + assert_eq!(synced_height(&manager, wallet_id).await, 1_226_329); + { + let stores = persister.stores.lock().unwrap(); + let lowering = stores + .iter() + .find(|(_, cs)| { + cs.core + .as_ref() + .and_then(|core| core.synced_height) + .is_some() + }) + .map(|(_, cs)| cs) + .expect("the lowering round"); + assert_eq!( + lowering.core.as_ref().and_then(|core| core.synced_height), + Some(1_226_329) + ); + let record = lowering + .dashpay_backfill + .as_ref() + .expect("the lowering round carries the record"); + assert_eq!( + (record.floor, record.rewound_from), + (1_226_329, 1_560_731), + "the real pair: the floor and the cursor it rewound from" + ); + // The last record written covers every contact, pair unchanged. + let last = last_stored_record(&stores) + .unwrap() + .dashpay_backfill + .as_ref() + .unwrap(); + assert_eq!((last.floor, last.rewound_from), (1_226_329, 1_560_731)); + for contact in &contacts { + assert!(last.covered_from(&owner, contact).is_some()); + } + } + + // Killed mid-climb: the host persisted the cursor at 1_400_000. + let snapshot = reload_snapshot(&manager, wallet_id, owner, 1_400_000, None).await; + let (restarted, restarted_persister, _) = reload(snapshot).await; + let restored = restarted.get_wallet(&wallet_id).await.expect("wallet"); + { + let wm = restarted.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + let managed = info.identity_manager.managed_identity(&owner).unwrap(); + for contact in &contacts { + assert!(!external_account_needs_rebuild( + &managed.dashpay().established_contacts()[contact], + true + )); + } + assert!( + crate::wallet::identity::network::dashpay_view::DashPayView::< + crate::broadcaster::SpvBroadcaster, + >::collect_account_build_candidates(info, &owner) + .is_empty() + ); + assert!(info.dashpay_backfill.is_pending(1_400_000)); + } + // A drain that re-registers anyway is a no-op on accounts on file. + for contact in &contacts { + restored + .identity() + .dashpay() + .register_contact_account(&owner, contact, 0, test_receiving_xpub(&owner, contact)) + .await + .expect("idempotent re-registration"); + } + assert_eq!( + restored + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None + ); + assert_eq!( + synced_height_of(&restarted, wallet_id).await, + 1_400_000, + "the climb resumes from the persisted cursor" + ); + assert!( + restarted_persister + .stores + .lock() + .unwrap() + .iter() + .all(|(_, cs)| cs + .core + .as_ref() + .and_then(|core| core.synced_height) + .is_none()), + "nothing lowers the cursor on the cold start" + ); + let wm = restarted.wallet_manager.read().await; + let record = &wm.get_wallet_info(&wallet_id).unwrap().dashpay_backfill; + assert_eq!((record.floor, record.rewound_from), (1_226_329, 1_560_731)); + assert!(record.is_pending(1_400_000)); + assert!(record.is_complete(1_560_731)); + } + async fn synced_height_of( manager: &Arc>, wallet_id: WalletId, From ffe15d6dbf903aa4544cbfbe7ba9ad2c8db3669c Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Sat, 26 Sep 2026 20:32:18 -0700 Subject: [PATCH 08/43] fix(platform-wallet): keep the backfill record's coverage paired with the cursor it vouches for MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review of #5026 found four ways coverage could reach disk over a cursor the rescan had not actually re-matched from, plus a Kotlin API break: - A `SyncHeightAdvanced` the engine queued before `reconcile_dashpay_rescan` lowered the cursor could be persisted after the rescan round and put the durable cursor back above the range still to be re-matched. The event adapter now drops an advance above the wallet's in-memory cursor at drain time; the rescan's own advances carry the cursor from the floor up. - A failed record store left the lowered cursor, the marks and the record in memory, so a later round with no cursor of its own stored that coverage beside a stale durable cursor. On failure the record is put back, the pass's marks are dropped so the next pass re-attempts, and the cursor the round owed rides the next successful round (`DashPayBackfillRecord::unpersisted_cursor`, in-memory only). - While the manager's persistence fault is latched the adapter holds the durable cursor back at the last fully persisted height; a record round written then would advance it past rows that never landed. `IdentityWallet` now carries the latch and the reconcile stores nothing while it is set — the in-memory rewind still runs the rescan, the next launch re-runs it. - `PlatformWalletChangeSet::merge` let the core merge's monotonic max keep an earlier advance over a later rewind round in an accumulating backend. A round carrying a backfill record with a cursor is a rewind and now overrides that max. - A process-local `rescan_triggered` mark was taken as coverage: a contact covered from 100 whose checkpoint later dropped to 50 had the lower height recorded without a rewind. A mark alone no longer counts; an uncovered contact re-arms through the candidate path whether or not it is marked. - `NativePersistenceBridge.onPersistContactUpsert` keeps its previous signature; the marker rides a new overload whose default delegates to it, so an external implementation of the old form keeps compiling and keeps receiving rows. Tests: a queued advance above the rewound cursor is dropped; a failed record round is retried with the cursor it owed; a latched fault keeps the record off disk; a rewind round overrides the merge max; a marked contact whose checkpoint dropped is rewound, not recorded lower. Co-Authored-By: Claude Fable 5.1 --- .../dashsdk/ffi/NativePersistenceBridge.kt | 47 ++- .../PlatformWalletPersistenceHandler.kt | 29 ++ .../rs-platform-wallet-ffi/src/persistence.rs | 1 + .../src/changeset/changeset.rs | 78 +++- .../src/changeset/core_bridge.rs | 104 +++++- .../src/changeset/dashpay_backfill.rs | 9 + .../rs-platform-wallet/src/manager/load.rs | 1 + .../rs-platform-wallet/src/manager/mod.rs | 1 + .../src/manager/wallet_lifecycle.rs | 1 + .../src/wallet/core/broadcast.rs | 1 + .../identity/network/dpns_marketplace.rs | 1 + .../identity/network/identity_handle.rs | 9 + .../src/wallet/identity/network/payments.rs | 343 +++++++++++++++--- .../src/wallet/platform_wallet.rs | 5 + .../src/wallet/provider_ecdsa_key_tests.rs | 1 + .../wallet/shielded/viewing_key_bind_tests.rs | 1 + 16 files changed, 570 insertions(+), 62 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt index c1b747c8b85..ad333c7e634 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt @@ -591,8 +591,10 @@ abstract class NativePersistenceBridge { // ── Contacts ────────────────────────────────────────────────────── /** - * One `ContactRequestFFI` upsert. Descriptor - * `([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[IZI)I`. + * One `ContactRequestFFI` upsert, marker-less form (the signature this + * API had before the outbound-account marker). Native calls the + * overload below; this one stays so existing implementations keep + * compiling and keep receiving rows through the delegating default. * * The tail block ([paymentChannelBroken] / [alias] / [note] / * [isHidden] / [contactAccountLabel] / [acceptedAccounts]) is @@ -629,10 +631,47 @@ abstract class NativePersistenceBridge { isHidden: Boolean, contactAccountLabel: String?, acceptedAccounts: IntArray, - hasExternalAccountReference: Boolean = false, - externalAccountReference: Int = 0, ): Int = 0 + /** + * The overload native calls (descriptor + * `([B[B[BZIII[B[B[BIJZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;[IZI)I`): + * the row above plus `EstablishedContact::external_account_reference` as + * `(hasExternalAccountReference, externalAccountReference)`. The default + * body delegates to the marker-less overload, so an implementation that + * overrides only that one keeps persisting every other field and merely + * loses the marker (native then rebuilds the outbound account once per + * launch, the pre-marker behaviour). Override this one to store it. + */ + @Suppress("LongParameterList") + open fun onPersistContactUpsert( + walletId: ByteArray, + ownerId: ByteArray, + contactId: ByteArray, + isOutgoing: Boolean, + senderKeyIndex: Int, + recipientKeyIndex: Int, + accountReference: Int, + encryptedPublicKey: ByteArray, + encryptedAccountLabel: ByteArray?, + autoAcceptProof: ByteArray?, + coreHeightCreatedAt: Int, + createdAt: Long, + paymentChannelBroken: Boolean, + alias: String?, + note: String?, + isHidden: Boolean, + contactAccountLabel: String?, + acceptedAccounts: IntArray, + hasExternalAccountReference: Boolean, + externalAccountReference: Int, + ): Int = onPersistContactUpsert( + walletId, ownerId, contactId, isOutgoing, senderKeyIndex, recipientKeyIndex, + accountReference, encryptedPublicKey, encryptedAccountLabel, autoAcceptProof, + coreHeightCreatedAt, createdAt, paymentChannelBroken, alias, note, isHidden, + contactAccountLabel, acceptedAccounts, + ) + /** One sent-side `ContactRequestRemovalFFI`. Descriptor `([B[B[B)I`. */ open fun onPersistContactRemovalSent( walletId: ByteArray, diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt index 5c94e271efe..e45dd60e2ea 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt @@ -2128,6 +2128,35 @@ class PlatformWalletPersistenceHandler( // ── Contacts ────────────────────────────────────────────────────── + /** Marker-less form: same row, no outbound-account marker (stored as NULL). */ + override fun onPersistContactUpsert( + walletId: ByteArray, + ownerId: ByteArray, + contactId: ByteArray, + isOutgoing: Boolean, + senderKeyIndex: Int, + recipientKeyIndex: Int, + accountReference: Int, + encryptedPublicKey: ByteArray, + encryptedAccountLabel: ByteArray?, + autoAcceptProof: ByteArray?, + coreHeightCreatedAt: Int, + createdAt: Long, + paymentChannelBroken: Boolean, + alias: String?, + note: String?, + isHidden: Boolean, + contactAccountLabel: String?, + acceptedAccounts: IntArray, + ): Int = onPersistContactUpsert( + walletId, ownerId, contactId, isOutgoing, senderKeyIndex, recipientKeyIndex, + accountReference, encryptedPublicKey, encryptedAccountLabel, autoAcceptProof, + coreHeightCreatedAt, createdAt, paymentChannelBroken, alias, note, isHidden, + contactAccountLabel, acceptedAccounts, + hasExternalAccountReference = false, + externalAccountReference = 0, + ) + override fun onPersistContactUpsert( walletId: ByteArray, ownerId: ByteArray, diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 9e0078ea5b0..e9691b69d2b 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -5334,6 +5334,7 @@ fn decode_dashpay_backfill( floor: entry.dashpay_backfill_floor, rewound_from: entry.dashpay_backfill_rewound_from, covered, + unpersisted_cursor: None, } } diff --git a/packages/rs-platform-wallet/src/changeset/changeset.rs b/packages/rs-platform-wallet/src/changeset/changeset.rs index 87d195d4bf1..413ed98dd2d 100644 --- a/packages/rs-platform-wallet/src/changeset/changeset.rs +++ b/packages/rs-platform-wallet/src/changeset/changeset.rs @@ -2162,7 +2162,9 @@ pub struct PlatformWalletChangeSet { /// [`ClientWalletStartState::dashpay_backfill`](crate::changeset::ClientWalletStartState::dashpay_backfill) /// so a fresh process does not rewind for a contact the previous one /// already rewound for (dashpay/platform#4302). Every write carries the - /// whole record, so merge is last-write-wins. See [`DashPayBackfillRecord`]. + /// whole record, so merge is last-write-wins — and when the round also + /// carries a `core.synced_height`, that cursor is a rewind and overrides + /// the core merge's monotonic max. See [`DashPayBackfillRecord`]. /// /// Durability caveat, the same one `identity_scan_state` carries: a host /// that has not adopted the slot keeps today's behaviour — the backfill @@ -2279,7 +2281,22 @@ impl Merge for PlatformWalletChangeSet { fn merge(&mut self, other: Self) { // `CoreChangeSet` implements `Merge`; delegate via the // `Option: Merge` blanket impl from this crate's merge module. + // + // One exception to its monotonic-max watermark: a round that + // carries a backfill record with a cursor is a REWIND, and the + // coverage in that record is only true over that cursor. A backend + // that accumulates rounds before flushing must therefore let the + // rewind win over an earlier advance in the same accumulation, or + // it flushes coverage beside the height the rewind retracted. + let rewind = other + .dashpay_backfill + .is_some() + .then(|| other.core.as_ref().and_then(|core| core.synced_height)) + .flatten(); self.core.merge(other.core); + if let (Some(rewound_to), Some(core)) = (rewind, self.core.as_mut()) { + core.synced_height = Some(rewound_to); + } self.identities.merge(other.identities); self.identity_keys.merge(other.identity_keys); self.contacts.merge(other.contacts); @@ -3356,6 +3373,65 @@ mod tests { assert_eq!(cs.addresses_marked_used.len(), 3); } + /// A round carrying a backfill record with a cursor is a rewind: when a + /// backend accumulates it behind an earlier advance, the rewind's cursor + /// must win over the core merge's monotonic max, or the accumulated + /// flush stores coverage beside the height the rewind retracted + /// (dashpay/platform#4302 review). + #[test] + fn a_rewind_round_with_a_backfill_record_overrides_the_core_merge_max() { + use crate::changeset::DashPayBackfillRecord; + + let mut acc = PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(1_000), + ..CoreChangeSet::default() + }), + ..PlatformWalletChangeSet::default() + }; + let mut record = DashPayBackfillRecord::default(); + record.record_pass( + 1_000, + Some(100), + [( + Identifier::from([1u8; 32]), + Identifier::from([2u8; 32]), + 100, + )], + ); + acc.merge(PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(100), + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(record.clone()), + ..PlatformWalletChangeSet::default() + }); + assert_eq!( + acc.core.as_ref().and_then(|core| core.synced_height), + Some(100), + "the rewind's cursor wins over the earlier advance" + ); + assert_eq!(acc.dashpay_backfill, Some(record)); + + // A record round without a cursor changes nothing about the merge. + let mut acc = PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(1_000), + ..CoreChangeSet::default() + }), + ..PlatformWalletChangeSet::default() + }; + acc.merge(PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }); + assert_eq!( + acc.core.as_ref().and_then(|core| core.synced_height), + Some(1_000) + ); + } + /// Highest-used watermarks merge monotonic-max per account per /// pool slot: a later batch can only advance a slot, and `None` /// never erases a prior `Some`. diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index de8c7839e9f..71cffdcf6a3 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -1113,6 +1113,8 @@ async fn build_core_changeset( wallet_manager: &Arc>>, event: &WalletEvent, ) -> CoreChangeSet { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + match event { WalletEvent::TransactionDetected { wallet_id, @@ -1339,10 +1341,38 @@ async fn build_core_changeset( ..CoreChangeSet::default() } } - WalletEvent::SyncHeightAdvanced { height, .. } => CoreChangeSet { - synced_height: Some(*height), - ..CoreChangeSet::default() - }, + WalletEvent::SyncHeightAdvanced { wallet_id, height } => { + // A watermark queued before a rewind must not land after it. + // `reconcile_dashpay_rescan` lowers the in-memory cursor and + // stores the lowered value with its coverage record on its own + // round; an advance the engine emitted just before that rewind + // (still in this channel) would otherwise be persisted after + // it and put the durable cursor back above the range the rescan + // has yet to re-match — with coverage on disk vouching for it. + // The in-memory cursor is the wallet's truth at drain time: an + // event above it is stale, so its watermark is dropped and the + // rescan's own advances carry the cursor from the floor up. + let current = { + let wm = wallet_manager.read().await; + wm.get_wallet_info(wallet_id) + .map(|info| info.core_wallet.synced_height()) + }; + match current { + Some(current) if *height > current => { + tracing::debug!( + wallet_id = %hex::encode(wallet_id), + event_height = *height, + current, + "wallet-event adapter: dropping a sync-height advance queued before a rewind" + ); + CoreChangeSet::default() + } + _ => CoreChangeSet { + synced_height: Some(*height), + ..CoreChangeSet::default() + }, + } + } WalletEvent::ChainLockProcessed { chain_lock, .. } => { // The wallet has already promoted the matching records from // `InBlock` to `InChainLockedBlock` by the time this event @@ -4477,6 +4507,72 @@ mod tests { WalletEvent::SyncHeightAdvanced { wallet_id, height } } + /// A watermark the engine queued BEFORE `reconcile_dashpay_rescan` + /// lowered the wallet's cursor must not be persisted after it: the + /// rescan round stores the lowered cursor with its coverage record, and + /// a stale advance landing afterwards would put the durable cursor back + /// above the range the rescan has yet to re-match, with coverage on + /// disk vouching for it (dashpay/platform#4302 review). The in-memory + /// cursor at drain time decides: an event above it is dropped, one at or + /// below it flows. + #[tokio::test] + async fn a_sync_height_advance_queued_before_a_rewind_is_dropped() { + use crate::wallet::core::WalletGeneration; + use crate::wallet::identity::IdentityManager; + use key_wallet::test_utils::TestWalletContext; + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let ctx = TestWalletContext::new_random(); + let mut info = PlatformWalletInfo { + observed_input_conflicts: Default::default(), + core_wallet: ctx.managed_wallet, + generation: Arc::new(WalletGeneration::new()), + identity_manager: IdentityManager::new(), + tracked_asset_locks: BTreeMap::new(), + dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + }; + info.core_wallet.update_synced_height(1_000); + let manager = test_manager(); + let wallet_id = manager + .write() + .await + .insert_wallet(ctx.wallet, info) + .expect("insert wallet"); + + // In step with the engine: the advance the engine just applied. + let cs = super::build_core_changeset(&manager, &sync_height_event(wallet_id, 1_000)).await; + assert_eq!(cs.synced_height, Some(1_000)); + + // The rescan reconcile rewinds the wallet to 100 while that advance + // is still queued: it must not land. + manager + .write() + .await + .get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(100); + let stale = + super::build_core_changeset(&manager, &sync_height_event(wallet_id, 1_000)).await; + assert_eq!( + stale.synced_height, None, + "an advance above the rewound cursor was queued before the rewind" + ); + assert!( + crate::changeset::Merge::is_empty(&stale), + "nothing else rides a bare watermark" + ); + + // The rescan's own advances carry the cursor from the floor up. + let fresh = super::build_core_changeset(&manager, &sync_height_event(wallet_id, 100)).await; + assert_eq!(fresh.synced_height, Some(100)); + + // An unknown wallet has no cursor to compare against: unchanged. + let unknown = super::build_core_changeset(&manager, &sync_height_event([9u8; 32], 7)).await; + assert_eq!(unknown.synced_height, Some(7)); + } + /// A record-bearing (non-watermark) event: carries /// `last_processed_height` but no `synced_height`, so it survives the /// freeze untouched. Used both as a sentinel to prove loop progress and diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 2b8a716403f..8c97da5a219 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -97,6 +97,14 @@ pub struct DashPayBackfillRecord { /// Receival contacts the backfill covers, sorted by `(owner, contact)`, /// at most one entry per pair. pub covered: Vec, + /// In-memory only, never persisted or crossed over the FFI: a cursor a + /// previous record round still owes the host because that round's + /// store failed. The next successful record round carries it as the + /// round's `synced_height`, so coverage never reaches disk over a + /// durable cursor that is still at a height the in-memory rewind left + /// behind. `None` when nothing is owed. + #[cfg_attr(feature = "serde", serde(skip))] + pub unpersisted_cursor: Option, } impl DashPayBackfillRecord { @@ -269,6 +277,7 @@ impl DashPayBackfillRecord { floor, rewound_from, covered, + unpersisted_cursor: None, }) } } diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 64688a1f12d..18906573c11 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -342,6 +342,7 @@ impl PlatformWalletManager

{ Arc::clone(&self.lock_notify), Arc::clone(&persister_dyn), broadcaster, + Arc::clone(&self.sync_fault), ); // Initialize the platform-address provider. If the snapshot diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 51a57c32451..db1c3d77fe6 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -1188,6 +1188,7 @@ mod tests { Arc::new(Notify::new()), Arc::new(NoopPersister) as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), )); mgr.wallets.rcu(|wallets| { let mut next = std::collections::BTreeMap::clone(wallets); diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 2dd8519c15f..830aeaa9125 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -568,6 +568,7 @@ impl PlatformWalletManager

{ Arc::clone(&self.lock_notify), persister_dyn, broadcaster, + Arc::clone(&self.sync_fault), ); // Restore the platform-address provider from the slice read above — diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs index 1879f18346c..d82a3ecbacd 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs @@ -819,6 +819,7 @@ mod tests { Arc::new(crate::test_support::NoopTestPersister) as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), )) } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs b/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs index 603aff74b8c..2d9d48479b7 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs @@ -3775,6 +3775,7 @@ mod tests { wallet_id, asset_locks, persister, + sync_fault: Arc::new(std::sync::atomic::AtomicBool::new(false)), broadcaster, sdk_writer: Arc::new(super::super::sdk_writer::SdkWriter::new(sdk)), dpns_operation_gate: Arc::new(tokio::sync::Mutex::new(())), 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 402b6930737..5cec0bce323 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 @@ -312,6 +312,14 @@ pub struct IdentityWallet { pub(crate) asset_locks: Arc>, /// Per-wallet persistence handle for queuing changesets. pub(crate) persister: crate::wallet::persister::WalletPersister, + /// The manager's persistence-fault latch (see + /// [`PlatformWalletManager::sync_fault_detected`](crate::PlatformWalletManager::sync_fault_detected)). + /// While it is set the wallet-event adapter holds the durable + /// `synced_height` back at the last fully persisted height, and a + /// direct cursor write from this side would advance it past rows that + /// never landed. The DashPay rescan reconcile consults it before it + /// stores a backfill record with a lowered cursor. + pub(crate) sync_fault: Arc, /// Broadcaster for DashPay payment transactions. Distinct from the /// asset-lock broadcaster — the asset-lock manager is always /// `SpvBroadcaster`-pinned, while this one picks the broadcaster @@ -353,6 +361,7 @@ impl Clone for IdentityWallet { wallet_id: self.wallet_id, asset_locks: Arc::clone(&self.asset_locks), persister: self.persister.clone(), + sync_fault: Arc::clone(&self.sync_fault), broadcaster: Arc::clone(&self.broadcaster), sdk_writer: Arc::clone(&self.sdk_writer), dpns_operation_gate: Arc::clone(&self.dpns_operation_gate), diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index d092bad92fe..c64b532b481 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -90,15 +90,16 @@ impl DashPayView<'_, B> { /// /// # The guard, in memory and on disk /// - /// Each contact the pass handles is recorded twice. In memory, in - /// [`DashPayState::rescan_triggered`](crate::wallet::identity::DashPayState), - /// so the recurring sweep does not re-lower the height every pass (which - /// would reset the in-flight backfill and keep it from ever completing); - /// the contact-request state transitions clear that mark when a request - /// changes, so the next pass re-evaluates the contact. And durably, in the + /// Each contact the pass handles is recorded twice. Durably, in the /// wallet's [`DashPayBackfillRecord`], keyed per contact on the checkpoint - /// it was covered from, written on the same persistence round as the - /// lowered cursor. + /// it was covered from and written on the same persistence round as the + /// lowered cursor — that record is what decides whether a contact is + /// covered. And in memory, in + /// [`DashPayState::rescan_triggered`](crate::wallet::identity::DashPayState), + /// which only tracks which contacts this process has handled; a mark on + /// its own never counts as coverage, so a contact whose record write + /// failed, or whose checkpoint dropped below the height it was recorded + /// at, is re-armed through the candidate path. /// /// The durable half exists because the cursor this pass lowers is itself /// durable: the host persists every `SyncHeightAdvanced` the engine emits @@ -163,21 +164,26 @@ impl DashPayView<'_, B> { }) .collect(); - // Candidates: established receival contacts not yet rescanned this - // lifetime whose funding height is below our scan tip. The floor is the - // minimum funding height — one rewind covers them all (deeper-funded - // contacts are in the watch set, so the backfill matches them too). The + // Candidates: established receival contacts the durable record does + // not cover at their current checkpoint. The floor is the minimum + // funding height — one rewind covers them all (deeper-funded contacts + // are in the watch set, so the backfill matches them too). The // funding height is `min(outgoing, incoming)` of the pair: the channel - // is payable only once both requests exist, so the earlier of the two is - // the conservative-correct lower bound. + // is payable only once both requests exist, so the earlier of the two + // is the conservative-correct lower bound. + // + // A process-local `rescan_triggered` mark is never taken as coverage + // on its own: the mark may belong to a pass whose record write failed, + // or the contact's checkpoint may since have dropped below the height + // it was recorded at (an older reciprocal request learned later). + // Either way the range the scan never tested with this contact has to + // be rewound to, so an uncovered contact goes through the candidate + // path whether or not it is marked. let mut floor: Option = None; // Contacts this pass handles, with the checkpoint each is covered from. let mut to_mark: Vec<(Identifier, Identifier, u32)> = Vec::new(); - // Contacts already marked in memory whose durable entry is missing or - // stale (a pass whose record write failed, or a mark that predates the - // record), so the next launch does not rewind for them again. - let mut to_record: Vec<(Identifier, Identifier, u32)> = Vec::new(); - // Contacts the durable record vouched for this pass. + // Contacts the durable record vouched for that are not yet marked in + // this process. let mut restored: Vec<(Identifier, Identifier)> = Vec::new(); for (owner, contact) in receival_pairs.iter().copied() { let Some(managed) = info.identity_manager.managed_identity(&owner) else { @@ -190,28 +196,21 @@ impl DashPayView<'_, B> { .outgoing_request .core_height_created_at .min(established.incoming_request.core_height_created_at); - if managed.dashpay().rescan_triggered.contains(&contact) { - if info - .dashpay_backfill - .covered_from(&owner, &contact) - .is_none_or(|covered_from| covered_from > checkpoint) - { - to_record.push((owner, contact, checkpoint)); - } - continue; - } - // The previous process already rewound for this contact and the - // persisted cursor tracked that scan with its addresses watched, - // so nothing below the cursor is missing: resume, don't restart. + // The previous round already rewound for this contact from a + // checkpoint no higher than today's, and the persisted cursor + // tracked that scan with its addresses watched, so nothing below + // the cursor is missing: resume, don't restart. if info.dashpay_backfill.covers(&owner, &contact, checkpoint) { - restored.push((owner, contact)); + if !managed.dashpay().rescan_triggered.contains(&contact) { + restored.push((owner, contact)); + } continue; } // Contacts funded below the tip need a backfill — their addresses // weren't watched when those blocks were first scanned. Contacts // funded at or after the tip are already covered by the ongoing // forward scan (their addresses are watched from establishment). - // EITHER way the contact is now handled, so mark it: once the + // EITHER way the contact is now handled, so record it: once the // forward pointer later climbs past a still-forward-covered // contact's funding height, the recurring sweep must NOT then // rewind to it and redundantly re-scan an already-scanned range. @@ -240,7 +239,7 @@ impl DashPayView<'_, B> { ); } - if to_mark.is_empty() && to_record.is_empty() { + if to_mark.is_empty() { return Ok(None); } @@ -272,36 +271,72 @@ impl DashPayView<'_, B> { // `floor <= checkpoint`, a forward-covered one because its addresses // are watched from `synced_height <= checkpoint` on. Entries for // accounts that no longer exist are dropped so the record tracks the - // live receival set. The record and the lowered cursor ride ONE round, - // so a host that stores the record has necessarily stored the cursor - // it vouches for — never a record over a cursor still at its - // high-water. + // live receival set. + // + // The record and the cursor it vouches for ride ONE round, and the + // record reaches memory only once that round is on disk: + // - the round carries the floor this pass rewound to, or failing that + // a cursor an earlier failed round still owes, so coverage is never + // stored beside a durable cursor the in-memory rewind left behind; + // - if the store fails, the in-memory record is put back, the pass's + // marks are dropped so the next pass re-attempts, and the cursor + // this round owed is remembered for that retry; + // - while the manager's persistence fault is latched the adapter is + // holding the durable cursor back at the last fully persisted + // height, and a cursor written from here could advance it past rows + // that never landed — so nothing is stored at all: the in-memory + // rewind still runs the rescan, and the next launch re-runs it, + // which is the pre-record behaviour. + let previous_record = info.dashpay_backfill.clone(); let record = &mut info.dashpay_backfill; - let mut changed = record.retain(|owner, contact| { + record.retain(|owner, contact| { receival_pairs .iter() .any(|(o, c)| (o, c) == (owner, contact)) }); - changed |= record.record_pass(synced_height, floor, to_mark.into_iter().chain(to_record)); - if changed { + record.record_pass(synced_height, floor, to_mark.iter().copied()); + let owed = record.unpersisted_cursor.take(); + let cursor_to_write = match (floor, owed) { + (Some(floor), Some(owed)) => Some(floor.min(owed)), + (Some(floor), None) => Some(floor), + (None, owed) => owed, + }; + let cursor_now = floor.unwrap_or(synced_height); + let faulted = self.sync_fault.load(std::sync::atomic::Ordering::Relaxed); + let stored = if faulted { + Err(None) + } else { let changeset = PlatformWalletChangeSet { - core: floor.map(|floor| CoreChangeSet { - synced_height: Some(floor), + core: cursor_to_write.map(|cursor| CoreChangeSet { + synced_height: Some(cursor), ..CoreChangeSet::default() }), dashpay_backfill: Some(record.clone()), ..PlatformWalletChangeSet::default() }; - if let Err(e) = self.persister.store(changeset) { - // The in-memory guard still holds for this process; the next - // launch re-runs the backfill for these contacts, which is the - // pre-record behaviour — slow, never lossy. - tracing::warn!( + self.persister.store(changeset).map_err(Some) + }; + if let Err(error) = stored { + match error { + Some(error) => tracing::warn!( wallet_id = %hex::encode(self.wallet_id), - error = %e, - "DashPay rescan: failed to persist the backfill record; the next launch \ - will re-run this backfill" - ); + error = %error, + "DashPay rescan: failed to persist the backfill record; the next pass \ + retries and the next launch re-runs this backfill" + ), + None => tracing::warn!( + wallet_id = %hex::encode(self.wallet_id), + "DashPay rescan: persistence fault latched; not recording backfill coverage \ + over a held-back cursor — the next launch re-runs this backfill" + ), + } + info.dashpay_backfill = previous_record; + info.dashpay_backfill.unpersisted_cursor = + Some(cursor_to_write.map_or(cursor_now, |cursor| cursor.min(cursor_now))); + for (owner, contact, _) in &to_mark { + if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { + managed.dashpay_rescan_triggered_mut().remove(contact); + } } } Ok(floor) @@ -1784,6 +1819,8 @@ mod tests { #[derive(Default)] struct RecordingPersister { stores: Mutex>, + /// When set, every `store` is refused (and not recorded). + fail_stores: Mutex, } impl PlatformWalletPersistence for RecordingPersister { @@ -1792,6 +1829,9 @@ mod tests { wallet_id: WalletId, changeset: PlatformWalletChangeSet, ) -> Result<(), PersistenceError> { + if *self.fail_stores.lock().unwrap() { + return Err(PersistenceError::backend("injected store failure")); + } self.stores.lock().unwrap().push((wallet_id, changeset)); Ok(()) } @@ -3983,6 +4023,199 @@ mod tests { assert!(record.is_complete(1_560_731)); } + /// A record round that fails to store must not leave its coverage + /// behind in memory: a later round (a forward-covered contact, no + /// cursor of its own) would then store the whole record — this + /// contact included — beside a durable cursor still at its high-water, + /// and the next launch would skip the rescan (dashpay/platform#4302 + /// review). On failure the record is put back, the pass's marks are + /// dropped, and the cursor the round owed rides the next successful + /// round. + #[tokio::test] + async fn a_failed_record_round_is_retried_with_the_cursor_it_owed() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let first = Identifier::from([0xBB; 32]); + let later = Identifier::from([0xCC; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, first, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + persister.stores.lock().unwrap().clear(); + *persister.fail_stores.lock().unwrap() = true; + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass whose store fails"), + Some(100), + "the in-memory rewind still runs the rescan" + ); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.synced_height(), 100); + assert!( + info.dashpay_backfill.covered_from(&owner, &first).is_none(), + "coverage that never reached disk must not stay in memory" + ); + assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); + assert!( + !info + .identity_manager + .managed_identity(&owner) + .unwrap() + .dashpay() + .rescan_triggered + .contains(&first), + "the pass's mark is dropped so the next pass re-attempts" + ); + } + + // Another contact turns up, forward-covered at the rewound cursor — + // exactly the round that used to store `first` without a cursor. + *persister.fail_stores.lock().unwrap() = false; + establish_receival_contact(&manager, &persister, wallet_id, owner, later, 200, 200).await; + persister.stores.lock().unwrap().clear(); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("retry pass"), + None, + "nothing is below the rewound cursor, so no new rewind" + ); + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the retry stores the record"); + assert_eq!( + round.core.as_ref().and_then(|core| core.synced_height), + Some(100), + "the retry carries the cursor the failed round owed" + ); + let record = round.dashpay_backfill.as_ref().unwrap(); + assert_eq!(record.covered_from(&owner, &first), Some(100)); + assert_eq!(record.covered_from(&owner, &later), Some(200)); + } + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .unwrap() + .dashpay_backfill + .unpersisted_cursor, + None + ); + } + + /// A process-local mark is not coverage. A contact covered from 100 + /// whose checkpoint then drops to 50 (an older reciprocal request) must + /// be rewound to 50 — not have the lower checkpoint recorded as covered + /// while the range 50–100 was never scanned for it + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_marked_contact_whose_checkpoint_dropped_is_rewound_not_recorded_lower() { + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("first pass"), + Some(100) + ); + // The scan climbs; then the reciprocal request turns out to be older. + set_synced_height(&manager, wallet_id, 400).await; + { + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 50, 0); + let managed = info + .identity_manager + .managed_identity_mut(&owner) + .expect("managed"); + managed.apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + assert!( + managed.dashpay().rescan_triggered.contains(&contact), + "the mark survives the request change on this tree" + ); + } + persister.stores.lock().unwrap().clear(); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass after the drop"), + Some(50), + "a marked contact below its recorded height re-arms through the candidate path" + ); + assert_eq!(synced_height(&manager, wallet_id).await, 50); + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the deeper rewind stores its record"); + assert_eq!( + round.core.as_ref().and_then(|core| core.synced_height), + Some(50) + ); + let record = round.dashpay_backfill.as_ref().unwrap(); + assert_eq!(record.covered_from(&owner, &contact), Some(50)); + assert_eq!((record.floor, record.rewound_from), (50, 1_000)); + } + + /// While the manager's persistence fault is latched the adapter holds + /// the durable cursor back at the last fully persisted height. A record + /// round written then would advance it past rows that never landed, so + /// the reconcile rewinds in memory but stores nothing — the next launch + /// re-runs the backfill, the pre-record behaviour + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_latched_persistence_fault_keeps_the_record_off_disk() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + wallet + .identity() + .sync_fault + .store(true, std::sync::atomic::Ordering::Relaxed); + persister.stores.lock().unwrap().clear(); + + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass under a latched fault"), + Some(100), + "the in-memory rewind still runs the rescan" + ); + assert!( + persister.stores.lock().unwrap().is_empty(), + "nothing reaches the persister while the fault is latched" + ); + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.synced_height(), 100); + assert!(info.dashpay_backfill.is_empty()); + assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); + } + async fn synced_height_of( manager: &Arc>, wallet_id: WalletId, @@ -8354,6 +8587,7 @@ mod tests { wallet_id: real.wallet_id, asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), + sync_fault: Arc::clone(&real.sync_fault), broadcaster: Arc::new(GatedRejectingBroadcaster { entered, release }), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), @@ -8393,6 +8627,7 @@ mod tests { wallet_id: real.wallet_id, asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), + sync_fault: Arc::clone(&real.sync_fault), broadcaster: Arc::new(GatedBroadcaster { entered, release }), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), @@ -8413,6 +8648,7 @@ mod tests { wallet_id: real.wallet_id, asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), + sync_fault: Arc::clone(&real.sync_fault), broadcaster: Arc::new(AcceptingBroadcaster), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), @@ -8448,6 +8684,7 @@ mod tests { wallet_id: real.wallet_id, asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), + sync_fault: Arc::clone(&real.sync_fault), broadcaster: Arc::new(RejectingBroadcaster), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index e25ffa798c6..b43db02d794 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -670,6 +670,9 @@ impl PlatformWallet { /// Construct a PlatformWallet from a WalletManager that already contains /// the wallet. The wallet must have been inserted into the WalletManager /// before calling this. + // Every argument is a shared handle the manager owns; a builder would + // only move the same eight names behind a struct. + #[allow(clippy::too_many_arguments)] pub(crate) fn new( sdk: Arc, wallet_id: WalletId, @@ -678,6 +681,7 @@ impl PlatformWallet { lock_notify: Arc, persister: Arc, broadcaster: Arc, + sync_fault: Arc, ) -> Self { // Build the per-wallet persister handle once and share it with // the sub-wallets that need to queue their own changesets @@ -723,6 +727,7 @@ impl PlatformWallet { wallet_id, asset_locks: Arc::clone(&asset_locks), persister: wallet_persister.clone(), + sync_fault, broadcaster: dashpay_broadcaster, // DashPay write helper: forwards to the live SDK, erasing its // generic write signatures behind concrete by-value methods. diff --git a/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs b/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs index 2e38a340120..be4a9916b59 100644 --- a/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs @@ -45,6 +45,7 @@ async fn platform_wallet() -> PlatformWallet { Arc::new(tokio::sync::Notify::new()), Arc::new(NoopTestPersister) as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), ) } diff --git a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs index 670eaef352a..976f352536e 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs @@ -188,6 +188,7 @@ where Arc::new(tokio::sync::Notify::new()), persister as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), ) } From a40871e4df49d22defff5922fd8adffd77781554 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Mon, 28 Sep 2026 09:52:51 -0700 Subject: [PATCH 09/43] fix(platform-wallet): order backfill cursor writes against the event adapter and keep rewinds explicit Second review round on #5026: - Ordering. The event adapter and the DashPay rescan reconcile both write the durable `synced_height`, and nothing ordered them: an advance the adapter had projected could be stored after a rewind, a rewind could write a cursor past rows the adapter had not persisted yet, and `spv_rescan_filters_blocking` lowered the cursor without any record round knowing. `DurableCursors` now holds the cursor each wallet's host last accepted and is the lock both writers take (then the manager, never the reverse) across "decide the height" and "store it". The adapter re-checks each advance against the live cursor under it and drops one projected before a rewind; the reconcile never writes a cursor at or above the durable one (coverage is safe beside a lower one); a manual rescan reset is recorded as owed and rides the next record round. Seeded at load and creation; the manager spawns the adapter through `spawn_wallet_event_adapter_with_durable_cursors`, and `spawn_wallet_event_adapter` keeps its signature. - Merge. Inferring "rewind" from a record and a cursor sharing a round was not associative. `CoreChangeSet::synced_height_is_rewind` marks it explicitly and merges as an ordered reset/advance composition. - Extent. A failed record round now keeps its `(floor, rewound_from)` in memory (`unpersisted_extent`) and the retry folds it back in, so the stored record reads pending with the real climb target. - Non-atomic C hosts. The backfill callback is withheld when the round's cursor did not reach the host (changeset callback failed or absent). - JVM compatibility. The backfill and marker fields of `WalletRestoreData` and `ContactRequestRestoreData` are body properties again, so both classes keep the exact JVM constructors they had on v4.2-dev. Each has a test; the FFI gate, the extent carry and the commit re-check were mutation-checked. Co-Authored-By: Claude Opus 5.5 --- .../dashsdk/ffi/NativePersistenceBridge.kt | 33 ++- .../PlatformWalletPersistenceHandler.kt | 22 +- .../PlatformWalletPersistenceHandlerTest.kt | 54 +++++ .../rs-platform-wallet-ffi/src/persistence.rs | 111 +++++++++- .../src/changeset/changeset.rs | 181 ++++++++++------- .../src/changeset/core_bridge.rs | 192 ++++++++++++++++++ .../src/changeset/dashpay_backfill.rs | 8 + .../rs-platform-wallet/src/changeset/mod.rs | 4 +- .../src/manager/accessors.rs | 6 + .../rs-platform-wallet/src/manager/load.rs | 11 + .../rs-platform-wallet/src/manager/mod.rs | 14 +- .../src/manager/wallet_lifecycle.rs | 6 + .../src/wallet/core/broadcast.rs | 1 + .../identity/network/dpns_marketplace.rs | 1 + .../identity/network/identity_handle.rs | 6 + .../src/wallet/identity/network/payments.rs | 172 +++++++++++++++- .../src/wallet/platform_wallet.rs | 4 +- .../src/wallet/provider_ecdsa_key_tests.rs | 1 + .../wallet/shielded/viewing_key_bind_tests.rs | 1 + 19 files changed, 732 insertions(+), 96 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt index ad333c7e634..3ed10cff7d1 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt @@ -1047,6 +1047,14 @@ class WalletRestoreData( * `loadWalletList`. */ @JvmField val lastAppliedChainLockBytes: ByteArray, +) { + // The DashPay backfill record rides as body properties, not constructor + // parameters, so this class keeps exactly the JVM constructors it had + // before the record existed: a bridge compiled against the older SDK — + // Java, or Kotlin using the defaulted `providerSpecialTxs` — still links + // (dashpay/platform#4302 review). Set them with `apply { … }`; native + // reads them by field name either way. + /** * Whether the wallet row holds a DashPay coreHeight backfill record * (dashpay/platform#4302). `false` for a wallet that never rewound for a @@ -1055,22 +1063,25 @@ class WalletRestoreData( * it always did and writes one. Mirror of * `WalletRestoreEntryFFI.has_dashpay_backfill`. */ - @JvmField val hasDashPayBackfill: Boolean = false, + @JvmField var hasDashPayBackfill: Boolean = false + /** Lowest height the backfill rewound the cursor to. */ - @JvmField val dashPayBackfillFloor: Int = 0, + @JvmField var dashPayBackfillFloor: Int = 0 + /** * Highest cursor the backfill rewound from — the height the scan climbs * back to for the backfill to be complete. */ - @JvmField val dashPayBackfillRewoundFrom: Int = 0, + @JvmField var dashPayBackfillRewoundFrom: Int = 0 + /** * The record's cover set, exactly as [NativePersistenceBridge.onWalletChangesetDashPayBackfill] * delivered it: `68` bytes per covered contact. Native re-packs it into * a `DashPayBackfillCoveredContactFFI` array; a blob that is not a whole * number of entries is read as no record. */ - @JvmField val dashPayBackfillCovered: ByteArray = ByteArray(0), -) + @JvmField var dashPayBackfillCovered: ByteArray = ByteArray(0) +} /** * Kotlin staging row for the unchanged `ProviderSpecialTxRestoreEntryFFI`. @@ -1360,15 +1371,21 @@ class ContactRequestRestoreData( @JvmField val isHidden: Boolean, @JvmField val contactAccountLabel: String?, @JvmField val acceptedAccounts: IntArray, +) { + // Body properties, not constructor parameters, for the same reason as + // [WalletRestoreData]'s backfill fields: the class keeps the JVM + // constructor it had before the marker existed. + /** * `EstablishedContact::external_account_reference` as a `(present, * value)` pair, exactly as [NativePersistenceBridge.onPersistContactUpsert] * delivered it. `false` restores `None`, which makes native rebuild the * outbound account once on the next sweep. */ - @JvmField val hasExternalAccountReference: Boolean = false, - @JvmField val externalAccountReference: Int = 0, -) + @JvmField var hasExternalAccountReference: Boolean = false + + @JvmField var externalAccountReference: Int = 0 +} /** * One flat identity-public-key row — mirror of `IdentityKeyRestoreFFI`. diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt index e45dd60e2ea..2bc88bc8134 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt @@ -2769,11 +2769,14 @@ class PlatformWalletPersistenceHandler( unresolvedAssetLockTxRecords = unresolvedAssetLockTxRecords, providerSpecialTxs = providerSpecialTxs, lastAppliedChainLockBytes = lastAppliedChainLockBytes, - hasDashPayBackfill = hasBackfill, - dashPayBackfillFloor = if (hasBackfill) backfillFloor!! else 0, - dashPayBackfillRewoundFrom = if (hasBackfill) backfillRewoundFrom!! else 0, - dashPayBackfillCovered = if (hasBackfill) backfillCovered!! else ByteArray(0), - ), + ).apply { + if (hasBackfill) { + hasDashPayBackfill = true + dashPayBackfillFloor = backfillFloor!! + dashPayBackfillRewoundFrom = backfillRewoundFrom!! + dashPayBackfillCovered = backfillCovered!! + } + }, ) } out.toTypedArray() @@ -3014,9 +3017,12 @@ class PlatformWalletPersistenceHandler( isHidden = c.contactHidden, contactAccountLabel = c.contactAccountLabel, acceptedAccounts = decodeAcceptedAccounts(c.contactAcceptedAccounts), - hasExternalAccountReference = c.externalAccountReference != null, - externalAccountReference = c.externalAccountReference ?: 0, - ) + ).apply { + c.externalAccountReference?.let { reference -> + hasExternalAccountReference = true + externalAccountReference = reference + } + } }.toTypedArray() // Ignored senders (per-sender mute) — restores the Rust // `ignored_senders` set so a previously-ignored sender doesn't diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt index 44281b16fca..bd68f084b97 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt @@ -4888,6 +4888,60 @@ class PlatformWalletPersistenceHandlerTest { assertEquals(4, db.dashpayDao().getContactRequestsByOwner(ownerId).single().externalAccountReference) } + /** + * dashpay/platform#4302 review: adding the backfill record and the + * outbound-account marker must not change the JVM constructors of the two + * restore holders, or a bridge compiled against the previous SDK throws + * `NoSuchMethodError` when it builds restore rows. Pins both the + * full-arity constructor (Java callers) and `WalletRestoreData`'s + * default-argument synthetic (Kotlin callers leaving `providerSpecialTxs` + * out) against the v4.2-dev parameter lists. + */ + @Test + fun restoreHoldersKeepTheirPreviousJvmConstructors() { + fun ctorTypes(cls: Class<*>) = cls.declaredConstructors.map { it.parameterTypes.map { p -> p.name } }.toSet() + + val walletBase = listOf( + "[B", "int", "[Lorg.dashfoundation.dashsdk.ffi.AccountSpecData;", "long", "long", "long", + "int", "int", "int", "long", + "[Lorg.dashfoundation.dashsdk.ffi.IdentityRestoreData;", + "[Lorg.dashfoundation.dashsdk.ffi.PlatformAddressBalanceRestoreData;", + "[Lorg.dashfoundation.dashsdk.ffi.UtxoRestoreData;", + "[Lorg.dashfoundation.dashsdk.ffi.CoreAddressPoolRestoreData;", + "[Lorg.dashfoundation.dashsdk.ffi.TrackedAssetLockRestoreData;", + "[Lorg.dashfoundation.dashsdk.ffi.UnresolvedAssetLockTxRecordData;", + "[Lorg.dashfoundation.dashsdk.ffi.ProviderSpecialTxRestoreData;", + "[B", + ) + val walletCtors = ctorTypes(org.dashfoundation.dashsdk.ffi.WalletRestoreData::class.java) + assertTrue("full-arity constructor unchanged", walletCtors.contains(walletBase)) + assertTrue( + "default-argument constructor unchanged", + walletCtors.contains(walletBase + listOf("int", "kotlin.jvm.internal.DefaultConstructorMarker")), + ) + + val contactBase = listOf( + "[B", "[B", "boolean", "int", "int", "int", "[B", "[B", "[B", "int", "long", "boolean", + "java.lang.String", "java.lang.String", "boolean", "java.lang.String", "[I", + ) + assertTrue( + "contact restore constructor unchanged", + ctorTypes(org.dashfoundation.dashsdk.ffi.ContactRequestRestoreData::class.java).contains(contactBase), + ) + + // The new fields are still plain fields native can read by name. + for ((cls, names) in listOf( + org.dashfoundation.dashsdk.ffi.WalletRestoreData::class.java to listOf( + "hasDashPayBackfill", "dashPayBackfillFloor", "dashPayBackfillRewoundFrom", "dashPayBackfillCovered", + ), + org.dashfoundation.dashsdk.ffi.ContactRequestRestoreData::class.java to listOf( + "hasExternalAccountReference", "externalAccountReference", + ), + )) { + for (name in names) assertNotNull("$name is a JVM field", cls.getField(name)) + } + } + @Test fun loadWalletListScopesToTheHandlerNetwork() = runTest { // A network-scoped handler must never hand the Rust loader a diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index e9691b69d2b..20760bd1afb 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -1889,6 +1889,13 @@ impl PlatformWalletPersistence for FFIPersister { } } let mut outcome = RoundOutcome::default(); + // Whether this round's `synced_height` reached the host. A backfill + // record rides the round that lowers the cursor it vouches for, and + // must not reach the host when that cursor did not: a host without + // atomic rounds would keep the coverage over its old cursor, and no + // rollback on this side can take it back (dashpay/platform#4302 + // review). + let mut cursor_delivered = false; // Wallet-registration metadata. Fires at most once per round // (registration emits the entry; subsequent rounds carry @@ -2158,6 +2165,8 @@ impl PlatformWalletPersistence for FFIPersister { result ); outcome.record(result); + } else if core_cs.synced_height.is_some() { + cursor_delivered = true; } } @@ -2226,7 +2235,15 @@ impl PlatformWalletPersistence for FFIPersister { // after the core changeset callback so the lowered cursor a rescan // round carries is staged before the record that vouches for it. // Whole-record semantics: the host replaces what it holds. - if let Some(record) = changeset.dashpay_backfill.as_ref() { + let backfill_cursor_owed = changeset + .core + .as_ref() + .is_some_and(|core| core.synced_height.is_some()); + if let Some(record) = changeset + .dashpay_backfill + .as_ref() + .filter(|_| !backfill_cursor_owed || cursor_delivered) + { if let Some(cb) = self.wallet_dashpay_backfill_callback { let covered = build_dashpay_backfill_covered_for_callback(record); let result = unsafe { @@ -5335,6 +5352,7 @@ fn decode_dashpay_backfill( rewound_from: entry.dashpay_backfill_rewound_from, covered, unpersisted_cursor: None, + unpersisted_extent: None, } } @@ -8517,6 +8535,97 @@ mod tests { drop(persister); } + /// A backfill record rides the round that lowers the cursor it vouches + /// for, and must not reach the host when that cursor did not — a host + /// without atomic rounds would keep the coverage over its old cursor + /// (dashpay/platform#4302 review). Withheld when the changeset callback + /// fails or is absent for a round that carries a cursor; delivered for a + /// record-only round. + #[test] + fn a_backfill_record_is_withheld_when_its_cursor_did_not_reach_the_host() { + use platform_wallet::changeset::{CoreChangeSet, DashPayBackfillRecord}; + use std::sync::atomic::{AtomicUsize, Ordering}; + + #[derive(Default)] + struct Sink { + backfills: AtomicUsize, + } + unsafe extern "C" fn reject_changeset( + _ctx: *mut c_void, + _wallet_id: *const u8, + _changeset: *const WalletChangeSetFFI, + ) -> i32 { + 1 + } + unsafe extern "C" fn count_backfill( + ctx: *mut c_void, + _wallet_id: *const u8, + _floor: u32, + _rewound_from: u32, + _covered: *const DashPayBackfillCoveredContactFFI, + _covered_count: usize, + ) -> i32 { + (*(ctx as *const Sink)) + .backfills + .fetch_add(1, Ordering::SeqCst); + 0 + } + fn persister( + sink: &Sink, + changeset: Option, + ) -> FFIPersister { + FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: sink as *const Sink as *mut c_void, + on_persist_wallet_changeset_fn: changeset, + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + PersistenceExtensionCallbacks { + wallet_dashpay_backfill: Some(count_backfill), + ..Default::default() + }, + ) + } + type PersistWalletChangesetFnForTest = + unsafe extern "C" fn(*mut c_void, *const u8, *const WalletChangeSetFFI) -> i32; + let rewind_round = || PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(100), + synced_height_is_rewind: true, + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }; + + // The host rejected the cursor: the record must not follow it. + let sink = Sink::default(); + let rejected = persister(&sink, Some(reject_changeset)); + assert!(rejected.store([1u8; 32], rewind_round()).is_err()); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 0); + + // The host takes no cursors at all: a record over one is meaningless. + let sink = Sink::default(); + let cursorless = persister(&sink, None); + cursorless + .store([1u8; 32], rewind_round()) + .expect("nothing failed"); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 0); + + // A record-only round (no cursor to pair with) is delivered. + cursorless + .store( + [1u8; 32], + PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }, + ) + .expect("record-only round"); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 1); + } + /// The restore side of dashpay/platform#4302: a wallet-restore entry /// carrying the record decodes into the start state's /// `dashpay_backfill` in canonical form, and an entry without it — the diff --git a/packages/rs-platform-wallet/src/changeset/changeset.rs b/packages/rs-platform-wallet/src/changeset/changeset.rs index 413ed98dd2d..50ceafcb384 100644 --- a/packages/rs-platform-wallet/src/changeset/changeset.rs +++ b/packages/rs-platform-wallet/src/changeset/changeset.rs @@ -162,6 +162,22 @@ pub struct CoreChangeSet { /// on merge. pub synced_height: Option, + /// Whether [`synced_height`](Self::synced_height) is a REWIND rather than + /// an advance. The DashPay rescan reconcile lowers the durable cursor + /// deliberately and stores the lowered value with the backfill record + /// that vouches for it; that value must replace whatever the round is + /// merged behind instead of losing to the ordinary monotonic max. + /// + /// Merge is an ordered reset/advance composition — `advance(a)·advance(b) + /// = advance(max)`, `x·rewind(b) = rewind(b)`, `rewind(a)·advance(b) = + /// rewind(max(a, b))` — which is associative, so how a backend groups + /// rounds into accumulations never changes the result. Meaningless while + /// `synced_height` is `None`. Hosts that write the cursor they are handed + /// honour it directly; the SQLite backend keeps its monotonic max and + /// does not store the backfill record, so it neither needs nor sees it. + #[cfg_attr(feature = "serde", serde(default))] + pub synced_height_is_rewind: bool, + /// Addresses freshly derived as a side effect of processing the /// records in this batch — i.e. `WalletEvent::TransactionDetected. /// addresses_derived` and `WalletEvent::BlockProcessed. @@ -747,7 +763,16 @@ impl Merge for CoreChangeSet { ); } if let Some(h) = other.synced_height { - self.synced_height = Some(self.synced_height.map_or(h, |existing| existing.max(h))); + if other.synced_height_is_rewind { + // A rewind replaces whatever it is merged behind. + self.synced_height = Some(h); + self.synced_height_is_rewind = true; + } else { + // An advance composes by max, and keeps a rewind a rewind: + // `rewind(a)·advance(b)` still has to replace anything an + // outer accumulation merges this result behind. + self.synced_height = Some(self.synced_height.map_or(h, |existing| existing.max(h))); + } } // Derived-address dedup. Within a single `WalletEvent` the @@ -2162,9 +2187,9 @@ pub struct PlatformWalletChangeSet { /// [`ClientWalletStartState::dashpay_backfill`](crate::changeset::ClientWalletStartState::dashpay_backfill) /// so a fresh process does not rewind for a contact the previous one /// already rewound for (dashpay/platform#4302). Every write carries the - /// whole record, so merge is last-write-wins — and when the round also - /// carries a `core.synced_height`, that cursor is a rewind and overrides - /// the core merge's monotonic max. See [`DashPayBackfillRecord`]. + /// whole record, so merge is last-write-wins. The lowered cursor it + /// travels with is marked [`CoreChangeSet::synced_height_is_rewind`], so + /// it survives any accumulation. See [`DashPayBackfillRecord`]. /// /// Durability caveat, the same one `identity_scan_state` carries: a host /// that has not adopted the slot keeps today's behaviour — the backfill @@ -2281,22 +2306,7 @@ impl Merge for PlatformWalletChangeSet { fn merge(&mut self, other: Self) { // `CoreChangeSet` implements `Merge`; delegate via the // `Option: Merge` blanket impl from this crate's merge module. - // - // One exception to its monotonic-max watermark: a round that - // carries a backfill record with a cursor is a REWIND, and the - // coverage in that record is only true over that cursor. A backend - // that accumulates rounds before flushing must therefore let the - // rewind win over an earlier advance in the same accumulation, or - // it flushes coverage beside the height the rewind retracted. - let rewind = other - .dashpay_backfill - .is_some() - .then(|| other.core.as_ref().and_then(|core| core.synced_height)) - .flatten(); self.core.merge(other.core); - if let (Some(rewound_to), Some(core)) = (rewind, self.core.as_mut()) { - core.synced_height = Some(rewound_to); - } self.identities.merge(other.identities); self.identity_keys.merge(other.identity_keys); self.contacts.merge(other.contacts); @@ -3373,63 +3383,90 @@ mod tests { assert_eq!(cs.addresses_marked_used.len(), 3); } - /// A round carrying a backfill record with a cursor is a rewind: when a - /// backend accumulates it behind an earlier advance, the rewind's cursor - /// must win over the core merge's monotonic max, or the accumulated - /// flush stores coverage beside the height the rewind retracted - /// (dashpay/platform#4302 review). + /// The rewind marker composes associatively: however a backend groups + /// rounds into accumulations, a rewind replaces what precedes it, an + /// advance after a rewind raises it and keeps it a rewind, and a round + /// that carries only a backfill record changes no cursor. The earlier + /// version inferred "rewind" from a record and a cursor riding one round, + /// which grouping could manufacture (dashpay/platform#4302 review). #[test] - fn a_rewind_round_with_a_backfill_record_overrides_the_core_merge_max() { + fn the_rewind_marker_composes_associatively() { use crate::changeset::DashPayBackfillRecord; - let mut acc = PlatformWalletChangeSet { - core: Some(CoreChangeSet { - synced_height: Some(1_000), - ..CoreChangeSet::default() - }), - ..PlatformWalletChangeSet::default() - }; - let mut record = DashPayBackfillRecord::default(); - record.record_pass( - 1_000, - Some(100), - [( - Identifier::from([1u8; 32]), - Identifier::from([2u8; 32]), - 100, - )], - ); - acc.merge(PlatformWalletChangeSet { - core: Some(CoreChangeSet { - synced_height: Some(100), - ..CoreChangeSet::default() - }), - dashpay_backfill: Some(record.clone()), - ..PlatformWalletChangeSet::default() - }); - assert_eq!( - acc.core.as_ref().and_then(|core| core.synced_height), - Some(100), - "the rewind's cursor wins over the earlier advance" - ); - assert_eq!(acc.dashpay_backfill, Some(record)); - - // A record round without a cursor changes nothing about the merge. - let mut acc = PlatformWalletChangeSet { - core: Some(CoreChangeSet { - synced_height: Some(1_000), - ..CoreChangeSet::default() - }), - ..PlatformWalletChangeSet::default() - }; - acc.merge(PlatformWalletChangeSet { - dashpay_backfill: Some(DashPayBackfillRecord::default()), - ..PlatformWalletChangeSet::default() - }); - assert_eq!( - acc.core.as_ref().and_then(|core| core.synced_height), - Some(1_000) + fn advance(h: u32) -> PlatformWalletChangeSet { + PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(h), + ..CoreChangeSet::default() + }), + ..PlatformWalletChangeSet::default() + } + } + fn rewind(h: u32) -> PlatformWalletChangeSet { + PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: Some(h), + synced_height_is_rewind: true, + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + } + } + fn record_only() -> PlatformWalletChangeSet { + PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + } + } + fn fold(parts: Vec) -> PlatformWalletChangeSet { + let mut acc = PlatformWalletChangeSet::default(); + for part in parts { + acc.merge(part); + } + acc + } + fn cursor(cs: &PlatformWalletChangeSet) -> (Option, bool) { + cs.core.as_ref().map_or((None, false), |core| { + (core.synced_height, core.synced_height_is_rewind) + }) + } + // Both parenthesizations of every triple. + type Case = ( + &'static str, + fn() -> Vec, + (Option, bool), ); + let cases: Vec = vec![ + ( + "advance, lower advance, record-only", + || vec![advance(1_000), advance(100), record_only()], + (Some(1_000), false), + ), + ( + "advance, rewind, later advance", + || vec![advance(1_000), rewind(100), advance(150)], + (Some(150), true), + ), + ( + "rewind, advance, rewind", + || vec![rewind(500), advance(900), rewind(200)], + (Some(200), true), + ), + ( + "advance, advance, rewind", + || vec![advance(1_000), advance(1_200), rewind(100)], + (Some(100), true), + ), + ]; + for (name, parts, expected) in cases { + let [a, b, c]: [PlatformWalletChangeSet; 3] = parts().try_into().ok().unwrap(); + let left = fold(vec![fold(vec![a, b]), c]); + let [a, b, c]: [PlatformWalletChangeSet; 3] = parts().try_into().ok().unwrap(); + let right = fold(vec![a, fold(vec![b, c])]); + assert_eq!(cursor(&left), expected, "{name}: (A·B)·C"); + assert_eq!(cursor(&right), expected, "{name}: A·(B·C)"); + } } /// Highest-used watermarks merge monotonic-max per account per diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index 71cffdcf6a3..fce84d5dd0d 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -172,6 +172,9 @@ struct BatchDiagnostics { wallets: usize, /// Highest watermark the store accepted and committed. persisted: Option, + /// The watermark each wallet's store accepted in this drain — what that + /// wallet's host now holds, recorded into [`DurableCursors`]. + persisted_by_wallet: BTreeMap, /// Highest watermark withheld by the fail-closed guard. frozen: Option, /// Highest watermark the store rejected. @@ -258,6 +261,49 @@ pub fn spawn_wallet_event_adapter

( sync_fault: Arc, cancel: CancellationToken, ) -> JoinHandle<()> +where + P: PlatformWalletPersistence + 'static, +{ + spawn_wallet_event_adapter_with_durable_cursors( + wallet_manager, + persister, + receiver, + sync_fault, + crate::changeset::DurableCursors::default(), + cancel, + ) +} + +/// The durable `synced_height` each wallet's host last accepted, shared by +/// every writer of that cursor and doubling as the lock that orders them. +/// +/// Two writers exist: the wallet-event adapter (advances, one per committed +/// batch) and the DashPay rescan reconcile (a deliberate rewind stored with +/// the backfill record that vouches for it). Without one lock across +/// "decide the height" and "store it", an advance the adapter had already +/// projected could be stored after the rewind and put the durable cursor back +/// above the range still to be re-matched, and a rewind could advance the +/// durable cursor past rows the adapter had not yet persisted +/// (dashpay/platform#4302 review). Both take this lock before they read the +/// wallet's in-memory cursor and hold it until their `store()` returns; the +/// adapter strips an advance above the in-memory cursor at commit time, and +/// the reconcile never writes a cursor at or above the recorded durable one. +/// +/// Lock order is this mutex, then the wallet manager — never the reverse. +/// Seeded with the cursor each wallet was loaded or created with. +pub type DurableCursors = Arc>>; + +/// [`spawn_wallet_event_adapter`] sharing `durable_cursors` with the other +/// writer of the durable sync cursor (the DashPay rescan reconcile). The +/// manager uses this form; see [`DurableCursors`]. +pub fn spawn_wallet_event_adapter_with_durable_cursors

( + wallet_manager: Arc>>, + persister: Weak

, + receiver: mpsc::UnboundedReceiver, + sync_fault: Arc, + durable_cursors: DurableCursors, + cancel: CancellationToken, +) -> JoinHandle<()> where P: PlatformWalletPersistence + 'static, { @@ -266,10 +312,46 @@ where persister, receiver, sync_fault, + durable_cursors, cancel, )) } +/// Strip, from each wallet's batch, a sync-height advance above that +/// wallet's in-memory cursor. Such an advance was projected before a rewind +/// lowered the cursor; storing it now would claim coverage the rescan has +/// not produced. Called with [`DurableCursors`] held, right before commit, +/// so no rewind can slip in between this check and the store. +async fn drop_watermarks_above_the_live_cursor( + wallet_manager: &Arc>>, + batch: &mut BTreeMap, +) { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let wm = wallet_manager.read().await; + for (wallet_id, wallet_batch) in batch.iter_mut() { + let Some(height) = wallet_batch.core.synced_height else { + continue; + }; + if wallet_batch.core.synced_height_is_rewind { + continue; + } + let Some(info) = wm.get_wallet_info(wallet_id) else { + continue; + }; + let live = info.core_wallet.synced_height(); + if height > live { + tracing::debug!( + wallet_id = %hex::encode(wallet_id), + projected = height, + live, + "wallet-event adapter: dropping a sync-height advance projected before a rewind" + ); + wallet_batch.core.synced_height = None; + } + } +} + /// Drain `receiver`, project each [`WalletEvent`] into a /// [`CoreChangeSet`], and forward to the persister. Split out of /// [`spawn_wallet_event_adapter`] so the loop — not just the @@ -326,6 +408,7 @@ async fn run_wallet_event_adapter

( persister: Weak

, mut receiver: mpsc::UnboundedReceiver, sync_fault: Arc, + durable_cursors: DurableCursors, cancel: CancellationToken, ) where P: PlatformWalletPersistence + 'static, @@ -497,6 +580,12 @@ async fn run_wallet_event_adapter

( // it again costs nothing — whereas the reverse error, omitting a wallet // whose store did run, would leave a watermark free to advance past // rows nobody can account for. + // Take the durable-cursor lock for the whole commit and re-check each + // watermark against the live cursor under it: a rewind that ran since + // this batch was projected must win (see [`DurableCursors`]). The + // guard rides into the commit thread and is released when it returns. + let mut cursors = Arc::clone(&durable_cursors).lock_owned().await; + drop_watermarks_above_the_live_cursor(&wallet_manager, &mut batch).await; let batch_wallet_ids: Vec = batch .iter() .filter(|(_, wallet_batch)| { @@ -538,6 +627,12 @@ async fn run_wallet_event_adapter

( &freeze_for_commit, &mut settled, ); + // What each host now holds, for the next writer to measure + // against. + for (wallet_id, height) in &diag.persisted_by_wallet { + cursors.insert(*wallet_id, *height); + } + drop(cursors); // Hand the claim back out: the next chunk of a cancellation drain // inherits it instead of racing a fresh upgrade against the owner's // release. A panicking `commit_batch` drops it instead, and the @@ -821,6 +916,7 @@ fn commit_wallet

( // the adapter's own progress marker and holding it back would // re-drive work without making anything safer. core.synced_height = None; + core.synced_height_is_rewind = false; } let cs = PlatformWalletChangeSet { @@ -890,6 +986,7 @@ fn commit_wallet

( Ok(()) => { if let Some(h) = offered_height { diag.record_persisted(h); + diag.persisted_by_wallet.insert(wallet_id, h); } } Err(e) => { @@ -992,6 +1089,7 @@ fn fault_and_freeze( fn freeze_synced_height_if_faulted(core: &mut CoreChangeSet, persistence_faulted: bool) { if persistence_faulted { core.synced_height = None; + core.synced_height_is_rewind = false; } } @@ -4573,6 +4671,83 @@ mod tests { assert_eq!(unknown.synced_height, Some(7)); } + /// The commit-time re-check that closes the projection-to-commit window: + /// the adapter projects an advance to 1000, releases the manager, and a + /// rescan rewinds the wallet to 100 and stores that before the commit. + /// Under the durable-cursor lock the commit re-reads the live cursor and + /// drops the stale advance; a rewind-marked cursor and an advance at or + /// below the live cursor pass through (dashpay/platform#4302 review). + #[tokio::test] + async fn the_commit_drops_an_advance_projected_before_a_rewind() { + use crate::wallet::core::WalletGeneration; + use crate::wallet::identity::IdentityManager; + use key_wallet::test_utils::TestWalletContext; + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let ctx = TestWalletContext::new_random(); + let mut info = PlatformWalletInfo { + observed_input_conflicts: Default::default(), + core_wallet: ctx.managed_wallet, + generation: Arc::new(WalletGeneration::new()), + identity_manager: IdentityManager::new(), + tracked_asset_locks: BTreeMap::new(), + dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + }; + info.core_wallet.update_synced_height(1_000); + let manager = test_manager(); + let wallet_id = manager + .write() + .await + .insert_wallet(ctx.wallet, info) + .expect("insert wallet"); + + // Projected while the wallet was at 1000. + let projected = + super::build_core_changeset(&manager, &sync_height_event(wallet_id, 1_000)).await; + assert_eq!(projected.synced_height, Some(1_000)); + + // The rescan rewinds before the commit. + manager + .write() + .await + .get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(100); + + let batch_with = |core: CoreChangeSet| { + let mut batch: BTreeMap = BTreeMap::new(); + batch.entry(wallet_id).or_default().core = core; + batch + }; + let mut stale = batch_with(projected); + super::drop_watermarks_above_the_live_cursor(&manager, &mut stale).await; + assert_eq!( + stale[&wallet_id].core.synced_height, None, + "stale advance dropped" + ); + + let mut in_step = batch_with(CoreChangeSet { + synced_height: Some(100), + ..CoreChangeSet::default() + }); + super::drop_watermarks_above_the_live_cursor(&manager, &mut in_step).await; + assert_eq!(in_step[&wallet_id].core.synced_height, Some(100)); + + let mut rewind = batch_with(CoreChangeSet { + synced_height: Some(500), + synced_height_is_rewind: true, + ..CoreChangeSet::default() + }); + super::drop_watermarks_above_the_live_cursor(&manager, &mut rewind).await; + assert_eq!( + rewind[&wallet_id].core.synced_height, + Some(500), + "a rewind is never second-guessed" + ); + } + /// A record-bearing (non-watermark) event: carries /// `last_processed_height` but no `synced_height`, so it survives the /// freeze untouched. Used both as a sentinel to prove loop progress and @@ -4616,6 +4791,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -4676,6 +4852,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::new(AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), cancel, ) .await; @@ -4730,6 +4907,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::new(AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), cancel, )); @@ -4791,6 +4969,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -4845,6 +5024,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -4893,6 +5073,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -4940,6 +5121,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -4992,6 +5174,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5039,6 +5222,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5126,6 +5310,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5187,6 +5372,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5270,6 +5456,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5371,6 +5558,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5459,6 +5647,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5533,6 +5722,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -5582,6 +5772,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), + crate::changeset::DurableCursors::default(), cancel.clone(), )); @@ -6903,6 +7094,7 @@ mod tests { Arc::downgrade(&persister), rx, Arc::new(AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), CancellationToken::new(), )); handle.await.expect("the adapter exits on a closed channel"); diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 8c97da5a219..879394004b1 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -105,6 +105,13 @@ pub struct DashPayBackfillRecord { /// behind. `None` when nothing is owed. #[cfg_attr(feature = "serde", serde(skip))] pub unpersisted_cursor: Option, + /// In-memory only, like [`unpersisted_cursor`](Self::unpersisted_cursor): + /// the `(floor, rewound_from)` a rewind whose record round failed to + /// store still has to contribute. The retry folds it back in, so the + /// stored record keeps the backfill's real extent instead of the empty + /// one a retry with no rewind of its own would produce. + #[cfg_attr(feature = "serde", serde(skip))] + pub unpersisted_extent: Option<(u32, u32)>, } impl DashPayBackfillRecord { @@ -278,6 +285,7 @@ impl DashPayBackfillRecord { rewound_from, covered, unpersisted_cursor: None, + unpersisted_extent: None, }) } } diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 5234052c1ad..bbe2bb9f6ca 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -46,7 +46,9 @@ pub use changeset::{ }; pub use client_start_state::ClientStartState; pub use client_wallet_start_state::ClientWalletStartState; -pub use core_bridge::spawn_wallet_event_adapter; +pub use core_bridge::{ + spawn_wallet_event_adapter, spawn_wallet_event_adapter_with_durable_cursors, DurableCursors, +}; pub use dashpay_backfill::{DashPayBackfillCoveredContact, DashPayBackfillRecord}; pub use identity_manager_start_state::IdentityManagerStartState; pub use identity_scan_state::IdentityScanStateEntry; diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index b94b76273cd..4751db99e4c 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -866,6 +866,12 @@ impl PlatformWalletManager

{ return true; } info.core_wallet.update_synced_height(from_height); + // Not persisted here, so remember it as owed: the next DashPay + // backfill record round carries it as that round's cursor, and no + // coverage reaches disk beside a durable cursor this reset retracted + // in memory only (dashpay/platform#4302 review). + let owed = &mut info.dashpay_backfill.unpersisted_cursor; + *owed = Some(owed.map_or(from_height, |cursor| cursor.min(from_height))); tracing::info!( wallet_id = %hex::encode(wallet_id), from_height, diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 18906573c11..f9e8e8588ae 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -240,6 +240,10 @@ impl PlatformWalletManager

{ &platform_info, ); + // The cursor the host just handed back is the durable one: seed + // the shared record of it before any writer can race it. + let loaded_cursor = platform_info.core_wallet.metadata.synced_height; + if wallet_id != expected_wallet_id { load_error = Some(PlatformWalletError::WalletCreation(format!( "Persisted wallet id {} does not match recomputed id {}", @@ -343,7 +347,14 @@ impl PlatformWalletManager

{ Arc::clone(&persister_dyn), broadcaster, Arc::clone(&self.sync_fault), + Arc::clone(&self.durable_cursors), ); + // No manager-lock guard is alive here, so taking the cursor lock + // keeps its order (cursor lock, then manager). + self.durable_cursors + .lock() + .await + .insert(wallet_id, loaded_cursor); // Initialize the platform-address provider. If the snapshot // carried a slice for this wallet, restore it directly; diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index db1c3d77fe6..a952440fcc6 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -24,7 +24,9 @@ use tokio_util::sync::CancellationToken; use key_wallet_manager::WalletManager; -use crate::changeset::{spawn_wallet_event_adapter, PlatformWalletPersistence}; +use crate::changeset::{ + spawn_wallet_event_adapter_with_durable_cursors, DurableCursors, PlatformWalletPersistence, +}; use crate::events::{PlatformEventHandler, PlatformEventManager}; use crate::manager::dashpay_sync::DashPaySyncManager; use crate::manager::dpns_sync::DpnsSyncManager; @@ -439,6 +441,10 @@ pub struct PlatformWalletManager { /// failed / rescan pending" state rather than re-freezing silently on /// the next launch. pub(super) sync_fault: Arc, + /// The durable sync cursor each wallet's host last accepted, shared by + /// the wallet-event adapter and the DashPay rescan reconcile — the lock + /// that orders their cursor writes. See [`DurableCursors`]. + pub(super) durable_cursors: DurableCursors, /// Per-WALLET in-broadcast fence maps, handed to every /// [`WalletGeneration`](crate::wallet::core::WalletGeneration) registered /// under each id. @@ -500,16 +506,18 @@ impl PlatformWalletManager

{ // Host-visible hard sync-fault latch. The // adapter raises it the first time it freezes a durable watermark. let sync_fault = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let durable_cursors = DurableCursors::default(); // Spawn the wallet-event adapter that translates upstream // `WalletEvent`s into `CoreChangeSet`s and forwards them to // the persister. let event_adapter_cancel = CancellationToken::new(); - let event_adapter_join = spawn_wallet_event_adapter( + let event_adapter_join = spawn_wallet_event_adapter_with_durable_cursors( Arc::clone(&wallet_manager), Arc::downgrade(&persister), event_receiver, Arc::clone(&sync_fault), + Arc::clone(&durable_cursors), event_adapter_cancel.clone(), ); @@ -607,6 +615,7 @@ impl PlatformWalletManager

{ event_adapter_join: tokio::sync::Mutex::new(Some(event_adapter_join)), registry, sync_fault, + durable_cursors, in_broadcast_fences: std::sync::Mutex::new(std::collections::BTreeMap::new()), } } @@ -1189,6 +1198,7 @@ mod tests { Arc::new(NoopPersister) as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), )); mgr.wallets.rcu(|wallets| { let mut next = std::collections::BTreeMap::clone(wallets); diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 830aeaa9125..d8b0cbfe8f0 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -437,6 +437,7 @@ impl PlatformWalletManager

{ // treat re-registering an existing wallet as a benign no-op // instead of substring-matching the error text. Everything else // stays `WalletCreation`. + let created_cursor = platform_info.core_wallet.metadata.synced_height; let wallet_id = { let mut wm = self.wallet_manager.write().await; wm.insert_wallet(wallet, platform_info) @@ -569,7 +570,12 @@ impl PlatformWalletManager

{ persister_dyn, broadcaster, Arc::clone(&self.sync_fault), + Arc::clone(&self.durable_cursors), ); + self.durable_cursors + .lock() + .await + .insert(wallet_id, created_cursor); // Restore the platform-address provider from the slice read above — // the only area wired up today. `from_persisted` skips the live diff --git a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs index d82a3ecbacd..b4f4813facf 100644 --- a/packages/rs-platform-wallet/src/wallet/core/broadcast.rs +++ b/packages/rs-platform-wallet/src/wallet/core/broadcast.rs @@ -820,6 +820,7 @@ mod tests { as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), )) } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs b/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs index 2d9d48479b7..45a64e26aa6 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/dpns_marketplace.rs @@ -3776,6 +3776,7 @@ mod tests { asset_locks, persister, sync_fault: Arc::new(std::sync::atomic::AtomicBool::new(false)), + durable_cursors: crate::changeset::DurableCursors::default(), broadcaster, sdk_writer: Arc::new(super::super::sdk_writer::SdkWriter::new(sdk)), dpns_operation_gate: Arc::new(tokio::sync::Mutex::new(())), 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 5cec0bce323..8f934c5a12c 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 @@ -320,6 +320,11 @@ pub struct IdentityWallet { /// never landed. The DashPay rescan reconcile consults it before it /// stores a backfill record with a lowered cursor. pub(crate) sync_fault: Arc, + /// The manager's shared record of each wallet's durable sync cursor, + /// also the lock that orders cursor writes between the wallet-event + /// adapter and the DashPay rescan reconcile. See + /// [`DurableCursors`](crate::changeset::DurableCursors). + pub(crate) durable_cursors: crate::changeset::DurableCursors, /// Broadcaster for DashPay payment transactions. Distinct from the /// asset-lock broadcaster — the asset-lock manager is always /// `SpvBroadcaster`-pinned, while this one picks the broadcaster @@ -362,6 +367,7 @@ impl Clone for IdentityWallet { asset_locks: Arc::clone(&self.asset_locks), persister: self.persister.clone(), sync_fault: Arc::clone(&self.sync_fault), + durable_cursors: Arc::clone(&self.durable_cursors), broadcaster: Arc::clone(&self.broadcaster), sdk_writer: Arc::clone(&self.sdk_writer), dpns_operation_gate: Arc::clone(&self.dpns_operation_gate), diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index c64b532b481..31d889af391 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -137,6 +137,11 @@ impl DashPayView<'_, B> { use crate::changeset::{CoreChangeSet, PlatformWalletChangeSet}; use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + // The durable-cursor lock first, then the manager (see + // `DurableCursors`): held until this pass's store returns, so no + // adapter commit can interleave between deciding a cursor and storing + // it. + let mut durable_cursors = self.durable_cursors.lock().await; let mut wm = self.wallet_manager.write().await; let Some(info) = wm.get_wallet_info_mut(&self.wallet_id) else { return Ok(None); @@ -294,13 +299,32 @@ impl DashPayView<'_, B> { .iter() .any(|(o, c)| (o, c) == (owner, contact)) }); - record.record_pass(synced_height, floor, to_mark.iter().copied()); + // This pass's extent, widened by one a failed earlier round still owes. + let extent = match ( + floor.map(|floor| (floor, synced_height)), + record.unpersisted_extent.take(), + ) { + (Some((f1, r1)), Some((f2, r2))) => Some((f1.min(f2), r1.max(r2))), + (this_pass, owed) => this_pass.or(owed), + }; + match extent { + Some((extent_floor, rewound_from)) => { + record.record_pass(rewound_from, Some(extent_floor), to_mark.iter().copied()) + } + None => record.record_pass(synced_height, None, to_mark.iter().copied()), + }; let owed = record.unpersisted_cursor.take(); - let cursor_to_write = match (floor, owed) { + let needed_cursor = match (floor, owed) { (Some(floor), Some(owed)) => Some(floor.min(owed)), (Some(floor), None) => Some(floor), (None, owed) => owed, }; + // Never write a cursor at or above the one the host already holds: + // coverage is safe beside any lower durable cursor, and writing a + // higher one would claim rows the adapter has not persisted yet. + let durable = durable_cursors.get(&self.wallet_id).copied(); + let cursor_to_write = + needed_cursor.filter(|cursor| durable.is_none_or(|durable| *cursor < durable)); let cursor_now = floor.unwrap_or(synced_height); let faulted = self.sync_fault.load(std::sync::atomic::Ordering::Relaxed); let stored = if faulted { @@ -309,6 +333,7 @@ impl DashPayView<'_, B> { let changeset = PlatformWalletChangeSet { core: cursor_to_write.map(|cursor| CoreChangeSet { synced_height: Some(cursor), + synced_height_is_rewind: true, ..CoreChangeSet::default() }), dashpay_backfill: Some(record.clone()), @@ -316,6 +341,11 @@ impl DashPayView<'_, B> { }; self.persister.store(changeset).map_err(Some) }; + if stored.is_ok() { + if let Some(cursor) = cursor_to_write { + durable_cursors.insert(self.wallet_id, cursor); + } + } if let Err(error) = stored { match error { Some(error) => tracing::warn!( @@ -332,7 +362,8 @@ impl DashPayView<'_, B> { } info.dashpay_backfill = previous_record; info.dashpay_backfill.unpersisted_cursor = - Some(cursor_to_write.map_or(cursor_now, |cursor| cursor.min(cursor_now))); + Some(needed_cursor.map_or(cursor_now, |cursor| cursor.min(cursor_now))); + info.dashpay_backfill.unpersisted_extent = extent; for (owner, contact, _) in &to_mark { if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { managed.dashpay_rescan_triggered_mut().remove(contact); @@ -2970,18 +3001,23 @@ mod tests { .retain(|o, c| (o, c) != (&owner, &contact)); } + /// Move the wallet's cursor to `height` the way a scan does: in memory, + /// and — as the event adapter would once it commits the advance — on the + /// host, recorded in the manager's durable-cursor map. async fn set_synced_height( manager: &Arc>, wallet_id: WalletId, height: u32, ) { use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + let mut durable = manager.durable_cursors.lock().await; let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); let mut wm = wallet.identity().wallet_manager.write().await; wm.get_wallet_info_mut(&wallet_id) .expect("info") .core_wallet .update_synced_height(height); + durable.insert(wallet_id, height); } async fn synced_height( @@ -4100,6 +4136,19 @@ mod tests { let record = round.dashpay_backfill.as_ref().unwrap(); assert_eq!(record.covered_from(&owner, &first), Some(100)); assert_eq!(record.covered_from(&owner, &later), Some(200)); + assert_eq!( + (record.floor, record.rewound_from), + (100, 1_000), + "the retry keeps the failed rewind's extent" + ); + assert!( + record.is_pending(100), + "the backfill still reads as climbing" + ); + assert!( + round.core.as_ref().unwrap().synced_height_is_rewind, + "the owed cursor rides as a rewind" + ); } let wm = manager.wallet_manager.read().await; assert_eq!( @@ -4216,6 +4265,119 @@ mod tests { assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); } + /// The host lags memory (a healthy backlog: the adapter has not yet + /// committed the advances that took memory to 1000). A rewind to 800 + /// must not write 800 — that would claim the rows between the host's + /// cursor and 800 are on disk when they are still queued. The host's + /// cursor is already below the floor, so the record is stored with no + /// cursor, and nothing records a durable height that was never written + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_rewind_above_the_hosts_cursor_writes_no_cursor() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 800, 800).await; + set_synced_height(&manager, wallet_id, 100).await; + // Memory advances to 1000; the host still holds 100. + { + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let mut wm = wallet.identity().wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(1_000); + } + persister.stores.lock().unwrap().clear(); + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + Some(800), + "memory rewinds to the floor" + ); + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + assert!( + round.core.is_none(), + "the host's 100 is already below the floor; 800 must not be written" + ); + assert_eq!( + round + .dashpay_backfill + .as_ref() + .unwrap() + .covered_from(&owner, &contact), + Some(800) + ); + } + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(), + Some(100), + "the recorded durable cursor is still what the host holds" + ); + } + + /// `spv_rescan_filters_blocking` lowers the cursor in memory only. The + /// next record round must carry that reset, or it stores coverage + /// justified by a rewind the host never saw (dashpay/platform#4302 + /// review): here a contact forward-covered at the reset cursor produces + /// a round with no rewind of its own, which now carries the owed 100. + #[tokio::test] + async fn a_manual_rescan_reset_rides_the_next_record_round() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + set_synced_height(&manager, wallet_id, 1_000).await; + { + let manager = Arc::clone(&manager); + tokio::task::spawn_blocking(move || { + assert!(manager.spv_rescan_filters_blocking(&wallet_id, 100)); + }) + .await + .expect("manual rescan"); + } + assert_eq!(synced_height(&manager, wallet_id).await, 100); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 200, 200).await; + persister.stores.lock().unwrap().clear(); + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None, + "the contact is forward-covered at the reset cursor" + ); + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + let core = round.core.as_ref().expect("the owed reset rides the round"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + assert_eq!( + round + .dashpay_backfill + .as_ref() + .unwrap() + .covered_from(&owner, &contact), + Some(200) + ); + } + async fn synced_height_of( manager: &Arc>, wallet_id: WalletId, @@ -8588,6 +8750,7 @@ mod tests { asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), sync_fault: Arc::clone(&real.sync_fault), + durable_cursors: Arc::clone(&real.durable_cursors), broadcaster: Arc::new(GatedRejectingBroadcaster { entered, release }), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), @@ -8628,6 +8791,7 @@ mod tests { asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), sync_fault: Arc::clone(&real.sync_fault), + durable_cursors: Arc::clone(&real.durable_cursors), broadcaster: Arc::new(GatedBroadcaster { entered, release }), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), @@ -8649,6 +8813,7 @@ mod tests { asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), sync_fault: Arc::clone(&real.sync_fault), + durable_cursors: Arc::clone(&real.durable_cursors), broadcaster: Arc::new(AcceptingBroadcaster), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), @@ -8685,6 +8850,7 @@ mod tests { asset_locks: Arc::clone(&real.asset_locks), persister: real.persister.clone(), sync_fault: Arc::clone(&real.sync_fault), + durable_cursors: Arc::clone(&real.durable_cursors), broadcaster: Arc::new(RejectingBroadcaster), sdk_writer: Arc::clone(&real.sdk_writer), dpns_operation_gate: Arc::clone(&real.dpns_operation_gate), diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index b43db02d794..2bd8f185fe5 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -671,7 +671,7 @@ impl PlatformWallet { /// the wallet. The wallet must have been inserted into the WalletManager /// before calling this. // Every argument is a shared handle the manager owns; a builder would - // only move the same eight names behind a struct. + // only move the same nine names behind a struct. #[allow(clippy::too_many_arguments)] pub(crate) fn new( sdk: Arc, @@ -682,6 +682,7 @@ impl PlatformWallet { persister: Arc, broadcaster: Arc, sync_fault: Arc, + durable_cursors: crate::changeset::DurableCursors, ) -> Self { // Build the per-wallet persister handle once and share it with // the sub-wallets that need to queue their own changesets @@ -728,6 +729,7 @@ impl PlatformWallet { asset_locks: Arc::clone(&asset_locks), persister: wallet_persister.clone(), sync_fault, + durable_cursors, broadcaster: dashpay_broadcaster, // DashPay write helper: forwards to the live SDK, erasing its // generic write signatures behind concrete by-value methods. diff --git a/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs b/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs index be4a9916b59..841393e7ea8 100644 --- a/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/provider_ecdsa_key_tests.rs @@ -46,6 +46,7 @@ async fn platform_wallet() -> PlatformWallet { Arc::new(NoopTestPersister) as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), ) } diff --git a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs index 976f352536e..b207cacf648 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs @@ -189,6 +189,7 @@ where persister as Arc, Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), ) } From af72d09cd7a75a203cbed5bb1385517cedbc9fd7 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Mon, 28 Sep 2026 19:17:09 -0700 Subject: [PATCH 10/43] fix(platform-wallet): order queued sync-height advances by emission, key coverage per receival account Third review round on the DashPay backfill record (dashpay/platform#4302): - Rewind barrier. A rewind emits no event, so a SyncHeightAdvanced the engine queued before it looked current once the rescan climbed back past it, and could reach the host ahead of the rescan's transaction rows. The engine emits each advance straight after PlatformWalletInfo::update_synced_height under the manager write lock; that hook now counts emissions, a rewind (reconcile or manual rescan reset) snapshots how many are in flight and bumps an epoch, and the adapter drops exactly those as it projects them. An advance projected before the rewind is stripped at commit by its superseded epoch, and a batch spanning a rewind keeps the rescan's height instead of max-merging the stale one. - Coverage is keyed on the receival account (owner, contact, account_index), so a second receiving account for a covered pair is rewound for instead of inheriting the first one's coverage. Covered entries grow to 72 bytes (FFI struct, JNI packing, Kotlin entry size). - The durable cursor is seeded in the same critical section that publishes the wallet (registration and load), so an adapter commit or reconcile can no longer be overwritten by a stale seed. - A host with no changeset callback never receives the backfill record, on record-only rounds too: it stores no cursor a record could vouch for. - record_pass decides "first rewind" off the extent, not the cover set, so pruning the last covered contact no longer lets the next rewind replace the extent it is still climbing. Co-Authored-By: Claude Opus 5.5 --- .../dashsdk/ffi/NativePersistenceBridge.kt | 9 +- .../PlatformWalletPersistenceHandler.kt | 6 +- .../persistence/entities/WalletEntity.kt | 5 +- .../PlatformWalletPersistenceHandlerTest.kt | 2 +- .../src/core_wallet_types.rs | 7 +- .../rs-platform-wallet-ffi/src/persistence.rs | 72 +++- .../src/changeset/core_bridge.rs | 354 ++++++++++++++++-- .../src/changeset/dashpay_backfill.rs | 208 +++++++--- .../src/manager/accessors.rs | 4 + .../rs-platform-wallet/src/manager/load.rs | 22 +- .../src/manager/wallet_lifecycle.rs | 92 ++++- .../rs-platform-wallet/src/test_support.rs | 5 + .../rs-platform-wallet/src/wallet/apply.rs | 1 + .../wallet/asset_lock/sync/reconstruction.rs | 1 + .../src/wallet/asset_lock/sync/recovery.rs | 1 + .../identity/network/contact_requests.rs | 1 + .../src/wallet/identity/network/payments.rs | 163 ++++++-- .../src/wallet/platform_wallet.rs | 5 + .../src/wallet/platform_wallet_traits.rs | 9 + .../rs-unified-sdk-jni/src/persistence.rs | 15 +- 20 files changed, 815 insertions(+), 167 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt index 3ed10cff7d1..0e38b6aaa46 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt @@ -447,9 +447,10 @@ abstract class NativePersistenceBridge { * stored. Descriptor `([BII[BI)I`. * * Whole-record semantics: replace what the wallet row holds, never - * merge. [covered] is ONE flat `byte[]` of `68 * coveredCount` bytes — - * per contact the owner identity id (32), the contact identity id (32) - * and the height it is covered from (u32, little-endian) — the same + * merge. [covered] is ONE flat `byte[]` of `72 * coveredCount` bytes — + * per receival account the owner identity id (32), the contact identity + * id (32), the account index (u32) and the height it is covered from + * (u32), both little-endian — the same * packing the sweep slot uses for its txids. Store it as an opaque blob * and hand it back unchanged on [WalletRestoreData.dashPayBackfillCovered]; * only native reads it. @@ -1076,7 +1077,7 @@ class WalletRestoreData( /** * The record's cover set, exactly as [NativePersistenceBridge.onWalletChangesetDashPayBackfill] - * delivered it: `68` bytes per covered contact. Native re-packs it into + * delivered it: `72` bytes per covered receival account. Native re-packs it into * a `DashPayBackfillCoveredContactFFI` array; a blob that is not a whole * number of entries is read as no record. */ diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt index 2bc88bc8134..dbaa458df17 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandler.kt @@ -4112,11 +4112,11 @@ class PlatformWalletPersistenceHandler( internal const val CAPABILITY_DPNS_NAME_STATES: Long = 0x100 internal const val CAPABILITY_TRACKED_ASSET_LOCKS: Long = 0x200 /** - * Bytes per contact in the opaque cover set + * Bytes per receival account in the opaque cover set * [onWalletChangesetDashPayBackfill] delivers: owner id (32), - * contact id (32), covered-from height (4). + * contact id (32), account index (4), covered-from height (4). */ - internal const val DASHPAY_BACKFILL_COVERED_ENTRY_SIZE: Int = 32 + 32 + 4 + internal const val DASHPAY_BACKFILL_COVERED_ENTRY_SIZE: Int = 32 + 32 + 4 + 4 internal const val CAPABILITY_CORE_SWEEP_REMOVAL: Long = NativePersistenceBridge.CAPABILITY_CORE_SWEEP_REMOVAL diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt index 2e0873d63ba..06682b2c724 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/entities/WalletEntity.kt @@ -76,8 +76,9 @@ data class WalletEntity( * record: native re-runs the backfill once and writes one. `floor` is the * lowest height the backfill rewound to, `rewoundFrom` the cursor it * rewound from — the scan is complete once `syncedHeight` climbs back - * past it — and `covered` the opaque cover set (68 bytes per contact: - * owner id, contact id, covered-from height) only native reads. + * past it — and `covered` the opaque cover set (72 bytes per receival + * account: owner id, contact id, account index, covered-from height) + * only native reads. */ val dashPayBackfillFloor: Int? = null, val dashPayBackfillRewoundFrom: Int? = null, diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt index bd68f084b97..cc6dede2141 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/PlatformWalletPersistenceHandlerTest.kt @@ -6838,7 +6838,7 @@ class PlatformWalletPersistenceHandlerTest { } /** One packed cover-set entry: owner id ‖ contact id ‖ covered-from (LE u32). */ private fun backfillEntry(owner: Byte, contact: Byte, coveredFrom: Int): ByteArray = - ByteArray(32) { owner } + ByteArray(32) { contact } + byteArrayOf( + ByteArray(32) { owner } + ByteArray(32) { contact } + ByteArray(4) + byteArrayOf( (coveredFrom and 0xFF).toByte(), ((coveredFrom shr 8) and 0xFF).toByte(), ((coveredFrom shr 16) and 0xFF).toByte(), diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs index 74f3f12d8ae..c8b68c8ad65 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs @@ -126,13 +126,17 @@ pub struct DashPayBackfillCoveredContactFFI { pub owner_identity_id: [u8; 32], /// The contact whose payments the receival account collects. pub contact_identity_id: [u8; 32], + /// The receival account's index — coverage is per receival account, not + /// per relationship. + pub account_index: u32, /// Lowest height the scan is guaranteed to test with this contact's /// addresses watched. pub covered_from: u32, } /// Project a backfill record's cover set into its C mirrors for the -/// extension slot, in record order (sorted by `(owner, contact)`). +/// extension slot, in record order (sorted by `(owner, contact, +/// account_index)`). pub fn build_dashpay_backfill_covered_for_callback( record: &platform_wallet::changeset::DashPayBackfillRecord, ) -> Vec { @@ -142,6 +146,7 @@ pub fn build_dashpay_backfill_covered_for_callback( .map(|entry| DashPayBackfillCoveredContactFFI { owner_identity_id: entry.owner.to_buffer(), contact_identity_id: entry.contact.to_buffer(), + account_index: entry.account_index, covered_from: entry.covered_from, }) .collect() diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 20760bd1afb..46c752945bf 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -2235,6 +2235,15 @@ impl PlatformWalletPersistence for FFIPersister { // after the core changeset callback so the lowered cursor a rescan // round carries is staged before the record that vouches for it. // Whole-record semantics: the host replaces what it holds. + // + // A host that registered no changeset callback stores no cursor at + // all, so no record can vouch for one it holds: it never receives + // the record, not even on a record-only round. Withholding only the + // cursor-bearing round is not enough — that round still returns + // `Ok`, the reconcile records its cursor as durable, and a later + // record-only round would then deliver coverage beside a host cursor + // that never moved. + let host_stores_cursors = self.callbacks.on_persist_wallet_changeset_fn.is_some(); let backfill_cursor_owed = changeset .core .as_ref() @@ -2242,7 +2251,7 @@ impl PlatformWalletPersistence for FFIPersister { if let Some(record) = changeset .dashpay_backfill .as_ref() - .filter(|_| !backfill_cursor_owed || cursor_delivered) + .filter(|_| host_stores_cursors && (!backfill_cursor_owed || cursor_delivered)) { if let Some(cb) = self.wallet_dashpay_backfill_callback { let covered = build_dashpay_backfill_covered_for_callback(record); @@ -5341,12 +5350,13 @@ fn decode_dashpay_backfill( .map(|c| DashPayBackfillCoveredContact { owner: Identifier::from(c.owner_identity_id), contact: Identifier::from(c.contact_identity_id), + account_index: c.account_index, covered_from: c.covered_from, }) .collect(); - // Canonical order and one entry per pair, whatever the host stored. - covered.sort_by_key(|entry| (entry.owner, entry.contact)); - covered.dedup_by(|a, b| (a.owner, a.contact) == (b.owner, b.contact)); + // Canonical order and one entry per account, whatever the host stored. + covered.sort_by_key(DashPayBackfillCoveredContact::key); + covered.dedup_by(|a, b| a.key() == b.key()); DashPayBackfillRecord { floor: entry.dashpay_backfill_floor, rewound_from: entry.dashpay_backfill_rewound_from, @@ -8461,11 +8471,13 @@ mod tests { ( dpp::prelude::Identifier::from([9u8; 32]), dpp::prelude::Identifier::from([2u8; 32]), + 0, 2_300_000, ), ( dpp::prelude::Identifier::from([1u8; 32]), dpp::prelude::Identifier::from([3u8; 32]), + 0, 2_167_092, ), ], @@ -8539,8 +8551,10 @@ mod tests { /// for, and must not reach the host when that cursor did not — a host /// without atomic rounds would keep the coverage over its old cursor /// (dashpay/platform#4302 review). Withheld when the changeset callback - /// fails or is absent for a round that carries a cursor; delivered for a - /// record-only round. + /// fails for a round that carries a cursor, and on every round — + /// record-only ones included — for a host with no changeset callback, + /// which holds no cursor a record could vouch for. Delivered for a + /// record-only round on a host that stores cursors. #[test] fn a_backfill_record_is_withheld_when_its_cursor_did_not_reach_the_host() { use platform_wallet::changeset::{CoreChangeSet, DashPayBackfillRecord}; @@ -8557,6 +8571,13 @@ mod tests { ) -> i32 { 1 } + unsafe extern "C" fn accept_changeset( + _ctx: *mut c_void, + _wallet_id: *const u8, + _changeset: *const WalletChangeSetFFI, + ) -> i32 { + 0 + } unsafe extern "C" fn count_backfill( ctx: *mut c_void, _wallet_id: *const u8, @@ -8605,25 +8626,34 @@ mod tests { assert!(rejected.store([1u8; 32], rewind_round()).is_err()); assert_eq!(sink.backfills.load(Ordering::SeqCst), 0); - // The host takes no cursors at all: a record over one is meaningless. + let record_only_round = || PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }; + + // The host takes no cursors at all: a record over one is meaningless, + // on the rewind round and on every record-only round after it. let sink = Sink::default(); let cursorless = persister(&sink, None); cursorless .store([1u8; 32], rewind_round()) .expect("nothing failed"); + cursorless + .store([1u8; 32], record_only_round()) + .expect("record-only round"); assert_eq!(sink.backfills.load(Ordering::SeqCst), 0); - // A record-only round (no cursor to pair with) is delivered. - cursorless - .store( - [1u8; 32], - PlatformWalletChangeSet { - dashpay_backfill: Some(DashPayBackfillRecord::default()), - ..PlatformWalletChangeSet::default() - }, - ) + // A host that stores cursors gets the record with its cursor, and a + // record-only round (no cursor to pair with) on its own. + let sink = Sink::default(); + let accepting = persister(&sink, Some(accept_changeset)); + accepting + .store([1u8; 32], rewind_round()) + .expect("rewind round"); + accepting + .store([1u8; 32], record_only_round()) .expect("record-only round"); - assert_eq!(sink.backfills.load(Ordering::SeqCst), 1); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 2); } /// The restore side of dashpay/platform#4302: a wallet-restore entry @@ -8657,16 +8687,19 @@ mod tests { DashPayBackfillCoveredContactFFI { owner_identity_id: [9u8; 32], contact_identity_id: [2u8; 32], + account_index: 0, covered_from: 2_300_000, }, DashPayBackfillCoveredContactFFI { owner_identity_id: [1u8; 32], contact_identity_id: [3u8; 32], + account_index: 0, covered_from: 2_167_092, }, DashPayBackfillCoveredContactFFI { owner_identity_id: [9u8; 32], contact_identity_id: [2u8; 32], + account_index: 0, covered_from: 2_999_999, }, ]; @@ -8687,11 +8720,13 @@ mod tests { ( dpp::prelude::Identifier::from([1u8; 32]), dpp::prelude::Identifier::from([3u8; 32]), + 0, 2_167_092, ), ( dpp::prelude::Identifier::from([9u8; 32]), dpp::prelude::Identifier::from([2u8; 32]), + 0, 2_300_000, ), ], @@ -8700,7 +8735,8 @@ mod tests { assert_eq!( decoded.covered_from( &dpp::prelude::Identifier::from([9u8; 32]), - &dpp::prelude::Identifier::from([2u8; 32]) + &dpp::prelude::Identifier::from([2u8; 32]), + 0 ), Some(2_300_000), "the first of a duplicated pair wins" diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index fce84d5dd0d..6a61ebad2c6 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -286,13 +286,82 @@ where /// durable cursor past rows the adapter had not yet persisted /// (dashpay/platform#4302 review). Both take this lock before they read the /// wallet's in-memory cursor and hold it until their `store()` returns; the -/// adapter strips an advance above the in-memory cursor at commit time, and -/// the reconcile never writes a cursor at or above the recorded durable one. +/// adapter strips, at commit time, an advance a rewind has overtaken (see +/// [`RewindBarrier`]) or one above the in-memory cursor, and the reconcile +/// never writes a cursor at or above the recorded durable one. /// /// Lock order is this mutex, then the wallet manager — never the reverse. /// Seeded with the cursor each wallet was loaded or created with. pub type DurableCursors = Arc>>; +/// Orders one wallet's `SyncHeightAdvanced` events against in-memory rewinds +/// of its scan cursor, so an advance the engine emitted *before* a rewind is +/// never persisted after it. +/// +/// A rewind emits nothing (the engine only reports forward moves), so the +/// persistence channel carries no marker of where one happened, and a +/// height comparison cannot place it either: once the rescan has climbed +/// back past a still-queued pre-rewind advance, that advance looks current, +/// and storing it would put the durable cursor above transaction rows the +/// rescan emitted behind it in the channel (dashpay/platform#4302 review). +/// +/// The barrier counts instead. The engine emits a `SyncHeightAdvanced` only +/// right after calling this wallet's `update_synced_height` hook, under the +/// same manager write lock, so the hook counts emissions exactly +/// ([`note_emitted`](Self::note_emitted)), and the adapter counts them back +/// down as it projects them ([`take_next`](Self::take_next)). A rewind — +/// made under that same write lock — snapshots how many are still in flight +/// ([`arm`](Self::arm)): exactly those were emitted before it, and the +/// adapter drops them as they arrive. Every later advance is tagged with the +/// rewind epoch it was projected in, and a commit strips one whose epoch a +/// rewind has since superseded, which covers an advance the adapter had +/// already projected when the rewind ran. +/// +/// Miscounting fails safe: an extra count only drops a later, genuine +/// advance, leaving the durable cursor behind the scan until the next one. +#[derive(Debug, Default)] +pub struct RewindBarrier { + /// `SyncHeightAdvanced` events emitted for this wallet that the adapter + /// has not projected yet. + in_flight: u64, + /// How many of the next projected advances were emitted before the + /// latest rewind. + stale: u64, + /// Bumped by every rewind. + epoch: u64, +} + +impl RewindBarrier { + /// The engine is about to emit a `SyncHeightAdvanced` for this wallet. + pub(crate) fn note_emitted(&mut self) { + self.in_flight = self.in_flight.saturating_add(1); + } + + /// The scan cursor was just lowered in memory: every advance still in + /// flight predates it. + pub(crate) fn arm(&mut self) { + self.stale = self.in_flight; + self.epoch = self.epoch.wrapping_add(1); + } + + /// Classify the next advance the adapter projects: `None` when it was + /// emitted before the latest rewind, else the epoch it belongs to. + pub(crate) fn take_next(&mut self) -> Option { + self.in_flight = self.in_flight.saturating_sub(1); + if self.stale > 0 { + self.stale -= 1; + None + } else { + Some(self.epoch) + } + } + + /// The current rewind epoch. + pub(crate) fn epoch(&self) -> u64 { + self.epoch + } +} + /// [`spawn_wallet_event_adapter`] sharing `durable_cursors` with the other /// writer of the durable sync cursor (the DashPay rescan reconcile). The /// manager uses this form; see [`DurableCursors`]. @@ -317,11 +386,67 @@ where )) } -/// Strip, from each wallet's batch, a sync-height advance above that -/// wallet's in-memory cursor. Such an advance was projected before a rewind -/// lowered the cursor; storing it now would claim coverage the rescan has -/// not produced. Called with [`DurableCursors`] held, right before commit, -/// so no rewind can slip in between this check and the store. +/// Project one event and fold it into its wallet's entry of `batch`, in +/// channel order. +/// +/// A `SyncHeightAdvanced` is first classified against the wallet's +/// [`RewindBarrier`]: one emitted before the latest rewind contributes no +/// height, and the first advance of a newer rewind epoch replaces the +/// batch's height instead of max-merging with it — a pre-rewind height is +/// higher than the rescan's by construction, and the max would keep it. +async fn project_event_into_batch( + wallet_manager: &Arc>>, + event: &WalletEvent, + batch: &mut BTreeMap, +) { + let wallet_id = event.wallet_id(); + let advance_epoch = match event { + WalletEvent::SyncHeightAdvanced { .. } => Some( + wallet_manager + .write() + .await + .get_wallet_info_mut(&wallet_id) + .map_or(Some(0), |info| info.rewind_barrier.take_next()), + ), + _ => None, + }; + let mut core = build_core_changeset(wallet_manager, event).await; + let asset_locks = reconstruct_asset_locks_for_event(wallet_manager, event).await; + let payments = sent_payment_verdicts(wallet_manager, event).await; + let entry = batch.entry(wallet_id).or_default(); + match advance_epoch { + Some(None) => { + tracing::debug!( + wallet_id = %hex::encode(wallet_id), + projected = ?core.synced_height, + "wallet-event adapter: dropping a sync-height advance emitted before a rewind" + ); + core.synced_height = None; + core.synced_height_is_rewind = false; + } + Some(Some(epoch)) if core.synced_height.is_some() => { + if entry.advance_epoch.is_some_and(|held| held != epoch) { + entry.core.synced_height = None; + entry.core.synced_height_is_rewind = false; + } + entry.advance_epoch = Some(epoch); + } + _ => {} + } + entry.core.merge(core); + entry.asset_locks.merge(asset_locks); + // Last-write-wins per `(owner, txid)`: a transaction swept and then + // reinstated inside one drain reaches the store as the verdict the drain + // ended on, never as two rows. + merge_payment_overlays(&mut entry.payments, payments); +} + +/// Strip, from each wallet's batch, a sync-height advance a rewind has +/// overtaken: one projected in a [`RewindBarrier`] epoch a rewind has since +/// superseded, or one above the wallet's in-memory cursor. Storing either +/// would claim coverage the rescan has not produced. Called with +/// [`DurableCursors`] held, right before commit, so no rewind can slip in +/// between this check and the store. async fn drop_watermarks_above_the_live_cursor( wallet_manager: &Arc>>, batch: &mut BTreeMap, @@ -339,6 +464,21 @@ async fn drop_watermarks_above_the_live_cursor( let Some(info) = wm.get_wallet_info(wallet_id) else { continue; }; + // Projected in a rewind epoch that has since been superseded: the + // advance predates the rewind whatever its height, even once the + // rescan has climbed back past it. + if wallet_batch + .advance_epoch + .is_some_and(|epoch| epoch != info.rewind_barrier.epoch()) + { + tracing::debug!( + wallet_id = %hex::encode(wallet_id), + projected = height, + "wallet-event adapter: dropping a sync-height advance a rewind overtook" + ); + wallet_batch.core.synced_height = None; + continue; + } let live = info.core_wallet.synced_height(); if height > live { tracing::debug!( @@ -496,20 +636,11 @@ async fn run_wallet_event_adapter

( let mut batch: BTreeMap = BTreeMap::new(); let mut closed = false; - { - let wallet_id = event.wallet_id(); - // For events that need to consult per-wallet state (today only - // `TransactionInstantLocked`, which checks finality before - // recording the IS lock), `build_core_changeset` takes a brief - // read lock on the manager. - let core = build_core_changeset(&wallet_manager, &event).await; - let asset_locks = reconstruct_asset_locks_for_event(&wallet_manager, &event).await; - let payments = sent_payment_verdicts(&wallet_manager, &event).await; - let entry = batch.entry(wallet_id).or_default(); - entry.core.merge(core); - entry.asset_locks.merge(asset_locks); - merge_payment_overlays(&mut entry.payments, payments); - } + // For events that need to consult per-wallet state (today only + // `TransactionInstantLocked`, which checks finality before recording + // the IS lock), `build_core_changeset` takes a brief read lock on the + // manager. + project_event_into_batch(&wallet_manager, &event, &mut batch).await; // Fold in whatever else is already buffered. `try_recv` never waits, // so this drains the backlog at projection speed and stops as soon as @@ -518,18 +649,7 @@ async fn run_wallet_event_adapter

( while folded < ADAPTER_STORE_BATCH_LIMIT { match receiver.try_recv() { Ok(event) => { - let wallet_id = event.wallet_id(); - let core = build_core_changeset(&wallet_manager, &event).await; - let asset_locks = - reconstruct_asset_locks_for_event(&wallet_manager, &event).await; - let payments = sent_payment_verdicts(&wallet_manager, &event).await; - let entry = batch.entry(wallet_id).or_default(); - entry.core.merge(core); - entry.asset_locks.merge(asset_locks); - // Last-write-wins per `(owner, txid)`: a transaction swept - // and then reinstated inside one drain reaches the store as - // the verdict the drain ended on, never as two rows. - merge_payment_overlays(&mut entry.payments, payments); + project_event_into_batch(&wallet_manager, &event, &mut batch).await; folded += 1; } Err(TryRecvError::Empty) => break, @@ -815,6 +935,7 @@ fn commit_wallet

( mut core, asset_locks, payments, + advance_epoch: _, } = wallet_batch; { // Sent-payment verdicts reach a host only through the payment-overlay @@ -1115,6 +1236,10 @@ struct WalletBatch { /// identity blob is instead patched row-by-row by the persister, inside /// its own write transaction — see the SQLite backend's `dashpay` module. payments: PaymentOverlay, + /// The [`RewindBarrier`] epoch of the advance in `core.synced_height`, + /// checked again at commit: an advance a rewind has since superseded is + /// stripped there. `None` while the batch carries no advance. + advance_epoch: Option, } /// Rebuild missing tracked asset locks from the records an event @@ -3932,6 +4057,7 @@ mod contact_watch_only_projection_tests { tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); @@ -4629,6 +4755,7 @@ mod tests { tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; info.core_wallet.update_synced_height(1_000); let manager = test_manager(); @@ -4693,6 +4820,7 @@ mod tests { tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; info.core_wallet.update_synced_height(1_000); let manager = test_manager(); @@ -4748,6 +4876,158 @@ mod tests { ); } + /// A wallet at `synced_height` with the manager's lossless persistence + /// receiver installed, so advances go through the engine's real + /// emission path (and the [`RewindBarrier`] hook counting it). + async fn engine_wallet( + synced_height: u32, + ) -> ( + Arc>>, + tokio::sync::mpsc::UnboundedReceiver, + WalletId, + ) { + use crate::wallet::core::WalletGeneration; + use crate::wallet::identity::IdentityManager; + use key_wallet::test_utils::TestWalletContext; + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let ctx = TestWalletContext::new_random(); + let mut info = PlatformWalletInfo { + observed_input_conflicts: Default::default(), + core_wallet: ctx.managed_wallet, + generation: Arc::new(WalletGeneration::new()), + identity_manager: IdentityManager::new(), + tracked_asset_locks: BTreeMap::new(), + dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), + }; + info.core_wallet.update_synced_height(synced_height); + let manager = test_manager(); + let receiver = manager + .write() + .await + .take_persistence_receiver() + .expect("persistence receiver"); + let wallet_id = manager + .write() + .await + .insert_wallet(ctx.wallet, info) + .expect("insert wallet"); + (manager, receiver, wallet_id) + } + + /// The scan cursor as the engine advances it: in memory, and emitted. + async fn engine_advance( + manager: &Arc>>, + wallet_id: WalletId, + height: u32, + ) { + use key_wallet_manager::WalletInterface; + manager + .write() + .await + .update_wallet_synced_height(&wallet_id, height); + } + + /// The scan cursor as `reconcile_dashpay_rescan` lowers it: in memory + /// only, arming the barrier under the same write lock. + async fn rewind( + manager: &Arc>>, + wallet_id: WalletId, + floor: u32, + ) { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + let mut wm = manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.core_wallet.update_synced_height(floor); + info.rewind_barrier.arm(); + } + + async fn project( + manager: &Arc>>, + receiver: &mut tokio::sync::mpsc::UnboundedReceiver, + count: usize, + ) -> BTreeMap { + let mut batch = BTreeMap::new(); + for _ in 0..count { + let event = receiver.try_recv().expect("queued event"); + super::project_event_into_batch(manager, &event, &mut batch).await; + } + batch + } + + /// dashpay/platform#4302 review: an advance the engine emitted before a + /// rewind, still queued when the rescan has climbed back past it, looks + /// current by height — the live cursor is above it again — yet storing + /// it would put the durable cursor above transaction rows the rescan + /// emitted behind it. The barrier drops it by emission order instead. + #[tokio::test] + async fn an_advance_queued_before_a_rewind_is_dropped_after_the_rescan_climbs_past_it() { + let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + engine_advance(&manager, wallet_id, 1_000).await; // pre-rewind, still queued + rewind(&manager, wallet_id, 100).await; + engine_advance(&manager, wallet_id, 500).await; + engine_advance(&manager, wallet_id, 1_000).await; // the rescan is back at 1000 + + // The adapter drains the pre-rewind advance on its own. + let mut stale = project(&manager, &mut receiver, 1).await; + assert_eq!(stale[&wallet_id].core.synced_height, None); + super::drop_watermarks_above_the_live_cursor(&manager, &mut stale).await; + assert_eq!(stale[&wallet_id].core.synced_height, None); + + // The rescan's own advances flow, behind the rows they imply. + let mut fresh = project(&manager, &mut receiver, 2).await; + super::drop_watermarks_above_the_live_cursor(&manager, &mut fresh).await; + assert_eq!(fresh[&wallet_id].core.synced_height, Some(1_000)); + } + + /// The same race with the advance already projected when the rewind + /// runs: by commit the rescan has climbed back past it, so the live + /// cursor no longer exposes it, and the superseded epoch does. + #[tokio::test] + async fn the_commit_drops_an_advance_a_rewind_overtook_after_the_rescan_climbs_past_it() { + let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + engine_advance(&manager, wallet_id, 1_000).await; + let mut projected = project(&manager, &mut receiver, 1).await; + assert_eq!(projected[&wallet_id].core.synced_height, Some(1_000)); + + rewind(&manager, wallet_id, 100).await; + engine_advance(&manager, wallet_id, 1_200).await; + super::drop_watermarks_above_the_live_cursor(&manager, &mut projected).await; + assert_eq!( + projected[&wallet_id].core.synced_height, None, + "projected before the rewind: stripped although the live cursor is above it" + ); + + // The rescan's advance, in the next batch, is stored. + let mut next = project(&manager, &mut receiver, 1).await; + super::drop_watermarks_above_the_live_cursor(&manager, &mut next).await; + assert_eq!(next[&wallet_id].core.synced_height, Some(1_200)); + } + + /// A batch that holds a pre-rewind advance and then the rescan's first + /// one ends on the rescan's height: the newer epoch replaces the older + /// advance rather than max-merging with it, which would keep the higher, + /// pre-rewind height. + #[tokio::test] + async fn a_batch_spanning_a_rewind_keeps_the_rescans_height_not_the_higher_stale_one() { + let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + engine_advance(&manager, wallet_id, 1_000).await; + let first = receiver.try_recv().expect("pre-rewind advance"); + let mut batch = BTreeMap::new(); + super::project_event_into_batch(&manager, &first, &mut batch).await; + + rewind(&manager, wallet_id, 100).await; + engine_advance(&manager, wallet_id, 400).await; + let second = receiver.try_recv().expect("rescan advance"); + super::project_event_into_batch(&manager, &second, &mut batch).await; + assert_eq!(batch[&wallet_id].core.synced_height, Some(400)); + + super::drop_watermarks_above_the_live_cursor(&manager, &mut batch).await; + assert_eq!(batch[&wallet_id].core.synced_height, Some(400)); + } + /// A record-bearing (non-watermark) event: carries /// `last_processed_height` but no `synced_height`, so it survives the /// freeze untouched. Used both as a sentinel to prove loop progress and @@ -6425,6 +6705,7 @@ mod tests { core: CoreChangeSet::default(), asset_locks, payments: super::PaymentOverlay::default(), + advance_epoch: None, }, ); commit_batch( @@ -6485,6 +6766,7 @@ mod tests { core, asset_locks: AssetLockChangeSet::default(), payments: super::PaymentOverlay::default(), + advance_epoch: None, }, ); batch @@ -6526,6 +6808,7 @@ mod tests { core: watermark_with_rows(700, 700), asset_locks: AssetLockChangeSet::default(), payments: one_verdict_overlay(PaymentStatus::Failed), + advance_epoch: None, }, ); let diag = commit_batch( @@ -6578,6 +6861,7 @@ mod tests { core: watermark_with_rows(700, 700), asset_locks: AssetLockChangeSet::default(), payments: one_verdict_overlay(PaymentStatus::Failed), + advance_epoch: None, }, ); let diag = commit_batch( @@ -6630,6 +6914,7 @@ mod tests { core: CoreChangeSet::default(), asset_locks: AssetLockChangeSet::default(), payments: one_verdict_overlay(PaymentStatus::Failed), + advance_epoch: None, }, ); commit_batch( @@ -6672,6 +6957,7 @@ mod tests { core: CoreChangeSet::default(), asset_locks: AssetLockChangeSet::default(), payments: one_verdict_overlay(PaymentStatus::Confirmed), + advance_epoch: None, }, ); commit_batch( @@ -7493,6 +7779,7 @@ mod utxo_credit_verdict_tests { tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); @@ -7588,6 +7875,7 @@ mod utxo_credit_verdict_tests { tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 879394004b1..5d989220a3c 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -21,9 +21,15 @@ use dpp::prelude::Identifier; -/// One receival contact the backfill covers, and the height it covers it +/// One receival account the backfill covers, and the height it covers it /// from. /// +/// Keyed on the whole receival-account key — `(owner, contact, +/// account_index)` — not just the relationship: a second receiving account +/// for the same pair derives a different xpub, so its scripts were never +/// watched by a scan that covered the first, and it must not inherit that +/// coverage. +/// /// `covered_from` is the contact's scan checkpoint at the moment its coverage /// was recorded — the earliest height whose block the scanner is guaranteed to /// test against this contact's addresses. It is stored per contact rather than @@ -38,11 +44,20 @@ pub struct DashPayBackfillCoveredContact { pub owner: Identifier, /// The contact whose payments the receival account collects. pub contact: Identifier, + /// The receival account's index (`DashpayAccountKey::index`). + pub account_index: u32, /// Lowest height the scan is guaranteed to have tested (or to test, as it /// climbs) with this contact's addresses watched. pub covered_from: u32, } +impl DashPayBackfillCoveredContact { + /// The receival-account key the record is sorted and looked up by. + pub fn key(&self) -> (Identifier, Identifier, u32) { + (self.owner, self.contact, self.account_index) + } +} + /// Per-wallet durable record of the DashPay coreHeight backfill. /// /// # What a record claims @@ -94,8 +109,8 @@ pub struct DashPayBackfillRecord { /// climb back to for the backfill to be complete. `0` while no pass has /// rewound anything ([`has_extent`](Self::has_extent)). pub rewound_from: u32, - /// Receival contacts the backfill covers, sorted by `(owner, contact)`, - /// at most one entry per pair. + /// Receival accounts the backfill covers, sorted by `(owner, contact, + /// account_index)`, at most one entry per account. pub covered: Vec, /// In-memory only, never persisted or crossed over the FFI: a cursor a /// previous record round still owes the host because that round's @@ -120,20 +135,31 @@ impl DashPayBackfillRecord { self.covered.is_empty() } - /// The height this record covers `(owner, contact)` from, if it covers - /// it at all. - pub fn covered_from(&self, owner: &Identifier, contact: &Identifier) -> Option { + /// The height this record covers the receival account `(owner, contact, + /// account_index)` from, if it covers it at all. + pub fn covered_from( + &self, + owner: &Identifier, + contact: &Identifier, + account_index: u32, + ) -> Option { self.covered - .binary_search_by(|entry| (entry.owner, entry.contact).cmp(&(*owner, *contact))) + .binary_search_by(|entry| entry.key().cmp(&(*owner, *contact, account_index))) .ok() .map(|index| self.covered[index].covered_from) } - /// Whether `(owner, contact)` is covered at `checkpoint`: the record lists - /// it and its checkpoint has not dropped below the height it was covered - /// from. - pub fn covers(&self, owner: &Identifier, contact: &Identifier, checkpoint: u32) -> bool { - self.covered_from(owner, contact) + /// Whether the receival account `(owner, contact, account_index)` is + /// covered at `checkpoint`: the record lists it and its checkpoint has not + /// dropped below the height it was covered from. + pub fn covers( + &self, + owner: &Identifier, + contact: &Identifier, + account_index: u32, + checkpoint: u32, + ) -> bool { + self.covered_from(owner, contact, account_index) .is_some_and(|covered_from| checkpoint >= covered_from) } @@ -161,27 +187,28 @@ impl DashPayBackfillRecord { /// `synced_height_before` is the cursor before the pass rewound it (the /// height the scan must climb back to); `floor` is where the pass rewound /// to, or `None` when every contact it handled was already covered by the - /// forward scan. Each `(owner, contact, covered_from)` entry replaces any - /// existing entry for the pair. + /// forward scan. Each `(owner, contact, account_index, covered_from)` entry + /// replaces any existing entry for that receival account. /// /// Returns `true` when the record changed. pub fn record_pass( &mut self, synced_height_before: u32, floor: Option, - entries: impl IntoIterator, + entries: impl IntoIterator, ) -> bool { - let was_empty = self.covered.is_empty(); let mut changed = false; - for (owner, contact, covered_from) in entries { + for (owner, contact, account_index, covered_from) in entries { let entry = DashPayBackfillCoveredContact { owner, contact, + account_index, covered_from, }; - match self.covered.binary_search_by(|existing| { - (existing.owner, existing.contact).cmp(&(owner, contact)) - }) { + match self + .covered + .binary_search_by(|existing| existing.key().cmp(&entry.key())) + { Ok(index) => { if self.covered[index] != entry { self.covered[index] = entry; @@ -204,8 +231,11 @@ impl DashPayBackfillRecord { return changed; }; // A first rewind takes the pass's extent as-is; a later one can only - // widen it — deeper floor, higher climb target. - let (new_floor, new_rewound_from) = if was_empty || !self.has_extent() { + // widen it — deeper floor, higher climb target. "First" is read off + // the extent, never off the cover set: `retain` can empty the set + // while the extent of the backfill those entries rode is still + // being climbed. + let (new_floor, new_rewound_from) = if !self.has_extent() { (pass_floor, synced_height_before) } else { ( @@ -221,20 +251,20 @@ impl DashPayBackfillRecord { changed } - /// Drop every entry for which `keep` is false — used to forget contacts - /// whose receival account no longer exists. Returns `true` when the record - /// changed. - pub fn retain(&mut self, mut keep: impl FnMut(&Identifier, &Identifier) -> bool) -> bool { + /// Drop every entry for which `keep(owner, contact, account_index)` is + /// false — used to forget receival accounts that no longer exist. Returns + /// `true` when the record changed. + pub fn retain(&mut self, mut keep: impl FnMut(&Identifier, &Identifier, u32) -> bool) -> bool { let before = self.covered.len(); self.covered - .retain(|entry| keep(&entry.owner, &entry.contact)); + .retain(|entry| keep(&entry.owner, &entry.contact, entry.account_index)); before != self.covered.len() } /// Wire size of one [`DashPayBackfillCoveredContact`] in - /// [`covered_bytes`](Self::covered_bytes): owner ‖ contact ‖ `covered_from` - /// (little-endian). - pub const COVERED_ENTRY_LEN: usize = 32 + 32 + 4; + /// [`covered_bytes`](Self::covered_bytes): owner ‖ contact ‖ + /// `account_index` ‖ `covered_from` (both little-endian). + pub const COVERED_ENTRY_LEN: usize = 32 + 32 + 4 + 4; /// Flat encoding of [`covered`](Self::covered) for hosts that store the /// list as one opaque blob: [`COVERED_ENTRY_LEN`](Self::COVERED_ENTRY_LEN) @@ -244,6 +274,7 @@ impl DashPayBackfillRecord { for entry in &self.covered { out.extend_from_slice(&entry.owner.to_buffer()); out.extend_from_slice(&entry.contact.to_buffer()); + out.extend_from_slice(&entry.account_index.to_le_bytes()); out.extend_from_slice(&entry.covered_from.to_le_bytes()); } out @@ -266,20 +297,23 @@ impl DashPayBackfillRecord { .map(|chunk| { let mut owner = [0u8; 32]; let mut contact = [0u8; 32]; + let mut account_index = [0u8; 4]; let mut height = [0u8; 4]; owner.copy_from_slice(&chunk[..32]); contact.copy_from_slice(&chunk[32..64]); - height.copy_from_slice(&chunk[64..68]); + account_index.copy_from_slice(&chunk[64..68]); + height.copy_from_slice(&chunk[68..72]); DashPayBackfillCoveredContact { owner: Identifier::from(owner), contact: Identifier::from(contact), + account_index: u32::from_le_bytes(account_index), covered_from: u32::from_le_bytes(height), } }) .collect(); - // Canonical order and one entry per pair, whatever the host stored. - covered.sort_by_key(|entry| (entry.owner, entry.contact)); - covered.dedup_by(|a, b| (a.owner, a.contact) == (b.owner, b.contact)); + // Canonical order and one entry per account, whatever the host stored. + covered.sort_by_key(DashPayBackfillCoveredContact::key); + covered.dedup_by(|a, b| a.key() == b.key()); Some(Self { floor, rewound_from, @@ -305,29 +339,29 @@ mod tests { assert!(!record.is_pending(0)); assert!(!record.is_complete(u32::MAX)); - assert!(record.record_pass(1_000, Some(100), [(id(1), id(2), 100)])); + assert!(record.record_pass(1_000, Some(100), [(id(1), id(2), 0, 100)])); assert_eq!((record.floor, record.rewound_from), (100, 1_000)); assert!(record.is_pending(999)); assert!(record.is_complete(1_000)); // A shallower rewind while climbing neither raises the floor nor // lowers the climb target. - assert!(record.record_pass(400, Some(300), [(id(1), id(3), 300)])); + assert!(record.record_pass(400, Some(300), [(id(1), id(3), 0, 300)])); assert_eq!((record.floor, record.rewound_from), (100, 1_000)); // A deeper rewind after completion lowers the floor and raises the // climb target to the cursor it rewound from. - assert!(record.record_pass(2_000, Some(50), [(id(1), id(4), 50)])); + assert!(record.record_pass(2_000, Some(50), [(id(1), id(4), 0, 50)])); assert_eq!((record.floor, record.rewound_from), (50, 2_000)); // A forward-covered pass (no rewind) records the contact without // touching the extent it cannot widen. - assert!(record.record_pass(1_500, None, [(id(1), id(5), 1_700)])); + assert!(record.record_pass(1_500, None, [(id(1), id(5), 0, 1_700)])); assert_eq!((record.floor, record.rewound_from), (50, 2_000)); - assert_eq!(record.covered_from(&id(1), &id(5)), Some(1_700)); + assert_eq!(record.covered_from(&id(1), &id(5), 0), Some(1_700)); // An unchanged replay is reported as such. - assert!(!record.record_pass(1_500, None, [(id(1), id(5), 1_700)])); + assert!(!record.record_pass(1_500, None, [(id(1), id(5), 0, 1_700)])); } /// A pass that rewound nothing must not invent an extent from the cursor @@ -337,13 +371,13 @@ mod tests { #[test] fn a_forward_only_pass_on_an_empty_record_leaves_no_extent() { let mut record = DashPayBackfillRecord::default(); - assert!(record.record_pass(1_251_329, None, [(id(1), id(2), 1_300_000)])); + assert!(record.record_pass(1_251_329, None, [(id(1), id(2), 0, 1_300_000)])); assert!(!record.has_extent()); assert!(!record.is_pending(1_000)); assert!(!record.is_complete(2_000_000)); - assert!(record.covers(&id(1), &id(2), 1_300_000)); + assert!(record.covers(&id(1), &id(2), 0, 1_300_000)); - assert!(record.record_pass(1_560_731, Some(1_226_329), [(id(1), id(3), 1_226_329)])); + assert!(record.record_pass(1_560_731, Some(1_226_329), [(id(1), id(3), 0, 1_226_329)])); assert!(record.has_extent()); assert_eq!((record.floor, record.rewound_from), (1_226_329, 1_560_731)); assert!(record.is_pending(1_251_329)); @@ -353,33 +387,41 @@ mod tests { #[test] fn coverage_is_per_contact_and_keyed_on_the_recorded_checkpoint() { let mut record = DashPayBackfillRecord::default(); - record.record_pass(1_000, Some(100), [(id(1), id(2), 100), (id(1), id(3), 300)]); + record.record_pass( + 1_000, + Some(100), + [(id(1), id(2), 0, 100), (id(1), id(3), 0, 300)], + ); - assert!(record.covers(&id(1), &id(2), 100)); - assert!(record.covers(&id(1), &id(2), 250)); + assert!(record.covers(&id(1), &id(2), 0, 100)); + assert!(record.covers(&id(1), &id(2), 0, 250)); assert!( - !record.covers(&id(1), &id(2), 99), + !record.covers(&id(1), &id(2), 0, 99), "a checkpoint below the covered height re-arms" ); assert!( - !record.covers(&id(1), &id(9), 500), + !record.covers(&id(1), &id(9), 0, 500), "an unlisted contact is never covered" ); assert!( - !record.covers(&id(7), &id(2), 500), + !record.covers(&id(7), &id(2), 0, 500), "coverage is per owner, too" ); // Re-recording a pair replaces its entry. - assert!(record.record_pass(500, Some(40), [(id(1), id(2), 40)])); - assert_eq!(record.covered_from(&id(1), &id(2)), Some(40)); + assert!(record.record_pass(500, Some(40), [(id(1), id(2), 0, 40)])); + assert_eq!(record.covered_from(&id(1), &id(2), 0), Some(40)); assert_eq!(record.covered.len(), 2); } #[test] fn the_flat_encoding_round_trips_and_refuses_a_torn_blob() { let mut record = DashPayBackfillRecord::default(); - record.record_pass(1_000, Some(100), [(id(9), id(2), 100), (id(1), id(3), 300)]); + record.record_pass( + 1_000, + Some(100), + [(id(9), id(2), 0, 100), (id(1), id(3), 0, 300)], + ); let bytes = record.covered_bytes(); assert_eq!(bytes.len(), 2 * DashPayBackfillRecord::COVERED_ENTRY_LEN); @@ -401,6 +443,7 @@ mod tests { let mut bytes = Vec::new(); bytes.extend_from_slice(&[owner; 32]); bytes.extend_from_slice(&[contact; 32]); + bytes.extend_from_slice(&0u32.to_le_bytes()); bytes.extend_from_slice(&from.to_le_bytes()); bytes }; @@ -410,16 +453,65 @@ mod tests { let record = DashPayBackfillRecord::from_parts(100, 1_000, &bytes).expect("decodes"); assert_eq!(record.covered.len(), 2); assert_eq!(record.covered[0].owner, id(1)); - assert_eq!(record.covered_from(&id(5), &id(6)), Some(700)); + assert_eq!(record.covered_from(&id(5), &id(6), 0), Some(700)); } #[test] fn retain_forgets_contacts_and_reports_whether_anything_went() { let mut record = DashPayBackfillRecord::default(); - record.record_pass(1_000, Some(100), [(id(1), id(2), 100), (id(1), id(3), 300)]); - assert!(!record.retain(|_, _| true)); - assert!(record.retain(|_, contact| *contact != id(3))); - assert_eq!(record.covered_from(&id(1), &id(3)), None); - assert_eq!(record.covered_from(&id(1), &id(2)), Some(100)); + record.record_pass( + 1_000, + Some(100), + [(id(1), id(2), 0, 100), (id(1), id(3), 0, 300)], + ); + assert!(!record.retain(|_, _, _| true)); + assert!(record.retain(|_, contact, _| *contact != id(3))); + assert_eq!(record.covered_from(&id(1), &id(3), 0), None); + assert_eq!(record.covered_from(&id(1), &id(2), 0), Some(100)); + } + + /// `retain` can empty the cover set while the backfill those entries rode + /// is still climbing. The next rewind must widen that extent, not replace + /// it: replacing `(100, 1000)` with `(200, 500)` would read complete at 500 + /// with `[500, 1000]` never re-matched. + #[test] + fn pruning_the_last_covered_contact_keeps_the_extent_the_next_rewind_widens() { + let mut record = DashPayBackfillRecord::default(); + record.record_pass(1_000, Some(100), [(id(1), id(2), 0, 100)]); + assert!(record.retain(|_, _, _| false)); + assert!(record.is_empty()); + assert_eq!((record.floor, record.rewound_from), (100, 1_000)); + + assert!(record.record_pass(500, Some(200), [(id(1), id(3), 0, 200)])); + assert_eq!((record.floor, record.rewound_from), (100, 1_000)); + assert!(record.is_pending(500)); + assert!(!record.is_complete(999)); + } + + /// A second receiving account for the same relationship derives its own + /// xpub, so a scan that watched the first never tested the second's + /// scripts: coverage is per receival account, not per pair. + #[test] + fn coverage_is_per_receival_account_not_per_relationship() { + let mut record = DashPayBackfillRecord::default(); + record.record_pass(1_000, Some(100), [(id(1), id(2), 0, 100)]); + assert!(record.covers(&id(1), &id(2), 0, 100)); + assert!( + !record.covers(&id(1), &id(2), 1, 100), + "account 1 never inherits account 0's coverage" + ); + + record.record_pass(1_000, Some(100), [(id(1), id(2), 1, 100)]); + assert_eq!(record.covered.len(), 2); + let restored = DashPayBackfillRecord::from_parts( + record.floor, + record.rewound_from, + &record.covered_bytes(), + ) + .expect("decodes"); + assert_eq!(restored, record); + assert!(record.retain(|_, _, account_index| account_index == 0)); + assert!(!record.covers(&id(1), &id(2), 1, 100)); + assert!(record.covers(&id(1), &id(2), 0, 100)); } } diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index 4751db99e4c..687d138774f 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -866,6 +866,9 @@ impl PlatformWalletManager

{ return true; } info.core_wallet.update_synced_height(from_height); + // Advances the engine emitted before this reset must not be stored + // after it. + info.rewind_barrier.arm(); // Not persisted here, so remember it as owed: the next DashPay // backfill record round carries it as that round's cursor, and no // coverage reaches disk beside a durable cursor this reset retracted @@ -1954,6 +1957,7 @@ mod txo_inventory_tests { tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), observed_input_conflicts: Default::default(), }; let mut wm = WalletManager::::new(dashcore::Network::Testnet); diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index f9e8e8588ae..c8b3ee7cd9e 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -231,6 +231,7 @@ impl PlatformWalletManager

{ tracked_asset_locks, dpns_name_states: std::collections::BTreeMap::new(), dashpay_backfill, + rewind_barrier: Default::default(), }; // Seed the double-spend screen's session memory from the // freshly restored state: it closes the race where SPV's @@ -240,8 +241,8 @@ impl PlatformWalletManager

{ &platform_info, ); - // The cursor the host just handed back is the durable one: seed - // the shared record of it before any writer can race it. + // The cursor the host just handed back is the durable one; it + // seeds the shared record of it as the wallet is published below. let loaded_cursor = platform_info.core_wallet.metadata.synced_height; if wallet_id != expected_wallet_id { @@ -271,7 +272,17 @@ impl PlatformWalletManager

{ // // The existence check and the insert share one write-lock scope // so a concurrent loader can't slip between them (TOCTOU). + // + // The durable cursor is seeded in that same scope, under the + // cursor lock taken first (its order: cursor lock, then manager). + // The insert makes the wallet visible to the scanner and the + // event adapter; seeding after it would let an adapter commit or + // a reconcile record a newer durable height first, and the seed + // would then overwrite it with the stale loaded one. An entry a + // removed or rolled-back registration left behind is overwritten + // here the same way, so it is never read against a live wallet. { + let mut durable_cursors = self.durable_cursors.lock().await; let mut wm = self.wallet_manager.write().await; if wm.get_wallet(&wallet_id).is_some() { continue 'load; @@ -283,6 +294,7 @@ impl PlatformWalletManager

{ ))); break 'load; } + durable_cursors.insert(wallet_id, loaded_cursor); } inserted_in_manager.push(wallet_id); @@ -349,12 +361,6 @@ impl PlatformWalletManager

{ Arc::clone(&self.sync_fault), Arc::clone(&self.durable_cursors), ); - // No manager-lock guard is alive here, so taking the cursor lock - // keeps its order (cursor lock, then manager). - self.durable_cursors - .lock() - .await - .insert(wallet_id, loaded_cursor); // Initialize the platform-address provider. If the snapshot // carried a slice for this wallet, restore it directly; diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index d8b0cbfe8f0..698f15bbe6f 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -371,6 +371,7 @@ impl PlatformWalletManager

{ tracked_asset_locks: std::collections::BTreeMap::new(), dpns_name_states: std::collections::BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; wallet.downgrade_to_external_signable(); @@ -437,17 +438,30 @@ impl PlatformWalletManager

{ // treat re-registering an existing wallet as a benign no-op // instead of substring-matching the error text. Everything else // stays `WalletCreation`. + // + // The durable cursor is seeded in the same scope as the insert, under + // the cursor lock taken first (its order: cursor lock, then manager). + // The insert makes the wallet visible to the scanner and the event + // adapter; seeding after it would let an adapter commit or a + // reconcile record a newer durable height first, only for the seed to + // overwrite it with the creation one. An entry a removed or + // rolled-back registration left behind is overwritten here the same + // way, so it is never read against a live wallet. let created_cursor = platform_info.core_wallet.metadata.synced_height; let wallet_id = { + let mut durable_cursors = self.durable_cursors.lock().await; let mut wm = self.wallet_manager.write().await; - wm.insert_wallet(wallet, platform_info) + let wallet_id = wm + .insert_wallet(wallet, platform_info) .map_err(|e| match e { key_wallet_manager::WalletError::WalletExists(id) => already_registered(id), other => PlatformWalletError::WalletCreation(format!( "Failed to register wallet in WalletManager: {}", other )), - })? + })?; + durable_cursors.insert(wallet_id, created_cursor); + wallet_id }; // `insert_wallet` recomputes the id from the (now external-signable) @@ -572,10 +586,6 @@ impl PlatformWalletManager

{ Arc::clone(&self.sync_fault), Arc::clone(&self.durable_cursors), ); - self.durable_cursors - .lock() - .await - .insert(wallet_id, created_cursor); // Restore the platform-address provider from the slice read above — // the only area wired up today. `from_persisted` skips the live @@ -1246,6 +1256,76 @@ mod register_wallet_duplicate_tests { ); } + /// The durable cursor is seeded in the critical section that publishes + /// the wallet. Published first and seeded after, an adapter commit or a + /// reconcile could record a newer durable height in between and the seed + /// would overwrite it with the creation one. While the cursor lock is + /// held the registration must not publish the wallet at all, and once it + /// does, the entry is the created cursor — never one left behind by an + /// earlier registration of the same id. + #[tokio::test] + async fn a_wallet_is_published_and_its_durable_cursor_seeded_in_one_step() { + let manager = make_manager(); + let network = Network::Testnet; + let mnemonic = Mnemonic::from_phrase(TEST_MNEMONIC).expect("valid test mnemonic"); + let seed_bytes = mnemonic.to_seed(""); + let create = |manager: Arc>| async move { + manager + .create_wallet_from_seed_bytes( + network, + &seed_bytes, + WalletAccountCreationOptions::Default, + Some(0), + ) + .await + }; + + let wallet_id = create(Arc::clone(&manager)) + .await + .expect("first create") + .wallet_id(); + manager.remove_wallet(&wallet_id).await.expect("remove"); + + let mut gate = manager.durable_cursors.lock().await; + // What a removed registration might leave behind. + gate.insert(wallet_id, 777); + let registering = tokio::spawn(create(Arc::clone(&manager))); + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + assert!( + manager + .wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .is_none(), + "no wallet may become visible while the durable-cursor lock is held" + ); + drop(gate); + + registering.await.expect("join").expect("second create"); + let created_cursor = { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + manager + .wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .expect("published") + .core_wallet + .synced_height() + }; + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(), + Some(created_cursor), + "the seed is the created cursor, not the leftover entry" + ); + } + /// PENDING-SPEND PROTECTION MUST SURVIVE WALLET RECREATION. /// /// If the in-broadcast fence lived in the `WalletGeneration` itself, it diff --git a/packages/rs-platform-wallet/src/test_support.rs b/packages/rs-platform-wallet/src/test_support.rs index a41e03967c4..3aa3d328129 100644 --- a/packages/rs-platform-wallet/src/test_support.rs +++ b/packages/rs-platform-wallet/src/test_support.rs @@ -257,6 +257,7 @@ pub async fn funded_wallet_manager_with_outputs( tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); @@ -387,6 +388,7 @@ pub(crate) async fn funded_wallet_manager_dual_standard( tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); let wallet_id = wm.insert_wallet(ctx.wallet, info).expect("insert wallet"); @@ -491,6 +493,7 @@ pub(crate) async fn funded_wallet_manager_with_contact( tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); let wallet_id = wm.insert_wallet(ctx.wallet, info).expect("insert wallet"); @@ -569,6 +572,7 @@ pub(crate) async fn funded_coinjoin_wallet_manager() -> ( tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); @@ -775,6 +779,7 @@ pub(crate) async fn mnemonic_wallet_manager( tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let mut wm = WalletManager::::new(Network::Testnet); diff --git a/packages/rs-platform-wallet/src/wallet/apply.rs b/packages/rs-platform-wallet/src/wallet/apply.rs index 24b70da93cd..2962ee77cf5 100644 --- a/packages/rs-platform-wallet/src/wallet/apply.rs +++ b/packages/rs-platform-wallet/src/wallet/apply.rs @@ -445,6 +445,7 @@ mod tests { observed_input_conflicts: Default::default(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), } } diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs index 06a5f44c4c9..6736299079d 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/reconstruction.rs @@ -665,6 +665,7 @@ mod tests { tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; assert_eq!( funding_account_index(&info, &txid), diff --git a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs index b1741b2a6ec..76bf44b562f 100644 --- a/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs +++ b/packages/rs-platform-wallet/src/wallet/asset_lock/sync/recovery.rs @@ -2525,6 +2525,7 @@ mod tests { tracked_asset_locks: BTreeMap::new(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), }; let out_point = OutPoint::new(tx.txid(), 0); let lock = TrackedAssetLock { 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 132a5c6db3a..36a8136ab07 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 @@ -4185,6 +4185,7 @@ mod sweep_tests { observed_input_conflicts: Default::default(), dpns_name_states: BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), } } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 31d889af391..921905de0e6 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -154,9 +154,12 @@ impl DashPayView<'_, B> { return Ok(None); } - // (owner, contact) pairs that have a receival account — we can only - // watch a contact's incoming addresses once its receival account exists. - let receival_pairs: Vec<(Identifier, Identifier)> = info + // (owner, contact, account_index) of every receival account — we can + // only watch a contact's incoming addresses once its receival account + // exists. Coverage is per account, not per pair: a second receiving + // account for a relationship derives its own xpub, and a scan that + // watched the first never tested the second's scripts. + let receival_accounts: Vec<(Identifier, Identifier, u32)> = info .core_wallet .accounts .dashpay_receival_accounts @@ -165,6 +168,7 @@ impl DashPayView<'_, B> { ( Identifier::from(k.user_identity_id), Identifier::from(k.friend_identity_id), + k.index, ) }) .collect(); @@ -185,12 +189,13 @@ impl DashPayView<'_, B> { // be rewound to, so an uncovered contact goes through the candidate // path whether or not it is marked. let mut floor: Option = None; - // Contacts this pass handles, with the checkpoint each is covered from. - let mut to_mark: Vec<(Identifier, Identifier, u32)> = Vec::new(); + // Receival accounts this pass handles, with the checkpoint each is + // covered from. + let mut to_mark: Vec<(Identifier, Identifier, u32, u32)> = Vec::new(); // Contacts the durable record vouched for that are not yet marked in // this process. let mut restored: Vec<(Identifier, Identifier)> = Vec::new(); - for (owner, contact) in receival_pairs.iter().copied() { + for (owner, contact, account_index) in receival_accounts.iter().copied() { let Some(managed) = info.identity_manager.managed_identity(&owner) else { continue; }; @@ -205,8 +210,13 @@ impl DashPayView<'_, B> { // checkpoint no higher than today's, and the persisted cursor // tracked that scan with its addresses watched, so nothing below // the cursor is missing: resume, don't restart. - if info.dashpay_backfill.covers(&owner, &contact, checkpoint) { - if !managed.dashpay().rescan_triggered.contains(&contact) { + if info + .dashpay_backfill + .covers(&owner, &contact, account_index, checkpoint) + { + if !managed.dashpay().rescan_triggered.contains(&contact) + && !restored.contains(&(owner, contact)) + { restored.push((owner, contact)); } continue; @@ -222,7 +232,7 @@ impl DashPayView<'_, B> { if checkpoint < synced_height { floor = Some(floor.map_or(checkpoint, |cur| cur.min(checkpoint))); } - to_mark.push((owner, contact, checkpoint)); + to_mark.push((owner, contact, account_index, checkpoint)); } let restored_count = restored.len(); @@ -254,9 +264,12 @@ impl DashPayView<'_, B> { // own header/birth floor, so no double-clamp here. if let Some(floor) = floor { info.core_wallet.update_synced_height(floor); + // Advances the engine emitted before this rewind must not be + // stored after it — see `RewindBarrier`. + info.rewind_barrier.arm(); } let triggered = to_mark.len(); - for (owner, contact, _) in &to_mark { + for (owner, contact, _, _) in &to_mark { if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { managed.dashpay_rescan_triggered_mut().insert(*contact); } @@ -294,10 +307,10 @@ impl DashPayView<'_, B> { // which is the pre-record behaviour. let previous_record = info.dashpay_backfill.clone(); let record = &mut info.dashpay_backfill; - record.retain(|owner, contact| { - receival_pairs + record.retain(|owner, contact, account_index| { + receival_accounts .iter() - .any(|(o, c)| (o, c) == (owner, contact)) + .any(|(o, c, i)| (o, c, *i) == (owner, contact, account_index)) }); // This pass's extent, widened by one a failed earlier round still owes. let extent = match ( @@ -364,7 +377,7 @@ impl DashPayView<'_, B> { info.dashpay_backfill.unpersisted_cursor = Some(needed_cursor.map_or(cursor_now, |cursor| cursor.min(cursor_now))); info.dashpay_backfill.unpersisted_extent = extent; - for (owner, contact, _) in &to_mark { + for (owner, contact, _, _) in &to_mark { if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { managed.dashpay_rescan_triggered_mut().remove(contact); } @@ -2981,7 +2994,7 @@ mod tests { // the record existed (and hosts that do not store it), so model that: // the account is on file, the record does not know it. info.dashpay_backfill - .retain(|o, c| (o, c) != (&owner, &contact)); + .retain(|o, c, _| (o, c) != (&owner, &contact)); } /// Drop `(owner, contact)` from the wallet's backfill record — the shape @@ -2998,7 +3011,7 @@ mod tests { wm.get_wallet_info_mut(&wallet_id) .expect("info") .dashpay_backfill - .retain(|o, c| (o, c) != (&owner, &contact)); + .retain(|o, c, _| (o, c) != (&owner, &contact)); } /// Move the wallet's cursor to `height` the way a scan does: in memory, @@ -3020,6 +3033,22 @@ mod tests { durable.insert(wallet_id, height); } + /// The wallet's [`RewindBarrier`](crate::changeset::core_bridge::RewindBarrier) + /// epoch: how many times its cursor was lowered in memory. + async fn rewind_epoch( + manager: &Arc>, + wallet_id: WalletId, + ) -> u64 { + manager + .wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .expect("info") + .rewind_barrier + .epoch() + } + async fn synced_height( manager: &Arc>, wallet_id: WalletId, @@ -3343,7 +3372,7 @@ mod tests { ); let record = round.dashpay_backfill.as_ref().unwrap(); assert_eq!((record.floor, record.rewound_from), (100, 1_000)); - assert_eq!(record.covered_from(&owner, &contact), Some(100)); + assert_eq!(record.covered_from(&owner, &contact, 0), Some(100)); assert!(record.is_pending(100)); assert!( !record.is_complete(999), @@ -3484,7 +3513,10 @@ mod tests { .get_mut(&0) .unwrap() .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); - assert_eq!(snapshot.backfill.covered_from(&owner, &contact), Some(100)); + assert_eq!( + snapshot.backfill.covered_from(&owner, &contact, 0), + Some(100) + ); let (restarted, restarted_persister, _) = reload(snapshot).await; assert_eq!( @@ -3504,7 +3536,7 @@ mod tests { let info = wm.get_wallet_info(&wallet_id).expect("info"); assert_eq!(info.core_wallet.synced_height(), 50); assert_eq!( - info.dashpay_backfill.covered_from(&owner, &contact), + info.dashpay_backfill.covered_from(&owner, &contact, 0), Some(50) ); assert_eq!( @@ -3578,11 +3610,11 @@ mod tests { let info = wm.get_wallet_info(&wallet_id).expect("info"); assert_eq!(info.core_wallet.synced_height(), 300); assert_eq!( - info.dashpay_backfill.covered_from(&owner, &covered), + info.dashpay_backfill.covered_from(&owner, &covered, 0), Some(100) ); assert_eq!( - info.dashpay_backfill.covered_from(&owner, &newcomer), + info.dashpay_backfill.covered_from(&owner, &newcomer, 0), Some(300) ); assert_eq!( @@ -3594,6 +3626,74 @@ mod tests { ); } + /// Coverage is per receival account, not per relationship: a second + /// receiving account for an already-covered pair derives its own xpub, + /// and the scan that covered the first never tested its scripts. It is + /// rewound for, not waved through on the first account's entry. + #[tokio::test] + async fn a_second_receival_account_for_a_covered_pair_is_rewound_not_inherited() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let dashpay = wallet.identity().dashpay(); + assert_eq!( + dashpay + .reconcile_dashpay_rescan() + .await + .expect("first pass"), + Some(100) + ); + assert_eq!( + rewind_epoch(&manager, wallet_id).await, + 1, + "the rewind arms the barrier" + ); + set_synced_height(&manager, wallet_id, 1_000).await; + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("steady"), + None, + "account 0 is covered" + ); + + dashpay + .register_contact_account( + &owner, + &contact, + 1, + test_receiving_xpub(&owner, &Identifier::from([0xDD; 32])), + ) + .await + .expect("register account 1"); + assert_eq!( + dashpay + .reconcile_dashpay_rescan() + .await + .expect("pass after account 1"), + Some(100), + "account 1 never inherits account 0's coverage" + ); + let wm = manager.wallet_manager.read().await; + let record = &wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill; + assert_eq!(record.covered_from(&owner, &contact, 0), Some(100)); + assert_eq!(record.covered_from(&owner, &contact, 1), Some(100)); + let stores = persister.stores.lock().unwrap(); + assert_eq!( + last_stored_record(&stores) + .and_then(|cs| cs.dashpay_backfill.as_ref()) + .expect("a record round") + .covered + .len(), + 2, + "both accounts reach the host" + ); + } + /// A contact's compact receiving xpub encrypted under `shared_key` — the /// wire shape a contact sends us — so `register_external_contact_account` /// can be driven without a signer. @@ -3990,7 +4090,7 @@ mod tests { .unwrap(); assert_eq!((last.floor, last.rewound_from), (1_226_329, 1_560_731)); for contact in &contacts { - assert!(last.covered_from(&owner, contact).is_some()); + assert!(last.covered_from(&owner, contact, 0).is_some()); } } @@ -4094,7 +4194,9 @@ mod tests { let info = wm.get_wallet_info(&wallet_id).expect("info"); assert_eq!(info.core_wallet.synced_height(), 100); assert!( - info.dashpay_backfill.covered_from(&owner, &first).is_none(), + info.dashpay_backfill + .covered_from(&owner, &first, 0) + .is_none(), "coverage that never reached disk must not stay in memory" ); assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); @@ -4134,8 +4236,8 @@ mod tests { "the retry carries the cursor the failed round owed" ); let record = round.dashpay_backfill.as_ref().unwrap(); - assert_eq!(record.covered_from(&owner, &first), Some(100)); - assert_eq!(record.covered_from(&owner, &later), Some(200)); + assert_eq!(record.covered_from(&owner, &first, 0), Some(100)); + assert_eq!(record.covered_from(&owner, &later, 0), Some(200)); assert_eq!( (record.floor, record.rewound_from), (100, 1_000), @@ -4220,7 +4322,7 @@ mod tests { Some(50) ); let record = round.dashpay_backfill.as_ref().unwrap(); - assert_eq!(record.covered_from(&owner, &contact), Some(50)); + assert_eq!(record.covered_from(&owner, &contact, 0), Some(50)); assert_eq!((record.floor, record.rewound_from), (50, 1_000)); } @@ -4313,7 +4415,7 @@ mod tests { .dashpay_backfill .as_ref() .unwrap() - .covered_from(&owner, &contact), + .covered_from(&owner, &contact, 0), Some(800) ); } @@ -4349,6 +4451,11 @@ mod tests { .expect("manual rescan"); } assert_eq!(synced_height(&manager, wallet_id).await, 100); + assert_eq!( + rewind_epoch(&manager, wallet_id).await, + 1, + "the reset arms the rewind barrier: advances queued before it are not stored after it" + ); establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 200, 200).await; persister.stores.lock().unwrap().clear(); @@ -4373,7 +4480,7 @@ mod tests { .dashpay_backfill .as_ref() .unwrap() - .covered_from(&owner, &contact), + .covered_from(&owner, &contact, 0), Some(200) ); } diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index 2bd8f185fe5..75a80e98a8f 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -305,6 +305,11 @@ pub struct PlatformWalletInfo { /// [`DashPayBackfillRecord`](crate::changeset::DashPayBackfillRecord) /// for how the two compose. pub dashpay_backfill: crate::changeset::DashPayBackfillRecord, + /// Orders this wallet's queued `SyncHeightAdvanced` events against + /// in-memory rewinds of its scan cursor; see + /// [`RewindBarrier`](crate::changeset::core_bridge::RewindBarrier). In + /// memory only. + pub rewind_barrier: crate::changeset::core_bridge::RewindBarrier, } /// A platform wallet that combines core UTXO functionality with identity management. diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs index 7b4ba75e2e5..9c96f984f4d 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs @@ -43,6 +43,7 @@ impl WalletInfoInterface for PlatformWalletInfo { observed_input_conflicts: Default::default(), dpns_name_states: std::collections::BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), } } @@ -58,6 +59,7 @@ impl WalletInfoInterface for PlatformWalletInfo { observed_input_conflicts: Default::default(), dpns_name_states: std::collections::BTreeMap::new(), dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), } } @@ -155,7 +157,14 @@ impl WalletInfoInterface for PlatformWalletInfo { .update_last_processed_height(current_height); } + /// The engine's only caller is `update_wallet_synced_height`, which emits + /// a `SyncHeightAdvanced` straight after this returns, under the same + /// manager write lock — so this is where emissions are counted for the + /// [`RewindBarrier`](crate::changeset::core_bridge::RewindBarrier). + /// Platform code that moves the cursor calls `core_wallet` directly and + /// emits nothing. fn update_synced_height(&mut self, current_height: u32) { + self.rewind_barrier.note_emitted(); self.core_wallet.update_synced_height(current_height); } diff --git a/packages/rs-unified-sdk-jni/src/persistence.rs b/packages/rs-unified-sdk-jni/src/persistence.rs index 40ef98b15c7..181cd53793f 100644 --- a/packages/rs-unified-sdk-jni/src/persistence.rs +++ b/packages/rs-unified-sdk-jni/src/persistence.rs @@ -855,14 +855,15 @@ unsafe extern "C" fn tramp_persist_wallet_changeset_chain_lock_height( /// Wire size of one covered contact in the flat `covered` array handed to /// `onWalletChangesetDashPayBackfill` and read back from /// `WalletRestoreData.dashPayBackfillCovered`: owner (32) ‖ contact (32) ‖ -/// `coveredFrom` (u32, little-endian). The same layout as +/// `accountIndex` (u32) ‖ `coveredFrom` (u32), both little-endian. The same +/// layout as /// `DashPayBackfillRecord::covered_bytes`, so the handler can store the array /// as one opaque blob and hand it straight back at load. -const DASHPAY_BACKFILL_COVERED_ENTRY_LEN: usize = 32 + 32 + 4; +const DASHPAY_BACKFILL_COVERED_ENTRY_LEN: usize = 32 + 32 + 4 + 4; /// Descriptor of `NativePersistenceBridge.onWalletChangesetDashPayBackfill`: /// `(walletId, floor, rewoundFrom, covered, coveredCount)`. The cover set is -/// shipped as ONE flat `byte[]` of `68·N` bytes plus a count, the same packing +/// shipped as ONE flat `byte[]` of `72·N` bytes plus a count, the same packing /// the sweep slot uses for its txids, rather than one JVM allocation per /// contact — a contact-heavy wallet records hundreds. const WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR: &str = "([BII[BI)I"; @@ -886,6 +887,7 @@ unsafe extern "C" fn tramp_persist_wallet_dashpay_backfill( for entry in entries { packed.extend_from_slice(&entry.owner_identity_id); packed.extend_from_slice(&entry.contact_identity_id); + packed.extend_from_slice(&entry.account_index.to_le_bytes()); packed.extend_from_slice(&entry.covered_from.to_le_bytes()); } let packed_arr = env.byte_array_from_slice(&packed)?; @@ -2724,7 +2726,7 @@ fn build_wallet_restore_entry( /// Read the Kotlin `WalletRestoreData.dashPayBackfill*` fields: the presence /// flag, the two scalars, and the cover set unpacked from its flat -/// `68·N`-byte blob (see [`DASHPAY_BACKFILL_COVERED_ENTRY_LEN`]) into staged +/// `72·N`-byte blob (see [`DASHPAY_BACKFILL_COVERED_ENTRY_LEN`]) into staged /// [`DashPayBackfillCoveredContactFFI`] rows. A blob whose length is not a /// whole number of entries yields `(false, 0, 0, [])` — no record — and a /// `warn`, never a partial cover set. @@ -2760,13 +2762,16 @@ fn build_dashpay_backfill_restore( .map(|chunk| { let mut owner_identity_id = [0u8; 32]; let mut contact_identity_id = [0u8; 32]; + let mut account_index = [0u8; 4]; let mut height = [0u8; 4]; owner_identity_id.copy_from_slice(&chunk[..32]); contact_identity_id.copy_from_slice(&chunk[32..64]); - height.copy_from_slice(&chunk[64..68]); + account_index.copy_from_slice(&chunk[64..68]); + height.copy_from_slice(&chunk[68..72]); DashPayBackfillCoveredContactFFI { owner_identity_id, contact_identity_id, + account_index: u32::from_le_bytes(account_index), covered_from: u32::from_le_bytes(height), } }) From 5ce582b210d5dade1be9aadd3230b7d5262f2751 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Tue, 29 Sep 2026 00:10:03 -0700 Subject: [PATCH 11/43] fix(platform-wallet): owe account-add rewinds, forward the account generation, and keep backfill writers panic-safe Fourth review round on the DashPay backfill record (dashpay/platform#4302): - key-wallet's account-add API rewinds the scan cursor to just below wallet birth without emitting or persisting it. PlatformWalletInfo's delegated add_managed_* methods now treat a lowering like any other in-memory rewind: they arm the RewindBarrier and record the lowered cursor as owed, so the next record round carries it instead of storing coverage over the host's high-water. - PlatformWalletInfo forwards account_generation (plus the receival accounts platform inserts itself, which now bump it). The trait default of a constant 0 had switched off the filter pipeline's #649 guard for every platform wallet, so a batch in flight across a contact-account registration could certify coverage the record then vouched for. - reconcile_dashpay_rescan stages the record and installs it, with the pass's marks, only after store() returns Ok; the owed cursor and extent are recorded before the store runs, so a panicking backend leaves the previous coverage plus the debt. - The adapter's commit records every accepted cursor in DurableCursors and faults the wallets a panic left unknown before the writer lock can be released, then re-raises the panic. Co-Authored-By: Claude Opus 5.5 --- .../src/changeset/core_bridge.rs | 141 +++++++++- .../src/wallet/identity/network/contacts.rs | 4 + .../src/wallet/identity/network/payments.rs | 251 +++++++++++++++--- .../src/wallet/platform_wallet_traits.rs | 78 +++++- 4 files changed, 415 insertions(+), 59 deletions(-) diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index 6a61ebad2c6..e2143a9934f 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -329,6 +329,12 @@ pub struct RewindBarrier { stale: u64, /// Bumped by every rewind. epoch: u64, + /// Receival accounts platform registered by inserting them directly + /// (not through key-wallet's `add_managed_account*`, which bump the + /// engine's own counter). Folded into the wallet's `account_generation` + /// so the filter pipeline refuses to certify a batch scanned before the + /// account's scripts were watched. + account_registrations: u64, } impl RewindBarrier { @@ -360,6 +366,17 @@ impl RewindBarrier { pub(crate) fn epoch(&self) -> u64 { self.epoch } + + /// A receival account was inserted outside key-wallet's account-add + /// API: invalidate every in-flight scan snapshot of the account set. + pub(crate) fn note_account_registered(&mut self) { + self.account_registrations = self.account_registrations.wrapping_add(1); + } + + /// See [`note_account_registered`](Self::note_account_registered). + pub(crate) fn account_registrations(&self) -> u64 { + self.account_registrations + } } /// [`spawn_wallet_event_adapter`] sharing `durable_cursors` with the other @@ -724,6 +741,7 @@ async fn run_wallet_event_adapter

( let sync_fault_for_commit = Arc::clone(&sync_fault); let fault_for_commit = Arc::clone(&fault); let freeze_for_commit = Arc::clone(&freeze_logged); + let wallet_ids_for_commit = batch_wallet_ids.clone(); let committed = tokio::task::spawn_blocking(move || { // The lock is uncontended by construction — this task is the only // writer, and one drain commits at a time — so it never blocks; @@ -738,20 +756,17 @@ async fn run_wallet_event_adapter

( // `commit_batch` returns is lost when a later store in the same // batch panics, and the panic branch would then emit the one-shot // marker a second time for a freeze already announced. - let diag = commit_batch( + let diag = commit_batch_under_writer_lock( &*persister_for_commit, batch, folded, + &wallet_ids_for_commit, &mut fault, &sync_fault_for_commit, &freeze_for_commit, &mut settled, + &mut cursors, ); - // What each host now holds, for the next writer to measure - // against. - for (wallet_id, height) in &diag.persisted_by_wallet { - cursors.insert(*wallet_id, *height); - } drop(cursors); // Hand the claim back out: the next chunk of a cancellation drain // inherits it instead of racing a fresh upgrade against the owner's @@ -881,6 +896,7 @@ async fn run_wallet_event_adapter

( /// means the rows never reached disk — the same condition that makes us /// fault the wallet — so counting it as persisted would make the trace /// contradict itself. +#[cfg_attr(not(test), allow(dead_code))] fn commit_batch

( persister: &P, batch: BTreeMap, @@ -909,6 +925,62 @@ where diag } +/// [`commit_batch`] as the adapter runs it: holding the [`DurableCursors`] +/// writer lock, and leaving that lock's state true even when a store panics. +/// +/// Every cursor a store accepted is recorded before the lock can be +/// released, and a panic faults the wallets whose outcome it left unknown +/// before it propagates. Otherwise the unwind would release the lock with +/// an accepted height unrecorded and the fault latch still clear, and a +/// reconcile entering in that window would measure a new contact against a +/// durable cursor lower than the one the host actually holds +/// (dashpay/platform#4302 review). The panic is re-raised afterwards, so +/// the adapter's `JoinError` arm still logs it and freezes the same set. +#[allow(clippy::too_many_arguments)] +fn commit_batch_under_writer_lock

( + persister: &P, + batch: BTreeMap, + folded: usize, + batch_wallet_ids: &[WalletId], + fault: &mut AdapterFaultState, + sync_fault: &AtomicBool, + freeze_logged: &AtomicBool, + settled: &mut Vec, + cursors: &mut BTreeMap, +) -> BatchDiagnostics +where + P: PlatformWalletPersistence + ?Sized, +{ + let mut diag = BatchDiagnostics::new(folded, batch.len()); + let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + for (wallet_id, wallet_batch) in batch { + commit_wallet( + persister, + wallet_id, + wallet_batch, + &mut diag, + fault, + sync_fault, + freeze_logged, + settled, + ); + } + })); + // What each host now holds, for the next writer to measure against. + for (wallet_id, height) in &diag.persisted_by_wallet { + cursors.insert(*wallet_id, *height); + } + if let Err(panic) = unwound { + for wallet_id in batch_wallet_ids { + if !settled.contains(wallet_id) { + fault.fault_wallet(*wallet_id, sync_fault); + } + } + std::panic::resume_unwind(panic); + } + diag +} + /// Commit one wallet's folded changeset — the per-wallet unit of /// [`commit_batch`]. /// @@ -5006,6 +5078,63 @@ mod tests { assert_eq!(next[&wallet_id].core.synced_height, Some(1_200)); } + /// A store panicking mid-batch must not release the writer lock with the + /// lock's state behind reality: wallet A's accepted cursor is recorded, + /// and wallet B — whose outcome the panic left unknown — is faulted, + /// both before the panic propagates to the adapter's async `JoinError` + /// arm, so the next writer to take the lock already sees them + /// (dashpay/platform#4302 review). + #[test] + fn a_panicking_commit_records_accepted_cursors_and_faults_before_the_lock_is_released() { + let (obs_tx, _obs_rx) = tokio::sync::mpsc::unbounded_channel(); + let persister = ProbePersister::new(obs_tx); + let a: WalletId = [1u8; 32]; + let b: WalletId = [2u8; 32]; + persister.panic_next(b); + let watermark = |height| super::WalletBatch { + core: CoreChangeSet { + synced_height: Some(height), + ..CoreChangeSet::default() + }, + ..Default::default() + }; + let mut batch = BTreeMap::new(); + batch.insert(a, watermark(1_000)); + batch.insert(b, watermark(500)); + + let mut fault = super::AdapterFaultState::default(); + let sync_fault = AtomicBool::new(false); + let freeze_logged = AtomicBool::new(false); + let mut settled = Vec::new(); + let mut cursors: BTreeMap = BTreeMap::from([(a, 100), (b, 100)]); + let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + super::commit_batch_under_writer_lock( + &persister, + batch, + 2, + &[a, b], + &mut fault, + &sync_fault, + &freeze_logged, + &mut settled, + &mut cursors, + ) + })); + assert!(unwound.is_err(), "the panic still propagates"); + assert_eq!( + cursors.get(&a), + Some(&1_000), + "A's accepted cursor is recorded" + ); + assert_eq!(cursors.get(&b), Some(&100), "B's store never returned"); + assert!( + fault.is_faulted(&b), + "B is faulted before the lock is released" + ); + assert!(!fault.is_faulted(&a), "A settled"); + assert!(sync_fault.load(Ordering::Relaxed)); + } + /// A batch that holds a pre-rewind advance and then the rescan's first /// one ends on the rescan's height: the newer epoch replaces the older /// advance rather than max-merging with it, which would keep the higher, 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 a19288f4128..04e6de22de9 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs @@ -247,6 +247,10 @@ impl DashPayView<'_, B> { "Failed to register contact account: {e}" )) })?; + // A filter batch in flight right now was scanned without this + // account's scripts: bump the account generation so the pipeline does + // not certify it, and the range is rescanned with the account watched. + info.rewind_barrier.note_account_registered(); tracing::info!( our_identity = %our_identity_id, diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 921905de0e6..a8f3dab8aad 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -269,11 +269,6 @@ impl DashPayView<'_, B> { info.rewind_barrier.arm(); } let triggered = to_mark.len(); - for (owner, contact, _, _) in &to_mark { - if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { - managed.dashpay_rescan_triggered_mut().insert(*contact); - } - } if let Some(floor) = floor { tracing::info!( wallet_id = %hex::encode(self.wallet_id), @@ -296,18 +291,21 @@ impl DashPayView<'_, B> { // - the round carries the floor this pass rewound to, or failing that // a cursor an earlier failed round still owes, so coverage is never // stored beside a durable cursor the in-memory rewind left behind; - // - if the store fails, the in-memory record is put back, the pass's - // marks are dropped so the next pass re-attempts, and the cursor - // this round owed is remembered for that retry; + // - the pass's record is staged on a copy and installed, with the + // pass's marks, only when the store returns `Ok`. The cursor and + // extent the round owes are recorded on the live record BEFORE the + // store runs, so a store that fails — or panics, which unwinds + // through here and releases the manager and cursor locks — leaves + // the previous coverage plus the debt for the next pass to carry, + // never coverage that did not reach disk; // - while the manager's persistence fault is latched the adapter is // holding the durable cursor back at the last fully persisted // height, and a cursor written from here could advance it past rows // that never landed — so nothing is stored at all: the in-memory // rewind still runs the rescan, and the next launch re-runs it, // which is the pre-record behaviour. - let previous_record = info.dashpay_backfill.clone(); - let record = &mut info.dashpay_backfill; - record.retain(|owner, contact, account_index| { + let mut staged = info.dashpay_backfill.clone(); + staged.retain(|owner, contact, account_index| { receival_accounts .iter() .any(|(o, c, i)| (o, c, *i) == (owner, contact, account_index)) @@ -315,18 +313,18 @@ impl DashPayView<'_, B> { // This pass's extent, widened by one a failed earlier round still owes. let extent = match ( floor.map(|floor| (floor, synced_height)), - record.unpersisted_extent.take(), + staged.unpersisted_extent.take(), ) { (Some((f1, r1)), Some((f2, r2))) => Some((f1.min(f2), r1.max(r2))), (this_pass, owed) => this_pass.or(owed), }; match extent { Some((extent_floor, rewound_from)) => { - record.record_pass(rewound_from, Some(extent_floor), to_mark.iter().copied()) + staged.record_pass(rewound_from, Some(extent_floor), to_mark.iter().copied()) } - None => record.record_pass(synced_height, None, to_mark.iter().copied()), + None => staged.record_pass(synced_height, None, to_mark.iter().copied()), }; - let owed = record.unpersisted_cursor.take(); + let owed = staged.unpersisted_cursor.take(); let needed_cursor = match (floor, owed) { (Some(floor), Some(owed)) => Some(floor.min(owed)), (Some(floor), None) => Some(floor), @@ -339,6 +337,10 @@ impl DashPayView<'_, B> { let cursor_to_write = needed_cursor.filter(|cursor| durable.is_none_or(|durable| *cursor < durable)); let cursor_now = floor.unwrap_or(synced_height); + // The debt, recorded before the store so no way out of it loses it. + info.dashpay_backfill.unpersisted_cursor = + Some(needed_cursor.map_or(cursor_now, |cursor| cursor.min(cursor_now))); + info.dashpay_backfill.unpersisted_extent = extent; let faulted = self.sync_fault.load(std::sync::atomic::Ordering::Relaxed); let stored = if faulted { Err(None) @@ -349,39 +351,34 @@ impl DashPayView<'_, B> { synced_height_is_rewind: true, ..CoreChangeSet::default() }), - dashpay_backfill: Some(record.clone()), + dashpay_backfill: Some(staged.clone()), ..PlatformWalletChangeSet::default() }; self.persister.store(changeset).map_err(Some) }; - if stored.is_ok() { - if let Some(cursor) = cursor_to_write { - durable_cursors.insert(self.wallet_id, cursor); - } - } - if let Err(error) = stored { - match error { - Some(error) => tracing::warn!( - wallet_id = %hex::encode(self.wallet_id), - error = %error, - "DashPay rescan: failed to persist the backfill record; the next pass \ - retries and the next launch re-runs this backfill" - ), - None => tracing::warn!( - wallet_id = %hex::encode(self.wallet_id), - "DashPay rescan: persistence fault latched; not recording backfill coverage \ - over a held-back cursor — the next launch re-runs this backfill" - ), - } - info.dashpay_backfill = previous_record; - info.dashpay_backfill.unpersisted_cursor = - Some(needed_cursor.map_or(cursor_now, |cursor| cursor.min(cursor_now))); - info.dashpay_backfill.unpersisted_extent = extent; - for (owner, contact, _, _) in &to_mark { - if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { - managed.dashpay_rescan_triggered_mut().remove(contact); + match stored { + Ok(()) => { + info.dashpay_backfill = staged; + if let Some(cursor) = cursor_to_write { + durable_cursors.insert(self.wallet_id, cursor); + } + for (owner, contact, _, _) in &to_mark { + if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { + managed.dashpay_rescan_triggered_mut().insert(*contact); + } } } + Err(Some(error)) => tracing::warn!( + wallet_id = %hex::encode(self.wallet_id), + error = %error, + "DashPay rescan: failed to persist the backfill record; the next pass \ + retries and the next launch re-runs this backfill" + ), + Err(None) => tracing::warn!( + wallet_id = %hex::encode(self.wallet_id), + "DashPay rescan: persistence fault latched; not recording backfill coverage \ + over a held-back cursor — the next launch re-runs this backfill" + ), } Ok(floor) } @@ -1865,6 +1862,8 @@ mod tests { stores: Mutex>, /// When set, every `store` is refused (and not recorded). fail_stores: Mutex, + /// When set, the next `store` panics — a backend that dies mid-write. + panic_next_store: Mutex, } impl PlatformWalletPersistence for RecordingPersister { @@ -1876,6 +1875,9 @@ mod tests { if *self.fail_stores.lock().unwrap() { return Err(PersistenceError::backend("injected store failure")); } + if std::mem::take(&mut *self.panic_next_store.lock().unwrap()) { + panic!("recording persister: injected store panic"); + } self.stores.lock().unwrap().push((wallet_id, changeset)); Ok(()) } @@ -4159,6 +4161,171 @@ mod tests { assert!(record.is_complete(1_560_731)); } + /// key-wallet's account-add API rewinds the cursor to just below wallet + /// birth without emitting or persisting anything. That lowering is owed + /// to the host like any other in-memory rewind: a later record-only + /// round (a contact forward-covered at the lowered cursor) must carry it, + /// or the host keeps its high-water beside the new coverage and the next + /// launch skips the rescan (dashpay/platform#4302 review). + #[tokio::test] + async fn an_account_add_rewind_is_owed_to_the_next_record_round() { + use key_wallet::account::{AccountType, StandardAccountType}; + use key_wallet::wallet::managed_wallet_info::managed_account_operations::ManagedAccountOperations; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + set_synced_height(&manager, wallet_id, 1_000).await; + { + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.core_wallet.metadata.birth_height = 101; + ManagedAccountOperations::add_managed_account_from_xpub( + info, + AccountType::Standard { + index: 9, + standard_account_type: StandardAccountType::BIP44Account, + }, + test_receiving_xpub(&owner, &Identifier::from([0xEE; 32])), + ) + .expect("add account"); + assert_eq!( + info.core_wallet.metadata.synced_height, 100, + "rewound to birth - 1" + ); + assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); + assert_eq!(info.rewind_barrier.epoch(), 1, "and the barrier is armed"); + } + + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 200, 200).await; + persister.stores.lock().unwrap().clear(); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None, + "forward-covered at the lowered cursor" + ); + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + let core = round + .core + .as_ref() + .expect("the owed cursor rides the round"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + } + + /// Registering a receival account bumps the wallet's account generation, + /// which the filter pipeline compares per batch: a batch scanned before + /// the registration can no longer certify coverage for the new account + /// set, so forward coverage recorded for the account is backed by a scan + /// that actually watched it (dashpay/platform#4302 review). + #[tokio::test] + async fn receival_registration_invalidates_in_flight_scans() { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let (manager, _persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let generation = |wm: &key_wallet_manager::WalletManager< + crate::wallet::platform_wallet::PlatformWalletInfo, + >| { + WalletInfoInterface::account_generation(wm.get_wallet_info(&wallet_id).expect("info")) + }; + let before = generation(&*manager.wallet_manager.read().await); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + wallet + .identity() + .dashpay() + .register_contact_account(&owner, &contact, 0, test_receiving_xpub(&owner, &contact)) + .await + .expect("register"); + let wm = manager.wallet_manager.read().await; + assert_ne!(generation(&wm), before); + assert_eq!( + key_wallet_manager::WalletInterface::wallet_account_generation(&*wm, &wallet_id), + generation(&wm), + "the engine sees the forwarded generation, not the trait's constant 0" + ); + } + + /// A backend that panics inside the record round's `store()` unwinds + /// through the reconcile and releases its locks. The pass's coverage + /// was only staged, and the cursor it owed was recorded before the + /// store ran, so the next round — a forward-covered contact with no + /// cursor of its own — carries that cursor beside both contacts' + /// coverage instead of storing coverage over the host's high-water + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_panicking_record_store_leaves_no_coverage_and_keeps_the_cursor_it_owed() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let first = Identifier::from([0xBB; 32]); + let later = Identifier::from([0xCC; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, first, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + persister.stores.lock().unwrap().clear(); + *persister.panic_next_store.lock().unwrap() = true; + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let panicking = { + let wallet = Arc::clone(&wallet); + tokio::spawn( + async move { wallet.identity().dashpay().reconcile_dashpay_rescan().await }, + ) + }; + assert!( + panicking.await.expect_err("the store panicked").is_panic(), + "the injected panic propagates" + ); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert!( + info.dashpay_backfill + .covered_from(&owner, &first, 0) + .is_none(), + "staged coverage never reached memory" + ); + assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(), + Some(1_000), + "the host still holds its high-water" + ); + } + + establish_receival_contact(&manager, &persister, wallet_id, owner, later, 200, 200).await; + persister.stores.lock().unwrap().clear(); + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("next pass"); + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + let core = round + .core + .as_ref() + .expect("the owed cursor rides the round"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + let record = round.dashpay_backfill.as_ref().unwrap(); + assert_eq!(record.covered_from(&owner, &first, 0), Some(100)); + assert_eq!(record.covered_from(&owner, &later, 0), Some(200)); + } + /// A record round that fails to store must not leave its coverage /// behind in memory: a later round (a forward-covered contact, no /// cursor of its own) would then store the whole record — this diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs index 9c96f984f4d..01a1b7caa57 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs @@ -148,6 +148,20 @@ impl WalletInfoInterface for PlatformWalletInfo { self.core_wallet.last_processed_height() } + /// Forwarded, plus the receival accounts platform inserts itself: the + /// filter pipeline snapshots this per batch and refuses to certify a + /// batch whose snapshot predates an account add, because the scan never + /// tested that account's scripts (dashpay/rust-dashcore#649). The trait + /// default is a constant `0`, which turned the guard off for every + /// platform wallet — so a batch in flight across a contact-account + /// registration could certify coverage the DashPay backfill record then + /// vouched for (dashpay/platform#4302 review). + fn account_generation(&self) -> u64 { + self.core_wallet + .account_generation() + .wrapping_add(self.rewind_barrier.account_registrations()) + } + fn synced_height(&self) -> CoreBlockHeight { self.core_wallet.synced_height() } @@ -246,7 +260,10 @@ impl ManagedAccountOperations for PlatformWalletInfo { wallet: &Wallet, account_type: AccountType, ) -> key_wallet::Result<()> { - self.core_wallet.add_managed_account(wallet, account_type) + let before = self.core_wallet.metadata.synced_height; + let added = self.core_wallet.add_managed_account(wallet, account_type); + self.note_account_add_rewind(before); + added } fn add_managed_account_from_xpub( @@ -254,8 +271,12 @@ impl ManagedAccountOperations for PlatformWalletInfo { account_type: AccountType, account_xpub: ExtendedPubKey, ) -> key_wallet::Result<()> { - self.core_wallet - .add_managed_account_from_xpub(account_type, account_xpub) + let before = self.core_wallet.metadata.synced_height; + let added = self + .core_wallet + .add_managed_account_from_xpub(account_type, account_xpub); + self.note_account_add_rewind(before); + added } #[cfg(feature = "bls")] @@ -264,8 +285,12 @@ impl ManagedAccountOperations for PlatformWalletInfo { wallet: &Wallet, account_type: AccountType, ) -> key_wallet::Result<()> { - self.core_wallet - .add_managed_bls_account(wallet, account_type) + let before = self.core_wallet.metadata.synced_height; + let added = self + .core_wallet + .add_managed_bls_account(wallet, account_type); + self.note_account_add_rewind(before); + added } #[cfg(feature = "bls")] @@ -274,8 +299,12 @@ impl ManagedAccountOperations for PlatformWalletInfo { account_type: AccountType, bls_public_key: [u8; 48], ) -> key_wallet::Result<()> { - self.core_wallet - .add_managed_bls_account_from_public_key(account_type, bls_public_key) + let before = self.core_wallet.metadata.synced_height; + let added = self + .core_wallet + .add_managed_bls_account_from_public_key(account_type, bls_public_key); + self.note_account_add_rewind(before); + added } #[cfg(feature = "eddsa")] @@ -284,8 +313,12 @@ impl ManagedAccountOperations for PlatformWalletInfo { wallet: &Wallet, account_type: AccountType, ) -> key_wallet::Result<()> { - self.core_wallet - .add_managed_eddsa_account(wallet, account_type) + let before = self.core_wallet.metadata.synced_height; + let added = self + .core_wallet + .add_managed_eddsa_account(wallet, account_type); + self.note_account_add_rewind(before); + added } #[cfg(feature = "eddsa")] @@ -294,8 +327,31 @@ impl ManagedAccountOperations for PlatformWalletInfo { account_type: AccountType, ed25519_public_key: [u8; 32], ) -> key_wallet::Result<()> { - self.core_wallet - .add_managed_eddsa_account_from_public_key(account_type, ed25519_public_key) + let before = self.core_wallet.metadata.synced_height; + let added = self + .core_wallet + .add_managed_eddsa_account_from_public_key(account_type, ed25519_public_key); + self.note_account_add_rewind(before); + added + } +} + +impl PlatformWalletInfo { + /// key-wallet's account-add API rewinds the scan cursor to just below + /// wallet birth (the new account has no filter coverage). That lowering + /// emits nothing and is not persisted here, so treat it like any other + /// in-memory rewind: advances queued before it must not be stored after + /// it, and the next DashPay backfill record round owes the host the + /// lowered cursor — otherwise a record-only round could store coverage + /// beside a host cursor that never came down (dashpay/platform#4302 + /// review). + fn note_account_add_rewind(&mut self, before: u32) { + let after = self.core_wallet.metadata.synced_height; + if after < before { + self.rewind_barrier.arm(); + let owed = &mut self.dashpay_backfill.unpersisted_cursor; + *owed = Some(owed.map_or(after, |cursor| cursor.min(after))); + } } } From 33612c94fa7b0464ce946594555b03d3431a7cc5 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Tue, 29 Sep 2026 13:30:36 -0700 Subject: [PATCH 12/43] fix(platform-wallet): seed a recreated wallet's durable cursor from the host and owe its lower built cursor Fifth review round on the DashPay backfill record (dashpay/platform#4302). A fresh manager recreating a wallet the host already holds seeded DurableCursors from the freshly built cursor (birth - 1), while the host kept its row's higher syncedHeight (registration writes no core cursor). A contact forward-covered at the built cursor was then stored with no cursor, and a reload skipped its backfill. register_wallet now reads the host's cursor for every persisted wallet from the load it already performs, seeds DurableCursors with it, and when the built cursor is lower records it as owed so the next record round carries it. Co-Authored-By: Claude Opus 5.5 --- .../src/manager/wallet_lifecycle.rs | 41 +++++- .../src/wallet/identity/network/payments.rs | 120 ++++++++++++++++++ 2 files changed, 157 insertions(+), 4 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 698f15bbe6f..853c86f4d32 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -414,12 +414,25 @@ impl PlatformWalletManager

{ // than sliced here: the authoritative id is the one that call returns, // and it is deliberately not assumed equal to `registration_wallet_id` // (see the divergence branch below). + // + // The same read also yields the cursor the host holds for every + // persisted wallet: a registration that recreates a wallet the host + // already has must measure durable writes against that cursor, not + // the one it just built (see the seed below). let load_persister: Arc = Arc::clone(&self.persister) as _; - let mut persisted_platform_addresses = + let (mut persisted_platform_addresses, host_cursors) = match super::run_blocking_load(move || load_persister.load()).await { Ok(crate::changeset::ClientStartState { - platform_addresses, .. - }) => platform_addresses, + platform_addresses, + wallets, + .. + }) => ( + platform_addresses, + wallets + .iter() + .map(|(id, state)| (*id, state.wallet_info.metadata.synced_height)) + .collect::>(), + ), Err(e) => { tracing::error!( wallet_id = %hex::encode(registration_wallet_id), @@ -447,6 +460,16 @@ impl PlatformWalletManager

{ // overwrite it with the creation one. An entry a removed or // rolled-back registration left behind is overwritten here the same // way, so it is never read against a live wallet. + // + // A registration can recreate a wallet the host already holds (the + // same seed added again on a fresh manager). The host keeps that + // row's cursor — registration writes no core cursor, and a host + // preserves `syncedHeight` on the metadata upsert — so the seed is + // the host's cursor, not the freshly built one. When the built cursor + // is lower, the wallet is in effect reset in memory only: that + // reset is owed to the host like any other, and the next DashPay + // backfill record round carries it, so coverage never reaches disk + // beside the host's higher cursor (dashpay/platform#4302 review). let created_cursor = platform_info.core_wallet.metadata.synced_height; let wallet_id = { let mut durable_cursors = self.durable_cursors.lock().await; @@ -460,7 +483,17 @@ impl PlatformWalletManager

{ other )), })?; - durable_cursors.insert(wallet_id, created_cursor); + let host_cursor = host_cursors + .get(&wallet_id) + .copied() + .unwrap_or(created_cursor); + if created_cursor < host_cursor { + if let Some(info) = wm.get_wallet_info_mut(&wallet_id) { + let owed = &mut info.dashpay_backfill.unpersisted_cursor; + *owed = Some(owed.map_or(created_cursor, |cursor| cursor.min(created_cursor))); + } + } + durable_cursors.insert(wallet_id, host_cursor); wallet_id }; diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index a8f3dab8aad..1f2fbd79b12 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -4220,6 +4220,126 @@ mod tests { assert!(core.synced_height_is_rewind); } + /// A fresh manager recreating a wallet the host already holds (the same + /// seed added again) builds a cursor below the host's — registration + /// writes no core cursor and the host keeps its row's `syncedHeight`. + /// The durable cursor is seeded from what the host holds, and the lower + /// built cursor is owed like any in-memory reset: the first record round + /// carries it, so a contact forward-covered at the built cursor is never + /// stored beside the host's higher one, and a reload rewinds for nothing + /// it should not (dashpay/platform#4302 review). + #[tokio::test] + async fn recreating_a_persisted_wallet_owes_its_lower_cursor_to_the_host() { + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + // What the host holds: this seed's wallet, scanned to 1000. + let (donor, _donor_persister, wallet_id) = make_wallet().await; + let (wallet, mut managed) = { + let wm = donor.wallet_manager.read().await; + ( + wm.get_wallet(&wallet_id).expect("wallet").clone(), + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .clone(), + ) + }; + managed.metadata.synced_height = 1_000; + let host = Arc::new(ReloadPersister { + wallet, + managed, + identities: std::collections::BTreeMap::new(), + backfill: Default::default(), + stores: Mutex::default(), + }); + + // A fresh manager recreates it from the seed, born at 101. + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + let seed = Mnemonic::from_phrase(TEST_MNEMONIC) + .expect("valid mnemonic") + .to_seed(""); + let recreated = manager + .create_wallet_from_seed_bytes( + Network::Testnet, + &seed, + WalletAccountCreationOptions::Default, + Some(101), + ) + .await + .expect("recreate"); + assert_eq!(recreated.wallet_id(), wallet_id); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.metadata.synced_height, 100); + assert_eq!( + info.dashpay_backfill.unpersisted_cursor, + Some(100), + "the lower built cursor is owed to the host" + ); + } + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(), + Some(1_000), + "the durable cursor is the host's, not the built one" + ); + + // A contact forward-covered at the built cursor. + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let iw = recreated.identity(); + iw.dashpay() + .register_contact_account(&owner, &contact, 0, test_receiving_xpub(&owner, &contact)) + .await + .expect("register receival account"); + { + let p = WalletPersister::new(wallet_id, Arc::clone(&host) as _); + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 200, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 200, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + } + host.stores.lock().unwrap().clear(); + assert_eq!( + iw.dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None, + "forward-covered at the built cursor" + ); + let stores = host.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + let core = round + .core + .as_ref() + .expect("the owed reset rides the record round"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + assert_eq!( + round + .dashpay_backfill + .as_ref() + .unwrap() + .covered_from(&owner, &contact, 0), + Some(200) + ); + } + /// Registering a receival account bumps the wallet's account generation, /// which the filter pipeline compares per batch: a batch scanned before /// the registration can no longer certify coverage for the new account From 011fcf19267737c5d4f19b907077312fc8011501 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 30 Sep 2026 14:37:27 -0700 Subject: [PATCH 13/43] fix(platform-wallet)!: deliver backfill coverage only on atomic rounds, keep maintenance and recreation state Sixth review round on the DashPay backfill record (dashpay/platform#4302): - FFI: the backfill record reaches a host only when it attests ATOMIC_CHANGESETS. Callbacks that run after the record (identities, payments, tokens, shielded) can still fail the round; a non-atomic host would keep the cursor and the coverage from a round Rust reports as failed. The Kotlin host advertises atomic rounds and wires begin/end, so Android keeps the record. - Duplicated covered accounts in a restored record keep their HIGHEST covered_from (both the Rust and FFI decoders), so an ambiguous record fails closed. - A recreated wallet keeps the host's backfill record instead of starting from an empty one that re-arms every covered contact. - The reconcile no longer returns early when no contact needs handling but coverage for a removed receival account must be pruned or an owed cursor or extent must reach the host: it runs a record-only round. - A wallet removed and registered again under the same id inherits the removed wallet's RewindBarrier state (in-flight advances and epoch), so the shared adapter drops the predecessor's queued advances and strips its projected ones at commit. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/persistence.rs | 62 ++++- .../src/changeset/core_bridge.rs | 80 +++++++ .../src/changeset/dashpay_backfill.rs | 12 +- .../rs-platform-wallet/src/manager/load.rs | 3 +- .../rs-platform-wallet/src/manager/mod.rs | 46 ++++ .../src/manager/wallet_lifecycle.rs | 103 ++++++-- .../src/wallet/identity/network/payments.rs | 220 +++++++++++++++--- 7 files changed, 458 insertions(+), 68 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 46c752945bf..30e1e52cedb 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -2243,7 +2243,17 @@ impl PlatformWalletPersistence for FFIPersister { // `Ok`, the reconcile records its cursor as durable, and a later // record-only round would then deliver coverage beside a host cursor // that never moved. - let host_stores_cursors = self.callbacks.on_persist_wallet_changeset_fn.is_some(); + // + // Nor does a host whose rounds are not atomic: callbacks that run + // after this one (identities, payments, tokens, shielded) can still + // fail the round, and such a host would keep the cursor and the + // coverage from a round Rust reports as failed — coverage that then + // suppresses the rescan on the next launch. Only a host attesting + // `ATOMIC_CHANGESETS` rolls the whole round back, record included. + let host_stores_cursors = self.callbacks.on_persist_wallet_changeset_fn.is_some() + && self + .persistence_capabilities() + .contains(PersistenceCapabilities::ATOMIC_CHANGESETS); let backfill_cursor_owed = changeset .core .as_ref() @@ -5355,7 +5365,9 @@ fn decode_dashpay_backfill( }) .collect(); // Canonical order and one entry per account, whatever the host stored. - covered.sort_by_key(DashPayBackfillCoveredContact::key); + // A duplicated account keeps its HIGHEST `covered_from`, so an ambiguous + // record fails closed. + covered.sort_by_key(|entry| (entry.key(), std::cmp::Reverse(entry.covered_from))); covered.dedup_by(|a, b| a.key() == b.key()); DashPayBackfillRecord { floor: entry.dashpay_backfill_floor, @@ -8432,12 +8444,14 @@ mod tests { let sink = Sink::default(); let callbacks = PersistenceCallbacks { context: &sink as *const Sink as *mut c_void, + on_changeset_begin_fn: Some(noop_begin), + on_changeset_end_fn: Some(noop_end), on_persist_wallet_changeset_fn: Some(record_changeset), ..PersistenceCallbacks::default() }; let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( callbacks, - PersistenceCapabilities::NONE, + PersistenceCapabilities::ATOMIC_CHANGESETS, PersistenceExtensionCallbacks { wallet_dashpay_backfill: Some(record_backfill), ..Default::default() @@ -8553,8 +8567,10 @@ mod tests { /// (dashpay/platform#4302 review). Withheld when the changeset callback /// fails for a round that carries a cursor, and on every round — /// record-only ones included — for a host with no changeset callback, - /// which holds no cursor a record could vouch for. Delivered for a - /// record-only round on a host that stores cursors. + /// which holds no cursor a record could vouch for, or whose rounds are + /// not atomic, so a later callback's failure could not roll the record + /// back. Delivered for a record-only round on an atomic host that stores + /// cursors. #[test] fn a_backfill_record_is_withheld_when_its_cursor_did_not_reach_the_host() { use platform_wallet::changeset::{CoreChangeSet, DashPayBackfillRecord}; @@ -8591,23 +8607,33 @@ mod tests { .fetch_add(1, Ordering::SeqCst); 0 } - fn persister( + fn persister_with( sink: &Sink, changeset: Option, + atomic: bool, ) -> FFIPersister { FFIPersister::new_with_persistence_capabilities_and_extensions( PersistenceCallbacks { context: sink as *const Sink as *mut c_void, + on_changeset_begin_fn: atomic.then_some(noop_begin as _), + on_changeset_end_fn: atomic.then_some(noop_end as _), on_persist_wallet_changeset_fn: changeset, ..PersistenceCallbacks::default() }, - PersistenceCapabilities::NONE, + if atomic { + PersistenceCapabilities::ATOMIC_CHANGESETS + } else { + PersistenceCapabilities::NONE + }, PersistenceExtensionCallbacks { wallet_dashpay_backfill: Some(count_backfill), ..Default::default() }, ) } + let persister = |sink: &Sink, changeset: Option| { + persister_with(sink, changeset, true) + }; type PersistWalletChangesetFnForTest = unsafe extern "C" fn(*mut c_void, *const u8, *const WalletChangeSetFFI) -> i32; let rewind_round = || PlatformWalletChangeSet { @@ -8654,6 +8680,19 @@ mod tests { .store([1u8; 32], record_only_round()) .expect("record-only round"); assert_eq!(sink.backfills.load(Ordering::SeqCst), 2); + + // A host whose rounds are not atomic cannot roll back a cursor and a + // record whose round a later callback failed: it gets neither round's + // record. + let sink = Sink::default(); + let non_atomic = persister_with(&sink, Some(accept_changeset), false); + non_atomic + .store([1u8; 32], rewind_round()) + .expect("rewind round"); + non_atomic + .store([1u8; 32], record_only_round()) + .expect("record-only round"); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 0); } /// The restore side of dashpay/platform#4302: a wallet-restore entry @@ -8682,7 +8721,8 @@ mod tests { assert_eq!((decoded.floor, decoded.rewound_from), (10, 20)); // Out of order and duplicated, as a host might hand it back: the - // decoded record is sorted with one entry per pair. + // decoded record is sorted with one entry per account, the highest + // height of a duplicate kept. let covered = [ DashPayBackfillCoveredContactFFI { owner_identity_id: [9u8; 32], @@ -8727,7 +8767,7 @@ mod tests { dpp::prelude::Identifier::from([9u8; 32]), dpp::prelude::Identifier::from([2u8; 32]), 0, - 2_300_000, + 2_999_999, ), ], ); @@ -8738,8 +8778,8 @@ mod tests { &dpp::prelude::Identifier::from([2u8; 32]), 0 ), - Some(2_300_000), - "the first of a duplicated pair wins" + Some(2_999_999), + "a duplicated account keeps its highest height, so the record fails closed" ); } diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index e2143a9934f..ae1ea1b3f8d 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -367,6 +367,23 @@ impl RewindBarrier { self.epoch } + /// What a removed wallet leaves for a successor registered under the same + /// id: its advances still in flight, and its epoch. + pub(crate) fn retire(&self) -> (u64, u64) { + (self.in_flight, self.epoch) + } + + /// Take over a removed predecessor's [`retire`](Self::retire) state. The + /// shared adapter can still hold advances the predecessor emitted, queued + /// or already projected; all of them predate this wallet's own scan, so + /// the queued ones are counted as stale and the epoch moves past the + /// predecessor's, which strips the projected ones at commit. + pub(crate) fn inherit(&mut self, (in_flight, epoch): (u64, u64)) { + self.in_flight = self.in_flight.saturating_add(in_flight); + self.stale = self.stale.saturating_add(in_flight); + self.epoch = self.epoch.wrapping_add(epoch).wrapping_add(1); + } + /// A receival account was inserted outside key-wallet's account-add /// API: invalidate every in-flight scan snapshot of the account set. pub(crate) fn note_account_registered(&mut self) { @@ -5078,6 +5095,69 @@ mod tests { assert_eq!(next[&wallet_id].core.synced_height, Some(1_200)); } + /// A wallet removed and registered again under the same id shares the + /// adapter with its predecessor, which can still hold that predecessor's + /// advances — queued, or already projected. The successor inherits the + /// predecessor's barrier: the queued ones drop as stale and the + /// projected ones fail the epoch check at commit, even once the new + /// scan has climbed above them (dashpay/platform#4302 review). + #[tokio::test] + async fn a_same_id_successor_drops_its_predecessors_queued_and_projected_advances() { + use crate::wallet::core::WalletGeneration; + use crate::wallet::identity::IdentityManager; + use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; + + let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + engine_advance(&manager, wallet_id, 700).await; + let mut projected = project(&manager, &mut receiver, 1).await; // in an adapter batch + engine_advance(&manager, wallet_id, 1_000).await; // still queued + + // Removed, then registered again under the same id. + let (wallet, retired) = { + let (wallet, info) = manager + .write() + .await + .remove_wallet(&wallet_id) + .expect("remove"); + (wallet, info.rewind_barrier.retire()) + }; + let mut successor = PlatformWalletInfo { + observed_input_conflicts: Default::default(), + core_wallet: ManagedWalletInfo::from_wallet(&wallet, 0), + generation: Arc::new(WalletGeneration::new()), + identity_manager: IdentityManager::new(), + tracked_asset_locks: BTreeMap::new(), + dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), + }; + successor.rewind_barrier.inherit(retired); + assert_eq!( + manager + .write() + .await + .insert_wallet(wallet, successor) + .expect("reinsert"), + wallet_id + ); + engine_advance(&manager, wallet_id, 1_200).await; // the successor's own scan + + super::drop_watermarks_above_the_live_cursor(&manager, &mut projected).await; + assert_eq!( + projected[&wallet_id].core.synced_height, None, + "the predecessor's projected advance is stripped although 1200 is above it" + ); + let mut queued = project(&manager, &mut receiver, 1).await; + assert_eq!( + queued[&wallet_id].core.synced_height, None, + "the predecessor's queued advance drops as stale" + ); + super::drop_watermarks_above_the_live_cursor(&manager, &mut queued).await; + let mut own = project(&manager, &mut receiver, 1).await; + super::drop_watermarks_above_the_live_cursor(&manager, &mut own).await; + assert_eq!(own[&wallet_id].core.synced_height, Some(1_200)); + } + /// A store panicking mid-batch must not release the writer lock with the /// lock's state behind reality: wallet A's accepted cursor is recorded, /// and wallet B — whose outcome the panic left unknown — is faulted, diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 5d989220a3c..3215d069f21 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -312,7 +312,10 @@ impl DashPayBackfillRecord { }) .collect(); // Canonical order and one entry per account, whatever the host stored. - covered.sort_by_key(DashPayBackfillCoveredContact::key); + // A duplicated account keeps its HIGHEST `covered_from`: an ambiguous + // record must fail closed, and the higher height is the one that + // re-arms more checkpoints. + covered.sort_by_key(|entry| (entry.key(), std::cmp::Reverse(entry.covered_from))); covered.dedup_by(|a, b| a.key() == b.key()); Some(Self { floor, @@ -453,7 +456,12 @@ mod tests { let record = DashPayBackfillRecord::from_parts(100, 1_000, &bytes).expect("decodes"); assert_eq!(record.covered.len(), 2); assert_eq!(record.covered[0].owner, id(1)); - assert_eq!(record.covered_from(&id(5), &id(6), 0), Some(700)); + assert_eq!( + record.covered_from(&id(5), &id(6), 0), + Some(900), + "a duplicated account keeps its highest height, re-arming more" + ); + assert!(!record.covers(&id(5), &id(6), 0, 800)); } #[test] diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index c8b3ee7cd9e..c9fddf71d5e 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -295,6 +295,7 @@ impl PlatformWalletManager

{ break 'load; } durable_cursors.insert(wallet_id, loaded_cursor); + self.inherit_rewind_barrier(&mut wm, &wallet_id); } inserted_in_manager.push(wallet_id); @@ -485,7 +486,7 @@ impl PlatformWalletManager

{ } continue; } - if let Err(e) = wm.remove_wallet(id) { + if let Err(e) = self.remove_from_wallet_manager(&mut wm, id) { tracing::warn!( wallet_id = %hex::encode(id), error = %e, diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index a952440fcc6..47accb931d8 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -445,6 +445,11 @@ pub struct PlatformWalletManager { /// the wallet-event adapter and the DashPay rescan reconcile — the lock /// that orders their cursor writes. See [`DurableCursors`]. pub(super) durable_cursors: DurableCursors, + /// The [`RewindBarrier`](crate::changeset::core_bridge::RewindBarrier) + /// state of removed wallets, keyed by id, for a successor registered + /// under the same id to inherit: the shared event adapter can still hold + /// advances the removed wallet emitted (dashpay/platform#4302 review). + pub(super) retired_barriers: std::sync::Mutex>, /// Per-WALLET in-broadcast fence maps, handed to every /// [`WalletGeneration`](crate::wallet::core::WalletGeneration) registered /// under each id. @@ -616,10 +621,51 @@ impl PlatformWalletManager

{ registry, sync_fault, durable_cursors, + retired_barriers: std::sync::Mutex::new(std::collections::BTreeMap::new()), in_broadcast_fences: std::sync::Mutex::new(std::collections::BTreeMap::new()), } } + /// Remove `wallet_id` from the inner manager, keeping its rewind + /// barrier's in-flight state for a successor registered under the same + /// id (see [`Self::inherit_rewind_barrier`]). + pub(super) fn remove_from_wallet_manager( + &self, + wm: &mut WalletManager, + wallet_id: &WalletId, + ) -> Result<(), key_wallet_manager::WalletError> { + let (_, info) = wm.remove_wallet(wallet_id)?; + let (in_flight, epoch) = info.rewind_barrier.retire(); + let mut retired = self + .retired_barriers + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let entry = retired.entry(*wallet_id).or_insert((0, 0)); + entry.0 = entry.0.saturating_add(in_flight); + entry.1 = entry.1.max(epoch); + Ok(()) + } + + /// Hand a newly published wallet the barrier state a removed wallet of + /// the same id left behind, so advances that wallet emitted — still + /// queued in, or already projected by, the shared adapter — are dropped + /// rather than stored against the new wallet's scan. Called inside the + /// critical section that publishes the wallet. + pub(super) fn inherit_rewind_barrier( + &self, + wm: &mut WalletManager, + wallet_id: &WalletId, + ) { + let retired = self + .retired_barriers + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .remove(wallet_id); + if let (Some(retired), Some(info)) = (retired, wm.get_wallet_info_mut(wallet_id)) { + info.rewind_barrier.inherit(retired); + } + } + /// The in-broadcast fence map for `wallet_id`, creating it on first use. /// /// Every [`WalletGeneration`](crate::wallet::core::WalletGeneration) this diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 853c86f4d32..9e8b8b5ae59 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -415,12 +415,14 @@ impl PlatformWalletManager

{ // and it is deliberately not assumed equal to `registration_wallet_id` // (see the divergence branch below). // - // The same read also yields the cursor the host holds for every - // persisted wallet: a registration that recreates a wallet the host - // already has must measure durable writes against that cursor, not - // the one it just built (see the seed below). + // The same read also yields what the host holds for every persisted + // wallet: a registration that recreates a wallet the host already has + // must measure durable writes against the host's cursor, not the one + // it just built, and must keep the host's DashPay backfill record + // rather than start from an empty one that re-arms every covered + // contact (see the seed below). let load_persister: Arc = Arc::clone(&self.persister) as _; - let (mut persisted_platform_addresses, host_cursors) = + let (mut persisted_platform_addresses, mut host_wallets) = match super::run_blocking_load(move || load_persister.load()).await { Ok(crate::changeset::ClientStartState { platform_addresses, @@ -429,9 +431,20 @@ impl PlatformWalletManager

{ }) => ( platform_addresses, wallets - .iter() - .map(|(id, state)| (*id, state.wallet_info.metadata.synced_height)) - .collect::>(), + .into_iter() + .map(|(id, state)| { + ( + id, + ( + state.wallet_info.metadata.synced_height, + state.dashpay_backfill, + ), + ) + }) + .collect::>(), ), Err(e) => { tracing::error!( @@ -483,17 +496,21 @@ impl PlatformWalletManager

{ other )), })?; - let host_cursor = host_cursors - .get(&wallet_id) - .copied() - .unwrap_or(created_cursor); - if created_cursor < host_cursor { - if let Some(info) = wm.get_wallet_info_mut(&wallet_id) { + let (host_cursor, host_backfill) = host_wallets + .remove(&wallet_id) + .unwrap_or((created_cursor, Default::default())); + if let Some(info) = wm.get_wallet_info_mut(&wallet_id) { + // The host's record vouches for `[covered_from, host_cursor]` + // per account; the recreated wallet scans from no higher than + // that, so the coverage stays true. + info.dashpay_backfill = host_backfill; + if created_cursor < host_cursor { let owed = &mut info.dashpay_backfill.unpersisted_cursor; *owed = Some(owed.map_or(created_cursor, |cursor| cursor.min(created_cursor))); } } durable_cursors.insert(wallet_id, host_cursor); + self.inherit_rewind_barrier(&mut wm, &wallet_id); wallet_id }; @@ -592,7 +609,7 @@ impl PlatformWalletManager

{ "failed to persist wallet registration changeset" ); let mut wm = self.wallet_manager.write().await; - if let Err(remove_err) = wm.remove_wallet(&wallet_id) { + if let Err(remove_err) = self.remove_from_wallet_manager(&mut wm, &wallet_id) { tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %remove_err, @@ -642,7 +659,7 @@ impl PlatformWalletManager

{ "failed to restore persisted platform-address state" ); let mut wm = self.wallet_manager.write().await; - if let Err(remove_err) = wm.remove_wallet(&wallet_id) { + if let Err(remove_err) = self.remove_from_wallet_manager(&mut wm, &wallet_id) { tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %remove_err, @@ -928,7 +945,7 @@ impl PlatformWalletManager

{ their identity-sync rows are left for the next launch to reconcile" ); } - if let Err(e) = wm.remove_wallet(wallet_id) { + if let Err(e) = self.remove_from_wallet_manager(&mut wm, wallet_id) { tracing::warn!( wallet_id = %hex::encode(wallet_id), error = %e, @@ -1289,6 +1306,58 @@ mod register_wallet_duplicate_tests { ); } + /// Removing a wallet keeps its rewind barrier's in-flight state, and a + /// same-id registration inherits it in the critical section that + /// publishes it, so advances the removed wallet emitted cannot be stored + /// against its successor (dashpay/platform#4302 review). + #[tokio::test] + async fn a_same_id_registration_inherits_the_removed_wallets_barrier() { + let manager = make_manager(); + let network = Network::Testnet; + let mnemonic = Mnemonic::from_phrase(TEST_MNEMONIC).expect("valid test mnemonic"); + let seed_bytes = mnemonic.to_seed(""); + let create = |manager: Arc>| async move { + manager + .create_wallet_from_seed_bytes( + network, + &seed_bytes, + WalletAccountCreationOptions::Default, + Some(0), + ) + .await + }; + let wallet_id = create(Arc::clone(&manager)) + .await + .expect("first create") + .wallet_id(); + { + // Three advances emitted and not yet drained by the adapter. + let mut wm = manager.wallet_manager.write().await; + let barrier = &mut wm + .get_wallet_info_mut(&wallet_id) + .expect("info") + .rewind_barrier; + for _ in 0..3 { + barrier.note_emitted(); + } + barrier.arm(); + } + manager.remove_wallet(&wallet_id).await.expect("remove"); + + create(Arc::clone(&manager)).await.expect("second create"); + let wm = manager.wallet_manager.read().await; + let (in_flight, epoch) = wm + .get_wallet_info(&wallet_id) + .expect("published") + .rewind_barrier + .retire(); + assert_eq!( + in_flight, 3, + "the predecessor's queued advances are carried" + ); + assert!(epoch > 1, "the epoch moves past the predecessor's"); + } + /// The durable cursor is seeded in the critical section that publishes /// the wallet. Published first and seeded after, an adapter commit or a /// reconcile could record a newer durable height in between and the seed diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 1f2fbd79b12..fe001dcdaf1 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -254,8 +254,24 @@ impl DashPayView<'_, B> { ); } + // No contact needs handling — but the record may still need a round: + // coverage for a receival account that no longer exists must go (its + // scripts stop being watched, so the coverage stops being true), and + // a cursor or extent owed by an earlier reset or failed round must + // reach the host before the process can die with it. Either one runs + // a record-only round below; otherwise there is nothing to store. if to_mark.is_empty() { - return Ok(None); + let record = &info.dashpay_backfill; + let prunes = record.covered.iter().any(|entry| { + !receival_accounts.contains(&(entry.owner, entry.contact, entry.account_index)) + }); + let owes = record.unpersisted_cursor.is_some() || record.unpersisted_extent.is_some(); + // While the fault latch is up nothing can be stored anyway (see + // below); the debt waits for the next launch. + let faulted = self.sync_fault.load(std::sync::atomic::Ordering::Relaxed); + if !(prunes || owes) || faulted { + return Ok(None); + } } // Lower the filter-scan checkpoint only when a contact is funded below @@ -4220,19 +4236,16 @@ mod tests { assert!(core.synced_height_is_rewind); } - /// A fresh manager recreating a wallet the host already holds (the same - /// seed added again) builds a cursor below the host's — registration - /// writes no core cursor and the host keeps its row's `syncedHeight`. - /// The durable cursor is seeded from what the host holds, and the lower - /// built cursor is owed like any in-memory reset: the first record round - /// carries it, so a contact forward-covered at the built cursor is never - /// stored beside the host's higher one, and a reload rewinds for nothing - /// it should not (dashpay/platform#4302 review). - #[tokio::test] - async fn recreating_a_persisted_wallet_owes_its_lower_cursor_to_the_host() { - use crate::wallet::identity::{ContactRequest, EstablishedContact}; - - // What the host holds: this seed's wallet, scanned to 1000. + /// A fresh manager recreating, born at 101, the wallet a host holds at + /// cursor 1000 with `backfill` on its row. + async fn recreate_on_host( + backfill: crate::changeset::DashPayBackfillRecord, + ) -> ( + Arc>, + Arc, + Arc, + WalletId, + ) { let (donor, _donor_persister, wallet_id) = make_wallet().await; let (wallet, mut managed) = { let wm = donor.wallet_manager.read().await; @@ -4249,11 +4262,9 @@ mod tests { wallet, managed, identities: std::collections::BTreeMap::new(), - backfill: Default::default(), + backfill, stores: Mutex::default(), }); - - // A fresh manager recreates it from the seed, born at 101. let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); let handler: Arc = Arc::new(NoopEventHandler); let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); @@ -4270,6 +4281,52 @@ mod tests { .await .expect("recreate"); assert_eq!(recreated.wallet_id(), wallet_id); + (manager, host, recreated, wallet_id) + } + + /// Register `(owner, contact)`'s receival account on a recreated wallet + /// and establish the contact at `checkpoint`. + async fn establish_on_recreated( + manager: &Arc>, + host: &Arc, + wallet: &Arc, + owner: Identifier, + contact: Identifier, + checkpoint: u32, + ) { + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + let wallet_id = wallet.wallet_id(); + wallet + .identity() + .dashpay() + .register_contact_account(&owner, &contact, 0, test_receiving_xpub(&owner, &contact)) + .await + .expect("register receival account"); + let p = WalletPersister::new(wallet_id, Arc::clone(host) as _); + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], checkpoint, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], checkpoint, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + } + + /// A fresh manager recreating a wallet the host already holds (the same + /// seed added again) builds a cursor below the host's — registration + /// writes no core cursor and the host keeps its row's `syncedHeight`. + /// The durable cursor is seeded from what the host holds, and the lower + /// built cursor is owed like any in-memory reset: the first record round + /// carries it, so a contact forward-covered at the built cursor is never + /// stored beside the host's higher one, and a reload rewinds for nothing + /// it should not (dashpay/platform#4302 review). + #[tokio::test] + async fn recreating_a_persisted_wallet_owes_its_lower_cursor_to_the_host() { + let (manager, host, recreated, wallet_id) = recreate_on_host(Default::default()).await; { let wm = manager.wallet_manager.read().await; let info = wm.get_wallet_info(&wallet_id).expect("info"); @@ -4294,28 +4351,12 @@ mod tests { // A contact forward-covered at the built cursor. let owner = Identifier::from([0xAA; 32]); let contact = Identifier::from([0xBB; 32]); - let iw = recreated.identity(); - iw.dashpay() - .register_contact_account(&owner, &contact, 0, test_receiving_xpub(&owner, &contact)) - .await - .expect("register receival account"); - { - let p = WalletPersister::new(wallet_id, Arc::clone(&host) as _); - let mut wm = manager.wallet_manager.write().await; - let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); - info.identity_manager - .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) - .expect("add owner"); - let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 200, 0); - let incoming = ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 200, 0); - info.identity_manager - .managed_identity_mut(&owner) - .expect("managed") - .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); - } + establish_on_recreated(&manager, &host, &recreated, owner, contact, 200).await; host.stores.lock().unwrap().clear(); assert_eq!( - iw.dashpay() + recreated + .identity() + .dashpay() .reconcile_dashpay_rescan() .await .expect("reconcile"), @@ -4340,6 +4381,111 @@ mod tests { ); } + /// Recreating a wallet keeps the host's backfill record instead of + /// starting from an empty one that re-arms every covered contact. With + /// every contact covered there is no contact to handle, yet the reset the + /// recreation owes must still reach the host: the reconcile runs a + /// record-only round for the debt instead of returning early, keeping + /// the carried coverage (dashpay/platform#4302 review). + #[tokio::test] + async fn a_recreated_wallet_keeps_the_hosts_record_and_still_pays_its_owed_cursor() { + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let mut host_record = crate::changeset::DashPayBackfillRecord::default(); + host_record.record_pass(900, Some(200), [(owner, contact, 0, 200)]); + let (manager, host, recreated, wallet_id) = recreate_on_host(host_record.clone()).await; + { + let wm = manager.wallet_manager.read().await; + let record = &wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill; + assert_eq!( + record.covered, host_record.covered, + "the host's coverage is kept" + ); + assert_eq!((record.floor, record.rewound_from), (200, 900)); + assert_eq!(record.unpersisted_cursor, Some(100)); + } + + establish_on_recreated(&manager, &host, &recreated, owner, contact, 200).await; + host.stores.lock().unwrap().clear(); + assert_eq!( + recreated + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None, + "the contact is covered by the carried record: no rewind" + ); + let stores = host.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("a record-only round pays the debt"); + let core = round.core.as_ref().expect("carrying the owed cursor"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + assert_eq!( + round + .dashpay_backfill + .as_ref() + .unwrap() + .covered_from(&owner, &contact, 0), + Some(200) + ); + } + + /// Coverage for a receival account that no longer exists stops being + /// true the moment its scripts stop being watched. A pass with no + /// contact to handle must still drop it and store the record, not + /// return early and leave it vouching on disk (dashpay/platform#4302 + /// review). + #[tokio::test] + async fn coverage_for_a_removed_receival_account_is_pruned_and_stored() { + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let dashpay = wallet.identity().dashpay(); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("rewind"), + Some(100) + ); + + { + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.core_wallet + .accounts + .dashpay_receival_accounts + .remove(&DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }); + } + persister.stores.lock().unwrap().clear(); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("prune"), + None + ); + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the pruned record is stored"); + assert!(round.dashpay_backfill.as_ref().unwrap().is_empty()); + } + let wm = manager.wallet_manager.read().await; + assert!(wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .is_empty()); + } + /// Registering a receival account bumps the wallet's account generation, /// which the filter pipeline compares per batch: a batch scanned before /// the registration can no longer certify coverage for the new account From 409044480423440f0305509b26f219cb07f89242 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Thu, 1 Oct 2026 16:11:13 -0700 Subject: [PATCH 14/43] fix(platform-wallet): drop an owed cursor once the host has climbed past it Found on the int27 device check (dashpay/platform#4302). A manual rescan reset (and the other in-memory lowerings: account-add birth rewinds, a recreated wallet's built cursor) records the lowered height as owed to the host, so the next DashPay record round writes it before storing coverage. But the adapter persists the rescan's own advances as it climbs (the host overwrites syncedHeight), so by the time a record round runs after the climb the host already tracks the scan. The round still wrote the old reset height as a rewind, dragging the host back to it until the next block advance; a kill in that window re-scanned from the reset height. DurableCursors now records, per wallet, the RewindBarrier epoch of the scan advance the host last accepted (DurableCursor { height, advance_epoch }; seeded and rewind-written cursors carry None). The owed cursor carries the epoch it was owed in (DashPayBackfillRecord::owe_cursor folds the lower cursor and the later epoch), and the reconcile settles it before deciding anything once the host has accepted an advance from that epoch or later. An advance from before the lowering never settles it. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/persistence.rs | 1 + .../src/changeset/core_bridge.rs | 72 ++++++++- .../src/changeset/dashpay_backfill.rs | 64 ++++++++ .../rs-platform-wallet/src/changeset/mod.rs | 3 +- .../src/manager/accessors.rs | 4 +- .../rs-platform-wallet/src/manager/load.rs | 5 +- .../src/manager/wallet_lifecycle.rs | 12 +- .../src/wallet/identity/network/payments.rs | 149 +++++++++++++++++- .../src/wallet/platform_wallet_traits.rs | 4 +- 9 files changed, 288 insertions(+), 26 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 30e1e52cedb..ce873f45302 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -5374,6 +5374,7 @@ fn decode_dashpay_backfill( rewound_from: entry.dashpay_backfill_rewound_from, covered, unpersisted_cursor: None, + unpersisted_cursor_epoch: None, unpersisted_extent: None, } } diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index ae1ea1b3f8d..93e36b71109 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -292,7 +292,35 @@ where /// /// Lock order is this mutex, then the wallet manager — never the reverse. /// Seeded with the cursor each wallet was loaded or created with. -pub type DurableCursors = Arc>>; +pub type DurableCursors = Arc>>; + +/// What one wallet's host holds, as [`DurableCursors`] records it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct DurableCursor { + /// The `synced_height` the host last accepted. + pub height: u32, + /// The [`RewindBarrier`] epoch of the scan advance that put `height` + /// there, when it was one; `None` for a cursor seeded at load or + /// creation, or written by a backfill round as a rewind. + /// + /// A cursor owed to the host after an in-memory lowering is paid in full + /// once the host accepts an advance from the lowering's epoch or later: + /// that advance was reached by the scan that restarted from the lowered + /// cursor, so the host already tracks it, and writing the owed height + /// afterwards would only drag the host back below the scan + /// (dashpay/platform#4302 device check). + pub advance_epoch: Option, +} + +impl DurableCursor { + /// A cursor the host holds without a known scan advance behind it. + pub fn at(height: u32) -> Self { + Self { + height, + advance_epoch: None, + } + } +} /// Orders one wallet's `SyncHeightAdvanced` events against in-memory rewinds /// of its scan cursor, so an advance the engine emitted *before* a rewind is @@ -963,12 +991,18 @@ fn commit_batch_under_writer_lock

( sync_fault: &AtomicBool, freeze_logged: &AtomicBool, settled: &mut Vec, - cursors: &mut BTreeMap, + cursors: &mut BTreeMap, ) -> BatchDiagnostics where P: PlatformWalletPersistence + ?Sized, { let mut diag = BatchDiagnostics::new(folded, batch.len()); + // The epoch each wallet's advance was projected in, for the cursor + // entry its accepted height lands in. + let advance_epochs: BTreeMap> = batch + .iter() + .map(|(wallet_id, wallet_batch)| (*wallet_id, wallet_batch.advance_epoch)) + .collect(); let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { for (wallet_id, wallet_batch) in batch { commit_wallet( @@ -985,7 +1019,13 @@ where })); // What each host now holds, for the next writer to measure against. for (wallet_id, height) in &diag.persisted_by_wallet { - cursors.insert(*wallet_id, *height); + cursors.insert( + *wallet_id, + DurableCursor { + height: *height, + advance_epoch: advance_epochs.get(wallet_id).copied().flatten(), + }, + ); } if let Err(panic) = unwound { for wallet_id in batch_wallet_ids { @@ -5179,14 +5219,23 @@ mod tests { ..Default::default() }; let mut batch = BTreeMap::new(); - batch.insert(a, watermark(1_000)); + batch.insert( + a, + super::WalletBatch { + advance_epoch: Some(3), + ..watermark(1_000) + }, + ); batch.insert(b, watermark(500)); let mut fault = super::AdapterFaultState::default(); let sync_fault = AtomicBool::new(false); let freeze_logged = AtomicBool::new(false); let mut settled = Vec::new(); - let mut cursors: BTreeMap = BTreeMap::from([(a, 100), (b, 100)]); + let mut cursors: BTreeMap = BTreeMap::from([ + (a, super::DurableCursor::at(100)), + (b, super::DurableCursor::at(100)), + ]); let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { super::commit_batch_under_writer_lock( &persister, @@ -5203,10 +5252,17 @@ mod tests { assert!(unwound.is_err(), "the panic still propagates"); assert_eq!( cursors.get(&a), - Some(&1_000), - "A's accepted cursor is recorded" + Some(&super::DurableCursor { + height: 1_000, + advance_epoch: Some(3), + }), + "A's accepted cursor is recorded with the epoch its advance was projected in" + ); + assert_eq!( + cursors.get(&b).map(|cursor| cursor.height), + Some(100), + "B's store never returned" ); - assert_eq!(cursors.get(&b), Some(&100), "B's store never returned"); assert!( fault.is_faulted(&b), "B is faulted before the lock is released" diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 3215d069f21..e63f649e3ef 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -120,6 +120,12 @@ pub struct DashPayBackfillRecord { /// behind. `None` when nothing is owed. #[cfg_attr(feature = "serde", serde(skip))] pub unpersisted_cursor: Option, + /// In-memory only: the `RewindBarrier` epoch the latest contribution to + /// [`unpersisted_cursor`](Self::unpersisted_cursor) was recorded in. The + /// debt is paid once the host accepts a scan advance from this epoch or + /// later — see [`owe_cursor`](Self::owe_cursor). + #[cfg_attr(feature = "serde", serde(skip))] + pub unpersisted_cursor_epoch: Option, /// In-memory only, like [`unpersisted_cursor`](Self::unpersisted_cursor): /// the `(floor, rewound_from)` a rewind whose record round failed to /// store still has to contribute. The retry folds it back in, so the @@ -130,6 +136,38 @@ pub struct DashPayBackfillRecord { } impl DashPayBackfillRecord { + /// Owe the host `cursor`, lowered in memory during rewind epoch `epoch`. + /// Folds into any debt already owed: the lower cursor, and the later + /// epoch — the debt is paid only by an advance the host accepts after the + /// latest lowering. + pub fn owe_cursor(&mut self, cursor: u32, epoch: u64) { + self.unpersisted_cursor = Some( + self.unpersisted_cursor + .map_or(cursor, |owed| owed.min(cursor)), + ); + self.unpersisted_cursor_epoch = Some( + self.unpersisted_cursor_epoch + .map_or(epoch, |owed| owed.max(epoch)), + ); + } + + /// Forget the owed cursor once the host has accepted a scan advance from + /// the epoch it was owed in or later (`host_advance_epoch`): the host + /// then tracks the scan that restarted from the lowered cursor, and + /// writing the owed height would only drag it back below that scan. + /// Returns whether a debt was dropped. + pub fn settle_owed_cursor(&mut self, host_advance_epoch: Option) -> bool { + let paid = self.unpersisted_cursor.is_some() + && host_advance_epoch + .zip(self.unpersisted_cursor_epoch) + .is_some_and(|(host, owed)| host >= owed); + if paid { + self.unpersisted_cursor = None; + self.unpersisted_cursor_epoch = None; + } + paid + } + /// Whether no backfill has ever been recorded for this wallet. pub fn is_empty(&self) -> bool { self.covered.is_empty() @@ -322,6 +360,7 @@ impl DashPayBackfillRecord { rewound_from, covered, unpersisted_cursor: None, + unpersisted_cursor_epoch: None, unpersisted_extent: None, }) } @@ -478,6 +517,31 @@ mod tests { assert_eq!(record.covered_from(&id(1), &id(2), 0), Some(100)); } + /// A debt folds to the lower cursor and the later epoch, and is settled + /// only by a host advance from that epoch or later. + #[test] + fn an_owed_cursor_is_settled_only_by_a_host_advance_from_its_epoch_or_later() { + let mut record = DashPayBackfillRecord::default(); + assert!(!record.settle_owed_cursor(Some(9)), "nothing owed"); + record.owe_cursor(500, 2); + record.owe_cursor(300, 1); + assert_eq!(record.unpersisted_cursor, Some(300)); + assert_eq!(record.unpersisted_cursor_epoch, Some(2)); + + assert!( + !record.settle_owed_cursor(None), + "a seeded host cursor proves nothing" + ); + assert!( + !record.settle_owed_cursor(Some(1)), + "an advance from before the latest lowering" + ); + assert_eq!(record.unpersisted_cursor, Some(300)); + assert!(record.settle_owed_cursor(Some(2))); + assert_eq!(record.unpersisted_cursor, None); + assert_eq!(record.unpersisted_cursor_epoch, None); + } + /// `retain` can empty the cover set while the backfill those entries rode /// is still climbing. The next rewind must widen that extent, not replace /// it: replacing `(100, 1000)` with `(200, 500)` would read complete at 500 diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index bbe2bb9f6ca..cd8c0deed19 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -47,7 +47,8 @@ pub use changeset::{ pub use client_start_state::ClientStartState; pub use client_wallet_start_state::ClientWalletStartState; pub use core_bridge::{ - spawn_wallet_event_adapter, spawn_wallet_event_adapter_with_durable_cursors, DurableCursors, + spawn_wallet_event_adapter, spawn_wallet_event_adapter_with_durable_cursors, DurableCursor, + DurableCursors, }; pub use dashpay_backfill::{DashPayBackfillCoveredContact, DashPayBackfillRecord}; pub use identity_manager_start_state::IdentityManagerStartState; diff --git a/packages/rs-platform-wallet/src/manager/accessors.rs b/packages/rs-platform-wallet/src/manager/accessors.rs index 687d138774f..a5bfc7822b7 100644 --- a/packages/rs-platform-wallet/src/manager/accessors.rs +++ b/packages/rs-platform-wallet/src/manager/accessors.rs @@ -873,8 +873,8 @@ impl PlatformWalletManager

{ // backfill record round carries it as that round's cursor, and no // coverage reaches disk beside a durable cursor this reset retracted // in memory only (dashpay/platform#4302 review). - let owed = &mut info.dashpay_backfill.unpersisted_cursor; - *owed = Some(owed.map_or(from_height, |cursor| cursor.min(from_height))); + let epoch = info.rewind_barrier.epoch(); + info.dashpay_backfill.owe_cursor(from_height, epoch); tracing::info!( wallet_id = %hex::encode(wallet_id), from_height, diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index c9fddf71d5e..58560ce56a3 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -294,7 +294,10 @@ impl PlatformWalletManager

{ ))); break 'load; } - durable_cursors.insert(wallet_id, loaded_cursor); + durable_cursors.insert( + wallet_id, + crate::changeset::DurableCursor::at(loaded_cursor), + ); self.inherit_rewind_barrier(&mut wm, &wallet_id); } inserted_in_manager.push(wallet_id); diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 9e8b8b5ae59..965c4f40192 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -505,11 +505,13 @@ impl PlatformWalletManager

{ // that, so the coverage stays true. info.dashpay_backfill = host_backfill; if created_cursor < host_cursor { - let owed = &mut info.dashpay_backfill.unpersisted_cursor; - *owed = Some(owed.map_or(created_cursor, |cursor| cursor.min(created_cursor))); + // Owed from this wallet's first epoch: any advance the + // host accepts from its own scan pays it. + let epoch = info.rewind_barrier.epoch(); + info.dashpay_backfill.owe_cursor(created_cursor, epoch); } } - durable_cursors.insert(wallet_id, host_cursor); + durable_cursors.insert(wallet_id, crate::changeset::DurableCursor::at(host_cursor)); self.inherit_rewind_barrier(&mut wm, &wallet_id); wallet_id }; @@ -1390,7 +1392,7 @@ mod register_wallet_duplicate_tests { let mut gate = manager.durable_cursors.lock().await; // What a removed registration might leave behind. - gate.insert(wallet_id, 777); + gate.insert(wallet_id, crate::changeset::DurableCursor::at(777)); let registering = tokio::spawn(create(Arc::clone(&manager))); tokio::time::sleep(std::time::Duration::from_millis(100)).await; assert!( @@ -1422,7 +1424,7 @@ mod register_wallet_duplicate_tests { .lock() .await .get(&wallet_id) - .copied(), + .map(|cursor| cursor.height), Some(created_cursor), "the seed is the created cursor, not the leftover entry" ); diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index fe001dcdaf1..b978d048287 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -146,6 +146,21 @@ impl DashPayView<'_, B> { let Some(info) = wm.get_wallet_info_mut(&self.wallet_id) else { return Ok(None); }; + let durable = durable_cursors.get(&self.wallet_id).copied(); + // A cursor owed since an in-memory lowering is paid once the host + // has accepted a scan advance from the lowering's epoch or later: the + // host then tracks the scan that restarted from the lowered cursor. + // Writing the old height after that would only drag the host back + // below the scan (dashpay/platform#4302 device check). + if info + .dashpay_backfill + .settle_owed_cursor(durable.and_then(|durable| durable.advance_epoch)) + { + tracing::debug!( + wallet_id = %hex::encode(self.wallet_id), + "DashPay rescan: the host has caught up past the owed cursor; dropping the debt" + ); + } let synced_height = info.core_wallet.synced_height(); // 0 means "scan from genesis / not yet started" — already a full @@ -341,6 +356,7 @@ impl DashPayView<'_, B> { None => staged.record_pass(synced_height, None, to_mark.iter().copied()), }; let owed = staged.unpersisted_cursor.take(); + staged.unpersisted_cursor_epoch = None; let needed_cursor = match (floor, owed) { (Some(floor), Some(owed)) => Some(floor.min(owed)), (Some(floor), None) => Some(floor), @@ -349,13 +365,13 @@ impl DashPayView<'_, B> { // Never write a cursor at or above the one the host already holds: // coverage is safe beside any lower durable cursor, and writing a // higher one would claim rows the adapter has not persisted yet. - let durable = durable_cursors.get(&self.wallet_id).copied(); let cursor_to_write = - needed_cursor.filter(|cursor| durable.is_none_or(|durable| *cursor < durable)); + needed_cursor.filter(|cursor| durable.is_none_or(|durable| *cursor < durable.height)); let cursor_now = floor.unwrap_or(synced_height); // The debt, recorded before the store so no way out of it loses it. info.dashpay_backfill.unpersisted_cursor = Some(needed_cursor.map_or(cursor_now, |cursor| cursor.min(cursor_now))); + info.dashpay_backfill.unpersisted_cursor_epoch = Some(info.rewind_barrier.epoch()); info.dashpay_backfill.unpersisted_extent = extent; let faulted = self.sync_fault.load(std::sync::atomic::Ordering::Relaxed); let stored = if faulted { @@ -376,7 +392,8 @@ impl DashPayView<'_, B> { Ok(()) => { info.dashpay_backfill = staged; if let Some(cursor) = cursor_to_write { - durable_cursors.insert(self.wallet_id, cursor); + durable_cursors + .insert(self.wallet_id, crate::changeset::DurableCursor::at(cursor)); } for (owner, contact, _, _) in &to_mark { if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { @@ -3048,7 +3065,7 @@ mod tests { .expect("info") .core_wallet .update_synced_height(height); - durable.insert(wallet_id, height); + durable.insert(wallet_id, crate::changeset::DurableCursor::at(height)); } /// The wallet's [`RewindBarrier`](crate::changeset::core_bridge::RewindBarrier) @@ -4343,7 +4360,7 @@ mod tests { .lock() .await .get(&wallet_id) - .copied(), + .map(|cursor| cursor.height), Some(1_000), "the durable cursor is the host's, not the built one" ); @@ -4565,7 +4582,7 @@ mod tests { .lock() .await .get(&wallet_id) - .copied(), + .map(|cursor| cursor.height), Some(1_000), "the host still holds its high-water" ); @@ -4858,7 +4875,7 @@ mod tests { .lock() .await .get(&wallet_id) - .copied(), + .map(|cursor| cursor.height), Some(100), "the recorded durable cursor is still what the host holds" ); @@ -4918,6 +4935,124 @@ mod tests { ); } + /// Reset the cursor to `from` the way the app's "rescan blockchain" + /// does, then model the rescan climbing back to `to` with the adapter + /// committing an advance from rewind epoch `host_epoch`. + async fn reset_then_climb( + manager: &Arc>, + wallet_id: WalletId, + from: u32, + to: u32, + host_epoch: u64, + ) { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + { + let manager = Arc::clone(manager); + tokio::task::spawn_blocking(move || { + assert!(manager.spv_rescan_filters_blocking(&wallet_id, from)); + }) + .await + .expect("manual rescan"); + } + let mut durable = manager.durable_cursors.lock().await; + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(to); + durable.insert( + wallet_id, + crate::changeset::DurableCursor { + height: to, + advance_epoch: Some(host_epoch), + }, + ); + } + + /// dashpay/platform#4302 device check: a manual reset owes the host the + /// reset height, but the adapter persists the rescan's own advances as it + /// climbs, so once the host has accepted one from the reset's epoch the + /// debt is paid. A record round after the climb must not write the old + /// reset height — that dragged the host back to it until the next block + /// advance, and a kill in that window re-scanned from it. + #[tokio::test] + async fn an_owed_cursor_the_host_has_climbed_past_is_dropped_not_written() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + set_synced_height(&manager, wallet_id, 1_000).await; + reset_then_climb(&manager, wallet_id, 100, 1_000, 1).await; + assert_eq!(rewind_epoch(&manager, wallet_id).await, 1); + + establish_receival_contact( + &manager, &persister, wallet_id, owner, contact, 1_200, 1_200, + ) + .await; + persister.stores.lock().unwrap().clear(); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + None + ); + { + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the contact is recorded"); + assert!( + round.core.is_none(), + "no stale reset cursor rides the round: {:?}", + round.core.as_ref().and_then(|core| core.synced_height) + ); + } + let wm = manager.wallet_manager.read().await; + let record = &wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill; + assert_eq!(record.unpersisted_cursor, None, "the paid debt is gone"); + assert_eq!(record.covered_from(&owner, &contact, 0), Some(1_200)); + } + + /// The other side: an advance the host accepted from BEFORE the reset + /// says nothing about the range the reset re-opened, so the debt stands + /// and rides the next record round. + #[tokio::test] + async fn an_owed_cursor_is_still_paid_when_the_host_only_advanced_before_the_reset() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + set_synced_height(&manager, wallet_id, 1_000).await; + // The host's last accepted advance is from epoch 0, before the reset + // (epoch 1); memory has not moved since the reset. + reset_then_climb(&manager, wallet_id, 100, 100, 0).await; + manager + .durable_cursors + .lock() + .await + .get_mut(&wallet_id) + .expect("seeded") + .height = 1_000; + + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 200, 200).await; + persister.stores.lock().unwrap().clear(); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"); + let stores = persister.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the record is stored"); + let core = round.core.as_ref().expect("the owed reset rides the round"); + assert_eq!(core.synced_height, Some(100)); + assert!(core.synced_height_is_rewind); + } + async fn synced_height_of( manager: &Arc>, wallet_id: WalletId, diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs index 01a1b7caa57..1898040b338 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs @@ -349,8 +349,8 @@ impl PlatformWalletInfo { let after = self.core_wallet.metadata.synced_height; if after < before { self.rewind_barrier.arm(); - let owed = &mut self.dashpay_backfill.unpersisted_cursor; - *owed = Some(owed.map_or(after, |cursor| cursor.min(after))); + let epoch = self.rewind_barrier.epoch(); + self.dashpay_backfill.owe_cursor(after, epoch); } } } From df6e3727abaf2456d165a118e05cba5c328c6922 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Thu, 1 Oct 2026 21:58:01 -0700 Subject: [PATCH 15/43] fix(platform-wallet): make same-id wallet recreation safe for the backfill record MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Seventh review round on the DashPay backfill record (dashpay/platform#4302), four findings about a wallet removed and registered again under the same id: - The adapter persists a sync-height advance only for a wallet the manager holds: while nothing holds the id, an advance the removed wallet emitted is dropped at projection and at commit, and counted off the id's retired barrier so the successor inherits exactly the advances still queued. The retired barrier state lives in the shared DurableCursors (Arc) so the adapter can see it. - A recreated wallet keeps the host's backfill record only for the receival accounts it holds — none, when built from the seed — so coverage for an account absent while the successor scans never suppresses that account's backfill; it re-arms when the account is registered. - The host snapshot a registration or load reads precedes the durable-cursor lock, and a same-id predecessor's commit can be accepted in between. Under the lock the DurableCursors entry wins over the snapshot when the host has a row; with no row the leftover entry is discarded. - The successor's account_generation starts past the predecessor's final value (RetiredBarrier::account_generation), so a filter batch reconciled against the predecessor's account set is never certified for it. Adapter tests that fed bare events for wallets never registered now insert their wallets and emit the way the engine does. Co-Authored-By: Claude Fable 5.1 --- .../src/changeset/core_bridge.rs | 336 ++++++++++++++---- .../rs-platform-wallet/src/changeset/mod.rs | 2 +- .../rs-platform-wallet/src/manager/load.rs | 12 +- .../rs-platform-wallet/src/manager/mod.rs | 37 +- .../src/manager/wallet_lifecycle.rs | 135 +++++-- .../src/wallet/identity/network/payments.rs | 177 +++++++-- .../src/wallet/platform_wallet_traits.rs | 6 + 7 files changed, 541 insertions(+), 164 deletions(-) diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index 93e36b71109..204859d59ac 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -292,7 +292,57 @@ where /// /// Lock order is this mutex, then the wallet manager — never the reverse. /// Seeded with the cursor each wallet was loaded or created with. -pub type DurableCursors = Arc>>; +/// +/// An entry outlives its wallet: a removed wallet's entry stays what its host +/// row holds, and a same-id successor seeds from it in preference to the +/// snapshot it read before taking the lock, which a predecessor's commit may +/// have overtaken. The same structure carries the removed wallet's +/// [`RetiredBarrier`] for the successor to inherit — and for the adapter to +/// count down while no wallet is registered under the id, since advances the +/// removed wallet emitted keep arriving and are dropped meanwhile. +pub type DurableCursors = Arc; + +/// The shared state behind [`DurableCursors`]. +#[derive(Debug, Default)] +pub struct DurableCursorState { + /// Per wallet, what its host holds. The ordering lock. + pub cursors: Arc>>, + /// Per wallet id with no registered wallet, the barrier state the removed + /// wallet left (see [`RewindBarrier::retire`]). A leaf lock: taken under + /// the manager lock on both sides, never held across an await. + pub retired: std::sync::Mutex>, +} + +impl DurableCursorState { + /// Lock the cursor map for a short, non-blocking critical section. + pub async fn lock(&self) -> tokio::sync::MutexGuard<'_, BTreeMap> { + self.cursors.lock().await + } + + /// The retired-barrier map, poison-tolerant. + pub fn retired(&self) -> std::sync::MutexGuard<'_, BTreeMap> { + self.retired + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + } +} + +/// What a removed wallet leaves under its id for a same-id successor +/// (dashpay/platform#4302 review). +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct RetiredBarrier { + /// Advances the removed wallet emitted that the adapter has not projected + /// yet. Counted down as they arrive while no wallet holds the id (they + /// are dropped), so a successor inherits exactly the ones still queued. + pub in_flight: u64, + /// The removed wallet's rewind epoch. + pub epoch: u64, + /// The removed wallet's final `account_generation`. The successor's + /// starts above it, so a filter batch whose scripts were reconciled + /// against the predecessor's account set never matches the successor's + /// generation and is not certified for it. + pub account_generation: u64, +} /// What one wallet's host holds, as [`DurableCursors`] records it. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -357,6 +407,9 @@ pub struct RewindBarrier { stale: u64, /// Bumped by every rewind. epoch: u64, + /// Inherited from a removed same-id predecessor: one past its final + /// `account_generation`, folded into this wallet's. + generation_base: u64, /// Receival accounts platform registered by inserting them directly /// (not through key-wallet's `add_managed_account*`, which bump the /// engine's own counter). Folded into the wallet's `account_generation` @@ -396,20 +449,36 @@ impl RewindBarrier { } /// What a removed wallet leaves for a successor registered under the same - /// id: its advances still in flight, and its epoch. - pub(crate) fn retire(&self) -> (u64, u64) { - (self.in_flight, self.epoch) + /// id: its advances still in flight, its epoch, and its final account + /// generation (`account_generation`, the wallet's full value). + pub(crate) fn retire(&self, account_generation: u64) -> RetiredBarrier { + RetiredBarrier { + in_flight: self.in_flight, + epoch: self.epoch, + account_generation, + } } /// Take over a removed predecessor's [`retire`](Self::retire) state. The /// shared adapter can still hold advances the predecessor emitted, queued /// or already projected; all of them predate this wallet's own scan, so /// the queued ones are counted as stale and the epoch moves past the - /// predecessor's, which strips the projected ones at commit. - pub(crate) fn inherit(&mut self, (in_flight, epoch): (u64, u64)) { - self.in_flight = self.in_flight.saturating_add(in_flight); - self.stale = self.stale.saturating_add(in_flight); - self.epoch = self.epoch.wrapping_add(epoch).wrapping_add(1); + /// predecessor's, which strips the projected ones at commit. The account + /// generation moves past the predecessor's too, so no in-flight filter + /// batch scanned against its account set certifies this wallet. + pub(crate) fn inherit(&mut self, retired: RetiredBarrier) { + self.in_flight = self.in_flight.saturating_add(retired.in_flight); + self.stale = self.stale.saturating_add(retired.in_flight); + self.epoch = self.epoch.wrapping_add(retired.epoch).wrapping_add(1); + self.generation_base = self + .generation_base + .max(retired.account_generation.saturating_add(1)); + } + + /// See [`inherit`](Self::inherit): added to this wallet's account + /// generation. + pub(crate) fn generation_base(&self) -> u64 { + self.generation_base } /// A receival account was inserted outside key-wallet's account-add @@ -456,20 +525,32 @@ where /// height, and the first advance of a newer rewind epoch replaces the /// batch's height instead of max-merging with it — a pre-rewind height is /// higher than the rescan's by construction, and the max would keep it. +/// +/// An advance for a wallet id with no wallet registered under it — removed, +/// or rolled back — contributes nothing: nothing may persist a cursor for a +/// wallet that is gone, and a same-id successor must not inherit it. It is +/// counted down from the id's [`RetiredBarrier`] so the successor inherits +/// exactly the advances still queued (dashpay/platform#4302 review). async fn project_event_into_batch( wallet_manager: &Arc>>, + durable_cursors: &DurableCursors, event: &WalletEvent, batch: &mut BTreeMap, ) { let wallet_id = event.wallet_id(); let advance_epoch = match event { - WalletEvent::SyncHeightAdvanced { .. } => Some( - wallet_manager - .write() - .await - .get_wallet_info_mut(&wallet_id) - .map_or(Some(0), |info| info.rewind_barrier.take_next()), - ), + WalletEvent::SyncHeightAdvanced { .. } => Some({ + let mut wm = wallet_manager.write().await; + match wm.get_wallet_info_mut(&wallet_id) { + Some(info) => info.rewind_barrier.take_next(), + None => { + if let Some(retired) = durable_cursors.retired().get_mut(&wallet_id) { + retired.in_flight = retired.in_flight.saturating_sub(1); + } + None + } + } + }), _ => None, }; let mut core = build_core_changeset(wallet_manager, event).await; @@ -505,7 +586,8 @@ async fn project_event_into_batch( /// Strip, from each wallet's batch, a sync-height advance a rewind has /// overtaken: one projected in a [`RewindBarrier`] epoch a rewind has since -/// superseded, or one above the wallet's in-memory cursor. Storing either +/// superseded, one above the wallet's in-memory cursor, or one for a wallet +/// id nothing is registered under any more. Storing either /// would claim coverage the rescan has not produced. Called with /// [`DurableCursors`] held, right before commit, so no rewind can slip in /// between this check and the store. @@ -524,6 +606,14 @@ async fn drop_watermarks_above_the_live_cursor( continue; } let Some(info) = wm.get_wallet_info(wallet_id) else { + // No wallet holds this id any more: nothing may persist a cursor + // for it, and a same-id successor must not find one. + tracing::debug!( + wallet_id = %hex::encode(wallet_id), + projected = height, + "wallet-event adapter: dropping a sync-height advance for a removed wallet" + ); + wallet_batch.core.synced_height = None; continue; }; // Projected in a rewind epoch that has since been superseded: the @@ -702,7 +792,7 @@ async fn run_wallet_event_adapter

( // `TransactionInstantLocked`, which checks finality before recording // the IS lock), `build_core_changeset` takes a brief read lock on the // manager. - project_event_into_batch(&wallet_manager, &event, &mut batch).await; + project_event_into_batch(&wallet_manager, &durable_cursors, &event, &mut batch).await; // Fold in whatever else is already buffered. `try_recv` never waits, // so this drains the backlog at projection speed and stops as soon as @@ -711,7 +801,8 @@ async fn run_wallet_event_adapter

( while folded < ADAPTER_STORE_BATCH_LIMIT { match receiver.try_recv() { Ok(event) => { - project_event_into_batch(&wallet_manager, &event, &mut batch).await; + project_event_into_batch(&wallet_manager, &durable_cursors, &event, &mut batch) + .await; folded += 1; } Err(TryRecvError::Empty) => break, @@ -766,7 +857,7 @@ async fn run_wallet_event_adapter

( // watermark against the live cursor under it: a rewind that ran since // this batch was projected must win (see [`DurableCursors`]). The // guard rides into the commit thread and is released when it returns. - let mut cursors = Arc::clone(&durable_cursors).lock_owned().await; + let mut cursors = Arc::clone(&durable_cursors.cursors).lock_owned().await; drop_watermarks_above_the_live_cursor(&wallet_manager, &mut batch).await; let batch_wallet_ids: Vec = batch .iter() @@ -4860,6 +4951,54 @@ mod tests { WalletEvent::SyncHeightAdvanced { wallet_id, height } } + /// Register a fresh test wallet (cursor 0) in `manager`. The adapter + /// persists advances only for a wallet the manager holds + /// (dashpay/platform#4302 review), so a test feeding it bare events needs + /// the wallet present. + async fn insert_test_wallet( + manager: &Arc>>, + ) -> WalletId { + use crate::wallet::core::WalletGeneration; + use crate::wallet::identity::IdentityManager; + use key_wallet::test_utils::TestWalletContext; + + let ctx = TestWalletContext::new_random(); + let info = PlatformWalletInfo { + observed_input_conflicts: Default::default(), + core_wallet: ctx.managed_wallet, + generation: Arc::new(WalletGeneration::new()), + identity_manager: IdentityManager::new(), + tracked_asset_locks: BTreeMap::new(), + dpns_name_states: BTreeMap::new(), + dashpay_backfill: Default::default(), + rewind_barrier: Default::default(), + }; + manager + .write() + .await + .insert_wallet(ctx.wallet, info) + .expect("insert wallet") + } + + /// Feed `tx` a sync-height advance the way the engine emits one: the + /// in-memory cursor moves and the wallet's barrier counts the emission. + async fn emit_advance( + manager: &Arc>>, + tx: &UnboundedSender, + wallet_id: WalletId, + height: u32, + ) { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + { + let mut wm = manager.write().await; + let info = wm + .get_wallet_info_mut(&wallet_id) + .expect("registered wallet"); + info.update_synced_height(height); + } + tx.send(sync_height_event(wallet_id, height)).unwrap(); + } + /// A watermark the engine queued BEFORE `reconcile_dashpay_rescan` /// lowered the wallet's cursor must not be persisted after it: the /// rescan round stores the lowered cursor with its coverage record, and @@ -5075,13 +5214,14 @@ mod tests { async fn project( manager: &Arc>>, + durable: &super::DurableCursors, receiver: &mut tokio::sync::mpsc::UnboundedReceiver, count: usize, ) -> BTreeMap { let mut batch = BTreeMap::new(); for _ in 0..count { let event = receiver.try_recv().expect("queued event"); - super::project_event_into_batch(manager, &event, &mut batch).await; + super::project_event_into_batch(manager, durable, &event, &mut batch).await; } batch } @@ -5094,19 +5234,20 @@ mod tests { #[tokio::test] async fn an_advance_queued_before_a_rewind_is_dropped_after_the_rescan_climbs_past_it() { let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + let durable = super::DurableCursors::default(); engine_advance(&manager, wallet_id, 1_000).await; // pre-rewind, still queued rewind(&manager, wallet_id, 100).await; engine_advance(&manager, wallet_id, 500).await; engine_advance(&manager, wallet_id, 1_000).await; // the rescan is back at 1000 // The adapter drains the pre-rewind advance on its own. - let mut stale = project(&manager, &mut receiver, 1).await; + let mut stale = project(&manager, &durable, &mut receiver, 1).await; assert_eq!(stale[&wallet_id].core.synced_height, None); super::drop_watermarks_above_the_live_cursor(&manager, &mut stale).await; assert_eq!(stale[&wallet_id].core.synced_height, None); // The rescan's own advances flow, behind the rows they imply. - let mut fresh = project(&manager, &mut receiver, 2).await; + let mut fresh = project(&manager, &durable, &mut receiver, 2).await; super::drop_watermarks_above_the_live_cursor(&manager, &mut fresh).await; assert_eq!(fresh[&wallet_id].core.synced_height, Some(1_000)); } @@ -5117,8 +5258,9 @@ mod tests { #[tokio::test] async fn the_commit_drops_an_advance_a_rewind_overtook_after_the_rescan_climbs_past_it() { let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + let durable = super::DurableCursors::default(); engine_advance(&manager, wallet_id, 1_000).await; - let mut projected = project(&manager, &mut receiver, 1).await; + let mut projected = project(&manager, &durable, &mut receiver, 1).await; assert_eq!(projected[&wallet_id].core.synced_height, Some(1_000)); rewind(&manager, wallet_id, 100).await; @@ -5130,37 +5272,65 @@ mod tests { ); // The rescan's advance, in the next batch, is stored. - let mut next = project(&manager, &mut receiver, 1).await; + let mut next = project(&manager, &durable, &mut receiver, 1).await; super::drop_watermarks_above_the_live_cursor(&manager, &mut next).await; assert_eq!(next[&wallet_id].core.synced_height, Some(1_200)); } /// A wallet removed and registered again under the same id shares the /// adapter with its predecessor, which can still hold that predecessor's - /// advances — queued, or already projected. The successor inherits the - /// predecessor's barrier: the queued ones drop as stale and the - /// projected ones fail the epoch check at commit, even once the new - /// scan has climbed above them (dashpay/platform#4302 review). + /// advances — queued, or already projected. While nothing holds the id + /// every one of them is dropped, at projection and at commit, and + /// counted off the retired barrier; the successor inherits what is still + /// queued, so those drop as stale and the projected ones fail the epoch + /// check at commit, even once the new scan has climbed above them — while + /// the successor's own advances flow (dashpay/platform#4302 review). #[tokio::test] async fn a_same_id_successor_drops_its_predecessors_queued_and_projected_advances() { use crate::wallet::core::WalletGeneration; use crate::wallet::identity::IdentityManager; + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + let durable = super::DurableCursors::default(); engine_advance(&manager, wallet_id, 700).await; - let mut projected = project(&manager, &mut receiver, 1).await; // in an adapter batch + let mut projected = project(&manager, &durable, &mut receiver, 1).await; // in a batch engine_advance(&manager, wallet_id, 1_000).await; // still queued + engine_advance(&manager, wallet_id, 1_100).await; // still queued - // Removed, then registered again under the same id. - let (wallet, retired) = { + // Removed: the barrier is retired under the id. + let (wallet, predecessor_generation) = { let (wallet, info) = manager .write() .await .remove_wallet(&wallet_id) .expect("remove"); - (wallet, info.rewind_barrier.retire()) + let generation = WalletInfoInterface::account_generation(&info); + durable + .retired() + .insert(wallet_id, info.rewind_barrier.retire(generation)); + (wallet, generation) }; + assert_eq!(durable.retired()[&wallet_id].in_flight, 2); + + // While nothing holds the id: the projected advance is stripped at + // commit, and a queued one is dropped at projection and counted off. + super::drop_watermarks_above_the_live_cursor(&manager, &mut projected).await; + assert_eq!( + projected[&wallet_id].core.synced_height, None, + "nothing may persist a cursor for a removed wallet" + ); + let absent = project(&manager, &durable, &mut receiver, 1).await; + assert_eq!(absent[&wallet_id].core.synced_height, None); + assert_eq!( + durable.retired()[&wallet_id].in_flight, + 1, + "the dropped advance no longer counts as queued" + ); + + // Registered again under the same id. + let retired = durable.retired().remove(&wallet_id).expect("retired"); let mut successor = PlatformWalletInfo { observed_input_conflicts: Default::default(), core_wallet: ManagedWalletInfo::from_wallet(&wallet, 0), @@ -5172,6 +5342,10 @@ mod tests { rewind_barrier: Default::default(), }; successor.rewind_barrier.inherit(retired); + assert!( + WalletInfoInterface::account_generation(&successor) > predecessor_generation, + "a batch reconciled against the predecessor's accounts cannot match the successor" + ); assert_eq!( manager .write() @@ -5182,20 +5356,19 @@ mod tests { ); engine_advance(&manager, wallet_id, 1_200).await; // the successor's own scan - super::drop_watermarks_above_the_live_cursor(&manager, &mut projected).await; - assert_eq!( - projected[&wallet_id].core.synced_height, None, - "the predecessor's projected advance is stripped although 1200 is above it" - ); - let mut queued = project(&manager, &mut receiver, 1).await; + let mut queued = project(&manager, &durable, &mut receiver, 1).await; assert_eq!( queued[&wallet_id].core.synced_height, None, - "the predecessor's queued advance drops as stale" + "the predecessor's last queued advance drops as stale" ); super::drop_watermarks_above_the_live_cursor(&manager, &mut queued).await; - let mut own = project(&manager, &mut receiver, 1).await; + let mut own = project(&manager, &durable, &mut receiver, 1).await; super::drop_watermarks_above_the_live_cursor(&manager, &mut own).await; - assert_eq!(own[&wallet_id].core.synced_height, Some(1_200)); + assert_eq!( + own[&wallet_id].core.synced_height, + Some(1_200), + "the successor's own advance is not mistaken for a stale one" + ); } /// A store panicking mid-batch must not release the writer lock with the @@ -5278,15 +5451,16 @@ mod tests { #[tokio::test] async fn a_batch_spanning_a_rewind_keeps_the_rescans_height_not_the_higher_stale_one() { let (manager, mut receiver, wallet_id) = engine_wallet(0).await; + let durable = super::DurableCursors::default(); engine_advance(&manager, wallet_id, 1_000).await; let first = receiver.try_recv().expect("pre-rewind advance"); let mut batch = BTreeMap::new(); - super::project_event_into_batch(&manager, &first, &mut batch).await; + super::project_event_into_batch(&manager, &durable, &first, &mut batch).await; rewind(&manager, wallet_id, 100).await; engine_advance(&manager, wallet_id, 400).await; let second = receiver.try_recv().expect("rescan advance"); - super::project_event_into_batch(&manager, &second, &mut batch).await; + super::project_event_into_batch(&manager, &durable, &second, &mut batch).await; assert_eq!(batch[&wallet_id].core.synced_height, Some(400)); super::drop_watermarks_above_the_live_cursor(&manager, &mut batch).await; @@ -5319,12 +5493,13 @@ mod tests { /// the watermark forever. #[tokio::test] async fn lossless_burst_never_freezes_and_watermark_reaches_tip() { + let manager = test_manager(); const BURST: u32 = 3000; // >> the old broadcast ring (1000) - let wallet_id = [7u8; 32]; + let wallet_id = insert_test_wallet(&manager).await; let (tx, rx) = unbounded_channel::(); // Pre-buffer the whole burst; an unbounded mpsc cannot drop it. for h in 1..=BURST { - tx.send(sync_height_event(wallet_id, h)).unwrap(); + emit_advance(&manager, &tx, wallet_id, h).await; } let (obs_tx, mut obs_rx) = unbounded_channel(); @@ -5332,7 +5507,7 @@ mod tests { let sync_fault = Arc::new(AtomicBool::new(false)); let cancel = CancellationToken::new(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + Arc::clone(&manager), Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), @@ -5380,10 +5555,11 @@ mod tests { /// such guarantee; see the `Drop` rustdoc on `PlatformWalletManager`. #[tokio::test] async fn cancellation_commits_the_events_already_buffered() { - let wallet_id = [11u8; 32]; + let manager = test_manager(); + let wallet_id = insert_test_wallet(&manager).await; let (tx, rx) = unbounded_channel::(); - tx.send(sync_height_event(wallet_id, 41)).unwrap(); - tx.send(sync_height_event(wallet_id, 42)).unwrap(); + emit_advance(&manager, &tx, wallet_id, 41).await; + emit_advance(&manager, &tx, wallet_id, 42).await; let (obs_tx, mut obs_rx) = unbounded_channel(); let persister = Arc::new(ProbePersister::new(obs_tx)); @@ -5393,7 +5569,7 @@ mod tests { cancel.cancel(); run_wallet_event_adapter( - test_manager(), + Arc::clone(&manager), Arc::downgrade(&persister), rx, Arc::new(AtomicBool::new(false)), @@ -5431,14 +5607,15 @@ mod tests { /// interleaving that exposes it. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn a_cancelled_drain_holds_the_persister_until_its_backlog_is_committed() { + let manager = test_manager(); use std::time::{Duration, Instant}; - let wallet_id = [0x55u8; 32]; + let wallet_id = insert_test_wallet(&manager).await; // One past the limit: the tail event cannot ride the first chunk. let backlog = ADAPTER_STORE_BATCH_LIMIT as u32 + 1; let (tx, rx) = unbounded_channel::(); for height in 1..=backlog { - tx.send(sync_height_event(wallet_id, height)).unwrap(); + emit_advance(&manager, &tx, wallet_id, height).await; } let (obs_tx, mut obs_rx) = unbounded_channel(); @@ -5448,7 +5625,7 @@ mod tests { let cancel = CancellationToken::new(); cancel.cancel(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + Arc::clone(&manager), Arc::downgrade(&persister), rx, Arc::new(AtomicBool::new(false)), @@ -5650,11 +5827,12 @@ mod tests { /// awaiting — making the batch boundary deterministic. #[tokio::test] async fn buffered_events_fold_into_one_store_per_wallet() { - let wallet_id = [11u8; 32]; + let manager = test_manager(); + let wallet_id = insert_test_wallet(&manager).await; // Comfortably larger than the burst, so nothing is dropped. let (tx, rx) = unbounded_channel::(); for h in 1..=5u32 { - tx.send(sync_height_event(wallet_id, h)).unwrap(); + emit_advance(&manager, &tx, wallet_id, h).await; } let (obs_tx, mut obs_rx) = unbounded_channel(); @@ -5662,7 +5840,7 @@ mod tests { let sync_fault = Arc::new(AtomicBool::new(false)); let cancel = CancellationToken::new(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + manager, Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), @@ -5701,21 +5879,22 @@ mod tests { /// other's id. #[tokio::test] async fn batch_folds_per_wallet_not_across_wallets() { - let wallet_a = [1u8; 32]; - let wallet_b = [2u8; 32]; + let manager = test_manager(); + let wallet_a = insert_test_wallet(&manager).await; + let wallet_b = insert_test_wallet(&manager).await; let (tx, rx) = unbounded_channel::(); // Interleaved on purpose. - tx.send(sync_height_event(wallet_a, 10)).unwrap(); - tx.send(sync_height_event(wallet_b, 20)).unwrap(); - tx.send(sync_height_event(wallet_a, 11)).unwrap(); - tx.send(sync_height_event(wallet_b, 21)).unwrap(); + emit_advance(&manager, &tx, wallet_a, 10).await; + emit_advance(&manager, &tx, wallet_b, 20).await; + emit_advance(&manager, &tx, wallet_a, 11).await; + emit_advance(&manager, &tx, wallet_b, 21).await; let (obs_tx, mut obs_rx) = unbounded_channel(); let persister = Arc::new(ProbePersister::new(obs_tx)); let sync_fault = Arc::new(AtomicBool::new(false)); let cancel = CancellationToken::new(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + manager, Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), @@ -5983,12 +6162,20 @@ mod tests { /// faulted every wallet in the drain. #[tokio::test] async fn a_panicking_commit_spares_the_wallets_it_already_stored() { + let manager = test_manager(); // `BTreeMap` order decides who is committed first, so the ids are // chosen to put the healthy wallet ahead of the panicking one. - let healthy = [0x11u8; 32]; - let doomed = [0x22u8; 32]; + let healthy = insert_test_wallet(&manager).await; + let doomed = insert_test_wallet(&manager).await; // Sorts after `doomed`, so the commit unwinds before it is reached. - let unreached = [0x33u8; 32]; + let unreached = insert_test_wallet(&manager).await; + // `BTreeMap` order decides who is committed first: sort the random ids + // so the roles keep that order. + let (healthy, doomed, unreached) = { + let mut ids = [healthy, doomed, unreached]; + ids.sort(); + (ids[0], ids[1], ids[2]) + }; let (tx, rx) = unbounded_channel::(); let (obs_tx, mut obs_rx) = unbounded_channel(); @@ -5997,7 +6184,7 @@ mod tests { let sync_fault = Arc::new(AtomicBool::new(false)); let cancel = CancellationToken::new(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + Arc::clone(&manager), Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), @@ -6025,7 +6212,7 @@ mod tests { // The healthy wallet's watermark must still advance: its store // returned, so its rows are accounted for. - tx.send(sync_height_event(healthy, 900)).unwrap(); + emit_advance(&manager, &tx, healthy, 900).await; // Bounded: on a regression the healthy wallet is frozen, its // watermark-only changeset collapses to nothing, and no store is // observed at all — an unbounded `recv` would hang CI instead of @@ -6043,7 +6230,7 @@ mod tests { // The wallet whose store panicked must be frozen. tx.send(block_processed_event(doomed, 60)).unwrap(); - tx.send(sync_height_event(doomed, 900)).unwrap(); + emit_advance(&manager, &tx, doomed, 900).await; let after_doomed = tokio::time::timeout(std::time::Duration::from_secs(5), obs_rx.recv()) .await .expect("a faulted wallet must still persist its rows") @@ -6060,7 +6247,7 @@ mod tests { // wallet's. A handler that faults only the direct casualty leaves this // one free to advance past rows that never landed. tx.send(block_processed_event(unreached, 60)).unwrap(); - tx.send(sync_height_event(unreached, 900)).unwrap(); + emit_advance(&manager, &tx, unreached, 900).await; let after_unreached = tokio::time::timeout(std::time::Duration::from_secs(5), obs_rx.recv()) .await @@ -6303,7 +6490,8 @@ mod tests { /// any other. #[tokio::test] async fn sweep_with_declared_capability_does_not_freeze() { - let wallet_id = [22u8; 32]; + let manager = test_manager(); + let wallet_id = insert_test_wallet(&manager).await; let (tx, rx) = unbounded_channel::(); let (obs_tx, mut obs_rx) = unbounded_channel(); let persister = Arc::new(ProbePersister::with_capabilities( @@ -6313,7 +6501,7 @@ mod tests { let sync_fault = Arc::new(AtomicBool::new(false)); let cancel = CancellationToken::new(); let handle = tokio::spawn(run_wallet_event_adapter( - test_manager(), + Arc::clone(&manager), Arc::downgrade(&persister), rx, Arc::clone(&sync_fault), @@ -6324,7 +6512,7 @@ mod tests { tx.send(swept_event(wallet_id, 0x61, 0x62)).unwrap(); // A watermark-bearing event right behind it, folded or not — either // way it must reach the store untouched while the capability holds. - tx.send(sync_height_event(wallet_id, 700)).unwrap(); + emit_advance(&manager, &tx, wallet_id, 700).await; let mut last_synced = None; // Drain until a store carries the watermark. Each receive is bounded: diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index cd8c0deed19..e4f8bfa9a34 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -48,7 +48,7 @@ pub use client_start_state::ClientStartState; pub use client_wallet_start_state::ClientWalletStartState; pub use core_bridge::{ spawn_wallet_event_adapter, spawn_wallet_event_adapter_with_durable_cursors, DurableCursor, - DurableCursors, + DurableCursorState, DurableCursors, RetiredBarrier, }; pub use dashpay_backfill::{DashPayBackfillCoveredContact, DashPayBackfillRecord}; pub use identity_manager_start_state::IdentityManagerStartState; diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 58560ce56a3..c867e29620f 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -279,8 +279,9 @@ impl PlatformWalletManager

{ // event adapter; seeding after it would let an adapter commit or // a reconcile record a newer durable height first, and the seed // would then overwrite it with the stale loaded one. An entry a - // removed or rolled-back registration left behind is overwritten - // here the same way, so it is never read against a live wallet. + // removed same-id predecessor left behind is what the host holds + // now — a commit of its may have been accepted after the start + // state was read — so it wins over the loaded cursor. { let mut durable_cursors = self.durable_cursors.lock().await; let mut wm = self.wallet_manager.write().await; @@ -294,10 +295,9 @@ impl PlatformWalletManager

{ ))); break 'load; } - durable_cursors.insert( - wallet_id, - crate::changeset::DurableCursor::at(loaded_cursor), - ); + durable_cursors + .entry(wallet_id) + .or_insert(crate::changeset::DurableCursor::at(loaded_cursor)); self.inherit_rewind_barrier(&mut wm, &wallet_id); } inserted_in_manager.push(wallet_id); diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 47accb931d8..e7d4509e458 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -445,11 +445,6 @@ pub struct PlatformWalletManager { /// the wallet-event adapter and the DashPay rescan reconcile — the lock /// that orders their cursor writes. See [`DurableCursors`]. pub(super) durable_cursors: DurableCursors, - /// The [`RewindBarrier`](crate::changeset::core_bridge::RewindBarrier) - /// state of removed wallets, keyed by id, for a successor registered - /// under the same id to inherit: the shared event adapter can still hold - /// advances the removed wallet emitted (dashpay/platform#4302 review). - pub(super) retired_barriers: std::sync::Mutex>, /// Per-WALLET in-broadcast fence maps, handed to every /// [`WalletGeneration`](crate::wallet::core::WalletGeneration) registered /// under each id. @@ -621,28 +616,32 @@ impl PlatformWalletManager

{ registry, sync_fault, durable_cursors, - retired_barriers: std::sync::Mutex::new(std::collections::BTreeMap::new()), in_broadcast_fences: std::sync::Mutex::new(std::collections::BTreeMap::new()), } } /// Remove `wallet_id` from the inner manager, keeping its rewind - /// barrier's in-flight state for a successor registered under the same - /// id (see [`Self::inherit_rewind_barrier`]). + /// barrier's state under the id in the shared [`DurableCursors`] for a + /// successor registered under the same id (see + /// [`Self::inherit_rewind_barrier`]) — and for the adapter, which drops + /// the removed wallet's advances while nothing holds the id and counts + /// them off this state as it does. Called under the manager write lock, + /// which is what makes the hand-off atomic against the adapter. pub(super) fn remove_from_wallet_manager( &self, wm: &mut WalletManager, wallet_id: &WalletId, ) -> Result<(), key_wallet_manager::WalletError> { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; let (_, info) = wm.remove_wallet(wallet_id)?; - let (in_flight, epoch) = info.rewind_barrier.retire(); - let mut retired = self - .retired_barriers - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - let entry = retired.entry(*wallet_id).or_insert((0, 0)); - entry.0 = entry.0.saturating_add(in_flight); - entry.1 = entry.1.max(epoch); + let retiring = info + .rewind_barrier + .retire(WalletInfoInterface::account_generation(&info)); + let mut retired = self.durable_cursors.retired(); + let entry = retired.entry(*wallet_id).or_default(); + entry.in_flight = entry.in_flight.saturating_add(retiring.in_flight); + entry.epoch = entry.epoch.max(retiring.epoch); + entry.account_generation = entry.account_generation.max(retiring.account_generation); Ok(()) } @@ -656,11 +655,7 @@ impl PlatformWalletManager

{ wm: &mut WalletManager, wallet_id: &WalletId, ) { - let retired = self - .retired_barriers - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .remove(wallet_id); + let retired = self.durable_cursors.retired().remove(wallet_id); if let (Some(retired), Some(info)) = (retired, wm.get_wallet_info_mut(wallet_id)) { info.rewind_barrier.inherit(retired); } diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 965c4f40192..f7db6572145 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -19,6 +19,7 @@ use crate::error::PlatformWalletError; use crate::wallet::core::WalletGeneration; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; use crate::wallet::PlatformWallet; +use dpp::prelude::Identifier; use super::PlatformWalletManager; @@ -418,9 +419,8 @@ impl PlatformWalletManager

{ // The same read also yields what the host holds for every persisted // wallet: a registration that recreates a wallet the host already has // must measure durable writes against the host's cursor, not the one - // it just built, and must keep the host's DashPay backfill record - // rather than start from an empty one that re-arms every covered - // contact (see the seed below). + // it just built, and keeps the host's DashPay backfill record for the + // receival accounts it holds (see the seed below). let load_persister: Arc = Arc::clone(&self.persister) as _; let (mut persisted_platform_addresses, mut host_wallets) = match super::run_blocking_load(move || load_persister.load()).await { @@ -470,19 +470,31 @@ impl PlatformWalletManager

{ // The insert makes the wallet visible to the scanner and the event // adapter; seeding after it would let an adapter commit or a // reconcile record a newer durable height first, only for the seed to - // overwrite it with the creation one. An entry a removed or - // rolled-back registration left behind is overwritten here the same - // way, so it is never read against a live wallet. + // overwrite it with the creation one. // // A registration can recreate a wallet the host already holds (the // same seed added again on a fresh manager). The host keeps that // row's cursor — registration writes no core cursor, and a host // preserves `syncedHeight` on the metadata upsert — so the seed is - // the host's cursor, not the freshly built one. When the built cursor - // is lower, the wallet is in effect reset in memory only: that - // reset is owed to the host like any other, and the next DashPay - // backfill record round carries it, so coverage never reaches disk - // beside the host's higher cursor (dashpay/platform#4302 review). + // the host's cursor, not the freshly built one. The snapshot above + // was read before this lock, and a same-id predecessor still + // registered then may have had a commit accepted since: the entry + // that commit left in `DurableCursors` is what the host holds now, + // so it wins over the snapshot. With no host row at all the host + // holds nothing, and a leftover entry is discarded. When the built + // cursor is lower than the host's, the wallet is in effect reset in + // memory only: that reset is owed to the host like any other, and + // the next DashPay backfill record round carries it, so coverage + // never reaches disk beside the host's higher cursor + // (dashpay/platform#4302 review). + // + // The host's record is kept only for the receival accounts the + // recreated wallet actually holds — none, when it is built from the + // seed. Coverage for an account that is absent while this wallet + // scans is not coverage: the scan does not watch the account's + // scripts, and the account-generation guard only stops batches still + // in flight, not an advance already committed. Those accounts re-arm + // when they are registered, at the cost of one more backfill. let created_cursor = platform_info.core_wallet.metadata.synced_height; let wallet_id = { let mut durable_cursors = self.durable_cursors.lock().await; @@ -496,14 +508,47 @@ impl PlatformWalletManager

{ other )), })?; - let (host_cursor, host_backfill) = host_wallets - .remove(&wallet_id) - .unwrap_or((created_cursor, Default::default())); + let (host_cursor, host_backfill) = match host_wallets.remove(&wallet_id) { + Some((snapshot_cursor, record)) => ( + durable_cursors + .get(&wallet_id) + .map_or(snapshot_cursor, |entry| entry.height), + record, + ), + None => { + durable_cursors.remove(&wallet_id); + (created_cursor, Default::default()) + } + }; if let Some(info) = wm.get_wallet_info_mut(&wallet_id) { - // The host's record vouches for `[covered_from, host_cursor]` - // per account; the recreated wallet scans from no higher than - // that, so the coverage stays true. - info.dashpay_backfill = host_backfill; + let held: Vec<(Identifier, Identifier, u32)> = info + .core_wallet + .accounts + .dashpay_receival_accounts + .keys() + .map(|key| { + ( + Identifier::from(key.user_identity_id), + Identifier::from(key.friend_identity_id), + key.index, + ) + }) + .collect(); + let mut record = host_backfill; + let dropped = record + .retain(|owner, contact, index| held.contains(&(*owner, *contact, index))); + if dropped && record.is_empty() { + record = Default::default(); + } + if dropped { + tracing::info!( + wallet_id = %hex::encode(wallet_id), + kept = record.covered.len(), + "wallet recreated: DashPay backfill coverage for receival accounts it \ + does not hold is re-armed" + ); + } + info.dashpay_backfill = record; if created_cursor < host_cursor { // Owed from this wallet's first epoch: any advance the // host accepts from its own scan pays it. @@ -1308,12 +1353,16 @@ mod register_wallet_duplicate_tests { ); } - /// Removing a wallet keeps its rewind barrier's in-flight state, and a - /// same-id registration inherits it in the critical section that - /// publishes it, so advances the removed wallet emitted cannot be stored - /// against its successor (dashpay/platform#4302 review). + /// Removing a wallet keeps its rewind barrier's state, and a same-id + /// registration inherits it in the critical section that publishes it: + /// advances the removed wallet emitted cannot be stored against its + /// successor, and the successor's account generation starts past the + /// predecessor's, so no filter batch scanned against the predecessor's + /// account set is certified for it (dashpay/platform#4302 review). #[tokio::test] async fn a_same_id_registration_inherits_the_removed_wallets_barrier() { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + let manager = make_manager(); let network = Network::Testnet; let mnemonic = Mnemonic::from_phrase(TEST_MNEMONIC).expect("valid test mnemonic"); @@ -1332,32 +1381,42 @@ mod register_wallet_duplicate_tests { .await .expect("first create") .wallet_id(); - { - // Three advances emitted and not yet drained by the adapter. + let predecessor_generation = { + // Three advances emitted and not yet drained by the adapter, and + // a receival account registered. let mut wm = manager.wallet_manager.write().await; - let barrier = &mut wm - .get_wallet_info_mut(&wallet_id) - .expect("info") - .rewind_barrier; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); for _ in 0..3 { - barrier.note_emitted(); + info.rewind_barrier.note_emitted(); } - barrier.arm(); - } + info.rewind_barrier.arm(); + info.rewind_barrier.note_account_registered(); + WalletInfoInterface::account_generation(info) + }; manager.remove_wallet(&wallet_id).await.expect("remove"); + assert_eq!( + manager.durable_cursors.retired()[&wallet_id].in_flight, + 3, + "removal retires the barrier under the id" + ); create(Arc::clone(&manager)).await.expect("second create"); + assert!( + manager.durable_cursors.retired().get(&wallet_id).is_none(), + "the successor took the retired state over" + ); let wm = manager.wallet_manager.read().await; - let (in_flight, epoch) = wm - .get_wallet_info(&wallet_id) - .expect("published") - .rewind_barrier - .retire(); + let info = wm.get_wallet_info(&wallet_id).expect("published"); + let retired = info.rewind_barrier.retire(0); assert_eq!( - in_flight, 3, + retired.in_flight, 3, "the predecessor's queued advances are carried" ); - assert!(epoch > 1, "the epoch moves past the predecessor's"); + assert!(retired.epoch > 1, "the epoch moves past the predecessor's"); + assert!( + WalletInfoInterface::account_generation(info) > predecessor_generation, + "the successor's account generation starts past the predecessor's" + ); } /// The durable cursor is seeded in the critical section that publishes diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index b978d048287..0bdd137c3b8 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -4262,6 +4262,23 @@ mod tests { Arc, Arc, WalletId, + ) { + recreate_on_host_at(backfill, 1_000, None).await + } + + /// [`recreate_on_host`] with the host row at `host_cursor`, and + /// optionally a `DurableCursors` entry already under the wallet's id + /// before the registration — what a same-id predecessor's commit, + /// accepted after the registration read its snapshot, leaves behind. + async fn recreate_on_host_at( + backfill: crate::changeset::DashPayBackfillRecord, + host_cursor: u32, + leftover_entry: Option, + ) -> ( + Arc>, + Arc, + Arc, + WalletId, ) { let (donor, _donor_persister, wallet_id) = make_wallet().await; let (wallet, mut managed) = { @@ -4274,7 +4291,7 @@ mod tests { .clone(), ) }; - managed.metadata.synced_height = 1_000; + managed.metadata.synced_height = host_cursor; let host = Arc::new(ReloadPersister { wallet, managed, @@ -4285,6 +4302,13 @@ mod tests { let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); let handler: Arc = Arc::new(NoopEventHandler); let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + if let Some(entry) = leftover_entry { + manager + .durable_cursors + .lock() + .await + .insert(wallet_id, entry); + } let seed = Mnemonic::from_phrase(TEST_MNEMONIC) .expect("valid mnemonic") .to_seed(""); @@ -4398,35 +4422,55 @@ mod tests { ); } - /// Recreating a wallet keeps the host's backfill record instead of - /// starting from an empty one that re-arms every covered contact. With - /// every contact covered there is no contact to handle, yet the reset the - /// recreation owes must still reach the host: the reconcile runs a - /// record-only round for the debt instead of returning early, keeping - /// the carried coverage (dashpay/platform#4302 review). + /// Coverage for a receival account the recreated wallet does not hold is + /// not coverage: the successor's scan does not watch that account's + /// scripts, and the account-generation guard only stops batches still in + /// flight, not an advance already committed. So recreation keeps the + /// host's record only for the accounts it holds — none, from the seed — + /// and the account is rewound for when it is registered, even though the + /// host's record said it was covered (dashpay/platform#4302 review). #[tokio::test] - async fn a_recreated_wallet_keeps_the_hosts_record_and_still_pays_its_owed_cursor() { + async fn a_recreated_wallet_rearms_coverage_for_receival_accounts_it_does_not_hold() { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + let owner = Identifier::from([0xAA; 32]); let contact = Identifier::from([0xBB; 32]); let mut host_record = crate::changeset::DashPayBackfillRecord::default(); host_record.record_pass(900, Some(200), [(owner, contact, 0, 200)]); - let (manager, host, recreated, wallet_id) = recreate_on_host(host_record.clone()).await; + let (manager, host, recreated, wallet_id) = recreate_on_host(host_record).await; { let wm = manager.wallet_manager.read().await; let record = &wm .get_wallet_info(&wallet_id) .expect("info") .dashpay_backfill; - assert_eq!( - record.covered, host_record.covered, - "the host's coverage is kept" + assert!( + record.is_empty(), + "no receival account is held, so nothing is covered" ); - assert_eq!((record.floor, record.rewound_from), (200, 900)); + assert!(!record.has_extent()); assert_eq!(record.unpersisted_cursor, Some(100)); } - establish_on_recreated(&manager, &host, &recreated, owner, contact, 200).await; + // The successor scans 101..1500 before the account exists, and the + // host accepts that advance (which also pays the owed 100). + { + let mut durable = manager.durable_cursors.lock().await; + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(1_500); + durable.insert( + wallet_id, + crate::changeset::DurableCursor { + height: 1_500, + advance_epoch: Some(0), + }, + ); + } host.stores.lock().unwrap().clear(); + establish_on_recreated(&manager, &host, &recreated, owner, contact, 200).await; assert_eq!( recreated .identity() @@ -4434,21 +4478,106 @@ mod tests { .reconcile_dashpay_rescan() .await .expect("reconcile"), - None, - "the contact is covered by the carried record: no rewind" + Some(200), + "101..1500 was scanned without the account: the old coverage must not suppress this" ); let stores = host.stores.lock().unwrap(); - let round = last_stored_record(&stores).expect("a record-only round pays the debt"); - let core = round.core.as_ref().expect("carrying the owed cursor"); - assert_eq!(core.synced_height, Some(100)); + let round = last_stored_record(&stores).expect("the rewind is recorded"); + let core = round.core.as_ref().expect("the rewind rides the round"); + assert_eq!(core.synced_height, Some(200)); assert!(core.synced_height_is_rewind); + } + + /// The host snapshot a registration reads comes before it takes the + /// durable-cursor lock, and a same-id predecessor still registered then + /// can have a commit accepted in between. Under the lock the entry that + /// commit left is what the host holds now, so it wins over the snapshot: + /// the seed is 1000, not the snapshot's 100, and the built cursor is owed + /// against it (dashpay/platform#4302 review). + #[tokio::test] + async fn a_recreated_wallet_seeds_from_the_cursor_a_predecessor_left_not_the_snapshot() { + let (manager, _host, _recreated, wallet_id) = recreate_on_host_at( + Default::default(), + 100, + Some(crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }), + ) + .await; assert_eq!( - round + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .map(|cursor| cursor.height), + Some(1_000), + "the predecessor's accepted commit, not the stale snapshot" + ); + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") .dashpay_backfill - .as_ref() - .unwrap() - .covered_from(&owner, &contact, 0), - Some(200) + .unpersisted_cursor, + Some(100), + "the built cursor is owed against the host's real cursor" + ); + } + + /// With no host row for the wallet, the host holds nothing: an entry a + /// removed wallet left under the id is stale and is discarded, and the + /// wallet seeds from its built cursor with nothing owed. + #[tokio::test] + async fn a_leftover_cursor_entry_without_a_host_row_is_discarded() { + let (donor, _, wallet_id) = make_wallet().await; + drop(donor); + let persister = Arc::new(RecordingPersister::default()); // `load` returns no rows + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new( + sdk, + Arc::clone(&persister), + handler, + )); + manager.durable_cursors.lock().await.insert( + wallet_id, + crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }, + ); + let seed = Mnemonic::from_phrase(TEST_MNEMONIC) + .expect("valid mnemonic") + .to_seed(""); + let wallet = manager + .create_wallet_from_seed_bytes( + Network::Testnet, + &seed, + WalletAccountCreationOptions::Default, + Some(101), + ) + .await + .expect("create"); + assert_eq!(wallet.wallet_id(), wallet_id); + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(), + Some(crate::changeset::DurableCursor::at(100)), + "seeded from the built cursor, the stale entry gone" + ); + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .unpersisted_cursor, + None ); } diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs index 1898040b338..c48d02552f1 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet_traits.rs @@ -156,10 +156,16 @@ impl WalletInfoInterface for PlatformWalletInfo { /// platform wallet — so a batch in flight across a contact-account /// registration could certify coverage the DashPay backfill record then /// vouched for (dashpay/platform#4302 review). + /// + /// Also offset past a removed same-id predecessor's final generation + /// (see `RewindBarrier::inherit`): both counters restart with the wallet, + /// so without the offset a batch reconciled against the predecessor's + /// account set could match this wallet's generation and be certified. fn account_generation(&self) -> u64 { self.core_wallet .account_generation() .wrapping_add(self.rewind_barrier.account_registrations()) + .wrapping_add(self.rewind_barrier.generation_base()) } fn synced_height(&self) -> CoreBlockHeight { From 72b34c0e7b9858179c1c8b4ec55e39f19be7d435 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Thu, 1 Oct 2026 22:16:46 -0700 Subject: [PATCH 16/43] fix(platform-wallet): record contacts as forward-covered when the scan has not started reconcile_dashpay_rescan returned early at synced_height == 0, so a receival account registered before the scan started (the startup drain builds them before SPV runs) was never recorded. Once the scan had climbed past that contact's funding height, the next sweep saw an uncovered contact below the cursor and rewound for a range the scan had watched the account through. No checkpoint is below 0, so the ordinary path handles the pass: every candidate is forward-covered, recorded with no cursor write, and never rewound for later. The int line had this behaviour from the earlier contact-scan work; the record makes it durable. Co-Authored-By: Claude Fable 5.1 --- .../src/wallet/identity/network/payments.rs | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 0bdd137c3b8..7b3757e9c43 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -162,12 +162,15 @@ impl DashPayView<'_, B> { ); } + // 0 means "scan from genesis / not yet started": a full historical + // scan is coming, so nothing is backfilled toward it — but every + // candidate is still handled, as forward-covered. Returning early + // instead left contacts the startup drain had just registered + // unrecorded, and the first sweep after the scan had climbed past + // their funding heights rewound mid-session for a range that scan had + // watched them through. No checkpoint is below 0, so the path below + // rewinds nothing here. let synced_height = info.core_wallet.synced_height(); - // 0 means "scan from genesis / not yet started" — already a full - // historical scan, nothing to backfill toward. - if synced_height == 0 { - return Ok(None); - } // (owner, contact, account_index) of every receival account — we can // only watch a contact's incoming addresses once its receival account @@ -3219,8 +3222,10 @@ mod tests { } /// `synced_height == 0` means "scan from genesis / not started" — already a - /// full historical scan, so the rescan is a no-op (the masking path the spec - /// warns about). + /// full historical scan, so the rescan rewinds nothing (the masking path the + /// spec warns about). The contact is still recorded as forward-covered, so + /// the sweep does not rewind for it once that scan has climbed past its + /// funding height. #[tokio::test] async fn rescan_is_a_noop_when_synced_height_is_zero() { let (manager, persister, wallet_id) = make_wallet().await; @@ -3242,6 +3247,14 @@ mod tests { "synced_height 0 -> no rescan" ); assert_eq!(synced_height(&manager, wallet_id).await, 0); + let wm = manager.wallet_manager.read().await; + assert!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .covers(&owner, &contact, 0, 100), + "recorded as forward-covered by the coming scan" + ); } /// The persisted half of the rescan guard, captured from a live manager From 8c323306f3e0cd491a68504bc709784a3a7a23ef Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Tue, 6 Oct 2026 12:35:42 -0700 Subject: [PATCH 17/43] fix(platform-wallet): keep a predecessor's cursor over the pre-lock snapshot and persist re-armed coverage before publishing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ninth review round on the DashPay backfill record (dashpay/platform#4302), two carried findings on same-id wallet recreation: - Snapshot ordering. The host snapshot a registration or load reads precedes the durable-cursor lock. Under the lock, an entry in DurableCursors now always wins — a predecessor's accepted commit, or a whole same-id registration that created the row and was removed again — whether or not the snapshot had a row, and a live cursor below it (the built or loaded one) is owed to the host in the wallet's inherited epoch. Before, the load path kept the entry but owed nothing, and registration discarded the entry when the snapshot had no row. - Durable re-arm. Filtering the host's record to the receival accounts a recreated wallet holds happened in memory only; the host still held the old record and the account rows, and a crash after the successor advanced restored both beside the higher cursor. The filtered record is now stored to the host before the wallet is published to the scanner; a store failure aborts the registration with nothing published. Co-Authored-By: Claude Fable 5.1 --- .../rs-platform-wallet/src/manager/load.rs | 15 +- .../src/manager/wallet_lifecycle.rs | 144 +++++++++------ .../src/wallet/identity/network/payments.rs | 172 ++++++++++++++++-- 3 files changed, 254 insertions(+), 77 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index c867e29620f..ed6926c5f5a 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -281,7 +281,11 @@ impl PlatformWalletManager

{ // would then overwrite it with the stale loaded one. An entry a // removed same-id predecessor left behind is what the host holds // now — a commit of its may have been accepted after the start - // state was read — so it wins over the loaded cursor. + // state was read — so it wins over the loaded cursor, and a + // loaded cursor below it is owed to the host like any in-memory + // reset, in this wallet's inherited epoch, so no coverage is + // recorded beside the host's higher cursor without a reset + // (dashpay/platform#4302 review). { let mut durable_cursors = self.durable_cursors.lock().await; let mut wm = self.wallet_manager.write().await; @@ -295,10 +299,19 @@ impl PlatformWalletManager

{ ))); break 'load; } + let host_cursor = durable_cursors + .get(&wallet_id) + .map_or(loaded_cursor, |entry| entry.height); durable_cursors .entry(wallet_id) .or_insert(crate::changeset::DurableCursor::at(loaded_cursor)); self.inherit_rewind_barrier(&mut wm, &wallet_id); + if loaded_cursor < host_cursor { + if let Some(info) = wm.get_wallet_info_mut(&wallet_id) { + let epoch = info.rewind_barrier.epoch(); + info.dashpay_backfill.owe_cursor(loaded_cursor, epoch); + } + } } inserted_in_manager.push(wallet_id); diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index f7db6572145..2b62a093e19 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -478,13 +478,14 @@ impl PlatformWalletManager

{ // preserves `syncedHeight` on the metadata upsert — so the seed is // the host's cursor, not the freshly built one. The snapshot above // was read before this lock, and a same-id predecessor still - // registered then may have had a commit accepted since: the entry - // that commit left in `DurableCursors` is what the host holds now, - // so it wins over the snapshot. With no host row at all the host - // holds nothing, and a leftover entry is discarded. When the built - // cursor is lower than the host's, the wallet is in effect reset in - // memory only: that reset is owed to the host like any other, and - // the next DashPay backfill record round carries it, so coverage + // registered then may have had a commit accepted since — or a whole + // same-id registration may have come and gone, creating the row the + // snapshot did not see. The entry those writes left in + // `DurableCursors` is what the host holds now, so it wins over the + // snapshot, row or no row. When the built cursor is lower than the + // host's, the wallet is in effect reset in memory only: that reset is + // owed to the host like any other, in this wallet's inherited epoch, + // and the next DashPay backfill record round carries it, so coverage // never reaches disk beside the host's higher cursor // (dashpay/platform#4302 review). // @@ -493,11 +494,63 @@ impl PlatformWalletManager

{ // seed. Coverage for an account that is absent while this wallet // scans is not coverage: the scan does not watch the account's // scripts, and the account-generation guard only stops batches still - // in flight, not an advance already committed. Those accounts re-arm - // when they are registered, at the cost of one more backfill. + // in flight, not an advance already committed. The host still holds + // the old record and the account rows, and would restore both beside + // this wallet's advanced cursor after a crash — so the filtered + // record is stored BEFORE the wallet is published to the scanner. + // Those accounts re-arm when they are registered, at the cost of one + // more backfill. let created_cursor = platform_info.core_wallet.metadata.synced_height; + let (snapshot_cursor, mut host_backfill) = + match host_wallets.remove(®istration_wallet_id) { + Some((cursor, record)) => (Some(cursor), record), + None => (None, Default::default()), + }; + let held: Vec<(Identifier, Identifier, u32)> = platform_info + .core_wallet + .accounts + .dashpay_receival_accounts + .keys() + .map(|key| { + ( + Identifier::from(key.user_identity_id), + Identifier::from(key.friend_identity_id), + key.index, + ) + }) + .collect(); + let dropped = + host_backfill.retain(|owner, contact, index| held.contains(&(*owner, *contact, index))); + if dropped && host_backfill.is_empty() { + host_backfill = Default::default(); + } let wallet_id = { let mut durable_cursors = self.durable_cursors.lock().await; + if dropped { + tracing::info!( + wallet_id = %hex::encode(registration_wallet_id), + kept = host_backfill.covered.len(), + "wallet recreated: DashPay backfill coverage for receival accounts it \ + does not hold is re-armed on the host before the wallet is published" + ); + self.persister + .store( + registration_wallet_id, + crate::changeset::PlatformWalletChangeSet { + dashpay_backfill: Some(host_backfill.clone()), + ..Default::default() + }, + ) + .map_err(|e| { + tracing::error!( + wallet_id = %hex::encode(registration_wallet_id), + error = %e, + "failed to re-arm the host's DashPay backfill coverage; \ + registration aborted before the wallet was registered" + ); + PlatformWalletError::from_store_failure(&*self.persister, e) + })?; + } let mut wm = self.wallet_manager.write().await; let wallet_id = wm .insert_wallet(wallet, platform_info) @@ -508,56 +561,22 @@ impl PlatformWalletManager

{ other )), })?; - let (host_cursor, host_backfill) = match host_wallets.remove(&wallet_id) { - Some((snapshot_cursor, record)) => ( - durable_cursors - .get(&wallet_id) - .map_or(snapshot_cursor, |entry| entry.height), - record, - ), - None => { - durable_cursors.remove(&wallet_id); - (created_cursor, Default::default()) - } - }; + let host_cursor = durable_cursors + .get(&wallet_id) + .map(|entry| entry.height) + .or(snapshot_cursor) + .unwrap_or(created_cursor); + self.inherit_rewind_barrier(&mut wm, &wallet_id); if let Some(info) = wm.get_wallet_info_mut(&wallet_id) { - let held: Vec<(Identifier, Identifier, u32)> = info - .core_wallet - .accounts - .dashpay_receival_accounts - .keys() - .map(|key| { - ( - Identifier::from(key.user_identity_id), - Identifier::from(key.friend_identity_id), - key.index, - ) - }) - .collect(); - let mut record = host_backfill; - let dropped = record - .retain(|owner, contact, index| held.contains(&(*owner, *contact, index))); - if dropped && record.is_empty() { - record = Default::default(); - } - if dropped { - tracing::info!( - wallet_id = %hex::encode(wallet_id), - kept = record.covered.len(), - "wallet recreated: DashPay backfill coverage for receival accounts it \ - does not hold is re-armed" - ); - } - info.dashpay_backfill = record; + info.dashpay_backfill = host_backfill; if created_cursor < host_cursor { - // Owed from this wallet's first epoch: any advance the - // host accepts from its own scan pays it. let epoch = info.rewind_barrier.epoch(); info.dashpay_backfill.owe_cursor(created_cursor, epoch); } } - durable_cursors.insert(wallet_id, crate::changeset::DurableCursor::at(host_cursor)); - self.inherit_rewind_barrier(&mut wm, &wallet_id); + durable_cursors + .entry(wallet_id) + .or_insert(crate::changeset::DurableCursor::at(host_cursor)); wallet_id }; @@ -1424,8 +1443,8 @@ mod register_wallet_duplicate_tests { /// reconcile could record a newer durable height in between and the seed /// would overwrite it with the creation one. While the cursor lock is /// held the registration must not publish the wallet at all, and once it - /// does, the entry is the created cursor — never one left behind by an - /// earlier registration of the same id. + /// does, an entry an earlier same-id registration left is what the host + /// holds now, so it is kept and the lower created cursor is owed to it. #[tokio::test] async fn a_wallet_is_published_and_its_durable_cursor_seeded_in_one_step() { let manager = make_manager(); @@ -1484,8 +1503,21 @@ mod register_wallet_duplicate_tests { .await .get(&wallet_id) .map(|cursor| cursor.height), + Some(777), + "the entry the earlier registration left is what the host holds" + ); + assert!(created_cursor < 777); + assert_eq!( + manager + .wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .expect("published") + .dashpay_backfill + .unpersisted_cursor, Some(created_cursor), - "the seed is the created cursor, not the leftover entry" + "the lower created cursor is owed" ); } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 7b3757e9c43..7a8ce007ad0 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -4450,7 +4450,7 @@ mod tests { let contact = Identifier::from([0xBB; 32]); let mut host_record = crate::changeset::DashPayBackfillRecord::default(); host_record.record_pass(900, Some(200), [(owner, contact, 0, 200)]); - let (manager, host, recreated, wallet_id) = recreate_on_host(host_record).await; + let (manager, host, recreated, wallet_id) = recreate_on_host(host_record.clone()).await; { let wm = manager.wallet_manager.read().await; let record = &wm @@ -4464,6 +4464,21 @@ mod tests { assert!(!record.has_extent()); assert_eq!(record.unpersisted_cursor, Some(100)); } + // The invalidation reached the host before anything else of this + // registration did: the host would otherwise restore the old + // coverage (and A's account rows) beside this wallet's advanced + // cursor after a crash. + let invalidated = { + let stores = host.stores.lock().unwrap(); + let (_, first) = stores.first().expect("the registration stored something"); + assert!(first.core.is_none(), "a record-only round"); + let record = first + .dashpay_backfill + .clone() + .expect("the first store of the registration is the re-armed record"); + assert!(record.is_empty()); + record + }; // The successor scans 101..1500 before the account exists, and the // host accepts that advance (which also pays the owed 100). @@ -4494,11 +4509,68 @@ mod tests { Some(200), "101..1500 was scanned without the account: the old coverage must not suppress this" ); - let stores = host.stores.lock().unwrap(); - let round = last_stored_record(&stores).expect("the rewind is recorded"); - let core = round.core.as_ref().expect("the rewind rides the round"); - assert_eq!(core.synced_height, Some(200)); - assert!(core.synced_height_is_rewind); + { + let stores = host.stores.lock().unwrap(); + let round = last_stored_record(&stores).expect("the rewind is recorded"); + let core = round.core.as_ref().expect("the rewind rides the round"); + assert_eq!(core.synced_height, Some(200)); + assert!(core.synced_height_is_rewind); + } + + // The crash case: the host restores A's account rows with the cursor + // the successor persisted (1500) and whatever record it holds. With + // the invalidated record the reload rewinds for A; with the old + // record it would have trusted coverage over a range scanned without + // A — which is what storing the invalidation first prevents. + let (wallet, managed, identity) = { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + let mut managed = info.core_wallet.clone(); + managed.metadata.synced_height = 1_500; + let mut identity = info + .identity_manager + .managed_identity(&owner) + .expect("owner") + .clone(); + identity.dashpay_rescan_triggered_mut().clear(); + ( + wm.get_wallet(&wallet_id).expect("wallet").clone(), + managed, + identity, + ) + }; + for (record, expected, why) in [ + (invalidated, Some(200), "the invalidated record re-arms A"), + ( + host_record, + None, + "the old record would have skipped 1001..1500", + ), + ] { + let mut identities = std::collections::BTreeMap::new(); + identities.insert(0, identity.clone()); + let (restarted, _, _) = reload(ReloadPersister { + wallet: wallet.clone(), + managed: managed.clone(), + identities, + backfill: record, + stores: Mutex::default(), + }) + .await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile after reload"), + expected, + "{why}" + ); + } } /// The host snapshot a registration reads comes before it takes the @@ -4539,11 +4611,12 @@ mod tests { ); } - /// With no host row for the wallet, the host holds nothing: an entry a - /// removed wallet left under the id is stale and is discarded, and the - /// wallet seeds from its built cursor with nothing owed. + /// A snapshot with no host row is not proof the host holds nothing: a + /// same-id registration can have created the row, advanced it and been + /// removed between the snapshot and this publication. The entry those + /// writes left wins, and the lower built cursor is owed against it. #[tokio::test] - async fn a_leftover_cursor_entry_without_a_host_row_is_discarded() { + async fn a_leftover_cursor_entry_wins_without_a_host_row_and_the_lower_built_cursor_is_owed() { let (donor, _, wallet_id) = make_wallet().await; drop(donor); let persister = Arc::new(RecordingPersister::default()); // `load` returns no rows @@ -4554,13 +4627,15 @@ mod tests { Arc::clone(&persister), handler, )); - manager.durable_cursors.lock().await.insert( - wallet_id, - crate::changeset::DurableCursor { - height: 1_000, - advance_epoch: Some(2), - }, - ); + let entry = crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }; + manager + .durable_cursors + .lock() + .await + .insert(wallet_id, entry); let seed = Mnemonic::from_phrase(TEST_MNEMONIC) .expect("valid mnemonic") .to_seed(""); @@ -4581,8 +4656,8 @@ mod tests { .await .get(&wallet_id) .copied(), - Some(crate::changeset::DurableCursor::at(100)), - "seeded from the built cursor, the stale entry gone" + Some(entry), + "the accepted entry is what the host holds" ); let wm = manager.wallet_manager.read().await; assert_eq!( @@ -4590,10 +4665,67 @@ mod tests { .expect("info") .dashpay_backfill .unpersisted_cursor, - None + Some(100), + "the lower built cursor is owed" ); } + /// The load path has the same window: the start state is read before + /// the lock, and a predecessor still registered then can have a commit + /// accepted since. The entry wins and the lower loaded cursor is owed, + /// so a contact forward-covered at the loaded cursor is never recorded + /// beside the host's higher one without a reset. + #[tokio::test] + async fn loading_a_wallet_owes_a_loaded_cursor_below_the_one_a_predecessor_left() { + let (donor, _, wallet_id) = make_wallet().await; + let (wallet, mut managed) = { + let wm = donor.wallet_manager.read().await; + ( + wm.get_wallet(&wallet_id).expect("wallet").clone(), + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .clone(), + ) + }; + drop(donor); + managed.metadata.synced_height = 100; + let host = Arc::new(ReloadPersister { + wallet, + managed, + identities: std::collections::BTreeMap::new(), + backfill: Default::default(), + stores: Mutex::default(), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + manager.durable_cursors.lock().await.insert( + wallet_id, + crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }, + ); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .map(|cursor| cursor.height), + Some(1_000) + ); + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.metadata.synced_height, 100); + assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); + } + /// Coverage for a receival account that no longer exists stops being /// true the moment its scripts stop being watched. A pass with no /// contact to handle must still drop it and store the record, not From 8ab6c9c079717d0156a8827a3d22d47b2e51dc15 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Fri, 18 Sep 2026 15:38:00 -0700 Subject: [PATCH 18/43] fix(platform-wallet): run the rescan sweep once more after the startup drain Startup runs the DashPay sync before draining the deferred contact-account queue, so the sweep inside that sync sees none of the receival accounts the drain is about to create. Every contact therefore stayed unmarked until the first sweep after SPV had already advanced, and that sweep then rewound the scan mid-session to backfill them. Run the sweep once more after a non-empty drain. At that point `synced_height` is still 0 (this sequence runs before SPV starts), so the sweep marks the contacts as covered by the coming full historical scan rather than scheduling a rewind. Not budgeted: a pass over in-memory state with no I/O. Builds on 72b34c0e7b in this PR, which makes the sweep mark candidates at `synced_height == 0` instead of returning early; this commit is what gets the sweep to run at that moment in the first place. The durable backfill record stops a rewind from repeating on later launches; this commit stops the first one. Device check (Android emulator, fresh restore of a testnet wallet with 11 DashPay contacts, on an integration build that carries this PR and also handles one-way contacts): without this commit the sweep rewound right after the first SYNCED (floor 1226329, rewound_from 1567499, 341,170 blocks re-scanned in about 92 s); with it, no rewind. A relaunch rewound in neither case. Co-Authored-By: Claude Opus 5 Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/startup.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/rs-platform-wallet/src/manager/startup.rs b/packages/rs-platform-wallet/src/manager/startup.rs index 27a1e76e5f3..5a8a78b24da 100644 --- a/packages/rs-platform-wallet/src/manager/startup.rs +++ b/packages/rs-platform-wallet/src/manager/startup.rs @@ -748,6 +748,23 @@ impl PlatformWalletManager ); } + // The DashPay sync above ran its rescan sweep BEFORE the drain created + // the receival accounts, so it saw none of them. Run it once more now: + // with `synced_height == 0` (this sequence runs before SPV starts) the + // sweep marks every registered receival contact as covered by the + // coming full scan, instead of leaving it for the first post-scan + // sweep to rewind for. Not budgeted: a pass over in-memory state, no + // I/O. + if drained > 0 { + if let Err(e) = identity_wallet.dashpay().reconcile_dashpay_rescan().await { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "startup: post-drain DashPay rescan sweep failed" + ); + } + } + Ok(tally.into_outcome(started.elapsed())) } From 64a383ad7303319f32894e5074ab58cb3eb296d9 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Fri, 18 Sep 2026 15:44:27 -0700 Subject: [PATCH 19/43] test(platform-wallet): pin the post-drain rescan sweep at startup Covers the hook added in the previous commit: after an owning-seed drain creates the receival account, the contact must be marked covered while `synced_height` is still 0, and a later sweep once the scan has advanced past its funding height must not rewind. The contact is established (our request and its reply, both at height 100), because the sweep handles established contacts from their request pair's height. Verified to fail without the hook (the mark is absent). Co-Authored-By: Claude Opus 5 Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/startup.rs | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/packages/rs-platform-wallet/src/manager/startup.rs b/packages/rs-platform-wallet/src/manager/startup.rs index 5a8a78b24da..9e6a99a8831 100644 --- a/packages/rs-platform-wallet/src/manager/startup.rs +++ b/packages/rs-platform-wallet/src/manager/startup.rs @@ -1560,6 +1560,120 @@ mod tests { ); } + /// The drain creates the receival accounts AFTER the DashPay sync has + /// already run its rescan sweep, so that sweep saw none of them. Startup + /// therefore runs the sweep once more once the drain has produced + /// something. This pins the consequence: the freshly drained contact is + /// marked covered while `synced_height` is still 0, so when the full + /// historical scan later advances past its funding height the recurring + /// sweep does not rewind mid-session to backfill a contact the scan had + /// already covered. + #[tokio::test] + async fn the_post_drain_sweep_marks_the_drained_contact_covered() { + use crate::wallet::identity::network::SeedCryptoProvider; + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let owner = Identifier::from([1u8; 32]); + let contact = Identifier::from([2u8; 32]); + let (manager, wallet_id) = manager_with_queued_contact_crypto().await; + // The sweep handles an ESTABLISHED contact, from its request pair's + // height. Give the queued contact both requests, ours and its reply + // (funded at 100), so the pair auto-establishes the relationship. + { + use crate::wallet::identity::ContactRequest; + use crate::wallet::persister::{NoPlatformPersistence, WalletPersister}; + + let persister = + WalletPersister::new(wallet_id, std::sync::Arc::new(NoPlatformPersistence)); + let mut wm = manager.wallet_manager.write().await; + let managed = wm + .get_wallet_info_mut(&wallet_id) + .expect("wallet info") + .identity_manager + .managed_identity_mut(&owner) + .expect("managed identity"); + managed + .add_sent_contact_request( + ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0), + &persister, + ) + .expect("sent request persists"); + managed + .add_incoming_contact_request( + ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 100, 0), + &persister, + ) + .expect("incoming request persists"); + assert!( + managed + .dashpay() + .established_contacts() + .contains_key(&contact), + "the request pair establishes the contact the sweep handles" + ); + } + let owning = + SeedCryptoProvider::from_seed(seed_for(OWNING_MNEMONIC), key_wallet::Network::Testnet); + + let outcome = manager + .start_wallet_subsystems( + &wallet_id, + None, + Some(&owning), + None::<&UnusedSigner>, + WalletStartupOptions::default(), + ) + .await + .expect("bring-up reports rather than raises"); + assert_eq!(outcome.contact_accounts_drained, 1); + + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("wallet info"); + assert_eq!( + info.core_wallet.synced_height(), + 0, + "bring-up runs before SPV starts; the sweep must not have rewound anything" + ); + assert!( + info.identity_manager + .managed_identity(&owner) + .expect("managed identity") + .dashpay() + .rescan_triggered + .contains(&contact), + "the post-drain sweep must mark the drained contact covered by the coming scan" + ); + } + + // Advance the scan past the contact's funding height. The mark must + // hold: no rewind, because the full scan already covered it. + { + let mut wm = manager.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("wallet info"); + info.core_wallet.update_synced_height(200); + } + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("sweep"), + None, + "a contact covered by the full historical scan must never trigger a rewind" + ); + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("wallet info") + .core_wallet + .synced_height(), + 200 + ); + } + /// The gate is paid for only when there is something to protect. An empty /// queue means the drain would derive nothing, so no key material is /// resolved — which is what keeps this affordable on a warm launch. From 28fa6bf033c8dacdda66306cfb630f6f40d57797 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 7 Oct 2026 13:12:19 -0700 Subject: [PATCH 20/43] fix(platform-wallet): replace the host's backfill record when a same-id wallet was published after the snapshot Registration and load both read the host's start state before taking the durable-cursor lock. A same-id wallet can be published after that read, register a receival account, store coverage for it, advance or rewind the cursor, and be removed before the new wallet publishes. Neither path saw that coverage: registration only stored a filtered record when its own snapshot had something to prune, and load stored nothing. The new wallet then scanned past the account unwatched, and a crash restored the account beside coverage claiming the range was watched. Every publication leaves a DurableCursors entry and removal keeps it, so an entry under the id at publication means such a predecessor may have written since the snapshot. With one present, both paths now store the record the new wallet carries over the host's before publishing it. The duplicate check moves ahead of that store, so a wallet still registered under the id never has its record overwritten. Tests cover both interleavings through a crash reload: the stored record re-arms the account, the predecessor's coverage would have skipped it. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/load.rs | 54 ++++- .../src/manager/wallet_lifecycle.rs | 30 ++- .../src/wallet/identity/network/payments.rs | 217 +++++++++++++++--- 3 files changed, 263 insertions(+), 38 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index ed6926c5f5a..d8645ea3995 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -3,7 +3,9 @@ use std::collections::BTreeMap; use std::sync::Arc; -use crate::changeset::{ClientStartState, ClientWalletStartState, PlatformWalletPersistence}; +use crate::changeset::{ + ClientStartState, ClientWalletStartState, PlatformWalletChangeSet, PlatformWalletPersistence, +}; use crate::error::PlatformWalletError; use crate::wallet::core::WalletGeneration; use crate::wallet::identity::IdentityManager; @@ -286,12 +288,58 @@ impl PlatformWalletManager

{ // reset, in this wallet's inherited epoch, so no coverage is // recorded beside the host's higher cursor without a reset // (dashpay/platform#4302 review). + // + // The same predecessor can also have changed the host's DashPay + // backfill record after the start state was read: registered a + // receival account this snapshot does not hold, stored coverage + // for it, and been removed. Published without that account, this + // wallet would scan past ranges the coverage claims were watched + // for it, and a crash would restore both. So when an entry shows + // a same-id wallet was published in this process, the record this + // wallet carries replaces the host's before the wallet is + // published; a contact that loses coverage that way is rewound + // for again, once. The existence check comes first, so a wallet + // still registered under this id is never overwritten, and every + // publisher holds the cursor lock, so none appears before the + // insert. { let mut durable_cursors = self.durable_cursors.lock().await; - let mut wm = self.wallet_manager.write().await; - if wm.get_wallet(&wallet_id).is_some() { + if self + .wallet_manager + .read() + .await + .get_wallet(&wallet_id) + .is_some() + { continue 'load; } + if durable_cursors.contains_key(&wallet_id) { + tracing::info!( + wallet_id = %hex::encode(wallet_id), + covered = platform_info.dashpay_backfill.covered.len(), + "wallet reloaded under an id published earlier in this process: \ + the host's DashPay backfill record is replaced with the loaded one \ + before the wallet is published" + ); + if let Err(e) = self.persister.store( + wallet_id, + PlatformWalletChangeSet { + dashpay_backfill: Some(platform_info.dashpay_backfill.clone()), + ..Default::default() + }, + ) { + tracing::error!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "failed to replace the host's DashPay backfill record; \ + load aborted before the wallet was registered" + ); + load_error = + Some(PlatformWalletError::from_store_failure(&*self.persister, e)); + break 'load; + } + } + let mut wm = self.wallet_manager.write().await; if let Err(e) = wm.insert_wallet(wallet, platform_info) { load_error = Some(PlatformWalletError::WalletCreation(format!( "Failed to register persisted wallet in WalletManager: {}", diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index 2b62a093e19..f65faf64ca0 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -500,6 +500,19 @@ impl PlatformWalletManager

{ // record is stored BEFORE the wallet is published to the scanner. // Those accounts re-arm when they are registered, at the cost of one // more backfill. + // + // Whether the snapshot held anything to prune is not the whole test, + // because the snapshot is older than the lock: a same-id registration + // can have been published after it, established a receival account, + // stored that account's coverage and been removed again, all before + // this one publishes. Nothing in this snapshot shows that coverage. + // An entry in `DurableCursors` is what tells: every publication leaves + // one and removal keeps it. So with an entry present the host's + // record is replaced with the one this wallet carries, whatever the + // snapshot showed. The duplicate check runs first, so a wallet still + // registered under this id never has its record overwritten; every + // publisher holds the cursor lock, so none can appear between that + // check and the insert. let created_cursor = platform_info.core_wallet.metadata.synced_height; let (snapshot_cursor, mut host_backfill) = match host_wallets.remove(®istration_wallet_id) { @@ -526,12 +539,23 @@ impl PlatformWalletManager

{ } let wallet_id = { let mut durable_cursors = self.durable_cursors.lock().await; - if dropped { + if self + .wallet_manager + .read() + .await + .get_wallet_info(®istration_wallet_id) + .is_some() + { + return Err(already_registered(registration_wallet_id)); + } + let predecessor = durable_cursors.contains_key(®istration_wallet_id); + if dropped || predecessor { tracing::info!( wallet_id = %hex::encode(registration_wallet_id), kept = host_backfill.covered.len(), - "wallet recreated: DashPay backfill coverage for receival accounts it \ - does not hold is re-armed on the host before the wallet is published" + predecessor, + "wallet recreated: the host's DashPay backfill record is replaced with \ + the one this wallet carries before the wallet is published" ); self.persister .store( diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 7a8ce007ad0..22cd86bef1d 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -4444,8 +4444,6 @@ mod tests { /// host's record said it was covered (dashpay/platform#4302 review). #[tokio::test] async fn a_recreated_wallet_rearms_coverage_for_receival_accounts_it_does_not_hold() { - use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; - let owner = Identifier::from([0xAA; 32]); let contact = Identifier::from([0xBB; 32]); let mut host_record = crate::changeset::DashPayBackfillRecord::default(); @@ -4468,35 +4466,11 @@ mod tests { // registration did: the host would otherwise restore the old // coverage (and A's account rows) beside this wallet's advanced // cursor after a crash. - let invalidated = { - let stores = host.stores.lock().unwrap(); - let (_, first) = stores.first().expect("the registration stored something"); - assert!(first.core.is_none(), "a record-only round"); - let record = first - .dashpay_backfill - .clone() - .expect("the first store of the registration is the re-armed record"); - assert!(record.is_empty()); - record - }; + let invalidated = first_store_is_an_empty_record(&host.stores.lock().unwrap()); // The successor scans 101..1500 before the account exists, and the // host accepts that advance (which also pays the owed 100). - { - let mut durable = manager.durable_cursors.lock().await; - let mut wm = manager.wallet_manager.write().await; - wm.get_wallet_info_mut(&wallet_id) - .expect("info") - .core_wallet - .update_synced_height(1_500); - durable.insert( - wallet_id, - crate::changeset::DurableCursor { - height: 1_500, - advance_epoch: Some(0), - }, - ); - } + accept_successor_advance(&manager, wallet_id, 1_500).await; host.stores.lock().unwrap().clear(); establish_on_recreated(&manager, &host, &recreated, owner, contact, 200).await; assert_eq!( @@ -4522,11 +4496,36 @@ mod tests { // the invalidated record the reload rewinds for A; with the old // record it would have trusted coverage over a range scanned without // A — which is what storing the invalidation first prevents. + assert_only_the_replaced_record_rearms_after_a_crash( + &manager, + wallet_id, + owner, + 1_500, + invalidated, + host_record, + ) + .await; + } + + /// Restore `wallet_id` the way a crash would — its accounts, `owner`'s + /// established contact and the cursor at `persisted_cursor` — once with + /// the record the publication stored over the host's (`replaced`) and + /// once with the coverage it replaced (`stale`). Only the stored record + /// may rewind for the contact; the stale one would have skipped the + /// range scanned without its account. + async fn assert_only_the_replaced_record_rearms_after_a_crash( + manager: &Arc>, + wallet_id: WalletId, + owner: Identifier, + persisted_cursor: u32, + replaced: crate::changeset::DashPayBackfillRecord, + stale: crate::changeset::DashPayBackfillRecord, + ) { let (wallet, managed, identity) = { let wm = manager.wallet_manager.read().await; let info = wm.get_wallet_info(&wallet_id).expect("info"); let mut managed = info.core_wallet.clone(); - managed.metadata.synced_height = 1_500; + managed.metadata.synced_height = persisted_cursor; let mut identity = info .identity_manager .managed_identity(&owner) @@ -4540,11 +4539,11 @@ mod tests { ) }; for (record, expected, why) in [ - (invalidated, Some(200), "the invalidated record re-arms A"), + (replaced, Some(200), "the stored record re-arms the account"), ( - host_record, + stale, None, - "the old record would have skipped 1001..1500", + "the replaced coverage would have skipped the unwatched range", ), ] { let mut identities = std::collections::BTreeMap::new(); @@ -4573,6 +4572,160 @@ mod tests { } } + /// The successor scans to `height` and the host accepts that advance in + /// the successor's own epoch, which also pays any cursor it owed. + async fn accept_successor_advance( + manager: &Arc>, + wallet_id: WalletId, + height: u32, + ) { + use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + + let mut durable = manager.durable_cursors.lock().await; + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(height); + durable.insert( + wallet_id, + crate::changeset::DurableCursor { + height, + advance_epoch: Some(0), + }, + ); + } + + /// The first thing `stores` received, which must be a record-only round + /// replacing the host's backfill record with an empty one. + fn first_store_is_an_empty_record( + stores: &[(WalletId, PlatformWalletChangeSet)], + ) -> crate::changeset::DashPayBackfillRecord { + let (_, first) = stores.first().expect("the publication stored something"); + assert!(first.core.is_none(), "a record-only round"); + let record = first + .dashpay_backfill + .clone() + .expect("the host's record is replaced before the wallet is published"); + assert!(record.is_empty()); + assert!(!record.has_extent()); + record + } + + /// The snapshot a registration reads is older than its lock. A same-id + /// registration can be published after it, establish a receival account, + /// store that account's coverage, advance and be removed, all before this + /// one publishes — so a snapshot with nothing to prune is no proof the + /// host holds nothing to re-arm. The entry that predecessor left makes + /// the publication replace the host's record anyway; otherwise this + /// wallet scans past the account unwatched and a crash restores the + /// account beside coverage that claims the range was watched + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_recreated_wallet_replaces_coverage_a_predecessor_stored_after_the_snapshot() { + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + // What the predecessor stored after this registration's snapshot. + let mut predecessor_record = crate::changeset::DashPayBackfillRecord::default(); + predecessor_record.record_pass(900, Some(200), [(owner, contact, 0, 200)]); + + let (manager, host, recreated, wallet_id) = recreate_on_host_at( + Default::default(), + 1_000, + Some(crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }), + ) + .await; + let replaced = first_store_is_an_empty_record(&host.stores.lock().unwrap()); + + accept_successor_advance(&manager, wallet_id, 1_500).await; + establish_on_recreated(&manager, &host, &recreated, owner, contact, 200).await; + assert_only_the_replaced_record_rearms_after_a_crash( + &manager, + wallet_id, + owner, + 1_500, + replaced, + predecessor_record, + ) + .await; + } + + /// The load path's start state is just as old. A predecessor can register + /// a receival account the snapshot does not hold, store its coverage + /// beside a rewind to 100 and be removed before the load publishes. The + /// loaded cursor (1000) is then above the host's, so no reset is owed, + /// and the snapshot holds no coverage to prune. The entry the predecessor + /// left makes the load replace the host's record with the one it loaded + /// before the wallet is published (dashpay/platform#4302 review). + #[tokio::test] + async fn loading_a_wallet_replaces_coverage_a_predecessor_stored_after_the_snapshot() { + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let mut predecessor_record = crate::changeset::DashPayBackfillRecord::default(); + predecessor_record.record_pass(900, Some(200), [(owner, contact, 0, 200)]); + + let (donor, _, wallet_id) = make_wallet().await; + let (wallet, mut managed) = { + let wm = donor.wallet_manager.read().await; + ( + wm.get_wallet(&wallet_id).expect("wallet").clone(), + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .clone(), + ) + }; + drop(donor); + managed.metadata.synced_height = 1_000; + let host = Arc::new(ReloadPersister { + wallet, + managed, + identities: std::collections::BTreeMap::new(), + backfill: Default::default(), + stores: Mutex::default(), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + manager.durable_cursors.lock().await.insert( + wallet_id, + crate::changeset::DurableCursor { + height: 100, + advance_epoch: Some(2), + }, + ); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + let replaced = first_store_is_an_empty_record(&host.stores.lock().unwrap()); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.metadata.synced_height, 1_000); + assert_eq!( + info.dashpay_backfill.unpersisted_cursor, None, + "the loaded cursor is above the host's, so no reset is owed" + ); + } + + accept_successor_advance(&manager, wallet_id, 1_500).await; + let loaded = manager.get_wallet(&wallet_id).await.expect("wallet"); + establish_on_recreated(&manager, &host, &loaded, owner, contact, 200).await; + assert_only_the_replaced_record_rearms_after_a_crash( + &manager, + wallet_id, + owner, + 1_500, + replaced, + predecessor_record, + ) + .await; + } + /// The host snapshot a registration reads comes before it takes the /// durable-cursor lock, and a same-id predecessor still registered then /// can have a commit accepted in between. Under the lock the entry that From dca544eb1df21a2ddc420551c59de04b7cde9e2f Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 7 Oct 2026 17:38:03 -0700 Subject: [PATCH 21/43] fix(platform-wallet): canonicalize the backfill cover set on every restore path, serde included The derived serde Deserialize took the covered entries as stored, without the sorted, one-per-account order covered_from's binary search relies on. An unordered set or a conflicting duplicate could then resolve to a lower covered_from and suppress a rewind the FFI decoders, which keep the highest duplicate, would have required. DashPayBackfillRecord::from_entries is now the one canonical constructor: it sorts by account key, keeps a duplicate's highest covered_from, and clears the in-memory fields. from_parts, the FFI restore decoder and serde deserialization (through serde(from)) all go through it. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/persistence.rs | 38 +++---- .../src/changeset/dashpay_backfill.rs | 105 +++++++++++++++--- 2 files changed, 106 insertions(+), 37 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index ce873f45302..e58e548a08f 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -5355,28 +5355,22 @@ fn decode_dashpay_backfill( ) } }; - let mut covered: Vec = covered - .iter() - .map(|c| DashPayBackfillCoveredContact { - owner: Identifier::from(c.owner_identity_id), - contact: Identifier::from(c.contact_identity_id), - account_index: c.account_index, - covered_from: c.covered_from, - }) - .collect(); - // Canonical order and one entry per account, whatever the host stored. - // A duplicated account keeps its HIGHEST `covered_from`, so an ambiguous - // record fails closed. - covered.sort_by_key(|entry| (entry.key(), std::cmp::Reverse(entry.covered_from))); - covered.dedup_by(|a, b| a.key() == b.key()); - DashPayBackfillRecord { - floor: entry.dashpay_backfill_floor, - rewound_from: entry.dashpay_backfill_rewound_from, - covered, - unpersisted_cursor: None, - unpersisted_cursor_epoch: None, - unpersisted_extent: None, - } + // `from_entries` puts the cover set in canonical form whatever the host + // stored: one entry per account, a duplicate keeping its HIGHEST + // `covered_from`, so an ambiguous record fails closed. + DashPayBackfillRecord::from_entries( + entry.dashpay_backfill_floor, + entry.dashpay_backfill_rewound_from, + covered + .iter() + .map(|c| DashPayBackfillCoveredContact { + owner: Identifier::from(c.owner_identity_id), + contact: Identifier::from(c.contact_identity_id), + account_index: c.account_index, + covered_from: c.covered_from, + }) + .collect(), + ) } fn build_wallet_start_state( diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index e63f649e3ef..45fc2294a5e 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -99,8 +99,14 @@ impl DashPayBackfillCoveredContact { /// A later rewind folds into the existing record ([`record_pass`](Self::record_pass)): /// the floor can only go down, `rewound_from` can only go up, and a contact's /// entry is replaced by the new checkpoint it was covered from. +/// +/// Every way in — [`from_entries`](Self::from_entries), [`from_parts`](Self::from_parts) +/// and serde deserialization — canonicalizes [`covered`](Self::covered), so a +/// restored record never carries an order or a duplicate the lookups would +/// misread. #[derive(Debug, Clone, PartialEq, Eq, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(from = "DashPayBackfillRecordSerde"))] pub struct DashPayBackfillRecord { /// Lowest height a backfill rewound the cursor to. Meaningless while /// [`has_extent`](Self::has_extent) is false. @@ -135,7 +141,49 @@ pub struct DashPayBackfillRecord { pub unpersisted_extent: Option<(u32, u32)>, } +/// The persisted fields of a [`DashPayBackfillRecord`] as serde reads them, +/// before [`DashPayBackfillRecord::from_entries`] canonicalizes the cover set. +#[cfg(feature = "serde")] +#[derive(serde::Deserialize)] +struct DashPayBackfillRecordSerde { + floor: u32, + rewound_from: u32, + covered: Vec, +} + +#[cfg(feature = "serde")] +impl From for DashPayBackfillRecord { + fn from(record: DashPayBackfillRecordSerde) -> Self { + Self::from_entries(record.floor, record.rewound_from, record.covered) + } +} + impl DashPayBackfillRecord { + /// A restored record from its persisted parts, with the cover set put in + /// canonical form whatever order or duplicates the source held: sorted by + /// receival-account key, one entry per account. A duplicated account + /// keeps its HIGHEST `covered_from` — an ambiguous record must fail + /// closed, and the higher height is the one that re-arms more + /// checkpoints. Every decoder of a stored record goes through here, so + /// [`covered_from`](Self::covered_from)'s binary search never lands on a + /// lower duplicate. + pub fn from_entries( + floor: u32, + rewound_from: u32, + mut covered: Vec, + ) -> Self { + covered.sort_by_key(|entry| (entry.key(), std::cmp::Reverse(entry.covered_from))); + covered.dedup_by(|a, b| a.key() == b.key()); + Self { + floor, + rewound_from, + covered, + unpersisted_cursor: None, + unpersisted_cursor_epoch: None, + unpersisted_extent: None, + } + } + /// Owe the host `cursor`, lowered in memory during rewind epoch `epoch`. /// Folds into any debt already owed: the lower cursor, and the later /// epoch — the debt is paid only by an advance the host accepts after the @@ -330,7 +378,7 @@ impl DashPayBackfillRecord { return None; } let (chunks, _) = covered_bytes.as_chunks::<{ Self::COVERED_ENTRY_LEN }>(); - let mut covered: Vec = chunks + let covered: Vec = chunks .iter() .map(|chunk| { let mut owner = [0u8; 32]; @@ -349,20 +397,7 @@ impl DashPayBackfillRecord { } }) .collect(); - // Canonical order and one entry per account, whatever the host stored. - // A duplicated account keeps its HIGHEST `covered_from`: an ambiguous - // record must fail closed, and the higher height is the one that - // re-arms more checkpoints. - covered.sort_by_key(|entry| (entry.key(), std::cmp::Reverse(entry.covered_from))); - covered.dedup_by(|a, b| a.key() == b.key()); - Some(Self { - floor, - rewound_from, - covered, - unpersisted_cursor: None, - unpersisted_cursor_epoch: None, - unpersisted_extent: None, - }) + Some(Self::from_entries(floor, rewound_from, covered)) } } @@ -503,6 +538,46 @@ mod tests { assert!(!record.covers(&id(5), &id(6), 0, 800)); } + /// Serde is a restore path too: a record serialized with its cover set + /// out of order and holding a conflicting duplicate must come back in + /// canonical form, the duplicate at its highest height — not left for + /// the binary search to land on the lower one and suppress a rewind. + #[cfg(feature = "serde")] + #[test] + fn a_serde_restored_record_is_canonicalised_on_the_way_in() { + let entry = |owner: u8, contact: u8, covered_from: u32| DashPayBackfillCoveredContact { + owner: id(owner), + contact: id(contact), + account_index: 0, + covered_from, + }; + let stored = DashPayBackfillRecord { + floor: 100, + rewound_from: 1_000, + covered: vec![entry(5, 6, 700), entry(1, 2, 100), entry(5, 6, 900)], + ..Default::default() + }; + let json = serde_json::to_string(&stored).expect("serializes"); + let restored: DashPayBackfillRecord = serde_json::from_str(&json).expect("deserializes"); + assert_eq!( + restored, + DashPayBackfillRecord::from_entries( + 100, + 1_000, + vec![entry(1, 2, 100), entry(5, 6, 900)] + ) + ); + assert_eq!( + restored.covered, + vec![entry(1, 2, 100), entry(5, 6, 900)], + "sorted by account, one entry each" + ); + assert!( + !restored.covers(&id(5), &id(6), 0, 800), + "the duplicate keeps its highest height, re-arming a checkpoint below it" + ); + } + #[test] fn retain_forgets_contacts_and_reports_whether_anything_went() { let mut record = DashPayBackfillRecord::default(); From 46349d0e2336c6834be9837afbf97e189b9dc163 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 7 Oct 2026 17:38:04 -0700 Subject: [PATCH 22/43] fix(unified-sdk-jni): wire the DashPay backfill slot only for a bridge that stores the cursor on_persist_wallet_changeset_fn is always wired, so the FFI persister's cursor-delivered gate cannot tell a cursor the host stored from one handed to NativePersistenceBridge's inherited no-op onWalletChangesetHeader. A custom bridge persisting the backfill record but not the cursor could commit coverage for a rewind while keeping the old cursor, and that coverage would suppress the recovery scan after a restart. The backfill extension slot is now wired only when the concrete bridge overrides onWalletChangesetHeader, using the same override probe as the sweep slot. The in-tree Room handler overrides it and is unaffected. The slot choice moves into a pure function so a test can pin it without a JVM. Co-Authored-By: Claude Opus 5.5 --- .../dashsdk/ffi/NativePersistenceBridge.kt | 4 ++ .../rs-unified-sdk-jni/src/persistence.rs | 67 ++++++++++++++++++- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt index 0e38b6aaa46..f662cfae2fe 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/ffi/NativePersistenceBridge.kt @@ -458,6 +458,10 @@ abstract class NativePersistenceBridge { * Purely additive: an implementation that ignores it keeps today's * behaviour — the backfill re-fires on every launch, re-walking every * filter from the earliest contact's core height. Slow, never lossy. + * + * Delivered only to a bridge that also overrides [onWalletChangesetHeader]: + * the record vouches for the cursor that callback stores, so a bridge + * keeping the inherited no-op header never receives it. */ open fun onWalletChangesetDashPayBackfill( walletId: ByteArray, diff --git a/packages/rs-unified-sdk-jni/src/persistence.rs b/packages/rs-unified-sdk-jni/src/persistence.rs index 181cd53793f..7c4a2ac9ab3 100644 --- a/packages/rs-unified-sdk-jni/src/persistence.rs +++ b/packages/rs-unified-sdk-jni/src/persistence.rs @@ -206,11 +206,39 @@ pub(crate) fn build_vtable(context: *mut c_void) -> PersistenceCallbacks { /// sync watermark with it, and the round is refused one layer up instead of /// advancing past a removal that never happened. Wiring the slot for every /// subclass would make "slot present" prove nothing. +/// +/// The DashPay backfill slot is likewise wired only when the bridge +/// OVERRIDES `onWalletChangesetHeader`, the callback that stores the cursor. +/// `on_persist_wallet_changeset_fn` is always wired, so Rust's "cursor +/// delivered" gate cannot tell a stored cursor from one handed to the +/// inherited no-op header. A bridge that persists the backfill record but +/// not the cursor would then commit coverage for a rewind while keeping the +/// old cursor, and that coverage would suppress the recovery scan after a +/// restart (dashpay/platform#4302 review). Without the slot the record is +/// never delivered and every launch re-runs the backfill: slow, never lossy. pub(crate) fn build_extension(env: &mut JNIEnv, bridge: &JObject) -> PersistenceCallbacksExtension { - let sweeps_overridden = bridge_overrides(env, bridge, "onWalletChangesetTransactionsSwept"); + extension_for(BridgeOverrides { + sweeps: bridge_overrides(env, bridge, "onWalletChangesetTransactionsSwept"), + header: bridge_overrides(env, bridge, "onWalletChangesetHeader"), + }) +} + +/// Which optional `NativePersistenceBridge` callbacks the concrete bridge +/// supplies its own body for (see [`bridge_overrides`]). +#[derive(Debug, Clone, Copy)] +struct BridgeOverrides { + /// `onWalletChangesetTransactionsSwept`. + sweeps: bool, + /// `onWalletChangesetHeader`, which carries the scan cursor. + header: bool, +} + +/// The extension slots wired for a bridge with `overrides` (see +/// [`build_extension`]). +fn extension_for(overrides: BridgeOverrides) -> PersistenceCallbacksExtension { PersistenceCallbacksExtension { on_persist_dpns_name_states_fn: Some(tramp_persist_dpns_name_states), - on_persist_wallet_changeset_sweeps_fn: if sweeps_overridden { + on_persist_wallet_changeset_sweeps_fn: if overrides.sweeps { Some(tramp_persist_wallet_changeset_sweeps) } else { None @@ -218,7 +246,11 @@ pub(crate) fn build_extension(env: &mut JNIEnv, bridge: &JObject) -> Persistence on_persist_wallet_changeset_chain_lock_height_fn: Some( tramp_persist_wallet_changeset_chain_lock_height, ), - on_persist_wallet_dashpay_backfill_fn: Some(tramp_persist_wallet_dashpay_backfill), + on_persist_wallet_dashpay_backfill_fn: if overrides.header { + Some(tramp_persist_wallet_dashpay_backfill) + } else { + None + }, ..Default::default() } } @@ -4893,6 +4925,35 @@ mod tests { /// A height past `i32::MAX` must refuse the call, never wrap into a /// negative `Int` the handler would read as absent or as a bogus /// collection boundary. + /// A bridge that inherits the no-op `onWalletChangesetHeader` stores no + /// cursor, so it must not be handed the DashPay backfill record either: + /// coverage committed beside a cursor that was never lowered would + /// suppress the recovery scan after a restart. Overriding the header is + /// what wires the slot; the sweep slot keeps its own probe. + #[test] + fn the_backfill_slot_is_wired_only_for_a_bridge_that_stores_the_cursor() { + let inherited_header = extension_for(BridgeOverrides { + sweeps: true, + header: false, + }); + assert!( + inherited_header + .on_persist_wallet_dashpay_backfill_fn + .is_none(), + "an inherited header means the record is never delivered" + ); + assert!(inherited_header + .on_persist_wallet_changeset_sweeps_fn + .is_some()); + + let own_header = extension_for(BridgeOverrides { + sweeps: false, + header: true, + }); + assert!(own_header.on_persist_wallet_dashpay_backfill_fn.is_some()); + assert!(own_header.on_persist_wallet_changeset_sweeps_fn.is_none()); + } + #[test] fn a_height_past_i32_max_is_refused_rather_than_wrapped() { assert_eq!(jint_height(0).unwrap(), 0); From 81e78a7cc25c92aa974af1f78a33f132e68f42e9 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Thu, 8 Oct 2026 00:31:07 -0700 Subject: [PATCH 23/43] fix(platform-wallet): keep backfill coverage off buffering backends and pair replaced coverage with its cursor A successful store() is a commit only where store_commits_inline() says so; elsewhere flush() is the boundary and can still drop the round. The rescan sweep nevertheless installed coverage, cleared its debt and moved DurableCursors as soon as store returned, and the adapter recorded every accepted height as durable. - A buffering backend now gets no backfill record. The sweep rewinds and marks in memory only, which is the pre-record guard, and neither it nor the adapter records a cursor such a backend accepted. Coverage reaches disk only where the store is the commit; the FFI host that persists the record already requires that. - Registration and load share settle_published_backfill. A replacement that puts coverage back on the host while the wallet's own cursor is below the host's now carries that cursor as a rewind in the same round, and the durable entry follows it. Before, a load could restore snapshot coverage beside a predecessor's higher cursor, owing the lower one only in memory, and a crash in between kept both. On a buffering backend the replacement is an empty record, flushed before the wallet is published. Regressions cover a buffered sweep with a failing flush, the adapter's buffered advance, a failed flush on publication, and a covering loaded record below the host's cursor. Each fails against the previous code. Co-Authored-By: Claude Opus 5.5 --- .../src/changeset/core_bridge.rs | 68 ++++- .../rs-platform-wallet/src/manager/load.rs | 62 ++--- .../rs-platform-wallet/src/manager/mod.rs | 100 +++++++- .../src/manager/wallet_lifecycle.rs | 43 +--- .../src/wallet/identity/network/payments.rs | 240 +++++++++++++++++- 5 files changed, 435 insertions(+), 78 deletions(-) diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index 3f9a6c833c1..55dcca601f7 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -1116,8 +1116,14 @@ where ); } })); - // What each host now holds, for the next writer to measure against. - for (wallet_id, height) in &diag.persisted_by_wallet { + // What each host now holds, for the next writer to measure against — + // known only where the store is the commit. A buffering backend commits + // at a later `flush` that can still drop the round, so its accepted + // heights are not durable and are not recorded; it gets no backfill + // record either (see `reconcile_dashpay_rescan`), so nothing measures + // against them. + let durable = persister.store_commits_inline(); + for (wallet_id, height) in diag.persisted_by_wallet.iter().filter(|_| durable) { cursors.insert( *wallet_id, DurableCursor { @@ -4843,6 +4849,8 @@ mod tests { /// for the block to be in effect rather than sleeping and hoping. blocked: Arc, capabilities: crate::changeset::PersistenceCapabilities, + /// When set, `store` only buffers: the backend commits at `flush`. + buffered: AtomicBool, } impl ProbePersister { @@ -4854,6 +4862,7 @@ mod tests { block_until: Mutex::new(None), blocked: Arc::new(AtomicBool::new(false)), capabilities: crate::changeset::PersistenceCapabilities::NONE, + buffered: AtomicBool::new(false), } } /// A probe that additionally attests `capabilities` — used by the @@ -4884,6 +4893,10 @@ mod tests { } impl PlatformWalletPersistence for ProbePersister { + fn store_commits_inline(&self) -> bool { + !self.buffered.load(Ordering::SeqCst) + } + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { self.capabilities } @@ -5452,6 +5465,57 @@ mod tests { assert!(sync_fault.load(Ordering::Relaxed)); } + /// A backend whose `store` only buffers commits at a later `flush`, which + /// can still fail and drop the round. An accepted store there is not a + /// durable cursor, so the adapter records nothing for the next writer to + /// measure against (dashpay/platform#4302 review). + #[test] + fn a_buffering_backends_accepted_advance_is_not_recorded_as_durable() { + let (obs_tx, _obs_rx) = tokio::sync::mpsc::unbounded_channel(); + let persister = ProbePersister::new(obs_tx); + persister.buffered.store(true, Ordering::SeqCst); + let a: WalletId = [1u8; 32]; + let mut batch = BTreeMap::new(); + batch.insert( + a, + super::WalletBatch { + core: CoreChangeSet { + synced_height: Some(1_000), + ..CoreChangeSet::default() + }, + advance_epoch: Some(3), + ..Default::default() + }, + ); + let mut fault = super::AdapterFaultState::default(); + let sync_fault = AtomicBool::new(false); + let freeze_logged = AtomicBool::new(false); + let mut settled = Vec::new(); + let mut cursors: BTreeMap = + BTreeMap::from([(a, super::DurableCursor::at(100))]); + let diag = super::commit_batch_under_writer_lock( + &persister, + batch, + 1, + &[a], + &mut fault, + &sync_fault, + &freeze_logged, + &mut settled, + &mut cursors, + ); + assert_eq!( + diag.persisted_by_wallet.get(&a).copied(), + Some(1_000), + "the store accepted the advance" + ); + assert_eq!( + cursors.get(&a), + Some(&super::DurableCursor::at(100)), + "an accepted store that is not a commit is not a durable cursor" + ); + } + /// A batch that holds a pre-rewind advance and then the rescan's first /// one ends on the rescan's height: the newer epoch replaces the older /// advance rather than max-merging with it, which would keep the higher, diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index d8645ea3995..a455b8a8b84 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -3,9 +3,7 @@ use std::collections::BTreeMap; use std::sync::Arc; -use crate::changeset::{ - ClientStartState, ClientWalletStartState, PlatformWalletChangeSet, PlatformWalletPersistence, -}; +use crate::changeset::{ClientStartState, ClientWalletStartState, PlatformWalletPersistence}; use crate::error::PlatformWalletError; use crate::wallet::core::WalletGeneration; use crate::wallet::identity::IdentityManager; @@ -225,7 +223,7 @@ impl PlatformWalletManager

{ core_balance.immature(), core_balance.locked(), ); - let platform_info = PlatformWalletInfo { + let mut platform_info = PlatformWalletInfo { observed_input_conflicts: Default::default(), core_wallet: wallet_info, generation: Arc::clone(&generation), @@ -290,18 +288,16 @@ impl PlatformWalletManager

{ // (dashpay/platform#4302 review). // // The same predecessor can also have changed the host's DashPay - // backfill record after the start state was read: registered a - // receival account this snapshot does not hold, stored coverage - // for it, and been removed. Published without that account, this - // wallet would scan past ranges the coverage claims were watched - // for it, and a crash would restore both. So when an entry shows - // a same-id wallet was published in this process, the record this - // wallet carries replaces the host's before the wallet is - // published; a contact that loses coverage that way is rewound - // for again, once. The existence check comes first, so a wallet - // still registered under this id is never overwritten, and every - // publisher holds the cursor lock, so none appears before the - // insert. + // backfill record after the start state was read: stored coverage + // for a receival account this snapshot does not hold, or + // invalidated coverage this snapshot still carries. So when an + // entry shows a same-id wallet was published in this process, the + // record this wallet carries replaces the host's before the wallet + // is published, paired with the loaded cursor when that is below + // the host's (`settle_published_backfill`). The existence check + // comes first, so a wallet still registered under this id is never + // overwritten, and every publisher holds the cursor lock, so none + // appears before the insert. { let mut durable_cursors = self.durable_cursors.lock().await; if self @@ -313,29 +309,17 @@ impl PlatformWalletManager

{ { continue 'load; } - if durable_cursors.contains_key(&wallet_id) { - tracing::info!( - wallet_id = %hex::encode(wallet_id), - covered = platform_info.dashpay_backfill.covered.len(), - "wallet reloaded under an id published earlier in this process: \ - the host's DashPay backfill record is replaced with the loaded one \ - before the wallet is published" - ); - if let Err(e) = self.persister.store( - wallet_id, - PlatformWalletChangeSet { - dashpay_backfill: Some(platform_info.dashpay_backfill.clone()), - ..Default::default() - }, - ) { - tracing::error!( - wallet_id = %hex::encode(wallet_id), - error = %e, - "failed to replace the host's DashPay backfill record; \ - load aborted before the wallet was registered" - ); - load_error = - Some(PlatformWalletError::from_store_failure(&*self.persister, e)); + match self.settle_published_backfill( + &mut durable_cursors, + wallet_id, + std::mem::take(&mut platform_info.dashpay_backfill), + false, + loaded_cursor, + loaded_cursor, + ) { + Ok(record) => platform_info.dashpay_backfill = record, + Err(e) => { + load_error = Some(e); break 'load; } } diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index e7d4509e458..d93745b5988 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -14,6 +14,7 @@ mod wallet_lifecycle; pub(crate) use persistence_load::run_blocking_load; +use std::collections::BTreeMap; use std::sync::Arc; use std::time::Duration; @@ -25,8 +26,10 @@ use tokio_util::sync::CancellationToken; use key_wallet_manager::WalletManager; use crate::changeset::{ - spawn_wallet_event_adapter_with_durable_cursors, DurableCursors, PlatformWalletPersistence, + spawn_wallet_event_adapter_with_durable_cursors, CoreChangeSet, DashPayBackfillRecord, + DurableCursor, DurableCursors, PlatformWalletChangeSet, PlatformWalletPersistence, }; +use crate::error::PlatformWalletError; use crate::events::{PlatformEventHandler, PlatformEventManager}; use crate::manager::dashpay_sync::DashPaySyncManager; use crate::manager::dpns_sync::DpnsSyncManager; @@ -645,6 +648,101 @@ impl PlatformWalletManager

{ Ok(()) } + /// Settle the host's DashPay backfill record for a wallet about to be + /// published under `wallet_id`, holding the durable-cursor lock + /// (`durable_cursors`), and return the record the wallet carries in + /// memory. + /// + /// `record` is what the wallet would carry — its snapshot's record, + /// already filtered to the receival accounts it holds — and `own_cursor` + /// the scan cursor it starts from. `snapshot_cursor` is the host cursor + /// the pre-lock snapshot saw; an entry in `durable_cursors` wins over it. + /// `dropped` says the filter removed coverage from the snapshot's record. + /// + /// The host's record is replaced, before the wallet is visible to the + /// scanner, when it may disagree with what the wallet carries: + /// - the filter dropped coverage for accounts this wallet does not hold; + /// - an entry shows a same-id wallet was published in this process — it + /// may have stored or invalidated coverage after the snapshot was read; + /// - the backend does not commit inline and the snapshot held a record. + /// + /// A replacement that puts coverage on the host while this wallet's cursor + /// is below the host's carries that cursor, as a rewind, in the same + /// round. Otherwise a crash right after it would restore the coverage + /// beside a cursor the wallet never scanned up to with those accounts + /// watched. The entry follows the cursor only once the round is + /// committed, so a lower cursor owed later is measured against it. + /// + /// A backend whose `store` only buffers (`store_commits_inline()` false) + /// gets no record at all, the same as the rescan sweep gives it: only an + /// empty one is written, and flushed before the wallet is published, so a + /// record an earlier run left cannot outlive this one. + #[allow(clippy::too_many_arguments)] + pub(super) fn settle_published_backfill( + &self, + durable_cursors: &mut BTreeMap, + wallet_id: WalletId, + record: DashPayBackfillRecord, + dropped: bool, + own_cursor: u32, + snapshot_cursor: u32, + ) -> Result { + let inline = self.persister.store_commits_inline(); + let predecessor = durable_cursors.contains_key(&wallet_id); + let held_on_host = !record.is_empty() || dropped; + let record = if inline { record } else { DashPayBackfillRecord::default() }; + if !(dropped || predecessor || (!inline && held_on_host)) { + return Ok(record); + } + let host_cursor = durable_cursors + .get(&wallet_id) + .map_or(snapshot_cursor, |entry| entry.height); + let cursor = (!record.is_empty() && own_cursor < host_cursor).then_some(own_cursor); + tracing::info!( + wallet_id = %hex::encode(wallet_id), + covered = record.covered.len(), + dropped, + predecessor, + inline, + cursor = ?cursor, + "the host's DashPay backfill record is replaced with the one this wallet \ + carries before the wallet is published" + ); + let changeset = PlatformWalletChangeSet { + core: cursor.map(|cursor| CoreChangeSet { + synced_height: Some(cursor), + synced_height_is_rewind: true, + ..CoreChangeSet::default() + }), + dashpay_backfill: Some(record.clone()), + ..PlatformWalletChangeSet::default() + }; + self.persister.store(wallet_id, changeset).map_err(|e| { + tracing::error!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "failed to replace the host's DashPay backfill record; \ + the wallet was not published" + ); + PlatformWalletError::from_store_failure(&*self.persister, e) + })?; + if !inline { + self.persister.flush(wallet_id).map_err(|e| { + tracing::error!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "failed to commit the replaced DashPay backfill record; \ + the wallet was not published" + ); + PlatformWalletError::Persistence(e.to_string()) + })?; + } + if let Some(cursor) = cursor { + durable_cursors.insert(wallet_id, DurableCursor::at(cursor)); + } + Ok(record) + } + /// Hand a newly published wallet the barrier state a removed wallet of /// the same id left behind, so advances that wallet emitted — still /// queued in, or already projected by, the shared adapter — are dropped diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index f65faf64ca0..a79e0d54144 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -509,10 +509,10 @@ impl PlatformWalletManager

{ // An entry in `DurableCursors` is what tells: every publication leaves // one and removal keeps it. So with an entry present the host's // record is replaced with the one this wallet carries, whatever the - // snapshot showed. The duplicate check runs first, so a wallet still - // registered under this id never has its record overwritten; every - // publisher holds the cursor lock, so none can appear between that - // check and the insert. + // snapshot showed (`settle_published_backfill`). The duplicate check + // runs first, so a wallet still registered under this id never has + // its record overwritten; every publisher holds the cursor lock, so + // none can appear between that check and the insert. let created_cursor = platform_info.core_wallet.metadata.synced_height; let (snapshot_cursor, mut host_backfill) = match host_wallets.remove(®istration_wallet_id) { @@ -548,33 +548,14 @@ impl PlatformWalletManager

{ { return Err(already_registered(registration_wallet_id)); } - let predecessor = durable_cursors.contains_key(®istration_wallet_id); - if dropped || predecessor { - tracing::info!( - wallet_id = %hex::encode(registration_wallet_id), - kept = host_backfill.covered.len(), - predecessor, - "wallet recreated: the host's DashPay backfill record is replaced with \ - the one this wallet carries before the wallet is published" - ); - self.persister - .store( - registration_wallet_id, - crate::changeset::PlatformWalletChangeSet { - dashpay_backfill: Some(host_backfill.clone()), - ..Default::default() - }, - ) - .map_err(|e| { - tracing::error!( - wallet_id = %hex::encode(registration_wallet_id), - error = %e, - "failed to re-arm the host's DashPay backfill coverage; \ - registration aborted before the wallet was registered" - ); - PlatformWalletError::from_store_failure(&*self.persister, e) - })?; - } + let host_backfill = self.settle_published_backfill( + &mut durable_cursors, + registration_wallet_id, + host_backfill, + dropped, + created_cursor, + snapshot_cursor.unwrap_or(created_cursor), + )?; let mut wm = self.wallet_manager.write().await; let wallet_id = wm .insert_wallet(wallet, platform_info) diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 3dcb682c25d..6784e94b31b 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -337,7 +337,14 @@ impl DashPayView<'_, B> { // height, and a cursor written from here could advance it past rows // that never landed — so nothing is stored at all: the in-memory // rewind still runs the rescan, and the next launch re-runs it, - // which is the pre-record behaviour. + // which is the pre-record behaviour; + // - a backend whose `store` only buffers (`store_commits_inline()` + // false) commits at a later `flush` that can still fail and drop the + // round. A record "stored" there is not on disk, and neither is the + // cursor it vouches for, so nothing would tell this pass whether the + // coverage it installs is durable. Such a backend gets no record at + // all: the pass runs in memory only, exactly the pre-record guard. + // Coverage is durable only where the store is the commit. let mut staged = info.dashpay_backfill.clone(); staged.retain(|owner, contact, account_index| { receival_accounts @@ -377,7 +384,9 @@ impl DashPayView<'_, B> { info.dashpay_backfill.unpersisted_cursor_epoch = Some(info.rewind_barrier.epoch()); info.dashpay_backfill.unpersisted_extent = extent; let faulted = self.sync_fault.load(std::sync::atomic::Ordering::Relaxed); - let stored = if faulted { + let stored = if !self.persister.store_commits_inline() { + Ok(false) + } else if faulted { Err(None) } else { let changeset = PlatformWalletChangeSet { @@ -389,12 +398,14 @@ impl DashPayView<'_, B> { dashpay_backfill: Some(staged.clone()), ..PlatformWalletChangeSet::default() }; - self.persister.store(changeset).map_err(Some) + self.persister.store(changeset).map(|()| true).map_err(Some) }; match stored { - Ok(()) => { + Ok(durable) => { + // In memory only (`durable` false), the staged record carries + // no debt: nothing will ever be stored for it to settle. info.dashpay_backfill = staged; - if let Some(cursor) = cursor_to_write { + if let Some(cursor) = cursor_to_write.filter(|_| durable) { durable_cursors .insert(self.wallet_id, crate::changeset::DurableCursor::at(cursor)); } @@ -1900,9 +1911,18 @@ mod tests { fail_stores: Mutex, /// When set, the next `store` panics — a backend that dies mid-write. panic_next_store: Mutex, + /// When set, `store` only buffers and the backend commits at `flush` + /// (`store_commits_inline()` false). + buffered: Mutex, + /// When set, every `flush` fails. + fail_flushes: Mutex, } impl PlatformWalletPersistence for RecordingPersister { + fn store_commits_inline(&self) -> bool { + !*self.buffered.lock().unwrap() + } + fn store( &self, wallet_id: WalletId, @@ -1919,6 +1939,9 @@ mod tests { } fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + if *self.fail_flushes.lock().unwrap() { + return Err(PersistenceError::backend("injected flush failure")); + } Ok(()) } @@ -3273,6 +3296,10 @@ mod tests { } impl PlatformWalletPersistence for ReloadPersister { + fn store_commits_inline(&self) -> bool { + true + } + fn store( &self, wallet_id: WalletId, @@ -4726,6 +4753,209 @@ mod tests { .await; } + /// A backend whose `store` only buffers commits at a later `flush` that + /// can still fail and drop the round, so a "stored" record is not proof + /// of anything on disk. Such a backend gets no record: the pass rewinds + /// and marks in memory only — the pre-record guard — and no accepted + /// store moves the durable cursor. A relaunch then finds no record and + /// rewinds again: slow, never lossy (dashpay/platform#4302 review). + #[tokio::test] + async fn a_buffering_backend_keeps_the_backfill_record_in_memory_only() { + let (manager, persister, wallet_id) = make_wallet().await; + *persister.buffered.lock().unwrap() = true; + *persister.fail_flushes.lock().unwrap() = true; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + let durable_before = manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(); + persister.stores.lock().unwrap().clear(); + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let dashpay = wallet.identity().dashpay(); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("reconcile"), + Some(100), + "the rewind still runs, in memory" + ); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("reconcile"), + None, + "the in-memory guard stops a second rewind this session" + ); + assert!( + last_stored_record(&persister.stores.lock().unwrap()).is_none(), + "no record is handed to a backend whose store is not the commit" + ); + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(), + durable_before, + "nothing it accepted is taken as durable" + ); + { + let wm = manager.wallet_manager.read().await; + let record = &wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill; + assert_eq!(record.covered_from(&owner, &contact, 0), Some(100)); + assert_eq!( + record.unpersisted_cursor, None, + "no debt for a record never stored" + ); + } + + // A relaunch finds no record on the host and rewinds again. + let snapshot = + reload_snapshot(&manager, wallet_id, owner, 1_000, Some(Default::default())).await; + let (restarted, _, _) = reload(snapshot).await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile after reload"), + Some(100) + ); + } + + /// On a buffering backend the pre-publication replacement is committed + /// with a `flush` before the wallet becomes visible, and a failed flush + /// fails the registration rather than publishing over a replacement that + /// never reached disk (dashpay/platform#4302 review). + #[tokio::test] + async fn a_buffering_backend_flushes_the_replaced_record_before_publishing() { + let (donor, _, wallet_id) = make_wallet().await; + drop(donor); + let persister = Arc::new(RecordingPersister::default()); + *persister.buffered.lock().unwrap() = true; + *persister.fail_flushes.lock().unwrap() = true; + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new( + sdk, + Arc::clone(&persister), + handler, + )); + // A same-id wallet was published earlier in this process. + manager + .durable_cursors + .lock() + .await + .insert(wallet_id, crate::changeset::DurableCursor::at(1_000)); + let seed = Mnemonic::from_phrase(TEST_MNEMONIC) + .expect("valid mnemonic") + .to_seed(""); + let create = || { + manager.create_wallet_from_seed_bytes( + Network::Testnet, + &seed, + WalletAccountCreationOptions::Default, + Some(101), + ) + }; + assert!( + create().await.is_err(), + "the replacement did not commit, so the wallet is not published" + ); + assert!(manager.get_wallet(&wallet_id).await.is_none()); + + *persister.fail_flushes.lock().unwrap() = false; + persister.stores.lock().unwrap().clear(); + create().await.expect("the replacement commits"); + first_store_is_an_empty_record(&persister.stores.lock().unwrap()); + } + + /// The replacement can also put coverage BACK: the loaded record covers + /// account A while a same-id predecessor scanned the host's cursor to + /// 1000 without it. Coverage from the snapshot beside cursor 1000 would + /// claim (800, 1000] was watched for A, and a crash straight after the + /// replacement would restore exactly that. So the replacement carries the + /// loaded cursor (800) as a rewind in the same round, and the durable + /// entry follows it — nothing is left owed in memory only + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn loading_a_covering_record_below_the_hosts_cursor_writes_the_loaded_cursor_with_it() { + let (donor, donor_persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact( + &donor, + &donor_persister, + wallet_id, + owner, + contact, + 200, + 200, + ) + .await; + let mut record = crate::changeset::DashPayBackfillRecord::default(); + record.record_pass(800, None, [(owner, contact, 0, 200)]); + let snapshot = reload_snapshot(&donor, wallet_id, owner, 800, Some(record.clone())).await; + drop(donor); + + let host = Arc::new(snapshot); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + manager.durable_cursors.lock().await.insert( + wallet_id, + crate::changeset::DurableCursor { + height: 1_000, + advance_epoch: Some(2), + }, + ); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + + { + let stores = host.stores.lock().unwrap(); + let (_, first) = stores.first().expect("the replacement was stored"); + assert_eq!(first.dashpay_backfill.as_ref(), Some(&record)); + let core = first + .core + .as_ref() + .expect("the loaded cursor rides the replacement round"); + assert_eq!(core.synced_height, Some(800)); + assert!(core.synced_height_is_rewind); + } + assert_eq!( + manager + .durable_cursors + .lock() + .await + .get(&wallet_id) + .copied(), + Some(crate::changeset::DurableCursor::at(800)), + "the entry follows the committed cursor" + ); + let wm = manager.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .unpersisted_cursor, + None, + "nothing is owed: the cursor went to the host with the coverage" + ); + } + /// The host snapshot a registration reads comes before it takes the /// durable-cursor lock, and a same-id predecessor still registered then /// can have a commit accepted in between. Under the lock the entry that From 2cb90493d34c9a1afa244415149f07e870d963d4 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Thu, 8 Oct 2026 00:31:07 -0700 Subject: [PATCH 24/43] fix(unified-sdk-jni): match the bridge override probe on the called descriptor, not the name bridge_overrides() looked for any declared method with the callback's name. A subclass declaring an overload of onWalletChangesetHeader while inheriting the no-op callback the trampoline calls would wire the DashPay backfill slot, and could then store coverage without the lowered cursor. The probe now builds each same-named declared method's JNI descriptor from its reflected parameter and return types and requires it to equal the descriptor the trampoline calls. The header descriptor becomes a constant shared by the call site, the verification table and the probe. The sweep slot uses the same check. Co-Authored-By: Claude Opus 5.5 --- .../rs-unified-sdk-jni/src/persistence.rs | 140 ++++++++++++++++-- 1 file changed, 124 insertions(+), 16 deletions(-) diff --git a/packages/rs-unified-sdk-jni/src/persistence.rs b/packages/rs-unified-sdk-jni/src/persistence.rs index 7c4a2ac9ab3..af0743ff4ca 100644 --- a/packages/rs-unified-sdk-jni/src/persistence.rs +++ b/packages/rs-unified-sdk-jni/src/persistence.rs @@ -218,8 +218,18 @@ pub(crate) fn build_vtable(context: *mut c_void) -> PersistenceCallbacks { /// never delivered and every launch re-runs the backfill: slow, never lossy. pub(crate) fn build_extension(env: &mut JNIEnv, bridge: &JObject) -> PersistenceCallbacksExtension { extension_for(BridgeOverrides { - sweeps: bridge_overrides(env, bridge, "onWalletChangesetTransactionsSwept"), - header: bridge_overrides(env, bridge, "onWalletChangesetHeader"), + sweeps: bridge_overrides( + env, + bridge, + "onWalletChangesetTransactionsSwept", + WALLET_CHANGESET_SWEEPS_DESCRIPTOR, + ), + header: bridge_overrides( + env, + bridge, + "onWalletChangesetHeader", + WALLET_CHANGESET_HEADER_DESCRIPTOR, + ), }) } @@ -256,15 +266,31 @@ fn extension_for(overrides: BridgeOverrides) -> PersistenceCallbacksExtension { } /// Whether `bridge`'s concrete class — or any superclass strictly below -/// `NativePersistenceBridge` — declares a method named `name`. A Kotlin -/// `override fun` is a declared method of the overriding class, so walking -/// `getDeclaredMethods()` up the hierarchy until the abstract bridge answers -/// "did a subclass supply its own body". Any JNI failure counts as "not -/// overridden" (the pending exception is cleared): the consequence is a -/// slot left unwired, which Rust turns into a stripped capability — the -/// safe direction, never a silently swallowed removal. -fn bridge_overrides(env: &mut JNIEnv, bridge: &JObject, name: &str) -> bool { - fn probe(env: &mut JNIEnv, bridge: &JObject, name: &str) -> Result { +/// `NativePersistenceBridge` — declares the method `name` with the JNI +/// `descriptor` the trampoline calls. A Kotlin `override fun` is a declared +/// method of the overriding class, so walking `getDeclaredMethods()` up the +/// hierarchy until the abstract bridge answers "did a subclass supply its own +/// body". The descriptor has to match, not just the name: an overload such +/// as `onWalletChangesetHeader(walletId, height)` leaves the callback the +/// trampoline invokes inherited, and must not count. Any JNI failure counts +/// as "not overridden" (the pending exception is cleared): the consequence is +/// a slot left unwired, which Rust turns into a stripped capability — the +/// safe direction, never a silently swallowed write. +fn bridge_overrides(env: &mut JNIEnv, bridge: &JObject, name: &str, descriptor: &str) -> bool { + fn class_name(env: &mut JNIEnv, class: &JObject) -> Result { + let name: JString = env + .call_method(class, "getName", "()Ljava/lang/String;", &[])? + .l()? + .into(); + let name = env.get_string(&name)?.to_str().map(str::to_owned); + Ok(name.unwrap_or_default()) + } + fn probe( + env: &mut JNIEnv, + bridge: &JObject, + name: &str, + descriptor: &str, + ) -> Result { let base = env.find_class("org/dashfoundation/dashsdk/ffi/NativePersistenceBridge")?; let mut class = env.get_object_class(bridge)?; loop { @@ -287,12 +313,28 @@ fn bridge_overrides(env: &mut JNIEnv, bridge: &JObject, name: &str) -> bool { .call_method(&method, "getName", "()Ljava/lang/String;", &[])? .l()? .into(); - let matches = env + let same_name = env .get_string(&method_name)? .to_str() .map(|s| s == name) .unwrap_or(false); - if matches { + if !same_name { + continue; + } + let params: JObjectArray = env + .call_method(&method, "getParameterTypes", "()[Ljava/lang/Class;", &[])? + .l()? + .into(); + let mut param_names = Vec::new(); + for p in 0..env.get_array_length(¶ms)? { + let param = env.get_object_array_element(¶ms, p)?; + param_names.push(class_name(env, ¶m)?); + } + let return_type = env + .call_method(&method, "getReturnType", "()Ljava/lang/Class;", &[])? + .l()?; + let return_name = class_name(env, &return_type)?; + if method_descriptor(¶m_names, &return_name) == descriptor { return Ok(true); } } @@ -305,7 +347,7 @@ fn bridge_overrides(env: &mut JNIEnv, bridge: &JObject, name: &str) -> bool { class = superclass.into(); } } - match probe(env, bridge, name) { + match probe(env, bridge, name, descriptor) { Ok(overridden) => overridden, Err(_) => { let _ = env.exception_clear(); @@ -314,6 +356,34 @@ fn bridge_overrides(env: &mut JNIEnv, bridge: &JObject, name: &str) -> bool { } } +/// The JNI descriptor of a method whose parameter and return types +/// `Class.getName()` reports as `params` and `return_type` — `int`, `[B`, +/// `java.lang.String`, `[Ljava.lang.String;` and so on. +fn method_descriptor(params: &[String], return_type: &str) -> String { + fn type_descriptor(name: &str) -> String { + match name { + "boolean" => "Z".to_owned(), + "byte" => "B".to_owned(), + "char" => "C".to_owned(), + "short" => "S".to_owned(), + "int" => "I".to_owned(), + "long" => "J".to_owned(), + "float" => "F".to_owned(), + "double" => "D".to_owned(), + "void" => "V".to_owned(), + array if array.starts_with('[') => array.replace('.', "/"), + class => format!("L{};", class.replace('.', "/")), + } + } + let mut descriptor = String::from("("); + for param in params { + descriptor.push_str(&type_descriptor(param)); + } + descriptor.push(')'); + descriptor.push_str(&type_descriptor(return_type)); + descriptor +} + /// `release_fn` for the persistence vtable: frees the boxed /// [`KotlinPersistenceCtx`] when the native manager's last persister /// reference drops. The FFI guarantees exactly one call, which may land @@ -724,7 +794,7 @@ unsafe extern "C" fn tramp_persist_wallet_changeset( .call_method( bridge, "onWalletChangesetHeader", - "([BZIZJJJJ[B)I", + WALLET_CHANGESET_HEADER_DESCRIPTOR, &[ (&wid).into(), JValue::Bool(has_synced as u8), @@ -810,6 +880,11 @@ unsafe extern "C" fn tramp_persist_wallet_changeset_sweeps( /// header's `(hasSyncedHeight, syncedHeight)`, not a sentinel. const WALLET_CHANGESET_SWEEPS_DESCRIPTOR: &str = "([B[BI[B[BIZI)I"; +/// `onWalletChangesetHeader`'s descriptor, shared by the `call_method` site, +/// the descriptor table and the override probe that gates the DashPay +/// backfill slot on it, so the three cannot drift apart. +const WALLET_CHANGESET_HEADER_DESCRIPTOR: &str = "([BZIZJJJJ[B)I"; + unsafe fn persist_changeset_sweep_batch( env: &mut JNIEnv, bridge: &JObject, @@ -4726,7 +4801,10 @@ const BRIDGE_METHOD_TABLE: &[(&str, &str)] = &[ "onPersistAccountAddressPoolEntry", "([BBBIII[B[BB[BZBIZJLjava/lang/String;Ljava/lang/String;)I", ), - ("onWalletChangesetHeader", "([BZIZJJJJ[B)I"), + ( + "onWalletChangesetHeader", + WALLET_CHANGESET_HEADER_DESCRIPTOR, + ), ("onWalletChangesetAccountBegin", "([BIBBII[B[BIZIZ)I"), ("onWalletChangesetAccountEnd", "([BI)I"), ( @@ -4925,6 +5003,36 @@ mod tests { /// A height past `i32::MAX` must refuse the call, never wrap into a /// negative `Int` the handler would read as absent or as a bogus /// collection boundary. + /// The override probe compares the reflected descriptor, not the name: an + /// overload of `onWalletChangesetHeader` leaves the callback the + /// trampoline invokes inherited, so it must not wire the backfill slot. + #[test] + fn the_override_probe_matches_the_called_descriptor_not_an_overload() { + let names = |names: &[&str]| names.iter().map(|n| n.to_string()).collect::>(); + let header = names(&[ + "[B", "boolean", "int", "boolean", "long", "long", "long", "long", "[B", + ]); + assert_eq!( + method_descriptor(&header, "int"), + WALLET_CHANGESET_HEADER_DESCRIPTOR, + "the real override resolves to the descriptor the trampoline calls" + ); + assert_ne!( + method_descriptor(&names(&["[B", "int"]), "int"), + WALLET_CHANGESET_HEADER_DESCRIPTOR, + "an overload is not the callback" + ); + let sweeps = names(&["[B", "[B", "int", "[B", "[B", "int", "boolean", "int"]); + assert_eq!( + method_descriptor(&sweeps, "int"), + WALLET_CHANGESET_SWEEPS_DESCRIPTOR + ); + assert_eq!( + method_descriptor(&names(&["java.lang.String", "[Ljava.lang.String;"]), "void"), + "(Ljava/lang/String;[Ljava/lang/String;)V" + ); + } + /// A bridge that inherits the no-op `onWalletChangesetHeader` stores no /// cursor, so it must not be handed the DashPay backfill record either: /// coverage committed beside a cursor that was never lowered would From f20cf8a6cfb45c0e66a7806ab23d62ad26a8c198 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Thu, 8 Oct 2026 23:53:25 -0700 Subject: [PATCH 25/43] fix(platform-wallet): require atomic changesets for durable backfill coverage and distrust restored markers over foreign pools Three review findings on 2cb90493d3, plus the rustfmt miss in manager/mod.rs that stopped the Rust workspace job. - Durable coverage now needs a backend whose store is the commit AND that attests ATOMIC_CHANGESETS (persists_backfill_coverage). An inline but non-atomic backend could keep a round's coverage and reject the cursor it vouches for. The rescan sweep and settle_published_backfill share the check; any other backend gets session-only coverage, and a record it still holds is replaced with an empty one before publication. - A restored outbound-account marker suppresses the startup rebuild of the contact's external account, but a host that replaced the xpub on rotation can keep the old pool's addresses, and send_payment hands out a pooled address before deriving one. Loading now regenerates each external pool from its account's current xpub and clears the marker wherever a restored address does not match, so the contact sweep rebuilds that account as every cold start did before the marker was persisted. - A failed pre-publication flush is reported as the new typed PlatformWalletError::PersisterFlush with the backend's own kind, instead of a string. It does not go through from_store_failure, whose reissue narrowing is about stores, not flushes. It falls to the generic FFI code like the string variant it replaces. Regressions: a non-atomic backend that keeps a record and fails its cursor, a non-atomic load holding an old record, a rotated xpub over the old pool, and the flush failure's type and kind. Each fails against the previous code. Co-Authored-By: Claude Opus 5.5 --- .../src/changeset/dashpay_backfill.rs | 22 ++ .../rs-platform-wallet/src/changeset/mod.rs | 4 +- packages/rs-platform-wallet/src/error.rs | 20 ++ .../rs-platform-wallet/src/manager/load.rs | 92 ++++++ .../rs-platform-wallet/src/manager/mod.rs | 28 +- .../src/wallet/identity/network/payments.rs | 272 +++++++++++++++++- .../src/wallet/persister.rs | 6 + 7 files changed, 422 insertions(+), 22 deletions(-) diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 45fc2294a5e..5db28263f0e 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -21,6 +21,8 @@ use dpp::prelude::Identifier; +use super::{PersistenceCapabilities, PlatformWalletPersistence}; + /// One receival account the backfill covers, and the height it covers it /// from. /// @@ -141,6 +143,26 @@ pub struct DashPayBackfillRecord { pub unpersisted_extent: Option<(u32, u32)>, } +/// Whether `persister` can hold a durable [`DashPayBackfillRecord`]. +/// +/// The record vouches for the cursor written in the same round, so both +/// halves have to land together or not at all, and be on disk once `store` +/// returns. That takes a backend whose `store` is the commit +/// ([`store_commits_inline`](PlatformWalletPersistence::store_commits_inline)) +/// AND that attests [`PersistenceCapabilities::ATOMIC_CHANGESETS`]: an +/// inline but non-atomic backend could keep the coverage after rejecting +/// the cursor, and a buffering one could still drop the round at `flush`. +/// Any other backend gets session-only coverage — the pre-record guard. +pub fn persists_backfill_coverage

(persister: &P) -> bool +where + P: PlatformWalletPersistence + ?Sized, +{ + persister.store_commits_inline() + && persister + .persistence_capabilities() + .contains(PersistenceCapabilities::ATOMIC_CHANGESETS) +} + /// The persisted fields of a [`DashPayBackfillRecord`] as serde reads them, /// before [`DashPayBackfillRecord::from_entries`] canonicalizes the cover set. #[cfg(feature = "serde")] diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index e4f8bfa9a34..8680e44f4c4 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -50,7 +50,9 @@ pub use core_bridge::{ spawn_wallet_event_adapter, spawn_wallet_event_adapter_with_durable_cursors, DurableCursor, DurableCursorState, DurableCursors, RetiredBarrier, }; -pub use dashpay_backfill::{DashPayBackfillCoveredContact, DashPayBackfillRecord}; +pub use dashpay_backfill::{ + persists_backfill_coverage, DashPayBackfillCoveredContact, DashPayBackfillRecord, +}; 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 73955b6604f..cf2aba64e9f 100644 --- a/packages/rs-platform-wallet/src/error.rs +++ b/packages/rs-platform-wallet/src/error.rs @@ -38,6 +38,16 @@ pub enum PlatformWalletError { #[error("failed to persist wallet changeset: {0}")] PersisterStore(#[source] crate::changeset::PersistenceError), + /// A persister `flush` failed: the commit of rounds a backend whose + /// `store` only buffers was still holding. It keeps the flush's own + /// classification, which means something different from a store's: + /// `Transient` says the buffer survived and another `flush` may commit + /// it, `Constraint` that the buffered data was rejected, and `Fatal` that + /// the buffered rounds were discarded. See [`Self::PersisterLoad`] for why + /// the typed cause is carried. + #[error("failed to commit buffered wallet changesets: {0}")] + PersisterFlush(#[source] crate::changeset::PersistenceError), + /// Restoring persisted platform-address state into a freshly registered /// wallet failed. Boxed to break the recursion; the inner variant and its /// `#[source]` chain survive intact. @@ -1012,6 +1022,16 @@ impl PlatformWalletError { Self::PersisterStore(source) } + /// A persister `flush` failed. Unlike + /// [`from_store_failure`](Self::from_store_failure) the classification is + /// kept as the backend reported it: a transient flush failure is retried + /// with another `flush`, which the store-side reissue attestation says + /// nothing about. See [`Self::from_load_failure`] for why no blanket + /// conversion exists. + pub fn from_flush_failure(source: crate::changeset::PersistenceError) -> Self { + Self::PersisterFlush(source) + } + /// Restoring persisted platform-address state failed. Boxes `source`, so /// callers never write `Box::new`. pub fn from_restore_failure(source: PlatformWalletError) -> Self { diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index a455b8a8b84..b6eadf57b98 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -13,6 +13,10 @@ use crate::wallet::PlatformWallet; use std::time::Duration; use crate::broadcaster::{BroadcastError, TransactionBroadcaster}; +use dpp::prelude::Identifier; +use key_wallet::bip32::ExtendedPubKey; +use key_wallet::managed_account::address_pool::{AddressPool, KeySource}; +use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; use key_wallet::transaction_checking::transaction_context::TransactionContext; use key_wallet::transaction_checking::wallet_checker::WalletTransactionChecker; @@ -233,6 +237,26 @@ impl PlatformWalletManager

{ dashpay_backfill, rewind_barrier: Default::default(), }; + // A restored outbound-account marker says the contact's external + // account was built from its current xpub, which suppresses the + // startup rebuild. It must not vouch for a pool the host kept + // across a rotation: the host replaces the account's xpub but may + // keep the old pool's addresses, and `send_payment` hands out a + // pooled address before deriving a new one. So any external pool + // holding an address its current xpub does not derive loses its + // contact's marker, and the first contact sweep rebuilds it from + // the current xpub, as every cold start did before the marker + // was persisted (dashpay/platform#4302 review). + let distrusted = + distrust_markers_of_foreign_external_pools(&wallet, &mut platform_info); + if distrusted > 0 { + tracing::warn!( + wallet_id = %hex::encode(expected_wallet_id), + contacts = distrusted, + "load: restored DashPay external pools hold addresses their current xpub \ + does not derive; their accounts will be rebuilt" + ); + } // Seed the double-spend screen's session memory from the // freshly restored state: it closes the race where SPV's // chainlock dispatcher promotion-evicts a restored spender @@ -1355,6 +1379,74 @@ mod idempotent_load_tests { } } +/// Clear the outbound-account marker of every established contact whose +/// restored DashPay external pool holds an address the account's current +/// xpub (on `wallet`) does not derive at that index, so the contact sweep +/// rebuilds the account instead of trusting the pool. Returns how many +/// markers were cleared. +fn distrust_markers_of_foreign_external_pools( + wallet: &key_wallet::Wallet, + info: &mut PlatformWalletInfo, +) -> usize { + let foreign: Vec<_> = info + .core_wallet + .accounts + .dashpay_external_accounts + .iter() + .filter(|(key, managed)| { + wallet + .accounts + .dashpay_external_accounts + .get(key) + .is_some_and(|account| { + managed + .managed_account_type() + .address_pools() + .into_iter() + .any(|pool| !pool_derives_from(pool, &account.account_xpub)) + }) + }) + .map(|(key, _)| *key) + .collect(); + let mut cleared = 0; + for key in foreign { + let owner = Identifier::from(key.user_identity_id); + let contact = Identifier::from(key.friend_identity_id); + let marker = info + .identity_manager + .managed_identity_mut(&owner) + .and_then(|managed| managed.established_contact_mut(&contact)) + .and_then(|established| established.external_account_reference.take()); + if marker.is_some() { + cleared += 1; + } + } + cleared +} + +/// Whether every address in `pool` is the one `xpub` derives at its index — +/// checked by regenerating a scratch pool with the same layout. +fn pool_derives_from(pool: &AddressPool, xpub: &ExtendedPubKey) -> bool { + let Some(highest) = pool.highest_generated else { + return true; + }; + let mut derived = AddressPool::new_without_generation( + pool.base_path.clone(), + pool.pool_type, + pool.gap_limit, + pool.network, + ); + if derived + .generate_addresses(highest.saturating_add(1), &KeySource::Public(*xpub), true) + .is_err() + { + return false; + } + pool.addresses + .iter() + .all(|(index, info)| derived.address_at_index(*index).as_ref() == Some(&info.address)) +} + #[cfg(test)] mod tests { use std::sync::atomic::{AtomicUsize, Ordering}; diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index d93745b5988..6af5729ed5c 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -26,8 +26,9 @@ use tokio_util::sync::CancellationToken; use key_wallet_manager::WalletManager; use crate::changeset::{ - spawn_wallet_event_adapter_with_durable_cursors, CoreChangeSet, DashPayBackfillRecord, - DurableCursor, DurableCursors, PlatformWalletChangeSet, PlatformWalletPersistence, + persists_backfill_coverage, spawn_wallet_event_adapter_with_durable_cursors, CoreChangeSet, + DashPayBackfillRecord, DurableCursor, DurableCursors, PlatformWalletChangeSet, + PlatformWalletPersistence, }; use crate::error::PlatformWalletError; use crate::events::{PlatformEventHandler, PlatformEventManager}; @@ -673,10 +674,12 @@ impl PlatformWalletManager

{ /// watched. The entry follows the cursor only once the round is /// committed, so a lower cursor owed later is measured against it. /// - /// A backend whose `store` only buffers (`store_commits_inline()` false) - /// gets no record at all, the same as the rescan sweep gives it: only an - /// empty one is written, and flushed before the wallet is published, so a - /// record an earlier run left cannot outlive this one. + /// A backend that cannot hold the record durably + /// ([`persists_backfill_coverage`](crate::changeset::persists_backfill_coverage): + /// 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. #[allow(clippy::too_many_arguments)] pub(super) fn settle_published_backfill( &self, @@ -688,10 +691,15 @@ impl PlatformWalletManager

{ 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; - let record = if inline { record } else { DashPayBackfillRecord::default() }; - if !(dropped || predecessor || (!inline && held_on_host)) { + let record = if durable { + record + } else { + DashPayBackfillRecord::default() + }; + if !(dropped || predecessor || (!durable && held_on_host)) { return Ok(record); } let host_cursor = durable_cursors @@ -703,7 +711,7 @@ impl PlatformWalletManager

{ covered = record.covered.len(), dropped, predecessor, - inline, + durable, cursor = ?cursor, "the host's DashPay backfill record is replaced with the one this wallet \ carries before the wallet is published" @@ -734,7 +742,7 @@ impl PlatformWalletManager

{ "failed to commit the replaced DashPay backfill record; \ the wallet was not published" ); - PlatformWalletError::Persistence(e.to_string()) + PlatformWalletError::from_flush_failure(e) })?; } if let Some(cursor) = cursor { diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 6784e94b31b..01b421add1d 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -338,13 +338,12 @@ impl DashPayView<'_, B> { // that never landed — so nothing is stored at all: the in-memory // rewind still runs the rescan, and the next launch re-runs it, // which is the pre-record behaviour; - // - a backend whose `store` only buffers (`store_commits_inline()` - // false) commits at a later `flush` that can still fail and drop the - // round. A record "stored" there is not on disk, and neither is the - // cursor it vouches for, so nothing would tell this pass whether the - // coverage it installs is durable. Such a backend gets no record at - // all: the pass runs in memory only, exactly the pre-record guard. - // Coverage is durable only where the store is the commit. + // - a backend that cannot hold the record durably gets none at all + // (`persists_backfill_coverage`): one whose `store` only buffers + // commits at a later `flush` that can still drop the round, and one + // that is not atomic can keep the coverage after rejecting the + // cursor it vouches for. The pass then runs in memory only, exactly + // the pre-record guard. let mut staged = info.dashpay_backfill.clone(); staged.retain(|owner, contact, account_index| { receival_accounts @@ -384,7 +383,7 @@ impl DashPayView<'_, B> { info.dashpay_backfill.unpersisted_cursor_epoch = Some(info.rewind_barrier.epoch()); info.dashpay_backfill.unpersisted_extent = extent; let faulted = self.sync_fault.load(std::sync::atomic::Ordering::Relaxed); - let stored = if !self.persister.store_commits_inline() { + let stored = if !self.persister.persists_backfill_coverage() { Ok(false) } else if faulted { Err(None) @@ -1916,6 +1915,12 @@ mod tests { buffered: Mutex, /// When set, every `flush` fails. fail_flushes: Mutex, + /// When set, the backend does not attest `ATOMIC_CHANGESETS`. + non_atomic: Mutex, + /// When set, a round carrying both a cursor and a backfill record + /// keeps the record and then fails on the cursor — the partial write + /// a non-atomic backend is allowed to leave. + partial_rounds: Mutex, } impl PlatformWalletPersistence for RecordingPersister { @@ -1923,6 +1928,14 @@ mod tests { !*self.buffered.lock().unwrap() } + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { + if *self.non_atomic.lock().unwrap() { + crate::changeset::PersistenceCapabilities::NONE + } else { + crate::changeset::PersistenceCapabilities::ATOMIC_CHANGESETS + } + } + fn store( &self, wallet_id: WalletId, @@ -1934,13 +1947,30 @@ mod tests { if std::mem::take(&mut *self.panic_next_store.lock().unwrap()) { panic!("recording persister: injected store panic"); } + if *self.partial_rounds.lock().unwrap() + && changeset.dashpay_backfill.is_some() + && changeset + .core + .as_ref() + .is_some_and(|core| core.synced_height.is_some()) + { + let kept = PlatformWalletChangeSet { + dashpay_backfill: changeset.dashpay_backfill, + ..PlatformWalletChangeSet::default() + }; + self.stores.lock().unwrap().push((wallet_id, kept)); + return Err(PersistenceError::backend("injected cursor write failure")); + } self.stores.lock().unwrap().push((wallet_id, changeset)); Ok(()) } fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { if *self.fail_flushes.lock().unwrap() { - return Err(PersistenceError::backend("injected flush failure")); + return Err(PersistenceError::backend_with_kind( + PersistenceErrorKind::Transient, + "injected flush failure", + )); } Ok(()) } @@ -3300,6 +3330,10 @@ mod tests { true } + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { + crate::changeset::PersistenceCapabilities::ATOMIC_CHANGESETS + } + fn store( &self, wallet_id: WalletId, @@ -4833,6 +4867,214 @@ mod tests { ); } + /// Inline is not enough: a backend that does not attest atomic + /// changesets may keep the coverage of a round and then reject the cursor + /// it vouches for, and a crash before the retry would restore coverage + /// beside the old cursor. Such a backend gets session-only coverage, like + /// a buffering one, so there is never a partial round to leave behind + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn a_non_atomic_backend_keeps_the_backfill_record_in_memory_only() { + let (manager, persister, wallet_id) = make_wallet().await; + *persister.non_atomic.lock().unwrap() = true; + *persister.partial_rounds.lock().unwrap() = true; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + persister.stores.lock().unwrap().clear(); + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let dashpay = wallet.identity().dashpay(); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("reconcile"), + Some(100) + ); + assert_eq!( + dashpay.reconcile_dashpay_rescan().await.expect("reconcile"), + None, + "the in-memory guard still holds for the session" + ); + assert!( + last_stored_record(&persister.stores.lock().unwrap()).is_none(), + "no record reaches a backend that could keep it without its cursor" + ); + let snapshot = + reload_snapshot(&manager, wallet_id, owner, 1_000, Some(Default::default())).await; + let (restarted, _, _) = reload(snapshot).await; + assert_eq!( + restarted + .get_wallet(&wallet_id) + .await + .expect("wallet") + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile after reload"), + Some(100), + "a relaunch rewinds again: slow, never lossy" + ); + } + + /// [`ReloadPersister`] behind an inline backend that does not attest + /// atomic changesets. + struct NonAtomicHost(Arc); + + impl PlatformWalletPersistence for NonAtomicHost { + fn store_commits_inline(&self) -> bool { + true + } + + fn store( + &self, + wallet_id: WalletId, + changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + self.0.store(wallet_id, changeset) + } + + fn flush(&self, wallet_id: WalletId) -> Result<(), PersistenceError> { + self.0.flush(wallet_id) + } + + fn load(&self) -> Result { + self.0.load() + } + } + + /// A non-atomic backend that still holds a record from an earlier run + /// must not have it trusted, nor left behind: the load carries no record + /// and replaces the host's with an empty one before publishing. + #[tokio::test] + async fn loading_on_a_non_atomic_backend_invalidates_the_hosts_record() { + let (donor, donor_persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact( + &donor, + &donor_persister, + wallet_id, + owner, + contact, + 200, + 200, + ) + .await; + let mut record = crate::changeset::DashPayBackfillRecord::default(); + record.record_pass(800, None, [(owner, contact, 0, 200)]); + let host = Arc::new(reload_snapshot(&donor, wallet_id, owner, 800, Some(record)).await); + drop(donor); + + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new( + sdk, + Arc::new(NonAtomicHost(Arc::clone(&host))), + handler, + )); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + first_store_is_an_empty_record(&host.stores.lock().unwrap()); + let wm = manager.wallet_manager.read().await; + assert!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .is_empty(), + "the restored record is not trusted in memory either" + ); + } + + /// A restored outbound-account marker suppresses the startup rebuild, so + /// it must not vouch for a pool its account's xpub does not derive. A + /// host that replaced the xpub on rotation but kept the old pool's rows + /// would otherwise hand out pre-rotation addresses on the next send. + /// The load clears the marker for such a pool, and the contact sweep + /// rebuilds the account from the current xpub (dashpay/platform#4302 + /// review). + #[tokio::test] + async fn a_restored_marker_is_not_trusted_over_a_pool_its_current_xpub_does_not_derive() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + let encrypted = encrypted_contact_xpub(&owner, &contact, &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + { + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 7, encrypted.clone(), 100, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + } + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &encrypted, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register outbound"); + iw.dashpay() + .note_external_account_registered(&owner, &contact, &encrypted) + .await; + let key = DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }; + + for (rotated, marker) in [(false, Some(7)), (true, None)] { + let mut snapshot = reload_snapshot(&manager, wallet_id, owner, 1_000, None).await; + if rotated { + // The host replaced the account's xpub and kept the old rows. + snapshot + .wallet + .accounts + .dashpay_external_accounts + .get_mut(&key) + .expect("outbound account") + .account_xpub = test_receiving_xpub(&contact, &owner); + } + let (restarted, _, _) = reload(snapshot).await; + let wm = restarted.wallet_manager.read().await; + let established = wm + .get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone(); + assert_eq!( + established.external_account_reference, marker, + "rotated: {rotated}" + ); + assert_eq!( + external_account_needs_rebuild(&established, true), + rotated, + "only a pool the current xpub does not derive is rebuilt" + ); + } + } + /// On a buffering backend the pre-publication replacement is committed /// with a `flush` before the wallet becomes visible, and a failed flush /// fails the registration rather than publishing over a replacement that @@ -4868,9 +5110,17 @@ mod tests { Some(101), ) }; + let error = match create().await { + Ok(_) => panic!("the replacement did not commit, so the wallet is not published"), + Err(error) => error, + }; assert!( - create().await.is_err(), - "the replacement did not commit, so the wallet is not published" + matches!( + &error, + crate::error::PlatformWalletError::PersisterFlush(source) + if source.kind() == Some(PersistenceErrorKind::Transient) + ), + "the flush failure keeps its own type and kind: {error:?}" ); assert!(manager.get_wallet(&wallet_id).await.is_none()); diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index fbfefcb2f9d..f22e88d3d90 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -81,6 +81,12 @@ impl WalletPersister { self.inner.store_commits_inline() } + /// Whether the backend can hold a durable DashPay backfill record (see + /// [`persists_backfill_coverage`](crate::changeset::persists_backfill_coverage)). + pub(crate) fn persists_backfill_coverage(&self) -> bool { + crate::changeset::persists_backfill_coverage(self.inner.as_ref()) + } + /// Feature-specific persistence contracts exposed by the backend. pub(crate) fn persistence_capabilities(&self) -> PersistenceCapabilities { self.inner.persistence_capabilities() From 90cb46acbfe10d673c2f595de6d6b971c498998e Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Fri, 9 Oct 2026 08:16:21 -0700 Subject: [PATCH 26/43] fix(platform-wallet-ffi): deliver empty backfill records as invalidations past the coverage gates The FFI adapter withheld every DashPay backfill record from a host that does not attest ATOMIC_CHANGESETS or stores no cursors, including the empty record settle_published_backfill sends to clear a host's coverage before publishing a wallet that cannot keep it durably. store() then returned Ok with the old coverage still on disk, so a later atomic run could restore it beside cursors the recreated wallet scanned without the account. Those gates guard coverage. An empty record covers no contact and vouches for no cursor, so it now reaches every host with the backfill slot, and a failing callback fails the round and with it the publication. Records that cover contacts keep both gates. The gating test now uses a covering record for the withheld cases and checks empty records reach cursorless and non-atomic hosts. A lifecycle test drives create, remove and recreate through a real non-atomic FFIPersister: the recreation delivers the invalidation, and a failing callback fails the registration. Both fail against the previous gate. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/persistence.rs | 176 +++++++++++++++++- 1 file changed, 169 insertions(+), 7 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index b940adb1db9..7154fdf5335 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -2250,6 +2250,18 @@ impl PlatformWalletPersistence for FFIPersister { // coverage from a round Rust reports as failed — coverage that then // suppresses the rescan on the next launch. Only a host attesting // `ATOMIC_CHANGESETS` rolls the whole round back, record included. + // + // Both gates guard COVERAGE. An empty record covers no contact, so it + // vouches for no cursor and is safe beside any, on any host. It is + // how Rust invalidates coverage a host already holds: + // `settle_published_backfill` sends one before publishing a wallet to + // a host that cannot keep coverage durably, so the host cannot + // restore an earlier run's coverage beside cursors this run scanned + // without it. Withholding it here would let `store` return `Ok` with + // the old coverage still on disk. So an empty record goes to every + // host with the slot, and a failing callback fails the round, which + // fails the publication. A host without the slot never received a + // record, so it holds none to invalidate. let host_stores_cursors = self.callbacks.on_persist_wallet_changeset_fn.is_some() && self .persistence_capabilities() @@ -2258,11 +2270,10 @@ impl PlatformWalletPersistence for FFIPersister { .core .as_ref() .is_some_and(|core| core.synced_height.is_some()); - if let Some(record) = changeset - .dashpay_backfill - .as_ref() - .filter(|_| host_stores_cursors && (!backfill_cursor_owed || cursor_delivered)) - { + if let Some(record) = changeset.dashpay_backfill.as_ref().filter(|record| { + record.is_empty() + || (host_stores_cursors && (!backfill_cursor_owed || cursor_delivered)) + }) { if let Some(cb) = self.wallet_dashpay_backfill_callback { let covered = build_dashpay_backfill_covered_for_callback(record); let result = unsafe { @@ -8631,13 +8642,29 @@ mod tests { }; type PersistWalletChangesetFnForTest = unsafe extern "C" fn(*mut c_void, *const u8, *const WalletChangeSetFFI) -> i32; + // The gates guard coverage, so the rounds below carry a record that + // covers a contact; an empty one is an invalidation (see the end). + let covering = || { + let mut record = DashPayBackfillRecord::default(); + record.record_pass( + 1_000, + Some(100), + [( + Identifier::from([0xAA; 32]), + Identifier::from([0xBB; 32]), + 0, + 100, + )], + ); + record + }; let rewind_round = || PlatformWalletChangeSet { core: Some(CoreChangeSet { synced_height: Some(100), synced_height_is_rewind: true, ..CoreChangeSet::default() }), - dashpay_backfill: Some(DashPayBackfillRecord::default()), + dashpay_backfill: Some(covering()), ..PlatformWalletChangeSet::default() }; @@ -8648,7 +8675,7 @@ mod tests { assert_eq!(sink.backfills.load(Ordering::SeqCst), 0); let record_only_round = || PlatformWalletChangeSet { - dashpay_backfill: Some(DashPayBackfillRecord::default()), + dashpay_backfill: Some(covering()), ..PlatformWalletChangeSet::default() }; @@ -8688,6 +8715,141 @@ mod tests { .store([1u8; 32], record_only_round()) .expect("record-only round"); assert_eq!(sink.backfills.load(Ordering::SeqCst), 0); + + // An empty record covers no one, so it vouches for no cursor: it is + // an invalidation and reaches the cursorless and the non-atomic host + // alike, so neither can keep an earlier run's coverage + // (dashpay/platform#4302 review). + let invalidation = || PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }; + non_atomic + .store([1u8; 32], invalidation()) + .expect("invalidation"); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 1); + let sink = Sink::default(); + let cursorless = persister(&sink, None); + cursorless + .store([1u8; 32], invalidation()) + .expect("invalidation"); + assert_eq!(sink.backfills.load(Ordering::SeqCst), 1); + } + + /// The lifecycle end of the invalidation above, through the real FFI + /// adapter on a host that is not atomic: recreating a wallet under an id + /// published earlier in this process must clear the host's backfill + /// record before the wallet is published, and a host that fails that + /// callback must fail the registration rather than keep coverage beside + /// cursors the recreated wallet will scan (dashpay/platform#4302 review). + #[test] + fn a_recreated_wallet_invalidates_the_record_on_a_non_atomic_ffi_host() { + use crate::event_handler::{EventHandlerCallbacks, FFIEventHandler}; + use crate::runtime::runtime; + use key_wallet::wallet::initialization::WalletAccountCreationOptions; + use platform_wallet::PlatformWalletManager; + use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; + + #[derive(Default)] + struct Host { + invalidations: AtomicUsize, + covering: AtomicUsize, + fail: AtomicBool, + } + unsafe extern "C" fn on_backfill( + ctx: *mut c_void, + _wallet_id: *const u8, + _floor: u32, + _rewound_from: u32, + _covered: *const DashPayBackfillCoveredContactFFI, + covered_count: usize, + ) -> i32 { + let host = &*(ctx as *const Host); + if covered_count == 0 { + host.invalidations.fetch_add(1, Ordering::SeqCst); + } else { + host.covering.fetch_add(1, Ordering::SeqCst); + } + i32::from(host.fail.load(Ordering::SeqCst)) + } + let host: &'static Host = Box::leak(Box::default()); + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: host as *const Host as *mut c_void, + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + PersistenceExtensionCallbacks { + wallet_dashpay_backfill: Some(on_backfill), + ..Default::default() + }, + ); + let events = FFIEventHandler::new( + EventHandlerCallbacks { + context: std::ptr::null_mut(), + on_wallet_event_fn: None, + on_error_fn: None, + on_platform_address_sync_completed_fn: None, + on_shielded_sync_completed_fn: None, + on_shielded_sync_progress_fn: None, + on_shielded_tree_progress_fn: None, + release_fn: None, + }, + None, + ); + let manager = { + let _runtime_guard = runtime().enter(); + PlatformWalletManager::new( + Arc::new( + dash_sdk::SdkBuilder::new_mock() + .with_network(dashcore::Network::Testnet) + .build() + .expect("mock sdk"), + ), + Arc::new(persister), + Arc::new(events), + ) + }; + runtime().block_on(async { + let create = || { + manager.create_wallet_from_seed_bytes( + dashcore::Network::Testnet, + &[42; 64], + WalletAccountCreationOptions::Default, + Some(0), + ) + }; + let wallet_id = create().await.expect("first registration").wallet_id(); + assert_eq!( + host.invalidations.load(Ordering::SeqCst), + 0, + "nothing was published under this id before" + ); + manager + .remove_wallet(&wallet_id) + .await + .expect("remove the wallet"); + + host.fail.store(true, Ordering::SeqCst); + assert!( + create().await.is_err(), + "an invalidation the host failed must fail the registration" + ); + assert!(manager.get_wallet(&wallet_id).await.is_none()); + + host.fail.store(false, Ordering::SeqCst); + create().await.expect("the invalidation lands"); + assert_eq!( + host.invalidations.load(Ordering::SeqCst), + 2, + "each recreation delivered the invalidation, the failed one included" + ); + assert_eq!( + host.covering.load(Ordering::SeqCst), + 0, + "a host that is not atomic never receives coverage" + ); + }); } /// The restore side of dashpay/platform#4302: a wallet-restore entry From 592cc1b9dd55f08f86900d371fdcf3d5c2bcaa1c Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Fri, 9 Oct 2026 20:09:21 -0700 Subject: [PATCH 27/43] fix(platform-wallet)!: refuse to publish over backfill coverage a host cannot invalidate A host without the FFI backfill slot still hands back a stored record on load, since restore decodes it whether or not the write slot is wired, but it cannot replace it. The empty-record invalidation sent before publishing a recreated wallet was skipped without an error, so the wallet published, scanned without the covered account, and a later session with the slot trusted the stale coverage. - PlatformWalletPersistence gains stores_dashpay_backfill(), false by default. persists_backfill_coverage now requires it alongside inline, atomic rounds; FFIPersister answers whether its backfill slot is wired. A backend that does not store the record gets session-only coverage. - settle_published_backfill replaces the record on such a backend only when the host handed one back, since this process never wrote one there. A host that holds no record keeps the no-record fallback. - FFIPersister::store fails a round carrying an empty record when the slot is absent, with a Fatal PersistenceError and before anything is applied, so the publication fails instead of being acknowledged. The cross-session FFI regression drives registration through a real FFIPersister whose load callback hands back stored coverage: with the slot the invalidation is delivered, and a failing callback fails the registration; without it the registration fails with a typed store error; a host with no record registers either way. The buffered-backend test now loads a host that holds a record. Both the FFI tests fail against the previous behaviour. BREAKING CHANGE: PlatformWalletPersistence implementations that store and restore the DashPay backfill record must override stores_dashpay_backfill() to return true, or they get session-only coverage. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/persistence.rs | 270 +++++++++++++----- .../src/changeset/dashpay_backfill.rs | 5 +- .../src/changeset/traits.rs | 18 ++ .../rs-platform-wallet/src/manager/mod.rs | 13 +- .../src/wallet/identity/network/payments.rs | 124 +++++--- 5 files changed, 317 insertions(+), 113 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 7154fdf5335..349e9b9f4dc 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -1832,6 +1832,12 @@ impl PlatformWalletPersistence for FFIPersister { Ok(out) } + fn stores_dashpay_backfill(&self) -> bool { + // The record reaches the host only through its extension slot; a + // host without it cannot store one, nor replace one it holds. + self.wallet_dashpay_backfill_callback.is_some() + } + fn store_commits_inline(&self) -> bool { // The end callback commits (or rolls back) the host transaction before // `store` returns. `flush` is only a later general-purpose notification. @@ -1855,6 +1861,28 @@ impl PlatformWalletPersistence for FFIPersister { // the sync mutex only adds waiting under genuine round contention. let mut round = self.round_lock.lock(); + // An empty backfill record is an invalidation: Rust sends one to + // clear coverage the host handed back on load, before publishing a + // wallet that will scan without it. A host without the backfill slot + // can still hand coverage back — its load path decodes the record + // whether or not the write slot is wired — but it cannot replace it. + // Acknowledging the round would leave that coverage on disk beside + // cursors scanned without its accounts, for a later session with the + // slot to trust. So the round fails before anything is applied, and + // the publication with it (dashpay/platform#4302 review). + if self.wallet_dashpay_backfill_callback.is_none() + && changeset + .dashpay_backfill + .as_ref() + .is_some_and(|record| record.is_empty()) + { + return Err(PersistenceError::backend_with_kind( + PersistenceErrorKind::Fatal, + "the host holds a DashPay backfill record but registered no backfill \ + callback to invalidate it", + )); + } + // Open the round on the Rust side (rejects a nested begin / an // unclean round left open by a prior unwind — error, never // panic). Matched 1:1 with the `round.end_round()` below. @@ -8543,7 +8571,9 @@ mod tests { vec!["backfill wallet=7 floor=0 rewound_from=0 covered=[]".to_string()] ); - // A host without the slot: the round still succeeds. + // A host without the slot: a covering record is withheld and the + // round still succeeds — Rust never offers it one, since such a host + // does not store the record. let sink = Sink::default(); let callbacks = PersistenceCallbacks { context: &sink as *const Sink as *mut c_void, @@ -8554,16 +8584,42 @@ mod tests { callbacks, PersistenceCapabilities::NONE, ); + assert!(!legacy.stores_dashpay_backfill()); + let covering = DashPayBackfillRecord::from_entries( + 100, + 1_000, + vec![platform_wallet::changeset::DashPayBackfillCoveredContact { + owner: Identifier::from([0xAA; 32]), + contact: Identifier::from([0xBB; 32]), + account_index: 0, + covered_from: 100, + }], + ); legacy .store( [7u8; 32], PlatformWalletChangeSet { - dashpay_backfill: Some(DashPayBackfillRecord::default()), + dashpay_backfill: Some(covering), ..PlatformWalletChangeSet::default() }, ) .expect("a host without the slot still stores the round"); assert!(sink.events.lock().unwrap().is_empty()); + // But an empty record is an invalidation of coverage the host handed + // back, which it cannot perform: the round fails before anything is + // applied, rather than being acknowledged. + let refused = legacy.store( + [7u8; 32], + PlatformWalletChangeSet { + dashpay_backfill: Some(DashPayBackfillRecord::default()), + ..PlatformWalletChangeSet::default() + }, + ); + assert!(matches!( + refused, + Err(ref error) if error.kind() == Some(PersistenceErrorKind::Fatal) + )); + assert!(sink.events.lock().unwrap().is_empty()); drop(persister); } @@ -8737,25 +8793,62 @@ mod tests { } /// The lifecycle end of the invalidation above, through the real FFI - /// adapter on a host that is not atomic: recreating a wallet under an id - /// published earlier in this process must clear the host's backfill - /// record before the wallet is published, and a host that fails that - /// callback must fail the registration rather than keep coverage beside - /// cursors the recreated wallet will scan (dashpay/platform#4302 review). + /// adapter, across sessions: an earlier session stored coverage the host + /// now hands back, and this session recreates the wallet without the + /// account it covers. The host's record must be cleared before the wallet + /// is published (dashpay/platform#4302 review): + /// - a host with the backfill slot but no atomic rounds gets the empty + /// record, and a failing callback fails the registration; + /// - a host WITHOUT the slot cannot clear it, so the registration fails + /// with a typed persistence error instead of being acknowledged; + /// - a host that hands back no record registers normally either way. #[test] - fn a_recreated_wallet_invalidates_the_record_on_a_non_atomic_ffi_host() { + fn a_recreated_wallet_must_clear_coverage_the_ffi_host_handed_back() { use crate::event_handler::{EventHandlerCallbacks, FFIEventHandler}; use crate::runtime::runtime; + use crate::wallet_restore_types::WalletRestoreEntryFFI; use key_wallet::wallet::initialization::WalletAccountCreationOptions; - use platform_wallet::PlatformWalletManager; + use platform_wallet::changeset::PersistenceErrorKind; + use platform_wallet::{PlatformWalletError, PlatformWalletManager}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; - #[derive(Default)] struct Host { + wallet_id: [u8; 32], + holds_record: bool, + covered: [DashPayBackfillCoveredContactFFI; 1], invalidations: AtomicUsize, covering: AtomicUsize, fail: AtomicBool, } + unsafe extern "C" fn load_wallets( + ctx: *mut c_void, + entries: *mut *const WalletRestoreEntryFFI, + count: *mut usize, + ) -> i32 { + let host = &*(ctx as *const Host); + *entries = Box::into_raw(Box::new(WalletRestoreEntryFFI { + wallet_id: host.wallet_id, + has_dashpay_backfill: host.holds_record, + dashpay_backfill_floor: 100, + dashpay_backfill_rewound_from: 1_000, + dashpay_backfill_covered: if host.holds_record { + host.covered.as_ptr() + } else { + std::ptr::null() + }, + dashpay_backfill_covered_count: usize::from(host.holds_record), + ..Default::default() + })); + *count = 1; + 0 + } + unsafe extern "C" fn free_wallets( + _: *mut c_void, + entries: *const WalletRestoreEntryFFI, + _: usize, + ) { + drop(Box::from_raw(entries.cast_mut())); + } unsafe extern "C" fn on_backfill( ctx: *mut c_void, _wallet_id: *const u8, @@ -8772,32 +8865,38 @@ mod tests { } i32::from(host.fail.load(Ordering::SeqCst)) } - let host: &'static Host = Box::leak(Box::default()); - let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( - PersistenceCallbacks { - context: host as *const Host as *mut c_void, - ..PersistenceCallbacks::default() - }, - PersistenceCapabilities::NONE, - PersistenceExtensionCallbacks { - wallet_dashpay_backfill: Some(on_backfill), - ..Default::default() - }, - ); - let events = FFIEventHandler::new( - EventHandlerCallbacks { - context: std::ptr::null_mut(), - on_wallet_event_fn: None, - on_error_fn: None, - on_platform_address_sync_completed_fn: None, - on_shielded_sync_completed_fn: None, - on_shielded_sync_progress_fn: None, - on_shielded_tree_progress_fn: None, - release_fn: None, - }, - None, - ); - let manager = { + fn manager_for( + host: Option<&'static Host>, + slot: bool, + ) -> PlatformWalletManager { + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: host.map_or(std::ptr::null_mut(), |host| { + host as *const Host as *mut c_void + }), + on_load_wallet_list_fn: host.map(|_| load_wallets as _), + on_load_wallet_list_free_fn: host.map(|_| free_wallets as _), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + PersistenceExtensionCallbacks { + wallet_dashpay_backfill: slot.then_some(on_backfill as _), + ..Default::default() + }, + ); + let events = FFIEventHandler::new( + EventHandlerCallbacks { + context: std::ptr::null_mut(), + on_wallet_event_fn: None, + on_error_fn: None, + on_platform_address_sync_completed_fn: None, + on_shielded_sync_completed_fn: None, + on_shielded_sync_progress_fn: None, + on_shielded_tree_progress_fn: None, + release_fn: None, + }, + None, + ); let _runtime_guard = runtime().enter(); PlatformWalletManager::new( Arc::new( @@ -8809,47 +8908,70 @@ mod tests { Arc::new(persister), Arc::new(events), ) + } + let create = |manager: &PlatformWalletManager| { + runtime().block_on(manager.create_wallet_from_seed_bytes( + dashcore::Network::Testnet, + &[42; 64], + WalletAccountCreationOptions::Default, + Some(0), + )) + }; + let wallet_id = create(&manager_for(None, true)) + .expect("the id this seed registers under") + .wallet_id(); + let host = |holds_record: bool| -> &'static Host { + Box::leak(Box::new(Host { + wallet_id, + holds_record, + covered: [DashPayBackfillCoveredContactFFI { + owner_identity_id: [0xAA; 32], + contact_identity_id: [0xBB; 32], + account_index: 0, + covered_from: 200, + }], + invalidations: AtomicUsize::new(0), + covering: AtomicUsize::new(0), + fail: AtomicBool::new(false), + })) }; - runtime().block_on(async { - let create = || { - manager.create_wallet_from_seed_bytes( - dashcore::Network::Testnet, - &[42; 64], - WalletAccountCreationOptions::Default, - Some(0), - ) - }; - let wallet_id = create().await.expect("first registration").wallet_id(); - assert_eq!( - host.invalidations.load(Ordering::SeqCst), - 0, - "nothing was published under this id before" - ); - manager - .remove_wallet(&wallet_id) - .await - .expect("remove the wallet"); - host.fail.store(true, Ordering::SeqCst); - assert!( - create().await.is_err(), - "an invalidation the host failed must fail the registration" - ); - assert!(manager.get_wallet(&wallet_id).await.is_none()); + // The slot, no atomic rounds: the invalidation is delivered, and a + // host that fails it fails the registration. + let with_slot = host(true); + with_slot.fail.store(true, Ordering::SeqCst); + let manager = manager_for(Some(with_slot), true); + assert!( + create(&manager).is_err(), + "an invalidation the host failed must fail the registration" + ); + with_slot.fail.store(false, Ordering::SeqCst); + create(&manager).expect("the invalidation lands"); + assert_eq!(with_slot.invalidations.load(Ordering::SeqCst), 2); + assert_eq!( + with_slot.covering.load(Ordering::SeqCst), + 0, + "a host that is not atomic never receives coverage" + ); - host.fail.store(false, Ordering::SeqCst); - create().await.expect("the invalidation lands"); - assert_eq!( - host.invalidations.load(Ordering::SeqCst), - 2, - "each recreation delivered the invalidation, the failed one included" - ); - assert_eq!( - host.covering.load(Ordering::SeqCst), - 0, - "a host that is not atomic never receives coverage" - ); - }); + // No slot: the coverage cannot be cleared, so nothing is published. + let manager = manager_for(Some(host(true)), false); + match create(&manager) { + Ok(_) => panic!("coverage the host cannot clear must block the registration"), + Err(PlatformWalletError::PersisterStore(source)) => { + assert_eq!(source.kind(), Some(PersistenceErrorKind::Fatal)); + } + Err(other) => panic!("expected a typed store failure, got {other:?}"), + } + assert!(runtime().block_on(manager.get_wallet(&wallet_id)).is_none()); + + // A host that hands back no record keeps the no-record fallback, + // with or without the slot. + for slot in [false, true] { + let clean = host(false); + create(&manager_for(Some(clean), slot)).expect("nothing to clear"); + assert_eq!(clean.invalidations.load(Ordering::SeqCst), 0); + } } /// The restore side of dashpay/platform#4302: a wallet-restore entry diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 5db28263f0e..1053c5a312e 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -145,6 +145,8 @@ pub struct DashPayBackfillRecord { /// Whether `persister` can hold a durable [`DashPayBackfillRecord`]. /// +/// The backend has to store the record at all +/// ([`stores_dashpay_backfill`](PlatformWalletPersistence::stores_dashpay_backfill)). /// The record vouches for the cursor written in the same round, so both /// halves have to land together or not at all, and be on disk once `store` /// returns. That takes a backend whose `store` is the commit @@ -157,7 +159,8 @@ pub fn persists_backfill_coverage

(persister: &P) -> bool where P: PlatformWalletPersistence + ?Sized, { - persister.store_commits_inline() + persister.stores_dashpay_backfill() + && persister.store_commits_inline() && persister .persistence_capabilities() .contains(PersistenceCapabilities::ATOMIC_CHANGESETS) diff --git a/packages/rs-platform-wallet/src/changeset/traits.rs b/packages/rs-platform-wallet/src/changeset/traits.rs index e1538a26263..8e57004ba3f 100644 --- a/packages/rs-platform-wallet/src/changeset/traits.rs +++ b/packages/rs-platform-wallet/src/changeset/traits.rs @@ -271,6 +271,24 @@ pub trait PlatformWalletPersistence: Send + Sync { false } + /// Whether this backend persists the + /// [`DashPayBackfillRecord`](crate::changeset::DashPayBackfillRecord) a + /// changeset carries, and can replace one it holds. + /// + /// Durable backfill coverage also needs inline, atomic rounds (see + /// [`persists_backfill_coverage`](crate::changeset::persists_backfill_coverage)). + /// A backend that answers `false` gets session-only coverage, and a + /// record it still hands back on load can only be invalidated through + /// [`store`](Self::store) — so a backend that cannot write the record + /// must fail such a store rather than acknowledge it. + /// + /// **Fail-closed:** the default is `false`. A backend that ignores the + /// record keeps the pre-record behaviour: the backfill re-runs on every + /// launch, slow but never lossy. + fn stores_dashpay_backfill(&self) -> bool { + false + } + /// Compatibility summary for older invitation callers. It is true when the /// backend attests atomic changesets plus durably persisted invitation rows /// and asset-lock funding indices. This does not attest restart hydration; diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 6af5729ed5c..bf511337f38 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -665,7 +665,7 @@ impl PlatformWalletManager

{ /// - the filter dropped coverage for accounts this wallet does not hold; /// - an entry shows a same-id wallet was published in this process — it /// may have stored or invalidated coverage after the snapshot was read; - /// - the backend does not commit inline and the snapshot held a record. + /// - the backend cannot hold the record durably and the snapshot held one. /// /// A replacement that puts coverage on the host while this wallet's cursor /// is below the host's carries that cursor, as a rewind, in the same @@ -699,7 +699,16 @@ impl PlatformWalletManager

{ } else { DashPayBackfillRecord::default() }; - if !(dropped || predecessor || (!durable && held_on_host)) { + // A backend that cannot hold the record durably never had one + // written by this process, so only a record it handed back can need + // invalidating; one that can is replaced when the snapshot's record + // may disagree with what the wallet carries. + let replace = if durable { + dropped || predecessor + } else { + held_on_host + }; + if !replace { return Ok(record); } let host_cursor = durable_cursors diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 01b421add1d..7c3e6a22b5a 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -1928,6 +1928,10 @@ mod tests { !*self.buffered.lock().unwrap() } + fn stores_dashpay_backfill(&self) -> bool { + true + } + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { if *self.non_atomic.lock().unwrap() { crate::changeset::PersistenceCapabilities::NONE @@ -3330,6 +3334,10 @@ mod tests { true } + fn stores_dashpay_backfill(&self) -> bool { + true + } + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { crate::changeset::PersistenceCapabilities::ATOMIC_CHANGESETS } @@ -5075,42 +5083,81 @@ mod tests { } } - /// On a buffering backend the pre-publication replacement is committed - /// with a `flush` before the wallet becomes visible, and a failed flush - /// fails the registration rather than publishing over a replacement that - /// never reached disk (dashpay/platform#4302 review). + /// [`ReloadPersister`] behind a backend whose `store` only buffers, with + /// a `flush` that can be made to fail transiently. + struct BufferedHost { + inner: Arc, + fail_flushes: std::sync::atomic::AtomicBool, + } + + impl PlatformWalletPersistence for BufferedHost { + fn stores_dashpay_backfill(&self) -> bool { + true + } + + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { + crate::changeset::PersistenceCapabilities::ATOMIC_CHANGESETS + } + + fn store( + &self, + wallet_id: WalletId, + changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + self.inner.store(wallet_id, changeset) + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + if self.fail_flushes.load(std::sync::atomic::Ordering::SeqCst) { + return Err(PersistenceError::backend_with_kind( + PersistenceErrorKind::Transient, + "injected flush failure", + )); + } + Ok(()) + } + + fn load(&self) -> Result { + self.inner.load() + } + } + + /// A buffering backend that hands back a record must have it replaced + /// with an empty one, committed with a `flush` before the wallet becomes + /// visible. A failed flush fails the load with the flush's own typed + /// error rather than publishing over a replacement that never reached + /// disk (dashpay/platform#4302 review). #[tokio::test] async fn a_buffering_backend_flushes_the_replaced_record_before_publishing() { - let (donor, _, wallet_id) = make_wallet().await; + let (donor, donor_persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact( + &donor, + &donor_persister, + wallet_id, + owner, + contact, + 200, + 200, + ) + .await; + let mut record = crate::changeset::DashPayBackfillRecord::default(); + record.record_pass(800, None, [(owner, contact, 0, 200)]); + let snapshot = Arc::new(reload_snapshot(&donor, wallet_id, owner, 800, Some(record)).await); drop(donor); - let persister = Arc::new(RecordingPersister::default()); - *persister.buffered.lock().unwrap() = true; - *persister.fail_flushes.lock().unwrap() = true; - let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); - let handler: Arc = Arc::new(NoopEventHandler); - let manager = Arc::new(PlatformWalletManager::new( - sdk, - Arc::clone(&persister), - handler, - )); - // A same-id wallet was published earlier in this process. - manager - .durable_cursors - .lock() - .await - .insert(wallet_id, crate::changeset::DurableCursor::at(1_000)); - let seed = Mnemonic::from_phrase(TEST_MNEMONIC) - .expect("valid mnemonic") - .to_seed(""); - let create = || { - manager.create_wallet_from_seed_bytes( - Network::Testnet, - &seed, - WalletAccountCreationOptions::Default, - Some(101), - ) + let host = Arc::new(BufferedHost { + inner: Arc::clone(&snapshot), + fail_flushes: std::sync::atomic::AtomicBool::new(true), + }); + let manager_over = |host: &Arc| { + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + Arc::new(PlatformWalletManager::new(sdk, Arc::clone(host), handler)) }; - let error = match create().await { + + let manager = manager_over(&host); + let error = match manager.load_from_persistor().await { Ok(_) => panic!("the replacement did not commit, so the wallet is not published"), Err(error) => error, }; @@ -5124,10 +5171,15 @@ mod tests { ); assert!(manager.get_wallet(&wallet_id).await.is_none()); - *persister.fail_flushes.lock().unwrap() = false; - persister.stores.lock().unwrap().clear(); - create().await.expect("the replacement commits"); - first_store_is_an_empty_record(&persister.stores.lock().unwrap()); + host.fail_flushes + .store(false, std::sync::atomic::Ordering::SeqCst); + snapshot.stores.lock().unwrap().clear(); + let manager = manager_over(&host); + manager + .load_from_persistor() + .await + .expect("the replacement commits"); + first_store_is_an_empty_record(&snapshot.stores.lock().unwrap()); } /// The replacement can also put coverage BACK: the loaded record covers From 0309ff69b8dc15a23fc478c62547504883f63700 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Sun, 11 Oct 2026 17:17:56 -0700 Subject: [PATCH 28/43] fix(unified-sdk-jni): wire the backfill slot only when the bridge also implements the backfill callback The slot was wired whenever the bridge overrode onWalletChangesetHeader, without checking onWalletChangesetDashPayBackfill. A bridge inheriting that method's no-op would acknowledge the empty record Rust sends to clear coverage the host handed back, so FFIPersister reported storage support for a no-op and the missing-slot refusal was bypassed. build_extension now probes onWalletChangesetDashPayBackfill with its called descriptor too, and wires the slot only when both callbacks are overridden. Unwired, FFIPersister reports the host does not store the record and refuses that invalidation, so the publication fails closed. The in-tree Room handler overrides both. Co-Authored-By: Claude Opus 5.5 --- .../rs-unified-sdk-jni/src/persistence.rs | 81 +++++++++++++------ 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/packages/rs-unified-sdk-jni/src/persistence.rs b/packages/rs-unified-sdk-jni/src/persistence.rs index af0743ff4ca..c726011cd2a 100644 --- a/packages/rs-unified-sdk-jni/src/persistence.rs +++ b/packages/rs-unified-sdk-jni/src/persistence.rs @@ -216,6 +216,14 @@ pub(crate) fn build_vtable(context: *mut c_void) -> PersistenceCallbacks { /// old cursor, and that coverage would suppress the recovery scan after a /// restart (dashpay/platform#4302 review). Without the slot the record is /// never delivered and every launch re-runs the backfill: slow, never lossy. +/// +/// It also needs the bridge to OVERRIDE `onWalletChangesetDashPayBackfill` +/// itself. The slot's presence is what `FFIPersister` reports as "this host +/// stores the record", and an inherited no-op would acknowledge the empty +/// record Rust sends to clear coverage the host handed back — so the +/// coverage would stay on disk while the wallet is published without it. +/// With the slot unwired that invalidation is refused instead, and the +/// publication fails closed. pub(crate) fn build_extension(env: &mut JNIEnv, bridge: &JObject) -> PersistenceCallbacksExtension { extension_for(BridgeOverrides { sweeps: bridge_overrides( @@ -230,6 +238,12 @@ pub(crate) fn build_extension(env: &mut JNIEnv, bridge: &JObject) -> Persistence "onWalletChangesetHeader", WALLET_CHANGESET_HEADER_DESCRIPTOR, ), + backfill: bridge_overrides( + env, + bridge, + "onWalletChangesetDashPayBackfill", + WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR, + ), }) } @@ -241,6 +255,8 @@ struct BridgeOverrides { sweeps: bool, /// `onWalletChangesetHeader`, which carries the scan cursor. header: bool, + /// `onWalletChangesetDashPayBackfill`, which stores the record. + backfill: bool, } /// The extension slots wired for a bridge with `overrides` (see @@ -256,7 +272,7 @@ fn extension_for(overrides: BridgeOverrides) -> PersistenceCallbacksExtension { on_persist_wallet_changeset_chain_lock_height_fn: Some( tramp_persist_wallet_changeset_chain_lock_height, ), - on_persist_wallet_dashpay_backfill_fn: if overrides.header { + on_persist_wallet_dashpay_backfill_fn: if overrides.header && overrides.backfill { Some(tramp_persist_wallet_dashpay_backfill) } else { None @@ -5033,33 +5049,52 @@ mod tests { ); } - /// A bridge that inherits the no-op `onWalletChangesetHeader` stores no - /// cursor, so it must not be handed the DashPay backfill record either: - /// coverage committed beside a cursor that was never lowered would - /// suppress the recovery scan after a restart. Overriding the header is - /// what wires the slot; the sweep slot keeps its own probe. + /// The backfill slot needs both callbacks a bridge supplies its own body + /// for. One that inherits the no-op `onWalletChangesetHeader` stores no + /// cursor, so coverage would land beside a cursor that never moved. One + /// that inherits the no-op `onWalletChangesetDashPayBackfill` stores no + /// record, yet would acknowledge the invalidation of coverage it handed + /// back on load. Left unwired, `FFIPersister` reports that the host does + /// not store the record and refuses that invalidation, so the publication + /// fails closed (dashpay/platform#4302 review). The sweep slot keeps its + /// own probe. #[test] - fn the_backfill_slot_is_wired_only_for_a_bridge_that_stores_the_cursor() { - let inherited_header = extension_for(BridgeOverrides { + fn the_backfill_slot_is_wired_only_for_a_bridge_that_stores_cursor_and_record() { + for (header, backfill, wired) in [ + (false, false, false), + (false, true, false), + (true, false, false), + (true, true, true), + ] { + let extension = extension_for(BridgeOverrides { + sweeps: false, + header, + backfill, + }); + assert_eq!( + extension.on_persist_wallet_dashpay_backfill_fn.is_some(), + wired, + "header overridden: {header}, backfill overridden: {backfill}" + ); + } + let sweeps_only = extension_for(BridgeOverrides { sweeps: true, header: false, + backfill: false, }); - assert!( - inherited_header - .on_persist_wallet_dashpay_backfill_fn - .is_none(), - "an inherited header means the record is never delivered" - ); - assert!(inherited_header - .on_persist_wallet_changeset_sweeps_fn - .is_some()); + assert!(sweeps_only.on_persist_wallet_changeset_sweeps_fn.is_some()); + assert!(sweeps_only.on_persist_wallet_dashpay_backfill_fn.is_none()); + } - let own_header = extension_for(BridgeOverrides { - sweeps: false, - header: true, - }); - assert!(own_header.on_persist_wallet_dashpay_backfill_fn.is_some()); - assert!(own_header.on_persist_wallet_changeset_sweeps_fn.is_none()); + /// The probe has to recognise a real `onWalletChangesetDashPayBackfill` + /// override by the descriptor the trampoline calls. + #[test] + fn the_backfill_probe_matches_the_called_descriptor() { + let names = |names: &[&str]| names.iter().map(|n| n.to_string()).collect::>(); + assert_eq!( + method_descriptor(&names(&["[B", "int", "int", "[B", "int"]), "int"), + WALLET_CHANGESET_DASHPAY_BACKFILL_DESCRIPTOR + ); } #[test] From 54459c07e96a7b1ec3317c632df195f98f7585d1 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Sun, 11 Oct 2026 17:17:57 -0700 Subject: [PATCH 29/43] fix(platform-wallet): serialize outbound account registration and bind its marker in the same round register_external_contact_account checked that the account was absent under a read guard, released it, persisted the account and pool, and only then took the write guard to insert. A registration that decrypted an older payload could pass the check, pause, and after a rotated build had persisted and stamped its account, overwrite that account's durable xpub and pool. Its in-memory insert then failed and nothing restored the durable row, leaving the newer marker beside the older account. Since the marker is now restored, a restart trusted that pairing. - The absence re-check, the durable write and the inserts now run under one write guard, so registrations of an account are serialized. - Under that guard the payload must still be the contact's live incoming request; otherwise the new ExternalAccountRegistration::Superseded is returned and nothing is written. - When the contact is established, its outbound-account marker is bound to the live request in the same store round as the account, so the durable marker always names the request the durable account came from. The regression registers a superseded payload, which writes nothing; then the live build, whose round carries account and marker together; then the superseded payload again, which finds the account and writes nothing; and reloads to find the live account with its marker. A test that registered a payload its contact never carried now uses the contact's own payload. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/identity/network/contacts.rs | 119 +++++++++++--- .../src/wallet/identity/network/payments.rs | 148 +++++++++++++++++- 2 files changed, 237 insertions(+), 30 deletions(-) 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 04e6de22de9..2e7b342c3a6 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs @@ -87,6 +87,10 @@ pub enum ExternalAccountRegistration { /// An account row already existed for the `(index, user, friend)` key; /// nothing was decrypted or replaced. AlreadyExisted, + /// The contact's live incoming request no longer carries the supplied + /// encrypted xpub — a rotation landed after the caller read it. Nothing + /// was persisted or inserted; the sweep rebuilds from the live request. + Superseded, } impl RegisterExternalError { @@ -406,16 +410,19 @@ impl DashPayView<'_, B> { /// tell a real (re)build from an already-existed no-op — only the former /// may stamp the rotation self-heal marker (see the enum docs). /// - /// **Never wrap this call in a timeout.** Unlike its sibling - /// [`Self::register_contact_account`], which persists inside the write lock - /// it already holds, this one persists *before* acquiring the lock — so - /// there is a genuine `.await` between the durable write and the in-memory - /// inserts. A future dropped in that window leaves an account on disk that - /// no in-memory collection knows about for the rest of the process's life; - /// a real crash reloads it from disk, an in-process cancellation does not. - /// (The next drain does re-register it idempotently, so this is a landmine - /// rather than data loss — but the asymmetry with the sibling is not - /// something to rediscover by analogy.) + /// The absence check, the durable write and the in-memory inserts run + /// under ONE wallet-manager write guard, so registrations of the same + /// account are serialized: a registration that decrypted a payload and + /// then lost the race to another one finds the account present and + /// returns [`ExternalAccountRegistration::AlreadyExisted`], instead of + /// overwriting the newer account's durable xpub and pool. Under the same + /// guard the payload is checked against the contact's LIVE incoming + /// request, and a stale one returns + /// [`ExternalAccountRegistration::Superseded`] without writing anything. + /// When the contact is established, the account and the contact's + /// outbound-account marker are written in the same store round, so the + /// durable marker always names the request the durable account was built + /// from (dashpay/platform#4302 review). pub async fn register_external_contact_account( &self, our_identity_id: &Identifier, @@ -542,22 +549,6 @@ impl DashPayView<'_, B> { // typed `insert_funds` API after the upstream split. let managed = key_wallet::managed_account::ManagedCoreFundsAccount::from_account(&account); - // Persist the registration BEFORE the in-memory inserts (same - // rationale as `register_contact_account`): without this round - // the account vanishes on relaunch and `send_payment` loses its - // xpub + derived-address state until the next sweep rebuilds it. - self.persister - .store(dashpay_account_registration_changeset( - account_type, - contact_xpub, - &managed, - )) - .map_err(|e| { - Transient(PlatformWalletError::InvalidIdentityData(format!( - "Failed to persist external contact account registration: {e}" - ))) - })?; - let mut wm = self.wallet_manager.write().await; let (wallet, info) = wm .get_wallet_mut_and_info_mut(&self.wallet_id) @@ -567,6 +558,72 @@ impl DashPayView<'_, B> { ))) })?; + // Re-check under the guard that serializes registrations: the early + // check above ran under a read guard that was released for the + // decrypt, and another registration may have built the account since. + let key = key_wallet::account::account_collection::DashpayAccountKey { + index: account_index, + user_identity_id: our_identity_id.to_buffer(), + friend_identity_id: contact_identity_id.to_buffer(), + }; + if info + .core_wallet + .accounts + .dashpay_external_accounts + .contains_key(&key) + { + return Ok(ExternalAccountRegistration::AlreadyExisted); + } + + // The payload must still be the contact's live request, and the + // durable marker is bound to it in the same round as the account. + let bound_contact = match info + .identity_manager + .managed_identity(our_identity_id) + .and_then(|managed| managed.established_contact(&contact_identity_id)) + { + Some(contact) + if contact.incoming_request.encrypted_public_key != contact_encrypted_xpub => + { + tracing::warn!( + our_identity = %our_identity_id, + contact = %contact_identity_id, + "external account payload superseded by a rotation before registration; \ + nothing written, the sweep rebuilds from the live request" + ); + return Ok(ExternalAccountRegistration::Superseded); + } + Some(contact) => { + let mut bound = contact.clone(); + bound.external_account_reference = Some(contact.incoming_request.account_reference); + Some(bound) + } + None => None, + }; + + // Persist the registration BEFORE the in-memory inserts (same + // rationale as `register_contact_account`): without this round + // the account vanishes on relaunch and `send_payment` loses its + // xpub + derived-address state until the next sweep rebuilds it. + let mut changeset = + dashpay_account_registration_changeset(account_type, contact_xpub, &managed); + if let Some(bound) = &bound_contact { + let mut contacts = crate::changeset::ContactChangeSet::default(); + contacts.established.insert( + crate::changeset::SentContactRequestKey { + owner_id: *our_identity_id, + recipient_id: contact_identity_id, + }, + bound.clone(), + ); + changeset.contacts = Some(contacts); + } + self.persister.store(changeset).map_err(|e| { + Transient(PlatformWalletError::InvalidIdentityData(format!( + "Failed to persist external contact account registration: {e}" + ))) + })?; + // (a) Insert Account into the immutable wallet account collection so the // xpub is accessible by `send_payment`. wallet @@ -589,6 +646,16 @@ impl DashPayView<'_, B> { ))) })?; + if let Some(bound) = bound_contact { + if let Some(contact) = info + .identity_manager + .managed_identity_mut(our_identity_id) + .and_then(|managed| managed.established_contact_mut(&contact_identity_id)) + { + contact.external_account_reference = bound.external_account_reference; + } + } + tracing::info!( our_identity = %our_identity_id, contact = %contact_identity_id, diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 7c3e6a22b5a..b45f2fe6c51 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -3859,6 +3859,8 @@ mod tests { let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); let iw = wallet.identity(); let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + let shared_key = [0x55u8; 32]; + let encrypted = encrypted_contact_xpub(&owner, &contact, &shared_key); { let mut wm = iw.wallet_manager.write().await; let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); @@ -3868,16 +3870,13 @@ mod tests { let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 1_226_329, 0); let incoming = - ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 1_226_329, 0); + ContactRequest::new(contact, owner, 0, 0, 0, encrypted.clone(), 1_226_329, 0); info.identity_manager .managed_identity_mut(&owner) .expect("managed") .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); info.core_wallet.update_synced_height(1_560_740); } - - let shared_key = [0x55u8; 32]; - let encrypted = encrypted_contact_xpub(&owner, &contact, &shared_key); let registration = iw .dashpay() .register_external_contact_account( @@ -4996,6 +4995,147 @@ mod tests { ); } + /// Two outbound-account registrations of one contact must not interleave: + /// one that decrypted a payload before a rotation, and lost the race to + /// the build from the rotated request, must not overwrite the newer + /// account's durable xpub and pool and leave the newer marker beside them. + /// The absence check, the live-request check and the durable write run + /// under one guard, and the account and its marker are written in one + /// round (dashpay/platform#4302 review). + #[tokio::test] + async fn a_superseded_outbound_registration_writes_nothing_and_the_marker_stays_bound() { + use crate::wallet::identity::network::contacts::ExternalAccountRegistration; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + // A: the payload an older build decrypted. B: the rotated request. + let payload_a = encrypted_contact_xpub(&owner, &Identifier::from([0x02; 32]), &shared_key); + let payload_b = encrypted_contact_xpub(&owner, &contact, &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + { + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 8, payload_b.clone(), 100, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + } + let register = |payload: Vec| { + let iw = iw.clone(); + async move { + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &payload, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register") + } + }; + let key = DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }; + let registrations = |stores: &[(WalletId, PlatformWalletChangeSet)]| { + stores + .iter() + .filter(|(_, cs)| !cs.account_registrations.is_empty()) + .count() + }; + + // A's payload against the live B request, account absent: refused, + // nothing written, nothing inserted. + persister.stores.lock().unwrap().clear(); + assert_eq!( + register(payload_a.clone()).await, + ExternalAccountRegistration::Superseded + ); + assert_eq!(registrations(&persister.stores.lock().unwrap()), 0); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert!(!info + .core_wallet + .accounts + .dashpay_external_accounts + .contains_key(&key)); + } + + // B builds: the account and the marker naming B ride ONE round. + assert_eq!( + register(payload_b.clone()).await, + ExternalAccountRegistration::Built + ); + { + let stores = persister.stores.lock().unwrap(); + let round = stores + .iter() + .map(|(_, cs)| cs) + .find(|cs| !cs.account_registrations.is_empty()) + .expect("the account was stored"); + let bound = round + .contacts + .as_ref() + .and_then(|contacts| contacts.established.values().next()) + .expect("the marker rides the account's round"); + assert_eq!(bound.external_account_reference, Some(8)); + } + let b_xpub = { + let wm = manager.wallet_manager.read().await; + wm.get_wallet(&wallet_id) + .expect("wallet") + .accounts + .dashpay_external_accounts[&key] + .account_xpub + }; + + // A, having passed its early check before B was built, resumes: it + // finds B's account under the guard and writes nothing. + persister.stores.lock().unwrap().clear(); + assert_eq!( + register(payload_a).await, + ExternalAccountRegistration::AlreadyExisted + ); + assert_eq!(registrations(&persister.stores.lock().unwrap()), 0); + + // What a restart restores: B's account, with B's marker, trusted. + let (restarted, _, _) = + reload(reload_snapshot(&manager, wallet_id, owner, 1_000, None).await).await; + let wm = restarted.wallet_manager.read().await; + assert_eq!( + wm.get_wallet(&wallet_id) + .expect("wallet") + .accounts + .dashpay_external_accounts[&key] + .account_xpub, + b_xpub + ); + let established = wm + .get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone(); + assert_eq!(established.external_account_reference, Some(8)); + } + /// A restored outbound-account marker suppresses the startup rebuild, so /// it must not vouch for a pool its account's xpub does not derive. A /// host that replaced the xpub on rotation but kept the old pool's rows From 7385b543f096f91b02b0af123c40f88d0b42b7b3 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Sun, 11 Oct 2026 23:32:27 -0700 Subject: [PATCH 30/43] fix(platform-wallet): bind the outbound marker in the account's round only on inline atomic hosts Writing the outbound-account marker in the same round as the account is safe only when that round is all-or-nothing. FFIPersister carries on to the contacts callback after the account callback fails, so a host without atomic rounds could reject a rotated account and pool, keep the contact row, and leave the live request's marker beside the previous account it still holds. A restart would trust that pairing. register_external_contact_account now binds the marker in the account's round only when the store commits inline and attests ATOMIC_CHANGESETS. On any other backend the round carries no marker, and the caller stamps it through note_external_account_registered only after the account round succeeded, as before the binding was added. Regressions: a rotation rebuilt on a host that rejects the account but keeps contact rows leaves no current marker on disk and keeps the repair armed (fails with the gate forced open); and an FFIPersister without begin/end brackets cannot claim atomic rounds and still delivers contact rows after a rejected account. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/persistence.rs | 113 ++++++++++++++ .../src/wallet/identity/network/contacts.rs | 30 +++- .../src/wallet/identity/network/payments.rs | 143 ++++++++++++++++++ 3 files changed, 278 insertions(+), 8 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 349e9b9f4dc..dd41d5c2750 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -8974,6 +8974,119 @@ mod tests { } } + /// Why the outbound-account marker may ride an account's round only on + /// an atomic host: without begin/end brackets `FFIPersister` cannot claim + /// `ATOMIC_CHANGESETS` even when the host declares it, and it carries on + /// to the contacts callback after the account callback failed. A marker + /// in that round would land beside the account the host still holds, so + /// `register_external_contact_account` leaves it out of rounds on such a + /// host (dashpay/platform#4302 review). + #[test] + fn a_non_atomic_host_keeps_contact_rows_after_rejecting_the_account() { + use dashcore::secp256k1::Secp256k1; + use key_wallet::account::AccountType; + use key_wallet::bip32::{ExtendedPrivKey, ExtendedPubKey}; + use key_wallet::mnemonic::Mnemonic; + use platform_wallet::changeset::{ + AccountRegistrationEntry, ContactChangeSet, SentContactRequestKey, + }; + use platform_wallet::wallet::identity::{ContactRequest, EstablishedContact}; + use std::sync::atomic::{AtomicUsize, Ordering}; + + #[derive(Default)] + struct Host { + marked_rows: AtomicUsize, + } + unsafe extern "C" fn reject_accounts( + _: *mut c_void, + _: *const u8, + _: *const AccountSpecFFI, + _: usize, + ) -> i32 { + 1 + } + #[allow(clippy::too_many_arguments)] + unsafe extern "C" fn keep_contacts( + ctx: *mut c_void, + _: *const u8, + upserts: *const ContactRequestFFI, + count: usize, + _: *const ContactRequestRemovalFFI, + _: usize, + _: *const ContactRequestRemovalFFI, + _: usize, + _: *const ContactIgnoredSenderFFI, + _: usize, + ) -> i32 { + let host = &*(ctx as *const Host); + for row in std::slice::from_raw_parts(upserts, count) { + if row.has_external_account_reference { + host.marked_rows.fetch_add(1, Ordering::SeqCst); + } + } + 0 + } + + let host = Host::default(); + let persister = FFIPersister::new_with_persistence_capabilities( + PersistenceCallbacks { + context: &host as *const Host as *mut c_void, + on_persist_account_registrations_fn: Some(reject_accounts), + on_persist_contacts_fn: Some(keep_contacts), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::ATOMIC_CHANGESETS, + ); + assert!( + !persister + .persistence_capabilities() + .contains(PersistenceCapabilities::ATOMIC_CHANGESETS), + "a declaration without begin/end brackets is not an atomic round" + ); + + let seed = Mnemonic::from_phrase( + "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", + ) + .expect("static BIP-39 vector") + .to_seed(""); + let master = + ExtendedPrivKey::new_master(dashcore::Network::Testnet, &seed).expect("master"); + let xpub = ExtendedPubKey::from_priv(&Secp256k1::new(), &master); + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let mut established = EstablishedContact::new( + contact, + ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0), + ContactRequest::new(contact, owner, 0, 0, 8, vec![0u8; 96], 100, 0), + ); + established.external_account_reference = Some(8); + let mut contacts = ContactChangeSet::default(); + contacts.established.insert( + SentContactRequestKey { + owner_id: owner, + recipient_id: contact, + }, + established, + ); + let round = PlatformWalletChangeSet { + account_registrations: vec![AccountRegistrationEntry { + account_type: AccountType::DashpayExternalAccount { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }, + account_xpub: xpub, + }], + contacts: Some(contacts), + ..PlatformWalletChangeSet::default() + }; + assert!(persister.store([7u8; 32], round).is_err()); + assert!( + host.marked_rows.load(Ordering::SeqCst) > 0, + "the contact rows still reached the host after the account was rejected" + ); + } + /// The restore side of dashpay/platform#4302: a wallet-restore entry /// carrying the record decodes into the start state's /// `dashpay_backfill` in canonical form, and an entry without it — the 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 2e7b342c3a6..5fc3b8c4533 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs @@ -419,10 +419,12 @@ impl DashPayView<'_, B> { /// guard the payload is checked against the contact's LIVE incoming /// request, and a stale one returns /// [`ExternalAccountRegistration::Superseded`] without writing anything. - /// When the contact is established, the account and the contact's - /// outbound-account marker are written in the same store round, so the - /// durable marker always names the request the durable account was built - /// from (dashpay/platform#4302 review). + /// When the contact is established and the backend's rounds are inline + /// and atomic, the account and the contact's outbound-account marker are + /// written in the same store round, so the durable marker always names + /// the request the durable account was built from. Any other backend + /// gets no marker from this round; the caller stamps it once the account + /// is stored (dashpay/platform#4302 review). pub async fn register_external_contact_account( &self, our_identity_id: &Identifier, @@ -575,8 +577,20 @@ impl DashPayView<'_, B> { return Ok(ExternalAccountRegistration::AlreadyExisted); } - // The payload must still be the contact's live request, and the - // durable marker is bound to it in the same round as the account. + // The payload must still be the contact's live request. The durable + // marker is bound to it in the same round as the account only where + // that round is all-or-nothing: an inline store that attests atomic + // changesets. A backend without that guarantee can reject the account + // and pool yet keep the contact row, leaving the live request's marker + // beside the previous account it still holds — which a restart would + // trust. There the marker keeps its old order: the caller stamps it + // (`note_external_account_registered`) only after this round succeeded + // (dashpay/platform#4302 review). + let binds_marker = self.persister.store_commits_inline() + && self + .persister + .persistence_capabilities() + .contains(crate::changeset::PersistenceCapabilities::ATOMIC_CHANGESETS); let bound_contact = match info .identity_manager .managed_identity(our_identity_id) @@ -593,12 +607,12 @@ impl DashPayView<'_, B> { ); return Ok(ExternalAccountRegistration::Superseded); } - Some(contact) => { + Some(contact) if binds_marker => { let mut bound = contact.clone(); bound.external_account_reference = Some(contact.incoming_request.account_reference); Some(bound) } - None => None, + Some(_) | None => None, }; // Persist the registration BEFORE the in-memory inserts (same diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index b45f2fe6c51..96a25ef97ae 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -1921,6 +1921,11 @@ mod tests { /// keeps the record and then fails on the cursor — the partial write /// a non-atomic backend is allowed to leave. partial_rounds: Mutex, + /// When set, a round carrying an account registration rejects the + /// account and pool but keeps the round's contact rows, then fails — + /// what `FFIPersister` does on a host without atomic rounds, which + /// carries on to later callbacks after one fails. + reject_account_rounds: Mutex, } impl PlatformWalletPersistence for RecordingPersister { @@ -1951,6 +1956,16 @@ mod tests { if std::mem::take(&mut *self.panic_next_store.lock().unwrap()) { panic!("recording persister: injected store panic"); } + if *self.reject_account_rounds.lock().unwrap() + && !changeset.account_registrations.is_empty() + { + let kept = PlatformWalletChangeSet { + contacts: changeset.contacts, + ..PlatformWalletChangeSet::default() + }; + self.stores.lock().unwrap().push((wallet_id, kept)); + return Err(PersistenceError::backend("injected account write failure")); + } if *self.partial_rounds.lock().unwrap() && changeset.dashpay_backfill.is_some() && changeset @@ -5136,6 +5151,134 @@ mod tests { assert_eq!(established.external_account_reference, Some(8)); } + /// The marker rides the account's round only where that round is + /// all-or-nothing. A host without atomic rounds can reject a rotated + /// account and its pool yet keep the round's contact rows; with the + /// marker in that round, the live request's marker would sit on disk + /// beside the previous account the host still holds, and a restart would + /// trust it. On such a host the round carries no marker, and the old one + /// stays stale so the sweep rebuilds (dashpay/platform#4302 review). + #[tokio::test] + async fn a_rejected_rotation_on_a_non_atomic_host_leaves_no_current_marker_on_disk() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::network::contacts::ExternalAccountRegistration; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + let payload_old = encrypted_contact_xpub(&owner, &contact, &shared_key); + let payload_new = + encrypted_contact_xpub(&owner, &Identifier::from([0x02; 32]), &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + let establish = |reference: u32, payload: Vec| { + let iw = iw.clone(); + let persister = Arc::clone(&persister); + async move { + let p = WalletPersister::new(wallet_id, persister as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + if info.identity_manager.managed_identity(&owner).is_none() { + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + } + let managed = info + .identity_manager + .managed_identity_mut(&owner) + .expect("managed"); + let marker = managed + .dashpay() + .established_contacts() + .get(&contact) + .and_then(|c| c.external_account_reference); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = + ContactRequest::new(contact, owner, 0, 0, reference, payload, 100, 0); + let mut established = EstablishedContact::new(contact, outgoing, incoming); + established.external_account_reference = marker; + managed.apply_established_contact(established); + } + }; + let register = |payload: Vec| { + let iw = iw.clone(); + async move { + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &payload, + zeroize::Zeroizing::new(shared_key), + ) + .await + } + }; + + // Built on an atomic host from request 7: account and marker together. + establish(7, payload_old.clone()).await; + assert_eq!( + register(payload_old).await.expect("first build"), + ExternalAccountRegistration::Built + ); + + // The contact rotates to request 8 and the sweep tears the account + // down in memory; the host still holds the old one. The host now + // has no atomic rounds and rejects the rebuilt account and pool. + establish(8, payload_new.clone()).await; + { + let key = DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }; + let mut wm = manager.wallet_manager.write().await; + let (wallet, info) = wm.get_wallet_mut_and_info_mut(&wallet_id).expect("wallet"); + wallet.accounts.dashpay_external_accounts.remove(&key); + info.core_wallet + .accounts + .dashpay_external_accounts + .remove(&key); + } + *persister.non_atomic.lock().unwrap() = true; + *persister.reject_account_rounds.lock().unwrap() = true; + persister.stores.lock().unwrap().clear(); + assert!( + register(payload_new).await.is_err(), + "the host rejected the account" + ); + + let reached_disk = persister + .stores + .lock() + .unwrap() + .iter() + .filter_map(|(_, cs)| cs.contacts.as_ref()) + .flat_map(|contacts| contacts.established.values()) + .any(|established| established.external_account_reference == Some(8)); + assert!( + !reached_disk, + "the live request's marker must not reach disk beside the previous account" + ); + let wm = manager.wallet_manager.read().await; + let established = wm + .get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone(); + assert_eq!(established.external_account_reference, Some(7)); + assert!( + external_account_needs_rebuild(&established, true), + "the stale marker keeps the repair armed" + ); + } + /// A restored outbound-account marker suppresses the startup rebuild, so /// it must not vouch for a pool its account's xpub does not derive. A /// host that replaced the xpub on rotation but kept the old pool's rows From cd17c61ce58a0668220d7288ffb89918e1466f4d Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Mon, 12 Oct 2026 00:17:58 -0700 Subject: [PATCH 31/43] fix(platform-wallet): keep outbound-account markers in memory unless the account is durably written with them A restored outbound-account marker suppresses the cold-start rebuild of the contact's outbound account, so it may only reach disk where the account it names is durably written with it. That was not guaranteed on a buffering backend, which can accept the account and marker and drop both at a failed flush while a later contact snapshot re-sends the marker alone; nor on an FFI host without account-write callbacks, or a JNI bridge inheriting the no-op account methods, which acknowledge an account they never keep. - persists_outbound_account_markers(persister): store commits inline and the backend attests ATOMIC_CHANGESETS and ACCOUNT_ADDRESS_POOLS. The latter is derived by FFIPersister only from both account-write slots. - WalletPersister::store writes every established-contact row without its marker on any other backend. The in-memory marker is kept, so the running process keeps its rotation self-heal. - register_external_contact_account binds the marker in the account's round only under that check. - Loading on any other backend clears restored markers, so the sweep rebuilds each outbound account once, as before the marker was persisted. - The JNI layer leaves the account-registration and address-pool slots unwired when the bridge does not override them, so an inherited no-op no longer derives ACCOUNT_ADDRESS_POOLS. Regressions: a buffered backend whose flush fails and a backend without an account writer each register, stamp and flush, with no marked row on disk and no trusted marker after a cold reload; each guard's removal fails the test. The FFI and JNI tests pin the capability derivation and the slot gating. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/persistence.rs | 50 ++++++ .../rs-platform-wallet/src/changeset/mod.rs | 3 +- .../src/changeset/traits.rs | 26 +++ .../rs-platform-wallet/src/manager/load.rs | 40 +++++ .../src/wallet/identity/network/contacts.rs | 21 ++- .../src/wallet/identity/network/payments.rs | 153 +++++++++++++++++- .../src/wallet/persister.rs | 27 +++- .../rs-unified-sdk-jni/src/persistence.rs | 90 ++++++++++- .../rs-unified-sdk-jni/src/wallet_manager.rs | 7 +- 9 files changed, 393 insertions(+), 24 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index dd41d5c2750..b0ec421b8bf 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -8974,6 +8974,56 @@ mod tests { } } + /// An atomic FFI host keeps outbound-account markers on disk only when it + /// also wires both account writers: `ACCOUNT_ADDRESS_POOLS` is derived + /// from them, and a host without them would acknowledge an account it + /// never stored beside a marker naming it (dashpay/platform#4302 review). + #[test] + fn outbound_markers_need_an_atomic_host_with_account_writers() { + use platform_wallet::changeset::persists_outbound_account_markers; + + unsafe extern "C" fn begin(_: *mut c_void, _: *const u8) -> i32 { + 0 + } + unsafe extern "C" fn end(_: *mut c_void, _: *const u8, _: bool) -> i32 { + 0 + } + unsafe extern "C" fn accounts( + _: *mut c_void, + _: *const u8, + _: *const AccountSpecFFI, + _: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn pools( + _: *mut c_void, + _: *const u8, + _: *const AccountAddressPoolFFI, + _: usize, + ) -> i32 { + 0 + } + let declared = PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS); + let host = |registrations: bool, address_pools: bool| { + FFIPersister::new_with_persistence_capabilities( + PersistenceCallbacks { + on_changeset_begin_fn: Some(begin), + on_changeset_end_fn: Some(end), + on_persist_account_registrations_fn: registrations.then_some(accounts as _), + on_persist_account_address_pools_fn: address_pools.then_some(pools as _), + ..PersistenceCallbacks::default() + }, + declared, + ) + }; + assert!(!persists_outbound_account_markers(&host(false, false))); + assert!(!persists_outbound_account_markers(&host(true, false))); + assert!(!persists_outbound_account_markers(&host(false, true))); + assert!(persists_outbound_account_markers(&host(true, true))); + } + /// Why the outbound-account marker may ride an account's round only on /// an atomic host: without begin/end brackets `FFIPersister` cannot claim /// `ATOMIC_CHANGESETS` even when the host declares it, and it carries on diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 8680e44f4c4..f246d3f6056 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -65,5 +65,6 @@ pub use shielded_changeset_disabled::ShieldedChangeSet; #[cfg(feature = "shielded")] pub use shielded_sync_start_state::{ShieldedSubwalletStartState, ShieldedSyncStartState}; pub use traits::{ - ListedCoreTxid, PersistenceError, PersistenceErrorKind, PlatformWalletPersistence, + persists_outbound_account_markers, ListedCoreTxid, PersistenceError, PersistenceErrorKind, + PlatformWalletPersistence, }; diff --git a/packages/rs-platform-wallet/src/changeset/traits.rs b/packages/rs-platform-wallet/src/changeset/traits.rs index 8e57004ba3f..11e5d2c58a7 100644 --- a/packages/rs-platform-wallet/src/changeset/traits.rs +++ b/packages/rs-platform-wallet/src/changeset/traits.rs @@ -172,6 +172,32 @@ impl PersistenceError { } } +/// Whether a contact's outbound-account marker +/// (`EstablishedContact::external_account_reference`) can be kept on +/// `persister` and trusted after a restart. +/// +/// A restored marker suppresses the cold-start rebuild of the contact's +/// outbound account, so it may only reach disk where the account it names is +/// durably written with it: a backend whose `store` is the commit +/// ([`store_commits_inline`](PlatformWalletPersistence::store_commits_inline)), +/// whose rounds are all-or-nothing +/// ([`ATOMIC_CHANGESETS`](PersistenceCapabilities::ATOMIC_CHANGESETS)), and +/// that persists account registrations and their address pools +/// ([`ACCOUNT_ADDRESS_POOLS`](PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS)). +/// On any other backend the marker is kept in memory only: contact rows are +/// written without it and a restored one is not trusted, so each launch +/// rebuilds the account once — the behaviour before the marker was persisted. +pub fn persists_outbound_account_markers

(persister: &P) -> bool +where + P: PlatformWalletPersistence + ?Sized, +{ + persister.store_commits_inline() + && persister.persistence_capabilities().contains( + PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS), + ) +} + /// Storage backend for [`PlatformWalletChangeSet`] deltas. /// /// The persister persists what the changeset carries — nothing more, diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index b6eadf57b98..127e9a6f098 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -257,6 +257,21 @@ impl PlatformWalletManager

{ does not derive; their accounts will be rebuilt" ); } + // A backend that cannot keep markers durably never had one written + // by this process, and one it hands back may name an account it + // did not keep: none is trusted, so the sweep rebuilds each + // outbound account once, as before the marker was persisted. + if !crate::changeset::persists_outbound_account_markers(&*self.persister) { + let cleared = clear_restored_outbound_markers(&mut platform_info); + if cleared > 0 { + tracing::info!( + wallet_id = %hex::encode(expected_wallet_id), + contacts = cleared, + "load: this backend does not keep outbound-account markers durably; \ + restored markers are not trusted" + ); + } + } // Seed the double-spend screen's session memory from the // freshly restored state: it closes the race where SPV's // chainlock dispatcher promotion-evicts a restored spender @@ -1424,6 +1439,31 @@ fn distrust_markers_of_foreign_external_pools( cleared } +/// Clear every restored outbound-account marker on `info`. Returns how many +/// were set. +fn clear_restored_outbound_markers(info: &mut PlatformWalletInfo) -> usize { + let mut cleared = 0; + for owner in info.identity_manager.identity_ids() { + let Some(managed) = info.identity_manager.managed_identity_mut(&owner) else { + continue; + }; + let contacts: Vec = managed + .dashpay() + .established_contacts() + .keys() + .copied() + .collect(); + for contact in contacts { + if let Some(established) = managed.established_contact_mut(&contact) { + if established.external_account_reference.take().is_some() { + cleared += 1; + } + } + } + } + cleared +} + /// Whether every address in `pool` is the one `xpub` derives at its index — /// checked by regenerating a scratch pool with the same layout. fn pool_derives_from(pool: &AddressPool, xpub: &ExtendedPubKey) -> bool { 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 5fc3b8c4533..59042915acc 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs @@ -579,18 +579,15 @@ impl DashPayView<'_, B> { // The payload must still be the contact's live request. The durable // marker is bound to it in the same round as the account only where - // that round is all-or-nothing: an inline store that attests atomic - // changesets. A backend without that guarantee can reject the account - // and pool yet keep the contact row, leaving the live request's marker - // beside the previous account it still holds — which a restart would - // trust. There the marker keeps its old order: the caller stamps it - // (`note_external_account_registered`) only after this round succeeded - // (dashpay/platform#4302 review). - let binds_marker = self.persister.store_commits_inline() - && self - .persister - .persistence_capabilities() - .contains(crate::changeset::PersistenceCapabilities::ATOMIC_CHANGESETS); + // the backend keeps markers durably at all + // (`persists_outbound_account_markers`: an inline, atomic round that + // writes accounts and pools). A backend without that guarantee could + // reject or drop the account and pool yet keep the contact row, + // leaving the live request's marker beside the previous account — + // which a restart would trust. There the marker stays in memory only: + // the caller stamps it once this round succeeded, and every contact + // row written to that backend omits it (dashpay/platform#4302 review). + let binds_marker = self.persister.persists_outbound_account_markers(); let bound_contact = match info .identity_manager .managed_identity(our_identity_id) diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 96a25ef97ae..e6444e3ab76 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -1917,6 +1917,9 @@ mod tests { fail_flushes: Mutex, /// When set, the backend does not attest `ATOMIC_CHANGESETS`. non_atomic: Mutex, + /// When set, the backend does not attest `ACCOUNT_ADDRESS_POOLS`: it + /// has no account writer (or only a no-op one). + no_account_writer: Mutex, /// When set, a round carrying both a cursor and a backfill record /// keeps the record and then fails on the cursor — the partial write /// a non-atomic backend is allowed to leave. @@ -1938,11 +1941,18 @@ mod tests { } fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { - if *self.non_atomic.lock().unwrap() { - crate::changeset::PersistenceCapabilities::NONE + use crate::changeset::PersistenceCapabilities as C; + let atomic = if *self.non_atomic.lock().unwrap() { + C::NONE } else { - crate::changeset::PersistenceCapabilities::ATOMIC_CHANGESETS - } + C::ATOMIC_CHANGESETS + }; + let accounts = if *self.no_account_writer.lock().unwrap() { + C::NONE + } else { + C::ACCOUNT_ADDRESS_POOLS + }; + atomic.union(accounts) } fn store( @@ -3355,6 +3365,7 @@ mod tests { fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { crate::changeset::PersistenceCapabilities::ATOMIC_CHANGESETS + .union(crate::changeset::PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS) } fn store( @@ -5279,6 +5290,140 @@ mod tests { ); } + /// A marker may reach disk only where the account it names is durably + /// written with it. A buffering backend can accept the account and the + /// marker and then drop both at a failed `flush`, while a later contact + /// snapshot re-sends the marker alone; a backend without an account + /// writer acknowledges the account without keeping it. On either, every + /// contact row is written without the marker and a restored one is not + /// trusted, so the sweep rebuilds the account after a restart instead of + /// pairing the live request's marker with the previous account + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn outbound_markers_stay_in_memory_where_the_account_is_not_durably_written() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::network::contacts::ExternalAccountRegistration; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + + for buffered in [true, false] { + let (manager, persister, wallet_id) = make_wallet().await; + if buffered { + *persister.buffered.lock().unwrap() = true; + *persister.fail_flushes.lock().unwrap() = true; + } else { + *persister.no_account_writer.lock().unwrap() = true; + } + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + let payload = encrypted_contact_xpub(&owner, &contact, &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + { + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = + ContactRequest::new(contact, owner, 0, 0, 8, payload.clone(), 100, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new( + contact, outgoing, incoming, + )); + } + persister.stores.lock().unwrap().clear(); + assert_eq!( + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &payload, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register"), + ExternalAccountRegistration::Built + ); + iw.dashpay() + .note_external_account_registered(&owner, &contact, &payload) + .await; + // A flush on the buffering backend fails and drops the round. + if buffered { + assert!(wallet.flush_persist().is_err()); + } + + // Memory keeps the self-heal marker; no row on disk carries it. + let established = { + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone() + }; + assert_eq!(established.external_account_reference, Some(8)); + let marked_rows = persister + .stores + .lock() + .unwrap() + .iter() + .filter_map(|(_, cs)| cs.contacts.as_ref()) + .flat_map(|contacts| contacts.established.values()) + .filter(|row| row.external_account_reference.is_some()) + .count(); + assert_eq!(marked_rows, 0, "buffered: {buffered}"); + + // A cold reload from such a backend does not trust a marker it + // hands back, even one an earlier configuration wrote. + let snapshot = Arc::new(reload_snapshot(&manager, wallet_id, owner, 1_000, None).await); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let restarted = if buffered { + let host = Arc::new(BufferedHost { + inner: Arc::clone(&snapshot), + fail_flushes: std::sync::atomic::AtomicBool::new(false), + }); + let manager = Arc::new(PlatformWalletManager::new(sdk, host, handler)); + manager.load_from_persistor().await.expect("load"); + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone() + } else { + let host = Arc::new(NonAtomicHost(Arc::clone(&snapshot))); + let manager = Arc::new(PlatformWalletManager::new(sdk, host, handler)); + manager.load_from_persistor().await.expect("load"); + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone() + }; + assert_eq!( + restarted.external_account_reference, None, + "buffered: {buffered}" + ); + assert!(external_account_needs_rebuild(&restarted, true)); + } + } + /// A restored outbound-account marker suppresses the startup rebuild, so /// it must not vouch for a pool its account's xpub does not derive. A /// host that replaced the xpub on rotation but kept the old pool's rows diff --git a/packages/rs-platform-wallet/src/wallet/persister.rs b/packages/rs-platform-wallet/src/wallet/persister.rs index f22e88d3d90..56a28fa4370 100644 --- a/packages/rs-platform-wallet/src/wallet/persister.rs +++ b/packages/rs-platform-wallet/src/wallet/persister.rs @@ -63,10 +63,35 @@ impl WalletPersister { Self { wallet_id, inner } } - pub(crate) fn store(&self, changeset: PlatformWalletChangeSet) -> Result<(), PersistenceError> { + /// Store `changeset` for this wallet. + /// + /// On a backend that cannot keep outbound-account markers durably (see + /// [`persists_outbound_account_markers`](crate::changeset::persists_outbound_account_markers)), + /// every established-contact row is written without its marker: a marker + /// that reached disk without its account — through a buffered round that + /// was later dropped, or a host that skips account writes — would be + /// trusted on restart beside the previous account. The in-memory marker is + /// untouched, so the running process keeps its rotation self-heal. + pub(crate) fn store( + &self, + mut changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + if !self.persists_outbound_account_markers() { + if let Some(contacts) = changeset.contacts.as_mut() { + for established in contacts.established.values_mut() { + established.external_account_reference = None; + } + } + } self.inner.store(self.wallet_id, changeset) } + /// Whether the backend keeps outbound-account markers durably (see + /// [`persists_outbound_account_markers`](crate::changeset::persists_outbound_account_markers)). + pub(crate) fn persists_outbound_account_markers(&self) -> bool { + crate::changeset::persists_outbound_account_markers(self.inner.as_ref()) + } + /// Preserve backend failure kinds, granting retry only when the backend /// guarantees a failed store retained and committed nothing. pub(crate) fn classify_store_failure(&self, error: PersistenceError) -> PlatformWalletError { diff --git a/packages/rs-unified-sdk-jni/src/persistence.rs b/packages/rs-unified-sdk-jni/src/persistence.rs index c726011cd2a..054d05c51da 100644 --- a/packages/rs-unified-sdk-jni/src/persistence.rs +++ b/packages/rs-unified-sdk-jni/src/persistence.rs @@ -247,6 +247,49 @@ pub(crate) fn build_extension(env: &mut JNIEnv, bridge: &JObject) -> Persistence }) } +/// Leave the account-registration and address-pool slots of `vtable` unwired +/// when the concrete `bridge` inherits `NativePersistenceBridge`'s +/// success-returning no-op for them (see [`bridge_overrides`]). +/// +/// Those slots are how `FFIPersister` derives `ACCOUNT_ADDRESS_POOLS`, and a +/// contact's outbound-account marker is kept on disk only where that +/// capability holds +/// ([`persists_outbound_account_markers`](platform_wallet::changeset::persists_outbound_account_markers)). +/// An inherited no-op would acknowledge an account it never stored, so the +/// marker naming that account could be trusted after a restart beside the +/// previous one (dashpay/platform#4302 review). Unwired, the capability is not +/// derived, account writes are reported as unsupported rather than silently +/// dropped, and markers stay in memory. +pub(crate) fn restrict_account_writers( + env: &mut JNIEnv, + bridge: &JObject, + vtable: &mut PersistenceCallbacks, +) { + let registrations = bridge_overrides( + env, + bridge, + "onPersistAccountRegistration", + ACCOUNT_REGISTRATION_DESCRIPTOR, + ); + let pools = bridge_overrides( + env, + bridge, + "onPersistAccountAddressPoolEntry", + ACCOUNT_ADDRESS_POOL_ENTRY_DESCRIPTOR, + ); + gate_account_writers(vtable, registrations, pools); +} + +/// The slot half of [`restrict_account_writers`]. +fn gate_account_writers(vtable: &mut PersistenceCallbacks, registrations: bool, pools: bool) { + if !registrations { + vtable.on_persist_account_registrations_fn = None; + } + if !pools { + vtable.on_persist_account_address_pools_fn = None; + } +} + /// Which optional `NativePersistenceBridge` callbacks the concrete bridge /// supplies its own body for (see [`bridge_overrides`]). #[derive(Debug, Clone, Copy)] @@ -705,7 +748,7 @@ fn call_persist_account_registration( env.call_method( bridge, "onPersistAccountRegistration", - "([BBBIII[B[B[B)I", + ACCOUNT_REGISTRATION_DESCRIPTOR, &[ wid.into(), JValue::Byte(s.type_tag as i8), @@ -749,7 +792,7 @@ unsafe extern "C" fn tramp_persist_account_address_pools( env.call_method( bridge, "onPersistAccountAddressPoolEntry", - "([BBBIII[B[BB[BZBIZJLjava/lang/String;Ljava/lang/String;)I", + ACCOUNT_ADDRESS_POOL_ENTRY_DESCRIPTOR, &[ (&wid).into(), JValue::Byte(spec.type_tag as i8), @@ -901,6 +944,15 @@ const WALLET_CHANGESET_SWEEPS_DESCRIPTOR: &str = "([B[BI[B[BIZI)I"; /// backfill slot on it, so the three cannot drift apart. const WALLET_CHANGESET_HEADER_DESCRIPTOR: &str = "([BZIZJJJJ[B)I"; +/// `onPersistAccountRegistration`'s descriptor, shared by the `call_method` +/// site, the descriptor table and the override probe in +/// [`restrict_account_writers`]. +const ACCOUNT_REGISTRATION_DESCRIPTOR: &str = "([BBBIII[B[B[B)I"; + +/// `onPersistAccountAddressPoolEntry`'s descriptor, shared the same way. +const ACCOUNT_ADDRESS_POOL_ENTRY_DESCRIPTOR: &str = + "([BBBIII[B[BB[BZBIZJLjava/lang/String;Ljava/lang/String;)I"; + unsafe fn persist_changeset_sweep_batch( env: &mut JNIEnv, bridge: &JObject, @@ -4812,10 +4864,13 @@ const BRIDGE_METHOD_TABLE: &[(&str, &str)] = &[ ("onPersistAddressBalance", "([BB[BJIIIJ)I"), ("onPersistSyncState", "([BJJJ)I"), ("onPersistWalletMetadata", "([BI[BI)I"), - ("onPersistAccountRegistration", "([BBBIII[B[B[B)I"), + ( + "onPersistAccountRegistration", + ACCOUNT_REGISTRATION_DESCRIPTOR, + ), ( "onPersistAccountAddressPoolEntry", - "([BBBIII[B[BB[BZBIZJLjava/lang/String;Ljava/lang/String;)I", + ACCOUNT_ADDRESS_POOL_ENTRY_DESCRIPTOR, ), ( "onWalletChangesetHeader", @@ -5086,6 +5141,33 @@ mod tests { assert!(sweeps_only.on_persist_wallet_dashpay_backfill_fn.is_none()); } + /// A bridge inheriting the no-op account writers gets neither account + /// slot, so `FFIPersister` cannot derive `ACCOUNT_ADDRESS_POOLS` and does + /// not keep outbound-account markers on disk; one implementing both keeps + /// both slots. + #[test] + fn account_writer_slots_follow_the_bridges_overrides() { + for (registrations, pools) in [(false, false), (true, false), (false, true), (true, true)] { + let mut vtable = build_vtable(std::ptr::null_mut()); + gate_account_writers(&mut vtable, registrations, pools); + assert_eq!( + vtable.on_persist_account_registrations_fn.is_some(), + registrations + ); + assert_eq!(vtable.on_persist_account_address_pools_fn.is_some(), pools); + // `release_fn` must not run on the null context when this drops. + vtable.release_fn = None; + } + let names = |names: &[&str]| names.iter().map(|n| n.to_string()).collect::>(); + assert_eq!( + method_descriptor( + &names(&["[B", "byte", "byte", "int", "int", "int", "[B", "[B", "[B"]), + "int" + ), + ACCOUNT_REGISTRATION_DESCRIPTOR + ); + } + /// The probe has to recognise a real `onWalletChangesetDashPayBackfill` /// override by the descriptor the trampoline calls. #[test] diff --git a/packages/rs-unified-sdk-jni/src/wallet_manager.rs b/packages/rs-unified-sdk-jni/src/wallet_manager.rs index 10396a29939..99eba41dbd0 100644 --- a/packages/rs-unified-sdk-jni/src/wallet_manager.rs +++ b/packages/rs-unified-sdk-jni/src/wallet_manager.rs @@ -41,7 +41,9 @@ #![allow(clippy::missing_safety_doc)] use crate::events::{build_event_extension, build_event_vtable, KotlinEventCtx}; -use crate::persistence::{build_extension, build_vtable, KotlinPersistenceCtx}; +use crate::persistence::{ + build_extension, build_vtable, restrict_account_writers, KotlinPersistenceCtx, +}; use crate::support::{guard, take_pwffi_error, throw_pwffi_result, throw_sdk_exception}; use jni::objects::{JByteArray, JClass, JObject, JObjectArray, JString, JValue}; use jni::sys::{ @@ -154,7 +156,8 @@ pub extern "system" fn Java_org_dashfoundation_dashsdk_ffi_WalletManagerNative_n }; let persistence_ctx = Box::into_raw(Box::new(KotlinPersistenceCtx::new(persistence_global))); - let persistence: PersistenceCallbacks = build_vtable(persistence_ctx as *mut c_void); + let mut persistence: PersistenceCallbacks = build_vtable(persistence_ctx as *mut c_void); + restrict_account_writers(env, &persistence_bridge, &mut persistence); let persistence_extension = build_extension(env, &persistence_bridge); let persistence_capabilities = PersistenceCapabilitiesFFI { version: declared_capabilities_version, From fd21b6f8e772e5e8c01046f53be2ed6624d477f7 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Mon, 12 Oct 2026 01:01:09 -0700 Subject: [PATCH 32/43] fix(platform-wallet): drop loaded backfill coverage for receival accounts the wallet did not restore Load passed the restored record to settle_published_backfill unchanged. A host that kept the record but not the account it covers, with an account writer missing or failing, would hand back coverage for account A with no A to watch. The scan could advance before A was registered again, and the old coverage would then accept A over a range scanned without it. - retain_coverage_for_held_accounts is now shared by both publish paths. Load applies it to the receival accounts it actually restored and passes the drop to settle_published_backfill, so the host's record is replaced before the wallet is published, as registration already did. - persists_backfill_coverage also requires ACCOUNT_ADDRESS_POOLS: a backend that cannot keep the receival accounts cannot keep coverage about them. The in-tree Android handler attests it. Regression: a load whose host holds A's coverage but not A replaces the host record with an empty one, the scan advances, A is registered again, and the reconcile rewinds for it. It fails without the load-side filter. Co-Authored-By: Claude Opus 5.5 --- .../src/changeset/dashpay_backfill.rs | 12 ++-- .../rs-platform-wallet/src/manager/load.rs | 10 ++- .../rs-platform-wallet/src/manager/mod.rs | 35 ++++++++++ .../src/manager/wallet_lifecycle.rs | 23 ++---- .../src/wallet/identity/network/payments.rs | 70 +++++++++++++++++++ 5 files changed, 126 insertions(+), 24 deletions(-) diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 1053c5a312e..3ec8bea51b5 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -146,7 +146,10 @@ pub struct DashPayBackfillRecord { /// Whether `persister` can hold a durable [`DashPayBackfillRecord`]. /// /// The backend has to store the record at all -/// ([`stores_dashpay_backfill`](PlatformWalletPersistence::stores_dashpay_backfill)). +/// ([`stores_dashpay_backfill`](PlatformWalletPersistence::stores_dashpay_backfill)), +/// and the receival accounts the coverage is about +/// ([`ACCOUNT_ADDRESS_POOLS`](PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS)): +/// coverage restored without its account vouches for scripts nothing watches. /// The record vouches for the cursor written in the same round, so both /// halves have to land together or not at all, and be on disk once `store` /// returns. That takes a backend whose `store` is the commit @@ -161,9 +164,10 @@ where { persister.stores_dashpay_backfill() && persister.store_commits_inline() - && persister - .persistence_capabilities() - .contains(PersistenceCapabilities::ATOMIC_CHANGESETS) + && persister.persistence_capabilities().contains( + PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS), + ) } /// The persisted fields of a [`DashPayBackfillRecord`] as serde reads them, diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 127e9a6f098..580f09da909 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -348,11 +348,19 @@ impl PlatformWalletManager

{ { continue 'load; } + // Coverage for a receival account this wallet did not restore + // vouches for scripts nothing will watch; it is dropped here, + // and from the host before the wallet is published, exactly + // as registration does (dashpay/platform#4302 review). + let dropped = super::retain_coverage_for_held_accounts( + &mut platform_info.dashpay_backfill, + &platform_info.core_wallet, + ); match self.settle_published_backfill( &mut durable_cursors, wallet_id, std::mem::take(&mut platform_info.dashpay_backfill), - false, + dropped, loaded_cursor, loaded_cursor, ) { diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index bf511337f38..e9977412cd5 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -23,6 +23,8 @@ use tokio::sync::{Notify, RwLock}; use tokio::task::JoinHandle; use tokio_util::sync::CancellationToken; +use dpp::prelude::Identifier; +use key_wallet::wallet::ManagedWalletInfo; use key_wallet_manager::WalletManager; use crate::changeset::{ @@ -476,6 +478,39 @@ pub struct PlatformWalletManager { >, } +/// Keep `record`'s coverage only for the DashPay receival accounts `core` +/// actually holds, and report whether anything was dropped. +/// +/// Coverage vouches that an account's scripts were watched; for an account +/// the published wallet does not hold, nothing watches them, and a scan the +/// wallet commits before the account is registered again would sit inside +/// coverage that never saw it. Both publish paths apply this before +/// [`PlatformWalletManager::settle_published_backfill`], which replaces the +/// host's record when it reports a drop. Dropping the last entry also drops +/// the extent: nothing is left for it to describe. +pub(crate) fn retain_coverage_for_held_accounts( + record: &mut DashPayBackfillRecord, + core: &ManagedWalletInfo, +) -> bool { + let held: Vec<(Identifier, Identifier, u32)> = core + .accounts + .dashpay_receival_accounts + .keys() + .map(|key| { + ( + Identifier::from(key.user_identity_id), + Identifier::from(key.friend_identity_id), + key.index, + ) + }) + .collect(); + let dropped = record.retain(|owner, contact, index| held.contains(&(*owner, *contact, index))); + if dropped && record.is_empty() { + *record = DashPayBackfillRecord::default(); + } + dropped +} + impl PlatformWalletManager

{ /// Create a new PlatformWalletManager. /// diff --git a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs index a79e0d54144..9366ca45d7d 100644 --- a/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs +++ b/packages/rs-platform-wallet/src/manager/wallet_lifecycle.rs @@ -19,7 +19,6 @@ use crate::error::PlatformWalletError; use crate::wallet::core::WalletGeneration; use crate::wallet::platform_wallet::{PlatformWalletInfo, WalletId}; use crate::wallet::PlatformWallet; -use dpp::prelude::Identifier; use super::PlatformWalletManager; @@ -519,24 +518,10 @@ impl PlatformWalletManager

{ Some((cursor, record)) => (Some(cursor), record), None => (None, Default::default()), }; - let held: Vec<(Identifier, Identifier, u32)> = platform_info - .core_wallet - .accounts - .dashpay_receival_accounts - .keys() - .map(|key| { - ( - Identifier::from(key.user_identity_id), - Identifier::from(key.friend_identity_id), - key.index, - ) - }) - .collect(); - let dropped = - host_backfill.retain(|owner, contact, index| held.contains(&(*owner, *contact, index))); - if dropped && host_backfill.is_empty() { - host_backfill = Default::default(); - } + let dropped = super::retain_coverage_for_held_accounts( + &mut host_backfill, + &platform_info.core_wallet, + ); let wallet_id = { let mut durable_cursors = self.durable_cursors.lock().await; if self diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index e6444e3ab76..a1aa38194c5 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -4747,6 +4747,76 @@ mod tests { .await; } + /// Load trusts restored coverage only for the receival accounts it + /// actually restored. A host that kept the record but not the account + /// (its account writer missing or failing) would otherwise hand back + /// coverage for account A beside cursor 1000 with no A to watch; the scan + /// can then advance to 1500 before A is registered again, and the old + /// coverage would accept A over a range scanned without it. The load drops + /// that coverage, from the host too, before publishing + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn loading_drops_coverage_for_a_receival_account_the_wallet_did_not_restore() { + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let mut host_record = crate::changeset::DashPayBackfillRecord::default(); + host_record.record_pass(1_000, Some(200), [(owner, contact, 0, 200)]); + + // The host restores the wallet without A's receival account. + let (donor, _, wallet_id) = make_wallet().await; + let (wallet, mut managed) = { + let wm = donor.wallet_manager.read().await; + ( + wm.get_wallet(&wallet_id).expect("wallet").clone(), + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .clone(), + ) + }; + drop(donor); + managed.metadata.synced_height = 1_000; + let host = Arc::new(ReloadPersister { + wallet, + managed, + identities: std::collections::BTreeMap::new(), + backfill: host_record, + stores: Mutex::default(), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + manager + .load_from_persistor() + .await + .expect("the snapshot must load"); + + first_store_is_an_empty_record(&host.stores.lock().unwrap()); + { + let wm = manager.wallet_manager.read().await; + assert!(wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .is_empty()); + } + + // The scan advances without A; then A is registered again. + accept_successor_advance(&manager, wallet_id, 1_500).await; + let loaded = manager.get_wallet(&wallet_id).await.expect("wallet"); + establish_on_recreated(&manager, &host, &loaded, owner, contact, 200).await; + assert_eq!( + loaded + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("reconcile"), + Some(200), + "A is rewound for: 1001..1500 was scanned without it" + ); + } + /// The load path's start state is just as old. A predecessor can register /// a receival account the snapshot does not hold, store its coverage /// beside a rewind to 100 and be removed before the load publishes. The From 49aab2ce66e9337c52478de49c573be5b20b4fea Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Mon, 12 Oct 2026 02:35:00 -0700 Subject: [PATCH 33/43] fix(platform-wallet): make registration the only writer of a durable outbound marker note_external_account_registered, which both registration callers await after a build, could still stamp the live request's reference in a contact-only round. Its ciphertext check does not tie the installed account to that request: a rotation may reuse the ciphertext under a new reference. A completion arriving after the account was torn down could then stamp the new reference beside the obsolete durable account, and a restart would trust the pairing. - On a backend that keeps markers on disk, registration binds the marker in the account's own round and is its only writer. Completion now only clears payment_channel_broken, and only while the bound marker is still the live reference. Backends that keep markers in memory only keep the previous stamp-on-completion behaviour. - DashPayBackfillRecord's cover set is now private behind a read-only covered() accessor, so the sorted, one-per-account invariant its binary search relies on cannot be broken from outside the type. Regressions: a late completion after a ciphertext-reusing rotation stamps nothing, keeps the stale marker through a cold reload, and still heals the broken flag while the marker is current; it fails without the guard. A gated-store test holds one registration inside its durable write, shows the manager stays locked meanwhile, and has a competing registration that passed its early check return AlreadyExisted without writing; it fails without the under-lock absence re-check. Co-Authored-By: Claude Opus 5.5 --- .../src/core_wallet_types.rs | 2 +- .../src/changeset/dashpay_backfill.rs | 12 +- .../rs-platform-wallet/src/manager/mod.rs | 2 +- .../identity/network/contact_requests.rs | 17 +- .../src/wallet/identity/network/payments.rs | 274 +++++++++++++++++- 5 files changed, 299 insertions(+), 8 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs index c8b68c8ad65..b7cbf4c7817 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs @@ -141,7 +141,7 @@ pub fn build_dashpay_backfill_covered_for_callback( record: &platform_wallet::changeset::DashPayBackfillRecord, ) -> Vec { record - .covered + .covered() .iter() .map(|entry| DashPayBackfillCoveredContactFFI { owner_identity_id: entry.owner.to_buffer(), diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 3ec8bea51b5..889a13d8c4f 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -118,8 +118,10 @@ pub struct DashPayBackfillRecord { /// rewound anything ([`has_extent`](Self::has_extent)). pub rewound_from: u32, /// Receival accounts the backfill covers, sorted by `(owner, contact, - /// account_index)`, at most one entry per account. - pub covered: Vec, + /// account_index)`, at most one entry per account. Private so nothing + /// outside this type can break that order, which `covered_from`'s binary + /// search relies on; read it through [`covered`](Self::covered). + covered: Vec, /// In-memory only, never persisted or crossed over the FFI: a cursor a /// previous record round still owes the host because that round's /// store failed. The next successful record round carries it as the @@ -245,6 +247,12 @@ impl DashPayBackfillRecord { paid } + /// The covered receival accounts, sorted by `(owner, contact, + /// account_index)`, at most one entry per account. + pub fn covered(&self) -> &[DashPayBackfillCoveredContact] { + &self.covered + } + /// Whether no backfill has ever been recorded for this wallet. pub fn is_empty(&self) -> bool { self.covered.is_empty() diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index e9977412cd5..419aab9ea30 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -752,7 +752,7 @@ impl PlatformWalletManager

{ let cursor = (!record.is_empty() && own_cursor < host_cursor).then_some(own_cursor); tracing::info!( wallet_id = %hex::encode(wallet_id), - covered = record.covered.len(), + covered = record.covered().len(), dropped, predecessor, durable, 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 35586a83bfd..35a5e35e3db 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 @@ -3214,7 +3214,10 @@ impl DashPayView<'_, B> { /// /// - Stamping `external_account_reference` lets the sweep's rotation /// self-heal skip a healthy account (marker matches the tracked - /// reference) and detect a stale one after a restart (F2). + /// reference) and detect a stale one after a restart (F2). On a + /// backend that keeps markers on disk the registration round already + /// bound it, and this only clears the broken flag while that marker is + /// still current; it never stamps one there. /// - Clearing `payment_channel_broken` heals a channel that a prior /// permanent-fault marked broken but that a successful re-register (via /// the drain or a user re-accept) proves is usable again — otherwise the @@ -3262,6 +3265,18 @@ impl DashPayView<'_, B> { } let current_reference = contact.incoming_request.account_reference; let already_current = contact.external_account_reference == Some(current_reference); + // Where markers are kept on disk, registration is their only writer: + // it binds the marker in the account's own round, to the request the + // account was built from. A completion arriving late — after the + // account was torn down and rebuilt, or from a request that reused + // the same ciphertext under another reference — could otherwise + // stamp the live reference beside an obsolete durable account, and a + // restart would trust the pairing. So it only heals the broken flag, + // and only while the marker registration bound is still the live one + // (dashpay/platform#4302 review). + if self.persister.persists_outbound_account_markers() && !already_current { + return; + } if already_current && !contact.payment_channel_broken { return; } diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index a1aa38194c5..385f649653b 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -280,7 +280,7 @@ impl DashPayView<'_, B> { // a record-only round below; otherwise there is nothing to store. if to_mark.is_empty() { let record = &info.dashpay_backfill; - let prunes = record.covered.iter().any(|entry| { + let prunes = record.covered().iter().any(|entry| { !receival_accounts.contains(&(entry.owner, entry.contact, entry.account_index)) }); let owes = record.unpersisted_cursor.is_some() || record.unpersisted_extent.is_some(); @@ -1929,6 +1929,11 @@ mod tests { /// what `FFIPersister` does on a host without atomic rounds, which /// carries on to later callbacks after one fails. reject_account_rounds: Mutex, + /// When set, the next round carrying an account registration raises + /// `gate_entered` and then blocks until the sender side is used or + /// dropped — a slow backend held mid-write. + account_gate: Mutex>>, + gate_entered: std::sync::atomic::AtomicBool, } impl PlatformWalletPersistence for RecordingPersister { @@ -1966,6 +1971,14 @@ mod tests { if std::mem::take(&mut *self.panic_next_store.lock().unwrap()) { panic!("recording persister: injected store panic"); } + if !changeset.account_registrations.is_empty() { + let gate = self.account_gate.lock().unwrap().take(); + if let Some(gate) = gate { + self.gate_entered + .store(true, std::sync::atomic::Ordering::SeqCst); + let _ = gate.recv(); + } + } if *self.reject_account_rounds.lock().unwrap() && !changeset.account_registrations.is_empty() { @@ -3728,7 +3741,7 @@ mod tests { .dashpay_backfill .clone() }; - assert_eq!(record.covered.len(), 1); + assert_eq!(record.covered().len(), 1); // The newcomer's account exists on this device by the time of the // relaunch, but the record on disk predates it. @@ -3830,7 +3843,7 @@ mod tests { last_stored_record(&stores) .and_then(|cs| cs.dashpay_backfill.as_ref()) .expect("a record round") - .covered + .covered() .len(), 2, "both accounts reach the host" @@ -5232,6 +5245,101 @@ mod tests { assert_eq!(established.external_account_reference, Some(8)); } + /// Outbound registrations of one account are serialized: the durable + /// write runs under the wallet-manager write guard, so nothing else can + /// take the manager while it is held, and a competing registration that + /// passed its early read-guard check before the account existed finds it + /// under the guard and writes nothing (dashpay/platform#4302 review). + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + async fn competing_outbound_registrations_are_serialized_under_the_write_guard() { + use crate::wallet::identity::network::contacts::ExternalAccountRegistration; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use std::sync::atomic::Ordering; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + let payload = encrypted_contact_xpub(&owner, &contact, &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + { + let p = WalletPersister::new(wallet_id, Arc::clone(&persister) as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = ContactRequest::new(contact, owner, 0, 0, 7, payload.clone(), 100, 0); + info.identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .apply_established_contact(EstablishedContact::new(contact, outgoing, incoming)); + } + let (release, gate) = std::sync::mpsc::channel::<()>(); + *persister.account_gate.lock().unwrap() = Some(gate); + persister.stores.lock().unwrap().clear(); + + // Both registrations pass the early read-guard check while the + // account is absent: the test holds a read guard, so neither can take + // the write guard yet, and both queue behind it. + let held = manager.wallet_manager.read().await; + let register = || { + let iw = iw.clone(); + let payload = payload.clone(); + tokio::spawn(async move { + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &payload, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register") + }) + }; + let first = register(); + let second = register(); + tokio::time::sleep(std::time::Duration::from_millis(200)).await; + drop(held); + + // One of them holds the write guard inside its durable write. + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + while !persister.gate_entered.load(Ordering::SeqCst) { + assert!( + std::time::Instant::now() < deadline, + "no registration reached its store" + ); + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + assert!( + manager.wallet_manager.try_write().is_err() + && manager.wallet_manager.try_read().is_err(), + "the manager stays locked while the registration's store is in progress" + ); + release.send(()).expect("release the store"); + + let mut outcomes = vec![first.await.expect("first"), second.await.expect("second")]; + outcomes.sort_by_key(|outcome| *outcome == ExternalAccountRegistration::Built); + assert_eq!( + outcomes, + vec![ + ExternalAccountRegistration::AlreadyExisted, + ExternalAccountRegistration::Built + ] + ); + let account_rounds = persister + .stores + .lock() + .unwrap() + .iter() + .filter(|(_, cs)| !cs.account_registrations.is_empty()) + .count(); + assert_eq!(account_rounds, 1, "the losing registration wrote nothing"); + } + /// The marker rides the account's round only where that round is /// all-or-nothing. A host without atomic rounds can reject a rotated /// account and its pool yet keep the round's contact rows; with the @@ -5360,6 +5468,166 @@ mod tests { ); } + /// Where markers are kept on disk, registration is their only writer. A + /// completion that arrives late — after the account was torn down for a + /// rotation whose request reused the same ciphertext under a new + /// reference — must not stamp the new reference beside the obsolete + /// durable account; it only heals the broken flag while the bound marker + /// is still current (dashpay/platform#4302 review). + #[tokio::test] + async fn a_late_completion_never_stamps_a_durable_marker() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + use crate::wallet::identity::network::contacts::ExternalAccountRegistration; + use crate::wallet::identity::{ContactRequest, EstablishedContact}; + use key_wallet::account::account_collection::DashpayAccountKey; + + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0x01; 32]); + let shared_key = [0x55u8; 32]; + let payload = encrypted_contact_xpub(&owner, &contact, &shared_key); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + let iw = wallet.identity(); + let set_request = |reference: u32| { + let iw = iw.clone(); + let persister = Arc::clone(&persister); + let payload = payload.clone(); + async move { + let p = WalletPersister::new(wallet_id, persister as _); + let mut wm = iw.wallet_manager.write().await; + let info = wm.get_wallet_info_mut(&wallet_id).expect("info"); + if info.identity_manager.managed_identity(&owner).is_none() { + info.identity_manager + .add_identity(bare_identity(owner.to_buffer()), 0, wallet_id, &p) + .expect("add owner"); + } + let managed = info + .identity_manager + .managed_identity_mut(&owner) + .expect("managed"); + let marker = managed + .dashpay() + .established_contacts() + .get(&contact) + .and_then(|c| c.external_account_reference); + let outgoing = ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0); + let incoming = + ContactRequest::new(contact, owner, 0, 0, reference, payload, 100, 0); + let mut established = EstablishedContact::new(contact, outgoing, incoming); + established.external_account_reference = marker; + managed.apply_established_contact(established); + } + }; + let established = || { + let manager = Arc::clone(&manager); + async move { + let wm = manager.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone() + } + }; + + // Built from request 7: registration binds the marker. + set_request(7).await; + assert_eq!( + iw.dashpay() + .register_external_contact_account( + &owner, + &bare_identity(contact.to_buffer()), + &payload, + zeroize::Zeroizing::new(shared_key), + ) + .await + .expect("register"), + ExternalAccountRegistration::Built + ); + assert_eq!(established().await.external_account_reference, Some(7)); + + // A rotation to request 8 reuses the ciphertext; the sweep tears the + // account down in memory while the host still holds the old one. + set_request(8).await; + { + let key = DashpayAccountKey { + index: 0, + user_identity_id: owner.to_buffer(), + friend_identity_id: contact.to_buffer(), + }; + let mut wm = manager.wallet_manager.write().await; + let (wallet, info) = wm.get_wallet_mut_and_info_mut(&wallet_id).expect("wallet"); + wallet.accounts.dashpay_external_accounts.remove(&key); + info.core_wallet + .accounts + .dashpay_external_accounts + .remove(&key); + } + persister.stores.lock().unwrap().clear(); + + // The first build's completion arrives now. + iw.dashpay() + .note_external_account_registered(&owner, &contact, &payload) + .await; + let after = established().await; + assert_eq!( + after.external_account_reference, + Some(7), + "the late completion must not stamp request 8" + ); + assert!(external_account_needs_rebuild(&after, true)); + let stamped = persister + .stores + .lock() + .unwrap() + .iter() + .filter_map(|(_, cs)| cs.contacts.as_ref()) + .flat_map(|contacts| contacts.established.values()) + .any(|row| row.external_account_reference == Some(8)); + assert!(!stamped, "nothing naming request 8 reached disk"); + + // Cold restoration keeps the stale marker, so the sweep rebuilds. + let (restarted, _, _) = + reload(reload_snapshot(&manager, wallet_id, owner, 1_000, None).await).await; + let restored = { + let wm = restarted.wallet_manager.read().await; + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .clone() + }; + assert_eq!(restored.external_account_reference, Some(7)); + assert!(external_account_needs_rebuild(&restored, true)); + + // While the bound marker is current, completion still heals the + // broken flag. + set_request(7).await; + { + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .identity_manager + .managed_identity_mut(&owner) + .expect("managed") + .established_contact_mut(&contact) + .expect("contact") + .payment_channel_broken = true; + } + iw.dashpay() + .note_external_account_registered(&owner, &contact, &payload) + .await; + let healed = established().await; + assert!(!healed.payment_channel_broken); + assert_eq!(healed.external_account_reference, Some(7)); + } + /// A marker may reach disk only where the account it names is durably /// written with it. A buffering backend can accept the account and the /// marker and then drop both at a failed `flush`, while a later contact From 3fda8866641454d7fc33756e46f4b41be9bab5c3 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Mon, 12 Oct 2026 04:18:02 -0700 Subject: [PATCH 34/43] fix(platform-wallet): validate restored outbound addresses per row and pin the registration race deterministically The two suggestions from the re-review of 49aab2ce66. - pool_derives_from rebuilt a scratch pool from index 0 to the highest restored index, so one sparse row at a huge index cost that many derivations and allocations at load. It now derives only the indices the pool holds, one at a time along the same relative path and P2PKH encoding key-wallet uses, and an invalid index or failed derivation counts as a mismatch. - The registration-race test relied on a held read guard and a sleep, which did not guarantee both registrations passed the early absence check. A test-only rendezvous, keyed by contact, now holds them between that check and the write-guard acquisition until both arrive, so the loser can only learn the account exists from the re-check under the guard. With the re-check removed the test now fails on every run. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/load.rs | 107 ++++++++++++++---- .../src/wallet/identity/network/contacts.rs | 39 +++++++ .../src/wallet/identity/network/payments.rs | 13 ++- 3 files changed, 133 insertions(+), 26 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 580f09da909..ddbea64c3ed 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -13,9 +13,12 @@ use crate::wallet::PlatformWallet; use std::time::Duration; use crate::broadcaster::{BroadcastError, TransactionBroadcaster}; +use dashcore::Address; use dpp::prelude::Identifier; -use key_wallet::bip32::ExtendedPubKey; -use key_wallet::managed_account::address_pool::{AddressPool, KeySource}; +use key_wallet::bip32::{ChildNumber, DerivationPath, ExtendedPubKey}; +use key_wallet::managed_account::address_pool::{ + AddressPool, AddressPoolType, DerivedKey, KeySource, +}; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; use key_wallet::transaction_checking::transaction_context::TransactionContext; use key_wallet::transaction_checking::wallet_checker::WalletTransactionChecker; @@ -1472,27 +1475,39 @@ fn clear_restored_outbound_markers(info: &mut PlatformWalletInfo) -> usize { cleared } -/// Whether every address in `pool` is the one `xpub` derives at its index — -/// checked by regenerating a scratch pool with the same layout. +/// Whether every address in `pool` is the one `xpub` derives at its index. +/// +/// Only the indices the pool actually holds are derived, one at a time, so +/// the cost follows the restored rows: a sparse row at a huge index costs one +/// derivation, not one per index below it. An index or derivation that fails +/// counts as a mismatch. fn pool_derives_from(pool: &AddressPool, xpub: &ExtendedPubKey) -> bool { - let Some(highest) = pool.highest_generated else { - return true; + let key_source = KeySource::Public(*xpub); + pool.addresses.iter().all(|(index, info)| { + derived_address(pool, &key_source, *index).is_some_and(|address| address == info.address) + }) +} + +/// The address `key_source` derives at `index` in a pool laid out like +/// `pool` — the same relative path and P2PKH encoding key-wallet uses for an +/// ECDSA pool. `None` when the index or the derivation is invalid, or the +/// layout cannot be derived from a public key. +fn derived_address(pool: &AddressPool, key_source: &KeySource, index: u32) -> Option

{ + let child = |i: u32| ChildNumber::from_normal_idx(i).ok(); + let path = match pool.pool_type { + AddressPoolType::External => DerivationPath::from(vec![child(0)?, child(index)?]), + AddressPoolType::Internal => DerivationPath::from(vec![child(1)?, child(index)?]), + AddressPoolType::Absent => DerivationPath::from(vec![child(index)?]), + // A public key cannot derive hardened children. + AddressPoolType::AbsentHardened => return None, }; - let mut derived = AddressPool::new_without_generation( - pool.base_path.clone(), - pool.pool_type, - pool.gap_limit, - pool.network, - ); - if derived - .generate_addresses(highest.saturating_add(1), &KeySource::Public(*xpub), true) - .is_err() - { - return false; + match key_source.derive_at_path(&path).ok()? { + DerivedKey::ECDSA(derived) => Some(Address::p2pkh( + &dashcore::PublicKey::new(derived.public_key), + pool.network, + )), + _ => None, } - pool.addresses - .iter() - .all(|(index, info)| derived.address_at_index(*index).as_ref() == Some(&info.address)) } #[cfg(test)] @@ -1743,4 +1758,56 @@ mod tests { idle adapter holds no strong reference to await" ); } + + /// Restored-address validation costs one derivation per restored row: a + /// single sparse row at index 1,000,000 is checked directly, not by + /// deriving every index below it, and a mismatching one is still caught + /// (dashpay/platform#4302 review). + #[test] + fn restored_address_validation_is_proportional_to_the_restored_rows() { + use key_wallet::bip32::{DerivationPath, ExtendedPrivKey, ExtendedPubKey}; + use key_wallet::managed_account::address_pool::{AddressPool, AddressPoolType, KeySource}; + + let secp = dashcore::secp256k1::Secp256k1::new(); + let xpub = |seed: u8| { + ExtendedPubKey::from_priv( + &secp, + &ExtendedPrivKey::new_master(dashcore::Network::Testnet, &[seed; 32]) + .expect("master"), + ) + }; + let (current, previous) = (xpub(1), xpub(2)); + let mut pool = AddressPool::new_without_generation( + DerivationPath::from(vec![]), + AddressPoolType::Absent, + 20, + dashcore::Network::Testnet, + ); + pool.generate_addresses(1, &KeySource::Public(current), true) + .expect("index 0"); + let template = pool.addresses[&0].clone(); + let sparse = |address| { + let mut pool = pool.clone(); + let mut info = template.clone(); + info.index = 1_000_000; + info.address = address; + pool.addresses.insert(1_000_000, info); + pool.highest_generated = Some(1_000_000); + pool + }; + let at = |key: ExtendedPubKey| { + super::derived_address(&pool, &KeySource::Public(key), 1_000_000).expect("derives") + }; + + let started = std::time::Instant::now(); + assert!(super::pool_derives_from(&sparse(at(current)), ¤t)); + assert!( + !super::pool_derives_from(&sparse(at(previous)), ¤t), + "a row the current xpub does not derive is caught" + ); + assert!( + started.elapsed() < std::time::Duration::from_secs(5), + "validation must not derive every index below the sparse row" + ); + } } 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 59042915acc..46e6eb487c0 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/contacts.rs @@ -551,6 +551,8 @@ impl DashPayView<'_, B> { // typed `insert_funds` API after the upstream split. let managed = key_wallet::managed_account::ManagedCoreFundsAccount::from_account(&account); + #[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) @@ -676,3 +678,40 @@ impl DashPayView<'_, B> { Ok(ExternalAccountRegistration::Built) } } + +/// Test-only rendezvous between [`IdentityWallet::register_external_contact_account`]'s +/// early absence check and its write-guard acquisition, so a test can hold +/// several registrations of one contact at exactly that point and release +/// them together. Keyed by contact id, so registrations of every other +/// contact — and every other test — pass straight through. +#[cfg(test)] +pub(crate) mod registration_rendezvous { + use std::collections::BTreeMap; + use std::sync::{Arc, Mutex}; + + use dpp::prelude::Identifier; + use tokio::sync::Barrier; + + static BARRIERS: Mutex>> = Mutex::new(BTreeMap::new()); + + /// Hold `parties` registrations of `contact` at the rendezvous until all + /// of them have arrived. + pub(crate) fn install(contact: Identifier, parties: usize) { + BARRIERS + .lock() + .unwrap() + .insert(contact, Arc::new(Barrier::new(parties))); + } + + /// Stop holding registrations of `contact`. + pub(crate) fn remove(contact: &Identifier) { + BARRIERS.lock().unwrap().remove(contact); + } + + pub(crate) async fn wait(contact: &Identifier) { + let barrier = BARRIERS.lock().unwrap().get(contact).cloned(); + if let Some(barrier) = barrier { + barrier.wait().await; + } + } +} diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 385f649653b..610232b581c 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -5258,7 +5258,8 @@ mod tests { let (manager, persister, wallet_id) = make_wallet().await; let owner = Identifier::from([0xAA; 32]); - let contact = Identifier::from([0x01; 32]); + // A contact no other test registers, so the rendezvous holds only these. + let contact = Identifier::from([0x7E; 32]); let shared_key = [0x55u8; 32]; let payload = encrypted_contact_xpub(&owner, &contact, &shared_key); let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); @@ -5282,9 +5283,10 @@ mod tests { persister.stores.lock().unwrap().clear(); // Both registrations pass the early read-guard check while the - // account is absent: the test holds a read guard, so neither can take - // the write guard yet, and both queue behind it. - let held = manager.wallet_manager.read().await; + // account is absent, and meet at the rendezvous before either asks + // for the write guard — so the loser can only learn that the account + // exists from the re-check under that guard. + crate::wallet::identity::network::contacts::registration_rendezvous::install(contact, 2); let register = || { let iw = iw.clone(); let payload = payload.clone(); @@ -5302,8 +5304,6 @@ mod tests { }; let first = register(); let second = register(); - tokio::time::sleep(std::time::Duration::from_millis(200)).await; - drop(held); // One of them holds the write guard inside its durable write. let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); @@ -5322,6 +5322,7 @@ mod tests { release.send(()).expect("release the store"); let mut outcomes = vec![first.await.expect("first"), second.await.expect("second")]; + crate::wallet::identity::network::contacts::registration_rendezvous::remove(&contact); outcomes.sort_by_key(|outcome| *outcome == ExternalAccountRegistration::Built); assert_eq!( outcomes, From a8d8dfb48a62f6935439ad431fcb951a87df931b Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Mon, 12 Oct 2026 05:28:11 -0700 Subject: [PATCH 35/43] refactor(platform-wallet): validate restored outbound addresses through key-wallet's own derivation The load-time validator re-implemented key-wallet's per-index relative paths and P2PKH encoding, which could drift from address generation. It now takes the restored account and delegates each restored index to AccountDerivation::derive_address_at, still one derivation per row, and compares by script: the pool watches and pays to the script, and an address encoded for another network still matches its key. A failed derivation counts as a mismatch, and a hardened pool never matches because its children cannot be derived from a public key. The sparse-row regression now takes its expected addresses from key-wallet, so it would catch the validator drifting from the owning implementation. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/load.rs | 115 ++++++++---------- 1 file changed, 54 insertions(+), 61 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index ddbea64c3ed..232c01804f3 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -13,12 +13,9 @@ use crate::wallet::PlatformWallet; use std::time::Duration; use crate::broadcaster::{BroadcastError, TransactionBroadcaster}; -use dashcore::Address; use dpp::prelude::Identifier; -use key_wallet::bip32::{ChildNumber, DerivationPath, ExtendedPubKey}; -use key_wallet::managed_account::address_pool::{ - AddressPool, AddressPoolType, DerivedKey, KeySource, -}; +use key_wallet::account::derivation::AccountDerivation; +use key_wallet::managed_account::address_pool::{AddressPool, AddressPoolType}; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; use key_wallet::transaction_checking::transaction_context::TransactionContext; use key_wallet::transaction_checking::wallet_checker::WalletTransactionChecker; @@ -1429,7 +1426,7 @@ fn distrust_markers_of_foreign_external_pools( .managed_account_type() .address_pools() .into_iter() - .any(|pool| !pool_derives_from(pool, &account.account_xpub)) + .any(|pool| !pool_derives_from(pool, account)) }) }) .map(|(key, _)| *key) @@ -1475,41 +1472,27 @@ fn clear_restored_outbound_markers(info: &mut PlatformWalletInfo) -> usize { cleared } -/// Whether every address in `pool` is the one `xpub` derives at its index. +/// Whether every address in `pool` is the one `account` derives at its index. /// -/// Only the indices the pool actually holds are derived, one at a time, so -/// the cost follows the restored rows: a sparse row at a huge index costs one -/// derivation, not one per index below it. An index or derivation that fails -/// counts as a mismatch. -fn pool_derives_from(pool: &AddressPool, xpub: &ExtendedPubKey) -> bool { - let key_source = KeySource::Public(*xpub); +/// Each restored index is derived on its own through key-wallet's +/// [`AccountDerivation::derive_address_at`] — the derivation that generates +/// the pool — so the cost follows the restored rows (a sparse row at a huge +/// index costs one derivation) and the check cannot drift from address +/// generation. The comparison is by script, which is what the pool watches +/// and pays to, so an address encoded for another network still matches its +/// key. A failed derivation counts as a mismatch, and a hardened pool never +/// matches: its children cannot be derived from a public key. +fn pool_derives_from(pool: &AddressPool, account: &key_wallet::Account) -> bool { + if pool.pool_type == AddressPoolType::AbsentHardened { + return pool.addresses.is_empty(); + } pool.addresses.iter().all(|(index, info)| { - derived_address(pool, &key_source, *index).is_some_and(|address| address == info.address) + account + .derive_address_at(pool.pool_type, *index, None) + .is_ok_and(|address| address.script_pubkey() == info.script_pubkey) }) } -/// The address `key_source` derives at `index` in a pool laid out like -/// `pool` — the same relative path and P2PKH encoding key-wallet uses for an -/// ECDSA pool. `None` when the index or the derivation is invalid, or the -/// layout cannot be derived from a public key. -fn derived_address(pool: &AddressPool, key_source: &KeySource, index: u32) -> Option
{ - let child = |i: u32| ChildNumber::from_normal_idx(i).ok(); - let path = match pool.pool_type { - AddressPoolType::External => DerivationPath::from(vec![child(0)?, child(index)?]), - AddressPoolType::Internal => DerivationPath::from(vec![child(1)?, child(index)?]), - AddressPoolType::Absent => DerivationPath::from(vec![child(index)?]), - // A public key cannot derive hardened children. - AddressPoolType::AbsentHardened => return None, - }; - match key_source.derive_at_path(&path).ok()? { - DerivedKey::ECDSA(derived) => Some(Address::p2pkh( - &dashcore::PublicKey::new(derived.public_key), - pool.network, - )), - _ => None, - } -} - #[cfg(test)] mod tests { use std::sync::atomic::{AtomicUsize, Ordering}; @@ -1761,49 +1744,59 @@ mod tests { /// Restored-address validation costs one derivation per restored row: a /// single sparse row at index 1,000,000 is checked directly, not by - /// deriving every index below it, and a mismatching one is still caught + /// deriving every index below it, and a row another xpub derived is still + /// caught. The expected addresses come from key-wallet's own per-index + /// derivation, so the validator cannot drift from it unnoticed /// (dashpay/platform#4302 review). #[test] fn restored_address_validation_is_proportional_to_the_restored_rows() { - use key_wallet::bip32::{DerivationPath, ExtendedPrivKey, ExtendedPubKey}; - use key_wallet::managed_account::address_pool::{AddressPool, AddressPoolType, KeySource}; + use key_wallet::account::derivation::AccountDerivation; + use key_wallet::account::AccountType; + use key_wallet::bip32::{ExtendedPrivKey, ExtendedPubKey}; + use key_wallet::managed_account::address_pool::AddressPoolType; + use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; + use key_wallet::managed_account::ManagedCoreFundsAccount; let secp = dashcore::secp256k1::Secp256k1::new(); - let xpub = |seed: u8| { - ExtendedPubKey::from_priv( + let account = |seed: u8| key_wallet::Account { + parent_wallet_id: None, + account_type: AccountType::DashpayExternalAccount { + index: 0, + user_identity_id: [0xAA; 32], + friend_identity_id: [0x01; 32], + }, + network: dashcore::Network::Testnet, + account_xpub: ExtendedPubKey::from_priv( &secp, &ExtendedPrivKey::new_master(dashcore::Network::Testnet, &[seed; 32]) .expect("master"), - ) + ), + is_watch_only: true, }; - let (current, previous) = (xpub(1), xpub(2)); - let mut pool = AddressPool::new_without_generation( - DerivationPath::from(vec![]), - AddressPoolType::Absent, - 20, - dashcore::Network::Testnet, - ); - pool.generate_addresses(1, &KeySource::Public(current), true) - .expect("index 0"); - let template = pool.addresses[&0].clone(); - let sparse = |address| { + let (current, previous) = (account(1), account(2)); + let managed = ManagedCoreFundsAccount::from_account(¤t); + let pool = managed.managed_account_type().address_pools()[0].clone(); + assert_eq!(pool.pool_type, AddressPoolType::Absent); + assert!(super::pool_derives_from(&pool, ¤t)); + + let sparse = |from: &key_wallet::Account| { let mut pool = pool.clone(); - let mut info = template.clone(); + let mut info = pool.addresses[&0].clone(); info.index = 1_000_000; - info.address = address; + info.address = from + .derive_address_at(AddressPoolType::Absent, 1_000_000, None) + .expect("key-wallet derives the row"); + info.script_pubkey = info.address.script_pubkey(); pool.addresses.insert(1_000_000, info); pool.highest_generated = Some(1_000_000); pool }; - let at = |key: ExtendedPubKey| { - super::derived_address(&pool, &KeySource::Public(key), 1_000_000).expect("derives") - }; let started = std::time::Instant::now(); - assert!(super::pool_derives_from(&sparse(at(current)), ¤t)); + assert!(super::pool_derives_from(&sparse(¤t), ¤t)); assert!( - !super::pool_derives_from(&sparse(at(previous)), ¤t), - "a row the current xpub does not derive is caught" + !super::pool_derives_from(&sparse(&previous), ¤t), + "a row the current account does not derive is caught" ); assert!( started.elapsed() < std::time::Duration::from_secs(5), From efb800d50c9a693e29489fbff88fba244e290c25 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 14 Oct 2026 00:06:06 -0700 Subject: [PATCH 36/43] fix(platform-wallet): keep the post-drain rescan sweep inside the startup budget The sweep that startup runs after the contact-crypto drain was left unbudgeted on the claim that it was a memory-only pass. It is not: it waits for the cursor and manager locks and, on a backend that persists backfill coverage, stores the record inline. A drain that finished at or past the deadline could therefore start another host transaction and delay the return that lets the caller start Core SPV. Run it through `within_budget`. That is safe for this step: its only await points are the two lock acquisitions, taken before it changes anything, and the rest (the store included) runs without yielding, so a drop can only land before the pass begins. A skipped sweep costs a scan, not a payment: the contact stays uncovered and the first sweep after SPV starts rewinds for it. The startup regression now also runs against a durable-capable backend: one test shows the sweep stores the drained contact's coverage there, another holds the drain's account round past the deadline and shows no coverage store starts and the next sweep picks the contact up. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/startup.rs | 315 +++++++++++++++--- .../rs-platform-wallet/src/test_support.rs | 12 +- 2 files changed, 281 insertions(+), 46 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/startup.rs b/packages/rs-platform-wallet/src/manager/startup.rs index 9e6a99a8831..36f4721bb3a 100644 --- a/packages/rs-platform-wallet/src/manager/startup.rs +++ b/packages/rs-platform-wallet/src/manager/startup.rs @@ -753,15 +753,39 @@ impl PlatformWalletManager // with `synced_height == 0` (this sequence runs before SPV starts) the // sweep marks every registered receival contact as covered by the // coming full scan, instead of leaving it for the first post-scan - // sweep to rewind for. Not budgeted: a pass over in-memory state, no - // I/O. + // sweep to rewind for. + // + // Budgeted, because it is not free: it waits for the cursor and + // manager locks, and on a backend that persists backfill coverage it + // stores the record inline. `within_budget` is safe here: the sweep's + // only await points are those two lock acquisitions, taken before it + // changes anything, and everything after them (the store included) + // runs to completion without yielding, so a drop can only land before + // the pass begins. Skipping it costs a scan, never a payment: the + // contact stays uncovered, and the first sweep after SPV starts + // rewinds to its funding height instead. if drained > 0 { - if let Err(e) = identity_wallet.dashpay().reconcile_dashpay_rescan().await { - tracing::warn!( - wallet_id = %hex::encode(wallet_id), - error = %e, - "startup: post-drain DashPay rescan sweep failed" - ); + match within_budget( + deadline, + identity_wallet.dashpay().reconcile_dashpay_rescan(), + ) + .await + { + Some(Ok(_)) => {} + Some(Err(e)) => { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = %e, + "startup: post-drain DashPay rescan sweep failed" + ); + } + None => { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + "startup: budget spent before the post-drain DashPay rescan sweep; \ + the first sweep after SPV starts will backfill the drained contacts" + ); + } } } @@ -1421,12 +1445,26 @@ mod tests { std::sync::Arc>, WalletId, ) { + manager_with_queued_contact_crypto_on(std::sync::Arc::new( + crate::test_support::NoopTestPersister, + )) + .await + } + + /// [`manager_with_queued_contact_crypto`] over a caller-supplied backend. + async fn manager_with_queued_contact_crypto_on

( + backend: std::sync::Arc

, + ) -> (std::sync::Arc>, WalletId) + where + P: crate::changeset::PlatformWalletPersistence + 'static, + { use crate::changeset::{ upsert_pending_contact_crypto, PendingContactCrypto, PendingContactCryptoOp, }; use crate::wallet::persister::{NoPlatformPersistence, WalletPersister}; - let (manager, wallet_id) = crate::test_support::test_platform_wallet_manager().await; + let (manager, wallet_id) = + crate::test_support::test_platform_wallet_manager_with(backend).await; let persister = WalletPersister::new(wallet_id, std::sync::Arc::new(NoPlatformPersistence)); let mut wm = manager.wallet_manager.write().await; @@ -1576,42 +1614,7 @@ mod tests { let owner = Identifier::from([1u8; 32]); let contact = Identifier::from([2u8; 32]); let (manager, wallet_id) = manager_with_queued_contact_crypto().await; - // The sweep handles an ESTABLISHED contact, from its request pair's - // height. Give the queued contact both requests, ours and its reply - // (funded at 100), so the pair auto-establishes the relationship. - { - use crate::wallet::identity::ContactRequest; - use crate::wallet::persister::{NoPlatformPersistence, WalletPersister}; - - let persister = - WalletPersister::new(wallet_id, std::sync::Arc::new(NoPlatformPersistence)); - let mut wm = manager.wallet_manager.write().await; - let managed = wm - .get_wallet_info_mut(&wallet_id) - .expect("wallet info") - .identity_manager - .managed_identity_mut(&owner) - .expect("managed identity"); - managed - .add_sent_contact_request( - ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0), - &persister, - ) - .expect("sent request persists"); - managed - .add_incoming_contact_request( - ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 100, 0), - &persister, - ) - .expect("incoming request persists"); - assert!( - managed - .dashpay() - .established_contacts() - .contains_key(&contact), - "the request pair establishes the contact the sweep handles" - ); - } + establish_queued_contact(&manager, wallet_id).await; let owning = SeedCryptoProvider::from_seed(seed_for(OWNING_MNEMONIC), key_wallet::Network::Testnet); @@ -1674,6 +1677,228 @@ mod tests { ); } + /// Give the queued contact both requests, ours and its reply (funded at + /// 100), so the pair auto-establishes the relationship. The rescan sweep + /// only handles an ESTABLISHED contact, from its request pair's height. + async fn establish_queued_contact

( + manager: &crate::PlatformWalletManager

, + wallet_id: WalletId, + ) where + P: crate::changeset::PlatformWalletPersistence + 'static, + { + use crate::wallet::identity::ContactRequest; + use crate::wallet::persister::{NoPlatformPersistence, WalletPersister}; + + let owner = Identifier::from([1u8; 32]); + let contact = Identifier::from([2u8; 32]); + let persister = WalletPersister::new(wallet_id, std::sync::Arc::new(NoPlatformPersistence)); + let mut wm = manager.wallet_manager.write().await; + let managed = wm + .get_wallet_info_mut(&wallet_id) + .expect("wallet info") + .identity_manager + .managed_identity_mut(&owner) + .expect("managed identity"); + managed + .add_sent_contact_request( + ContactRequest::new(owner, contact, 0, 0, 0, vec![0u8; 96], 100, 0), + &persister, + ) + .expect("sent request persists"); + managed + .add_incoming_contact_request( + ContactRequest::new(contact, owner, 0, 0, 0, vec![0u8; 96], 100, 0), + &persister, + ) + .expect("incoming request persists"); + assert!( + managed + .dashpay() + .established_contacts() + .contains_key(&contact), + "the request pair establishes the contact the sweep handles" + ); + } + + /// A backend that persists backfill coverage, so the post-drain sweep + /// stores inline rather than staying in memory: inline commits, atomic + /// rounds and account writers, plus the backfill slot. Records the + /// coverage each round carried, and can hold the drain's account round + /// past the startup deadline — a slow host transaction. + #[derive(Default)] + struct DurableStartupBackend { + /// Coverage carried by each stored round that had a backfill record. + backfill_rounds: std::sync::Mutex>, + /// Whether a round carrying an account registration was stored. + account_round_stored: std::sync::atomic::AtomicBool, + /// When set, the next account round blocks this long before + /// committing. + stall_account_round: std::sync::Mutex>, + } + + impl crate::changeset::PlatformWalletPersistence for DurableStartupBackend { + fn store_commits_inline(&self) -> bool { + true + } + + fn stores_dashpay_backfill(&self) -> bool { + true + } + + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { + use crate::changeset::PersistenceCapabilities as C; + C::ATOMIC_CHANGESETS.union(C::ACCOUNT_ADDRESS_POOLS) + } + + fn store( + &self, + _wallet_id: WalletId, + changeset: crate::changeset::PlatformWalletChangeSet, + ) -> Result<(), crate::changeset::PersistenceError> { + if !changeset.account_registrations.is_empty() { + if let Some(stall) = self.stall_account_round.lock().unwrap().take() { + std::thread::sleep(stall); + } + self.account_round_stored + .store(true, std::sync::atomic::Ordering::SeqCst); + } + if let Some(record) = changeset.dashpay_backfill { + self.backfill_rounds.lock().unwrap().push(record); + } + Ok(()) + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), crate::changeset::PersistenceError> { + Ok(()) + } + + fn load( + &self, + ) -> Result + { + Ok(crate::changeset::ClientStartState::default()) + } + } + + fn rounds_covering(backend: &DurableStartupBackend, contact: Identifier) -> usize { + backend + .backfill_rounds + .lock() + .unwrap() + .iter() + .filter(|record| { + record + .covered() + .iter() + .any(|entry| entry.contact == contact) + }) + .count() + } + + /// On a backend that persists coverage, the post-drain sweep is a real + /// store, not a memory-only pass: the drained contact reaches the host as + /// covered on the same bring-up. + #[tokio::test] + async fn the_post_drain_sweep_stores_coverage_on_a_durable_backend() { + use crate::wallet::identity::network::SeedCryptoProvider; + + let contact = Identifier::from([2u8; 32]); + let backend = std::sync::Arc::new(DurableStartupBackend::default()); + let (manager, wallet_id) = manager_with_queued_contact_crypto_on(backend.clone()).await; + establish_queued_contact(&manager, wallet_id).await; + let owning = + SeedCryptoProvider::from_seed(seed_for(OWNING_MNEMONIC), key_wallet::Network::Testnet); + + let outcome = manager + .start_wallet_subsystems( + &wallet_id, + None, + Some(&owning), + None::<&UnusedSigner>, + WalletStartupOptions::default(), + ) + .await + .expect("bring-up reports rather than raises"); + + assert_eq!(outcome.contact_accounts_drained, 1); + assert_eq!( + rounds_covering(&backend, contact), + 1, + "the post-drain sweep must store the drained contact's coverage" + ); + } + + /// The post-drain sweep stores inline on a durable backend, so it must + /// respect the startup budget like every other step that can block. A + /// drain whose last host write ran past the deadline still completes and + /// is counted, but no further store is started: bring-up returns, and the + /// contact is left for the first sweep after SPV starts. + #[tokio::test] + async fn the_post_drain_sweep_starts_no_store_once_the_budget_is_spent() { + use crate::wallet::identity::network::SeedCryptoProvider; + + let owner = Identifier::from([1u8; 32]); + let contact = Identifier::from([2u8; 32]); + let budget = Duration::from_secs(1); + let backend = std::sync::Arc::new(DurableStartupBackend::default()); + let (manager, wallet_id) = manager_with_queued_contact_crypto_on(backend.clone()).await; + establish_queued_contact(&manager, wallet_id).await; + *backend.stall_account_round.lock().unwrap() = Some(budget + Duration::from_millis(500)); + let owning = + SeedCryptoProvider::from_seed(seed_for(OWNING_MNEMONIC), key_wallet::Network::Testnet); + + let outcome = manager + .start_wallet_subsystems( + &wallet_id, + None, + Some(&owning), + None::<&UnusedSigner>, + WalletStartupOptions { + budget, + ..WalletStartupOptions::default() + }, + ) + .await + .expect("bring-up reports rather than raises"); + + assert!( + backend + .account_round_stored + .load(std::sync::atomic::Ordering::SeqCst), + "the drain's account round is the write held past the deadline" + ); + assert_eq!(outcome.contact_accounts_drained, 1); + assert_eq!( + rounds_covering(&backend, contact), + 0, + "no coverage store may start once the budget is spent" + ); + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + { + let wm = manager.wallet_manager.read().await; + assert!( + !wm.get_wallet_info(&wallet_id) + .expect("wallet info") + .identity_manager + .managed_identity(&owner) + .expect("managed identity") + .dashpay() + .rescan_triggered + .contains(&contact), + "a skipped sweep leaves the contact unhandled" + ); + } + + // The next sweep picks it up. + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("sweep"); + assert_eq!(rounds_covering(&backend, contact), 1); + } + /// The gate is paid for only when there is something to protect. An empty /// queue means the drain would derive nothing, so no key material is /// resolved — which is what keeps this affordable on a warm launch. diff --git a/packages/rs-platform-wallet/src/test_support.rs b/packages/rs-platform-wallet/src/test_support.rs index 3aa3d328129..bf58b45b29f 100644 --- a/packages/rs-platform-wallet/src/test_support.rs +++ b/packages/rs-platform-wallet/src/test_support.rs @@ -676,6 +676,17 @@ pub async fn test_platform_wallet_manager() -> ( Arc>, WalletId, ) { + test_platform_wallet_manager_with(Arc::new(NoopTestPersister)).await +} + +/// [`test_platform_wallet_manager`] over a caller-supplied persister, for tests +/// whose behaviour depends on what the backend attests. +pub async fn test_platform_wallet_manager_with

( + persister: Arc

, +) -> (Arc>, WalletId) +where + P: crate::changeset::PlatformWalletPersistence + 'static, +{ use key_wallet::mnemonic::Mnemonic; use key_wallet::wallet::initialization::WalletAccountCreationOptions; @@ -684,7 +695,6 @@ pub async fn test_platform_wallet_manager() -> ( abandon abandon abandon abandon abandon about"; let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); - let persister = Arc::new(NoopTestPersister); let event_handler: Arc = Arc::new(NoopTestEventHandler); let manager = Arc::new(crate::PlatformWalletManager::new( From c9cbdfe6d68a84540b17fb4ce03cf1f41dc19d93 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 14 Oct 2026 01:15:18 -0700 Subject: [PATCH 37/43] fix(platform-wallet): keep a faulted session's backfill coverage in memory While the manager's persistence fault is latched, the rescan sweep correctly stores nothing: the adapter is holding the durable cursor back, and a record round could advance it past rows that never landed. But the pass also dropped its coverage and rescan marks, and the latch is never cleared. Every later sweep therefore saw the contact as uncovered and, once the scan had climbed past its checkpoint, lowered the cursor back to it again for the rest of the process lifetime. Install the pass's coverage in memory on that path, as the memory-only backend path does, but keep the cursor and extent the round owes beside it, so anything that stores the record later still carries the cursor that justifies it. The next launch has no durable record for the contact and re-runs the backfill, as before. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/identity/network/payments.rs | 107 ++++++++++++++---- 1 file changed, 85 insertions(+), 22 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 610232b581c..7d39e8dae21 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -337,7 +337,11 @@ impl DashPayView<'_, B> { // height, and a cursor written from here could advance it past rows // that never landed — so nothing is stored at all: the in-memory // rewind still runs the rescan, and the next launch re-runs it, - // which is the pre-record behaviour; + // which is the pre-record behaviour. The latch is never cleared, so + // the pass's coverage is still installed in memory for the rest of + // the session, or every later sweep would rewind to the same + // contacts again. It keeps the debt: should anything store the + // record later, the owed cursor rides with it; // - a backend that cannot hold the record durably gets none at all // (`persists_backfill_coverage`): one whose `store` only buffers // commits at a later `flush` that can still drop the round, and one @@ -399,7 +403,7 @@ impl DashPayView<'_, B> { }; self.persister.store(changeset).map(|()| true).map_err(Some) }; - match stored { + let installed = match stored { Ok(durable) => { // In memory only (`durable` false), the staged record carries // no debt: nothing will ever be stored for it to settle. @@ -408,23 +412,38 @@ impl DashPayView<'_, B> { durable_cursors .insert(self.wallet_id, crate::changeset::DurableCursor::at(cursor)); } - for (owner, contact, _, _) in &to_mark { - if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { - managed.dashpay_rescan_triggered_mut().insert(*contact); - } + true + } + Err(Some(error)) => { + tracing::warn!( + wallet_id = %hex::encode(self.wallet_id), + error = %error, + "DashPay rescan: failed to persist the backfill record; the next pass \ + retries and the next launch re-runs this backfill" + ); + false + } + Err(None) => { + tracing::warn!( + wallet_id = %hex::encode(self.wallet_id), + "DashPay rescan: persistence fault latched; keeping this backfill's coverage \ + for the session only — the next launch re-runs it" + ); + // Coverage for the session, the debt kept beside it. + let live = &info.dashpay_backfill; + staged.unpersisted_cursor = live.unpersisted_cursor; + staged.unpersisted_cursor_epoch = live.unpersisted_cursor_epoch; + staged.unpersisted_extent = live.unpersisted_extent; + info.dashpay_backfill = staged; + true + } + }; + if installed { + for (owner, contact, _, _) in &to_mark { + if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { + managed.dashpay_rescan_triggered_mut().insert(*contact); } } - Err(Some(error)) => tracing::warn!( - wallet_id = %hex::encode(self.wallet_id), - error = %error, - "DashPay rescan: failed to persist the backfill record; the next pass \ - retries and the next launch re-runs this backfill" - ), - Err(None) => tracing::warn!( - wallet_id = %hex::encode(self.wallet_id), - "DashPay rescan: persistence fault latched; not recording backfill coverage \ - over a held-back cursor — the next launch re-runs this backfill" - ), } Ok(floor) } @@ -6507,7 +6526,9 @@ mod tests { /// round written then would advance it past rows that never landed, so /// the reconcile rewinds in memory but stores nothing — the next launch /// re-runs the backfill, the pre-record behaviour - /// (dashpay/platform#4302 review). + /// (dashpay/platform#4302 review). The latch never clears, so the pass's + /// coverage is kept for the session: once the scan climbs back past the + /// contact, later sweeps must not rewind to it again. #[tokio::test] async fn a_latched_persistence_fault_keeps_the_record_off_disk() { let (manager, persister, wallet_id) = make_wallet().await; @@ -6536,11 +6557,53 @@ mod tests { persister.stores.lock().unwrap().is_empty(), "nothing reaches the persister while the fault is latched" ); + { + let wm = manager.wallet_manager.read().await; + let info = wm.get_wallet_info(&wallet_id).expect("info"); + assert_eq!(info.core_wallet.synced_height(), 100); + assert_eq!( + info.dashpay_backfill.covered_from(&owner, &contact, 0), + Some(100), + "the pass's coverage is kept for the session" + ); + assert_eq!( + info.dashpay_backfill.unpersisted_cursor, + Some(100), + "beside the cursor it still owes the host" + ); + } + + // The rescan climbs back past the contact in memory; the latched + // adapter records no durable advance. Later sweeps must not rewind to + // the contact again. + { + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(1_000); + } + for _ in 0..2 { + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("later pass under the latched fault"), + None, + "a contact this session already backfilled must not be rewound to again" + ); + } + assert!(persister.stores.lock().unwrap().is_empty()); let wm = manager.wallet_manager.read().await; - let info = wm.get_wallet_info(&wallet_id).expect("info"); - assert_eq!(info.core_wallet.synced_height(), 100); - assert!(info.dashpay_backfill.is_empty()); - assert_eq!(info.dashpay_backfill.unpersisted_cursor, Some(100)); + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .core_wallet + .synced_height(), + 1_000 + ); } /// The host lags memory (a healthy backlog: the adapter has not yet From 87cbe07b55ebaac19f6f2ca44685910d11d0b990 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 14 Oct 2026 02:24:32 -0700 Subject: [PATCH 38/43] fix(platform-wallet): ignore a rewind flag that has no height beside it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `synced_height_is_rewind` is meaningless while `synced_height` is None, but an advance merged behind such a changeset kept the flag and became a rewind. Regrouping the same rounds could then give different cursors: `advance(1000)·(flagged-empty·advance(100))` rewound to 100 while `(advance(1000)·flagged-empty)·advance(100)` stayed at 1000. Clear the flag before an advance lands on an absent height, and cover heightless changesets carrying either flag value in the associativity regression. Co-Authored-By: Claude Opus 5.5 --- .../src/changeset/changeset.rs | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/rs-platform-wallet/src/changeset/changeset.rs b/packages/rs-platform-wallet/src/changeset/changeset.rs index 50ceafcb384..b79d0bfa171 100644 --- a/packages/rs-platform-wallet/src/changeset/changeset.rs +++ b/packages/rs-platform-wallet/src/changeset/changeset.rs @@ -770,7 +770,12 @@ impl Merge for CoreChangeSet { } else { // An advance composes by max, and keeps a rewind a rewind: // `rewind(a)·advance(b)` still has to replace anything an - // outer accumulation merges this result behind. + // outer accumulation merges this result behind. A flag with + // no height beside it is meaningless, so it must not turn + // this advance into a rewind. + if self.synced_height.is_none() { + self.synced_height_is_rewind = false; + } self.synced_height = Some(self.synced_height.map_or(h, |existing| existing.max(h))); } } @@ -3413,6 +3418,18 @@ mod tests { ..PlatformWalletChangeSet::default() } } + // A changeset carrying the flag but no height, as an external + // constructor could build one: the flag is meaningless there. + fn heightless(flag: bool) -> PlatformWalletChangeSet { + PlatformWalletChangeSet { + core: Some(CoreChangeSet { + synced_height: None, + synced_height_is_rewind: flag, + ..CoreChangeSet::default() + }), + ..PlatformWalletChangeSet::default() + } + } fn record_only() -> PlatformWalletChangeSet { PlatformWalletChangeSet { dashpay_backfill: Some(DashPayBackfillRecord::default()), @@ -3458,6 +3475,21 @@ mod tests { || vec![advance(1_000), advance(1_200), rewind(100)], (Some(100), true), ), + ( + "advance, heightless flagged, lower advance", + || vec![advance(1_000), heightless(true), advance(100)], + (Some(1_000), false), + ), + ( + "advance, heightless unflagged, lower advance", + || vec![advance(1_000), heightless(false), advance(100)], + (Some(1_000), false), + ), + ( + "rewind, heightless flagged, advance", + || vec![rewind(500), heightless(true), advance(900)], + (Some(900), true), + ), ]; for (name, parts, expected) in cases { let [a, b, c]: [PlatformWalletChangeSet; 3] = parts().try_into().ok().unwrap(); From d95a0bc92d3e5ce4b64417814e13038fb43b353f Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 14 Oct 2026 02:24:32 -0700 Subject: [PATCH 39/43] fix(platform-wallet): re-read a stale load snapshot, and stop retrying a refused backfill record Coverage is keyed by (owner, contact, index), not by xpub. A same-id predecessor could replace a receival account's xpub under an unchanged key, store coverage scanned with the new one, and be removed before a loader that had read the old snapshot published it. The loader then restored the old xpub beside that key, and its scan vouched for the key while the host held the new xpub, so a later launch could skip a range the new xpub was never watched for. When publication finds a same-id predecessor's durable entry, the loader now re-reads the start state under the cursor lock, where no publisher or adapter commit can move the host, and processes the wallet from that read. A state read under an entry that has not moved since is published as is, so this settles in one re-read, shared across the wallets of one load. A backfill record the backend refuses outright (a non-transient store failure) was retried every sweep. The pass's coverage stayed uninstalled, so once the scan climbed back past the contact every sweep rewound to it again. The record is now marked `store_refused` for the session: the coverage is kept in memory beside the cursor it owes, and no further record round is attempted, as under the latched fault. A transient failure keeps the retry. The next launch has no durable coverage for those contacts and re-runs their backfill. Co-Authored-By: Claude Opus 5.5 --- .../src/changeset/dashpay_backfill.rs | 10 + .../rs-platform-wallet/src/manager/load.rs | 65 +++- .../src/wallet/identity/network/payments.rs | 361 ++++++++++++++++-- 3 files changed, 395 insertions(+), 41 deletions(-) diff --git a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs index 889a13d8c4f..fdaa8f2facb 100644 --- a/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs +++ b/packages/rs-platform-wallet/src/changeset/dashpay_backfill.rs @@ -143,6 +143,15 @@ pub struct DashPayBackfillRecord { /// one a retry with no rewind of its own would produce. #[cfg_attr(feature = "serde", serde(skip))] pub unpersisted_extent: Option<(u32, u32)>, + /// In-memory only: the backend permanently refused a record round this + /// session (a non-transient store failure). Coverage is then kept for + /// the session only, beside the debt, and no further record round is + /// attempted: retrying a rejection that will keep failing would only + /// leave every later sweep rewinding to the same contacts again. A + /// relaunch starts with it clear, and with no durable coverage for those + /// contacts re-runs their backfill. + #[cfg_attr(feature = "serde", serde(skip))] + pub store_refused: bool, } /// Whether `persister` can hold a durable [`DashPayBackfillRecord`]. @@ -212,6 +221,7 @@ impl DashPayBackfillRecord { unpersisted_cursor: None, unpersisted_cursor_epoch: None, unpersisted_extent: None, + store_refused: false, } } diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 232c01804f3..fa78a81143d 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -126,7 +126,26 @@ impl PlatformWalletManager

{ Vec::new(); let mut load_error: Option = None; - 'load: for (expected_wallet_id, wallet_state) in wallets { + // Each wallet's start state, with the durable entry it was re-read + // under when it is a fresh read (see the publication step below). + let mut pending: std::collections::VecDeque<( + WalletId, + ClientWalletStartState, + Option, + )> = wallets + .into_iter() + .map(|(wallet_id, state)| (wallet_id, state, None)) + .collect(); + // A start state re-read under the durable-cursor lock, with the cursor + // map it was read against, reused for every wallet whose entry has not + // moved since. + let mut fresh: Option<( + BTreeMap, + BTreeMap, + )> = None; + + 'load: while let Some((expected_wallet_id, wallet_state, read_under)) = pending.pop_front() + { let ClientWalletStartState { mut wallet, mut wallet_info, @@ -337,6 +356,19 @@ impl PlatformWalletManager

{ // comes first, so a wallet still registered under this id is never // overwritten, and every publisher holds the cursor lock, so none // appears before the insert. + // + // A record is not all such a predecessor can have changed. It can + // also have replaced a receival account's xpub under the same + // `(owner, contact, index)` key and stored coverage scanned with + // the new one. Publishing this snapshot would restore the old + // xpub beside that key, and the scan it then runs would vouch for + // scripts the host's account does not derive. Coverage carries no + // xpub to check against, so the snapshot itself is replaced: under + // the lock, where no publisher or adapter commit can move the + // host, the start state is read again and this wallet is + // processed from that read instead. A state read under an entry + // that has not moved since is current, so this settles in one + // re-read (dashpay/platform#4302 review). { let mut durable_cursors = self.durable_cursors.lock().await; if self @@ -348,6 +380,37 @@ impl PlatformWalletManager

{ { continue 'load; } + if let Some(entry) = durable_cursors.get(&wallet_id).copied() { + if read_under != Some(entry) { + let current = fresh + .as_ref() + .is_some_and(|(_, seen)| seen.get(&wallet_id) == Some(&entry)); + if !current { + let persister = Arc::clone(&self.persister); + match run_blocking_load(move || persister.load()).await { + Ok(state) => fresh = Some((state.wallets, durable_cursors.clone())), + Err(e) => { + load_error = Some(PlatformWalletError::from_load_failure(e)); + break 'load; + } + } + } + let reread = fresh + .as_mut() + .and_then(|(wallets, _)| wallets.remove(&expected_wallet_id)); + match reread { + Some(state) => { + pending.push_front((expected_wallet_id, state, Some(entry))) + } + None => tracing::warn!( + wallet_id = %hex::encode(expected_wallet_id), + "load: the wallet is no longer on the host; not publishing \ + its stale snapshot" + ), + } + continue 'load; + } + } // Coverage for a receival account this wallet did not restore // vouches for scripts nothing will watch; it is dropped here, // and from the host before the wallet is published, exactly diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 7d39e8dae21..7da6a5665f6 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -284,10 +284,13 @@ impl DashPayView<'_, B> { !receival_accounts.contains(&(entry.owner, entry.contact, entry.account_index)) }); let owes = record.unpersisted_cursor.is_some() || record.unpersisted_extent.is_some(); - // While the fault latch is up nothing can be stored anyway (see - // below); the debt waits for the next launch. - let faulted = self.sync_fault.load(std::sync::atomic::Ordering::Relaxed); - if !(prunes || owes) || faulted { + // While the fault latch is up, or after the backend refused a + // round, nothing is stored (see below); the debt waits for the + // next launch. A prune still runs, in memory: the session's + // coverage must stop vouching for an account that is gone. + let unstorable = + self.sync_fault.load(std::sync::atomic::Ordering::Relaxed) || record.store_refused; + if !(prunes || owes) || (unstorable && !prunes) { return Ok(None); } } @@ -328,10 +331,15 @@ impl DashPayView<'_, B> { // - the pass's record is staged on a copy and installed, with the // pass's marks, only when the store returns `Ok`. The cursor and // extent the round owes are recorded on the live record BEFORE the - // store runs, so a store that fails — or panics, which unwinds - // through here and releases the manager and cursor locks — leaves - // the previous coverage plus the debt for the next pass to carry, - // never coverage that did not reach disk; + // store runs, so a store that fails transiently — or panics, which + // unwinds through here and releases the manager and cursor locks — + // leaves the previous coverage plus the debt for the next pass to + // carry, never coverage that did not reach disk; + // - a store the backend refuses outright (a non-transient failure) + // would be refused again, and retrying it would leave every later + // sweep rewinding to the same contacts. The record is marked + // `store_refused` and handled like the latched fault below for the + // rest of the session; // - while the manager's persistence fault is latched the adapter is // holding the durable cursor back at the last fully persisted // height, and a cursor written from here could advance it past rows @@ -386,11 +394,28 @@ impl DashPayView<'_, B> { Some(needed_cursor.map_or(cursor_now, |cursor| cursor.min(cursor_now))); info.dashpay_backfill.unpersisted_cursor_epoch = Some(info.rewind_barrier.epoch()); info.dashpay_backfill.unpersisted_extent = extent; + /// What this pass's staged record becomes. + enum Settled { + /// Installed: stored durably, or a backend that keeps it in + /// memory only (and so carries no debt). + Installed { durable: bool }, + /// Not installed: a retryable failure; the next pass retries. + Retry, + /// Installed for the session only, beside the debt. + SessionOnly, + } let faulted = self.sync_fault.load(std::sync::atomic::Ordering::Relaxed); - let stored = if !self.persister.persists_backfill_coverage() { - Ok(false) + let settled = if !self.persister.persists_backfill_coverage() { + Settled::Installed { durable: false } } else if faulted { - Err(None) + tracing::warn!( + wallet_id = %hex::encode(self.wallet_id), + "DashPay rescan: persistence fault latched; keeping this backfill's coverage \ + for the session only — the next launch re-runs it" + ); + Settled::SessionOnly + } else if staged.store_refused { + Settled::SessionOnly } else { let changeset = PlatformWalletChangeSet { core: cursor_to_write.map(|cursor| CoreChangeSet { @@ -401,10 +426,35 @@ impl DashPayView<'_, B> { dashpay_backfill: Some(staged.clone()), ..PlatformWalletChangeSet::default() }; - self.persister.store(changeset).map(|()| true).map_err(Some) + match self.persister.store(changeset) { + Ok(()) => Settled::Installed { durable: true }, + Err(error) if error.is_transient() => { + tracing::warn!( + wallet_id = %hex::encode(self.wallet_id), + error = %error, + "DashPay rescan: failed to persist the backfill record; the next pass \ + retries and the next launch re-runs this backfill" + ); + Settled::Retry + } + Err(error) => { + // Retrying a rejection that will keep failing would leave + // every later sweep rewinding to these contacts again. + // Stop storing for the session instead. + tracing::error!( + wallet_id = %hex::encode(self.wallet_id), + error = %error, + "DashPay rescan: the backend refused the backfill record; keeping its \ + coverage for the session only and storing no further record round — \ + the next launch re-runs this backfill" + ); + staged.store_refused = true; + Settled::SessionOnly + } + } }; - let installed = match stored { - Ok(durable) => { + match settled { + Settled::Installed { durable } => { // In memory only (`durable` false), the staged record carries // no debt: nothing will ever be stored for it to settle. info.dashpay_backfill = staged; @@ -412,37 +462,22 @@ impl DashPayView<'_, B> { durable_cursors .insert(self.wallet_id, crate::changeset::DurableCursor::at(cursor)); } - true } - Err(Some(error)) => { - tracing::warn!( - wallet_id = %hex::encode(self.wallet_id), - error = %error, - "DashPay rescan: failed to persist the backfill record; the next pass \ - retries and the next launch re-runs this backfill" - ); - false - } - Err(None) => { - tracing::warn!( - wallet_id = %hex::encode(self.wallet_id), - "DashPay rescan: persistence fault latched; keeping this backfill's coverage \ - for the session only — the next launch re-runs it" - ); - // Coverage for the session, the debt kept beside it. + Settled::Retry => return Ok(floor), + Settled::SessionOnly => { + // Coverage for the session, the debt kept beside it: should + // anything store the record later, the owed cursor rides + // with it. let live = &info.dashpay_backfill; staged.unpersisted_cursor = live.unpersisted_cursor; staged.unpersisted_cursor_epoch = live.unpersisted_cursor_epoch; staged.unpersisted_extent = live.unpersisted_extent; info.dashpay_backfill = staged; - true } - }; - if installed { - for (owner, contact, _, _) in &to_mark { - if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { - managed.dashpay_rescan_triggered_mut().insert(*contact); - } + } + for (owner, contact, _, _) in &to_mark { + if let Some(managed) = info.identity_manager.managed_identity_mut(owner) { + managed.dashpay_rescan_triggered_mut().insert(*contact); } } Ok(floor) @@ -1925,8 +1960,12 @@ mod tests { #[derive(Default)] struct RecordingPersister { stores: Mutex>, - /// When set, every `store` is refused (and not recorded). + /// When set, every `store` fails transiently (and is not recorded). fail_stores: Mutex, + /// When set, every `store` is refused permanently (a `Fatal` + /// failure, not recorded); each refused attempt is counted. + refuse_stores: Mutex, + refused_attempts: std::sync::atomic::AtomicUsize, /// When set, the next `store` panics — a backend that dies mid-write. panic_next_store: Mutex, /// When set, `store` only buffers and the backend commits at `flush` @@ -1985,7 +2024,15 @@ mod tests { changeset: PlatformWalletChangeSet, ) -> Result<(), PersistenceError> { if *self.fail_stores.lock().unwrap() { - return Err(PersistenceError::backend("injected store failure")); + return Err(PersistenceError::backend_with_kind( + crate::changeset::PersistenceErrorKind::Transient, + "injected store failure", + )); + } + if *self.refuse_stores.lock().unwrap() { + self.refused_attempts + .fetch_add(1, std::sync::atomic::Ordering::SeqCst); + return Err(PersistenceError::backend("injected store refusal")); } if std::mem::take(&mut *self.panic_next_store.lock().unwrap()) { panic!("recording persister: injected store panic"); @@ -4922,6 +4969,157 @@ mod tests { .await; } + /// A host whose first `load` returns a stale snapshot and every later + /// one the current state: what a loader sees when a same-id predecessor + /// changed the host after the loader read its start state. + struct SequencedHost { + stale: ReloadPersister, + current: ReloadPersister, + loads: std::sync::atomic::AtomicUsize, + } + + impl PlatformWalletPersistence for SequencedHost { + fn store_commits_inline(&self) -> bool { + self.current.store_commits_inline() + } + + fn stores_dashpay_backfill(&self) -> bool { + self.current.stores_dashpay_backfill() + } + + fn persistence_capabilities(&self) -> crate::changeset::PersistenceCapabilities { + self.current.persistence_capabilities() + } + + fn store( + &self, + wallet_id: WalletId, + changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + self.current.store(wallet_id, changeset) + } + + fn flush(&self, wallet_id: WalletId) -> Result<(), PersistenceError> { + self.current.flush(wallet_id) + } + + fn load(&self) -> Result { + if self.loads.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0 { + self.stale.load() + } else { + self.current.load() + } + } + } + + /// Coverage is keyed by `(owner, contact, index)`, not by xpub. A same-id + /// predecessor can replace a receival account's xpub under an unchanged + /// key and be removed before a loader that read the old snapshot + /// publishes it. Publishing that snapshot would restore the old xpub + /// with coverage scanned for it, and its scan would then vouch for the + /// key while the host holds the new xpub. With a predecessor's entry + /// present, the loader re-reads the host under the cursor lock and + /// publishes the current account and record instead + /// (dashpay/platform#4302 review). + #[tokio::test] + async fn loading_after_a_predecessor_replaced_an_xpub_publishes_the_current_account() { + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let (donor, donor_persister, wallet_id) = make_wallet().await; + establish_receival_contact( + &donor, + &donor_persister, + wallet_id, + owner, + contact, + 200, + 200, + ) + .await; + // The stale snapshot: xpub A, covered from 200. + let mut stale_record = crate::changeset::DashPayBackfillRecord::default(); + stale_record.record_pass(1_000, Some(200), [(owner, contact, 0, 200)]); + let stale = reload_snapshot(&donor, wallet_id, owner, 1_000, Some(stale_record)).await; + // The host now: the same key under xpub B, and no coverage yet. + let mut current = reload_snapshot( + &donor, + wallet_id, + owner, + 1_000, + Some(crate::changeset::DashPayBackfillRecord::default()), + ) + .await; + drop(donor); + let xpub_a = stale + .wallet + .accounts + .dashpay_receival_accounts + .values() + .next() + .expect("the receival account") + .account_xpub; + let xpub_b = current + .wallet + .accounts + .standard_bip44_accounts + .get(&0) + .expect("bip44 account") + .account_xpub; + assert_ne!(xpub_a, xpub_b); + current + .wallet + .accounts + .dashpay_receival_accounts + .values_mut() + .next() + .expect("the receival account") + .account_xpub = xpub_b; + + let host = Arc::new(SequencedHost { + stale, + current, + loads: std::sync::atomic::AtomicUsize::new(0), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + // The entry the removed predecessor left. + manager + .durable_cursors + .lock() + .await + .insert(wallet_id, crate::changeset::DurableCursor::at(1_000)); + manager + .load_from_persistor() + .await + .expect("the current state must load"); + + let wm = manager.wallet_manager.read().await; + let published = wm + .get_wallet(&wallet_id) + .expect("wallet") + .accounts + .dashpay_receival_accounts + .values() + .next() + .expect("the receival account") + .account_xpub; + assert_eq!(published, xpub_b, "the host's current xpub is published"); + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill + .covered_from(&owner, &contact, 0), + None, + "coverage scanned for the old xpub must not be published" + ); + assert_eq!( + host.loads.load(std::sync::atomic::Ordering::SeqCst), + 2, + "the stale snapshot is re-read once, under the lock" + ); + } + /// A backend whose `store` only buffers commits at a later `flush` that /// can still fail and drop the round, so a "stored" record is not proof /// of anything on disk. Such a backend gets no record: the pass rewinds @@ -6606,6 +6804,89 @@ mod tests { ); } + /// A record round the backend refuses outright (a non-transient failure) + /// would be refused again. Retrying it every sweep left the contact + /// uncovered, so each sweep after the scan climbed back past it rewound + /// to it again (dashpay/platform#4302 review). The coverage is kept for + /// the session beside the cursor it owes, and no further record round is + /// attempted. + #[tokio::test] + async fn a_permanently_refused_record_is_kept_for_the_session_without_rewinding_again() { + let (manager, persister, wallet_id) = make_wallet().await; + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + establish_receival_contact(&manager, &persister, wallet_id, owner, contact, 100, 100).await; + set_synced_height(&manager, wallet_id, 1_000).await; + persister.stores.lock().unwrap().clear(); + *persister.refuse_stores.lock().unwrap() = true; + + let wallet = manager.get_wallet(&wallet_id).await.expect("wallet"); + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("pass whose store is refused"), + Some(100), + "the in-memory rewind still runs the rescan" + ); + assert_eq!( + persister + .refused_attempts + .load(std::sync::atomic::Ordering::SeqCst), + 1 + ); + { + let wm = manager.wallet_manager.read().await; + let record = &wm + .get_wallet_info(&wallet_id) + .expect("info") + .dashpay_backfill; + assert_eq!( + record.covered_from(&owner, &contact, 0), + Some(100), + "the refused round's coverage is kept for the session" + ); + assert_eq!( + record.unpersisted_cursor, + Some(100), + "beside the cursor it owes" + ); + assert!(record.store_refused); + } + + // The rescan climbs back past the contact over several advances, + // with a sweep after each. None may rewind again or retry the store. + for height in [400, 700, 1_000] { + { + let mut wm = manager.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet_id) + .expect("info") + .core_wallet + .update_synced_height(height); + } + assert_eq!( + wallet + .identity() + .dashpay() + .reconcile_dashpay_rescan() + .await + .expect("later pass"), + None, + "no rewind at {height} for a contact this session already backfilled" + ); + } + assert_eq!( + persister + .refused_attempts + .load(std::sync::atomic::Ordering::SeqCst), + 1, + "a refused record round is not retried" + ); + assert!(persister.stores.lock().unwrap().is_empty()); + } + /// The host lags memory (a healthy backlog: the adapter has not yet /// committed the advances that took memory to 1000). A rewind to 800 /// must not write 800 — that would claim the rows between the host's From 5182e717ff80fdd35ba452581a26f619fc359295 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 14 Oct 2026 03:11:38 -0700 Subject: [PATCH 40/43] fix(platform-wallet): hold the cursor lock from a load's re-read to its publication The re-read that replaces a stale load snapshot released the durable- cursor lock before the wallet was hydrated and published, and trusted the re-read only while the wallet's DurableCursor entry was unchanged. That entry does not move on account writes or same-id lifecycle changes: a same-id wallet registered at an equal cursor could rotate a receival account's xpub and be removed in that window, and the loader would still publish the old xpub with its coverage. The loader now keeps the cursor lock from the re-read through the publication of the wallet processed from it. A same-id wallet can only appear by being published, which takes that lock, so nothing can change the host's account between the two. Nothing in between takes the lock, and it is released before the rollback. Each wallet re-reads on its own rather than through a cache shared across wallets, since a read cannot be shown current once the lock is let go. The new gated regression queues a same-id registration on the cursor lock during the re-read and asserts the wallet is already published by the time it gets the lock; releasing the lock after the re-read fails it. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/load.rs | 100 +++++++++--------- .../src/wallet/identity/network/payments.rs | 92 ++++++++++++++++ 2 files changed, 140 insertions(+), 52 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index fa78a81143d..03970d87ba9 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -126,26 +126,20 @@ impl PlatformWalletManager

{ Vec::new(); let mut load_error: Option = None; - // Each wallet's start state, with the durable entry it was re-read - // under when it is a fresh read (see the publication step below). - let mut pending: std::collections::VecDeque<( - WalletId, - ClientWalletStartState, - Option, - )> = wallets - .into_iter() - .map(|(wallet_id, state)| (wallet_id, state, None)) - .collect(); - // A start state re-read under the durable-cursor lock, with the cursor - // map it was read against, reused for every wallet whose entry has not - // moved since. - let mut fresh: Option<( - BTreeMap, - BTreeMap, - )> = None; - - 'load: while let Some((expected_wallet_id, wallet_state, read_under)) = pending.pop_front() - { + // Each wallet's start state, and whether it was re-read under the + // durable-cursor lock that `held` still holds (see the publication + // step below). + let mut pending: std::collections::VecDeque<(WalletId, ClientWalletStartState, bool)> = + wallets + .into_iter() + .map(|(wallet_id, state)| (wallet_id, state, false)) + .collect(); + // The durable-cursor lock, kept from a re-read to the publication of + // the wallet processed from it. Nothing between the two takes the + // lock, and it is released before the rollback below. + let mut held = None; + + 'load: while let Some((expected_wallet_id, wallet_state, reread)) = pending.pop_front() { let ClientWalletStartState { mut wallet, mut wallet_info, @@ -365,12 +359,20 @@ impl PlatformWalletManager

{ // scripts the host's account does not derive. Coverage carries no // xpub to check against, so the snapshot itself is replaced: under // the lock, where no publisher or adapter commit can move the - // host, the start state is read again and this wallet is - // processed from that read instead. A state read under an entry - // that has not moved since is current, so this settles in one - // re-read (dashpay/platform#4302 review). + // host, the start state is read again, and this wallet is + // processed from that read and published WITHOUT releasing the + // lock in between. A same-id wallet can only appear by being + // published, which takes this lock, so nothing can register, + // rotate an account and be removed between the read and the + // publication. No token stands in for that guarantee: a cursor + // entry does not move on account writes or same-id lifecycle + // changes, so it cannot prove a read is still current + // (dashpay/platform#4302 review). { - let mut durable_cursors = self.durable_cursors.lock().await; + let mut durable_cursors = match held.take() { + Some(guard) => guard, + None => self.durable_cursors.lock().await, + }; if self .wallet_manager .read() @@ -380,36 +382,27 @@ impl PlatformWalletManager

{ { continue 'load; } - if let Some(entry) = durable_cursors.get(&wallet_id).copied() { - if read_under != Some(entry) { - let current = fresh - .as_ref() - .is_some_and(|(_, seen)| seen.get(&wallet_id) == Some(&entry)); - if !current { - let persister = Arc::clone(&self.persister); - match run_blocking_load(move || persister.load()).await { - Ok(state) => fresh = Some((state.wallets, durable_cursors.clone())), - Err(e) => { - load_error = Some(PlatformWalletError::from_load_failure(e)); - break 'load; - } - } + if !reread && durable_cursors.contains_key(&wallet_id) { + let persister = Arc::clone(&self.persister); + let current = match run_blocking_load(move || persister.load()).await { + Ok(mut state) => state.wallets.remove(&expected_wallet_id), + Err(e) => { + load_error = Some(PlatformWalletError::from_load_failure(e)); + break 'load; } - let reread = fresh - .as_mut() - .and_then(|(wallets, _)| wallets.remove(&expected_wallet_id)); - match reread { - Some(state) => { - pending.push_front((expected_wallet_id, state, Some(entry))) - } - None => tracing::warn!( - wallet_id = %hex::encode(expected_wallet_id), - "load: the wallet is no longer on the host; not publishing \ - its stale snapshot" - ), + }; + match current { + Some(state) => { + pending.push_front((expected_wallet_id, state, true)); + held = Some(durable_cursors); } - continue 'load; + None => tracing::warn!( + wallet_id = %hex::encode(expected_wallet_id), + "load: the wallet is no longer on the host; not publishing its \ + stale snapshot" + ), } + continue 'load; } // Coverage for a receival account this wallet did not restore // vouches for scripts nothing will watch; it is dropped here, @@ -581,6 +574,9 @@ impl PlatformWalletManager

{ } } } + // A `break` between a re-read and its publication leaves the lock + // held; the rollback must not run under it. + drop(held); if let Some(err) = load_error { // Walk back every wallet committed in this call so the diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 7da6a5665f6..b9f7c2cf1ad 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -4976,6 +4976,9 @@ mod tests { stale: ReloadPersister, current: ReloadPersister, loads: std::sync::atomic::AtomicUsize, + /// When set, the second `load` (the loader's re-read) reports that + /// it has started and waits to be released. + reread_gate: Mutex, std::sync::mpsc::Receiver<()>)>>, } impl PlatformWalletPersistence for SequencedHost { @@ -5007,6 +5010,11 @@ mod tests { if self.loads.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0 { self.stale.load() } else { + let gate = self.reread_gate.lock().unwrap().take(); + if let Some((entered, release)) = gate { + let _ = entered.send(()); + let _ = release.recv(); + } self.current.load() } } @@ -5079,6 +5087,7 @@ mod tests { stale, current, loads: std::sync::atomic::AtomicUsize::new(0), + reread_gate: Mutex::new(None), }); let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); let handler: Arc = Arc::new(NoopEventHandler); @@ -5120,6 +5129,89 @@ mod tests { ); } + /// The re-read is only current while nothing can change the host. A + /// same-id wallet can register, rotate an account's xpub and be removed + /// without moving the durable cursor entry, so no token can prove the + /// read is still current once the lock is released. The loader keeps the + /// cursor lock from the re-read through the wallet's publication instead. + /// A same-id registration needs that lock, so the first moment one could + /// run is after the re-read state is published (dashpay/platform#4302 + /// review). + #[tokio::test] + async fn the_loader_holds_the_cursor_lock_from_its_re_read_to_publication() { + use std::future::Future; + + let owner = Identifier::from([0xAA; 32]); + let contact = Identifier::from([0xBB; 32]); + let (donor, donor_persister, wallet_id) = make_wallet().await; + establish_receival_contact( + &donor, + &donor_persister, + wallet_id, + owner, + contact, + 200, + 200, + ) + .await; + let stale = reload_snapshot(&donor, wallet_id, owner, 1_000, None).await; + let current = reload_snapshot(&donor, wallet_id, owner, 1_000, None).await; + drop(donor); + let (entered_tx, entered_rx) = std::sync::mpsc::channel(); + let (release_tx, release_rx) = std::sync::mpsc::channel(); + let host = Arc::new(SequencedHost { + stale, + current, + loads: std::sync::atomic::AtomicUsize::new(0), + reread_gate: Mutex::new(Some((entered_tx, release_rx))), + }); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let manager = Arc::new(PlatformWalletManager::new(sdk, Arc::clone(&host), handler)); + // The entry a removed same-id predecessor left. + manager + .durable_cursors + .lock() + .await + .insert(wallet_id, crate::changeset::DurableCursor::at(1_000)); + + let loader = tokio::spawn({ + let manager = Arc::clone(&manager); + async move { manager.load_from_persistor().await } + }); + tokio::task::spawn_blocking(move || entered_rx.recv()) + .await + .expect("join") + .expect("the loader re-reads the host"); + + // A same-id registration queues for the cursor lock while the + // re-read runs. + let mut registration = Box::pin(manager.durable_cursors.lock()); + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + assert!( + registration.as_mut().poll(&mut cx).is_pending(), + "the loader holds the cursor lock during its re-read" + ); + release_tx.send(()).expect("release the re-read"); + + let guard = registration.await; + assert!( + manager + .wallet_manager + .read() + .await + .get_wallet(&wallet_id) + .is_some(), + "nothing may take the cursor lock between the re-read and the publication" + ); + drop(guard); + loader + .await + .expect("join") + .expect("the re-read state must load"); + assert_eq!(host.loads.load(std::sync::atomic::Ordering::SeqCst), 2); + } + /// A backend whose `store` only buffers commits at a later `flush` that /// can still fail and drop the round, so a "stored" record is not proof /// of anything on disk. Such a backend gets no record: the pass rewinds From ab88f3da29655cee066d90ff602170acc6bd0c8b Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 14 Oct 2026 04:10:22 -0700 Subject: [PATCH 41/43] fix(platform-wallet-ffi): advertise the backfill record only to hosts that store its cursor `stores_dashpay_backfill()` reported support from the backfill slot alone, so an atomic, account-capable host with the slot but no changeset callback passed `persists_backfill_coverage()`. `store` withholds every covering record from such a host, since the cursor the coverage vouches for cannot reach it, and still returns Ok, so the owning layer counted coverage as stored that the host never received. The claim now also requires the changeset callback. Such a host is treated like any other that cannot keep coverage durably, which still sends it the empty record that clears coverage it holds. Covered by an eligibility regression over a real FFIPersister, and the recreated-wallet lifecycle now includes an atomic, account-capable, cursorless host: its handed-back coverage is cleared and it is never offered coverage. Also bounds the loader re-read test's blocking wait, so a loader that skipped the re-read fails the test instead of hanging it. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-ffi/src/persistence.rs | 132 +++++++++++++++++- .../src/wallet/identity/network/payments.rs | 13 +- 2 files changed, 135 insertions(+), 10 deletions(-) diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index 5da4c87791d..aba5e2ed668 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -1834,8 +1834,16 @@ impl PlatformWalletPersistence for FFIPersister { fn stores_dashpay_backfill(&self) -> bool { // The record reaches the host only through its extension slot; a - // host without it cannot store one, nor replace one it holds. + // host without it cannot store one, nor replace one it holds. And + // coverage vouches for the cursor it rides with, which reaches the + // host only through the changeset callback: `store` withholds every + // covering record from a host without one, so claiming the record + // here would let the owning layer count coverage as stored that the + // host never received. Such a host is treated like any other that + // cannot keep coverage durably, which still sends it the empty + // record that clears coverage it holds. self.wallet_dashpay_backfill_callback.is_some() + && self.callbacks.on_persist_wallet_changeset_fn.is_some() } fn store_commits_inline(&self) -> bool { @@ -8865,9 +8873,28 @@ mod tests { } i32::from(host.fail.load(Ordering::SeqCst)) } + unsafe extern "C" fn accounts( + _: *mut c_void, + _: *const u8, + _: *const AccountSpecFFI, + _: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn pools( + _: *mut c_void, + _: *const u8, + _: *const AccountAddressPoolFFI, + _: usize, + ) -> i32 { + 0 + } + // `atomic_accounts`: atomic rounds and both account writers, but no + // changeset callback, so no cursor reaches the host. fn manager_for( host: Option<&'static Host>, slot: bool, + atomic_accounts: bool, ) -> PlatformWalletManager { let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( PersistenceCallbacks { @@ -8876,9 +8903,18 @@ mod tests { }), on_load_wallet_list_fn: host.map(|_| load_wallets as _), on_load_wallet_list_free_fn: host.map(|_| free_wallets as _), + on_changeset_begin_fn: atomic_accounts.then_some(noop_begin as _), + on_changeset_end_fn: atomic_accounts.then_some(noop_end as _), + on_persist_account_registrations_fn: atomic_accounts.then_some(accounts as _), + on_persist_account_address_pools_fn: atomic_accounts.then_some(pools as _), ..PersistenceCallbacks::default() }, - PersistenceCapabilities::NONE, + if atomic_accounts { + PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS) + } else { + PersistenceCapabilities::NONE + }, PersistenceExtensionCallbacks { wallet_dashpay_backfill: slot.then_some(on_backfill as _), ..Default::default() @@ -8917,7 +8953,7 @@ mod tests { Some(0), )) }; - let wallet_id = create(&manager_for(None, true)) + let wallet_id = create(&manager_for(None, true, false)) .expect("the id this seed registers under") .wallet_id(); let host = |holds_record: bool| -> &'static Host { @@ -8940,7 +8976,7 @@ mod tests { // host that fails it fails the registration. let with_slot = host(true); with_slot.fail.store(true, Ordering::SeqCst); - let manager = manager_for(Some(with_slot), true); + let manager = manager_for(Some(with_slot), true, false); assert!( create(&manager).is_err(), "an invalidation the host failed must fail the registration" @@ -8955,7 +8991,7 @@ mod tests { ); // No slot: the coverage cannot be cleared, so nothing is published. - let manager = manager_for(Some(host(true)), false); + let manager = manager_for(Some(host(true)), false, false); match create(&manager) { Ok(_) => panic!("coverage the host cannot clear must block the registration"), Err(PlatformWalletError::PersisterStore(source)) => { @@ -8969,9 +9005,93 @@ mod tests { // with or without the slot. for slot in [false, true] { let clean = host(false); - create(&manager_for(Some(clean), slot)).expect("nothing to clear"); + create(&manager_for(Some(clean), slot, false)).expect("nothing to clear"); assert_eq!(clean.invalidations.load(Ordering::SeqCst), 0); } + + // Atomic rounds and both account writers, but no cursor callback: + // the host cannot keep coverage, so it is cleared like on any other + // such host and never offered coverage. + let cursorless = host(true); + create(&manager_for(Some(cursorless), true, true)).expect("the invalidation lands"); + assert_eq!(cursorless.invalidations.load(Ordering::SeqCst), 1); + assert_eq!(cursorless.covering.load(Ordering::SeqCst), 0); + } + + /// Coverage counts as durable on an FFI host only when everything it + /// rides with can reach the host: atomic rounds, both account writers, + /// the backfill slot, AND the changeset callback that carries the cursor + /// the coverage vouches for. `store` withholds every covering record + /// from a host without that callback, so advertising the record there + /// let the owning layer treat suppressed coverage as stored + /// (dashpay/platform#4302 review). + #[test] + fn backfill_coverage_needs_a_host_that_stores_its_cursor() { + use platform_wallet::changeset::persists_backfill_coverage; + + unsafe extern "C" fn begin(_: *mut c_void, _: *const u8) -> i32 { + 0 + } + unsafe extern "C" fn end(_: *mut c_void, _: *const u8, _: bool) -> i32 { + 0 + } + unsafe extern "C" fn accounts( + _: *mut c_void, + _: *const u8, + _: *const AccountSpecFFI, + _: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn pools( + _: *mut c_void, + _: *const u8, + _: *const AccountAddressPoolFFI, + _: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn changeset( + _: *mut c_void, + _: *const u8, + _: *const WalletChangeSetFFI, + ) -> i32 { + 0 + } + unsafe extern "C" fn backfill( + _: *mut c_void, + _: *const u8, + _: u32, + _: u32, + _: *const DashPayBackfillCoveredContactFFI, + _: usize, + ) -> i32 { + 0 + } + let host = |cursor_callback: bool, slot: bool| { + FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + on_changeset_begin_fn: Some(begin), + on_changeset_end_fn: Some(end), + on_persist_account_registrations_fn: Some(accounts), + on_persist_account_address_pools_fn: Some(pools), + on_persist_wallet_changeset_fn: cursor_callback.then_some(changeset as _), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::ATOMIC_CHANGESETS + .union(PersistenceCapabilities::ACCOUNT_ADDRESS_POOLS), + PersistenceExtensionCallbacks { + wallet_dashpay_backfill: slot.then_some(backfill as _), + ..Default::default() + }, + ) + }; + assert!( + !persists_backfill_coverage(&host(false, true)), + "an atomic, account-capable host with the slot but no cursor callback" + ); + assert!(!persists_backfill_coverage(&host(true, false))); + assert!(persists_backfill_coverage(&host(true, true))); } /// An atomic FFI host keeps outbound-account markers on disk only when it diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index b9f7c2cf1ad..6c14eabb5f2 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -5179,10 +5179,15 @@ mod tests { let manager = Arc::clone(&manager); async move { manager.load_from_persistor().await } }); - tokio::task::spawn_blocking(move || entered_rx.recv()) - .await - .expect("join") - .expect("the loader re-reads the host"); + // Bounded inside the closure: a running blocking task cannot be + // cancelled, so a loader that skipped the re-read would otherwise + // hang the test instead of failing it. + tokio::task::spawn_blocking(move || { + entered_rx.recv_timeout(std::time::Duration::from_secs(10)) + }) + .await + .expect("join") + .expect("the loader re-reads the host within the deadline"); // A same-id registration queues for the cursor lock while the // re-read runs. From f7ffed38bf39b58d9ee58e13971ba839b8956c3f Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 14 Oct 2026 04:46:44 -0700 Subject: [PATCH 42/43] perf(platform-wallet): validate restored outbound pools only where a marker could be trusted The load validated every restored DashPay external pool, deriving each restored address, before checking whether the relationship had a marker or the backend could keep markers at all. Only a marker can be distrusted by that validation, so the work was wasted for markerless relationships, and on a backend that cannot keep markers durably every marker is cleared regardless. A contact-heavy wallet paid for all of it on every restore. The validator now skips a relationship restored without a marker, and the load runs it only on a backend that keeps markers durably; the others go straight to clearing every restored marker. A marker that would otherwise be trusted is still validated per index. A test-only per-thread counter of pool validations lets the new regression show a markerless relationship and an ineligible backend derive nothing, while a trusted marker's pool is still validated. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet/src/manager/load.rs | 56 +++++++-- .../rs-platform-wallet/src/manager/mod.rs | 2 + .../src/wallet/identity/network/payments.rs | 118 ++++++++++++++++-- 3 files changed, 154 insertions(+), 22 deletions(-) diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 03970d87ba9..a4bde29f0ec 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -260,21 +260,25 @@ impl PlatformWalletManager

{ // contact's marker, and the first contact sweep rebuilds it from // the current xpub, as every cold start did before the marker // was persisted (dashpay/platform#4302 review). - let distrusted = - distrust_markers_of_foreign_external_pools(&wallet, &mut platform_info); - if distrusted > 0 { - tracing::warn!( - wallet_id = %hex::encode(expected_wallet_id), - contacts = distrusted, - "load: restored DashPay external pools hold addresses their current xpub \ - does not derive; their accounts will be rebuilt" - ); - } + // // A backend that cannot keep markers durably never had one written // by this process, and one it hands back may name an account it // did not keep: none is trusted, so the sweep rebuilds each - // outbound account once, as before the marker was persisted. - if !crate::changeset::persists_outbound_account_markers(&*self.persister) { + // outbound account once, as before the marker was persisted. Its + // pools are then not validated at all: no result could keep a + // marker that is cleared regardless. + if crate::changeset::persists_outbound_account_markers(&*self.persister) { + let distrusted = + distrust_markers_of_foreign_external_pools(&wallet, &mut platform_info); + if distrusted > 0 { + tracing::warn!( + wallet_id = %hex::encode(expected_wallet_id), + contacts = distrusted, + "load: restored DashPay external pools hold addresses their current \ + xpub does not derive; their accounts will be rebuilt" + ); + } + } else { let cleared = clear_restored_outbound_markers(&mut platform_info); if cleared > 0 { tracing::info!( @@ -1475,6 +1479,19 @@ fn distrust_markers_of_foreign_external_pools( .accounts .dashpay_external_accounts .iter() + // Only a marker can be distrusted, so a relationship without one + // costs no derivation. + .filter(|(key, _)| { + info.identity_manager + .managed_identity(&Identifier::from(key.user_identity_id)) + .and_then(|managed| { + managed + .dashpay() + .established_contacts() + .get(&Identifier::from(key.friend_identity_id)) + }) + .is_some_and(|established| established.external_account_reference.is_some()) + }) .filter(|(key, managed)| { wallet .accounts @@ -1506,6 +1523,19 @@ fn distrust_markers_of_foreign_external_pools( cleared } +#[cfg(test)] +thread_local! { + /// How many restored pools [`pool_derives_from`] validated on this + /// thread, so a test can show a load did no derivation it did not need. + static POOL_VALIDATIONS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// How many restored pools have been validated on this thread. +#[cfg(test)] +pub(crate) fn pool_validations_on_this_thread() -> usize { + POOL_VALIDATIONS.with(std::cell::Cell::get) +} + /// Clear every restored outbound-account marker on `info`. Returns how many /// were set. fn clear_restored_outbound_markers(info: &mut PlatformWalletInfo) -> usize { @@ -1542,6 +1572,8 @@ fn clear_restored_outbound_markers(info: &mut PlatformWalletInfo) -> usize { /// key. A failed derivation counts as a mismatch, and a hardened pool never /// matches: its children cannot be derived from a public key. fn pool_derives_from(pool: &AddressPool, account: &key_wallet::Account) -> bool { + #[cfg(test)] + POOL_VALIDATIONS.with(|count| count.set(count.get() + 1)); if pool.pool_type == AddressPoolType::AbsentHardened { return pool.addresses.is_empty(); } diff --git a/packages/rs-platform-wallet/src/manager/mod.rs b/packages/rs-platform-wallet/src/manager/mod.rs index 419aab9ea30..f7fae03602c 100644 --- a/packages/rs-platform-wallet/src/manager/mod.rs +++ b/packages/rs-platform-wallet/src/manager/mod.rs @@ -5,6 +5,8 @@ pub mod dashpay_sync; pub mod dpns_sync; pub mod identity_sync; mod load; +#[cfg(test)] +pub(crate) use load::pool_validations_on_this_thread; mod persistence_load; pub mod platform_address_sync; #[cfg(feature = "shielded")] diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 6c14eabb5f2..0027b9b22d4 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -6077,16 +6077,16 @@ mod tests { } } - /// A restored outbound-account marker suppresses the startup rebuild, so - /// it must not vouch for a pool its account's xpub does not derive. A - /// host that replaced the xpub on rotation but kept the old pool's rows - /// would otherwise hand out pre-rotation addresses on the next send. - /// The load clears the marker for such a pool, and the contact sweep - /// rebuilds the account from the current xpub (dashpay/platform#4302 - /// review). - #[tokio::test] - async fn a_restored_marker_is_not_trusted_over_a_pool_its_current_xpub_does_not_derive() { - use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + /// A wallet with one established contact whose outbound account is + /// registered and whose marker (7) is stamped: the state the restored- + /// marker validation runs against. + async fn wallet_with_a_marked_outbound_account() -> ( + Arc>, + WalletId, + Identifier, + Identifier, + key_wallet::account::account_collection::DashpayAccountKey, + ) { use crate::wallet::identity::{ContactRequest, EstablishedContact}; use key_wallet::account::account_collection::DashpayAccountKey; @@ -6128,6 +6128,22 @@ mod tests { user_identity_id: owner.to_buffer(), friend_identity_id: contact.to_buffer(), }; + (manager, wallet_id, owner, contact, key) + } + + /// A restored outbound-account marker suppresses the startup rebuild, so + /// it must not vouch for a pool its account's xpub does not derive. A + /// host that replaced the xpub on rotation but kept the old pool's rows + /// would otherwise hand out pre-rotation addresses on the next send. + /// The load clears the marker for such a pool, and the contact sweep + /// rebuilds the account from the current xpub (dashpay/platform#4302 + /// review). + #[tokio::test] + async fn a_restored_marker_is_not_trusted_over_a_pool_its_current_xpub_does_not_derive() { + use crate::wallet::identity::network::contact_requests::external_account_needs_rebuild; + + let (manager, wallet_id, owner, contact, key) = + wallet_with_a_marked_outbound_account().await; for (rotated, marker) in [(false, Some(7)), (true, None)] { let mut snapshot = reload_snapshot(&manager, wallet_id, owner, 1_000, None).await; @@ -6164,6 +6180,88 @@ mod tests { } } + /// Validating a restored pool derives every restored address, and only a + /// marker can be distrusted by it. So the load derives nothing for a + /// relationship restored without a marker, nor on a backend whose markers + /// are all cleared anyway because it cannot keep them durably; it still + /// validates a marker it would otherwise trust (dashpay/platform#4302 + /// review). + #[tokio::test] + async fn restored_pools_are_validated_only_where_a_marker_could_be_trusted() { + use crate::manager::pool_validations_on_this_thread as validations; + + let (manager, wallet_id, owner, contact, key) = + wallet_with_a_marked_outbound_account().await; + // Rotated, so a validation would distrust the marker if it ran. + let snapshot = |marker: bool| { + let manager = Arc::clone(&manager); + async move { + let mut snapshot = reload_snapshot(&manager, wallet_id, owner, 1_000, None).await; + snapshot + .wallet + .accounts + .dashpay_external_accounts + .get_mut(&key) + .expect("outbound account") + .account_xpub = test_receiving_xpub(&contact, &owner); + if !marker { + snapshot + .identities + .get_mut(&0) + .expect("owner") + .established_contact_mut(&contact) + .expect("contact") + .external_account_reference = None; + } + snapshot + } + }; + // No marker restored: nothing to validate. + let before = validations(); + let _ = reload(snapshot(false).await).await; + assert_eq!( + validations(), + before, + "a markerless relationship costs no derivation" + ); + + // A backend that cannot keep markers: all are cleared, none validated. + let before = validations(); + let host = Arc::new(NonAtomicHost(Arc::new(snapshot(true).await))); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let handler: Arc = Arc::new(NoopEventHandler); + let restarted = Arc::new(PlatformWalletManager::new(sdk, host, handler)); + restarted.load_from_persistor().await.expect("load"); + assert_eq!( + validations(), + before, + "an ineligible backend derives nothing" + ); + { + let wm = restarted.wallet_manager.read().await; + assert_eq!( + wm.get_wallet_info(&wallet_id) + .expect("info") + .identity_manager + .managed_identity(&owner) + .expect("owner") + .dashpay() + .established_contacts()[&contact] + .external_account_reference, + None, + "and its marker is still cleared" + ); + } + + // A marker an eligible backend would trust is still validated. + let before = validations(); + let _ = reload(snapshot(true).await).await; + assert!( + validations() > before, + "a trusted marker's pool is validated" + ); + } + /// [`ReloadPersister`] behind a backend whose `store` only buffers, with /// a `flush` that can be made to fail transiently. struct BufferedHost { From 88243e3415b28782f5d29f0b8bbd1f59cee00173 Mon Sep 17 00:00:00 2001 From: HashEngineering Date: Wed, 14 Oct 2026 13:44:48 -0700 Subject: [PATCH 43/43] perf(platform-wallet): index receival-account membership in the rescan sweep The sweep collected the wallet's receival accounts into a Vec and then searched it linearly once per coverage entry: in the steady-state prune check and again when staging the record. Both run under the cursor lock and the wallet-manager write lock, so the cost grew quadratically with a wallet's contacts on every sweep. Collect them into a BTreeSet instead and use `contains` for both checks. Nothing in the pass depends on the iteration order: the floor is a minimum, the marks are a set, and the record canonicalizes its entries. Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/identity/network/payments.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs index 0027b9b22d4..e4f688a8e96 100644 --- a/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs +++ b/packages/rs-platform-wallet/src/wallet/identity/network/payments.rs @@ -176,8 +176,9 @@ impl DashPayView<'_, B> { // only watch a contact's incoming addresses once its receival account // exists. Coverage is per account, not per pair: a second receiving // account for a relationship derives its own xpub, and a scan that - // watched the first never tested the second's scripts. - let receival_accounts: Vec<(Identifier, Identifier, u32)> = info + // watched the first never tested the second's scripts. A set, because + // every coverage entry is checked against it under both locks. + let receival_accounts: std::collections::BTreeSet<(Identifier, Identifier, u32)> = info .core_wallet .accounts .dashpay_receival_accounts @@ -358,9 +359,7 @@ impl DashPayView<'_, B> { // the pre-record guard. let mut staged = info.dashpay_backfill.clone(); staged.retain(|owner, contact, account_index| { - receival_accounts - .iter() - .any(|(o, c, i)| (o, c, *i) == (owner, contact, account_index)) + receival_accounts.contains(&(*owner, *contact, account_index)) }); // This pass's extent, widened by one a failed earlier round still owes. let extent = match (