Skip to content

Commit 9a5a086

Browse files
committed
fix(tests): bdk_bitcoind_rpc to use ClientExt
- move `ClientExt` trait to it's own common module. - update the `bdk_bitcoind_rpc` lib.rs tests to use `ClientExt`. - update the `bdk_bitcoind_rpc` test_emitter.rs tests to use `ClientExt`. - update the `bdk_bitcoind_rpc` test_filter_iter.rs tests to use `ClientExt`.
1 parent 0088bec commit 9a5a086

File tree

5 files changed

+100
-58
lines changed

5 files changed

+100
-58
lines changed

crates/bitcoind_rpc/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ workspace = true
1717

1818
[dependencies]
1919
bitcoin = { version = "0.32.0", default-features = false }
20-
# FIXME: (@leonardo) update the code to use corepc-node, and corepc-client instead and then remove this.
2120
bitcoincore-rpc = { version = "0.19.0" }
2221
bdk_core = { path = "../core", version = "0.6.1", default-features = false }
2322

2423
[dev-dependencies]
2524
bdk_bitcoind_rpc = { path = "." }
26-
# FIXME: (@leonardo) update the code to use corepc-node, and corepc-client instead and then remove this.
27-
bdk_testenv = { version = "0.13.1" }
25+
bdk_testenv = { path = "../testenv" }
2826
bdk_chain = { path = "../chain" }
2927

3028
[features]

crates/bitcoind_rpc/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ where
201201
/// Emit the next block height and block (if any).
202202
pub fn next_block(&mut self) -> Result<Option<BlockEvent<Block>>, bitcoincore_rpc::Error> {
203203
if let Some((checkpoint, block)) = poll(self, move |hash, client| client.get_block(hash))? {
204+
println!("block: {:?}", block);
204205
// Stop tracking unconfirmed transactions that have been confirmed in this block.
205206
for tx in &block.txdata {
206207
self.mempool_snapshot.remove(&tx.compute_txid());
@@ -399,7 +400,7 @@ impl BitcoindRpcErrorExt for bitcoincore_rpc::Error {
399400
#[cfg(test)]
400401
#[cfg_attr(coverage_nightly, coverage(off))]
401402
mod test {
402-
use crate::{bitcoincore_rpc::RpcApi, Emitter, NO_EXPECTED_MEMPOOL_TXS};
403+
use crate::{Emitter, NO_EXPECTED_MEMPOOL_TXS};
403404
use bdk_chain::local_chain::LocalChain;
404405
use bdk_testenv::{anyhow, TestEnv};
405406
use bitcoin::{hashes::Hash, Address, Amount, ScriptBuf, Txid, WScriptHash};
@@ -408,10 +409,17 @@ mod test {
408409
#[test]
409410
fn test_expected_mempool_txids_accumulate_and_remove() -> anyhow::Result<()> {
410411
let env = TestEnv::new()?;
411-
let chain = LocalChain::from_genesis(env.rpc_client().get_block_hash(0)?).0;
412+
let (chain, _) = LocalChain::from_genesis(env.genesis_hash()?);
412413
let chain_tip = chain.tip();
414+
415+
// TODO: (@oleonardolima) We should use the `ClientExt` trait instead.
416+
let mut bitcoincore_rpc_client = bitcoincore_rpc::Client::new(
417+
&env.bitcoind.rpc_url(),
418+
bitcoincore_rpc::Auth::CookieFile(env.bitcoind.workdir().join("regtest/.cookie")),
419+
)?;
420+
413421
let mut emitter = Emitter::new(
414-
env.rpc_client(),
422+
&mut bitcoincore_rpc_client,
415423
chain_tip.clone(),
416424
1,
417425
NO_EXPECTED_MEMPOOL_TXS,

crates/bitcoind_rpc/tests/test_emitter.rs

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ use bdk_testenv::{anyhow, TestEnv};
1111
use bitcoin::{hashes::Hash, Block, Network, OutPoint, ScriptBuf, WScriptHash};
1212
use bitcoincore_rpc::RpcApi;
1313

14+
use crate::common::ClientExt;
15+
16+
mod common;
17+
1418
/// Ensure that blocks are emitted in order even after reorg.
1519
///
1620
/// 1. Mine 101 blocks.
@@ -20,10 +24,12 @@ use bitcoincore_rpc::RpcApi;
2024
#[test]
2125
pub fn test_sync_local_chain() -> anyhow::Result<()> {
2226
let env = TestEnv::new()?;
23-
let network_tip = env.rpc_client().get_block_count()?;
24-
let (mut local_chain, _) = LocalChain::from_genesis(env.rpc_client().get_block_hash(0)?);
27+
let network_tip = env.rpc_client().get_block_count()?.into_model().0;
28+
let (mut local_chain, _) = LocalChain::from_genesis(env.genesis_hash()?);
29+
30+
let mut bitcoincore_rpc_client = ClientExt::get_rpc_client(&env)?;
2531
let mut emitter = Emitter::new(
26-
env.rpc_client(),
32+
&mut bitcoincore_rpc_client,
2733
local_chain.tip(),
2834
0,
2935
NO_EXPECTED_MEMPOOL_TXS,
@@ -34,7 +40,11 @@ pub fn test_sync_local_chain() -> anyhow::Result<()> {
3440
// returning block hashes.
3541
let exp_hashes = {
3642
let mut hashes = (0..=network_tip)
37-
.map(|height| env.rpc_client().get_block_hash(height))
43+
.map(|height| {
44+
ClientExt::get_rpc_client(&env)
45+
.unwrap()
46+
.get_block_hash(height)
47+
})
3848
.collect::<Result<Vec<_>, _>>()?;
3949
hashes.extend(env.mine_blocks(101 - network_tip as usize, None)?);
4050
hashes
@@ -137,22 +147,21 @@ pub fn test_sync_local_chain() -> anyhow::Result<()> {
137147
fn test_into_tx_graph() -> anyhow::Result<()> {
138148
let env = TestEnv::new()?;
139149

140-
let addr_0 = env
141-
.rpc_client()
150+
let mut bitcoincore_rpc_client = ClientExt::get_rpc_client(&env)?;
151+
152+
let addr_0 = bitcoincore_rpc_client
142153
.get_new_address(None, None)?
143154
.assume_checked();
144-
let addr_1 = env
145-
.rpc_client()
155+
let addr_1 = bitcoincore_rpc_client
146156
.get_new_address(None, None)?
147157
.assume_checked();
148-
let addr_2 = env
149-
.rpc_client()
158+
let addr_2 = bitcoincore_rpc_client
150159
.get_new_address(None, None)?
151160
.assume_checked();
152161

153162
env.mine_blocks(101, None)?;
154163

155-
let (mut chain, _) = LocalChain::from_genesis(env.rpc_client().get_block_hash(0)?);
164+
let (mut chain, _) = LocalChain::from_genesis(env.genesis_hash()?);
156165
let mut indexed_tx_graph = IndexedTxGraph::<BlockId, _>::new({
157166
let mut index = SpkTxOutIndex::<usize>::default();
158167
index.insert_spk(0, addr_0.script_pubkey());
@@ -161,7 +170,12 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
161170
index
162171
});
163172

164-
let emitter = &mut Emitter::new(env.rpc_client(), chain.tip(), 0, NO_EXPECTED_MEMPOOL_TXS);
173+
let emitter = &mut Emitter::new(
174+
&mut bitcoincore_rpc_client,
175+
chain.tip(),
176+
0,
177+
NO_EXPECTED_MEMPOOL_TXS,
178+
);
165179

166180
while let Some(emission) = emitter.next_block()? {
167181
let height = emission.block_height();
@@ -174,7 +188,7 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
174188
let exp_txids = {
175189
let mut txids = BTreeSet::new();
176190
for _ in 0..3 {
177-
txids.insert(env.rpc_client().send_to_address(
191+
txids.insert(ClientExt::get_rpc_client(&env)?.send_to_address(
178192
&addr_0,
179193
Amount::from_sat(10_000),
180194
None,
@@ -210,7 +224,9 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
210224

211225
// mine a block that confirms the 3 txs
212226
let exp_block_hash = env.mine_blocks(1, None)?[0];
213-
let exp_block_height = env.rpc_client().get_block_info(&exp_block_hash)?.height as u32;
227+
let exp_block_height = ClientExt::get_rpc_client(&env)?
228+
.get_block_info(&exp_block_hash)?
229+
.height as u32;
214230
let exp_anchors = exp_txids
215231
.iter()
216232
.map({
@@ -250,9 +266,11 @@ fn ensure_block_emitted_after_reorg_is_at_reorg_height() -> anyhow::Result<()> {
250266
const CHAIN_TIP_HEIGHT: usize = 110;
251267

252268
let env = TestEnv::new()?;
269+
270+
let mut bitcoincore_rpc_client = ClientExt::get_rpc_client(&env)?;
253271
let mut emitter = Emitter::new(
254-
env.rpc_client(),
255-
CheckPoint::new(0, env.rpc_client().get_block_hash(0)?),
272+
&mut bitcoincore_rpc_client,
273+
CheckPoint::new(0, env.genesis_hash()?),
256274
EMITTER_START_HEIGHT as _,
257275
NO_EXPECTED_MEMPOOL_TXS,
258276
);
@@ -325,23 +343,24 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
325343
const SEND_AMOUNT: Amount = Amount::from_sat(10_000);
326344

327345
let env = TestEnv::new()?;
346+
347+
let mut bitcoincore_rpc_client = ClientExt::get_rpc_client(&env)?;
328348
let mut emitter = Emitter::new(
329-
env.rpc_client(),
330-
CheckPoint::new(0, env.rpc_client().get_block_hash(0)?),
349+
&mut bitcoincore_rpc_client,
350+
CheckPoint::new(0, env.genesis_hash()?),
331351
0,
332352
NO_EXPECTED_MEMPOOL_TXS,
333353
);
334354

335355
// setup addresses
336-
let addr_to_mine = env
337-
.rpc_client()
356+
let addr_to_mine = ClientExt::get_rpc_client(&env)?
338357
.get_new_address(None, None)?
339358
.assume_checked();
340359
let spk_to_track = ScriptBuf::new_p2wsh(&WScriptHash::all_zeros());
341360
let addr_to_track = Address::from_script(&spk_to_track, Network::Regtest)?;
342361

343362
// setup receiver
344-
let (mut recv_chain, _) = LocalChain::from_genesis(env.rpc_client().get_block_hash(0)?);
363+
let (mut recv_chain, _) = LocalChain::from_genesis(env.genesis_hash()?);
345364
let mut recv_graph = IndexedTxGraph::<BlockId, _>::new({
346365
let mut recv_index = SpkTxOutIndex::default();
347366
recv_index.insert_spk((), spk_to_track.clone());
@@ -356,8 +375,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
356375
let txid = env.send(&addr_to_track, SEND_AMOUNT)?;
357376

358377
// lock outputs that send to `addr_to_track`
359-
let outpoints_to_lock = env
360-
.rpc_client()
378+
let outpoints_to_lock = ClientExt::get_rpc_client(&env)?
361379
.get_transaction(&txid, None)?
362380
.transaction()?
363381
.output
@@ -366,7 +384,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
366384
.filter(|(_, txo)| txo.script_pubkey == spk_to_track)
367385
.map(|(vout, _)| OutPoint::new(txid, vout as _))
368386
.collect::<Vec<_>>();
369-
env.rpc_client().lock_unspent(&outpoints_to_lock)?;
387+
ClientExt::get_rpc_client(&env)?.lock_unspent(&outpoints_to_lock)?;
370388

371389
let _ = env.mine_blocks(1, None)?;
372390
}
@@ -413,16 +431,16 @@ fn mempool_avoids_re_emission() -> anyhow::Result<()> {
413431
const MEMPOOL_TX_COUNT: usize = 2;
414432

415433
let env = TestEnv::new()?;
434+
let mut bitcoincore_rpc_client = ClientExt::get_rpc_client(&env)?;
416435
let mut emitter = Emitter::new(
417-
env.rpc_client(),
418-
CheckPoint::new(0, env.rpc_client().get_block_hash(0)?),
436+
&mut bitcoincore_rpc_client,
437+
CheckPoint::new(0, env.genesis_hash()?),
419438
0,
420439
NO_EXPECTED_MEMPOOL_TXS,
421440
);
422441

423442
// mine blocks and sync up emitter
424-
let addr = env
425-
.rpc_client()
443+
let addr = ClientExt::get_rpc_client(&env)?
426444
.get_new_address(None, None)?
427445
.assume_checked();
428446
env.mine_blocks(BLOCKS_TO_MINE, Some(addr.clone()))?;
@@ -482,10 +500,11 @@ fn no_agreement_point() -> anyhow::Result<()> {
482500

483501
let env = TestEnv::new()?;
484502

503+
let mut bitcoincore_rpc_client = ClientExt::get_rpc_client(&env)?;
485504
// start height is 99
486505
let mut emitter = Emitter::new(
487-
env.rpc_client(),
488-
CheckPoint::new(0, env.rpc_client().get_block_hash(0)?),
506+
&mut bitcoincore_rpc_client,
507+
CheckPoint::new(0, env.genesis_hash()?),
489508
(PREMINE_COUNT - 2) as u32,
490509
NO_EXPECTED_MEMPOOL_TXS,
491510
);
@@ -507,12 +526,12 @@ fn no_agreement_point() -> anyhow::Result<()> {
507526
let block_hash_100a = block_header_100a.block_hash();
508527

509528
// get hash for block 101a
510-
let block_hash_101a = env.rpc_client().get_block_hash(101)?;
529+
let block_hash_101a = ClientExt::get_rpc_client(&env)?.get_block_hash(101)?;
511530

512531
// invalidate blocks 99a, 100a, 101a
513-
env.rpc_client().invalidate_block(&block_hash_99a)?;
514-
env.rpc_client().invalidate_block(&block_hash_100a)?;
515-
env.rpc_client().invalidate_block(&block_hash_101a)?;
532+
ClientExt::get_rpc_client(&env)?.invalidate_block(&block_hash_99a)?;
533+
ClientExt::get_rpc_client(&env)?.invalidate_block(&block_hash_100a)?;
534+
ClientExt::get_rpc_client(&env)?.invalidate_block(&block_hash_101a)?;
516535

517536
// mine new blocks 99b, 100b, 101b
518537
env.mine_blocks(3, None)?;
@@ -570,12 +589,17 @@ fn test_expect_tx_evicted() -> anyhow::Result<()> {
570589
&Address::from_script(&spk, Network::Regtest)?,
571590
Amount::ONE_BTC,
572591
)?;
573-
let tx_1 = env
574-
.rpc_client()
592+
let tx_1 = ClientExt::get_rpc_client(&env)?
575593
.get_transaction(&txid_1, None)?
576594
.transaction()?;
577595

578-
let mut emitter = Emitter::new(env.rpc_client(), chain.tip(), 1, core::iter::once(tx_1));
596+
let mut bitcoincore_rpc_client = ClientExt::get_rpc_client(&env)?;
597+
let mut emitter = Emitter::new(
598+
&mut bitcoincore_rpc_client,
599+
chain.tip(),
600+
1,
601+
core::iter::once(tx_1),
602+
);
579603
while let Some(emission) = emitter.next_block()? {
580604
let height = emission.block_height();
581605
chain.apply_header(&emission.block.header, height)?;
@@ -591,7 +615,7 @@ fn test_expect_tx_evicted() -> anyhow::Result<()> {
591615
// Double spend tx1.
592616

593617
// Get `prevout` from core.
594-
let core = env.rpc_client();
618+
let core = ClientExt::get_rpc_client(&env)?;
595619
let tx1 = &core.get_raw_transaction(&txid_1, None)?;
596620
let txin = &tx1.input[0];
597621
let op = txin.previous_output;
@@ -648,14 +672,15 @@ fn detect_new_mempool_txs() -> anyhow::Result<()> {
648672
let env = TestEnv::new()?;
649673
env.mine_blocks(101, None)?;
650674

651-
let addr = env
652-
.rpc_client()
675+
let mut bitcoincore_rpc_client = ClientExt::get_rpc_client(&env)?;
676+
677+
let addr = bitcoincore_rpc_client
653678
.get_new_address(None, None)?
654679
.require_network(Network::Regtest)?;
655680

656681
let mut emitter = Emitter::new(
657-
env.rpc_client(),
658-
CheckPoint::new(0, env.rpc_client().get_block_hash(0)?),
682+
&mut bitcoincore_rpc_client,
683+
CheckPoint::new(0, env.genesis_hash()?),
659684
0,
660685
NO_EXPECTED_MEMPOOL_TXS,
661686
);

0 commit comments

Comments
 (0)