From 9e04ff6eb1a0a8a4bedbe21e7ffdb77e2516a2b7 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Mon, 28 Sep 2026 21:42:55 +0300 Subject: [PATCH 01/24] fix(key-wallet): recover CoinJoin outputs returned past the gap limit A wallet migrated from DashSync can hold mixed coins past a run of empty CoinJoin addresses wider than the gap limit: DashSync marked an address used when it handed it to a mixing session, so every failed session left an empty index behind. Discovery stops at the first such run, and every later coin stays invisible through any number of rescans. When a transaction spends our CoinJoin coins of a denomination but pays fewer outputs of that denomination back to our pools, derive each CoinJoin chain past its generated end (up to COINJOIN_RECOVERY_PROBE_WINDOW) and compare against the transaction's unclaimed outputs of that value. A match extends the pool to it before the transaction is checked, so the output is credited and the new addresses reach SPV as newly derived scripts. Co-Authored-By: Claude Opus 5.5 --- .../transaction_checking/coinjoin_recovery.rs | 368 ++++++++++++++++++ key-wallet/src/transaction_checking/mod.rs | 1 + .../transaction_checking/wallet_checker.rs | 12 + 3 files changed, 381 insertions(+) create mode 100644 key-wallet/src/transaction_checking/coinjoin_recovery.rs diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs new file mode 100644 index 000000000..2676b6600 --- /dev/null +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -0,0 +1,368 @@ +//! Recovery of CoinJoin outputs that landed past the address pool's gap limit. +//! +//! CoinJoin discovery walks each CoinJoin chain forward and stops after +//! `gap_limit` unused addresses. A wallet migrated from DashSync breaks that +//! assumption: DashSync marked a CoinJoin address used the moment it handed it +//! to a mixing session, so every failed session left an address that is used +//! locally and empty on chain. Enough failed sessions in a row leave a run of +//! empty addresses wider than any fixed gap, and every mixed coin past that run +//! stays invisible no matter how often the wallet rescans (ticket 32008). +//! +//! A wider gap only moves the wall. Instead, the wallet reacts to the evidence +//! that a coin went missing: a transaction that spends our CoinJoin coins of a +//! denomination but pays fewer outputs of that denomination back to our +//! CoinJoin pools. For such a transaction only, the chain is derived past the +//! generated end of the pool and compared against the transaction's unclaimed +//! outputs of that denomination. A match extends the pool up to it, which the +//! regular gap maintenance and the SPV rescan of newly derived scripts then +//! carry forward. + +use std::collections::{BTreeMap, HashMap, HashSet}; + +use dashcore::blockdata::transaction::Transaction; +use dashcore::ScriptBuf; + +use super::account_checker::DerivedAddressInfo; +use super::transaction_router::AccountTypeToCheck; +use crate::managed_account::managed_account_trait::ManagedAccountTrait; +use crate::wallet::{ManagedWalletInfo, Wallet}; +use crate::KeySource; + +/// How far past the generated end of each CoinJoin chain a missing output is +/// searched for. Derivation only; nothing in this window is watched unless an +/// output is found in it. +pub const COINJOIN_RECOVERY_PROBE_WINDOW: u32 = 2_000; + +impl ManagedWalletInfo { + /// Extend a CoinJoin pool to any output of `tx` that is ours but lies past + /// the pool's generated end, when `tx` shows that one of our denominations + /// was spent without coming back. + /// + /// Must run before the transaction is checked, so that the outputs found + /// here are credited by the regular check. Returns the addresses added to + /// the pools, for the caller to hand to SPV as newly derived scripts. + pub(crate) fn recover_coinjoin_outputs( + &mut self, + tx: &Transaction, + wallet: &Wallet, + ) -> Vec { + let mut derived = Vec::new(); + + for (&index, account) in self.accounts.coinjoin_accounts.iter_mut() { + // Our denominations spent by this transaction, and how many outputs + // of each value it already pays to our pools. + let mut spent: BTreeMap = BTreeMap::new(); + for input in &tx.input { + if let Some(utxo) = account.utxos.get(&input.previous_output) { + *spent.entry(utxo.txout.value).or_default() += 1; + } + } + if spent.is_empty() { + continue; + } + + let pools = account.managed_account_type().address_pools(); + let is_ours = + |script: &ScriptBuf| pools.iter().any(|pool| pool.contains_script_pubkey(script)); + let mut returned: HashMap = HashMap::new(); + let mut unclaimed: HashSet = HashSet::new(); + for output in &tx.output { + if !spent.contains_key(&output.value) { + continue; + } + if is_ours(&output.script_pubkey) { + *returned.entry(output.value).or_default() += 1; + } else { + unclaimed.insert(output.script_pubkey.clone()); + } + } + let missing = spent + .iter() + .any(|(value, count)| returned.get(value).copied().unwrap_or(0) < *count); + if !missing || unclaimed.is_empty() { + continue; + } + + let key_source = + wallet.key_source_for_account_type(&AccountTypeToCheck::CoinJoin, Some(index)); + if matches!(key_source, KeySource::NoKeySource) { + continue; + } + + let account_type = account.managed_account_type().to_account_type(); + let mut extended = false; + for pool in account.managed_account_type_mut().address_pools_mut() { + let start = pool.highest_generated.map(|h| h + 1).unwrap_or(0); + let mut found: Vec = Vec::new(); + for probe in start..start.saturating_add(COINJOIN_RECOVERY_PROBE_WINDOW) { + let address = match pool.generate_address_at_index(probe, &key_source, false) { + Ok(address) => address, + Err(e) => { + tracing::warn!(error = %e, "CoinJoin recovery: derivation failed"); + break; + } + }; + if unclaimed.contains(&address.script_pubkey()) { + found.push(probe); + } + } + let Some(&highest) = found.last() else { + continue; + }; + + let pool_type = pool.pool_type; + let mut new_infos = Vec::new(); + for next in start..=highest { + if pool.generate_address_at_index(next, &key_source, true).is_err() { + break; + } + if let Some(info) = pool.info_at_index(next) { + new_infos.push(info.clone()); + } + } + for &probe in &found { + pool.mark_index_used(probe); + } + match pool.maintain_gap_limit(&key_source) { + Ok(infos) => new_infos.extend(infos), + Err(e) => tracing::error!( + error = %e, + "CoinJoin recovery: failed to maintain gap limit after extending pool" + ), + } + + tracing::info!( + txid = %tx.txid(), + pool_type = ?pool_type, + previous_end = start.saturating_sub(1), + found = ?found, + added = new_infos.len(), + "CoinJoin recovery: extended pool to outputs past the gap limit" + ); + derived.extend(new_infos.into_iter().map(|info| DerivedAddressInfo { + account_type, + pool_type, + info, + })); + extended = true; + } + if extended { + account.bump_monitor_revision(); + } + } + + derived + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::account::AccountType; + use crate::managed_account::address_pool::AddressPoolType; + use crate::managed_account::managed_account_type::ManagedAccountType; + use crate::transaction_checking::{BlockInfo, TransactionContext, WalletTransactionChecker}; + use crate::wallet::initialization::WalletAccountCreationOptions; + use crate::Network; + use dashcore::hashes::Hash; + use dashcore::{Address, BlockHash, OutPoint, TxIn, TxOut, Txid}; + + const NETWORK: Network = Network::Testnet; + /// 0.001 DASH CoinJoin denomination, fee included. + const DENOM: u64 = 100_001; + + struct Fixture { + wallet: Wallet, + managed: ManagedWalletInfo, + key_source: KeySource, + } + + fn fixture() -> Fixture { + let mut wallet = + Wallet::new_random(NETWORK, WalletAccountCreationOptions::None).expect("wallet"); + wallet + .add_account( + AccountType::CoinJoin { + index: 0, + }, + None, + ) + .expect("CoinJoin account"); + let managed = ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0); + let xpub = wallet.accounts.coinjoin_accounts.get(&0).expect("account").account_xpub; + Fixture { + wallet, + managed, + key_source: KeySource::Public(xpub), + } + } + + fn coinjoin_address(fx: &mut Fixture, internal: bool, index: u32) -> Address { + let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); + let ManagedAccountType::CoinJoin { + external_addresses, + internal_addresses, + .. + } = account.managed_account_type_mut() + else { + panic!("expected a CoinJoin account"); + }; + let pool = if internal { + internal_addresses + } else { + external_addresses + }; + pool.generate_address_at_index(index, &fx.key_source, false).expect("derive") + } + + fn pool_end(fx: &Fixture, pool_type: AddressPoolType) -> Option { + let account = fx.managed.first_coinjoin_managed_account().expect("account"); + account + .managed_account_type() + .address_pools() + .into_iter() + .find(|pool| pool.pool_type == pool_type) + .and_then(|pool| pool.highest_generated) + } + + fn block(height: u32) -> TransactionContext { + TransactionContext::InChainLockedBlock(BlockInfo::new( + height, + BlockHash::from_slice(&[height as u8; 32]).expect("hash"), + 1_650_000_000 + height, + )) + } + + /// Funds our CoinJoin external index 0 with one denomination, then mixes it: + /// our coin plus two foreign ones in, three denominations out, ours to `ours`. + async fn fund_and_mix( + fx: &mut Fixture, + ours: Address, + ) -> crate::transaction_checking::TransactionCheckResult { + let first = coinjoin_address(fx, false, 0); + let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); + let funded = + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; + assert_eq!(funded.total_received, DENOM, "funding must be credited"); + + let foreign_input = |n: u8| TxIn { + previous_output: OutPoint { + txid: Txid::from_byte_array([n; 32]), + vout: 0, + }, + ..Default::default() + }; + let mix = dashcore::Transaction { + version: 2, + lock_time: 0, + input: vec![ + foreign_input(0xa1), + TxIn { + previous_output: OutPoint { + txid: funding.txid(), + vout: 0, + }, + ..Default::default() + }, + foreign_input(0xa2), + ], + output: vec![ + TxOut { + value: DENOM, + script_pubkey: Address::dummy(NETWORK, 1).script_pubkey(), + }, + TxOut { + value: DENOM, + script_pubkey: ours.script_pubkey(), + }, + TxOut { + value: DENOM, + script_pubkey: Address::dummy(NETWORK, 2).script_pubkey(), + }, + ], + special_transaction_payload: None, + }; + fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await + } + + /// 32008: the mixed coin comes back past the gap limit. Without recovery the + /// wallet books the mix as a one-sided loss and never watches the address. + #[tokio::test] + async fn mixed_coin_returned_past_the_gap_limit_is_recovered() { + let mut fx = fixture(); + let far = coinjoin_address(&mut fx, false, 350); + let result = fund_and_mix(&mut fx, far.clone()).await; + + assert_eq!(result.total_sent, DENOM, "our input is debited"); + assert_eq!(result.total_received, DENOM, "the far output must be credited"); + let account = fx.managed.first_coinjoin_managed_account().expect("account"); + assert!( + account.utxos.values().any(|utxo| utxo.address == far), + "the far output must be a UTXO" + ); + assert!( + pool_end(&fx, AddressPoolType::External) + >= Some(350 + crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT), + "the pool must extend past the recovered index by the gap limit" + ); + assert!( + result.new_addresses.iter().any(|d| d.info.index == 350), + "the recovered address must be reported as newly derived" + ); + assert_eq!(fx.managed.balance.total(), DENOM, "nothing lost: balance equals the coin"); + } + + /// DashSync also mixed onto its internal CoinJoin chain. + #[tokio::test] + async fn mixed_coin_returned_past_the_gap_limit_on_the_internal_chain_is_recovered() { + let mut fx = fixture(); + let far = coinjoin_address(&mut fx, true, 250); + let result = fund_and_mix(&mut fx, far.clone()).await; + + assert_eq!(result.total_received, DENOM, "the far internal output must be credited"); + assert!(pool_end(&fx, AddressPoolType::Internal) >= Some(250)); + assert_eq!(fx.managed.balance.total(), DENOM); + } + + /// A normal mix that returns the coin inside the pool derives nothing extra. + #[tokio::test] + async fn mix_returning_inside_the_pool_does_not_probe() { + let mut fx = fixture(); + let near = coinjoin_address(&mut fx, false, 5); + let result = fund_and_mix(&mut fx, near).await; + + assert_eq!(result.total_received, DENOM); + assert_eq!( + pool_end(&fx, AddressPoolType::External), + Some(5 + crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT), + "only the regular gap maintenance past the highest used index" + ); + } + + /// A coin that really left (all outputs foreign) is not credited, and the + /// pool is not extended by the probe. + #[tokio::test] + async fn coin_that_really_left_is_not_credited() { + let mut fx = fixture(); + let foreign = Address::dummy(NETWORK, 3); + let result = fund_and_mix(&mut fx, foreign).await; + + assert_eq!(result.total_received, 0); + assert_eq!( + pool_end(&fx, AddressPoolType::External), + Some(crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT), + "the probe found nothing, so the pool stays at the funding's gap" + ); + assert_eq!(fx.managed.balance.total(), 0); + } + + /// Past the probe window the output stays unknown — the window is a bound, + /// not a guarantee. + #[tokio::test] + async fn output_past_the_probe_window_is_not_found() { + let mut fx = fixture(); + let beyond = coinjoin_address(&mut fx, false, 100 + COINJOIN_RECOVERY_PROBE_WINDOW + 10); + let result = fund_and_mix(&mut fx, beyond).await; + assert_eq!(result.total_received, 0); + } +} diff --git a/key-wallet/src/transaction_checking/mod.rs b/key-wallet/src/transaction_checking/mod.rs index 0ccb2048d..c359ad856 100644 --- a/key-wallet/src/transaction_checking/mod.rs +++ b/key-wallet/src/transaction_checking/mod.rs @@ -5,6 +5,7 @@ //! transaction types. pub mod account_checker; +pub mod coinjoin_recovery; pub mod platform_checker; pub mod transaction_context; pub mod transaction_router; diff --git a/key-wallet/src/transaction_checking/wallet_checker.rs b/key-wallet/src/transaction_checking/wallet_checker.rs index 81f1adf1b..34ce1e369 100644 --- a/key-wallet/src/transaction_checking/wallet_checker.rs +++ b/key-wallet/src/transaction_checking/wallet_checker.rs @@ -119,8 +119,20 @@ impl WalletTransactionChecker for ManagedWalletInfo { // Get relevant account types for this transaction type let relevant_types = TransactionRouter::get_relevant_account_types(&tx_type); + // A CoinJoin coin of ours that came back past the pool's gap limit is + // found here, before the check, so the check below credits it (32008). + let recovered = if update_state { + self.recover_coinjoin_outputs(tx, wallet) + } else { + Vec::new() + }; + // Check only relevant account types let mut result = self.accounts.check_transaction(tx, &relevant_types); + if !recovered.is_empty() { + result.new_addresses.extend(recovered); + result.state_modified = true; + } // #649: remember every spend seen in a block, independent of the // spending tx's classification or whether the wallet can attribute it. From 6b53ee7f5b3b52267c560af096fe65c63da22bff Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Mon, 28 Sep 2026 23:25:32 +0300 Subject: [PATCH 02/24] fix(key-wallet): probe 10,000 CoinJoin addresses deep, derived once per chain DashSync derived up to ~10,500 CoinJoin addresses on heavily mixed wallets, so a 2,000 window can stop short. Cache the probe derivation per chain so the many one-sided mixes of one wallet derive the window once. The whole window is still compared on every trigger: a mix can return more of our coins than the inputs the wallet knows, and stopping early would lose them. Co-Authored-By: Claude Opus 5.5 --- .../transaction_checking/coinjoin_recovery.rs | 143 ++++++++++++++++-- 1 file changed, 130 insertions(+), 13 deletions(-) diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index 2676b6600..f21124256 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -18,6 +18,7 @@ //! carry forward. use std::collections::{BTreeMap, HashMap, HashSet}; +use std::sync::{LazyLock, Mutex}; use dashcore::blockdata::transaction::Transaction; use dashcore::ScriptBuf; @@ -30,8 +31,35 @@ use crate::KeySource; /// How far past the generated end of each CoinJoin chain a missing output is /// searched for. Derivation only; nothing in this window is watched unless an -/// output is found in it. -pub const COINJOIN_RECOVERY_PROBE_WINDOW: u32 = 2_000; +/// output is found in it. DashSync derived up to ~10,500 CoinJoin addresses on +/// heavily mixed wallets. +pub const COINJOIN_RECOVERY_PROBE_WINDOW: u32 = 10_000; + +/// Scripts derived for probing, per CoinJoin chain, so that the many one-sided +/// mixes of one wallet derive the window once rather than once each. The whole +/// window is always compared — a mix can return more of our coins than the +/// inputs we know about — so the cache changes cost, never the result. +#[derive(Default)] +struct ProbeCache { + scripts: HashMap, + /// Every index below this is in `scripts`. + derived_until: u32, +} + +static PROBE_CACHE: LazyLock>> = + LazyLock::new(|| Mutex::new(HashMap::new())); + +/// Identifies the chain a key source derives, for the probe cache. +fn pool_key(key_source: &KeySource) -> String { + match key_source { + KeySource::Public(xpub) => xpub.to_string(), + KeySource::Private(xpriv) => { + crate::bip32::ExtendedPubKey::from_priv(&dashcore::secp256k1::Secp256k1::new(), xpriv) + .to_string() + } + _ => String::new(), + } +} impl ManagedWalletInfo { /// Extend a CoinJoin pool to any output of `tx` that is ours but lies past @@ -93,19 +121,32 @@ impl ManagedWalletInfo { let mut extended = false; for pool in account.managed_account_type_mut().address_pools_mut() { let start = pool.highest_generated.map(|h| h + 1).unwrap_or(0); - let mut found: Vec = Vec::new(); - for probe in start..start.saturating_add(COINJOIN_RECOVERY_PROBE_WINDOW) { - let address = match pool.generate_address_at_index(probe, &key_source, false) { - Ok(address) => address, - Err(e) => { - tracing::warn!(error = %e, "CoinJoin recovery: derivation failed"); - break; + let end = start.saturating_add(COINJOIN_RECOVERY_PROBE_WINDOW); + let cache_key = + format!("{:?}/{:?}/{}", pool.pool_type, pool.base_path, pool_key(&key_source)); + let mut found: Vec = { + let mut caches = PROBE_CACHE.lock().unwrap_or_else(|e| e.into_inner()); + let cache = caches.entry(cache_key).or_default(); + while cache.derived_until < end { + let probe = cache.derived_until; + match pool.generate_address_at_index(probe, &key_source, false) { + Ok(address) => { + cache.scripts.insert(address.script_pubkey(), probe); + cache.derived_until += 1; + } + Err(e) => { + tracing::warn!(error = %e, "CoinJoin recovery: derivation failed"); + break; + } } - }; - if unclaimed.contains(&address.script_pubkey()) { - found.push(probe); } - } + unclaimed + .iter() + .filter_map(|script| cache.scripts.get(script).copied()) + .filter(|index| (start..end).contains(index)) + .collect() + }; + found.sort_unstable(); let Some(&highest) = found.last() else { continue; }; @@ -365,4 +406,80 @@ mod tests { let result = fund_and_mix(&mut fx, beyond).await; assert_eq!(result.total_received, 0); } + + /// A mix can return more of our coins than the inputs the wallet knows + /// about (one of ours came from an address it has not discovered yet). + /// Every one of them in the window must be found, not just as many as the + /// known inputs. + #[tokio::test] + async fn every_returned_coin_is_found_not_just_the_known_inputs() { + let mut fx = fixture(); + let far_a = coinjoin_address(&mut fx, false, 300); + let far_b = coinjoin_address(&mut fx, false, 900); + let first = coinjoin_address(&mut fx, false, 0); + let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; + + let mix = dashcore::Transaction { + version: 2, + lock_time: 0, + input: vec![ + TxIn { + previous_output: OutPoint { + txid: funding.txid(), + vout: 0, + }, + ..Default::default() + }, + // Ours as well, but on an address the wallet has not discovered. + TxIn { + previous_output: OutPoint { + txid: Txid::from_byte_array([0xb1; 32]), + vout: 0, + }, + ..Default::default() + }, + TxIn { + previous_output: OutPoint { + txid: Txid::from_byte_array([0xb2; 32]), + vout: 0, + }, + ..Default::default() + }, + ], + output: vec![ + TxOut { + value: DENOM, + script_pubkey: far_a.script_pubkey(), + }, + TxOut { + value: DENOM, + script_pubkey: Address::dummy(NETWORK, 4).script_pubkey(), + }, + TxOut { + value: DENOM, + script_pubkey: far_b.script_pubkey(), + }, + ], + special_transaction_payload: None, + }; + let result = + fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; + + assert_eq!(result.total_received, 2 * DENOM, "both returned coins must be credited"); + assert_eq!(fx.managed.balance.total(), 2 * DENOM); + } + + /// Two wallets probing the same indices never see each other's scripts. + #[tokio::test] + async fn probe_cache_is_per_wallet() { + let mut first = fixture(); + let far = coinjoin_address(&mut first, false, 400); + assert_eq!(fund_and_mix(&mut first, far).await.total_received, DENOM); + + let mut second = fixture(); + let foreign = coinjoin_address(&mut first, false, 400); + let result = fund_and_mix(&mut second, foreign).await; + assert_eq!(result.total_received, 0, "the first wallet's address is not ours"); + } } From f7dfad1ec74546e9183758cfe9249c9882e8b6bc Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 13:55:26 +0300 Subject: [PATCH 03/24] fix(key-wallet): adapt CoinJoin recovery to dev's key-source API Co-Authored-By: Claude Opus 5.5 --- .../src/transaction_checking/coinjoin_recovery.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index f21124256..3a4d4583b 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -24,7 +24,6 @@ use dashcore::blockdata::transaction::Transaction; use dashcore::ScriptBuf; use super::account_checker::DerivedAddressInfo; -use super::transaction_router::AccountTypeToCheck; use crate::managed_account::managed_account_trait::ManagedAccountTrait; use crate::wallet::{ManagedWalletInfo, Wallet}; use crate::KeySource; @@ -53,10 +52,7 @@ static PROBE_CACHE: LazyLock>> = fn pool_key(key_source: &KeySource) -> String { match key_source { KeySource::Public(xpub) => xpub.to_string(), - KeySource::Private(xpriv) => { - crate::bip32::ExtendedPubKey::from_priv(&dashcore::secp256k1::Secp256k1::new(), xpriv) - .to_string() - } + KeySource::Private(xpriv) => crate::bip32::ExtendedPubKey::from_priv(xpriv).to_string(), _ => String::new(), } } @@ -76,7 +72,7 @@ impl ManagedWalletInfo { ) -> Vec { let mut derived = Vec::new(); - for (&index, account) in self.accounts.coinjoin_accounts.iter_mut() { + for account in self.accounts.coinjoin_accounts.values_mut() { // Our denominations spent by this transaction, and how many outputs // of each value it already pays to our pools. let mut spent: BTreeMap = BTreeMap::new(); @@ -111,13 +107,12 @@ impl ManagedWalletInfo { continue; } - let key_source = - wallet.key_source_for_account_type(&AccountTypeToCheck::CoinJoin, Some(index)); + let account_type = account.managed_account_type().to_account_type(); + let key_source = wallet.key_source_for_account(account_type); if matches!(key_source, KeySource::NoKeySource) { continue; } - let account_type = account.managed_account_type().to_account_type(); let mut extended = false; for pool in account.managed_account_type_mut().address_pools_mut() { let start = pool.highest_generated.map(|h| h + 1).unwrap_or(0); From fe85a791cc61040ded323cab31ac0b46fbe0d597 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 14:34:16 +0300 Subject: [PATCH 04/24] fix(key-wallet): keep the CoinJoin probe cache per wallet Address review: the probe cache moves from a process-global static into ManagedWalletInfo (never persisted, a clone starts empty), so no global lock is held while deriving and nothing outlives the wallet. Each chain caches only the probed window instead of every index from 0. Recovery runs only when the router includes CoinJoin accounts, tests derive their indices from the CoinJoin gap constant, and the module doc states that older blocks need a rescan. Co-Authored-By: Claude Opus 5.5 --- .../transaction_checking/coinjoin_recovery.rs | 106 +++++++++++------- .../transaction_checking/wallet_checker.rs | 2 +- .../src/wallet/managed_wallet_info/mod.rs | 8 ++ 3 files changed, 76 insertions(+), 40 deletions(-) diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index 3a4d4583b..44b735b8f 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -13,17 +13,19 @@ //! denomination but pays fewer outputs of that denomination back to our //! CoinJoin pools. For such a transaction only, the chain is derived past the //! generated end of the pool and compared against the transaction's unclaimed -//! outputs of that denomination. A match extends the pool up to it, which the -//! regular gap maintenance and the SPV rescan of newly derived scripts then -//! carry forward. +//! outputs of that denomination. A match extends the pool up to it; the regular +//! gap maintenance and SPV's matching of newly derived scripts carry it forward +//! from this block. Coins paid to the new addresses in blocks already scanned +//! are found only by a rescan from wallet creation, which is also what applies +//! this recovery to a wallet that synced before it existed. use std::collections::{BTreeMap, HashMap, HashSet}; -use std::sync::{LazyLock, Mutex}; use dashcore::blockdata::transaction::Transaction; use dashcore::ScriptBuf; use super::account_checker::DerivedAddressInfo; +use crate::managed_account::address_pool::AddressPoolType; use crate::managed_account::managed_account_trait::ManagedAccountTrait; use crate::wallet::{ManagedWalletInfo, Wallet}; use crate::KeySource; @@ -34,26 +36,36 @@ use crate::KeySource; /// heavily mixed wallets. pub const COINJOIN_RECOVERY_PROBE_WINDOW: u32 = 10_000; -/// Scripts derived for probing, per CoinJoin chain, so that the many one-sided -/// mixes of one wallet derive the window once rather than once each. The whole -/// window is always compared — a mix can return more of our coins than the -/// inputs we know about — so the cache changes cost, never the result. +/// Scripts derived for probing, per CoinJoin chain of one wallet, so that the +/// many one-sided mixes of a wallet derive the window once rather than once +/// each. The whole window is always compared — a mix can return more of our +/// coins than the inputs we know about — so the cache changes cost, never the +/// result. #[derive(Default)] -struct ProbeCache { +pub struct CoinJoinProbeCache { + chains: HashMap<(u32, AddressPoolType), ProbeRange>, +} + +/// A contiguous run of derived indices `[from, until)` of one chain. +#[derive(Default)] +struct ProbeRange { scripts: HashMap, - /// Every index below this is in `scripts`. - derived_until: u32, + from: u32, + until: u32, } -static PROBE_CACHE: LazyLock>> = - LazyLock::new(|| Mutex::new(HashMap::new())); +/// A clone starts empty: the cache is only an optimisation, and copying tens +/// of thousands of scripts with every wallet snapshot would cost more than it +/// saves. +impl Clone for CoinJoinProbeCache { + fn clone(&self) -> Self { + Self::default() + } +} -/// Identifies the chain a key source derives, for the probe cache. -fn pool_key(key_source: &KeySource) -> String { - match key_source { - KeySource::Public(xpub) => xpub.to_string(), - KeySource::Private(xpriv) => crate::bip32::ExtendedPubKey::from_priv(xpriv).to_string(), - _ => String::new(), +impl std::fmt::Debug for CoinJoinProbeCache { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CoinJoinProbeCache").field("chains", &self.chains.len()).finish() } } @@ -72,7 +84,7 @@ impl ManagedWalletInfo { ) -> Vec { let mut derived = Vec::new(); - for account in self.accounts.coinjoin_accounts.values_mut() { + for (&index, account) in self.accounts.coinjoin_accounts.iter_mut() { // Our denominations spent by this transaction, and how many outputs // of each value it already pays to our pools. let mut spent: BTreeMap = BTreeMap::new(); @@ -117,17 +129,27 @@ impl ManagedWalletInfo { for pool in account.managed_account_type_mut().address_pools_mut() { let start = pool.highest_generated.map(|h| h + 1).unwrap_or(0); let end = start.saturating_add(COINJOIN_RECOVERY_PROBE_WINDOW); - let cache_key = - format!("{:?}/{:?}/{}", pool.pool_type, pool.base_path, pool_key(&key_source)); let mut found: Vec = { - let mut caches = PROBE_CACHE.lock().unwrap_or_else(|e| e.into_inner()); - let cache = caches.entry(cache_key).or_default(); - while cache.derived_until < end { - let probe = cache.derived_until; + let range = self + .coinjoin_probe_cache + .chains + .entry((index, pool.pool_type)) + .or_default(); + // Pools only grow, so a start below the cached run means the + // pool was replaced; derive afresh from the new start. + if start < range.from || range.until == 0 { + *range = ProbeRange { + from: start, + until: start, + ..Default::default() + }; + } + while range.until < end { + let probe = range.until; match pool.generate_address_at_index(probe, &key_source, false) { Ok(address) => { - cache.scripts.insert(address.script_pubkey(), probe); - cache.derived_until += 1; + range.scripts.insert(address.script_pubkey(), probe); + range.until += 1; } Err(e) => { tracing::warn!(error = %e, "CoinJoin recovery: derivation failed"); @@ -137,7 +159,7 @@ impl ManagedWalletInfo { } unclaimed .iter() - .filter_map(|script| cache.scripts.get(script).copied()) + .filter_map(|script| range.scripts.get(script).copied()) .filter(|index| (start..end).contains(index)) .collect() }; @@ -206,6 +228,8 @@ mod tests { const NETWORK: Network = Network::Testnet; /// 0.001 DASH CoinJoin denomination, fee included. const DENOM: u64 = 100_001; + /// An index past the generated end of a fresh CoinJoin pool. + const PAST_GAP: u32 = crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; struct Fixture { wallet: Wallet, @@ -326,7 +350,7 @@ mod tests { #[tokio::test] async fn mixed_coin_returned_past_the_gap_limit_is_recovered() { let mut fx = fixture(); - let far = coinjoin_address(&mut fx, false, 350); + let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); let result = fund_and_mix(&mut fx, far.clone()).await; assert_eq!(result.total_sent, DENOM, "our input is debited"); @@ -338,11 +362,11 @@ mod tests { ); assert!( pool_end(&fx, AddressPoolType::External) - >= Some(350 + crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT), + >= Some(PAST_GAP + 250 + crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT), "the pool must extend past the recovered index by the gap limit" ); assert!( - result.new_addresses.iter().any(|d| d.info.index == 350), + result.new_addresses.iter().any(|d| d.info.index == PAST_GAP + 250), "the recovered address must be reported as newly derived" ); assert_eq!(fx.managed.balance.total(), DENOM, "nothing lost: balance equals the coin"); @@ -352,11 +376,11 @@ mod tests { #[tokio::test] async fn mixed_coin_returned_past_the_gap_limit_on_the_internal_chain_is_recovered() { let mut fx = fixture(); - let far = coinjoin_address(&mut fx, true, 250); + let far = coinjoin_address(&mut fx, true, PAST_GAP + 150); let result = fund_and_mix(&mut fx, far.clone()).await; assert_eq!(result.total_received, DENOM, "the far internal output must be credited"); - assert!(pool_end(&fx, AddressPoolType::Internal) >= Some(250)); + assert!(pool_end(&fx, AddressPoolType::Internal) >= Some(PAST_GAP + 150)); assert_eq!(fx.managed.balance.total(), DENOM); } @@ -397,7 +421,11 @@ mod tests { #[tokio::test] async fn output_past_the_probe_window_is_not_found() { let mut fx = fixture(); - let beyond = coinjoin_address(&mut fx, false, 100 + COINJOIN_RECOVERY_PROBE_WINDOW + 10); + let beyond = coinjoin_address( + &mut fx, + false, + crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT + COINJOIN_RECOVERY_PROBE_WINDOW + 10, + ); let result = fund_and_mix(&mut fx, beyond).await; assert_eq!(result.total_received, 0); } @@ -409,8 +437,8 @@ mod tests { #[tokio::test] async fn every_returned_coin_is_found_not_just_the_known_inputs() { let mut fx = fixture(); - let far_a = coinjoin_address(&mut fx, false, 300); - let far_b = coinjoin_address(&mut fx, false, 900); + let far_a = coinjoin_address(&mut fx, false, PAST_GAP + 200); + let far_b = coinjoin_address(&mut fx, false, PAST_GAP + 800); let first = coinjoin_address(&mut fx, false, 0); let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; @@ -469,11 +497,11 @@ mod tests { #[tokio::test] async fn probe_cache_is_per_wallet() { let mut first = fixture(); - let far = coinjoin_address(&mut first, false, 400); + let far = coinjoin_address(&mut first, false, PAST_GAP + 300); assert_eq!(fund_and_mix(&mut first, far).await.total_received, DENOM); let mut second = fixture(); - let foreign = coinjoin_address(&mut first, false, 400); + let foreign = coinjoin_address(&mut first, false, PAST_GAP + 300); let result = fund_and_mix(&mut second, foreign).await; assert_eq!(result.total_received, 0, "the first wallet's address is not ours"); } diff --git a/key-wallet/src/transaction_checking/wallet_checker.rs b/key-wallet/src/transaction_checking/wallet_checker.rs index 34ce1e369..268e7b805 100644 --- a/key-wallet/src/transaction_checking/wallet_checker.rs +++ b/key-wallet/src/transaction_checking/wallet_checker.rs @@ -121,7 +121,7 @@ impl WalletTransactionChecker for ManagedWalletInfo { // A CoinJoin coin of ours that came back past the pool's gap limit is // found here, before the check, so the check below credits it (32008). - let recovered = if update_state { + let recovered = if update_state && relevant_types.contains(&AccountTypeToCheck::CoinJoin) { self.recover_coinjoin_outputs(tx, wallet) } else { Vec::new() diff --git a/key-wallet/src/wallet/managed_wallet_info/mod.rs b/key-wallet/src/wallet/managed_wallet_info/mod.rs index 0d288e35f..1865f3b63 100644 --- a/key-wallet/src/wallet/managed_wallet_info/mod.rs +++ b/key-wallet/src/wallet/managed_wallet_info/mod.rs @@ -60,6 +60,11 @@ pub struct ManagedWalletInfo { /// Transactions that have received an InstantSend lock. #[cfg_attr(feature = "serde", serde(skip))] pub(crate) instant_send_locks: HashSet, + /// CoinJoin addresses derived past the pool ends while probing for lost + /// mixed coins; a cache only, never persisted. + #[cfg_attr(feature = "serde", serde(skip))] + pub(crate) coinjoin_probe_cache: + crate::transaction_checking::coinjoin_recovery::CoinJoinProbeCache, /// Outpoints observed spent by transactions seen in a processed block, /// mapped to the height of the block that spent them. /// @@ -244,6 +249,7 @@ impl ManagedWalletInfo { accounts: ManagedAccountCollection::new(), balance: WalletCoreBalance::default(), instant_send_locks: HashSet::new(), + coinjoin_probe_cache: Default::default(), observed_spent_outpoints: BTreeMap::new(), account_generation: 0, noted_chain_lock_height: None, @@ -261,6 +267,7 @@ impl ManagedWalletInfo { accounts: ManagedAccountCollection::new(), balance: WalletCoreBalance::default(), instant_send_locks: HashSet::new(), + coinjoin_probe_cache: Default::default(), observed_spent_outpoints: BTreeMap::new(), account_generation: 0, noted_chain_lock_height: None, @@ -288,6 +295,7 @@ impl ManagedWalletInfo { accounts: ManagedAccountCollection::from_account_collection(&wallet.accounts), balance: WalletCoreBalance::default(), instant_send_locks: HashSet::new(), + coinjoin_probe_cache: Default::default(), observed_spent_outpoints: BTreeMap::new(), account_generation: 0, noted_chain_lock_height: None, From ada424db9d9f39a31e7adf28c31021c3771d33cd Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 14:40:45 +0300 Subject: [PATCH 05/24] fix(key-wallet): bound the CoinJoin probe cache and report partial extensions Address review: the cache keeps only the window above the pool end and moves up with it, an index that fails to derive is not retried on every later mix, and a pool extension that fails partway keeps only the coins below the failure instead of logging them as recovered. Adds a mainnet test and a test of a recovered coin mixed again past the new pool end. Co-Authored-By: Claude Opus 5.5 --- .../transaction_checking/coinjoin_recovery.rs | 127 +++++++++++++++--- 1 file changed, 109 insertions(+), 18 deletions(-) diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index 44b735b8f..96f6d510e 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -31,9 +31,11 @@ use crate::wallet::{ManagedWalletInfo, Wallet}; use crate::KeySource; /// How far past the generated end of each CoinJoin chain a missing output is -/// searched for. Derivation only; nothing in this window is watched unless an -/// output is found in it. DashSync derived up to ~10,500 CoinJoin addresses on -/// heavily mixed wallets. +/// searched for: the widest run of empty addresses that recovery can cross in +/// one step. Each recovered coin moves the pool end, so the window travels with +/// it; DashSync derived up to ~10,500 CoinJoin addresses on heavily mixed +/// wallets. Derivation only; nothing in this window is watched unless an +/// output is found in it. pub const COINJOIN_RECOVERY_PROBE_WINDOW: u32 = 10_000; /// Scripts derived for probing, per CoinJoin chain of one wallet, so that the @@ -52,6 +54,9 @@ struct ProbeRange { scripts: HashMap, from: u32, until: u32, + /// The first index that failed to derive; nothing at or above it is tried + /// again. + failed_at: Option, } /// A clone starts empty: the cache is only an optimisation, and copying tens @@ -135,16 +140,20 @@ impl ManagedWalletInfo { .chains .entry((index, pool.pool_type)) .or_default(); - // Pools only grow, so a start below the cached run means the - // pool was replaced; derive afresh from the new start. - if start < range.from || range.until == 0 { + // The cache covers `[start, end)` only: indices below the + // pool end are watched already. A start outside the cached + // run (the pool moved past it, or was replaced) starts afresh. + if start < range.from || start > range.until { *range = ProbeRange { from: start, until: start, ..Default::default() }; + } else if start > range.from { + range.scripts.retain(|_, index| *index >= start); + range.from = start; } - while range.until < end { + while range.until < end && range.failed_at.is_none() { let probe = range.until; match pool.generate_address_at_index(probe, &key_source, false) { Ok(address) => { @@ -152,8 +161,12 @@ impl ManagedWalletInfo { range.until += 1; } Err(e) => { - tracing::warn!(error = %e, "CoinJoin recovery: derivation failed"); - break; + tracing::warn!( + error = %e, + index = probe, + "CoinJoin recovery: derivation failed, searching below it only" + ); + range.failed_at = Some(probe); } } } @@ -171,13 +184,22 @@ impl ManagedWalletInfo { let pool_type = pool.pool_type; let mut new_infos = Vec::new(); for next in start..=highest { - if pool.generate_address_at_index(next, &key_source, true).is_err() { + if let Err(e) = pool.generate_address_at_index(next, &key_source, true) { + tracing::error!( + error = %e, + index = next, + "CoinJoin recovery: failed to extend pool, coins above it stay missing" + ); + found.retain(|&index| index < next); break; } if let Some(info) = pool.info_at_index(next) { new_infos.push(info.clone()); } } + if new_infos.is_empty() { + continue; + } for &probe in &found { pool.mark_index_used(probe); } @@ -238,8 +260,12 @@ mod tests { } fn fixture() -> Fixture { + fixture_on(NETWORK) + } + + fn fixture_on(network: Network) -> Fixture { let mut wallet = - Wallet::new_random(NETWORK, WalletAccountCreationOptions::None).expect("wallet"); + Wallet::new_random(network, WalletAccountCreationOptions::None).expect("wallet"); wallet .add_account( AccountType::CoinJoin { @@ -304,7 +330,22 @@ mod tests { let funded = fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; assert_eq!(funded.total_received, DENOM, "funding must be credited"); + let spend = OutPoint { + txid: funding.txid(), + vout: 0, + }; + mix_spending(fx, spend, ours, 2).await.1 + } + /// Mixes our coin at `spend` with two foreign ones at `height`; ours comes + /// back to `ours` as output 1. Returns the mix's outpoint for ours too. + async fn mix_spending( + fx: &mut Fixture, + spend: OutPoint, + ours: Address, + height: u32, + ) -> (OutPoint, crate::transaction_checking::TransactionCheckResult) { + let network = fx.wallet.network; let foreign_input = |n: u8| TxIn { previous_output: OutPoint { txid: Txid::from_byte_array([n; 32]), @@ -318,10 +359,7 @@ mod tests { input: vec![ foreign_input(0xa1), TxIn { - previous_output: OutPoint { - txid: funding.txid(), - vout: 0, - }, + previous_output: spend, ..Default::default() }, foreign_input(0xa2), @@ -329,7 +367,7 @@ mod tests { output: vec![ TxOut { value: DENOM, - script_pubkey: Address::dummy(NETWORK, 1).script_pubkey(), + script_pubkey: Address::dummy(network, 1).script_pubkey(), }, TxOut { value: DENOM, @@ -337,12 +375,20 @@ mod tests { }, TxOut { value: DENOM, - script_pubkey: Address::dummy(NETWORK, 2).script_pubkey(), + script_pubkey: Address::dummy(network, 2).script_pubkey(), }, ], special_transaction_payload: None, }; - fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await + let result = fx + .managed + .check_core_transaction(&mix, block(height), &mut fx.wallet, true, true) + .await; + let returned = OutPoint { + txid: mix.txid(), + vout: 1, + }; + (returned, result) } /// 32008: the mixed coin comes back past the gap limit. Without recovery the @@ -505,4 +551,49 @@ mod tests { let result = fund_and_mix(&mut second, foreign).await; assert_eq!(result.total_received, 0, "the first wallet's address is not ours"); } + + /// A wallet on mainnet recovers the same way (32008 is a mainnet wallet). + #[tokio::test] + async fn mixed_coin_returned_past_the_gap_limit_is_recovered_on_mainnet() { + let mut fx = fixture_on(Network::Mainnet); + let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); + let result = fund_and_mix(&mut fx, far).await; + assert_eq!(result.total_received, DENOM, "the far output must be credited"); + assert_eq!(fx.managed.balance.total(), DENOM); + } + + /// The recovered coin is mixed again and comes back past the new pool end: + /// the second recovery reuses the cache, moved up to the new end. + #[tokio::test] + async fn a_recovered_coin_mixed_again_past_the_new_end_is_recovered() { + let mut fx = fixture(); + let first_far = coinjoin_address(&mut fx, false, PAST_GAP + 200); + let first = coinjoin_address(&mut fx, false, 0); + let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; + let spend = OutPoint { + txid: funding.txid(), + vout: 0, + }; + let (recovered, result) = mix_spending(&mut fx, spend, first_far, 2).await; + assert_eq!(result.total_received, DENOM, "the first far output must be credited"); + + let new_start = pool_end(&fx, AddressPoolType::External).expect("pool end") + 1; + let second_far = coinjoin_address(&mut fx, false, new_start + 500); + let (_, result) = mix_spending(&mut fx, recovered, second_far.clone(), 3).await; + assert_eq!(result.total_sent, DENOM, "the recovered coin is debited"); + assert_eq!(result.total_received, DENOM, "the second far output must be credited"); + assert_eq!(fx.managed.balance.total(), DENOM, "exactly one coin, at the second address"); + let account = fx.managed.first_coinjoin_managed_account().expect("account"); + assert!(account.utxos.values().any(|utxo| utxo.address == second_far)); + + let range = fx + .managed + .coinjoin_probe_cache + .chains + .get(&(0, AddressPoolType::External)) + .expect("cached chain"); + assert_eq!(range.from, new_start, "the cache moves up with the pool"); + assert!(range.scripts.values().all(|&index| index >= new_start)); + } } From 55ea942a5666a1e61fd0f26f42f942cd27e5e9b5 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 14:47:57 +0300 Subject: [PATCH 06/24] refactor(key-wallet): probe CoinJoin chains from the branch key, leave the gap to the check Address review: the probe derives each chain's branch key once, so an index costs one child derivation instead of two plus an AddressInfo. Recovery only extends the pool through the found index; the regular check marks it used and maintains the gap as it does for any output. Drops a range filter that could never remove anything. Co-Authored-By: Claude Opus 5.5 --- .../transaction_checking/coinjoin_recovery.rs | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index 96f6d510e..47f28fb85 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -74,6 +74,35 @@ impl std::fmt::Debug for CoinJoinProbeCache { } } +/// The chain's branch key (`/0` or `/1`), derived once per +/// probe so each index costs one child derivation. `None` for key sources +/// that only the pool's own derivation handles. +fn probe_branch( + key_source: &KeySource, + pool_type: AddressPoolType, +) -> Option { + let branch = match pool_type { + AddressPoolType::External => 0, + AddressPoolType::Internal => 1, + AddressPoolType::Absent | AddressPoolType::AbsentHardened => return None, + }; + let KeySource::Public(xpub) = key_source else { + return None; + }; + xpub.ckd_pub(crate::bip32::ChildNumber::from_normal_idx(branch).ok()?).ok() +} + +/// The P2PKH script of `branch`'s child `index`, as the pool derives it. +fn probe_script( + branch: &crate::bip32::ExtendedPubKey, + index: u32, + network: crate::Network, +) -> crate::Result { + let child = crate::bip32::ChildNumber::from_normal_idx(index).map_err(crate::Error::Bip32)?; + let key = branch.ckd_pub(child).map_err(crate::Error::Bip32)?; + Ok(dashcore::Address::p2pkh(&dashcore::PublicKey::new(key.public_key), network).script_pubkey()) +} + impl ManagedWalletInfo { /// Extend a CoinJoin pool to any output of `tx` that is ours but lies past /// the pool's generated end, when `tx` shows that one of our denominations @@ -153,11 +182,18 @@ impl ManagedWalletInfo { range.scripts.retain(|_, index| *index >= start); range.from = start; } + let branch = probe_branch(&key_source, pool.pool_type); while range.until < end && range.failed_at.is_none() { let probe = range.until; - match pool.generate_address_at_index(probe, &key_source, false) { - Ok(address) => { - range.scripts.insert(address.script_pubkey(), probe); + let script = match &branch { + Some(branch) => probe_script(branch, probe, pool.network), + None => pool + .generate_address_at_index(probe, &key_source, false) + .map(|address| address.script_pubkey()), + }; + match script { + Ok(script) => { + range.scripts.insert(script, probe); range.until += 1; } Err(e) => { @@ -173,7 +209,6 @@ impl ManagedWalletInfo { unclaimed .iter() .filter_map(|script| range.scripts.get(script).copied()) - .filter(|index| (start..end).contains(index)) .collect() }; found.sort_unstable(); @@ -200,17 +235,8 @@ impl ManagedWalletInfo { if new_infos.is_empty() { continue; } - for &probe in &found { - pool.mark_index_used(probe); - } - match pool.maintain_gap_limit(&key_source) { - Ok(infos) => new_infos.extend(infos), - Err(e) => tracing::error!( - error = %e, - "CoinJoin recovery: failed to maintain gap limit after extending pool" - ), - } - + // The check that follows marks the found addresses used and + // extends the gap past them, as for any other output of ours. tracing::info!( txid = %tx.txid(), pool_type = ?pool_type, From e9e33cd7a58c2815d084f66054cd530d4c3cb07f Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 15:01:34 +0300 Subject: [PATCH 07/24] fix(key-wallet): probe past the CoinJoin gap for any denomination we may own Address review: the trigger counted how many coins of a denomination came back against the known inputs, so a mix that also spent a coin of ours the wallet had not found yet could return that one inside the pool and hide the missing one, and denominations DashSync created from the main account were never looked for. Probe whenever a transaction spends any coin of ours and pays a CoinJoin denomination to a script no account of ours holds. The new app no longer mixes, so this fires while replaying history, once per process for the derivation. Co-Authored-By: Claude Opus 5.5 --- .../transaction_checking/coinjoin_recovery.rs | 197 ++++++++++++++---- .../transaction_router/mod.rs | 20 +- 2 files changed, 162 insertions(+), 55 deletions(-) diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index 47f28fb85..c7b8a6832 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -8,23 +8,26 @@ //! empty addresses wider than any fixed gap, and every mixed coin past that run //! stays invisible no matter how often the wallet rescans (ticket 32008). //! -//! A wider gap only moves the wall. Instead, the wallet reacts to the evidence -//! that a coin went missing: a transaction that spends our CoinJoin coins of a -//! denomination but pays fewer outputs of that denomination back to our -//! CoinJoin pools. For such a transaction only, the chain is derived past the -//! generated end of the pool and compared against the transaction's unclaimed -//! outputs of that denomination. A match extends the pool up to it; the regular +//! A wider gap only moves the wall. Instead, the wallet looks past it whenever +//! a coin of ours may have gone there: a transaction that spends any of our +//! coins — a mix, or DashSync creating denominations from the main account — +//! and pays a CoinJoin denomination to a script no account of ours holds. Only +//! then is the chain derived past the generated end of the pool and compared +//! against those outputs. Counting what came back is not enough: a mix can +//! also spend a coin of ours the wallet has not found yet, whose return then +//! hides the one that is missing. A match extends the pool up to it; the regular //! gap maintenance and SPV's matching of newly derived scripts carry it forward //! from this block. Coins paid to the new addresses in blocks already scanned //! are found only by a rescan from wallet creation, which is also what applies //! this recovery to a wallet that synced before it existed. -use std::collections::{BTreeMap, HashMap, HashSet}; +use std::collections::{HashMap, HashSet}; use dashcore::blockdata::transaction::Transaction; use dashcore::ScriptBuf; use super::account_checker::DerivedAddressInfo; +use super::transaction_router::COINJOIN_DENOMINATIONS; use crate::managed_account::address_pool::AddressPoolType; use crate::managed_account::managed_account_trait::ManagedAccountTrait; use crate::wallet::{ManagedWalletInfo, Wallet}; @@ -105,8 +108,8 @@ fn probe_script( impl ManagedWalletInfo { /// Extend a CoinJoin pool to any output of `tx` that is ours but lies past - /// the pool's generated end, when `tx` shows that one of our denominations - /// was spent without coming back. + /// the pool's generated end, when `tx` spends a coin of ours and pays a + /// CoinJoin denomination to a script no account of ours holds. /// /// Must run before the transaction is checked, so that the outputs found /// here are credited by the regular check. Returns the addresses added to @@ -118,41 +121,30 @@ impl ManagedWalletInfo { ) -> Vec { let mut derived = Vec::new(); - for (&index, account) in self.accounts.coinjoin_accounts.iter_mut() { - // Our denominations spent by this transaction, and how many outputs - // of each value it already pays to our pools. - let mut spent: BTreeMap = BTreeMap::new(); - for input in &tx.input { - if let Some(utxo) = account.utxos.get(&input.previous_output) { - *spent.entry(utxo.txout.value).or_default() += 1; - } - } - if spent.is_empty() { - continue; - } - - let pools = account.managed_account_type().address_pools(); - let is_ours = - |script: &ScriptBuf| pools.iter().any(|pool| pool.contains_script_pubkey(script)); - let mut returned: HashMap = HashMap::new(); - let mut unclaimed: HashSet = HashSet::new(); - for output in &tx.output { - if !spent.contains_key(&output.value) { - continue; - } - if is_ours(&output.script_pubkey) { - *returned.entry(output.value).or_default() += 1; - } else { - unclaimed.insert(output.script_pubkey.clone()); - } - } - let missing = spent - .iter() - .any(|(value, count)| returned.get(value).copied().unwrap_or(0) < *count); - if !missing || unclaimed.is_empty() { - continue; - } + let spends_ours = self.accounts.all_funding_accounts().iter().any(|account| { + tx.input.iter().any(|input| account.utxos.contains_key(&input.previous_output)) + }); + if !spends_ours { + return derived; + } + let accounts = self.accounts.all_accounts(); + let unclaimed: HashSet = tx + .output + .iter() + .filter(|output| COINJOIN_DENOMINATIONS.contains(&output.value)) + .filter(|output| { + !accounts + .iter() + .any(|account| account.contains_script_pub_key(&output.script_pubkey)) + }) + .map(|output| output.script_pubkey.clone()) + .collect(); + drop(accounts); + if unclaimed.is_empty() { + return derived; + } + for (&index, account) in self.accounts.coinjoin_accounts.iter_mut() { let account_type = account.managed_account_type().to_account_type(); let key_source = wallet.key_source_for_account(account_type); if matches!(key_source, KeySource::NoKeySource) { @@ -456,9 +448,10 @@ mod tests { assert_eq!(fx.managed.balance.total(), DENOM); } - /// A normal mix that returns the coin inside the pool derives nothing extra. + /// A normal mix that returns the coin inside the pool extends nothing: + /// the other participants' outputs are probed and found not ours. #[tokio::test] - async fn mix_returning_inside_the_pool_does_not_probe() { + async fn mix_returning_inside_the_pool_extends_nothing() { let mut fx = fixture(); let near = coinjoin_address(&mut fx, false, 5); let result = fund_and_mix(&mut fx, near).await; @@ -622,4 +615,118 @@ mod tests { assert_eq!(range.from, new_start, "the cache moves up with the pool"); assert!(range.scripts.values().all(|&index| index >= new_start)); } + + /// A mix spends a known coin of ours and one the wallet has not found yet; + /// one coin comes back inside the pool, the other past the wall. As many + /// came back as the wallet knew went in, yet one is still missing. + #[tokio::test] + async fn a_return_for_an_undiscovered_input_does_not_hide_a_far_coin() { + let mut fx = fixture(); + let near = coinjoin_address(&mut fx, false, 5); + let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); + let first = coinjoin_address(&mut fx, false, 0); + let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; + + let input = |txid: Txid| TxIn { + previous_output: OutPoint { + txid, + vout: 0, + }, + ..Default::default() + }; + let mix = dashcore::Transaction { + version: 2, + lock_time: 0, + input: vec![ + input(funding.txid()), + // Ours, on an address the wallet has not discovered. + input(Txid::from_byte_array([0xc1; 32])), + input(Txid::from_byte_array([0xc2; 32])), + ], + output: vec![ + TxOut { + value: DENOM, + script_pubkey: near.script_pubkey(), + }, + TxOut { + value: DENOM, + script_pubkey: Address::dummy(NETWORK, 5).script_pubkey(), + }, + TxOut { + value: DENOM, + script_pubkey: far.script_pubkey(), + }, + ], + special_transaction_payload: None, + }; + let result = + fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; + assert_eq!(result.total_received, 2 * DENOM, "both returned coins must be credited"); + assert_eq!(fx.managed.balance.total(), 2 * DENOM); + } + + /// DashSync created denominations from the main account onto CoinJoin + /// addresses past the wall; no CoinJoin coin is spent, yet they are ours. + #[tokio::test] + async fn denominations_created_past_the_gap_from_the_main_account_are_recovered() { + let mut fx = fixture(); + fx.wallet + .add_account( + AccountType::Standard { + index: 0, + standard_account_type: crate::account::StandardAccountType::BIP44Account, + }, + None, + ) + .expect("BIP44 account"); + fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 0); + let main_xpub = + fx.wallet.accounts.standard_bip44_accounts.get(&0).expect("BIP44 account").account_xpub; + let main = { + let account = fx.managed.first_bip44_managed_account_mut().expect("account"); + let pool = account + .managed_account_type_mut() + .address_pools_mut() + .into_iter() + .find(|pool| pool.pool_type == AddressPoolType::External) + .expect("external pool"); + pool.generate_address_at_index(0, &KeySource::Public(main_xpub), false).expect("derive") + }; + let funding = dashcore::Transaction::dummy(&main, 0..1, &[5 * DENOM]); + let funded = + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; + assert_eq!(funded.total_received, 5 * DENOM, "funding must be credited"); + + let far_a = coinjoin_address(&mut fx, false, PAST_GAP + 400); + let far_b = coinjoin_address(&mut fx, false, PAST_GAP + 401); + let create = dashcore::Transaction { + version: 2, + lock_time: 0, + input: vec![TxIn { + previous_output: OutPoint { + txid: funding.txid(), + vout: 0, + }, + ..Default::default() + }], + output: vec![ + TxOut { + value: DENOM, + script_pubkey: far_a.script_pubkey(), + }, + TxOut { + value: DENOM, + script_pubkey: far_b.script_pubkey(), + }, + ], + special_transaction_payload: None, + }; + let result = + fx.managed.check_core_transaction(&create, block(2), &mut fx.wallet, true, true).await; + assert_eq!(result.total_sent, 5 * DENOM); + assert_eq!(result.total_received, 2 * DENOM, "both denominations must be credited"); + let coinjoin = fx.managed.first_coinjoin_managed_account().expect("account"); + assert_eq!(coinjoin.utxos.len(), 2); + } } diff --git a/key-wallet/src/transaction_checking/transaction_router/mod.rs b/key-wallet/src/transaction_checking/transaction_router/mod.rs index fc91fbb54..b4c5d5958 100644 --- a/key-wallet/src/transaction_checking/transaction_router/mod.rs +++ b/key-wallet/src/transaction_checking/transaction_router/mod.rs @@ -38,6 +38,16 @@ pub enum TransactionType { /// Router for determining which accounts to check for a transaction pub struct TransactionRouter; +/// Standard CoinJoin denominations, each including the per-round fee +/// (Dash Core `coinjoin/common.h`): denom + denom/1000 + 1, with COIN = 100_000_000. +pub(crate) const COINJOIN_DENOMINATIONS: [u64; 5] = [ + 1_000_010_000, // 10 DASH + fee + 100_001_000, // 1 DASH + fee + 10_000_100, // 0.1 DASH + fee + 1_000_010, // 0.01 DASH + fee + 100_001, // 0.001 DASH + fee +]; + impl TransactionRouter { /// Classify a transaction based on its type and payload pub fn classify_transaction(tx: &Transaction) -> TransactionType { @@ -212,16 +222,6 @@ impl TransactionRouter { /// Check if transaction has denomination outputs typical of CoinJoin fn has_denomination_outputs(tx: &Transaction) -> bool { - // Standard CoinJoin denominations, each including the per-round fee - // (Dash Core `coinjoin/common.h`): denom + denom/1000 + 1, with COIN = 100_000_000. - const COINJOIN_DENOMINATIONS: [u64; 5] = [ - 1_000_010_000, // 10 DASH + fee - 100_001_000, // 1 DASH + fee - 10_000_100, // 0.1 DASH + fee - 1_000_010, // 0.01 DASH + fee - 100_001, // 0.001 DASH + fee - ]; - let mut denomination_count = 0; for output in &tx.output { if COINJOIN_DENOMINATIONS.contains(&output.value) { From 098074341752e4929b181bc8e79f487d1f81aa14 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 15:09:20 +0300 Subject: [PATCH 08/24] fix(key-wallet): probe CoinJoin collaterals and mixes seen before their funding Address review: a mix delivered before the coin it spends (rescan order) now triggers through its output to our pool, and collateral amounts (0.0001-0.0004 DASH) are probed alongside denominations. The probe cache is tied to the branch key it was derived from, and a transaction with no CoinJoin-shaped output returns before any account is scanned. Co-Authored-By: Claude Opus 5.5 --- .../transaction_checking/coinjoin_recovery.rs | 144 ++++++++++++++---- 1 file changed, 114 insertions(+), 30 deletions(-) diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index c7b8a6832..d43a292c4 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -9,9 +9,10 @@ //! stays invisible no matter how often the wallet rescans (ticket 32008). //! //! A wider gap only moves the wall. Instead, the wallet looks past it whenever -//! a coin of ours may have gone there: a transaction that spends any of our -//! coins — a mix, or DashSync creating denominations from the main account — -//! and pays a CoinJoin denomination to a script no account of ours holds. Only +//! a coin of ours may have gone there: a transaction that spends or pays any +//! coin of ours — a mix, or DashSync creating denominations and collaterals +//! from the main account — and pays a CoinJoin denomination or collateral +//! amount to a script no account of ours holds. Only //! then is the chain derived past the generated end of the pool and compared //! against those outputs. Counting what came back is not enough: a mix can //! also spend a coin of ours the wallet has not found yet, whose return then @@ -54,6 +55,8 @@ pub struct CoinJoinProbeCache { /// A contiguous run of derived indices `[from, until)` of one chain. #[derive(Default)] struct ProbeRange { + /// The branch key the scripts were derived from. + branch: Option, scripts: HashMap, from: u32, until: u32, @@ -78,8 +81,9 @@ impl std::fmt::Debug for CoinJoinProbeCache { } /// The chain's branch key (`/0` or `/1`), derived once per -/// probe so each index costs one child derivation. `None` for key sources -/// that only the pool's own derivation handles. +/// probe so each index costs one child derivation. `None` for anything but a +/// public key source on an external or internal chain, which is all a CoinJoin +/// account has. fn probe_branch( key_source: &KeySource, pool_type: AddressPoolType, @@ -95,6 +99,12 @@ fn probe_branch( xpub.ckd_pub(crate::bip32::ChildNumber::from_normal_idx(branch).ok()?).ok() } +/// A CoinJoin denomination, or a collateral amount (Dash Core +/// `CCoinJoin::IsCollateralAmount`: one to four times 0.0001 DASH). +fn is_coinjoin_amount(value: u64) -> bool { + COINJOIN_DENOMINATIONS.contains(&value) || (10_000..=40_000).contains(&value) +} + /// The P2PKH script of `branch`'s child `index`, as the pool derives it. fn probe_script( branch: &crate::bip32::ExtendedPubKey, @@ -108,8 +118,8 @@ fn probe_script( impl ManagedWalletInfo { /// Extend a CoinJoin pool to any output of `tx` that is ours but lies past - /// the pool's generated end, when `tx` spends a coin of ours and pays a - /// CoinJoin denomination to a script no account of ours holds. + /// the pool's generated end, when `tx` involves us and pays a CoinJoin + /// denomination or collateral amount to a script no account of ours holds. /// /// Must run before the transaction is checked, so that the outputs found /// here are credited by the regular check. Returns the addresses added to @@ -121,26 +131,29 @@ impl ManagedWalletInfo { ) -> Vec { let mut derived = Vec::new(); - let spends_ours = self.accounts.all_funding_accounts().iter().any(|account| { - tx.input.iter().any(|input| account.utxos.contains_key(&input.previous_output)) - }); - if !spends_ours { + // Cheap first: most transactions pay no CoinJoin-shaped amount at all. + if !tx.output.iter().any(|output| is_coinjoin_amount(output.value)) { return derived; } let accounts = self.accounts.all_accounts(); + let is_ours = |script: &ScriptBuf| { + accounts.iter().any(|account| account.contains_script_pub_key(script)) + }; let unclaimed: HashSet = tx .output .iter() - .filter(|output| COINJOIN_DENOMINATIONS.contains(&output.value)) - .filter(|output| { - !accounts - .iter() - .any(|account| account.contains_script_pub_key(&output.script_pubkey)) - }) + .filter(|output| is_coinjoin_amount(output.value) && !is_ours(&output.script_pubkey)) .map(|output| output.script_pubkey.clone()) .collect(); + // The transaction must involve us: it spends a coin of ours, or pays + // one of our addresses (its spend can arrive before the coin it spends). + let involved = !unclaimed.is_empty() + && (tx.output.iter().any(|output| is_ours(&output.script_pubkey)) + || self.accounts.all_funding_accounts().iter().any(|account| { + tx.input.iter().any(|input| account.utxos.contains_key(&input.previous_output)) + })); drop(accounts); - if unclaimed.is_empty() { + if !involved { return derived; } @@ -155,17 +168,22 @@ impl ManagedWalletInfo { for pool in account.managed_account_type_mut().address_pools_mut() { let start = pool.highest_generated.map(|h| h + 1).unwrap_or(0); let end = start.saturating_add(COINJOIN_RECOVERY_PROBE_WINDOW); + let Some(branch) = probe_branch(&key_source, pool.pool_type) else { + continue; + }; let mut found: Vec = { let range = self .coinjoin_probe_cache .chains .entry((index, pool.pool_type)) .or_default(); - // The cache covers `[start, end)` only: indices below the - // pool end are watched already. A start outside the cached - // run (the pool moved past it, or was replaced) starts afresh. - if start < range.from || start > range.until { + // The cache covers `[start, end)` of this branch key only: + // indices below the pool end are watched already. A start + // outside the cached run (the pool moved past it, or was + // replaced) or another key starts afresh. + if range.branch != Some(branch) || start < range.from || start > range.until { *range = ProbeRange { + branch: Some(branch), from: start, until: start, ..Default::default() @@ -174,16 +192,9 @@ impl ManagedWalletInfo { range.scripts.retain(|_, index| *index >= start); range.from = start; } - let branch = probe_branch(&key_source, pool.pool_type); while range.until < end && range.failed_at.is_none() { let probe = range.until; - let script = match &branch { - Some(branch) => probe_script(branch, probe, pool.network), - None => pool - .generate_address_at_index(probe, &key_source, false) - .map(|address| address.script_pubkey()), - }; - match script { + match probe_script(&branch, probe, pool.network) { Ok(script) => { range.scripts.insert(script, probe); range.until += 1; @@ -729,4 +740,77 @@ mod tests { let coinjoin = fx.managed.first_coinjoin_managed_account().expect("account"); assert_eq!(coinjoin.utxos.len(), 2); } + + /// The mix arrives before the coin it spends (rescan delivery order): no + /// input is known yet, but an output to our pool shows the mix is ours. + #[tokio::test] + async fn a_mix_seen_before_its_funding_still_recovers_the_far_coin() { + let mut fx = fixture(); + let near = coinjoin_address(&mut fx, false, 5); + let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); + let mix = dashcore::Transaction { + version: 2, + lock_time: 0, + input: (0xd1..=0xd3) + .map(|n| TxIn { + previous_output: OutPoint { + txid: Txid::from_byte_array([n; 32]), + vout: 0, + }, + ..Default::default() + }) + .collect(), + output: vec![ + TxOut { + value: DENOM, + script_pubkey: near.script_pubkey(), + }, + TxOut { + value: DENOM, + script_pubkey: Address::dummy(NETWORK, 6).script_pubkey(), + }, + TxOut { + value: DENOM, + script_pubkey: far.script_pubkey(), + }, + ], + special_transaction_payload: None, + }; + let result = + fx.managed.check_core_transaction(&mix, block(3), &mut fx.wallet, true, true).await; + assert_eq!(result.total_received, 2 * DENOM, "both of our outputs must be credited"); + } + + /// A collateral output (0.0004 DASH) past the wall is ours as well. + #[tokio::test] + async fn a_collateral_output_past_the_gap_is_recovered() { + const COLLATERAL: u64 = 40_000; + let mut fx = fixture(); + let first = coinjoin_address(&mut fx, false, 0); + let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; + let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); + let make_collateral = dashcore::Transaction { + version: 2, + lock_time: 0, + input: vec![TxIn { + previous_output: OutPoint { + txid: funding.txid(), + vout: 0, + }, + ..Default::default() + }], + output: vec![TxOut { + value: COLLATERAL, + script_pubkey: far.script_pubkey(), + }], + special_transaction_payload: None, + }; + let result = fx + .managed + .check_core_transaction(&make_collateral, block(2), &mut fx.wallet, true, true) + .await; + assert_eq!(result.total_received, COLLATERAL, "the collateral must be credited"); + assert_eq!(fx.managed.balance.total(), COLLATERAL); + } } From dafdb0860bc8ad6b075e040c1288f40e19c2e6a6 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 15:20:44 +0300 Subject: [PATCH 09/24] fix(key-wallet): credit CoinJoin coins recovered in finalized transactions Address review: a one-sided mix that a chainlock already made final went through confirm_transaction, which no longer touches UTXOs, so a coin found in it on a rescan was marked used but never credited. Credit those outputs, and let a rescan's redelivery trigger the probe through the spend it recorded. Once probing, every output no account holds is compared, so DashSync's odd change past the wall is found with the coins. The collateral range derives from the smallest denomination, and the probe cache is keyed by the account key with a plain branch key and no sticky failure state. Co-Authored-By: Claude Opus 5.5 --- .../managed_account/managed_account_ref.rs | 21 ++ .../managed_core_funds_account.rs | 18 ++ .../transaction_checking/coinjoin_recovery.rs | 246 ++++++++++++------ .../transaction_router/mod.rs | 5 + .../transaction_checking/wallet_checker.rs | 14 +- 5 files changed, 228 insertions(+), 76 deletions(-) diff --git a/key-wallet/src/managed_account/managed_account_ref.rs b/key-wallet/src/managed_account/managed_account_ref.rs index 192a358e2..17eb5b082 100644 --- a/key-wallet/src/managed_account/managed_account_ref.rs +++ b/key-wallet/src/managed_account/managed_account_ref.rs @@ -413,6 +413,27 @@ impl<'a> ManagedAccountRefMut<'a> { } } + /// See [`ManagedCoreFundsAccount::credit_outputs_of_finalized`]. No-op for + /// the [`Keys`](Self::Keys) variant (no UTXOs). + pub(crate) fn credit_outputs_of_finalized( + &mut self, + tx: &Transaction, + account_match: &AccountMatch, + context: TransactionContext, + observed_spent: &BTreeMap, + external_final_parents: &BTreeSet, + ) { + if let ManagedAccountRefMut::Funds(a) = self { + a.credit_outputs_of_finalized( + tx, + account_match, + context, + observed_spent, + external_final_parents, + ); + } + } + /// Mark all UTXOs belonging to `txid` as InstantSend-locked. /// /// Returns `true` if any UTXO was newly marked. Always returns `false` 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 e075f977c..89f09a0f2 100644 --- a/key-wallet/src/managed_account/managed_core_funds_account.rs +++ b/key-wallet/src/managed_account/managed_core_funds_account.rs @@ -788,6 +788,24 @@ impl ManagedCoreFundsAccount { record_after } + /// Credit the outputs of a transaction finalized before the wallet could + /// attribute them: CoinJoin recovery (32008) finds a coin of ours past the + /// gap limit in a mix that a chainlock already made final, where + /// [`Self::confirm_transaction`] no longer touches UTXOs. No-op for a + /// transaction that is not finalized. + pub(crate) fn credit_outputs_of_finalized( + &mut self, + tx: &Transaction, + account_match: &AccountMatch, + context: TransactionContext, + observed_spent: &BTreeMap, + external_final_parents: &BTreeSet, + ) { + if self.keys.transaction_is_finalized(&tx.txid()) { + self.update_utxos(tx, account_match, context, observed_spent, external_final_parents); + } + } + /// Record a new transaction and update UTXOs for spendable account types. /// /// `observed_spent` is the wallet-level `observed_spent_outpoints` view diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index d43a292c4..975fb20b7 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -28,7 +28,10 @@ use dashcore::blockdata::transaction::Transaction; use dashcore::ScriptBuf; use super::account_checker::DerivedAddressInfo; -use super::transaction_router::COINJOIN_DENOMINATIONS; +use super::transaction_router::{ + COINJOIN_DENOMINATIONS, COINJOIN_MAX_COLLATERAL, COINJOIN_MIN_COLLATERAL, +}; +use crate::bip32::{ChildNumber, ExtendedPubKey}; use crate::managed_account::address_pool::AddressPoolType; use crate::managed_account::managed_account_trait::ManagedAccountTrait; use crate::wallet::{ManagedWalletInfo, Wallet}; @@ -53,16 +56,14 @@ pub struct CoinJoinProbeCache { } /// A contiguous run of derived indices `[from, until)` of one chain. -#[derive(Default)] struct ProbeRange { - /// The branch key the scripts were derived from. - branch: Option, + /// The account key the run belongs to; another key starts afresh. + account_xpub: ExtendedPubKey, + /// The chain's branch key, derived once per run. + branch: ExtendedPubKey, scripts: HashMap, from: u32, until: u32, - /// The first index that failed to derive; nothing at or above it is tried - /// again. - failed_at: Option, } /// A clone starts empty: the cache is only an optimisation, and copying tens @@ -80,38 +81,29 @@ impl std::fmt::Debug for CoinJoinProbeCache { } } -/// The chain's branch key (`/0` or `/1`), derived once per -/// probe so each index costs one child derivation. `None` for anything but a -/// public key source on an external or internal chain, which is all a CoinJoin -/// account has. -fn probe_branch( - key_source: &KeySource, - pool_type: AddressPoolType, -) -> Option { - let branch = match pool_type { - AddressPoolType::External => 0, - AddressPoolType::Internal => 1, - AddressPoolType::Absent | AddressPoolType::AbsentHardened => return None, - }; - let KeySource::Public(xpub) = key_source else { - return None; - }; - xpub.ckd_pub(crate::bip32::ChildNumber::from_normal_idx(branch).ok()?).ok() +/// The branch index of a CoinJoin chain; `None` for pools a CoinJoin account +/// does not have. +fn branch_index(pool_type: AddressPoolType) -> Option { + match pool_type { + AddressPoolType::External => Some(0), + AddressPoolType::Internal => Some(1), + AddressPoolType::Absent | AddressPoolType::AbsentHardened => None, + } } -/// A CoinJoin denomination, or a collateral amount (Dash Core -/// `CCoinJoin::IsCollateralAmount`: one to four times 0.0001 DASH). +/// A CoinJoin denomination or collateral amount. fn is_coinjoin_amount(value: u64) -> bool { - COINJOIN_DENOMINATIONS.contains(&value) || (10_000..=40_000).contains(&value) + COINJOIN_DENOMINATIONS.contains(&value) + || (COINJOIN_MIN_COLLATERAL..=COINJOIN_MAX_COLLATERAL).contains(&value) } /// The P2PKH script of `branch`'s child `index`, as the pool derives it. fn probe_script( - branch: &crate::bip32::ExtendedPubKey, + branch: &ExtendedPubKey, index: u32, network: crate::Network, ) -> crate::Result { - let child = crate::bip32::ChildNumber::from_normal_idx(index).map_err(crate::Error::Bip32)?; + let child = ChildNumber::from_normal_idx(index).map_err(crate::Error::Bip32)?; let key = branch.ckd_pub(child).map_err(crate::Error::Bip32)?; Ok(dashcore::Address::p2pkh(&dashcore::PublicKey::new(key.public_key), network).script_pubkey()) } @@ -139,16 +131,25 @@ impl ManagedWalletInfo { let is_ours = |script: &ScriptBuf| { accounts.iter().any(|account| account.contains_script_pub_key(script)) }; + // Every output no account of ours holds is compared once probing; a + // CoinJoin-shaped one among them is what starts it. let unclaimed: HashSet = tx .output .iter() - .filter(|output| is_coinjoin_amount(output.value) && !is_ours(&output.script_pubkey)) + .filter(|output| !is_ours(&output.script_pubkey)) .map(|output| output.script_pubkey.clone()) .collect(); + let coinjoin_shaped_unclaimed = tx.output.iter().any(|output| { + is_coinjoin_amount(output.value) && unclaimed.contains(&output.script_pubkey) + }); // The transaction must involve us: it spends a coin of ours, or pays - // one of our addresses (its spend can arrive before the coin it spends). - let involved = !unclaimed.is_empty() + // one of our addresses (its spend can arrive before the coin it spends), + // or a rescan redelivers it after its spend of our coin was recorded. + let involved = coinjoin_shaped_unclaimed && (tx.output.iter().any(|output| is_ours(&output.script_pubkey)) + || tx.input.iter().any(|input| { + self.observed_spent_outpoints.contains_key(&input.previous_output) + }) || self.accounts.all_funding_accounts().iter().any(|account| { tx.input.iter().any(|input| account.utxos.contains_key(&input.previous_output)) })); @@ -160,60 +161,78 @@ impl ManagedWalletInfo { for (&index, account) in self.accounts.coinjoin_accounts.iter_mut() { let account_type = account.managed_account_type().to_account_type(); let key_source = wallet.key_source_for_account(account_type); - if matches!(key_source, KeySource::NoKeySource) { + let KeySource::Public(account_xpub) = key_source else { continue; - } + }; let mut extended = false; for pool in account.managed_account_type_mut().address_pools_mut() { + let Some(branch_index) = branch_index(pool.pool_type) else { + continue; + }; let start = pool.highest_generated.map(|h| h + 1).unwrap_or(0); let end = start.saturating_add(COINJOIN_RECOVERY_PROBE_WINDOW); - let Some(branch) = probe_branch(&key_source, pool.pool_type) else { - continue; + let chains = &mut self.coinjoin_probe_cache.chains; + let key = (index, pool.pool_type); + // The cache covers `[start, end)` of this account key only: + // indices below the pool end are watched already. A start + // outside the cached run (the pool moved past it, or was + // replaced) or another key starts afresh. + let fresh = match chains.get(&key) { + Some(range) => { + range.account_xpub != account_xpub + || start < range.from + || start > range.until + } + None => true, }; - let mut found: Vec = { - let range = self - .coinjoin_probe_cache - .chains - .entry((index, pool.pool_type)) - .or_default(); - // The cache covers `[start, end)` of this branch key only: - // indices below the pool end are watched already. A start - // outside the cached run (the pool moved past it, or was - // replaced) or another key starts afresh. - if range.branch != Some(branch) || start < range.from || start > range.until { - *range = ProbeRange { - branch: Some(branch), + if fresh { + let branch = match ChildNumber::from_normal_idx(branch_index) + .and_then(|child| account_xpub.ckd_pub(child)) + { + Ok(branch) => branch, + Err(e) => { + tracing::warn!(error = %e, "CoinJoin recovery: branch derivation failed"); + continue; + } + }; + chains.insert( + key, + ProbeRange { + account_xpub, + branch, + scripts: HashMap::new(), from: start, until: start, - ..Default::default() - }; - } else if start > range.from { - range.scripts.retain(|_, index| *index >= start); - range.from = start; - } - while range.until < end && range.failed_at.is_none() { - let probe = range.until; - match probe_script(&branch, probe, pool.network) { - Ok(script) => { - range.scripts.insert(script, probe); - range.until += 1; - } - Err(e) => { - tracing::warn!( - error = %e, - index = probe, - "CoinJoin recovery: derivation failed, searching below it only" - ); - range.failed_at = Some(probe); - } + }, + ); + } + let range = chains.get_mut(&key).expect("inserted above"); + if start > range.from { + range.scripts.retain(|_, index| *index >= start); + range.from = start; + } + while range.until < end { + let probe = range.until; + match probe_script(&range.branch, probe, pool.network) { + Ok(script) => { + range.scripts.insert(script, probe); + range.until += 1; + } + Err(e) => { + tracing::warn!( + error = %e, + index = probe, + "CoinJoin recovery: derivation failed, searching below it only" + ); + break; } } - unclaimed - .iter() - .filter_map(|script| range.scripts.get(script).copied()) - .collect() - }; + } + let mut found: Vec = unclaimed + .iter() + .filter_map(|script| range.scripts.get(script).copied()) + .collect(); found.sort_unstable(); let Some(&highest) = found.last() else { continue; @@ -813,4 +832,81 @@ mod tests { assert_eq!(result.total_received, COLLATERAL, "the collateral must be credited"); assert_eq!(fx.managed.balance.total(), COLLATERAL); } + + /// A wallet synced before recovery existed holds the one-sided mix as a + /// chain-locked record. A rescan redelivers it once the far coin is in + /// reach; the coin must be credited even though the record is final. + #[tokio::test] + async fn a_finalized_one_sided_mix_redelivered_by_a_rescan_is_credited() { + let mut fx = fixture(); + let far_index = PAST_GAP + COINJOIN_RECOVERY_PROBE_WINDOW + 500; + let far = coinjoin_address(&mut fx, false, far_index); + let first = coinjoin_address(&mut fx, false, 0); + let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; + let spend = OutPoint { + txid: funding.txid(), + vout: 0, + }; + let (_, before) = mix_spending(&mut fx, spend, far.clone(), 2).await; + assert_eq!(before.total_received, 0, "out of reach: booked as a one-sided loss"); + assert_eq!(fx.managed.balance.total(), 0); + + // The pool grows (other recoveries) until the far coin is in reach. + { + let key_source = fx.key_source.clone(); + let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); + let pool = account + .managed_account_type_mut() + .address_pools_mut() + .into_iter() + .find(|pool| pool.pool_type == AddressPoolType::External) + .expect("external pool"); + for index in 0..far_index - 1_000 { + pool.generate_address_at_index(index, &key_source, true).expect("derive"); + } + } + + let (_, again) = mix_spending(&mut fx, spend, far.clone(), 2).await; + assert_eq!(again.total_received, DENOM, "the far coin must be credited on redelivery"); + assert_eq!(fx.managed.balance.total(), DENOM); + } + + /// DashSync's denomination-creating transaction sent its odd change to the + /// CoinJoin internal chain past the wall; it is found with the coins. + #[tokio::test] + async fn odd_change_past_the_gap_beside_a_far_denomination_is_recovered() { + const CHANGE: u64 = 1_234_567; + let mut fx = fixture(); + let first = coinjoin_address(&mut fx, false, 0); + let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM + CHANGE]); + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; + let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); + let change = coinjoin_address(&mut fx, true, PAST_GAP + 600); + let create = dashcore::Transaction { + version: 2, + lock_time: 0, + input: vec![TxIn { + previous_output: OutPoint { + txid: funding.txid(), + vout: 0, + }, + ..Default::default() + }], + output: vec![ + TxOut { + value: DENOM, + script_pubkey: far.script_pubkey(), + }, + TxOut { + value: CHANGE, + script_pubkey: change.script_pubkey(), + }, + ], + special_transaction_payload: None, + }; + let result = + fx.managed.check_core_transaction(&create, block(2), &mut fx.wallet, true, true).await; + assert_eq!(result.total_received, DENOM + CHANGE, "the change must be credited too"); + } } diff --git a/key-wallet/src/transaction_checking/transaction_router/mod.rs b/key-wallet/src/transaction_checking/transaction_router/mod.rs index b4c5d5958..2ab1abb2d 100644 --- a/key-wallet/src/transaction_checking/transaction_router/mod.rs +++ b/key-wallet/src/transaction_checking/transaction_router/mod.rs @@ -48,6 +48,11 @@ pub(crate) const COINJOIN_DENOMINATIONS: [u64; 5] = [ 100_001, // 0.001 DASH + fee ]; +/// The CoinJoin collateral range (Dash Core `CCoinJoin::IsCollateralAmount`): +/// from the smallest denomination / 10 up to four times that. +pub(crate) const COINJOIN_MIN_COLLATERAL: u64 = COINJOIN_DENOMINATIONS[4] / 10; +pub(crate) const COINJOIN_MAX_COLLATERAL: u64 = COINJOIN_MIN_COLLATERAL * 4; + impl TransactionRouter { /// Classify a transaction based on its type and payload pub fn classify_transaction(tx: &Transaction) -> TransactionType { diff --git a/key-wallet/src/transaction_checking/wallet_checker.rs b/key-wallet/src/transaction_checking/wallet_checker.rs index 268e7b805..64e685a26 100644 --- a/key-wallet/src/transaction_checking/wallet_checker.rs +++ b/key-wallet/src/transaction_checking/wallet_checker.rs @@ -129,7 +129,8 @@ impl WalletTransactionChecker for ManagedWalletInfo { // Check only relevant account types let mut result = self.accounts.check_transaction(tx, &relevant_types); - if !recovered.is_empty() { + let recovered_any = !recovered.is_empty(); + if recovered_any { result.new_addresses.extend(recovered); result.state_modified = true; } @@ -303,6 +304,17 @@ impl WalletTransactionChecker for ManagedWalletInfo { } else { result.new_records.push(record); } + } else if recovered_any && account.transaction_is_finalized(&tx.txid()) { + // A coin recovered past the gap in a mix that was already + // final: confirming no longer updates UTXOs, so credit it. + account.credit_outputs_of_finalized( + tx, + &account_match, + context.clone(), + &self.observed_spent_outpoints, + &external_final_parents, + ); + result.state_modified = true; } } From 842651a94c040ee4f56026395cd9a21912edd132 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 15:28:32 +0300 Subject: [PATCH 10/24] fix(key-wallet): drop the finalized-transaction credit, tighten the recovery trigger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Address review: crediting every output of an already-final transaction could re-insert change a later final transaction had spent, never debited recovered coins spent by final transactions, and emitted no record — a phantom-balance risk. Final transactions are left to confirm_transaction as before; the observed-spent trigger, which matched every spend in a block, goes with it. The trigger now partitions the outputs once, and the probe cache uses the entry API instead of a panicking lookup. Co-Authored-By: Claude Opus 5.5 --- .../managed_account/managed_account_ref.rs | 21 --- .../managed_core_funds_account.rs | 18 --- .../transaction_checking/coinjoin_recovery.rs | 125 ++++++------------ .../transaction_checking/wallet_checker.rs | 14 +- 4 files changed, 42 insertions(+), 136 deletions(-) diff --git a/key-wallet/src/managed_account/managed_account_ref.rs b/key-wallet/src/managed_account/managed_account_ref.rs index 17eb5b082..192a358e2 100644 --- a/key-wallet/src/managed_account/managed_account_ref.rs +++ b/key-wallet/src/managed_account/managed_account_ref.rs @@ -413,27 +413,6 @@ impl<'a> ManagedAccountRefMut<'a> { } } - /// See [`ManagedCoreFundsAccount::credit_outputs_of_finalized`]. No-op for - /// the [`Keys`](Self::Keys) variant (no UTXOs). - pub(crate) fn credit_outputs_of_finalized( - &mut self, - tx: &Transaction, - account_match: &AccountMatch, - context: TransactionContext, - observed_spent: &BTreeMap, - external_final_parents: &BTreeSet, - ) { - if let ManagedAccountRefMut::Funds(a) = self { - a.credit_outputs_of_finalized( - tx, - account_match, - context, - observed_spent, - external_final_parents, - ); - } - } - /// Mark all UTXOs belonging to `txid` as InstantSend-locked. /// /// Returns `true` if any UTXO was newly marked. Always returns `false` 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 89f09a0f2..e075f977c 100644 --- a/key-wallet/src/managed_account/managed_core_funds_account.rs +++ b/key-wallet/src/managed_account/managed_core_funds_account.rs @@ -788,24 +788,6 @@ impl ManagedCoreFundsAccount { record_after } - /// Credit the outputs of a transaction finalized before the wallet could - /// attribute them: CoinJoin recovery (32008) finds a coin of ours past the - /// gap limit in a mix that a chainlock already made final, where - /// [`Self::confirm_transaction`] no longer touches UTXOs. No-op for a - /// transaction that is not finalized. - pub(crate) fn credit_outputs_of_finalized( - &mut self, - tx: &Transaction, - account_match: &AccountMatch, - context: TransactionContext, - observed_spent: &BTreeMap, - external_final_parents: &BTreeSet, - ) { - if self.keys.transaction_is_finalized(&tx.txid()) { - self.update_utxos(tx, account_match, context, observed_spent, external_final_parents); - } - } - /// Record a new transaction and update UTXOs for spendable account types. /// /// `observed_spent` is the wallet-level `observed_spent_outpoints` view diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index 975fb20b7..efd71ff65 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -22,6 +22,7 @@ //! are found only by a rescan from wallet creation, which is also what applies //! this recovery to a wallet that synced before it existed. +use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use dashcore::blockdata::transaction::Transaction; @@ -128,31 +129,23 @@ impl ManagedWalletInfo { return derived; } let accounts = self.accounts.all_accounts(); - let is_ours = |script: &ScriptBuf| { - accounts.iter().any(|account| account.contains_script_pub_key(script)) - }; // Every output no account of ours holds is compared once probing; a // CoinJoin-shaped one among them is what starts it. - let unclaimed: HashSet = tx - .output - .iter() - .filter(|output| !is_ours(&output.script_pubkey)) - .map(|output| output.script_pubkey.clone()) - .collect(); - let coinjoin_shaped_unclaimed = tx.output.iter().any(|output| { - is_coinjoin_amount(output.value) && unclaimed.contains(&output.script_pubkey) - }); - // The transaction must involve us: it spends a coin of ours, or pays - // one of our addresses (its spend can arrive before the coin it spends), - // or a rescan redelivers it after its spend of our coin was recorded. - let involved = coinjoin_shaped_unclaimed - && (tx.output.iter().any(|output| is_ours(&output.script_pubkey)) - || tx.input.iter().any(|input| { - self.observed_spent_outpoints.contains_key(&input.previous_output) - }) - || self.accounts.all_funding_accounts().iter().any(|account| { + let (claimed, unclaimed): (Vec<&dashcore::TxOut>, Vec<&dashcore::TxOut>) = + tx.output.iter().partition(|output| { + accounts + .iter() + .any(|account| account.contains_script_pub_key(&output.script_pubkey)) + }); + // The transaction must involve us: it pays one of our addresses (its + // spend can arrive before the coin it spends), or spends a coin of ours. + let involved = unclaimed.iter().any(|output| is_coinjoin_amount(output.value)) + && (!claimed.is_empty() + || accounts.iter().filter_map(|account| account.as_funds()).any(|account| { tx.input.iter().any(|input| account.utxos.contains_key(&input.previous_output)) })); + let unclaimed: HashSet = + unclaimed.into_iter().map(|output| output.script_pubkey.clone()).collect(); drop(accounts); if !involved { return derived; @@ -178,36 +171,39 @@ impl ManagedWalletInfo { // indices below the pool end are watched already. A start // outside the cached run (the pool moved past it, or was // replaced) or another key starts afresh. - let fresh = match chains.get(&key) { - Some(range) => { - range.account_xpub != account_xpub - || start < range.from - || start > range.until - } - None => true, - }; - if fresh { - let branch = match ChildNumber::from_normal_idx(branch_index) - .and_then(|child| account_xpub.ckd_pub(child)) + let range = match chains.entry(key) { + Entry::Occupied(entry) + if entry.get().account_xpub == account_xpub + && (entry.get().from..=entry.get().until).contains(&start) => { - Ok(branch) => branch, - Err(e) => { - tracing::warn!(error = %e, "CoinJoin recovery: branch derivation failed"); - continue; - } - }; - chains.insert( - key, - ProbeRange { + entry.into_mut() + } + entry => { + let branch = match ChildNumber::from_normal_idx(branch_index) + .and_then(|child| account_xpub.ckd_pub(child)) + { + Ok(branch) => branch, + Err(e) => { + tracing::warn!(error = %e, "CoinJoin recovery: branch derivation failed"); + continue; + } + }; + let fresh = ProbeRange { account_xpub, branch, scripts: HashMap::new(), from: start, until: start, - }, - ); - } - let range = chains.get_mut(&key).expect("inserted above"); + }; + match entry { + Entry::Occupied(mut entry) => { + entry.insert(fresh); + entry.into_mut() + } + Entry::Vacant(entry) => entry.insert(fresh), + } + } + }; if start > range.from { range.scripts.retain(|_, index| *index >= start); range.from = start; @@ -833,45 +829,6 @@ mod tests { assert_eq!(fx.managed.balance.total(), COLLATERAL); } - /// A wallet synced before recovery existed holds the one-sided mix as a - /// chain-locked record. A rescan redelivers it once the far coin is in - /// reach; the coin must be credited even though the record is final. - #[tokio::test] - async fn a_finalized_one_sided_mix_redelivered_by_a_rescan_is_credited() { - let mut fx = fixture(); - let far_index = PAST_GAP + COINJOIN_RECOVERY_PROBE_WINDOW + 500; - let far = coinjoin_address(&mut fx, false, far_index); - let first = coinjoin_address(&mut fx, false, 0); - let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - let spend = OutPoint { - txid: funding.txid(), - vout: 0, - }; - let (_, before) = mix_spending(&mut fx, spend, far.clone(), 2).await; - assert_eq!(before.total_received, 0, "out of reach: booked as a one-sided loss"); - assert_eq!(fx.managed.balance.total(), 0); - - // The pool grows (other recoveries) until the far coin is in reach. - { - let key_source = fx.key_source.clone(); - let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); - let pool = account - .managed_account_type_mut() - .address_pools_mut() - .into_iter() - .find(|pool| pool.pool_type == AddressPoolType::External) - .expect("external pool"); - for index in 0..far_index - 1_000 { - pool.generate_address_at_index(index, &key_source, true).expect("derive"); - } - } - - let (_, again) = mix_spending(&mut fx, spend, far.clone(), 2).await; - assert_eq!(again.total_received, DENOM, "the far coin must be credited on redelivery"); - assert_eq!(fx.managed.balance.total(), DENOM); - } - /// DashSync's denomination-creating transaction sent its odd change to the /// CoinJoin internal chain past the wall; it is found with the coins. #[tokio::test] diff --git a/key-wallet/src/transaction_checking/wallet_checker.rs b/key-wallet/src/transaction_checking/wallet_checker.rs index 64e685a26..268e7b805 100644 --- a/key-wallet/src/transaction_checking/wallet_checker.rs +++ b/key-wallet/src/transaction_checking/wallet_checker.rs @@ -129,8 +129,7 @@ impl WalletTransactionChecker for ManagedWalletInfo { // Check only relevant account types let mut result = self.accounts.check_transaction(tx, &relevant_types); - let recovered_any = !recovered.is_empty(); - if recovered_any { + if !recovered.is_empty() { result.new_addresses.extend(recovered); result.state_modified = true; } @@ -304,17 +303,6 @@ impl WalletTransactionChecker for ManagedWalletInfo { } else { result.new_records.push(record); } - } else if recovered_any && account.transaction_is_finalized(&tx.txid()) { - // A coin recovered past the gap in a mix that was already - // final: confirming no longer updates UTXOs, so credit it. - account.credit_outputs_of_finalized( - tx, - &account_match, - context.clone(), - &self.observed_spent_outpoints, - &external_final_parents, - ); - result.state_modified = true; } } From f125f1f3fe4df586188421f92d5f11a3e1bba2fa Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 2 Oct 2026 15:36:05 +0300 Subject: [PATCH 11/24] perf(key-wallet): key the CoinJoin probe cache by P2PKH key hash Address review: probed addresses are all P2PKH, so the cache keeps 20-byte key hashes instead of heap-allocated scripts; the unclaimed outputs are collected only once the transaction is known to involve us; the reuse-or-reset decision on the cache is a plain lookup. Co-Authored-By: Claude Opus 5.5 --- .../transaction_checking/coinjoin_recovery.rs | 94 +++++++++---------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index efd71ff65..fb988a841 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -22,11 +22,11 @@ //! are found only by a rescan from wallet creation, which is also what applies //! this recovery to a wallet that synced before it existed. -use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use dashcore::blockdata::transaction::Transaction; -use dashcore::ScriptBuf; +use dashcore::hashes::Hash; +use dashcore::{PubkeyHash, ScriptBuf}; use super::account_checker::DerivedAddressInfo; use super::transaction_router::{ @@ -62,7 +62,8 @@ struct ProbeRange { account_xpub: ExtendedPubKey, /// The chain's branch key, derived once per run. branch: ExtendedPubKey, - scripts: HashMap, + /// Every probed address is P2PKH, so its key hash identifies it. + hashes: HashMap, from: u32, until: u32, } @@ -98,15 +99,19 @@ fn is_coinjoin_amount(value: u64) -> bool { || (COINJOIN_MIN_COLLATERAL..=COINJOIN_MAX_COLLATERAL).contains(&value) } -/// The P2PKH script of `branch`'s child `index`, as the pool derives it. -fn probe_script( - branch: &ExtendedPubKey, - index: u32, - network: crate::Network, -) -> crate::Result { +/// The key hash of `branch`'s child `index`, which the pool pays as P2PKH. +fn probe_hash(branch: &ExtendedPubKey, index: u32) -> crate::Result { let child = ChildNumber::from_normal_idx(index).map_err(crate::Error::Bip32)?; let key = branch.ckd_pub(child).map_err(crate::Error::Bip32)?; - Ok(dashcore::Address::p2pkh(&dashcore::PublicKey::new(key.public_key), network).script_pubkey()) + Ok(dashcore::PublicKey::new(key.public_key).pubkey_hash()) +} + +/// The key hash a P2PKH script pays; `None` for any other script. +fn p2pkh_hash(script: &ScriptBuf) -> Option { + if !script.is_p2pkh() { + return None; + } + PubkeyHash::from_slice(&script.as_bytes()[3..23]).ok() } impl ManagedWalletInfo { @@ -144,12 +149,12 @@ impl ManagedWalletInfo { || accounts.iter().filter_map(|account| account.as_funds()).any(|account| { tx.input.iter().any(|input| account.utxos.contains_key(&input.previous_output)) })); - let unclaimed: HashSet = - unclaimed.into_iter().map(|output| output.script_pubkey.clone()).collect(); drop(accounts); if !involved { return derived; } + let unclaimed: HashSet = + unclaimed.iter().filter_map(|output| p2pkh_hash(&output.script_pubkey)).collect(); for (&index, account) in self.accounts.coinjoin_accounts.iter_mut() { let account_type = account.managed_account_type().to_account_type(); @@ -171,48 +176,43 @@ impl ManagedWalletInfo { // indices below the pool end are watched already. A start // outside the cached run (the pool moved past it, or was // replaced) or another key starts afresh. - let range = match chains.entry(key) { - Entry::Occupied(entry) - if entry.get().account_xpub == account_xpub - && (entry.get().from..=entry.get().until).contains(&start) => + let reusable = chains.get(&key).is_some_and(|range| { + range.account_xpub == account_xpub + && (range.from..=range.until).contains(&start) + }); + if !reusable { + let branch = match ChildNumber::from_normal_idx(branch_index) + .and_then(|child| account_xpub.ckd_pub(child)) { - entry.into_mut() - } - entry => { - let branch = match ChildNumber::from_normal_idx(branch_index) - .and_then(|child| account_xpub.ckd_pub(child)) - { - Ok(branch) => branch, - Err(e) => { - tracing::warn!(error = %e, "CoinJoin recovery: branch derivation failed"); - continue; - } - }; - let fresh = ProbeRange { + Ok(branch) => branch, + Err(e) => { + tracing::warn!(error = %e, "CoinJoin recovery: branch derivation failed"); + continue; + } + }; + chains.insert( + key, + ProbeRange { account_xpub, branch, - scripts: HashMap::new(), + hashes: HashMap::new(), from: start, until: start, - }; - match entry { - Entry::Occupied(mut entry) => { - entry.insert(fresh); - entry.into_mut() - } - Entry::Vacant(entry) => entry.insert(fresh), - } - } + }, + ); + } + let Some(range) = chains.get_mut(&key) else { + continue; }; if start > range.from { - range.scripts.retain(|_, index| *index >= start); + range.hashes.retain(|_, index| *index >= start); range.from = start; } while range.until < end { let probe = range.until; - match probe_script(&range.branch, probe, pool.network) { - Ok(script) => { - range.scripts.insert(script, probe); + match probe_hash(&range.branch, probe) { + Ok(hash) => { + range.hashes.insert(hash, probe); range.until += 1; } Err(e) => { @@ -225,10 +225,8 @@ impl ManagedWalletInfo { } } } - let mut found: Vec = unclaimed - .iter() - .filter_map(|script| range.scripts.get(script).copied()) - .collect(); + let mut found: Vec = + unclaimed.iter().filter_map(|hash| range.hashes.get(hash).copied()).collect(); found.sort_unstable(); let Some(&highest) = found.last() else { continue; @@ -639,7 +637,7 @@ mod tests { .get(&(0, AddressPoolType::External)) .expect("cached chain"); assert_eq!(range.from, new_start, "the cache moves up with the pool"); - assert!(range.scripts.values().all(|&index| index >= new_start)); + assert!(range.hashes.values().all(|&index| index >= new_start)); } /// A mix spends a known coin of ours and one the wallet has not found yet; From 796ee10e17ad7db45fd57258125b70af4577666c Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:43:02 +0300 Subject: [PATCH 12/24] feat(key-wallet)!: opt-in CoinJoin recovery replay instead of a permanent probe Rework of the CoinJoin gap-limit recovery (ticket 32008) along the reviewers' proposal. The probe no longer lives in every transaction check: - `WalletManager::begin_coinjoin_recovery(wallet_id, lowest_replayable_height)` (+ FFI `wallet_manager_begin_coinjoin_recovery`) drops the wallet's chain state, rewinds its checkpoint to birth with a new account generation and emits `WalletEvent::WalletReset` for that wallet only; the sync layer replays it from birth. - During the replay, transactions the regular check finds relevant have their unclaimed P2PKH outputs compared against each CoinJoin chain past the pool end; only a matched address (plus the gap after it) joins the pool. - The recovery ends at the height it started from with `WalletEvent::CoinJoinRecoveryFinished`; `cancel_coinjoin_recovery` and `coinjoin_recovery_progress` (+ FFI) cover the rest. Recoveries run one at a time, and builds funded from the wallet fail with `BuilderError::RecoveryInProgress` while it replays. - Only CoinJoin accounts get fresh pools (keeping used, handed-out and nearby addresses); other pools keep their addresses and usage. BREAKING CHANGE: `WalletInfoInterface` gains four required methods, `WalletEvent` two variants, `WalletError` two variants, `BuilderError` one, and `FFIWalletEventCallbacks` two callbacks. Co-Authored-By: Claude Opus 5.5 --- dash-spv-ffi/src/bin/ffi_cli.rs | 37 + dash-spv-ffi/src/callbacks.rs | 178 +- dash-spv-ffi/tests/dashd_sync/callbacks.rs | 3 + key-wallet-ffi/FFI_API.md | 55 +- key-wallet-ffi/src/error.rs | 9 +- key-wallet-ffi/src/wallet_manager.rs | 164 ++ key-wallet-ffi/src/wallet_manager_tests.rs | 136 ++ .../src/coinjoin_recovery_tests.rs | 265 +++ key-wallet-manager/src/error.rs | 9 + key-wallet-manager/src/events.rs | 70 + key-wallet-manager/src/lib.rs | 141 ++ key-wallet-manager/src/process_block.rs | 9 + .../managed_account_collection.rs | 17 +- .../managed_core_funds_account.rs | 26 + .../managed_core_keys_account.rs | 29 + .../transaction_checking/coinjoin_recovery.rs | 1758 ++++++++++++----- .../transaction_router/mod.rs | 25 +- .../transaction_checking/wallet_checker.rs | 26 +- .../managed_wallet_info/asset_lock_builder.rs | 3 + .../src/wallet/managed_wallet_info/mod.rs | 32 +- .../transaction_builder.rs | 17 + .../transaction_building.rs | 5 + .../wallet_info_interface.rs | 70 + 23 files changed, 2525 insertions(+), 559 deletions(-) create mode 100644 key-wallet-manager/src/coinjoin_recovery_tests.rs diff --git a/dash-spv-ffi/src/bin/ffi_cli.rs b/dash-spv-ffi/src/bin/ffi_cli.rs index b83348edc..3f178d0ad 100644 --- a/dash-spv-ffi/src/bin/ffi_cli.rs +++ b/dash-spv-ffi/src/bin/ffi_cli.rs @@ -292,6 +292,41 @@ extern "C" fn on_chain_truncated( ); } +extern "C" fn on_wallet_reset( + wallet_id: *const c_char, + synced_height: u32, + balance: *const FFIBalance, + _coinjoin_addresses: *const dash_spv_ffi::FFIDerivedAddress, + coinjoin_addresses_count: u32, + _user_data: *mut c_void, +) { + let b = read_balance(balance); + println!( + "[Wallet] Reset for CoinJoin recovery: wallet={}..., synced_height={}, coinjoin_addresses={}, balance[confirmed={}, unconfirmed={}]", + short_wallet(wallet_id), + synced_height, + coinjoin_addresses_count, + b.confirmed, + b.unconfirmed, + ); +} + +extern "C" fn on_coinjoin_recovery_finished( + wallet_id: *const c_char, + height: u32, + found: u32, + cancelled: bool, + _user_data: *mut c_void, +) { + println!( + "[Wallet] CoinJoin recovery finished: wallet={}..., height={}, found={}, cancelled={}", + short_wallet(wallet_id), + height, + found, + cancelled, + ); +} + extern "C" fn on_transaction_instant_locked( wallet_id: *const c_char, txid: *const [u8; 32], @@ -619,6 +654,8 @@ fn main() { on_sync_height_advanced: Some(on_sync_height_advanced), on_chain_lock_processed: Some(on_wallet_chain_lock_processed), on_chain_truncated: Some(on_chain_truncated), + on_wallet_reset: Some(on_wallet_reset), + on_coinjoin_recovery_finished: Some(on_coinjoin_recovery_finished), user_data: ptr::null_mut(), }, error: FFIClientErrorCallback { diff --git a/dash-spv-ffi/src/callbacks.rs b/dash-spv-ffi/src/callbacks.rs index 44eb76986..b3f739807 100644 --- a/dash-spv-ffi/src/callbacks.rs +++ b/dash-spv-ffi/src/callbacks.rs @@ -788,8 +788,9 @@ impl FFIOutPoint { /// provably beat to one of their inputs: they can never confirm, so their /// outputs are gone from the UTXO set and their records deleted. /// -/// **The only removal-shaped wallet callback.** Every other one is additive, -/// so a consumer mirroring wallet state to disk must act on this — delete the +/// **A removal-shaped wallet callback**, like `on_chain_truncated` and +/// `on_wallet_reset`; every other one is additive. A consumer mirroring +/// wallet state to disk must act on this — delete the /// named transactions and any UTXO they created. Ignoring it leaves the dead /// rows in the mirror, which replays them on the next load and re-creates a /// balance the wallet has already corrected. @@ -865,6 +866,50 @@ pub type OnChainTruncatedCallback = Option< ), >; +/// Callback for `WalletEvent::WalletReset`. +/// +/// Fires once for the one wallet a CoinJoin recovery was started on: it dropped +/// everything it learned from the chain and rewound its sync checkpoint to +/// `synced_height`, just below its birth height, to be replayed from birth. A +/// consumer mirroring wallet state to disk must delete every transaction and +/// UTXO it holds for this wallet, replace its CoinJoin address rows with the +/// `coinjoin_addresses_count` +/// entries at `coinjoin_addresses`, and lower its synced height to +/// `synced_height`. Other address rows stay, and annotations such as labels +/// should be kept keyed by txid or address. The replay then delivers the +/// history again through the usual callbacks. Other wallets are not affected. +/// +/// All pointer parameters are borrowed and only valid for the duration of the +/// callback. `balance` is the wallet's balance *after* the reset. +/// `coinjoin_addresses` is null when its count is 0. +pub type OnWalletResetCallback = Option< + extern "C" fn( + wallet_id: *const c_char, + synced_height: u32, + balance: *const FFIBalance, + coinjoin_addresses: *const FFIDerivedAddress, + coinjoin_addresses_count: u32, + user_data: *mut c_void, + ), +>; + +/// Callback for `WalletEvent::CoinJoinRecoveryFinished`. +/// +/// Fires when a CoinJoin recovery ended: its replay reached the committed sync +/// height `height` it was to run to, the wallet's coins are current again and +/// builds from it are allowed. `found` is the number of CoinJoin addresses past +/// the gap limit the replay found coins on; `cancelled` says its probe had been +/// stopped before the replay completed. +pub type OnCoinJoinRecoveryFinishedCallback = Option< + extern "C" fn( + wallet_id: *const c_char, + height: u32, + found: u32, + cancelled: bool, + user_data: *mut c_void, + ), +>; + /// Callback for `WalletEvent::TransactionInstantLocked`. /// /// Fires when an InstantSend lock is applied to a previously-seen off-chain @@ -1027,6 +1072,8 @@ pub struct FFIWalletEventCallbacks { pub on_sync_height_advanced: OnSyncHeightAdvancedCallback, pub on_chain_lock_processed: OnWalletChainLockProcessedCallback, pub on_chain_truncated: OnChainTruncatedCallback, + pub on_wallet_reset: OnWalletResetCallback, + pub on_coinjoin_recovery_finished: OnCoinJoinRecoveryFinishedCallback, pub user_data: *mut c_void, } @@ -1044,6 +1091,8 @@ impl Default for FFIWalletEventCallbacks { on_sync_height_advanced: None, on_chain_lock_processed: None, on_chain_truncated: None, + on_wallet_reset: None, + on_coinjoin_recovery_finished: None, user_data: std::ptr::null_mut(), } } @@ -1442,6 +1491,53 @@ impl FFIWalletEventCallbacks { cb(c_wallet_id.as_ptr(), *height, self.user_data); } } + WalletEvent::WalletReset { + wallet_id, + synced_height, + balance, + coinjoin_addresses, + } => { + if let Some(cb) = self.on_wallet_reset { + let wallet_id_hex = wallet_id.to_lower_hex_string(); + let c_wallet_id = CString::new(wallet_id_hex).unwrap_or_default(); + let ffi_balance = FFIBalance::from(*balance); + let ffi_addresses = FFIDerivedAddress::from_slice(coinjoin_addresses); + let addresses_ptr = if ffi_addresses.is_empty() { + ptr::null() + } else { + ffi_addresses.as_ptr() + }; + cb( + c_wallet_id.as_ptr(), + *synced_height, + &ffi_balance, + addresses_ptr, + ffi_addresses.len() as u32, + self.user_data, + ); + drop(ffi_addresses); + } else { + // Like the sweep callback: a consumer that leaves this one + // unset keeps every row of a wallet that dropped them all. + tracing::warn!( + wallet_id = %wallet_id.to_lower_hex_string(), + "no on_wallet_reset callback set; the consumer will keep mirroring \ + state the wallet dropped for its CoinJoin recovery" + ); + } + } + WalletEvent::CoinJoinRecoveryFinished { + wallet_id, + height, + found, + cancelled, + } => { + if let Some(cb) = self.on_coinjoin_recovery_finished { + let wallet_id_hex = wallet_id.to_lower_hex_string(); + let c_wallet_id = CString::new(wallet_id_hex).unwrap_or_default(); + cb(c_wallet_id.as_ptr(), *height, *found, *cancelled, self.user_data); + } + } WalletEvent::ChainLockProcessed { wallet_id, chain_lock, @@ -1594,6 +1690,84 @@ mod tests { ); } + /// The reset reaches the consumer with its wallet, rewound height and + /// balance in their places, so it deletes the right wallet's rows. + #[test] + fn test_wallet_reset_dispatch_marshals_its_fields() { + static WALLET_ID: std::sync::Mutex = std::sync::Mutex::new(String::new()); + static SYNCED_HEIGHT: AtomicU32 = AtomicU32::new(u32::MAX); + static CONFIRMED: AtomicU32 = AtomicU32::new(u32::MAX); + static ADDRESSES_NULL: AtomicU32 = AtomicU32::new(u32::MAX); + static ADDRESSES_COUNT: AtomicU32 = AtomicU32::new(u32::MAX); + + extern "C" fn cb( + wallet_id: *const c_char, + synced_height: u32, + balance: *const FFIBalance, + addresses: *const FFIDerivedAddress, + addresses_count: u32, + _user: *mut c_void, + ) { + ADDRESSES_NULL.store(u32::from(addresses.is_null()), Ordering::SeqCst); + ADDRESSES_COUNT.store(addresses_count, Ordering::SeqCst); + let id = unsafe { std::ffi::CStr::from_ptr(wallet_id) }; + *WALLET_ID.lock().expect("id") = id.to_string_lossy().into_owned(); + SYNCED_HEIGHT.store(synced_height, Ordering::SeqCst); + CONFIRMED.store(unsafe { (*balance).confirmed } as u32, Ordering::SeqCst); + } + + let callbacks = FFIWalletEventCallbacks { + on_wallet_reset: Some(cb), + ..FFIWalletEventCallbacks::default() + }; + callbacks.dispatch(&WalletEvent::WalletReset { + wallet_id: [7u8; 32], + synced_height: 1_999, + balance: WalletCoreBalance::new(42, 0, 0, 0), + coinjoin_addresses: Vec::new(), + }); + + assert_eq!(*WALLET_ID.lock().expect("id"), "07".repeat(32)); + assert_eq!(SYNCED_HEIGHT.load(Ordering::SeqCst), 1_999); + assert_eq!(CONFIRMED.load(Ordering::SeqCst), 42); + assert_eq!(ADDRESSES_NULL.load(Ordering::SeqCst), 1, "no addresses: a null pointer"); + assert_eq!(ADDRESSES_COUNT.load(Ordering::SeqCst), 0); + } + + #[test] + fn test_coinjoin_recovery_finished_dispatch_marshals_its_fields() { + static HEIGHT: AtomicU32 = AtomicU32::new(u32::MAX); + static FOUND: AtomicU32 = AtomicU32::new(u32::MAX); + static CANCELLED: AtomicU32 = AtomicU32::new(u32::MAX); + + extern "C" fn cb( + _wallet_id: *const c_char, + height: u32, + found: u32, + cancelled: bool, + _user: *mut c_void, + ) { + HEIGHT.store(height, Ordering::SeqCst); + FOUND.store(found, Ordering::SeqCst); + CANCELLED.store(u32::from(cancelled), Ordering::SeqCst); + } + + let callbacks = FFIWalletEventCallbacks { + on_coinjoin_recovery_finished: Some(cb), + ..FFIWalletEventCallbacks::default() + }; + callbacks.dispatch(&WalletEvent::CoinJoinRecoveryFinished { + wallet_id: [7u8; 32], + height: 2_400_000, + found: 3, + cancelled: true, + }); + + assert_eq!(HEIGHT.load(Ordering::SeqCst), 2_400_000); + assert_eq!(FOUND.load(Ordering::SeqCst), 3); + assert_eq!(CANCELLED.load(Ordering::SeqCst), 1); + } + /// `BlocksNeeded` dispatch must pass exactly one entry per /// `FilterMatchKey` to the FFI callback (i.e. iterate keys, not /// inflated by the per-block wallet attribution). diff --git a/dash-spv-ffi/tests/dashd_sync/callbacks.rs b/dash-spv-ffi/tests/dashd_sync/callbacks.rs index b33f78cb1..71fe67d87 100644 --- a/dash-spv-ffi/tests/dashd_sync/callbacks.rs +++ b/dash-spv-ffi/tests/dashd_sync/callbacks.rs @@ -625,6 +625,9 @@ pub(super) fn create_wallet_callbacks(tracker: &Arc) -> FFIWall // Not exercised by these tests: they never build a conflicting spend. on_transactions_swept: None, on_chain_truncated: None, + // Not exercised by these tests: they never start a CoinJoin recovery. + on_wallet_reset: None, + on_coinjoin_recovery_finished: None, user_data: Arc::as_ptr(tracker) as *mut c_void, } } diff --git a/key-wallet-ffi/FFI_API.md b/key-wallet-ffi/FFI_API.md index 3304a763c..26f0f28a0 100644 --- a/key-wallet-ffi/FFI_API.md +++ b/key-wallet-ffi/FFI_API.md @@ -4,7 +4,7 @@ This document provides a comprehensive reference for all FFI (Foreign Function I **Auto-generated**: This documentation is automatically generated from the source code. Do not edit manually. -**Total Functions**: 259 +**Total Functions**: 262 ## Table of Contents @@ -43,13 +43,16 @@ Functions: 4 ### Wallet Manager -Functions: 20 +Functions: 23 | Function | Description | Module | |----------|-------------|--------| | `wallet_manager_add_wallet_from_mnemonic` | Add a wallet from mnemonic to the manager (backward compatibility) # Safety... | wallet_manager | | `wallet_manager_add_wallet_from_mnemonic_return_serialized_bytes` | No description | wallet_manager | | `wallet_manager_add_wallet_from_mnemonic_with_options` | Add a wallet from mnemonic to the manager with options # Safety -... | wallet_manager | +| `wallet_manager_begin_coinjoin_recovery` | Start a CoinJoin recovery on one wallet Finds mixed coins that landed past... | wallet_manager | +| `wallet_manager_cancel_coinjoin_recovery` | Stop the CoinJoin recovery running on one wallet The wallet keeps what the... | wallet_manager | +| `wallet_manager_coinjoin_recovery_progress` | Report whether a CoinJoin recovery is running on one wallet Returns true... | wallet_manager | | `wallet_manager_create` | Create a new wallet manager # Safety `error` must be a valid pointer to an... | wallet_manager | | `wallet_manager_current_height` | Get current height for a network # Safety - `manager` must be a valid... | wallet_manager | | `wallet_manager_describe` | Describe the wallet manager for a given network and return a newly allocated... | wallet_manager | @@ -490,6 +493,54 @@ Add a wallet from mnemonic to the manager with options # Safety - `manager` mu --- +#### `wallet_manager_begin_coinjoin_recovery` + +```c +wallet_manager_begin_coinjoin_recovery(manager: *mut FFIWalletManager, wallet_id: *const u8, lowest_replayable_height: u32, until_height_out: *mut u32, error: *mut FFIError,) -> bool +``` + +**Description:** +Start a CoinJoin recovery on one wallet Finds mixed coins that landed past the CoinJoin pools' gap limit, which a normal sync never discovers. The wallet's chain-derived state is dropped and it is replayed from its birth height by the running SPV client; see `WalletManager::begin_coinjoin_recovery`. The reset is announced by the wallet-reset event and the end of the replay by the CoinJoin recovery-finished event. `lowest_replayable_height` is the lowest height the SPV client can replay, where its header storage starts (`0` when it syncs from genesis); a wallet born below it is refused with `InvalidState`. On success writes the committed sync height at which the recovery ends to `until_height_out` (when non-null) and returns true. Fails with `NotFound` when the wallet is unknown, and with `InvalidState` when a recovery is already running on any wallet of this manager, the wallet has no CoinJoin account, unconfirmed transactions or coins reserved by a transaction build, or nothing is known of the chain past the wallet's birth yet (sync it first). The error message says which. The recovery lives in memory only: after a restart it is gone and has to be started again (see `wallet_manager_coinjoin_recovery_progress`). Start it while the SPV client is stopped, or idle at the tip: a block the client already had in flight for this wallet can otherwise be applied to the reset wallet ahead of the replay, out of order. Until the replay is back at the returned height, transaction builds from this wallet fail. # Safety - `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `until_height_out` must be null or a valid pointer to a `u32` - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call + +**Safety:** +- `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `until_height_out` must be null or a valid pointer to a `u32` - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call + +**Module:** `wallet_manager` + +--- + +#### `wallet_manager_cancel_coinjoin_recovery` + +```c +wallet_manager_cancel_coinjoin_recovery(manager: *mut FFIWalletManager, wallet_id: *const u8, error: *mut FFIError,) -> bool +``` + +**Description:** +Stop the CoinJoin recovery running on one wallet The wallet keeps what the replay has found and goes on syncing normally; see `WalletManager::cancel_coinjoin_recovery`. Builds from the wallet stay refused until the replay completes, which the CoinJoin recovery-finished event (with `cancelled` set) reports. Returns false when no recovery probe runs on that wallet, and false with `NotFound` when the wallet is unknown. # Safety - `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call + +**Safety:** +- `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call + +**Module:** `wallet_manager` + +--- + +#### `wallet_manager_coinjoin_recovery_progress` + +```c +wallet_manager_coinjoin_recovery_progress(manager: *const FFIWalletManager, wallet_id: *const u8, until_height_out: *mut u32, found_out: *mut u32, probing_out: *mut bool, error: *mut FFIError,) -> bool +``` + +**Description:** +Report whether a CoinJoin recovery is running on one wallet Returns true while one runs, a cancelled one whose replay goes on included, writing the committed sync height at which it ends to `until_height_out`, the number of CoinJoin addresses past the gap limit found so far to `found_out`, and whether its probe still runs to `probing_out` (each when non-null). Returns false when none runs, including after a restart interrupted one, and false with `NotFound` when the wallet is unknown. # Safety - `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `until_height_out` and `found_out` must each be null or a valid pointer to a `u32` - `probing_out` must be null or a valid pointer to a `bool` - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call + +**Safety:** +- `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `until_height_out` and `found_out` must each be null or a valid pointer to a `u32` - `probing_out` must be null or a valid pointer to a `bool` - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call + +**Module:** `wallet_manager` + +--- + #### `wallet_manager_create` ```c diff --git a/key-wallet-ffi/src/error.rs b/key-wallet-ffi/src/error.rs index 24721f98d..7c98882b5 100644 --- a/key-wallet-ffi/src/error.rs +++ b/key-wallet-ffi/src/error.rs @@ -309,6 +309,8 @@ impl From for FFIError { WalletError::InvalidParameter(_) => FFIErrorCode::InvalidInput, WalletError::TransactionBuild(_) => FFIErrorCode::InvalidTransaction, WalletError::InsufficientFunds + | WalletError::InvalidState(_) + | WalletError::CoinJoinRecoveryInProgress(_) | WalletError::TruncateBelowChainLock { .. } => FFIErrorCode::InvalidState, @@ -380,8 +382,13 @@ impl From for FFIError { impl From for FFIError { fn from(value: BuilderError) -> Self { + // A replayed wallet is a state to wait out, not a bad request. + let code = match value { + BuilderError::RecoveryInProgress => FFIErrorCode::InvalidState, + _ => FFIErrorCode::TransactionBuildingError, + }; FFIError { - code: FFIErrorCode::TransactionBuildingError, + code, message: CString::new(value.to_string()) .unwrap_or( CString::new( diff --git a/key-wallet-ffi/src/wallet_manager.rs b/key-wallet-ffi/src/wallet_manager.rs index 2cb96add8..615c44cea 100644 --- a/key-wallet-ffi/src/wallet_manager.rs +++ b/key-wallet-ffi/src/wallet_manager.rs @@ -508,6 +508,170 @@ pub unsafe extern "C" fn wallet_manager_process_transaction( !result.affected_wallets.is_empty() } +/// Start a CoinJoin recovery on one wallet +/// +/// Finds mixed coins that landed past the CoinJoin pools' gap limit, which a +/// normal sync never discovers. The wallet's chain-derived state is dropped and +/// it is replayed from its birth height by the running SPV client; see +/// `WalletManager::begin_coinjoin_recovery`. The reset is announced by the +/// wallet-reset event and the end of the replay by the CoinJoin +/// recovery-finished event. +/// +/// `lowest_replayable_height` is the lowest height the SPV client can replay, +/// where its header storage starts (`0` when it syncs from genesis); a wallet +/// born below it is refused with `InvalidState`. +/// +/// On success writes the committed sync height at which the recovery ends to +/// `until_height_out` (when non-null) and returns true. Fails with `NotFound` +/// when the wallet is unknown, and with `InvalidState` when a recovery is +/// already running on any wallet of this manager, the wallet has no CoinJoin +/// account, unconfirmed +/// transactions or coins reserved by a transaction build, or nothing is known +/// of the chain past the wallet's birth yet (sync it first). The error message +/// says which. The recovery lives in memory only: after a restart it is gone +/// and has to be started again (see `wallet_manager_coinjoin_recovery_progress`). +/// +/// Start it while the SPV client is stopped, or idle at the tip: a block the +/// client already had in flight for this wallet can otherwise be applied to +/// the reset wallet ahead of the replay, out of order. Until the replay is +/// back at the returned height, transaction builds from this wallet fail. +/// +/// # Safety +/// +/// - `manager` must be a valid pointer to an FFIWalletManager instance +/// - `wallet_id` must be a valid pointer to a 32-byte wallet ID +/// - `until_height_out` must be null or a valid pointer to a `u32` +/// - `error` must be a valid pointer to an FFIError structure +/// - The caller must ensure all pointers remain valid for the duration of this call +#[no_mangle] +pub unsafe extern "C" fn wallet_manager_begin_coinjoin_recovery( + manager: *mut FFIWalletManager, + wallet_id: *const u8, + lowest_replayable_height: u32, + until_height_out: *mut u32, + error: *mut FFIError, +) -> bool { + let manager_ref = deref_ptr_mut!(manager, error); + check_ptr!(wallet_id, error); + + let mut wallet_id_array = [0u8; 32]; + ptr::copy_nonoverlapping(wallet_id, wallet_id_array.as_mut_ptr(), 32); + + let result = manager_ref.runtime.block_on(async { + let mut manager_guard = manager_ref.manager.write().await; + manager_guard.begin_coinjoin_recovery(&wallet_id_array, lowest_replayable_height) + }); + let until_height = unwrap_or_return!(result, error); + if !until_height_out.is_null() { + *until_height_out = until_height; + } + + (*error).clean(); + true +} + +/// Report whether a CoinJoin recovery is running on one wallet +/// +/// Returns true while one runs, a cancelled one whose replay goes on included, +/// writing the committed sync height at which it ends to `until_height_out`, +/// the number of CoinJoin addresses past the gap limit found so far to +/// `found_out`, and whether its probe still runs to `probing_out` (each when +/// non-null). Returns false when +/// none runs, including after a restart interrupted one, and false with +/// `NotFound` when the wallet is unknown. +/// +/// # Safety +/// +/// - `manager` must be a valid pointer to an FFIWalletManager instance +/// - `wallet_id` must be a valid pointer to a 32-byte wallet ID +/// - `until_height_out` and `found_out` must each be null or a valid pointer to a `u32` +/// - `probing_out` must be null or a valid pointer to a `bool` +/// - `error` must be a valid pointer to an FFIError structure +/// - The caller must ensure all pointers remain valid for the duration of this call +#[no_mangle] +pub unsafe extern "C" fn wallet_manager_coinjoin_recovery_progress( + manager: *const FFIWalletManager, + wallet_id: *const u8, + until_height_out: *mut u32, + found_out: *mut u32, + probing_out: *mut bool, + error: *mut FFIError, +) -> bool { + let manager_ref = deref_ptr!(manager, error); + check_ptr!(wallet_id, error); + + let mut wallet_id_array = [0u8; 32]; + ptr::copy_nonoverlapping(wallet_id, wallet_id_array.as_mut_ptr(), 32); + + let progress = manager_ref.runtime.block_on(async { + let manager_guard = manager_ref.manager.read().await; + manager_guard + .get_wallet(&wallet_id_array) + .map(|_| manager_guard.coinjoin_recovery_progress(&wallet_id_array)) + }); + let progress = unwrap_or_return!( + progress.ok_or(key_wallet_manager::WalletError::WalletNotFound(wallet_id_array)), + error + ); + + (*error).clean(); + let Some(progress) = progress else { + return false; + }; + if !until_height_out.is_null() { + *until_height_out = progress.until_height; + } + if !found_out.is_null() { + *found_out = progress.found; + } + if !probing_out.is_null() { + *probing_out = progress.probing; + } + true +} + +/// Stop the CoinJoin recovery running on one wallet +/// +/// The wallet keeps what the replay has found and goes on syncing normally; +/// see `WalletManager::cancel_coinjoin_recovery`. Builds from the wallet stay +/// refused until the replay completes, which the CoinJoin recovery-finished +/// event (with `cancelled` set) reports. Returns false when no recovery probe +/// runs on that wallet, and false with `NotFound` when the wallet is unknown. +/// +/// # Safety +/// +/// - `manager` must be a valid pointer to an FFIWalletManager instance +/// - `wallet_id` must be a valid pointer to a 32-byte wallet ID +/// - `error` must be a valid pointer to an FFIError structure +/// - The caller must ensure all pointers remain valid for the duration of this call +#[no_mangle] +pub unsafe extern "C" fn wallet_manager_cancel_coinjoin_recovery( + manager: *mut FFIWalletManager, + wallet_id: *const u8, + error: *mut FFIError, +) -> bool { + let manager_ref = deref_ptr_mut!(manager, error); + check_ptr!(wallet_id, error); + + let mut wallet_id_array = [0u8; 32]; + ptr::copy_nonoverlapping(wallet_id, wallet_id_array.as_mut_ptr(), 32); + + let cancelled = manager_ref.runtime.block_on(async { + let mut manager_guard = manager_ref.manager.write().await; + manager_guard + .get_wallet(&wallet_id_array) + .is_some() + .then(|| manager_guard.cancel_coinjoin_recovery(&wallet_id_array)) + }); + let cancelled = unwrap_or_return!( + cancelled.ok_or(key_wallet_manager::WalletError::WalletNotFound(wallet_id_array)), + error + ); + + (*error).clean(); + cancelled +} + /// Get the network for this wallet manager /// /// # Safety diff --git a/key-wallet-ffi/src/wallet_manager_tests.rs b/key-wallet-ffi/src/wallet_manager_tests.rs index 7a8239582..213b4e02d 100644 --- a/key-wallet-ffi/src/wallet_manager_tests.rs +++ b/key-wallet-ffi/src/wallet_manager_tests.rs @@ -311,6 +311,142 @@ mod tests { } } + #[test] + fn test_wallet_manager_begin_coinjoin_recovery() { + let mut error = FFIError::default(); + let error = &mut error as *mut FFIError; + + let manager = unsafe { wallet_manager::wallet_manager_create(FFINetwork::Testnet, error) }; + assert!(!manager.is_null()); + let mnemonic = CString::new(TEST_MNEMONIC).unwrap(); + let success = unsafe { + wallet_manager::wallet_manager_add_wallet_from_mnemonic( + manager, + mnemonic.as_ptr(), + error, + ) + }; + assert!(success); + let mut wallet_ids: *mut u8 = ptr::null_mut(); + let mut id_count: usize = 0; + let success = unsafe { + wallet_manager::wallet_manager_get_wallet_ids( + manager, + &mut wallet_ids, + &mut id_count, + error, + ) + }; + assert!(success); + + let unknown = [9u8; 32]; + let started = unsafe { + wallet_manager::wallet_manager_begin_coinjoin_recovery( + manager, + unknown.as_ptr(), + 0, + ptr::null_mut(), + error, + ) + }; + assert!(!started); + assert_eq!(unsafe { (*error).code }, FFIErrorCode::NotFound); + + // Nothing is known of the chain yet: there is nothing to recover. + let started = unsafe { + wallet_manager::wallet_manager_begin_coinjoin_recovery( + manager, + wallet_ids, + 0, + ptr::null_mut(), + error, + ) + }; + assert!(!started); + assert_eq!(unsafe { (*error).code }, FFIErrorCode::InvalidState); + + let mut wallet_id: WalletId = [0u8; 32]; + unsafe { + ptr::copy_nonoverlapping(wallet_ids, wallet_id.as_mut_ptr(), 32); + let manager_ref = &*manager; + manager_ref.runtime.block_on(async { + manager_ref.manager.write().await.update_wallet_synced_height(&wallet_id, 500); + }); + } + + let mut until_height = u32::MAX; + let started = unsafe { + wallet_manager::wallet_manager_begin_coinjoin_recovery( + manager, + wallet_ids, + 0, + &mut until_height, + error, + ) + }; + assert!(started); + assert_eq!(unsafe { (*error).code }, FFIErrorCode::Success); + assert_eq!(until_height, 500, "the recovery ends where the wallet had synced to"); + + // A second recovery is refused while the first runs; the out-pointer + // may be null. + let started = unsafe { + wallet_manager::wallet_manager_begin_coinjoin_recovery( + manager, + wallet_ids, + 0, + ptr::null_mut(), + error, + ) + }; + assert!(!started); + assert_eq!(unsafe { (*error).code }, FFIErrorCode::InvalidState); + + let mut until = 0u32; + let mut found = u32::MAX; + let mut probing = false; + let running = unsafe { + wallet_manager::wallet_manager_coinjoin_recovery_progress( + manager, + wallet_ids, + &mut until, + &mut found, + &mut probing, + error, + ) + }; + assert!(running); + assert_eq!((until, found, probing), (500, 0, true)); + + let cancelled = unsafe { + wallet_manager::wallet_manager_cancel_coinjoin_recovery(manager, wallet_ids, error) + }; + assert!(cancelled); + // The replay goes on without the probe. + let running = unsafe { + wallet_manager::wallet_manager_coinjoin_recovery_progress( + manager, + wallet_ids, + ptr::null_mut(), + ptr::null_mut(), + &mut probing, + error, + ) + }; + assert!(running); + assert!(!probing); + let cancelled = unsafe { + wallet_manager::wallet_manager_cancel_coinjoin_recovery(manager, wallet_ids, error) + }; + assert!(!cancelled, "nothing left to cancel"); + + unsafe { + (*error).clean(); + wallet_manager::wallet_manager_free_wallet_ids(wallet_ids, id_count); + wallet_manager::wallet_manager_free(manager); + } + } + #[test] fn test_wallet_manager_get_wallet_balance() { let mut error = FFIError::default(); diff --git a/key-wallet-manager/src/coinjoin_recovery_tests.rs b/key-wallet-manager/src/coinjoin_recovery_tests.rs new file mode 100644 index 000000000..b777defe2 --- /dev/null +++ b/key-wallet-manager/src/coinjoin_recovery_tests.rs @@ -0,0 +1,265 @@ +//! Manager-level CoinJoin recovery (ticket 32008): one wallet is reset and +//! replayed while another, already synced, stays as it is. + +use super::test_helpers::*; +use super::*; +use crate::wallet_interface::WalletInterface; +use dashcore::block::{Block, Header, Version}; +use dashcore::ephemerealdata::chain_lock::ChainLock; +use dashcore::hashes::Hash; +use dashcore::{BlockHash, CompactTarget, OutPoint, TxIn, TxMerkleNode, TxOut, Txid}; +use key_wallet::bip32::ChildNumber; +use key_wallet::wallet::initialization::WalletAccountCreationOptions; + +/// 0.001 DASH CoinJoin denomination, fee included. +const DENOM: u64 = 100_001; +/// An index past the generated end of a fresh CoinJoin pool. +const FAR: u32 = key_wallet::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT + 250; +const OTHER_MNEMONIC: &str = + "legal winner thank year wave sausage worth useful legal winner thank yellow"; + +fn block(txdata: Vec, seed: u8) -> Block { + Block { + header: Header { + version: Version::default(), + prev_blockhash: BlockHash::from_byte_array([seed; 32]), + merkle_root: TxMerkleNode::all_zeros(), + time: 1_650_000_000 + seed as u32, + bits: CompactTarget::from_consensus(0x1d00ffff), + nonce: 0, + }, + txdata, + } +} + +/// The external CoinJoin address at `index` of `wallet_id`. +fn coinjoin_address(manager: &WalletManager, wallet_id: &WalletId, index: u32) -> Address { + let wallet = manager.get_wallet(wallet_id).expect("wallet"); + let xpub = wallet.accounts.coinjoin_accounts.get(&0).expect("CoinJoin account").account_xpub; + let key = xpub + .derive_pub(&[ + ChildNumber::from_normal_idx(0).unwrap(), + ChildNumber::from_normal_idx(index).unwrap(), + ]) + .expect("derive"); + Address::p2pkh(&dashcore::PublicKey::new(key.public_key), Network::Testnet) +} + +fn tx(input: Vec, output: Vec<(u64, &Address)>) -> Transaction { + Transaction { + version: 2, + lock_time: 0, + input: input + .into_iter() + .map(|previous_output| TxIn { + previous_output, + ..Default::default() + }) + .collect(), + output: output + .into_iter() + .map(|(value, address)| TxOut { + value, + script_pubkey: address.script_pubkey(), + }) + .collect(), + special_transaction_payload: None, + } +} + +fn foreign(n: u8) -> OutPoint { + OutPoint { + txid: Txid::from_byte_array([n; 32]), + vout: 0, + } +} + +#[tokio::test] +async fn recovery_replays_one_wallet_and_leaves_the_synced_one_alone() { + let (mut manager, synced, synced_address) = setup_manager_with_wallet(); + let recovering = manager + .create_wallet_from_mnemonic(OTHER_MNEMONIC, 0, WalletAccountCreationOptions::Default) + .expect("second wallet"); + let both = BTreeSet::from([synced, recovering]); + + // Block 10 pays both wallets; block 20 mixes the recovering wallet's coin + // onto a CoinJoin address past its gap limit, where a normal sync never + // looks. + let near = coinjoin_address(&manager, &recovering, 0); + let far = coinjoin_address(&manager, &recovering, FAR); + let funding = tx(vec![foreign(0x01)], vec![(TX_AMOUNT, &synced_address), (DENOM, &near)]); + let mix = tx( + vec![ + foreign(0x02), + OutPoint { + txid: funding.txid(), + vout: 1, + }, + ], + vec![(DENOM, &Address::dummy(Network::Testnet, 1)), (DENOM, &far)], + ); + let block10 = block(vec![funding.clone()], 10); + let block20 = block(vec![mix.clone()], 20); + manager.process_block_for_wallets(&block10, block10.block_hash(), 10, &both).await; + manager.process_block_for_wallets(&block20, block20.block_hash(), 20, &both).await; + manager.update_wallet_synced_height(&synced, 100); + manager.update_wallet_synced_height(&recovering, 100); + manager.apply_chain_lock(ChainLock::dummy(100)); + assert_eq!( + manager.get_wallet_balance(&recovering).unwrap().total(), + 0, + "the far coin is missing" + ); + + let synced_info = manager.get_wallet_info(&synced).expect("info"); + let synced_balance = synced_info.balance(); + let synced_history: Vec = + synced_info.transaction_history().iter().map(|record| record.txid).collect(); + assert_eq!(synced_balance.total(), TX_AMOUNT); + + let mut rx = manager.subscribe_events(); + let until = manager.begin_coinjoin_recovery(&recovering, 0).expect("recovery starts"); + assert_eq!(until, 100); + let events = drain_events(&mut rx); + assert_eq!(events.len(), 1, "one reset event, got {:?}", events); + let WalletEvent::WalletReset { + wallet_id, + synced_height: 0, + coinjoin_addresses, + .. + } = &events[0] + else { + panic!("expected WalletReset, got {:?}", events[0]); + }; + assert_eq!(*wallet_id, recovering); + assert!( + coinjoin_addresses.iter().any(|address| address.address == near), + "the rebuilt CoinJoin pools are announced" + ); + assert!(coinjoin_addresses.iter().all(|address| address.derivation_index < FAR)); + assert!(matches!( + manager.begin_coinjoin_recovery(&synced, 0), + Err(WalletError::CoinJoinRecoveryInProgress(running)) if running == recovering + )); + assert!( + matches!( + manager.begin_coinjoin_recovery(&[9u8; 32], 0), + Err(WalletError::WalletNotFound(_)) + ), + "an unknown wallet is reported as such while another recovers" + ); + assert_eq!(manager.wallets_behind(100), BTreeSet::from([recovering])); + + // The sync layer replays the recovering wallet alone. + let only = BTreeSet::from([recovering]); + manager.process_block_for_wallets(&block10, block10.block_hash(), 10, &only).await; + manager.process_block_for_wallets(&block20, block20.block_hash(), 20, &only).await; + assert_eq!( + manager.get_wallet_balance(&recovering).unwrap().total(), + DENOM, + "the replay credits the far coin" + ); + + let synced_info = manager.get_wallet_info(&synced).expect("info"); + assert_eq!(synced_info.balance(), synced_balance, "the synced wallet's balance is untouched"); + assert_eq!( + synced_info.transaction_history().iter().map(|record| record.txid).collect::>(), + synced_history, + "the synced wallet's records are untouched" + ); + assert_eq!(synced_info.synced_height(), 100); + + assert_eq!( + manager.coinjoin_recovery_progress(&recovering).map(|progress| progress.found), + Some(1) + ); + drain_events(&mut rx); + manager.update_wallet_synced_height(&recovering, 50); + assert!(drain_events(&mut rx) + .iter() + .all(|event| !matches!(event, WalletEvent::CoinJoinRecoveryFinished { .. }))); + manager.update_wallet_synced_height(&recovering, 100); + let events = drain_events(&mut rx); + assert!(matches!( + events.last(), + Some(WalletEvent::CoinJoinRecoveryFinished { wallet_id, height: 100, found: 1, cancelled: false }) + if *wallet_id == recovering + )); + assert!(manager.begin_coinjoin_recovery(&synced, 0).is_ok(), "the next recovery may start"); +} + +#[tokio::test] +async fn recovery_of_an_unknown_wallet_fails() { + let (mut manager, _, _) = setup_manager_with_wallet(); + assert!(matches!( + manager.begin_coinjoin_recovery(&[9u8; 32], 0), + Err(WalletError::WalletNotFound(_)) + )); + assert!(!manager.cancel_coinjoin_recovery(&[9u8; 32])); +} + +#[tokio::test] +async fn a_cancelled_recovery_reports_its_end_when_the_replay_completes() { + let (mut manager, wallet_id, _) = setup_manager_with_wallet(); + manager.update_wallet_last_processed_height(&wallet_id, 300); + manager.update_wallet_synced_height(&wallet_id, 300); + let mut rx = manager.subscribe_events(); + manager.begin_coinjoin_recovery(&wallet_id, 0).expect("recovery starts"); + assert!(manager.cancel_coinjoin_recovery(&wallet_id)); + assert!(!manager.cancel_coinjoin_recovery(&wallet_id), "the probe is already off"); + drain_events(&mut rx); + + // Builds come back, and the end is reported, once the replay is done. + manager.update_wallet_synced_height(&wallet_id, 300); + assert!(matches!( + drain_events(&mut rx).last(), + Some(WalletEvent::CoinJoinRecoveryFinished { + height: 300, + found: 0, + cancelled: true, + .. + }) + )); + assert!(manager.coinjoin_recovery_progress(&wallet_id).is_none()); +} + +/// A cancelled recovery's replay still holds the shared scan pipeline, so +/// another wallet waits; the cancelled wallet itself may start over. +#[tokio::test] +async fn a_cancelled_replay_still_holds_the_slot_for_other_wallets() { + let (mut manager, first, _) = setup_manager_with_wallet(); + let second = manager + .create_wallet_from_mnemonic(OTHER_MNEMONIC, 0, WalletAccountCreationOptions::Default) + .expect("second wallet"); + manager.update_wallet_synced_height(&first, 300); + manager.update_wallet_synced_height(&second, 300); + + manager.begin_coinjoin_recovery(&first, 0).expect("recovery starts"); + assert!(manager.cancel_coinjoin_recovery(&first)); + assert!(matches!( + manager.coinjoin_recovery_progress(&first), + Some(progress) if !progress.probing + )); + assert!(matches!( + manager.begin_coinjoin_recovery(&second, 0), + Err(WalletError::CoinJoinRecoveryInProgress(running)) if running == first + )); + assert_eq!(manager.begin_coinjoin_recovery(&first, 0).expect("starts over"), 300); + + manager.update_wallet_synced_height(&first, 300); + assert!(manager.coinjoin_recovery_progress(&first).is_none(), "the replay is done"); + assert!(manager.begin_coinjoin_recovery(&second, 0).is_ok()); +} + +/// A wallet born below where the sync layer's headers start could not get its +/// history there back, so it is refused before anything is dropped. +#[tokio::test] +async fn a_wallet_born_below_the_replayable_range_is_refused() { + let (mut manager, wallet_id, _) = setup_manager_with_wallet(); + manager.update_wallet_synced_height(&wallet_id, 300); + assert!(matches!( + manager.begin_coinjoin_recovery(&wallet_id, 200), + Err(WalletError::InvalidState(_)) + )); + assert_eq!(manager.get_wallet_info(&wallet_id).expect("info").synced_height(), 300); +} diff --git a/key-wallet-manager/src/error.rs b/key-wallet-manager/src/error.rs index f644fcf57..fc59c3c70 100644 --- a/key-wallet-manager/src/error.rs +++ b/key-wallet-manager/src/error.rs @@ -29,6 +29,11 @@ pub enum WalletError { TransactionBuild(String), /// Insufficient funds InsufficientFunds, + /// The wallet's state does not allow the operation + InvalidState(String), + /// A CoinJoin recovery is already running on a wallet of this manager; + /// recoveries run one at a time + CoinJoinRecoveryInProgress(WalletId), /// A truncation below a wallet's applied ChainLock, whose blocks are final TruncateBelowChainLock { /// The wallet that refused the truncation @@ -54,6 +59,10 @@ impl core::fmt::Display for WalletError { WalletError::InvalidParameter(msg) => write!(f, "Invalid parameter: {}", msg), WalletError::TransactionBuild(err) => write!(f, "Transaction build failed: {}", err), WalletError::InsufficientFunds => write!(f, "Insufficient funds"), + WalletError::InvalidState(msg) => write!(f, "Invalid state: {}", msg), + WalletError::CoinJoinRecoveryInProgress(id) => { + write!(f, "A CoinJoin recovery is already running on wallet {:x}", id.as_hex()) + } WalletError::TruncateBelowChainLock { wallet_id, height, diff --git a/key-wallet-manager/src/events.rs b/key-wallet-manager/src/events.rs index a1861f679..ba6980b0e 100644 --- a/key-wallet-manager/src/events.rs +++ b/key-wallet-manager/src/events.rs @@ -427,6 +427,46 @@ pub enum WalletEvent { /// the parent transaction). locked_transactions: BTreeMap>, }, + /// The wallet dropped everything it learned from the chain and rewound its + /// sync checkpoint to `synced_height`, just below its birth height, to be + /// replayed from birth by a CoinJoin recovery + /// ([`crate::WalletManager::begin_coinjoin_recovery`]). No other event + /// reports what was dropped, and other wallets are not affected. + /// + /// A consumer mirroring wallet state to disk must delete every + /// transaction and UTXO it holds for this wallet, replace its CoinJoin + /// address rows with `coinjoin_addresses`, and lower its synced height to + /// `synced_height`. The CoinJoin pools are rebuilt so the wallet stops + /// watching empty runs; every other address row stays as it is, as the + /// wallet keeps those addresses and their usage. Addresses the replay + /// derives anew arrive in the usual events. Annotations the consumer + /// keeps beside the rows (labels, notes) should survive keyed by txid or + /// address: the replay delivers the same transactions again, without them. + WalletReset { + /// ID of the affected wallet. + wallet_id: WalletId, + /// The rewound sync checkpoint: one below the wallet's birth height, or + /// lower when the wallet had not synced that far. + synced_height: CoreBlockHeight, + /// Wallet balance after the reset. + balance: WalletCoreBalance, + /// Every address the rebuilt CoinJoin pools hold. + coinjoin_addresses: Vec, + }, + /// A CoinJoin recovery ended: its replay reached the committed sync height + /// it was to run to, the wallet's coins are current again and builds from + /// it are allowed. `cancelled` says its probe had been stopped before + /// ([`crate::WalletManager::cancel_coinjoin_recovery`]). + CoinJoinRecoveryFinished { + /// ID of the affected wallet. + wallet_id: WalletId, + /// The committed sync height the replay reached. + height: CoreBlockHeight, + /// CoinJoin addresses past the gap limit the replay found coins on. + found: u32, + /// Whether the probe had been cancelled before the replay completed. + cancelled: bool, + }, } impl WalletEvent { @@ -460,6 +500,14 @@ impl WalletEvent { | WalletEvent::ChainLockProcessed { wallet_id, .. + } + | WalletEvent::WalletReset { + wallet_id, + .. + } + | WalletEvent::CoinJoinRecoveryFinished { + wallet_id, + .. } => *wallet_id, } } @@ -574,6 +622,28 @@ impl fmt::Display for WalletEvent { total_txids, ) } + WalletEvent::WalletReset { + synced_height, + balance, + coinjoin_addresses, + .. + } => write!( + f, + "WalletReset(synced_height={}, balance={}, coinjoin_addresses={})", + synced_height, + balance, + coinjoin_addresses.len() + ), + WalletEvent::CoinJoinRecoveryFinished { + height, + found, + cancelled, + .. + } => write!( + f, + "CoinJoinRecoveryFinished(height={}, found={}, cancelled={})", + height, found, cancelled + ), } } } diff --git a/key-wallet-manager/src/lib.rs b/key-wallet-manager/src/lib.rs index 798f1ce17..45c966e97 100644 --- a/key-wallet-manager/src/lib.rs +++ b/key-wallet-manager/src/lib.rs @@ -28,6 +28,7 @@ use dashcore::blockdata::transaction::{OutPoint, Transaction}; use dashcore::prelude::CoreBlockHeight; use dashcore::Txid; use key_wallet::account::AccountCollection; +use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; use key_wallet::managed_account::transaction_record::TransactionRecord; use key_wallet::transaction_checking::{DerivedAddressInfo, TransactionContext}; use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionStrategy; @@ -220,6 +221,144 @@ impl WalletManager { } } + /// Start a CoinJoin recovery on one wallet: find mixed coins that landed + /// past the CoinJoin pools' gap limit, which a normal sync never discovers + /// (ticket 32008). + /// + /// The wallet's chain-derived state is dropped and its sync checkpoint is + /// rewound to just below its birth height, announced by one + /// [`WalletEvent::WalletReset`] for this wallet only. The sync layer then + /// sees the wallet behind and replays it from birth, from the filters and + /// blocks it already holds where it can, while the wallet looks for its + /// CoinJoin addresses past the gap in every transaction it finds relevant. + /// Once its committed sync height reaches the returned height, it ends + /// with a [`WalletEvent::CoinJoinRecoveryFinished`]. Nothing about it is + /// persisted: a restart ends it without that event, and + /// [`Self::coinjoin_recovery_progress`] then reports nothing either, so a + /// caller that must see it through keeps its own "pending" flag until the + /// event arrives and starts the recovery again after a restart. + /// + /// While it runs the wallet's coins are those of a past height, so building + /// a transaction funded from its accounts fails with + /// `BuilderError::RecoveryInProgress`. + /// + /// A block the sync layer already had in flight for this wallet when it + /// is reset can still be applied to the reset wallet ahead of the replay, + /// out of order. Start a recovery while the SPV client is stopped, or idle + /// at the tip, to rule that out. + /// + /// Wallets not being recovered keep their state, but the scan pipeline is + /// shared: the replay restarts in-flight scan work, and confirmations for + /// the other wallets can wait until it catches up. Recoveries therefore run + /// one at a time, ideally right after the first sync. + /// + /// `lowest_replayable_height` is the lowest height the sync layer can + /// replay, where its header storage starts (`0` for a sync from genesis); + /// a wallet born below it is refused, since the reset would drop history + /// the replay cannot bring back. + /// + /// Returns the committed sync height at which the recovery will end: the + /// highest of the wallet's own synced height and chainlocks seen, and of + /// any other wallet's synced height or applied chainlock. Fails when the wallet is unknown, a recovery is + /// already running on any wallet of this manager, the wallet has no + /// CoinJoin account, unconfirmed transactions or coins reserved by a + /// transaction build, was born below `lowest_replayable_height`, or + /// nothing is known of the chain past the wallet's birth yet. + pub fn begin_coinjoin_recovery( + &mut self, + wallet_id: &WalletId, + lowest_replayable_height: CoreBlockHeight, + ) -> Result { + let wallet = self.wallets.get(wallet_id).ok_or(WalletError::WalletNotFound(*wallet_id))?; + // The reset drops the history down to the wallet's birth, and the sync + // layer replays only from where its headers start: anything between + // would be lost for good. + let birth_height = self + .wallet_infos + .get(wallet_id) + .ok_or(WalletError::WalletNotFound(*wallet_id))? + .birth_height(); + if birth_height < lowest_replayable_height { + return Err(WalletError::InvalidState(format!( + "CoinJoin recovery: the wallet was born at {birth_height}, below {lowest_replayable_height}, \ + the lowest height the sync can replay; its history there would be lost" + ))); + } + // One replay at a time, a cancelled one included: it still holds the + // scan pipeline. Only a cancelled replay of this very wallet may be + // started over. + if let Some((running, _)) = self.wallet_infos.iter().find(|(id, info)| { + info.coinjoin_recovery_progress() + .is_some_and(|progress| *id != wallet_id || progress.probing) + }) { + return Err(WalletError::CoinJoinRecoveryInProgress(*running)); + } + // What the other wallets know of the chain: a wallet imported moments + // ago may know nothing itself yet. Its own heights are its own call. + let known_height = self + .wallet_infos + .iter() + .filter(|(id, _)| *id != wallet_id) + .map(|(_, info)| { + info.synced_height() + .max(info.last_applied_chain_lock().map_or(0, |cl| cl.block_height)) + }) + .max() + .unwrap_or(0); + let info = + self.wallet_infos.get_mut(wallet_id).ok_or(WalletError::WalletNotFound(*wallet_id))?; + let until_height = + info.begin_coinjoin_recovery(wallet, known_height).map_err(|err| match err { + key_wallet::Error::InvalidState(msg) => WalletError::InvalidState(msg), + err => err.into(), + })?; + let coinjoin_addresses = events::project_derived_addresses( + info.accounts().coinjoin_accounts.values().flat_map(|account| { + let account_type = account.managed_account_type().to_account_type(); + account.managed_account_type().address_pools().into_iter().flat_map(move |pool| { + pool.addresses.values().map(move |info| DerivedAddressInfo { + account_type, + pool_type: pool.pool_type, + info: info.clone(), + }) + }) + }), + ); + let event = WalletEvent::WalletReset { + wallet_id: *wallet_id, + synced_height: info.synced_height(), + balance: info.balance(), + coinjoin_addresses, + }; + self.emit_event(event); + Ok(until_height) + } + + /// Where the CoinJoin recovery running on `wallet_id` stands, including a + /// cancelled one whose replay goes on; `None` when none runs. The state is in memory only, so after a restart this is + /// `None` and an interrupted recovery has to be started again. + pub fn coinjoin_recovery_progress( + &self, + wallet_id: &WalletId, + ) -> Option { + self.wallet_infos.get(wallet_id)?.coinjoin_recovery_progress() + } + + /// Stop the probe of the CoinJoin recovery running on `wallet_id` before + /// its replay is complete. The wallet keeps what the probe has found and + /// the replay goes on as a normal sync; coins past the gap limit in the + /// rest of its history stay missing until a recovery runs again. Builds + /// stay refused until the replay is back at the height the recovery was + /// to run to, since until then the coins are those of a past height; the + /// [`WalletEvent::CoinJoinRecoveryFinished`] at that point, with + /// `cancelled` set, says when they are allowed again. Returns `false` when + /// no probe runs on that wallet. + pub fn cancel_coinjoin_recovery(&mut self, wallet_id: &WalletId) -> bool { + self.wallet_infos + .get_mut(wallet_id) + .is_some_and(|info| info.cancel_coinjoin_recovery().is_some()) + } + /// Emit a wallet event to BOTH the incidental broadcast fan-out and the /// lossless persistence channel (dashpay/platform#4069). /// @@ -795,6 +934,8 @@ fn current_timestamp() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs() } +#[cfg(test)] +mod coinjoin_recovery_tests; #[cfg(test)] mod event_tests; #[cfg(test)] diff --git a/key-wallet-manager/src/process_block.rs b/key-wallet-manager/src/process_block.rs index f0b8e0ce0..05b5c2f94 100644 --- a/key-wallet-manager/src/process_block.rs +++ b/key-wallet-manager/src/process_block.rs @@ -384,10 +384,19 @@ impl WalletInterface for WalletM if let Some(info) = self.wallet_infos.get_mut(wallet_id) { if height > info.synced_height() { info.update_synced_height(height); + let recovery_found = info.take_finished_coinjoin_recovery(); self.emit_event(WalletEvent::SyncHeightAdvanced { wallet_id: *wallet_id, height, }); + if let Some(outcome) = recovery_found { + self.emit_event(WalletEvent::CoinJoinRecoveryFinished { + wallet_id: *wallet_id, + height, + found: outcome.found, + cancelled: !outcome.probing, + }); + } } } } diff --git a/key-wallet/src/managed_account/managed_account_collection.rs b/key-wallet/src/managed_account/managed_account_collection.rs index 51742dd52..28c5570f3 100644 --- a/key-wallet/src/managed_account/managed_account_collection.rs +++ b/key-wallet/src/managed_account/managed_account_collection.rs @@ -499,13 +499,18 @@ impl ManagedAccountCollection { fn create_managed_funds_account_from_account( account: &Account, ) -> Result { - let key_source = KeySource::Public(account.account_xpub); - let managed_type = ManagedAccountType::from_account_type( - account.account_type, + Ok(ManagedCoreFundsAccount::new( + Self::fresh_managed_account_type(account)?, account.network, - &key_source, - )?; - Ok(ManagedCoreFundsAccount::new(managed_type, account.network)) + )) + } + + /// The address pools a funds account built from `account` starts with. + pub(crate) fn fresh_managed_account_type( + account: &Account, + ) -> Result { + let key_source = KeySource::Public(account.account_xpub); + ManagedAccountType::from_account_type(account.account_type, account.network, &key_source) } /// Create a keys-only managed account from an [`Account`]. 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 fa67530de..76c0ff361 100644 --- a/key-wallet/src/managed_account/managed_core_funds_account.rs +++ b/key-wallet/src/managed_account/managed_core_funds_account.rs @@ -73,6 +73,10 @@ pub struct ManagedCoreFundsAccount { /// re-establish which coins are spent. #[cfg_attr(feature = "serde", serde(skip))] reservations: ReservationSet, + /// Set while a CoinJoin recovery replays the wallet: the UTXOs are those + /// of a past height, so no transaction build may fund from them. + #[cfg_attr(feature = "serde", serde(skip))] + pub(crate) replaying: bool, } /// What [`ManagedCoreFundsAccount::apply_abandon`] removed from one account. @@ -111,9 +115,29 @@ impl ManagedCoreFundsAccount { spent_before_funded: BTreeMap::new(), unsettled_spends: BTreeMap::new(), reservations: ReservationSet::default(), + replaying: false, } } + /// Forget everything this account learned from the chain, for a replay + /// from wallet birth; see [`ManagedCoreKeysAccount::reset_chain_state`]. + /// Reservations are left alone: a caller resets only an account no build + /// holds coins of. + pub(crate) fn reset_chain_state(&mut self, fresh_type: Option) { + self.keys.reset_chain_state(fresh_type); + self.balance = WalletCoreBalance::default(); + self.utxos.clear(); + self.spent_outpoints.clear(); + self.spent_before_funded.clear(); + self.unsettled_spends.clear(); + } + + /// Whether a transaction build holds coins of this account at + /// `current_height`. + pub(crate) fn has_reservations(&self, current_height: u32) -> bool { + !self.reservations.reserved(current_height).is_empty() + } + /// Create a `ManagedCoreFundsAccount` from an [`Account`](super::super::Account). pub fn from_account(account: &super::super::Account) -> Self { Self::wrap(ManagedCoreKeysAccount::from_account(account)) @@ -140,6 +164,7 @@ impl ManagedCoreFundsAccount { spent_before_funded: BTreeMap::new(), unsettled_spends: BTreeMap::new(), reservations: ReservationSet::default(), + replaying: false, } } @@ -1454,6 +1479,7 @@ impl<'de> Deserialize<'de> for ManagedCoreFundsAccount { spent_before_funded: helper.spent_before_funded, unsettled_spends: helper.unsettled_spends, reservations: ReservationSet::default(), + replaying: false, }) } } diff --git a/key-wallet/src/managed_account/managed_core_keys_account.rs b/key-wallet/src/managed_account/managed_core_keys_account.rs index 1d16995e4..9f4531f14 100644 --- a/key-wallet/src/managed_account/managed_core_keys_account.rs +++ b/key-wallet/src/managed_account/managed_core_keys_account.rs @@ -195,6 +195,35 @@ impl ManagedCoreKeysAccount { promoted } + /// Forget everything this account learned from the chain, for a replay + /// from wallet birth: its transaction history, finalized txids and + /// per-address totals. `fresh_type` replaces the address pools with newly + /// created ones; without it the pools keep their addresses and which of + /// them were used or handed out. + pub(crate) fn reset_chain_state(&mut self, fresh_type: Option) { + match fresh_type { + Some(fresh_type) => self.managed_account_type = fresh_type, + None => { + // Which addresses were used or handed out stays as it was: the + // replay marks the same ones used again, and clearing it would + // hand an address paid years ago out as fresh meanwhile. Only + // the per-address totals, which the replay adds up again, go. + for pool in self.managed_account_type.address_pools_mut() { + for info in pool.addresses.values_mut() { + info.tx_count = 0; + info.total_received = 0; + info.total_sent = 0; + info.balance = 0; + } + } + } + } + self.transactions.clear(); + #[cfg(not(feature = "keep-finalized-transactions"))] + self.finalized_txids.clear(); + self.monitor_revision += 1; + } + /// Create a `ManagedCoreKeysAccount` from an [`Account`](super::super::Account). pub fn from_account(account: &super::super::Account) -> Self { let key_source = address_pool::KeySource::Public(account.account_xpub); diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index fb988a841..d6aec52e3 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -1,4 +1,5 @@ -//! Recovery of CoinJoin outputs that landed past the address pool's gap limit. +//! Opt-in recovery of CoinJoin coins that landed past the address pool's gap +//! limit. //! //! CoinJoin discovery walks each CoinJoin chain forward and stops after //! `gap_limit` unused addresses. A wallet migrated from DashSync breaks that @@ -8,78 +9,134 @@ //! empty addresses wider than any fixed gap, and every mixed coin past that run //! stays invisible no matter how often the wallet rescans (ticket 32008). //! -//! A wider gap only moves the wall. Instead, the wallet looks past it whenever -//! a coin of ours may have gone there: a transaction that spends or pays any -//! coin of ours — a mix, or DashSync creating denominations and collaterals -//! from the main account — and pays a CoinJoin denomination or collateral -//! amount to a script no account of ours holds. Only -//! then is the chain derived past the generated end of the pool and compared -//! against those outputs. Counting what came back is not enough: a mix can -//! also spend a coin of ours the wallet has not found yet, whose return then -//! hides the one that is missing. A match extends the pool up to it; the regular -//! gap maintenance and SPV's matching of newly derived scripts carry it forward -//! from this block. Coins paid to the new addresses in blocks already scanned -//! are found only by a rescan from wallet creation, which is also what applies -//! this recovery to a wallet that synced before it existed. +//! Rescanning the wallet as it stands cannot fix that either: every old mix is +//! chainlocked by now, and a finalized transaction is never revisited, so a +//! coin found later in it is never credited. Recovery therefore starts from +//! fresh wallet state. [`ManagedWalletInfo::begin_coinjoin_recovery`] forgets +//! everything the wallet learned from the chain, rewinds its sync checkpoint to +//! just below its birth height, and turns the probe on for the replay that +//! follows; the sync layer sees the wallet behind and replays it from birth. +//! +//! During the replay, every transaction the regular check finds relevant has +//! its unclaimed P2PKH outputs compared against each CoinJoin chain, derived up +//! to [`COINJOIN_RECOVERY_PROBE_WINDOW`] addresses past the pool's generated +//! end. Only a matched address is added to the pool; the regular check then +//! credits the coin, marks the address used and maintains the gap past it, so +//! the empty run below it is never watched. The window keeps covering that run +//! for later transactions, and it moves up with every address the pool gains. +//! +//! The probe switches itself off once the wallet's committed sync height +//! reaches the height it had when the recovery began. +use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use dashcore::blockdata::transaction::Transaction; use dashcore::hashes::Hash; +use dashcore::prelude::CoreBlockHeight; use dashcore::{PubkeyHash, ScriptBuf}; use super::account_checker::DerivedAddressInfo; -use super::transaction_router::{ - COINJOIN_DENOMINATIONS, COINJOIN_MAX_COLLATERAL, COINJOIN_MIN_COLLATERAL, -}; +use crate::account::AccountType; use crate::bip32::{ChildNumber, ExtendedPubKey}; use crate::managed_account::address_pool::AddressPoolType; +use crate::managed_account::managed_account_collection::ManagedAccountCollection; +use crate::managed_account::managed_account_ref::ManagedAccountRefMut; use crate::managed_account::managed_account_trait::ManagedAccountTrait; +use crate::managed_account::managed_account_type::ManagedAccountType; +use crate::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use crate::wallet::{ManagedWalletInfo, Wallet}; use crate::KeySource; -/// How far past the generated end of each CoinJoin chain a missing output is -/// searched for: the widest run of empty addresses that recovery can cross in -/// one step. Each recovered coin moves the pool end, so the window travels with -/// it; DashSync derived up to ~10,500 CoinJoin addresses on heavily mixed -/// wallets. Derivation only; nothing in this window is watched unless an -/// output is found in it. +/// How far past the generated end of each CoinJoin chain a coin is searched +/// for: the widest run of empty addresses one step of recovery can cross. Each +/// address found moves the pool end, so the window travels with the coins; +/// DashSync derived up to ~10,500 CoinJoin addresses on heavily mixed wallets. +/// Derivation only; nothing in the window is watched unless a coin is found in +/// it. pub const COINJOIN_RECOVERY_PROBE_WINDOW: u32 = 10_000; -/// Scripts derived for probing, per CoinJoin chain of one wallet, so that the -/// many one-sided mixes of a wallet derive the window once rather than once -/// each. The whole window is always compared — a mix can return more of our -/// coins than the inputs we know about — so the cache changes cost, never the -/// result. -#[derive(Default)] -pub struct CoinJoinProbeCache { +/// A CoinJoin recovery replay running on one wallet. +pub struct CoinJoinRecovery { + /// The committed sync height the wallet had when the recovery began. The + /// replay is complete once the wallet is synced to it again. + until_height: CoreBlockHeight, + /// CoinJoin addresses found past the gap limit so far. + found: u32, + /// Whether the probe still runs; a cancel stops it while the replay goes + /// on, and builds stay refused until it is done. + probing: bool, + /// Probe scripts derived per CoinJoin chain (account index, pool type), so + /// the window is derived once per replay rather than once per transaction. chains: HashMap<(u32, AddressPoolType), ProbeRange>, } -/// A contiguous run of derived indices `[from, until)` of one chain. +/// The indices of one chain derived for probing: every index from the first +/// one the pool lacked when this replay first probed it, up to `until`. Indices the +/// pool has gained since stay here: an output paying one of them is claimed by +/// the pool and so is never looked up. struct ProbeRange { - /// The account key the run belongs to; another key starts afresh. - account_xpub: ExtendedPubKey, - /// The chain's branch key, derived once per run. + /// The chain's branch key, derived once. branch: ExtendedPubKey, /// Every probed address is P2PKH, so its key hash identifies it. hashes: HashMap, - from: u32, until: u32, } -/// A clone starts empty: the cache is only an optimisation, and copying tens -/// of thousands of scripts with every wallet snapshot would cost more than it -/// saves. -impl Clone for CoinJoinProbeCache { +impl CoinJoinRecovery { + fn new(until_height: CoreBlockHeight) -> Self { + Self { + until_height, + found: 0, + probing: true, + chains: HashMap::new(), + } + } + + /// Where the replay stands. + pub fn progress(&self) -> CoinJoinRecoveryProgress { + CoinJoinRecoveryProgress { + until_height: self.until_height, + found: self.found, + probing: self.probing, + } + } +} + +/// Where a running CoinJoin recovery stands. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct CoinJoinRecoveryProgress { + /// The committed sync height at which the replay is complete. + pub until_height: CoreBlockHeight, + /// CoinJoin addresses past the gap limit found so far. + pub found: u32, + /// Whether the probe still runs: `false` once the recovery was cancelled + /// and only its replay goes on. + pub probing: bool, +} + +/// A clone keeps the recovery running but derives its window again: copying +/// tens of thousands of scripts with every wallet snapshot would cost more than +/// deriving them. +impl Clone for CoinJoinRecovery { fn clone(&self) -> Self { - Self::default() + Self { + until_height: self.until_height, + found: self.found, + probing: self.probing, + chains: HashMap::new(), + } } } -impl std::fmt::Debug for CoinJoinProbeCache { +impl std::fmt::Debug for CoinJoinRecovery { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("CoinJoinProbeCache").field("chains", &self.chains.len()).finish() + f.debug_struct("CoinJoinRecovery") + .field("until_height", &self.until_height) + .field("found", &self.found) + .field("probing", &self.probing) + .field("chains", &self.chains.len()) + .finish() } } @@ -93,12 +150,6 @@ fn branch_index(pool_type: AddressPoolType) -> Option { } } -/// A CoinJoin denomination or collateral amount. -fn is_coinjoin_amount(value: u64) -> bool { - COINJOIN_DENOMINATIONS.contains(&value) - || (COINJOIN_MIN_COLLATERAL..=COINJOIN_MAX_COLLATERAL).contains(&value) -} - /// The key hash of `branch`'s child `index`, which the pool pays as P2PKH. fn probe_hash(branch: &ExtendedPubKey, index: u32) -> crate::Result { let child = ChildNumber::from_normal_idx(index).map_err(crate::Error::Bip32)?; @@ -108,53 +159,308 @@ fn probe_hash(branch: &ExtendedPubKey, index: u32) -> crate::Result /// The key hash a P2PKH script pays; `None` for any other script. fn p2pkh_hash(script: &ScriptBuf) -> Option { - if !script.is_p2pkh() { - return None; + script.p2pkh_public_key_hash_bytes().and_then(|bytes| PubkeyHash::from_slice(bytes).ok()) +} + +/// Fresh address pools for the CoinJoin account `account`, carrying over from +/// its `old` pools what the replay cannot rediscover: +/// - a gap limit widened at runtime, with which the wallet found coins the +/// default gap would not; +/// - every address seen in use or handed out, with that state: its coin may lie +/// farther from the others than the probe window reaches, and one handed out +/// but not yet paid must not go to a second payer; +/// - the runs between those addresses no wider than the gap, which a normal +/// walk watches, and the gap past each used one. Only wider runs, the +/// walls, stay out of the pool. +fn fresh_coinjoin_pools( + old: &ManagedAccountType, + account: &crate::Account, +) -> Option { + let mut fresh = match ManagedAccountCollection::fresh_managed_account_type(account) { + Ok(fresh) => fresh, + Err(e) => { + tracing::warn!(error = %e, "CoinJoin recovery: could not rebuild the CoinJoin pools, keeping the old ones"); + return None; + } + }; + let key_source = KeySource::Public(account.account_xpub); + let old_pools = old.address_pools(); + for pool in fresh.address_pools_mut() { + let Some(old) = old_pools.iter().find(|old| old.pool_type == pool.pool_type) else { + continue; + }; + if old.gap_limit > pool.gap_limit { + if let Err(e) = pool.set_gap_limit(old.gap_limit, &key_source) { + tracing::warn!(error = %e, gap = old.gap_limit, "CoinJoin recovery: could not keep a widened gap limit"); + } + } + for (&index, info) in &old.addresses { + let used = old.used_indices.contains(&index); + if !used && !info.is_reserved() { + continue; + } + if let Err(e) = pool.generate_address_at_index(index, &key_source, true) { + tracing::warn!(error = %e, index, "CoinJoin recovery: could not keep an address"); + continue; + } + if used { + pool.mark_index_used(index); + } + if let Some(kept) = pool.addresses.get_mut(&index) { + if !used { + kept.state = info.state; + } + kept.label = info.label.clone(); + kept.metadata = info.metadata.clone(); + } + } + let kept: Vec = pool.addresses.keys().copied().collect(); + for pair in kept.windows(2) { + let (low, high) = (pair[0], pair[1]); + if high - low - 1 > pool.gap_limit { + continue; + } + for index in low + 1..high { + if let Err(e) = pool.generate_address_at_index(index, &key_source, true) { + tracing::warn!(error = %e, index, "CoinJoin recovery: could not fill a narrow run"); + } + } + } + // The usual lookahead past every kept used address, as the old pool + // had it, not only past the highest one. + let used: Vec = pool.used_indices.iter().copied().collect(); + for index in used { + for next in index.saturating_add(1)..=index.saturating_add(pool.gap_limit) { + if let Err(e) = pool.generate_address_at_index(next, &key_source, true) { + tracing::warn!(error = %e, index = next, "CoinJoin recovery: could not extend the gap past a kept address"); + break; + } + } + } } - PubkeyHash::from_slice(&script.as_bytes()[3..23]).ok() + Some(fresh) } impl ManagedWalletInfo { - /// Extend a CoinJoin pool to any output of `tx` that is ours but lies past - /// the pool's generated end, when `tx` involves us and pays a CoinJoin - /// denomination or collateral amount to a script no account of ours holds. + /// Forget everything the wallet learned from the chain and rewind it to + /// just below its birth height, with the CoinJoin probe on for the replay. + /// + /// `known_height` is the highest height other sources (other wallets) know + /// the chain to have reached. The replay is complete once the wallet is + /// synced to it, or to its own synced or processed height, highest + /// chainlock seen or unfinished replay target when higher; that height is + /// returned. Fails when a recovery is already running on this wallet, + /// the wallet has no CoinJoin account, unconfirmed transactions or coins + /// reserved by a transaction build, nothing is known of the chain past its + /// birth, or `wallet` is not this wallet's. + pub(crate) fn begin_coinjoin_recovery( + &mut self, + wallet: &Wallet, + known_height: CoreBlockHeight, + ) -> crate::Result { + if wallet.wallet_id != self.wallet_id { + return Err(crate::Error::InvalidParameter( + "CoinJoin recovery: the wallet does not match this wallet info".to_string(), + )); + } + if self.coinjoin_recovery.as_ref().is_some_and(|recovery| recovery.probing) { + return Err(crate::Error::InvalidState( + "a CoinJoin recovery is already running on this wallet".to_string(), + )); + } + if self.accounts.coinjoin_accounts.is_empty() { + return Err(crate::Error::InvalidState( + "CoinJoin recovery: the wallet has no CoinJoin account".to_string(), + )); + } + // Records not yet in a block are never delivered again by the replay, + // which only brings back blocks; dropping them would hide a pending + // send and offer the coins it spends again. Coins a build has reserved + // are about to be spent the same way. + let current_height = self.metadata.last_processed_height; + let pending = self.accounts.all_accounts().iter().any(|account| { + account.transactions().values().any(|record| !record.is_confirmed()) + || account.as_funds().is_some_and(|funds| funds.has_reservations(current_height)) + }); + if pending { + return Err(crate::Error::InvalidState( + "CoinJoin recovery: the wallet has unconfirmed transactions or coins reserved \ + for one; start it once they are in a block, or abandon the ones that never \ + will be" + .to_string(), + )); + } + // A wallet still in its first sync has a synced height far below the + // chain; the chainlocks seen mark where the final history, the part a + // normal sync never revisits, ends. + let chain_locked = self + .metadata + .last_applied_chain_lock + .as_ref() + .map(|chain_lock| chain_lock.block_height); + // A replay a cancelled recovery left behind still has to get back to + // where it was going. + let until_height = known_height + .max(self.metadata.synced_height) + .max(self.metadata.last_processed_height) + .max(self.coinjoin_recovery.as_ref().map_or(0, |recovery| recovery.until_height)) + .max(chain_locked.unwrap_or(0)) + .max(self.noted_chain_lock_height.unwrap_or(0)); + let floor = self.metadata.birth_height.saturating_sub(1); + if until_height <= floor { + return Err(crate::Error::InvalidState( + "CoinJoin recovery: nothing is known of the chain past the wallet's birth yet; \ + start it once the wallet has synced" + .to_string(), + )); + } + self.reset_chain_state(wallet); + self.set_funds_replaying(true); + self.finished_coinjoin_recovery = None; + self.coinjoin_recovery = Some(CoinJoinRecovery::new(until_height)); + tracing::info!( + from_height = self.metadata.synced_height, + until_height, + "CoinJoin recovery: wallet reset, replaying from birth" + ); + Ok(until_height) + } + + /// Stop the probe of the running recovery before its replay is complete, + /// returning how many CoinJoin addresses it found past the gap limit; + /// `None` when no probe runs. The replay itself goes on as a normal sync, + /// and builds stay refused until it is back at the height the recovery was + /// to run to: until then the coins are those of a past height. + pub(crate) fn cancel_coinjoin_recovery(&mut self) -> Option { + let synced = self.metadata.synced_height; + let recovery = self.coinjoin_recovery.as_mut().filter(|recovery| recovery.probing)?; + recovery.probing = false; + recovery.chains.clear(); + tracing::info!( + height = synced, + until_height = recovery.until_height, + found = recovery.found, + "CoinJoin recovery: cancelled, the replay goes on without the probe" + ); + Some(recovery.found) + } + + /// Called whenever the committed sync height advances: once it reaches the + /// height the recovery was to run to, the recovery ends, builds are + /// allowed again, and its result waits for + /// [`Self::take_finished_coinjoin_recovery`]. + pub(crate) fn on_synced_height_advanced(&mut self) { + let synced = self.metadata.synced_height; + if !self.coinjoin_recovery.as_ref().is_some_and(|recovery| synced >= recovery.until_height) + { + return; + } + let Some(recovery) = self.coinjoin_recovery.take() else { + return; + }; + self.set_funds_replaying(false); + tracing::info!( + height = synced, + found = recovery.found, + "CoinJoin recovery: replay complete" + ); + self.finished_coinjoin_recovery = Some(recovery.progress()); + } + + /// Where a recovery whose replay completed since the last call ended up; + /// `probing` is `false` when it had been cancelled. + pub(crate) fn take_finished_coinjoin_recovery(&mut self) -> Option { + self.finished_coinjoin_recovery.take() + } + + /// Whether a CoinJoin recovery replay is still catching up, so the coins + /// are those of a past height. + pub(crate) fn is_replaying(&self) -> bool { + self.coinjoin_recovery.is_some() + } + + /// Mark every funds account as replayed, or no longer, so transaction + /// builds refuse to fund from it meanwhile. + pub(crate) fn set_funds_replaying(&mut self, replaying: bool) { + for account in self.accounts.all_accounts_mut() { + if let ManagedAccountRefMut::Funds(funds) = account { + funds.replaying = replaying; + } + } + } + + /// Drop all chain-derived state so the wallet can be replayed from birth. + /// + /// Each CoinJoin account the wallet holds the key of gets fresh address + /// pools (see [`fresh_coinjoin_pools`]), so a recovered wallet does not go + /// on watching empty runs. Every other pool may hold addresses a walk from + /// index 0 would never reach again (a DIP-15 contact pool, a migrated + /// wallet's history), so it keeps its addresses and their usage. Platform + /// accounts are not Core chain state and are left alone, as are the applied + /// ChainLock, which keeps the replayed history final as it arrives, and the + /// wallet's identity and metadata. + fn reset_chain_state(&mut self, wallet: &Wallet) { + for account in self.accounts.all_accounts_mut() { + match account { + ManagedAccountRefMut::Funds(funds) => { + let account_type = funds.managed_account_type().to_account_type(); + let fresh = match account_type { + AccountType::CoinJoin { + .. + } => wallet.accounts.account_of_type(account_type).and_then(|account| { + fresh_coinjoin_pools(funds.managed_account_type(), account) + }), + _ => None, + }; + funds.reset_chain_state(fresh); + } + ManagedAccountRefMut::Keys(keys) => keys.reset_chain_state(None), + } + } + self.instant_send_locks.clear(); + self.observed_spent_outpoints.clear(); + // Only coverage is rewound, as for a new account, with a new account + // generation so in-flight filter scans, which snapshotted the old + // state, cannot certify coverage for the reset wallet. + // `last_processed_height` is a chain fact: keeping it keeps replayed + // coins' maturity and confirmations measured against the real chain. + self.rewind_sync_checkpoint_for_new_account(); + self.update_balance(); + } + + /// While a recovery is running, add to the CoinJoin pools every address of + /// ours that `tx` pays past a pool's generated end. `tx` must be one the + /// regular check found relevant. /// - /// Must run before the transaction is checked, so that the outputs found - /// here are credited by the regular check. Returns the addresses added to - /// the pools, for the caller to hand to SPV as newly derived scripts. + /// Must run before the transaction updates the wallet, so that the caller's + /// check that follows credits the outputs found here. Returns the addresses + /// added, for the caller to hand to SPV as newly derived scripts. pub(crate) fn recover_coinjoin_outputs( &mut self, tx: &Transaction, wallet: &Wallet, ) -> Vec { let mut derived = Vec::new(); - - // Cheap first: most transactions pay no CoinJoin-shaped amount at all. - if !tx.output.iter().any(|output| is_coinjoin_amount(output.value)) { + let Some(recovery) = self.coinjoin_recovery.as_mut().filter(|recovery| recovery.probing) + else { return derived; - } + }; + let accounts = self.accounts.all_accounts(); - // Every output no account of ours holds is compared once probing; a - // CoinJoin-shaped one among them is what starts it. - let (claimed, unclaimed): (Vec<&dashcore::TxOut>, Vec<&dashcore::TxOut>) = - tx.output.iter().partition(|output| { - accounts + let unclaimed: HashSet = tx + .output + .iter() + .filter(|output| { + !accounts .iter() .any(|account| account.contains_script_pub_key(&output.script_pubkey)) - }); - // The transaction must involve us: it pays one of our addresses (its - // spend can arrive before the coin it spends), or spends a coin of ours. - let involved = unclaimed.iter().any(|output| is_coinjoin_amount(output.value)) - && (!claimed.is_empty() - || accounts.iter().filter_map(|account| account.as_funds()).any(|account| { - tx.input.iter().any(|input| account.utxos.contains_key(&input.previous_output)) - })); + }) + .filter_map(|output| p2pkh_hash(&output.script_pubkey)) + .collect(); drop(accounts); - if !involved { + if unclaimed.is_empty() { return derived; } - let unclaimed: HashSet = - unclaimed.iter().filter_map(|output| p2pkh_hash(&output.script_pubkey)).collect(); for (&index, account) in self.accounts.coinjoin_accounts.iter_mut() { let account_type = account.managed_account_type().to_account_type(); @@ -168,84 +474,86 @@ impl ManagedWalletInfo { let Some(branch_index) = branch_index(pool.pool_type) else { continue; }; - let start = pool.highest_generated.map(|h| h + 1).unwrap_or(0); - let end = start.saturating_add(COINJOIN_RECOVERY_PROBE_WINDOW); - let chains = &mut self.coinjoin_probe_cache.chains; - let key = (index, pool.pool_type); - // The cache covers `[start, end)` of this account key only: - // indices below the pool end are watched already. A start - // outside the cached run (the pool moved past it, or was - // replaced) or another key starts afresh. - let reusable = chains.get(&key).is_some_and(|range| { - range.account_xpub == account_xpub - && (range.from..=range.until).contains(&start) - }); - if !reusable { - let branch = match ChildNumber::from_normal_idx(branch_index) - .and_then(|child| account_xpub.ckd_pub(child)) - { - Ok(branch) => branch, - Err(e) => { - tracing::warn!(error = %e, "CoinJoin recovery: branch derivation failed"); - continue; - } - }; - chains.insert( - key, - ProbeRange { - account_xpub, + let pool_end = pool.highest_generated.map(|h| h + 1).unwrap_or(0); + let range = match recovery.chains.entry((index, pool.pool_type)) { + Entry::Occupied(entry) => entry.into_mut(), + Entry::Vacant(entry) => { + let branch = match ChildNumber::from_normal_idx(branch_index) + .and_then(|child| account_xpub.ckd_pub(child)) + { + Ok(branch) => branch, + Err(e) => { + tracing::warn!(error = %e, "CoinJoin recovery: branch derivation failed"); + continue; + } + }; + // From the first index the pool lacks: the reset may + // have kept used addresses past an empty run. + let first_missing = (0..pool_end) + .find(|&i| pool.info_at_index(i).is_none()) + .unwrap_or(pool_end); + entry.insert(ProbeRange { branch, hashes: HashMap::new(), - from: start, - until: start, - }, - ); - } - let Some(range) = chains.get_mut(&key) else { - continue; - }; - if start > range.from { - range.hashes.retain(|_, index| *index >= start); - range.from = start; - } - while range.until < end { - let probe = range.until; - match probe_hash(&range.branch, probe) { - Ok(hash) => { - range.hashes.insert(hash, probe); - range.until += 1; - } - Err(e) => { - tracing::warn!( - error = %e, - index = probe, - "CoinJoin recovery: derivation failed, searching below it only" - ); - break; - } + until: first_missing, + }) } - } - let mut found: Vec = - unclaimed.iter().filter_map(|hash| range.hashes.get(hash).copied()).collect(); - found.sort_unstable(); - let Some(&highest) = found.last() else { - continue; }; - + // Each address found moves the pool end and with it the window, + // so a second coin of this transaction may only come into reach + // once the first is added: search again until nothing new turns + // up, since a finalized transaction is never looked at again. let pool_type = pool.pool_type; let mut new_infos = Vec::new(); - for next in start..=highest { - if let Err(e) = pool.generate_address_at_index(next, &key_source, true) { - tracing::error!( - error = %e, - index = next, - "CoinJoin recovery: failed to extend pool, coins above it stay missing" - ); - found.retain(|&index| index < next); + let mut failed = HashSet::new(); + loop { + let window_start = pool.highest_generated.map(|h| h + 1).unwrap_or(0); + let end = window_start.saturating_add(COINJOIN_RECOVERY_PROBE_WINDOW); + while range.until < end { + let probe = range.until; + match probe_hash(&range.branch, probe) { + Ok(hash) => { + range.hashes.insert(hash, probe); + range.until += 1; + } + Err(e) => { + tracing::warn!( + error = %e, + index = probe, + "CoinJoin recovery: derivation failed, searching below it only" + ); + break; + } + } + } + let mut hits: Vec = unclaimed + .iter() + .filter_map(|hash| range.hashes.get(hash).copied()) + .filter(|&index| { + pool.info_at_index(index).is_none() && !failed.contains(&index) + }) + .collect(); + if hits.is_empty() { break; } - if let Some(info) = pool.info_at_index(next) { - new_infos.push(info.clone()); + hits.sort_unstable(); + for next in hits { + if let Err(e) = pool.generate_address_at_index(next, &key_source, true) { + tracing::error!( + error = %e, + index = next, + "CoinJoin recovery: failed to add an address to the pool, its coin stays missing" + ); + failed.insert(next); + continue; + } + match pool.info_at_index(next) { + Some(info) => new_infos.push(info.clone()), + // Never selected again, or the search would not end. + None => { + failed.insert(next); + } + } } } if new_infos.is_empty() { @@ -256,11 +564,11 @@ impl ManagedWalletInfo { tracing::info!( txid = %tx.txid(), pool_type = ?pool_type, - previous_end = start.saturating_sub(1), - found = ?found, - added = new_infos.len(), - "CoinJoin recovery: extended pool to outputs past the gap limit" + previous_end = pool_end.saturating_sub(1), + found = ?new_infos.iter().map(|info| info.index).collect::>(), + "CoinJoin recovery: found outputs past the gap limit" ); + recovery.found += new_infos.len() as u32; derived.extend(new_infos.into_iter().map(|info| DerivedAddressInfo { account_type, pool_type, @@ -280,13 +588,13 @@ impl ManagedWalletInfo { #[cfg(test)] mod tests { use super::*; - use crate::account::AccountType; - use crate::managed_account::address_pool::AddressPoolType; + use crate::account::{AccountType, StandardAccountType}; use crate::managed_account::managed_account_type::ManagedAccountType; use crate::transaction_checking::{BlockInfo, TransactionContext, WalletTransactionChecker}; use crate::wallet::initialization::WalletAccountCreationOptions; + use crate::wallet::managed_wallet_info::managed_account_operations::ManagedAccountOperations; use crate::Network; - use dashcore::hashes::Hash; + use dashcore::ephemerealdata::chain_lock::ChainLock; use dashcore::{Address, BlockHash, OutPoint, TxIn, TxOut, Txid}; const NETWORK: Network = Network::Testnet; @@ -294,6 +602,7 @@ mod tests { const DENOM: u64 = 100_001; /// An index past the generated end of a fresh CoinJoin pool. const PAST_GAP: u32 = crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; + const GAP: u32 = crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; struct Fixture { wallet: Wallet, @@ -301,11 +610,14 @@ mod tests { key_source: KeySource, } + /// A wallet with one CoinJoin account and a recovery running. fn fixture() -> Fixture { - fixture_on(NETWORK) + let mut fx = fixture_without_recovery(NETWORK); + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start recovery"); + fx } - fn fixture_on(network: Network) -> Fixture { + fn fixture_without_recovery(network: Network) -> Fixture { let mut wallet = Wallet::new_random(network, WalletAccountCreationOptions::None).expect("wallet"); wallet @@ -343,14 +655,22 @@ mod tests { pool.generate_address_at_index(index, &fx.key_source, false).expect("derive") } - fn pool_end(fx: &Fixture, pool_type: AddressPoolType) -> Option { - let account = fx.managed.first_coinjoin_managed_account().expect("account"); - account + fn pool( + fx: &Fixture, + pool_type: AddressPoolType, + ) -> &crate::managed_account::address_pool::AddressPool { + fx.managed + .first_coinjoin_managed_account() + .expect("account") .managed_account_type() .address_pools() .into_iter() .find(|pool| pool.pool_type == pool_type) - .and_then(|pool| pool.highest_generated) + .expect("pool") + } + + fn pool_end(fx: &Fixture, pool_type: AddressPoolType) -> Option { + pool(fx, pool_type).highest_generated } fn block(height: u32) -> TransactionContext { @@ -361,103 +681,92 @@ mod tests { )) } - /// Funds our CoinJoin external index 0 with one denomination, then mixes it: - /// our coin plus two foreign ones in, three denominations out, ours to `ours`. - async fn fund_and_mix( - fx: &mut Fixture, - ours: Address, - ) -> crate::transaction_checking::TransactionCheckResult { - let first = coinjoin_address(fx, false, 0); - let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); - let funded = - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - assert_eq!(funded.total_received, DENOM, "funding must be credited"); - let spend = OutPoint { - txid: funding.txid(), - vout: 0, - }; - mix_spending(fx, spend, ours, 2).await.1 - } - - /// Mixes our coin at `spend` with two foreign ones at `height`; ours comes - /// back to `ours` as output 1. Returns the mix's outpoint for ours too. - async fn mix_spending( - fx: &mut Fixture, - spend: OutPoint, - ours: Address, - height: u32, - ) -> (OutPoint, crate::transaction_checking::TransactionCheckResult) { - let network = fx.wallet.network; - let foreign_input = |n: u8| TxIn { + fn input(txid: Txid) -> TxIn { + TxIn { previous_output: OutPoint { - txid: Txid::from_byte_array([n; 32]), + txid, vout: 0, }, ..Default::default() - }; - let mix = dashcore::Transaction { + } + } + + fn output(value: u64, address: &Address) -> TxOut { + TxOut { + value, + script_pubkey: address.script_pubkey(), + } + } + + fn tx(input: Vec, output: Vec) -> Transaction { + Transaction { version: 2, lock_time: 0, - input: vec![ - foreign_input(0xa1), - TxIn { - previous_output: spend, - ..Default::default() - }, - foreign_input(0xa2), + input, + output, + special_transaction_payload: None, + } + } + + /// Funds our CoinJoin external index 0 with one denomination at block 1. + async fn fund(fx: &mut Fixture) -> Transaction { + let first = coinjoin_address(fx, false, 0); + let funding = Transaction::dummy(&first, 0..1, &[DENOM]); + let funded = + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; + assert_eq!(funded.total_received, DENOM, "funding must be credited"); + funding + } + + /// Our funded coin plus two foreign ones in, three denominations out, ours + /// to `ours` as output 1. + fn mix_of(funding: &Transaction, ours: &Address) -> Transaction { + tx( + vec![ + input(Txid::from_byte_array([0xa1; 32])), + input(funding.txid()), + input(Txid::from_byte_array([0xa2; 32])), ], - output: vec![ - TxOut { - value: DENOM, - script_pubkey: Address::dummy(network, 1).script_pubkey(), - }, - TxOut { - value: DENOM, - script_pubkey: ours.script_pubkey(), - }, - TxOut { - value: DENOM, - script_pubkey: Address::dummy(network, 2).script_pubkey(), - }, + vec![ + output(DENOM, &Address::dummy(NETWORK, 1)), + output(DENOM, ours), + output(DENOM, &Address::dummy(NETWORK, 2)), ], - special_transaction_payload: None, - }; - let result = fx - .managed - .check_core_transaction(&mix, block(height), &mut fx.wallet, true, true) - .await; - let returned = OutPoint { - txid: mix.txid(), - vout: 1, - }; - (returned, result) + ) + } + + async fn fund_and_mix( + fx: &mut Fixture, + ours: &Address, + ) -> crate::transaction_checking::TransactionCheckResult { + let funding = fund(fx).await; + let mix = mix_of(&funding, ours); + fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await } - /// 32008: the mixed coin comes back past the gap limit. Without recovery the - /// wallet books the mix as a one-sided loss and never watches the address. + /// 32008: the mixed coin comes back past the gap limit. Only its address + /// and the gap after it join the pool, not the empty run below it. #[tokio::test] async fn mixed_coin_returned_past_the_gap_limit_is_recovered() { let mut fx = fixture(); - let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); - let result = fund_and_mix(&mut fx, far.clone()).await; + let far_index = PAST_GAP + 250; + let far = coinjoin_address(&mut fx, false, far_index); + let result = fund_and_mix(&mut fx, &far).await; assert_eq!(result.total_sent, DENOM, "our input is debited"); assert_eq!(result.total_received, DENOM, "the far output must be credited"); - let account = fx.managed.first_coinjoin_managed_account().expect("account"); - assert!( - account.utxos.values().any(|utxo| utxo.address == far), - "the far output must be a UTXO" - ); - assert!( - pool_end(&fx, AddressPoolType::External) - >= Some(PAST_GAP + 250 + crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT), - "the pool must extend past the recovered index by the gap limit" - ); assert!( - result.new_addresses.iter().any(|d| d.info.index == PAST_GAP + 250), + result.new_addresses.iter().any(|d| d.info.index == far_index), "the recovered address must be reported as newly derived" ); assert_eq!(fx.managed.balance.total(), DENOM, "nothing lost: balance equals the coin"); + let external = pool(&fx, AddressPoolType::External); + assert_eq!(external.highest_generated, Some(far_index + GAP), "the gap follows the coin"); + assert!( + (PAST_GAP + 1..far_index).all(|index| external.info_at_index(index).is_none()), + "the empty run below the recovered address is not watched" + ); + assert_eq!(fx.managed.coinjoin_recovery.as_ref().map(|r| r.progress().found), Some(1)); } /// DashSync also mixed onto its internal CoinJoin chain. @@ -465,27 +774,33 @@ mod tests { async fn mixed_coin_returned_past_the_gap_limit_on_the_internal_chain_is_recovered() { let mut fx = fixture(); let far = coinjoin_address(&mut fx, true, PAST_GAP + 150); - let result = fund_and_mix(&mut fx, far.clone()).await; + let result = fund_and_mix(&mut fx, &far).await; assert_eq!(result.total_received, DENOM, "the far internal output must be credited"); - assert!(pool_end(&fx, AddressPoolType::Internal) >= Some(PAST_GAP + 150)); + assert_eq!(pool_end(&fx, AddressPoolType::Internal), Some(PAST_GAP + 150 + GAP)); assert_eq!(fx.managed.balance.total(), DENOM); } - /// A normal mix that returns the coin inside the pool extends nothing: - /// the other participants' outputs are probed and found not ours. + /// A wallet on mainnet recovers the same way (32008 is a mainnet wallet). #[tokio::test] - async fn mix_returning_inside_the_pool_extends_nothing() { - let mut fx = fixture(); - let near = coinjoin_address(&mut fx, false, 5); - let result = fund_and_mix(&mut fx, near).await; + async fn mixed_coin_returned_past_the_gap_limit_is_recovered_on_mainnet() { + let mut fx = fixture_without_recovery(Network::Mainnet); + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start recovery"); + let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); + let result = fund_and_mix(&mut fx, &far).await; + assert_eq!(result.total_received, DENOM, "the far output must be credited"); + assert_eq!(fx.managed.balance.total(), DENOM); + } - assert_eq!(result.total_received, DENOM); - assert_eq!( - pool_end(&fx, AddressPoolType::External), - Some(5 + crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT), - "only the regular gap maintenance past the highest used index" - ); + /// Without a recovery running, nothing is probed: a normal wallet pays + /// nothing for this, and the far coin stays missing as before. + #[tokio::test] + async fn without_a_recovery_nothing_is_probed() { + let mut fx = fixture_without_recovery(NETWORK); + let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); + let result = fund_and_mix(&mut fx, &far).await; + assert_eq!(result.total_received, 0); + assert_eq!(pool_end(&fx, AddressPoolType::External), Some(GAP)); } /// A coin that really left (all outputs foreign) is not credited, and the @@ -493,88 +808,66 @@ mod tests { #[tokio::test] async fn coin_that_really_left_is_not_credited() { let mut fx = fixture(); - let foreign = Address::dummy(NETWORK, 3); - let result = fund_and_mix(&mut fx, foreign).await; + let result = fund_and_mix(&mut fx, &Address::dummy(NETWORK, 3)).await; assert_eq!(result.total_received, 0); assert_eq!( pool_end(&fx, AddressPoolType::External), - Some(crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT), + Some(GAP), "the probe found nothing, so the pool stays at the funding's gap" ); assert_eq!(fx.managed.balance.total(), 0); } - /// Past the probe window the output stays unknown — the window is a bound, + /// Past the probe window the output stays unknown: the window is a bound, /// not a guarantee. #[tokio::test] async fn output_past_the_probe_window_is_not_found() { let mut fx = fixture(); - let beyond = coinjoin_address( - &mut fx, - false, - crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT + COINJOIN_RECOVERY_PROBE_WINDOW + 10, - ); - let result = fund_and_mix(&mut fx, beyond).await; + let beyond = coinjoin_address(&mut fx, false, GAP + COINJOIN_RECOVERY_PROBE_WINDOW + 10); + let result = fund_and_mix(&mut fx, &beyond).await; assert_eq!(result.total_received, 0); } + /// A transaction the wallet does not find relevant is never probed, even + /// when it pays a CoinJoin address of ours past the gap: the recovery + /// follows the wallet's own coins, not every block it downloads. + #[tokio::test] + async fn an_irrelevant_transaction_is_not_probed() { + let mut fx = fixture(); + let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); + let foreign = tx( + vec![input(Txid::from_byte_array([0xe1; 32]))], + vec![output(DENOM, &far), output(DENOM, &Address::dummy(NETWORK, 7))], + ); + let result = + fx.managed.check_core_transaction(&foreign, block(2), &mut fx.wallet, true, true).await; + assert!(!result.is_relevant); + assert_eq!(pool_end(&fx, AddressPoolType::External), Some(GAP - 1)); + } + /// A mix can return more of our coins than the inputs the wallet knows /// about (one of ours came from an address it has not discovered yet). - /// Every one of them in the window must be found, not just as many as the - /// known inputs. + /// Every one of them in the window must be found. #[tokio::test] async fn every_returned_coin_is_found_not_just_the_known_inputs() { let mut fx = fixture(); let far_a = coinjoin_address(&mut fx, false, PAST_GAP + 200); let far_b = coinjoin_address(&mut fx, false, PAST_GAP + 800); - let first = coinjoin_address(&mut fx, false, 0); - let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - - let mix = dashcore::Transaction { - version: 2, - lock_time: 0, - input: vec![ - TxIn { - previous_output: OutPoint { - txid: funding.txid(), - vout: 0, - }, - ..Default::default() - }, + let funding = fund(&mut fx).await; + let mix = tx( + vec![ + input(funding.txid()), // Ours as well, but on an address the wallet has not discovered. - TxIn { - previous_output: OutPoint { - txid: Txid::from_byte_array([0xb1; 32]), - vout: 0, - }, - ..Default::default() - }, - TxIn { - previous_output: OutPoint { - txid: Txid::from_byte_array([0xb2; 32]), - vout: 0, - }, - ..Default::default() - }, + input(Txid::from_byte_array([0xb1; 32])), + input(Txid::from_byte_array([0xb2; 32])), ], - output: vec![ - TxOut { - value: DENOM, - script_pubkey: far_a.script_pubkey(), - }, - TxOut { - value: DENOM, - script_pubkey: Address::dummy(NETWORK, 4).script_pubkey(), - }, - TxOut { - value: DENOM, - script_pubkey: far_b.script_pubkey(), - }, + vec![ + output(DENOM, &far_a), + output(DENOM, &Address::dummy(NETWORK, 4)), + output(DENOM, &far_b), ], - special_transaction_payload: None, - }; + ); let result = fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; @@ -582,129 +875,68 @@ mod tests { assert_eq!(fx.managed.balance.total(), 2 * DENOM); } - /// Two wallets probing the same indices never see each other's scripts. - #[tokio::test] - async fn probe_cache_is_per_wallet() { - let mut first = fixture(); - let far = coinjoin_address(&mut first, false, PAST_GAP + 300); - assert_eq!(fund_and_mix(&mut first, far).await.total_received, DENOM); - - let mut second = fixture(); - let foreign = coinjoin_address(&mut first, false, PAST_GAP + 300); - let result = fund_and_mix(&mut second, foreign).await; - assert_eq!(result.total_received, 0, "the first wallet's address is not ours"); - } - - /// A wallet on mainnet recovers the same way (32008 is a mainnet wallet). - #[tokio::test] - async fn mixed_coin_returned_past_the_gap_limit_is_recovered_on_mainnet() { - let mut fx = fixture_on(Network::Mainnet); - let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); - let result = fund_and_mix(&mut fx, far).await; - assert_eq!(result.total_received, DENOM, "the far output must be credited"); - assert_eq!(fx.managed.balance.total(), DENOM); - } - - /// The recovered coin is mixed again and comes back past the new pool end: - /// the second recovery reuses the cache, moved up to the new end. - #[tokio::test] - async fn a_recovered_coin_mixed_again_past_the_new_end_is_recovered() { - let mut fx = fixture(); - let first_far = coinjoin_address(&mut fx, false, PAST_GAP + 200); - let first = coinjoin_address(&mut fx, false, 0); - let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - let spend = OutPoint { - txid: funding.txid(), - vout: 0, - }; - let (recovered, result) = mix_spending(&mut fx, spend, first_far, 2).await; - assert_eq!(result.total_received, DENOM, "the first far output must be credited"); - - let new_start = pool_end(&fx, AddressPoolType::External).expect("pool end") + 1; - let second_far = coinjoin_address(&mut fx, false, new_start + 500); - let (_, result) = mix_spending(&mut fx, recovered, second_far.clone(), 3).await; - assert_eq!(result.total_sent, DENOM, "the recovered coin is debited"); - assert_eq!(result.total_received, DENOM, "the second far output must be credited"); - assert_eq!(fx.managed.balance.total(), DENOM, "exactly one coin, at the second address"); - let account = fx.managed.first_coinjoin_managed_account().expect("account"); - assert!(account.utxos.values().any(|utxo| utxo.address == second_far)); - - let range = fx - .managed - .coinjoin_probe_cache - .chains - .get(&(0, AddressPoolType::External)) - .expect("cached chain"); - assert_eq!(range.from, new_start, "the cache moves up with the pool"); - assert!(range.hashes.values().all(|&index| index >= new_start)); - } - /// A mix spends a known coin of ours and one the wallet has not found yet; - /// one coin comes back inside the pool, the other past the wall. As many - /// came back as the wallet knew went in, yet one is still missing. + /// one coin comes back inside the pool, the other past the wall. #[tokio::test] async fn a_return_for_an_undiscovered_input_does_not_hide_a_far_coin() { let mut fx = fixture(); let near = coinjoin_address(&mut fx, false, 5); let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); - let first = coinjoin_address(&mut fx, false, 0); - let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - - let input = |txid: Txid| TxIn { - previous_output: OutPoint { - txid, - vout: 0, - }, - ..Default::default() - }; - let mix = dashcore::Transaction { - version: 2, - lock_time: 0, - input: vec![ + let funding = fund(&mut fx).await; + let mix = tx( + vec![ input(funding.txid()), - // Ours, on an address the wallet has not discovered. input(Txid::from_byte_array([0xc1; 32])), input(Txid::from_byte_array([0xc2; 32])), ], - output: vec![ - TxOut { - value: DENOM, - script_pubkey: near.script_pubkey(), - }, - TxOut { - value: DENOM, - script_pubkey: Address::dummy(NETWORK, 5).script_pubkey(), - }, - TxOut { - value: DENOM, - script_pubkey: far.script_pubkey(), - }, + vec![ + output(DENOM, &near), + output(DENOM, &Address::dummy(NETWORK, 5)), + output(DENOM, &far), ], - special_transaction_payload: None, - }; + ); let result = fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; assert_eq!(result.total_received, 2 * DENOM, "both returned coins must be credited"); assert_eq!(fx.managed.balance.total(), 2 * DENOM); } - /// DashSync created denominations from the main account onto CoinJoin - /// addresses past the wall; no CoinJoin coin is spent, yet they are ours. + /// The mix arrives before the coin it spends (rescan delivery order): no + /// input is known yet, but an output to our pool makes it relevant. #[tokio::test] - async fn denominations_created_past_the_gap_from_the_main_account_are_recovered() { + async fn a_mix_seen_before_its_funding_still_recovers_the_far_coin() { let mut fx = fixture(); - fx.wallet - .add_account( - AccountType::Standard { - index: 0, - standard_account_type: crate::account::StandardAccountType::BIP44Account, + let near = coinjoin_address(&mut fx, false, 5); + let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); + let mix = tx( + (0xd1..=0xd3).map(|n| input(Txid::from_byte_array([n; 32]))).collect(), + vec![ + output(DENOM, &near), + output(DENOM, &Address::dummy(NETWORK, 6)), + output(DENOM, &far), + ], + ); + let result = + fx.managed.check_core_transaction(&mix, block(3), &mut fx.wallet, true, true).await; + assert_eq!(result.total_received, 2 * DENOM, "both of our outputs must be credited"); + } + + /// DashSync created denominations from the main account onto CoinJoin + /// addresses past the wall; no CoinJoin coin is spent, yet they are ours. + #[tokio::test] + async fn denominations_created_past_the_gap_from_the_main_account_are_recovered() { + let mut fx = fixture_without_recovery(NETWORK); + fx.wallet + .add_account( + AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, }, None, ) .expect("BIP44 account"); fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 0); + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start recovery"); let main_xpub = fx.wallet.accounts.standard_bip44_accounts.get(&0).expect("BIP44 account").account_xpub; let main = { @@ -717,35 +949,15 @@ mod tests { .expect("external pool"); pool.generate_address_at_index(0, &KeySource::Public(main_xpub), false).expect("derive") }; - let funding = dashcore::Transaction::dummy(&main, 0..1, &[5 * DENOM]); + let funding = Transaction::dummy(&main, 0..1, &[5 * DENOM]); let funded = fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; assert_eq!(funded.total_received, 5 * DENOM, "funding must be credited"); let far_a = coinjoin_address(&mut fx, false, PAST_GAP + 400); let far_b = coinjoin_address(&mut fx, false, PAST_GAP + 401); - let create = dashcore::Transaction { - version: 2, - lock_time: 0, - input: vec![TxIn { - previous_output: OutPoint { - txid: funding.txid(), - vout: 0, - }, - ..Default::default() - }], - output: vec![ - TxOut { - value: DENOM, - script_pubkey: far_a.script_pubkey(), - }, - TxOut { - value: DENOM, - script_pubkey: far_b.script_pubkey(), - }, - ], - special_transaction_payload: None, - }; + let create = + tx(vec![input(funding.txid())], vec![output(DENOM, &far_a), output(DENOM, &far_b)]); let result = fx.managed.check_core_transaction(&create, block(2), &mut fx.wallet, true, true).await; assert_eq!(result.total_sent, 5 * DENOM); @@ -754,114 +966,632 @@ mod tests { assert_eq!(coinjoin.utxos.len(), 2); } - /// The mix arrives before the coin it spends (rescan delivery order): no - /// input is known yet, but an output to our pool shows the mix is ours. + /// Whatever the amount: DashSync's denomination-creating transaction sent + /// its odd change to the CoinJoin internal chain past the wall. #[tokio::test] - async fn a_mix_seen_before_its_funding_still_recovers_the_far_coin() { + async fn odd_change_past_the_gap_beside_a_far_denomination_is_recovered() { + const CHANGE: u64 = 1_234_567; let mut fx = fixture(); - let near = coinjoin_address(&mut fx, false, 5); + let first = coinjoin_address(&mut fx, false, 0); + let funding = Transaction::dummy(&first, 0..1, &[DENOM + CHANGE]); + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); - let mix = dashcore::Transaction { - version: 2, - lock_time: 0, - input: (0xd1..=0xd3) - .map(|n| TxIn { + let change = coinjoin_address(&mut fx, true, PAST_GAP + 600); + let create = + tx(vec![input(funding.txid())], vec![output(DENOM, &far), output(CHANGE, &change)]); + let result = + fx.managed.check_core_transaction(&create, block(2), &mut fx.wallet, true, true).await; + assert_eq!(result.total_received, DENOM + CHANGE, "the change must be credited too"); + } + + /// The recovered coin is mixed again and comes back past the new pool end: + /// the window moves up with the pool. + #[tokio::test] + async fn a_recovered_coin_mixed_again_past_the_new_end_is_recovered() { + let mut fx = fixture(); + let first_far = coinjoin_address(&mut fx, false, PAST_GAP + 200); + let funding = fund(&mut fx).await; + let mix = mix_of(&funding, &first_far); + let result = + fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; + assert_eq!(result.total_received, DENOM, "the first far output must be credited"); + + let new_end = pool_end(&fx, AddressPoolType::External).expect("pool end"); + let second_far = coinjoin_address(&mut fx, false, new_end + COINJOIN_RECOVERY_PROBE_WINDOW); + let remix = mix_of(&mix, &second_far); + let remix = tx( + vec![ + input(Txid::from_byte_array([0xf1; 32])), + TxIn { previous_output: OutPoint { - txid: Txid::from_byte_array([n; 32]), - vout: 0, + txid: mix.txid(), + vout: 1, }, ..Default::default() - }) - .collect(), - output: vec![ - TxOut { - value: DENOM, - script_pubkey: near.script_pubkey(), - }, - TxOut { - value: DENOM, - script_pubkey: Address::dummy(NETWORK, 6).script_pubkey(), - }, - TxOut { - value: DENOM, - script_pubkey: far.script_pubkey(), }, ], - special_transaction_payload: None, - }; + remix.output, + ); let result = - fx.managed.check_core_transaction(&mix, block(3), &mut fx.wallet, true, true).await; - assert_eq!(result.total_received, 2 * DENOM, "both of our outputs must be credited"); + fx.managed.check_core_transaction(&remix, block(3), &mut fx.wallet, true, true).await; + assert_eq!(result.total_sent, DENOM, "the recovered coin is debited"); + assert_eq!(result.total_received, DENOM, "the second far output must be credited"); + assert_eq!(fx.managed.balance.total(), DENOM, "exactly one coin, at the second address"); } - /// A collateral output (0.0004 DASH) past the wall is ours as well. + /// A coin paid later into the empty run below an earlier recovered address + /// is still found: the window keeps covering the run the pool skipped. #[tokio::test] - async fn a_collateral_output_past_the_gap_is_recovered() { - const COLLATERAL: u64 = 40_000; + async fn a_later_coin_in_the_skipped_run_is_found() { let mut fx = fixture(); - let first = coinjoin_address(&mut fx, false, 0); - let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM]); - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); - let make_collateral = dashcore::Transaction { - version: 2, - lock_time: 0, - input: vec![TxIn { - previous_output: OutPoint { - txid: funding.txid(), - vout: 0, + let far = coinjoin_address(&mut fx, false, PAST_GAP + 900); + let funding = fund(&mut fx).await; + let mix = mix_of(&funding, &far); + fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; + + let skipped = coinjoin_address(&mut fx, false, PAST_GAP + 300); + let remix = tx( + vec![ + input(Txid::from_byte_array([0xf2; 32])), + TxIn { + previous_output: OutPoint { + txid: mix.txid(), + vout: 1, + }, + ..Default::default() }, - ..Default::default() - }], - output: vec![TxOut { - value: COLLATERAL, - script_pubkey: far.script_pubkey(), - }], - special_transaction_payload: None, + ], + vec![output(DENOM, &skipped), output(DENOM, &Address::dummy(NETWORK, 8))], + ); + let result = + fx.managed.check_core_transaction(&remix, block(3), &mut fx.wallet, true, true).await; + assert_eq!(result.total_received, DENOM, "the coin in the skipped run must be credited"); + assert_eq!(fx.managed.balance.total(), DENOM); + } + + /// The case behind the reset: the wallet synced past a mix whose coin it + /// could not see, and the chainlocked mix is never revisited. After the + /// reset, replaying the same blocks credits the coin. + #[tokio::test] + async fn the_replay_credits_a_coin_of_a_finalized_mix() { + let mut fx = fixture_without_recovery(NETWORK); + let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); + let funding = fund(&mut fx).await; + let mix = mix_of(&funding, &far); + let first = + fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; + assert_eq!(first.total_received, 0, "without a recovery the coin is missing"); + assert_eq!(fx.managed.balance.total(), 0); + + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start recovery"); + assert_eq!(fx.managed.balance.total(), 0); + let funded = + fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; + assert_eq!(funded.total_received, DENOM, "the replay records the funding again"); + let replayed = + fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; + assert_eq!(replayed.total_received, DENOM, "the replay credits the far coin"); + assert_eq!(fx.managed.balance.total(), DENOM); + } + + /// Starting a recovery drops the chain state, rewinds the checkpoint to + /// just below birth and bumps the account generation, but keeps the + /// processed height and the applied ChainLock; it ends once the replay is + /// back at its start height. + #[tokio::test] + async fn the_reset_rewinds_and_the_recovery_ends_at_its_start_height() { + let mut fx = fixture_without_recovery(NETWORK); + fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 1); + fund(&mut fx).await; + fx.managed.update_synced_height(500); + fx.managed.update_last_processed_height(500); + fx.managed.apply_chain_lock(ChainLock::dummy(400)); + let generation = fx.managed.account_generation(); + assert_eq!(fx.managed.balance.total(), DENOM); + + let until = fx.managed.begin_coinjoin_recovery(&fx.wallet, 0).expect("start recovery"); + assert_eq!(until, 500); + assert_eq!(fx.managed.balance.total(), 0); + assert!(fx.managed.transaction_history().is_empty()); + assert!(fx.managed.first_coinjoin_managed_account().expect("account").utxos.is_empty()); + assert_eq!(fx.managed.synced_height(), 0); + assert_eq!(fx.managed.last_processed_height(), 500, "a chain fact, kept"); + assert_eq!(fx.managed.account_generation(), generation + 1); + assert_eq!(fx.managed.last_applied_chain_lock().map(|cl| cl.block_height), Some(400)); + assert!( + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).is_err(), + "one recovery at a time per wallet" + ); + + fx.managed.update_synced_height(499); + assert_eq!( + { + fx.managed.on_synced_height_advanced(); + fx.managed.take_finished_coinjoin_recovery() + }, + None + ); + assert!(fx.managed.coinjoin_recovery.is_some()); + fx.managed.update_synced_height(500); + assert_eq!( + { + fx.managed.on_synced_height_advanced(); + fx.managed.take_finished_coinjoin_recovery().map(|p| p.found) + }, + Some(0) + ); + assert!(fx.managed.coinjoin_recovery.is_none(), "the probe is off again"); + } + + /// An account added from an extended public key alone cannot derive its + /// addresses again, so the reset keeps them, with their usage. + #[tokio::test] + async fn the_reset_keeps_an_account_the_wallet_cannot_derive() { + let mut fx = fixture_without_recovery(NETWORK); + let other = + Wallet::new_random(NETWORK, WalletAccountCreationOptions::Default).expect("wallet"); + let xpub = + other.accounts.standard_bip44_accounts.get(&0).expect("BIP44 account").account_xpub; + let account_type = AccountType::Standard { + index: 7, + standard_account_type: StandardAccountType::BIP44Account, + }; + fx.managed.add_managed_account_from_xpub(account_type, xpub).expect("watch-only account"); + let addresses = |managed: &ManagedWalletInfo| { + managed + .accounts + .standard_bip44_accounts + .get(&7) + .expect("account") + .managed_account_type() + .all_addresses() }; - let result = fx - .managed - .check_core_transaction(&make_collateral, block(2), &mut fx.wallet, true, true) + let before = addresses(&fx.managed); + assert!(!before.is_empty()); + + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start recovery"); + assert_eq!(addresses(&fx.managed), before); + } + + /// A wallet still in its first sync replays up to the chainlock it has + /// seen, not just to where its own sync had got: the final history is + /// what a normal sync would never revisit. + #[tokio::test] + async fn a_recovery_started_mid_sync_runs_to_the_chainlock() { + let mut fx = fixture_without_recovery(NETWORK); + fx.managed.update_synced_height(300); + fx.managed.apply_chain_lock(ChainLock::dummy(900)); + assert_eq!(fx.managed.begin_coinjoin_recovery(&fx.wallet, 0).expect("start"), 900); + fx.managed.update_synced_height(300); + assert_eq!( + { + fx.managed.on_synced_height_advanced(); + fx.managed.take_finished_coinjoin_recovery() + }, + None + ); + } + + /// The replay only brings back blocks, so a pending transaction would be + /// lost: the recovery waits until the wallet has none. + #[tokio::test] + async fn a_wallet_with_an_unconfirmed_transaction_cannot_start_a_recovery() { + let mut fx = fixture_without_recovery(NETWORK); + let first = coinjoin_address(&mut fx, false, 0); + let pending = Transaction::dummy(&first, 0..1, &[DENOM]); + fx.managed + .check_core_transaction( + &pending, + TransactionContext::Mempool, + &mut fx.wallet, + true, + true, + ) .await; - assert_eq!(result.total_received, COLLATERAL, "the collateral must be credited"); - assert_eq!(fx.managed.balance.total(), COLLATERAL); + assert!(matches!( + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000), + Err(crate::Error::InvalidState(_)) + )); + assert!(fx.managed.coinjoin_recovery.is_none()); + assert_eq!(fx.managed.transaction_history().len(), 1, "nothing was reset"); } - /// DashSync's denomination-creating transaction sent its odd change to the - /// CoinJoin internal chain past the wall; it is found with the coins. + /// Two coins of one transaction: the second lies past the window the pool + /// end opens, but inside the window the first coin's address opens. The + /// probe searches again after each find, since the transaction is final. #[tokio::test] - async fn odd_change_past_the_gap_beside_a_far_denomination_is_recovered() { - const CHANGE: u64 = 1_234_567; + async fn a_second_coin_reachable_only_from_the_first_is_found() { let mut fx = fixture(); - let first = coinjoin_address(&mut fx, false, 0); - let funding = dashcore::Transaction::dummy(&first, 0..1, &[DENOM + CHANGE]); - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); - let change = coinjoin_address(&mut fx, true, PAST_GAP + 600); - let create = dashcore::Transaction { - version: 2, - lock_time: 0, - input: vec![TxIn { - previous_output: OutPoint { - txid: funding.txid(), - vout: 0, - }, - ..Default::default() + let start = GAP + 1; + let near_far = coinjoin_address(&mut fx, false, start + 9_000); + let farther = coinjoin_address(&mut fx, false, start + 9_000 + 5_000); + assert!(start + 9_000 + 5_000 >= start + COINJOIN_RECOVERY_PROBE_WINDOW); + let funding = fund(&mut fx).await; + let create = tx( + vec![input(funding.txid())], + vec![output(DENOM, &near_far), output(DENOM, &farther)], + ); + let result = + fx.managed.check_core_transaction(&create, block(2), &mut fx.wallet, true, true).await; + assert_eq!(result.total_received, 2 * DENOM, "both coins must be credited"); + } + + /// A wallet nobody has synced past its birth has nothing to recover yet; + /// starting would end the replay at once. + #[tokio::test] + async fn a_recovery_with_nothing_known_of_the_chain_is_refused() { + let mut fx = fixture_without_recovery(NETWORK); + fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 100); + assert!(matches!( + fx.managed.begin_coinjoin_recovery(&fx.wallet, 0), + Err(crate::Error::InvalidState(_)) + )); + assert_eq!(fx.managed.begin_coinjoin_recovery(&fx.wallet, 5_000).expect("start"), 5_000); + assert_eq!(fx.managed.synced_height(), 99); + } + + /// Coins a transaction build has reserved are about to be spent by a + /// transaction the replay would never deliver. + #[tokio::test] + async fn coins_reserved_by_a_build_block_the_recovery() { + let mut fx = fixture_without_recovery(NETWORK); + let funding = fund(&mut fx).await; + let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); + account.reservations().reserve( + &[OutPoint { + txid: funding.txid(), + vout: 0, }], - output: vec![ - TxOut { - value: DENOM, - script_pubkey: far.script_pubkey(), + 0, + crate::managed_account::reservation::ReservationToken::next(), + ); + assert!(matches!( + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000), + Err(crate::Error::InvalidState(_)) + )); + assert_eq!(fx.managed.balance.total(), DENOM, "nothing was reset"); + } + + /// A wallet whose checkpoint sits below its birth stays there: the reset + /// only ever rewinds. + #[tokio::test] + async fn the_reset_never_moves_the_checkpoint_forward() { + let mut fx = fixture_without_recovery(NETWORK); + fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 100); + fx.managed.metadata.synced_height = 0; + fx.managed.begin_coinjoin_recovery(&fx.wallet, 5_000).expect("start"); + assert_eq!(fx.managed.synced_height(), 0); + } + + /// A cancelled recovery leaves the probe off and lets a new one start. + #[tokio::test] + async fn a_cancelled_recovery_turns_the_probe_off() { + let mut fx = fixture(); + assert_eq!(fx.managed.cancel_coinjoin_recovery(), Some(0)); + assert_eq!(fx.managed.cancel_coinjoin_recovery(), None); + let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); + assert_eq!(fund_and_mix(&mut fx, &far).await.total_received, 0); + assert!(fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).is_ok()); + } + + /// A gap limit the wallet widened at runtime survives the reset, so the + /// replay finds what the normal sync found with it. + #[tokio::test] + async fn the_reset_keeps_a_widened_gap_limit() { + let mut fx = fixture_without_recovery(NETWORK); + let wide = GAP + 50; + { + let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); + for pool in account.managed_account_type_mut().address_pools_mut() { + pool.set_gap_limit(wide, &fx.key_source).expect("widen"); + } + } + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); + let external = pool(&fx, AddressPoolType::External); + assert_eq!(external.gap_limit, wide); + assert_eq!(external.highest_generated, Some(wide - 1)); + } + + /// A pool outside CoinJoin may hold addresses a walk from index 0 never + /// reaches again; the reset keeps them and their usage. + #[tokio::test] + async fn the_reset_keeps_addresses_outside_coinjoin_past_their_gap() { + let mut fx = fixture_without_recovery(NETWORK); + fx.wallet + .add_account( + AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, }, - TxOut { - value: CHANGE, - script_pubkey: change.script_pubkey(), + None, + ) + .expect("BIP44 account"); + fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 0); + let main_xpub = + fx.wallet.accounts.standard_bip44_accounts.get(&0).expect("BIP44 account").account_xpub; + let far = { + let account = fx.managed.first_bip44_managed_account_mut().expect("account"); + let pool = account + .managed_account_type_mut() + .address_pools_mut() + .into_iter() + .find(|pool| pool.pool_type == AddressPoolType::External) + .expect("external pool"); + let far = pool + .generate_address_at_index(500, &KeySource::Public(main_xpub), true) + .expect("derive"); + pool.mark_used(&far); + far + }; + + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); + let account = fx.managed.first_bip44_managed_account_mut().expect("account"); + let pool = account + .managed_account_type() + .address_pools() + .into_iter() + .find(|pool| pool.pool_type == AddressPoolType::External) + .expect("external pool"); + assert_eq!(pool.address_index(&far), Some(500), "the far address is still watched"); + assert!( + pool.used_indices.contains(&500), + "its usage stays, so it is not handed out as fresh meanwhile" + ); + } + + /// While the replay runs the coins are those of a past height, so the + /// wallet refuses to build from them. + #[tokio::test] + async fn a_build_is_refused_while_the_replay_runs() { + use crate::wallet::managed_wallet_info::coin_selection::SelectionStrategy; + use crate::wallet::managed_wallet_info::fee::FeeRate; + use crate::wallet::managed_wallet_info::transaction_builder::BuilderError; + + let mut fx = fixture(); + let to = Address::dummy(NETWORK, 9).to_string().parse().expect("address"); + let result = fx.managed.build_unsigned_transaction( + &fx.wallet, + &[], + 0, + vec![(to, 1_000)], + FeeRate::new(1000), + SelectionStrategy::LargestFirst, + ); + assert!(matches!(result, Err(BuilderError::RecoveryInProgress))); + } + + /// An address handed out but not yet paid stays handed out across the + /// reset, so it is not given to a second payer. + #[tokio::test] + async fn the_reset_keeps_reserved_addresses_reserved() { + let mut fx = fixture_without_recovery(NETWORK); + fx.wallet + .add_account( + AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, }, - ], - special_transaction_payload: None, + None, + ) + .expect("BIP44 account"); + fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 0); + let key_source = KeySource::Public( + fx.wallet.accounts.standard_bip44_accounts.get(&0).expect("account").account_xpub, + ); + let external = |managed: &mut ManagedWalletInfo| { + managed + .first_bip44_managed_account_mut() + .expect("account") + .managed_account_type_mut() + .address_pools_mut() + .into_iter() + .find(|pool| pool.pool_type == AddressPoolType::External) + .expect("external pool") + .clone() }; - let result = - fx.managed.check_core_transaction(&create, block(2), &mut fx.wallet, true, true).await; - assert_eq!(result.total_received, DENOM + CHANGE, "the change must be credited too"); + let handed_out = { + let account = fx.managed.first_bip44_managed_account_mut().expect("account"); + let pool = account + .managed_account_type_mut() + .address_pools_mut() + .into_iter() + .find(|pool| pool.pool_type == AddressPoolType::External) + .expect("external pool"); + pool.next_unused_and_reserve(&key_source, 1).expect("reserve") + }; + + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); + let pool = external(&mut fx.managed); + assert!(pool.address_info(&handed_out).expect("kept").is_reserved()); + } + + /// A CoinJoin address the wallet had seen in use is kept by the reset even + /// past an empty run wider than the window, and a coin later found in that + /// run below it is still found. + #[tokio::test] + async fn the_reset_keeps_used_coinjoin_addresses_and_probes_below_them() { + let mut fx = fixture_without_recovery(NETWORK); + let kept_index = GAP + COINJOIN_RECOVERY_PROBE_WINDOW + 500; + { + let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); + let pool = account + .managed_account_type_mut() + .address_pools_mut() + .into_iter() + .find(|pool| pool.pool_type == AddressPoolType::External) + .expect("external pool"); + pool.generate_address_at_index(kept_index, &fx.key_source, true).expect("derive"); + pool.mark_index_used(kept_index); + } + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); + let external = pool(&fx, AddressPoolType::External); + assert!(external.info_at_index(kept_index).is_some(), "the used address is still watched"); + assert!(external.used_indices.contains(&kept_index), "and stays marked used"); + assert!(external.info_at_index(GAP + 300).is_none(), "the empty run is not"); + + let below = coinjoin_address(&mut fx, false, GAP + 300); + let result = fund_and_mix(&mut fx, &below).await; + assert_eq!(result.total_received, DENOM, "a coin in the run below it is found"); + } + + /// A wallet without a CoinJoin account has nothing for a recovery to find. + #[tokio::test] + async fn a_wallet_without_coinjoin_cannot_start_a_recovery() { + let wallet = + Wallet::new_random(NETWORK, WalletAccountCreationOptions::None).expect("wallet"); + let mut managed = ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0); + assert!(matches!( + managed.begin_coinjoin_recovery(&wallet, 1_000), + Err(crate::Error::InvalidState(_)) + )); + } + + /// A CoinJoin address handed out but not yet paid stays handed out in the + /// rebuilt pool. + #[tokio::test] + async fn the_reset_keeps_reserved_coinjoin_addresses_reserved() { + let mut fx = fixture_without_recovery(NETWORK); + let handed_out = { + let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); + let pool = account + .managed_account_type_mut() + .address_pools_mut() + .into_iter() + .find(|pool| pool.pool_type == AddressPoolType::External) + .expect("external pool"); + pool.next_unused_and_reserve(&fx.key_source, 1).expect("reserve") + }; + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); + let external = pool(&fx, AddressPoolType::External); + assert!(external.address_info(&handed_out).expect("kept").is_reserved()); + } + + /// A build assembled straight from the builder refuses too: the replayed + /// accounts offer no coins. + #[tokio::test] + async fn a_direct_builder_refuses_to_fund_from_a_replayed_account() { + use crate::wallet::managed_wallet_info::transaction_builder::{ + BuilderError, TransactionBuilder, + }; + + let mut fx = fixture(); + let account = fx.wallet.accounts.coinjoin_accounts.get(&0).expect("account").clone(); + let funds = fx.managed.first_coinjoin_managed_account_mut().expect("account"); + let result = TransactionBuilder::new() + .add_output(&Address::dummy(NETWORK, 9), 1_000) + .add_funding(funds, &account) + .build_unsigned_reserved(); + assert!(matches!(result, Err(BuilderError::RecoveryInProgress))); + } + + /// Kept addresses close to each other keep the run between them watched, + /// and each used one the gap past it, as a normal gap walk would; the rest + /// of a wider run stays out. + #[tokio::test] + async fn the_reset_fills_narrow_runs_between_kept_addresses_only() { + let mut fx = fixture_without_recovery(NETWORK); + let near = GAP + 5; + let far = near + GAP + 500; + { + let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); + let pool = account + .managed_account_type_mut() + .address_pools_mut() + .into_iter() + .find(|pool| pool.pool_type == AddressPoolType::External) + .expect("external pool"); + for index in [near, far] { + pool.generate_address_at_index(index, &fx.key_source, true).expect("derive"); + pool.mark_index_used(index); + } + } + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); + let external = pool(&fx, AddressPoolType::External); + assert!((0..=near + GAP).all(|index| external.info_at_index(index).is_some())); + assert!(external.info_at_index(near + GAP + 1).is_none(), "the wide run stays out"); + assert!(external.info_at_index(far).is_some()); + } + + /// A cancelled probe does not free the coins: until the replay is back at + /// the height the recovery was to run to, they are those of a past height. + #[tokio::test] + async fn builds_stay_refused_after_a_cancel_until_the_replay_catches_up() { + use crate::wallet::managed_wallet_info::coin_selection::SelectionStrategy; + use crate::wallet::managed_wallet_info::fee::FeeRate; + use crate::wallet::managed_wallet_info::transaction_builder::BuilderError; + + let mut fx = fixture(); + fx.managed.cancel_coinjoin_recovery(); + let build = |fx: &mut Fixture| { + let to = Address::dummy(NETWORK, 9).to_string().parse().expect("address"); + fx.managed.build_unsigned_transaction( + &fx.wallet, + &[], + 0, + vec![(to, 1_000)], + FeeRate::new(1000), + SelectionStrategy::LargestFirst, + ) + }; + assert!(matches!(build(&mut fx), Err(BuilderError::RecoveryInProgress))); + fx.managed.update_synced_height(1_000); + assert!(!matches!(build(&mut fx), Err(BuilderError::RecoveryInProgress))); + assert!( + matches!( + fx.managed.take_finished_coinjoin_recovery(), + Some(CoinJoinRecoveryProgress { + probing: false, + .. + }) + ), + "the end of a cancelled replay is reported as such" + ); + } + + /// An account added while the replay runs holds coins of a past height + /// as much as the others, so it refuses to fund a build too. + #[tokio::test] + async fn an_account_added_during_the_replay_is_replaying_too() { + let mut fx = fixture(); + fx.wallet + .add_account( + AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }, + None, + ) + .expect("BIP44 account"); + fx.managed + .add_managed_account( + &fx.wallet, + AccountType::Standard { + index: 0, + standard_account_type: StandardAccountType::BIP44Account, + }, + ) + .expect("managed BIP44 account"); + assert!(fx.managed.first_bip44_managed_account_mut().expect("account").replaying); + } + + /// The rebuilt pool keeps the usual lookahead past the highest kept used + /// address, so a coin just past it is watched, not left to the probe. + #[tokio::test] + async fn the_rebuilt_pool_watches_the_gap_past_kept_addresses() { + let mut fx = fixture_without_recovery(NETWORK); + let kept_index = GAP + COINJOIN_RECOVERY_PROBE_WINDOW + 500; + { + let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); + let pool = account + .managed_account_type_mut() + .address_pools_mut() + .into_iter() + .find(|pool| pool.pool_type == AddressPoolType::External) + .expect("external pool"); + pool.generate_address_at_index(kept_index, &fx.key_source, true).expect("derive"); + pool.mark_index_used(kept_index); + } + fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); + assert_eq!(pool_end(&fx, AddressPoolType::External), Some(kept_index + GAP)); } } diff --git a/key-wallet/src/transaction_checking/transaction_router/mod.rs b/key-wallet/src/transaction_checking/transaction_router/mod.rs index 201f9561c..ff643e422 100644 --- a/key-wallet/src/transaction_checking/transaction_router/mod.rs +++ b/key-wallet/src/transaction_checking/transaction_router/mod.rs @@ -38,21 +38,6 @@ pub enum TransactionType { /// Router for determining which accounts to check for a transaction pub struct TransactionRouter; -/// Standard CoinJoin denominations, each including the per-round fee -/// (Dash Core `coinjoin/common.h`): denom + denom/1000 + 1, with COIN = 100_000_000. -pub(crate) const COINJOIN_DENOMINATIONS: [u64; 5] = [ - 1_000_010_000, // 10 DASH + fee - 100_001_000, // 1 DASH + fee - 10_000_100, // 0.1 DASH + fee - 1_000_010, // 0.01 DASH + fee - 100_001, // 0.001 DASH + fee -]; - -/// The CoinJoin collateral range (Dash Core `CCoinJoin::IsCollateralAmount`): -/// from the smallest denomination / 10 up to four times that. -pub(crate) const COINJOIN_MIN_COLLATERAL: u64 = COINJOIN_DENOMINATIONS[4] / 10; -pub(crate) const COINJOIN_MAX_COLLATERAL: u64 = COINJOIN_MIN_COLLATERAL * 4; - impl TransactionRouter { /// Classify a transaction based on its type and payload pub fn classify_transaction(tx: &Transaction) -> TransactionType { @@ -218,6 +203,16 @@ impl TransactionRouter { /// Check if transaction has denomination outputs typical of CoinJoin fn has_denomination_outputs(tx: &Transaction) -> bool { + // Standard CoinJoin denominations, each including the per-round fee + // (Dash Core `coinjoin/common.h`): denom + denom/1000 + 1, with COIN = 100_000_000. + const COINJOIN_DENOMINATIONS: [u64; 5] = [ + 1_000_010_000, // 10 DASH + fee + 100_001_000, // 1 DASH + fee + 10_000_100, // 0.1 DASH + fee + 1_000_010, // 0.01 DASH + fee + 100_001, // 0.001 DASH + fee + ]; + let mut denomination_count = 0; for output in &tx.output { if COINJOIN_DENOMINATIONS.contains(&output.value) { diff --git a/key-wallet/src/transaction_checking/wallet_checker.rs b/key-wallet/src/transaction_checking/wallet_checker.rs index 590ddabb5..cabfbf044 100644 --- a/key-wallet/src/transaction_checking/wallet_checker.rs +++ b/key-wallet/src/transaction_checking/wallet_checker.rs @@ -228,19 +228,23 @@ impl WalletTransactionChecker for ManagedWalletInfo { // Get relevant account types for this transaction type let relevant_types = TransactionRouter::get_relevant_account_types(&tx_type); - // A CoinJoin coin of ours that came back past the pool's gap limit is - // found here, before the check, so the check below credits it (32008). - let recovered = if update_state && relevant_types.contains(&AccountTypeToCheck::CoinJoin) { - self.recover_coinjoin_outputs(tx, wallet) - } else { - Vec::new() - }; - // Check only relevant account types let mut result = self.accounts.check_transaction(tx, &relevant_types); - if !recovered.is_empty() { - result.new_addresses.extend(recovered); - result.state_modified = true; + + // During a CoinJoin recovery replay, a relevant transaction may also pay + // CoinJoin addresses of ours past the pools' gap limit (32008). They are + // added to the pools before any state is touched, and the check is + // repeated so the update below credits them. + if update_state + && result.is_relevant + && relevant_types.contains(&AccountTypeToCheck::CoinJoin) + { + let recovered = self.recover_coinjoin_outputs(tx, wallet); + if !recovered.is_empty() { + result = self.accounts.check_transaction(tx, &relevant_types); + result.new_addresses.extend(recovered); + result.state_modified = true; + } } // #649: remember every spend seen in a block, independent of the diff --git a/key-wallet/src/wallet/managed_wallet_info/asset_lock_builder.rs b/key-wallet/src/wallet/managed_wallet_info/asset_lock_builder.rs index 322098b3c..074e49bf9 100644 --- a/key-wallet/src/wallet/managed_wallet_info/asset_lock_builder.rs +++ b/key-wallet/src/wallet/managed_wallet_info/asset_lock_builder.rs @@ -384,6 +384,9 @@ impl ManagedWalletInfo { drain: bool, signer: &T, ) -> Result { + if self.is_replaying() { + return Err(BuilderError::RecoveryInProgress.into()); + } validate_funding_sources(funding_sources, credit_outputs.len(), drain)?; // Build first, derive credit keys after — a build failure must not diff --git a/key-wallet/src/wallet/managed_wallet_info/mod.rs b/key-wallet/src/wallet/managed_wallet_info/mod.rs index b44e1ddd1..163f6547c 100644 --- a/key-wallet/src/wallet/managed_wallet_info/mod.rs +++ b/key-wallet/src/wallet/managed_wallet_info/mod.rs @@ -60,11 +60,19 @@ pub struct ManagedWalletInfo { /// Transactions that have received an InstantSend lock. #[cfg_attr(feature = "serde", serde(skip))] pub(crate) instant_send_locks: HashSet, - /// CoinJoin addresses derived past the pool ends while probing for lost - /// mixed coins; a cache only, never persisted. + /// The CoinJoin recovery replay running on this wallet, if any, until the + /// replay is back at the height it was to run to (builds are refused + /// meanwhile, even after a cancel stopped its probe). In memory only: a + /// restart ends it, and the wallet resumes a normal sync from wherever the + /// replay got to. #[cfg_attr(feature = "serde", serde(skip))] - pub(crate) coinjoin_probe_cache: - crate::transaction_checking::coinjoin_recovery::CoinJoinProbeCache, + pub(crate) coinjoin_recovery: + Option, + /// What a CoinJoin recovery found, once its replay completed, until the + /// manager takes it to report the end. + #[cfg_attr(feature = "serde", serde(skip))] + pub(crate) finished_coinjoin_recovery: + Option, /// Outpoints observed spent by transactions seen in a processed block, /// mapped to the height of the block that spent them. /// @@ -249,7 +257,8 @@ impl ManagedWalletInfo { accounts: ManagedAccountCollection::new(), balance: WalletCoreBalance::default(), instant_send_locks: HashSet::new(), - coinjoin_probe_cache: Default::default(), + coinjoin_recovery: None, + finished_coinjoin_recovery: None, observed_spent_outpoints: BTreeMap::new(), account_generation: 0, noted_chain_lock_height: None, @@ -267,7 +276,8 @@ impl ManagedWalletInfo { accounts: ManagedAccountCollection::new(), balance: WalletCoreBalance::default(), instant_send_locks: HashSet::new(), - coinjoin_probe_cache: Default::default(), + coinjoin_recovery: None, + finished_coinjoin_recovery: None, observed_spent_outpoints: BTreeMap::new(), account_generation: 0, noted_chain_lock_height: None, @@ -295,7 +305,8 @@ impl ManagedWalletInfo { accounts: ManagedAccountCollection::from_account_collection(&wallet.accounts), balance: WalletCoreBalance::default(), instant_send_locks: HashSet::new(), - coinjoin_probe_cache: Default::default(), + coinjoin_recovery: None, + finished_coinjoin_recovery: None, observed_spent_outpoints: BTreeMap::new(), account_generation: 0, noted_chain_lock_height: None, @@ -406,7 +417,7 @@ impl ManagedWalletInfo { /// `last_processed_height` and `last_applied_chain_lock` are intentionally /// left as-is: they are chain facts, not coverage facts, and keeping the /// chainlock is what lets the backfilled history arrive born-chainlocked. - fn rewind_sync_checkpoint_for_new_account(&mut self) { + pub(crate) fn rewind_sync_checkpoint_for_new_account(&mut self) { let floor = self.metadata.birth_height.saturating_sub(1); self.metadata.synced_height = self.metadata.synced_height.min(floor); // Bump unconditionally — even when the checkpoint was already at or @@ -415,6 +426,11 @@ impl ManagedWalletInfo { // the generation mismatch is the only signal a commit-time check has // when the rewind is height-invisible (dashpay/rust-dashcore#649). self.account_generation += 1; + // An account added during a CoinJoin recovery replay holds coins of a + // past height as much as the others. + if self.coinjoin_recovery.is_some() { + self.set_funds_replaying(true); + } } /// Current generation of the wallet's account set; see the diff --git a/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs b/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs index 57afdefa6..0e5ebe7bf 100644 --- a/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs +++ b/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs @@ -95,6 +95,9 @@ pub struct TransactionBuilder { /// account that holds the UTXO, so each account reserves its own share of /// the chosen inputs — all under the one token this build is stamped with. funding: Vec<(ReservationSet, HashSet)>, + /// A funding account was being replayed by a CoinJoin recovery, so the + /// build must fail rather than spend coins of a past height. + replaying_funds: bool, } impl Default for TransactionBuilder { @@ -119,6 +122,7 @@ impl TransactionBuilder { special_payload: None, payload_finalizer: None, funding: Vec::new(), + replaying_funds: false, } } @@ -195,6 +199,10 @@ impl TransactionBuilder { acc: &Account, contribute_candidates: bool, ) -> Self { + if funds_acc.replaying { + self.replaying_funds = true; + return self; + } let reserved = funds_acc.reservations().reserved(self.current_height); // An outpoint the builder already holds — seeded by `add_inputs`, or // offered by an earlier `add_funding` of an overlapping account — must @@ -452,6 +460,9 @@ impl TransactionBuilder { fn assemble_unsigned( mut self, ) -> Result<(Transaction, Vec, Option), BuilderError> { + if self.replaying_funds { + return Err(BuilderError::RecoveryInProgress); + } if let Some(TransactionPayload::AssetLockPayloadType(p)) = &self.special_payload { if p.credit_outputs.is_empty() { return Err(BuilderError::NoOutputs); @@ -1040,6 +1051,9 @@ pub enum BuilderError { len: usize, max: usize, }, + /// The wallet is being replayed by a CoinJoin recovery, so its coins are + /// those of a past height; a build has to wait until the replay is done. + RecoveryInProgress, } impl fmt::Display for BuilderError { @@ -1072,6 +1086,9 @@ impl fmt::Display for BuilderError { } => { write!(f, "OP_RETURN payload too large: {len} bytes (max {max})") } + Self::RecoveryInProgress => { + write!(f, "The wallet is being replayed by a CoinJoin recovery; try again when it finishes") + } } } } diff --git a/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs b/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs index e82a1212e..3cbb50397 100644 --- a/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs +++ b/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs @@ -197,6 +197,11 @@ impl ManagedWalletInfo { fee_rate: FeeRate, strategy: SelectionStrategy, ) -> Result<(TransactionBuilder, HashMap), BuilderError> { + // Up front, whichever sources were asked for; the builder refuses on + // its own as well, for callers that fund it directly. + if self.is_replaying() { + return Err(BuilderError::RecoveryInProgress); + } let outputs = outputs .into_iter() .map(|(address, value)| { diff --git a/key-wallet/src/wallet/managed_wallet_info/wallet_info_interface.rs b/key-wallet/src/wallet/managed_wallet_info/wallet_info_interface.rs index fe0f169cd..958ecf1a9 100644 --- a/key-wallet/src/wallet/managed_wallet_info/wallet_info_interface.rs +++ b/key-wallet/src/wallet/managed_wallet_info/wallet_info_interface.rs @@ -217,6 +217,51 @@ pub trait WalletInfoInterface: Sized + WalletTransactionChecker + ManagedAccount 0 } + /// Start a CoinJoin recovery replay through + /// `WalletManager::begin_coinjoin_recovery`, which also emits the reset + /// event a persisting consumer needs and runs one recovery at a time; + /// calling this directly skips both. Forgets everything the wallet learned + /// from the chain, rewind its sync checkpoint to just below its birth + /// height, and look for CoinJoin coins past the gap limit while the sync + /// layer replays it (ticket 32008). The account generation is bumped, so + /// in-flight scans cannot certify coverage for the reset wallet. + /// + /// `known_height` is the highest height the caller knows the chain to + /// have reached; the replay is complete once the wallet is synced to it, + /// or to its own synced height or chainlock when higher. Returns that + /// height. Required, so that a wrapper around another implementation has + /// to forward it (and the three methods below) rather than opt out + /// silently. + fn begin_coinjoin_recovery( + &mut self, + wallet: &Wallet, + known_height: CoreBlockHeight, + ) -> Result; + + /// Stop the probe of a running CoinJoin recovery before its replay is + /// complete, returning how many CoinJoin addresses it found past the gap + /// limit; `None` when no probe runs, which includes a cancelled recovery + /// whose replay still goes on (see `coinjoin_recovery_progress`). + fn cancel_coinjoin_recovery(&mut self) -> Option; + + /// Where the running CoinJoin recovery replay stands; `None` when none + /// runs. + fn coinjoin_recovery_progress( + &self, + ) -> Option; + + /// Where a CoinJoin recovery whose replay reached the height it was to + /// run to (the one `begin_coinjoin_recovery` returned) since the last call + /// ended up: how many CoinJoin addresses it found past the gap limit, and + /// whether it had been cancelled. The recovery itself ends inside + /// `update_synced_height`, whoever calls it; this only hands its result + /// over once. `WalletManager::update_wallet_synced_height` takes it right + /// after advancing and reports it as an event, so a caller advancing the + /// height any other way reports nothing. + fn take_finished_coinjoin_recovery( + &mut self, + ) -> Option; + /// Return the highest chainlock that has been applied to this /// wallet, retaining the signing proof. Blocks at or below /// `chain_lock.block_height` are considered chainlock-finalized @@ -362,6 +407,30 @@ impl WalletInfoInterface for ManagedWalletInfo { self.account_generation } + fn begin_coinjoin_recovery( + &mut self, + wallet: &Wallet, + known_height: CoreBlockHeight, + ) -> Result { + ManagedWalletInfo::begin_coinjoin_recovery(self, wallet, known_height) + } + + fn cancel_coinjoin_recovery(&mut self) -> Option { + ManagedWalletInfo::cancel_coinjoin_recovery(self) + } + + fn coinjoin_recovery_progress( + &self, + ) -> Option { + self.coinjoin_recovery.as_ref().map(|recovery| recovery.progress()) + } + + fn take_finished_coinjoin_recovery( + &mut self, + ) -> Option { + ManagedWalletInfo::take_finished_coinjoin_recovery(self) + } + fn last_applied_chain_lock(&self) -> Option<&ChainLock> { self.metadata.last_applied_chain_lock.as_ref() } @@ -566,6 +635,7 @@ impl WalletInfoInterface for ManagedWalletInfo { // A newly committed checkpoint can lift the finality boundary when the // chainlock was already ahead of the old synced_height. self.prune_finalized_observed_spends(); + self.on_synced_height_advanced(); } fn truncate_above(&mut self, height: CoreBlockHeight) -> Result { From 85e5735f55ae25082aeb65f8d34ef31fa78f0e64 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Fri, 16 Oct 2026 15:04:55 +0300 Subject: [PATCH 13/24] docs(key-wallet): link the public recovery entry point from the module doc Co-Authored-By: Claude Opus 5.5 --- key-wallet/src/transaction_checking/coinjoin_recovery.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs index d6aec52e3..88b83c3af 100644 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ b/key-wallet/src/transaction_checking/coinjoin_recovery.rs @@ -12,7 +12,7 @@ //! Rescanning the wallet as it stands cannot fix that either: every old mix is //! chainlocked by now, and a finalized transaction is never revisited, so a //! coin found later in it is never credited. Recovery therefore starts from -//! fresh wallet state. [`ManagedWalletInfo::begin_coinjoin_recovery`] forgets +//! fresh wallet state. [`WalletInfoInterface::begin_coinjoin_recovery`] forgets //! everything the wallet learned from the chain, rewinds its sync checkpoint to //! just below its birth height, and turns the probe on for the replay that //! follows; the sync layer sees the wallet behind and replays it from birth. From 775e533c91b44abf7502c321e4a9aef47436dc6b Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Thu, 22 Oct 2026 14:58:29 +0300 Subject: [PATCH 14/24] refactor(key-wallet): replace the CoinJoin reset-and-replay recovery with extendable pools Per review: drop the reset + replay recovery (wallet-manager entry point, events, FFI, scan engine) and keep only what the remove + re-add flow needs. ManagedWalletInfo::extend_coinjoin_pools derives both pools of every CoinJoin account to a given depth on fresh wallet state, before the first sync from birth, so mixed coins far past the gap limit are found by the regular sync. trim_coinjoin_pools drops the unused tail afterwards. Co-Authored-By: Claude Opus 5.5 --- dash-spv-ffi/src/bin/ffi_cli.rs | 37 - dash-spv-ffi/src/callbacks.rs | 178 +- dash-spv-ffi/tests/dashd_sync/callbacks.rs | 3 - key-wallet-ffi/FFI_API.md | 55 +- key-wallet-ffi/src/error.rs | 9 +- key-wallet-ffi/src/wallet_manager.rs | 164 -- key-wallet-ffi/src/wallet_manager_tests.rs | 136 -- .../src/coinjoin_recovery_tests.rs | 265 --- key-wallet-manager/src/error.rs | 9 - key-wallet-manager/src/events.rs | 70 - key-wallet-manager/src/lib.rs | 141 -- key-wallet-manager/src/process_block.rs | 9 - .../managed_account_collection.rs | 17 +- .../managed_core_funds_account.rs | 26 - .../managed_core_keys_account.rs | 29 - .../transaction_checking/coinjoin_recovery.rs | 1597 ----------------- key-wallet/src/transaction_checking/mod.rs | 1 - .../transaction_checking/wallet_checker.rs | 16 - .../managed_wallet_info/asset_lock_builder.rs | 3 - .../managed_wallet_info/coinjoin_pools.rs | 203 +++ .../src/wallet/managed_wallet_info/mod.rs | 27 +- .../transaction_builder.rs | 17 - .../transaction_building.rs | 5 - .../wallet_info_interface.rs | 70 - 24 files changed, 216 insertions(+), 2871 deletions(-) delete mode 100644 key-wallet-manager/src/coinjoin_recovery_tests.rs delete mode 100644 key-wallet/src/transaction_checking/coinjoin_recovery.rs create mode 100644 key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs diff --git a/dash-spv-ffi/src/bin/ffi_cli.rs b/dash-spv-ffi/src/bin/ffi_cli.rs index 3f178d0ad..b83348edc 100644 --- a/dash-spv-ffi/src/bin/ffi_cli.rs +++ b/dash-spv-ffi/src/bin/ffi_cli.rs @@ -292,41 +292,6 @@ extern "C" fn on_chain_truncated( ); } -extern "C" fn on_wallet_reset( - wallet_id: *const c_char, - synced_height: u32, - balance: *const FFIBalance, - _coinjoin_addresses: *const dash_spv_ffi::FFIDerivedAddress, - coinjoin_addresses_count: u32, - _user_data: *mut c_void, -) { - let b = read_balance(balance); - println!( - "[Wallet] Reset for CoinJoin recovery: wallet={}..., synced_height={}, coinjoin_addresses={}, balance[confirmed={}, unconfirmed={}]", - short_wallet(wallet_id), - synced_height, - coinjoin_addresses_count, - b.confirmed, - b.unconfirmed, - ); -} - -extern "C" fn on_coinjoin_recovery_finished( - wallet_id: *const c_char, - height: u32, - found: u32, - cancelled: bool, - _user_data: *mut c_void, -) { - println!( - "[Wallet] CoinJoin recovery finished: wallet={}..., height={}, found={}, cancelled={}", - short_wallet(wallet_id), - height, - found, - cancelled, - ); -} - extern "C" fn on_transaction_instant_locked( wallet_id: *const c_char, txid: *const [u8; 32], @@ -654,8 +619,6 @@ fn main() { on_sync_height_advanced: Some(on_sync_height_advanced), on_chain_lock_processed: Some(on_wallet_chain_lock_processed), on_chain_truncated: Some(on_chain_truncated), - on_wallet_reset: Some(on_wallet_reset), - on_coinjoin_recovery_finished: Some(on_coinjoin_recovery_finished), user_data: ptr::null_mut(), }, error: FFIClientErrorCallback { diff --git a/dash-spv-ffi/src/callbacks.rs b/dash-spv-ffi/src/callbacks.rs index b3f739807..44eb76986 100644 --- a/dash-spv-ffi/src/callbacks.rs +++ b/dash-spv-ffi/src/callbacks.rs @@ -788,9 +788,8 @@ impl FFIOutPoint { /// provably beat to one of their inputs: they can never confirm, so their /// outputs are gone from the UTXO set and their records deleted. /// -/// **A removal-shaped wallet callback**, like `on_chain_truncated` and -/// `on_wallet_reset`; every other one is additive. A consumer mirroring -/// wallet state to disk must act on this — delete the +/// **The only removal-shaped wallet callback.** Every other one is additive, +/// so a consumer mirroring wallet state to disk must act on this — delete the /// named transactions and any UTXO they created. Ignoring it leaves the dead /// rows in the mirror, which replays them on the next load and re-creates a /// balance the wallet has already corrected. @@ -866,50 +865,6 @@ pub type OnChainTruncatedCallback = Option< ), >; -/// Callback for `WalletEvent::WalletReset`. -/// -/// Fires once for the one wallet a CoinJoin recovery was started on: it dropped -/// everything it learned from the chain and rewound its sync checkpoint to -/// `synced_height`, just below its birth height, to be replayed from birth. A -/// consumer mirroring wallet state to disk must delete every transaction and -/// UTXO it holds for this wallet, replace its CoinJoin address rows with the -/// `coinjoin_addresses_count` -/// entries at `coinjoin_addresses`, and lower its synced height to -/// `synced_height`. Other address rows stay, and annotations such as labels -/// should be kept keyed by txid or address. The replay then delivers the -/// history again through the usual callbacks. Other wallets are not affected. -/// -/// All pointer parameters are borrowed and only valid for the duration of the -/// callback. `balance` is the wallet's balance *after* the reset. -/// `coinjoin_addresses` is null when its count is 0. -pub type OnWalletResetCallback = Option< - extern "C" fn( - wallet_id: *const c_char, - synced_height: u32, - balance: *const FFIBalance, - coinjoin_addresses: *const FFIDerivedAddress, - coinjoin_addresses_count: u32, - user_data: *mut c_void, - ), ->; - -/// Callback for `WalletEvent::CoinJoinRecoveryFinished`. -/// -/// Fires when a CoinJoin recovery ended: its replay reached the committed sync -/// height `height` it was to run to, the wallet's coins are current again and -/// builds from it are allowed. `found` is the number of CoinJoin addresses past -/// the gap limit the replay found coins on; `cancelled` says its probe had been -/// stopped before the replay completed. -pub type OnCoinJoinRecoveryFinishedCallback = Option< - extern "C" fn( - wallet_id: *const c_char, - height: u32, - found: u32, - cancelled: bool, - user_data: *mut c_void, - ), ->; - /// Callback for `WalletEvent::TransactionInstantLocked`. /// /// Fires when an InstantSend lock is applied to a previously-seen off-chain @@ -1072,8 +1027,6 @@ pub struct FFIWalletEventCallbacks { pub on_sync_height_advanced: OnSyncHeightAdvancedCallback, pub on_chain_lock_processed: OnWalletChainLockProcessedCallback, pub on_chain_truncated: OnChainTruncatedCallback, - pub on_wallet_reset: OnWalletResetCallback, - pub on_coinjoin_recovery_finished: OnCoinJoinRecoveryFinishedCallback, pub user_data: *mut c_void, } @@ -1091,8 +1044,6 @@ impl Default for FFIWalletEventCallbacks { on_sync_height_advanced: None, on_chain_lock_processed: None, on_chain_truncated: None, - on_wallet_reset: None, - on_coinjoin_recovery_finished: None, user_data: std::ptr::null_mut(), } } @@ -1491,53 +1442,6 @@ impl FFIWalletEventCallbacks { cb(c_wallet_id.as_ptr(), *height, self.user_data); } } - WalletEvent::WalletReset { - wallet_id, - synced_height, - balance, - coinjoin_addresses, - } => { - if let Some(cb) = self.on_wallet_reset { - let wallet_id_hex = wallet_id.to_lower_hex_string(); - let c_wallet_id = CString::new(wallet_id_hex).unwrap_or_default(); - let ffi_balance = FFIBalance::from(*balance); - let ffi_addresses = FFIDerivedAddress::from_slice(coinjoin_addresses); - let addresses_ptr = if ffi_addresses.is_empty() { - ptr::null() - } else { - ffi_addresses.as_ptr() - }; - cb( - c_wallet_id.as_ptr(), - *synced_height, - &ffi_balance, - addresses_ptr, - ffi_addresses.len() as u32, - self.user_data, - ); - drop(ffi_addresses); - } else { - // Like the sweep callback: a consumer that leaves this one - // unset keeps every row of a wallet that dropped them all. - tracing::warn!( - wallet_id = %wallet_id.to_lower_hex_string(), - "no on_wallet_reset callback set; the consumer will keep mirroring \ - state the wallet dropped for its CoinJoin recovery" - ); - } - } - WalletEvent::CoinJoinRecoveryFinished { - wallet_id, - height, - found, - cancelled, - } => { - if let Some(cb) = self.on_coinjoin_recovery_finished { - let wallet_id_hex = wallet_id.to_lower_hex_string(); - let c_wallet_id = CString::new(wallet_id_hex).unwrap_or_default(); - cb(c_wallet_id.as_ptr(), *height, *found, *cancelled, self.user_data); - } - } WalletEvent::ChainLockProcessed { wallet_id, chain_lock, @@ -1690,84 +1594,6 @@ mod tests { ); } - /// The reset reaches the consumer with its wallet, rewound height and - /// balance in their places, so it deletes the right wallet's rows. - #[test] - fn test_wallet_reset_dispatch_marshals_its_fields() { - static WALLET_ID: std::sync::Mutex = std::sync::Mutex::new(String::new()); - static SYNCED_HEIGHT: AtomicU32 = AtomicU32::new(u32::MAX); - static CONFIRMED: AtomicU32 = AtomicU32::new(u32::MAX); - static ADDRESSES_NULL: AtomicU32 = AtomicU32::new(u32::MAX); - static ADDRESSES_COUNT: AtomicU32 = AtomicU32::new(u32::MAX); - - extern "C" fn cb( - wallet_id: *const c_char, - synced_height: u32, - balance: *const FFIBalance, - addresses: *const FFIDerivedAddress, - addresses_count: u32, - _user: *mut c_void, - ) { - ADDRESSES_NULL.store(u32::from(addresses.is_null()), Ordering::SeqCst); - ADDRESSES_COUNT.store(addresses_count, Ordering::SeqCst); - let id = unsafe { std::ffi::CStr::from_ptr(wallet_id) }; - *WALLET_ID.lock().expect("id") = id.to_string_lossy().into_owned(); - SYNCED_HEIGHT.store(synced_height, Ordering::SeqCst); - CONFIRMED.store(unsafe { (*balance).confirmed } as u32, Ordering::SeqCst); - } - - let callbacks = FFIWalletEventCallbacks { - on_wallet_reset: Some(cb), - ..FFIWalletEventCallbacks::default() - }; - callbacks.dispatch(&WalletEvent::WalletReset { - wallet_id: [7u8; 32], - synced_height: 1_999, - balance: WalletCoreBalance::new(42, 0, 0, 0), - coinjoin_addresses: Vec::new(), - }); - - assert_eq!(*WALLET_ID.lock().expect("id"), "07".repeat(32)); - assert_eq!(SYNCED_HEIGHT.load(Ordering::SeqCst), 1_999); - assert_eq!(CONFIRMED.load(Ordering::SeqCst), 42); - assert_eq!(ADDRESSES_NULL.load(Ordering::SeqCst), 1, "no addresses: a null pointer"); - assert_eq!(ADDRESSES_COUNT.load(Ordering::SeqCst), 0); - } - - #[test] - fn test_coinjoin_recovery_finished_dispatch_marshals_its_fields() { - static HEIGHT: AtomicU32 = AtomicU32::new(u32::MAX); - static FOUND: AtomicU32 = AtomicU32::new(u32::MAX); - static CANCELLED: AtomicU32 = AtomicU32::new(u32::MAX); - - extern "C" fn cb( - _wallet_id: *const c_char, - height: u32, - found: u32, - cancelled: bool, - _user: *mut c_void, - ) { - HEIGHT.store(height, Ordering::SeqCst); - FOUND.store(found, Ordering::SeqCst); - CANCELLED.store(u32::from(cancelled), Ordering::SeqCst); - } - - let callbacks = FFIWalletEventCallbacks { - on_coinjoin_recovery_finished: Some(cb), - ..FFIWalletEventCallbacks::default() - }; - callbacks.dispatch(&WalletEvent::CoinJoinRecoveryFinished { - wallet_id: [7u8; 32], - height: 2_400_000, - found: 3, - cancelled: true, - }); - - assert_eq!(HEIGHT.load(Ordering::SeqCst), 2_400_000); - assert_eq!(FOUND.load(Ordering::SeqCst), 3); - assert_eq!(CANCELLED.load(Ordering::SeqCst), 1); - } - /// `BlocksNeeded` dispatch must pass exactly one entry per /// `FilterMatchKey` to the FFI callback (i.e. iterate keys, not /// inflated by the per-block wallet attribution). diff --git a/dash-spv-ffi/tests/dashd_sync/callbacks.rs b/dash-spv-ffi/tests/dashd_sync/callbacks.rs index 71fe67d87..b33f78cb1 100644 --- a/dash-spv-ffi/tests/dashd_sync/callbacks.rs +++ b/dash-spv-ffi/tests/dashd_sync/callbacks.rs @@ -625,9 +625,6 @@ pub(super) fn create_wallet_callbacks(tracker: &Arc) -> FFIWall // Not exercised by these tests: they never build a conflicting spend. on_transactions_swept: None, on_chain_truncated: None, - // Not exercised by these tests: they never start a CoinJoin recovery. - on_wallet_reset: None, - on_coinjoin_recovery_finished: None, user_data: Arc::as_ptr(tracker) as *mut c_void, } } diff --git a/key-wallet-ffi/FFI_API.md b/key-wallet-ffi/FFI_API.md index 26f0f28a0..3304a763c 100644 --- a/key-wallet-ffi/FFI_API.md +++ b/key-wallet-ffi/FFI_API.md @@ -4,7 +4,7 @@ This document provides a comprehensive reference for all FFI (Foreign Function I **Auto-generated**: This documentation is automatically generated from the source code. Do not edit manually. -**Total Functions**: 262 +**Total Functions**: 259 ## Table of Contents @@ -43,16 +43,13 @@ Functions: 4 ### Wallet Manager -Functions: 23 +Functions: 20 | Function | Description | Module | |----------|-------------|--------| | `wallet_manager_add_wallet_from_mnemonic` | Add a wallet from mnemonic to the manager (backward compatibility) # Safety... | wallet_manager | | `wallet_manager_add_wallet_from_mnemonic_return_serialized_bytes` | No description | wallet_manager | | `wallet_manager_add_wallet_from_mnemonic_with_options` | Add a wallet from mnemonic to the manager with options # Safety -... | wallet_manager | -| `wallet_manager_begin_coinjoin_recovery` | Start a CoinJoin recovery on one wallet Finds mixed coins that landed past... | wallet_manager | -| `wallet_manager_cancel_coinjoin_recovery` | Stop the CoinJoin recovery running on one wallet The wallet keeps what the... | wallet_manager | -| `wallet_manager_coinjoin_recovery_progress` | Report whether a CoinJoin recovery is running on one wallet Returns true... | wallet_manager | | `wallet_manager_create` | Create a new wallet manager # Safety `error` must be a valid pointer to an... | wallet_manager | | `wallet_manager_current_height` | Get current height for a network # Safety - `manager` must be a valid... | wallet_manager | | `wallet_manager_describe` | Describe the wallet manager for a given network and return a newly allocated... | wallet_manager | @@ -493,54 +490,6 @@ Add a wallet from mnemonic to the manager with options # Safety - `manager` mu --- -#### `wallet_manager_begin_coinjoin_recovery` - -```c -wallet_manager_begin_coinjoin_recovery(manager: *mut FFIWalletManager, wallet_id: *const u8, lowest_replayable_height: u32, until_height_out: *mut u32, error: *mut FFIError,) -> bool -``` - -**Description:** -Start a CoinJoin recovery on one wallet Finds mixed coins that landed past the CoinJoin pools' gap limit, which a normal sync never discovers. The wallet's chain-derived state is dropped and it is replayed from its birth height by the running SPV client; see `WalletManager::begin_coinjoin_recovery`. The reset is announced by the wallet-reset event and the end of the replay by the CoinJoin recovery-finished event. `lowest_replayable_height` is the lowest height the SPV client can replay, where its header storage starts (`0` when it syncs from genesis); a wallet born below it is refused with `InvalidState`. On success writes the committed sync height at which the recovery ends to `until_height_out` (when non-null) and returns true. Fails with `NotFound` when the wallet is unknown, and with `InvalidState` when a recovery is already running on any wallet of this manager, the wallet has no CoinJoin account, unconfirmed transactions or coins reserved by a transaction build, or nothing is known of the chain past the wallet's birth yet (sync it first). The error message says which. The recovery lives in memory only: after a restart it is gone and has to be started again (see `wallet_manager_coinjoin_recovery_progress`). Start it while the SPV client is stopped, or idle at the tip: a block the client already had in flight for this wallet can otherwise be applied to the reset wallet ahead of the replay, out of order. Until the replay is back at the returned height, transaction builds from this wallet fail. # Safety - `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `until_height_out` must be null or a valid pointer to a `u32` - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call - -**Safety:** -- `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `until_height_out` must be null or a valid pointer to a `u32` - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call - -**Module:** `wallet_manager` - ---- - -#### `wallet_manager_cancel_coinjoin_recovery` - -```c -wallet_manager_cancel_coinjoin_recovery(manager: *mut FFIWalletManager, wallet_id: *const u8, error: *mut FFIError,) -> bool -``` - -**Description:** -Stop the CoinJoin recovery running on one wallet The wallet keeps what the replay has found and goes on syncing normally; see `WalletManager::cancel_coinjoin_recovery`. Builds from the wallet stay refused until the replay completes, which the CoinJoin recovery-finished event (with `cancelled` set) reports. Returns false when no recovery probe runs on that wallet, and false with `NotFound` when the wallet is unknown. # Safety - `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call - -**Safety:** -- `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call - -**Module:** `wallet_manager` - ---- - -#### `wallet_manager_coinjoin_recovery_progress` - -```c -wallet_manager_coinjoin_recovery_progress(manager: *const FFIWalletManager, wallet_id: *const u8, until_height_out: *mut u32, found_out: *mut u32, probing_out: *mut bool, error: *mut FFIError,) -> bool -``` - -**Description:** -Report whether a CoinJoin recovery is running on one wallet Returns true while one runs, a cancelled one whose replay goes on included, writing the committed sync height at which it ends to `until_height_out`, the number of CoinJoin addresses past the gap limit found so far to `found_out`, and whether its probe still runs to `probing_out` (each when non-null). Returns false when none runs, including after a restart interrupted one, and false with `NotFound` when the wallet is unknown. # Safety - `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `until_height_out` and `found_out` must each be null or a valid pointer to a `u32` - `probing_out` must be null or a valid pointer to a `bool` - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call - -**Safety:** -- `manager` must be a valid pointer to an FFIWalletManager instance - `wallet_id` must be a valid pointer to a 32-byte wallet ID - `until_height_out` and `found_out` must each be null or a valid pointer to a `u32` - `probing_out` must be null or a valid pointer to a `bool` - `error` must be a valid pointer to an FFIError structure - The caller must ensure all pointers remain valid for the duration of this call - -**Module:** `wallet_manager` - ---- - #### `wallet_manager_create` ```c diff --git a/key-wallet-ffi/src/error.rs b/key-wallet-ffi/src/error.rs index 7c98882b5..24721f98d 100644 --- a/key-wallet-ffi/src/error.rs +++ b/key-wallet-ffi/src/error.rs @@ -309,8 +309,6 @@ impl From for FFIError { WalletError::InvalidParameter(_) => FFIErrorCode::InvalidInput, WalletError::TransactionBuild(_) => FFIErrorCode::InvalidTransaction, WalletError::InsufficientFunds - | WalletError::InvalidState(_) - | WalletError::CoinJoinRecoveryInProgress(_) | WalletError::TruncateBelowChainLock { .. } => FFIErrorCode::InvalidState, @@ -382,13 +380,8 @@ impl From for FFIError { impl From for FFIError { fn from(value: BuilderError) -> Self { - // A replayed wallet is a state to wait out, not a bad request. - let code = match value { - BuilderError::RecoveryInProgress => FFIErrorCode::InvalidState, - _ => FFIErrorCode::TransactionBuildingError, - }; FFIError { - code, + code: FFIErrorCode::TransactionBuildingError, message: CString::new(value.to_string()) .unwrap_or( CString::new( diff --git a/key-wallet-ffi/src/wallet_manager.rs b/key-wallet-ffi/src/wallet_manager.rs index 615c44cea..2cb96add8 100644 --- a/key-wallet-ffi/src/wallet_manager.rs +++ b/key-wallet-ffi/src/wallet_manager.rs @@ -508,170 +508,6 @@ pub unsafe extern "C" fn wallet_manager_process_transaction( !result.affected_wallets.is_empty() } -/// Start a CoinJoin recovery on one wallet -/// -/// Finds mixed coins that landed past the CoinJoin pools' gap limit, which a -/// normal sync never discovers. The wallet's chain-derived state is dropped and -/// it is replayed from its birth height by the running SPV client; see -/// `WalletManager::begin_coinjoin_recovery`. The reset is announced by the -/// wallet-reset event and the end of the replay by the CoinJoin -/// recovery-finished event. -/// -/// `lowest_replayable_height` is the lowest height the SPV client can replay, -/// where its header storage starts (`0` when it syncs from genesis); a wallet -/// born below it is refused with `InvalidState`. -/// -/// On success writes the committed sync height at which the recovery ends to -/// `until_height_out` (when non-null) and returns true. Fails with `NotFound` -/// when the wallet is unknown, and with `InvalidState` when a recovery is -/// already running on any wallet of this manager, the wallet has no CoinJoin -/// account, unconfirmed -/// transactions or coins reserved by a transaction build, or nothing is known -/// of the chain past the wallet's birth yet (sync it first). The error message -/// says which. The recovery lives in memory only: after a restart it is gone -/// and has to be started again (see `wallet_manager_coinjoin_recovery_progress`). -/// -/// Start it while the SPV client is stopped, or idle at the tip: a block the -/// client already had in flight for this wallet can otherwise be applied to -/// the reset wallet ahead of the replay, out of order. Until the replay is -/// back at the returned height, transaction builds from this wallet fail. -/// -/// # Safety -/// -/// - `manager` must be a valid pointer to an FFIWalletManager instance -/// - `wallet_id` must be a valid pointer to a 32-byte wallet ID -/// - `until_height_out` must be null or a valid pointer to a `u32` -/// - `error` must be a valid pointer to an FFIError structure -/// - The caller must ensure all pointers remain valid for the duration of this call -#[no_mangle] -pub unsafe extern "C" fn wallet_manager_begin_coinjoin_recovery( - manager: *mut FFIWalletManager, - wallet_id: *const u8, - lowest_replayable_height: u32, - until_height_out: *mut u32, - error: *mut FFIError, -) -> bool { - let manager_ref = deref_ptr_mut!(manager, error); - check_ptr!(wallet_id, error); - - let mut wallet_id_array = [0u8; 32]; - ptr::copy_nonoverlapping(wallet_id, wallet_id_array.as_mut_ptr(), 32); - - let result = manager_ref.runtime.block_on(async { - let mut manager_guard = manager_ref.manager.write().await; - manager_guard.begin_coinjoin_recovery(&wallet_id_array, lowest_replayable_height) - }); - let until_height = unwrap_or_return!(result, error); - if !until_height_out.is_null() { - *until_height_out = until_height; - } - - (*error).clean(); - true -} - -/// Report whether a CoinJoin recovery is running on one wallet -/// -/// Returns true while one runs, a cancelled one whose replay goes on included, -/// writing the committed sync height at which it ends to `until_height_out`, -/// the number of CoinJoin addresses past the gap limit found so far to -/// `found_out`, and whether its probe still runs to `probing_out` (each when -/// non-null). Returns false when -/// none runs, including after a restart interrupted one, and false with -/// `NotFound` when the wallet is unknown. -/// -/// # Safety -/// -/// - `manager` must be a valid pointer to an FFIWalletManager instance -/// - `wallet_id` must be a valid pointer to a 32-byte wallet ID -/// - `until_height_out` and `found_out` must each be null or a valid pointer to a `u32` -/// - `probing_out` must be null or a valid pointer to a `bool` -/// - `error` must be a valid pointer to an FFIError structure -/// - The caller must ensure all pointers remain valid for the duration of this call -#[no_mangle] -pub unsafe extern "C" fn wallet_manager_coinjoin_recovery_progress( - manager: *const FFIWalletManager, - wallet_id: *const u8, - until_height_out: *mut u32, - found_out: *mut u32, - probing_out: *mut bool, - error: *mut FFIError, -) -> bool { - let manager_ref = deref_ptr!(manager, error); - check_ptr!(wallet_id, error); - - let mut wallet_id_array = [0u8; 32]; - ptr::copy_nonoverlapping(wallet_id, wallet_id_array.as_mut_ptr(), 32); - - let progress = manager_ref.runtime.block_on(async { - let manager_guard = manager_ref.manager.read().await; - manager_guard - .get_wallet(&wallet_id_array) - .map(|_| manager_guard.coinjoin_recovery_progress(&wallet_id_array)) - }); - let progress = unwrap_or_return!( - progress.ok_or(key_wallet_manager::WalletError::WalletNotFound(wallet_id_array)), - error - ); - - (*error).clean(); - let Some(progress) = progress else { - return false; - }; - if !until_height_out.is_null() { - *until_height_out = progress.until_height; - } - if !found_out.is_null() { - *found_out = progress.found; - } - if !probing_out.is_null() { - *probing_out = progress.probing; - } - true -} - -/// Stop the CoinJoin recovery running on one wallet -/// -/// The wallet keeps what the replay has found and goes on syncing normally; -/// see `WalletManager::cancel_coinjoin_recovery`. Builds from the wallet stay -/// refused until the replay completes, which the CoinJoin recovery-finished -/// event (with `cancelled` set) reports. Returns false when no recovery probe -/// runs on that wallet, and false with `NotFound` when the wallet is unknown. -/// -/// # Safety -/// -/// - `manager` must be a valid pointer to an FFIWalletManager instance -/// - `wallet_id` must be a valid pointer to a 32-byte wallet ID -/// - `error` must be a valid pointer to an FFIError structure -/// - The caller must ensure all pointers remain valid for the duration of this call -#[no_mangle] -pub unsafe extern "C" fn wallet_manager_cancel_coinjoin_recovery( - manager: *mut FFIWalletManager, - wallet_id: *const u8, - error: *mut FFIError, -) -> bool { - let manager_ref = deref_ptr_mut!(manager, error); - check_ptr!(wallet_id, error); - - let mut wallet_id_array = [0u8; 32]; - ptr::copy_nonoverlapping(wallet_id, wallet_id_array.as_mut_ptr(), 32); - - let cancelled = manager_ref.runtime.block_on(async { - let mut manager_guard = manager_ref.manager.write().await; - manager_guard - .get_wallet(&wallet_id_array) - .is_some() - .then(|| manager_guard.cancel_coinjoin_recovery(&wallet_id_array)) - }); - let cancelled = unwrap_or_return!( - cancelled.ok_or(key_wallet_manager::WalletError::WalletNotFound(wallet_id_array)), - error - ); - - (*error).clean(); - cancelled -} - /// Get the network for this wallet manager /// /// # Safety diff --git a/key-wallet-ffi/src/wallet_manager_tests.rs b/key-wallet-ffi/src/wallet_manager_tests.rs index 213b4e02d..7a8239582 100644 --- a/key-wallet-ffi/src/wallet_manager_tests.rs +++ b/key-wallet-ffi/src/wallet_manager_tests.rs @@ -311,142 +311,6 @@ mod tests { } } - #[test] - fn test_wallet_manager_begin_coinjoin_recovery() { - let mut error = FFIError::default(); - let error = &mut error as *mut FFIError; - - let manager = unsafe { wallet_manager::wallet_manager_create(FFINetwork::Testnet, error) }; - assert!(!manager.is_null()); - let mnemonic = CString::new(TEST_MNEMONIC).unwrap(); - let success = unsafe { - wallet_manager::wallet_manager_add_wallet_from_mnemonic( - manager, - mnemonic.as_ptr(), - error, - ) - }; - assert!(success); - let mut wallet_ids: *mut u8 = ptr::null_mut(); - let mut id_count: usize = 0; - let success = unsafe { - wallet_manager::wallet_manager_get_wallet_ids( - manager, - &mut wallet_ids, - &mut id_count, - error, - ) - }; - assert!(success); - - let unknown = [9u8; 32]; - let started = unsafe { - wallet_manager::wallet_manager_begin_coinjoin_recovery( - manager, - unknown.as_ptr(), - 0, - ptr::null_mut(), - error, - ) - }; - assert!(!started); - assert_eq!(unsafe { (*error).code }, FFIErrorCode::NotFound); - - // Nothing is known of the chain yet: there is nothing to recover. - let started = unsafe { - wallet_manager::wallet_manager_begin_coinjoin_recovery( - manager, - wallet_ids, - 0, - ptr::null_mut(), - error, - ) - }; - assert!(!started); - assert_eq!(unsafe { (*error).code }, FFIErrorCode::InvalidState); - - let mut wallet_id: WalletId = [0u8; 32]; - unsafe { - ptr::copy_nonoverlapping(wallet_ids, wallet_id.as_mut_ptr(), 32); - let manager_ref = &*manager; - manager_ref.runtime.block_on(async { - manager_ref.manager.write().await.update_wallet_synced_height(&wallet_id, 500); - }); - } - - let mut until_height = u32::MAX; - let started = unsafe { - wallet_manager::wallet_manager_begin_coinjoin_recovery( - manager, - wallet_ids, - 0, - &mut until_height, - error, - ) - }; - assert!(started); - assert_eq!(unsafe { (*error).code }, FFIErrorCode::Success); - assert_eq!(until_height, 500, "the recovery ends where the wallet had synced to"); - - // A second recovery is refused while the first runs; the out-pointer - // may be null. - let started = unsafe { - wallet_manager::wallet_manager_begin_coinjoin_recovery( - manager, - wallet_ids, - 0, - ptr::null_mut(), - error, - ) - }; - assert!(!started); - assert_eq!(unsafe { (*error).code }, FFIErrorCode::InvalidState); - - let mut until = 0u32; - let mut found = u32::MAX; - let mut probing = false; - let running = unsafe { - wallet_manager::wallet_manager_coinjoin_recovery_progress( - manager, - wallet_ids, - &mut until, - &mut found, - &mut probing, - error, - ) - }; - assert!(running); - assert_eq!((until, found, probing), (500, 0, true)); - - let cancelled = unsafe { - wallet_manager::wallet_manager_cancel_coinjoin_recovery(manager, wallet_ids, error) - }; - assert!(cancelled); - // The replay goes on without the probe. - let running = unsafe { - wallet_manager::wallet_manager_coinjoin_recovery_progress( - manager, - wallet_ids, - ptr::null_mut(), - ptr::null_mut(), - &mut probing, - error, - ) - }; - assert!(running); - assert!(!probing); - let cancelled = unsafe { - wallet_manager::wallet_manager_cancel_coinjoin_recovery(manager, wallet_ids, error) - }; - assert!(!cancelled, "nothing left to cancel"); - - unsafe { - (*error).clean(); - wallet_manager::wallet_manager_free_wallet_ids(wallet_ids, id_count); - wallet_manager::wallet_manager_free(manager); - } - } - #[test] fn test_wallet_manager_get_wallet_balance() { let mut error = FFIError::default(); diff --git a/key-wallet-manager/src/coinjoin_recovery_tests.rs b/key-wallet-manager/src/coinjoin_recovery_tests.rs deleted file mode 100644 index b777defe2..000000000 --- a/key-wallet-manager/src/coinjoin_recovery_tests.rs +++ /dev/null @@ -1,265 +0,0 @@ -//! Manager-level CoinJoin recovery (ticket 32008): one wallet is reset and -//! replayed while another, already synced, stays as it is. - -use super::test_helpers::*; -use super::*; -use crate::wallet_interface::WalletInterface; -use dashcore::block::{Block, Header, Version}; -use dashcore::ephemerealdata::chain_lock::ChainLock; -use dashcore::hashes::Hash; -use dashcore::{BlockHash, CompactTarget, OutPoint, TxIn, TxMerkleNode, TxOut, Txid}; -use key_wallet::bip32::ChildNumber; -use key_wallet::wallet::initialization::WalletAccountCreationOptions; - -/// 0.001 DASH CoinJoin denomination, fee included. -const DENOM: u64 = 100_001; -/// An index past the generated end of a fresh CoinJoin pool. -const FAR: u32 = key_wallet::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT + 250; -const OTHER_MNEMONIC: &str = - "legal winner thank year wave sausage worth useful legal winner thank yellow"; - -fn block(txdata: Vec, seed: u8) -> Block { - Block { - header: Header { - version: Version::default(), - prev_blockhash: BlockHash::from_byte_array([seed; 32]), - merkle_root: TxMerkleNode::all_zeros(), - time: 1_650_000_000 + seed as u32, - bits: CompactTarget::from_consensus(0x1d00ffff), - nonce: 0, - }, - txdata, - } -} - -/// The external CoinJoin address at `index` of `wallet_id`. -fn coinjoin_address(manager: &WalletManager, wallet_id: &WalletId, index: u32) -> Address { - let wallet = manager.get_wallet(wallet_id).expect("wallet"); - let xpub = wallet.accounts.coinjoin_accounts.get(&0).expect("CoinJoin account").account_xpub; - let key = xpub - .derive_pub(&[ - ChildNumber::from_normal_idx(0).unwrap(), - ChildNumber::from_normal_idx(index).unwrap(), - ]) - .expect("derive"); - Address::p2pkh(&dashcore::PublicKey::new(key.public_key), Network::Testnet) -} - -fn tx(input: Vec, output: Vec<(u64, &Address)>) -> Transaction { - Transaction { - version: 2, - lock_time: 0, - input: input - .into_iter() - .map(|previous_output| TxIn { - previous_output, - ..Default::default() - }) - .collect(), - output: output - .into_iter() - .map(|(value, address)| TxOut { - value, - script_pubkey: address.script_pubkey(), - }) - .collect(), - special_transaction_payload: None, - } -} - -fn foreign(n: u8) -> OutPoint { - OutPoint { - txid: Txid::from_byte_array([n; 32]), - vout: 0, - } -} - -#[tokio::test] -async fn recovery_replays_one_wallet_and_leaves_the_synced_one_alone() { - let (mut manager, synced, synced_address) = setup_manager_with_wallet(); - let recovering = manager - .create_wallet_from_mnemonic(OTHER_MNEMONIC, 0, WalletAccountCreationOptions::Default) - .expect("second wallet"); - let both = BTreeSet::from([synced, recovering]); - - // Block 10 pays both wallets; block 20 mixes the recovering wallet's coin - // onto a CoinJoin address past its gap limit, where a normal sync never - // looks. - let near = coinjoin_address(&manager, &recovering, 0); - let far = coinjoin_address(&manager, &recovering, FAR); - let funding = tx(vec![foreign(0x01)], vec![(TX_AMOUNT, &synced_address), (DENOM, &near)]); - let mix = tx( - vec![ - foreign(0x02), - OutPoint { - txid: funding.txid(), - vout: 1, - }, - ], - vec![(DENOM, &Address::dummy(Network::Testnet, 1)), (DENOM, &far)], - ); - let block10 = block(vec![funding.clone()], 10); - let block20 = block(vec![mix.clone()], 20); - manager.process_block_for_wallets(&block10, block10.block_hash(), 10, &both).await; - manager.process_block_for_wallets(&block20, block20.block_hash(), 20, &both).await; - manager.update_wallet_synced_height(&synced, 100); - manager.update_wallet_synced_height(&recovering, 100); - manager.apply_chain_lock(ChainLock::dummy(100)); - assert_eq!( - manager.get_wallet_balance(&recovering).unwrap().total(), - 0, - "the far coin is missing" - ); - - let synced_info = manager.get_wallet_info(&synced).expect("info"); - let synced_balance = synced_info.balance(); - let synced_history: Vec = - synced_info.transaction_history().iter().map(|record| record.txid).collect(); - assert_eq!(synced_balance.total(), TX_AMOUNT); - - let mut rx = manager.subscribe_events(); - let until = manager.begin_coinjoin_recovery(&recovering, 0).expect("recovery starts"); - assert_eq!(until, 100); - let events = drain_events(&mut rx); - assert_eq!(events.len(), 1, "one reset event, got {:?}", events); - let WalletEvent::WalletReset { - wallet_id, - synced_height: 0, - coinjoin_addresses, - .. - } = &events[0] - else { - panic!("expected WalletReset, got {:?}", events[0]); - }; - assert_eq!(*wallet_id, recovering); - assert!( - coinjoin_addresses.iter().any(|address| address.address == near), - "the rebuilt CoinJoin pools are announced" - ); - assert!(coinjoin_addresses.iter().all(|address| address.derivation_index < FAR)); - assert!(matches!( - manager.begin_coinjoin_recovery(&synced, 0), - Err(WalletError::CoinJoinRecoveryInProgress(running)) if running == recovering - )); - assert!( - matches!( - manager.begin_coinjoin_recovery(&[9u8; 32], 0), - Err(WalletError::WalletNotFound(_)) - ), - "an unknown wallet is reported as such while another recovers" - ); - assert_eq!(manager.wallets_behind(100), BTreeSet::from([recovering])); - - // The sync layer replays the recovering wallet alone. - let only = BTreeSet::from([recovering]); - manager.process_block_for_wallets(&block10, block10.block_hash(), 10, &only).await; - manager.process_block_for_wallets(&block20, block20.block_hash(), 20, &only).await; - assert_eq!( - manager.get_wallet_balance(&recovering).unwrap().total(), - DENOM, - "the replay credits the far coin" - ); - - let synced_info = manager.get_wallet_info(&synced).expect("info"); - assert_eq!(synced_info.balance(), synced_balance, "the synced wallet's balance is untouched"); - assert_eq!( - synced_info.transaction_history().iter().map(|record| record.txid).collect::>(), - synced_history, - "the synced wallet's records are untouched" - ); - assert_eq!(synced_info.synced_height(), 100); - - assert_eq!( - manager.coinjoin_recovery_progress(&recovering).map(|progress| progress.found), - Some(1) - ); - drain_events(&mut rx); - manager.update_wallet_synced_height(&recovering, 50); - assert!(drain_events(&mut rx) - .iter() - .all(|event| !matches!(event, WalletEvent::CoinJoinRecoveryFinished { .. }))); - manager.update_wallet_synced_height(&recovering, 100); - let events = drain_events(&mut rx); - assert!(matches!( - events.last(), - Some(WalletEvent::CoinJoinRecoveryFinished { wallet_id, height: 100, found: 1, cancelled: false }) - if *wallet_id == recovering - )); - assert!(manager.begin_coinjoin_recovery(&synced, 0).is_ok(), "the next recovery may start"); -} - -#[tokio::test] -async fn recovery_of_an_unknown_wallet_fails() { - let (mut manager, _, _) = setup_manager_with_wallet(); - assert!(matches!( - manager.begin_coinjoin_recovery(&[9u8; 32], 0), - Err(WalletError::WalletNotFound(_)) - )); - assert!(!manager.cancel_coinjoin_recovery(&[9u8; 32])); -} - -#[tokio::test] -async fn a_cancelled_recovery_reports_its_end_when_the_replay_completes() { - let (mut manager, wallet_id, _) = setup_manager_with_wallet(); - manager.update_wallet_last_processed_height(&wallet_id, 300); - manager.update_wallet_synced_height(&wallet_id, 300); - let mut rx = manager.subscribe_events(); - manager.begin_coinjoin_recovery(&wallet_id, 0).expect("recovery starts"); - assert!(manager.cancel_coinjoin_recovery(&wallet_id)); - assert!(!manager.cancel_coinjoin_recovery(&wallet_id), "the probe is already off"); - drain_events(&mut rx); - - // Builds come back, and the end is reported, once the replay is done. - manager.update_wallet_synced_height(&wallet_id, 300); - assert!(matches!( - drain_events(&mut rx).last(), - Some(WalletEvent::CoinJoinRecoveryFinished { - height: 300, - found: 0, - cancelled: true, - .. - }) - )); - assert!(manager.coinjoin_recovery_progress(&wallet_id).is_none()); -} - -/// A cancelled recovery's replay still holds the shared scan pipeline, so -/// another wallet waits; the cancelled wallet itself may start over. -#[tokio::test] -async fn a_cancelled_replay_still_holds_the_slot_for_other_wallets() { - let (mut manager, first, _) = setup_manager_with_wallet(); - let second = manager - .create_wallet_from_mnemonic(OTHER_MNEMONIC, 0, WalletAccountCreationOptions::Default) - .expect("second wallet"); - manager.update_wallet_synced_height(&first, 300); - manager.update_wallet_synced_height(&second, 300); - - manager.begin_coinjoin_recovery(&first, 0).expect("recovery starts"); - assert!(manager.cancel_coinjoin_recovery(&first)); - assert!(matches!( - manager.coinjoin_recovery_progress(&first), - Some(progress) if !progress.probing - )); - assert!(matches!( - manager.begin_coinjoin_recovery(&second, 0), - Err(WalletError::CoinJoinRecoveryInProgress(running)) if running == first - )); - assert_eq!(manager.begin_coinjoin_recovery(&first, 0).expect("starts over"), 300); - - manager.update_wallet_synced_height(&first, 300); - assert!(manager.coinjoin_recovery_progress(&first).is_none(), "the replay is done"); - assert!(manager.begin_coinjoin_recovery(&second, 0).is_ok()); -} - -/// A wallet born below where the sync layer's headers start could not get its -/// history there back, so it is refused before anything is dropped. -#[tokio::test] -async fn a_wallet_born_below_the_replayable_range_is_refused() { - let (mut manager, wallet_id, _) = setup_manager_with_wallet(); - manager.update_wallet_synced_height(&wallet_id, 300); - assert!(matches!( - manager.begin_coinjoin_recovery(&wallet_id, 200), - Err(WalletError::InvalidState(_)) - )); - assert_eq!(manager.get_wallet_info(&wallet_id).expect("info").synced_height(), 300); -} diff --git a/key-wallet-manager/src/error.rs b/key-wallet-manager/src/error.rs index fc59c3c70..f644fcf57 100644 --- a/key-wallet-manager/src/error.rs +++ b/key-wallet-manager/src/error.rs @@ -29,11 +29,6 @@ pub enum WalletError { TransactionBuild(String), /// Insufficient funds InsufficientFunds, - /// The wallet's state does not allow the operation - InvalidState(String), - /// A CoinJoin recovery is already running on a wallet of this manager; - /// recoveries run one at a time - CoinJoinRecoveryInProgress(WalletId), /// A truncation below a wallet's applied ChainLock, whose blocks are final TruncateBelowChainLock { /// The wallet that refused the truncation @@ -59,10 +54,6 @@ impl core::fmt::Display for WalletError { WalletError::InvalidParameter(msg) => write!(f, "Invalid parameter: {}", msg), WalletError::TransactionBuild(err) => write!(f, "Transaction build failed: {}", err), WalletError::InsufficientFunds => write!(f, "Insufficient funds"), - WalletError::InvalidState(msg) => write!(f, "Invalid state: {}", msg), - WalletError::CoinJoinRecoveryInProgress(id) => { - write!(f, "A CoinJoin recovery is already running on wallet {:x}", id.as_hex()) - } WalletError::TruncateBelowChainLock { wallet_id, height, diff --git a/key-wallet-manager/src/events.rs b/key-wallet-manager/src/events.rs index ba6980b0e..a1861f679 100644 --- a/key-wallet-manager/src/events.rs +++ b/key-wallet-manager/src/events.rs @@ -427,46 +427,6 @@ pub enum WalletEvent { /// the parent transaction). locked_transactions: BTreeMap>, }, - /// The wallet dropped everything it learned from the chain and rewound its - /// sync checkpoint to `synced_height`, just below its birth height, to be - /// replayed from birth by a CoinJoin recovery - /// ([`crate::WalletManager::begin_coinjoin_recovery`]). No other event - /// reports what was dropped, and other wallets are not affected. - /// - /// A consumer mirroring wallet state to disk must delete every - /// transaction and UTXO it holds for this wallet, replace its CoinJoin - /// address rows with `coinjoin_addresses`, and lower its synced height to - /// `synced_height`. The CoinJoin pools are rebuilt so the wallet stops - /// watching empty runs; every other address row stays as it is, as the - /// wallet keeps those addresses and their usage. Addresses the replay - /// derives anew arrive in the usual events. Annotations the consumer - /// keeps beside the rows (labels, notes) should survive keyed by txid or - /// address: the replay delivers the same transactions again, without them. - WalletReset { - /// ID of the affected wallet. - wallet_id: WalletId, - /// The rewound sync checkpoint: one below the wallet's birth height, or - /// lower when the wallet had not synced that far. - synced_height: CoreBlockHeight, - /// Wallet balance after the reset. - balance: WalletCoreBalance, - /// Every address the rebuilt CoinJoin pools hold. - coinjoin_addresses: Vec, - }, - /// A CoinJoin recovery ended: its replay reached the committed sync height - /// it was to run to, the wallet's coins are current again and builds from - /// it are allowed. `cancelled` says its probe had been stopped before - /// ([`crate::WalletManager::cancel_coinjoin_recovery`]). - CoinJoinRecoveryFinished { - /// ID of the affected wallet. - wallet_id: WalletId, - /// The committed sync height the replay reached. - height: CoreBlockHeight, - /// CoinJoin addresses past the gap limit the replay found coins on. - found: u32, - /// Whether the probe had been cancelled before the replay completed. - cancelled: bool, - }, } impl WalletEvent { @@ -500,14 +460,6 @@ impl WalletEvent { | WalletEvent::ChainLockProcessed { wallet_id, .. - } - | WalletEvent::WalletReset { - wallet_id, - .. - } - | WalletEvent::CoinJoinRecoveryFinished { - wallet_id, - .. } => *wallet_id, } } @@ -622,28 +574,6 @@ impl fmt::Display for WalletEvent { total_txids, ) } - WalletEvent::WalletReset { - synced_height, - balance, - coinjoin_addresses, - .. - } => write!( - f, - "WalletReset(synced_height={}, balance={}, coinjoin_addresses={})", - synced_height, - balance, - coinjoin_addresses.len() - ), - WalletEvent::CoinJoinRecoveryFinished { - height, - found, - cancelled, - .. - } => write!( - f, - "CoinJoinRecoveryFinished(height={}, found={}, cancelled={})", - height, found, cancelled - ), } } } diff --git a/key-wallet-manager/src/lib.rs b/key-wallet-manager/src/lib.rs index 45c966e97..798f1ce17 100644 --- a/key-wallet-manager/src/lib.rs +++ b/key-wallet-manager/src/lib.rs @@ -28,7 +28,6 @@ use dashcore::blockdata::transaction::{OutPoint, Transaction}; use dashcore::prelude::CoreBlockHeight; use dashcore::Txid; use key_wallet::account::AccountCollection; -use key_wallet::managed_account::managed_account_trait::ManagedAccountTrait; use key_wallet::managed_account::transaction_record::TransactionRecord; use key_wallet::transaction_checking::{DerivedAddressInfo, TransactionContext}; use key_wallet::wallet::managed_wallet_info::coin_selection::SelectionStrategy; @@ -221,144 +220,6 @@ impl WalletManager { } } - /// Start a CoinJoin recovery on one wallet: find mixed coins that landed - /// past the CoinJoin pools' gap limit, which a normal sync never discovers - /// (ticket 32008). - /// - /// The wallet's chain-derived state is dropped and its sync checkpoint is - /// rewound to just below its birth height, announced by one - /// [`WalletEvent::WalletReset`] for this wallet only. The sync layer then - /// sees the wallet behind and replays it from birth, from the filters and - /// blocks it already holds where it can, while the wallet looks for its - /// CoinJoin addresses past the gap in every transaction it finds relevant. - /// Once its committed sync height reaches the returned height, it ends - /// with a [`WalletEvent::CoinJoinRecoveryFinished`]. Nothing about it is - /// persisted: a restart ends it without that event, and - /// [`Self::coinjoin_recovery_progress`] then reports nothing either, so a - /// caller that must see it through keeps its own "pending" flag until the - /// event arrives and starts the recovery again after a restart. - /// - /// While it runs the wallet's coins are those of a past height, so building - /// a transaction funded from its accounts fails with - /// `BuilderError::RecoveryInProgress`. - /// - /// A block the sync layer already had in flight for this wallet when it - /// is reset can still be applied to the reset wallet ahead of the replay, - /// out of order. Start a recovery while the SPV client is stopped, or idle - /// at the tip, to rule that out. - /// - /// Wallets not being recovered keep their state, but the scan pipeline is - /// shared: the replay restarts in-flight scan work, and confirmations for - /// the other wallets can wait until it catches up. Recoveries therefore run - /// one at a time, ideally right after the first sync. - /// - /// `lowest_replayable_height` is the lowest height the sync layer can - /// replay, where its header storage starts (`0` for a sync from genesis); - /// a wallet born below it is refused, since the reset would drop history - /// the replay cannot bring back. - /// - /// Returns the committed sync height at which the recovery will end: the - /// highest of the wallet's own synced height and chainlocks seen, and of - /// any other wallet's synced height or applied chainlock. Fails when the wallet is unknown, a recovery is - /// already running on any wallet of this manager, the wallet has no - /// CoinJoin account, unconfirmed transactions or coins reserved by a - /// transaction build, was born below `lowest_replayable_height`, or - /// nothing is known of the chain past the wallet's birth yet. - pub fn begin_coinjoin_recovery( - &mut self, - wallet_id: &WalletId, - lowest_replayable_height: CoreBlockHeight, - ) -> Result { - let wallet = self.wallets.get(wallet_id).ok_or(WalletError::WalletNotFound(*wallet_id))?; - // The reset drops the history down to the wallet's birth, and the sync - // layer replays only from where its headers start: anything between - // would be lost for good. - let birth_height = self - .wallet_infos - .get(wallet_id) - .ok_or(WalletError::WalletNotFound(*wallet_id))? - .birth_height(); - if birth_height < lowest_replayable_height { - return Err(WalletError::InvalidState(format!( - "CoinJoin recovery: the wallet was born at {birth_height}, below {lowest_replayable_height}, \ - the lowest height the sync can replay; its history there would be lost" - ))); - } - // One replay at a time, a cancelled one included: it still holds the - // scan pipeline. Only a cancelled replay of this very wallet may be - // started over. - if let Some((running, _)) = self.wallet_infos.iter().find(|(id, info)| { - info.coinjoin_recovery_progress() - .is_some_and(|progress| *id != wallet_id || progress.probing) - }) { - return Err(WalletError::CoinJoinRecoveryInProgress(*running)); - } - // What the other wallets know of the chain: a wallet imported moments - // ago may know nothing itself yet. Its own heights are its own call. - let known_height = self - .wallet_infos - .iter() - .filter(|(id, _)| *id != wallet_id) - .map(|(_, info)| { - info.synced_height() - .max(info.last_applied_chain_lock().map_or(0, |cl| cl.block_height)) - }) - .max() - .unwrap_or(0); - let info = - self.wallet_infos.get_mut(wallet_id).ok_or(WalletError::WalletNotFound(*wallet_id))?; - let until_height = - info.begin_coinjoin_recovery(wallet, known_height).map_err(|err| match err { - key_wallet::Error::InvalidState(msg) => WalletError::InvalidState(msg), - err => err.into(), - })?; - let coinjoin_addresses = events::project_derived_addresses( - info.accounts().coinjoin_accounts.values().flat_map(|account| { - let account_type = account.managed_account_type().to_account_type(); - account.managed_account_type().address_pools().into_iter().flat_map(move |pool| { - pool.addresses.values().map(move |info| DerivedAddressInfo { - account_type, - pool_type: pool.pool_type, - info: info.clone(), - }) - }) - }), - ); - let event = WalletEvent::WalletReset { - wallet_id: *wallet_id, - synced_height: info.synced_height(), - balance: info.balance(), - coinjoin_addresses, - }; - self.emit_event(event); - Ok(until_height) - } - - /// Where the CoinJoin recovery running on `wallet_id` stands, including a - /// cancelled one whose replay goes on; `None` when none runs. The state is in memory only, so after a restart this is - /// `None` and an interrupted recovery has to be started again. - pub fn coinjoin_recovery_progress( - &self, - wallet_id: &WalletId, - ) -> Option { - self.wallet_infos.get(wallet_id)?.coinjoin_recovery_progress() - } - - /// Stop the probe of the CoinJoin recovery running on `wallet_id` before - /// its replay is complete. The wallet keeps what the probe has found and - /// the replay goes on as a normal sync; coins past the gap limit in the - /// rest of its history stay missing until a recovery runs again. Builds - /// stay refused until the replay is back at the height the recovery was - /// to run to, since until then the coins are those of a past height; the - /// [`WalletEvent::CoinJoinRecoveryFinished`] at that point, with - /// `cancelled` set, says when they are allowed again. Returns `false` when - /// no probe runs on that wallet. - pub fn cancel_coinjoin_recovery(&mut self, wallet_id: &WalletId) -> bool { - self.wallet_infos - .get_mut(wallet_id) - .is_some_and(|info| info.cancel_coinjoin_recovery().is_some()) - } - /// Emit a wallet event to BOTH the incidental broadcast fan-out and the /// lossless persistence channel (dashpay/platform#4069). /// @@ -934,8 +795,6 @@ fn current_timestamp() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs() } -#[cfg(test)] -mod coinjoin_recovery_tests; #[cfg(test)] mod event_tests; #[cfg(test)] diff --git a/key-wallet-manager/src/process_block.rs b/key-wallet-manager/src/process_block.rs index 05b5c2f94..f0b8e0ce0 100644 --- a/key-wallet-manager/src/process_block.rs +++ b/key-wallet-manager/src/process_block.rs @@ -384,19 +384,10 @@ impl WalletInterface for WalletM if let Some(info) = self.wallet_infos.get_mut(wallet_id) { if height > info.synced_height() { info.update_synced_height(height); - let recovery_found = info.take_finished_coinjoin_recovery(); self.emit_event(WalletEvent::SyncHeightAdvanced { wallet_id: *wallet_id, height, }); - if let Some(outcome) = recovery_found { - self.emit_event(WalletEvent::CoinJoinRecoveryFinished { - wallet_id: *wallet_id, - height, - found: outcome.found, - cancelled: !outcome.probing, - }); - } } } } diff --git a/key-wallet/src/managed_account/managed_account_collection.rs b/key-wallet/src/managed_account/managed_account_collection.rs index 28c5570f3..51742dd52 100644 --- a/key-wallet/src/managed_account/managed_account_collection.rs +++ b/key-wallet/src/managed_account/managed_account_collection.rs @@ -499,18 +499,13 @@ impl ManagedAccountCollection { fn create_managed_funds_account_from_account( account: &Account, ) -> Result { - Ok(ManagedCoreFundsAccount::new( - Self::fresh_managed_account_type(account)?, - account.network, - )) - } - - /// The address pools a funds account built from `account` starts with. - pub(crate) fn fresh_managed_account_type( - account: &Account, - ) -> Result { let key_source = KeySource::Public(account.account_xpub); - ManagedAccountType::from_account_type(account.account_type, account.network, &key_source) + let managed_type = ManagedAccountType::from_account_type( + account.account_type, + account.network, + &key_source, + )?; + Ok(ManagedCoreFundsAccount::new(managed_type, account.network)) } /// Create a keys-only managed account from an [`Account`]. 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 76c0ff361..fa67530de 100644 --- a/key-wallet/src/managed_account/managed_core_funds_account.rs +++ b/key-wallet/src/managed_account/managed_core_funds_account.rs @@ -73,10 +73,6 @@ pub struct ManagedCoreFundsAccount { /// re-establish which coins are spent. #[cfg_attr(feature = "serde", serde(skip))] reservations: ReservationSet, - /// Set while a CoinJoin recovery replays the wallet: the UTXOs are those - /// of a past height, so no transaction build may fund from them. - #[cfg_attr(feature = "serde", serde(skip))] - pub(crate) replaying: bool, } /// What [`ManagedCoreFundsAccount::apply_abandon`] removed from one account. @@ -115,29 +111,9 @@ impl ManagedCoreFundsAccount { spent_before_funded: BTreeMap::new(), unsettled_spends: BTreeMap::new(), reservations: ReservationSet::default(), - replaying: false, } } - /// Forget everything this account learned from the chain, for a replay - /// from wallet birth; see [`ManagedCoreKeysAccount::reset_chain_state`]. - /// Reservations are left alone: a caller resets only an account no build - /// holds coins of. - pub(crate) fn reset_chain_state(&mut self, fresh_type: Option) { - self.keys.reset_chain_state(fresh_type); - self.balance = WalletCoreBalance::default(); - self.utxos.clear(); - self.spent_outpoints.clear(); - self.spent_before_funded.clear(); - self.unsettled_spends.clear(); - } - - /// Whether a transaction build holds coins of this account at - /// `current_height`. - pub(crate) fn has_reservations(&self, current_height: u32) -> bool { - !self.reservations.reserved(current_height).is_empty() - } - /// Create a `ManagedCoreFundsAccount` from an [`Account`](super::super::Account). pub fn from_account(account: &super::super::Account) -> Self { Self::wrap(ManagedCoreKeysAccount::from_account(account)) @@ -164,7 +140,6 @@ impl ManagedCoreFundsAccount { spent_before_funded: BTreeMap::new(), unsettled_spends: BTreeMap::new(), reservations: ReservationSet::default(), - replaying: false, } } @@ -1479,7 +1454,6 @@ impl<'de> Deserialize<'de> for ManagedCoreFundsAccount { spent_before_funded: helper.spent_before_funded, unsettled_spends: helper.unsettled_spends, reservations: ReservationSet::default(), - replaying: false, }) } } diff --git a/key-wallet/src/managed_account/managed_core_keys_account.rs b/key-wallet/src/managed_account/managed_core_keys_account.rs index 9f4531f14..1d16995e4 100644 --- a/key-wallet/src/managed_account/managed_core_keys_account.rs +++ b/key-wallet/src/managed_account/managed_core_keys_account.rs @@ -195,35 +195,6 @@ impl ManagedCoreKeysAccount { promoted } - /// Forget everything this account learned from the chain, for a replay - /// from wallet birth: its transaction history, finalized txids and - /// per-address totals. `fresh_type` replaces the address pools with newly - /// created ones; without it the pools keep their addresses and which of - /// them were used or handed out. - pub(crate) fn reset_chain_state(&mut self, fresh_type: Option) { - match fresh_type { - Some(fresh_type) => self.managed_account_type = fresh_type, - None => { - // Which addresses were used or handed out stays as it was: the - // replay marks the same ones used again, and clearing it would - // hand an address paid years ago out as fresh meanwhile. Only - // the per-address totals, which the replay adds up again, go. - for pool in self.managed_account_type.address_pools_mut() { - for info in pool.addresses.values_mut() { - info.tx_count = 0; - info.total_received = 0; - info.total_sent = 0; - info.balance = 0; - } - } - } - } - self.transactions.clear(); - #[cfg(not(feature = "keep-finalized-transactions"))] - self.finalized_txids.clear(); - self.monitor_revision += 1; - } - /// Create a `ManagedCoreKeysAccount` from an [`Account`](super::super::Account). pub fn from_account(account: &super::super::Account) -> Self { let key_source = address_pool::KeySource::Public(account.account_xpub); diff --git a/key-wallet/src/transaction_checking/coinjoin_recovery.rs b/key-wallet/src/transaction_checking/coinjoin_recovery.rs deleted file mode 100644 index 88b83c3af..000000000 --- a/key-wallet/src/transaction_checking/coinjoin_recovery.rs +++ /dev/null @@ -1,1597 +0,0 @@ -//! Opt-in recovery of CoinJoin coins that landed past the address pool's gap -//! limit. -//! -//! CoinJoin discovery walks each CoinJoin chain forward and stops after -//! `gap_limit` unused addresses. A wallet migrated from DashSync breaks that -//! assumption: DashSync marked a CoinJoin address used the moment it handed it -//! to a mixing session, so every failed session left an address that is used -//! locally and empty on chain. Enough failed sessions in a row leave a run of -//! empty addresses wider than any fixed gap, and every mixed coin past that run -//! stays invisible no matter how often the wallet rescans (ticket 32008). -//! -//! Rescanning the wallet as it stands cannot fix that either: every old mix is -//! chainlocked by now, and a finalized transaction is never revisited, so a -//! coin found later in it is never credited. Recovery therefore starts from -//! fresh wallet state. [`WalletInfoInterface::begin_coinjoin_recovery`] forgets -//! everything the wallet learned from the chain, rewinds its sync checkpoint to -//! just below its birth height, and turns the probe on for the replay that -//! follows; the sync layer sees the wallet behind and replays it from birth. -//! -//! During the replay, every transaction the regular check finds relevant has -//! its unclaimed P2PKH outputs compared against each CoinJoin chain, derived up -//! to [`COINJOIN_RECOVERY_PROBE_WINDOW`] addresses past the pool's generated -//! end. Only a matched address is added to the pool; the regular check then -//! credits the coin, marks the address used and maintains the gap past it, so -//! the empty run below it is never watched. The window keeps covering that run -//! for later transactions, and it moves up with every address the pool gains. -//! -//! The probe switches itself off once the wallet's committed sync height -//! reaches the height it had when the recovery began. - -use std::collections::hash_map::Entry; -use std::collections::{HashMap, HashSet}; - -use dashcore::blockdata::transaction::Transaction; -use dashcore::hashes::Hash; -use dashcore::prelude::CoreBlockHeight; -use dashcore::{PubkeyHash, ScriptBuf}; - -use super::account_checker::DerivedAddressInfo; -use crate::account::AccountType; -use crate::bip32::{ChildNumber, ExtendedPubKey}; -use crate::managed_account::address_pool::AddressPoolType; -use crate::managed_account::managed_account_collection::ManagedAccountCollection; -use crate::managed_account::managed_account_ref::ManagedAccountRefMut; -use crate::managed_account::managed_account_trait::ManagedAccountTrait; -use crate::managed_account::managed_account_type::ManagedAccountType; -use crate::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; -use crate::wallet::{ManagedWalletInfo, Wallet}; -use crate::KeySource; - -/// How far past the generated end of each CoinJoin chain a coin is searched -/// for: the widest run of empty addresses one step of recovery can cross. Each -/// address found moves the pool end, so the window travels with the coins; -/// DashSync derived up to ~10,500 CoinJoin addresses on heavily mixed wallets. -/// Derivation only; nothing in the window is watched unless a coin is found in -/// it. -pub const COINJOIN_RECOVERY_PROBE_WINDOW: u32 = 10_000; - -/// A CoinJoin recovery replay running on one wallet. -pub struct CoinJoinRecovery { - /// The committed sync height the wallet had when the recovery began. The - /// replay is complete once the wallet is synced to it again. - until_height: CoreBlockHeight, - /// CoinJoin addresses found past the gap limit so far. - found: u32, - /// Whether the probe still runs; a cancel stops it while the replay goes - /// on, and builds stay refused until it is done. - probing: bool, - /// Probe scripts derived per CoinJoin chain (account index, pool type), so - /// the window is derived once per replay rather than once per transaction. - chains: HashMap<(u32, AddressPoolType), ProbeRange>, -} - -/// The indices of one chain derived for probing: every index from the first -/// one the pool lacked when this replay first probed it, up to `until`. Indices the -/// pool has gained since stay here: an output paying one of them is claimed by -/// the pool and so is never looked up. -struct ProbeRange { - /// The chain's branch key, derived once. - branch: ExtendedPubKey, - /// Every probed address is P2PKH, so its key hash identifies it. - hashes: HashMap, - until: u32, -} - -impl CoinJoinRecovery { - fn new(until_height: CoreBlockHeight) -> Self { - Self { - until_height, - found: 0, - probing: true, - chains: HashMap::new(), - } - } - - /// Where the replay stands. - pub fn progress(&self) -> CoinJoinRecoveryProgress { - CoinJoinRecoveryProgress { - until_height: self.until_height, - found: self.found, - probing: self.probing, - } - } -} - -/// Where a running CoinJoin recovery stands. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct CoinJoinRecoveryProgress { - /// The committed sync height at which the replay is complete. - pub until_height: CoreBlockHeight, - /// CoinJoin addresses past the gap limit found so far. - pub found: u32, - /// Whether the probe still runs: `false` once the recovery was cancelled - /// and only its replay goes on. - pub probing: bool, -} - -/// A clone keeps the recovery running but derives its window again: copying -/// tens of thousands of scripts with every wallet snapshot would cost more than -/// deriving them. -impl Clone for CoinJoinRecovery { - fn clone(&self) -> Self { - Self { - until_height: self.until_height, - found: self.found, - probing: self.probing, - chains: HashMap::new(), - } - } -} - -impl std::fmt::Debug for CoinJoinRecovery { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("CoinJoinRecovery") - .field("until_height", &self.until_height) - .field("found", &self.found) - .field("probing", &self.probing) - .field("chains", &self.chains.len()) - .finish() - } -} - -/// The branch index of a CoinJoin chain; `None` for pools a CoinJoin account -/// does not have. -fn branch_index(pool_type: AddressPoolType) -> Option { - match pool_type { - AddressPoolType::External => Some(0), - AddressPoolType::Internal => Some(1), - AddressPoolType::Absent | AddressPoolType::AbsentHardened => None, - } -} - -/// The key hash of `branch`'s child `index`, which the pool pays as P2PKH. -fn probe_hash(branch: &ExtendedPubKey, index: u32) -> crate::Result { - let child = ChildNumber::from_normal_idx(index).map_err(crate::Error::Bip32)?; - let key = branch.ckd_pub(child).map_err(crate::Error::Bip32)?; - Ok(dashcore::PublicKey::new(key.public_key).pubkey_hash()) -} - -/// The key hash a P2PKH script pays; `None` for any other script. -fn p2pkh_hash(script: &ScriptBuf) -> Option { - script.p2pkh_public_key_hash_bytes().and_then(|bytes| PubkeyHash::from_slice(bytes).ok()) -} - -/// Fresh address pools for the CoinJoin account `account`, carrying over from -/// its `old` pools what the replay cannot rediscover: -/// - a gap limit widened at runtime, with which the wallet found coins the -/// default gap would not; -/// - every address seen in use or handed out, with that state: its coin may lie -/// farther from the others than the probe window reaches, and one handed out -/// but not yet paid must not go to a second payer; -/// - the runs between those addresses no wider than the gap, which a normal -/// walk watches, and the gap past each used one. Only wider runs, the -/// walls, stay out of the pool. -fn fresh_coinjoin_pools( - old: &ManagedAccountType, - account: &crate::Account, -) -> Option { - let mut fresh = match ManagedAccountCollection::fresh_managed_account_type(account) { - Ok(fresh) => fresh, - Err(e) => { - tracing::warn!(error = %e, "CoinJoin recovery: could not rebuild the CoinJoin pools, keeping the old ones"); - return None; - } - }; - let key_source = KeySource::Public(account.account_xpub); - let old_pools = old.address_pools(); - for pool in fresh.address_pools_mut() { - let Some(old) = old_pools.iter().find(|old| old.pool_type == pool.pool_type) else { - continue; - }; - if old.gap_limit > pool.gap_limit { - if let Err(e) = pool.set_gap_limit(old.gap_limit, &key_source) { - tracing::warn!(error = %e, gap = old.gap_limit, "CoinJoin recovery: could not keep a widened gap limit"); - } - } - for (&index, info) in &old.addresses { - let used = old.used_indices.contains(&index); - if !used && !info.is_reserved() { - continue; - } - if let Err(e) = pool.generate_address_at_index(index, &key_source, true) { - tracing::warn!(error = %e, index, "CoinJoin recovery: could not keep an address"); - continue; - } - if used { - pool.mark_index_used(index); - } - if let Some(kept) = pool.addresses.get_mut(&index) { - if !used { - kept.state = info.state; - } - kept.label = info.label.clone(); - kept.metadata = info.metadata.clone(); - } - } - let kept: Vec = pool.addresses.keys().copied().collect(); - for pair in kept.windows(2) { - let (low, high) = (pair[0], pair[1]); - if high - low - 1 > pool.gap_limit { - continue; - } - for index in low + 1..high { - if let Err(e) = pool.generate_address_at_index(index, &key_source, true) { - tracing::warn!(error = %e, index, "CoinJoin recovery: could not fill a narrow run"); - } - } - } - // The usual lookahead past every kept used address, as the old pool - // had it, not only past the highest one. - let used: Vec = pool.used_indices.iter().copied().collect(); - for index in used { - for next in index.saturating_add(1)..=index.saturating_add(pool.gap_limit) { - if let Err(e) = pool.generate_address_at_index(next, &key_source, true) { - tracing::warn!(error = %e, index = next, "CoinJoin recovery: could not extend the gap past a kept address"); - break; - } - } - } - } - Some(fresh) -} - -impl ManagedWalletInfo { - /// Forget everything the wallet learned from the chain and rewind it to - /// just below its birth height, with the CoinJoin probe on for the replay. - /// - /// `known_height` is the highest height other sources (other wallets) know - /// the chain to have reached. The replay is complete once the wallet is - /// synced to it, or to its own synced or processed height, highest - /// chainlock seen or unfinished replay target when higher; that height is - /// returned. Fails when a recovery is already running on this wallet, - /// the wallet has no CoinJoin account, unconfirmed transactions or coins - /// reserved by a transaction build, nothing is known of the chain past its - /// birth, or `wallet` is not this wallet's. - pub(crate) fn begin_coinjoin_recovery( - &mut self, - wallet: &Wallet, - known_height: CoreBlockHeight, - ) -> crate::Result { - if wallet.wallet_id != self.wallet_id { - return Err(crate::Error::InvalidParameter( - "CoinJoin recovery: the wallet does not match this wallet info".to_string(), - )); - } - if self.coinjoin_recovery.as_ref().is_some_and(|recovery| recovery.probing) { - return Err(crate::Error::InvalidState( - "a CoinJoin recovery is already running on this wallet".to_string(), - )); - } - if self.accounts.coinjoin_accounts.is_empty() { - return Err(crate::Error::InvalidState( - "CoinJoin recovery: the wallet has no CoinJoin account".to_string(), - )); - } - // Records not yet in a block are never delivered again by the replay, - // which only brings back blocks; dropping them would hide a pending - // send and offer the coins it spends again. Coins a build has reserved - // are about to be spent the same way. - let current_height = self.metadata.last_processed_height; - let pending = self.accounts.all_accounts().iter().any(|account| { - account.transactions().values().any(|record| !record.is_confirmed()) - || account.as_funds().is_some_and(|funds| funds.has_reservations(current_height)) - }); - if pending { - return Err(crate::Error::InvalidState( - "CoinJoin recovery: the wallet has unconfirmed transactions or coins reserved \ - for one; start it once they are in a block, or abandon the ones that never \ - will be" - .to_string(), - )); - } - // A wallet still in its first sync has a synced height far below the - // chain; the chainlocks seen mark where the final history, the part a - // normal sync never revisits, ends. - let chain_locked = self - .metadata - .last_applied_chain_lock - .as_ref() - .map(|chain_lock| chain_lock.block_height); - // A replay a cancelled recovery left behind still has to get back to - // where it was going. - let until_height = known_height - .max(self.metadata.synced_height) - .max(self.metadata.last_processed_height) - .max(self.coinjoin_recovery.as_ref().map_or(0, |recovery| recovery.until_height)) - .max(chain_locked.unwrap_or(0)) - .max(self.noted_chain_lock_height.unwrap_or(0)); - let floor = self.metadata.birth_height.saturating_sub(1); - if until_height <= floor { - return Err(crate::Error::InvalidState( - "CoinJoin recovery: nothing is known of the chain past the wallet's birth yet; \ - start it once the wallet has synced" - .to_string(), - )); - } - self.reset_chain_state(wallet); - self.set_funds_replaying(true); - self.finished_coinjoin_recovery = None; - self.coinjoin_recovery = Some(CoinJoinRecovery::new(until_height)); - tracing::info!( - from_height = self.metadata.synced_height, - until_height, - "CoinJoin recovery: wallet reset, replaying from birth" - ); - Ok(until_height) - } - - /// Stop the probe of the running recovery before its replay is complete, - /// returning how many CoinJoin addresses it found past the gap limit; - /// `None` when no probe runs. The replay itself goes on as a normal sync, - /// and builds stay refused until it is back at the height the recovery was - /// to run to: until then the coins are those of a past height. - pub(crate) fn cancel_coinjoin_recovery(&mut self) -> Option { - let synced = self.metadata.synced_height; - let recovery = self.coinjoin_recovery.as_mut().filter(|recovery| recovery.probing)?; - recovery.probing = false; - recovery.chains.clear(); - tracing::info!( - height = synced, - until_height = recovery.until_height, - found = recovery.found, - "CoinJoin recovery: cancelled, the replay goes on without the probe" - ); - Some(recovery.found) - } - - /// Called whenever the committed sync height advances: once it reaches the - /// height the recovery was to run to, the recovery ends, builds are - /// allowed again, and its result waits for - /// [`Self::take_finished_coinjoin_recovery`]. - pub(crate) fn on_synced_height_advanced(&mut self) { - let synced = self.metadata.synced_height; - if !self.coinjoin_recovery.as_ref().is_some_and(|recovery| synced >= recovery.until_height) - { - return; - } - let Some(recovery) = self.coinjoin_recovery.take() else { - return; - }; - self.set_funds_replaying(false); - tracing::info!( - height = synced, - found = recovery.found, - "CoinJoin recovery: replay complete" - ); - self.finished_coinjoin_recovery = Some(recovery.progress()); - } - - /// Where a recovery whose replay completed since the last call ended up; - /// `probing` is `false` when it had been cancelled. - pub(crate) fn take_finished_coinjoin_recovery(&mut self) -> Option { - self.finished_coinjoin_recovery.take() - } - - /// Whether a CoinJoin recovery replay is still catching up, so the coins - /// are those of a past height. - pub(crate) fn is_replaying(&self) -> bool { - self.coinjoin_recovery.is_some() - } - - /// Mark every funds account as replayed, or no longer, so transaction - /// builds refuse to fund from it meanwhile. - pub(crate) fn set_funds_replaying(&mut self, replaying: bool) { - for account in self.accounts.all_accounts_mut() { - if let ManagedAccountRefMut::Funds(funds) = account { - funds.replaying = replaying; - } - } - } - - /// Drop all chain-derived state so the wallet can be replayed from birth. - /// - /// Each CoinJoin account the wallet holds the key of gets fresh address - /// pools (see [`fresh_coinjoin_pools`]), so a recovered wallet does not go - /// on watching empty runs. Every other pool may hold addresses a walk from - /// index 0 would never reach again (a DIP-15 contact pool, a migrated - /// wallet's history), so it keeps its addresses and their usage. Platform - /// accounts are not Core chain state and are left alone, as are the applied - /// ChainLock, which keeps the replayed history final as it arrives, and the - /// wallet's identity and metadata. - fn reset_chain_state(&mut self, wallet: &Wallet) { - for account in self.accounts.all_accounts_mut() { - match account { - ManagedAccountRefMut::Funds(funds) => { - let account_type = funds.managed_account_type().to_account_type(); - let fresh = match account_type { - AccountType::CoinJoin { - .. - } => wallet.accounts.account_of_type(account_type).and_then(|account| { - fresh_coinjoin_pools(funds.managed_account_type(), account) - }), - _ => None, - }; - funds.reset_chain_state(fresh); - } - ManagedAccountRefMut::Keys(keys) => keys.reset_chain_state(None), - } - } - self.instant_send_locks.clear(); - self.observed_spent_outpoints.clear(); - // Only coverage is rewound, as for a new account, with a new account - // generation so in-flight filter scans, which snapshotted the old - // state, cannot certify coverage for the reset wallet. - // `last_processed_height` is a chain fact: keeping it keeps replayed - // coins' maturity and confirmations measured against the real chain. - self.rewind_sync_checkpoint_for_new_account(); - self.update_balance(); - } - - /// While a recovery is running, add to the CoinJoin pools every address of - /// ours that `tx` pays past a pool's generated end. `tx` must be one the - /// regular check found relevant. - /// - /// Must run before the transaction updates the wallet, so that the caller's - /// check that follows credits the outputs found here. Returns the addresses - /// added, for the caller to hand to SPV as newly derived scripts. - pub(crate) fn recover_coinjoin_outputs( - &mut self, - tx: &Transaction, - wallet: &Wallet, - ) -> Vec { - let mut derived = Vec::new(); - let Some(recovery) = self.coinjoin_recovery.as_mut().filter(|recovery| recovery.probing) - else { - return derived; - }; - - let accounts = self.accounts.all_accounts(); - let unclaimed: HashSet = tx - .output - .iter() - .filter(|output| { - !accounts - .iter() - .any(|account| account.contains_script_pub_key(&output.script_pubkey)) - }) - .filter_map(|output| p2pkh_hash(&output.script_pubkey)) - .collect(); - drop(accounts); - if unclaimed.is_empty() { - return derived; - } - - for (&index, account) in self.accounts.coinjoin_accounts.iter_mut() { - let account_type = account.managed_account_type().to_account_type(); - let key_source = wallet.key_source_for_account(account_type); - let KeySource::Public(account_xpub) = key_source else { - continue; - }; - - let mut extended = false; - for pool in account.managed_account_type_mut().address_pools_mut() { - let Some(branch_index) = branch_index(pool.pool_type) else { - continue; - }; - let pool_end = pool.highest_generated.map(|h| h + 1).unwrap_or(0); - let range = match recovery.chains.entry((index, pool.pool_type)) { - Entry::Occupied(entry) => entry.into_mut(), - Entry::Vacant(entry) => { - let branch = match ChildNumber::from_normal_idx(branch_index) - .and_then(|child| account_xpub.ckd_pub(child)) - { - Ok(branch) => branch, - Err(e) => { - tracing::warn!(error = %e, "CoinJoin recovery: branch derivation failed"); - continue; - } - }; - // From the first index the pool lacks: the reset may - // have kept used addresses past an empty run. - let first_missing = (0..pool_end) - .find(|&i| pool.info_at_index(i).is_none()) - .unwrap_or(pool_end); - entry.insert(ProbeRange { - branch, - hashes: HashMap::new(), - until: first_missing, - }) - } - }; - // Each address found moves the pool end and with it the window, - // so a second coin of this transaction may only come into reach - // once the first is added: search again until nothing new turns - // up, since a finalized transaction is never looked at again. - let pool_type = pool.pool_type; - let mut new_infos = Vec::new(); - let mut failed = HashSet::new(); - loop { - let window_start = pool.highest_generated.map(|h| h + 1).unwrap_or(0); - let end = window_start.saturating_add(COINJOIN_RECOVERY_PROBE_WINDOW); - while range.until < end { - let probe = range.until; - match probe_hash(&range.branch, probe) { - Ok(hash) => { - range.hashes.insert(hash, probe); - range.until += 1; - } - Err(e) => { - tracing::warn!( - error = %e, - index = probe, - "CoinJoin recovery: derivation failed, searching below it only" - ); - break; - } - } - } - let mut hits: Vec = unclaimed - .iter() - .filter_map(|hash| range.hashes.get(hash).copied()) - .filter(|&index| { - pool.info_at_index(index).is_none() && !failed.contains(&index) - }) - .collect(); - if hits.is_empty() { - break; - } - hits.sort_unstable(); - for next in hits { - if let Err(e) = pool.generate_address_at_index(next, &key_source, true) { - tracing::error!( - error = %e, - index = next, - "CoinJoin recovery: failed to add an address to the pool, its coin stays missing" - ); - failed.insert(next); - continue; - } - match pool.info_at_index(next) { - Some(info) => new_infos.push(info.clone()), - // Never selected again, or the search would not end. - None => { - failed.insert(next); - } - } - } - } - if new_infos.is_empty() { - continue; - } - // The check that follows marks the found addresses used and - // extends the gap past them, as for any other output of ours. - tracing::info!( - txid = %tx.txid(), - pool_type = ?pool_type, - previous_end = pool_end.saturating_sub(1), - found = ?new_infos.iter().map(|info| info.index).collect::>(), - "CoinJoin recovery: found outputs past the gap limit" - ); - recovery.found += new_infos.len() as u32; - derived.extend(new_infos.into_iter().map(|info| DerivedAddressInfo { - account_type, - pool_type, - info, - })); - extended = true; - } - if extended { - account.bump_monitor_revision(); - } - } - - derived - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::account::{AccountType, StandardAccountType}; - use crate::managed_account::managed_account_type::ManagedAccountType; - use crate::transaction_checking::{BlockInfo, TransactionContext, WalletTransactionChecker}; - use crate::wallet::initialization::WalletAccountCreationOptions; - use crate::wallet::managed_wallet_info::managed_account_operations::ManagedAccountOperations; - use crate::Network; - use dashcore::ephemerealdata::chain_lock::ChainLock; - use dashcore::{Address, BlockHash, OutPoint, TxIn, TxOut, Txid}; - - const NETWORK: Network = Network::Testnet; - /// 0.001 DASH CoinJoin denomination, fee included. - const DENOM: u64 = 100_001; - /// An index past the generated end of a fresh CoinJoin pool. - const PAST_GAP: u32 = crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; - const GAP: u32 = crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; - - struct Fixture { - wallet: Wallet, - managed: ManagedWalletInfo, - key_source: KeySource, - } - - /// A wallet with one CoinJoin account and a recovery running. - fn fixture() -> Fixture { - let mut fx = fixture_without_recovery(NETWORK); - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start recovery"); - fx - } - - fn fixture_without_recovery(network: Network) -> Fixture { - let mut wallet = - Wallet::new_random(network, WalletAccountCreationOptions::None).expect("wallet"); - wallet - .add_account( - AccountType::CoinJoin { - index: 0, - }, - None, - ) - .expect("CoinJoin account"); - let managed = ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0); - let xpub = wallet.accounts.coinjoin_accounts.get(&0).expect("account").account_xpub; - Fixture { - wallet, - managed, - key_source: KeySource::Public(xpub), - } - } - - fn coinjoin_address(fx: &mut Fixture, internal: bool, index: u32) -> Address { - let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); - let ManagedAccountType::CoinJoin { - external_addresses, - internal_addresses, - .. - } = account.managed_account_type_mut() - else { - panic!("expected a CoinJoin account"); - }; - let pool = if internal { - internal_addresses - } else { - external_addresses - }; - pool.generate_address_at_index(index, &fx.key_source, false).expect("derive") - } - - fn pool( - fx: &Fixture, - pool_type: AddressPoolType, - ) -> &crate::managed_account::address_pool::AddressPool { - fx.managed - .first_coinjoin_managed_account() - .expect("account") - .managed_account_type() - .address_pools() - .into_iter() - .find(|pool| pool.pool_type == pool_type) - .expect("pool") - } - - fn pool_end(fx: &Fixture, pool_type: AddressPoolType) -> Option { - pool(fx, pool_type).highest_generated - } - - fn block(height: u32) -> TransactionContext { - TransactionContext::InChainLockedBlock(BlockInfo::new( - height, - BlockHash::from_slice(&[height as u8; 32]).expect("hash"), - 1_650_000_000 + height, - )) - } - - fn input(txid: Txid) -> TxIn { - TxIn { - previous_output: OutPoint { - txid, - vout: 0, - }, - ..Default::default() - } - } - - fn output(value: u64, address: &Address) -> TxOut { - TxOut { - value, - script_pubkey: address.script_pubkey(), - } - } - - fn tx(input: Vec, output: Vec) -> Transaction { - Transaction { - version: 2, - lock_time: 0, - input, - output, - special_transaction_payload: None, - } - } - - /// Funds our CoinJoin external index 0 with one denomination at block 1. - async fn fund(fx: &mut Fixture) -> Transaction { - let first = coinjoin_address(fx, false, 0); - let funding = Transaction::dummy(&first, 0..1, &[DENOM]); - let funded = - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - assert_eq!(funded.total_received, DENOM, "funding must be credited"); - funding - } - - /// Our funded coin plus two foreign ones in, three denominations out, ours - /// to `ours` as output 1. - fn mix_of(funding: &Transaction, ours: &Address) -> Transaction { - tx( - vec![ - input(Txid::from_byte_array([0xa1; 32])), - input(funding.txid()), - input(Txid::from_byte_array([0xa2; 32])), - ], - vec![ - output(DENOM, &Address::dummy(NETWORK, 1)), - output(DENOM, ours), - output(DENOM, &Address::dummy(NETWORK, 2)), - ], - ) - } - - async fn fund_and_mix( - fx: &mut Fixture, - ours: &Address, - ) -> crate::transaction_checking::TransactionCheckResult { - let funding = fund(fx).await; - let mix = mix_of(&funding, ours); - fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await - } - - /// 32008: the mixed coin comes back past the gap limit. Only its address - /// and the gap after it join the pool, not the empty run below it. - #[tokio::test] - async fn mixed_coin_returned_past_the_gap_limit_is_recovered() { - let mut fx = fixture(); - let far_index = PAST_GAP + 250; - let far = coinjoin_address(&mut fx, false, far_index); - let result = fund_and_mix(&mut fx, &far).await; - - assert_eq!(result.total_sent, DENOM, "our input is debited"); - assert_eq!(result.total_received, DENOM, "the far output must be credited"); - assert!( - result.new_addresses.iter().any(|d| d.info.index == far_index), - "the recovered address must be reported as newly derived" - ); - assert_eq!(fx.managed.balance.total(), DENOM, "nothing lost: balance equals the coin"); - let external = pool(&fx, AddressPoolType::External); - assert_eq!(external.highest_generated, Some(far_index + GAP), "the gap follows the coin"); - assert!( - (PAST_GAP + 1..far_index).all(|index| external.info_at_index(index).is_none()), - "the empty run below the recovered address is not watched" - ); - assert_eq!(fx.managed.coinjoin_recovery.as_ref().map(|r| r.progress().found), Some(1)); - } - - /// DashSync also mixed onto its internal CoinJoin chain. - #[tokio::test] - async fn mixed_coin_returned_past_the_gap_limit_on_the_internal_chain_is_recovered() { - let mut fx = fixture(); - let far = coinjoin_address(&mut fx, true, PAST_GAP + 150); - let result = fund_and_mix(&mut fx, &far).await; - - assert_eq!(result.total_received, DENOM, "the far internal output must be credited"); - assert_eq!(pool_end(&fx, AddressPoolType::Internal), Some(PAST_GAP + 150 + GAP)); - assert_eq!(fx.managed.balance.total(), DENOM); - } - - /// A wallet on mainnet recovers the same way (32008 is a mainnet wallet). - #[tokio::test] - async fn mixed_coin_returned_past_the_gap_limit_is_recovered_on_mainnet() { - let mut fx = fixture_without_recovery(Network::Mainnet); - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start recovery"); - let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); - let result = fund_and_mix(&mut fx, &far).await; - assert_eq!(result.total_received, DENOM, "the far output must be credited"); - assert_eq!(fx.managed.balance.total(), DENOM); - } - - /// Without a recovery running, nothing is probed: a normal wallet pays - /// nothing for this, and the far coin stays missing as before. - #[tokio::test] - async fn without_a_recovery_nothing_is_probed() { - let mut fx = fixture_without_recovery(NETWORK); - let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); - let result = fund_and_mix(&mut fx, &far).await; - assert_eq!(result.total_received, 0); - assert_eq!(pool_end(&fx, AddressPoolType::External), Some(GAP)); - } - - /// A coin that really left (all outputs foreign) is not credited, and the - /// pool is not extended by the probe. - #[tokio::test] - async fn coin_that_really_left_is_not_credited() { - let mut fx = fixture(); - let result = fund_and_mix(&mut fx, &Address::dummy(NETWORK, 3)).await; - - assert_eq!(result.total_received, 0); - assert_eq!( - pool_end(&fx, AddressPoolType::External), - Some(GAP), - "the probe found nothing, so the pool stays at the funding's gap" - ); - assert_eq!(fx.managed.balance.total(), 0); - } - - /// Past the probe window the output stays unknown: the window is a bound, - /// not a guarantee. - #[tokio::test] - async fn output_past_the_probe_window_is_not_found() { - let mut fx = fixture(); - let beyond = coinjoin_address(&mut fx, false, GAP + COINJOIN_RECOVERY_PROBE_WINDOW + 10); - let result = fund_and_mix(&mut fx, &beyond).await; - assert_eq!(result.total_received, 0); - } - - /// A transaction the wallet does not find relevant is never probed, even - /// when it pays a CoinJoin address of ours past the gap: the recovery - /// follows the wallet's own coins, not every block it downloads. - #[tokio::test] - async fn an_irrelevant_transaction_is_not_probed() { - let mut fx = fixture(); - let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); - let foreign = tx( - vec![input(Txid::from_byte_array([0xe1; 32]))], - vec![output(DENOM, &far), output(DENOM, &Address::dummy(NETWORK, 7))], - ); - let result = - fx.managed.check_core_transaction(&foreign, block(2), &mut fx.wallet, true, true).await; - assert!(!result.is_relevant); - assert_eq!(pool_end(&fx, AddressPoolType::External), Some(GAP - 1)); - } - - /// A mix can return more of our coins than the inputs the wallet knows - /// about (one of ours came from an address it has not discovered yet). - /// Every one of them in the window must be found. - #[tokio::test] - async fn every_returned_coin_is_found_not_just_the_known_inputs() { - let mut fx = fixture(); - let far_a = coinjoin_address(&mut fx, false, PAST_GAP + 200); - let far_b = coinjoin_address(&mut fx, false, PAST_GAP + 800); - let funding = fund(&mut fx).await; - let mix = tx( - vec![ - input(funding.txid()), - // Ours as well, but on an address the wallet has not discovered. - input(Txid::from_byte_array([0xb1; 32])), - input(Txid::from_byte_array([0xb2; 32])), - ], - vec![ - output(DENOM, &far_a), - output(DENOM, &Address::dummy(NETWORK, 4)), - output(DENOM, &far_b), - ], - ); - let result = - fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; - - assert_eq!(result.total_received, 2 * DENOM, "both returned coins must be credited"); - assert_eq!(fx.managed.balance.total(), 2 * DENOM); - } - - /// A mix spends a known coin of ours and one the wallet has not found yet; - /// one coin comes back inside the pool, the other past the wall. - #[tokio::test] - async fn a_return_for_an_undiscovered_input_does_not_hide_a_far_coin() { - let mut fx = fixture(); - let near = coinjoin_address(&mut fx, false, 5); - let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); - let funding = fund(&mut fx).await; - let mix = tx( - vec![ - input(funding.txid()), - input(Txid::from_byte_array([0xc1; 32])), - input(Txid::from_byte_array([0xc2; 32])), - ], - vec![ - output(DENOM, &near), - output(DENOM, &Address::dummy(NETWORK, 5)), - output(DENOM, &far), - ], - ); - let result = - fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; - assert_eq!(result.total_received, 2 * DENOM, "both returned coins must be credited"); - assert_eq!(fx.managed.balance.total(), 2 * DENOM); - } - - /// The mix arrives before the coin it spends (rescan delivery order): no - /// input is known yet, but an output to our pool makes it relevant. - #[tokio::test] - async fn a_mix_seen_before_its_funding_still_recovers_the_far_coin() { - let mut fx = fixture(); - let near = coinjoin_address(&mut fx, false, 5); - let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); - let mix = tx( - (0xd1..=0xd3).map(|n| input(Txid::from_byte_array([n; 32]))).collect(), - vec![ - output(DENOM, &near), - output(DENOM, &Address::dummy(NETWORK, 6)), - output(DENOM, &far), - ], - ); - let result = - fx.managed.check_core_transaction(&mix, block(3), &mut fx.wallet, true, true).await; - assert_eq!(result.total_received, 2 * DENOM, "both of our outputs must be credited"); - } - - /// DashSync created denominations from the main account onto CoinJoin - /// addresses past the wall; no CoinJoin coin is spent, yet they are ours. - #[tokio::test] - async fn denominations_created_past_the_gap_from_the_main_account_are_recovered() { - let mut fx = fixture_without_recovery(NETWORK); - fx.wallet - .add_account( - AccountType::Standard { - index: 0, - standard_account_type: StandardAccountType::BIP44Account, - }, - None, - ) - .expect("BIP44 account"); - fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 0); - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start recovery"); - let main_xpub = - fx.wallet.accounts.standard_bip44_accounts.get(&0).expect("BIP44 account").account_xpub; - let main = { - let account = fx.managed.first_bip44_managed_account_mut().expect("account"); - let pool = account - .managed_account_type_mut() - .address_pools_mut() - .into_iter() - .find(|pool| pool.pool_type == AddressPoolType::External) - .expect("external pool"); - pool.generate_address_at_index(0, &KeySource::Public(main_xpub), false).expect("derive") - }; - let funding = Transaction::dummy(&main, 0..1, &[5 * DENOM]); - let funded = - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - assert_eq!(funded.total_received, 5 * DENOM, "funding must be credited"); - - let far_a = coinjoin_address(&mut fx, false, PAST_GAP + 400); - let far_b = coinjoin_address(&mut fx, false, PAST_GAP + 401); - let create = - tx(vec![input(funding.txid())], vec![output(DENOM, &far_a), output(DENOM, &far_b)]); - let result = - fx.managed.check_core_transaction(&create, block(2), &mut fx.wallet, true, true).await; - assert_eq!(result.total_sent, 5 * DENOM); - assert_eq!(result.total_received, 2 * DENOM, "both denominations must be credited"); - let coinjoin = fx.managed.first_coinjoin_managed_account().expect("account"); - assert_eq!(coinjoin.utxos.len(), 2); - } - - /// Whatever the amount: DashSync's denomination-creating transaction sent - /// its odd change to the CoinJoin internal chain past the wall. - #[tokio::test] - async fn odd_change_past_the_gap_beside_a_far_denomination_is_recovered() { - const CHANGE: u64 = 1_234_567; - let mut fx = fixture(); - let first = coinjoin_address(&mut fx, false, 0); - let funding = Transaction::dummy(&first, 0..1, &[DENOM + CHANGE]); - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - let far = coinjoin_address(&mut fx, false, PAST_GAP + 300); - let change = coinjoin_address(&mut fx, true, PAST_GAP + 600); - let create = - tx(vec![input(funding.txid())], vec![output(DENOM, &far), output(CHANGE, &change)]); - let result = - fx.managed.check_core_transaction(&create, block(2), &mut fx.wallet, true, true).await; - assert_eq!(result.total_received, DENOM + CHANGE, "the change must be credited too"); - } - - /// The recovered coin is mixed again and comes back past the new pool end: - /// the window moves up with the pool. - #[tokio::test] - async fn a_recovered_coin_mixed_again_past_the_new_end_is_recovered() { - let mut fx = fixture(); - let first_far = coinjoin_address(&mut fx, false, PAST_GAP + 200); - let funding = fund(&mut fx).await; - let mix = mix_of(&funding, &first_far); - let result = - fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; - assert_eq!(result.total_received, DENOM, "the first far output must be credited"); - - let new_end = pool_end(&fx, AddressPoolType::External).expect("pool end"); - let second_far = coinjoin_address(&mut fx, false, new_end + COINJOIN_RECOVERY_PROBE_WINDOW); - let remix = mix_of(&mix, &second_far); - let remix = tx( - vec![ - input(Txid::from_byte_array([0xf1; 32])), - TxIn { - previous_output: OutPoint { - txid: mix.txid(), - vout: 1, - }, - ..Default::default() - }, - ], - remix.output, - ); - let result = - fx.managed.check_core_transaction(&remix, block(3), &mut fx.wallet, true, true).await; - assert_eq!(result.total_sent, DENOM, "the recovered coin is debited"); - assert_eq!(result.total_received, DENOM, "the second far output must be credited"); - assert_eq!(fx.managed.balance.total(), DENOM, "exactly one coin, at the second address"); - } - - /// A coin paid later into the empty run below an earlier recovered address - /// is still found: the window keeps covering the run the pool skipped. - #[tokio::test] - async fn a_later_coin_in_the_skipped_run_is_found() { - let mut fx = fixture(); - let far = coinjoin_address(&mut fx, false, PAST_GAP + 900); - let funding = fund(&mut fx).await; - let mix = mix_of(&funding, &far); - fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; - - let skipped = coinjoin_address(&mut fx, false, PAST_GAP + 300); - let remix = tx( - vec![ - input(Txid::from_byte_array([0xf2; 32])), - TxIn { - previous_output: OutPoint { - txid: mix.txid(), - vout: 1, - }, - ..Default::default() - }, - ], - vec![output(DENOM, &skipped), output(DENOM, &Address::dummy(NETWORK, 8))], - ); - let result = - fx.managed.check_core_transaction(&remix, block(3), &mut fx.wallet, true, true).await; - assert_eq!(result.total_received, DENOM, "the coin in the skipped run must be credited"); - assert_eq!(fx.managed.balance.total(), DENOM); - } - - /// The case behind the reset: the wallet synced past a mix whose coin it - /// could not see, and the chainlocked mix is never revisited. After the - /// reset, replaying the same blocks credits the coin. - #[tokio::test] - async fn the_replay_credits_a_coin_of_a_finalized_mix() { - let mut fx = fixture_without_recovery(NETWORK); - let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); - let funding = fund(&mut fx).await; - let mix = mix_of(&funding, &far); - let first = - fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; - assert_eq!(first.total_received, 0, "without a recovery the coin is missing"); - assert_eq!(fx.managed.balance.total(), 0); - - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start recovery"); - assert_eq!(fx.managed.balance.total(), 0); - let funded = - fx.managed.check_core_transaction(&funding, block(1), &mut fx.wallet, true, true).await; - assert_eq!(funded.total_received, DENOM, "the replay records the funding again"); - let replayed = - fx.managed.check_core_transaction(&mix, block(2), &mut fx.wallet, true, true).await; - assert_eq!(replayed.total_received, DENOM, "the replay credits the far coin"); - assert_eq!(fx.managed.balance.total(), DENOM); - } - - /// Starting a recovery drops the chain state, rewinds the checkpoint to - /// just below birth and bumps the account generation, but keeps the - /// processed height and the applied ChainLock; it ends once the replay is - /// back at its start height. - #[tokio::test] - async fn the_reset_rewinds_and_the_recovery_ends_at_its_start_height() { - let mut fx = fixture_without_recovery(NETWORK); - fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 1); - fund(&mut fx).await; - fx.managed.update_synced_height(500); - fx.managed.update_last_processed_height(500); - fx.managed.apply_chain_lock(ChainLock::dummy(400)); - let generation = fx.managed.account_generation(); - assert_eq!(fx.managed.balance.total(), DENOM); - - let until = fx.managed.begin_coinjoin_recovery(&fx.wallet, 0).expect("start recovery"); - assert_eq!(until, 500); - assert_eq!(fx.managed.balance.total(), 0); - assert!(fx.managed.transaction_history().is_empty()); - assert!(fx.managed.first_coinjoin_managed_account().expect("account").utxos.is_empty()); - assert_eq!(fx.managed.synced_height(), 0); - assert_eq!(fx.managed.last_processed_height(), 500, "a chain fact, kept"); - assert_eq!(fx.managed.account_generation(), generation + 1); - assert_eq!(fx.managed.last_applied_chain_lock().map(|cl| cl.block_height), Some(400)); - assert!( - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).is_err(), - "one recovery at a time per wallet" - ); - - fx.managed.update_synced_height(499); - assert_eq!( - { - fx.managed.on_synced_height_advanced(); - fx.managed.take_finished_coinjoin_recovery() - }, - None - ); - assert!(fx.managed.coinjoin_recovery.is_some()); - fx.managed.update_synced_height(500); - assert_eq!( - { - fx.managed.on_synced_height_advanced(); - fx.managed.take_finished_coinjoin_recovery().map(|p| p.found) - }, - Some(0) - ); - assert!(fx.managed.coinjoin_recovery.is_none(), "the probe is off again"); - } - - /// An account added from an extended public key alone cannot derive its - /// addresses again, so the reset keeps them, with their usage. - #[tokio::test] - async fn the_reset_keeps_an_account_the_wallet_cannot_derive() { - let mut fx = fixture_without_recovery(NETWORK); - let other = - Wallet::new_random(NETWORK, WalletAccountCreationOptions::Default).expect("wallet"); - let xpub = - other.accounts.standard_bip44_accounts.get(&0).expect("BIP44 account").account_xpub; - let account_type = AccountType::Standard { - index: 7, - standard_account_type: StandardAccountType::BIP44Account, - }; - fx.managed.add_managed_account_from_xpub(account_type, xpub).expect("watch-only account"); - let addresses = |managed: &ManagedWalletInfo| { - managed - .accounts - .standard_bip44_accounts - .get(&7) - .expect("account") - .managed_account_type() - .all_addresses() - }; - let before = addresses(&fx.managed); - assert!(!before.is_empty()); - - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start recovery"); - assert_eq!(addresses(&fx.managed), before); - } - - /// A wallet still in its first sync replays up to the chainlock it has - /// seen, not just to where its own sync had got: the final history is - /// what a normal sync would never revisit. - #[tokio::test] - async fn a_recovery_started_mid_sync_runs_to_the_chainlock() { - let mut fx = fixture_without_recovery(NETWORK); - fx.managed.update_synced_height(300); - fx.managed.apply_chain_lock(ChainLock::dummy(900)); - assert_eq!(fx.managed.begin_coinjoin_recovery(&fx.wallet, 0).expect("start"), 900); - fx.managed.update_synced_height(300); - assert_eq!( - { - fx.managed.on_synced_height_advanced(); - fx.managed.take_finished_coinjoin_recovery() - }, - None - ); - } - - /// The replay only brings back blocks, so a pending transaction would be - /// lost: the recovery waits until the wallet has none. - #[tokio::test] - async fn a_wallet_with_an_unconfirmed_transaction_cannot_start_a_recovery() { - let mut fx = fixture_without_recovery(NETWORK); - let first = coinjoin_address(&mut fx, false, 0); - let pending = Transaction::dummy(&first, 0..1, &[DENOM]); - fx.managed - .check_core_transaction( - &pending, - TransactionContext::Mempool, - &mut fx.wallet, - true, - true, - ) - .await; - assert!(matches!( - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000), - Err(crate::Error::InvalidState(_)) - )); - assert!(fx.managed.coinjoin_recovery.is_none()); - assert_eq!(fx.managed.transaction_history().len(), 1, "nothing was reset"); - } - - /// Two coins of one transaction: the second lies past the window the pool - /// end opens, but inside the window the first coin's address opens. The - /// probe searches again after each find, since the transaction is final. - #[tokio::test] - async fn a_second_coin_reachable_only_from_the_first_is_found() { - let mut fx = fixture(); - let start = GAP + 1; - let near_far = coinjoin_address(&mut fx, false, start + 9_000); - let farther = coinjoin_address(&mut fx, false, start + 9_000 + 5_000); - assert!(start + 9_000 + 5_000 >= start + COINJOIN_RECOVERY_PROBE_WINDOW); - let funding = fund(&mut fx).await; - let create = tx( - vec![input(funding.txid())], - vec![output(DENOM, &near_far), output(DENOM, &farther)], - ); - let result = - fx.managed.check_core_transaction(&create, block(2), &mut fx.wallet, true, true).await; - assert_eq!(result.total_received, 2 * DENOM, "both coins must be credited"); - } - - /// A wallet nobody has synced past its birth has nothing to recover yet; - /// starting would end the replay at once. - #[tokio::test] - async fn a_recovery_with_nothing_known_of_the_chain_is_refused() { - let mut fx = fixture_without_recovery(NETWORK); - fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 100); - assert!(matches!( - fx.managed.begin_coinjoin_recovery(&fx.wallet, 0), - Err(crate::Error::InvalidState(_)) - )); - assert_eq!(fx.managed.begin_coinjoin_recovery(&fx.wallet, 5_000).expect("start"), 5_000); - assert_eq!(fx.managed.synced_height(), 99); - } - - /// Coins a transaction build has reserved are about to be spent by a - /// transaction the replay would never deliver. - #[tokio::test] - async fn coins_reserved_by_a_build_block_the_recovery() { - let mut fx = fixture_without_recovery(NETWORK); - let funding = fund(&mut fx).await; - let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); - account.reservations().reserve( - &[OutPoint { - txid: funding.txid(), - vout: 0, - }], - 0, - crate::managed_account::reservation::ReservationToken::next(), - ); - assert!(matches!( - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000), - Err(crate::Error::InvalidState(_)) - )); - assert_eq!(fx.managed.balance.total(), DENOM, "nothing was reset"); - } - - /// A wallet whose checkpoint sits below its birth stays there: the reset - /// only ever rewinds. - #[tokio::test] - async fn the_reset_never_moves_the_checkpoint_forward() { - let mut fx = fixture_without_recovery(NETWORK); - fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 100); - fx.managed.metadata.synced_height = 0; - fx.managed.begin_coinjoin_recovery(&fx.wallet, 5_000).expect("start"); - assert_eq!(fx.managed.synced_height(), 0); - } - - /// A cancelled recovery leaves the probe off and lets a new one start. - #[tokio::test] - async fn a_cancelled_recovery_turns_the_probe_off() { - let mut fx = fixture(); - assert_eq!(fx.managed.cancel_coinjoin_recovery(), Some(0)); - assert_eq!(fx.managed.cancel_coinjoin_recovery(), None); - let far = coinjoin_address(&mut fx, false, PAST_GAP + 250); - assert_eq!(fund_and_mix(&mut fx, &far).await.total_received, 0); - assert!(fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).is_ok()); - } - - /// A gap limit the wallet widened at runtime survives the reset, so the - /// replay finds what the normal sync found with it. - #[tokio::test] - async fn the_reset_keeps_a_widened_gap_limit() { - let mut fx = fixture_without_recovery(NETWORK); - let wide = GAP + 50; - { - let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); - for pool in account.managed_account_type_mut().address_pools_mut() { - pool.set_gap_limit(wide, &fx.key_source).expect("widen"); - } - } - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); - let external = pool(&fx, AddressPoolType::External); - assert_eq!(external.gap_limit, wide); - assert_eq!(external.highest_generated, Some(wide - 1)); - } - - /// A pool outside CoinJoin may hold addresses a walk from index 0 never - /// reaches again; the reset keeps them and their usage. - #[tokio::test] - async fn the_reset_keeps_addresses_outside_coinjoin_past_their_gap() { - let mut fx = fixture_without_recovery(NETWORK); - fx.wallet - .add_account( - AccountType::Standard { - index: 0, - standard_account_type: StandardAccountType::BIP44Account, - }, - None, - ) - .expect("BIP44 account"); - fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 0); - let main_xpub = - fx.wallet.accounts.standard_bip44_accounts.get(&0).expect("BIP44 account").account_xpub; - let far = { - let account = fx.managed.first_bip44_managed_account_mut().expect("account"); - let pool = account - .managed_account_type_mut() - .address_pools_mut() - .into_iter() - .find(|pool| pool.pool_type == AddressPoolType::External) - .expect("external pool"); - let far = pool - .generate_address_at_index(500, &KeySource::Public(main_xpub), true) - .expect("derive"); - pool.mark_used(&far); - far - }; - - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); - let account = fx.managed.first_bip44_managed_account_mut().expect("account"); - let pool = account - .managed_account_type() - .address_pools() - .into_iter() - .find(|pool| pool.pool_type == AddressPoolType::External) - .expect("external pool"); - assert_eq!(pool.address_index(&far), Some(500), "the far address is still watched"); - assert!( - pool.used_indices.contains(&500), - "its usage stays, so it is not handed out as fresh meanwhile" - ); - } - - /// While the replay runs the coins are those of a past height, so the - /// wallet refuses to build from them. - #[tokio::test] - async fn a_build_is_refused_while_the_replay_runs() { - use crate::wallet::managed_wallet_info::coin_selection::SelectionStrategy; - use crate::wallet::managed_wallet_info::fee::FeeRate; - use crate::wallet::managed_wallet_info::transaction_builder::BuilderError; - - let mut fx = fixture(); - let to = Address::dummy(NETWORK, 9).to_string().parse().expect("address"); - let result = fx.managed.build_unsigned_transaction( - &fx.wallet, - &[], - 0, - vec![(to, 1_000)], - FeeRate::new(1000), - SelectionStrategy::LargestFirst, - ); - assert!(matches!(result, Err(BuilderError::RecoveryInProgress))); - } - - /// An address handed out but not yet paid stays handed out across the - /// reset, so it is not given to a second payer. - #[tokio::test] - async fn the_reset_keeps_reserved_addresses_reserved() { - let mut fx = fixture_without_recovery(NETWORK); - fx.wallet - .add_account( - AccountType::Standard { - index: 0, - standard_account_type: StandardAccountType::BIP44Account, - }, - None, - ) - .expect("BIP44 account"); - fx.managed = ManagedWalletInfo::from_wallet_with_name(&fx.wallet, "Test".to_string(), 0); - let key_source = KeySource::Public( - fx.wallet.accounts.standard_bip44_accounts.get(&0).expect("account").account_xpub, - ); - let external = |managed: &mut ManagedWalletInfo| { - managed - .first_bip44_managed_account_mut() - .expect("account") - .managed_account_type_mut() - .address_pools_mut() - .into_iter() - .find(|pool| pool.pool_type == AddressPoolType::External) - .expect("external pool") - .clone() - }; - let handed_out = { - let account = fx.managed.first_bip44_managed_account_mut().expect("account"); - let pool = account - .managed_account_type_mut() - .address_pools_mut() - .into_iter() - .find(|pool| pool.pool_type == AddressPoolType::External) - .expect("external pool"); - pool.next_unused_and_reserve(&key_source, 1).expect("reserve") - }; - - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); - let pool = external(&mut fx.managed); - assert!(pool.address_info(&handed_out).expect("kept").is_reserved()); - } - - /// A CoinJoin address the wallet had seen in use is kept by the reset even - /// past an empty run wider than the window, and a coin later found in that - /// run below it is still found. - #[tokio::test] - async fn the_reset_keeps_used_coinjoin_addresses_and_probes_below_them() { - let mut fx = fixture_without_recovery(NETWORK); - let kept_index = GAP + COINJOIN_RECOVERY_PROBE_WINDOW + 500; - { - let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); - let pool = account - .managed_account_type_mut() - .address_pools_mut() - .into_iter() - .find(|pool| pool.pool_type == AddressPoolType::External) - .expect("external pool"); - pool.generate_address_at_index(kept_index, &fx.key_source, true).expect("derive"); - pool.mark_index_used(kept_index); - } - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); - let external = pool(&fx, AddressPoolType::External); - assert!(external.info_at_index(kept_index).is_some(), "the used address is still watched"); - assert!(external.used_indices.contains(&kept_index), "and stays marked used"); - assert!(external.info_at_index(GAP + 300).is_none(), "the empty run is not"); - - let below = coinjoin_address(&mut fx, false, GAP + 300); - let result = fund_and_mix(&mut fx, &below).await; - assert_eq!(result.total_received, DENOM, "a coin in the run below it is found"); - } - - /// A wallet without a CoinJoin account has nothing for a recovery to find. - #[tokio::test] - async fn a_wallet_without_coinjoin_cannot_start_a_recovery() { - let wallet = - Wallet::new_random(NETWORK, WalletAccountCreationOptions::None).expect("wallet"); - let mut managed = ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0); - assert!(matches!( - managed.begin_coinjoin_recovery(&wallet, 1_000), - Err(crate::Error::InvalidState(_)) - )); - } - - /// A CoinJoin address handed out but not yet paid stays handed out in the - /// rebuilt pool. - #[tokio::test] - async fn the_reset_keeps_reserved_coinjoin_addresses_reserved() { - let mut fx = fixture_without_recovery(NETWORK); - let handed_out = { - let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); - let pool = account - .managed_account_type_mut() - .address_pools_mut() - .into_iter() - .find(|pool| pool.pool_type == AddressPoolType::External) - .expect("external pool"); - pool.next_unused_and_reserve(&fx.key_source, 1).expect("reserve") - }; - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); - let external = pool(&fx, AddressPoolType::External); - assert!(external.address_info(&handed_out).expect("kept").is_reserved()); - } - - /// A build assembled straight from the builder refuses too: the replayed - /// accounts offer no coins. - #[tokio::test] - async fn a_direct_builder_refuses_to_fund_from_a_replayed_account() { - use crate::wallet::managed_wallet_info::transaction_builder::{ - BuilderError, TransactionBuilder, - }; - - let mut fx = fixture(); - let account = fx.wallet.accounts.coinjoin_accounts.get(&0).expect("account").clone(); - let funds = fx.managed.first_coinjoin_managed_account_mut().expect("account"); - let result = TransactionBuilder::new() - .add_output(&Address::dummy(NETWORK, 9), 1_000) - .add_funding(funds, &account) - .build_unsigned_reserved(); - assert!(matches!(result, Err(BuilderError::RecoveryInProgress))); - } - - /// Kept addresses close to each other keep the run between them watched, - /// and each used one the gap past it, as a normal gap walk would; the rest - /// of a wider run stays out. - #[tokio::test] - async fn the_reset_fills_narrow_runs_between_kept_addresses_only() { - let mut fx = fixture_without_recovery(NETWORK); - let near = GAP + 5; - let far = near + GAP + 500; - { - let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); - let pool = account - .managed_account_type_mut() - .address_pools_mut() - .into_iter() - .find(|pool| pool.pool_type == AddressPoolType::External) - .expect("external pool"); - for index in [near, far] { - pool.generate_address_at_index(index, &fx.key_source, true).expect("derive"); - pool.mark_index_used(index); - } - } - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); - let external = pool(&fx, AddressPoolType::External); - assert!((0..=near + GAP).all(|index| external.info_at_index(index).is_some())); - assert!(external.info_at_index(near + GAP + 1).is_none(), "the wide run stays out"); - assert!(external.info_at_index(far).is_some()); - } - - /// A cancelled probe does not free the coins: until the replay is back at - /// the height the recovery was to run to, they are those of a past height. - #[tokio::test] - async fn builds_stay_refused_after_a_cancel_until_the_replay_catches_up() { - use crate::wallet::managed_wallet_info::coin_selection::SelectionStrategy; - use crate::wallet::managed_wallet_info::fee::FeeRate; - use crate::wallet::managed_wallet_info::transaction_builder::BuilderError; - - let mut fx = fixture(); - fx.managed.cancel_coinjoin_recovery(); - let build = |fx: &mut Fixture| { - let to = Address::dummy(NETWORK, 9).to_string().parse().expect("address"); - fx.managed.build_unsigned_transaction( - &fx.wallet, - &[], - 0, - vec![(to, 1_000)], - FeeRate::new(1000), - SelectionStrategy::LargestFirst, - ) - }; - assert!(matches!(build(&mut fx), Err(BuilderError::RecoveryInProgress))); - fx.managed.update_synced_height(1_000); - assert!(!matches!(build(&mut fx), Err(BuilderError::RecoveryInProgress))); - assert!( - matches!( - fx.managed.take_finished_coinjoin_recovery(), - Some(CoinJoinRecoveryProgress { - probing: false, - .. - }) - ), - "the end of a cancelled replay is reported as such" - ); - } - - /// An account added while the replay runs holds coins of a past height - /// as much as the others, so it refuses to fund a build too. - #[tokio::test] - async fn an_account_added_during_the_replay_is_replaying_too() { - let mut fx = fixture(); - fx.wallet - .add_account( - AccountType::Standard { - index: 0, - standard_account_type: StandardAccountType::BIP44Account, - }, - None, - ) - .expect("BIP44 account"); - fx.managed - .add_managed_account( - &fx.wallet, - AccountType::Standard { - index: 0, - standard_account_type: StandardAccountType::BIP44Account, - }, - ) - .expect("managed BIP44 account"); - assert!(fx.managed.first_bip44_managed_account_mut().expect("account").replaying); - } - - /// The rebuilt pool keeps the usual lookahead past the highest kept used - /// address, so a coin just past it is watched, not left to the probe. - #[tokio::test] - async fn the_rebuilt_pool_watches_the_gap_past_kept_addresses() { - let mut fx = fixture_without_recovery(NETWORK); - let kept_index = GAP + COINJOIN_RECOVERY_PROBE_WINDOW + 500; - { - let account = fx.managed.first_coinjoin_managed_account_mut().expect("account"); - let pool = account - .managed_account_type_mut() - .address_pools_mut() - .into_iter() - .find(|pool| pool.pool_type == AddressPoolType::External) - .expect("external pool"); - pool.generate_address_at_index(kept_index, &fx.key_source, true).expect("derive"); - pool.mark_index_used(kept_index); - } - fx.managed.begin_coinjoin_recovery(&fx.wallet, 1_000).expect("start"); - assert_eq!(pool_end(&fx, AddressPoolType::External), Some(kept_index + GAP)); - } -} diff --git a/key-wallet/src/transaction_checking/mod.rs b/key-wallet/src/transaction_checking/mod.rs index c359ad856..0ccb2048d 100644 --- a/key-wallet/src/transaction_checking/mod.rs +++ b/key-wallet/src/transaction_checking/mod.rs @@ -5,7 +5,6 @@ //! transaction types. pub mod account_checker; -pub mod coinjoin_recovery; pub mod platform_checker; pub mod transaction_context; pub mod transaction_router; diff --git a/key-wallet/src/transaction_checking/wallet_checker.rs b/key-wallet/src/transaction_checking/wallet_checker.rs index cabfbf044..356786035 100644 --- a/key-wallet/src/transaction_checking/wallet_checker.rs +++ b/key-wallet/src/transaction_checking/wallet_checker.rs @@ -231,22 +231,6 @@ impl WalletTransactionChecker for ManagedWalletInfo { // Check only relevant account types let mut result = self.accounts.check_transaction(tx, &relevant_types); - // During a CoinJoin recovery replay, a relevant transaction may also pay - // CoinJoin addresses of ours past the pools' gap limit (32008). They are - // added to the pools before any state is touched, and the check is - // repeated so the update below credits them. - if update_state - && result.is_relevant - && relevant_types.contains(&AccountTypeToCheck::CoinJoin) - { - let recovered = self.recover_coinjoin_outputs(tx, wallet); - if !recovered.is_empty() { - result = self.accounts.check_transaction(tx, &relevant_types); - result.new_addresses.extend(recovered); - result.state_modified = true; - } - } - // #649: remember every spend seen in a block, independent of the // spending tx's classification or whether the wallet can attribute it. // Insert-only here (no account mutation), so a spend that IS attributed diff --git a/key-wallet/src/wallet/managed_wallet_info/asset_lock_builder.rs b/key-wallet/src/wallet/managed_wallet_info/asset_lock_builder.rs index 074e49bf9..322098b3c 100644 --- a/key-wallet/src/wallet/managed_wallet_info/asset_lock_builder.rs +++ b/key-wallet/src/wallet/managed_wallet_info/asset_lock_builder.rs @@ -384,9 +384,6 @@ impl ManagedWalletInfo { drain: bool, signer: &T, ) -> Result { - if self.is_replaying() { - return Err(BuilderError::RecoveryInProgress.into()); - } validate_funding_sources(funding_sources, credit_outputs.len(), drain)?; // Build first, derive credit keys after — a build failure must not diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs new file mode 100644 index 000000000..db1b82361 --- /dev/null +++ b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs @@ -0,0 +1,203 @@ +//! Pre-generating CoinJoin address pools past the gap limit. +//! +//! A wallet migrated from DashSync can hold mixed coins on CoinJoin addresses +//! far past the gap limit. Extending its CoinJoin pools to a fixed depth +//! before the first sync from birth lets the regular sync find them; +//! trimming afterwards drops the unused tail again. + +use super::ManagedWalletInfo; +use crate::account::AccountType; +use crate::error::Result; +use crate::managed_account::managed_account_trait::ManagedAccountTrait; +use crate::wallet::Wallet; +use crate::KeySource; + +impl ManagedWalletInfo { + /// Derive both pools of every CoinJoin account up to and including `last_index`. + /// + /// Call it on fresh wallet state, before the first sync from the wallet's + /// birth height: addresses added after a block was processed do not bring + /// that block back. Returns the number of addresses generated. + pub fn extend_coinjoin_pools(&mut self, wallet: &Wallet, last_index: u32) -> Result { + let mut generated = 0; + for (index, account) in self.accounts.coinjoin_accounts.iter_mut() { + let key_source = wallet.key_source_for_account(AccountType::CoinJoin { + index: *index, + }); + if matches!(key_source, KeySource::NoKeySource) { + continue; + } + let mut account_generated = 0; + for pool in account.managed_account_type_mut().address_pools_mut() { + let start = pool.stats().highest_generated.map_or(0, |h| h + 1); + if start <= last_index { + account_generated += + pool.generate_addresses(last_index - start + 1, &key_source, true)?.len(); + } + } + if account_generated > 0 { + account.bump_monitor_revision(); + } + generated += account_generated; + } + Ok(generated) + } + + /// Drop the unused addresses past the highest used one plus the gap limit + /// from every CoinJoin pool. Returns the number of addresses removed. + pub fn trim_coinjoin_pools(&mut self) -> u32 { + let mut pruned = 0; + for account in self.accounts.coinjoin_accounts.values_mut() { + let mut account_pruned = 0; + for pool in account.managed_account_type_mut().address_pools_mut() { + account_pruned += pool.prune_unused(); + } + if account_pruned > 0 { + account.bump_monitor_revision(); + } + pruned += account_pruned; + } + pruned + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::managed_account::address_pool::{AddressPool, AddressPoolType}; + use crate::transaction_checking::{BlockInfo, TransactionContext, WalletTransactionChecker}; + use crate::wallet::initialization::WalletAccountCreationOptions; + use crate::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; + use crate::Network; + use dashcore::hashes::Hash; + use dashcore::{Address, BlockHash, Transaction}; + + const NETWORK: Network = Network::Testnet; + const DENOM: u64 = 100_001; + const GAP: u32 = crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; + + fn fresh() -> (Wallet, ManagedWalletInfo) { + let wallet = + Wallet::new_random(NETWORK, WalletAccountCreationOptions::Default).expect("wallet"); + let managed = ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0); + (wallet, managed) + } + + fn pool(managed: &ManagedWalletInfo, pool_type: AddressPoolType) -> &AddressPool { + managed + .accounts + .coinjoin_accounts + .get(&0) + .expect("CoinJoin account") + .managed_account_type() + .address_pools() + .into_iter() + .find(|pool| pool.pool_type == pool_type) + .expect("pool") + } + + fn coinjoin_address(wallet: &Wallet, internal: bool, index: u32) -> Address { + let key_source = wallet.key_source_for_account(AccountType::CoinJoin { + index: 0, + }); + let mut scratch = AddressPool::new_without_generation( + Default::default(), + if internal { + AddressPoolType::Internal + } else { + AddressPoolType::External + }, + GAP, + NETWORK, + ); + scratch.generate_address_at_index(index, &key_source, false).expect("derive") + } + + fn block(height: u32) -> TransactionContext { + TransactionContext::InBlock(BlockInfo::new( + height, + BlockHash::from_byte_array([height as u8; 32]), + 1_650_000_000 + height, + )) + } + + #[test] + fn extend_derives_both_chains_to_the_depth_and_is_idempotent() { + let (wallet, mut managed) = fresh(); + let generated = managed.extend_coinjoin_pools(&wallet, 1_999).expect("extend"); + assert_eq!(generated, 2 * 2_000 - 2 * GAP as usize); + for pool_type in [AddressPoolType::External, AddressPoolType::Internal] { + assert_eq!(pool(&managed, pool_type).stats().highest_generated, Some(1_999)); + assert_eq!(pool(&managed, pool_type).stats().total_generated, 2_000); + } + assert_eq!(managed.extend_coinjoin_pools(&wallet, 1_999).expect("again"), 0); + assert_eq!(managed.extend_coinjoin_pools(&wallet, 10).expect("shallower"), 0); + } + + #[test] + fn extend_leaves_other_accounts_alone() { + let (wallet, mut managed) = fresh(); + let before: Vec<_> = managed + .accounts + .standard_bip44_accounts + .values() + .map(|a| a.managed_account_type().address_pools()[0].stats().total_generated) + .collect(); + managed.extend_coinjoin_pools(&wallet, 999).expect("extend"); + let after: Vec<_> = managed + .accounts + .standard_bip44_accounts + .values() + .map(|a| a.managed_account_type().address_pools()[0].stats().total_generated) + .collect(); + assert_eq!(before, after); + } + + /// 32008: a coin far past the gap limit on either chain is credited by the + /// regular check once the pools are extended past it, and missed without. + #[tokio::test] + async fn coins_past_the_gap_are_found_only_after_extending() { + for internal in [false, true] { + let far_index = GAP + 4_000; + for extend in [false, true] { + let (mut wallet, mut managed) = fresh(); + if extend { + managed.extend_coinjoin_pools(&wallet, 11_999).expect("extend"); + } + let far = coinjoin_address(&wallet, internal, far_index); + let tx = Transaction::dummy(&far, 0..1, &[DENOM]); + let result = + managed.check_core_transaction(&tx, block(1), &mut wallet, true, true).await; + let expected = if extend { + DENOM + } else { + 0 + }; + assert_eq!(result.total_received, expected, "internal={internal} extend={extend}"); + assert_eq!(managed.balance().total(), expected); + } + } + } + + #[tokio::test] + async fn trim_keeps_used_addresses_and_the_gap() { + let (mut wallet, mut managed) = fresh(); + managed.extend_coinjoin_pools(&wallet, 11_999).expect("extend"); + let used = 2_500; + let far = coinjoin_address(&wallet, false, used); + let tx = Transaction::dummy(&far, 0..1, &[DENOM]); + managed.check_core_transaction(&tx, block(1), &mut wallet, true, true).await; + + let pruned = managed.trim_coinjoin_pools(); + assert_eq!(pruned, (11_999 - (used + GAP)) + (11_999 - (GAP - 1))); + let external = pool(&managed, AddressPoolType::External); + assert_eq!(external.stats().highest_generated, Some(used + GAP)); + assert!(external.info_at_index(used).is_some_and(|info| info.is_used())); + assert_eq!( + pool(&managed, AddressPoolType::Internal).stats().highest_generated, + Some(GAP - 1) + ); + assert_eq!(managed.balance().total(), DENOM, "trimming must not touch the balance"); + assert_eq!(managed.trim_coinjoin_pools(), 0); + } +} diff --git a/key-wallet/src/wallet/managed_wallet_info/mod.rs b/key-wallet/src/wallet/managed_wallet_info/mod.rs index 163f6547c..40d56d57d 100644 --- a/key-wallet/src/wallet/managed_wallet_info/mod.rs +++ b/key-wallet/src/wallet/managed_wallet_info/mod.rs @@ -5,6 +5,7 @@ pub mod asset_lock_builder; pub mod coin_selection; +pub mod coinjoin_pools; pub mod fee; pub mod helpers; pub use helpers::AbandonOutcome; @@ -60,19 +61,6 @@ pub struct ManagedWalletInfo { /// Transactions that have received an InstantSend lock. #[cfg_attr(feature = "serde", serde(skip))] pub(crate) instant_send_locks: HashSet, - /// The CoinJoin recovery replay running on this wallet, if any, until the - /// replay is back at the height it was to run to (builds are refused - /// meanwhile, even after a cancel stopped its probe). In memory only: a - /// restart ends it, and the wallet resumes a normal sync from wherever the - /// replay got to. - #[cfg_attr(feature = "serde", serde(skip))] - pub(crate) coinjoin_recovery: - Option, - /// What a CoinJoin recovery found, once its replay completed, until the - /// manager takes it to report the end. - #[cfg_attr(feature = "serde", serde(skip))] - pub(crate) finished_coinjoin_recovery: - Option, /// Outpoints observed spent by transactions seen in a processed block, /// mapped to the height of the block that spent them. /// @@ -257,8 +245,6 @@ impl ManagedWalletInfo { accounts: ManagedAccountCollection::new(), balance: WalletCoreBalance::default(), instant_send_locks: HashSet::new(), - coinjoin_recovery: None, - finished_coinjoin_recovery: None, observed_spent_outpoints: BTreeMap::new(), account_generation: 0, noted_chain_lock_height: None, @@ -276,8 +262,6 @@ impl ManagedWalletInfo { accounts: ManagedAccountCollection::new(), balance: WalletCoreBalance::default(), instant_send_locks: HashSet::new(), - coinjoin_recovery: None, - finished_coinjoin_recovery: None, observed_spent_outpoints: BTreeMap::new(), account_generation: 0, noted_chain_lock_height: None, @@ -305,8 +289,6 @@ impl ManagedWalletInfo { accounts: ManagedAccountCollection::from_account_collection(&wallet.accounts), balance: WalletCoreBalance::default(), instant_send_locks: HashSet::new(), - coinjoin_recovery: None, - finished_coinjoin_recovery: None, observed_spent_outpoints: BTreeMap::new(), account_generation: 0, noted_chain_lock_height: None, @@ -417,7 +399,7 @@ impl ManagedWalletInfo { /// `last_processed_height` and `last_applied_chain_lock` are intentionally /// left as-is: they are chain facts, not coverage facts, and keeping the /// chainlock is what lets the backfilled history arrive born-chainlocked. - pub(crate) fn rewind_sync_checkpoint_for_new_account(&mut self) { + fn rewind_sync_checkpoint_for_new_account(&mut self) { let floor = self.metadata.birth_height.saturating_sub(1); self.metadata.synced_height = self.metadata.synced_height.min(floor); // Bump unconditionally — even when the checkpoint was already at or @@ -426,11 +408,6 @@ impl ManagedWalletInfo { // the generation mismatch is the only signal a commit-time check has // when the rewind is height-invisible (dashpay/rust-dashcore#649). self.account_generation += 1; - // An account added during a CoinJoin recovery replay holds coins of a - // past height as much as the others. - if self.coinjoin_recovery.is_some() { - self.set_funds_replaying(true); - } } /// Current generation of the wallet's account set; see the diff --git a/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs b/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs index 0e5ebe7bf..57afdefa6 100644 --- a/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs +++ b/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs @@ -95,9 +95,6 @@ pub struct TransactionBuilder { /// account that holds the UTXO, so each account reserves its own share of /// the chosen inputs — all under the one token this build is stamped with. funding: Vec<(ReservationSet, HashSet)>, - /// A funding account was being replayed by a CoinJoin recovery, so the - /// build must fail rather than spend coins of a past height. - replaying_funds: bool, } impl Default for TransactionBuilder { @@ -122,7 +119,6 @@ impl TransactionBuilder { special_payload: None, payload_finalizer: None, funding: Vec::new(), - replaying_funds: false, } } @@ -199,10 +195,6 @@ impl TransactionBuilder { acc: &Account, contribute_candidates: bool, ) -> Self { - if funds_acc.replaying { - self.replaying_funds = true; - return self; - } let reserved = funds_acc.reservations().reserved(self.current_height); // An outpoint the builder already holds — seeded by `add_inputs`, or // offered by an earlier `add_funding` of an overlapping account — must @@ -460,9 +452,6 @@ impl TransactionBuilder { fn assemble_unsigned( mut self, ) -> Result<(Transaction, Vec, Option), BuilderError> { - if self.replaying_funds { - return Err(BuilderError::RecoveryInProgress); - } if let Some(TransactionPayload::AssetLockPayloadType(p)) = &self.special_payload { if p.credit_outputs.is_empty() { return Err(BuilderError::NoOutputs); @@ -1051,9 +1040,6 @@ pub enum BuilderError { len: usize, max: usize, }, - /// The wallet is being replayed by a CoinJoin recovery, so its coins are - /// those of a past height; a build has to wait until the replay is done. - RecoveryInProgress, } impl fmt::Display for BuilderError { @@ -1086,9 +1072,6 @@ impl fmt::Display for BuilderError { } => { write!(f, "OP_RETURN payload too large: {len} bytes (max {max})") } - Self::RecoveryInProgress => { - write!(f, "The wallet is being replayed by a CoinJoin recovery; try again when it finishes") - } } } } diff --git a/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs b/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs index 3cbb50397..e82a1212e 100644 --- a/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs +++ b/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs @@ -197,11 +197,6 @@ impl ManagedWalletInfo { fee_rate: FeeRate, strategy: SelectionStrategy, ) -> Result<(TransactionBuilder, HashMap), BuilderError> { - // Up front, whichever sources were asked for; the builder refuses on - // its own as well, for callers that fund it directly. - if self.is_replaying() { - return Err(BuilderError::RecoveryInProgress); - } let outputs = outputs .into_iter() .map(|(address, value)| { diff --git a/key-wallet/src/wallet/managed_wallet_info/wallet_info_interface.rs b/key-wallet/src/wallet/managed_wallet_info/wallet_info_interface.rs index 958ecf1a9..fe0f169cd 100644 --- a/key-wallet/src/wallet/managed_wallet_info/wallet_info_interface.rs +++ b/key-wallet/src/wallet/managed_wallet_info/wallet_info_interface.rs @@ -217,51 +217,6 @@ pub trait WalletInfoInterface: Sized + WalletTransactionChecker + ManagedAccount 0 } - /// Start a CoinJoin recovery replay through - /// `WalletManager::begin_coinjoin_recovery`, which also emits the reset - /// event a persisting consumer needs and runs one recovery at a time; - /// calling this directly skips both. Forgets everything the wallet learned - /// from the chain, rewind its sync checkpoint to just below its birth - /// height, and look for CoinJoin coins past the gap limit while the sync - /// layer replays it (ticket 32008). The account generation is bumped, so - /// in-flight scans cannot certify coverage for the reset wallet. - /// - /// `known_height` is the highest height the caller knows the chain to - /// have reached; the replay is complete once the wallet is synced to it, - /// or to its own synced height or chainlock when higher. Returns that - /// height. Required, so that a wrapper around another implementation has - /// to forward it (and the three methods below) rather than opt out - /// silently. - fn begin_coinjoin_recovery( - &mut self, - wallet: &Wallet, - known_height: CoreBlockHeight, - ) -> Result; - - /// Stop the probe of a running CoinJoin recovery before its replay is - /// complete, returning how many CoinJoin addresses it found past the gap - /// limit; `None` when no probe runs, which includes a cancelled recovery - /// whose replay still goes on (see `coinjoin_recovery_progress`). - fn cancel_coinjoin_recovery(&mut self) -> Option; - - /// Where the running CoinJoin recovery replay stands; `None` when none - /// runs. - fn coinjoin_recovery_progress( - &self, - ) -> Option; - - /// Where a CoinJoin recovery whose replay reached the height it was to - /// run to (the one `begin_coinjoin_recovery` returned) since the last call - /// ended up: how many CoinJoin addresses it found past the gap limit, and - /// whether it had been cancelled. The recovery itself ends inside - /// `update_synced_height`, whoever calls it; this only hands its result - /// over once. `WalletManager::update_wallet_synced_height` takes it right - /// after advancing and reports it as an event, so a caller advancing the - /// height any other way reports nothing. - fn take_finished_coinjoin_recovery( - &mut self, - ) -> Option; - /// Return the highest chainlock that has been applied to this /// wallet, retaining the signing proof. Blocks at or below /// `chain_lock.block_height` are considered chainlock-finalized @@ -407,30 +362,6 @@ impl WalletInfoInterface for ManagedWalletInfo { self.account_generation } - fn begin_coinjoin_recovery( - &mut self, - wallet: &Wallet, - known_height: CoreBlockHeight, - ) -> Result { - ManagedWalletInfo::begin_coinjoin_recovery(self, wallet, known_height) - } - - fn cancel_coinjoin_recovery(&mut self) -> Option { - ManagedWalletInfo::cancel_coinjoin_recovery(self) - } - - fn coinjoin_recovery_progress( - &self, - ) -> Option { - self.coinjoin_recovery.as_ref().map(|recovery| recovery.progress()) - } - - fn take_finished_coinjoin_recovery( - &mut self, - ) -> Option { - ManagedWalletInfo::take_finished_coinjoin_recovery(self) - } - fn last_applied_chain_lock(&self) -> Option<&ChainLock> { self.metadata.last_applied_chain_lock.as_ref() } @@ -635,7 +566,6 @@ impl WalletInfoInterface for ManagedWalletInfo { // A newly committed checkpoint can lift the finality boundary when the // chainlock was already ahead of the old synced_height. self.prune_finalized_observed_spends(); - self.on_synced_height_advanced(); } fn truncate_above(&mut self, height: CoreBlockHeight) -> Result { From dd9eee1080eb9f76dcaa8b80df7bca35b171257c Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:01:37 +0300 Subject: [PATCH 15/24] test(key-wallet): keep the CoinJoin pool tests shallow enough for debug runs Co-Authored-By: Claude Opus 5.5 --- .../src/wallet/managed_wallet_info/coinjoin_pools.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs index db1b82361..0713438c2 100644 --- a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs +++ b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs @@ -75,6 +75,8 @@ mod tests { const NETWORK: Network = Network::Testnet; const DENOM: u64 = 100_001; const GAP: u32 = crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; + /// Deep enough to sit far past the gap limit, shallow enough for debug test runs. + const DEPTH: u32 = 2_999; fn fresh() -> (Wallet, ManagedWalletInfo) { let wallet = @@ -158,11 +160,11 @@ mod tests { #[tokio::test] async fn coins_past_the_gap_are_found_only_after_extending() { for internal in [false, true] { - let far_index = GAP + 4_000; + let far_index = DEPTH - 1; for extend in [false, true] { let (mut wallet, mut managed) = fresh(); if extend { - managed.extend_coinjoin_pools(&wallet, 11_999).expect("extend"); + managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); } let far = coinjoin_address(&wallet, internal, far_index); let tx = Transaction::dummy(&far, 0..1, &[DENOM]); @@ -182,14 +184,14 @@ mod tests { #[tokio::test] async fn trim_keeps_used_addresses_and_the_gap() { let (mut wallet, mut managed) = fresh(); - managed.extend_coinjoin_pools(&wallet, 11_999).expect("extend"); + managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); let used = 2_500; let far = coinjoin_address(&wallet, false, used); let tx = Transaction::dummy(&far, 0..1, &[DENOM]); managed.check_core_transaction(&tx, block(1), &mut wallet, true, true).await; let pruned = managed.trim_coinjoin_pools(); - assert_eq!(pruned, (11_999 - (used + GAP)) + (11_999 - (GAP - 1))); + assert_eq!(pruned, (DEPTH - (used + GAP)) + (DEPTH - (GAP - 1))); let external = pool(&managed, AddressPoolType::External); assert_eq!(external.stats().highest_generated, Some(used + GAP)); assert!(external.info_at_index(used).is_some_and(|info| info.is_used())); From fab4eba4f96131f12daf24d77d92fbd847f21c09 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:25:46 +0300 Subject: [PATCH 16/24] fix(key-wallet): bound the CoinJoin pool depth and signal partial growth Reject a depth at or above MAX_COINJOIN_POOL_DEPTH instead of overflowing or deriving for ever, and bump the monitor revision for every account whose pools grew even when derivation fails partway. Drop trim_coinjoin_pools: it has no caller and could prune addresses before the sync from birth is done. Tests run on a fixed seed, on mainnet and testnet, and cover the monitor revision and the depth bound. Co-Authored-By: Claude Opus 5.5 --- .../managed_wallet_info/coinjoin_pools.rs | 160 ++++++++++-------- 1 file changed, 86 insertions(+), 74 deletions(-) diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs index 0713438c2..8206cbf5e 100644 --- a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs +++ b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs @@ -2,23 +2,36 @@ //! //! A wallet migrated from DashSync can hold mixed coins on CoinJoin addresses //! far past the gap limit. Extending its CoinJoin pools to a fixed depth -//! before the first sync from birth lets the regular sync find them; -//! trimming afterwards drops the unused tail again. +//! before the first sync from birth lets the regular sync find them. use super::ManagedWalletInfo; use crate::account::AccountType; -use crate::error::Result; +use crate::error::{Error, Result}; use crate::managed_account::managed_account_trait::ManagedAccountTrait; use crate::wallet::Wallet; use crate::KeySource; +/// Upper bound (exclusive) for the `last_index` passed to +/// [`ManagedWalletInfo::extend_coinjoin_pools`]. DashSync derived about +/// 10,500 CoinJoin addresses per chain on the deepest wallets seen. +pub const MAX_COINJOIN_POOL_DEPTH: u32 = 100_000; + impl ManagedWalletInfo { /// Derive both pools of every CoinJoin account up to and including `last_index`. /// /// Call it on fresh wallet state, before the first sync from the wallet's - /// birth height: addresses added after a block was processed do not bring - /// that block back. Returns the number of addresses generated. + /// birth height and before the wallet's addresses are persisted: addresses + /// added after a block was processed do not bring that block back, and the + /// new addresses are not reported anywhere but in the returned count. + /// + /// Returns the number of addresses generated. On an error, every account + /// whose pools already grew still gets its monitor revision bumped. pub fn extend_coinjoin_pools(&mut self, wallet: &Wallet, last_index: u32) -> Result { + if last_index >= MAX_COINJOIN_POOL_DEPTH { + return Err(Error::InvalidParameter(format!( + "CoinJoin pool depth {last_index} must be below {MAX_COINJOIN_POOL_DEPTH}" + ))); + } let mut generated = 0; for (index, account) in self.accounts.coinjoin_accounts.iter_mut() { let key_source = wallet.key_source_for_account(AccountType::CoinJoin { @@ -28,37 +41,27 @@ impl ManagedWalletInfo { continue; } let mut account_generated = 0; + let mut outcome = Ok(()); for pool in account.managed_account_type_mut().address_pools_mut() { - let start = pool.stats().highest_generated.map_or(0, |h| h + 1); - if start <= last_index { - account_generated += - pool.generate_addresses(last_index - start + 1, &key_source, true)?.len(); + let start = pool.highest_generated.map_or(0, |h| h + 1); + if start > last_index { + continue; + } + let result = pool.generate_addresses(last_index - start + 1, &key_source, true); + account_generated += (pool.highest_generated.map_or(0, |h| h + 1) - start) as usize; + if let Err(error) = result { + outcome = Err(error); + break; } } if account_generated > 0 { account.bump_monitor_revision(); } generated += account_generated; + outcome?; } Ok(generated) } - - /// Drop the unused addresses past the highest used one plus the gap limit - /// from every CoinJoin pool. Returns the number of addresses removed. - pub fn trim_coinjoin_pools(&mut self) -> u32 { - let mut pruned = 0; - for account in self.accounts.coinjoin_accounts.values_mut() { - let mut account_pruned = 0; - for pool in account.managed_account_type_mut().address_pools_mut() { - account_pruned += pool.prune_unused(); - } - if account_pruned > 0 { - account.bump_monitor_revision(); - } - pruned += account_pruned; - } - pruned - } } #[cfg(test)] @@ -72,15 +75,15 @@ mod tests { use dashcore::hashes::Hash; use dashcore::{Address, BlockHash, Transaction}; - const NETWORK: Network = Network::Testnet; const DENOM: u64 = 100_001; const GAP: u32 = crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; /// Deep enough to sit far past the gap limit, shallow enough for debug test runs. - const DEPTH: u32 = 2_999; + const DEPTH: u32 = 1_499; - fn fresh() -> (Wallet, ManagedWalletInfo) { + fn fresh(network: Network) -> (Wallet, ManagedWalletInfo) { let wallet = - Wallet::new_random(NETWORK, WalletAccountCreationOptions::Default).expect("wallet"); + Wallet::from_seed_bytes([0x01; 64], network, WalletAccountCreationOptions::Default) + .expect("wallet"); let managed = ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0); (wallet, managed) } @@ -98,7 +101,11 @@ mod tests { .expect("pool") } - fn coinjoin_address(wallet: &Wallet, internal: bool, index: u32) -> Address { + fn monitor_revision(managed: &ManagedWalletInfo) -> u64 { + managed.accounts.coinjoin_accounts.get(&0).expect("CoinJoin account").monitor_revision() + } + + fn coinjoin_address(wallet: &Wallet, network: Network, internal: bool, index: u32) -> Address { let key_source = wallet.key_source_for_account(AccountType::CoinJoin { index: 0, }); @@ -110,7 +117,7 @@ mod tests { AddressPoolType::External }, GAP, - NETWORK, + network, ); scratch.generate_address_at_index(index, &key_source, false).expect("derive") } @@ -125,20 +132,44 @@ mod tests { #[test] fn extend_derives_both_chains_to_the_depth_and_is_idempotent() { - let (wallet, mut managed) = fresh(); + let (wallet, mut managed) = fresh(Network::Testnet); let generated = managed.extend_coinjoin_pools(&wallet, 1_999).expect("extend"); assert_eq!(generated, 2 * 2_000 - 2 * GAP as usize); for pool_type in [AddressPoolType::External, AddressPoolType::Internal] { - assert_eq!(pool(&managed, pool_type).stats().highest_generated, Some(1_999)); + assert_eq!(pool(&managed, pool_type).highest_generated, Some(1_999)); assert_eq!(pool(&managed, pool_type).stats().total_generated, 2_000); } assert_eq!(managed.extend_coinjoin_pools(&wallet, 1_999).expect("again"), 0); assert_eq!(managed.extend_coinjoin_pools(&wallet, 10).expect("shallower"), 0); } + /// dash-spv rebuilds its filter only when the monitor revision moves. + #[test] + fn extend_bumps_the_monitor_revision_only_when_addresses_were_added() { + let (wallet, mut managed) = fresh(Network::Testnet); + let before = monitor_revision(&managed); + managed.extend_coinjoin_pools(&wallet, 999).expect("extend"); + let after = monitor_revision(&managed); + assert!(after > before); + managed.extend_coinjoin_pools(&wallet, 999).expect("again"); + assert_eq!(monitor_revision(&managed), after); + } + + #[test] + fn extend_rejects_a_depth_at_or_above_the_maximum() { + let (wallet, mut managed) = fresh(Network::Testnet); + for last_index in [MAX_COINJOIN_POOL_DEPTH, u32::MAX] { + assert!(matches!( + managed.extend_coinjoin_pools(&wallet, last_index), + Err(Error::InvalidParameter(_)) + )); + } + assert_eq!(pool(&managed, AddressPoolType::External).highest_generated, Some(GAP - 1)); + } + #[test] fn extend_leaves_other_accounts_alone() { - let (wallet, mut managed) = fresh(); + let (wallet, mut managed) = fresh(Network::Testnet); let before: Vec<_> = managed .accounts .standard_bip44_accounts @@ -159,47 +190,28 @@ mod tests { /// regular check once the pools are extended past it, and missed without. #[tokio::test] async fn coins_past_the_gap_are_found_only_after_extending() { - for internal in [false, true] { - let far_index = DEPTH - 1; - for extend in [false, true] { - let (mut wallet, mut managed) = fresh(); - if extend { - managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); + for network in [Network::Mainnet, Network::Testnet] { + for internal in [false, true] { + for extend in [false, true] { + let (mut wallet, mut managed) = fresh(network); + if extend { + managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); + } + let far = coinjoin_address(&wallet, network, internal, DEPTH - 1); + let tx = Transaction::dummy(&far, 0..1, &[DENOM]); + let result = managed + .check_core_transaction(&tx, block(1), &mut wallet, true, true) + .await; + let expected = if extend { + DENOM + } else { + 0 + }; + let case = format!("{network:?} internal={internal} extend={extend}"); + assert_eq!(result.total_received, expected, "{case}"); + assert_eq!(managed.balance().total(), expected, "{case}"); } - let far = coinjoin_address(&wallet, internal, far_index); - let tx = Transaction::dummy(&far, 0..1, &[DENOM]); - let result = - managed.check_core_transaction(&tx, block(1), &mut wallet, true, true).await; - let expected = if extend { - DENOM - } else { - 0 - }; - assert_eq!(result.total_received, expected, "internal={internal} extend={extend}"); - assert_eq!(managed.balance().total(), expected); } } } - - #[tokio::test] - async fn trim_keeps_used_addresses_and_the_gap() { - let (mut wallet, mut managed) = fresh(); - managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); - let used = 2_500; - let far = coinjoin_address(&wallet, false, used); - let tx = Transaction::dummy(&far, 0..1, &[DENOM]); - managed.check_core_transaction(&tx, block(1), &mut wallet, true, true).await; - - let pruned = managed.trim_coinjoin_pools(); - assert_eq!(pruned, (DEPTH - (used + GAP)) + (DEPTH - (GAP - 1))); - let external = pool(&managed, AddressPoolType::External); - assert_eq!(external.stats().highest_generated, Some(used + GAP)); - assert!(external.info_at_index(used).is_some_and(|info| info.is_used())); - assert_eq!( - pool(&managed, AddressPoolType::Internal).stats().highest_generated, - Some(GAP - 1) - ); - assert_eq!(managed.balance().total(), DENOM, "trimming must not touch the balance"); - assert_eq!(managed.trim_coinjoin_pools(), 0); - } } From e9af123ba68cf546b8b2e73402239562f60af155 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:30:38 +0300 Subject: [PATCH 17/24] fix(key-wallet): validate before extending the CoinJoin pools Check the wallet owns the info and every CoinJoin account has a key source before any pool changes, instead of silently skipping an account. Lower the depth cap to 25,000 so the mempool bloom filter stays usable, derive in place without collecting the addresses, and pin the inclusive depth edge, the untouched non-CoinJoin accounts and the ownership check in tests. Co-Authored-By: Claude Opus 5.5 --- .../managed_wallet_info/coinjoin_pools.rs | 125 ++++++++++++------ 1 file changed, 86 insertions(+), 39 deletions(-) diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs index 8206cbf5e..227bdb60f 100644 --- a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs +++ b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs @@ -13,8 +13,9 @@ use crate::KeySource; /// Upper bound (exclusive) for the `last_index` passed to /// [`ManagedWalletInfo::extend_coinjoin_pools`]. DashSync derived about -/// 10,500 CoinJoin addresses per chain on the deepest wallets seen. -pub const MAX_COINJOIN_POOL_DEPTH: u32 = 100_000; +/// 10,500 CoinJoin addresses per chain on the deepest wallets seen; much +/// deeper pools saturate dash-spv's size-capped mempool bloom filter. +pub const MAX_COINJOIN_POOL_DEPTH: u32 = 25_000; impl ManagedWalletInfo { /// Derive both pools of every CoinJoin account up to and including `last_index`. @@ -22,36 +23,51 @@ impl ManagedWalletInfo { /// Call it on fresh wallet state, before the first sync from the wallet's /// birth height and before the wallet's addresses are persisted: addresses /// added after a block was processed do not bring that block back, and the - /// new addresses are not reported anywhere but in the returned count. + /// new addresses are not reported anywhere but in the returned count. The + /// pools stay that deep for the life of the wallet state. /// - /// Returns the number of addresses generated. On an error, every account - /// whose pools already grew still gets its monitor revision bumped. + /// Returns the number of addresses generated. The depth, the wallet and + /// every CoinJoin account's key source are checked before anything changes. + /// A derivation error past that point leaves the pools grown partway (the + /// accounts that grew still get their monitor revision bumped); discard the + /// wallet state then, as it is no longer fresh. pub fn extend_coinjoin_pools(&mut self, wallet: &Wallet, last_index: u32) -> Result { if last_index >= MAX_COINJOIN_POOL_DEPTH { return Err(Error::InvalidParameter(format!( "CoinJoin pool depth {last_index} must be below {MAX_COINJOIN_POOL_DEPTH}" ))); } - let mut generated = 0; - for (index, account) in self.accounts.coinjoin_accounts.iter_mut() { + if wallet.wallet_id != self.wallet_id { + return Err(Error::InvalidParameter( + "the wallet does not own this wallet info".to_string(), + )); + } + let mut key_sources = Vec::with_capacity(self.accounts.coinjoin_accounts.len()); + for index in self.accounts.coinjoin_accounts.keys() { let key_source = wallet.key_source_for_account(AccountType::CoinJoin { index: *index, }); if matches!(key_source, KeySource::NoKeySource) { - continue; + return Err(Error::InvalidParameter(format!( + "the wallet has no key source for CoinJoin account {index}" + ))); } + key_sources.push(key_source); + } + + let mut generated = 0; + for (account, key_source) in self.accounts.coinjoin_accounts.values_mut().zip(&key_sources) + { let mut account_generated = 0; let mut outcome = Ok(()); - for pool in account.managed_account_type_mut().address_pools_mut() { + 'pools: for pool in account.managed_account_type_mut().address_pools_mut() { let start = pool.highest_generated.map_or(0, |h| h + 1); - if start > last_index { - continue; - } - let result = pool.generate_addresses(last_index - start + 1, &key_source, true); - account_generated += (pool.highest_generated.map_or(0, |h| h + 1) - start) as usize; - if let Err(error) = result { - outcome = Err(error); - break; + for index in start..=last_index { + if let Err(error) = pool.generate_address_at_index(index, key_source, true) { + outcome = Err(error); + break 'pools; + } + account_generated += 1; } } if account_generated > 0 { @@ -167,47 +183,78 @@ mod tests { assert_eq!(pool(&managed, AddressPoolType::External).highest_generated, Some(GAP - 1)); } + #[test] + fn extend_rejects_a_wallet_that_does_not_own_the_info() { + let (_, mut managed) = fresh(Network::Testnet); + let other = Wallet::from_seed_bytes( + [0x02; 64], + Network::Testnet, + WalletAccountCreationOptions::Default, + ) + .expect("wallet"); + assert!(matches!( + managed.extend_coinjoin_pools(&other, 999), + Err(Error::InvalidParameter(_)) + )); + assert_eq!(pool(&managed, AddressPoolType::External).highest_generated, Some(GAP - 1)); + } + #[test] fn extend_leaves_other_accounts_alone() { + fn others(managed: &ManagedWalletInfo) -> Vec<(String, u64, Vec)> { + managed + .accounts + .all_accounts() + .into_iter() + .filter(|account| { + !matches!( + account.managed_account_type().to_account_type(), + AccountType::CoinJoin { .. } + ) + }) + .map(|account| { + let pools = account + .managed_account_type() + .address_pools() + .iter() + .map(|pool| pool.stats().total_generated) + .collect(); + ( + format!("{:?}", account.managed_account_type().to_account_type()), + account.monitor_revision(), + pools, + ) + }) + .collect() + } let (wallet, mut managed) = fresh(Network::Testnet); - let before: Vec<_> = managed - .accounts - .standard_bip44_accounts - .values() - .map(|a| a.managed_account_type().address_pools()[0].stats().total_generated) - .collect(); + let before = others(&managed); + assert!(!before.is_empty()); managed.extend_coinjoin_pools(&wallet, 999).expect("extend"); - let after: Vec<_> = managed - .accounts - .standard_bip44_accounts - .values() - .map(|a| a.managed_account_type().address_pools()[0].stats().total_generated) - .collect(); - assert_eq!(before, after); + assert_eq!(others(&managed), before); } /// 32008: a coin far past the gap limit on either chain is credited by the - /// regular check once the pools are extended past it, and missed without. + /// regular check once the pools are extended through its index (inclusive), + /// and missed without, or one index past the depth. #[tokio::test] async fn coins_past_the_gap_are_found_only_after_extending() { for network in [Network::Mainnet, Network::Testnet] { for internal in [false, true] { - for extend in [false, true] { + for (extend, index, expected) in + [(false, DEPTH, 0), (true, DEPTH, DENOM), (true, DEPTH + 1, 0)] + { let (mut wallet, mut managed) = fresh(network); if extend { managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); } - let far = coinjoin_address(&wallet, network, internal, DEPTH - 1); + let far = coinjoin_address(&wallet, network, internal, index); let tx = Transaction::dummy(&far, 0..1, &[DENOM]); let result = managed .check_core_transaction(&tx, block(1), &mut wallet, true, true) .await; - let expected = if extend { - DENOM - } else { - 0 - }; - let case = format!("{network:?} internal={internal} extend={extend}"); + let case = + format!("{network:?} internal={internal} extend={extend} index={index}"); assert_eq!(result.total_received, expected, "{case}"); assert_eq!(managed.balance().total(), expected, "{case}"); } From b37ea6c6e6bdb71eb12dd2a998456b54b240e3ea Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Thu, 22 Oct 2026 16:04:10 +0300 Subject: [PATCH 18/24] fix(key-wallet): refuse to extend CoinJoin pools on synced state Reject a wallet info that has processed a block past its birth or holds transactions, and one without a CoinJoin account, instead of returning Ok(0). Document the real filter cost of deep pools. Cut the tests down to the boundary the reviewer asked for (found at the depth, missed one past it, in a ChainLocked block on both chains and networks) plus one test for every rejection. Co-Authored-By: Claude Opus 5.5 --- .../managed_wallet_info/coinjoin_pools.rs | 232 +++++++----------- 1 file changed, 95 insertions(+), 137 deletions(-) diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs index 227bdb60f..db641f84a 100644 --- a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs +++ b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs @@ -12,25 +12,30 @@ use crate::wallet::Wallet; use crate::KeySource; /// Upper bound (exclusive) for the `last_index` passed to -/// [`ManagedWalletInfo::extend_coinjoin_pools`]. DashSync derived about -/// 10,500 CoinJoin addresses per chain on the deepest wallets seen; much -/// deeper pools saturate dash-spv's size-capped mempool bloom filter. +/// [`ManagedWalletInfo::extend_coinjoin_pools`], so a bad argument cannot +/// derive for ever. +/// +/// The only DashSync log at hand reported 10,530 CoinJoin addresses derived +/// (both chains together). Deep pools are not free: every extra script is +/// matched against every compact filter (about 3% of blocks match falsely at +/// 12,000 per chain), and past about 9,000 scripts in total dash-spv's +/// size-capped mempool bloom filter runs above its target false-positive rate. pub const MAX_COINJOIN_POOL_DEPTH: u32 = 25_000; impl ManagedWalletInfo { /// Derive both pools of every CoinJoin account up to and including `last_index`. /// - /// Call it on fresh wallet state, before the first sync from the wallet's - /// birth height and before the wallet's addresses are persisted: addresses - /// added after a block was processed do not bring that block back, and the - /// new addresses are not reported anywhere but in the returned count. The - /// pools stay that deep for the life of the wallet state. + /// Only for fresh wallet state, before the first sync from the wallet's + /// birth height and before its addresses are persisted: addresses added + /// after a block was processed do not bring that block back, and the new + /// addresses are reported nowhere but in the returned count. The pools stay + /// that deep for the life of the wallet state. /// - /// Returns the number of addresses generated. The depth, the wallet and - /// every CoinJoin account's key source are checked before anything changes. - /// A derivation error past that point leaves the pools grown partway (the - /// accounts that grew still get their monitor revision bumped); discard the - /// wallet state then, as it is no longer fresh. + /// Returns the number of addresses generated. The depth, the wallet, the + /// fresh state and every CoinJoin account's key source are checked before + /// anything changes. A derivation error past that point leaves the pools + /// grown partway (the accounts that grew still get their monitor revision + /// bumped); discard the wallet state then. pub fn extend_coinjoin_pools(&mut self, wallet: &Wallet, last_index: u32) -> Result { if last_index >= MAX_COINJOIN_POOL_DEPTH { return Err(Error::InvalidParameter(format!( @@ -42,6 +47,18 @@ impl ManagedWalletInfo { "the wallet does not own this wallet info".to_string(), )); } + let synced_past_birth = + self.metadata.last_processed_height > self.metadata.birth_height.saturating_sub(1); + if synced_past_birth + || self.accounts.all_accounts().iter().any(|account| !account.transactions().is_empty()) + { + return Err(Error::InvalidParameter( + "CoinJoin pools can only be extended before the first sync".to_string(), + )); + } + if self.accounts.coinjoin_accounts.is_empty() { + return Err(Error::InvalidParameter("the wallet has no CoinJoin account".to_string())); + } let mut key_sources = Vec::with_capacity(self.accounts.coinjoin_accounts.len()); for index in self.accounts.coinjoin_accounts.keys() { let key_source = wallet.key_source_for_account(AccountType::CoinJoin { @@ -93,32 +110,39 @@ mod tests { const DENOM: u64 = 100_001; const GAP: u32 = crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; - /// Deep enough to sit far past the gap limit, shallow enough for debug test runs. - const DEPTH: u32 = 1_499; - - fn fresh(network: Network) -> (Wallet, ManagedWalletInfo) { - let wallet = - Wallet::from_seed_bytes([0x01; 64], network, WalletAccountCreationOptions::Default) - .expect("wallet"); + /// Far past the gap limit, shallow enough for debug test runs. + const DEPTH: u32 = 2 * GAP + 50; + + fn fresh_with( + network: Network, + options: WalletAccountCreationOptions, + ) -> (Wallet, ManagedWalletInfo) { + let wallet = Wallet::from_seed_bytes([0x01; 64], network, options).expect("wallet"); let managed = ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0); (wallet, managed) } - fn pool(managed: &ManagedWalletInfo, pool_type: AddressPoolType) -> &AddressPool { + fn fresh(network: Network) -> (Wallet, ManagedWalletInfo) { + fresh_with(network, WalletAccountCreationOptions::Default) + } + + fn highest_generated(managed: &ManagedWalletInfo) -> Vec> { managed .accounts .coinjoin_accounts - .get(&0) - .expect("CoinJoin account") - .managed_account_type() - .address_pools() - .into_iter() - .find(|pool| pool.pool_type == pool_type) - .expect("pool") + .values() + .flat_map(|account| account.managed_account_type().address_pools()) + .map(|pool| pool.highest_generated) + .collect() } - fn monitor_revision(managed: &ManagedWalletInfo) -> u64 { - managed.accounts.coinjoin_accounts.get(&0).expect("CoinJoin account").monitor_revision() + fn assert_rejected(managed: &mut ManagedWalletInfo, wallet: &Wallet) { + let before = highest_generated(managed); + assert!(matches!( + managed.extend_coinjoin_pools(wallet, DEPTH), + Err(Error::InvalidParameter(_)) + )); + assert_eq!(highest_generated(managed), before, "nothing may change on a rejection"); } fn coinjoin_address(wallet: &Wallet, network: Network, internal: bool, index: u32) -> Address { @@ -138,41 +162,38 @@ mod tests { scratch.generate_address_at_index(index, &key_source, false).expect("derive") } - fn block(height: u32) -> TransactionContext { - TransactionContext::InBlock(BlockInfo::new( - height, - BlockHash::from_byte_array([height as u8; 32]), - 1_650_000_000 + height, - )) - } + /// 32008: a coin far past the gap limit, in an old ChainLocked block, is + /// credited by the regular check when the pools were extended through its + /// index (inclusive), and missed one index past the depth. + #[tokio::test] + async fn coins_up_to_the_extended_depth_are_found() { + for network in [Network::Mainnet, Network::Testnet] { + for internal in [false, true] { + for (index, expected) in [(DEPTH, DENOM), (DEPTH + 1, 0)] { + let (mut wallet, mut managed) = fresh(network); + let revision = managed.accounts.coinjoin_accounts[&0].monitor_revision(); + managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); + assert!(managed.accounts.coinjoin_accounts[&0].monitor_revision() > revision); - #[test] - fn extend_derives_both_chains_to_the_depth_and_is_idempotent() { - let (wallet, mut managed) = fresh(Network::Testnet); - let generated = managed.extend_coinjoin_pools(&wallet, 1_999).expect("extend"); - assert_eq!(generated, 2 * 2_000 - 2 * GAP as usize); - for pool_type in [AddressPoolType::External, AddressPoolType::Internal] { - assert_eq!(pool(&managed, pool_type).highest_generated, Some(1_999)); - assert_eq!(pool(&managed, pool_type).stats().total_generated, 2_000); + let far = coinjoin_address(&wallet, network, internal, index); + let tx = Transaction::dummy(&far, 0..1, &[DENOM]); + let context = TransactionContext::InChainLockedBlock(BlockInfo::new( + 1, + BlockHash::from_byte_array([1; 32]), + 1_650_000_000, + )); + let result = + managed.check_core_transaction(&tx, context, &mut wallet, true, true).await; + let case = format!("{network:?} internal={internal} index={index}"); + assert_eq!(result.total_received, expected, "{case}"); + assert_eq!(managed.balance().total(), expected, "{case}"); + } + } } - assert_eq!(managed.extend_coinjoin_pools(&wallet, 1_999).expect("again"), 0); - assert_eq!(managed.extend_coinjoin_pools(&wallet, 10).expect("shallower"), 0); } - /// dash-spv rebuilds its filter only when the monitor revision moves. #[test] - fn extend_bumps_the_monitor_revision_only_when_addresses_were_added() { - let (wallet, mut managed) = fresh(Network::Testnet); - let before = monitor_revision(&managed); - managed.extend_coinjoin_pools(&wallet, 999).expect("extend"); - let after = monitor_revision(&managed); - assert!(after > before); - managed.extend_coinjoin_pools(&wallet, 999).expect("again"); - assert_eq!(monitor_revision(&managed), after); - } - - #[test] - fn extend_rejects_a_depth_at_or_above_the_maximum() { + fn extend_rejects_bad_arguments_and_state_without_changing_anything() { let (wallet, mut managed) = fresh(Network::Testnet); for last_index in [MAX_COINJOIN_POOL_DEPTH, u32::MAX] { assert!(matches!( @@ -180,85 +201,22 @@ mod tests { Err(Error::InvalidParameter(_)) )); } - assert_eq!(pool(&managed, AddressPoolType::External).highest_generated, Some(GAP - 1)); - } - #[test] - fn extend_rejects_a_wallet_that_does_not_own_the_info() { - let (_, mut managed) = fresh(Network::Testnet); - let other = Wallet::from_seed_bytes( - [0x02; 64], - Network::Testnet, - WalletAccountCreationOptions::Default, - ) - .expect("wallet"); - assert!(matches!( - managed.extend_coinjoin_pools(&other, 999), - Err(Error::InvalidParameter(_)) - )); - assert_eq!(pool(&managed, AddressPoolType::External).highest_generated, Some(GAP - 1)); - } + let (other, _) = fresh_with(Network::Mainnet, WalletAccountCreationOptions::Default); + assert_rejected(&mut managed, &other); - #[test] - fn extend_leaves_other_accounts_alone() { - fn others(managed: &ManagedWalletInfo) -> Vec<(String, u64, Vec)> { - managed - .accounts - .all_accounts() - .into_iter() - .filter(|account| { - !matches!( - account.managed_account_type().to_account_type(), - AccountType::CoinJoin { .. } - ) - }) - .map(|account| { - let pools = account - .managed_account_type() - .address_pools() - .iter() - .map(|pool| pool.stats().total_generated) - .collect(); - ( - format!("{:?}", account.managed_account_type().to_account_type()), - account.monitor_revision(), - pools, - ) - }) - .collect() - } - let (wallet, mut managed) = fresh(Network::Testnet); - let before = others(&managed); - assert!(!before.is_empty()); - managed.extend_coinjoin_pools(&wallet, 999).expect("extend"); - assert_eq!(others(&managed), before); - } + let (no_coinjoin, mut no_coinjoin_info) = + fresh_with(Network::Testnet, WalletAccountCreationOptions::None); + assert!(no_coinjoin_info.accounts.coinjoin_accounts.is_empty()); + assert_rejected(&mut no_coinjoin_info, &no_coinjoin); - /// 32008: a coin far past the gap limit on either chain is credited by the - /// regular check once the pools are extended through its index (inclusive), - /// and missed without, or one index past the depth. - #[tokio::test] - async fn coins_past_the_gap_are_found_only_after_extending() { - for network in [Network::Mainnet, Network::Testnet] { - for internal in [false, true] { - for (extend, index, expected) in - [(false, DEPTH, 0), (true, DEPTH, DENOM), (true, DEPTH + 1, 0)] - { - let (mut wallet, mut managed) = fresh(network); - if extend { - managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); - } - let far = coinjoin_address(&wallet, network, internal, index); - let tx = Transaction::dummy(&far, 0..1, &[DENOM]); - let result = managed - .check_core_transaction(&tx, block(1), &mut wallet, true, true) - .await; - let case = - format!("{network:?} internal={internal} extend={extend} index={index}"); - assert_eq!(result.total_received, expected, "{case}"); - assert_eq!(managed.balance().total(), expected, "{case}"); - } - } - } + let (wallet, mut missing_key) = fresh(Network::Testnet); + let extra = missing_key.accounts.coinjoin_accounts[&0].clone(); + missing_key.accounts.coinjoin_accounts.insert(1, extra); + assert_rejected(&mut missing_key, &wallet); + + let (wallet, mut synced) = fresh(Network::Testnet); + synced.metadata.last_processed_height = 10; + assert_rejected(&mut synced, &wallet); } } From 6578875eb7b43e12c5e7c0b7e535ad074ebe2525 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Thu, 22 Oct 2026 16:13:01 +0300 Subject: [PATCH 19/24] fix(key-wallet): treat any sync fact as non-fresh and fill pool holes Also refuse a wallet info with a certified sync height or an applied ChainLock, report a missing key source as Error::NoKeySource, derive every missing index up to the depth instead of only past the highest one, and bump account_generation so a filter batch scanned before the call cannot commit. Co-Authored-By: Claude Opus 5.5 --- .../managed_wallet_info/coinjoin_pools.rs | 59 ++++++++++++++----- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs index db641f84a..42166bc7a 100644 --- a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs +++ b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs @@ -31,11 +31,11 @@ impl ManagedWalletInfo { /// addresses are reported nowhere but in the returned count. The pools stay /// that deep for the life of the wallet state. /// - /// Returns the number of addresses generated. The depth, the wallet, the - /// fresh state and every CoinJoin account's key source are checked before - /// anything changes. A derivation error past that point leaves the pools - /// grown partway (the accounts that grew still get their monitor revision - /// bumped); discard the wallet state then. + /// Returns the number of addresses generated; indices already in a pool are + /// skipped. The depth, the wallet, the fresh state and every CoinJoin + /// account's key source are checked before anything changes. A derivation + /// error past that point leaves the pools grown partway (the revisions are + /// still bumped); discard the wallet state then. pub fn extend_coinjoin_pools(&mut self, wallet: &Wallet, last_index: u32) -> Result { if last_index >= MAX_COINJOIN_POOL_DEPTH { return Err(Error::InvalidParameter(format!( @@ -47,8 +47,10 @@ impl ManagedWalletInfo { "the wallet does not own this wallet info".to_string(), )); } - let synced_past_birth = - self.metadata.last_processed_height > self.metadata.birth_height.saturating_sub(1); + let floor = self.metadata.birth_height.saturating_sub(1); + let synced_past_birth = self.metadata.last_processed_height > floor + || self.metadata.synced_height > floor + || self.metadata.last_applied_chain_lock.is_some(); if synced_past_birth || self.accounts.all_accounts().iter().any(|account| !account.transactions().is_empty()) { @@ -65,21 +67,21 @@ impl ManagedWalletInfo { index: *index, }); if matches!(key_source, KeySource::NoKeySource) { - return Err(Error::InvalidParameter(format!( - "the wallet has no key source for CoinJoin account {index}" - ))); + return Err(Error::NoKeySource); } key_sources.push(key_source); } let mut generated = 0; + let mut outcome = Ok(()); for (account, key_source) in self.accounts.coinjoin_accounts.values_mut().zip(&key_sources) { let mut account_generated = 0; - let mut outcome = Ok(()); 'pools: for pool in account.managed_account_type_mut().address_pools_mut() { - let start = pool.highest_generated.map_or(0, |h| h + 1); - for index in start..=last_index { + for index in 0..=last_index { + if pool.info_at_index(index).is_some() { + continue; + } if let Err(error) = pool.generate_address_at_index(index, key_source, true) { outcome = Err(error); break 'pools; @@ -91,9 +93,16 @@ impl ManagedWalletInfo { account.bump_monitor_revision(); } generated += account_generated; - outcome?; + if outcome.is_err() { + break; + } } - Ok(generated) + if generated > 0 { + // Like an account add: a filter scan snapshot taken before this + // call must not commit (see `rewind_sync_checkpoint_for_new_account`). + self.account_generation += 1; + } + outcome.map(|()| generated) } } @@ -140,7 +149,7 @@ mod tests { let before = highest_generated(managed); assert!(matches!( managed.extend_coinjoin_pools(wallet, DEPTH), - Err(Error::InvalidParameter(_)) + Err(Error::InvalidParameter(_) | Error::NoKeySource) )); assert_eq!(highest_generated(managed), before, "nothing may change on a rejection"); } @@ -172,7 +181,9 @@ mod tests { for (index, expected) in [(DEPTH, DENOM), (DEPTH + 1, 0)] { let (mut wallet, mut managed) = fresh(network); let revision = managed.accounts.coinjoin_accounts[&0].monitor_revision(); + let generation = managed.account_generation(); managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); + assert!(managed.account_generation() > generation); assert!(managed.accounts.coinjoin_accounts[&0].monitor_revision() > revision); let far = coinjoin_address(&wallet, network, internal, index); @@ -218,5 +229,21 @@ mod tests { let (wallet, mut synced) = fresh(Network::Testnet); synced.metadata.last_processed_height = 10; assert_rejected(&mut synced, &wallet); + + let (wallet, mut certified) = fresh(Network::Testnet); + certified.metadata.synced_height = 10; + assert_rejected(&mut certified, &wallet); + } + + #[tokio::test] + async fn extend_rejects_a_wallet_that_already_saw_a_transaction() { + let (mut wallet, mut managed) = fresh(Network::Testnet); + let near = coinjoin_address(&wallet, Network::Testnet, false, 0); + let tx = Transaction::dummy(&near, 0..1, &[DENOM]); + managed + .check_core_transaction(&tx, TransactionContext::Mempool, &mut wallet, true, true) + .await; + assert_eq!(managed.metadata.last_processed_height, 0); + assert_rejected(&mut managed, &wallet); } } From 1b9fba1aa4e6062b6e09efc0af019db801f96897 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Thu, 22 Oct 2026 16:14:52 +0300 Subject: [PATCH 20/24] test(key-wallet): pin the generated count of the CoinJoin pool extension Co-Authored-By: Claude Opus 5.5 --- key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs index 42166bc7a..999d751ef 100644 --- a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs +++ b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs @@ -182,7 +182,8 @@ mod tests { let (mut wallet, mut managed) = fresh(network); let revision = managed.accounts.coinjoin_accounts[&0].monitor_revision(); let generation = managed.account_generation(); - managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); + let generated = managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); + assert_eq!(generated, 2 * (DEPTH + 1 - GAP) as usize); assert!(managed.account_generation() > generation); assert!(managed.accounts.coinjoin_accounts[&0].monitor_revision() > revision); From d4ce586156ace0ef3d6c29734d86ec5132f12e1e Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Thu, 22 Oct 2026 16:21:15 +0300 Subject: [PATCH 21/24] fix(key-wallet): drop the ChainLock freshness signal and check the network dash-spv applies ChainLocks to every wallet whether it scanned or not, so only the heights and recorded transactions prove a scan. Check the network along with the wallet id, correct the bloom filter threshold in the docs and move the per-pool fill into a helper with a single error path. Co-Authored-By: Claude Opus 5.5 --- .../managed_wallet_info/coinjoin_pools.rs | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs index 999d751ef..cb4b10744 100644 --- a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs +++ b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs @@ -7,6 +7,7 @@ use super::ManagedWalletInfo; use crate::account::AccountType; use crate::error::{Error, Result}; +use crate::managed_account::address_pool::AddressPool; use crate::managed_account::managed_account_trait::ManagedAccountTrait; use crate::wallet::Wallet; use crate::KeySource; @@ -18,7 +19,7 @@ use crate::KeySource; /// The only DashSync log at hand reported 10,530 CoinJoin addresses derived /// (both chains together). Deep pools are not free: every extra script is /// matched against every compact filter (about 3% of blocks match falsely at -/// 12,000 per chain), and past about 9,000 scripts in total dash-spv's +/// 12,000 per chain), and past about 18,000 scripts in total dash-spv's /// size-capped mempool bloom filter runs above its target false-positive rate. pub const MAX_COINJOIN_POOL_DEPTH: u32 = 25_000; @@ -42,15 +43,16 @@ impl ManagedWalletInfo { "CoinJoin pool depth {last_index} must be below {MAX_COINJOIN_POOL_DEPTH}" ))); } - if wallet.wallet_id != self.wallet_id { + if wallet.wallet_id != self.wallet_id || wallet.network != self.network { return Err(Error::InvalidParameter( "the wallet does not own this wallet info".to_string(), )); } + // ChainLocks are applied to every wallet whether it scanned or not, so + // only the heights and recorded transactions prove a scan. let floor = self.metadata.birth_height.saturating_sub(1); - let synced_past_birth = self.metadata.last_processed_height > floor - || self.metadata.synced_height > floor - || self.metadata.last_applied_chain_lock.is_some(); + let synced_past_birth = + self.metadata.last_processed_height > floor || self.metadata.synced_height > floor; if synced_past_birth || self.accounts.all_accounts().iter().any(|account| !account.transactions().is_empty()) { @@ -77,18 +79,10 @@ impl ManagedWalletInfo { for (account, key_source) in self.accounts.coinjoin_accounts.values_mut().zip(&key_sources) { let mut account_generated = 0; - 'pools: for pool in account.managed_account_type_mut().address_pools_mut() { - for index in 0..=last_index { - if pool.info_at_index(index).is_some() { - continue; - } - if let Err(error) = pool.generate_address_at_index(index, key_source, true) { - outcome = Err(error); - break 'pools; - } - account_generated += 1; - } - } + outcome = + account.managed_account_type_mut().address_pools_mut().into_iter().try_for_each( + |pool| fill_pool(pool, key_source, last_index, &mut account_generated), + ); if account_generated > 0 { account.bump_monitor_revision(); } @@ -106,10 +100,27 @@ impl ManagedWalletInfo { } } +/// Derive every index of `pool` up to and including `last_index` that it does +/// not hold yet, counting each one in `generated` as it lands. +fn fill_pool( + pool: &mut AddressPool, + key_source: &KeySource, + last_index: u32, + generated: &mut usize, +) -> Result<()> { + for index in 0..=last_index { + if pool.info_at_index(index).is_none() { + pool.generate_address_at_index(index, key_source, true)?; + *generated += 1; + } + } + Ok(()) +} + #[cfg(test)] mod tests { use super::*; - use crate::managed_account::address_pool::{AddressPool, AddressPoolType}; + use crate::managed_account::address_pool::AddressPoolType; use crate::transaction_checking::{BlockInfo, TransactionContext, WalletTransactionChecker}; use crate::wallet::initialization::WalletAccountCreationOptions; use crate::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; From cedb32394e21ac61c9f55347615daad7fd663b73 Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Thu, 22 Oct 2026 16:21:48 +0300 Subject: [PATCH 22/24] test(key-wallet): cover the network check of the CoinJoin pool extension Co-Authored-By: Claude Opus 5.5 --- key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs index cb4b10744..84d9f4e69 100644 --- a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs +++ b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs @@ -242,6 +242,10 @@ mod tests { synced.metadata.last_processed_height = 10; assert_rejected(&mut synced, &wallet); + let (wallet, mut other_network) = fresh(Network::Testnet); + other_network.network = Network::Mainnet; + assert_rejected(&mut other_network, &wallet); + let (wallet, mut certified) = fresh(Network::Testnet); certified.metadata.synced_height = 10; assert_rejected(&mut certified, &wallet); From c22bce566cac9c4fe8eb114d41ab17bc8084df6c Mon Sep 17 00:00:00 2001 From: Roman <51091564+jeanpierreroma@users.noreply.github.com> Date: Thu, 22 Oct 2026 16:27:47 +0300 Subject: [PATCH 23/24] fix(key-wallet): count pruned finalized transactions in the freshness check A ChainLocked transaction's record is pruned in the default build and only its txid is kept, so test tx_count instead of the transaction map. Co-Authored-By: Claude Opus 5.5 --- .../managed_wallet_info/coinjoin_pools.rs | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs index 84d9f4e69..bea8a69f5 100644 --- a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs +++ b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs @@ -54,7 +54,8 @@ impl ManagedWalletInfo { let synced_past_birth = self.metadata.last_processed_height > floor || self.metadata.synced_height > floor; if synced_past_birth - || self.accounts.all_accounts().iter().any(|account| !account.transactions().is_empty()) + // `tx_count` also counts finalized transactions whose records were pruned. + || self.accounts.all_accounts().into_iter().any(|account| account.tx_count() > 0) { return Err(Error::InvalidParameter( "CoinJoin pools can only be extended before the first sync".to_string(), @@ -253,13 +254,18 @@ mod tests { #[tokio::test] async fn extend_rejects_a_wallet_that_already_saw_a_transaction() { - let (mut wallet, mut managed) = fresh(Network::Testnet); - let near = coinjoin_address(&wallet, Network::Testnet, false, 0); - let tx = Transaction::dummy(&near, 0..1, &[DENOM]); - managed - .check_core_transaction(&tx, TransactionContext::Mempool, &mut wallet, true, true) - .await; - assert_eq!(managed.metadata.last_processed_height, 0); - assert_rejected(&mut managed, &wallet); + let chain_locked = TransactionContext::InChainLockedBlock(BlockInfo::new( + 1, + BlockHash::from_byte_array([1; 32]), + 1_650_000_000, + )); + for context in [TransactionContext::Mempool, chain_locked] { + let (mut wallet, mut managed) = fresh(Network::Testnet); + let near = coinjoin_address(&wallet, Network::Testnet, false, 0); + let tx = Transaction::dummy(&near, 0..1, &[DENOM]); + managed.check_core_transaction(&tx, context, &mut wallet, true, true).await; + assert_eq!(managed.metadata.last_processed_height, 0); + assert_rejected(&mut managed, &wallet); + } } } From 17d7b48a37a7f2877bc447429c90267e1c66003a Mon Sep 17 00:00:00 2001 From: Borja Castellano Date: Wed, 28 Oct 2026 06:09:32 -0700 Subject: [PATCH 24/24] refactor(key-wallet): reduce the CoinJoin pool extension to a pool helper Add `AddressPool::generate_up_to` and build `extend_coinjoin_pools` on it, dropping the separate module, the depth cap and the freshness checks. Co-Authored-By: Claude Opus 5.5 --- .../src/managed_account/address_pool.rs | 27 ++ .../managed_wallet_info/coinjoin_pools.rs | 271 ------------------ .../src/wallet/managed_wallet_info/mod.rs | 25 +- 3 files changed, 51 insertions(+), 272 deletions(-) delete mode 100644 key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs diff --git a/key-wallet/src/managed_account/address_pool.rs b/key-wallet/src/managed_account/address_pool.rs index f0b426bc9..2f504fa8b 100644 --- a/key-wallet/src/managed_account/address_pool.rs +++ b/key-wallet/src/managed_account/address_pool.rs @@ -471,6 +471,12 @@ impl AddressPool { Ok(new_addresses) } + /// Generate every address up to and including `index` + pub fn generate_up_to(&mut self, index: u32, key_source: &KeySource) -> Result> { + let next = self.highest_generated.map_or(0, |highest| highest + 1); + self.generate_addresses((index + 1).saturating_sub(next), key_source, true) + } + /// Generate a specific address at an index pub(crate) fn generate_address_at_index( &mut self, @@ -1363,6 +1369,27 @@ mod tests { assert_eq!(pool.addresses.len(), 10); } + #[test] + fn test_generate_up_to() { + let base_path = DerivationPath::from(vec![ChildNumber::from_normal_idx(0).unwrap()]); + let key_source = test_key_source(); + let mut pool = AddressPool::new( + base_path, + AddressPoolType::External, + 5, + Network::Testnet, + &key_source, + ) + .unwrap(); + + assert_eq!(pool.generate_up_to(29, &key_source).unwrap().len(), 25); + assert_eq!(pool.highest_generated, Some(29)); + assert_eq!(pool.addresses.len(), 30); + + assert!(pool.generate_up_to(10, &key_source).unwrap().is_empty()); + assert_eq!(pool.highest_generated, Some(29)); + } + #[test] fn test_address_usage() { let base_path = DerivationPath::from(vec![ChildNumber::from_normal_idx(0).unwrap()]); diff --git a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs b/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs deleted file mode 100644 index bea8a69f5..000000000 --- a/key-wallet/src/wallet/managed_wallet_info/coinjoin_pools.rs +++ /dev/null @@ -1,271 +0,0 @@ -//! Pre-generating CoinJoin address pools past the gap limit. -//! -//! A wallet migrated from DashSync can hold mixed coins on CoinJoin addresses -//! far past the gap limit. Extending its CoinJoin pools to a fixed depth -//! before the first sync from birth lets the regular sync find them. - -use super::ManagedWalletInfo; -use crate::account::AccountType; -use crate::error::{Error, Result}; -use crate::managed_account::address_pool::AddressPool; -use crate::managed_account::managed_account_trait::ManagedAccountTrait; -use crate::wallet::Wallet; -use crate::KeySource; - -/// Upper bound (exclusive) for the `last_index` passed to -/// [`ManagedWalletInfo::extend_coinjoin_pools`], so a bad argument cannot -/// derive for ever. -/// -/// The only DashSync log at hand reported 10,530 CoinJoin addresses derived -/// (both chains together). Deep pools are not free: every extra script is -/// matched against every compact filter (about 3% of blocks match falsely at -/// 12,000 per chain), and past about 18,000 scripts in total dash-spv's -/// size-capped mempool bloom filter runs above its target false-positive rate. -pub const MAX_COINJOIN_POOL_DEPTH: u32 = 25_000; - -impl ManagedWalletInfo { - /// Derive both pools of every CoinJoin account up to and including `last_index`. - /// - /// Only for fresh wallet state, before the first sync from the wallet's - /// birth height and before its addresses are persisted: addresses added - /// after a block was processed do not bring that block back, and the new - /// addresses are reported nowhere but in the returned count. The pools stay - /// that deep for the life of the wallet state. - /// - /// Returns the number of addresses generated; indices already in a pool are - /// skipped. The depth, the wallet, the fresh state and every CoinJoin - /// account's key source are checked before anything changes. A derivation - /// error past that point leaves the pools grown partway (the revisions are - /// still bumped); discard the wallet state then. - pub fn extend_coinjoin_pools(&mut self, wallet: &Wallet, last_index: u32) -> Result { - if last_index >= MAX_COINJOIN_POOL_DEPTH { - return Err(Error::InvalidParameter(format!( - "CoinJoin pool depth {last_index} must be below {MAX_COINJOIN_POOL_DEPTH}" - ))); - } - if wallet.wallet_id != self.wallet_id || wallet.network != self.network { - return Err(Error::InvalidParameter( - "the wallet does not own this wallet info".to_string(), - )); - } - // ChainLocks are applied to every wallet whether it scanned or not, so - // only the heights and recorded transactions prove a scan. - let floor = self.metadata.birth_height.saturating_sub(1); - let synced_past_birth = - self.metadata.last_processed_height > floor || self.metadata.synced_height > floor; - if synced_past_birth - // `tx_count` also counts finalized transactions whose records were pruned. - || self.accounts.all_accounts().into_iter().any(|account| account.tx_count() > 0) - { - return Err(Error::InvalidParameter( - "CoinJoin pools can only be extended before the first sync".to_string(), - )); - } - if self.accounts.coinjoin_accounts.is_empty() { - return Err(Error::InvalidParameter("the wallet has no CoinJoin account".to_string())); - } - let mut key_sources = Vec::with_capacity(self.accounts.coinjoin_accounts.len()); - for index in self.accounts.coinjoin_accounts.keys() { - let key_source = wallet.key_source_for_account(AccountType::CoinJoin { - index: *index, - }); - if matches!(key_source, KeySource::NoKeySource) { - return Err(Error::NoKeySource); - } - key_sources.push(key_source); - } - - let mut generated = 0; - let mut outcome = Ok(()); - for (account, key_source) in self.accounts.coinjoin_accounts.values_mut().zip(&key_sources) - { - let mut account_generated = 0; - outcome = - account.managed_account_type_mut().address_pools_mut().into_iter().try_for_each( - |pool| fill_pool(pool, key_source, last_index, &mut account_generated), - ); - if account_generated > 0 { - account.bump_monitor_revision(); - } - generated += account_generated; - if outcome.is_err() { - break; - } - } - if generated > 0 { - // Like an account add: a filter scan snapshot taken before this - // call must not commit (see `rewind_sync_checkpoint_for_new_account`). - self.account_generation += 1; - } - outcome.map(|()| generated) - } -} - -/// Derive every index of `pool` up to and including `last_index` that it does -/// not hold yet, counting each one in `generated` as it lands. -fn fill_pool( - pool: &mut AddressPool, - key_source: &KeySource, - last_index: u32, - generated: &mut usize, -) -> Result<()> { - for index in 0..=last_index { - if pool.info_at_index(index).is_none() { - pool.generate_address_at_index(index, key_source, true)?; - *generated += 1; - } - } - Ok(()) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::managed_account::address_pool::AddressPoolType; - use crate::transaction_checking::{BlockInfo, TransactionContext, WalletTransactionChecker}; - use crate::wallet::initialization::WalletAccountCreationOptions; - use crate::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; - use crate::Network; - use dashcore::hashes::Hash; - use dashcore::{Address, BlockHash, Transaction}; - - const DENOM: u64 = 100_001; - const GAP: u32 = crate::gap_limit::DEFAULT_COINJOIN_GAP_LIMIT; - /// Far past the gap limit, shallow enough for debug test runs. - const DEPTH: u32 = 2 * GAP + 50; - - fn fresh_with( - network: Network, - options: WalletAccountCreationOptions, - ) -> (Wallet, ManagedWalletInfo) { - let wallet = Wallet::from_seed_bytes([0x01; 64], network, options).expect("wallet"); - let managed = ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0); - (wallet, managed) - } - - fn fresh(network: Network) -> (Wallet, ManagedWalletInfo) { - fresh_with(network, WalletAccountCreationOptions::Default) - } - - fn highest_generated(managed: &ManagedWalletInfo) -> Vec> { - managed - .accounts - .coinjoin_accounts - .values() - .flat_map(|account| account.managed_account_type().address_pools()) - .map(|pool| pool.highest_generated) - .collect() - } - - fn assert_rejected(managed: &mut ManagedWalletInfo, wallet: &Wallet) { - let before = highest_generated(managed); - assert!(matches!( - managed.extend_coinjoin_pools(wallet, DEPTH), - Err(Error::InvalidParameter(_) | Error::NoKeySource) - )); - assert_eq!(highest_generated(managed), before, "nothing may change on a rejection"); - } - - fn coinjoin_address(wallet: &Wallet, network: Network, internal: bool, index: u32) -> Address { - let key_source = wallet.key_source_for_account(AccountType::CoinJoin { - index: 0, - }); - let mut scratch = AddressPool::new_without_generation( - Default::default(), - if internal { - AddressPoolType::Internal - } else { - AddressPoolType::External - }, - GAP, - network, - ); - scratch.generate_address_at_index(index, &key_source, false).expect("derive") - } - - /// 32008: a coin far past the gap limit, in an old ChainLocked block, is - /// credited by the regular check when the pools were extended through its - /// index (inclusive), and missed one index past the depth. - #[tokio::test] - async fn coins_up_to_the_extended_depth_are_found() { - for network in [Network::Mainnet, Network::Testnet] { - for internal in [false, true] { - for (index, expected) in [(DEPTH, DENOM), (DEPTH + 1, 0)] { - let (mut wallet, mut managed) = fresh(network); - let revision = managed.accounts.coinjoin_accounts[&0].monitor_revision(); - let generation = managed.account_generation(); - let generated = managed.extend_coinjoin_pools(&wallet, DEPTH).expect("extend"); - assert_eq!(generated, 2 * (DEPTH + 1 - GAP) as usize); - assert!(managed.account_generation() > generation); - assert!(managed.accounts.coinjoin_accounts[&0].monitor_revision() > revision); - - let far = coinjoin_address(&wallet, network, internal, index); - let tx = Transaction::dummy(&far, 0..1, &[DENOM]); - let context = TransactionContext::InChainLockedBlock(BlockInfo::new( - 1, - BlockHash::from_byte_array([1; 32]), - 1_650_000_000, - )); - let result = - managed.check_core_transaction(&tx, context, &mut wallet, true, true).await; - let case = format!("{network:?} internal={internal} index={index}"); - assert_eq!(result.total_received, expected, "{case}"); - assert_eq!(managed.balance().total(), expected, "{case}"); - } - } - } - } - - #[test] - fn extend_rejects_bad_arguments_and_state_without_changing_anything() { - let (wallet, mut managed) = fresh(Network::Testnet); - for last_index in [MAX_COINJOIN_POOL_DEPTH, u32::MAX] { - assert!(matches!( - managed.extend_coinjoin_pools(&wallet, last_index), - Err(Error::InvalidParameter(_)) - )); - } - - let (other, _) = fresh_with(Network::Mainnet, WalletAccountCreationOptions::Default); - assert_rejected(&mut managed, &other); - - let (no_coinjoin, mut no_coinjoin_info) = - fresh_with(Network::Testnet, WalletAccountCreationOptions::None); - assert!(no_coinjoin_info.accounts.coinjoin_accounts.is_empty()); - assert_rejected(&mut no_coinjoin_info, &no_coinjoin); - - let (wallet, mut missing_key) = fresh(Network::Testnet); - let extra = missing_key.accounts.coinjoin_accounts[&0].clone(); - missing_key.accounts.coinjoin_accounts.insert(1, extra); - assert_rejected(&mut missing_key, &wallet); - - let (wallet, mut synced) = fresh(Network::Testnet); - synced.metadata.last_processed_height = 10; - assert_rejected(&mut synced, &wallet); - - let (wallet, mut other_network) = fresh(Network::Testnet); - other_network.network = Network::Mainnet; - assert_rejected(&mut other_network, &wallet); - - let (wallet, mut certified) = fresh(Network::Testnet); - certified.metadata.synced_height = 10; - assert_rejected(&mut certified, &wallet); - } - - #[tokio::test] - async fn extend_rejects_a_wallet_that_already_saw_a_transaction() { - let chain_locked = TransactionContext::InChainLockedBlock(BlockInfo::new( - 1, - BlockHash::from_byte_array([1; 32]), - 1_650_000_000, - )); - for context in [TransactionContext::Mempool, chain_locked] { - let (mut wallet, mut managed) = fresh(Network::Testnet); - let near = coinjoin_address(&wallet, Network::Testnet, false, 0); - let tx = Transaction::dummy(&near, 0..1, &[DENOM]); - managed.check_core_transaction(&tx, context, &mut wallet, true, true).await; - assert_eq!(managed.metadata.last_processed_height, 0); - assert_rejected(&mut managed, &wallet); - } - } -} diff --git a/key-wallet/src/wallet/managed_wallet_info/mod.rs b/key-wallet/src/wallet/managed_wallet_info/mod.rs index a3315a1d6..4938ebec9 100644 --- a/key-wallet/src/wallet/managed_wallet_info/mod.rs +++ b/key-wallet/src/wallet/managed_wallet_info/mod.rs @@ -5,7 +5,6 @@ pub mod asset_lock_builder; pub mod coin_selection; -pub mod coinjoin_pools; pub mod fee; pub mod helpers; pub use helpers::AbandonOutcome; @@ -615,6 +614,30 @@ impl ManagedWalletInfo { address } + + /// Derive both pools of every CoinJoin account up to and including `last_index`. + /// + /// Call it on fresh wallet state, before the first sync from birth: blocks + /// already processed are not scanned again for the new addresses. + pub fn extend_coinjoin_pools( + &mut self, + wallet: &Wallet, + last_index: u32, + ) -> crate::error::Result<()> { + for (index, account) in self.accounts.coinjoin_accounts.iter_mut() { + let key_source = wallet.key_source_for_account(crate::AccountType::CoinJoin { + index: *index, + }); + + for pool in account.managed_account_type_mut().address_pools_mut() { + pool.generate_up_to(last_index, &key_source)?; + } + + account.bump_monitor_revision(); + } + + Ok(()) + } } /// Re-export types from account module for convenience