Skip to content

Commit ef27736

Browse files
committed
wip(fix(tests)): on bdk_esplora, bdk_electrum and bdk_bitcoind_rpc
1 parent d835d4d commit ef27736

File tree

4 files changed

+83
-120
lines changed

4 files changed

+83
-120
lines changed

crates/electrum/tests/test_electrum.rs

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ use bdk_chain::{
77
};
88
use bdk_core::bitcoin::{
99
key::{Secp256k1, UntweakedPublicKey},
10-
Network,
10+
Denomination, Network,
1111
};
1212
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};
1814
use core::time::Duration;
1915
use electrum_client::ElectrumApi;
2016
use std::collections::{BTreeSet, HashMap, HashSet};
@@ -88,7 +84,8 @@ pub fn detect_receive_tx_cancel() -> anyhow::Result<()> {
8884
let client = BdkElectrumClient::new(electrum_client);
8985

9086
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()?);
9289

9390
// Get receiving address.
9491
let receiver_spk = get_test_spk();
@@ -198,7 +195,7 @@ pub fn chained_mempool_tx_sync() -> anyhow::Result<()> {
198195
let electrum_client = electrum_client::Client::new(env.electrsd.electrum_url.as_str())?;
199196

200197
let tracked_addr = rpc_client
201-
.get_new_address(None, None)?
198+
.new_address()?
202199
.require_network(Network::Regtest)?;
203200

204201
env.mine_blocks(100, None)?;
@@ -271,26 +268,16 @@ pub fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> {
271268
];
272269

273270
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();
294281
env.mine_blocks(1, None)?;
295282
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
296283

@@ -337,7 +324,8 @@ pub fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> {
337324
let tx_fee = env
338325
.bitcoind
339326
.client
340-
.get_transaction(&tx.compute_txid(), None)
327+
.get_transaction(&tx.compute_txid())?
328+
.into
341329
.expect("Tx must exist")
342330
.fee
343331
.expect("Fee must exist")
@@ -393,16 +381,11 @@ pub fn test_update_tx_graph_stop_gap() -> anyhow::Result<()> {
393381
.collect();
394382

395383
// 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()?;
406389
env.mine_blocks(1, None)?;
407390
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
408391

@@ -437,16 +420,11 @@ pub fn test_update_tx_graph_stop_gap() -> anyhow::Result<()> {
437420
assert_eq!(full_scan_update.last_active_indices[&0], 3);
438421

439422
// 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()?;
450428
env.mine_blocks(1, None)?;
451429
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
452430

@@ -498,11 +476,7 @@ fn test_sync() -> anyhow::Result<()> {
498476
let client = BdkElectrumClient::new(electrum_client);
499477

500478
// 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()?;
506480
let spk_to_track = ScriptBuf::new_p2wsh(&WScriptHash::all_zeros());
507481
let addr_to_track = Address::from_script(&spk_to_track, bdk_chain::bitcoin::Network::Regtest)?;
508482

@@ -610,7 +584,7 @@ fn test_sync() -> anyhow::Result<()> {
610584
let tx_fee = env
611585
.bitcoind
612586
.client
613-
.get_transaction(&tx.txid, None)
587+
.get_transaction(&tx.txid)?
614588
.expect("Tx must exist")
615589
.fee
616590
.expect("Fee must exist")
@@ -641,11 +615,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
641615
let client = BdkElectrumClient::new(electrum_client);
642616

643617
// 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()?;
649619
let spk_to_track = ScriptBuf::new_p2wsh(&WScriptHash::all_zeros());
650620
let addr_to_track = Address::from_script(&spk_to_track, bdk_chain::bitcoin::Network::Regtest)?;
651621

@@ -784,10 +754,7 @@ fn test_check_fee_calculation() -> anyhow::Result<()> {
784754

785755
// Send a preliminary tx such that the new utxo in Core's wallet
786756
// becomes the input of the next tx
787-
let new_addr = env
788-
.rpc_client()
789-
.get_new_address(None, None)?
790-
.assume_checked();
757+
let new_addr = env.rpc_client().new_address()?;
791758
let prev_amt = SEND_AMOUNT + FEE_AMOUNT;
792759
env.send(&new_addr, prev_amt)?;
793760
let prev_block_hash = env.mine_blocks(1, None)?.into_iter().next();
@@ -864,11 +831,12 @@ fn test_check_fee_calculation() -> anyhow::Result<()> {
864831
let tx_fee = env
865832
.bitcoind
866833
.client
867-
.get_transaction(&tx.txid, None)
834+
.get_transaction(tx.txid)
868835
.expect("Tx must exist")
869836
.fee
837+
.map(|fee| Amount::from_float_in(fee.abs(), Denomination::BTC))
870838
.expect("Fee must exist")
871-
.abs()
839+
.expect("Amount parsing should succeed")
872840
.to_sat() as u64;
873841

874842
// Check that the calculated fee matches the fee from the transaction data.

crates/esplora/src/async_ext.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ mod test {
546546
BlockId,
547547
};
548548
use bdk_core::ConfirmationBlockTime;
549-
use bdk_testenv::{anyhow, bitcoincore_rpc::RpcApi, TestEnv};
549+
use bdk_testenv::{anyhow, TestEnv};
550550
use esplora_client::Builder;
551551

552552
use crate::async_ext::{chain_update, fetch_latest_blocks};
@@ -626,7 +626,11 @@ mod test {
626626
ConfirmationBlockTime {
627627
block_id: BlockId {
628628
height,
629-
hash: env.bitcoind.client.get_block_hash(height as _)?,
629+
hash: env
630+
.bitcoind
631+
.client
632+
.get_block_hash(height as _)?
633+
.block_hash()?,
630634
},
631635
confirmation_time: height as _,
632636
},
@@ -666,7 +670,11 @@ mod test {
666670
ConfirmationBlockTime {
667671
block_id: BlockId {
668672
height,
669-
hash: env.bitcoind.client.get_block_hash(height as _)?,
673+
hash: env
674+
.bitcoind
675+
.client
676+
.get_block_hash(height as _)?
677+
.block_hash()?,
670678
},
671679
confirmation_time: height as _,
672680
},

crates/esplora/src/blocking_ext.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ mod test {
503503
use bdk_chain::local_chain::LocalChain;
504504
use bdk_chain::BlockId;
505505
use bdk_core::ConfirmationBlockTime;
506-
use bdk_testenv::{anyhow, bitcoincore_rpc::RpcApi, TestEnv};
506+
use bdk_testenv::{anyhow, TestEnv};
507507
use esplora_client::{BlockHash, Builder};
508508
use std::collections::{BTreeMap, BTreeSet};
509509
use std::time::Duration;
@@ -591,7 +591,11 @@ mod test {
591591
ConfirmationBlockTime {
592592
block_id: BlockId {
593593
height,
594-
hash: env.bitcoind.client.get_block_hash(height as _)?,
594+
hash: env
595+
.bitcoind
596+
.client
597+
.get_block_hash(height as _)?
598+
.block_hash()?,
595599
},
596600
confirmation_time: height as _,
597601
},
@@ -630,7 +634,11 @@ mod test {
630634
ConfirmationBlockTime {
631635
block_id: BlockId {
632636
height,
633-
hash: env.bitcoind.client.get_block_hash(height as _)?,
637+
hash: env
638+
.bitcoind
639+
.client
640+
.get_block_hash(height as _)?
641+
.block_hash()?,
634642
},
635643
confirmation_time: height as _,
636644
},
@@ -693,10 +701,10 @@ mod test {
693701
let env = TestEnv::new()?;
694702
let blocks = {
695703
let bitcoind_client = &env.bitcoind.client;
696-
assert_eq!(bitcoind_client.get_block_count()?, 1);
704+
assert_eq!(bitcoind_client.get_block_count()?.0, 1);
697705
[
698-
(0, bitcoind_client.get_block_hash(0)?),
699-
(1, bitcoind_client.get_block_hash(1)?),
706+
(0, bitcoind_client.get_block_hash(0)?.block_hash()?),
707+
(1, bitcoind_client.get_block_hash(1)?.block_hash()?),
700708
]
701709
.into_iter()
702710
.chain((2..).zip(env.mine_blocks((TIP_HEIGHT - 1) as usize, None)?))

crates/esplora/tests/async_ext.rs

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use bdk_chain::spk_client::{FullScanRequest, SyncRequest};
44
use bdk_chain::spk_txout::SpkTxOutIndex;
55
use bdk_chain::{ConfirmationBlockTime, IndexedTxGraph, TxGraph};
66
use bdk_esplora::EsploraAsyncExt;
7-
use bdk_testenv::bitcoincore_rpc::json::CreateRawTransactionInput;
8-
use bdk_testenv::bitcoincore_rpc::RawTx;
9-
use bdk_testenv::{anyhow, bitcoincore_rpc::RpcApi, TestEnv};
7+
use bdk_testenv::{anyhow, TestEnv};
108
use esplora_client::{self, Builder};
119
use std::collections::{BTreeSet, HashMap, HashSet};
1210
use std::str::FromStr;
@@ -30,7 +28,8 @@ pub async fn detect_receive_tx_cancel() -> anyhow::Result<()> {
3028
let client = Builder::new(base_url.as_str()).build_async()?;
3129

3230
let mut graph = IndexedTxGraph::<ConfirmationBlockTime, _>::new(SpkTxOutIndex::<()>::default());
33-
let (chain, _) = LocalChain::from_genesis_hash(env.bitcoind.client.get_block_hash(0)?);
31+
let (chain, _) =
32+
LocalChain::from_genesis_hash(env.bitcoind.client.get_block_hash(0)?.block_hash()?);
3433

3534
// Get receiving address.
3635
let receiver_spk = common::get_test_spk();
@@ -147,26 +146,16 @@ pub async fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> {
147146
];
148147

149148
let _block_hashes = env.mine_blocks(101, None)?;
150-
let txid1 = env.bitcoind.client.send_to_address(
151-
&receive_address1,
152-
Amount::from_sat(10000),
153-
None,
154-
None,
155-
None,
156-
None,
157-
Some(1),
158-
None,
159-
)?;
160-
let txid2 = env.bitcoind.client.send_to_address(
161-
&receive_address0,
162-
Amount::from_sat(20000),
163-
None,
164-
None,
165-
None,
166-
None,
167-
Some(1),
168-
None,
169-
)?;
149+
let txid1 = env
150+
.bitcoind
151+
.client
152+
.send_to_address(&receive_address1, Amount::from_sat(10000))?
153+
.txid()?;
154+
let txid2 = env
155+
.bitcoind
156+
.client
157+
.send_to_address(&receive_address0, Amount::from_sat(20000))?
158+
.txid()?;
170159
let _block_hashes = env.mine_blocks(1, None)?;
171160
while client.get_height().await.unwrap() < 102 {
172161
sleep(Duration::from_millis(10))
@@ -215,7 +204,7 @@ pub async fn test_update_tx_graph_without_keychain() -> anyhow::Result<()> {
215204
let tx_fee = env
216205
.bitcoind
217206
.client
218-
.get_transaction(&tx.compute_txid(), None)
207+
.get_transaction(&tx.compute_txid())
219208
.expect("Tx must exist")
220209
.fee
221210
.expect("Fee must exist")
@@ -271,16 +260,11 @@ pub async fn test_async_update_tx_graph_stop_gap() -> anyhow::Result<()> {
271260
.collect();
272261

273262
// Then receive coins on the 4th address.
274-
let txid_4th_addr = env.bitcoind.client.send_to_address(
275-
&addresses[3],
276-
Amount::from_sat(10000),
277-
None,
278-
None,
279-
None,
280-
None,
281-
Some(1),
282-
None,
283-
)?;
263+
let txid_4th_addr = env
264+
.bitcoind
265+
.client
266+
.send_to_address(&addresses[3], Amount::from_sat(10000))?
267+
.txid()?;
284268
let _block_hashes = env.mine_blocks(1, None)?;
285269
while client.get_height().await.unwrap() < 103 {
286270
sleep(Duration::from_millis(10))
@@ -317,16 +301,11 @@ pub async fn test_async_update_tx_graph_stop_gap() -> anyhow::Result<()> {
317301
assert_eq!(full_scan_update.last_active_indices[&0], 3);
318302

319303
// Now receive a coin on the last address.
320-
let txid_last_addr = env.bitcoind.client.send_to_address(
321-
&addresses[addresses.len() - 1],
322-
Amount::from_sat(10000),
323-
None,
324-
None,
325-
None,
326-
None,
327-
Some(1),
328-
None,
329-
)?;
304+
let txid_last_addr = env
305+
.bitcoind
306+
.client
307+
.send_to_address(&addresses[addresses.len() - 1], Amount::from_sat(10000))?
308+
.txid()?;
330309
let _block_hashes = env.mine_blocks(1, None)?;
331310
while client.get_height().await.unwrap() < 104 {
332311
sleep(Duration::from_millis(10))

0 commit comments

Comments
 (0)