From e97b2477d5e6d861858b11fd32103e8374c35560 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 6 Nov 2026 16:48:13 +0000 Subject: [PATCH 1/8] test(key-wallet-manager): cover rescans over chainlocked blocks #1326 fixed crediting outputs that a rescan newly matches on a known or finalized transaction, with tests at the `check_core_transaction` level. These tests drive `WalletManager::process_block_for_wallets` the way the filter rescan does: the block is processed again while a pass derives new scripts, with the ChainLock applied before the first pass, between passes, or not at all. For a transaction paying a run of `gap + gap/2` addresses, and for a transaction whose far output only a later transaction's gap extension reaches, they check the coins held, the balance, and the last record the `BlockProcessed` events carried, which is what a store built from the events keeps. Another set checks that outputs spent in the next block end spent after the rescan, including after a checkpoint prunes the observed spends and after the account is reloaded through serde. The `ManagedAccountRefMut::confirm_transaction` doc said it returns a record only when the confirmation status changes. It also returns one that gains outputs derived since it was recorded. Co-Authored-By: Claude Opus 5.5 --- key-wallet-manager/Cargo.toml | 1 + key-wallet-manager/src/lib.rs | 2 + key-wallet-manager/src/rescan_tests.rs | 387 ++++++++++++++++++ .../managed_account/managed_account_ref.rs | 5 +- 4 files changed, 393 insertions(+), 2 deletions(-) create mode 100644 key-wallet-manager/src/rescan_tests.rs diff --git a/key-wallet-manager/Cargo.toml b/key-wallet-manager/Cargo.toml index fab1bba8e..831146ec9 100644 --- a/key-wallet-manager/Cargo.toml +++ b/key-wallet-manager/Cargo.toml @@ -43,6 +43,7 @@ bincode = { workspace = true, optional = true } serde = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] +bincode = { workspace = true, features = ["serde"] } key-wallet = { workspace = true, features = ["test-utils", "bincode"] } dashcore = { workspace = true, features = ["test-utils"] } tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } diff --git a/key-wallet-manager/src/lib.rs b/key-wallet-manager/src/lib.rs index 491ab08f5..0f0f2e694 100644 --- a/key-wallet-manager/src/lib.rs +++ b/key-wallet-manager/src/lib.rs @@ -800,6 +800,8 @@ fn current_timestamp() -> u64 { #[cfg(test)] mod event_tests; #[cfg(test)] +mod rescan_tests; +#[cfg(test)] mod test_helpers; #[cfg(any(test, feature = "test-utils"))] diff --git a/key-wallet-manager/src/rescan_tests.rs b/key-wallet-manager/src/rescan_tests.rs new file mode 100644 index 000000000..7e2f299e3 --- /dev/null +++ b/key-wallet-manager/src/rescan_tests.rs @@ -0,0 +1,387 @@ +//! Rescans of a block that pays addresses past the gap limit, re-delivered after each pass that +//! derives new addresses. The coins, the record the events carry and the balance must not depend on +//! when the ChainLock arrives: before the first pass, between passes, or never. + +use super::test_helpers::*; +use super::*; +use crate::wallet_interface::WalletInterface; +use dashcore::block::{Block, Header, Version}; +use dashcore::ephemerealdata::chain_lock::ChainLock; +use dashcore::{BlockHash, CompactTarget, ScriptBuf, TxIn, TxMerkleNode, TxOut}; +use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; +use key_wallet::managed_account::managed_account_type::ManagedAccountType; +use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; +use key_wallet::managed_account::transaction_record::OutputRole; +use key_wallet::{AddressPool, KeySource}; +use std::collections::BTreeSet; + +const HEIGHT: CoreBlockHeight = 100; +const VALUE: u64 = 100_000; +const MAX_PASSES: usize = 16; + +#[derive(Clone, Copy, Debug)] +enum ChainLockAt { + BeforeFirstPass, + BetweenPasses, + Never, +} + +fn account<'a>(manager: &'a WalletManager, wallet_id: &WalletId) -> &'a ManagedCoreFundsAccount { + manager + .get_wallet_info(wallet_id) + .expect("wallet info") + .first_bip44_managed_account() + .expect("BIP44 account 0") +} + +fn external_pool(manager: &WalletManager, wallet_id: &WalletId) -> AddressPool { + match account(manager, wallet_id).managed_account_type() { + ManagedAccountType::Standard { + external_addresses, + .. + } => external_addresses.clone(), + other => panic!("BIP44 account 0 has an unexpected type {other:?}"), + } +} + +/// The BIP44 account 0 external address at `index`, derived on a copy of the +/// pool so the wallet's own pool stays as it is. +fn external_address(manager: &WalletManager, wallet_id: &WalletId, index: u32) -> Address { + let wallet = manager.get_wallet(wallet_id).expect("wallet"); + let xpub = wallet.get_bip44_account(0).expect("BIP44 account 0").account_xpub; + let mut pool = external_pool(manager, wallet_id); + pool.generate_up_to(index, &KeySource::Public(xpub)).expect("derives"); + pool.address_at_index(index).expect("derived") +} + +fn transaction(inputs: impl IntoIterator, output: Vec) -> Transaction { + Transaction { + version: 2, + lock_time: 0, + input: inputs + .into_iter() + .map(|previous_output| TxIn { + previous_output, + script_sig: ScriptBuf::new(), + sequence: u32::MAX, + witness: Default::default(), + }) + .collect(), + output, + special_transaction_payload: None, + } +} + +fn paying_external( + manager: &WalletManager, + wallet_id: &WalletId, + indices: impl IntoIterator, + input_seed: u8, +) -> Transaction { + let output = indices + .into_iter() + .map(|index| TxOut { + value: VALUE, + script_pubkey: external_address(manager, wallet_id, index).script_pubkey(), + }) + .collect(); + transaction([OutPoint::new(Txid::from_byte_array([input_seed; 32]), 0)], output) +} + +fn spending_to_nobody(outpoints: impl IntoIterator) -> Transaction { + let output = TxOut { + value: VALUE / 2, + script_pubkey: ScriptBuf::new_op_return(&[0x57]), + }; + transaction(outpoints, vec![output]) +} + +fn block_at(height: CoreBlockHeight, txdata: Vec) -> Block { + Block { + header: Header { + version: Version::default(), + prev_blockhash: BlockHash::from_byte_array([height as u8; 32]), + merkle_root: TxMerkleNode::all_zeros(), + time: 1_000 + height, + bits: CompactTarget::from_consensus(0x1d00ffff), + nonce: 0, + }, + txdata, + } +} + +/// One pass over `block`. Returns whether it derived new scripts, which is +/// what makes the filter rescan deliver the block again. +async fn pass( + manager: &mut WalletManager, + wallet_id: WalletId, + block: &Block, + height: CoreBlockHeight, +) -> bool { + let wallets = BTreeSet::from([wallet_id]); + let result = + manager.process_block_for_wallets(block, block.block_hash(), height, &wallets).await; + result.new_scripts.values().any(|scripts| !scripts.is_empty()) +} + +/// Processes `block` like the filter pipeline does: once, then again while a +/// pass derives new scripts. Applies the ChainLock at `chain_lock_at`. +async fn process_with_rescans( + manager: &mut WalletManager, + wallet_id: WalletId, + block: &Block, + chain_lock_at: ChainLockAt, +) { + if matches!(chain_lock_at, ChainLockAt::BeforeFirstPass) { + manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); + } + let mut derived = pass(manager, wallet_id, block, HEIGHT).await; + if matches!(chain_lock_at, ChainLockAt::BetweenPasses) { + manager.apply_chain_lock(ChainLock::dummy(HEIGHT)); + } + for _ in 0..MAX_PASSES { + if !derived { + return; + } + derived = pass(manager, wallet_id, block, HEIGHT).await; + } + panic!("the rescan passes did not settle"); +} + +/// The rescan re-delivers the funding block and the block after it in height +/// order, as the filters of the newly derived scripts match both, until no +/// pass derives new scripts. +async fn rescan_in_height_order( + manager: &mut WalletManager, + wallet_id: WalletId, + funding: &Block, + spends: &Block, +) { + for _ in 0..MAX_PASSES { + let derived = pass(manager, wallet_id, funding, HEIGHT).await; + let derived_next = pass(manager, wallet_id, spends, HEIGHT + 1).await; + if !derived && !derived_next { + return; + } + } + panic!("the rescan passes did not settle"); +} + +fn assert_no_observed_spends(manager: &WalletManager, wallet_id: &WalletId) { + let info = manager.get_wallet_info(wallet_id).expect("wallet info"); + let observed = info.observed_spent_outpoints(); + assert!(observed.is_empty(), "the checkpoint left observed spends {observed:?}"); +} + +fn held_vouts(manager: &WalletManager, wallet_id: &WalletId, txid: &Txid) -> BTreeSet { + let utxos = account(manager, wallet_id).utxos.keys(); + utxos.filter(|outpoint| outpoint.txid == *txid).map(|outpoint| outpoint.vout).collect() +} + +/// The record for `txid` a store mirroring the `BlockProcessed` events would +/// hold: the last one any of them carried, inserted or updated. +fn last_emitted(events: &[WalletEvent], txid: &Txid) -> TransactionRecord { + events + .iter() + .filter_map(|event| match event { + WalletEvent::BlockProcessed { + inserted, + updated, + .. + } => Some(inserted.iter().chain(updated)), + _ => None, + }) + .flatten() + .rfind(|record| record.txid == *txid) + .cloned() + .unwrap_or_else(|| panic!("no BlockProcessed event carried {txid}")) +} + +fn owned_output_indices(record: &TransactionRecord) -> BTreeSet { + record + .output_details + .iter() + .filter(|detail| matches!(detail.role, OutputRole::Received | OutputRole::Change)) + .map(|detail| detail.index) + .collect() +} + +fn assert_credited_in_full( + manager: &WalletManager, + wallet_id: &WalletId, + events: &[WalletEvent], + tx: &Transaction, + chain_lock_at: ChainLockAt, +) { + let txid = tx.txid(); + let all: BTreeSet = (0..tx.output.len() as u32).collect(); + let paid = (VALUE * tx.output.len() as u64) as i64; + assert_eq!(held_vouts(manager, wallet_id, &txid), all, "{chain_lock_at:?}: coins held"); + let record = last_emitted(events, &txid); + assert_eq!(owned_output_indices(&record), all, "{chain_lock_at:?}: emitted record owns"); + assert_eq!(record.net_amount, paid, "{chain_lock_at:?}: emitted net amount"); + if let Some(stored) = account(manager, wallet_id).transactions().get(&txid) { + assert_eq!(owned_output_indices(stored), all, "{chain_lock_at:?}: stored record owns"); + assert_eq!(stored.net_amount, paid, "{chain_lock_at:?}: stored net amount"); + } +} + +fn assert_balance( + manager: &WalletManager, + wallet_id: &WalletId, + expected: u64, + chain_lock_at: ChainLockAt, +) { + let balance = manager.get_wallet_info(wallet_id).expect("wallet info").balance(); + assert_eq!(balance.total(), expected, "{chain_lock_at:?}: balance"); +} + +/// One transaction paying a contiguous run of external addresses from index 0 +/// to well past the gap limit. +async fn run_past_the_gap_limit(chain_lock_at: ChainLockAt) { + let (mut manager, wallet_id, _) = setup_manager_with_wallet(); + let gap = external_pool(&manager, &wallet_id).gap_limit; + let tx = paying_external(&manager, &wallet_id, 0..gap + gap / 2, 0x57); + let block = block_at(HEIGHT, vec![tx.clone()]); + let mut rx = manager.subscribe_events(); + + process_with_rescans(&mut manager, wallet_id, &block, chain_lock_at).await; + + let events = drain_events(&mut rx); + assert_credited_in_full(&manager, &wallet_id, &events, &tx, chain_lock_at); + assert_balance(&manager, &wallet_id, VALUE * tx.output.len() as u64, chain_lock_at); +} + +#[tokio::test] +async fn test_born_chainlocked_rescan_credits_a_run_past_the_gap_limit() { + run_past_the_gap_limit(ChainLockAt::BeforeFirstPass).await; +} + +#[tokio::test] +async fn test_chain_lock_between_passes_keeps_a_run_past_the_gap_limit() { + run_past_the_gap_limit(ChainLockAt::BetweenPasses).await; +} + +#[tokio::test] +async fn test_unchainlocked_rescan_emits_a_record_owning_a_run_past_the_gap_limit() { + run_past_the_gap_limit(ChainLockAt::Never).await; +} + +/// The block's first transaction pays index 0 and an index the window only +/// reaches after the block's second transaction, which pays inside the +/// window, extends it: only a re-delivery can find the first one's far output. +fn late_and_extending(manager: &WalletManager, wallet_id: &WalletId) -> (Transaction, Transaction) { + let gap = external_pool(manager, wallet_id).gap_limit; + let late = paying_external(manager, wallet_id, [0, gap + gap / 3], 0x51); + let extending = paying_external(manager, wallet_id, [gap * 2 / 3], 0x52); + (late, extending) +} + +async fn output_a_later_payment_derives(chain_lock_at: ChainLockAt) { + let (mut manager, wallet_id, _) = setup_manager_with_wallet(); + let (late, extending) = late_and_extending(&manager, &wallet_id); + let block = block_at(HEIGHT, vec![late.clone(), extending.clone()]); + let mut rx = manager.subscribe_events(); + + process_with_rescans(&mut manager, wallet_id, &block, chain_lock_at).await; + + let events = drain_events(&mut rx); + assert_credited_in_full(&manager, &wallet_id, &events, &late, chain_lock_at); + assert_credited_in_full(&manager, &wallet_id, &events, &extending, chain_lock_at); + assert_balance(&manager, &wallet_id, 3 * VALUE, chain_lock_at); +} + +#[tokio::test] +async fn test_born_chainlocked_rescan_credits_an_output_a_later_payment_derives() { + output_a_later_payment_derives(ChainLockAt::BeforeFirstPass).await; +} + +#[tokio::test] +async fn test_chain_lock_between_passes_credits_an_output_a_later_payment_derives() { + output_a_later_payment_derives(ChainLockAt::BetweenPasses).await; +} + +#[tokio::test] +async fn test_unchainlocked_rescan_emits_the_record_an_output_a_later_payment_derives_corrects() { + output_a_later_payment_derives(ChainLockAt::Never).await; +} + +/// A late output and a held output, both spent in the next block, must end spent once the rescan +/// has re-delivered both blocks. After the observed spends are pruned, the funding block's +/// re-delivery credits the late output until the next block's re-delivery spends it. +async fn spent_outputs_stay_spent(chain_lock_at: ChainLockAt) { + let (mut manager, wallet_id, _) = setup_manager_with_wallet(); + let (late, extending) = late_and_extending(&manager, &wallet_id); + let funding = block_at(HEIGHT, vec![late.clone(), extending.clone()]); + let spends = block_at( + HEIGHT + 1, + vec![ + spending_to_nobody([OutPoint::new(late.txid(), 0)]), + spending_to_nobody([OutPoint::new(late.txid(), 1)]), + ], + ); + if matches!(chain_lock_at, ChainLockAt::BeforeFirstPass) { + manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); + } + pass(&mut manager, wallet_id, &funding, HEIGHT).await; + pass(&mut manager, wallet_id, &spends, HEIGHT + 1).await; + if !matches!(chain_lock_at, ChainLockAt::Never) { + manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); + // The committed checkpoint lifts the finality boundary over both + // blocks, which prunes the spends observed in them. + manager.update_wallet_synced_height(&wallet_id, HEIGHT + 1); + assert_no_observed_spends(&manager, &wallet_id); + } + rescan_in_height_order(&mut manager, wallet_id, &funding, &spends).await; + + let late_coins = held_vouts(&manager, &wallet_id, &late.txid()); + assert!(late_coins.is_empty(), "{chain_lock_at:?}: late coins {late_coins:?}"); + let extending_coins = held_vouts(&manager, &wallet_id, &extending.txid()); + assert_eq!(extending_coins, BTreeSet::from([0]), "{chain_lock_at:?}: extending coins"); + assert_balance(&manager, &wallet_id, VALUE, chain_lock_at); +} + +#[tokio::test] +async fn test_born_chainlocked_rescan_keeps_late_outputs_spent() { + spent_outputs_stay_spent(ChainLockAt::BeforeFirstPass).await; +} + +#[tokio::test] +async fn test_chain_locked_rescan_keeps_late_outputs_spent() { + spent_outputs_stay_spent(ChainLockAt::BetweenPasses).await; +} + +#[tokio::test] +async fn test_unchainlocked_rescan_keeps_late_outputs_spent() { + spent_outputs_stay_spent(ChainLockAt::Never).await; +} + +/// One spender in the next block spends the held output and the late one. Reloading the account +/// through serde after the checkpoint rebuilds its spent marks from the records finality kept, and +/// the rescan must still end with both coins spent. +#[tokio::test] +async fn test_rescan_after_an_account_reload_keeps_late_outputs_spent() { + let (mut manager, wallet_id, _) = setup_manager_with_wallet(); + let (late, extending) = late_and_extending(&manager, &wallet_id); + let funding = block_at(HEIGHT, vec![late.clone(), extending.clone()]); + let both = [OutPoint::new(late.txid(), 0), OutPoint::new(late.txid(), 1)]; + let spends = block_at(HEIGHT + 1, vec![spending_to_nobody(both)]); + manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); + pass(&mut manager, wallet_id, &funding, HEIGHT).await; + pass(&mut manager, wallet_id, &spends, HEIGHT + 1).await; + manager.update_wallet_synced_height(&wallet_id, HEIGHT + 1); + assert_no_observed_spends(&manager, &wallet_id); + + let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet info"); + let account = info.first_bip44_managed_account_mut().expect("BIP44 account 0"); + let config = bincode::config::standard(); + let bytes = bincode::serde::encode_to_vec(&*account, config).expect("encodes"); + *account = bincode::serde::decode_from_slice(&bytes, config).expect("decodes").0; + + rescan_in_height_order(&mut manager, wallet_id, &funding, &spends).await; + + let late_coins = held_vouts(&manager, &wallet_id, &late.txid()); + assert!(late_coins.is_empty(), "late coins {late_coins:?}"); + assert_eq!(held_vouts(&manager, &wallet_id, &extending.txid()), BTreeSet::from([0])); + assert_balance(&manager, &wallet_id, VALUE, ChainLockAt::BeforeFirstPass); +} diff --git a/key-wallet/src/managed_account/managed_account_ref.rs b/key-wallet/src/managed_account/managed_account_ref.rs index eb79a95e4..ec67aa037 100644 --- a/key-wallet/src/managed_account/managed_account_ref.rs +++ b/key-wallet/src/managed_account/managed_account_ref.rs @@ -346,8 +346,9 @@ impl<'a> ManagedAccountRefMut<'a> { /// Re-process an existing transaction with updated context. /// - /// Funds variants additionally refresh UTXO state. Returns the updated - /// record only when confirmation status actually changes. + /// Funds variants additionally refresh UTXO state. Returns the record when it is new, when + /// its confirmation status changed, or, for funds variants, when it gains outputs derived + /// since it was recorded; a record finality reduced to a summary comes back rebuilt from it. /// /// This public entry point confirms with no observed-spend context — the /// pre-#649 behavior, appropriate for callers driving an account directly From 16a24cc145d8dcd4ca555af70b45df97a594fd5d Mon Sep 17 00:00:00 2001 From: pasta Date: Thu, 12 Nov 2026 15:32:30 +0000 Subject: [PATCH 2/8] test(key-wallet-manager): reuse the shared block and tx helpers The rescan tests built their blocks with a local `block_at`, a near copy of `Block::dummy`. They now use `Block::dummy` and `TX_AMOUNT`, and their multi-input, multi-output `transaction` builder moves to `test_helpers` as `create_tx`, which `create_tx_paying_to` now builds on. Co-Authored-By: Claude Opus 5.5 --- key-wallet-manager/src/rescan_tests.rs | 67 +++++++------------------- key-wallet-manager/src/test_helpers.rs | 36 ++++++++------ 2 files changed, 39 insertions(+), 64 deletions(-) diff --git a/key-wallet-manager/src/rescan_tests.rs b/key-wallet-manager/src/rescan_tests.rs index 7e2f299e3..0435448a6 100644 --- a/key-wallet-manager/src/rescan_tests.rs +++ b/key-wallet-manager/src/rescan_tests.rs @@ -5,9 +5,9 @@ use super::test_helpers::*; use super::*; use crate::wallet_interface::WalletInterface; -use dashcore::block::{Block, Header, Version}; +use dashcore::block::Block; use dashcore::ephemerealdata::chain_lock::ChainLock; -use dashcore::{BlockHash, CompactTarget, ScriptBuf, TxIn, TxMerkleNode, TxOut}; +use dashcore::{ScriptBuf, TxOut}; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; use key_wallet::managed_account::managed_account_type::ManagedAccountType; use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; @@ -16,7 +16,6 @@ use key_wallet::{AddressPool, KeySource}; use std::collections::BTreeSet; const HEIGHT: CoreBlockHeight = 100; -const VALUE: u64 = 100_000; const MAX_PASSES: usize = 16; #[derive(Clone, Copy, Debug)] @@ -54,24 +53,6 @@ fn external_address(manager: &WalletManager, wallet_id: &WalletId, index: u32) - pool.address_at_index(index).expect("derived") } -fn transaction(inputs: impl IntoIterator, output: Vec) -> Transaction { - Transaction { - version: 2, - lock_time: 0, - input: inputs - .into_iter() - .map(|previous_output| TxIn { - previous_output, - script_sig: ScriptBuf::new(), - sequence: u32::MAX, - witness: Default::default(), - }) - .collect(), - output, - special_transaction_payload: None, - } -} - fn paying_external( manager: &WalletManager, wallet_id: &WalletId, @@ -81,33 +62,19 @@ fn paying_external( let output = indices .into_iter() .map(|index| TxOut { - value: VALUE, + value: TX_AMOUNT, script_pubkey: external_address(manager, wallet_id, index).script_pubkey(), }) .collect(); - transaction([OutPoint::new(Txid::from_byte_array([input_seed; 32]), 0)], output) + create_tx([OutPoint::new(Txid::from_byte_array([input_seed; 32]), 0)], output) } fn spending_to_nobody(outpoints: impl IntoIterator) -> Transaction { let output = TxOut { - value: VALUE / 2, + value: TX_AMOUNT / 2, script_pubkey: ScriptBuf::new_op_return(&[0x57]), }; - transaction(outpoints, vec![output]) -} - -fn block_at(height: CoreBlockHeight, txdata: Vec) -> Block { - Block { - header: Header { - version: Version::default(), - prev_blockhash: BlockHash::from_byte_array([height as u8; 32]), - merkle_root: TxMerkleNode::all_zeros(), - time: 1_000 + height, - bits: CompactTarget::from_consensus(0x1d00ffff), - nonce: 0, - }, - txdata, - } + create_tx(outpoints, vec![output]) } /// One pass over `block`. Returns whether it derived new scripts, which is @@ -215,7 +182,7 @@ fn assert_credited_in_full( ) { let txid = tx.txid(); let all: BTreeSet = (0..tx.output.len() as u32).collect(); - let paid = (VALUE * tx.output.len() as u64) as i64; + let paid = (TX_AMOUNT * tx.output.len() as u64) as i64; assert_eq!(held_vouts(manager, wallet_id, &txid), all, "{chain_lock_at:?}: coins held"); let record = last_emitted(events, &txid); assert_eq!(owned_output_indices(&record), all, "{chain_lock_at:?}: emitted record owns"); @@ -242,14 +209,14 @@ async fn run_past_the_gap_limit(chain_lock_at: ChainLockAt) { let (mut manager, wallet_id, _) = setup_manager_with_wallet(); let gap = external_pool(&manager, &wallet_id).gap_limit; let tx = paying_external(&manager, &wallet_id, 0..gap + gap / 2, 0x57); - let block = block_at(HEIGHT, vec![tx.clone()]); + let block = Block::dummy(HEIGHT, vec![tx.clone()]); let mut rx = manager.subscribe_events(); process_with_rescans(&mut manager, wallet_id, &block, chain_lock_at).await; let events = drain_events(&mut rx); assert_credited_in_full(&manager, &wallet_id, &events, &tx, chain_lock_at); - assert_balance(&manager, &wallet_id, VALUE * tx.output.len() as u64, chain_lock_at); + assert_balance(&manager, &wallet_id, TX_AMOUNT * tx.output.len() as u64, chain_lock_at); } #[tokio::test] @@ -280,7 +247,7 @@ fn late_and_extending(manager: &WalletManager, wallet_id: &WalletId) -> (Transac async fn output_a_later_payment_derives(chain_lock_at: ChainLockAt) { let (mut manager, wallet_id, _) = setup_manager_with_wallet(); let (late, extending) = late_and_extending(&manager, &wallet_id); - let block = block_at(HEIGHT, vec![late.clone(), extending.clone()]); + let block = Block::dummy(HEIGHT, vec![late.clone(), extending.clone()]); let mut rx = manager.subscribe_events(); process_with_rescans(&mut manager, wallet_id, &block, chain_lock_at).await; @@ -288,7 +255,7 @@ async fn output_a_later_payment_derives(chain_lock_at: ChainLockAt) { let events = drain_events(&mut rx); assert_credited_in_full(&manager, &wallet_id, &events, &late, chain_lock_at); assert_credited_in_full(&manager, &wallet_id, &events, &extending, chain_lock_at); - assert_balance(&manager, &wallet_id, 3 * VALUE, chain_lock_at); + assert_balance(&manager, &wallet_id, 3 * TX_AMOUNT, chain_lock_at); } #[tokio::test] @@ -312,8 +279,8 @@ async fn test_unchainlocked_rescan_emits_the_record_an_output_a_later_payment_de async fn spent_outputs_stay_spent(chain_lock_at: ChainLockAt) { let (mut manager, wallet_id, _) = setup_manager_with_wallet(); let (late, extending) = late_and_extending(&manager, &wallet_id); - let funding = block_at(HEIGHT, vec![late.clone(), extending.clone()]); - let spends = block_at( + let funding = Block::dummy(HEIGHT, vec![late.clone(), extending.clone()]); + let spends = Block::dummy( HEIGHT + 1, vec![ spending_to_nobody([OutPoint::new(late.txid(), 0)]), @@ -338,7 +305,7 @@ async fn spent_outputs_stay_spent(chain_lock_at: ChainLockAt) { assert!(late_coins.is_empty(), "{chain_lock_at:?}: late coins {late_coins:?}"); let extending_coins = held_vouts(&manager, &wallet_id, &extending.txid()); assert_eq!(extending_coins, BTreeSet::from([0]), "{chain_lock_at:?}: extending coins"); - assert_balance(&manager, &wallet_id, VALUE, chain_lock_at); + assert_balance(&manager, &wallet_id, TX_AMOUNT, chain_lock_at); } #[tokio::test] @@ -363,9 +330,9 @@ async fn test_unchainlocked_rescan_keeps_late_outputs_spent() { async fn test_rescan_after_an_account_reload_keeps_late_outputs_spent() { let (mut manager, wallet_id, _) = setup_manager_with_wallet(); let (late, extending) = late_and_extending(&manager, &wallet_id); - let funding = block_at(HEIGHT, vec![late.clone(), extending.clone()]); + let funding = Block::dummy(HEIGHT, vec![late.clone(), extending.clone()]); let both = [OutPoint::new(late.txid(), 0), OutPoint::new(late.txid(), 1)]; - let spends = block_at(HEIGHT + 1, vec![spending_to_nobody(both)]); + let spends = Block::dummy(HEIGHT + 1, vec![spending_to_nobody(both)]); manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); pass(&mut manager, wallet_id, &funding, HEIGHT).await; pass(&mut manager, wallet_id, &spends, HEIGHT + 1).await; @@ -383,5 +350,5 @@ async fn test_rescan_after_an_account_reload_keeps_late_outputs_spent() { let late_coins = held_vouts(&manager, &wallet_id, &late.txid()); assert!(late_coins.is_empty(), "late coins {late_coins:?}"); assert_eq!(held_vouts(&manager, &wallet_id, &extending.txid()), BTreeSet::from([0])); - assert_balance(&manager, &wallet_id, VALUE, ChainLockAt::BeforeFirstPass); + assert_balance(&manager, &wallet_id, TX_AMOUNT, ChainLockAt::BeforeFirstPass); } diff --git a/key-wallet-manager/src/test_helpers.rs b/key-wallet-manager/src/test_helpers.rs index f930ea46b..fed468476 100644 --- a/key-wallet-manager/src/test_helpers.rs +++ b/key-wallet-manager/src/test_helpers.rs @@ -29,27 +29,35 @@ pub(crate) fn setup_manager_with_wallet() -> (WalletManager, (manager, wallet_id, addr) } -pub(crate) fn create_tx_paying_to(addr: &Address, input_seed: u8) -> Transaction { +pub(crate) fn create_tx( + inputs: impl IntoIterator, + output: Vec, +) -> Transaction { Transaction { version: 2, lock_time: 0, - input: vec![TxIn { - previous_output: OutPoint { - txid: Txid::from_byte_array([input_seed; 32]), - vout: 0, - }, - script_sig: ScriptBuf::new(), - sequence: u32::MAX, - witness: Witness::default(), - }], - output: vec![TxOut { - value: TX_AMOUNT, - script_pubkey: addr.script_pubkey(), - }], + input: inputs + .into_iter() + .map(|previous_output| TxIn { + previous_output, + script_sig: ScriptBuf::new(), + sequence: u32::MAX, + witness: Witness::default(), + }) + .collect(), + output, special_transaction_payload: None, } } +pub(crate) fn create_tx_paying_to(addr: &Address, input_seed: u8) -> Transaction { + let output = TxOut { + value: TX_AMOUNT, + script_pubkey: addr.script_pubkey(), + }; + create_tx([OutPoint::new(Txid::from_byte_array([input_seed; 32]), 0)], vec![output]) +} + pub(crate) fn drain_events(rx: &mut broadcast::Receiver) -> Vec { let mut events = Vec::new(); while let Ok(e) = rx.try_recv() { From 543db753da84704a80cceec3f8e1802714789970 Mon Sep 17 00:00:00 2001 From: pasta Date: Thu, 12 Nov 2026 16:05:55 +0000 Subject: [PATCH 3/8] test(key-wallet-manager): trim the rescan tests to two The rescan tests ran ten cases over three ChainLock timings. Under mutants of the #1326 code, the case where the ChainLock lands before the first pass caught exactly what the case where it lands between passes caught, the gap-limit run and the spent-output cases caught nothing the others did not, and the unchainlocked path is caught by #1326's own wallet_checker tests. Two tests remain: a later payment's derived output with the ChainLock between passes, and the spend after an account reload. One `rescan` helper now delivers the blocks with their heights, then re-delivers them until a pass derives nothing, with a hook after the first pass. The payments come from `create_tx_paying`, which takes a list of addresses and which `create_tx_paying_to` now builds on; the spender is the one local builder left. The `confirm_transaction` doc is shortened. Co-Authored-By: Claude Opus 5.5 --- key-wallet-manager/src/rescan_tests.rs | 306 +++++------------- key-wallet-manager/src/test_helpers.rs | 41 +-- .../managed_account/managed_account_ref.rs | 6 +- 3 files changed, 107 insertions(+), 246 deletions(-) diff --git a/key-wallet-manager/src/rescan_tests.rs b/key-wallet-manager/src/rescan_tests.rs index 0435448a6..61db380c3 100644 --- a/key-wallet-manager/src/rescan_tests.rs +++ b/key-wallet-manager/src/rescan_tests.rs @@ -1,13 +1,9 @@ -//! Rescans of a block that pays addresses past the gap limit, re-delivered after each pass that -//! derives new addresses. The coins, the record the events carry and the balance must not depend on -//! when the ChainLock arrives: before the first pass, between passes, or never. - use super::test_helpers::*; use super::*; use crate::wallet_interface::WalletInterface; use dashcore::block::Block; use dashcore::ephemerealdata::chain_lock::ChainLock; -use dashcore::{ScriptBuf, TxOut}; +use dashcore::{ScriptBuf, TxIn, TxOut}; use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; use key_wallet::managed_account::managed_account_type::ManagedAccountType; use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; @@ -18,13 +14,6 @@ use std::collections::BTreeSet; const HEIGHT: CoreBlockHeight = 100; const MAX_PASSES: usize = 16; -#[derive(Clone, Copy, Debug)] -enum ChainLockAt { - BeforeFirstPass, - BetweenPasses, - Never, -} - fn account<'a>(manager: &'a WalletManager, wallet_id: &WalletId) -> &'a ManagedCoreFundsAccount { manager .get_wallet_info(wallet_id) @@ -43,103 +32,82 @@ fn external_pool(manager: &WalletManager, wallet_id: &WalletId) -> AddressPool { } } -/// The BIP44 account 0 external address at `index`, derived on a copy of the +/// The BIP44 account 0 external addresses at `indices`, derived on a copy of the /// pool so the wallet's own pool stays as it is. -fn external_address(manager: &WalletManager, wallet_id: &WalletId, index: u32) -> Address { +fn external_addresses( + manager: &WalletManager, + wallet_id: &WalletId, + indices: &[u32], +) -> Vec
{ let wallet = manager.get_wallet(wallet_id).expect("wallet"); let xpub = wallet.get_bip44_account(0).expect("BIP44 account 0").account_xpub; let mut pool = external_pool(manager, wallet_id); - pool.generate_up_to(index, &KeySource::Public(xpub)).expect("derives"); - pool.address_at_index(index).expect("derived") -} - -fn paying_external( - manager: &WalletManager, - wallet_id: &WalletId, - indices: impl IntoIterator, - input_seed: u8, -) -> Transaction { - let output = indices - .into_iter() - .map(|index| TxOut { - value: TX_AMOUNT, - script_pubkey: external_address(manager, wallet_id, index).script_pubkey(), + indices + .iter() + .map(|&index| { + pool.generate_up_to(index, &KeySource::Public(xpub)).expect("derives"); + pool.address_at_index(index).expect("derived") }) - .collect(); - create_tx([OutPoint::new(Txid::from_byte_array([input_seed; 32]), 0)], output) + .collect() } -fn spending_to_nobody(outpoints: impl IntoIterator) -> Transaction { - let output = TxOut { - value: TX_AMOUNT / 2, - script_pubkey: ScriptBuf::new_op_return(&[0x57]), - }; - create_tx(outpoints, vec![output]) +fn spending(outpoints: impl IntoIterator) -> Transaction { + Transaction { + version: 2, + lock_time: 0, + input: outpoints + .into_iter() + .map(|previous_output| TxIn { + previous_output, + ..TxIn::default() + }) + .collect(), + output: vec![TxOut { + value: TX_AMOUNT / 2, + script_pubkey: ScriptBuf::new_op_return(&[0x57]), + }], + special_transaction_payload: None, + } } -/// One pass over `block`. Returns whether it derived new scripts, which is -/// what makes the filter rescan deliver the block again. -async fn pass( - manager: &mut WalletManager, - wallet_id: WalletId, - block: &Block, - height: CoreBlockHeight, -) -> bool { - let wallets = BTreeSet::from([wallet_id]); - let result = - manager.process_block_for_wallets(block, block.block_hash(), height, &wallets).await; - result.new_scripts.values().any(|scripts| !scripts.is_empty()) +/// Returns two payments for one block. `late` pays index 0 and an index past the window. +/// `extending` pays inside the window and extends it over that index, so only a re-delivery +/// credits `late`'s far output. +fn late_and_extending(manager: &WalletManager, wallet_id: &WalletId) -> (Transaction, Transaction) { + let gap = external_pool(manager, wallet_id).gap_limit; + let late = create_tx_paying(&external_addresses(manager, wallet_id, &[0, gap + gap / 3]), 0x51); + let extending = create_tx_paying(&external_addresses(manager, wallet_id, &[gap * 2 / 3]), 0x52); + (late, extending) } -/// Processes `block` like the filter pipeline does: once, then again while a -/// pass derives new scripts. Applies the ChainLock at `chain_lock_at`. -async fn process_with_rescans( +/// Delivers `blocks` in order, then again while a pass derives new scripts, as the filter +/// rescan does. `after_first_pass` runs between the first pass and the re-deliveries. +async fn rescan( manager: &mut WalletManager, wallet_id: WalletId, - block: &Block, - chain_lock_at: ChainLockAt, + blocks: &[(CoreBlockHeight, &Block)], + after_first_pass: impl FnOnce(&mut WalletManager), ) { - if matches!(chain_lock_at, ChainLockAt::BeforeFirstPass) { - manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); - } - let mut derived = pass(manager, wallet_id, block, HEIGHT).await; - if matches!(chain_lock_at, ChainLockAt::BetweenPasses) { - manager.apply_chain_lock(ChainLock::dummy(HEIGHT)); - } + let wallets = BTreeSet::from([wallet_id]); + let mut after_first_pass = Some(after_first_pass); for _ in 0..MAX_PASSES { - if !derived { - return; + let mut derived = false; + for &(height, block) in blocks { + let result = manager + .process_block_for_wallets(block, block.block_hash(), height, &wallets) + .await; + derived |= result.new_scripts.values().any(|scripts| !scripts.is_empty()); } - derived = pass(manager, wallet_id, block, HEIGHT).await; - } - panic!("the rescan passes did not settle"); -} - -/// The rescan re-delivers the funding block and the block after it in height -/// order, as the filters of the newly derived scripts match both, until no -/// pass derives new scripts. -async fn rescan_in_height_order( - manager: &mut WalletManager, - wallet_id: WalletId, - funding: &Block, - spends: &Block, -) { - for _ in 0..MAX_PASSES { - let derived = pass(manager, wallet_id, funding, HEIGHT).await; - let derived_next = pass(manager, wallet_id, spends, HEIGHT + 1).await; - if !derived && !derived_next { + if let Some(after_first_pass) = after_first_pass.take() { + assert!(derived, "the first pass derived no new scripts"); + after_first_pass(manager); + } else if !derived { return; } } panic!("the rescan passes did not settle"); } -fn assert_no_observed_spends(manager: &WalletManager, wallet_id: &WalletId) { - let info = manager.get_wallet_info(wallet_id).expect("wallet info"); - let observed = info.observed_spent_outpoints(); - assert!(observed.is_empty(), "the checkpoint left observed spends {observed:?}"); -} - fn held_vouts(manager: &WalletManager, wallet_id: &WalletId, txid: &Txid) -> BTreeSet { let utxos = account(manager, wallet_id).utxos.keys(); utxos.filter(|outpoint| outpoint.txid == *txid).map(|outpoint| outpoint.vout).collect() @@ -178,149 +146,41 @@ fn assert_credited_in_full( wallet_id: &WalletId, events: &[WalletEvent], tx: &Transaction, - chain_lock_at: ChainLockAt, ) { let txid = tx.txid(); let all: BTreeSet = (0..tx.output.len() as u32).collect(); let paid = (TX_AMOUNT * tx.output.len() as u64) as i64; - assert_eq!(held_vouts(manager, wallet_id, &txid), all, "{chain_lock_at:?}: coins held"); + assert_eq!(held_vouts(manager, wallet_id, &txid), all, "coins held"); let record = last_emitted(events, &txid); - assert_eq!(owned_output_indices(&record), all, "{chain_lock_at:?}: emitted record owns"); - assert_eq!(record.net_amount, paid, "{chain_lock_at:?}: emitted net amount"); + assert_eq!(owned_output_indices(&record), all, "emitted record owns"); + assert_eq!(record.net_amount, paid, "emitted net amount"); if let Some(stored) = account(manager, wallet_id).transactions().get(&txid) { - assert_eq!(owned_output_indices(stored), all, "{chain_lock_at:?}: stored record owns"); - assert_eq!(stored.net_amount, paid, "{chain_lock_at:?}: stored net amount"); + assert_eq!(owned_output_indices(stored), all, "stored record owns"); + assert_eq!(stored.net_amount, paid, "stored net amount"); } } -fn assert_balance( - manager: &WalletManager, - wallet_id: &WalletId, - expected: u64, - chain_lock_at: ChainLockAt, -) { +fn assert_balance(manager: &WalletManager, wallet_id: &WalletId, expected: u64) { let balance = manager.get_wallet_info(wallet_id).expect("wallet info").balance(); - assert_eq!(balance.total(), expected, "{chain_lock_at:?}: balance"); -} - -/// One transaction paying a contiguous run of external addresses from index 0 -/// to well past the gap limit. -async fn run_past_the_gap_limit(chain_lock_at: ChainLockAt) { - let (mut manager, wallet_id, _) = setup_manager_with_wallet(); - let gap = external_pool(&manager, &wallet_id).gap_limit; - let tx = paying_external(&manager, &wallet_id, 0..gap + gap / 2, 0x57); - let block = Block::dummy(HEIGHT, vec![tx.clone()]); - let mut rx = manager.subscribe_events(); - - process_with_rescans(&mut manager, wallet_id, &block, chain_lock_at).await; - - let events = drain_events(&mut rx); - assert_credited_in_full(&manager, &wallet_id, &events, &tx, chain_lock_at); - assert_balance(&manager, &wallet_id, TX_AMOUNT * tx.output.len() as u64, chain_lock_at); + assert_eq!(balance.total(), expected, "balance"); } #[tokio::test] -async fn test_born_chainlocked_rescan_credits_a_run_past_the_gap_limit() { - run_past_the_gap_limit(ChainLockAt::BeforeFirstPass).await; -} - -#[tokio::test] -async fn test_chain_lock_between_passes_keeps_a_run_past_the_gap_limit() { - run_past_the_gap_limit(ChainLockAt::BetweenPasses).await; -} - -#[tokio::test] -async fn test_unchainlocked_rescan_emits_a_record_owning_a_run_past_the_gap_limit() { - run_past_the_gap_limit(ChainLockAt::Never).await; -} - -/// The block's first transaction pays index 0 and an index the window only -/// reaches after the block's second transaction, which pays inside the -/// window, extends it: only a re-delivery can find the first one's far output. -fn late_and_extending(manager: &WalletManager, wallet_id: &WalletId) -> (Transaction, Transaction) { - let gap = external_pool(manager, wallet_id).gap_limit; - let late = paying_external(manager, wallet_id, [0, gap + gap / 3], 0x51); - let extending = paying_external(manager, wallet_id, [gap * 2 / 3], 0x52); - (late, extending) -} - -async fn output_a_later_payment_derives(chain_lock_at: ChainLockAt) { +async fn test_chain_lock_between_passes_credits_an_output_a_later_payment_derives() { let (mut manager, wallet_id, _) = setup_manager_with_wallet(); let (late, extending) = late_and_extending(&manager, &wallet_id); let block = Block::dummy(HEIGHT, vec![late.clone(), extending.clone()]); let mut rx = manager.subscribe_events(); - process_with_rescans(&mut manager, wallet_id, &block, chain_lock_at).await; + rescan(&mut manager, wallet_id, &[(HEIGHT, &block)], |manager| { + manager.apply_chain_lock(ChainLock::dummy(HEIGHT)); + }) + .await; let events = drain_events(&mut rx); - assert_credited_in_full(&manager, &wallet_id, &events, &late, chain_lock_at); - assert_credited_in_full(&manager, &wallet_id, &events, &extending, chain_lock_at); - assert_balance(&manager, &wallet_id, 3 * TX_AMOUNT, chain_lock_at); -} - -#[tokio::test] -async fn test_born_chainlocked_rescan_credits_an_output_a_later_payment_derives() { - output_a_later_payment_derives(ChainLockAt::BeforeFirstPass).await; -} - -#[tokio::test] -async fn test_chain_lock_between_passes_credits_an_output_a_later_payment_derives() { - output_a_later_payment_derives(ChainLockAt::BetweenPasses).await; -} - -#[tokio::test] -async fn test_unchainlocked_rescan_emits_the_record_an_output_a_later_payment_derives_corrects() { - output_a_later_payment_derives(ChainLockAt::Never).await; -} - -/// A late output and a held output, both spent in the next block, must end spent once the rescan -/// has re-delivered both blocks. After the observed spends are pruned, the funding block's -/// re-delivery credits the late output until the next block's re-delivery spends it. -async fn spent_outputs_stay_spent(chain_lock_at: ChainLockAt) { - let (mut manager, wallet_id, _) = setup_manager_with_wallet(); - let (late, extending) = late_and_extending(&manager, &wallet_id); - let funding = Block::dummy(HEIGHT, vec![late.clone(), extending.clone()]); - let spends = Block::dummy( - HEIGHT + 1, - vec![ - spending_to_nobody([OutPoint::new(late.txid(), 0)]), - spending_to_nobody([OutPoint::new(late.txid(), 1)]), - ], - ); - if matches!(chain_lock_at, ChainLockAt::BeforeFirstPass) { - manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); - } - pass(&mut manager, wallet_id, &funding, HEIGHT).await; - pass(&mut manager, wallet_id, &spends, HEIGHT + 1).await; - if !matches!(chain_lock_at, ChainLockAt::Never) { - manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); - // The committed checkpoint lifts the finality boundary over both - // blocks, which prunes the spends observed in them. - manager.update_wallet_synced_height(&wallet_id, HEIGHT + 1); - assert_no_observed_spends(&manager, &wallet_id); - } - rescan_in_height_order(&mut manager, wallet_id, &funding, &spends).await; - - let late_coins = held_vouts(&manager, &wallet_id, &late.txid()); - assert!(late_coins.is_empty(), "{chain_lock_at:?}: late coins {late_coins:?}"); - let extending_coins = held_vouts(&manager, &wallet_id, &extending.txid()); - assert_eq!(extending_coins, BTreeSet::from([0]), "{chain_lock_at:?}: extending coins"); - assert_balance(&manager, &wallet_id, TX_AMOUNT, chain_lock_at); -} - -#[tokio::test] -async fn test_born_chainlocked_rescan_keeps_late_outputs_spent() { - spent_outputs_stay_spent(ChainLockAt::BeforeFirstPass).await; -} - -#[tokio::test] -async fn test_chain_locked_rescan_keeps_late_outputs_spent() { - spent_outputs_stay_spent(ChainLockAt::BetweenPasses).await; -} - -#[tokio::test] -async fn test_unchainlocked_rescan_keeps_late_outputs_spent() { - spent_outputs_stay_spent(ChainLockAt::Never).await; + assert_credited_in_full(&manager, &wallet_id, &events, &late); + assert_credited_in_full(&manager, &wallet_id, &events, &extending); + assert_balance(&manager, &wallet_id, 3 * TX_AMOUNT); } /// One spender in the next block spends the held output and the late one. Reloading the account @@ -332,23 +192,23 @@ async fn test_rescan_after_an_account_reload_keeps_late_outputs_spent() { let (late, extending) = late_and_extending(&manager, &wallet_id); let funding = Block::dummy(HEIGHT, vec![late.clone(), extending.clone()]); let both = [OutPoint::new(late.txid(), 0), OutPoint::new(late.txid(), 1)]; - let spends = Block::dummy(HEIGHT + 1, vec![spending_to_nobody(both)]); + let spends = Block::dummy(HEIGHT + 1, vec![spending(both)]); manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); - pass(&mut manager, wallet_id, &funding, HEIGHT).await; - pass(&mut manager, wallet_id, &spends, HEIGHT + 1).await; - manager.update_wallet_synced_height(&wallet_id, HEIGHT + 1); - assert_no_observed_spends(&manager, &wallet_id); - let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet info"); - let account = info.first_bip44_managed_account_mut().expect("BIP44 account 0"); - let config = bincode::config::standard(); - let bytes = bincode::serde::encode_to_vec(&*account, config).expect("encodes"); - *account = bincode::serde::decode_from_slice(&bytes, config).expect("decodes").0; - - rescan_in_height_order(&mut manager, wallet_id, &funding, &spends).await; + rescan(&mut manager, wallet_id, &[(HEIGHT, &funding), (HEIGHT + 1, &spends)], |manager| { + manager.update_wallet_synced_height(&wallet_id, HEIGHT + 1); + let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet info"); + let observed = info.observed_spent_outpoints(); + assert!(observed.is_empty(), "the checkpoint left observed spends {observed:?}"); + let account = info.first_bip44_managed_account_mut().expect("BIP44 account 0"); + let config = bincode::config::standard(); + let bytes = bincode::serde::encode_to_vec(&*account, config).expect("encodes"); + *account = bincode::serde::decode_from_slice(&bytes, config).expect("decodes").0; + }) + .await; let late_coins = held_vouts(&manager, &wallet_id, &late.txid()); assert!(late_coins.is_empty(), "late coins {late_coins:?}"); assert_eq!(held_vouts(&manager, &wallet_id, &extending.txid()), BTreeSet::from([0])); - assert_balance(&manager, &wallet_id, TX_AMOUNT, ChainLockAt::BeforeFirstPass); + assert_balance(&manager, &wallet_id, TX_AMOUNT); } diff --git a/key-wallet-manager/src/test_helpers.rs b/key-wallet-manager/src/test_helpers.rs index fed468476..72f85a640 100644 --- a/key-wallet-manager/src/test_helpers.rs +++ b/key-wallet-manager/src/test_helpers.rs @@ -4,6 +4,7 @@ use dashcore::{OutPoint, ScriptBuf, TxIn, TxOut, Txid, Witness}; use key_wallet::wallet::initialization::WalletAccountCreationOptions; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; use key_wallet::Network; +use std::slice; use tokio::sync::broadcast; pub(crate) const TEST_MNEMONIC: &str = @@ -29,35 +30,35 @@ pub(crate) fn setup_manager_with_wallet() -> (WalletManager, (manager, wallet_id, addr) } -pub(crate) fn create_tx( - inputs: impl IntoIterator, - output: Vec, -) -> Transaction { +pub(crate) fn create_tx_paying_to(addr: &Address, input_seed: u8) -> Transaction { + create_tx_paying(slice::from_ref(addr), input_seed) +} + +/// Pays `TX_AMOUNT` to each of `addrs`, from one input seeded with `input_seed`. +pub(crate) fn create_tx_paying(addrs: &[Address], input_seed: u8) -> Transaction { Transaction { version: 2, lock_time: 0, - input: inputs - .into_iter() - .map(|previous_output| TxIn { - previous_output, - script_sig: ScriptBuf::new(), - sequence: u32::MAX, - witness: Witness::default(), + input: vec![TxIn { + previous_output: OutPoint { + txid: Txid::from_byte_array([input_seed; 32]), + vout: 0, + }, + script_sig: ScriptBuf::new(), + sequence: u32::MAX, + witness: Witness::default(), + }], + output: addrs + .iter() + .map(|addr| TxOut { + value: TX_AMOUNT, + script_pubkey: addr.script_pubkey(), }) .collect(), - output, special_transaction_payload: None, } } -pub(crate) fn create_tx_paying_to(addr: &Address, input_seed: u8) -> Transaction { - let output = TxOut { - value: TX_AMOUNT, - script_pubkey: addr.script_pubkey(), - }; - create_tx([OutPoint::new(Txid::from_byte_array([input_seed; 32]), 0)], vec![output]) -} - pub(crate) fn drain_events(rx: &mut broadcast::Receiver) -> Vec { let mut events = Vec::new(); while let Ok(e) = rx.try_recv() { diff --git a/key-wallet/src/managed_account/managed_account_ref.rs b/key-wallet/src/managed_account/managed_account_ref.rs index ec67aa037..b9a75a5c8 100644 --- a/key-wallet/src/managed_account/managed_account_ref.rs +++ b/key-wallet/src/managed_account/managed_account_ref.rs @@ -346,9 +346,9 @@ impl<'a> ManagedAccountRefMut<'a> { /// Re-process an existing transaction with updated context. /// - /// Funds variants additionally refresh UTXO state. Returns the record when it is new, when - /// its confirmation status changed, or, for funds variants, when it gains outputs derived - /// since it was recorded; a record finality reduced to a summary comes back rebuilt from it. + /// Funds variants additionally refresh UTXO state. Returns the record when it is new or its + /// confirmation status changed, and, for funds variants, when it gained outputs derived since + /// it was recorded. /// /// This public entry point confirms with no observed-spend context — the /// pre-#649 behavior, appropriate for callers driving an account directly From ce21e7afc1da62ff9ced9ad5c06955831db304e9 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 17:23:25 +0000 Subject: [PATCH 4/8] test(key-wallet-manager): consolidate rescan coverage on dev's block passes Since #1368, process_block_for_wallets checks a block again until no wallet derives more, so a block that derives its own addresses is credited by one call and the filter rescan no longer delivers it again. The rescan still delivers an earlier block again when a later block derives an address it pays, which is the path #1326 fixed. Both tests now live in process_block.rs and use that cross-block shape on BIP44 account 0; dev's part of the credits test keeps the shared CoinJoin helper from test_utils: - test_process_block_credits_addresses_the_block_derives also delivers a block whose far output only the next block derives, applies a ChainLock, and delivers it again: the coins, the balance and the last emitted record must own every output. - test_rescan_after_an_account_reload_keeps_late_outputs_spent spends both outputs in the block after, prunes observed spends at a checkpoint, reloads the account through serde, and delivers the blocks again: both coins must end spent. rescan_tests.rs, its rescan helper and MAX_PASSES are gone. Co-Authored-By: Claude Opus 5.5 --- key-wallet-manager/src/lib.rs | 2 - key-wallet-manager/src/process_block.rs | 225 +++++++++++++++++++++++- key-wallet-manager/src/rescan_tests.rs | 214 ---------------------- 3 files changed, 224 insertions(+), 217 deletions(-) delete mode 100644 key-wallet-manager/src/rescan_tests.rs diff --git a/key-wallet-manager/src/lib.rs b/key-wallet-manager/src/lib.rs index 0f0f2e694..491ab08f5 100644 --- a/key-wallet-manager/src/lib.rs +++ b/key-wallet-manager/src/lib.rs @@ -800,8 +800,6 @@ fn current_timestamp() -> u64 { #[cfg(test)] mod event_tests; #[cfg(test)] -mod rescan_tests; -#[cfg(test)] mod test_helpers; #[cfg(any(test, feature = "test-utils"))] diff --git a/key-wallet-manager/src/process_block.rs b/key-wallet-manager/src/process_block.rs index dd54d5ec2..713c143da 100644 --- a/key-wallet-manager/src/process_block.rs +++ b/key-wallet-manager/src/process_block.rs @@ -740,12 +740,17 @@ mod tests { use key_wallet::account::ManagedAccountTrait as _; use key_wallet::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; use key_wallet::managed_account::address_pool::AddressPoolType; + use key_wallet::managed_account::managed_account_type::ManagedAccountType; + use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; + use key_wallet::managed_account::transaction_record::OutputRole; use key_wallet::mnemonic::Language; use key_wallet::wallet::initialization::WalletAccountCreationOptions; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; - use key_wallet::{AccountType, Mnemonic}; + use key_wallet::{AccountType, AddressPool, KeySource, Mnemonic}; use std::collections::HashMap; + const HEIGHT: CoreBlockHeight = 100; + fn coinjoin_account<'a>( manager: &'a WalletManager, wallet_id: &WalletId, @@ -759,6 +764,152 @@ mod tests { .expect("CoinJoin account 0") } + fn account<'a>( + manager: &'a WalletManager, + wallet_id: &WalletId, + ) -> &'a ManagedCoreFundsAccount { + manager + .get_wallet_info(wallet_id) + .expect("wallet info") + .first_bip44_managed_account() + .expect("BIP44 account 0") + } + + fn external_pool( + manager: &WalletManager, + wallet_id: &WalletId, + ) -> AddressPool { + match account(manager, wallet_id).managed_account_type() { + ManagedAccountType::Standard { + external_addresses, + .. + } => external_addresses.clone(), + other => panic!("BIP44 account 0 has an unexpected type {other:?}"), + } + } + + /// The BIP44 account 0 external addresses at `indices`, derived on a copy of the + /// pool so the wallet's own pool stays as it is. + fn external_addresses( + manager: &WalletManager, + wallet_id: &WalletId, + indices: &[u32], + ) -> Vec
{ + let wallet = manager.get_wallet(wallet_id).expect("wallet"); + let xpub = wallet.get_bip44_account(0).expect("BIP44 account 0").account_xpub; + let mut pool = external_pool(manager, wallet_id); + indices + .iter() + .map(|&index| { + pool.generate_up_to(index, &KeySource::Public(xpub)).expect("derives"); + pool.address_at_index(index).expect("derived") + }) + .collect() + } + + /// `late` pays index 0 and an index past the window. `extending` pays inside the + /// window and extends it over that index. In a later block than `late`, it leaves + /// `late`'s far output to the filter rescan, which delivers `late`'s block again. + fn late_and_extending( + manager: &WalletManager, + wallet_id: &WalletId, + ) -> (Transaction, Transaction) { + let gap = external_pool(manager, wallet_id).gap_limit; + let late = + create_tx_paying(&external_addresses(manager, wallet_id, &[0, gap + gap / 3]), 0x51); + let extending = + create_tx_paying(&external_addresses(manager, wallet_id, &[gap * 2 / 3]), 0x52); + (late, extending) + } + + fn spending(outpoints: impl IntoIterator) -> Transaction { + Transaction { + version: 2, + lock_time: 0, + input: outpoints + .into_iter() + .map(|previous_output| TxIn { + previous_output, + ..TxIn::default() + }) + .collect(), + output: vec![TxOut { + value: TX_AMOUNT / 2, + script_pubkey: ScriptBuf::new_op_return(&[0x57]), + }], + special_transaction_payload: None, + } + } + + fn held_vouts( + manager: &WalletManager, + wallet_id: &WalletId, + txid: &Txid, + ) -> BTreeSet { + let utxos = account(manager, wallet_id).utxos.keys(); + utxos.filter(|outpoint| outpoint.txid == *txid).map(|outpoint| outpoint.vout).collect() + } + + /// The record for `txid` a store mirroring the `BlockProcessed` events would + /// hold: the last one any of them carried, inserted or updated. + fn last_emitted(events: &[WalletEvent], txid: &Txid) -> TransactionRecord { + events + .iter() + .filter_map(|event| match event { + WalletEvent::BlockProcessed { + inserted, + updated, + .. + } => Some(inserted.iter().chain(updated)), + _ => None, + }) + .flatten() + .rfind(|record| record.txid == *txid) + .cloned() + .unwrap_or_else(|| panic!("no BlockProcessed event carried {txid}")) + } + + fn owned_output_indices(record: &TransactionRecord) -> BTreeSet { + record + .output_details + .iter() + .filter(|detail| matches!(detail.role, OutputRole::Received | OutputRole::Change)) + .map(|detail| detail.index) + .collect() + } + + fn assert_credited_in_full( + manager: &WalletManager, + wallet_id: &WalletId, + events: &[WalletEvent], + tx: &Transaction, + ) { + let txid = tx.txid(); + let all: BTreeSet = (0..tx.output.len() as u32).collect(); + let paid = (TX_AMOUNT * tx.output.len() as u64) as i64; + assert_eq!(held_vouts(manager, wallet_id, &txid), all, "coins held"); + let record = last_emitted(events, &txid); + assert_eq!(owned_output_indices(&record), all, "emitted record owns"); + assert_eq!(record.net_amount, paid, "emitted net amount"); + if let Some(stored) = account(manager, wallet_id).transactions().get(&txid) { + assert_eq!(owned_output_indices(stored), all, "stored record owns"); + assert_eq!(stored.net_amount, paid, "stored net amount"); + } + } + + fn assert_balance( + manager: &WalletManager, + wallet_id: &WalletId, + expected: u64, + ) { + let balance = manager.get_wallet_info(wallet_id).expect("wallet info").balance(); + assert_eq!(balance.total(), expected, "balance"); + } + + fn derives(result: &BlockProcessingResult, wallet_id: &WalletId, script: &ScriptBuf) -> bool { + result.new_scripts.get(wallet_id).is_some_and(|scripts| scripts.contains(script)) + } + fn spend_first_output_of(tx: &Transaction) -> Transaction { Transaction { version: 2, @@ -1022,6 +1173,8 @@ mod tests { /// A block paying addresses that only its own transactions derive is fully /// credited by one call. A transaction checked again is listed and recorded once. + /// A payment whose far output only a later block derives is credited in full when + /// the filter rescan delivers its block again after a ChainLock. #[tokio::test] async fn test_process_block_credits_addresses_the_block_derives() { let (mut manager, wallet_id, _addr) = setup_manager_with_wallet(); @@ -1090,6 +1243,76 @@ mod tests { assert_eq!(inserted.iter().map(|record| record.txid).collect::>(), txids); let both_record = inserted.iter().find(|record| record.txid == both.txid()); assert_eq!(both_record.map(|record| record.net_amount), Some(2 * TX_AMOUNT as i64)); + + let (late, extending) = late_and_extending(&manager, &wallet_id); + let late_block = Block::dummy(200, vec![late.clone()]); + let extending_block = Block::dummy(201, vec![extending.clone()]); + let wallets = BTreeSet::from([wallet_id]); + let mut rx = manager.subscribe_events(); + manager + .process_block_for_wallets(&late_block, late_block.block_hash(), 200, &wallets) + .await; + let extended = manager + .process_block_for_wallets( + &extending_block, + extending_block.block_hash(), + 201, + &wallets, + ) + .await; + assert!(derives(&extended, &wallet_id, &late.output[1].script_pubkey)); + manager.apply_chain_lock(ChainLock::dummy(201)); + let again = manager + .process_block_for_wallets(&late_block, late_block.block_hash(), 200, &wallets) + .await; + assert_eq!(again.existing_txids, vec![late.txid()]); + let events = drain_events(&mut rx); + assert_credited_in_full(&manager, &wallet_id, &events, &late); + assert_credited_in_full(&manager, &wallet_id, &events, &extending); + assert_balance(&manager, &wallet_id, 7 * TX_AMOUNT); + } + + /// The block after `extending`'s spends the held output of `late` and its far one. + /// After a checkpoint prunes the observed spends and the account is reloaded through + /// serde, which rebuilds its spent marks from the records finality kept, delivering + /// `late`'s block and the spender again must leave both coins spent. + #[tokio::test] + async fn test_rescan_after_an_account_reload_keeps_late_outputs_spent() { + let (mut manager, wallet_id, _) = setup_manager_with_wallet(); + let (late, extending) = late_and_extending(&manager, &wallet_id); + let funding = Block::dummy(HEIGHT, vec![late.clone()]); + let extending_block = Block::dummy(HEIGHT + 1, vec![extending.clone()]); + let both = [OutPoint::new(late.txid(), 0), OutPoint::new(late.txid(), 1)]; + let spends = Block::dummy(HEIGHT + 2, vec![spending(both)]); + manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); + + let wallets = BTreeSet::from([wallet_id]); + manager.process_block_for_wallets(&funding, funding.block_hash(), HEIGHT, &wallets).await; + let extended = manager + .process_block_for_wallets( + &extending_block, + extending_block.block_hash(), + HEIGHT + 1, + &wallets, + ) + .await; + assert!(derives(&extended, &wallet_id, &late.output[1].script_pubkey)); + manager.process_block_for_wallets(&spends, spends.block_hash(), HEIGHT + 2, &wallets).await; + manager.update_wallet_synced_height(&wallet_id, HEIGHT + 2); + let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet info"); + let observed = info.observed_spent_outpoints(); + assert!(observed.is_empty(), "the checkpoint left observed spends {observed:?}"); + let account = info.first_bip44_managed_account_mut().expect("BIP44 account 0"); + let config = bincode::config::standard(); + let bytes = bincode::serde::encode_to_vec(&*account, config).expect("encodes"); + *account = bincode::serde::decode_from_slice(&bytes, config).expect("decodes").0; + manager.process_block_for_wallets(&funding, funding.block_hash(), HEIGHT, &wallets).await; + manager.process_block_for_wallets(&spends, spends.block_hash(), HEIGHT + 2, &wallets).await; + + let late_coins = held_vouts(&manager, &wallet_id, &late.txid()); + assert!(late_coins.is_empty(), "late coins {late_coins:?}"); + assert_eq!(held_vouts(&manager, &wallet_id, &extending.txid()), BTreeSet::from([0])); + assert_balance(&manager, &wallet_id, TX_AMOUNT); } #[tokio::test] diff --git a/key-wallet-manager/src/rescan_tests.rs b/key-wallet-manager/src/rescan_tests.rs deleted file mode 100644 index 61db380c3..000000000 --- a/key-wallet-manager/src/rescan_tests.rs +++ /dev/null @@ -1,214 +0,0 @@ -use super::test_helpers::*; -use super::*; -use crate::wallet_interface::WalletInterface; -use dashcore::block::Block; -use dashcore::ephemerealdata::chain_lock::ChainLock; -use dashcore::{ScriptBuf, TxIn, TxOut}; -use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; -use key_wallet::managed_account::managed_account_type::ManagedAccountType; -use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; -use key_wallet::managed_account::transaction_record::OutputRole; -use key_wallet::{AddressPool, KeySource}; -use std::collections::BTreeSet; - -const HEIGHT: CoreBlockHeight = 100; -const MAX_PASSES: usize = 16; - -fn account<'a>(manager: &'a WalletManager, wallet_id: &WalletId) -> &'a ManagedCoreFundsAccount { - manager - .get_wallet_info(wallet_id) - .expect("wallet info") - .first_bip44_managed_account() - .expect("BIP44 account 0") -} - -fn external_pool(manager: &WalletManager, wallet_id: &WalletId) -> AddressPool { - match account(manager, wallet_id).managed_account_type() { - ManagedAccountType::Standard { - external_addresses, - .. - } => external_addresses.clone(), - other => panic!("BIP44 account 0 has an unexpected type {other:?}"), - } -} - -/// The BIP44 account 0 external addresses at `indices`, derived on a copy of the -/// pool so the wallet's own pool stays as it is. -fn external_addresses( - manager: &WalletManager, - wallet_id: &WalletId, - indices: &[u32], -) -> Vec
{ - let wallet = manager.get_wallet(wallet_id).expect("wallet"); - let xpub = wallet.get_bip44_account(0).expect("BIP44 account 0").account_xpub; - let mut pool = external_pool(manager, wallet_id); - indices - .iter() - .map(|&index| { - pool.generate_up_to(index, &KeySource::Public(xpub)).expect("derives"); - pool.address_at_index(index).expect("derived") - }) - .collect() -} - -fn spending(outpoints: impl IntoIterator) -> Transaction { - Transaction { - version: 2, - lock_time: 0, - input: outpoints - .into_iter() - .map(|previous_output| TxIn { - previous_output, - ..TxIn::default() - }) - .collect(), - output: vec![TxOut { - value: TX_AMOUNT / 2, - script_pubkey: ScriptBuf::new_op_return(&[0x57]), - }], - special_transaction_payload: None, - } -} - -/// Returns two payments for one block. `late` pays index 0 and an index past the window. -/// `extending` pays inside the window and extends it over that index, so only a re-delivery -/// credits `late`'s far output. -fn late_and_extending(manager: &WalletManager, wallet_id: &WalletId) -> (Transaction, Transaction) { - let gap = external_pool(manager, wallet_id).gap_limit; - let late = create_tx_paying(&external_addresses(manager, wallet_id, &[0, gap + gap / 3]), 0x51); - let extending = create_tx_paying(&external_addresses(manager, wallet_id, &[gap * 2 / 3]), 0x52); - (late, extending) -} - -/// Delivers `blocks` in order, then again while a pass derives new scripts, as the filter -/// rescan does. `after_first_pass` runs between the first pass and the re-deliveries. -async fn rescan( - manager: &mut WalletManager, - wallet_id: WalletId, - blocks: &[(CoreBlockHeight, &Block)], - after_first_pass: impl FnOnce(&mut WalletManager), -) { - let wallets = BTreeSet::from([wallet_id]); - let mut after_first_pass = Some(after_first_pass); - for _ in 0..MAX_PASSES { - let mut derived = false; - for &(height, block) in blocks { - let result = manager - .process_block_for_wallets(block, block.block_hash(), height, &wallets) - .await; - derived |= result.new_scripts.values().any(|scripts| !scripts.is_empty()); - } - if let Some(after_first_pass) = after_first_pass.take() { - assert!(derived, "the first pass derived no new scripts"); - after_first_pass(manager); - } else if !derived { - return; - } - } - panic!("the rescan passes did not settle"); -} - -fn held_vouts(manager: &WalletManager, wallet_id: &WalletId, txid: &Txid) -> BTreeSet { - let utxos = account(manager, wallet_id).utxos.keys(); - utxos.filter(|outpoint| outpoint.txid == *txid).map(|outpoint| outpoint.vout).collect() -} - -/// The record for `txid` a store mirroring the `BlockProcessed` events would -/// hold: the last one any of them carried, inserted or updated. -fn last_emitted(events: &[WalletEvent], txid: &Txid) -> TransactionRecord { - events - .iter() - .filter_map(|event| match event { - WalletEvent::BlockProcessed { - inserted, - updated, - .. - } => Some(inserted.iter().chain(updated)), - _ => None, - }) - .flatten() - .rfind(|record| record.txid == *txid) - .cloned() - .unwrap_or_else(|| panic!("no BlockProcessed event carried {txid}")) -} - -fn owned_output_indices(record: &TransactionRecord) -> BTreeSet { - record - .output_details - .iter() - .filter(|detail| matches!(detail.role, OutputRole::Received | OutputRole::Change)) - .map(|detail| detail.index) - .collect() -} - -fn assert_credited_in_full( - manager: &WalletManager, - wallet_id: &WalletId, - events: &[WalletEvent], - tx: &Transaction, -) { - let txid = tx.txid(); - let all: BTreeSet = (0..tx.output.len() as u32).collect(); - let paid = (TX_AMOUNT * tx.output.len() as u64) as i64; - assert_eq!(held_vouts(manager, wallet_id, &txid), all, "coins held"); - let record = last_emitted(events, &txid); - assert_eq!(owned_output_indices(&record), all, "emitted record owns"); - assert_eq!(record.net_amount, paid, "emitted net amount"); - if let Some(stored) = account(manager, wallet_id).transactions().get(&txid) { - assert_eq!(owned_output_indices(stored), all, "stored record owns"); - assert_eq!(stored.net_amount, paid, "stored net amount"); - } -} - -fn assert_balance(manager: &WalletManager, wallet_id: &WalletId, expected: u64) { - let balance = manager.get_wallet_info(wallet_id).expect("wallet info").balance(); - assert_eq!(balance.total(), expected, "balance"); -} - -#[tokio::test] -async fn test_chain_lock_between_passes_credits_an_output_a_later_payment_derives() { - let (mut manager, wallet_id, _) = setup_manager_with_wallet(); - let (late, extending) = late_and_extending(&manager, &wallet_id); - let block = Block::dummy(HEIGHT, vec![late.clone(), extending.clone()]); - let mut rx = manager.subscribe_events(); - - rescan(&mut manager, wallet_id, &[(HEIGHT, &block)], |manager| { - manager.apply_chain_lock(ChainLock::dummy(HEIGHT)); - }) - .await; - - let events = drain_events(&mut rx); - assert_credited_in_full(&manager, &wallet_id, &events, &late); - assert_credited_in_full(&manager, &wallet_id, &events, &extending); - assert_balance(&manager, &wallet_id, 3 * TX_AMOUNT); -} - -/// One spender in the next block spends the held output and the late one. Reloading the account -/// through serde after the checkpoint rebuilds its spent marks from the records finality kept, and -/// the rescan must still end with both coins spent. -#[tokio::test] -async fn test_rescan_after_an_account_reload_keeps_late_outputs_spent() { - let (mut manager, wallet_id, _) = setup_manager_with_wallet(); - let (late, extending) = late_and_extending(&manager, &wallet_id); - let funding = Block::dummy(HEIGHT, vec![late.clone(), extending.clone()]); - let both = [OutPoint::new(late.txid(), 0), OutPoint::new(late.txid(), 1)]; - let spends = Block::dummy(HEIGHT + 1, vec![spending(both)]); - manager.apply_chain_lock(ChainLock::dummy(HEIGHT + 10)); - - rescan(&mut manager, wallet_id, &[(HEIGHT, &funding), (HEIGHT + 1, &spends)], |manager| { - manager.update_wallet_synced_height(&wallet_id, HEIGHT + 1); - let info = manager.get_wallet_info_mut(&wallet_id).expect("wallet info"); - let observed = info.observed_spent_outpoints(); - assert!(observed.is_empty(), "the checkpoint left observed spends {observed:?}"); - let account = info.first_bip44_managed_account_mut().expect("BIP44 account 0"); - let config = bincode::config::standard(); - let bytes = bincode::serde::encode_to_vec(&*account, config).expect("encodes"); - *account = bincode::serde::decode_from_slice(&bytes, config).expect("decodes").0; - }) - .await; - - let late_coins = held_vouts(&manager, &wallet_id, &late.txid()); - assert!(late_coins.is_empty(), "late coins {late_coins:?}"); - assert_eq!(held_vouts(&manager, &wallet_id, &extending.txid()), BTreeSet::from([0])); - assert_balance(&manager, &wallet_id, TX_AMOUNT); -} From de90ccc552fbae74472cee9b2a66bb005583d62b Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 19:08:04 +0000 Subject: [PATCH 5/8] test(key-wallet-manager): assert the reloaded rescan credits the late coin With keep-finalized-transactions the reloaded account still holds the spender's record, so the parent's re-delivery credits nothing. Under the default features finalized records are pruned, and the re-delivery must credit the far output before the spender's re-delivery spends it again. Checking that keeps the reload test from passing when the crediting itself stops. Co-Authored-By: Claude Opus 5.5 --- key-wallet-manager/src/process_block.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/key-wallet-manager/src/process_block.rs b/key-wallet-manager/src/process_block.rs index 713c143da..b443d1330 100644 --- a/key-wallet-manager/src/process_block.rs +++ b/key-wallet-manager/src/process_block.rs @@ -1307,6 +1307,14 @@ mod tests { let bytes = bincode::serde::encode_to_vec(&*account, config).expect("encodes"); *account = bincode::serde::decode_from_slice(&bytes, config).expect("decodes").0; manager.process_block_for_wallets(&funding, funding.block_hash(), HEIGHT, &wallets).await; + // Kept records rebuild the spender's marks on reload, so only pruned + // ones leave the far coin credited until its spender comes again. + let credited = if cfg!(feature = "keep-finalized-transactions") { + BTreeSet::new() + } else { + BTreeSet::from([1]) + }; + assert_eq!(held_vouts(&manager, &wallet_id, &late.txid()), credited); manager.process_block_for_wallets(&spends, spends.block_hash(), HEIGHT + 2, &wallets).await; let late_coins = held_vouts(&manager, &wallet_id, &late.txid()); From 1c1f50abe1ff2a47152d12539e793bac722dc67e Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 19:34:22 +0000 Subject: [PATCH 6/8] test(key-wallet-manager): share the standalone external address helper The rescan tests carried a private copy of the shared CoinJoin helper for BIP44 account 0. The shared helper now takes the account type, as `external_addresses`, and `coinjoin_external_addresses` calls it for CoinJoin account 0, so its callers are unchanged. Co-Authored-By: Claude Opus 5.5 --- key-wallet-manager/src/process_block.rs | 39 +++++++------------ .../test_utils/{coinjoin.rs => addresses.rs} | 39 +++++++++++++++---- key-wallet-manager/src/test_utils/mod.rs | 4 +- 3 files changed, 48 insertions(+), 34 deletions(-) rename key-wallet-manager/src/test_utils/{coinjoin.rs => addresses.rs} (54%) diff --git a/key-wallet-manager/src/process_block.rs b/key-wallet-manager/src/process_block.rs index b443d1330..099687e36 100644 --- a/key-wallet-manager/src/process_block.rs +++ b/key-wallet-manager/src/process_block.rs @@ -730,7 +730,7 @@ mod tests { use super::*; use crate::matching::{check_compact_filters_for_elements, FilterMatchKey}; use crate::test_helpers::*; - use crate::test_utils::coinjoin_external_addresses; + use crate::test_utils::{coinjoin_external_addresses, external_addresses}; use dashcore::bip158::BlockFilter; use dashcore::block::{Header, Version}; use dashcore::pow::CompactTarget; @@ -738,6 +738,7 @@ mod tests { BlockHash, Network, OutPoint, ScriptBuf, TxIn, TxMerkleNode, TxOut, Txid, Witness, }; use key_wallet::account::ManagedAccountTrait as _; + use key_wallet::account::StandardAccountType; use key_wallet::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; use key_wallet::managed_account::address_pool::AddressPoolType; use key_wallet::managed_account::managed_account_type::ManagedAccountType; @@ -746,7 +747,7 @@ mod tests { use key_wallet::mnemonic::Language; use key_wallet::wallet::initialization::WalletAccountCreationOptions; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; - use key_wallet::{AccountType, AddressPool, KeySource, Mnemonic}; + use key_wallet::{AccountType, AddressPool, Mnemonic}; use std::collections::HashMap; const HEIGHT: CoreBlockHeight = 100; @@ -788,25 +789,6 @@ mod tests { } } - /// The BIP44 account 0 external addresses at `indices`, derived on a copy of the - /// pool so the wallet's own pool stays as it is. - fn external_addresses( - manager: &WalletManager, - wallet_id: &WalletId, - indices: &[u32], - ) -> Vec
{ - let wallet = manager.get_wallet(wallet_id).expect("wallet"); - let xpub = wallet.get_bip44_account(0).expect("BIP44 account 0").account_xpub; - let mut pool = external_pool(manager, wallet_id); - indices - .iter() - .map(|&index| { - pool.generate_up_to(index, &KeySource::Public(xpub)).expect("derives"); - pool.address_at_index(index).expect("derived") - }) - .collect() - } - /// `late` pays index 0 and an index past the window. `extending` pays inside the /// window and extends it over that index. In a later block than `late`, it leaves /// `late`'s far output to the filter rescan, which delivers `late`'s block again. @@ -815,10 +797,17 @@ mod tests { wallet_id: &WalletId, ) -> (Transaction, Transaction) { let gap = external_pool(manager, wallet_id).gap_limit; - let late = - create_tx_paying(&external_addresses(manager, wallet_id, &[0, gap + gap / 3]), 0x51); - let extending = - create_tx_paying(&external_addresses(manager, wallet_id, &[gap * 2 / 3]), 0x52); + let far = gap + gap / 3; + let bip44 = AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }; + let addresses = external_addresses(manager, wallet_id, bip44, far + 1); + let at = |indices: &[u32]| -> Vec
{ + indices.iter().map(|&i| addresses[i as usize].clone()).collect() + }; + let late = create_tx_paying(&at(&[0, far]), 0x51); + let extending = create_tx_paying(&at(&[gap * 2 / 3]), 0x52); (late, extending) } diff --git a/key-wallet-manager/src/test_utils/coinjoin.rs b/key-wallet-manager/src/test_utils/addresses.rs similarity index 54% rename from key-wallet-manager/src/test_utils/coinjoin.rs rename to key-wallet-manager/src/test_utils/addresses.rs index 474ce5eb2..2e153cecf 100644 --- a/key-wallet-manager/src/test_utils/coinjoin.rs +++ b/key-wallet-manager/src/test_utils/addresses.rs @@ -1,26 +1,34 @@ use crate::{WalletId, WalletManager}; use dashcore::Address; -use key_wallet::account::ManagedAccountTrait; +use key_wallet::account::{AccountType, ManagedAccountTrait}; use key_wallet::managed_account::address_pool::{AddressPool, AddressPoolType, KeySource}; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; -/// Returns the first `count` External addresses of the wallet's CoinJoin account 0. +/// Returns the first `count` External addresses of the wallet's `account_type` account. /// They come from a standalone pool over the account xpub, so they can lie beyond /// the wallet's watch window without changing its state. -pub fn coinjoin_external_addresses( +pub fn external_addresses( manager: &WalletManager, wallet_id: &WalletId, + account_type: AccountType, count: u32, ) -> Vec
{ let wallet = manager.get_wallet(wallet_id).expect("wallet present"); - let xpub = wallet.accounts.coinjoin_accounts.get(&0).expect("CoinJoin account 0").account_xpub; + let xpub = wallet + .accounts + .account_of_type(account_type) + .unwrap_or_else(|| panic!("{account_type} account")) + .account_xpub; let info = manager.get_wallet_info(wallet_id).expect("wallet info present"); - let account = info.accounts.coinjoin_accounts.get(&0).expect("managed CoinJoin account 0"); + let account = info + .accounts + .funds_account(&account_type) + .unwrap_or_else(|| panic!("managed {account_type} account")); let pools = account.managed_account_type().address_pools(); let live = pools .iter() .find(|p| p.pool_type == AddressPoolType::External) - .expect("CoinJoin External pool"); + .unwrap_or_else(|| panic!("{account_type} External pool")); let pool = AddressPool::new( live.base_path.clone(), AddressPoolType::External, @@ -28,6 +36,23 @@ pub fn coinjoin_external_addresses( wallet.network, &KeySource::Public(xpub), ) - .expect("derive standalone CoinJoin External pool"); + .expect("derive standalone External pool"); (0..count).map(|i| pool.address_at_index(i).expect("address generated")).collect() } + +/// Returns the first `count` External addresses of the wallet's CoinJoin account 0, +/// as [`external_addresses`] does. +pub fn coinjoin_external_addresses( + manager: &WalletManager, + wallet_id: &WalletId, + count: u32, +) -> Vec
{ + external_addresses( + manager, + wallet_id, + AccountType::CoinJoin { + index: 0, + }, + count, + ) +} diff --git a/key-wallet-manager/src/test_utils/mod.rs b/key-wallet-manager/src/test_utils/mod.rs index d1f4bc21c..afcae569c 100644 --- a/key-wallet-manager/src/test_utils/mod.rs +++ b/key-wallet-manager/src/test_utils/mod.rs @@ -1,7 +1,7 @@ -mod coinjoin; +mod addresses; mod mock_wallet; -pub use coinjoin::coinjoin_external_addresses; +pub use addresses::{coinjoin_external_addresses, external_addresses}; pub use mock_wallet::MockWallet; pub use mock_wallet::MockWalletState; pub use mock_wallet::MultiMockWallet; From 0bf07baa8935bd4f8d20384418d1e35eeccfb598 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 20:40:55 +0000 Subject: [PATCH 7/8] test(key-wallet-manager): read the BIP44 gap limit from the account late_and_extending only needs the external gap limit, so take it from `external_gap_limit()` and drop the `external_pool` clone. Co-Authored-By: Claude Opus 5.5 --- key-wallet-manager/src/process_block.rs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/key-wallet-manager/src/process_block.rs b/key-wallet-manager/src/process_block.rs index 099687e36..63185d62f 100644 --- a/key-wallet-manager/src/process_block.rs +++ b/key-wallet-manager/src/process_block.rs @@ -741,13 +741,12 @@ mod tests { use key_wallet::account::StandardAccountType; use key_wallet::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; use key_wallet::managed_account::address_pool::AddressPoolType; - use key_wallet::managed_account::managed_account_type::ManagedAccountType; use key_wallet::managed_account::managed_core_funds_account::ManagedCoreFundsAccount; use key_wallet::managed_account::transaction_record::OutputRole; use key_wallet::mnemonic::Language; use key_wallet::wallet::initialization::WalletAccountCreationOptions; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; - use key_wallet::{AccountType, AddressPool, Mnemonic}; + use key_wallet::{AccountType, Mnemonic}; use std::collections::HashMap; const HEIGHT: CoreBlockHeight = 100; @@ -776,19 +775,6 @@ mod tests { .expect("BIP44 account 0") } - fn external_pool( - manager: &WalletManager, - wallet_id: &WalletId, - ) -> AddressPool { - match account(manager, wallet_id).managed_account_type() { - ManagedAccountType::Standard { - external_addresses, - .. - } => external_addresses.clone(), - other => panic!("BIP44 account 0 has an unexpected type {other:?}"), - } - } - /// `late` pays index 0 and an index past the window. `extending` pays inside the /// window and extends it over that index. In a later block than `late`, it leaves /// `late`'s far output to the filter rescan, which delivers `late`'s block again. @@ -796,7 +782,8 @@ mod tests { manager: &WalletManager, wallet_id: &WalletId, ) -> (Transaction, Transaction) { - let gap = external_pool(manager, wallet_id).gap_limit; + let gap = + account(manager, wallet_id).external_gap_limit().expect("BIP44 external gap limit"); let far = gap + gap / 3; let bip44 = AccountType::Standard { index: 0, From 440cf83ccb74dc23eccfe19fdb3c11c20170b27b Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 20:59:10 +0000 Subject: [PATCH 8/8] test(key-wallet-manager): build spend_first_output_of on spending Both helpers built a version 2 spender with default inputs; let spend_first_output_of start from `spending` and set the output it needs. The transactions it builds are byte-identical. Co-Authored-By: Claude Opus 5.5 --- key-wallet-manager/src/process_block.rs | 26 ++++++------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/key-wallet-manager/src/process_block.rs b/key-wallet-manager/src/process_block.rs index 63185d62f..0a9ab07f6 100644 --- a/key-wallet-manager/src/process_block.rs +++ b/key-wallet-manager/src/process_block.rs @@ -887,26 +887,12 @@ mod tests { } fn spend_first_output_of(tx: &Transaction) -> Transaction { - Transaction { - version: 2, - lock_time: 0, - input: vec![TxIn { - previous_output: OutPoint { - txid: tx.txid(), - vout: 0, - }, - script_sig: ScriptBuf::new(), - sequence: u32::MAX, - witness: Witness::default(), - }], - output: vec![TxOut { - value: tx.output[0].value, - script_pubkey: ScriptBuf::new_p2pkh(&dashcore::PubkeyHash::from_byte_array( - [0x77; 20], - )), - }], - special_transaction_payload: None, - } + let mut spend = spending([OutPoint::new(tx.txid(), 0)]); + spend.output[0] = TxOut { + value: tx.output[0].value, + script_pubkey: ScriptBuf::new_p2pkh(&dashcore::PubkeyHash::from_byte_array([0x77; 20])), + }; + spend } fn make_block(txdata: Vec) -> Block {