From 2f83b4508fe2369c6336c9579517f3a4eb25d923 Mon Sep 17 00:00:00 2001 From: nymius <155548262+nymius@users.noreply.github.com> Date: Thu, 23 Jan 2025 13:59:38 -0300 Subject: [PATCH] test(wallet): check there are no duplicates across required and optional utxos This test replaces the one used to test `coin_selection::filter_duplicates` introduced in 5299db34cb9117ad1b66a6afcb51f6ca7e1f0d95. As the code changed and there is not a single point to verificate the following properties: - there are no duplicates in required utxos - there are no duplicates in optional utxos - there are no duplicates across optional and required utxos anymore, test have been prefixed with `not_duplicated_utxos*` to allow its joint execution by using the following command: cargo test -- not_duplicated_utxos --- crates/wallet/src/wallet/mod.rs | 70 +++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/crates/wallet/src/wallet/mod.rs b/crates/wallet/src/wallet/mod.rs index 2626a583..e4dc6d05 100644 --- a/crates/wallet/src/wallet/mod.rs +++ b/crates/wallet/src/wallet/mod.rs @@ -2559,3 +2559,73 @@ macro_rules! doctest_wallet { wallet }} } + +#[cfg(test)] +mod test { + use super::*; + use crate::test_utils::get_test_tr_single_sig_xprv_and_change_desc; + use crate::test_utils::insert_tx; + + #[test] + fn not_duplicated_utxos_across_optional_and_required() { + let (external_desc, internal_desc) = get_test_tr_single_sig_xprv_and_change_desc(); + + // create new wallet + let mut wallet = Wallet::create(external_desc, internal_desc) + .network(Network::Testnet) + .create_wallet_no_persist() + .unwrap(); + + let two_output_tx = Transaction { + input: vec![], + output: vec![ + TxOut { + script_pubkey: wallet + .next_unused_address(KeychainKind::External) + .script_pubkey(), + value: Amount::from_sat(25_000), + }, + TxOut { + script_pubkey: wallet + .next_unused_address(KeychainKind::External) + .script_pubkey(), + value: Amount::from_sat(75_000), + }, + ], + version: transaction::Version::non_standard(0), + lock_time: absolute::LockTime::ZERO, + }; + + let txid = two_output_tx.compute_txid(); + insert_tx(&mut wallet, two_output_tx); + + let mut params = TxParams::default(); + let output = wallet.get_utxo(OutPoint { txid, vout: 0 }).unwrap(); + params.utxos.insert( + output.outpoint, + WeightedUtxo { + satisfaction_weight: wallet + .public_descriptor(output.keychain) + .max_weight_to_satisfy() + .unwrap(), + utxo: Utxo::Local(output), + }, + ); + // enforce selection of first output in transaction + let received = wallet.filter_utxos(¶ms, wallet.latest_checkpoint().block_id().height); + // notice expected doesn't include the first output from two_output_tx as it should be + // filtered out + let expected = vec![wallet + .get_utxo(OutPoint { txid, vout: 1 }) + .map(|utxo| WeightedUtxo { + satisfaction_weight: wallet + .public_descriptor(utxo.keychain) + .max_weight_to_satisfy() + .unwrap(), + utxo: Utxo::Local(utxo), + }) + .unwrap()]; + + assert_eq!(expected, received); + } +} -- 2.49.0