@@ -7,14 +7,10 @@ use bdk_chain::{
7
7
} ;
8
8
use bdk_core:: bitcoin:: {
9
9
key:: { Secp256k1 , UntweakedPublicKey } ,
10
- Network ,
10
+ Denomination , Network ,
11
11
} ;
12
12
use bdk_electrum:: BdkElectrumClient ;
13
- use bdk_testenv:: {
14
- anyhow,
15
- bitcoincore_rpc:: { json:: CreateRawTransactionInput , RawTx , RpcApi } ,
16
- TestEnv ,
17
- } ;
13
+ use bdk_testenv:: { anyhow, TestEnv } ;
18
14
use core:: time:: Duration ;
19
15
use electrum_client:: ElectrumApi ;
20
16
use std:: collections:: { BTreeSet , HashMap , HashSet } ;
@@ -88,7 +84,8 @@ pub fn detect_receive_tx_cancel() -> anyhow::Result<()> {
88
84
let client = BdkElectrumClient :: new ( electrum_client) ;
89
85
90
86
let mut graph = IndexedTxGraph :: < ConfirmationBlockTime , _ > :: new ( SpkTxOutIndex :: < ( ) > :: default ( ) ) ;
91
- let ( chain, _) = LocalChain :: from_genesis_hash ( env. bitcoind . client . get_block_hash ( 0 ) ?) ;
87
+ let ( chain, _) =
88
+ LocalChain :: from_genesis_hash ( env. bitcoind . client . get_block_hash ( 0 ) ?. block_hash ( ) ?) ;
92
89
93
90
// Get receiving address.
94
91
let receiver_spk = get_test_spk ( ) ;
@@ -99,11 +96,13 @@ pub fn detect_receive_tx_cancel() -> anyhow::Result<()> {
99
96
100
97
// Select a UTXO to use as an input for constructing our test transactions.
101
98
let selected_utxo = rpc_client
102
- . list_unspent ( None , None , None , Some ( false ) , None ) ?
99
+ . list_unspent ( ) ?
100
+ . 0
103
101
. into_iter ( )
104
102
// Find a block reward tx.
105
- . find ( |utxo| utxo. amount == Amount :: from_int_btc ( 50 ) )
106
- . expect ( "Must find a block reward UTXO" ) ;
103
+ . find ( |utxo| utxo. amount == Amount :: from_int_btc ( 50 ) . to_btc ( ) )
104
+ . expect ( "Must find a block reward UTXO" )
105
+ . into_model ( ) ?;
107
106
108
107
// Derive the sender's address from the selected UTXO.
109
108
let sender_spk = selected_utxo. script_pub_key . clone ( ) ;
@@ -122,9 +121,9 @@ pub fn detect_receive_tx_cancel() -> anyhow::Result<()> {
122
121
receiver_addr. to_string ( ) ,
123
122
selected_utxo. amount - SEND_TX_FEE ,
124
123
) ] ) ;
125
- let send_tx = rpc_client. create_raw_transaction ( & inputs, & send_tx_outputs, None , Some ( true ) ) ?;
124
+ let send_tx = rpc_client. create_raw_transaction ( & inputs, & send_tx_outputs) ?;
126
125
let send_tx = rpc_client
127
- . sign_raw_transaction_with_wallet ( send_tx. raw_hex ( ) , None , None ) ?
126
+ . sign_raw_transaction_with_wallet ( send_tx. raw_hex ( ) ) ?
128
127
. transaction ( ) ?;
129
128
130
129
// Create and sign the `undo_send_tx` transaction. This redirects funds back to the sender
@@ -133,10 +132,9 @@ pub fn detect_receive_tx_cancel() -> anyhow::Result<()> {
133
132
sender_addr. to_string ( ) ,
134
133
selected_utxo. amount - UNDO_SEND_TX_FEE ,
135
134
) ] ) ;
136
- let undo_send_tx =
137
- rpc_client. create_raw_transaction ( & inputs, & undo_send_outputs, None , Some ( true ) ) ?;
135
+ let undo_send_tx = rpc_client. create_raw_transaction ( & inputs, & undo_send_outputs) ?;
138
136
let undo_send_tx = rpc_client
139
- . sign_raw_transaction_with_wallet ( undo_send_tx. raw_hex ( ) , None , None ) ?
137
+ . sign_raw_transaction_with_wallet ( undo_send_tx. raw_hex ( ) ) ?
140
138
. transaction ( ) ?;
141
139
142
140
// Sync after broadcasting the `send_tx`. Ensure that we detect and receive the `send_tx`.
@@ -198,7 +196,7 @@ pub fn chained_mempool_tx_sync() -> anyhow::Result<()> {
198
196
let electrum_client = electrum_client:: Client :: new ( env. electrsd . electrum_url . as_str ( ) ) ?;
199
197
200
198
let tracked_addr = rpc_client
201
- . get_new_address ( None , None ) ?
199
+ . new_address ( ) ?
202
200
. require_network ( Network :: Regtest ) ?;
203
201
204
202
env. mine_blocks ( 100 , None ) ?;
@@ -208,7 +206,8 @@ pub fn chained_mempool_tx_sync() -> anyhow::Result<()> {
208
206
209
207
// Create second unconfirmed tx that spends the first.
210
208
let utxo = rpc_client
211
- . list_unspent ( None , Some ( 0 ) , None , Some ( true ) , None ) ?
209
+ . list_unspent ( ) ?
210
+ . 0
212
211
. into_iter ( )
213
212
. find ( |utxo| utxo. script_pub_key == tracked_addr. script_pubkey ( ) )
214
213
. expect ( "must find the newly created utxo" ) ;
@@ -223,11 +222,9 @@ pub fn chained_mempool_tx_sync() -> anyhow::Result<()> {
223
222
utxo. amount - Amount :: from_sat ( 1000 ) ,
224
223
) ]
225
224
. into ( ) ,
226
- None ,
227
- None ,
228
225
) ?;
229
226
let signed_tx = rpc_client
230
- . sign_raw_transaction_with_wallet ( tx_that_spends_unconfirmed. raw_hex ( ) , None , None ) ?
227
+ . sign_raw_transaction_with_wallet ( tx_that_spends_unconfirmed. raw_hex ( ) ) ?
231
228
. transaction ( ) ?;
232
229
let txid2 = rpc_client. send_raw_transaction ( signed_tx. raw_hex ( ) ) ?;
233
230
@@ -271,26 +268,16 @@ pub fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> {
271
268
] ;
272
269
273
270
let _block_hashes = env. mine_blocks ( 101 , None ) ?;
274
- let txid1 = env. bitcoind . client . send_to_address (
275
- & receive_address1,
276
- Amount :: from_sat ( 10000 ) ,
277
- None ,
278
- None ,
279
- None ,
280
- None ,
281
- Some ( 1 ) ,
282
- None ,
283
- ) ?;
284
- let txid2 = env. bitcoind . client . send_to_address (
285
- & receive_address0,
286
- Amount :: from_sat ( 20000 ) ,
287
- None ,
288
- None ,
289
- None ,
290
- None ,
291
- Some ( 1 ) ,
292
- None ,
293
- ) ?;
271
+ let txid1 = env
272
+ . bitcoind
273
+ . client
274
+ . send_to_address ( & receive_address1, Amount :: from_sat ( 10000 ) ) ?
275
+ . txid ( ) ;
276
+ let txid2 = env
277
+ . bitcoind
278
+ . client
279
+ . send_to_address ( & receive_address0, Amount :: from_sat ( 20000 ) ) ?
280
+ . txid ( ) ;
294
281
env. mine_blocks ( 1 , None ) ?;
295
282
env. wait_until_electrum_sees_block ( Duration :: from_secs ( 6 ) ) ?;
296
283
@@ -337,7 +324,8 @@ pub fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> {
337
324
let tx_fee = env
338
325
. bitcoind
339
326
. client
340
- . get_transaction ( & tx. compute_txid ( ) , None )
327
+ . get_transaction ( & tx. compute_txid ( ) ) ?
328
+ . into
341
329
. expect ( "Tx must exist" )
342
330
. fee
343
331
. expect ( "Fee must exist" )
@@ -393,16 +381,11 @@ pub fn test_update_tx_graph_stop_gap() -> anyhow::Result<()> {
393
381
. collect ( ) ;
394
382
395
383
// Then receive coins on the 4th address.
396
- let txid_4th_addr = env. bitcoind . client . send_to_address (
397
- & addresses[ 3 ] ,
398
- Amount :: from_sat ( 10000 ) ,
399
- None ,
400
- None ,
401
- None ,
402
- None ,
403
- Some ( 1 ) ,
404
- None ,
405
- ) ?;
384
+ let txid_4th_addr = env
385
+ . bitcoind
386
+ . client
387
+ . send_to_address ( & addresses[ 3 ] , Amount :: from_sat ( 10000 ) ) ?
388
+ . txid ( ) ?;
406
389
env. mine_blocks ( 1 , None ) ?;
407
390
env. wait_until_electrum_sees_block ( Duration :: from_secs ( 6 ) ) ?;
408
391
@@ -437,16 +420,11 @@ pub fn test_update_tx_graph_stop_gap() -> anyhow::Result<()> {
437
420
assert_eq ! ( full_scan_update. last_active_indices[ & 0 ] , 3 ) ;
438
421
439
422
// Now receive a coin on the last address.
440
- let txid_last_addr = env. bitcoind . client . send_to_address (
441
- & addresses[ addresses. len ( ) - 1 ] ,
442
- Amount :: from_sat ( 10000 ) ,
443
- None ,
444
- None ,
445
- None ,
446
- None ,
447
- Some ( 1 ) ,
448
- None ,
449
- ) ?;
423
+ let txid_last_addr = env
424
+ . bitcoind
425
+ . client
426
+ . send_to_address ( & addresses[ addresses. len ( ) - 1 ] , Amount :: from_sat ( 10000 ) ) ?
427
+ . txid ( ) ?;
450
428
env. mine_blocks ( 1 , None ) ?;
451
429
env. wait_until_electrum_sees_block ( Duration :: from_secs ( 6 ) ) ?;
452
430
@@ -498,11 +476,7 @@ fn test_sync() -> anyhow::Result<()> {
498
476
let client = BdkElectrumClient :: new ( electrum_client) ;
499
477
500
478
// Setup addresses.
501
- let addr_to_mine = env
502
- . bitcoind
503
- . client
504
- . get_new_address ( None , None ) ?
505
- . assume_checked ( ) ;
479
+ let addr_to_mine = env. bitcoind . client . new_address ( ) ?;
506
480
let spk_to_track = ScriptBuf :: new_p2wsh ( & WScriptHash :: all_zeros ( ) ) ;
507
481
let addr_to_track = Address :: from_script ( & spk_to_track, bdk_chain:: bitcoin:: Network :: Regtest ) ?;
508
482
@@ -610,7 +584,7 @@ fn test_sync() -> anyhow::Result<()> {
610
584
let tx_fee = env
611
585
. bitcoind
612
586
. client
613
- . get_transaction ( & tx. txid , None )
587
+ . get_transaction ( & tx. txid ) ?
614
588
. expect ( "Tx must exist" )
615
589
. fee
616
590
. expect ( "Fee must exist" )
@@ -641,16 +615,13 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
641
615
let client = BdkElectrumClient :: new ( electrum_client) ;
642
616
643
617
// Setup addresses.
644
- let addr_to_mine = env
645
- . bitcoind
646
- . client
647
- . get_new_address ( None , None ) ?
648
- . assume_checked ( ) ;
618
+ let addr_to_mine = env. bitcoind . client . new_address ( ) ?;
649
619
let spk_to_track = ScriptBuf :: new_p2wsh ( & WScriptHash :: all_zeros ( ) ) ;
650
620
let addr_to_track = Address :: from_script ( & spk_to_track, bdk_chain:: bitcoin:: Network :: Regtest ) ?;
651
621
652
622
// Setup receiver.
653
- let ( mut recv_chain, _) = LocalChain :: from_genesis_hash ( env. bitcoind . client . get_block_hash ( 0 ) ?) ;
623
+ let ( mut recv_chain, _) =
624
+ LocalChain :: from_genesis_hash ( env. bitcoind . client . get_block_hash ( 0 ) ?. block_hash ( ) ?) ;
654
625
let mut recv_graph = IndexedTxGraph :: < ConfirmationBlockTime , _ > :: new ( {
655
626
let mut recv_index = SpkTxOutIndex :: default ( ) ;
656
627
recv_index. insert_spk ( ( ) , spk_to_track. clone ( ) ) ;
@@ -737,7 +708,8 @@ fn test_sync_with_coinbase() -> anyhow::Result<()> {
737
708
let addr_to_track = Address :: from_script ( & spk_to_track, bdk_chain:: bitcoin:: Network :: Regtest ) ?;
738
709
739
710
// Setup receiver.
740
- let ( mut recv_chain, _) = LocalChain :: from_genesis_hash ( env. bitcoind . client . get_block_hash ( 0 ) ?) ;
711
+ let ( mut recv_chain, _) =
712
+ LocalChain :: from_genesis_hash ( env. bitcoind . client . get_block_hash ( 0 ) ?. block_hash ( ) ?) ;
741
713
let mut recv_graph = IndexedTxGraph :: < ConfirmationBlockTime , _ > :: new ( {
742
714
let mut recv_index = SpkTxOutIndex :: default ( ) ;
743
715
recv_index. insert_spk ( ( ) , spk_to_track. clone ( ) ) ;
@@ -772,7 +744,8 @@ fn test_check_fee_calculation() -> anyhow::Result<()> {
772
744
let addr_to_track = Address :: from_script ( & spk_to_track, bdk_chain:: bitcoin:: Network :: Regtest ) ?;
773
745
774
746
// Setup receiver.
775
- let ( mut recv_chain, _) = LocalChain :: from_genesis_hash ( env. bitcoind . client . get_block_hash ( 0 ) ?) ;
747
+ let ( mut recv_chain, _) =
748
+ LocalChain :: from_genesis_hash ( env. bitcoind . client . get_block_hash ( 0 ) ?. block_hash ( ) ?) ;
776
749
let mut recv_graph = IndexedTxGraph :: < ConfirmationBlockTime , _ > :: new ( {
777
750
let mut recv_index = SpkTxOutIndex :: default ( ) ;
778
751
recv_index. insert_spk ( ( ) , spk_to_track. clone ( ) ) ;
@@ -784,10 +757,7 @@ fn test_check_fee_calculation() -> anyhow::Result<()> {
784
757
785
758
// Send a preliminary tx such that the new utxo in Core's wallet
786
759
// becomes the input of the next tx
787
- let new_addr = env
788
- . rpc_client ( )
789
- . get_new_address ( None , None ) ?
790
- . assume_checked ( ) ;
760
+ let new_addr = env. rpc_client ( ) . new_address ( ) ?;
791
761
let prev_amt = SEND_AMOUNT + FEE_AMOUNT ;
792
762
env. send ( & new_addr, prev_amt) ?;
793
763
let prev_block_hash = env. mine_blocks ( 1 , None ) ?. into_iter ( ) . next ( ) ;
@@ -864,11 +834,12 @@ fn test_check_fee_calculation() -> anyhow::Result<()> {
864
834
let tx_fee = env
865
835
. bitcoind
866
836
. client
867
- . get_transaction ( & tx. txid , None )
837
+ . get_transaction ( tx. txid )
868
838
. expect ( "Tx must exist" )
869
839
. fee
840
+ . map ( |fee| Amount :: from_float_in ( fee. abs ( ) , Denomination :: BTC ) )
870
841
. expect ( "Fee must exist" )
871
- . abs ( )
842
+ . expect ( "Amount parsing should succeed" )
872
843
. to_sat ( ) as u64 ;
873
844
874
845
// Check that the calculated fee matches the fee from the transaction data.
0 commit comments