diff --git a/key-wallet/src/managed_account/managed_core_funds_account.rs b/key-wallet/src/managed_account/managed_core_funds_account.rs index 3bd0f96a8..d4c556ae5 100644 --- a/key-wallet/src/managed_account/managed_core_funds_account.rs +++ b/key-wallet/src/managed_account/managed_core_funds_account.rs @@ -267,6 +267,99 @@ impl ManagedCoreFundsAccount { } } + /// Take in `output`, an output paying one of this account's addresses, + /// the same way whether a processed transaction or + /// `ManagedWalletInfo::restore_utxos` brings it, and return whether it was + /// credited to `utxos`. + /// + /// An output already known spent is held back instead, as given: under + /// its spender while this account has an unsettled record spending it, + /// so a fork dropping the spend gives it back; in `spent_before_funded` + /// when the spend was observed in an earlier-processed block + /// (`observed_spent`, dashpay/rust-dashcore#649) or it is already held + /// there. When `spent_claims` is given, an output it names that this + /// account does not already hold is kept in `claim_guarded_outputs`. + /// + /// A credited output first gets its context flags from `credit`. It then + /// keeps the InstantSend lock and trust of any copy it replaces, the + /// account's own or one of `copies` another account held, and takes their + /// coin lock in place of its own, and it leaves `claim_guarded_outputs`. + pub(crate) fn take_in_output( + &mut self, + mut output: Utxo, + mut copies: Vec, + observed_spent: &BTreeMap, + spent_claims: Option<&BTreeMap>>, + credit: impl FnOnce(&mut Utxo), + ) -> bool { + let outpoint = output.outpoint; + // Check if this outpoint was already spent by a transaction we've seen. + // This handles out-of-order block processing during rescan where a + // spending transaction at a higher height may be processed before + // the transaction that created the UTXO. + // TODO: This is mostly needed for wallet rescan from storage with the + // there is a timing issue with event processing which might lead to + // invalid UTXO set / balances. There might be a way around it. + if self.is_outpoint_spent(&outpoint) { + tracing::debug!( + outpoint = %outpoint, + "Skipping UTXO already spent by previously processed transaction" + ); + // Held under its spend, so a fork dropping the spend + // gives it back. + if let Some(spender) = self.unsettled_spender_of(&outpoint) { + self.unsettled_spends.entry(spender).or_default().push(output); + } + return false; + } + + // #649 spend-first ordering: the spend was observed in an + // earlier-processed block, so this output is genuinely spent + // on-chain even though this account has never seen it before — + // never insert it, so the record built below is born correct. + if observed_spent.contains_key(&outpoint) + || self.spent_before_funded.contains_key(&outpoint) + { + tracing::debug!( + outpoint = %outpoint, + "Skipping UTXO already observed spent in an earlier-processed block (#649)" + ); + self.spent_before_funded.insert(outpoint, output); + return false; + } + + // Held back by a claim alone, so its spender is still to be + // recorded: keep the output to resolve that input. Checked + // after the observed spends, whose replay of the spending + // block depends on `spent_before_funded`. A coin already + // held is past guarding and goes on following its funding. + if spent_claims.is_some_and(|claims| claims.contains_key(&outpoint)) + && !self.utxos.contains_key(&outpoint) + { + tracing::debug!(outpoint = %outpoint, "Skipping UTXO guarded by a spent-output claim"); + self.claim_guarded_outputs.insert(outpoint, output); + return false; + } + + credit(&mut output); + // Reprocessing (e.g. mempool→block) rebuilds this UTXO from + // scratch, so carry forward flags that must not regress. An + // InstantSend lock is permanent for a txid (DIP-0010) and + // trust only ever settles, so both latch monotonically. A + // coin reservation is orthogonal to chain context and is kept + // as-is. `is_confirmed` stays freshly derived so a reorg can + // still downgrade it. + copies.extend(self.utxos.get(&outpoint).cloned()); + if !copies.is_empty() { + output.is_instantlocked |= copies.iter().any(|copy| copy.is_instantlocked); + output.is_trusted |= copies.iter().any(|copy| copy.is_trusted); + output.is_locked = copies.iter().any(|copy| copy.is_locked); + } + self.claim_guarded_outputs.remove(&outpoint); + self.utxos.insert(outpoint, output); + true + } + /// Add new UTXOs for received outputs, remove spent ones. /// /// Skips any output whose outpoint is already in `observed_spent` — it is @@ -378,83 +471,6 @@ impl ManagedCoreFundsAccount { vout: vout as u32, }; - // Check if this outpoint was already spent by a transaction we've seen. - // This handles out-of-order block processing during rescan where a - // spending transaction at a higher height may be processed before - // the transaction that created the UTXO. - // TODO: This is mostly needed for wallet rescan from storage with the - // there is a timing issue with event processing which might lead to - // invalid UTXO set / balances. There might be a way around it. - if self.is_outpoint_spent(&outpoint) { - tracing::debug!( - outpoint = %outpoint, - "Skipping UTXO already spent by previously processed transaction" - ); - // Held under its spend, so a fork dropping the spend - // gives it back. - if let Some(spender) = self.unsettled_spender_of(&outpoint) { - self.unsettled_spends.entry(spender).or_default().push( - Utxo::new( - outpoint, - output.clone(), - addr.clone(), - context.block_info().map_or(0, |i| i.height), - tx.is_coin_base(), - ), - ); - } - continue; - } - - // #649 spend-first ordering: the spend was observed in an - // earlier-processed block, so this output is genuinely spent - // on-chain even though this account has never seen it before — - // never insert it, so the record built below is born correct. - if observed_spent.contains_key(&outpoint) - || self.spent_before_funded.contains_key(&outpoint) - { - tracing::debug!( - outpoint = %outpoint, - "Skipping UTXO already observed spent in an earlier-processed block (#649)" - ); - self.spent_before_funded.insert( - outpoint, - Utxo::new( - outpoint, - output.clone(), - addr.clone(), - context.block_info().map_or(0, |i| i.height), - tx.is_coin_base(), - ), - ); - continue; - } - - // Held back by a claim alone, so its spender is still to be - // recorded: keep the output to resolve that input. Checked - // after the observed spends, whose replay of the spending - // block depends on `spent_before_funded`. A coin already - // held is past guarding and goes on following its funding. - if spent_claims.contains_key(&outpoint) - && !self.utxos.contains_key(&outpoint) - { - tracing::debug!( - outpoint = %outpoint, - "Skipping UTXO guarded by a spent-output claim" - ); - self.claim_guarded_outputs.insert( - outpoint, - Utxo::new( - outpoint, - output.clone(), - addr.clone(), - context.block_info().map_or(0, |i| i.height), - tx.is_coin_base(), - ), - ); - continue; - } - // Flag outputs from a "trusted" mempool transaction we created — // one whose inputs all spend our own final UTXOs and which pays // this output back to one of our internal (change) addresses. @@ -463,31 +479,25 @@ impl ManagedCoreFundsAccount { // before the parent transaction settles. let is_trusted_output = all_inputs_final_and_ours && change_addrs.contains(&addr); - let txout = dashcore::TxOut { - value: output.value, - script_pubkey: output.script_pubkey.clone(), - }; - let block_height = context.block_info().map_or(0, |info| info.height); - let mut utxo = - Utxo::new(outpoint, txout, addr, block_height, tx.is_coin_base()); - utxo.is_confirmed = context.confirmed(); - utxo.is_instantlocked = - matches!(context, TransactionContext::InstantSend(_)); - utxo.is_trusted = is_trusted_output; - // Reprocessing (e.g. mempool→block) rebuilds this UTXO from - // scratch, so carry forward flags that must not regress. An - // InstantSend lock is permanent for a txid (DIP-0010) and - // trust only ever settles, so both latch monotonically. A - // coin reservation is orthogonal to chain context and is kept - // as-is. `is_confirmed` stays freshly derived so a reorg can - // still downgrade it. - if let Some(prior) = self.utxos.get(&outpoint) { - utxo.is_instantlocked |= prior.is_instantlocked; - utxo.is_trusted |= prior.is_trusted; - utxo.is_locked = prior.is_locked; - } - self.utxos.insert(outpoint, utxo); - utxos_changed = true; + let received = Utxo::new( + outpoint, + output.clone(), + addr, + context.block_info().map_or(0, |info| info.height), + tx.is_coin_base(), + ); + utxos_changed |= self.take_in_output( + received, + Vec::new(), + observed_spent, + Some(spent_claims), + |utxo| { + utxo.is_confirmed = context.confirmed(); + utxo.is_instantlocked = + matches!(context, TransactionContext::InstantSend(_)); + utxo.is_trusted = is_trusted_output; + }, + ); } } } diff --git a/key-wallet/src/tests/mod.rs b/key-wallet/src/tests/mod.rs index bcc1f4cda..0481a7a3b 100644 --- a/key-wallet/src/tests/mod.rs +++ b/key-wallet/src/tests/mod.rs @@ -30,6 +30,8 @@ mod performance_tests; mod provider_key_derivation_tests; +mod restore_utxos_tests; + mod special_transaction_matching_tests; mod special_transaction_tests; diff --git a/key-wallet/src/tests/restore_utxos_tests.rs b/key-wallet/src/tests/restore_utxos_tests.rs new file mode 100644 index 000000000..3eedfe4ec --- /dev/null +++ b/key-wallet/src/tests/restore_utxos_tests.rs @@ -0,0 +1,384 @@ +//! `ManagedWalletInfo::restore_utxos` places each UTXO in the account that +//! owns its address (#1294). + +use dashcore::hashes::Hash; +use dashcore::{Address, BlockHash, OutPoint, Transaction}; +use test_case::test_case; + +use crate::account::AccountType; +use crate::managed_account::address_pool::AddressPool; +use crate::managed_account::managed_account_trait::ManagedAccountTrait; +use crate::test_utils::TestWalletContext; +use crate::transaction_checking::{BlockInfo, TransactionContext}; +use crate::wallet::managed_wallet_info::managed_account_operations::ManagedAccountOperations; +use crate::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; +use crate::{KeySource, ManagedAccountType, Utxo}; + +/// The external pool of CoinJoin account 0. +fn coinjoin_pool(ctx: &TestWalletContext) -> &AddressPool { + match ctx.managed_wallet.accounts.coinjoin_accounts[&0].managed_account_type() { + ManagedAccountType::CoinJoin { + external_addresses, + .. + } => external_addresses, + _ => unreachable!("a CoinJoin account carries the CoinJoin managed type"), + } +} + +/// The CoinJoin account 0 external address at `index`, derived on a copy of +/// the pool so the wallet's own pool stays as it is. +fn coinjoin_address(ctx: &TestWalletContext, index: u32) -> Address { + let xpub = ctx.wallet.get_coinjoin_account(0).expect("CoinJoin account").account_xpub; + let mut pool = coinjoin_pool(ctx).clone(); + pool.generate_up_to(index, &KeySource::Public(xpub)).expect("derives"); + pool.address_at_index(index).expect("derived") +} + +/// A confirmed UTXO paying `value` to `address`, with its funding transaction. +fn funded(address: &Address, input: u8, value: u64) -> (Transaction, Utxo) { + let tx = Transaction::dummy(address, input..input + 1, &[value]); + let mut utxo = + Utxo::new(OutPoint::new(tx.txid(), 0), tx.output[0].clone(), address.clone(), 1_000, false); + utxo.is_confirmed = true; + (tx, utxo) +} + +/// Add the DashPay receiving account of a friendship and the reverse +/// external account a wallet holding both identities also has. Both derive +/// the same addresses. Returns their account types. +fn befriend_both_ways(ctx: &mut TestWalletContext) -> (AccountType, AccountType) { + let (us, them) = ([0xaa; 32], [0xbb; 32]); + let receiving = AccountType::DashpayReceivingFunds { + index: 0, + user_identity_id: us, + friend_identity_id: them, + }; + let reverse_external = AccountType::DashpayExternalAccount { + index: 0, + user_identity_id: them, + friend_identity_id: us, + }; + for contact in [receiving, reverse_external] { + ctx.wallet.add_account(contact, None).expect("contact account is addable"); + ctx.managed_wallet + .add_managed_account(&ctx.wallet, contact) + .expect("contact account is manageable"); + } + (receiving, reverse_external) +} + +fn receiving_pool(ctx: &TestWalletContext) -> &AddressPool { + let account = ctx.managed_wallet.accounts.dashpay_receival_accounts.values().next(); + account.expect("receiving account").managed_account_type().address_pools()[0] +} + +fn held_by_receiving(ctx: &TestWalletContext, outpoint: &OutPoint) -> bool { + let accounts = &ctx.managed_wallet.accounts.dashpay_receival_accounts; + accounts.values().any(|account| account.utxos.contains_key(outpoint)) +} + +fn held_by_contact(ctx: &TestWalletContext, outpoint: &OutPoint) -> bool { + let accounts = &ctx.managed_wallet.accounts.dashpay_external_accounts; + accounts.values().any(|account| account.utxos.contains_key(outpoint)) +} + +fn held_by_any_funds_account(ctx: &TestWalletContext, outpoint: &OutPoint) -> bool { + ctx.managed_wallet + .accounts + .all_funding_accounts() + .iter() + .any(|account| account.utxos.contains_key(outpoint)) +} + +/// A CoinJoin address first derived during a sync is beyond the window a +/// fresh wallet holds. Its UTXO restores into CoinJoin once the UTXO at the +/// window's last address marks that address used, not into BIP44, and a +/// rescan of its funding transaction counts it once. +#[tokio::test] +async fn a_coinjoin_utxo_past_the_stored_window_restores_into_coinjoin() { + let mut ctx = TestWalletContext::new_random(); + let gap = coinjoin_pool(&ctx).gap_limit; + assert_eq!(coinjoin_pool(&ctx).highest_generated, Some(gap - 1)); + let (_, inside) = funded(&coinjoin_address(&ctx, gap - 1), 0, 100_000); + let (beyond_tx, beyond) = funded(&coinjoin_address(&ctx, 2 * gap - 1), 1, 200_000); + + let unmatched = ctx + .managed_wallet + .restore_utxos(&ctx.wallet, [beyond.clone(), inside.clone()]) + .expect("restores"); + + assert!(unmatched.is_empty()); + let accounts = &ctx.managed_wallet.accounts; + for utxo in [&inside, &beyond] { + assert!(accounts.coinjoin_accounts[&0].utxos.contains_key(&utxo.outpoint)); + assert!(!accounts.standard_bip44_accounts[&0].utxos.contains_key(&utxo.outpoint)); + } + + let block = TransactionContext::InBlock(BlockInfo::new( + 1_000, + BlockHash::from_byte_array([0x10; 32]), + 1_700_000_000, + )); + ctx.check_transaction(&beyond_tx, block).await; + assert_eq!(ctx.managed_wallet.balance.total(), 300_000); +} + +/// A UTXO no account's pools reach comes back to the caller and is placed in +/// no account. +#[test_case(false; "foreign script")] +#[test_case(true; "coinjoin address past the reach")] +fn a_utxo_matching_no_account_comes_back(ours_but_unreachable: bool) { + let mut ctx = TestWalletContext::new_random(); + let address = if ours_but_unreachable { + let gap = coinjoin_pool(&ctx).gap_limit; + coinjoin_address(&ctx, 2 * gap) + } else { + TestWalletContext::new_random().receive_address + }; + let (_, utxo) = funded(&address, 0, 100_000); + + let unmatched = + ctx.managed_wallet.restore_utxos(&ctx.wallet, [utxo.clone()]).expect("restores"); + + assert_eq!(unmatched, std::slice::from_ref(&utxo)); + assert!(!held_by_any_funds_account(&ctx, &utxo.outpoint)); +} + +/// A wallet holding both identities of a friendship derives the same +/// addresses for its receiving chain and for the reverse external chain. A +/// UTXO at such an address restores into the receiving account, whose coins +/// are the wallet's. +#[test] +fn a_utxo_both_a_receiving_and_a_contact_account_hold_restores_into_the_receiving_one() { + let mut ctx = TestWalletContext::new_random(); + befriend_both_ways(&mut ctx); + let address = receiving_pool(&ctx).address_at_index(0).expect("first address"); + let external = ctx.managed_wallet.accounts.dashpay_external_accounts.values().next(); + assert!(external.expect("external account").contains_script_pub_key(&address.script_pubkey())); + let (_, utxo) = funded(&address, 0, 29_000); + + let unmatched = + ctx.managed_wallet.restore_utxos(&ctx.wallet, [utxo.clone()]).expect("restores"); + + assert!(unmatched.is_empty()); + assert!(held_by_receiving(&ctx, &utxo.outpoint)); + assert!(!held_by_contact(&ctx, &utxo.outpoint)); +} + +/// The contact account's pool, derived further, holds a script from the +/// start, while the receiving account reaches it only once the UTXO at the +/// end of its window marks that address used. The UTXO still restores into +/// the receiving account. +#[test] +fn a_receiving_pool_that_reaches_a_script_in_a_later_pass_claims_it() { + let mut ctx = TestWalletContext::new_random(); + let (receiving, reverse_external) = befriend_both_ways(&mut ctx); + let pool = receiving_pool(&ctx); + let last = pool.highest_generated.expect("an initial window"); + let far_index = last + pool.gap_limit; + let mut far_pool = pool.clone(); + far_pool + .generate_up_to(far_index, &ctx.wallet.key_source_for_account(receiving)) + .expect("derives"); + let edge = pool.address_at_index(last).expect("derived"); + let far = far_pool.address_at_index(far_index).expect("derived"); + let key_source = ctx.wallet.key_source_for_account(reverse_external); + let external = ctx.managed_wallet.accounts.dashpay_external_accounts.values_mut().next(); + let external_pools = external.expect("external account").managed_account_type_mut(); + let external_pool = external_pools.address_pools_mut().into_iter().next().expect("a pool"); + external_pool.generate_up_to(far_index, &key_source).expect("derives"); + assert!(external_pool.contains_address(&far)); + let (_, at_edge) = funded(&edge, 0, 10_000); + let (_, beyond) = funded(&far, 1, 20_000); + + let unmatched = ctx + .managed_wallet + .restore_utxos(&ctx.wallet, [beyond.clone(), at_edge.clone()]) + .expect("restores"); + + assert!(unmatched.is_empty()); + for utxo in [&at_edge, &beyond] { + assert!(held_by_receiving(&ctx, &utxo.outpoint)); + assert!(!held_by_contact(&ctx, &utxo.outpoint)); + } +} + +/// A pool already marked used up to the end of its window, as restored pool +/// state can leave it, is extended before matching, so a UTXO one gap past +/// that address restores without a UTXO at the used address. +#[test] +fn a_pool_restored_as_used_is_extended_before_matching() { + let mut ctx = TestWalletContext::new_random(); + let gap = coinjoin_pool(&ctx).gap_limit; + let used = coinjoin_address(&ctx, gap - 1); + ctx.managed_wallet.accounts.coinjoin_accounts.get_mut(&0).unwrap().mark_address_used(&used); + let (_, utxo) = funded(&coinjoin_address(&ctx, 2 * gap - 1), 0, 100_000); + + let unmatched = + ctx.managed_wallet.restore_utxos(&ctx.wallet, [utxo.clone()]).expect("restores"); + + assert!(unmatched.is_empty()); + assert!(ctx.managed_wallet.accounts.coinjoin_accounts[&0].utxos.contains_key(&utxo.outpoint)); +} + +/// A copy BIP44 already holds of a CoinJoin UTXO, as an earlier restore into +/// account 0 leaves it, is taken out when the UTXO is restored, so the +/// balance counts it once. +#[test] +fn a_copy_another_account_holds_is_taken_out_on_restore() { + let mut ctx = TestWalletContext::new_random(); + let (_, utxo) = funded(&coinjoin_address(&ctx, 0), 0, 100_000); + let bip44 = ctx.managed_wallet.accounts.standard_bip44_accounts.get_mut(&0).unwrap(); + bip44.utxos.insert(utxo.outpoint, utxo.clone()); + let bip44_revision = bip44.monitor_revision(); + + let unmatched = + ctx.managed_wallet.restore_utxos(&ctx.wallet, [utxo.clone()]).expect("restores"); + + assert!(unmatched.is_empty()); + let accounts = &ctx.managed_wallet.accounts; + assert!(accounts.coinjoin_accounts[&0].utxos.contains_key(&utxo.outpoint)); + assert!(!accounts.standard_bip44_accounts[&0].utxos.contains_key(&utxo.outpoint)); + assert!(accounts.standard_bip44_accounts[&0].monitor_revision() > bip44_revision); + ctx.managed_wallet.update_balance(); + assert_eq!(ctx.managed_wallet.balance.total(), 100_000); +} + +/// The restored UTXO keeps the InstantSend lock and trust of the copy it +/// takes out of BIP44, and takes the coin lock of the copies it replaces in +/// place of its own: locked if either the copy CoinJoin held or the one taken +/// out of BIP44 is locked. +#[test_case(true, true, true; "a held lock stays")] +#[test_case(false, false, false; "a held unlock stays")] +#[test_case(false, true, true; "a lock on the misplaced copy stays")] +fn a_restored_utxo_keeps_the_flags_of_the_copies_it_replaces( + held_locked: bool, + misplaced_locked: bool, + expected_locked: bool, +) { + let mut ctx = TestWalletContext::new_random(); + let (_, mut utxo) = funded(&coinjoin_address(&ctx, 0), 0, 100_000); + let accounts = &mut ctx.managed_wallet.accounts; + let mut held = utxo.clone(); + held.is_locked = held_locked; + accounts.coinjoin_accounts.get_mut(&0).unwrap().utxos.insert(utxo.outpoint, held); + let mut misplaced = utxo.clone(); + misplaced.is_instantlocked = true; + misplaced.is_trusted = true; + misplaced.is_locked = misplaced_locked; + accounts.standard_bip44_accounts.get_mut(&0).unwrap().utxos.insert(utxo.outpoint, misplaced); + utxo.is_locked = !(held_locked || misplaced_locked); + + ctx.managed_wallet.restore_utxos(&ctx.wallet, [utxo.clone()]).expect("restores"); + + let restored = &ctx.managed_wallet.accounts.coinjoin_accounts[&0].utxos[&utxo.outpoint]; + assert_eq!(restored.is_locked, expected_locked); + assert!(restored.is_instantlocked); + assert!(restored.is_trusted); +} + +enum SeenSpent { + InABlock, + BeforeFunded, + ByTheAccount, + ClaimOnly, +} + +/// An outpoint the wallet already saw spent is not credited or returned, as +/// when its funding transaction is processed. One spent in a processed block +/// is held as spent before funded; a spent-output claim alone does not hold +/// the UTXO back. +#[test_case(SeenSpent::InABlock, false, true; "spent in a processed block")] +#[test_case(SeenSpent::BeforeFunded, false, true; "held as spent before funded")] +#[test_case(SeenSpent::ByTheAccount, false, false; "spent by the owning account")] +#[test_case(SeenSpent::ClaimOnly, true, false; "only claimed spent")] +#[tokio::test] +async fn an_outpoint_already_seen_spent_is_not_credited( + seen: SeenSpent, + credited: bool, + held_as_spent_before_funded: bool, +) { + let mut ctx = TestWalletContext::new_random(); + let (_, utxo) = funded(&coinjoin_address(&ctx, 0), 0, 100_000); + let outpoint = utxo.outpoint; + match seen { + SeenSpent::InABlock => { + ctx.managed_wallet.observed_spent_outpoints.insert(outpoint, 1_001); + } + SeenSpent::BeforeFunded => { + let coinjoin = ctx.managed_wallet.accounts.coinjoin_accounts.get_mut(&0).unwrap(); + coinjoin.spent_before_funded.insert(outpoint, utxo.clone()); + } + SeenSpent::ByTheAccount => { + ctx.managed_wallet.restore_utxos(&ctx.wallet, [utxo.clone()]).expect("restores"); + let mut spend = Transaction::dummy(&coinjoin_address(&ctx, 1), 9..10, &[90_000]); + spend.input[0].previous_output = outpoint; + let block = TransactionContext::InBlock(BlockInfo::new( + 1_001, + BlockHash::from_byte_array([0x11; 32]), + 1_700_000_100, + )); + ctx.check_transaction(&spend, block).await; + assert!(!held_by_any_funds_account(&ctx, &outpoint)); + // Only the account's own record of the spend is left to guard it. + ctx.managed_wallet.observed_spent_outpoints.clear(); + } + SeenSpent::ClaimOnly => { + ctx.managed_wallet.spent_claims.insert(outpoint, None); + } + } + + let unmatched = + ctx.managed_wallet.restore_utxos(&ctx.wallet, [utxo.clone()]).expect("restores"); + + assert!(unmatched.is_empty()); + assert_eq!(held_by_any_funds_account(&ctx, &outpoint), credited); + let coinjoin = &ctx.managed_wallet.accounts.coinjoin_accounts[&0]; + assert_eq!(coinjoin.spent_before_funded.contains_key(&outpoint), held_as_spent_before_funded); +} + +/// A coin the owning account already recorded spending is held under that +/// spend, as when its funding transaction is processed, so a fork dropping +/// the spend gives the coin back. +#[tokio::test] +async fn a_restored_coin_whose_recorded_spend_a_fork_drops_comes_back() { + let mut ctx = TestWalletContext::new_random(); + let (_, coin) = funded(&ctx.receive_address, 0, 1_000_000); + let second_receive = ctx.managed_wallet.accounts.standard_bip44_accounts[&0] + .managed_account_type() + .address_pools()[0] + .address_at_index(1) + .expect("a second receive address"); + let mut spend = Transaction::dummy(&second_receive, 9..10, &[900_000]); + spend.input[0].previous_output = coin.outpoint; + let block = BlockInfo::new(1_001, BlockHash::from_byte_array([0x12; 32]), 1_700_000_200); + assert!(ctx.check_transaction(&spend, TransactionContext::InBlock(block)).await.is_relevant); + + ctx.managed_wallet.restore_utxos(&ctx.wallet, [coin.clone()]).expect("restores"); + assert!(!held_by_any_funds_account(&ctx, &coin.outpoint)); + + ctx.managed_wallet.truncate_above(1_000).expect("truncates"); + ctx.managed_wallet.update_balance(); + + assert!(held_by_any_funds_account(&ctx, &coin.outpoint)); + assert_eq!(ctx.managed_wallet.balance.total(), 1_000_000); +} + +/// A claimed coin whose funding was processed is kept aside for the claim. +/// Restoring it credits it and no longer keeps it aside, so it is never in +/// both. +#[tokio::test] +async fn a_restored_coin_kept_aside_for_a_claim_leaves_it() { + let mut ctx = TestWalletContext::new_random(); + let (funding, coin) = funded(&ctx.receive_address, 0, 1_000_000); + ctx.managed_wallet.restore_spent_outpoints(&[(coin.outpoint, None)]); + let block = BlockInfo::new(1_000, BlockHash::from_byte_array([0x13; 32]), 1_700_000_300); + ctx.check_transaction(&funding, TransactionContext::InBlock(block)).await; + assert!(ctx.bip44_account().claim_guarded_outputs.contains_key(&coin.outpoint)); + + ctx.managed_wallet.restore_utxos(&ctx.wallet, [coin.clone()]).expect("restores"); + + let bip44 = ctx.bip44_account(); + assert!(bip44.utxos.contains_key(&coin.outpoint)); + assert!(!bip44.claim_guarded_outputs.contains_key(&coin.outpoint)); +} diff --git a/key-wallet/src/wallet/managed_wallet_info/mod.rs b/key-wallet/src/wallet/managed_wallet_info/mod.rs index 4938ebec9..b75ea4cf7 100644 --- a/key-wallet/src/wallet/managed_wallet_info/mod.rs +++ b/key-wallet/src/wallet/managed_wallet_info/mod.rs @@ -344,11 +344,11 @@ impl ManagedWalletInfo { /// /// For a wallet loaded without the transactions that spent its coins. /// Each entry is a spent outpoint and, when known, the transaction that - /// spent it (its claimant). A restored outpoint is never added to the - /// `utxos` of any account that holds funds — a contact's watch-only - /// DashPay account and an account added after this call included — and - /// the guard survives ChainLock pruning. Outpoints this wallet does not - /// own are accepted. + /// spent it (its claimant). A transaction processed later does not + /// credit a restored outpoint to any account that holds funds — a + /// contact's watch-only DashPay account and an account added after this + /// call included — and the guard survives ChainLock pruning. Outpoints + /// this wallet does not own are accepted. /// /// **When to call:** once after each load, since claims are not part of /// wallet snapshots — before any transaction funding a restored outpoint @@ -356,6 +356,9 @@ impl ManagedWalletInfo { /// The guard only stops a later credit: a restored outpoint already in /// `utxos` stays credited and spendable until it leaves `utxos`. Those /// outpoints are returned, sorted, and their count is logged at `warn`. + /// A host that also restores UTXOs calls + /// [`restore_utxos`](Self::restore_utxos) first, so this call reports any + /// restored UTXO a claim names. /// /// **Claimants:** [`sweep_conflicts`](Self::sweep_conflicts) or /// [`abandon_transaction`](Self::abandon_transaction) removing the @@ -370,6 +373,8 @@ impl ManagedWalletInfo { /// * Only transactions processed by the wallet are guarded: /// `ManagedAccountRefMut::record_transaction` and `confirm_transaction` /// drive one account and credit a restored outpoint. + /// * [`restore_utxos`](Self::restore_utxos) does not consult claims: it + /// credits a restored UTXO a claim names, called before or after this. /// * A claim restored once the funding is held as a txid only — /// ChainLocked, without `keep-finalized-transactions` — leaves a later /// spender recorded as if the coin were not the wallet's. @@ -417,6 +422,112 @@ impl ManagedWalletInfo { still_held.into_iter().collect() } + /// Restore `utxos` the host stored, each into the funds account whose + /// address pools hold its script, and return the ones no account holds. + /// + /// Call it when restoring wallet state, before sync resumes: the pools + /// may grow, and blocks already processed are not scanned again for the + /// newly derived addresses. Mark the addresses the host stored as used + /// first: only used addresses extend the reach. Call it before + /// [`restore_spent_outpoints`](Self::restore_spent_outpoints), which then + /// reports any restored UTXO a claim names. + /// + /// Addresses are found the way a rescan finds them: each pool is extended + /// to its own `gap_limit` past its highest used address, the address of + /// each UTXO is marked used in every account that holds it, and this + /// repeats until no pool grows. Each UTXO then goes to the first account + /// that holds its script, trying the wallet's own accounts before DashPay + /// contact accounts, so for a script both hold the UTXO goes to the + /// wallet's account. A UTXO beyond that reach comes back unmatched rather + /// than placed in another account. Only processing its funding + /// transaction credits it then, and sync resumes at `synced_height + 1`, + /// so one at or below `synced_height` is credited only if the host + /// rewinds `synced_height` below its height. Accounts without a public + /// key source match only the addresses they already hold. + /// + /// A restored outpoint that another funds account already holds is taken + /// out of that account. The owning account then takes the UTXO in the same + /// way as when it processes the funding transaction, except that claims + /// are not consulted. The restored UTXO + /// keeps the InstantSend lock and trust of any copy it replaces or takes + /// out, and their coin lock replaces its own. A matched outpoint the + /// wallet already saw spent is neither credited nor returned: one the + /// owning account recorded spending is held under that spend, so a fork + /// dropping the spend gives it back, and one spent in a processed block + /// ([`observed_spent_outpoints`](Self::observed_spent_outpoints), or held + /// as spent before it was funded) is held as spent before funded. A + /// spent-output claim does not hold a restored UTXO back; a credited + /// UTXO is no longer kept aside for one. + /// + /// Each account whose pools grow, or that is credited a UTXO or loses one, + /// gets its monitor revision bumped, so a running client rebuilds its + /// filters. + /// + /// Every pool is extended before any UTXO is inserted, so an `Err` from + /// address derivation leaves the UTXO sets as they were; the addresses + /// derived and marked used by then stay. The balance is not recomputed: + /// the next [`update_balance`](WalletInfoInterface::update_balance) or + /// processed transaction reflects the restored UTXOs. + pub fn restore_utxos( + &mut self, + wallet: &Wallet, + utxos: impl IntoIterator, + ) -> crate::error::Result> { + let utxos: Vec = utxos.into_iter().collect(); + let network = self.network; + let mut accounts = funds_accounts_wallet_first(&mut self.accounts); + // Each UTXO's owner is the index in `accounts` of the first account + // holding its script. Every pass matches all UTXOs again after the + // pools grew, so an account tried earlier claims a UTXO once its pool + // reaches it. + let owners = loop { + let owners: Vec> = utxos + .iter() + .map(|utxo| { + let script = &utxo.txout.script_pubkey; + let address = Address::from_script(script, network).ok(); + let mut owner = None; + for (index, account) in accounts.iter_mut().enumerate() { + if account.contains_script_pub_key(script) { + if let Some(address) = &address { + account.mark_address_used(address); + } + owner.get_or_insert(index); + } + } + owner + }) + .collect(); + if !extend_pools(wallet, &mut accounts)? { + break owners; + } + }; + let mut unmatched = Vec::new(); + for (utxo, owner) in utxos.into_iter().zip(owners) { + let Some(owner) = owner else { + unmatched.push(utxo); + continue; + }; + let outpoint = utxo.outpoint; + let mut copies = Vec::new(); + for (index, account) in accounts.iter_mut().enumerate() { + if index != owner { + if let Some(copy) = account.utxos.remove(&outpoint) { + copies.push(copy); + account.bump_monitor_revision(); + } + } + } + let account = &mut accounts[owner]; + let credited = + account.take_in_output(utxo, copies, &self.observed_spent_outpoints, None, |_| {}); + if credited { + account.bump_monitor_revision(); + } + } + Ok(unmatched) + } + /// Record every outpoint `tx` spends into [`Self::observed_spent_outpoints`] /// at `height`. Insert-only bookkeeping — it never touches account UTXO sets, /// so it is safe to call before `record_transaction` builds a spend's @@ -640,6 +751,54 @@ impl ManagedWalletInfo { } } +/// Extend every pool of `accounts` that has a public key source to its gap +/// limit, bumping the monitor revision of each account that grew. Returns +/// whether any pool grew. +fn extend_pools( + wallet: &Wallet, + accounts: &mut [&mut crate::managed_account::ManagedCoreFundsAccount], +) -> crate::error::Result { + let mut any_grew = false; + for account in accounts.iter_mut() { + let key_source = + wallet.key_source_for_account(account.managed_account_type().to_account_type()); + if matches!(key_source, crate::KeySource::NoKeySource) { + continue; + } + let mut grew = false; + for pool in account.managed_account_type_mut().address_pools_mut() { + grew |= !pool.maintain_gap_limit(&key_source)?.is_empty(); + } + if grew { + account.bump_monitor_revision(); + any_grew = true; + } + } + Ok(any_grew) +} + +/// The wallet's own funds accounts, in [`ManagedAccountCollection::all_funding_accounts`] +/// order, then the DashPay contact accounts. +fn funds_accounts_wallet_first( + accounts: &mut ManagedAccountCollection, +) -> Vec<&mut crate::managed_account::ManagedCoreFundsAccount> { + let ManagedAccountCollection { + standard_bip44_accounts, + standard_bip32_accounts, + coinjoin_accounts, + dashpay_receival_accounts, + dashpay_external_accounts, + .. + } = accounts; + standard_bip44_accounts + .values_mut() + .chain(standard_bip32_accounts.values_mut()) + .chain(coinjoin_accounts.values_mut()) + .chain(dashpay_receival_accounts.values_mut()) + .chain(dashpay_external_accounts.values_mut()) + .collect() +} + /// Re-export types from account module for convenience pub use crate::account::TransactionRecord; pub use crate::utxo::Utxo;