From 5a3610ad2d5e7fab6a6a0f1ca9497a011effec21 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 3 Nov 2026 02:47:16 +0000 Subject: [PATCH 1/6] fix(key-wallet): keep a funds account's output out of the other funds accounts A host can leave an outpoint in a funds account that does not own its address, for example a restore that loads a CoinJoin output into BIP44. When the owning account then records the transaction (a rescan), both accounts hold the outpoint and the wallet balance, a sum of the account balances, counts it twice. After each account records or confirms a transaction, take every output that pays one of its own addresses and that it holds out of the wallet's other funding accounts (BIP44, BIP32, CoinJoin, DashPay receiving). The removed copy's InstantSend-lock, trusted and locked flags carry over to the owner's copy. A DashPay external account holds a contact's coins and takes nothing from the wallet's own accounts. Co-Authored-By: Claude Opus 5.5 --- .../managed_account_collection.rs | 52 ++++++++++++++++ .../tests/dashpay_contact_gap_limit_tests.rs | 28 ++++++++- .../transaction_checking/wallet_checker.rs | 60 +++++++++++++++++++ 3 files changed, 139 insertions(+), 1 deletion(-) diff --git a/key-wallet/src/managed_account/managed_account_collection.rs b/key-wallet/src/managed_account/managed_account_collection.rs index 51742dd52..c99e679dd 100644 --- a/key-wallet/src/managed_account/managed_account_collection.rs +++ b/key-wallet/src/managed_account/managed_account_collection.rs @@ -1054,6 +1054,58 @@ impl ManagedAccountCollection { accounts } + /// Removes each output of `tx` that pays one of the `owner` funds + /// account's addresses and that `owner` holds from the other accounts of + /// [`Self::all_funding_accounts`] (a host can put a copy there), so the + /// wallet counts it once. The removed copy's `is_instantlocked`, + /// `is_trusted` and `is_locked` flags carry over to `owner`'s copy. An + /// account holding a contact's coins takes nothing from the others. + pub(crate) fn settle_outputs_held_elsewhere( + &mut self, + owner: &CoreAccountTypeMatch, + tx: &Transaction, + ) { + let Some(ManagedAccountRef::Funds(owner_account)) = self.get_by_account_type_match(owner) + else { + return; + }; + let owner_type = owner_account.managed_account_type().to_account_type(); + if owner_type.is_contact_owned() { + return; + } + let txid = tx.txid(); + let held: Vec = tx + .output + .iter() + .zip(0u32..) + .filter(|(output, _)| owner_account.contains_script_pub_key(&output.script_pubkey)) + .map(|(_, vout)| OutPoint::new(txid, vout)) + .filter(|outpoint| owner_account.utxos.contains_key(outpoint)) + .collect(); + if held.is_empty() { + return; + } + let mut removed = Vec::new(); + for account in self.all_funding_accounts_mut() { + if account.managed_account_type().to_account_type() == owner_type { + continue; + } + removed.extend(held.iter().filter_map(|outpoint| account.utxos.remove(outpoint))); + } + let Some(ManagedAccountRefMut::Funds(owner_account)) = + self.get_by_account_type_match_mut(owner) + else { + return; + }; + for prior in removed { + if let Some(utxo) = owner_account.utxos.get_mut(&prior.outpoint) { + utxo.is_instantlocked |= prior.is_instantlocked; + utxo.is_trusted |= prior.is_trusted; + utxo.is_locked |= prior.is_locked; + } + } + } + /// Get the count of accounts pub fn count(&self) -> usize { self.all_accounts().len() diff --git a/key-wallet/src/tests/dashpay_contact_gap_limit_tests.rs b/key-wallet/src/tests/dashpay_contact_gap_limit_tests.rs index e4511f16a..437aea17c 100644 --- a/key-wallet/src/tests/dashpay_contact_gap_limit_tests.rs +++ b/key-wallet/src/tests/dashpay_contact_gap_limit_tests.rs @@ -1,4 +1,4 @@ -//! Regression test for DIP-15 contact address pool maintenance (issue #1032). +//! Regression tests for DIP-15 contact address pools (issues #1032 and #1294). use dashcore::hashes::Hash; use dashcore::{Address, BlockHash, Transaction}; @@ -109,3 +109,29 @@ async fn contact_payments_extend_the_contact_pool() { ); } } + +/// A wallet holding both identities of a friendship derives the same +/// addresses for its receiving chain and for the reverse external chain. A +/// payment to one of them stays in the receiving account, whose coins are +/// the wallet's, and counts in the balance. +#[tokio::test] +async fn a_payment_the_reverse_external_chain_also_matches_stays_ours() { + let mut ctx = TestWalletContext::new_random(); + let receiving = CONTACT_CHAINS[0]; + 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"); + } + let address = contact_pool(&ctx, receiving).address_at_index(0).expect("first address"); + assert_eq!(contact_pool(&ctx, reverse_external).address_at_index(0), Some(address.clone())); + + assert!(pay(&mut ctx, &address, 1_555_196).await); + assert_eq!(ctx.managed_wallet.balance.total(), 29_000); +} diff --git a/key-wallet/src/transaction_checking/wallet_checker.rs b/key-wallet/src/transaction_checking/wallet_checker.rs index 168534e47..fbce51f0a 100644 --- a/key-wallet/src/transaction_checking/wallet_checker.rs +++ b/key-wallet/src/transaction_checking/wallet_checker.rs @@ -356,6 +356,10 @@ impl WalletTransactionChecker for ManagedWalletInfo { result.new_records.push(record); } } + for account_match in &result.affected_accounts { + self.accounts + .settle_outputs_held_elsewhere(&account_match.account_type_match, tx); + } self.attribute_late_inputs(tx, &mut result); if update_balance { self.update_balance(); @@ -444,6 +448,9 @@ impl WalletTransactionChecker for ManagedWalletInfo { } } + for account_match in &result.affected_accounts { + self.accounts.settle_outputs_held_elsewhere(&account_match.account_type_match, tx); + } self.attribute_late_inputs(tx, &mut result); if is_new { @@ -4024,4 +4031,57 @@ mod tests { .utxos .contains_key(&outpoint)); } + + /// A restore can leave a UTXO in a funds account that does not own its + /// address: here BIP44 holds X, an output paid to a CoinJoin address by + /// a transaction that also pays BIP44. Recording the transaction in a + /// block (a rescan) leaves X in the CoinJoin account only, so the wallet + /// balance counts it once (#1294). A lock already on the CoinJoin copy + /// stays. + #[test_case::test_case(false; "rescan")] + #[test_case::test_case(true; "after a locked mempool copy")] + #[tokio::test] + async fn an_output_held_by_another_funds_account_moves_to_its_owner(locked_in_mempool: bool) { + let mut ctx = TestWalletContext::new_random(); + let xpub = ctx.wallet.get_coinjoin_account(0).expect("CoinJoin account").account_xpub; + let coinjoin = ctx.managed_wallet.accounts.coinjoin_accounts.get_mut(&0).expect("CoinJoin"); + let coinjoin_address = match coinjoin.managed_account_type_mut() { + crate::ManagedAccountType::CoinJoin { + external_addresses, + .. + } => external_addresses + .next_unused(&crate::KeySource::Public(xpub), true) + .expect("CoinJoin address"), + _ => unreachable!("a CoinJoin account carries the CoinJoin managed type"), + }; + let mut tx = Transaction::dummy(&ctx.receive_address, 0..1, &[300_000]); + tx.output.push(TxOut { + value: 500_000, + script_pubkey: coinjoin_address.script_pubkey(), + }); + let x = OutPoint::new(tx.txid(), 1); + + if locked_in_mempool { + ctx.check_transaction(&tx, TransactionContext::Mempool).await; + let coinjoin = ctx.managed_wallet.accounts.coinjoin_accounts.get_mut(&0).unwrap(); + coinjoin.utxos.get_mut(&x).expect("CoinJoin holds X").lock(); + } + let restored = crate::Utxo::new(x, tx.output[1].clone(), coinjoin_address, 1_000, false); + let bip44 = ctx.managed_wallet.accounts.standard_bip44_accounts.get_mut(&0).expect("BIP44"); + bip44.utxos.insert(x, restored); + + let block = TransactionContext::InBlock(BlockInfo::new( + 1_000, + BlockHash::from_byte_array([0x10; 32]), + 1_700_000_000, + )); + ctx.check_transaction(&tx, block).await; + + let accounts = &ctx.managed_wallet.accounts; + assert!(!accounts.standard_bip44_accounts[&0].utxos.contains_key(&x)); + let held = &accounts.coinjoin_accounts[&0].utxos[&x]; + assert!(held.is_confirmed); + assert_eq!(held.is_locked, locked_in_mempool); + assert_eq!(ctx.managed_wallet.balance.total(), 800_000); + } } From 095f7f2083809205db6060fc703cc5fff15b1680 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 3 Nov 2026 03:25:30 +0000 Subject: [PATCH 2/6] fix(key-wallet): also take an output held as spent before funded out of other accounts When a block spending an output was seen before the transaction funding it, the owning account keeps the output as spent before funded, not in its UTXOs. A copy another funds account held then stayed there, counted and spendable. Treat such an output as held by its owner too; it is removed from the other accounts and credited nowhere. Co-Authored-By: Claude Opus 5.5 --- .../managed_account_collection.rs | 12 ++++++--- .../transaction_checking/wallet_checker.rs | 26 ++++++++++++++++--- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/key-wallet/src/managed_account/managed_account_collection.rs b/key-wallet/src/managed_account/managed_account_collection.rs index c99e679dd..781c8fa4c 100644 --- a/key-wallet/src/managed_account/managed_account_collection.rs +++ b/key-wallet/src/managed_account/managed_account_collection.rs @@ -1055,9 +1055,10 @@ impl ManagedAccountCollection { } /// Removes each output of `tx` that pays one of the `owner` funds - /// account's addresses and that `owner` holds from the other accounts of - /// [`Self::all_funding_accounts`] (a host can put a copy there), so the - /// wallet counts it once. The removed copy's `is_instantlocked`, + /// account's addresses and that `owner` holds, or holds as spent before it + /// was funded, from the other accounts of + /// [`Self::all_funding_accounts`] (a host can put a copy there), so no + /// other account counts it. The removed copy's `is_instantlocked`, /// `is_trusted` and `is_locked` flags carry over to `owner`'s copy. An /// account holding a contact's coins takes nothing from the others. pub(crate) fn settle_outputs_held_elsewhere( @@ -1080,7 +1081,10 @@ impl ManagedAccountCollection { .zip(0u32..) .filter(|(output, _)| owner_account.contains_script_pub_key(&output.script_pubkey)) .map(|(_, vout)| OutPoint::new(txid, vout)) - .filter(|outpoint| owner_account.utxos.contains_key(outpoint)) + .filter(|outpoint| { + owner_account.utxos.contains_key(outpoint) + || owner_account.spent_before_funded.contains_key(outpoint) + }) .collect(); if held.is_empty() { return; diff --git a/key-wallet/src/transaction_checking/wallet_checker.rs b/key-wallet/src/transaction_checking/wallet_checker.rs index fbce51f0a..aaf08f957 100644 --- a/key-wallet/src/transaction_checking/wallet_checker.rs +++ b/key-wallet/src/transaction_checking/wallet_checker.rs @@ -4037,11 +4037,16 @@ mod tests { /// a transaction that also pays BIP44. Recording the transaction in a /// block (a rescan) leaves X in the CoinJoin account only, so the wallet /// balance counts it once (#1294). A lock already on the CoinJoin copy - /// stays. - #[test_case::test_case(false; "rescan")] - #[test_case::test_case(true; "after a locked mempool copy")] + /// stays. When a block spending X was seen first, X leaves BIP44 and is + /// credited nowhere. + #[test_case::test_case(false, false; "rescan")] + #[test_case::test_case(true, false; "after a locked mempool copy")] + #[test_case::test_case(false, true; "after a block spending it")] #[tokio::test] - async fn an_output_held_by_another_funds_account_moves_to_its_owner(locked_in_mempool: bool) { + async fn an_output_held_by_another_funds_account_moves_to_its_owner( + locked_in_mempool: bool, + spent_first: bool, + ) { let mut ctx = TestWalletContext::new_random(); let xpub = ctx.wallet.get_coinjoin_account(0).expect("CoinJoin account").account_xpub; let coinjoin = ctx.managed_wallet.accounts.coinjoin_accounts.get_mut(&0).expect("CoinJoin"); @@ -4066,6 +4071,14 @@ mod tests { let coinjoin = ctx.managed_wallet.accounts.coinjoin_accounts.get_mut(&0).unwrap(); coinjoin.utxos.get_mut(&x).expect("CoinJoin holds X").lock(); } + if spent_first { + let mut spend = Transaction::dummy(&ctx.receive_address, 0..1, &[400_000]); + spend.input[0].previous_output = x; + spend.output[0].script_pubkey = ScriptBuf::new(); + let later = + BlockInfo::new(1_001, BlockHash::from_byte_array([0x11; 32]), 1_700_000_600); + ctx.check_transaction(&spend, TransactionContext::InBlock(later)).await; + } let restored = crate::Utxo::new(x, tx.output[1].clone(), coinjoin_address, 1_000, false); let bip44 = ctx.managed_wallet.accounts.standard_bip44_accounts.get_mut(&0).expect("BIP44"); bip44.utxos.insert(x, restored); @@ -4079,6 +4092,11 @@ mod tests { let accounts = &ctx.managed_wallet.accounts; assert!(!accounts.standard_bip44_accounts[&0].utxos.contains_key(&x)); + if spent_first { + assert!(!accounts.coinjoin_accounts[&0].utxos.contains_key(&x)); + assert_eq!(ctx.managed_wallet.balance.total(), 300_000); + return; + } let held = &accounts.coinjoin_accounts[&0].utxos[&x]; assert!(held.is_confirmed); assert_eq!(held.is_locked, locked_in_mempool); From 6c2804b93342125fd31a172e86eb853a012511a7 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 3 Nov 2026 09:17:11 +0000 Subject: [PATCH 3/6] feat(key-wallet): restore UTXOs into the account that owns their address A host restoring UTXOs had to pick the account for each one itself. One whose address the stored pools no longer reach, such as a CoinJoin address first derived during a sync, could end up in another account, and the next rescan then credited it to its owner as well, so the wallet balance counted it twice. Add ManagedWalletInfo::restore_utxos. It places each UTXO in the funds account whose pools hold its script, extending the pools to their gap limit past the addresses used, the way a rescan finds them, and returns the UTXOs no account reaches instead of guessing. The wallet's own accounts are tried before DashPay contact accounts, and a copy another funds account already holds of a restored outpoint is taken out. This replaces the earlier pass that removed such a copy once the owning account recorded the transaction. Refs #1294 Co-Authored-By: Claude Opus 5.5 --- .../managed_account_collection.rs | 56 ----- .../tests/dashpay_contact_gap_limit_tests.rs | 28 +-- key-wallet/src/tests/mod.rs | 2 + key-wallet/src/tests/restore_utxos_tests.rs | 200 ++++++++++++++++++ .../transaction_checking/wallet_checker.rs | 78 ------- .../src/wallet/managed_wallet_info/mod.rs | 102 +++++++++ 6 files changed, 305 insertions(+), 161 deletions(-) create mode 100644 key-wallet/src/tests/restore_utxos_tests.rs diff --git a/key-wallet/src/managed_account/managed_account_collection.rs b/key-wallet/src/managed_account/managed_account_collection.rs index 781c8fa4c..51742dd52 100644 --- a/key-wallet/src/managed_account/managed_account_collection.rs +++ b/key-wallet/src/managed_account/managed_account_collection.rs @@ -1054,62 +1054,6 @@ impl ManagedAccountCollection { accounts } - /// Removes each output of `tx` that pays one of the `owner` funds - /// account's addresses and that `owner` holds, or holds as spent before it - /// was funded, from the other accounts of - /// [`Self::all_funding_accounts`] (a host can put a copy there), so no - /// other account counts it. The removed copy's `is_instantlocked`, - /// `is_trusted` and `is_locked` flags carry over to `owner`'s copy. An - /// account holding a contact's coins takes nothing from the others. - pub(crate) fn settle_outputs_held_elsewhere( - &mut self, - owner: &CoreAccountTypeMatch, - tx: &Transaction, - ) { - let Some(ManagedAccountRef::Funds(owner_account)) = self.get_by_account_type_match(owner) - else { - return; - }; - let owner_type = owner_account.managed_account_type().to_account_type(); - if owner_type.is_contact_owned() { - return; - } - let txid = tx.txid(); - let held: Vec = tx - .output - .iter() - .zip(0u32..) - .filter(|(output, _)| owner_account.contains_script_pub_key(&output.script_pubkey)) - .map(|(_, vout)| OutPoint::new(txid, vout)) - .filter(|outpoint| { - owner_account.utxos.contains_key(outpoint) - || owner_account.spent_before_funded.contains_key(outpoint) - }) - .collect(); - if held.is_empty() { - return; - } - let mut removed = Vec::new(); - for account in self.all_funding_accounts_mut() { - if account.managed_account_type().to_account_type() == owner_type { - continue; - } - removed.extend(held.iter().filter_map(|outpoint| account.utxos.remove(outpoint))); - } - let Some(ManagedAccountRefMut::Funds(owner_account)) = - self.get_by_account_type_match_mut(owner) - else { - return; - }; - for prior in removed { - if let Some(utxo) = owner_account.utxos.get_mut(&prior.outpoint) { - utxo.is_instantlocked |= prior.is_instantlocked; - utxo.is_trusted |= prior.is_trusted; - utxo.is_locked |= prior.is_locked; - } - } - } - /// Get the count of accounts pub fn count(&self) -> usize { self.all_accounts().len() diff --git a/key-wallet/src/tests/dashpay_contact_gap_limit_tests.rs b/key-wallet/src/tests/dashpay_contact_gap_limit_tests.rs index 437aea17c..e4511f16a 100644 --- a/key-wallet/src/tests/dashpay_contact_gap_limit_tests.rs +++ b/key-wallet/src/tests/dashpay_contact_gap_limit_tests.rs @@ -1,4 +1,4 @@ -//! Regression tests for DIP-15 contact address pools (issues #1032 and #1294). +//! Regression test for DIP-15 contact address pool maintenance (issue #1032). use dashcore::hashes::Hash; use dashcore::{Address, BlockHash, Transaction}; @@ -109,29 +109,3 @@ async fn contact_payments_extend_the_contact_pool() { ); } } - -/// A wallet holding both identities of a friendship derives the same -/// addresses for its receiving chain and for the reverse external chain. A -/// payment to one of them stays in the receiving account, whose coins are -/// the wallet's, and counts in the balance. -#[tokio::test] -async fn a_payment_the_reverse_external_chain_also_matches_stays_ours() { - let mut ctx = TestWalletContext::new_random(); - let receiving = CONTACT_CHAINS[0]; - 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"); - } - let address = contact_pool(&ctx, receiving).address_at_index(0).expect("first address"); - assert_eq!(contact_pool(&ctx, reverse_external).address_at_index(0), Some(address.clone())); - - assert!(pay(&mut ctx, &address, 1_555_196).await); - assert_eq!(ctx.managed_wallet.balance.total(), 29_000); -} 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..37379e023 --- /dev/null +++ b/key-wallet/src/tests/restore_utxos_tests.rs @@ -0,0 +1,200 @@ +//! `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) +} + +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(); + 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"); + } + let accounts = &ctx.managed_wallet.accounts; + let address = accounts + .dashpay_receival_accounts + .values() + .next() + .expect("receiving account") + .managed_account_type() + .address_pools()[0] + .address_at_index(0) + .expect("first address"); + let external = accounts.dashpay_external_accounts.values().next().expect("external account"); + assert!(external.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()); + let accounts = &ctx.managed_wallet.accounts; + assert!(accounts + .dashpay_receival_accounts + .values() + .any(|a| a.utxos.contains_key(&utxo.outpoint))); + assert!(!accounts + .dashpay_external_accounts + .values() + .any(|a| a.utxos.contains_key(&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); +} diff --git a/key-wallet/src/transaction_checking/wallet_checker.rs b/key-wallet/src/transaction_checking/wallet_checker.rs index aaf08f957..168534e47 100644 --- a/key-wallet/src/transaction_checking/wallet_checker.rs +++ b/key-wallet/src/transaction_checking/wallet_checker.rs @@ -356,10 +356,6 @@ impl WalletTransactionChecker for ManagedWalletInfo { result.new_records.push(record); } } - for account_match in &result.affected_accounts { - self.accounts - .settle_outputs_held_elsewhere(&account_match.account_type_match, tx); - } self.attribute_late_inputs(tx, &mut result); if update_balance { self.update_balance(); @@ -448,9 +444,6 @@ impl WalletTransactionChecker for ManagedWalletInfo { } } - for account_match in &result.affected_accounts { - self.accounts.settle_outputs_held_elsewhere(&account_match.account_type_match, tx); - } self.attribute_late_inputs(tx, &mut result); if is_new { @@ -4031,75 +4024,4 @@ mod tests { .utxos .contains_key(&outpoint)); } - - /// A restore can leave a UTXO in a funds account that does not own its - /// address: here BIP44 holds X, an output paid to a CoinJoin address by - /// a transaction that also pays BIP44. Recording the transaction in a - /// block (a rescan) leaves X in the CoinJoin account only, so the wallet - /// balance counts it once (#1294). A lock already on the CoinJoin copy - /// stays. When a block spending X was seen first, X leaves BIP44 and is - /// credited nowhere. - #[test_case::test_case(false, false; "rescan")] - #[test_case::test_case(true, false; "after a locked mempool copy")] - #[test_case::test_case(false, true; "after a block spending it")] - #[tokio::test] - async fn an_output_held_by_another_funds_account_moves_to_its_owner( - locked_in_mempool: bool, - spent_first: bool, - ) { - let mut ctx = TestWalletContext::new_random(); - let xpub = ctx.wallet.get_coinjoin_account(0).expect("CoinJoin account").account_xpub; - let coinjoin = ctx.managed_wallet.accounts.coinjoin_accounts.get_mut(&0).expect("CoinJoin"); - let coinjoin_address = match coinjoin.managed_account_type_mut() { - crate::ManagedAccountType::CoinJoin { - external_addresses, - .. - } => external_addresses - .next_unused(&crate::KeySource::Public(xpub), true) - .expect("CoinJoin address"), - _ => unreachable!("a CoinJoin account carries the CoinJoin managed type"), - }; - let mut tx = Transaction::dummy(&ctx.receive_address, 0..1, &[300_000]); - tx.output.push(TxOut { - value: 500_000, - script_pubkey: coinjoin_address.script_pubkey(), - }); - let x = OutPoint::new(tx.txid(), 1); - - if locked_in_mempool { - ctx.check_transaction(&tx, TransactionContext::Mempool).await; - let coinjoin = ctx.managed_wallet.accounts.coinjoin_accounts.get_mut(&0).unwrap(); - coinjoin.utxos.get_mut(&x).expect("CoinJoin holds X").lock(); - } - if spent_first { - let mut spend = Transaction::dummy(&ctx.receive_address, 0..1, &[400_000]); - spend.input[0].previous_output = x; - spend.output[0].script_pubkey = ScriptBuf::new(); - let later = - BlockInfo::new(1_001, BlockHash::from_byte_array([0x11; 32]), 1_700_000_600); - ctx.check_transaction(&spend, TransactionContext::InBlock(later)).await; - } - let restored = crate::Utxo::new(x, tx.output[1].clone(), coinjoin_address, 1_000, false); - let bip44 = ctx.managed_wallet.accounts.standard_bip44_accounts.get_mut(&0).expect("BIP44"); - bip44.utxos.insert(x, restored); - - let block = TransactionContext::InBlock(BlockInfo::new( - 1_000, - BlockHash::from_byte_array([0x10; 32]), - 1_700_000_000, - )); - ctx.check_transaction(&tx, block).await; - - let accounts = &ctx.managed_wallet.accounts; - assert!(!accounts.standard_bip44_accounts[&0].utxos.contains_key(&x)); - if spent_first { - assert!(!accounts.coinjoin_accounts[&0].utxos.contains_key(&x)); - assert_eq!(ctx.managed_wallet.balance.total(), 300_000); - return; - } - let held = &accounts.coinjoin_accounts[&0].utxos[&x]; - assert!(held.is_confirmed); - assert_eq!(held.is_locked, locked_in_mempool); - assert_eq!(ctx.managed_wallet.balance.total(), 800_000); - } } diff --git a/key-wallet/src/wallet/managed_wallet_info/mod.rs b/key-wallet/src/wallet/managed_wallet_info/mod.rs index 4938ebec9..3c9b36d86 100644 --- a/key-wallet/src/wallet/managed_wallet_info/mod.rs +++ b/key-wallet/src/wallet/managed_wallet_info/mod.rs @@ -417,6 +417,86 @@ 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. + /// + /// Addresses are found the way a rescan finds them: each pool is extended + /// to its own `gap_limit` past its highest used address, a matched + /// address is marked used, and this repeats until a pass matches nothing + /// new. A UTXO beyond that reach comes back unmatched rather than placed + /// in another account; a rescan that processes its funding transaction + /// credits it. Accounts without a public key source match only the + /// addresses they already hold. The wallet's own accounts are tried before + /// DashPay contact accounts, so for a script both hold the UTXO goes to + /// the wallet's account. A restored outpoint that another funds account + /// already holds is taken out of that account. + /// + /// Each account whose pools grow or whose UTXOs change 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 mut pending: Vec = utxos.into_iter().collect(); + let mut placed = Vec::new(); + loop { + let placed_before = placed.len(); + let mut accounts = funds_accounts_wallet_first(&mut self.accounts); + 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(); + } + } + let mut unmatched = Vec::new(); + for utxo in pending { + let script = &utxo.txout.script_pubkey; + match accounts.iter_mut().find(|account| account.contains_script_pub_key(script)) { + Some(account) => { + if let Ok(address) = Address::from_script(script, self.network) { + account.mark_address_used(&address); + } + placed.push((account.managed_account_type().to_account_type(), utxo)); + } + None => unmatched.push(utxo), + } + } + pending = unmatched; + if placed.len() == placed_before { + break; + } + } + let mut accounts = funds_accounts_wallet_first(&mut self.accounts); + for (account_type, utxo) in placed { + for account in accounts.iter_mut() { + let changed = if account.managed_account_type().to_account_type() == account_type { + account.utxos.insert(utxo.outpoint, utxo.clone()).is_none() + } else { + account.utxos.remove(&utxo.outpoint).is_some() + }; + if changed { + account.bump_monitor_revision(); + } + } + } + Ok(pending) + } + /// 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 +720,28 @@ impl ManagedWalletInfo { } } +/// 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; From d3ae48776025426b9cf3d49f8c0706f348c0efd2 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 3 Nov 2026 10:01:53 +0000 Subject: [PATCH 4/6] fix(key-wallet): rematch restored UTXOs each pass and keep spent and lock state restore_utxos now matches every UTXO again on each pass after the pools grew, so an account tried earlier claims a UTXO once its pool reaches it, and marks the address used in every account holding it, as a rescan does. The assignment is applied once, after the pools stop growing. A restored UTXO keeps the InstantSend lock and trust of any copy it replaces or takes out of another account, and their coin lock replaces its own, as reprocessing a transaction keeps them. An outpoint the wallet already saw spent is not credited or returned: one spent in a processed block is held as spent before funded, as processing its funding transaction does, and one the owning account recorded spending is dropped. A spent-output claim still does not hold one back. The rustdoc also says the call belongs before sync resumes, since blocks already processed are not scanned again for newly derived addresses. Co-Authored-By: Claude Opus 5.5 --- key-wallet/src/tests/restore_utxos_tests.rs | 208 ++++++++++++++---- .../src/wallet/managed_wallet_info/mod.rs | 167 +++++++++----- 2 files changed, 282 insertions(+), 93 deletions(-) diff --git a/key-wallet/src/tests/restore_utxos_tests.rs b/key-wallet/src/tests/restore_utxos_tests.rs index 37379e023..53b9174c2 100644 --- a/key-wallet/src/tests/restore_utxos_tests.rs +++ b/key-wallet/src/tests/restore_utxos_tests.rs @@ -43,6 +43,45 @@ fn funded(address: &Address, input: u8, value: u64) -> (Transaction, Utxo) { (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 @@ -112,50 +151,56 @@ fn a_utxo_matching_no_account_comes_back(ours_but_unreachable: bool) { #[test] fn a_utxo_both_a_receiving_and_a_contact_account_hold_restores_into_the_receiving_one() { let mut ctx = TestWalletContext::new_random(); - 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"); - } - let accounts = &ctx.managed_wallet.accounts; - let address = accounts - .dashpay_receival_accounts - .values() - .next() - .expect("receiving account") - .managed_account_type() - .address_pools()[0] - .address_at_index(0) - .expect("first address"); - let external = accounts.dashpay_external_accounts.values().next().expect("external account"); - assert!(external.contains_script_pub_key(&address.script_pubkey())); + 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()); - let accounts = &ctx.managed_wallet.accounts; - assert!(accounts - .dashpay_receival_accounts - .values() - .any(|a| a.utxos.contains_key(&utxo.outpoint))); - assert!(!accounts - .dashpay_external_accounts - .values() - .any(|a| a.utxos.contains_key(&utxo.outpoint))); + 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 @@ -198,3 +243,90 @@ fn a_copy_another_account_holds_is_taken_out_on_restore() { 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 copy CoinJoin already +/// held in place of its own. +#[test_case(true; "a held lock stays")] +#[test_case(false; "a held unlock stays")] +fn a_restored_utxo_keeps_the_flags_of_the_copies_it_replaces(held_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 = held_locked; + accounts.standard_bip44_accounts.get_mut(&0).unwrap().utxos.insert(utxo.outpoint, misplaced); + utxo.is_locked = !held_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, held_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); +} diff --git a/key-wallet/src/wallet/managed_wallet_info/mod.rs b/key-wallet/src/wallet/managed_wallet_info/mod.rs index 3c9b36d86..0bdf5239b 100644 --- a/key-wallet/src/wallet/managed_wallet_info/mod.rs +++ b/key-wallet/src/wallet/managed_wallet_info/mod.rs @@ -420,19 +420,35 @@ impl ManagedWalletInfo { /// 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. + /// /// Addresses are found the way a rescan finds them: each pool is extended - /// to its own `gap_limit` past its highest used address, a matched - /// address is marked used, and this repeats until a pass matches nothing - /// new. A UTXO beyond that reach comes back unmatched rather than placed - /// in another account; a rescan that processes its funding transaction - /// credits it. Accounts without a public key source match only the - /// addresses they already hold. The wallet's own accounts are tried before - /// DashPay contact accounts, so for a script both hold the UTXO goes to - /// the wallet's account. A restored outpoint that another funds account - /// already holds is taken out of that account. + /// 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; a rescan that processes its funding + /// transaction credits it. 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 restored UTXO keeps the InstantSend lock and + /// trust of any copy it replaces or takes out, and their coin lock replaces + /// its own, as reprocessing a transaction keeps them. A matched outpoint + /// the wallet already saw spent is neither credited nor returned: 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, as + /// processing its funding transaction does, and one the owning account + /// recorded spending is dropped. A spent-output claim + /// does not hold a restored UTXO back. /// - /// Each account whose pools grow or whose UTXOs change gets its monitor - /// revision bumped, so a running client rebuilds its filters. + /// Each account whose pools grow or that gains or loses a UTXO 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 @@ -444,57 +460,72 @@ impl ManagedWalletInfo { wallet: &Wallet, utxos: impl IntoIterator, ) -> crate::error::Result> { - let mut pending: Vec = utxos.into_iter().collect(); - let mut placed = Vec::new(); - loop { - let placed_before = placed.len(); - let mut accounts = funds_accounts_wallet_first(&mut self.accounts); - 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(); - } - } - let mut unmatched = Vec::new(); - for utxo in pending { - let script = &utxo.txout.script_pubkey; - match accounts.iter_mut().find(|account| account.contains_script_pub_key(script)) { - Some(account) => { - if let Ok(address) = Address::from_script(script, self.network) { - account.mark_address_used(&address); + 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); } - placed.push((account.managed_account_type().to_account_type(), utxo)); } - None => unmatched.push(utxo), + owner + }) + .collect(); + if !extend_pools(wallet, &mut accounts)? { + break owners; + } + }; + let mut unmatched = Vec::new(); + for (mut 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(); + } } } - pending = unmatched; - if placed.len() == placed_before { - break; + let account = &mut accounts[owner]; + if account.is_outpoint_spent(&outpoint) { + continue; } - } - let mut accounts = funds_accounts_wallet_first(&mut self.accounts); - for (account_type, utxo) in placed { - for account in accounts.iter_mut() { - let changed = if account.managed_account_type().to_account_type() == account_type { - account.utxos.insert(utxo.outpoint, utxo.clone()).is_none() - } else { - account.utxos.remove(&utxo.outpoint).is_some() - }; - if changed { - account.bump_monitor_revision(); - } + if self.observed_spent_outpoints.contains_key(&outpoint) + || account.spent_before_funded.contains_key(&outpoint) + { + account.spent_before_funded.entry(outpoint).or_insert(utxo); + continue; + } + copies.extend(account.utxos.get(&outpoint).cloned()); + if !copies.is_empty() { + utxo.is_instantlocked |= copies.iter().any(|copy| copy.is_instantlocked); + utxo.is_trusted |= copies.iter().any(|copy| copy.is_trusted); + utxo.is_locked = copies.iter().any(|copy| copy.is_locked); + } + if account.utxos.insert(outpoint, utxo).is_none() { + account.bump_monitor_revision(); } } - Ok(pending) + Ok(unmatched) } /// Record every outpoint `tx` spends into [`Self::observed_spent_outpoints`] @@ -720,6 +751,32 @@ 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( From c49f43269805456318f13c10c1750cfc7bb0adf9 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 3 Nov 2026 12:48:48 +0000 Subject: [PATCH 5/6] test(key-wallet): cover a lock held only by the copy restore_utxos takes out restore_utxos takes the coin lock of the copies it replaces. Add the mixed case CodeRabbit suggested in its review of #1295: the CoinJoin copy is unlocked and the misplaced BIP44 copy is locked, so the restored UTXO stays locked. Co-Authored-By: Claude Opus 5.5 --- key-wallet/src/tests/restore_utxos_tests.rs | 22 +++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/key-wallet/src/tests/restore_utxos_tests.rs b/key-wallet/src/tests/restore_utxos_tests.rs index 53b9174c2..c8c4b21d1 100644 --- a/key-wallet/src/tests/restore_utxos_tests.rs +++ b/key-wallet/src/tests/restore_utxos_tests.rs @@ -245,11 +245,17 @@ fn a_copy_another_account_holds_is_taken_out_on_restore() { } /// The restored UTXO keeps the InstantSend lock and trust of the copy it -/// takes out of BIP44, and takes the coin lock of the copy CoinJoin already -/// held in place of its own. -#[test_case(true; "a held lock stays")] -#[test_case(false; "a held unlock stays")] -fn a_restored_utxo_keeps_the_flags_of_the_copies_it_replaces(held_locked: bool) { +/// 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; @@ -259,14 +265,14 @@ fn a_restored_utxo_keeps_the_flags_of_the_copies_it_replaces(held_locked: bool) let mut misplaced = utxo.clone(); misplaced.is_instantlocked = true; misplaced.is_trusted = true; - misplaced.is_locked = held_locked; + misplaced.is_locked = misplaced_locked; accounts.standard_bip44_accounts.get_mut(&0).unwrap().utxos.insert(utxo.outpoint, misplaced); - utxo.is_locked = !held_locked; + 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, held_locked); + assert_eq!(restored.is_locked, expected_locked); assert!(restored.is_instantlocked); assert!(restored.is_trusted); } From f233dcc962f408d08d952053d128aea369e1688c Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 3 Nov 2026 13:34:33 +0000 Subject: [PATCH 6/6] refactor(key-wallet): share the per-output credit decision of update_utxos and restore_utxos Move how a funds account takes in an output paying one of its addresses into ManagedCoreFundsAccount::take_in_output, which update_utxos and restore_utxos both call. A restored coin the owning account already recorded spending is now held under that spend, as update_utxos holds it, so a fork dropping the spend gives it back instead of losing it. Crediting an output also takes it out of claim_guarded_outputs, so a claimed coin restored after its funding is no longer in both. Document that a UTXO restore_utxos returns is credited only if the host rewinds synced_height below its height, that the host marks its stored used addresses first, and the call order with restore_spent_outpoints, whose guard description now says restore_utxos does not consult claims. Addresses xdustinface's review of #1295. Co-Authored-By: Claude Opus 5.5 --- .../managed_core_funds_account.rs | 214 +++++++++--------- key-wallet/src/tests/restore_utxos_tests.rs | 46 ++++ .../src/wallet/managed_wallet_info/mod.rs | 74 +++--- 3 files changed, 195 insertions(+), 139 deletions(-) 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/restore_utxos_tests.rs b/key-wallet/src/tests/restore_utxos_tests.rs index c8c4b21d1..3eedfe4ec 100644 --- a/key-wallet/src/tests/restore_utxos_tests.rs +++ b/key-wallet/src/tests/restore_utxos_tests.rs @@ -336,3 +336,49 @@ async fn an_outpoint_already_seen_spent_is_not_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 0bdf5239b..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. @@ -422,7 +427,10 @@ impl ManagedWalletInfo { /// /// 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. + /// 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 @@ -431,24 +439,29 @@ impl ManagedWalletInfo { /// 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; a rescan that processes its funding - /// transaction credits it. Accounts without a public key source match only - /// the addresses they already hold. + /// 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 restored UTXO keeps the InstantSend lock and - /// trust of any copy it replaces or takes out, and their coin lock replaces - /// its own, as reprocessing a transaction keeps them. A matched outpoint - /// the wallet already saw spent is neither credited nor returned: one - /// spent in a processed block + /// 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, as - /// processing its funding transaction does, and one the owning account - /// recorded spending is dropped. A spent-output claim - /// does not hold a restored UTXO back. + /// 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 gains or loses a UTXO gets its - /// monitor revision bumped, so a running client rebuilds its filters. + /// 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 @@ -490,7 +503,7 @@ impl ManagedWalletInfo { } }; let mut unmatched = Vec::new(); - for (mut utxo, owner) in utxos.into_iter().zip(owners) { + for (utxo, owner) in utxos.into_iter().zip(owners) { let Some(owner) = owner else { unmatched.push(utxo); continue; @@ -506,22 +519,9 @@ impl ManagedWalletInfo { } } let account = &mut accounts[owner]; - if account.is_outpoint_spent(&outpoint) { - continue; - } - if self.observed_spent_outpoints.contains_key(&outpoint) - || account.spent_before_funded.contains_key(&outpoint) - { - account.spent_before_funded.entry(outpoint).or_insert(utxo); - continue; - } - copies.extend(account.utxos.get(&outpoint).cloned()); - if !copies.is_empty() { - utxo.is_instantlocked |= copies.iter().any(|copy| copy.is_instantlocked); - utxo.is_trusted |= copies.iter().any(|copy| copy.is_trusted); - utxo.is_locked = copies.iter().any(|copy| copy.is_locked); - } - if account.utxos.insert(outpoint, utxo).is_none() { + let credited = + account.take_in_output(utxo, copies, &self.observed_spent_outpoints, None, |_| {}); + if credited { account.bump_monitor_revision(); } }