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 2943926d9f8..3761b17d800 100644 --- a/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs +++ b/packages/rs-platform-wallet-ffi/src/core_wallet_types.rs @@ -1,5 +1,7 @@ //! C-compatible types for core wallet changeset FFI. +use key_wallet::managed_account::transaction_record::TransactionDirection; +use platform_wallet::changeset::WalletAccounting; use platform_wallet::masternode::{provider_payload_fields, MasternodeRecord}; use std::os::raw::c_char; @@ -363,6 +365,38 @@ pub struct AccountChangeSetFFI { pub has_internal_highest_used: bool, } +/// One transaction's Core accounting from ONE wallet's point of view, as +/// `platform_wallet::changeset::WalletAccounting` computes it. The host +/// persists it keyed by `(txid, wallet_id)` (the wallet is the callback's +/// `wallet_id`) and displays it; it never derives or corrects it. +/// +/// [`TransactionRecordFFI`]'s own `net_amount` / `direction` / `fee` stay +/// for hosts that predate this row. A record with no accounting evidence +/// yields NO row here, so the host keeps what it stored. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct TransactionAccountingFFI { + pub txid: [u8; 32], + /// `Σ owned outputs − Σ owned inputs` in duffs (positive = received). + pub net_amount: i64, + /// 0=incoming, 1=outgoing, 2=internal, 3=coinJoin. + pub direction: u32, + /// Meaningful only when `has_fee`. + pub fee: u64, + pub has_fee: bool, +} + +/// One stored Core transaction without this wallet's accounting row, handed +/// back for the load-time backfill (see +/// `on_load_core_history_for_accounting_fn`). Host-owned until the matching +/// free callback. +#[repr(C)] +pub struct CoreHistoryTransactionFFI { + /// Consensus-encoded transaction body. + pub tx_bytes: *const u8, + pub tx_bytes_len: usize, +} + // --------------------------------------------------------------------------- // Top-level wallet changeset // --------------------------------------------------------------------------- @@ -1523,6 +1557,37 @@ pub(crate) fn masternode_entry_v2_ffi( } } +/// `direction` wire values of [`TransactionRecordFFI`] and +/// [`TransactionAccountingFFI`], exported so hosts name them from the +/// generated header instead of re-declaring them. +pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_INCOMING: u32 = 0; +pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_OUTGOING: u32 = 1; +pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_INTERNAL: u32 = 2; +pub const PLATFORM_WALLET_TRANSACTION_DIRECTION_COIN_JOIN: u32 = 3; + +fn direction_to_u32(direction: TransactionDirection) -> u32 { + match direction { + TransactionDirection::Incoming => PLATFORM_WALLET_TRANSACTION_DIRECTION_INCOMING, + TransactionDirection::Outgoing => PLATFORM_WALLET_TRANSACTION_DIRECTION_OUTGOING, + TransactionDirection::Internal => PLATFORM_WALLET_TRANSACTION_DIRECTION_INTERNAL, + TransactionDirection::CoinJoin => PLATFORM_WALLET_TRANSACTION_DIRECTION_COIN_JOIN, + } +} + +impl From<&WalletAccounting> for TransactionAccountingFFI { + fn from(row: &WalletAccounting) -> Self { + let mut txid = [0u8; 32]; + txid.copy_from_slice(row.txid.as_ref()); + Self { + txid, + net_amount: row.net_amount, + direction: direction_to_u32(row.direction), + fee: row.fee.unwrap_or(0), + has_fee: row.fee.is_some(), + } + } +} + fn tx_record_to_ffi( tr: &key_wallet::managed_account::transaction_record::TransactionRecord, ) -> TransactionRecordFFI { @@ -1569,12 +1634,7 @@ fn tx_record_to_ffi( } }; - let dir_val = match tr.direction { - key_wallet::managed_account::transaction_record::TransactionDirection::Incoming => 0u32, - key_wallet::managed_account::transaction_record::TransactionDirection::Outgoing => 1, - key_wallet::managed_account::transaction_record::TransactionDirection::Internal => 2, - key_wallet::managed_account::transaction_record::TransactionDirection::CoinJoin => 3, - }; + let dir_val = direction_to_u32(tr.direction); let type_str = CString::new(format!("{:?}", tr.transaction_type)) .unwrap_or_else(|_| CString::new("Unknown").unwrap()); diff --git a/packages/rs-platform-wallet-ffi/src/manager.rs b/packages/rs-platform-wallet-ffi/src/manager.rs index 0723585c6e2..bc40aa72de1 100644 --- a/packages/rs-platform-wallet-ffi/src/manager.rs +++ b/packages/rs-platform-wallet-ffi/src/manager.rs @@ -8,10 +8,11 @@ use crate::event_handler::{ }; use crate::handle::*; use crate::persistence::{ - FFIPersister, FreeTrackedMasternodesFn, LoadAssetLockOperationIdFn, - LoadAssetLockShieldedRecipientFn, LoadIdentityBalanceBlockTimeFn, LoadTrackedMasternodesFn, - PersistAssetLockOperationIdFn, PersistAssetLockShieldedRecipientFn, PersistDpnsNameStatesFn, - PersistIdentityBalanceBlockTimeFn, PersistTrackedMasternodesFn, + FFIPersister, FreeCoreHistoryForAccountingFn, FreeTrackedMasternodesFn, + LoadAssetLockOperationIdFn, LoadAssetLockShieldedRecipientFn, LoadCoreHistoryForAccountingFn, + LoadIdentityBalanceBlockTimeFn, LoadTrackedMasternodesFn, PersistAssetLockOperationIdFn, + PersistAssetLockShieldedRecipientFn, PersistCoreHistoryAccountedFn, PersistDpnsNameStatesFn, + PersistIdentityBalanceBlockTimeFn, PersistTrackedMasternodesFn, PersistTransactionAccountingFn, PersistWalletChangesetChainLockHeightFn, PersistWalletChangesetSweepsFn, PersistWalletChangesetUtxoVerdictsFn, PersistWalletDashPayBackfillFn, PersistenceCallbacks, PersistenceCallbacksExtension, PersistenceCapabilitiesFFI, PersistenceExtensionCallbacks, @@ -299,6 +300,22 @@ unsafe fn persistence_extension_callbacks( on_load_asset_lock_shielded_recipient_fn, LoadAssetLockShieldedRecipientFn ), + persist_transaction_accounting: slot!( + on_persist_transaction_accounting_fn, + PersistTransactionAccountingFn + ), + load_core_history_for_accounting: slot!( + on_load_core_history_for_accounting_fn, + LoadCoreHistoryForAccountingFn + ), + load_core_history_for_accounting_free: slot!( + on_load_core_history_for_accounting_free_fn, + FreeCoreHistoryForAccountingFn + ), + persist_core_history_accounted: slot!( + on_persist_core_history_accounted_fn, + PersistCoreHistoryAccountedFn + ), } } @@ -1424,6 +1441,72 @@ mod tests { assert!(parsed.load_asset_lock_operation_id.is_none()); } + /// Each transaction-accounting slot is read only when the host's + /// `struct_size` covers it, so a host built before any of them simply + /// never has them read. + #[test] + fn should_size_gate_transaction_accounting_slots_independently() { + use crate::core_wallet_types::{CoreHistoryTransactionFFI, TransactionAccountingFFI}; + unsafe extern "C" fn persist( + _: *mut c_void, + _: *const u8, + _: *const TransactionAccountingFFI, + _: usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn load( + _: *mut c_void, + _: *const u8, + _: *mut bool, + _: *mut *const CoreHistoryTransactionFFI, + _: *mut usize, + ) -> i32 { + 0 + } + unsafe extern "C" fn free(_: *mut c_void, _: *const CoreHistoryTransactionFFI, _: usize) {} + unsafe extern "C" fn accounted(_: *mut c_void, _: *const u8) -> i32 { + 0 + } + let mut ext = PersistenceCallbacksExtension { + on_persist_transaction_accounting_fn: Some(persist), + on_load_core_history_for_accounting_fn: Some(load), + on_load_core_history_for_accounting_free_fn: Some(free), + on_persist_core_history_accounted_fn: Some(accounted), + ..Default::default() + }; + let present = |ext: &PersistenceCallbacksExtension| { + let parsed = unsafe { persistence_extension_callbacks(ext) }; + [ + parsed.persist_transaction_accounting.is_some(), + parsed.load_core_history_for_accounting.is_some(), + parsed.load_core_history_for_accounting_free.is_some(), + parsed.persist_core_history_accounted.is_some(), + ] + }; + assert_eq!(present(&ext), [true; 4]); + ext.struct_size = std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_core_history_accounted_fn + ); + assert_eq!(present(&ext), [true, true, true, false]); + ext.struct_size = std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_free_fn + ); + assert_eq!(present(&ext), [true, true, false, false]); + ext.struct_size = std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_fn + ); + assert_eq!(present(&ext), [true, false, false, false]); + ext.struct_size = std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_transaction_accounting_fn + ); + assert_eq!(present(&ext), [false; 4]); + } + /// A host whose `struct_size` stops right after the chainlock-height /// slot (built before the credit-verdict slot existed) keeps every /// earlier slot and simply never has the verdict slot read; a host diff --git a/packages/rs-platform-wallet-ffi/src/persistence.rs b/packages/rs-platform-wallet-ffi/src/persistence.rs index c1a86ac0a98..25bd3263a9b 100644 --- a/packages/rs-platform-wallet-ffi/src/persistence.rs +++ b/packages/rs-platform-wallet-ffi/src/persistence.rs @@ -30,11 +30,12 @@ use crate::types::{FFINetwork, Network}; use platform_wallet::changeset::provider_key_account::{ rebuild_provider_key_account, ProviderAccountRebuildError, }; +use platform_wallet::changeset::record_accounting; use platform_wallet::changeset::{ AccountAddressPoolEntry, AccountRegistrationEntry, ClientStartState, ClientWalletStartState, ListedCoreTxid, PersistenceCapabilities, PersistenceError, PersistenceErrorKind, PlatformWalletChangeSet, PlatformWalletPersistence, ProviderKeyAccountEntry, - ProviderKeyExtendedPubKey, PERSISTENCE_CAPABILITIES_VERSION, + ProviderKeyExtendedPubKey, WalletAccounting, PERSISTENCE_CAPABILITIES_VERSION, }; use platform_wallet::wallet::asset_lock::restored_record::held_output_details; use platform_wallet::wallet::core::stored_coin::{reconcile_with_parent, StoredParentState}; @@ -58,8 +59,9 @@ use crate::contact_persistence::{ use crate::core_address_types::{AddressPoolTypeTagFFI, CoreAddressEntryFFI, KeyTypeTagFFI}; use crate::core_wallet_types::{ 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, + build_utxo_credit_verdicts_for_callback, free_wallet_changeset_ffi, CoreHistoryTransactionFFI, + DashPayBackfillCoveredContactFFI, SweepBatchFFI, TransactionAccountingFFI, + UtxoCreditVerdictFFI, WalletChangeSetFFI, }; use crate::dashpay_payment::{build_payment_persist_entries, DashpayPaymentPersistEntryFFI}; use crate::dpns_name_state_persistence::{ @@ -352,6 +354,54 @@ pub type LoadAssetLockShieldedRecipientFn = unsafe extern "C" fn( out_recipient_raw_43: *mut u8, ) -> i32; +/// Carries one wallet's accounting rows (see [`TransactionAccountingFFI`]). +/// The host upserts each keyed by `(txid, wallet_id)`. Always fired inside a +/// begin/end round: right after `on_persist_wallet_changeset_fn`, so the +/// transaction rows the accounting belongs to already exist, when the round +/// has a record with accounting evidence; and in the load-time backfill's +/// own round, before [`PersistCoreHistoryAccountedFn`]. A non-zero return +/// fails the round like any other per-kind callback. +pub type PersistTransactionAccountingFn = unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + rows: *const TransactionAccountingFFI, + rows_count: usize, +) -> i32; + +/// The load-time accounting backfill's read. Sets `*out_backfilled = true` +/// (and returns no rows) once [`PersistCoreHistoryAccountedFn`] has been +/// stored for this wallet. Otherwise returns the wallet's stored +/// transactions that have no accounting row for it; any subset is safe, +/// since Rust fetches the funding transactions it needs itself +/// (`on_get_core_tx_record_fn`, where a hit in any context is used and a +/// non-zero return fails the backfill) and a transaction left out only +/// stays without a row. A null array with a non-zero count is an error. The +/// host keeps the array valid until [`FreeCoreHistoryForAccountingFn`]. +pub type LoadCoreHistoryForAccountingFn = unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + out_backfilled: *mut bool, + out_rows: *mut *const CoreHistoryTransactionFFI, + out_count: *mut usize, +) -> i32; + +/// Release an array returned by [`LoadCoreHistoryForAccountingFn`]. +pub type FreeCoreHistoryForAccountingFn = unsafe extern "C" fn( + context: *mut c_void, + rows: *const CoreHistoryTransactionFFI, + count: usize, +); + +/// Record that this wallet's history backfill is complete, so the next +/// [`LoadCoreHistoryForAccountingFn`] reports it backfilled. Fired in the +/// backfill's round after its rows, and committed with them, only when every +/// transaction got its row. A round that had to leave a transaction +/// unaccounted (a wallet-signed input whose funding transaction is not +/// available yet) commits its rows without it, so the next load reads the +/// rest again. +pub type PersistCoreHistoryAccountedFn = + unsafe extern "C" fn(context: *mut c_void, wallet_id: *const u8) -> i32; + /// Size- and version-tagged additive persistence callbacks. /// /// `context` is the context in the accompanying [`PersistenceCallbacks`] @@ -541,6 +591,41 @@ pub struct PersistenceCallbacksExtension { out_recipient_raw_43: *mut u8, ) -> i32, >, + /// Per-wallet transaction accounting (see + /// [`PersistTransactionAccountingFn`]). Additive like the slots above: a + /// host without it keeps reading `TransactionRecordFFI`'s own values. + pub on_persist_transaction_accounting_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + rows: *const TransactionAccountingFFI, + rows_count: usize, + ) -> i32, + >, + /// Stored history for the load-time accounting backfill (see + /// [`LoadCoreHistoryForAccountingFn`]). Read only together with the free + /// slot below, the persist slot above and the completion slot. + pub on_load_core_history_for_accounting_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + wallet_id: *const u8, + out_backfilled: *mut bool, + out_rows: *mut *const CoreHistoryTransactionFFI, + out_count: *mut usize, + ) -> i32, + >, + pub on_load_core_history_for_accounting_free_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + rows: *const CoreHistoryTransactionFFI, + count: usize, + ), + >, + /// Backfill completion marker (see [`PersistCoreHistoryAccountedFn`]). + /// The backfill runs only when all four accounting slots and the + /// changeset begin/end pair are present. + pub on_persist_core_history_accounted_fn: + Option i32>, } impl Default for PersistenceCallbacksExtension { @@ -563,6 +648,10 @@ impl Default for PersistenceCallbacksExtension { on_load_asset_lock_operation_id_fn: None, on_persist_asset_lock_shielded_recipient_fn: None, on_load_asset_lock_shielded_recipient_fn: None, + on_persist_transaction_accounting_fn: None, + on_load_core_history_for_accounting_fn: None, + on_load_core_history_for_accounting_free_fn: None, + on_persist_core_history_accounted_fn: None, } } } @@ -586,6 +675,10 @@ pub struct PersistenceExtensionCallbacks { pub load_asset_lock_operation_id: Option, pub persist_asset_lock_shielded_recipient: Option, pub load_asset_lock_shielded_recipient: Option, + pub persist_transaction_accounting: Option, + pub load_core_history_for_accounting: Option, + pub load_core_history_for_accounting_free: Option, + pub persist_core_history_accounted: Option, } /// Return value by which a persistence callback reports a **retryable** @@ -1527,6 +1620,15 @@ pub struct FFIPersister { tracked_masternodes_callbacks: PersistenceExtensionCallbacks, persist_identity_balance_block_time_callback: Option, load_identity_balance_block_time_callback: Option, + /// Per-wallet accounting rows, fired after each round's changeset and by + /// the load-time backfill. `None` keeps a host on `TransactionRecordFFI`'s + /// values, as before the slot existed. + persist_transaction_accounting_callback: Option, + /// The load-time backfill's read, free and completion slots; the + /// backfill runs only when all of them and the persist slot are wired. + load_core_history_for_accounting_callback: Option, + load_core_history_for_accounting_free_callback: Option, + persist_core_history_accounted_callback: Option, /// Semantic capability declaration supplied separately from the callback /// vtable by the additive manager-create API. Keeping this out of /// `PersistenceCallbacks` preserves that established C struct's size. @@ -1652,6 +1754,11 @@ impl FFIPersister { persist_identity_balance_block_time_callback: extensions .persist_identity_balance_block_time, load_identity_balance_block_time_callback: extensions.load_identity_balance_block_time, + persist_transaction_accounting_callback: extensions.persist_transaction_accounting, + load_core_history_for_accounting_callback: extensions.load_core_history_for_accounting, + load_core_history_for_accounting_free_callback: extensions + .load_core_history_for_accounting_free, + persist_core_history_accounted_callback: extensions.persist_core_history_accounted, declared_capabilities, round_lock: Mutex::new(RoundGuardState::default()), } @@ -1814,6 +1921,119 @@ impl FFIPersister { } } +/// What `on_get_core_tx_record_fn` answered for one txid. +enum CoreTxRead { + /// The callback is unset, or the host does not store the txid. + Missing, + /// The callback returned this non-zero code. + Failed(i32), + /// The host reported a hit but handed over no decodable body. + Unreadable(&'static str), + /// A hit: the host's raw context fields and the decoded body. + Found { + context_kind: u8, + block_height: u32, + block_hash: [u8; 32], + block_timestamp: u32, + transaction: dashcore::Transaction, + }, +} + +impl FFIPersister { + /// Call `on_get_core_tx_record_fn` for `txid` and decode its answer, + /// handing the tx-bytes buffer back to the host on every path. Each + /// caller applies its own policy to the outcome. + fn read_core_tx(&self, wallet_id: WalletId, txid: &dashcore::Txid) -> CoreTxRead { + use dashcore::consensus::Decodable; + + let Some(get_cb) = self.callbacks.on_get_core_tx_record_fn else { + return CoreTxRead::Missing; + }; + + let txid_bytes: [u8; 32] = *txid.as_byte_array(); + let mut context_kind: u8 = 0; + let mut block_height: u32 = 0; + let mut block_hash: [u8; 32] = [0u8; 32]; + let mut block_timestamp: u32 = 0; + let mut tx_bytes_ptr: *const u8 = std::ptr::null(); + let mut tx_bytes_len: usize = 0; + let mut found: bool = false; + + // SAFETY: All output pointers reference Rust-owned stack + // locals that outlive the callback invocation. `wallet_id` + // and `txid` are fixed-size byte arrays. + let rc = unsafe { + get_cb( + self.callbacks.context, + wallet_id.as_ptr(), + txid_bytes.as_ptr(), + &mut context_kind, + &mut block_height, + block_hash.as_mut_ptr(), + &mut block_timestamp, + &mut tx_bytes_ptr, + &mut tx_bytes_len, + &mut found, + ) + }; + + // RAII guard so the tx-bytes free callback fires on every + // exit path past this point, handing the buffer back to the host. + struct TxBytesGuard { + ptr: *const u8, + len: usize, + free_fn: Option< + unsafe extern "C" fn( + context: *mut c_void, + tx_bytes: *const u8, + tx_bytes_len: usize, + ), + >, + ctx: *mut c_void, + } + impl Drop for TxBytesGuard { + fn drop(&mut self) { + if let (Some(free), false) = (self.free_fn, self.ptr.is_null()) { + // SAFETY: ptr+len match the values the lookup + // callback wrote; the host owns the allocation + // until this free fires. + unsafe { free(self.ctx, self.ptr, self.len) }; + } + } + } + let _bytes_guard = TxBytesGuard { + ptr: tx_bytes_ptr, + len: tx_bytes_len, + free_fn: self.callbacks.on_get_core_tx_record_free_fn, + ctx: self.callbacks.context, + }; + + if rc != 0 { + return CoreTxRead::Failed(rc); + } + if !found { + return CoreTxRead::Missing; + } + if tx_bytes_ptr.is_null() || tx_bytes_len == 0 { + return CoreTxRead::Unreadable("handed over no transaction bytes"); + } + // SAFETY: the host guarantees `tx_bytes_ptr` points to + // `tx_bytes_len` valid bytes until the free callback — + // `_bytes_guard` keeps that window open until this returns. + let tx_slice = unsafe { slice::from_raw_parts(tx_bytes_ptr, tx_bytes_len) }; + match dashcore::Transaction::consensus_decode(&mut &tx_slice[..]) { + Ok(transaction) => CoreTxRead::Found { + context_kind, + block_height, + block_hash, + block_timestamp, + transaction, + }, + Err(_) => CoreTxRead::Unreadable("its transaction bytes do not decode"), + } + } +} + impl PlatformWalletPersistence for FFIPersister { /// A host attests `ATOMIC_CHANGESETS` for a round it commits or rolls back /// whole; nothing of a rolled-back round survives on this side either, so @@ -1899,6 +2119,151 @@ impl PlatformWalletPersistence for FFIPersister { Ok(()) } + fn load_core_history_for_accounting( + &self, + wallet_id: WalletId, + ) -> Result>, PersistenceError> { + // A backfill that could not be stored or completed, or not as one + // round, is not worth reading. + let (Some(load), Some(free), Some(_), Some(_), Some(_), Some(_)) = ( + self.load_core_history_for_accounting_callback, + self.load_core_history_for_accounting_free_callback, + self.persist_transaction_accounting_callback, + self.persist_core_history_accounted_callback, + self.callbacks.on_changeset_begin_fn, + self.callbacks.on_changeset_end_fn, + ) else { + return Ok(None); + }; + let mut backfilled = false; + let mut rows_ptr: *const CoreHistoryTransactionFFI = std::ptr::null(); + let mut count: usize = 0; + let rc = unsafe { + load( + self.callbacks.context, + wallet_id.as_ptr(), + &mut backfilled, + &mut rows_ptr, + &mut count, + ) + }; + if rc != 0 { + return Err(persist_callback_error( + rc, + format!("on_load_core_history_for_accounting_fn returned error code {rc}"), + )); + } + if rows_ptr.is_null() && count > 0 { + unsafe { free(self.callbacks.context, rows_ptr, count) }; + return Err(PersistenceError::backend(format!( + "on_load_core_history_for_accounting_fn reported {count} transactions \ + but handed over no array" + ))); + } + let mut out = Vec::with_capacity(count); + if !rows_ptr.is_null() && count > 0 { + let rows = unsafe { slice::from_raw_parts(rows_ptr, count) }; + for row in rows { + let bytes = unsafe { slice_from_raw(row.tx_bytes, row.tx_bytes_len) }; + match dashcore::consensus::encode::deserialize::(bytes) { + Ok(transaction) => out.push(transaction), + // One undecodable row only goes without accounting. + Err(e) => tracing::warn!( + error = %e, + "load: skipping an undecodable stored transaction in the accounting backfill" + ), + } + } + } + unsafe { free(self.callbacks.context, rows_ptr, count) }; + Ok((!backfilled).then_some(out)) + } + + /// The backfill's rows and, when `complete`, its completion marker, as + /// one round under `round_lock`: they commit together or not at all, and + /// never land in another wallet's open round. + fn store_history_accounting( + &self, + wallet_id: WalletId, + rows: &[WalletAccounting], + complete: bool, + ) -> Result<(), PersistenceError> { + let (Some(persist_rows), Some(accounted)) = ( + self.persist_transaction_accounting_callback, + self.persist_core_history_accounted_callback, + ) else { + return Ok(()); + }; + // Rows and marker must commit as one unit; without the round pair + // nothing would make them, so nothing is written. + let (Some(begin), Some(end)) = ( + self.callbacks.on_changeset_begin_fn, + self.callbacks.on_changeset_end_fn, + ) else { + return Err(PersistenceError::backend( + "the accounting backfill needs the changeset begin/end pair to commit \ + atomically; nothing was written", + )); + }; + let mut round = self.round_lock.lock(); + round.begin_round()?; + let result = unsafe { begin(self.callbacks.context, wallet_id.as_ptr()) }; + if result != 0 { + let _ = round.end_round(); + return Err(persist_callback_error( + result, + format!( + "changeset-begin callback returned error code {result}; \ + accounting backfill aborted before any write" + ), + )); + } + let mut outcome = RoundOutcome::default(); + if !rows.is_empty() { + let rows: Vec = + rows.iter().map(TransactionAccountingFFI::from).collect(); + let result = unsafe { + persist_rows( + self.callbacks.context, + wallet_id.as_ptr(), + rows.as_ptr(), + rows.len(), + ) + }; + if result != 0 { + outcome.record(result); + } + } + if complete && outcome.is_success() { + let result = unsafe { accounted(self.callbacks.context, wallet_id.as_ptr()) }; + if result != 0 { + outcome.record(result); + } + } + let result = unsafe { + end( + self.callbacks.context, + wallet_id.as_ptr(), + outcome.is_success(), + ) + }; + if result != 0 { + if outcome.is_success() { + outcome.record(result); + } else { + outcome.record_fatal(); + } + } + round.end_round()?; + match outcome.failure_kind() { + Some(kind) => Err(PersistenceError::backend_with_kind( + self.reportable_round_kind(kind), + "the accounting backfill round failed and was rolled back", + )), + None => Ok(()), + } + } + fn load_tracked_masternodes( &self, network: dashcore::Network, @@ -1936,6 +2301,13 @@ impl PlatformWalletPersistence for FFIPersister { format!("on_load_tracked_masternodes_fn returned error code {rc}"), )); } + if rows_ptr.is_null() && count > 0 { + unsafe { free(self.callbacks.context, rows_ptr, count) }; + return Err(PersistenceError::backend(format!( + "on_load_core_history_for_accounting_fn reported {count} transactions \ + but handed over no array" + ))); + } let mut out = Vec::with_capacity(count); if !rows_ptr.is_null() && count > 0 { let rows = unsafe { slice::from_raw_parts(rows_ptr, count) }; @@ -2343,6 +2715,37 @@ impl PlatformWalletPersistence for FFIPersister { } } + // Per-wallet accounting rides its own size-negotiated slot: the + // frozen `TransactionRecordFFI` stride cannot say "this record + // carries no accounting", and this row can, by being absent. + // Fired after the changeset callback so the host already holds + // the transaction rows these belong to. + if let Some(cb) = self.persist_transaction_accounting_callback { + let rows: Vec = core_cs + .records + .iter() + .filter_map(record_accounting) + .map(|row| TransactionAccountingFFI::from(&row)) + .collect(); + if !rows.is_empty() { + let result = unsafe { + cb( + self.callbacks.context, + wallet_id.as_ptr(), + rows.as_ptr(), + rows.len(), + ) + }; + if result != 0 { + eprintln!( + "Transaction accounting persistence callback returned error code {}", + result + ); + outcome.record(result); + } + } + } + // The numeric chainlock height rides its own size-negotiated // extension slot for the same layout reason the sweeps below do: // the bincode blob on the changeset struct is opaque to a @@ -4125,160 +4528,89 @@ impl PlatformWalletPersistence for FFIPersister { Option, PersistenceError, > { - use dashcore::consensus::Decodable; use key_wallet::account::{AccountType, StandardAccountType}; use key_wallet::managed_account::transaction_record::{ TransactionDirection, TransactionRecord, }; use key_wallet::transaction_checking::{BlockInfo, TransactionContext, TransactionType}; - let Some(get_cb) = self.callbacks.on_get_core_tx_record_fn else { - return Ok(None); - }; + let (context_kind, block_height, block_hash, block_timestamp, transaction) = + match self.read_core_tx(wallet_id, txid) { + CoreTxRead::Missing => return Ok(None), + CoreTxRead::Failed(rc) => { + // TODO(tx-record-read-sentinels): `on_get_core_tx_record_fn` + // collapses every non-zero return, sentinels included, into a + // miss, so the transient-vs-permanent read distinction is + // unreachable from FFI hosts. Deferred deliberately: converting it + // to `persist_callback_error` and letting + // `get_core_tx_record_or_transient_miss` do the collapsing is a + // behaviour change for every host on the current contract. + // `get_core_transaction` already reports it. + tracing::debug!( + txid = %txid, + rc, + "on_get_core_tx_record_fn returned a non-zero result; \ + treating as miss" + ); + return Ok(None); + } + CoreTxRead::Unreadable(reason) => { + tracing::debug!( + txid = %txid, + reason, + "on_get_core_tx_record_fn reported a hit it could not hand \ + over; treating as miss" + ); + return Ok(None); + } + CoreTxRead::Found { + context_kind, + block_height, + block_hash, + block_timestamp, + transaction, + } => ( + context_kind, + block_height, + block_hash, + block_timestamp, + transaction, + ), + }; - let txid_bytes: [u8; 32] = *txid.as_byte_array(); - let mut context_kind: u8 = 0; - let mut block_height: u32 = 0; - let mut block_hash: [u8; 32] = [0u8; 32]; - let mut block_timestamp: u32 = 0; - let mut tx_bytes_ptr: *const u8 = std::ptr::null(); - let mut tx_bytes_len: usize = 0; - let mut found: bool = false; - - // SAFETY: All output pointers reference Rust-owned stack - // locals that outlive the callback invocation. `wallet_id` - // and `txid` are fixed-size byte arrays. - let rc = unsafe { - get_cb( - self.callbacks.context, - wallet_id.as_ptr(), - txid_bytes.as_ptr(), - &mut context_kind, - &mut block_height, - block_hash.as_mut_ptr(), - &mut block_timestamp, - &mut tx_bytes_ptr, - &mut tx_bytes_len, - &mut found, - ) - }; - - // RAII guard so the tx-bytes free callback fires on every - // exit path past this point — early returns for unknown - // context kinds, decode failures, and the IS-skip case all - // correctly hand the buffer back to Swift. - struct TxBytesGuard<'a> { - ptr: *const u8, - len: usize, - free_fn: Option< - unsafe extern "C" fn( - context: *mut c_void, - tx_bytes: *const u8, - tx_bytes_len: usize, - ), - >, - ctx: *mut c_void, - _marker: std::marker::PhantomData<&'a ()>, - } - impl<'a> Drop for TxBytesGuard<'a> { - fn drop(&mut self) { - if let (Some(free), false) = (self.free_fn, self.ptr.is_null()) { - // SAFETY: ptr+len match the values the lookup - // callback wrote; Swift owns the allocation - // until this free fires. - unsafe { free(self.ctx, self.ptr, self.len) }; - } - } - } - let _bytes_guard = TxBytesGuard { - ptr: tx_bytes_ptr, - len: tx_bytes_len, - free_fn: self.callbacks.on_get_core_tx_record_free_fn, - ctx: self.callbacks.context, - _marker: std::marker::PhantomData, - }; - - if rc != 0 { - // TODO(tx-record-read-sentinels): `on_get_core_tx_record_fn` - // collapses every non-zero return, sentinels included, into a - // miss, so the transient-vs-permanent read distinction is - // unreachable from FFI hosts. Deferred deliberately: converting it - // to `persist_callback_error` and letting - // `get_core_tx_record_or_transient_miss` do the collapsing is a - // behaviour change for every host on the current contract. - tracing::debug!( - txid = %txid, - rc, - "on_get_core_tx_record_fn returned a non-zero result; \ - treating as miss" - ); - return Ok(None); - } - if !found { - return Ok(None); - } - - let context = match context_kind { - 0 => TransactionContext::Mempool, - k if u32::from(k) == TX_CONTEXT_RAW_INSTANT_SEND => { - // InstantSend requires the IS-lock blob, which the - // persister doesn't currently store. Treat as miss - // so the proof flow's SPV wait path completes the - // proof from the live event stream. - return Ok(None); - } - k if u32::from(k) == TX_CONTEXT_RAW_IN_BLOCK => { - TransactionContext::InBlock(BlockInfo::new( - block_height, - dashcore::BlockHash::from_byte_array(block_hash), - block_timestamp, - )) - } - k if u32::from(k) == TX_CONTEXT_RAW_IN_CHAIN_LOCKED_BLOCK => { - TransactionContext::InChainLockedBlock(BlockInfo::new( - block_height, - dashcore::BlockHash::from_byte_array(block_hash), - block_timestamp, - )) - } - unknown => { - tracing::debug!( - txid = %txid, - unknown, - "on_get_core_tx_record_fn returned an unknown \ - context kind; treating as miss" - ); - return Ok(None); - } - }; - - if tx_bytes_ptr.is_null() || tx_bytes_len == 0 { - tracing::debug!( - txid = %txid, - "on_get_core_tx_record_fn reported a hit but no tx \ - bytes; treating as miss" - ); - return Ok(None); - } - // SAFETY: Swift guarantees `tx_bytes_ptr` points to - // `tx_bytes_len` valid bytes for the duration of the - // callback window — `_bytes_guard` keeps that window open - // until this function returns. - let tx_slice = unsafe { slice::from_raw_parts(tx_bytes_ptr, tx_bytes_len) }; - let transaction = match dashcore::blockdata::transaction::Transaction::consensus_decode( - &mut &tx_slice[..], - ) { - Ok(tx) => tx, - Err(err) => { - tracing::debug!( - txid = %txid, - error = %err, - "on_get_core_tx_record_fn returned undecodable \ - tx bytes; treating as miss" - ); - return Ok(None); - } - }; + let context = match context_kind { + 0 => TransactionContext::Mempool, + k if u32::from(k) == TX_CONTEXT_RAW_INSTANT_SEND => { + // InstantSend requires the IS-lock blob, which the + // persister doesn't currently store. Treat as miss + // so the proof flow's SPV wait path completes the + // proof from the live event stream. + return Ok(None); + } + k if u32::from(k) == TX_CONTEXT_RAW_IN_BLOCK => { + TransactionContext::InBlock(BlockInfo::new( + block_height, + dashcore::BlockHash::from_byte_array(block_hash), + block_timestamp, + )) + } + k if u32::from(k) == TX_CONTEXT_RAW_IN_CHAIN_LOCKED_BLOCK => { + TransactionContext::InChainLockedBlock(BlockInfo::new( + block_height, + dashcore::BlockHash::from_byte_array(block_hash), + block_timestamp, + )) + } + unknown => { + tracing::debug!( + txid = %txid, + unknown, + "on_get_core_tx_record_fn returned an unknown \ + context kind; treating as miss" + ); + return Ok(None); + } + }; Ok(Some(TransactionRecord { transaction, @@ -4298,6 +4630,30 @@ impl PlatformWalletPersistence for FFIPersister { })) } + /// The body behind `on_get_core_tx_record_fn`, read strictly for the + /// accounting backfill: a hit in any context (InstantSend included) is + /// returned, and a host failure or a hit with no decodable body is an + /// error, so the backfill fails its round instead of reading a wallet + /// input as foreign. [`Self::get_core_tx_record`]'s miss policy is + /// unchanged. + fn get_core_transaction( + &self, + wallet_id: WalletId, + txid: &dashcore::Txid, + ) -> Result, PersistenceError> { + match self.read_core_tx(wallet_id, txid) { + CoreTxRead::Missing => Ok(None), + CoreTxRead::Failed(rc) => Err(persist_callback_error( + rc, + format!("on_get_core_tx_record_fn returned error code {rc} for {txid}"), + )), + CoreTxRead::Unreadable(reason) => Err(PersistenceError::backend(format!( + "on_get_core_tx_record_fn reported {txid} but {reason}" + ))), + CoreTxRead::Found { transaction, .. } => Ok(Some(transaction)), + } + } + fn list_wallet_core_txids( &self, wallet_id: WalletId, @@ -9921,6 +10277,501 @@ mod tests { drop(persister); } + /// Per-wallet accounting crosses right after the round's changeset, one + /// row per record that carries details. A detail-less record (an asset + /// lock's funding record rebuilt at load and re-emitted when a chain + /// lock promotes it) crosses as NO row, so the host keeps what it stored + /// instead of overwriting it with the placeholder 0. A host without the + /// slot still succeeds. + #[test] + fn store_delivers_transaction_accounting_after_the_changeset_skipping_detailless_records() { + use key_wallet::account::{AccountType, StandardAccountType}; + use key_wallet::managed_account::transaction_record::{ + OutputDetail, OutputRole, TransactionDirection, TransactionRecord, + }; + use key_wallet::transaction_checking::TransactionType; + use platform_wallet::changeset::CoreChangeSet; + use platform_wallet::test_support::core_fixtures::{ + block, foreign_address, outside, pay, spend_of, + }; + + #[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_accounting_rows( + ctx: *mut c_void, + wallet_id: *const u8, + rows: *const TransactionAccountingFFI, + rows_count: usize, + ) -> i32 { + let sink = &*(ctx as *const Sink); + let wallet = *wallet_id; + let mut events = sink.events.lock().unwrap(); + for row in slice::from_raw_parts(rows, rows_count) { + events.push(format!( + "accounting wallet={wallet:02x} txid={} net={} direction={} has_fee={}", + hex::encode(row.txid), + row.net_amount, + row.direction, + row.has_fee + )); + } + 0 + } + let bip44 = AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }; + let received_tx = spend_of(&[outside(1)], vec![pay(5_000, &foreign_address(2))]); + let received = TransactionRecord::new( + received_tx.clone(), + bip44, + block(10), + TransactionType::Standard, + TransactionDirection::Incoming, + Vec::new(), + vec![OutputDetail { + index: 0, + role: OutputRole::Received, + address: Some(foreign_address(2)), + value: 5_000, + }], + 5_000, + ); + let restored_lock = TransactionRecord::new( + spend_of(&[outside(3)], vec![pay(9_000, &foreign_address(4))]), + bip44, + block(11), + TransactionType::AssetLock, + TransactionDirection::Internal, + Vec::new(), + Vec::new(), + 0, + ); + let round = || PlatformWalletChangeSet { + core: Some(CoreChangeSet { + records: vec![received.clone(), restored_lock.clone()], + ..Default::default() + }), + ..Default::default() + }; + + 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 { + persist_transaction_accounting: Some(record_accounting_rows), + ..Default::default() + }, + ); + persister + .store([0xAA; 32], round()) + .expect("accounting round must succeed"); + assert_eq!( + sink.events.lock().unwrap().clone(), + vec![ + "changeset".to_string(), + format!( + "accounting wallet=aa txid={} net=5000 direction=0 has_fee=false", + hex::encode(received_tx.txid().as_byte_array()) + ), + ], + ); + drop(persister); + + 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( + callbacks, + PersistenceCapabilities::NONE, + ); + persister + .store([0xAA; 32], round()) + .expect("slotless-host round must still succeed"); + assert_eq!( + sink.events.lock().unwrap().clone(), + vec!["changeset".to_string()] + ); + } + + /// The load-time backfill reads the host's unaccounted transactions + /// through the load/free pair (an undecodable body is skipped, not + /// fatal) until the host reports the wallet backfilled. It then writes + /// the rows and the completion marker as ONE round: begin, rows, marker, + /// end. A failing row write rolls the round back with no marker, so the + /// backfill runs again. Without every slot nothing is read. + #[test] + fn should_backfill_accounting_through_the_extension_in_one_round() { + use key_wallet::managed_account::transaction_record::TransactionDirection; + use platform_wallet::test_support::core_fixtures::{ + foreign_address, outside, pay, spend_of, + }; + + struct Host { + backfilled: bool, + rows: Vec, + /// Reported instead of `rows.len()`, with a null array when + /// `rows` is empty. + claimed_count: Option, + fail_rows: bool, + loads: std::sync::Mutex, + frees: std::sync::Mutex, + events: std::sync::Mutex>, + } + unsafe fn host<'a>(ctx: *mut c_void) -> &'a Host { + &*(ctx as *const Host) + } + unsafe extern "C" fn load( + ctx: *mut c_void, + _wallet_id: *const u8, + out_backfilled: *mut bool, + out_rows: *mut *const CoreHistoryTransactionFFI, + out_count: *mut usize, + ) -> i32 { + let host = host(ctx); + *host.loads.lock().unwrap() += 1; + *out_backfilled = host.backfilled; + if !host.backfilled { + *out_rows = if host.rows.is_empty() { + std::ptr::null() + } else { + host.rows.as_ptr() + }; + *out_count = host.claimed_count.unwrap_or(host.rows.len()); + } + 0 + } + unsafe extern "C" fn free( + ctx: *mut c_void, + _rows: *const CoreHistoryTransactionFFI, + _count: usize, + ) { + *host(ctx).frees.lock().unwrap() += 1; + } + unsafe extern "C" fn persist( + ctx: *mut c_void, + _wallet_id: *const u8, + rows: *const TransactionAccountingFFI, + rows_count: usize, + ) -> i32 { + let host = host(ctx); + for row in slice::from_raw_parts(rows, rows_count) { + host.events.lock().unwrap().push(format!( + "row net={} direction={} fee={}", + row.net_amount, row.direction, row.fee + )); + } + i32::from(host.fail_rows) + } + unsafe extern "C" fn accounted(ctx: *mut c_void, _wallet_id: *const u8) -> i32 { + host(ctx).events.lock().unwrap().push("accounted".into()); + 0 + } + unsafe extern "C" fn begin(ctx: *mut c_void, _wallet_id: *const u8) -> i32 { + host(ctx).events.lock().unwrap().push("begin".into()); + 0 + } + unsafe extern "C" fn end(ctx: *mut c_void, _wallet_id: *const u8, success: bool) -> i32 { + host(ctx) + .events + .lock() + .unwrap() + .push(format!("end success={success}")); + 0 + } + + let first = spend_of(&[outside(1)], vec![pay(5_000, &foreign_address(2))]); + let second = spend_of(&[outside(3)], vec![pay(7_000, &foreign_address(4))]); + let bodies = [ + dashcore::consensus::encode::serialize(&first), + vec![0xFF], + dashcore::consensus::encode::serialize(&second), + ]; + let make_host = |backfilled: bool, fail_rows: bool| Host { + backfilled, + rows: bodies + .iter() + .map(|body| CoreHistoryTransactionFFI { + tx_bytes: body.as_ptr(), + tx_bytes_len: body.len(), + }) + .collect(), + claimed_count: None, + fail_rows, + loads: std::sync::Mutex::new(0), + frees: std::sync::Mutex::new(0), + events: std::sync::Mutex::new(Vec::new()), + }; + let extensions = PersistenceExtensionCallbacks { + persist_transaction_accounting: Some(persist), + load_core_history_for_accounting: Some(load), + load_core_history_for_accounting_free: Some(free), + persist_core_history_accounted: Some(accounted), + ..Default::default() + }; + let persister_for = |host: &Host, extensions: PersistenceExtensionCallbacks| { + FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: host as *const Host as *mut c_void, + on_changeset_begin_fn: Some(begin), + on_changeset_end_fn: Some(end), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + extensions, + ) + }; + let row = WalletAccounting { + txid: first.txid(), + net_amount: -5_000, + direction: TransactionDirection::Outgoing, + fee: Some(250), + }; + + // Not yet backfilled: the decodable bodies come back, the loan is returned. + let host = make_host(false, false); + let persister = persister_for(&host, extensions); + let unaccounted = persister + .load_core_history_for_accounting([1; 32]) + .expect("history read must succeed") + .expect("not backfilled yet"); + assert_eq!( + unaccounted.iter().map(|tx| tx.txid()).collect::>(), + vec![first.txid(), second.txid()] + ); + assert_eq!(*host.frees.lock().unwrap(), 1); + + // Rows and marker commit in one round. + persister + .store_history_accounting([1; 32], &[row], true) + .expect("backfill round must succeed"); + assert_eq!( + *host.events.lock().unwrap(), + vec![ + "begin", + "row net=-5000 direction=1 fee=250", + "accounted", + "end success=true" + ] + ); + drop(persister); + + // Nothing to write still records completion. + let host = make_host(false, false); + persister_for(&host, extensions) + .store_history_accounting([1; 32], &[], true) + .expect("empty backfill round must succeed"); + assert_eq!( + *host.events.lock().unwrap(), + vec!["begin", "accounted", "end success=true"] + ); + + // An incomplete backfill writes its rows but no marker, so it stays + // open for the next load. + let host = make_host(false, false); + persister_for(&host, extensions) + .store_history_accounting([1; 32], &[row], false) + .expect("incomplete backfill round must succeed"); + assert_eq!( + *host.events.lock().unwrap(), + vec![ + "begin", + "row net=-5000 direction=1 fee=250", + "end success=true" + ] + ); + + // A failing row write rolls back, with no marker. + let host = make_host(false, true); + assert!(persister_for(&host, extensions) + .store_history_accounting([1; 32], &[row], true) + .is_err()); + assert_eq!( + *host.events.lock().unwrap(), + vec![ + "begin", + "row net=-5000 direction=1 fee=250", + "end success=false" + ] + ); + + // Backfilled: nothing to do. + let host = make_host(true, false); + assert_eq!( + persister_for(&host, extensions) + .load_core_history_for_accounting([1; 32]) + .expect("history read must succeed"), + None + ); + + // Without the completion slot the host is never read. + let host = make_host(false, false); + let without_marker = PersistenceExtensionCallbacks { + persist_core_history_accounted: None, + ..extensions + }; + assert_eq!( + persister_for(&host, without_marker) + .load_core_history_for_accounting([1; 32]) + .expect("a host that cannot complete a backfill is not read"), + None + ); + assert_eq!(*host.loads.lock().unwrap(), 0); + + // A count with no array is a broken answer, not an empty history: + // reading it as empty would let the round record completion. + let mut host = make_host(false, false); + host.rows.clear(); + host.claimed_count = Some(3); + assert!(persister_for(&host, extensions) + .load_core_history_for_accounting([1; 32]) + .is_err()); + assert_eq!(*host.frees.lock().unwrap(), 1, "the loan is still returned"); + + // Without the begin/end pair the rows and the marker could not commit + // as one unit: the host is never read, and a store writes nothing. + let host = make_host(false, false); + let unbracketed = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: &host as *const Host as *mut c_void, + on_changeset_begin_fn: Some(begin), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + extensions, + ); + assert_eq!( + unbracketed + .load_core_history_for_accounting([1; 32]) + .expect("a host without the round pair is not read"), + None + ); + assert_eq!(*host.loads.lock().unwrap(), 0); + assert!(unbracketed + .store_history_accounting([1; 32], &[row], true) + .is_err()); + assert!(host.events.lock().unwrap().is_empty()); + } + + /// The accounting backfill's funding read uses a stored body in any + /// context, InstantSend included, and reports a host failure or an + /// unusable body as an error rather than a miss. `get_core_tx_record` + /// keeps its policy: all three read as a miss there. + #[test] + fn should_read_a_funding_body_strictly_for_the_accounting_backfill() { + use platform_wallet::test_support::core_fixtures::{ + foreign_address, outside, pay, spend_of, + }; + + struct Host { + rc: i32, + found: bool, + context_kind: u8, + body: Vec, + frees: std::sync::Mutex, + } + unsafe extern "C" fn get( + ctx: *mut c_void, + _wallet_id: *const u8, + _txid: *const u8, + out_context_kind: *mut u8, + _out_block_height: *mut u32, + _out_block_hash: *mut u8, + _out_block_timestamp: *mut u32, + out_tx_bytes: *mut *const u8, + out_tx_bytes_len: *mut usize, + out_found: *mut bool, + ) -> i32 { + let host = &*(ctx as *const Host); + *out_context_kind = host.context_kind; + *out_found = host.found; + if !host.body.is_empty() { + *out_tx_bytes = host.body.as_ptr(); + *out_tx_bytes_len = host.body.len(); + } + host.rc + } + unsafe extern "C" fn free(ctx: *mut c_void, _bytes: *const u8, _len: usize) { + *(&*(ctx as *const Host)).frees.lock().unwrap() += 1; + } + + let funding = spend_of(&[outside(1)], vec![pay(5_000, &foreign_address(2))]); + let body = dashcore::consensus::encode::serialize(&funding); + let read = |rc: i32, found: bool, context_kind: u8, body: Vec| { + let host = Host { + rc, + found, + context_kind, + body, + frees: std::sync::Mutex::new(0), + }; + let persister = FFIPersister::new_with_persistence_capabilities_and_extensions( + PersistenceCallbacks { + context: &host as *const Host as *mut c_void, + on_get_core_tx_record_fn: Some(get), + on_get_core_tx_record_free_fn: Some(free), + ..PersistenceCallbacks::default() + }, + PersistenceCapabilities::NONE, + PersistenceExtensionCallbacks::default(), + ); + let strict = persister.get_core_transaction([1; 32], &funding.txid()); + let record = persister + .get_core_tx_record([1; 32], &funding.txid()) + .expect("the record read keeps reading failures as a miss") + .map(|record| record.transaction); + let frees = *host.frees.lock().unwrap(); + drop(persister); + (strict, record, frees) + }; + + // An InstantSend hit: the body is real, only the IS lock is missing. + let (strict, record, frees) = read(0, true, 1, body.clone()); + assert_eq!(strict.expect("an IS hit is a hit"), Some(funding.clone())); + assert_eq!(record, None, "the proof-facing read still skips IS"); + assert_eq!(frees, 2, "each read hands the buffer back"); + + let (strict, record, _) = read(0, true, TX_CONTEXT_RAW_IN_BLOCK as u8, body.clone()); + assert_eq!(strict.unwrap(), Some(funding.clone())); + assert_eq!(record, Some(funding.clone())); + + let (strict, record, _) = read(0, false, 0, Vec::new()); + assert_eq!(strict.unwrap(), None, "not stored is the only miss"); + assert_eq!(record, None); + + let (strict, record, _) = read(7, false, 0, Vec::new()); + assert!(strict.is_err(), "a failing host is not a miss"); + assert_eq!(record, None); + + let (strict, record, _) = read(0, true, 0, vec![0xFF]); + assert!(strict.is_err(), "an undecodable stored body is not a miss"); + assert_eq!(record, None); + + let (strict, record, _) = read(0, true, 0, Vec::new()); + assert!(strict.is_err(), "a hit without a body is not a miss"); + assert_eq!(record, None); + } + #[test] fn should_roundtrip_drain_operation_ids_including_consumed_tombstones() { use key_wallet::wallet::managed_wallet_info::asset_lock_builder::AssetLockFundingType; @@ -10583,6 +11434,46 @@ mod tests { PersistenceCallbacksExtension, on_load_asset_lock_shielded_recipient_fn ) + std::mem::size_of::>(), + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_transaction_accounting_fn + ) + ); + assert_eq!( + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_transaction_accounting_fn + ) + std::mem::size_of::>(), + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_fn + ) + ); + assert_eq!( + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_fn + ) + std::mem::size_of::>(), + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_free_fn + ) + ); + assert_eq!( + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_load_core_history_for_accounting_free_fn + ) + std::mem::size_of::>(), + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_core_history_accounted_fn + ) + ); + assert_eq!( + std::mem::offset_of!( + PersistenceCallbacksExtension, + on_persist_core_history_accounted_fn + ) + std::mem::size_of::>(), std::mem::size_of::() ); assert_eq!( @@ -11204,6 +12095,26 @@ mod tests { assert_eq!(detail.value, ASSET_LOCK_CHANGE); } + #[test] + fn should_keep_stored_accounting_when_a_restored_lock_gains_finality() { + let rt = tokio::runtime::Runtime::new().expect("runtime"); + let (mut ctx, lock, change) = + reloaded_after_unlocked_asset_lock(&rt, ChangeTo::FundingAccount); + assert!(ctx + .managed_wallet + .mark_instant_send_utxos(&lock.txid(), &InstantLock::default())); + + let records = wallet_slices(&ctx.managed_wallet, &lock.txid()); + assert!(!records.is_empty()); + assert!( + records + .iter() + .all(|record| record_accounting(record).is_none()), + "held change without spent-input ownership must not overwrite stored accounting" + ); + assert!(stored_coin_flags(records).contains(&(change, 0, false, true))); + } + /// A change coin the account no longer holds gets no detail: a store /// whose row for it is already gone would insert it as unspent. #[test] 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 8d4219fab61..cb5b992e2e7 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 @@ -593,10 +593,10 @@ mod tests { ); } - /// Shared with the Swift SDK's `TransactionAccountingTests` direction - /// table; pins the rule repair takes from `platform_wallet`. + /// Direction case table; pins the rule repair takes from `platform_wallet`, + /// the same one the live projection and the host-history backfill use. #[test] - fn should_classify_repaired_direction_like_the_swift_sdk() { + fn should_classify_repaired_direction_like_the_live_projection() { use TransactionDirection::{CoinJoin, Incoming, Internal, Outgoing}; use TransactionType::{AssetLock, Standard}; // (type, spends ours, has owned output, has external output, expected) diff --git a/packages/rs-platform-wallet/src/changeset/changeset.rs b/packages/rs-platform-wallet/src/changeset/changeset.rs index bbb9c19affb..6d52531d60a 100644 --- a/packages/rs-platform-wallet/src/changeset/changeset.rs +++ b/packages/rs-platform-wallet/src/changeset/changeset.rs @@ -36,7 +36,7 @@ use key_wallet::{AddressInfo, Network, PlatformP2PKHAddress, Utxo}; use crate::changeset::dashpay_backfill::DashPayBackfillRecord; use crate::changeset::identity_scan_state::IdentityScanStateEntry; -use crate::changeset::wallet_accounting::apply_wallet_accounting; +use crate::changeset::wallet_accounting::{apply_wallet_accounting, has_missing_accounting_inputs}; use crate::wallet::platform_wallet::WalletId; use dpp::balances::credits::Credits; @@ -460,7 +460,7 @@ impl HighestUsedIndexes { /// - identity fields (`transaction`, `txid`, `transaction_type`, /// `label`, `account_type`) — from the FUNDING record (the one with /// input details) so the row's account attribution names the spender, -/// else the first record. +/// else an incomplete restored funding record, else the first record. /// /// Then EVERY record — folded or single — gets its wallet-level /// `net_amount` and `direction` from its details via @@ -510,11 +510,18 @@ fn fold_groups(records: &mut Vec) { let mut drop_idx: BTreeSet = BTreeSet::new(); let mut folded: BTreeMap = BTreeMap::new(); for group in by_txid.values().filter(|g| g.len() >= 2) { - // Base: the funding record (has input details), else the first. + // Prefer complete funding evidence, then a restored funding record + // whose missing inputs must not be mistaken for a receipt. let base_pos = group .iter() .copied() .find(|&i| !records[i].input_details.is_empty()) + .or_else(|| { + group + .iter() + .copied() + .find(|&i| has_missing_accounting_inputs(&records[i])) + }) .unwrap_or(group[0]); let mut merged = records[base_pos].clone(); let mut net: i64 = 0; diff --git a/packages/rs-platform-wallet/src/changeset/mod.rs b/packages/rs-platform-wallet/src/changeset/mod.rs index 02a637bc2b0..a8024132ff4 100644 --- a/packages/rs-platform-wallet/src/changeset/mod.rs +++ b/packages/rs-platform-wallet/src/changeset/mod.rs @@ -70,4 +70,7 @@ 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::{ + funding_txids, history_accounting, is_owned, record_accounting, wallet_accounting, + wallet_direction, HistoryAccounting, WalletAccounting, +}; diff --git a/packages/rs-platform-wallet/src/changeset/traits.rs b/packages/rs-platform-wallet/src/changeset/traits.rs index 11e5d2c58a7..24aaffe75c0 100644 --- a/packages/rs-platform-wallet/src/changeset/traits.rs +++ b/packages/rs-platform-wallet/src/changeset/traits.rs @@ -8,8 +8,9 @@ use std::error::Error as StdError; use crate::changeset::changeset::{DpnsNameStateEntry, PlatformWalletChangeSet}; use crate::changeset::client_start_state::ClientStartState; use crate::changeset::persistence_capabilities::PersistenceCapabilities; +use crate::changeset::wallet_accounting::WalletAccounting; use crate::wallet::platform_wallet::WalletId; -use dashcore::Txid; +use dashcore::{Transaction, Txid}; use dpp::prelude::Identifier; use key_wallet::managed_account::transaction_record::TransactionRecord; @@ -494,6 +495,28 @@ pub trait PlatformWalletPersistence: Send + Sync { Ok(None) } + /// The body of a stored Core transaction, for the accounting backfill. + /// + /// Stricter than [`Self::get_core_tx_record`], whose policy suits the + /// asset-lock and DashPay callers that retry a miss anyway: here + /// `Ok(None)` must mean only "this backend does not store `txid`". A + /// stored transaction is returned in any context, InstantSend included, + /// and a stored one that cannot be read (a failing backend, an + /// undecodable body) is an `Err`. The backfill then fails its round and + /// runs again at the next load, rather than reading a wallet-owned input + /// as someone else's. + /// + /// The default delegates to [`Self::get_core_tx_record`]. + fn get_core_transaction( + &self, + wallet_id: WalletId, + txid: &Txid, + ) -> Result, PersistenceError> { + Ok(self + .get_core_tx_record(wallet_id, txid)? + .map(|record| record.transaction)) + } + /// Enumerate the persisted Core transaction ids that belong to /// `wallet_id`, each tagged with whether the transaction spends an /// input this wallet funded. @@ -528,6 +551,49 @@ pub trait PlatformWalletPersistence: Send + Sync { Ok(None) } + /// The wallet's stored Core transactions that have no + /// [`WalletAccounting`] yet, or `None` once this wallet's history + /// backfill has completed (see [`Self::store_history_accounting`]). + /// + /// Read at every load of the wallet until the backfill completes. The + /// manager computes the missing rows with + /// [`history_accounting`](crate::changeset::history_accounting), + /// fetching the funding transactions it needs through + /// [`Self::get_core_transaction`] itself, and hands them back through + /// [`Self::store_history_accounting`]. Any subset is safe: a transaction + /// left out only stays without a row. + /// + /// A transaction without a row is "not yet accounted", never "accounted + /// as nothing": the host displays it without an amount or direction + /// until a backfill or a live update writes the row. + /// + /// The default (`None`) suits backends that never stored rows without + /// accounting, such as the SQLite store, whose own migration repairs + /// history. + fn load_core_history_for_accounting( + &self, + _wallet_id: WalletId, + ) -> Result>, PersistenceError> { + Ok(None) + } + + /// Persist the backfilled rows (upsert keyed by `(txid, wallet_id)`) + /// and, when `complete`, record that this wallet's history backfill is + /// done, as one atomic write; `rows` may be empty. After a complete + /// write, [`Self::load_core_history_for_accounting`] returns `None` for + /// the wallet. An incomplete one (some transaction could not be + /// accounted yet) leaves the backfill open, so the next load reads the + /// transactions still without a row and tries again. The default drops + /// both. + fn store_history_accounting( + &self, + _wallet_id: WalletId, + _rows: &[WalletAccounting], + _complete: bool, + ) -> Result<(), PersistenceError> { + Ok(()) + } + /// Look up the persisted DPNS marketplace row for one /// `(wallet_id, wallet_identity_id, normalized_label)` triple. /// diff --git a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs index 48ae44a914a..3299385ddf5 100644 --- a/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs +++ b/packages/rs-platform-wallet/src/changeset/wallet_accounting.rs @@ -1,11 +1,16 @@ //! Wallet-level accounting of a Core transaction record: the ownership //! predicate ([`is_owned`]), the direction rule ([`wallet_direction`]) and //! the net formula ([`wallet_accounting`]). The live projection -//! (`fold_same_txid_records`) and the SQLite repair -//! (`platform-wallet-storage`'s `core_history`) both call them, so a row +//! (`fold_same_txid_records`), the per-wallet rows handed to hosts +//! ([`record_accounting`]), the host-history backfill +//! ([`history_accounting`]) and the SQLite repair +//! (`platform-wallet-storage`'s `core_history`) all call them, so a row //! cannot change accounting when storage repairs it; they differ only on //! an `i64` overflow (live saturates, repair errors). //! +//! Hosts (the Swift and Kotlin SDKs) persist [`WalletAccounting`] keyed by +//! `(txid, wallet id)` and display it; they never derive it themselves. +//! //! Upstream `key-wallet` classifies each matched account on its own, and //! its account-local views disagree for an asset lock: the funding //! account reads a lock with no change as `Outgoing` (the OP_RETURN burn @@ -14,12 +19,32 @@ //! moves Core duffs into its own Platform credits, so only the fee //! leaves the wallet. -use std::collections::BTreeSet; +use std::collections::{BTreeMap, BTreeSet}; +use dashcore::blockdata::script::Instruction; +use dashcore::hashes::hash160; +use dashcore::{Address, OutPoint, PubkeyHash, ScriptBuf, Transaction, TxIn, Txid}; +use key_wallet::account::AccountType; +use key_wallet::managed_account::managed_account_ref::ManagedAccountRef; +use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; use key_wallet::managed_account::transaction_record::{ OutputRole, TransactionDirection, TransactionRecord, }; -use key_wallet::transaction_checking::transaction_router::TransactionType; +use key_wallet::transaction_checking::transaction_router::{TransactionRouter, TransactionType}; +use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; + +/// One transaction's Core accounting from one wallet's point of view: the +/// value a host persists keyed by `(txid, wallet id)`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct WalletAccounting { + pub txid: Txid, + /// `Σ owned outputs − Σ owned inputs` in duffs, saturated to `i64`. + pub net_amount: i64, + pub direction: TransactionDirection, + /// The transaction fee, when the wallet funded every input; otherwise + /// the wallet cannot know it. + pub fee: Option, +} /// Classify a transaction from the wallet's point of view. /// @@ -30,11 +55,8 @@ use key_wallet::transaction_checking::transaction_router::TransactionType; /// /// 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`). +/// Platform credits. The case table lives in `platform-wallet-storage` +/// (`should_classify_repaired_direction_like_the_live_projection`). pub fn wallet_direction( transaction_type: TransactionType, spends_ours: bool, @@ -52,23 +74,260 @@ pub fn wallet_direction( } } +/// A funding account's outgoing/internal direction requires owned inputs. +/// Restore can retain that direction and held outputs without those inputs; +/// keys-account markers carry no Core funding evidence either way. +pub(crate) fn has_missing_accounting_inputs(record: &TransactionRecord) -> bool { + matches!( + record.account_type, + AccountType::Standard { .. } + | AccountType::CoinJoin { .. } + | AccountType::DashpayReceivingFunds { .. } + ) && record.input_details.is_empty() + && matches!( + record.direction, + TransactionDirection::Outgoing | TransactionDirection::Internal + ) +} + +fn has_accounting_evidence(record: &TransactionRecord) -> bool { + !(record.input_details.is_empty() && record.output_details.is_empty()) + && !has_missing_accounting_inputs(record) +} + /// Recompute a record's wallet-level `net_amount` and `direction` from /// its input and output details, saturating a net that does not fit `i64`. /// /// 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. +/// it. A restored funding record without its owned inputs is also left +/// alone: its held outputs prove coin ownership, not complete accounting. pub(crate) fn apply_wallet_accounting(record: &mut TransactionRecord) { - if record.input_details.is_empty() && record.output_details.is_empty() { + if !has_accounting_evidence(record) { return; } let (net, direction) = wallet_accounting(record); - // 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.net_amount = saturating_net(net); record.direction = direction; } +/// The wallet-level accounting `record` carries, or `None` when it carries +/// none. +/// +/// A record with no input or output details is no evidence of what the +/// transaction did to the wallet: a keys-account marker, or a record the +/// load path rebuilt from persisted bytes alone (an asset lock's funding +/// transaction, a spender of its input) and re-emits when a chain lock +/// promotes it. Such a record's `net_amount` is a placeholder, so the host +/// keeps whatever accounting it already stored. Restored outgoing/internal +/// funding records may name held outputs while still lacking their inputs; +/// those outputs do not turn the incomplete funding record into a receipt. +pub fn record_accounting(record: &TransactionRecord) -> Option { + if !has_accounting_evidence(record) { + return None; + } + let accounting = record_parts(record); + Some(WalletAccounting { + txid: record.txid, + net_amount: saturating_net(accounting.net), + direction: accounting.direction, + fee: accounting.fee, + }) +} + +/// The txids whose transactions [`history_accounting`] needs, beyond +/// `unaccounted` itself, to resolve the inputs of `unaccounted`: every +/// prevout that is neither one of those transactions' outputs nor one of the +/// wallet's restored UTXOs. Some of them are foreign and will not be found; +/// that is expected. +pub fn funding_txids(wallet: &ManagedWalletInfo, unaccounted: &[Transaction]) -> BTreeSet { + let own: BTreeSet = unaccounted.iter().map(Transaction::txid).collect(); + let utxos: BTreeSet = wallet_funds(wallet) + .flat_map(|funds| funds.utxos.keys().copied()) + .collect(); + unaccounted + .iter() + .flat_map(|transaction| &transaction.input) + .map(|input| input.previous_output) + .filter(|prevout| { + !prevout.is_null() && !own.contains(&prevout.txid) && !utxos.contains(prevout) + }) + .map(|prevout| prevout.txid) + .collect() +} + +/// Accounting for each of `unaccounted`, a host's stored transactions that +/// have no [`WalletAccounting`] yet, from the wallet's own view of which +/// scripts it owns. The live projection's rule, recomputed for rows a host +/// stored before it kept per-wallet accounting: +/// - an output is owned when its script belongs to one of the wallet's funds +/// accounts other than a contact's watch-only chain +/// ([`AccountType::is_contact_owned`](key_wallet::account::AccountType::is_contact_owned)); +/// an address-less, provably unspendable output is `Unspendable`, as +/// key-wallet classifies it; +/// - an input is owned when its prevout is an owned output of `unaccounted`, +/// of `funding` (the stored transactions [`funding_txids`] named), or one +/// of the wallet's restored UTXOs; +/// - type, net, direction and fee follow [`record_accounting`]. +/// +/// A transaction that touches no owned script yields no row, like a +/// detail-less record. +/// +/// An input whose prevout none of the above resolves is either someone +/// else's coin (every input of a genuine receipt) or the wallet's own coin +/// whose funding transaction the host could not supply. The two are told +/// apart by the input's redeem: when its scriptSig ends with a public key +/// whose P2PKH script the wallet owns, the wallet signed it. Such a +/// transaction gets no row and is listed in +/// [`HistoryAccounting::unresolved`] instead: reading the input as foreign +/// would turn a payment with change into an `Incoming` receipt of the +/// change. An input redeemed any other way reads as foreign; the wallet's +/// funds accounts hold only P2PKH scripts, so its own coins are always +/// redeemed with a public key. +/// +/// A transaction left without a row is not yet accounted, and the host +/// shows it as such until a later backfill (or the live projection) writes +/// it. Any subset of a host's unaccounted transactions is therefore safe to +/// pass. +pub fn history_accounting( + wallet: &ManagedWalletInfo, + unaccounted: &[Transaction], + funding: &[Transaction], +) -> HistoryAccounting { + let mut owned_scripts: BTreeSet = BTreeSet::new(); + let mut prevouts: BTreeMap = BTreeMap::new(); + for funds in wallet_funds(wallet) { + owned_scripts.extend(ManagedAccountRef::Funds(funds).all_script_pubkeys()); + for (outpoint, utxo) in &funds.utxos { + prevouts.insert(*outpoint, utxo.txout.value); + } + } + for transaction in unaccounted.iter().chain(funding) { + let txid = transaction.txid(); + for (vout, output) in transaction.output.iter().enumerate() { + if owned_scripts.contains(&output.script_pubkey) { + prevouts.insert( + OutPoint { + txid, + vout: vout as u32, + }, + output.value, + ); + } + } + } + + let mut accounted = HistoryAccounting::default(); + for transaction in unaccounted { + let mut spent: Vec = Vec::new(); + let mut owned_but_unresolved = false; + for input in &transaction.input { + match prevouts.get(&input.previous_output) { + Some(value) => spent.push(*value), + None if !input.previous_output.is_null() => { + owned_but_unresolved |= p2pkh_redeemed_script(input) + .is_some_and(|script| owned_scripts.contains(&script)); + } + None => {} + } + } + if owned_but_unresolved { + accounted.unresolved.push(transaction.txid()); + continue; + } + let owned: Vec<(usize, u64)> = transaction + .output + .iter() + .enumerate() + .filter(|(_, output)| owned_scripts.contains(&output.script_pubkey)) + .map(|(index, output)| (index, output.value)) + .collect(); + if spent.is_empty() && owned.is_empty() { + continue; + } + let unspendable: BTreeSet = transaction + .output + .iter() + .enumerate() + .filter(|(_, output)| { + Address::from_script(&output.script_pubkey, wallet.network).is_err() + && output.script_pubkey.is_provably_unspendable() + }) + .map(|(index, _)| index) + .collect(); + let accounting = accounting( + transaction, + TransactionRouter::classify_transaction(transaction), + &spent, + &owned, + &unspendable, + ); + accounted.rows.push(WalletAccounting { + txid: transaction.txid(), + net_amount: saturating_net(accounting.net), + direction: accounting.direction, + fee: accounting.fee, + }); + } + accounted +} + +/// What [`history_accounting`] made of a host's unaccounted transactions. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct HistoryAccounting { + /// One row per transaction it could account for. + pub rows: Vec, + /// Transactions left without a row because an input spends a coin the + /// wallet signed for but could not resolve. A later backfill retries + /// them. + pub unresolved: Vec, +} + +impl HistoryAccounting { + /// Whether every transaction that touches the wallet got its row, so the + /// backfill may be recorded as complete. + pub fn is_complete(&self) -> bool { + self.unresolved.is_empty() + } +} + +/// The P2PKH script `input` redeems, when its scriptSig ends with a public +/// key push (` `, how a P2PKH output is spent). `None` for any +/// other redeem. +fn p2pkh_redeemed_script(input: &TxIn) -> Option { + let Instruction::PushBytes(pubkey) = input.script_sig.instructions().last()?.ok()? else { + return None; + }; + let pubkey = pubkey.as_bytes(); + matches!(pubkey.len(), 33 | 65) + .then(|| ScriptBuf::new_p2pkh(&PubkeyHash::from_raw_hash(hash160::Hash::hash(pubkey)))) +} + +/// The wallet's funds accounts, without contacts' watch-only chains. +fn wallet_funds(wallet: &ManagedWalletInfo) -> impl Iterator + '_ { + wallet + .accounts + .all_accounts() + .into_iter() + .filter_map(|account| match account { + ManagedAccountRef::Funds(funds) + if !account + .managed_account_type() + .to_account_type() + .is_contact_owned() => + { + Some(funds) + } + _ => None, + }) +} + +/// Unreachable for real amounts (the whole supply fits i64 many times over); +/// saturate rather than panic on a corrupt record. +fn saturating_net(net: i128) -> i64 { + i64::try_from(net).unwrap_or(if net < 0 { i64::MIN } else { i64::MAX }) +} + /// Wallet-level net amount and direction of `record`, from its details. /// /// The net is `Σ owned outputs − Σ owned inputs`: the change in the @@ -77,41 +336,548 @@ pub(crate) fn apply_wallet_accounting(record: &mut TransactionRecord) { /// 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) { - let owned: i128 = record + let accounting = record_parts(record); + (accounting.net, accounting.direction) +} + +/// What [`accounting`] computes, before an `i64` policy is applied to `net`. +struct Accounting { + net: i128, + direction: TransactionDirection, + fee: Option, +} + +/// [`accounting`] over a record's details. +fn record_parts(record: &TransactionRecord) -> Accounting { + let spent: Vec = record.input_details.iter().map(|d| d.value).collect(); + let owned: Vec<(usize, u64)> = record .output_details .iter() .filter(|d| is_owned(d.role)) - .map(|d| i128::from(d.value)) - .sum(); - let spent: i128 = record - .input_details - .iter() - .map(|d| i128::from(d.value)) - .sum(); - let has_ours = record.output_details.iter().any(|d| is_owned(d.role)); - // Indexed once so the per-output check below stays linear. - let accounted: BTreeSet = record + .map(|d| (d.index as usize, d.value)) + .collect(); + let unspendable: BTreeSet = record .output_details .iter() - .filter(|d| is_owned(d.role) || d.role == OutputRole::Unspendable) + .filter(|d| d.role == OutputRole::Unspendable) .map(|d| d.index as usize) .collect(); - let has_external = record - .transaction + accounting( + &record.transaction, + record.transaction_type, + &spent, + &owned, + &unspendable, + ) +} + +/// The net formula, direction rule and fee over a transaction's owned +/// inputs (`spent` values) and owned outputs (`(index, value)`); +/// `unspendable` outputs leave nothing the wallet could lose, like an +/// OP_RETURN. The fee is known only when every input is an owned one. +fn accounting( + transaction: &Transaction, + transaction_type: TransactionType, + spent: &[u64], + owned: &[(usize, u64)], + unspendable: &BTreeSet, +) -> Accounting { + let received: i128 = owned.iter().map(|(_, value)| i128::from(*value)).sum(); + let paid: i128 = spent.iter().map(|value| i128::from(*value)).sum(); + // Indexed once so the per-output check below stays linear. + let accounted: BTreeSet = owned + .iter() + .map(|(index, _)| *index) + .chain(unspendable.iter().copied()) + .collect(); + let has_external = transaction .output .iter() .enumerate() .any(|(index, output)| !output.script_pubkey.is_op_return() && !accounted.contains(&index)); let direction = wallet_direction( - record.transaction_type, - !record.input_details.is_empty(), - has_ours, + transaction_type, + !spent.is_empty(), + !owned.is_empty(), has_external, ); - (owned - spent, direction) + let fee = (!spent.is_empty() && spent.len() == transaction.input.len()) + .then(|| { + let funded = spent + .iter() + .try_fold(0u64, |sum, value| sum.checked_add(*value))?; + let paid_out = transaction + .output + .iter() + .try_fold(0u64, |sum, output| sum.checked_add(output.value))?; + funded.checked_sub(paid_out) + }) + .flatten(); + Accounting { + net: received - paid, + direction, + fee, + } } /// Whether an output with this role belongs to the wallet. pub fn is_owned(role: OutputRole) -> bool { matches!(role, OutputRole::Received | OutputRole::Change) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::changeset::changeset::fold_same_txid_records; + use crate::test_support::core_fixtures::{ + block, foreign_address, outside, pay, records_of, spend_of, wallet_with_receive_address, + }; + use dashcore::transaction::special_transaction::asset_lock::AssetLockPayload; + use dashcore::transaction::special_transaction::TransactionPayload; + use dashcore::{Network, TxOut}; + use key_wallet::account::{Account, AccountType, StandardAccountType}; + use key_wallet::managed_account::address_pool::PublicKeyType; + use key_wallet::wallet::initialization::WalletAccountCreationOptions; + use key_wallet::Wallet; + + const FUNDING: u64 = 100_000; + const PAID: u64 = 30_000; + const CHANGE: u64 = 69_000; + const FEE: u64 = FUNDING - PAID - CHANGE; + /// A CoinJoin denomination (0.001 DASH + fee). + const DENOMINATION: u64 = 100_001; + + /// A payment from outside into the wallet, then `spend` of that coin, + /// both seen by the live projection. Returns the wallet as it stands + /// afterwards, the live rows, and the two transactions. + async fn live( + spend: impl FnOnce(OutPoint, &Address) -> Transaction, + ) -> ( + ManagedWalletInfo, + Vec, + Transaction, + Transaction, + ) { + let (mut wallet, mut info, receive) = wallet_with_receive_address(); + let xpub = wallet.accounts.standard_bip44_accounts[&0].account_xpub; + let change = info + .accounts + .standard_bip44_accounts + .get_mut(&0) + .unwrap() + .next_change_address(Some(&xpub), true) + .unwrap(); + let funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); + let spend = spend(OutPoint::new(funding.txid(), 0), &change); + let mut rows = Vec::new(); + for (tx, height) in [(&funding, 100), (&spend, 101)] { + let mut records = records_of(&mut info, &mut wallet, tx, block(height)).await; + fold_same_txid_records(&mut records); + rows.extend(records.iter().filter_map(record_accounting)); + } + (info, rows, funding, spend) + } + + fn payment_with_change(coin: OutPoint, change: &Address) -> Transaction { + spend_of( + &[coin], + vec![pay(PAID, &foreign_address(9)), pay(CHANGE, change)], + ) + } + + /// The backfill must write exactly what the live projection emitted for + /// the same transactions: amount, direction AND fee. + async fn assert_backfill_matches_live( + spend: impl FnOnce(OutPoint, &Address) -> Transaction, + ) -> Vec { + let (info, live, funding, spend) = live(spend).await; + assert_eq!(live.len(), 2, "the live projection accounts for both"); + let backfilled = history_accounting(&info, &[funding, spend], &[]).rows; + assert_eq!(backfilled, live); + backfilled + } + + #[tokio::test] + async fn should_backfill_a_payment_with_change_exactly_like_the_live_projection() { + let rows = assert_backfill_matches_live(payment_with_change).await; + + assert_eq!(rows[0].net_amount, FUNDING as i64); + assert_eq!(rows[0].fee, None, "a foreign input hides the fee"); + assert_eq!(rows[1].net_amount, CHANGE as i64 - FUNDING as i64); + assert_eq!(rows[1].direction, TransactionDirection::Outgoing); + assert_eq!(rows[1].fee, Some(FEE), "the wallet funded every input"); + } + + /// An asset lock with no change: the OP_RETURN burn becomes the wallet's + /// own Platform credits, so it is `Internal`, and the Core net is the + /// whole coin. + #[tokio::test] + async fn should_backfill_a_no_change_asset_lock_exactly_like_the_live_projection() { + let credit = FUNDING - FEE; + let rows = assert_backfill_matches_live(|coin, _| { + let mut lock = spend_of( + &[coin], + vec![TxOut { + value: credit, + script_pubkey: ScriptBuf::new_op_return(&[]), + }], + ); + lock.version = 3; + lock.special_transaction_payload = + Some(TransactionPayload::AssetLockPayloadType(AssetLockPayload { + version: 1, + credit_outputs: vec![pay(credit, &foreign_address(5))], + })); + lock + }) + .await; + + assert_eq!(rows[1].net_amount, -(FUNDING as i64)); + assert_eq!(rows[1].direction, TransactionDirection::Internal); + assert_eq!(rows[1].fee, Some(FEE)); + } + + /// An address-less, provably unspendable output that is not an OP_RETURN + /// takes nothing out of the wallet either: a self-transfer stays + /// `Internal`, as key-wallet's `Unspendable` role makes it live. + #[tokio::test] + async fn should_backfill_an_unspendable_output_exactly_like_the_live_projection() { + let rows = assert_backfill_matches_live(|coin, change| { + spend_of( + &[coin], + vec![ + pay(CHANGE, change), + TxOut { + value: 0, + // OP_VERIF: an illegal opcode, so provably unspendable. + script_pubkey: ScriptBuf::from_bytes(vec![0x65]), + }, + ], + ) + }) + .await; + + assert_eq!(rows[1].direction, TransactionDirection::Internal); + } + + #[tokio::test] + async fn should_backfill_a_coinjoin_exactly_like_the_live_projection() { + let rows = assert_backfill_matches_live(|coin, change| { + spend_of( + &[coin, outside(1), outside(2)], + vec![ + pay(DENOMINATION, change), + pay(DENOMINATION, &foreign_address(3)), + pay(DENOMINATION, &foreign_address(4)), + ], + ) + }) + .await; + + assert_eq!(rows[1].direction, TransactionDirection::CoinJoin); + assert_eq!(rows[1].fee, None, "other participants funded inputs"); + } + + /// A spend's coin resolves from a funding transaction passed in + /// `funding`; only `unaccounted` transactions get a row. + #[tokio::test] + async fn should_resolve_inputs_from_funding_transactions_and_answer_only_unaccounted() { + let (info, live, funding, spend) = live(payment_with_change).await; + + let backfilled = history_accounting(&info, std::slice::from_ref(&spend), &[funding]).rows; + + assert_eq!(backfilled, vec![live[1]]); + } + + /// A spend whose funding transaction the host no longer has still + /// resolves while its coin is a restored UTXO. + #[tokio::test] + async fn should_resolve_an_input_from_a_restored_utxo_alone() { + let (mut wallet, mut info, receive) = wallet_with_receive_address(); + let funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); + records_of(&mut info, &mut wallet, &funding, block(100)).await; + let spend = spend_of( + &[OutPoint::new(funding.txid(), 0)], + vec![pay(FUNDING - FEE, &foreign_address(9))], + ); + + let rows = history_accounting(&info, std::slice::from_ref(&spend), &[]).rows; + + assert_eq!( + rows, + vec![WalletAccounting { + txid: spend.txid(), + net_amount: -(FUNDING as i64), + direction: TransactionDirection::Outgoing, + fee: Some(FEE), + }] + ); + } + + /// The manager fetches only prevouts nothing else resolves: not the + /// unaccounted transactions' own outputs, not restored UTXOs, not a + /// coinbase's null prevout. + #[tokio::test] + async fn should_name_only_the_funding_transactions_nothing_else_resolves() { + let (mut wallet, mut info, receive) = wallet_with_receive_address(); + let utxo_funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); + records_of(&mut info, &mut wallet, &utxo_funding, block(100)).await; + let chained = spend_of(&[outside(8)], vec![pay(FUNDING, &foreign_address(1))]); + let spend = spend_of( + &[ + OutPoint::new(utxo_funding.txid(), 0), + OutPoint::new(chained.txid(), 0), + outside(6), + OutPoint::null(), + ], + vec![pay(1, &foreign_address(2))], + ); + + let needed = funding_txids(&info, &[chained.clone(), spend]); + + assert_eq!( + needed, + BTreeSet::from([outside(6).txid, outside(8).txid]), + "`chained` is itself unaccounted and the UTXO is restored" + ); + } + + #[tokio::test] + async fn should_yield_no_row_for_a_transaction_touching_no_owned_script() { + let (info, _, _, _) = live(payment_with_change).await; + let unrelated = spend_of(&[outside(3)], vec![pay(5_000, &foreign_address(4))]); + + assert_eq!( + history_accounting(&info, &[unrelated], &[]), + HistoryAccounting::default() + ); + } + + /// An output on a contact's watch-only chain is the contact's money, + /// not the wallet's: paying it is outgoing, the same rule the live + /// projection applies by dropping the contact account's slice. + #[tokio::test] + async fn should_not_count_a_contacts_watch_only_output_as_owned() { + let (_, mut info, receive) = wallet_with_receive_address(); + let contact_wallet = + Wallet::new_random(Network::Testnet, WalletAccountCreationOptions::Default).unwrap(); + let contact_xpub = contact_wallet.accounts.standard_bip44_accounts[&0].account_xpub; + let account = Account::new( + None, + AccountType::DashpayExternalAccount { + index: 0, + user_identity_id: [1; 32], + friend_identity_id: [2; 32], + }, + contact_xpub, + Network::Testnet, + ) + .unwrap(); + let contact_account = ManagedCoreFundsAccount::from_account(&account); + let contact_address = ManagedAccountRef::Funds(&contact_account) + .all_addresses() + .into_iter() + .next() + .expect("the contact chain derives addresses"); + info.accounts + .insert_funds_bearing_account(contact_account) + .unwrap(); + let funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); + let to_contact = spend_of( + &[OutPoint::new(funding.txid(), 0)], + vec![pay(FUNDING - FEE, &contact_address)], + ); + + let rows = history_accounting(&info, std::slice::from_ref(&to_contact), &[funding]).rows; + + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].net_amount, -(FUNDING as i64)); + assert_eq!(rows[0].direction, TransactionDirection::Outgoing); + } + + /// `tx` with input `index` redeemed the P2PKH way, ` `. + fn signed_by(mut tx: Transaction, index: usize, pubkey: &[u8]) -> Transaction { + let signature = [0x30; 71]; + tx.input[index].script_sig = dashcore::script::Builder::new() + .push_slice(<&dashcore::script::PushBytes>::try_from(&signature[..]).unwrap()) + .push_slice(<&dashcore::script::PushBytes>::try_from(pubkey).unwrap()) + .into_script(); + tx + } + + /// The compressed public key behind one of the wallet's own addresses. + fn owned_pubkey(info: &ManagedWalletInfo, address: &Address) -> Vec { + let account = &info.accounts.standard_bip44_accounts[&0]; + match ManagedAccountRef::Funds(account) + .get_address_info(address) + .and_then(|info| info.public_key) + { + Some(PublicKeyType::ECDSA(bytes)) => bytes, + other => panic!("the wallet derived {address} from an ECDSA key, got {other:?}"), + } + } + + /// A spend signed by the wallet's own key whose coin the host no longer + /// holds (funding transaction gone, no longer a UTXO) cannot be accounted: + /// reading the input as foreign would turn a payment with change into + /// an `Incoming` receipt of the change. It gets no row and is reported + /// unresolved, and accounts normally once the funding body is passed. + #[test] + fn should_withhold_the_row_of_a_spend_whose_owned_input_cannot_be_resolved() { + let (wallet, mut info, receive) = wallet_with_receive_address(); + let xpub = wallet.accounts.standard_bip44_accounts[&0].account_xpub; + let change = info + .accounts + .standard_bip44_accounts + .get_mut(&0) + .unwrap() + .next_change_address(Some(&xpub), true) + .unwrap(); + let funding = spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]); + let spend = signed_by( + payment_with_change(OutPoint::new(funding.txid(), 0), &change), + 0, + &owned_pubkey(&info, &receive), + ); + + let missing = history_accounting(&info, std::slice::from_ref(&spend), &[]); + assert_eq!( + missing, + HistoryAccounting { + rows: Vec::new(), + unresolved: vec![spend.txid()], + } + ); + assert!(!missing.is_complete()); + + let filled = history_accounting(&info, std::slice::from_ref(&spend), &[funding]); + assert_eq!( + filled.rows, + vec![WalletAccounting { + txid: spend.txid(), + net_amount: CHANGE as i64 - FUNDING as i64, + direction: TransactionDirection::Outgoing, + fee: Some(FEE), + }] + ); + assert!(filled.is_complete()); + } + + /// A genuine receipt's inputs are someone else's and never resolve; that + /// must not hold back its row. + #[test] + fn should_still_account_a_receipt_whose_foreign_inputs_cannot_be_resolved() { + let (_, info, receive) = wallet_with_receive_address(); + let mut foreign_key = vec![0x02]; + foreign_key.extend([7u8; 32]); + let receipt = signed_by( + spend_of(&[outside(7)], vec![pay(FUNDING, &receive)]), + 0, + &foreign_key, + ); + + let accounted = history_accounting(&info, std::slice::from_ref(&receipt), &[]); + + assert_eq!( + accounted, + HistoryAccounting { + rows: vec![WalletAccounting { + txid: receipt.txid(), + net_amount: FUNDING as i64, + direction: TransactionDirection::Incoming, + fee: None, + }], + unresolved: Vec::new(), + } + ); + } + + #[test] + fn should_keep_stored_accounting_when_restored_change_is_folded() { + use key_wallet::managed_account::transaction_record::OutputDetail; + + let tx = spend_of(&[outside(1)], vec![pay(5_000, &foreign_address(2))]); + let restored = TransactionRecord::new( + tx, + AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }, + block(10), + TransactionType::AssetLock, + TransactionDirection::Internal, + Vec::new(), + vec![OutputDetail { + index: 0, + role: OutputRole::Change, + address: Some(foreign_address(2)), + value: 5_000, + }], + 0, + ); + let mut receiving_slice = restored.clone(); + receiving_slice.account_type = AccountType::Standard { + index: 1, + standard_account_type: StandardAccountType::BIP44Account, + }; + receiving_slice.direction = TransactionDirection::Incoming; + receiving_slice.net_amount = 5_000; + receiving_slice.output_details[0].role = OutputRole::Received; + let mut restored_without_change = restored.clone(); + restored_without_change.output_details.clear(); + for mut records in [ + vec![restored.clone()], + vec![restored.clone(), receiving_slice.clone()], + vec![receiving_slice.clone(), restored.clone()], + vec![receiving_slice.clone(), restored_without_change], + ] { + fold_same_txid_records(&mut records); + assert_eq!(records.len(), 1); + assert_eq!( + record_accounting(&records[0]), + None, + "a funding record missing its inputs cannot turn held change into income" + ); + assert_eq!(records[0].output_details.len(), 1); + assert!(is_owned(records[0].output_details[0].role)); + } + + // A keys-only asset-lock marker says nothing about who funded it. + // It must not suppress a genuine receipt from another wallet. + let mut keys_marker = restored; + keys_marker.account_type = AccountType::IdentityRegistration; + keys_marker.output_details.clear(); + for mut records in [ + vec![keys_marker.clone(), receiving_slice.clone()], + vec![receiving_slice, keys_marker], + ] { + fold_same_txid_records(&mut records); + let accounting = record_accounting(&records[0]).expect("receipt accounting"); + assert_eq!(accounting.direction, TransactionDirection::Incoming); + assert_eq!(accounting.net_amount, 5_000); + } + } + + /// A record rebuilt at load from bytes alone (an asset lock's funding + /// transaction) is re-emitted on chain-lock promotion with a placeholder + /// net of 0: it must reach the host as "no accounting", not as 0. + #[test] + fn should_report_no_accounting_for_a_record_without_details() { + let tx = spend_of(&[outside(1)], vec![pay(5_000, &foreign_address(2))]); + let record = TransactionRecord::new( + tx, + AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }, + block(10), + TransactionType::AssetLock, + TransactionDirection::Internal, + Vec::new(), + Vec::new(), + 0, + ); + + assert_eq!(record_accounting(&record), None); + } +} diff --git a/packages/rs-platform-wallet/src/manager/load.rs b/packages/rs-platform-wallet/src/manager/load.rs index 1b612536087..2a07ec6f12f 100644 --- a/packages/rs-platform-wallet/src/manager/load.rs +++ b/packages/rs-platform-wallet/src/manager/load.rs @@ -3,7 +3,10 @@ use std::collections::BTreeMap; use std::sync::Arc; -use crate::changeset::{ClientStartState, ClientWalletStartState, PlatformWalletPersistence}; +use crate::changeset::{ + funding_txids, history_accounting, ClientStartState, ClientWalletStartState, + PlatformWalletPersistence, +}; use crate::error::PlatformWalletError; use crate::wallet::core::WalletGeneration; use crate::wallet::identity::IdentityManager; @@ -125,6 +128,10 @@ impl PlatformWalletManager

{ let mut inserted_in_wallets: Vec<(WalletId, Arc)> = Vec::new(); let mut load_error: Option = None; + // Wallets this load found already registered and skipped. Their + // accounting backfill still runs below: one that did not complete at + // an earlier load is retried by this one. + let mut already_registered: Vec = Vec::new(); // Each wallet's start state, and whether it was re-read under the // durable-cursor lock that `held` still holds (see the publication @@ -384,6 +391,7 @@ impl PlatformWalletManager

{ .get_wallet(&wallet_id) .is_some() { + already_registered.push(wallet_id); continue 'load; } if !reread && durable_cursors.contains_key(&wallet_id) { @@ -656,6 +664,19 @@ impl PlatformWalletManager

{ return Err(err); } + // Past the rollback point. Accounting is display-only, so a failure + // here is logged and never fails the load. Every wallet this load saw + // is offered the backfill, so an unfinished one is retried at the + // next load; a finished one costs the host one marker read. + let seen: std::collections::BTreeSet = inserted_in_wallets + .iter() + .map(|(wallet_id, _)| *wallet_id) + .chain(already_registered) + .collect(); + for wallet_id in seen { + self.backfill_history_accounting(wallet_id).await; + } + // Past the rollback point: every registration here is one this load // actually committed, so the transactions now have a wallet to belong // to for as long as it stays registered. @@ -735,6 +756,95 @@ impl PlatformWalletManager

{ Ok(()) } + + /// Backfill per-wallet accounting for the stored transactions the + /// persister holds without one (see + /// [`PlatformWalletPersistence::load_core_history_for_accounting`]). + /// Nothing is written unless every step succeeds, so a failed backfill + /// simply runs again at the next load. A transaction whose wallet-signed + /// input cannot be resolved yet gets no row and keeps the backfill open + /// (no completion marker), so the next load retries it. + async fn backfill_history_accounting(&self, wallet_id: WalletId) { + let persister = Arc::clone(&self.persister); + let unaccounted = + match run_blocking_load(move || persister.load_core_history_for_accounting(wallet_id)) + .await + { + Ok(Some(unaccounted)) => unaccounted, + Ok(None) => return, + Err(e) => { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = ?e, + "load: reading stored history for the accounting backfill failed" + ); + return; + } + }; + let needed = { + let wm = self.wallet_manager.read().await; + let Some(info) = wm.get_wallet_info(&wallet_id) else { + return; + }; + funding_txids(&info.core_wallet, &unaccounted) + }; + // Fetched without the manager lock held: each lookup calls the host. + // A stored funding transaction the host cannot hand over fails the + // round: read as missing, a wallet input could pass for a foreign one. + let persister = Arc::clone(&self.persister); + let funding = match run_blocking_load(move || { + let mut funding = Vec::new(); + for txid in &needed { + if let Some(transaction) = persister.get_core_transaction(wallet_id, txid)? { + funding.push(transaction); + } + } + Ok(funding) + }) + .await + { + Ok(funding) => funding, + Err(e) => { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + error = ?e, + "load: reading funding transactions for the accounting backfill failed" + ); + return; + } + }; + let accounted = { + let wm = self.wallet_manager.read().await; + let Some(info) = wm.get_wallet_info(&wallet_id) else { + return; + }; + history_accounting(&info.core_wallet, &unaccounted, &funding) + }; + let complete = accounted.is_complete(); + if !complete { + tracing::info!( + wallet_id = %hex::encode(wallet_id), + unresolved = accounted.unresolved.len(), + "load: some stored transactions spend wallet coins whose funding \ + transaction is unavailable; leaving them unaccounted for the next load" + ); + } + let rows = accounted.rows; + let persister = Arc::clone(&self.persister); + let count = rows.len(); + if let Err(e) = run_blocking_load(move || { + persister.store_history_accounting(wallet_id, &rows, complete) + }) + .await + { + tracing::warn!( + wallet_id = %hex::encode(wallet_id), + rows = count, + error = ?e, + "load: persisting backfilled transaction accounting failed" + ); + } + } } /// What one re-dispatch attempt did, so a caller — and a test — can tell the @@ -856,9 +966,18 @@ mod idempotent_load_tests { use key_wallet::wallet::ManagedWalletInfo; use key_wallet::Wallet; + use std::sync::Mutex; + + use dashcore::{OutPoint, Transaction, Txid}; + use key_wallet::account::{AccountType, StandardAccountType}; + use key_wallet::managed_account::transaction_record::{ + TransactionDirection, TransactionRecord, + }; + use key_wallet::transaction_checking::{TransactionContext, TransactionType}; + use crate::changeset::{ ClientStartState, ClientWalletStartState, IdentityManagerStartState, PersistenceError, - PlatformWalletChangeSet, PlatformWalletPersistence, + PlatformWalletChangeSet, PlatformWalletPersistence, WalletAccounting, }; use crate::events::PlatformEventHandler; use crate::test_support::NoopTestEventHandler; @@ -1312,6 +1431,381 @@ mod idempotent_load_tests { ); } + /// Hands back the stored transactions lacking accounting (`None` once + /// backfilled), serves funding transactions by txid, and records what + /// the manager persists. Like a host, a stored row takes its + /// transaction out of the unaccounted set and a complete backfill sets + /// the marker. + struct HistoryPersister { + wallet: Wallet, + managed: ManagedWalletInfo, + unaccounted: Mutex>>, + stored_transactions: Mutex>, + failing_lookup: bool, + /// Every stored transaction is found but cannot be handed over: the + /// lenient record read reports a miss, the strict body read fails. + unusable_funding: bool, + lookups: Mutex>, + stored: Mutex, bool)>>, + } + + impl PlatformWalletPersistence for HistoryPersister { + fn store( + &self, + _wallet_id: WalletId, + _changeset: PlatformWalletChangeSet, + ) -> Result<(), PersistenceError> { + Ok(()) + } + + fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + Ok(()) + } + + fn load(&self) -> Result { + SingleWalletPersister { + wallet: self.wallet.clone(), + managed: self.managed.clone(), + } + .load() + } + + fn get_core_tx_record( + &self, + _wallet_id: WalletId, + txid: &Txid, + ) -> Result, PersistenceError> { + if self.failing_lookup { + return Err(PersistenceError::backend("store unreadable")); + } + if self.unusable_funding { + return Ok(None); + } + Ok(self + .stored_transactions + .lock() + .unwrap() + .get(txid) + .map(|tx| { + TransactionRecord::new( + tx.clone(), + AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }, + TransactionContext::Mempool, + TransactionType::Standard, + TransactionDirection::Incoming, + Vec::new(), + Vec::new(), + 0, + ) + })) + } + + fn get_core_transaction( + &self, + wallet_id: WalletId, + txid: &Txid, + ) -> Result, PersistenceError> { + self.lookups.lock().unwrap().push(*txid); + if self.unusable_funding && self.stored_transactions.lock().unwrap().contains_key(txid) + { + return Err(PersistenceError::backend("stored but unreadable")); + } + Ok(self + .get_core_tx_record(wallet_id, txid)? + .map(|record| record.transaction)) + } + + fn load_core_history_for_accounting( + &self, + _wallet_id: WalletId, + ) -> Result>, PersistenceError> { + Ok(self.unaccounted.lock().unwrap().clone()) + } + + fn store_history_accounting( + &self, + wallet_id: WalletId, + rows: &[WalletAccounting], + complete: bool, + ) -> Result<(), PersistenceError> { + self.stored + .lock() + .unwrap() + .push((wallet_id, rows.to_vec(), complete)); + let mut unaccounted = self.unaccounted.lock().unwrap(); + if complete { + *unaccounted = None; + } else if let Some(unaccounted) = unaccounted.as_mut() { + unaccounted.retain(|tx| rows.iter().all(|row| row.txid != tx.txid())); + } + Ok(()) + } + } + + /// A wallet funded by `funding` (100_000 duffs, output 0), and a spend of + /// that coin paying 74_000 to a foreign address. + async fn funded_wallet() -> (Wallet, ManagedWalletInfo, Transaction, Transaction) { + let (ctx, funding) = TestWalletContext::new_random() + .with_mempool_funding(100_000) + .await; + let spend = spend_to(OutPoint::new(funding.txid(), 0), 74_000); + (ctx.wallet, ctx.managed_wallet, funding, spend) + } + + async fn backfill(persister: HistoryPersister) -> Arc { + let persister = Arc::new(persister); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let event_handler: Arc = Arc::new(NoopTestEventHandler); + let manager = PlatformWalletManager::new(sdk, Arc::clone(&persister), event_handler); + manager + .load_from_persistor() + .await + .expect("the wallet must load"); + persister + } + + fn history_persister( + wallet: Wallet, + managed: ManagedWalletInfo, + unaccounted: Option>, + stored: &[&Transaction], + ) -> HistoryPersister { + HistoryPersister { + wallet, + managed, + unaccounted: Mutex::new(unaccounted), + stored_transactions: Mutex::new( + stored.iter().map(|tx| (tx.txid(), (*tx).clone())).collect(), + ), + failing_lookup: false, + unusable_funding: false, + lookups: Mutex::new(Vec::new()), + stored: Mutex::new(Vec::new()), + } + } + + fn without_utxos(mut managed: ManagedWalletInfo) -> ManagedWalletInfo { + for account in managed.accounts.standard_bip44_accounts.values_mut() { + account.utxos.clear(); + } + managed + } + + /// Rows stored before per-wallet accounting are backfilled at load. A + /// spend whose coin is among the unaccounted transactions resolves + /// without a lookup; only the funding transaction's foreign prevout is + /// asked for (and not found). + #[tokio::test] + async fn load_backfills_accounting_for_unaccounted_history() { + let (wallet, managed, funding, spend) = funded_wallet().await; + let wallet_id = wallet.compute_wallet_id(); + let persister = backfill(history_persister( + wallet, + without_utxos(managed), + Some(vec![funding.clone(), spend.clone()]), + &[], + )) + .await; + + assert_eq!( + *persister.stored.lock().unwrap(), + vec![( + wallet_id, + vec![ + WalletAccounting { + txid: funding.txid(), + net_amount: 100_000, + direction: TransactionDirection::Incoming, + fee: None, + }, + WalletAccounting { + txid: spend.txid(), + net_amount: -100_000, + direction: TransactionDirection::Outgoing, + fee: Some(26_000), + }, + ], + true + )] + ); + assert!( + !persister.lookups.lock().unwrap().contains(&funding.txid()), + "the spend's coin resolves from the unaccounted funding transaction" + ); + } + + /// A spend whose coin is neither among the unaccounted transactions nor + /// a UTXO any more resolves through the funding transaction the manager + /// fetches itself; the host is never asked to send its whole history. + #[tokio::test] + async fn load_fetches_the_funding_transaction_a_backfilled_spend_needs() { + let (wallet, managed, funding, spend) = funded_wallet().await; + let persister = backfill(history_persister( + wallet, + without_utxos(managed), + Some(vec![spend.clone()]), + &[&funding], + )) + .await; + + assert_eq!(*persister.lookups.lock().unwrap(), vec![funding.txid()]); + let stored = persister.stored.lock().unwrap().clone(); + assert_eq!(stored.len(), 1); + assert!(stored[0].2, "every transaction was accounted"); + assert_eq!( + stored[0].1, + vec![WalletAccounting { + txid: spend.txid(), + net_amount: -100_000, + direction: TransactionDirection::Outgoing, + fee: Some(26_000), + }] + ); + } + + /// A failed funding lookup writes nothing, so the backfill (and the + /// completion it records) runs again at the next load. + #[tokio::test] + async fn load_writes_no_backfill_when_a_funding_lookup_fails() { + let (wallet, managed, _, spend) = funded_wallet().await; + let mut persister = + history_persister(wallet, without_utxos(managed), Some(vec![spend]), &[]); + persister.failing_lookup = true; + let persister = backfill(persister).await; + + assert_eq!(persister.lookups.lock().unwrap().len(), 1); + assert!(persister.stored.lock().unwrap().is_empty()); + } + + /// A funding transaction the host stores but cannot hand over (an + /// InstantSend-only row the lenient record read drops, a failing read) + /// fails the round: nothing is written, not even the rows that did + /// resolve, so the next load runs the whole backfill again. + #[tokio::test] + async fn load_writes_no_backfill_when_a_stored_funding_transaction_is_unusable() { + let (wallet, managed, funding, spend) = funded_wallet().await; + let mut persister = history_persister( + wallet, + without_utxos(managed), + Some(vec![spend]), + &[&funding], + ); + persister.unusable_funding = true; + let persister = backfill(persister).await; + + assert_eq!(*persister.lookups.lock().unwrap(), vec![funding.txid()]); + assert!(persister.stored.lock().unwrap().is_empty()); + } + + /// `spend` with its input redeemed by the key behind `funding`'s output + /// 0, the wallet's own receive address: what the wallet itself signed. + fn signed_by_the_wallet( + managed: &ManagedWalletInfo, + funding: &Transaction, + mut spend: Transaction, + ) -> Transaction { + use key_wallet::managed_account::address_pool::PublicKeyType; + use key_wallet::managed_account::ManagedAccountRef; + + let address = + dashcore::Address::from_script(&funding.output[0].script_pubkey, managed.network) + .expect("the funding pays a P2PKH address"); + let Some(PublicKeyType::ECDSA(pubkey)) = + ManagedAccountRef::Funds(&managed.accounts.standard_bip44_accounts[&0]) + .get_address_info(&address) + .and_then(|info| info.public_key) + else { + panic!("the wallet derived the funded address from an ECDSA key"); + }; + spend.input[0].script_sig = dashcore::script::Builder::new() + .push_slice(<&dashcore::script::PushBytes>::try_from(&[0x30; 71][..]).unwrap()) + .push_slice(<&dashcore::script::PushBytes>::try_from(&pubkey[..]).unwrap()) + .into_script(); + spend + } + + /// A spend the wallet signed whose funding transaction the host does not + /// have yet is left without a row and keeps the backfill open: no + /// completion marker. A later load of the same, already registered, + /// wallet retries it and writes the row once the funding transaction is + /// there, and only then completes. A load after that reads nothing. + #[tokio::test] + async fn load_leaves_an_unresolvable_wallet_spend_open_then_fills_it_at_a_later_load() { + let (wallet, managed, funding, spend) = funded_wallet().await; + let wallet_id = wallet.compute_wallet_id(); + let managed = without_utxos(managed); + let spend = signed_by_the_wallet(&managed, &funding, spend); + let persister = Arc::new(history_persister( + wallet, + managed, + Some(vec![spend.clone()]), + &[], + )); + let sdk = Arc::new(dash_sdk::SdkBuilder::new_mock().build().expect("mock sdk")); + let event_handler: Arc = Arc::new(NoopTestEventHandler); + let manager = PlatformWalletManager::new(sdk, Arc::clone(&persister), event_handler); + + manager.load_from_persistor().await.expect("first load"); + assert_eq!( + *persister.stored.lock().unwrap(), + vec![(wallet_id, Vec::new(), false)], + "skipped: no row, and the backfill stays open" + ); + + persister + .stored_transactions + .lock() + .unwrap() + .insert(funding.txid(), funding.clone()); + manager.load_from_persistor().await.expect("second load"); + assert_eq!( + persister.stored.lock().unwrap()[1..], + [( + wallet_id, + vec![WalletAccounting { + txid: spend.txid(), + net_amount: -100_000, + direction: TransactionDirection::Outgoing, + fee: Some(26_000), + }], + true + )], + "filled by the retry on the already registered wallet" + ); + + manager.load_from_persistor().await.expect("third load"); + assert_eq!( + persister.stored.lock().unwrap().len(), + 2, + "a completed backfill is not run again" + ); + } + + /// Nothing unaccounted still completes the backfill, so the host stops + /// being asked; an already backfilled wallet is left alone. + #[tokio::test] + async fn load_completes_an_empty_backfill_and_skips_a_finished_one() { + let (wallet, managed, _, _) = funded_wallet().await; + let wallet_id = wallet.compute_wallet_id(); + let empty = backfill(history_persister( + wallet.clone(), + managed.clone(), + Some(Vec::new()), + &[], + )) + .await; + assert_eq!( + *empty.stored.lock().unwrap(), + vec![(wallet_id, Vec::new(), true)] + ); + + let finished = backfill(history_persister(wallet, managed, None, &[])).await; + assert!(finished.stored.lock().unwrap().is_empty()); + } + fn make_pending_manager( persister: PendingSendPersister, ) -> Arc> { diff --git a/packages/swift-sdk/SCHEMA_RELEASES.md b/packages/swift-sdk/SCHEMA_RELEASES.md index bc9c7f29453..755185a48c4 100644 --- a/packages/swift-sdk/SCHEMA_RELEASES.md +++ b/packages/swift-sdk/SCHEMA_RELEASES.md @@ -5,7 +5,8 @@ distribution. TestFlight uploads capture provenance and a synthetic SQLite fixture, but do not by themselves register a released schema. The accepted frozen V1 remains unchanged. Historical V2 is now reconstructed from `52e8d4ec68f0c772313fa1bbef223fb1eabbf1cc`; all 35 entity hashes and the model checksum match the observed App Store 9.0.2 database. Active models are -V3. Other intermediate development shapes remain unsupported. +V5. The merged V4 graph is preserved by `DashSchemaSnapshotV4`. The released V3 graph is preserved by `DashSchemaSnapshotV3`. Other +intermediate development shapes remain unsupported. The old V2 fixture was generated from September 8 sources containing 13 properties added on August 28, after the August 27 App Store release. The @@ -15,26 +16,39 @@ separate from archive-captured releases. This identifies a matching model source, not the confirmed build commit of Apple's binary. V2 is reserved: automated release capture must not register another shape under that number. -The main migration plan is historical V2 → V3. Accepted V1 has a separate -V1 → V3 plan: V1 already contains the 13 properties missing from historical -V2, so a V1 → V2 → V3 chain could discard values. Routing uses model metadata +The main migration plan is historical V2 → V3 → V4 → V5. Accepted V1 has a separate +V1 → V3 → V4 → V5 plan: V1 already contains the 13 properties missing from historical +V2, so a chain through V2 could discard values. Routing uses model metadata and runs after recovery; a version label alone never selects an unknown beta schema. The former live V2 is accepted only when its complete graph matches -current V3 exactly, entity hashes and checksum alike. That alias therefore -lasts only until the next live-graph change: after it, every store still -labelled `2.0.0` with the former live shape becomes `unsupported-v2` and fails -closed. Check internal devices still carrying that label before the next -shape change and migrate or deliberately reset them then, rather than -discovering them afterwards as failed opens. +frozen V3 exactly, entity hashes and checksum alike. The alias remains supported +when the live graph changes; other stores labelled `2.0.0` fail closed. + +V5 adds the `PersistentTransactionAccounting` entity and the +`PersistentWallet.transactionAccountingBackfilled` attribute (default `false`) +through a lightweight migration, alongside the optional +`PersistentAssetLock.shieldedRecipientRaw43` attribute (default `nil`), the +Orchard recipient of a shielded asset-lock funding. Each accounting row, one per +`(walletId, txid)`, holds the net amount, direction and fee `rs-platform-wallet` +emitted for that wallet. Migrated stores start with no rows; at the first load +of each wallet Rust reads back the wallet's transactions that lack a row +(`on_load_core_history_for_accounting_fn`), fetches any funding transaction it +needs through `on_get_core_tx_record_fn`, and writes the rows +(`on_persist_transaction_accounting_fn`) and the marker +(`on_persist_core_history_accounted_fn`) in one round. A transaction whose +wallet-signed input Rust cannot resolve yet gets no row, and the round then +writes no marker, so the next load reads the transactions still without a +row again. A missing row means "not yet accounted", never zero. Once the +marker is set, later loads skip the read. Deleting a wallet deletes its rows; +deleting a transaction cascades to them. The route decision is logged as `store_migration_route` with the validated `source_version`, `source_checksum` and one of `new-store`, -`accepted-v1-to-v3`, `historical-v2-to-v3`, `previous-live-v2-current-shape`, -`unsupported-v2`, `published-v3-to-v4`, `published-v4-to-v5`, -`labelled-current-v5`, `ordinary-current-plan` or, from the +`accepted-v1-to-v5`, `historical-v2-to-v5`, `previous-live-v2-v3-shape`, +`published-v3-to-v5`, `published-v4-to-v5`, `unsupported-v2`, `labelled-current-v5`, `ordinary-current-plan` or, from the bridge, `legacy-v1-bridge-to-v3`. Resolving a frozen schema's identity builds a temporary store, so it is memoized per process and consulted only where a -label is undecidable without it (`1.0.0`, `2.0.0`); a `3.0.0` label takes the +label is undecidable without it (`1.0.0`, `2.0.0`); a `3.0.0`, `4.0.0` or `5.0.0` label takes the default plan without any probe, and a probe failure on the undecidable labels refuses the open with the probe's own error while leaving the store untouched. @@ -138,9 +152,9 @@ The run failed before upload, so this provenance establishes a tested source layout rather than proof of publication. Regression tests exercise the public factory, data/default preservation, writes, reopen, and failure recovery. -Keep the bridge for installations that skip the V3 app release. When advancing -to V4, bind `DashSchemaV3` to its released snapshot and retain the legacy-to-V3 -step before the normal V3-to-current plan. The bridge must never automatically +Keep the bridge for installations that skip the V3 app release. `DashSchemaV3` +stays bound to its released snapshot, retaining the legacy-to-V3 step before +the normal V3-to-current plan. The bridge must never automatically follow the latest live model graph. The release observer's one-time `bootstrap` only records its observation baseline; it neither runs this migration nor proves V1's App Store provenance. diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift index e1ba0479049..a29dc65f7df 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/DashModelContainer.swift @@ -94,13 +94,24 @@ public enum DashModelContainer { PersistentInvitation.self, PersistentMasternode.self, PersistentTrackedMasternode.self, - PersistentIdentityBalanceMetadata.self + PersistentIdentityBalanceMetadata.self, + PersistentTransactionAccounting.self ] } + /// The live versioned schema. `schema` and every diagnostic's + /// `target_version` derive from it, so a schema bump is one edit. + typealias LiveSchema = DashSchemaV5 + /// Create the schema for all Dash Platform models public static var schema: Schema { - Schema(versionedSchema: DashSchemaV5.self) + Schema(versionedSchema: LiveSchema.self) + } + + /// `LiveSchema`'s version as logged in `target_version` (`major.minor.patch`). + static var targetVersion: String { + let version = LiveSchema.versionIdentifier + return "\(version.major).\(version.minor).\(version.patch)" } /// Create a persistent model container for storing data. @@ -172,7 +183,7 @@ public enum DashModelContainer { bridgeLegacyStore: Bool = true ) throws -> ModelContainer { SDKLogger.event("store_open_started", category: .persistence, - fields: ["target_version": .publicText("5.0.0")]) + fields: ["target_version": .publicText(targetVersion)]) do { let container: ModelContainer if bridgeLegacyStore { @@ -185,7 +196,7 @@ public enum DashModelContainer { configurations: [configuration]) } SDKLogger.event("store_open_succeeded", category: .persistence, - fields: ["target_version": .publicText("5.0.0")]) + fields: ["target_version": .publicText(targetVersion)]) return container } catch { logMigrationFailure(error) @@ -201,7 +212,7 @@ public enum DashModelContainer { let domain = systemDomains.contains(nsError.domain) ? nsError.domain : String(reflecting: type(of: error)) SDKLogger.event("store_open_failed", category: .persistence, severity: .error, fields: ["error_domain": .publicText(domain), "error_code": .integer(Int64(nsError.code)), - "target_version": .publicText("5.0.0")]) + "target_version": .publicText(targetVersion)]) } /// Select by the complete stored model identity, after journal recovery. @@ -218,7 +229,7 @@ public enum DashModelContainer { guard ObjectIdentifier(defaultPlan) == ObjectIdentifier(DashMigrationPlan.self) else { return defaultPlan } guard FileManager.default.fileExists(atPath: url.path) else { SDKLogger.event("store_migration_route", category: .persistence, fields: [ - "route": .publicText("new-store"), "target_version": .publicText("5.0.0")]) + "route": .publicText("new-store"), "target_version": .publicText(targetVersion)]) return defaultPlan } let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(type: .sqlite, at: url) @@ -236,7 +247,7 @@ public enum DashModelContainer { func logRoute(_ route: String) { SDKLogger.event("store_migration_route", category: .persistence, fields: [ "source_version": .publicText(safeVersions), "source_checksum": .publicText(safeChecksum), - "route": .publicText(route), "target_version": .publicText("5.0.0")]) + "route": .publicText(route), "target_version": .publicText(targetVersion)]) } func matches(_ type: any VersionedSchema.Type) throws -> Bool { let expected = try identity(type) @@ -247,7 +258,7 @@ public enum DashModelContainer { // probe stays fatal here: the open fails with the probe's own error // instead of an inapplicable plan, and the store is untouched. if versions == ["1.0.0"], try matches(DashSchemaV1.self) { - logRoute("accepted-v1-to-v3") + logRoute("accepted-v1-to-v5") return DashAcceptedV1MigrationPlan.self } if versions == ["2.0.0"] { @@ -261,15 +272,15 @@ public enum DashModelContainer { throw DashLegacyStoreSQLite.Failure.unsupported( "The database identifies itself as schema 2.0.0 but its model does not match the supported historical or current schema. The original database has not been replaced. Contact support; do not delete the app.") } - logRoute(historical ? "historical-v2-to-v3" : "previous-live-v2-current-shape") + logRoute(historical ? "historical-v2-to-v5" : "previous-live-v2-v3-shape") } else if versions == ["3.0.0"] { - // Same plan either way. Opening a current store must neither wait - // on nor fail with a schema probe that could only refine this line; - // `source_checksum` above already identifies the exact graph. - logRoute("published-v3-to-v4") + logRoute("published-v3-to-v5") } else if versions == ["4.0.0"] { logRoute("published-v4-to-v5") } else if versions == ["5.0.0"] { + // Same plan either way. Opening a current store must neither wait + // on nor fail with a schema probe that could only refine this line; + // `source_checksum` above already identifies the exact graph. logRoute("labelled-current-v5") } else { logRoute("ordinary-current-plan") @@ -394,7 +405,9 @@ public enum DashSchemaV4: VersionedSchema { public static var models: [any PersistentModel.Type] { DashSchemaSnapshotV4.models } } -/// Current schema: an asset lock records the optional Orchard recipient of its shielded funding. +/// Current schema: an asset lock records the optional Orchard recipient of its shielded +/// funding; per-wallet transaction accounting (`PersistentTransactionAccounting`) and the +/// wallet's accounting-backfill marker. public enum DashSchemaV5: VersionedSchema { public static var versionIdentifier: Schema.Version { Schema.Version(5, 0, 0) } public static var models: [any PersistentModel.Type] { DashModelContainer.modelTypes } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift index f0ecd0fce34..fde28b02c95 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransaction.swift @@ -84,9 +84,12 @@ public final class PersistentTransaction { /// replaces it with the real discriminant on touch. Accessors /// treat the sentinel as unknown (no branch fires). public var transactionTypeKind: UInt8 = 0xFF - /// Net amount in duffs (signed: positive=received, negative=sent). + /// Net amount in duffs (signed: positive=received, negative=sent) as the + /// last recording wallet's `TransactionRecordFFI` carried it. Shared by + /// every wallet in the transaction, so only unscoped views (the storage + /// explorer) show it; per-wallet values live in [`walletAccounting`]. public var netAmount: Int64 - /// Fee in duffs (nil if unknown). + /// Fee in duffs (nil if unknown); same last-writer caveat as `netAmount`. public var fee: UInt64? /// User-assigned label. public var label: String @@ -180,6 +183,11 @@ public final class PersistentTransaction { @Relationship(inverse: \PersistentAccount.involvedTransactions) public var involvedAccounts: [PersistentAccount] = [] + /// Rust-emitted accounting per wallet, one row per `(wallet, txid)`. + /// Cascade-deletes with the transaction. + @Relationship(deleteRule: .cascade, inverse: \PersistentTransactionAccounting.transaction) + public var walletAccounting: [PersistentTransactionAccounting] = [] + public init( txid: Data, transactionData: Data, @@ -228,12 +236,68 @@ public final class PersistentTransaction { } } + /// The accounting Rust emitted for `walletId`, if any. + public func accounting(for walletId: Data) -> PersistentTransactionAccounting? { + walletAccounting.first { $0.walletId == walletId } + } + + /// Core value movement for one wallet; `nil` when Rust has emitted no + /// accounting for this wallet and transaction yet. `nil` means "not yet + /// resolved, will be filled", not zero (see + /// `PersistentTransactionAccounting`). + public func netAmount(for walletId: Data) -> Int64? { + accounting(for: walletId)?.netAmount + } + + /// `CoreDirectionCode` for one wallet; `nil` like `netAmount(for:)`. + public func direction(for walletId: Data) -> UInt32? { + accounting(for: walletId)?.direction + } + + /// Fee for one wallet; `nil` without a row, or when it funded only some inputs. + public func fee(for walletId: Data) -> UInt64? { + accounting(for: walletId)?.fee + } + + /// The wallet's Core value movement in DASH, or "—" without accounting. + public func formattedAmount(for walletId: Data) -> String { + netAmount(for: walletId).map(Self.format(duffs:)) ?? Self.noAccounting + } + + /// Net amount for `walletId`, or the stored scalar when no wallet scope is given. + public func displayNetAmount(for walletId: Data?) -> Int64? { + walletId.map { netAmount(for: $0) } ?? netAmount + } + + /// `CoreDirectionCode` for `walletId`, or the stored direction when no wallet scope is given. + public func displayDirectionCode(for walletId: Data?) -> UInt32? { + walletId.map { direction(for: $0) } ?? direction + } + + /// Fee for `walletId`, or the stored fee when no wallet scope is given. + public func displayFee(for walletId: Data?) -> UInt64? { + walletId.map { fee(for: $0) } ?? fee + } + + /// Formatted amount for `walletId`, or the stored scalar's when no wallet scope is given. + public func displayFormattedAmount(for walletId: Data?) -> String { + walletId.map { formattedAmount(for: $0) } ?? formattedAmount + } + + /// Shown in place of an amount Rust has not accounted for this wallet. + public static let noAccounting = "—" + + /// Signed DASH text for a duff amount; `magnitude` cannot trap on `Int64.min`. + public static func format(duffs: Int64) -> String { + String(format: "%@%.8f DASH", duffs >= 0 ? "+" : "-", Double(duffs.magnitude) / 100_000_000) + } + public var directionName: String { switch direction { - case 0: return "Incoming" - case 1: return "Outgoing" - case 2: return "Internal" - case 3: return "CoinJoin" + case CoreDirectionCode.incoming: return "Incoming" + case CoreDirectionCode.outgoing: return "Outgoing" + case CoreDirectionCode.internalTransfer: return "Internal" + case CoreDirectionCode.coinJoin: return "CoinJoin" default: return "Unknown" } } @@ -337,9 +401,7 @@ public final class PersistentTransaction { } public var formattedAmount: String { - let dash = Double(abs(netAmount)) / 100_000_000.0 - let sign = netAmount >= 0 ? "+" : "-" - return String(format: "%@%.8f DASH", sign, dash) + Self.format(duffs: netAmount) } } diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift new file mode 100644 index 00000000000..28df6c50ddd --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift @@ -0,0 +1,52 @@ +import Foundation +import SwiftData + +/// One transaction's Core accounting for ONE wallet, exactly as +/// `rs-platform-wallet` emitted it (`TransactionAccountingFFI`, through +/// `on_persist_transaction_accounting_fn`). +/// +/// A transaction shared by several local wallets has one row per wallet, +/// so each wallet shows its own side of it. A missing row means "not yet +/// accounted, will be filled", never "accounted as zero": a payload-only +/// provider transaction, a stored row the load-time backfill has not +/// reached, or one whose wallet-signed input Rust cannot resolve yet (its +/// funding transaction is unavailable). Rust writes the row once it can, +/// at a later backfill or a live update, and never writes a guessed one. +/// Pure storage: nothing on the Swift side derives, corrects or vetoes +/// these values. +@Model +public final class PersistentTransactionAccounting { + #Unique([\.walletId, \.txid]) + #Index([\.walletId], [\.txid]) + + /// The wallet this accounting belongs to (32 bytes). + public var walletId: Data + /// Transaction id, raw wire bytes like `PersistentTransaction.txid`. + public var txid: Data + /// Net Core amount in duffs for this wallet (positive = received). + public var netAmount: Int64 + /// `CoreDirectionCode` wire value for this wallet. + public var direction: UInt32 + /// Fee in duffs; `nil` unless this wallet funded every input. + public var fee: UInt64? + /// Inverse of `PersistentTransaction.walletAccounting`, which cascades. + public var transaction: PersistentTransaction? + public var lastUpdated: Date + + public init( + walletId: Data, + txid: Data, + netAmount: Int64, + direction: UInt32, + fee: UInt64?, + transaction: PersistentTransaction? + ) { + self.walletId = walletId + self.txid = txid + self.netAmount = netAmount + self.direction = direction + self.fee = fee + self.transaction = transaction + self.lastUpdated = Date() + } +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentWallet.swift b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentWallet.swift index 52365db3006..2d98dac84be 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentWallet.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentWallet.swift @@ -118,6 +118,11 @@ public final class PersistentWallet { /// column, or never verified) means the full check runs at the next /// unlock. public var seedBindingVerifiedMarker: String? + /// Set once Rust's load-time accounting backfill has evaluated this + /// wallet's stored transactions (`on_persist_core_history_accounted_fn`). + /// Opaque to Swift: it only stops the history from being offered again. + /// `false` for rows predating the column, so they are backfilled once. + public var transactionAccountingBackfilled: Bool = false /// Record timestamps. public var createdAt: Date public var lastUpdated: Date diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/CoreDirectionCode.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/CoreDirectionCode.swift new file mode 100644 index 00000000000..27f3c9abf4f --- /dev/null +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/CoreDirectionCode.swift @@ -0,0 +1,11 @@ +import DashSDKFFI + +/// Swift names for the `direction` wire values of `PersistentTransaction` +/// and `PersistentTransactionAccounting`, bound to the constants the Rust +/// FFI exports (`PLATFORM_WALLET_TRANSACTION_DIRECTION_*`), never re-declared. +public enum CoreDirectionCode { + public static let incoming = UInt32(PLATFORM_WALLET_TRANSACTION_DIRECTION_INCOMING) + public static let outgoing = UInt32(PLATFORM_WALLET_TRANSACTION_DIRECTION_OUTGOING) + public static let internalTransfer = UInt32(PLATFORM_WALLET_TRANSACTION_DIRECTION_INTERNAL) + public static let coinJoin = UInt32(PLATFORM_WALLET_TRANSACTION_DIRECTION_COIN_JOIN) +} diff --git a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift index 7bc7e76188e..36d05ef1781 100644 --- a/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift +++ b/packages/swift-sdk/Sources/SwiftDashSDK/PlatformWallet/PlatformWalletPersistenceHandler.swift @@ -2709,6 +2709,8 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { record.providerVotingKeyHash = tx.has_provider_voting_key_hash ? withUnsafeBytes(of: tx.provider_voting_key_hash) { Data($0) } : nil + // The shared last-writer scalars, kept for unscoped display. The + // per-wallet values arrive through `persistTransactionAccounting`. record.netAmount = tx.net_amount record.fee = tx.has_fee ? tx.fee : nil if let labelPtr = tx.label { @@ -3354,6 +3356,15 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { persistAssetLockShieldedRecipientCallback extensionCallbacks.on_load_asset_lock_shielded_recipient_fn = loadAssetLockShieldedRecipientCallback + // Per-wallet transaction accounting exactly as Rust computes it: + // after each round's changeset, and once per wallet at load for + // stored rows that have none yet (Rust reads them back, computes, + // and writes the rows plus the completion marker in one round). + extensionCallbacks.on_persist_transaction_accounting_fn = persistTransactionAccountingCallback + extensionCallbacks.on_load_core_history_for_accounting_fn = loadCoreHistoryForAccountingCallback + extensionCallbacks.on_load_core_history_for_accounting_free_fn = + loadCoreHistoryForAccountingFreeCallback + extensionCallbacks.on_persist_core_history_accounted_fn = persistCoreHistoryAccountedCallback return extensionCallbacks } @@ -6713,6 +6724,13 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { } if let walletRow = walletRow { + // The departing wallet's accounting rows go with it; a + // shared transaction keeps the other wallets' rows. + let accountingRows = FetchDescriptor( + predicate: #Predicate { $0.walletId == walletId }) + for row in try backgroundContext.fetch(accountingRows) { + backgroundContext.delete(row) + } // Wallet → identities is `.nullify`; this delete // path cascades them explicitly. let identitiesToDelete = Array(walletRow.identities) @@ -9119,11 +9137,187 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { } } + /// Upsert one wallet's Rust-emitted accounting rows, keyed by + /// `(walletId, txid)`. Always fired inside a round (a sync round, or the + /// load-time backfill's own), so they stage with it and commit at + /// `endChangeset`. Returns `false` to fail the round, like the other + /// per-kind callbacks, including when a transaction row cannot be read: + /// read as a miss, the backfill would record completion over a row it + /// never wrote. + func persistTransactionAccounting( + walletId: Data, + rows: UnsafePointer?, + count: UInt + ) -> Bool { + onQueue { + switch roundWalletLookup(walletId: walletId, callback: "transaction_accounting") { + case .failed: return false + case .absent: return true + case .found: break + } + guard count > 0, let rows else { return true } + do { + for i in 0.. Bool { + onQueue { + switch roundWalletLookup(walletId: walletId, callback: "core_history_accounted") { + case .failed: return false + case .absent: return true + case .found(let wallet): + wallet.transactionAccountingBackfilled = true + return true + } + } + } + + private func upsertTransactionAccounting(walletId: Data, row: TransactionAccountingFFI) throws { + // Rust only accounts for transactions it also persisted; a row this + // store lacks has nothing to attach to. A failed read throws. + guard let transaction = try fetchTransactionRow(txid: hashData(row.txid), prefetching: []) + else { return } + let fee: UInt64? = row.has_fee ? row.fee : nil + if let existing = transaction.accounting(for: walletId) { + existing.netAmount = row.net_amount + existing.direction = row.direction + existing.fee = fee + existing.lastUpdated = Date() + } else { + let accounting = PersistentTransactionAccounting( + walletId: walletId, + txid: transaction.txid, + netAmount: row.net_amount, + direction: row.direction, + fee: fee, + transaction: nil + ) + backgroundContext.insert(accounting) + transaction.walletAccounting.append(accounting) + } + } + + /// Rust's accounting-backfill read. `backfilled` once the wallet's + /// marker is set (or the wallet row is gone): one indexed fetch, no + /// history scan. Otherwise the wallet's stored transactions lacking its + /// accounting row, gathered from the wallet's own relationships and + /// `walletId`-indexed rows rather than the whole store. Rust fetches any + /// funding transaction it needs itself, so a transaction missed here + /// only stays without a row. + func loadCoreHistoryForAccounting( + walletId: Data + ) -> (backfilled: Bool, rows: UnsafePointer?, count: Int, errored: Bool) { + onQueue { + let unaccounted: [PersistentTransaction] + do { + guard let wallet = try fetchWalletRecord(walletId: walletId), + !wallet.transactionAccountingBackfilled else { + return (true, nil, 0, false) + } + unaccounted = try walletTransactions(wallet: wallet) + .filter { $0.accounting(for: walletId) == nil } + } catch { + SDKLogger.event( + "persistence_core_history_load_failed", category: .persistence, severity: .error, + fields: ["wallet_reference": .reference(walletId)], error: error + ) + return (false, nil, 0, true) + } + guard !unaccounted.isEmpty else { return (false, nil, 0, false) } + let allocation = CoreHistoryLoadAllocation() + let buf = UnsafeMutablePointer.allocate(capacity: unaccounted.count) + allocation.entries = buf + for (index, transaction) in unaccounted.enumerated() { + let body = transaction.transactionData + let bytes = UnsafeMutablePointer.allocate(capacity: max(body.count, 1)) + body.copyBytes(to: bytes, count: body.count) + allocation.buffers.append(bytes) + buf[index] = CoreHistoryTransactionFFI( + tx_bytes: UnsafePointer(bytes), + tx_bytes_len: UInt(body.count) + ) + } + coreHistoryLoadAllocations[UnsafeRawPointer(buf)] = allocation + return (false, UnsafePointer(buf), unaccounted.count, false) + } + } + + /// Every stored transaction touching `wallet`, deduplicated by txid: + /// its accounts' `involvedTransactions`, plus the creating and spending + /// transactions of its TXOs and pending inputs. TXOs migrated with an + /// empty `walletId` are matched through their account, like + /// `walletOwnsTransaction`. + private func walletTransactions(wallet: PersistentWallet) throws -> [PersistentTransaction] { + let walletId = wallet.walletId + var byTxid: [Data: PersistentTransaction] = [:] + func add(_ transaction: PersistentTransaction?) { + guard let transaction else { return } + byTxid[transaction.txid] = transaction + } + for account in wallet.accounts { + account.involvedTransactions.forEach(add) + } + let emptyWalletId = Data() + let ownTxos: [PersistentTxo] = try modelFetcher.fetch( + FetchDescriptor(predicate: #Predicate { $0.walletId == walletId }), + in: backgroundContext + ) + let legacyTxos: [PersistentTxo] = try modelFetcher.fetch( + FetchDescriptor(predicate: #Predicate { $0.walletId == emptyWalletId }), + in: backgroundContext + ).filter { Self.resolvedWalletId(of: $0) == walletId } + for txo in ownTxos + legacyTxos { + add(txo.transaction) + add(txo.spendingTransaction) + } + let pendingInputs = try modelFetcher.fetch( + FetchDescriptor(predicate: #Predicate { $0.walletId == walletId }), + in: backgroundContext + ) + pendingInputs.forEach { add($0.spendingTransaction) } + return Array(byTxid.values) + } + + /// Release a loan handed out by `loadCoreHistoryForAccounting`. + func loadCoreHistoryForAccountingFree(rows: UnsafeRawPointer?) { + onQueue { + guard let rows, + let allocation = coreHistoryLoadAllocations.removeValue(forKey: rows) else { return } + allocation.release() + } + } + /// Outstanding tracked-masternode load allocations keyed by the /// entries pointer we handed to Rust. private var trackedMasternodeLoadAllocations: [UnsafeRawPointer: TrackedMasternodeLoadAllocation] = [:] + /// Outstanding unaccounted-history loans handed to Rust for the + /// accounting backfill, keyed by the rows pointer. + private var coreHistoryLoadAllocations: [UnsafeRawPointer: CoreHistoryLoadAllocation] = [:] + + /// Loans Rust has not returned yet; zero between backfills. + var outstandingCoreHistoryLoanCount: Int { + onQueue { coreHistoryLoadAllocations.count } + } + /// Human-readable name for a persisted account, mirroring the /// top-level `AccountTypeTagFFI` discriminant plus — for tag 0 /// (Standard) — the `StandardAccountTypeTagFFI` sub-discriminant. @@ -9199,7 +9393,9 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { /// hash that would round-trip back to Rust as a real block id), /// or when the row has no `transactionData` (the FFI write path /// always populates it, so a missing one signals a corrupt row - /// the Rust side can't decode anyway). + /// the Rust side can't decode anyway). Throws when the fetch + /// itself fails: that is not a miss, and Rust's accounting + /// backfill must not read it as one. /// /// The wallet-id is currently unused (`txid` is globally /// unique), but is accepted to match the Rust trait signature @@ -9207,13 +9403,13 @@ public final class PlatformWalletPersistenceHandler: @unchecked Sendable { func coreTxRecord( walletId: Data, txid: Data - ) -> (context: UInt32, blockHeight: UInt32, blockHash: Data, blockTimestamp: UInt32, transactionData: Data)? { + ) throws -> (context: UInt32, blockHeight: UInt32, blockHash: Data, blockTimestamp: UInt32, transactionData: Data)? { _ = walletId - return onQueue { + return try onQueue { let descriptor = FetchDescriptor( predicate: #Predicate { $0.txid == txid } ) - guard let row = try? backgroundContext.fetch(descriptor).first else { + guard let row = try backgroundContext.fetch(descriptor).first else { return nil } // The Rust side decodes `transactionData` into a @@ -9379,6 +9575,23 @@ private final class TrackedMasternodeLoadAllocation { } } +/// Owned allocations behind one `loadCoreHistoryForAccounting` answer: the +/// entries buffer plus each copied transaction body. Released by +/// `loadCoreHistoryForAccountingFree`. +private final class CoreHistoryLoadAllocation { + var entries: UnsafeMutablePointer? + var buffers: [UnsafeMutablePointer] = [] + + func release() { + for buffer in buffers { + buffer.deallocate() + } + buffers.removeAll() + entries?.deallocate() + entries = nil + } +} + private final class LoadAllocation { var entries: UnsafeMutablePointer? /// Allocated capacity — equal to `restorable.count`. Used for @@ -11275,9 +11488,11 @@ private func loadShieldedViewingKeysFreeCallback( /// the three block fields when context is 2 or 3, plus the tx /// bytes pointer + length) on a hit; returns `0`. /// - Sets `*outFound = false` on a miss; returns `0`. -/// - Returns `0` even on Swift-side errors (treated as miss); the -/// Rust side's `record_or_persister` helper logs and falls -/// through to the caller's existing not-found / poll path. +/// - Returns `1` when the SwiftData fetch fails. Rust's +/// `get_core_tx_record` still reads that as a miss (the asset-lock +/// and DashPay callers retry); the accounting backfill's strict +/// read fails its round instead of reading a wallet input as +/// foreign. private func getCoreTxRecordCallback( context: UnsafeMutableRawPointer?, walletIdPtr: UnsafePointer?, @@ -11306,7 +11521,17 @@ private func getCoreTxRecordCallback( let walletId = Data(bytes: walletIdPtr, count: 32) let txid = Data(bytes: txidPtr, count: 32) - guard let row = handler.coreTxRecord(walletId: walletId, txid: txid) else { + let found: (context: UInt32, blockHeight: UInt32, blockHash: Data, blockTimestamp: UInt32, transactionData: Data)? + do { + found = try handler.coreTxRecord(walletId: walletId, txid: txid) + } catch { + SDKLogger.event( + "persistence_core_tx_record_read_failed", category: .persistence, severity: .error, + fields: ["wallet_reference": .reference(walletId)], error: error + ) + return 1 + } + guard let row = found else { // Miss — outFound already set to false above. return 0 } @@ -11925,6 +12150,64 @@ extension PlatformWalletPersistenceHandler { } } +/// C shim for the extension's `on_persist_transaction_accounting_fn`. +private func persistTransactionAccountingCallback( + context: UnsafeMutableRawPointer?, + walletIdPtr: UnsafePointer?, + rows: UnsafePointer?, + count: UInt +) -> Int32 { + guard let context, let walletIdPtr else { return 0 } + let handler = Unmanaged.fromOpaque(context).takeUnretainedValue() + return handler.persistTransactionAccounting( + walletId: Data(bytes: walletIdPtr, count: 32), rows: rows, count: count + ) ? 0 : 1 +} + +/// C shim for `on_load_core_history_for_accounting_fn`. Hands Rust a loaned +/// array released by the free shim below. +private func loadCoreHistoryForAccountingCallback( + context: UnsafeMutableRawPointer?, + walletIdPtr: UnsafePointer?, + outBackfilled: UnsafeMutablePointer?, + outRows: UnsafeMutablePointer?>?, + outCount: UnsafeMutablePointer? +) -> Int32 { + guard let context, let walletIdPtr, let outBackfilled, let outRows, let outCount else { return 1 } + outBackfilled.pointee = false + outRows.pointee = nil + outCount.pointee = 0 + let handler = Unmanaged.fromOpaque(context).takeUnretainedValue() + let (backfilled, rows, count, errored) = handler.loadCoreHistoryForAccounting( + walletId: Data(bytes: walletIdPtr, count: 32) + ) + outBackfilled.pointee = backfilled + outRows.pointee = rows + outCount.pointee = UInt(count) + return errored ? 1 : 0 +} + +/// C shim for `on_persist_core_history_accounted_fn`. +private func persistCoreHistoryAccountedCallback( + context: UnsafeMutableRawPointer?, + walletIdPtr: UnsafePointer? +) -> Int32 { + guard let context, let walletIdPtr else { return 1 } + let handler = Unmanaged.fromOpaque(context).takeUnretainedValue() + return handler.persistCoreHistoryAccounted(walletId: Data(bytes: walletIdPtr, count: 32)) ? 0 : 1 +} + +/// C shim for `on_load_core_history_for_accounting_free_fn`. +private func loadCoreHistoryForAccountingFreeCallback( + context: UnsafeMutableRawPointer?, + rows: UnsafePointer?, + _ count: UInt +) { + guard let context else { return } + let handler = Unmanaged.fromOpaque(context).takeUnretainedValue() + handler.loadCoreHistoryForAccountingFree(rows: rows.map(UnsafeRawPointer.init)) +} + private func persistAssetLockOperationIdCallback( context: UnsafeMutableRawPointer?, walletId: UnsafePointer?, outPoint: UnsafePointer?, operationId: UnsafePointer? diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/AccountDetailView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/AccountDetailView.swift index e056190e51b..2b5bbc1fe7c 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/AccountDetailView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/AccountDetailView.swift @@ -164,7 +164,7 @@ struct AccountDetailView: View { .navigationTitle(account.accountTypeName) .navigationBarTitleDisplayMode(.large) .sheet(item: $selectedTransaction) { transaction in - TransactionDetailView(transaction: transaction) + TransactionDetailView(transaction: transaction, walletId: wallet.walletId) } .sheet(isPresented: $showingPINPrompt) { PINPromptView( @@ -465,7 +465,7 @@ struct AccountDetailView: View { Button { selectedTransaction = tx } label: { - TransactionRowView(transaction: tx) + TransactionRowView(transaction: tx, walletId: wallet.walletId) } .buttonStyle(.plain) if idx < txs.count - 1 { diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift index c222a532b70..0875e244d16 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDetailView.swift @@ -3,36 +3,29 @@ import SwiftDashSDK struct TransactionDetailView: View { let transaction: PersistentTransaction - /// Override amount for asset-lock txs. The wallet's `netAmount` - /// shows ~0 for these (credit output is structurally self-owned), - /// so the list view passes the linked - /// `PersistentAssetLock.amountDuffs`. `nil` for non-asset-lock - /// rows OR consumed asset locks whose tracking row was cleaned - /// up after successful identity registration. + var walletId: Data? = nil + /// This wallet's Rust-emitted amount and direction; `nil` when Rust + /// emitted no accounting for it (see `PersistentTransaction.netAmount(for:)`). + private var netAmount: Int64? { transaction.displayNetAmount(for: walletId) } + private var direction: UInt32? { transaction.displayDirectionCode(for: walletId) } + /// Asset-lock payload funding amount, excluding the Core transaction fee. var assetLockAmountDuffs: Int64? = nil @Environment(\.dismiss) private var dismiss @State private var showCopiedAlert = false - /// Amount label rendered prominently at the top of the sheet. - /// Same precedence rule as the row: asset-lock duffs when we - /// have them, else an explicit "amount unknown" label for the - /// historical-asset-lock case (rather than the misleading - /// `+0.00000000 DASH` from `transaction.formattedAmount`). - /// `nil` for a payload-only provider special tx — a ProRegTx - /// observed via the owner/voting keys moves no wallet balance, - /// and `+0.00000000 DASH` reads as a broken zero-value receive. + /// Show the lock's funding amount, or the wallet's Core value movement for ordinary transactions. private var displayAmount: String? { if transaction.isAssetLock { if let duffs = assetLockAmountDuffs { let dash = Double(duffs) / 100_000_000.0 return String(format: "-%.8f DASH", dash) } - return "Asset Lock (amount unknown)" + return "Asset Lock (amount unavailable)" } - if transaction.isProviderSpecial && transaction.netAmount == 0 { + if transaction.isProviderSpecial && netAmount == 0 { return nil } - return transaction.formattedAmount + return transaction.displayFormattedAmount(for: walletId) } private var typeDescription: String { @@ -42,11 +35,14 @@ struct TransactionDetailView: View { || transaction.isProviderSpecial { return transaction.displayDirection } - switch transaction.netAmount { - case let amount where amount > 0: + guard let direction else { return "Transaction" } + switch direction { + case CoreDirectionCode.incoming: return "Received" - case let amount where amount < 0: + case CoreDirectionCode.outgoing: return "Sent" + case CoreDirectionCode.coinJoin: + return "CoinJoin" default: return "Self-Transfer" } @@ -56,14 +52,7 @@ struct TransactionDetailView: View { if transaction.isAssetLock { return "lock.fill" } if transaction.isAssetUnlock { return "lock.open.fill" } if transaction.isProviderSpecial { return "server.rack" } - switch transaction.netAmount { - case let amount where amount > 0: - return "arrow.down.circle.fill" - case let amount where amount < 0: - return "arrow.up.circle.fill" - default: - return "arrow.triangle.2.circlepath" - } + return TransactionDirectionStyle.icon(for: direction) } private var typeColor: Color { @@ -73,14 +62,7 @@ struct TransactionDetailView: View { if transaction.isProviderSpecial { return .orange } - switch transaction.netAmount { - case let amount where amount > 0: - return .green - case let amount where amount < 0: - return .red - default: - return .blue - } + return TransactionDirectionStyle.color(for: direction) } private var isConfirmed: Bool { @@ -97,7 +79,7 @@ struct TransactionDetailView: View { } private var formattedFee: String? { - guard let fee = transaction.fee else { return nil } + guard let fee = transaction.displayFee(for: walletId) else { return nil } let dash = Double(fee) / 100_000_000.0 return String(format: "%.8f DASH", dash) } @@ -208,7 +190,7 @@ struct TransactionDetailView: View { ) } - if let fee = formattedFee, transaction.netAmount < 0 { + if let fee = formattedFee, let amount = netAmount, amount < 0 { TransactionDetailRow( label: "Network Fee", value: fee diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift new file mode 100644 index 00000000000..256b7a8ae19 --- /dev/null +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionDirectionStyle.swift @@ -0,0 +1,28 @@ +import SwiftUI +import SwiftDashSDK + +/// Direction icon and colour shared by the transaction list and detail views. +enum TransactionDirectionStyle { + /// `nil` (no accounting from Rust for this wallet) reads as unknown. + static func icon(for direction: UInt32?) -> String { + guard let direction else { return "questionmark.circle" } + switch direction { + case CoreDirectionCode.incoming: return "arrow.down.circle.fill" + case CoreDirectionCode.outgoing: return "arrow.up.circle.fill" + case CoreDirectionCode.internalTransfer: return "arrow.triangle.2.circlepath" + case CoreDirectionCode.coinJoin: return "shuffle.circle.fill" + default: return "questionmark.circle" + } + } + + /// Internal transfers share the outgoing colour: they still pay a fee. + static func color(for direction: UInt32?) -> Color { + guard let direction else { return .secondary } + switch direction { + case CoreDirectionCode.incoming: return .green + case CoreDirectionCode.outgoing, CoreDirectionCode.internalTransfer: return .red + case CoreDirectionCode.coinJoin: return .blue + default: return .secondary + } + } +} diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift index f185f78c20f..3d6eaffa633 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/TransactionListView.swift @@ -29,10 +29,9 @@ struct TransactionListView: View { @Query private var walletAccounts: [PersistentAccount] @Query private var transactionObservation: [PersistentTransaction] /// Per-wallet asset-lock rows. Used to look up the *locked* amount - /// for each asset-lock tx — `PersistentTransaction.netAmount` is - /// the wallet's input-vs-output diff, which sees the credit - /// output as "to-self" and reports ~0 for asset locks. The - /// `amountDuffs` on the asset-lock row is the actual L1 burn. + /// for each asset-lock tx: `amountDuffs` on the asset-lock row is the + /// payload funding amount, while `PersistentTransaction.netAmount` is + /// the Core debit, which includes the fee. @Query private var assetLocks: [PersistentAssetLock] /// This wallet's owning identities. The DashPay payment / contact /// join below must be scoped to these — two identities in one store @@ -169,6 +168,7 @@ struct TransactionListView: View { .sheet(item: $selectedTransaction) { transaction in TransactionDetailView( transaction: transaction, + walletId: walletId, assetLockAmountDuffs: assetLockAmountByTxid[transaction.txidHex] ) } @@ -202,6 +202,7 @@ struct TransactionListView: View { } label: { TransactionRowView( transaction: transaction, + walletId: walletId, assetLockAmountDuffs: assetLockAmounts[transaction.txidHex], dashpayPayment: payment, dashpayCounterpartyName: payment.map { @@ -219,12 +220,12 @@ struct TransactionListView: View { struct TransactionRowView: View { let transaction: PersistentTransaction - /// Override amount displayed for asset-lock rows. The wallet's - /// `netAmount` shows ~0 for these (credit output is structurally - /// self-owned), so the list view passes the linked - /// `PersistentAssetLock.amountDuffs` — the actual L1 DASH burned - /// to mint platform credits. `nil` for non-asset-lock rows or - /// when no matching row was found. + var walletId: Data? = nil + /// This wallet's Rust-emitted amount and direction; `nil` when Rust + /// emitted no accounting for it (see `PersistentTransaction.netAmount(for:)`). + private var netAmount: Int64? { transaction.displayNetAmount(for: walletId) } + private var direction: UInt32? { transaction.displayDirectionCode(for: walletId) } + /// Asset-lock payload funding amount, excluding the Core transaction fee. var assetLockAmountDuffs: Int64? = nil /// The DashPay payment this tx belongs to, if any — joined by `txid` in /// `TransactionListView`. When set, the row shows the contact context @@ -253,14 +254,7 @@ struct TransactionRowView: View { // `Internal` — the wallet just sees its own owner/voting/payout // keys in the payload — so the self-transfer arrows would lie. if transaction.isProviderSpecial { return "server.rack" } - // direction: 0=incoming, 1=outgoing, 2=internal, 3=coinJoin - switch transaction.direction { - case 0: return "arrow.down.circle.fill" - case 1: return "arrow.up.circle.fill" - case 2: return "arrow.triangle.2.circlepath" - case 3: return "shuffle.circle.fill" - default: return "questionmark.circle" - } + return TransactionDirectionStyle.icon(for: direction) } private var typeColor: Color { @@ -278,12 +272,7 @@ struct TransactionRowView: View { if transaction.isProviderSpecial { return .orange } - switch transaction.direction { - case 0: return .green - case 1, 2: return .red - case 3: return .blue - default: return .secondary - } + return TransactionDirectionStyle.color(for: direction) } /// Primary label: the contact context for a DashPay payment, else the @@ -400,7 +389,7 @@ struct TransactionRowView: View { .font(.headline) .foregroundColor(typeColor) - if let fee = transaction.fee, transaction.netAmount < 0 { + if let fee = transaction.displayFee(for: walletId), let amount = netAmount, amount < 0 { Text("Fee: \(formatFee(fee))") .font(.caption2) .foregroundColor(.secondary) @@ -417,35 +406,23 @@ struct TransactionRowView: View { return String(format: "%.8f DASH", dash) } - /// Amount label for the row. For asset-lock txs we substitute - /// the linked `PersistentAssetLock.amountDuffs` (the L1 DASH - /// actually burned to mint platform credits); the wallet's - /// `netAmount` is ~0 for these because the credit output is a - /// self-owned address. Rendered as a negative (DASH leaving L1). - /// - /// If we know the row is an asset lock but the linked - /// `PersistentAssetLock` is missing (e.g. a historical record - /// from before the `Consumed`-status retention change shipped), - /// we render "Asset Lock (amount unknown)" instead of falling - /// through to `transaction.formattedAmount` — that would say - /// `+0.00000000 DASH`, which is misleading for a row the user - /// can see was a funding tx. + /// Keep asset-lock funding amounts distinct from the Core debit, which includes fees. private var displayAmount: String { if transaction.isAssetLock { if let duffs = assetLockAmountDuffs { let dash = Double(duffs) / 100_000_000.0 return String(format: "-%.8f DASH", dash) } - return "Asset Lock (amount unknown)" + return "Asset Lock (amount unavailable)" } // A payload-only provider special tx moves no wallet balance; // `+0.00000000 DASH` reads as a broken zero-value receive, so // put the tx kind in the amount slot instead. A provider tx // that DOES move value (e.g. this wallet funded the collateral) // falls through and shows the real signed amount. - if transaction.isProviderSpecial && transaction.netAmount == 0 { + if transaction.isProviderSpecial && netAmount == 0 { return transaction.providerSpecialName ?? transaction.transactionType } - return transaction.formattedAmount + return transaction.displayFormattedAmount(for: walletId) } } diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageExplorerView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageExplorerView.swift index a567917dedf..d2d429ac86e 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageExplorerView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageExplorerView.swift @@ -145,6 +145,11 @@ struct StorageExplorerView: View { modelRow("Pending Inputs", icon: "hourglass", type: PersistentPendingInput.self) { PendingInputStorageListView(network: network) } + modelRow( + "Transaction Accounting", icon: "plusminus.circle", type: PersistentTransactionAccounting.self + ) { + TransactionAccountingStorageListView(network: network) + } modelRow("Asset Locks", icon: "lock.shield", type: PersistentAssetLock.self) { AssetLockStorageListView(network: network) } @@ -328,6 +333,9 @@ struct StorageExplorerView: View { filteredCount(PersistentPendingInput.self) { walletsOnNetwork.contains($0.walletId) } + filteredCount(PersistentTransactionAccounting.self) { + walletsOnNetwork.contains($0.walletId) + } filteredCount(PersistentShieldedNote.self) { walletsOnNetwork.contains($0.walletId) } diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift index 23bc60dd8c3..a3433e034fd 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageModelListViews.swift @@ -1162,10 +1162,10 @@ struct TransactionStorageListView: View { // Direction. switch directionFilter { case .all: break - case .incoming where record.direction != 0: return false - case .outgoing where record.direction != 1: return false - case .internalTx where record.direction != 2: return false - case .coinjoin where record.direction != 3: return false + case .incoming where record.direction != CoreDirectionCode.incoming: return false + case .outgoing where record.direction != CoreDirectionCode.outgoing: return false + case .internalTx where record.direction != CoreDirectionCode.internalTransfer: return false + case .coinjoin where record.direction != CoreDirectionCode.coinJoin: return false default: break } // Type. Treat the legacy `"Standard"` placeholder (the @@ -1757,6 +1757,53 @@ struct TxoStorageListView: View { } } +// MARK: - PersistentTransactionAccounting + +/// Every per-wallet accounting row `rs-platform-wallet` emitted, newest +/// first: one per `(walletId, txid)`, so a transaction shared by two local +/// wallets appears twice. +struct TransactionAccountingStorageListView: View { + let network: Network + @Query(sort: [SortDescriptor(\PersistentTransactionAccounting.lastUpdated, order: .reverse)]) + private var records: [PersistentTransactionAccounting] + + @Query private var allWallets: [PersistentWallet] + + private var scopedRecords: [PersistentTransactionAccounting] { + let ids = Set(allWallets.lazy + .filter { $0.networkRaw == network.rawValue } + .map(\.walletId)) + return records.filter { ids.contains($0.walletId) } + } + + var body: some View { + let visible = scopedRecords + List(visible) { record in + NavigationLink(destination: TransactionAccountingStorageDetailView(record: record)) { + VStack(alignment: .leading, spacing: 4) { + Text(record.txid.reversed().map { String(format: "%02x", $0) }.joined()) + .font(.system(.caption, design: .monospaced)) + .lineLimit(1).truncationMode(.middle) + HStack(spacing: 8) { + Text(PersistentTransaction.format(duffs: record.netAmount)) + .font(.caption2) + Spacer() + Text(record.walletId.prefix(4).map { String(format: "%02x", $0) }.joined()) + .font(.system(.caption2, design: .monospaced)) + .foregroundColor(.secondary) + } + } + } + } + .navigationTitle("Transaction Accounting (\(visible.count))") + .overlay { + if visible.isEmpty { + ContentUnavailableView("No Accounting Rows", systemImage: "plusminus.circle") + } + } + } +} + // MARK: - PersistentPendingInput /// Diagnostic list of every `PersistentPendingInput` row — one per diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageRecordDetailViews.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageRecordDetailViews.swift index 7fe6ed1cd28..d6b06c037a4 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageRecordDetailViews.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/StorageRecordDetailViews.swift @@ -1590,6 +1590,10 @@ struct WalletStorageDetailView: View { FieldRow(label: "Birth Height", value: "\(record.birthHeight)") FieldRow(label: "Synced Height", value: "\(record.syncedHeight)") FieldRow(label: "Imported", value: record.isImported ? "Yes" : "No") + FieldRow( + label: "Accounting Backfilled", + value: record.transactionAccountingBackfilled ? "Yes" : "No" + ) } Section("Relationships") { FieldRow(label: "Accounts", value: "\(record.accounts.count)") @@ -2180,6 +2184,40 @@ struct TxoStorageDetailView: View { } } +// MARK: - PersistentTransactionAccounting + +struct TransactionAccountingStorageDetailView: View { + let record: PersistentTransactionAccounting + + var body: some View { + Form { + Section("Accounting") { + FieldRow(label: "Net Amount", value: PersistentTransaction.format(duffs: record.netAmount)) + FieldRow(label: "Direction", value: "\(record.direction)") + FieldRow(label: "Fee", value: record.fee.map { "\($0) duffs" } ?? "—") + } + Section("Keys") { + FieldRow(label: "Wallet ID", value: hexString(record.walletId)) + FieldRow(label: "TXID", value: record.txid.reversed().map { String(format: "%02x", $0) }.joined()) + } + Section("Relationships") { + if let transaction = record.transaction { + NavigationLink(destination: TransactionStorageDetailView(record: transaction)) { + FieldRow(label: "Transaction", value: transaction.txidHex) + } + } else { + FieldRow(label: "Transaction", value: "— (unlinked)") + } + } + Section("Timestamps") { + FieldRow(label: "Last Updated", value: dateString(record.lastUpdated)) + } + } + .navigationTitle("Transaction Accounting") + .navigationBarTitleDisplayMode(.inline) + } +} + // MARK: - PersistentPendingInput struct PendingInputStorageDetailView: View { diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockOperationPersistenceTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockOperationPersistenceTests.swift index 6c6888c53f4..bcf1305bbd3 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockOperationPersistenceTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AssetLockOperationPersistenceTests.swift @@ -122,7 +122,8 @@ final class AssetLockOperationPersistenceTests: XCTestCase { XCTAssertNil(rows.first?.operationId) XCTAssertNil(rows.first?.shieldedRecipientRaw43) XCTAssertEqual(rows.first?.amountDuffs, 1000) - XCTAssertEqual(DashModelContainer.schema.version, Schema.Version(5, 0, 0)) + let currentSchema = try XCTUnwrap(DashMigrationPlan.schemas.last) + XCTAssertEqual(DashModelContainer.schema.version, currentSchema.versionIdentifier) } func testMalformedOperationIdFailsLoadInsteadOfBecomingAbsent() throws { diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift index b07ad5a84f2..8a494a0a09c 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashLegacySchemaMigrationTests.swift @@ -891,8 +891,9 @@ final class DashLegacySchemaMigrationTests: XCTestCase { let container = try ModelContainer(for: schema, configurations: [ ModelConfiguration(schema: schema, url: url, cloudKitDatabase: .none) ]) - XCTAssertEqual(try container.mainContext.fetchCount( - FetchDescriptor()), 1) + let wallet = try XCTUnwrap(container.mainContext.fetch( + FetchDescriptor()).first) + XCTAssertEqual(wallet.name, "historical audit wallet") } XCTAssertEqual(try DashLegacySchemaBridge.identity(at: url).versions, ["2.0.0"]) let container = try open(url, hooks: .init(visit: { _, _ in diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift index 94ee9120cf3..0b5d6272d1e 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift @@ -75,10 +75,11 @@ final class DashModelMigrationTests: XCTestCase { SDKLogger.flush() let log = try String(contentsOf: session.appendingPathComponent("swift/run.log"), encoding: .utf8) XCTAssertTrue(log.contains("event=store_open_started")) - XCTAssertTrue(log.contains("route=\"historical-v2-to-v3\"")) + XCTAssertTrue(log.contains("route=\"historical-v2-to-v5\"")) XCTAssertTrue(log.contains("source_version=\"2.0.0\"")) XCTAssertTrue(log.contains("source_checksum=\"\(sourceChecksum)\"")) XCTAssertTrue(log.contains("target_version=\"5.0.0\"")) + XCTAssertFalse(log.contains("target_version=\"4.0.0\""), "every diagnostic names the live schema") XCTAssertTrue(log.contains("route=\"labelled-current-v5\"")) XCTAssertEqual(log.components(separatedBy: "event=store_open_succeeded").count - 1, 2) XCTAssertFalse(log.contains("event=store_open_failed")) @@ -94,6 +95,7 @@ final class DashModelMigrationTests: XCTestCase { SDKLogger.flush() let updatedLog = try String(contentsOf: session.appendingPathComponent("swift/run.log"), encoding: .utf8) XCTAssertTrue(updatedLog.contains("route=\"new-store\"")) + XCTAssertFalse(updatedLog.contains("target_version=\"4.0.0\"")) let failure = try XCTUnwrap(updatedLog.split(separator: "\n").first { $0.contains("event=store_open_failed") }) XCTAssertTrue(failure.contains("target_version=\"5.0.0\"")) XCTAssertTrue(failure.contains("error_code=")) @@ -454,6 +456,13 @@ final class DashModelMigrationTests: XCTestCase { ["PersistentTrackedMasternode", "PersistentIdentityBalanceMetadata"]) } + func testV5AddsPerWalletTransactionAccountingToV4() { + XCTAssertEqual( + Set(Schema(versionedSchema: DashSchemaV5.self).entities.map(\.name)) + .subtracting(Schema(versionedSchema: DashSchemaV4.self).entities.map(\.name)), + ["PersistentTransactionAccounting"]) + } + @MainActor func testV1BalanceGetsNoWatermarkUntilOneIsPersistedInLiveV3() throws { let (directory, url) = try copyFixture(Self.fixtures[0]) @@ -513,7 +522,7 @@ final class DashModelMigrationTests: XCTestCase { try v1Container?.mainContext.save() v1Container = nil - let v2Schema = Schema(versionedSchema: DashSchemaV5.self) + let v2Schema = DashModelContainer.schema let v2Configuration = ModelConfiguration( "DashKeyLimitsMigrationTest", schema: v2Schema, @@ -590,7 +599,7 @@ final class DashModelMigrationTests: XCTestCase { try v1Container?.mainContext.save() v1Container = nil - let v2Schema = Schema(versionedSchema: DashSchemaV5.self) + let v2Schema = DashModelContainer.schema let v2Configuration = ModelConfiguration( "DashContractBoundsKindMigrationTest", schema: v2Schema, diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift index d283b5a20c0..aa42eab571b 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashReleasedSchemaTests.swift @@ -99,6 +99,64 @@ final class DashReleasedSchemaTests: XCTestCase { } } + private enum BaseV4Plan: SchemaMigrationPlan { + static var schemas: [any VersionedSchema.Type] { [DashSchemaV3.self, DashSchemaV4.self] } + static var stages: [MigrationStage] { + [.lightweight(fromVersion: DashSchemaV3.self, toVersion: DashSchemaV4.self)] + } + } + + /// V4 → V5 adds the per-wallet accounting entity: migrated rows start + /// without one and keep their stored amount, and a row written after the + /// migration survives a reopen and cascades with its transaction. + @MainActor + // TODO(macOS-validation): Run the V4-to-V5 SwiftData migration test on macOS. + func testShouldMigrateV4ToPerWalletAccountingRows() throws { + let fixture = try XCTUnwrap(DashReleasedSchemaRegistry.fixtures.first { + $0.version.versionIdentifier == Schema.Version(3, 0, 0) + }) + let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: directory) } + let url = directory.appendingPathComponent("accounting.store") + try FileManager.default.copyItem(at: source(fixture), to: url) + try autoreleasepool { + let schema = Schema(versionedSchema: DashSchemaV4.self) + let container = try ModelContainer(for: schema, migrationPlan: BaseV4Plan.self, + configurations: [ModelConfiguration(schema: schema, url: url, cloudKitDatabase: .none)]) + try container.mainContext.save() + } + XCTAssertEqual(try DashLegacySchemaBridge.identity(at: url).versions, ["4.0.0"]) + let txid = Data(repeating: 0x32, count: 32) + let walletId = Data(repeating: 0x07, count: 32) + try autoreleasepool { + let container = try DashModelContainer.create(url: url) + let row = try XCTUnwrap(container.mainContext.fetch(FetchDescriptor()) + .first { $0.txid == txid }) + XCTAssertTrue(row.walletAccounting.isEmpty, "V4 rows carry no per-wallet accounting yet") + XCTAssertNil(row.netAmount(for: walletId), "Global V4 accounting is not wallet-scoped") + let accounting = PersistentTransactionAccounting( + walletId: walletId, txid: txid, netAmount: -1_234, + direction: CoreDirectionCode.outgoing, fee: 34, transaction: nil + ) + container.mainContext.insert(accounting) + row.walletAccounting.append(accounting) + try container.mainContext.save() + } + try autoreleasepool { + let container = try DashModelContainer.create(url: url) + let row = try XCTUnwrap(container.mainContext.fetch(FetchDescriptor()) + .first { $0.txid == txid }) + XCTAssertEqual(row.netAmount(for: walletId), -1_234) + XCTAssertEqual(row.direction(for: walletId), CoreDirectionCode.outgoing) + XCTAssertEqual(row.fee(for: walletId), 34) + XCTAssertEqual(try DashLegacySchemaBridge.identity(at: url).versions, ["5.0.0"]) + container.mainContext.delete(row) + try container.mainContext.save() + XCTAssertTrue(try container.mainContext.fetch(FetchDescriptor()).isEmpty) + } + } + @MainActor func testRuntimePlanHasNoDuplicateModelChecksums() throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PersistentTransactionDisplayTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PersistentTransactionDisplayTests.swift index 02b0bc78db8..f3f6d5deb40 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PersistentTransactionDisplayTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/PersistentTransactionDisplayTests.swift @@ -12,7 +12,10 @@ final class PersistentTransactionDisplayTests: XCTestCase { /// Direction 2 = internal — the raw classification every special /// tx gets (asset locks, provider txs), which the display helpers /// exist to override. - private func makeTransaction(kind: UInt8, direction: UInt32 = 2) -> PersistentTransaction { + private func makeTransaction( + kind: UInt8, + direction: UInt32 = CoreDirectionCode.internalTransfer + ) -> PersistentTransaction { let tx = PersistentTransaction( txid: Data(repeating: 0xAB, count: 32), transactionData: Data(), @@ -53,7 +56,7 @@ final class PersistentTransactionDisplayTests: XCTestCase { XCTAssertEqual(makeTransaction(kind: 7).displayDirection, "Asset Unlock") // …and non-special kinds fall through to the raw direction. XCTAssertEqual(makeTransaction(kind: 0).displayDirection, "Internal") - XCTAssertEqual(makeTransaction(kind: 0, direction: 0).displayDirection, "Incoming") + XCTAssertEqual(makeTransaction(kind: 0, direction: CoreDirectionCode.incoming).displayDirection, "Incoming") XCTAssertEqual(makeTransaction(kind: 0xFF).displayDirection, "Internal") } } diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift new file mode 100644 index 00000000000..c2955b0621b --- /dev/null +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/TransactionAccountingTests.swift @@ -0,0 +1,324 @@ +import XCTest +import SwiftData +import DashSDKFFI +@testable import SwiftDashSDK + +/// The Swift store persists the per-wallet accounting `rs-platform-wallet` +/// emits and displays it; it never derives it. The rule itself is tested in +/// Rust (`platform_wallet::changeset::wallet_accounting`). +@MainActor +final class TransactionAccountingTests: XCTestCase { + private let walletA = Data(repeating: 1, count: 32) + private let walletB = Data(repeating: 2, count: 32) + private let txid = Data(repeating: 3, count: 32) + + private func makeStore() throws -> (ModelContainer, PlatformWalletPersistenceHandler) { + let container = try DashModelContainer.createInMemory() + container.mainContext.insert(PersistentWallet(walletId: walletA, network: .testnet)) + container.mainContext.insert(PersistentWallet(walletId: walletB, network: .testnet)) + try container.mainContext.save() + return (container, PlatformWalletPersistenceHandler(modelContainer: container, network: .testnet)) + } + + private func row(_ txid: Data, net: Int64, direction: UInt32, fee: UInt64? = nil) -> TransactionAccountingFFI { + var row = TransactionAccountingFFI() + withUnsafeMutableBytes(of: &row.txid) { $0.copyBytes(from: txid) } + row.net_amount = net + row.direction = direction + row.fee = fee ?? 0 + row.has_fee = fee != nil + return row + } + + /// Hand the handler `rows` the way Rust's extension slot does: inside a + /// round, optionally followed by the backfill's completion marker. + @discardableResult + private func account( + _ handler: PlatformWalletPersistenceHandler, walletId: Data, + _ rows: [TransactionAccountingFFI], markBackfilled: Bool = false, success: Bool = true + ) -> Bool { + handler.beginChangeset(walletId: walletId) + var stored = rows.withUnsafeBufferPointer { + handler.persistTransactionAccounting(walletId: walletId, rows: $0.baseAddress, count: UInt($0.count)) + } + if markBackfilled { + stored = handler.persistCoreHistoryAccounted(walletId: walletId) && stored + } + _ = handler.endChangeset(walletId: walletId, success: success) + return stored + } + + /// The bodies `loadCoreHistoryForAccounting` offers, returning the loan. + private func offered( + _ handler: PlatformWalletPersistenceHandler, walletId: Data + ) throws -> (backfilled: Bool, bodies: [Data]) { + let history = handler.loadCoreHistoryForAccounting(walletId: walletId) + XCTAssertFalse(history.errored) + guard let rows = history.rows else { + XCTAssertEqual(history.count, 0) + return (history.backfilled, []) + } + let bodies = (0.. Data in + Data(bytes: rows[index].tx_bytes!, count: Int(rows[index].tx_bytes_len)) + } + handler.loadCoreHistoryForAccountingFree(rows: UnsafeRawPointer(rows)) + return (history.backfilled, bodies) + } + + private func transaction(_ container: ModelContainer, _ txid: Data) throws -> PersistentTransaction { + try XCTUnwrap(ModelContext(container).fetch(FetchDescriptor()).first { $0.txid == txid }) + } + + private func persist( + _ handler: PlatformWalletPersistenceHandler, walletId: Data, txid: Data, + bytes: Data? = nil, net: Int64 = 0, kind: UInt8 = 0, + inputTxids: [Data] = [], outputs: [(Data, UInt64)] = [] + ) { + let name = strdup("Standard { index: 0 }") + let address = strdup("") + defer { free(name); free(address) } + var record = TransactionRecordFFI() + withUnsafeMutableBytes(of: &record.txid) { $0.copyBytes(from: txid) } + record.context = 2 + record.net_amount = net + record.transaction_type_kind = kind + var inputs = inputTxids.map { txid -> OutPointFFI in + var result = OutPointFFI() + withUnsafeMutableBytes(of: &result.txid) { $0.copyBytes(from: txid) } + return result + } + var txos = outputs.map { txid, amount -> UtxoEntryFFI in + var result = UtxoEntryFFI() + withUnsafeMutableBytes(of: &result.outpoint.txid) { $0.copyBytes(from: txid) } + result.amount = amount + result.address = address + return result + } + var raw = Array(bytes ?? Data()) + handler.beginChangeset(walletId: walletId) + raw.withUnsafeMutableBufferPointer { rawPtr in + inputs.withUnsafeMutableBufferPointer { inputPtr in + txos.withUnsafeMutableBufferPointer { txoPtr in + record.tx_data = rawPtr.baseAddress + record.tx_data_len = UInt(rawPtr.count) + record.input_outpoints = inputPtr.baseAddress + record.input_outpoints_count = UInt(inputPtr.count) + withUnsafeMutablePointer(to: &record) { recordPtr in + var account = AccountChangeSetFFI() + account.account_type_name = name + account.transactions = recordPtr + account.transactions_count = bytes == nil ? 0 : 1 + account.utxos_added = txoPtr.baseAddress + account.utxos_added_count = UInt(txoPtr.count) + withUnsafeMutablePointer(to: &account) { accountPtr in + var changeset = WalletChangeSetFFI() + changeset.accounts = accountPtr + changeset.accounts_count = 1 + withUnsafePointer(to: &changeset) { ptr in + XCTAssertTrue(handler.persistWalletChangeset(walletId: walletId, changeset: ptr)) + } + } + } + } + } + } + XCTAssertTrue(handler.endChangeset(walletId: walletId, success: true)) + } + + func testShouldStoreEachWalletsEmittedAccountingSeparately() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: -100) + persist(handler, walletId: walletB, txid: txid, bytes: Data([1]), net: 90) + account(handler, walletId: walletA, [row(txid, net: -100, direction: CoreDirectionCode.outgoing, fee: 10)]) + account(handler, walletId: walletB, [row(txid, net: 90, direction: CoreDirectionCode.incoming)]) + + let stored = try transaction(container, txid) + XCTAssertEqual(stored.walletAccounting.count, 2) + XCTAssertEqual(stored.netAmount(for: walletA), -100) + XCTAssertEqual(stored.direction(for: walletA), CoreDirectionCode.outgoing) + XCTAssertEqual(stored.fee(for: walletA), 10) + XCTAssertEqual(stored.netAmount(for: walletB), 90) + XCTAssertEqual(stored.direction(for: walletB), CoreDirectionCode.incoming) + XCTAssertNil(stored.fee(for: walletB), "a wallet that funded nothing knows no fee") + XCTAssertEqual(stored.displayFormattedAmount(for: walletB), "+0.00000090 DASH") + } + + func testShouldUpdateTheWalletsRowInsteadOfAddingAnother() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: 40) + account(handler, walletId: walletA, [row(txid, net: 40, direction: CoreDirectionCode.incoming)]) + account(handler, walletId: walletA, [row(txid, net: -60, direction: CoreDirectionCode.outgoing)]) + + let stored = try transaction(container, txid) + XCTAssertEqual(stored.walletAccounting.count, 1) + XCTAssertEqual(stored.netAmount(for: walletA), -60) + XCTAssertEqual(stored.direction(for: walletA), CoreDirectionCode.outgoing) + } + + /// The load-time backfill's rows and marker commit with its round. + func testShouldCommitTheBackfillsRowsAndMarkerTogether() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: 40) + + XCTAssertTrue(account(handler, walletId: walletA, + [row(txid, net: -60, direction: CoreDirectionCode.outgoing)], markBackfilled: true)) + + XCTAssertEqual(try transaction(container, txid).netAmount(for: walletA), -60) + XCTAssertEqual(try offered(handler, walletId: walletA).backfilled, true) + } + + /// A failed backfill round leaves neither rows nor marker, so the next + /// load offers the history again. + func testShouldDropTheBackfillsRowsAndMarkerWhenItsRoundFails() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: 40) + + account(handler, walletId: walletA, + [row(txid, net: -60, direction: CoreDirectionCode.outgoing)], markBackfilled: true, success: false) + + XCTAssertNil(try transaction(container, txid).netAmount(for: walletA)) + let history = try offered(handler, walletId: walletA) + XCTAssertFalse(history.backfilled) + XCTAssertEqual(history.bodies, [Data([1])]) + } + + func testShouldIgnoreAccountingForATransactionTheStoreDoesNotHold() throws { + let (container, handler) = try makeStore() + + XCTAssertTrue(account(handler, walletId: walletA, [row(txid, net: 5, direction: CoreDirectionCode.incoming)])) + + XCTAssertTrue(try ModelContext(container).fetch(FetchDescriptor()).isEmpty) + } + + /// A record Rust re-emits without accounting (an asset lock restored from + /// its bytes, promoted by a chain lock) overwrites only the shared + /// scalar; the wallet's row, which Rust did not resend, still answers. + func testShouldKeepTheWalletsRowWhenARecordArrivesWithoutAccounting() throws { + let (container, handler) = try makeStore() + let assetLock = TransactionTypeKind.assetLock.rawValue + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: -100, kind: assetLock) + account(handler, walletId: walletA, [row(txid, net: -100, direction: CoreDirectionCode.internalTransfer)]) + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: 0, kind: assetLock) + + let stored = try transaction(container, txid) + XCTAssertEqual(stored.netAmount, 0, "the shared scalar is written as delivered") + XCTAssertEqual(stored.netAmount(for: walletA), -100) + XCTAssertEqual(stored.direction(for: walletA), CoreDirectionCode.internalTransfer) + } + + /// Without a row for the wallet there is no amount to show for it; the + /// shared scalar answers only unscoped views. + func testShouldShowNoAccountingWithoutAWalletRow() { + let tx = PersistentTransaction( + txid: txid, transactionData: Data(), direction: CoreDirectionCode.incoming, netAmount: 25 + ) + XCTAssertNil(tx.netAmount(for: walletA)) + XCTAssertNil(tx.direction(for: walletA)) + XCTAssertEqual(tx.formattedAmount(for: walletA), PersistentTransaction.noAccounting) + XCTAssertEqual(tx.displayNetAmount(for: nil), 25) + XCTAssertEqual(tx.displayDirectionCode(for: nil), CoreDirectionCode.incoming) + } + + func testShouldOfferNothingWhenEveryTransactionHasTheWalletsRow() throws { + let (_, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1, 2]), net: 40) + account(handler, walletId: walletA, [row(txid, net: 40, direction: CoreDirectionCode.incoming)]) + + let history = try offered(handler, walletId: walletA) + + XCTAssertFalse(history.backfilled, "Rust still has to record completion") + XCTAssertEqual(history.bodies, []) + } + + /// Only this wallet's transactions lacking its row cross; Rust fetches + /// any funding transaction it needs on its own. Every loan is returned. + func testShouldOfferOnlyTheWalletsUnaccountedTransactions() throws { + let (_, handler) = try makeStore() + let other = Data(repeating: 4, count: 32) + persist(handler, walletId: walletA, txid: txid, bytes: Data([1, 2]), net: 40) + persist(handler, walletId: walletA, txid: other, bytes: Data([7, 8, 9]), net: -40) + persist(handler, walletId: walletB, txid: Data(repeating: 5, count: 32), bytes: Data([5]), net: 1) + account(handler, walletId: walletA, [row(txid, net: 40, direction: CoreDirectionCode.incoming)]) + + let history = try offered(handler, walletId: walletA) + + XCTAssertFalse(history.backfilled) + XCTAssertEqual(history.bodies, [Data([7, 8, 9])]) + XCTAssertEqual(handler.outstandingCoreHistoryLoanCount, 0) + } + + /// Once the marker is stored, later loads skip the history entirely. + func testShouldReportABackfilledWalletWithoutOfferingHistory() throws { + let (_, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1, 2]), net: 40) + account(handler, walletId: walletA, [], markBackfilled: true) + + let history = try offered(handler, walletId: walletA) + + XCTAssertTrue(history.backfilled) + XCTAssertEqual(history.bodies, []) + } + + /// The backfill end to end through the C shims Rust calls: read the + /// unaccounted body, return the loan, write the row and the marker in + /// one round, then report the wallet backfilled. + func testShouldDriveTheBackfillThroughTheExtensionShims() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1, 2]), net: 40) + let callbacks = handler.makePersistenceCallbacksExtension() + let load = try XCTUnwrap(callbacks.on_load_core_history_for_accounting_fn) + let free = try XCTUnwrap(callbacks.on_load_core_history_for_accounting_free_fn) + let persistRows = try XCTUnwrap(callbacks.on_persist_transaction_accounting_fn) + let accounted = try XCTUnwrap(callbacks.on_persist_core_history_accounted_fn) + let context = Unmanaged.passUnretained(handler).toOpaque() + let rows = [row(txid, net: -60, direction: CoreDirectionCode.outgoing, fee: 5)] + + try walletA.withUnsafeBytes { wallet in + let walletBytes = try XCTUnwrap(wallet.bindMemory(to: UInt8.self).baseAddress) + var backfilled = true + var entries: UnsafePointer? + var count: UInt = 0 + XCTAssertEqual(load(context, walletBytes, &backfilled, &entries, &count), 0) + XCTAssertFalse(backfilled) + XCTAssertEqual(count, 1) + let entry = try XCTUnwrap(entries)[0] + XCTAssertEqual(Data(bytes: try XCTUnwrap(entry.tx_bytes), count: Int(entry.tx_bytes_len)), Data([1, 2])) + free(context, entries, count) + XCTAssertEqual(handler.outstandingCoreHistoryLoanCount, 0) + + handler.beginChangeset(walletId: walletA) + XCTAssertEqual(rows.withUnsafeBufferPointer { + persistRows(context, walletBytes, $0.baseAddress, UInt($0.count)) + }, 0) + XCTAssertEqual(accounted(context, walletBytes), 0) + XCTAssertTrue(handler.endChangeset(walletId: walletA, success: true)) + + XCTAssertEqual(load(context, walletBytes, &backfilled, &entries, &count), 0) + XCTAssertTrue(backfilled) + XCTAssertEqual(count, 0) + XCTAssertNil(entries) + XCTAssertEqual(load(nil, walletBytes, &backfilled, &entries, &count), 1) + } + XCTAssertEqual(try transaction(container, txid).fee(for: walletA), 5) + } + + func testShouldDeleteOnlyTheDepartingWalletsRows() throws { + let (container, handler) = try makeStore() + persist(handler, walletId: walletA, txid: txid, bytes: Data([1]), net: -100) + persist(handler, walletId: walletB, txid: txid, bytes: Data([1]), net: 90) + account(handler, walletId: walletA, [row(txid, net: -100, direction: CoreDirectionCode.outgoing)]) + account(handler, walletId: walletB, [row(txid, net: 90, direction: CoreDirectionCode.incoming)]) + + try handler.deleteWalletData(walletId: walletA) + + let rows = try ModelContext(container).fetch(FetchDescriptor()) + XCTAssertEqual(rows.map(\.walletId), [walletB]) + XCTAssertEqual(rows.first?.netAmount, 90) + } + + func testShouldFormatSignedDuffsWithoutTrappingOnInt64Min() { + XCTAssertEqual(PersistentTransaction.format(duffs: 150_000_000), "+1.50000000 DASH") + XCTAssertEqual(PersistentTransaction.format(duffs: -100), "-0.00000100 DASH") + XCTAssertTrue(PersistentTransaction.format(duffs: .min).hasPrefix("-92233720368.")) + } +} diff --git a/packages/swift-sdk/schema-models.json b/packages/swift-sdk/schema-models.json index 439a181f8e3..b712344a688 100644 --- a/packages/swift-sdk/schema-models.json +++ b/packages/swift-sdk/schema-models.json @@ -36,7 +36,8 @@ "PersistentInvitation": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentInvitation.swift", "PersistentMasternode": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentMasternode.swift", "PersistentTrackedMasternode": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTrackedMasternode.swift", - "PersistentIdentityBalanceMetadata": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentIdentityBalanceMetadata.swift" + "PersistentIdentityBalanceMetadata": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentIdentityBalanceMetadata.swift", + "PersistentTransactionAccounting": "packages/swift-sdk/Sources/SwiftDashSDK/Persistence/Models/PersistentTransactionAccounting.swift" }, "value_types": [ { diff --git a/packages/swift-sdk/scripts/freeze_schema_models.py b/packages/swift-sdk/scripts/freeze_schema_models.py index ef4cf8b08d2..d5c59b05b57 100755 --- a/packages/swift-sdk/scripts/freeze_schema_models.py +++ b/packages/swift-sdk/scripts/freeze_schema_models.py @@ -333,6 +333,8 @@ def emit(path, text): f"{OUT_DIR}/{freeze.schema}+{model}.swift", render_model(freeze, sha, source, model, sibling), ) + # Preserve the merged V4 model graph independently of release capture. + files.update(render_source_snapshot(root, "4.0.0", "292b5101f33b7b7a68f7ffd97df1520b2ff9ef67")) return files @@ -662,13 +664,19 @@ def render_snapshot(root, version, entry, *, inventory=None): commit = entry["platform_sha"] if not re.fullmatch(r"[0-9a-f]{40}", commit): raise SystemExit("release source must be a full Git SHA") - namespace = "DashSchemaSnapshotV" + version.split(".")[0] - if entry["namespace"] != namespace: + inventory = read_inventory(root, commit) if inventory is None else validate_inventory(inventory) + if set(inventory["models"]) != set(entry["schema"]["entity_hashes"]): + raise SystemExit("historical inventory differs from captured model membership") + if entry["namespace"] != "DashSchemaSnapshotV" + version.split(".")[0]: raise SystemExit("unexpected snapshot namespace") + return render_source_snapshot(root, version, commit, inventory=inventory) + + +def render_source_snapshot(root, version, commit, *, inventory=None): + """Freeze a source graph without asserting App Store release provenance.""" + namespace = "DashSchemaSnapshotV" + version.split(".")[0] inventory = read_inventory(root, commit) if inventory is None else validate_inventory(inventory) models = inventory["models"] - if set(models) != set(entry["schema"]["entity_hashes"]): - raise SystemExit("historical inventory differs from captured model membership") sources = inventory_sources(root, inventory, commit) validate_storage_graph(inventory, sources) names = set(models) | {name for group in inventory["value_types"] for name in group["names"]} diff --git a/packages/swift-sdk/scripts/test_freeze_schema_models.py b/packages/swift-sdk/scripts/test_freeze_schema_models.py index 128f501bd42..4d2ef3be8c6 100644 --- a/packages/swift-sdk/scripts/test_freeze_schema_models.py +++ b/packages/swift-sdk/scripts/test_freeze_schema_models.py @@ -51,7 +51,7 @@ def setUp(self): shutil.copyfile(os.path.join(ROOT, gen.TEST_REGISTRY_FILE), target) def test_should_find_the_committed_files_are_the_generators_output(self): - self.assertEqual(len(gen.render_baseline(ROOT)), 71) + self.assertEqual(len(gen.render_baseline(ROOT)), 109) self.assertIn(gen.TEST_REGISTRY_FILE, self.files) self.assertEqual(gen.check_problems(ROOT, self.files), [])