Skip to content

Commit 0b5d927

Browse files
committed
test(wallet): Add test_tx_ordering_untouched_preserves_insertion_ordering_bnb_success
The test is set up in such a way that BnB can find a solution and demonstrates that `calculate_cs_result` correctly places required UTXOs before selected ones.
1 parent e020391 commit 0b5d927

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

tests/wallet.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,3 +2961,55 @@ fn test_tx_ordering_untouched_preserves_insertion_ordering() {
29612961
// Check vout is sorted by recipient insertion order
29622962
assert!(txouts == vec![400, 300, 500]);
29632963
}
2964+
2965+
// BnB coin selection should find a solution using the optional UTXO.
2966+
// This demonstrates that `calculate_cs_result` correctly orders required UTXOs before selected
2967+
// ones.
2968+
#[test]
2969+
fn test_tx_ordering_untouched_preserves_insertion_ordering_bnb_success() {
2970+
// Create empty wallet
2971+
let (desc, change_desc) = get_test_wpkh_and_change_desc();
2972+
let mut wallet = Wallet::create(desc, change_desc)
2973+
.network(bdk_wallet::bitcoin::Network::Regtest)
2974+
.create_wallet_no_persist()
2975+
.unwrap();
2976+
2977+
// Set up UTXOs with specific values so BnB can find an exact match (avoiding change).
2978+
// - outpoint_0 (required): 35,000 sat - not enough alone
2979+
// - outpoint_1 (optional): 25,200 sat
2980+
// - Target: 60,000 sat
2981+
// - Expected fee: 200 sat
2982+
2983+
let outpoint_0 = receive_output(
2984+
&mut wallet,
2985+
Amount::from_sat(35_000),
2986+
ReceiveTo::Mempool(50),
2987+
);
2988+
let outpoint_1 = receive_output(
2989+
&mut wallet,
2990+
Amount::from_sat(25_200),
2991+
ReceiveTo::Mempool(100),
2992+
);
2993+
2994+
let send_to = wallet.next_unused_address(KeychainKind::External).address;
2995+
let mut tx_builder = wallet.build_tx();
2996+
tx_builder
2997+
.add_utxo(outpoint_0)
2998+
.unwrap()
2999+
.add_recipient(send_to.script_pubkey(), Amount::from_sat(60_000))
3000+
.fee_rate(FeeRate::from_sat_per_vb(1).unwrap())
3001+
.ordering(bdk_wallet::TxOrdering::Untouched);
3002+
let psbt = tx_builder.finish().unwrap();
3003+
3004+
// Verify that both UTXOs are selected in the correct order:
3005+
// required (outpoint_0) should appear before optional (outpoint_1)
3006+
assert_eq!(
3007+
psbt.unsigned_tx
3008+
.input
3009+
.iter()
3010+
.map(|txin| txin.previous_output)
3011+
.collect::<Vec<_>>(),
3012+
vec![outpoint_0, outpoint_1],
3013+
"UTXOs should be ordered with required first, then selected"
3014+
);
3015+
}

0 commit comments

Comments
 (0)