Skip to content

Commit 9c6ce71

Browse files
committed
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 5299db3. 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
1 parent 0748844 commit 9c6ce71

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

crates/wallet/src/wallet/mod.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,3 +2571,76 @@ macro_rules! doctest_wallet {
25712571
wallet
25722572
}}
25732573
}
2574+
2575+
#[cfg(test)]
2576+
mod test {
2577+
use super::*;
2578+
use crate::test_utils::get_test_tr_single_sig_xprv_and_change_desc;
2579+
use crate::test_utils::insert_anchor;
2580+
use crate::test_utils::insert_tx;
2581+
2582+
#[test]
2583+
fn not_duplicated_utxos_across_optional_and_required() {
2584+
let (external_desc, internal_desc) = get_test_tr_single_sig_xprv_and_change_desc();
2585+
2586+
// create new wallet
2587+
let mut wallet = Wallet::create(external_desc, internal_desc)
2588+
.network(Network::Testnet)
2589+
.create_wallet_no_persist()
2590+
.unwrap();
2591+
2592+
let two_output_tx = Transaction {
2593+
input: vec![],
2594+
output: vec![
2595+
TxOut {
2596+
script_pubkey: wallet
2597+
.next_unused_address(KeychainKind::External)
2598+
.script_pubkey(),
2599+
value: Amount::from_sat(25_000),
2600+
},
2601+
TxOut {
2602+
script_pubkey: wallet
2603+
.next_unused_address(KeychainKind::External)
2604+
.script_pubkey(),
2605+
value: Amount::from_sat(75_000),
2606+
},
2607+
],
2608+
version: transaction::Version::non_standard(0),
2609+
lock_time: absolute::LockTime::ZERO,
2610+
};
2611+
2612+
let txid = two_output_tx.compute_txid();
2613+
insert_tx(&mut wallet, two_output_tx);
2614+
2615+
let expected_anchor = ConfirmationBlockTime {
2616+
block_id: wallet.latest_checkpoint().block_id(),
2617+
confirmation_time: 200,
2618+
};
2619+
2620+
insert_anchor(&mut wallet, txid, expected_anchor);
2621+
2622+
let mut params = TxParams::default();
2623+
wallet
2624+
.get_utxo(OutPoint { txid, vout: 0 })
2625+
.map(|outpoint| {
2626+
params.utxos.insert(outpoint);
2627+
})
2628+
.unwrap();
2629+
// enforce selection of first output in transaction
2630+
let received = wallet.filter_utxos(&params, wallet.latest_checkpoint().block_id().height);
2631+
// notice expected doesn't include the first output from two_output_tx as it should be
2632+
// filtered out
2633+
let expected = vec![wallet
2634+
.get_utxo(OutPoint { txid, vout: 1 })
2635+
.map(|utxo| WeightedUtxo {
2636+
satisfaction_weight: wallet
2637+
.public_descriptor(utxo.keychain)
2638+
.max_weight_to_satisfy()
2639+
.unwrap(),
2640+
utxo: Utxo::Local(utxo),
2641+
})
2642+
.unwrap()];
2643+
2644+
assert_eq!(expected, received);
2645+
}
2646+
}

crates/wallet/src/wallet/tx_builder.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,4 +1087,17 @@ mod test {
10871087
builder.fee_rate(FeeRate::from_sat_per_kwu(feerate + 250));
10881088
let _ = builder.finish().unwrap();
10891089
}
1090+
1091+
#[test]
1092+
fn not_duplicated_utxos_in_required_list() {
1093+
let mut params = TxParams::default();
1094+
let test_utxos = get_test_utxos();
1095+
for _ in 0..3 {
1096+
params.utxos.insert(test_utxos[0].clone());
1097+
}
1098+
assert_eq!(
1099+
vec![test_utxos[0].clone()],
1100+
params.utxos.into_iter().collect::<Vec<_>>()
1101+
);
1102+
}
10901103
}

0 commit comments

Comments
 (0)