From 12f901fdfc0b13f7c2bea01d1ee082faee013b21 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 6 Nov 2026 16:07:51 +0000 Subject: [PATCH 1/4] feat(platform-wallet-storage): read one asset lock's status by outpoint `load()` does not rehydrate a consumed asset lock without an operation ID, so after a reopen a lock the wallet spent and one it never stored look alike to a host. Add `SqlitePersister::asset_lock_status(wallet_id, outpoint)`, a point read of the committed table that includes terminal `Consumed` rows and returns `None` when the wallet holds no row for the outpoint. Decoding and status drift follow the full-history reader. Co-Authored-By: Claude Opus 5.5 --- .../src/sqlite/persister.rs | 26 ++++++++++ .../src/sqlite/schema/asset_locks.rs | 39 +++++++++++++++ .../integration/sqlite_asset_locks_filter.rs | 50 +++++++++++++++++++ 3 files changed, 115 insertions(+) diff --git a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs index 15634f6293f..744290b490d 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs @@ -634,6 +634,32 @@ impl SqlitePersister { prune_backups_in(dir, policy) } + /// One asset lock's persisted status by outpoint, `Consumed` rows + /// included, or `None` when `wallet_id` stored none for it. + /// + /// `load()` does not rehydrate a consumed lock without an operation ID, + /// so a lock missing from the live wallet after a reopen may have been + /// spent or never stored here; + /// this read tells them apart. A wallet delete removes every row, so a + /// re-imported wallet reads `None` for locks it held before. + /// + /// Reads the *committed* table: in [`FlushMode::Manual`] a row still in + /// the write buffer is not visible yet. + pub fn asset_lock_status( + &self, + wallet_id: &WalletId, + outpoint: &dashcore::OutPoint, + ) -> Result< + Option, + WalletStorageError, + > { + let conn = self.conn()?; + let ctx = LoadCtx::new(self.config.load_policy); + let status = schema::asset_locks::status_of(&conn, wallet_id, outpoint, &ctx)?; + self.merge_load_degradation(ctx.degradation()); + Ok(status) + } + /// Read every identity that belongs to NO wallet, each already /// carrying its own persisted public keys. /// diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/asset_locks.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/asset_locks.rs index ced3c0e2ec6..764ebdcb4c5 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/asset_locks.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/asset_locks.rs @@ -394,6 +394,45 @@ pub fn load_state( Ok(out) } +/// One asset lock's status by outpoint, **including** a terminal `Consumed` +/// row, or `None` when `wallet_id` has no row for it. The point lookup a +/// host uses to tell a lock this wallet spent from one it never stored: +/// [`load_unconsumed`] drops a consumed lock without an operation ID, so +/// absence there proves nothing. +/// Decoding and status drift follow [`load_state`]. +pub fn status_of( + conn: &Connection, + wallet_id: &WalletId, + outpoint: &OutPoint, + ctx: &LoadCtx, +) -> Result, WalletStorageError> { + let op_bytes = blob::encode_outpoint(outpoint)?; + let mut stmt = conn.prepare( + "SELECT length(outpoint), outpoint, account_index, length(lifecycle_blob), lifecycle_blob, status, operation_id \ + FROM asset_locks WHERE wallet_id = ?1 AND outpoint = ?2", + )?; + let mut rows = stmt.query(params![wallet_id.as_slice(), &op_bytes[..]])?; + let Some(row) = rows.next()? else { + return Ok(None); + }; + blob::check_size(row.get::<_, i64>(0)?)?; + let op_bytes: Vec = row.get(1)?; + let account_index: i64 = row.get(2)?; + blob::check_size(row.get::<_, i64>(3)?)?; + let blob_bytes: Vec = row.get(4)?; + let status: String = row.get(5)?; + let operation_id: Option> = row.get(6)?; + let (_, _, tracked) = decode_row( + &op_bytes, + account_index, + &blob_bytes, + &status, + operation_id, + ctx, + )?; + Ok(Some(tracked.status)) +} + /// Status-filtered rehydration feed: every asset lock **except** terminal /// `Consumed` rows without an operation ID, bucketed by account index. /// Consumed wallet-drain locks are non-actionable recovery tombstones: retaining diff --git a/packages/rs-platform-wallet-storage/tests/integration/sqlite_asset_locks_filter.rs b/packages/rs-platform-wallet-storage/tests/integration/sqlite_asset_locks_filter.rs index f8e1250bf09..709b542a7c9 100644 --- a/packages/rs-platform-wallet-storage/tests/integration/sqlite_asset_locks_filter.rs +++ b/packages/rs-platform-wallet-storage/tests/integration/sqlite_asset_locks_filter.rs @@ -339,3 +339,53 @@ fn should_reject_conflicting_operation_id_before_mutating_any_stored_columns() { ); } } + +/// The point read sees a `Consumed` row the rehydration feed drops, keeps +/// it across a reopen, returns a live row's status, and `None` for an +/// outpoint the wallet never stored or another wallet's row. +#[test] +fn should_read_an_asset_locks_status_by_outpoint_including_consumed_after_reopen() { + let (persister, _tmp, path) = fresh_persister(); + let w = wid(0xA6); + let other = wid(0xA7); + ensure_wallet_meta(&persister, &w); + ensure_wallet_meta(&persister, &other); + let consumed = op(0x30); + let live = op(0x31); + let mut cs = AssetLockChangeSet::default(); + cs.asset_locks + .insert(consumed, entry(consumed, AssetLockStatus::Consumed)); + cs.asset_locks + .insert(live, entry(live, AssetLockStatus::InstantSendLocked)); + persister + .store( + w, + platform_wallet::changeset::PlatformWalletChangeSet { + asset_locks: Some(cs), + ..Default::default() + }, + ) + .unwrap(); + assert_eq!( + persister.asset_lock_status(&w, &consumed).unwrap(), + Some(AssetLockStatus::Consumed) + ); + drop(persister); + + let p2 = reopen(&path); + assert_eq!( + p2.asset_lock_status(&w, &consumed).unwrap(), + Some(AssetLockStatus::Consumed), + "a consumed row reads back after a reopen" + ); + assert_eq!( + p2.asset_lock_status(&w, &live).unwrap(), + Some(AssetLockStatus::InstantSendLocked) + ); + assert_eq!(p2.asset_lock_status(&w, &op(0x32)).unwrap(), None); + assert_eq!( + p2.asset_lock_status(&other, &consumed).unwrap(), + None, + "another wallet's row is not this wallet's" + ); +} From 97ded2d2b59790512a8da4cf24a4705b9ea8ab98 Mon Sep 17 00:00:00 2001 From: pasta Date: Sat, 7 Nov 2026 00:57:27 +0000 Subject: [PATCH 2/4] docs(platform-wallet-storage): say asset_lock_status adds to the degradation snapshot Under Recovery the point read adds one AssetLockStatusDrift per read of a drifted lock to last_load_degradation; under Strict the drift is an error. The snapshot's doc now names it beside load_unowned_identities as a reader that adds, and get_core_tx_record stays the one that never tallies. Co-Authored-By: Claude Opus 5.5 --- .../src/sqlite/persister.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs index 744290b490d..03210918ba2 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/persister.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/persister.rs @@ -444,9 +444,10 @@ impl SqlitePersister { /// database restored from a backup and reloaded clean reports clean. A /// `load()` that returns `Err` leaves it empty — the error is the /// verdict there, not a partial tally. - /// [`load_unowned_identities`](Self::load_unowned_identities) *adds* - /// into the current snapshot, so its counts read as "since the last - /// `load()`". Reading does not clear. The point read + /// [`load_unowned_identities`](Self::load_unowned_identities) and + /// [`asset_lock_status`](Self::asset_lock_status) *add* into the current + /// snapshot, so their counts read as "since the last `load()`". Reading + /// does not clear. The point read /// [`get_core_tx_record`](PlatformWalletPersistence::get_core_tx_record) /// is the exception: its tolerated drift is logged and never tallied, /// because one read per transaction folded into a per-load snapshot @@ -645,6 +646,15 @@ impl SqlitePersister { /// /// Reads the *committed* table: in [`FlushMode::Manual`] a row still in /// the write buffer is not visible yet. + /// + /// Status drift between the typed column and the lifecycle blob follows + /// the load policy: under [`LoadPolicy::Strict`] it is + /// [`WalletStorageError::AssetLockStatusMismatch`]; under + /// [`LoadPolicy::Recovery`] the read returns the reconciled status + /// (consumption is sticky) and *adds* one `AssetLockStatusDrift` to + /// [`last_load_degradation`](Self::last_load_degradation). The tally + /// counts reads, not rows: reading the same drifted lock again counts it + /// again. pub fn asset_lock_status( &self, wallet_id: &WalletId, From a5562c0c5501550eb36fea6c575803e4a0643059 Mon Sep 17 00:00:00 2001 From: pasta Date: Sat, 7 Nov 2026 02:36:27 +0000 Subject: [PATCH 3/4] test(platform-wallet-storage): pin asset_lock_status's drift, DrainCompleted and buffer cases Through the public read: a typed `consumed` over a `Built` blob is AssetLockStatusMismatch under Strict, and Some(Consumed) plus one drift per read under Recovery; a DrainCompleted lock reads back exactly; under Manual flush a stored lock reads None until its flush. Co-Authored-By: Claude Opus 5.5 --- .../integration/sqlite_asset_locks_filter.rs | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/packages/rs-platform-wallet-storage/tests/integration/sqlite_asset_locks_filter.rs b/packages/rs-platform-wallet-storage/tests/integration/sqlite_asset_locks_filter.rs index 709b542a7c9..ba10bacb993 100644 --- a/packages/rs-platform-wallet-storage/tests/integration/sqlite_asset_locks_filter.rs +++ b/packages/rs-platform-wallet-storage/tests/integration/sqlite_asset_locks_filter.rs @@ -389,3 +389,124 @@ fn should_read_an_asset_locks_status_by_outpoint_including_consumed_after_reopen "another wallet's row is not this wallet's" ); } + +/// Plant a row whose typed status says `consumed` while its lifecycle blob +/// says `Built`. +fn plant_consumed_column_over_built_blob( + persister: &platform_wallet_storage::SqlitePersister, + w: &platform_wallet::wallet::platform_wallet::WalletId, + outpoint: OutPoint, +) { + use platform_wallet_storage::sqlite::schema::blob; + ensure_wallet_meta(persister, w); + let conn = persister.lock_conn_for_test(); + conn.execute( + "INSERT INTO asset_locks \ + (wallet_id, outpoint, status, account_index, identity_index, amount_duffs, lifecycle_blob) \ + VALUES (?1, ?2, 'consumed', 0, 0, 1000, ?3)", + rusqlite::params![ + w.as_slice(), + blob::encode_outpoint(&outpoint).unwrap(), + asset_locks::encode_entry_for_test(&entry(outpoint, AssetLockStatus::Built)).unwrap() + ], + ) + .expect("plant asset-lock status drift"); +} + +/// Status drift through the point read: Strict refuses it with the named +/// error; Recovery returns the sticky `Consumed` and adds one drift per +/// read to the degradation snapshot. +#[test] +fn should_follow_the_load_policy_when_a_point_read_meets_status_drift() { + use platform_wallet_storage::LoadSite; + let w = wid(0xA8); + let drifted = op(0x40); + + let (strict, _tmp, _path) = fresh_persister(); + plant_consumed_column_over_built_blob(&strict, &w, drifted); + assert!(matches!( + strict.asset_lock_status(&w, &drifted), + Err(WalletStorageError::AssetLockStatusMismatch { .. }) + )); + assert!(!strict.last_load_degradation().degraded); + + let (recovery, _tmp, _path) = common::fresh_recovery_persister(|strict| { + plant_consumed_column_over_built_blob(strict, &w, drifted); + }); + for reads in 1..=2 { + assert_eq!( + recovery.asset_lock_status(&w, &drifted).unwrap(), + Some(AssetLockStatus::Consumed), + "consumption is sticky" + ); + let degradation = recovery.last_load_degradation(); + assert!(degradation.degraded); + assert_eq!( + degradation + .by_site + .get(&LoadSite::AssetLockStatusDrift) + .copied(), + Some(reads), + "{:?}", + degradation.by_site + ); + assert_eq!(degradation.total, reads); + } +} + +/// A `DrainCompleted` lock reads back as exactly that, though its typed +/// column projects it to `consumed`. +#[test] +fn should_read_a_drain_completed_lock_as_drain_completed() { + let (persister, _tmp, _path) = fresh_persister(); + let w = wid(0xA9); + ensure_wallet_meta(&persister, &w); + let drained = op(0x41); + let mut cs = AssetLockChangeSet::default(); + cs.asset_locks + .insert(drained, entry(drained, AssetLockStatus::DrainCompleted)); + persister + .store( + w, + platform_wallet::changeset::PlatformWalletChangeSet { + asset_locks: Some(cs), + ..Default::default() + }, + ) + .unwrap(); + assert_eq!( + persister.asset_lock_status(&w, &drained).unwrap(), + Some(AssetLockStatus::DrainCompleted) + ); + assert!(!persister.last_load_degradation().degraded); +} + +/// Under `FlushMode::Manual` a stored lock is in the write buffer, not the +/// committed table: the point read sees none until the flush. +#[test] +fn should_not_see_a_buffered_lock_until_its_flush() { + let (persister, _tmp, _path) = common::fresh_persister_with_mode(common::FlushMode::Manual); + let w = wid(0xAA); + ensure_wallet_meta(&persister, &w); + let buffered = op(0x42); + let mut cs = AssetLockChangeSet::default(); + cs.asset_locks.insert( + buffered, + entry(buffered, AssetLockStatus::InstantSendLocked), + ); + persister + .store( + w, + platform_wallet::changeset::PlatformWalletChangeSet { + asset_locks: Some(cs), + ..Default::default() + }, + ) + .unwrap(); + assert_eq!(persister.asset_lock_status(&w, &buffered).unwrap(), None); + persister.flush(w).unwrap(); + assert_eq!( + persister.asset_lock_status(&w, &buffered).unwrap(), + Some(AssetLockStatus::InstantSendLocked) + ); +} From 3038bdf2b121e1be3a9a3f55a97bdd53475f9b6c Mon Sep 17 00:00:00 2001 From: pasta Date: Sat, 7 Nov 2026 00:57:28 +0000 Subject: [PATCH 4/4] perf(platform-wallet-storage): cache the asset-lock status statement status_of prepares through the connection's statement cache, as the other committed-table point reads (get_tx_record) do. Co-Authored-By: Claude Opus 5.5 --- .../rs-platform-wallet-storage/src/sqlite/schema/asset_locks.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/asset_locks.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/asset_locks.rs index 764ebdcb4c5..94e602235b4 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/asset_locks.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/asset_locks.rs @@ -407,7 +407,7 @@ pub fn status_of( ctx: &LoadCtx, ) -> Result, WalletStorageError> { let op_bytes = blob::encode_outpoint(outpoint)?; - let mut stmt = conn.prepare( + let mut stmt = conn.prepare_cached( "SELECT length(outpoint), outpoint, account_index, length(lifecycle_blob), lifecycle_blob, status, operation_id \ FROM asset_locks WHERE wallet_id = ?1 AND outpoint = ?2", )?;