@@ -436,14 +436,23 @@ fn test_create_tx_change_policy_no_internal() {
436436 builder. finish ( ) . unwrap ( ) ;
437437}
438438
439+ macro_rules! check_fee {
440+ ( $wallet: expr, $psbt: expr) => { {
441+ let tx = $psbt. clone( ) . extract_tx( ) ;
442+ let tx_fee = $wallet. calculate_fee( & tx) . ok( ) ;
443+ assert_eq!( tx_fee, $psbt. fee_amount( ) ) ;
444+ tx_fee
445+ } } ;
446+ }
447+
439448#[ test]
440449fn test_create_tx_drain_wallet_and_drain_to ( ) {
441450 let ( mut wallet, _) = get_funded_wallet ( get_test_wpkh ( ) ) ;
442451 let addr = wallet. get_address ( New ) ;
443452 let mut builder = wallet. build_tx ( ) ;
444453 builder. drain_to ( addr. script_pubkey ( ) ) . drain_wallet ( ) ;
445454 let psbt = builder. finish ( ) . unwrap ( ) ;
446- let fee = psbt . fee_amount ( ) ;
455+ let fee = check_fee ! ( wallet , psbt ) ;
447456
448457 assert_eq ! ( psbt. unsigned_tx. output. len( ) , 1 ) ;
449458 assert_eq ! ( psbt. unsigned_tx. output[ 0 ] . value, 50_000 - fee. unwrap_or( 0 ) ) ;
@@ -462,7 +471,7 @@ fn test_create_tx_drain_wallet_and_drain_to_and_with_recipient() {
462471 . drain_to ( drain_addr. script_pubkey ( ) )
463472 . drain_wallet ( ) ;
464473 let psbt = builder. finish ( ) . unwrap ( ) ;
465- let fee = psbt . fee_amount ( ) ;
474+ let fee = check_fee ! ( wallet , psbt ) ;
466475 let outputs = psbt. unsigned_tx . output ;
467476
468477 assert_eq ! ( outputs. len( ) , 2 ) ;
@@ -489,7 +498,7 @@ fn test_create_tx_drain_to_and_utxos() {
489498 . add_utxos ( & utxos)
490499 . unwrap ( ) ;
491500 let psbt = builder. finish ( ) . unwrap ( ) ;
492- let fee = psbt . fee_amount ( ) ;
501+ let fee = check_fee ! ( wallet , psbt ) ;
493502
494503 assert_eq ! ( psbt. unsigned_tx. output. len( ) , 1 ) ;
495504 assert_eq ! ( psbt. unsigned_tx. output[ 0 ] . value, 50_000 - fee. unwrap_or( 0 ) ) ;
@@ -512,7 +521,7 @@ fn test_create_tx_default_fee_rate() {
512521 let mut builder = wallet. build_tx ( ) ;
513522 builder. add_recipient ( addr. script_pubkey ( ) , 25_000 ) ;
514523 let psbt = builder. finish ( ) . unwrap ( ) ;
515- let fee = psbt . fee_amount ( ) ;
524+ let fee = check_fee ! ( wallet , psbt ) ;
516525
517526 assert_fee_rate ! ( psbt, fee. unwrap_or( 0 ) , FeeRate :: default ( ) , @add_signature) ;
518527}
@@ -526,7 +535,7 @@ fn test_create_tx_custom_fee_rate() {
526535 . add_recipient ( addr. script_pubkey ( ) , 25_000 )
527536 . fee_rate ( FeeRate :: from_sat_per_vb ( 5.0 ) ) ;
528537 let psbt = builder. finish ( ) . unwrap ( ) ;
529- let fee = psbt . fee_amount ( ) ;
538+ let fee = check_fee ! ( wallet , psbt ) ;
530539
531540 assert_fee_rate ! ( psbt, fee. unwrap_or( 0 ) , FeeRate :: from_sat_per_vb( 5.0 ) , @add_signature) ;
532541}
@@ -541,7 +550,7 @@ fn test_create_tx_absolute_fee() {
541550 . drain_wallet ( )
542551 . fee_absolute ( 100 ) ;
543552 let psbt = builder. finish ( ) . unwrap ( ) ;
544- let fee = psbt . fee_amount ( ) ;
553+ let fee = check_fee ! ( wallet , psbt ) ;
545554
546555 assert_eq ! ( fee. unwrap_or( 0 ) , 100 ) ;
547556 assert_eq ! ( psbt. unsigned_tx. output. len( ) , 1 ) ;
@@ -558,7 +567,7 @@ fn test_create_tx_absolute_zero_fee() {
558567 . drain_wallet ( )
559568 . fee_absolute ( 0 ) ;
560569 let psbt = builder. finish ( ) . unwrap ( ) ;
561- let fee = psbt . fee_amount ( ) ;
570+ let fee = check_fee ! ( wallet , psbt ) ;
562571
563572 assert_eq ! ( fee. unwrap_or( 0 ) , 0 ) ;
564573 assert_eq ! ( psbt. unsigned_tx. output. len( ) , 1 ) ;
@@ -589,7 +598,7 @@ fn test_create_tx_add_change() {
589598 . add_recipient ( addr. script_pubkey ( ) , 25_000 )
590599 . ordering ( TxOrdering :: Untouched ) ;
591600 let psbt = builder. finish ( ) . unwrap ( ) ;
592- let fee = psbt . fee_amount ( ) ;
601+ let fee = check_fee ! ( wallet , psbt ) ;
593602
594603 assert_eq ! ( psbt. unsigned_tx. output. len( ) , 2 ) ;
595604 assert_eq ! ( psbt. unsigned_tx. output[ 0 ] . value, 25_000 ) ;
@@ -603,7 +612,7 @@ fn test_create_tx_skip_change_dust() {
603612 let mut builder = wallet. build_tx ( ) ;
604613 builder. add_recipient ( addr. script_pubkey ( ) , 49_800 ) ;
605614 let psbt = builder. finish ( ) . unwrap ( ) ;
606- let fee = psbt . fee_amount ( ) ;
615+ let fee = check_fee ! ( wallet , psbt ) ;
607616
608617 assert_eq ! ( psbt. unsigned_tx. output. len( ) , 1 ) ;
609618 assert_eq ! ( psbt. unsigned_tx. output[ 0 ] . value, 49_800 ) ;
@@ -634,7 +643,7 @@ fn test_create_tx_ordering_respected() {
634643 . add_recipient ( addr. script_pubkey ( ) , 10_000 )
635644 . ordering ( bdk:: wallet:: tx_builder:: TxOrdering :: Bip69Lexicographic ) ;
636645 let psbt = builder. finish ( ) . unwrap ( ) ;
637- let fee = psbt . fee_amount ( ) ;
646+ let fee = check_fee ! ( wallet , psbt ) ;
638647
639648 assert_eq ! ( psbt. unsigned_tx. output. len( ) , 3 ) ;
640649 assert_eq ! ( psbt. unsigned_tx. output[ 0 ] . value, 10_000 - fee. unwrap_or( 0 ) ) ;
@@ -1050,7 +1059,8 @@ fn test_add_foreign_utxo() {
10501059 . add_foreign_utxo ( utxo. outpoint , psbt_input, foreign_utxo_satisfaction)
10511060 . unwrap ( ) ;
10521061 let mut psbt = builder. finish ( ) . unwrap ( ) ;
1053- let fee = psbt. fee_amount ( ) ;
1062+ wallet1. insert_txout ( utxo. outpoint , utxo. txout ) ;
1063+ let fee = check_fee ! ( wallet1, psbt) ;
10541064 let sent_received = wallet1. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
10551065
10561066 assert_eq ! (
@@ -1129,42 +1139,6 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
11291139 wallet1. calculate_fee ( & tx) . unwrap ( ) ;
11301140}
11311141
1132- #[ test]
1133- fn test_calculate_fee_with_inserted_foreign_utxo ( ) {
1134- let ( mut wallet1, _) = get_funded_wallet ( get_test_wpkh ( ) ) ;
1135- let ( wallet2, _) =
1136- get_funded_wallet ( "wpkh(cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm)" ) ;
1137-
1138- let addr = Address :: from_str ( "2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX" )
1139- . unwrap ( )
1140- . assume_checked ( ) ;
1141- let utxo = wallet2. list_unspent ( ) . next ( ) . expect ( "must take!" ) ;
1142- #[ allow( deprecated) ]
1143- let foreign_utxo_satisfaction = wallet2
1144- . get_descriptor_for_keychain ( KeychainKind :: External )
1145- . max_satisfaction_weight ( )
1146- . unwrap ( ) ;
1147-
1148- let psbt_input = psbt:: Input {
1149- witness_utxo : Some ( utxo. txout . clone ( ) ) ,
1150- ..Default :: default ( )
1151- } ;
1152-
1153- let mut builder = wallet1. build_tx ( ) ;
1154- builder
1155- . add_recipient ( addr. script_pubkey ( ) , 60_000 )
1156- . only_witness_utxo ( )
1157- . add_foreign_utxo ( utxo. outpoint , psbt_input, foreign_utxo_satisfaction)
1158- . unwrap ( ) ;
1159- let psbt = builder. finish ( ) . unwrap ( ) ;
1160- let psbt_fee = psbt. fee_amount ( ) . expect ( "psbt fee" ) ;
1161- let tx = psbt. extract_tx ( ) ;
1162-
1163- wallet1. insert_txout ( utxo. outpoint , utxo. txout ) ;
1164- let wallet1_fee = wallet1. calculate_fee ( & tx) . expect ( "wallet fee" ) ;
1165- assert_eq ! ( psbt_fee, wallet1_fee) ;
1166- }
1167-
11681142#[ test]
11691143#[ should_panic( expected = "Generic(\" Foreign utxo missing witness_utxo or non_witness_utxo\" )" ) ]
11701144fn test_add_foreign_utxo_invalid_psbt_input ( ) {
@@ -1463,7 +1437,7 @@ fn test_bump_fee_reduce_change() {
14631437 . enable_rbf ( ) ;
14641438 let psbt = builder. finish ( ) . unwrap ( ) ;
14651439 let original_sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
1466- let original_fee = psbt . fee_amount ( ) ;
1440+ let original_fee = check_fee ! ( wallet , psbt ) ;
14671441
14681442 let tx = psbt. extract_tx ( ) ;
14691443 let txid = tx. txid ( ) ;
@@ -1475,7 +1449,7 @@ fn test_bump_fee_reduce_change() {
14751449 builder. fee_rate ( FeeRate :: from_sat_per_vb ( 2.5 ) ) . enable_rbf ( ) ;
14761450 let psbt = builder. finish ( ) . unwrap ( ) ;
14771451 let sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
1478- let fee = psbt . fee_amount ( ) ;
1452+ let fee = check_fee ! ( wallet , psbt ) ;
14791453
14801454 assert_eq ! ( sent_received. 0 , original_sent_received. 0 ) ;
14811455 assert_eq ! (
@@ -1510,7 +1484,7 @@ fn test_bump_fee_reduce_change() {
15101484 builder. enable_rbf ( ) ;
15111485 let psbt = builder. finish ( ) . unwrap ( ) ;
15121486 let sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
1513- let fee = psbt . fee_amount ( ) ;
1487+ let fee = check_fee ! ( wallet , psbt ) ;
15141488
15151489 assert_eq ! ( sent_received. 0 , original_sent_received. 0 ) ;
15161490 assert_eq ! (
@@ -1560,7 +1534,7 @@ fn test_bump_fee_reduce_single_recipient() {
15601534 let psbt = builder. finish ( ) . unwrap ( ) ;
15611535 let tx = psbt. clone ( ) . extract_tx ( ) ;
15621536 let original_sent_received = wallet. sent_and_received ( & tx) ;
1563- let original_fee = psbt . fee_amount ( ) ;
1537+ let original_fee = check_fee ! ( wallet , psbt ) ;
15641538 let txid = tx. txid ( ) ;
15651539 wallet
15661540 . insert_tx ( tx, ConfirmationTime :: Unconfirmed { last_seen : 0 } )
@@ -1573,7 +1547,7 @@ fn test_bump_fee_reduce_single_recipient() {
15731547 . unwrap ( ) ;
15741548 let psbt = builder. finish ( ) . unwrap ( ) ;
15751549 let sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
1576- let fee = psbt . fee_amount ( ) ;
1550+ let fee = check_fee ! ( wallet , psbt ) ;
15771551
15781552 assert_eq ! ( sent_received. 0 , original_sent_received. 0 ) ;
15791553 assert ! ( fee. unwrap_or( 0 ) > original_fee. unwrap_or( 0 ) ) ;
@@ -1597,7 +1571,7 @@ fn test_bump_fee_absolute_reduce_single_recipient() {
15971571 . drain_wallet ( )
15981572 . enable_rbf ( ) ;
15991573 let psbt = builder. finish ( ) . unwrap ( ) ;
1600- let original_fee = psbt . fee_amount ( ) ;
1574+ let original_fee = check_fee ! ( wallet , psbt ) ;
16011575 let tx = psbt. extract_tx ( ) ;
16021576 let original_sent_received = wallet. sent_and_received ( & tx) ;
16031577 let txid = tx. txid ( ) ;
@@ -1613,7 +1587,7 @@ fn test_bump_fee_absolute_reduce_single_recipient() {
16131587 let psbt = builder. finish ( ) . unwrap ( ) ;
16141588 let tx = & psbt. unsigned_tx ;
16151589 let sent_received = wallet. sent_and_received ( tx) ;
1616- let fee = psbt . fee_amount ( ) ;
1590+ let fee = check_fee ! ( wallet , psbt ) ;
16171591
16181592 assert_eq ! ( sent_received. 0 , original_sent_received. 0 ) ;
16191593 assert ! ( fee. unwrap_or( 0 ) > original_fee. unwrap_or( 0 ) ) ;
@@ -1784,7 +1758,7 @@ fn test_bump_fee_add_input() {
17841758 builder. fee_rate ( FeeRate :: from_sat_per_vb ( 50.0 ) ) ;
17851759 let psbt = builder. finish ( ) . unwrap ( ) ;
17861760 let sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
1787- let fee = psbt . fee_amount ( ) ;
1761+ let fee = check_fee ! ( wallet , psbt ) ;
17881762 assert_eq ! ( sent_received. 0 , original_details. 0 + 25_000 ) ;
17891763 assert_eq ! ( fee. unwrap_or( 0 ) + sent_received. 1 , 30_000 ) ;
17901764
@@ -1834,7 +1808,7 @@ fn test_bump_fee_absolute_add_input() {
18341808 builder. fee_absolute ( 6_000 ) ;
18351809 let psbt = builder. finish ( ) . unwrap ( ) ;
18361810 let sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
1837- let fee = psbt . fee_amount ( ) ;
1811+ let fee = check_fee ! ( wallet , psbt ) ;
18381812
18391813 assert_eq ! ( sent_received. 0 , original_sent_received. 0 + 25_000 ) ;
18401814 assert_eq ! ( fee. unwrap_or( 0 ) + sent_received. 1 , 30_000 ) ;
@@ -1880,7 +1854,7 @@ fn test_bump_fee_no_change_add_input_and_change() {
18801854 . enable_rbf ( ) ;
18811855 let psbt = builder. finish ( ) . unwrap ( ) ;
18821856 let original_sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
1883- let original_fee = psbt . fee_amount ( ) ;
1857+ let original_fee = check_fee ! ( wallet , psbt ) ;
18841858
18851859 let tx = psbt. extract_tx ( ) ;
18861860 let txid = tx. txid ( ) ;
@@ -1894,7 +1868,7 @@ fn test_bump_fee_no_change_add_input_and_change() {
18941868 builder. fee_rate ( FeeRate :: from_sat_per_vb ( 50.0 ) ) ;
18951869 let psbt = builder. finish ( ) . unwrap ( ) ;
18961870 let sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
1897- let fee = psbt . fee_amount ( ) ;
1871+ let fee = check_fee ! ( wallet , psbt ) ;
18981872
18991873 let original_send_all_amount = original_sent_received. 0 - original_fee. unwrap_or ( 0 ) ;
19001874 assert_eq ! ( sent_received. 0 , original_sent_received. 0 + 50_000 ) ;
@@ -1939,7 +1913,7 @@ fn test_bump_fee_add_input_change_dust() {
19391913 . enable_rbf ( ) ;
19401914 let psbt = builder. finish ( ) . unwrap ( ) ;
19411915 let original_sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
1942- let original_fee = psbt . fee_amount ( ) ;
1916+ let original_fee = check_fee ! ( wallet , psbt ) ;
19431917
19441918 let mut tx = psbt. extract_tx ( ) ;
19451919 for txin in & mut tx. input {
@@ -1971,7 +1945,7 @@ fn test_bump_fee_add_input_change_dust() {
19711945 builder. fee_rate ( FeeRate :: from_wu ( fee_abs, new_tx_weight) ) ;
19721946 let psbt = builder. finish ( ) . unwrap ( ) ;
19731947 let sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
1974- let fee = psbt . fee_amount ( ) ;
1948+ let fee = check_fee ! ( wallet , psbt ) ;
19751949
19761950 assert_eq ! ( original_sent_received. 1 , 5_000 - original_fee. unwrap_or( 0 ) ) ;
19771951
@@ -2025,7 +1999,7 @@ fn test_bump_fee_force_add_input() {
20251999 . fee_rate ( FeeRate :: from_sat_per_vb ( 5.0 ) ) ;
20262000 let psbt = builder. finish ( ) . unwrap ( ) ;
20272001 let sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
2028- let fee = psbt . fee_amount ( ) ;
2002+ let fee = check_fee ! ( wallet , psbt ) ;
20292003
20302004 assert_eq ! ( sent_received. 0 , original_sent_received. 0 + 25_000 ) ;
20312005 assert_eq ! ( fee. unwrap_or( 0 ) + sent_received. 1 , 30_000 ) ;
@@ -2083,7 +2057,7 @@ fn test_bump_fee_absolute_force_add_input() {
20832057 builder. add_utxo ( incoming_op) . unwrap ( ) . fee_absolute ( 250 ) ;
20842058 let psbt = builder. finish ( ) . unwrap ( ) ;
20852059 let sent_received = wallet. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
2086- let fee = psbt . fee_amount ( ) ;
2060+ let fee = check_fee ! ( wallet , psbt ) ;
20872061
20882062 assert_eq ! ( sent_received. 0 , original_sent_received. 0 + 25_000 ) ;
20892063 assert_eq ! ( fee. unwrap_or( 0 ) + sent_received. 1 , 30_000 ) ;
@@ -2212,7 +2186,7 @@ fn test_fee_amount_negative_drain_val() {
22122186 . enable_rbf ( )
22132187 . fee_rate ( fee_rate) ;
22142188 let psbt = builder. finish ( ) . unwrap ( ) ;
2215- let fee = psbt . fee_amount ( ) ;
2189+ let fee = check_fee ! ( wallet , psbt ) ;
22162190
22172191 assert_eq ! ( psbt. inputs. len( ) , 1 ) ;
22182192 assert_fee_rate ! ( psbt, fee. unwrap_or( 0 ) , fee_rate, @add_signature) ;
@@ -2951,7 +2925,8 @@ fn test_taproot_foreign_utxo() {
29512925 . unwrap ( ) ;
29522926 let psbt = builder. finish ( ) . unwrap ( ) ;
29532927 let sent_received = wallet1. sent_and_received ( & psbt. clone ( ) . extract_tx ( ) ) ;
2954- let fee = psbt. fee_amount ( ) ;
2928+ wallet1. insert_txout ( utxo. outpoint , utxo. txout ) ;
2929+ let fee = check_fee ! ( wallet1, psbt) ;
29552930
29562931 assert_eq ! (
29572932 sent_received. 0 - sent_received. 1 ,
@@ -3403,7 +3378,7 @@ fn test_fee_rate_sign_no_grinding_high_r() {
34033378 . fee_rate ( fee_rate)
34043379 . add_data ( & data) ;
34053380 let mut psbt = builder. finish ( ) . unwrap ( ) ;
3406- let fee = psbt . fee_amount ( ) ;
3381+ let fee = check_fee ! ( wallet , psbt ) ;
34073382 let ( op_return_vout, _) = psbt
34083383 . unsigned_tx
34093384 . output
@@ -3467,7 +3442,7 @@ fn test_fee_rate_sign_grinding_low_r() {
34673442 . drain_wallet ( )
34683443 . fee_rate ( fee_rate) ;
34693444 let mut psbt = builder. finish ( ) . unwrap ( ) ;
3470- let fee = psbt . fee_amount ( ) ;
3445+ let fee = check_fee ! ( wallet , psbt ) ;
34713446
34723447 wallet
34733448 . sign (
0 commit comments