diff --git a/packages/rs-platform-wallet-storage/src/sqlite/rehydrate.rs b/packages/rs-platform-wallet-storage/src/sqlite/rehydrate.rs index 6b0e00b676f..542f02ac9de 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/rehydrate.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/rehydrate.rs @@ -4274,7 +4274,7 @@ mod tests { "the child is recorded in both accounts" ); records.extend(records_of(&mut winner_info, &mut wallet, &winner, block(101)).await); - platform_wallet::test_support::fold_wallet_records(&mut records); + platform_wallet::test_support::fold_wallet_records(&mut records, &Default::default()); let child_outpoint = OutPoint::new(child.txid(), 0); let dir = tempfile::tempdir().unwrap(); #[cfg(unix)] diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/accounts.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/accounts.rs index d7ab594edcd..27a1e0cb0bf 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/accounts.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/accounts.rs @@ -657,6 +657,9 @@ pub(crate) const ACCOUNT_TYPE_LABELS: &[&str] = &[ /// singles out contact watch-only rows binds this instead of a literal. pub(crate) const DASHPAY_EXTERNAL_LABEL: &str = "dashpay_external"; +/// Database label of `AccountType::PlatformPayment`. +pub(crate) const PLATFORM_PAYMENT_LABEL: &str = "platform_payment"; + /// Stable database label for an `AccountType` variant (the `Debug` impl is not /// a stable format; this match is the contract). An added upstream variant /// fails this match's exhaustiveness check at compile time. diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/core_history.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/core_history.rs index 82544eb0b0b..2c7e53ed398 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/core_history.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/core_history.rs @@ -1,6 +1,6 @@ //! Wallet accounting repaired from historical owned outputs, independent of live UTXOs. -use std::collections::{BTreeMap, HashSet}; +use std::collections::{BTreeMap, BTreeSet, HashSet}; use dashcore::hashes::Hash; use dashcore::{Address, OutPoint, ScriptBuf, Txid}; @@ -8,11 +8,13 @@ use key_wallet::managed_account::transaction_record::{ InputDetail, OutputDetail, OutputRole, TransactionRecord, }; use key_wallet::transaction_checking::TransactionContext; -use platform_wallet::changeset::{is_owned, wallet_accounting, CoreChangeSet}; +use platform_wallet::changeset::{ + asset_lock_credit_outputs, is_owned, wallet_accounting, CoreChangeSet, +}; use platform_wallet::wallet::platform_wallet::WalletId; use rusqlite::{params, Connection, OptionalExtension, Transaction}; -use super::accounts::DASHPAY_EXTERNAL_LABEL; +use super::accounts::{DASHPAY_EXTERNAL_LABEL, PLATFORM_PAYMENT_LABEL}; use super::{blob, core_state, wallets}; use crate::sqlite::error::WalletStorageError; use crate::sqlite::load_ctx::LoadCtx; @@ -287,7 +289,8 @@ fn repair_record( record.output_details = outputs.into_values().collect(); // The live projection computes the same accounting, so repair never // flips a row it just wrote; only the overflow policy differs. - let (net, direction) = wallet_accounting(&record); + let credits = owned_credit_scripts(tx, wallet_id, &record)?; + let (net, direction) = wallet_accounting(&record, &credits); record.net_amount = match i64::try_from(net) { Ok(net) => net, // Nothing authenticates a mempool transaction: this is peer-supplied data, @@ -324,8 +327,44 @@ fn repair_record( Ok(()) } +/// The credit scripts of `record`'s asset-lock payload that a persisted +/// address pool of the wallet's own Core accounts holds. That is the live +/// projection's domain: key-wallet's `all_accounts()` minus a contact's +/// watch-only chain, so `PlatformPayment` pools (held outside it) do not +/// count either. The live projection persists the pool entry of every credit +/// key it counts before the record, so this sees what the live accounting saw. +fn owned_credit_scripts( + conn: &Connection, + wallet_id: &WalletId, + record: &TransactionRecord, +) -> Result, WalletStorageError> { + let mut owned = BTreeSet::new(); + let credits = asset_lock_credit_outputs(&record.transaction); + if credits.is_empty() { + return Ok(owned); + } + let mut probe = conn.prepare_cached( + "SELECT EXISTS(SELECT 1 FROM core_address_pool \ + WHERE wallet_id = ?1 AND script = ?2 AND account_type NOT IN (?3, ?4))", + )?; + for output in credits { + let key = params![ + wallet_id.as_slice(), + output.script_pubkey.as_bytes(), + DASHPAY_EXTERNAL_LABEL, + PLATFORM_PAYMENT_LABEL + ]; + if probe.query_row(key, |row| row.get::<_, bool>(0))? { + owned.insert(output.script_pubkey.clone()); + } + } + Ok(owned) +} + #[cfg(test)] mod tests { + use dashcore::transaction::special_transaction::asset_lock::AssetLockPayload; + use dashcore::transaction::TransactionPayload; use dashcore::TxOut; use key_wallet::account::{AccountType, StandardAccountType}; use key_wallet::managed_account::transaction_record::TransactionDirection; @@ -338,6 +377,7 @@ mod tests { use super::*; use crate::sqlite::migrations::oversize_script_db; + use crate::sqlite::schema::accounts::account_type_db_label; const WALLET_ID: WalletId = [0x5Au8; 32]; @@ -523,28 +563,40 @@ mod tests { ); } - /// Shared with the Swift SDK's `TransactionAccountingTests` direction - /// table; pins the rule repair takes from `platform_wallet`. + /// The direction case table; pins the rule repair takes from + /// `platform_wallet`, which Swift's history reconciliation + /// (dashpay/platform#5417) consumes. #[test] fn should_classify_repaired_direction_like_the_swift_sdk() { use TransactionDirection::{CoinJoin, Incoming, Internal, Outgoing}; use TransactionType::{AssetLock, Standard}; - // (type, spends ours, has owned output, has external output, expected) + // (type, spends ours, has owned output, has external output, + // owns every credit, expected) let cases = [ - (Standard, true, true, false, Internal), - (Standard, true, true, true, Outgoing), - (Standard, true, false, false, Outgoing), - (AssetLock, true, false, false, Internal), - (AssetLock, true, true, false, Internal), - (AssetLock, true, false, true, Outgoing), - (Standard, false, true, false, Incoming), - (TransactionType::CoinJoin, true, true, false, CoinJoin), + (Standard, true, true, false, false, Internal), + (Standard, true, true, true, false, Outgoing), + (Standard, true, false, false, false, Outgoing), + (AssetLock, true, false, false, true, Internal), + (AssetLock, true, true, false, true, Internal), + (AssetLock, true, false, true, true, Outgoing), + (AssetLock, true, false, false, false, Outgoing), + (AssetLock, true, true, false, false, Outgoing), + (Standard, false, true, false, false, Incoming), + ( + TransactionType::CoinJoin, + true, + true, + false, + false, + CoinJoin, + ), ]; - for (kind, spends_ours, has_ours, has_external, expected) in cases { + for (kind, spends_ours, has_ours, has_external, owns_credits, expected) in cases { assert_eq!( - wallet_direction(kind, spends_ours, has_ours, has_external), + wallet_direction(kind, spends_ours, has_ours, has_external, owns_credits), expected, - "{kind:?} spends_ours={spends_ours} has_ours={has_ours} has_external={has_external}" + "{kind:?} spends_ours={spends_ours} has_ours={has_ours} \ + has_external={has_external} owns_credits={owns_credits}" ); } } @@ -626,11 +678,42 @@ mod tests { ) } + /// `record` with an asset-lock payload whose credit outputs pay `credits`. + fn with_credits(mut record: TransactionRecord, credits: &[Address]) -> TransactionRecord { + let credit_outputs = credits + .iter() + .map(|credit| pay(LOCK_CREDIT / credits.len() as u64, credit)) + .collect(); + record.transaction.special_transaction_payload = Some( + TransactionPayload::AssetLockPayloadType(AssetLockPayload::new(credit_outputs)), + ); + record.txid = record.transaction.txid(); + record + } + + /// A persisted address-pool row holding `script` for `account_type`. + fn pool_row(conn: &Connection, account_type: &AccountType, index: u32, script: &ScriptBuf) { + conn.execute( + "INSERT INTO core_address_pool \ + (wallet_id, account_type, account_index, pool_type, address_index, script) \ + VALUES (?1, ?2, 0, 2, ?3, ?4)", + params![ + &WALLET_ID[..], + account_type_db_label(account_type), + index, + script.as_bytes() + ], + ) + .unwrap(); + } + /// The live projection and the SQLite repair must agree on a row's /// net and direction, or the row flips each time storage repairs /// what the live path just wrote. Covers the asset-lock shapes (no - /// change, change, paying an external output) and a plain - /// cross-account transfer. + /// change, change, paying an external output), credits paying the + /// wallet's own, a foreign, a mixed or a contact's key, and a plain + /// cross-account transfer. Live credit ownership comes from the + /// wallet's keys, repair's from the persisted pool rows of those keys. #[test] fn should_repair_live_folded_records_without_changing_their_accounting() { let burn = || ScriptBuf::new_op_return(&[]); @@ -680,46 +763,104 @@ mod tests { transfer_receiver.net_amount = (FUNDING - LOCK_FEE) as i64; let lock_net = -((LOCK_CREDIT + LOCK_FEE) as i64); + let (own, foreign, contact, platform) = (address(5), address(6), address(8), address(10)); + let lock = |funding: &TransactionRecord, credits: &[Address]| { + let funding = with_credits(funding.clone(), credits); + let keys = keys_marker(&funding); + vec![funding, keys] + }; + use TransactionDirection::{Internal, Outgoing}; let cases = [ ( "asset lock, no change", - vec![lock_no_change.clone(), keys_marker(&lock_no_change)], - TransactionDirection::Internal, + lock(&lock_no_change, std::slice::from_ref(&own)), + Internal, -(FUNDING as i64), ), ( "asset lock, change", - vec![lock_change.clone(), keys_marker(&lock_change)], - TransactionDirection::Internal, + lock(&lock_change, std::slice::from_ref(&own)), + Internal, lock_net, ), ( "asset lock, no change, funding slice alone", - vec![lock_no_change.clone()], - TransactionDirection::Internal, + vec![with_credits( + lock_no_change.clone(), + std::slice::from_ref(&own), + )], + Internal, -(FUNDING as i64), ), ( "asset lock paying an external output", - vec![lock_external.clone(), keys_marker(&lock_external)], - TransactionDirection::Outgoing, + lock(&lock_external, std::slice::from_ref(&own)), + Outgoing, lock_net - 5_000, ), + ( + "asset lock, foreign credit", + lock(&lock_change, std::slice::from_ref(&foreign)), + Outgoing, + lock_net, + ), + ( + "asset lock, mixed credits", + lock(&lock_change, &[own.clone(), foreign]), + Outgoing, + lock_net, + ), + ( + "asset lock, contact's credit", + lock(&lock_change, std::slice::from_ref(&contact)), + Outgoing, + lock_net, + ), + ( + "asset lock, platform-payment credit", + lock(&lock_change, std::slice::from_ref(&platform)), + Outgoing, + lock_net, + ), + ( + "asset lock, no payload", + vec![lock_change.clone(), keys_marker(&lock_change)], + Outgoing, + lock_net, + ), ( "cross-account transfer", vec![transfer, transfer_receiver], - TransactionDirection::Internal, + Internal, -(LOCK_FEE as i64), ), ]; + let contact_account = AccountType::DashpayExternalAccount { + index: 0, + user_identity_id: [1; 32], + friend_identity_id: [2; 32], + }; for (name, mut records, direction, net) in cases { - fold_wallet_records(&mut records); + let owned_credits = BTreeSet::from([own.script_pubkey()]); + fold_wallet_records(&mut records, &owned_credits); assert_eq!(records.len(), 1, "{name}"); let live = &records[0]; assert_eq!(live.direction, direction, "{name}: live direction"); assert_eq!(live.net_amount, net, "{name}: live net"); let mut conn = wallet_db("testnet"); + pool_row( + &conn, + &AccountType::IdentityRegistration, + 0, + &own.script_pubkey(), + ); + pool_row(&conn, &contact_account, 0, &contact.script_pubkey()); + let platform_account = AccountType::PlatformPayment { + account: 0, + key_class: 0, + }; + pool_row(&conn, &platform_account, 0, &platform.script_pubkey()); store(&conn, live); let tx = conn.transaction().unwrap(); repair_record(&tx, &WALLET_ID, &live.txid, dashcore::Network::Testnet).unwrap(); diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/core_pool.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/core_pool.rs index 401756f9161..f7f3936b94c 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/core_pool.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/core_pool.rs @@ -11,9 +11,14 @@ //! from the `account_address_pools` changeset snapshots; the UTXO writer reads //! it back to attribute an outpoint to its owning account. +use std::collections::BTreeSet; + +use key_wallet::AddressInfo; use rusqlite::{params, Connection, Transaction}; -use platform_wallet::changeset::AccountAddressPoolEntry; +use platform_wallet::changeset::{ + asset_lock_credit_outputs, AccountAddressPoolEntry, CoreChangeSet, +}; use platform_wallet::wallet::platform_wallet::WalletId; use key_wallet::managed_account::address_pool::{ @@ -72,6 +77,12 @@ const UPSERT_POOL_SQL: &str = "INSERT INTO core_address_pool \ ELSE excluded.reserved_at \ END"; +const INSERT_POOL_ROW_SQL: &str = "INSERT INTO core_address_pool \ + (wallet_id, account_type, account_index, key_class, user_identity_id, friend_identity_id, \ + pool_type, address_index, script, used, public_key, key_type, reserved_at) \ + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13) \ + ON CONFLICT DO NOTHING"; + const TYPED_POOL_CONFLICT_SQL: &str = "SELECT EXISTS( \ SELECT 1 FROM core_address_pool \ WHERE wallet_id = ?1 AND account_type = ?2 AND account_index = ?3 \ @@ -107,12 +118,35 @@ pub fn apply_pools( tx: &Transaction<'_>, wallet_id: &WalletId, pools: &[AccountAddressPoolEntry], +) -> Result<(), WalletStorageError> { + apply_pool_rows(tx, wallet_id, pools, PoolWrite::Snapshot) +} + +/// How [`apply_pool_rows`] writes its rows. +#[derive(Clone, Copy, PartialEq, Eq)] +enum PoolWrite { + /// Upsert a pool snapshot; contradicting stored key material fails. + Snapshot, + /// Insert ownership evidence only where no row exists. A row that would + /// contradict stored key material is skipped: evidence must not fail + /// the Core write it rides in. + Evidence, +} + +fn apply_pool_rows( + tx: &Transaction<'_>, + wallet_id: &WalletId, + pools: &[AccountAddressPoolEntry], + write: PoolWrite, ) -> Result<(), WalletStorageError> { if pools.is_empty() { return Ok(()); } let mut conflict_stmt = tx.prepare_cached(TYPED_POOL_CONFLICT_SQL)?; - let mut upsert_stmt = tx.prepare_cached(UPSERT_POOL_SQL)?; + let mut upsert_stmt = tx.prepare_cached(match write { + PoolWrite::Snapshot => UPSERT_POOL_SQL, + PoolWrite::Evidence => INSERT_POOL_ROW_SQL, + })?; for entry in pools { // `account_type` discriminates accounts that collapse to the same // `(account_index, key_class)` sentinel (e.g. `IdentityRegistration` @@ -173,6 +207,14 @@ pub fn apply_pools( ], |row| row.get::<_, bool>(0), )?; + if conflicts && write == PoolWrite::Evidence { + tracing::warn!( + account_type, + address_index = info.index, + "asset-lock credit key contradicts the stored pool key; kept the stored row" + ); + continue; + } if conflicts { return Err(WalletStorageError::TypedPoolKeyConflict { account_type, @@ -205,6 +247,61 @@ pub fn apply_pools( Ok(()) } +/// Insert the pool rows of the wallet's own keys that `cs`'s asset-lock +/// credits pay, so the repair's credit ownership sees what the live +/// projection saw. Rows already stored are left untouched: the projection +/// reads a wallet snapshot that may be older than a stored reservation. See +/// [`PoolWrite::Evidence`] for a row contradicting stored key material. +pub(super) fn apply_credit_key_rows( + tx: &Transaction<'_>, + wallet_id: &WalletId, + cs: &CoreChangeSet, +) -> Result<(), WalletStorageError> { + let credits: BTreeSet<_> = cs + .records + .iter() + .flat_map(|record| asset_lock_credit_outputs(&record.transaction)) + .map(|output| &output.script_pubkey) + .collect(); + if credits.is_empty() { + return Ok(()); + } + let derived = cs + .addresses_derived + .iter() + .filter(|derived| credits.contains(&derived.address.script_pubkey())) + .map(|derived| AccountAddressPoolEntry { + account_type: derived.account_type, + pool_type: derived.pool_type, + addresses: vec![AddressInfo { + address: derived.address.clone(), + script_pubkey: derived.address.script_pubkey(), + public_key: Some(PublicKeyType::ECDSA(derived.public_key.to_bytes())), + index: derived.derivation_index, + // The writer stores only the key, index, script and state. + path: Default::default(), + state: AddressState::Available, + tx_count: 0, + total_received: 0, + total_sent: 0, + balance: 0, + label: None, + metadata: Default::default(), + }], + }); + let used = cs + .addresses_marked_used + .iter() + .filter(|used| credits.contains(&used.info.script_pubkey)) + .map(|used| AccountAddressPoolEntry { + account_type: used.account_type, + pool_type: used.pool_type, + addresses: vec![used.info.clone()], + }); + let rows: Vec<_> = derived.chain(used).collect(); + apply_pool_rows(tx, wallet_id, &rows, PoolWrite::Evidence) +} + /// One restored typed-pool row: `(address_index, script_bytes, public_key, used)`. /// /// `reserved_at` is not part of the row: [`restore_pools`] restores it for diff --git a/packages/rs-platform-wallet-storage/src/sqlite/schema/core_state.rs b/packages/rs-platform-wallet-storage/src/sqlite/schema/core_state.rs index 19570322b88..932ce7abb25 100644 --- a/packages/rs-platform-wallet-storage/src/sqlite/schema/core_state.rs +++ b/packages/rs-platform-wallet-storage/src/sqlite/schema/core_state.rs @@ -19,7 +19,7 @@ use crate::sqlite::error::WalletStorageError; use crate::sqlite::load_ctx::{LoadCtx, LoadSite}; use crate::sqlite::schema::blob::impl_persistable_blob; use crate::sqlite::schema::core_history; -use crate::sqlite::schema::core_pool::{owning_account_for_script, OwningAccount}; +use crate::sqlite::schema::core_pool::{self, owning_account_for_script, OwningAccount}; use crate::sqlite::schema::{asset_locks, blob, dashpay}; // PUBLIC material only: core-chain state reaching `record_blob` / @@ -83,6 +83,11 @@ pub fn apply( wallet_id: &WalletId, cs: &CoreChangeSet, ) -> Result<(), WalletStorageError> { + // Before the records: their repair reads credit ownership from the pool. + // Other `addresses_derived` entries are NOT persisted — the pool + // snapshot (`account_address_pools`) is the derived-address source used + // to resolve UTXO ownership during rehydration. + core_pool::apply_credit_key_rows(tx, wallet_id, cs)?; if !cs.records.is_empty() { let mut stmt = tx.prepare_cached( "INSERT INTO core_transactions \ @@ -115,9 +120,6 @@ pub fn apply( core_history::index_record(tx, wallet_id, &record)?; } } - // `addresses_derived` is intentionally NOT persisted here — the pool - // snapshot (`account_address_pools`) is the derived-address source used - // to resolve UTXO ownership during rehydration. if !cs.new_utxos.is_empty() { // Blob-bearing records always win; height-only writes never overwrite them. // Placeholder confirmation is `height IS NOT NULL`; `finalized` stays 0. diff --git a/packages/rs-platform-wallet-storage/tests/integration/main.rs b/packages/rs-platform-wallet-storage/tests/integration/main.rs index 4c9e3c4cfd0..264ad81540f 100644 --- a/packages/rs-platform-wallet-storage/tests/integration/main.rs +++ b/packages/rs-platform-wallet-storage/tests/integration/main.rs @@ -34,6 +34,7 @@ mod sqlite_contacts_keys_rehydration; mod sqlite_core_pool_writer; mod sqlite_core_rescan; mod sqlite_core_state_reader; +mod sqlite_credit_direction; mod sqlite_dashpay_overlay_contract; mod sqlite_delete_buffer_reconcile; mod sqlite_delete_cross_process_exclusion; diff --git a/packages/rs-platform-wallet-storage/tests/integration/sqlite_core_pool_writer.rs b/packages/rs-platform-wallet-storage/tests/integration/sqlite_core_pool_writer.rs index 4a8922b3da4..fa3b436cd87 100644 --- a/packages/rs-platform-wallet-storage/tests/integration/sqlite_core_pool_writer.rs +++ b/packages/rs-platform-wallet-storage/tests/integration/sqlite_core_pool_writer.rs @@ -8,8 +8,12 @@ use crate::common; use common::{ensure_wallet_meta, fresh_persister, wid}; +use dashcore::transaction::special_transaction::asset_lock::AssetLockPayload; +use dashcore::transaction::TransactionPayload; use key_wallet::account::{AccountType, StandardAccountType}; use key_wallet::managed_account::address_pool::{AddressPoolType, AddressState, PublicKeyType}; +use key_wallet::managed_account::transaction_record::{TransactionDirection, TransactionRecord}; +use key_wallet::transaction_checking::{DerivedAddressInfo, TransactionContext, TransactionType}; use key_wallet::wallet::initialization::WalletAccountCreationOptions; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; use key_wallet::wallet::Wallet; @@ -19,6 +23,8 @@ use platform_wallet::changeset::{ PlatformWalletPersistence, ProviderKeyAccountEntry, ProviderKeyExtendedPubKey, WalletMetadataEntry, }; +use platform_wallet::key_wallet_manager::DerivedAddress; +use platform_wallet::test_support::core_fixtures::{outside, pay, spend_of}; use platform_wallet::wallet::platform_wallet::WalletId; use platform_wallet_storage::sqlite::schema::{core_pool, core_state}; use platform_wallet_storage::{SqlitePersister, SqlitePersisterConfig, WalletStorageError}; @@ -1286,3 +1292,146 @@ fn typed_pool_key_at_fresh_index_succeeds() { fresh.public_key ); } + +/// An asset-lock record whose single credit output pays `credit`. +fn credit_lock(credit: &AddressInfo) -> TransactionRecord { + let mut body = spend_of(&[outside(71)], vec![]); + body.version = 3; + body.special_transaction_payload = Some(TransactionPayload::AssetLockPayloadType( + AssetLockPayload::new(vec![pay(1, &credit.address)]), + )); + TransactionRecord::new( + body, + AccountType::IdentityRegistration, + TransactionContext::Mempool, + TransactionType::AssetLock, + TransactionDirection::Internal, + vec![], + vec![], + 0, + ) +} + +fn derived(account_type: AccountType, info: &AddressInfo) -> DerivedAddress { + let Some(PublicKeyType::ECDSA(bytes)) = &info.public_key else { + panic!("ECDSA fixture") + }; + DerivedAddress { + account_type, + pool_type: AddressPoolType::External, + derivation_index: info.index, + address: info.address.clone(), + public_key: dashcore::PublicKey::from_slice(bytes).unwrap(), + } +} + +fn pool_state(persister: &SqlitePersister, wallet_id: &WalletId) -> Vec<(u32, bool, Option)> { + let conn = persister.lock_conn_for_test(); + let mut stmt = conn + .prepare( + "SELECT address_index, used, reserved_at FROM core_address_pool \ + WHERE wallet_id = ?1 ORDER BY address_index", + ) + .unwrap(); + stmt.query_map([wallet_id.as_slice()], |r| { + Ok((r.get(0)?, r.get(1)?, r.get(2)?)) + }) + .unwrap() + .map(Result::unwrap) + .collect() +} + +/// The pool rows of the keys an asset lock's credits pay are stored with +/// the record; derived or used addresses no credit pays are not. A stored +/// row is never rewritten: the projection's wallet snapshot may predate a +/// reservation. A row contradicting stored key material is skipped without +/// failing the rest of the Core write. +#[test] +fn should_store_credit_key_rows_without_rewriting_stored_ones() { + let (persister, _dir, _) = fresh_persister(); + let wallet_id = wid(91); + ensure_wallet_meta(&persister, &wallet_id); + let account = AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }; + let infos = external_infos(21); + let (credit, other) = (&infos[0], &infos[1]); + let mut other_used = other.clone(); + other_used.state = AddressState::Used; + let emit = |core| { + persister.store( + wallet_id, + PlatformWalletChangeSet { + core: Some(core), + ..Default::default() + }, + ) + }; + let observed = CoreChangeSet { + records: vec![credit_lock(credit)], + addresses_derived: vec![derived(account, credit), derived(account, other)], + addresses_marked_used: vec![DerivedAddressInfo { + account_type: account, + pool_type: AddressPoolType::External, + info: other_used, + }], + ..Default::default() + }; + let mut recordless = observed.clone(); + recordless.records.clear(); + emit(recordless).unwrap(); + assert_eq!( + pool_state(&persister, &wallet_id), + vec![], + "no credit, no row" + ); + + emit(observed.clone()).unwrap(); + assert_eq!( + pool_state(&persister, &wallet_id), + vec![(credit.index, false, None)], + "only the credit key's row is stored" + ); + + let mut reserved = credit.clone(); + reserved.state = AddressState::Reserved { at: 9876 }; + persister + .store( + wallet_id, + PlatformWalletChangeSet { + account_address_pools: vec![pool_entry( + account, + AddressPoolType::External, + vec![reserved], + )], + ..Default::default() + }, + ) + .unwrap(); + emit(observed.clone()).unwrap(); + assert_eq!( + pool_state(&persister, &wallet_id), + vec![(credit.index, false, Some(9876))], + "a late observation cannot release a newer reservation" + ); + + let mut conflicting = observed; + conflicting.addresses_derived = vec![derived(account, &external_infos(22)[0])]; + conflicting.addresses_derived[0].derivation_index = credit.index; + conflicting.addresses_derived[0].address = credit.address.clone(); + let txid = conflicting.records[0].txid; + emit(conflicting).expect("conflicting evidence must not fail the Core write"); + assert_eq!( + pool_state(&persister, &wallet_id), + vec![(credit.index, false, Some(9876))], + "the stored row stands" + ); + assert!( + persister + .get_core_tx_record(wallet_id, &txid) + .unwrap() + .is_some(), + "the record is still persisted" + ); +} diff --git a/packages/rs-platform-wallet-storage/tests/integration/sqlite_core_rescan.rs b/packages/rs-platform-wallet-storage/tests/integration/sqlite_core_rescan.rs index 0a6148068c0..6fc3394e2a6 100644 --- a/packages/rs-platform-wallet-storage/tests/integration/sqlite_core_rescan.rs +++ b/packages/rs-platform-wallet-storage/tests/integration/sqlite_core_rescan.rs @@ -305,12 +305,13 @@ fn assert_rebuilt( -61_000, TransactionDirection::Outgoing, ), + // The lock's credit pays a foreign key. ( "asset lock", &chain.lock, vec![input(20_000)], -20_000, - TransactionDirection::Internal, + TransactionDirection::Outgoing, ), ]; for (case, tx, inputs, net, direction) in cases { @@ -425,7 +426,8 @@ async fn should_keep_unconsumed_asset_locks_when_a_wiped_wallet_loads_and_rescan assert_eq!( stored(&path, &wallet_id, &chain.lock).direction, - TransactionDirection::Internal + TransactionDirection::Outgoing, + "the lock's credit pays a foreign key" ); let after: Vec<_> = state.wallets[&wallet_id] .unused_asset_locks diff --git a/packages/rs-platform-wallet-storage/tests/integration/sqlite_credit_direction.rs b/packages/rs-platform-wallet-storage/tests/integration/sqlite_credit_direction.rs new file mode 100644 index 00000000000..a875c58d16c --- /dev/null +++ b/packages/rs-platform-wallet-storage/tests/integration/sqlite_credit_direction.rs @@ -0,0 +1,285 @@ +//! Asset-lock direction from credit ownership, end to end: a wallet-signed +//! lock through the live event adapter, the SQLite store and its repair, and +//! a reload. + +use dashcore::transaction::special_transaction::asset_lock::AssetLockPayload; +use dashcore::transaction::TransactionPayload; +use dashcore::{Network, OutPoint, ScriptBuf, Transaction}; +use key_wallet::account::AccountType; +use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; +use key_wallet::managed_account::transaction_record::TransactionDirection; +use key_wallet::wallet::initialization::WalletAccountCreationOptions; +use key_wallet::wallet::managed_wallet_info::ManagedAccountOperations; +use platform_wallet::changeset::{AccountAddressPoolEntry, PlatformWalletPersistence}; +use platform_wallet::key_wallet_manager::{WalletInterface, WalletManager}; +use platform_wallet::test_support::core_fixtures::{foreign_address, outside, pay, spend_of}; +use platform_wallet::wallet::platform_wallet::PlatformWalletInfo; +use platform_wallet_storage::{SqlitePersister, SqlitePersisterConfig}; +use std::sync::Arc; +use tokio::sync::RwLock; + +use crate::common::{self, mine, persist_events, registration, Manager}; + +struct Fixture { + persister: Arc, + manager: Manager, + wallet_id: [u8; 32], + transaction: Transaction, + initial_pool_rows: i64, + _directory: tempfile::TempDir, + path: std::path::PathBuf, +} + +/// A wallet registered with its initial pools and a mined coin, then an +/// asset lock it signs, observed in the mempool and persisted, whose credits +/// pay `case`'s keys. `deep` first derives 80 more registration keys in +/// memory only, so the credit key is missing from the persisted pool. +async fn observed_lock(case: &str, deep: bool) -> Fixture { + let (persister, directory, path) = common::fresh_persister(); + let persister = Arc::new(persister); + let mut wm = WalletManager::::new(Network::Testnet); + let mut events = wm.take_persistence_receiver().unwrap(); + let wallet_id = wm.create_wallet_from_mnemonic( + "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", + 0, WalletAccountCreationOptions::Default, + ).unwrap(); + let contact_type = AccountType::DashpayExternalAccount { + index: 0, + user_identity_id: [3; 32], + friend_identity_id: [4; 32], + }; + if case == "contact" { + wm.create_account(&wallet_id, contact_type, None).unwrap(); + let wallet = wm.get_wallet(&wallet_id).unwrap().clone(); + wm.get_wallet_info_mut(&wallet_id) + .unwrap() + .core_wallet + .add_managed_account(&wallet, contact_type) + .unwrap(); + } + let wallet = wm.get_wallet(&wallet_id).unwrap().clone(); + let info = wm.get_wallet_info_mut(&wallet_id).unwrap(); + let address = info + .core_wallet + .accounts + .standard_bip44_accounts + .get_mut(&0) + .unwrap() + .next_receive_address( + Some(&wallet.accounts.standard_bip44_accounts[&0].account_xpub), + true, + ) + .unwrap(); + let initial_pools: Vec<_> = info + .core_wallet + .accounts + .all_accounts() + .into_iter() + .flat_map(|account| { + let account_type = account.managed_account_type().to_account_type(); + account + .managed_account_type() + .address_pools() + .into_iter() + .map(move |pool| AccountAddressPoolEntry { + account_type, + pool_type: pool.pool_type, + addresses: pool.addresses.values().cloned().collect(), + }) + .collect::>() + }) + .collect(); + let key_account = info + .core_wallet + .accounts + .identity_registration + .as_mut() + .unwrap(); + if deep { + let xpub = wallet + .accounts + .identity_registration + .as_ref() + .unwrap() + .account_xpub; + for _ in 0..80 { + let used = key_account.next_address(Some(&xpub), true).unwrap(); + key_account.mark_address_used(&used); + } + } + let pools = key_account.managed_account_type().address_pools(); + let last = pools[0] + .addresses + .iter() + .max_by_key(|(index, _)| *index) + .unwrap() + .1 + .address + .clone(); + let first = pools[0].addresses[&0].address.clone(); + let watched = info + .core_wallet + .accounts + .dashpay_external_accounts + .values() + .next() + .map(|account| { + account.managed_account_type().address_pools()[0] + .address_at_index(0) + .unwrap() + }); + let credits = match case { + "own" => vec![last.clone()], + "contact" => vec![watched.unwrap()], + "two owned" => vec![first, last.clone()], + "mixed" => vec![last.clone(), foreign_address(92)], + _ => vec![foreign_address(92)], + }; + let mut registered = registration(&wallet, None); + registered.account_address_pools = initial_pools; + persister.store(wallet_id, registered).unwrap(); + let initial_pool_rows = pool_rows(&persister); + let funding = spend_of(&[outside(31)], vec![pay(100_000, &address)]); + let manager = Arc::new(RwLock::new(wm)); + mine(&manager, wallet_id, funding.clone(), 100).await; + persist_events(&manager, &persister, &mut events).await; + let mut transaction = spend_of( + &[OutPoint::new(funding.txid(), 0)], + vec![ + dashcore::TxOut { + value: 60_000, + script_pubkey: ScriptBuf::new_op_return(&[]), + }, + pay(39_000, &address), + ], + ); + transaction.version = 3; + transaction.special_transaction_payload = Some(TransactionPayload::AssetLockPayloadType( + AssetLockPayload::new( + credits + .iter() + .map(|address| pay(60_000 / credits.len() as u64, address)) + .collect(), + ), + )); + { + let wm = manager.read().await; + transaction = common::authorize( + wm.get_wallet(&wallet_id).unwrap(), + &wm.get_wallet_info(&wallet_id).unwrap().core_wallet, + &address, + transaction, + ); + } + assert!( + manager + .write() + .await + .process_mempool_transaction(&transaction, None) + .await + .is_relevant + ); + persist_events(&manager, &persister, &mut events).await; + Fixture { + persister, + manager, + wallet_id, + transaction, + initial_pool_rows, + _directory: directory, + path, + } +} + +fn pool_rows(persister: &SqlitePersister) -> i64 { + persister + .lock_conn_for_test() + .query_row("SELECT COUNT(*) FROM core_address_pool", [], |row| { + row.get(0) + }) + .unwrap() +} + +/// Live projection, repair and reload agree: `Internal` only when every +/// credit pays a key of the wallet's own (non-contact) accounts. +#[tokio::test] +async fn should_persist_and_restore_asset_lock_credit_direction() { + for case in ["own", "foreign", "mixed", "two owned", "contact"] { + let fixture = observed_lock(case, false).await; + let expected = if matches!(case, "own" | "two owned") { + TransactionDirection::Internal + } else { + TransactionDirection::Outgoing + }; + let row = fixture + .persister + .get_core_tx_record(fixture.wallet_id, &fixture.transaction.txid()) + .unwrap() + .unwrap(); + assert_eq!( + (row.direction, row.net_amount), + (expected, -61_000), + "{case}" + ); + fixture.persister.load().unwrap(); + let restored = fixture + .persister + .get_core_tx_record(fixture.wallet_id, &fixture.transaction.txid()) + .unwrap() + .unwrap(); + assert_eq!( + (restored.direction, restored.net_amount), + (expected, -61_000), + "{case} reload" + ); + } +} + +/// A credit key the wallet derived past its persisted pool snapshot is +/// stored with the lock, so the repair and a reopened store still read the +/// wallet's own lock as `Internal`. +#[tokio::test] +async fn should_keep_a_lock_internal_when_its_credit_key_is_past_the_persisted_pool() { + let fixture = observed_lock("own", true).await; + assert!( + pool_rows(&fixture.persister) > fixture.initial_pool_rows, + "new credit-key evidence must persist with its record" + ); + let Fixture { + persister, + manager, + wallet_id, + transaction, + _directory, + path, + .. + } = fixture; + drop(manager); + drop(persister); + let persister = SqlitePersister::open(SqlitePersisterConfig::new(path)).unwrap(); + let loaded = persister.load().unwrap(); + let Some(TransactionPayload::AssetLockPayloadType(payload)) = + &transaction.special_transaction_payload + else { + panic!("asset lock fixture") + }; + assert!( + loaded.wallets[&wallet_id] + .wallet_info + .accounts + .all_accounts() + .iter() + .any(|account| account + .managed_account_type() + .contains_script_pub_key(&payload.credit_outputs[0].script_pubkey)), + "the deep credit key is restored into the real managed wallet" + ); + let row = persister + .get_core_tx_record(wallet_id, &transaction.txid()) + .unwrap() + .unwrap(); + assert_eq!( + (row.direction, row.net_amount), + (TransactionDirection::Internal, -61_000) + ); +} diff --git a/packages/rs-platform-wallet/src/changeset/changeset.rs b/packages/rs-platform-wallet/src/changeset/changeset.rs index 7a93cb94b6e..1be9dd0fde2 100644 --- a/packages/rs-platform-wallet/src/changeset/changeset.rs +++ b/packages/rs-platform-wallet/src/changeset/changeset.rs @@ -24,7 +24,7 @@ use std::collections::{BTreeMap, BTreeSet}; use dashcore::blockdata::transaction::{OutPoint, Transaction}; use dashcore::ephemerealdata::chain_lock::ChainLock; use dashcore::ephemerealdata::instant_lock::InstantLock; -use dashcore::Txid; +use dashcore::{ScriptBuf, Txid}; use dash_sdk::platform::address_sync::AddressFunds; use dpp::prelude::AssetLockProof; @@ -472,7 +472,7 @@ impl HighestUsedIndexes { /// stays out: an asset lock nets `−(credit + fee)`, not `−fee`. /// - `direction` is [`wallet_direction`](super::wallet_direction): account-local slices that said /// `Outgoing`/`Incoming` for a cross-account move become `Internal`, and -/// an asset lock with no change is `Internal` like one with change. +/// an asset lock is `Internal` only when every payload credit is owned. /// Deriving direction from the net's sign instead would erase /// `Internal` and `CoinJoin`. /// - A group or record with no details at all (keys markers only) keeps @@ -484,10 +484,13 @@ impl HighestUsedIndexes { /// ahead of the folded transaction. Contact-watch-only records never /// reach here (filtered at projection — see /// `core_bridge::is_contact_watch_only`). -pub(crate) fn fold_same_txid_records(records: &mut Vec) { +pub(crate) fn fold_same_txid_records( + records: &mut Vec, + owned_credit_scripts: &BTreeSet, +) { fold_groups(records); for record in records.iter_mut() { - apply_wallet_accounting(record); + apply_wallet_accounting(record, owned_credit_scripts); } } diff --git a/packages/rs-platform-wallet/src/changeset/core_bridge.rs b/packages/rs-platform-wallet/src/changeset/core_bridge.rs index 8d588b64817..a53a33692d0 100644 --- a/packages/rs-platform-wallet/src/changeset/core_bridge.rs +++ b/packages/rs-platform-wallet/src/changeset/core_bridge.rs @@ -46,14 +46,16 @@ use std::sync::{Arc, Mutex, Weak}; use dashcore::blockdata::transaction::{txout::TxOut, OutPoint}; use dashcore::ephemerealdata::instant_lock::InstantLock; -use dashcore::Txid; +use dashcore::{PublicKey, ScriptBuf, Txid}; use key_wallet::account::AccountType; -use key_wallet::managed_account::address_pool::{AddressPool, AddressPoolType, AddressState}; +use key_wallet::managed_account::address_pool::{ + AddressPool, AddressPoolType, AddressState, PublicKeyType, +}; use key_wallet::managed_account::transaction_record::{OutputRole, TransactionRecord}; use key_wallet::transaction_checking::transaction_router::AccountTypeToCheck; use key_wallet::transaction_checking::{DerivedAddressInfo, TransactionContext}; use key_wallet::Utxo; -use key_wallet_manager::{WalletEvent, WalletId, WalletManager}; +use key_wallet_manager::{DerivedAddress, WalletEvent, WalletId, WalletManager}; use tokio::sync::mpsc; use tokio::sync::mpsc::error::TryRecvError; use tokio::sync::RwLock; @@ -68,6 +70,7 @@ use crate::changeset::changeset::{ use crate::changeset::merge::Merge; use crate::changeset::persistence_capabilities::PersistenceCapabilities; use crate::changeset::traits::PlatformWalletPersistence; +use crate::changeset::wallet_accounting::asset_lock_credit_outputs; use crate::wallet::asset_lock::sync::reconstruction; use crate::wallet::identity::network::sent_payment_status_for_record; use crate::wallet::identity::types::dashpay::payment::{ @@ -1629,7 +1632,10 @@ async fn instant_lock_changeset( .filter(|r| !is_contact_watch_only(r)) .collect(); cs.records = cs.account_records.clone(); - fold_same_txid_records(&mut cs.records); + let credits = credit_ownership(info, &cs.records); + fold_same_txid_records(&mut cs.records, &credits.scripts); + cs.addresses_marked_used = credits.used; + cs.addresses_derived = credits.derived; cs.utxo_credit_verdicts = verdicts; cs } @@ -1683,15 +1689,17 @@ async fn build_core_changeset( // `Doomed`). Read from two snapshots, a funding that confirmed // and lost its coin to a mempool child between them would be // judged on a stale mempool context and marked spent for good. - let (slices, utxo_credit_verdicts): ( - Vec, - BTreeMap, - ) = match wallet_slices_and_verdicts_for_txid(wallet_manager, wallet_id, &record.txid) - .await - { - Some(read) => read, - None => (vec![(**record).clone()], BTreeMap::new()), - }; + let (slices, utxo_credit_verdicts, credits) = + match wallet_slices_and_verdicts_for_txid(wallet_manager, wallet_id, &record.txid) + .await + { + Some(read) => read, + None => ( + vec![(**record).clone()], + BTreeMap::new(), + CreditOwnership::default(), + ), + }; // A contact's watch-only chain never defines the wallet's // transaction row or its TXOs (see `is_contact_watch_only`); // the usage deltas below are still emitted, so the event @@ -1702,10 +1710,13 @@ async fn build_core_changeset( .filter(|r| !is_contact_watch_only(r)) .cloned() .collect(); - let (addresses_marked_used, account_highest_used) = + let (mut addresses_marked_used, account_highest_used) = collect_usage_deltas(wallet_manager, wallet_id, vec![&**record]).await; + addresses_marked_used.extend(credits.used); + let mut addresses_derived = addresses_derived.clone(); + addresses_derived.extend(credits.derived); let mut folded = owned.clone(); - fold_same_txid_records(&mut folded); + fold_same_txid_records(&mut folded, &credits.scripts); CoreChangeSet { // New UTXOs from the owned slices only (a watch-only // chain's outputs are the contact's coins); spends @@ -1721,7 +1732,7 @@ async fn build_core_changeset( // triggered the extension. See // `CoreChangeSet.addresses_derived` for the cascade- // link rationale. - addresses_derived: addresses_derived.clone(), + addresses_derived, addresses_marked_used, account_highest_used, utxo_credit_verdicts, @@ -1773,14 +1784,16 @@ async fn build_core_changeset( // The wallet-level row folds every slice the manager holds for // the transaction, not only the ones this block re-emitted (see // `with_held_sibling_slices`). - cs.records = + let (records, credits) = with_held_sibling_slices(wallet_manager, wallet_id, &cs.account_records).await; - fold_same_txid_records(&mut cs.records); + cs.records = records; + fold_same_txid_records(&mut cs.records, &credits.scripts); cs.last_processed_height = Some(*height); // Pool extensions triggered by any record in this block. // Already deduped upstream by `project_derived_addresses`; // `Merge` re-dedupes if multiple events fold together. cs.addresses_derived = addresses_derived.clone(); + cs.addresses_derived.extend(credits.derived); // Used-flag flips + highest-used watermarks for every // record in the block. `updated` / `matured` records // re-emit their involved addresses — idempotent at the @@ -1794,6 +1807,7 @@ async fn build_core_changeset( let (addresses_marked_used, account_highest_used) = collect_usage_deltas(wallet_manager, wallet_id, records).await; cs.addresses_marked_used = addresses_marked_used; + cs.addresses_marked_used.extend(credits.used); cs.account_highest_used = account_highest_used; // The engine's verdict on the outputs the persister is about to // materialise from these records — see @@ -2497,10 +2511,13 @@ async fn wallet_slices_and_verdicts_for_txid( ) -> Option<( Vec, BTreeMap, + CreditOwnership, )> { let guard = wallet_manager.read().await; let info = guard.get_wallet_info(wallet_id)?; - Some(slices_and_verdicts_for_txid(info, txid)) + let (slices, verdicts) = slices_and_verdicts_for_txid(info, txid); + let credits = credit_ownership(info, &slices); + Some((slices, verdicts, credits)) } /// `slices` plus every slice the manager still holds, for the same @@ -2525,14 +2542,14 @@ async fn with_held_sibling_slices( wallet_manager: &Arc>>, wallet_id: &WalletId, slices: &[TransactionRecord], -) -> Vec { +) -> (Vec, CreditOwnership) { let mut complete = slices.to_vec(); if slices.is_empty() { - return complete; + return (complete, CreditOwnership::default()); } let guard = wallet_manager.read().await; let Some(info) = guard.get_wallet_info(wallet_id) else { - return complete; + return (complete, CreditOwnership::default()); }; let carried: HashSet<(Txid, AccountType)> = slices .iter() @@ -2550,7 +2567,87 @@ async fn with_held_sibling_slices( } } } - complete + let credits = credit_ownership(info, &complete); + (complete, credits) +} + +/// Which asset-lock credit scripts of a transaction the wallet's own keys +/// pay, and the pool entries of those keys, so a persister keeps the same +/// ownership evidence the live accounting used. +#[derive(Default)] +struct CreditOwnership { + /// Credit scripts held by a non-contact account's address pool and + /// carried by one of the entries below. + scripts: BTreeSet, + /// The owning addresses already used or reserved, with their state, as + /// `addresses_marked_used` entries. + used: Vec, + /// The owning addresses still available, as `addresses_derived` entries. + derived: Vec, +} + +/// Credit ownership of `records`' asset-lock payloads, read from the same +/// wallet snapshot as the records. A contact's watch-only chain never makes +/// a credit the wallet's own, and a key no entry can carry is not claimed: +/// the SQLite repair could not see it. +fn credit_ownership(info: &PlatformWalletInfo, records: &[TransactionRecord]) -> CreditOwnership { + let scripts: BTreeSet<_> = records + .iter() + .flat_map(|record| asset_lock_credit_outputs(&record.transaction)) + .map(|output| &output.script_pubkey) + .collect(); + let mut evidence = CreditOwnership::default(); + if scripts.is_empty() { + return evidence; + } + for account in info.core_wallet.accounts.all_accounts() { + let kind = account.managed_account_type(); + if kind.is_contact_owned() { + continue; + } + let account_type = kind.to_account_type(); + for pool in kind.address_pools() { + for script in &scripts { + let Some(address) = pool + .script_pubkey_index + .get(*script) + .and_then(|index| pool.addresses.get(index)) + else { + continue; + }; + // Only an `Available` key may be announced as freshly derived: + // hosts store a derived entry as unused, which would release + // a reservation. + if !address.is_available() { + evidence.used.push(DerivedAddressInfo { + account_type, + pool_type: pool.pool_type, + info: address.clone(), + }); + } else if let Some(public_key) = match &address.public_key { + Some(PublicKeyType::ECDSA(bytes)) => PublicKey::from_slice(bytes).ok(), + _ => None, + } { + evidence.derived.push(DerivedAddress { + account_type, + pool_type: pool.pool_type, + derivation_index: address.index, + address: address.address.clone(), + public_key, + }); + } else { + tracing::debug!( + ?account_type, + index = address.index, + "asset-lock credit key has no ECDSA public key to persist; not counted as owned" + ); + continue; + } + evidence.scripts.insert((*script).clone()); + } + } + } + evidence } /// The slices `info` holds of `txid` and the verdicts of their owned outputs. @@ -4462,19 +4559,232 @@ mod contact_watch_only_projection_tests { (funding, keys) } - /// The wallet's Core balance drops by the locked credit plus the fee - /// — the credit leaves Core for the wallet's own Platform keys, so - /// the move is `Internal` and the net is `-(credit + fee)`, with or - /// without change. The keys account's `+credit` marker net counts - /// Platform credits, not Core funds, and must not be summed in (it - /// made the live row read `-fee` while every repair path wrote - /// `-(credit + fee)`). A funding slice that never meets the keys - /// marker (the keys account did not match, or its slice lands in - /// another round) is projected with the same wallet rule — not left - /// on upstream's account-local `Outgoing`. The keys account's marker - /// alone carries no Core accounting evidence, so the projection leaves - /// upstream's verdict alone. An asset lock that also pays someone else - /// is a real outgoing payment; the asset-lock exception covers only the burn. + /// A wallet-signed asset lock is `Internal` only when every payload + /// credit pays a key of one of the wallet's own (non-contact) accounts, + /// on every event that projects it; a foreign, mixed, empty or + /// contact-only credit set is `Outgoing`. The net is `-(credit + fee)` + /// either way. An owning key still `Available` is emitted as a derived + /// address; a used or reserved one keeps its state as a marked-used entry. + /// An available key with no ECDSA key material cannot be kept as + /// evidence, so it is not claimed either. + #[tokio::test] + async fn should_project_asset_lock_credit_direction_from_every_payload_script() { + use dashcore::transaction::special_transaction::asset_lock::AssetLockPayload; + use dashcore::transaction::TransactionPayload; + use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; + use key_wallet::wallet::managed_wallet_info::ManagedAccountOperations; + for case in [ + "own", + "foreign", + "mixed", + "two owned", + "empty", + "contact", + "shared contact", + "funds key", + "keyless funds key", + "reserved funds key", + ] { + let (mut ctx, funding) = single_coin_wallet().await; + let contact = contact_external_account(); + ctx.wallet.add_account(contact, None).unwrap(); + ctx.managed_wallet + .add_managed_account(&ctx.wallet, contact) + .unwrap(); + let keys = ctx + .managed_wallet + .accounts + .identity_registration + .as_ref() + .unwrap(); + let own: Vec<_> = keys.managed_account_type().address_pools()[0] + .addresses + .values() + .take(2) + .map(|entry| entry.address.clone()) + .collect(); + let own_info = keys.managed_account_type().address_pools()[0].addresses[&0].clone(); + let contact_account = ctx + .managed_wallet + .accounts + .dashpay_external_accounts + .values_mut() + .next() + .unwrap(); + let pool = contact_account + .managed_account_type_mut() + .address_pools_mut() + .remove(0); + let watched_address = pool.address_at_index(0).unwrap(); + if case == "shared contact" { + pool.address_index + .insert(own_info.address.clone(), own_info.index); + pool.script_pubkey_index + .insert(own_info.script_pubkey.clone(), own_info.index); + pool.addresses.insert(own_info.index, own_info); + } + let funds_key = ctx.managed_wallet.accounts.standard_bip44_accounts[&0] + .managed_account_type() + .address_pools()[0] + .addresses[&10] + .address + .clone(); + let mut funds_pools = ctx + .managed_wallet + .accounts + .standard_bip44_accounts + .get_mut(&0) + .unwrap() + .managed_account_type_mut() + .address_pools_mut(); + let reserved = funds_pools[0].addresses.get_mut(&11).unwrap(); + reserved.state = AddressState::Reserved { at: 7 }; + let reserved_key = reserved.address.clone(); + let keyless = funds_pools[0].addresses.get_mut(&12).unwrap(); + keyless.public_key = None; + let keyless_key = keyless.address.clone(); + let credit_addresses = match case { + "own" | "shared contact" => vec![own[0].clone()], + "contact" => vec![watched_address], + "funds key" => vec![funds_key.clone()], + "reserved funds key" => vec![reserved_key.clone()], + "keyless funds key" => vec![keyless_key.clone()], + "foreign" => vec![contact_address()], + "mixed" => vec![own[0].clone(), contact_address()], + "two owned" => own, + _ => vec![], + }; + let mut tx = spend_of( + &funding, + vec![ + TxOut { + value: LOCK_CREDIT, + script_pubkey: ScriptBuf::new_op_return(&[]), + }, + TxOut { + value: FUNDING - LOCK_CREDIT - LOCK_FEE, + script_pubkey: ctx.receive_address.script_pubkey(), + }, + ], + ); + tx.version = 3; + let credits = credit_addresses + .iter() + .map(|address| TxOut { + value: LOCK_CREDIT / credit_addresses.len() as u64, + script_pubkey: address.script_pubkey(), + }) + .collect(); + tx.special_transaction_payload = Some(TransactionPayload::AssetLockPayloadType( + AssetLockPayload::new(credits), + )); + let checked = ctx + .check_transaction(&tx, TransactionContext::Mempool) + .await; + assert!(checked.is_relevant); + let records = checked.new_records; + let mut wm = WalletManager::new(Network::Testnet); + let wallet_id = wm + .insert_wallet(ctx.wallet, platform_info(ctx.managed_wallet)) + .unwrap(); + let manager = Arc::new(RwLock::new(wm)); + // A key the store could not keep as evidence is not claimed live. + let expected = if matches!( + case, + "own" | "two owned" | "shared contact" | "funds key" | "reserved funds key" + ) { + TransactionDirection::Internal + } else { + TransactionDirection::Outgoing + }; + let events = [ + WalletEvent::TransactionDetected { + wallet_id, + record: Box::new(records[0].clone()), + balance: Default::default(), + account_balances: Default::default(), + addresses_derived: vec![], + }, + WalletEvent::BlockProcessed { + wallet_id, + height: 101, + chain_lock: None, + inserted: records, + updated: vec![], + matured: vec![], + balance: Default::default(), + account_balances: Default::default(), + addresses_derived: vec![], + }, + WalletEvent::TransactionInstantLocked { + wallet_id, + txid: tx.txid(), + instant_lock: InstantLock { + version: 1, + txid: tx.txid(), + inputs: tx.input.iter().map(|input| input.previous_output).collect(), + cyclehash: BlockHash::all_zeros(), + signature: BLSSignature::from([0; 96]), + }, + balance: Default::default(), + account_balances: Default::default(), + }, + ]; + for (event_index, event) in events.iter().enumerate() { + let projected = build_core_changeset(&manager, event).await; + if case == "reserved funds key" { + assert!( + projected.addresses_marked_used.iter().any(|entry| { + entry.info.address == reserved_key && entry.info.is_reserved() + }), + "a reserved credit key keeps its reservation" + ); + assert!( + !projected + .addresses_derived + .iter() + .any(|entry| entry.address == reserved_key), + "a reserved credit key is not re-announced as fresh" + ); + } + if case == "funds key" { + assert!( + !projected + .addresses_marked_used + .iter() + .any(|entry| entry.info.address == funds_key), + "credit observation must not invent final usage" + ); + assert!(projected + .addresses_derived + .iter() + .any(|entry| entry.address == funds_key)); + } + let record = projected + .records + .iter() + .find(|record| record.txid == tx.txid()) + .unwrap(); + assert_eq!( + record.net_amount, + -((LOCK_CREDIT + LOCK_FEE) as i64), + "{case}/{event_index}" + ); + assert_eq!(record.direction, expected, "{case}/{event_index}"); + } + } + } + + /// The wallet's Core balance drops by the locked credit plus the fee, + /// with or without change: the keys account's `+credit` marker net + /// counts Platform credits, not Core funds, and is not summed in. A + /// funding slice that never meets the keys marker is projected with the + /// same wallet rule. These fixtures' manager holds no wallet, so no + /// credit is provably the wallet's own and every lock reads `Outgoing` + /// even beside a keys marker that says `Internal` (credit ownership is + /// pinned by `should_project_asset_lock_credit_direction_from_every_payload_script`). + /// The keys marker alone carries no Core accounting evidence, so the + /// projection leaves upstream's verdict alone. #[tokio::test] async fn should_project_asset_lock_accounting_from_the_wallet_side() { use TransactionDirection::{Internal, Outgoing}; @@ -4504,17 +4814,17 @@ mod contact_watch_only_projection_tests { let keys_net = lock_credit(false) as i64; let paying_net = -((LOCK_CREDIT + LOCK_FEE + paid) as i64); for (case, records, expected) in [ - ("no change", both(false), (Internal, lock_net(false), bip44)), - ("change", both(true), (Internal, lock_net(true), bip44)), + ("no change", both(false), (Outgoing, lock_net(false), bip44)), + ("change", both(true), (Outgoing, lock_net(true), bip44)), ( "lone funding slice", funding_alone(false), - (Internal, lock_net(false), bip44), + (Outgoing, lock_net(false), bip44), ), ( "lone funding slice, change", funding_alone(true), - (Internal, lock_net(true), bip44), + (Outgoing, lock_net(true), bip44), ), ( "lone keys marker", @@ -4874,7 +5184,7 @@ mod contact_watch_only_projection_tests { ); let mut records = vec![no_input_slice, unrelated, funding_slice]; - fold_same_txid_records(&mut records); + fold_same_txid_records(&mut records, &BTreeSet::new()); assert_eq!(records.len(), 2); assert_eq!( @@ -5111,9 +5421,10 @@ mod contact_watch_only_projection_tests { let wallet_id = wm.insert_wallet(ctx.wallet, info).expect("insert wallet"); let manager = Arc::new(RwLock::new(wm)); - let (slices, _) = wallet_slices_and_verdicts_for_txid(&manager, &wallet_id, &spend.txid()) - .await - .expect("manager knows the wallet"); + let (slices, _, _) = + wallet_slices_and_verdicts_for_txid(&manager, &wallet_id, &spend.txid()) + .await + .expect("manager knows the wallet"); assert_eq!(slices.len(), 2, "both funding accounts hold a slice"); let lone_slice = slices .iter() diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 90550f78c8f..91b60c4cdc1 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -69,4 +69,6 @@ pub use traits::{ persists_outbound_account_markers, ListedCoreTxid, PersistenceError, PersistenceErrorKind, PlatformWalletPersistence, }; -pub use wallet_accounting::{is_owned, wallet_accounting, wallet_direction}; +pub use wallet_accounting::{ + asset_lock_credit_outputs, is_owned, wallet_accounting, wallet_direction, +}; diff --git a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs index 48ae44a914a..8c792651a4f 100644 --- a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs +++ b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs @@ -10,12 +10,16 @@ //! its account-local views disagree for an asset lock: the funding //! account reads a lock with no change as `Outgoing` (the OP_RETURN burn //! is not an owned output), while the keys account holding the credit -//! keys reads it as `Internal`. From the wallet's side an asset lock -//! moves Core duffs into its own Platform credits, so only the fee -//! leaves the wallet. +//! keys reads it as `Internal`. From the wallet's side an asset lock is +//! `Internal` only when its payload credits all pay the wallet's own keys; +//! credits paying anyone else leave the wallet, so the lock is `Outgoing`. +//! The net counts Core funds only, so it is the same either way. use std::collections::BTreeSet; +use dashcore::transaction::TransactionPayload; +use dashcore::{ScriptBuf, Transaction, TxOut}; + use key_wallet::managed_account::transaction_record::{ OutputRole, TransactionDirection, TransactionRecord, }; @@ -27,25 +31,31 @@ use key_wallet::transaction_checking::transaction_router::TransactionType; /// - `has_ours`: at least one output is wallet-owned (`Received`/`Change`). /// - `has_external`: at least one output is neither wallet-owned nor an /// OP_RETURN burn, so value leaves the wallet. +/// - `owns_all_credits`: the asset-lock payload has credit outputs and every +/// one pays a wallet-owned script; ignored for other transaction types. /// -/// An asset lock is `Internal` whenever nothing leaves the wallet, even -/// with no change output: its OP_RETURN value becomes the wallet's own -/// Platform credits. The Swift SDK's -/// `PersistentTransaction.reconciledAccounting` applies the same rule; -/// keep them in step. The case table -/// shared with Swift lives in `platform-wallet-storage` -/// (`should_classify_repaired_direction_like_the_swift_sdk`). +/// An asset lock is `Internal` when nothing leaves the wallet: no external +/// output and every credit owned, change or not. The case table lives in +/// `platform-wallet-storage` +/// (`should_classify_repaired_direction_like_the_swift_sdk`). The rule is +/// consumed by Swift's history reconciliation (dashpay/platform#5417). pub fn wallet_direction( transaction_type: TransactionType, spends_ours: bool, has_ours: bool, has_external: bool, + owns_all_credits: bool, ) -> TransactionDirection { + let stays_in_wallet = if transaction_type == TransactionType::AssetLock { + owns_all_credits + } else { + has_ours + }; if transaction_type == TransactionType::CoinJoin { TransactionDirection::CoinJoin } else if !spends_ours { TransactionDirection::Incoming - } else if !has_external && (has_ours || transaction_type == TransactionType::AssetLock) { + } else if !has_external && stays_in_wallet { TransactionDirection::Internal } else { TransactionDirection::Outgoing @@ -58,25 +68,33 @@ pub fn wallet_direction( /// A record with no details carries no accounting evidence (a keys-account /// marker that met no funding slice), so it is left as upstream emitted /// it. The SQLite repair skips such records for the same reason. -pub(crate) fn apply_wallet_accounting(record: &mut TransactionRecord) { +pub(crate) fn apply_wallet_accounting( + record: &mut TransactionRecord, + owned_credit_scripts: &BTreeSet, +) { if record.input_details.is_empty() && record.output_details.is_empty() { return; } - let (net, direction) = wallet_accounting(record); + let (net, direction) = wallet_accounting(record, owned_credit_scripts); // Unreachable for real amounts (the whole supply fits i64 many times // over); saturate rather than panic on a corrupt record. record.net_amount = i64::try_from(net).unwrap_or(if net < 0 { i64::MIN } else { i64::MAX }); record.direction = direction; } -/// Wallet-level net amount and direction of `record`, from its details. +/// Wallet-level net amount and direction of `record`, from its details +/// and `owned_credit_scripts`: the payload credit scripts the caller found +/// among the wallet's keys (live) or persisted address pools (repair). /// /// The net is `Σ owned outputs − Σ owned inputs`: the change in the /// wallet's Core balance. A keys-account marker's `+credit` (Platform /// credits, not Core funds) is therefore not part of it, so an asset lock /// nets `−(credit + fee)`. The net is returned as `i128` so each caller /// picks its own policy for a value outside `i64`. -pub fn wallet_accounting(record: &TransactionRecord) -> (i128, TransactionDirection) { +pub fn wallet_accounting( + record: &TransactionRecord, + owned_credit_scripts: &BTreeSet, +) -> (i128, TransactionDirection) { let owned: i128 = record .output_details .iter() @@ -102,15 +120,30 @@ pub fn wallet_accounting(record: &TransactionRecord) -> (i128, TransactionDirect .iter() .enumerate() .any(|(index, output)| !output.script_pubkey.is_op_return() && !accounted.contains(&index)); + let credits = asset_lock_credit_outputs(&record.transaction); + let owns_all_credits = !credits.is_empty() + && credits + .iter() + .all(|output| owned_credit_scripts.contains(&output.script_pubkey)); let direction = wallet_direction( record.transaction_type, !record.input_details.is_empty(), has_ours, has_external, + owns_all_credits, ); (owned - spent, direction) } +/// The credit outputs of `transaction`'s asset-lock payload; empty for any +/// other transaction. +pub fn asset_lock_credit_outputs(transaction: &Transaction) -> &[TxOut] { + match &transaction.special_transaction_payload { + Some(TransactionPayload::AssetLockPayloadType(payload)) => &payload.credit_outputs, + _ => &[], + } +} + /// Whether an output with this role belongs to the wallet. pub fn is_owned(role: OutputRole) -> bool { matches!(role, OutputRole::Received | OutputRole::Change) diff --git a/packages/rs-platform-wallet/src/test_support.rs b/packages/rs-platform-wallet/src/test_support.rs index b47ad1ab101..e670dca15fc 100644 --- a/packages/rs-platform-wallet/src/test_support.rs +++ b/packages/rs-platform-wallet/src/test_support.rs @@ -4,7 +4,7 @@ //! Used by the broadcast-failure regression tests in `wallet::core::broadcast` //! and `wallet::asset_lock::build`. -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; #[cfg(test)] use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -198,8 +198,13 @@ impl ExtendedPubKeySigner for WalletSigner { /// Runs the live projection's per-txid fold and wallet-level accounting /// over upstream per-account records, so downstream crates can pin their /// own accounting (the SQLite repair) against the live path. -pub fn fold_wallet_records(records: &mut Vec) { - fold_same_txid_records(records); +/// `owned_credit_scripts` are the asset-lock credit scripts the wallet's +/// keys pay. +pub fn fold_wallet_records( + records: &mut Vec, + owned_credit_scripts: &BTreeSet, +) { + fold_same_txid_records(records, owned_credit_scripts); } /// Core transaction and wallet fixtures shared by the history tests of this