Skip to content

Commit b662030

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 80ee89c commit b662030

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

crates/wallet/src/wallet/mod.rs

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

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)