Skip to content

Commit 0e506d2

Browse files
committed
refactor(core-rpc): Update tests to use client
- update all tests to use bdk-bitcoind-client
1 parent df23b06 commit 0e506d2

3 files changed

Lines changed: 28 additions & 33 deletions

File tree

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
use bdk_bitcoind_client::Auth;
12
use bdk_testenv::anyhow;
23
use bdk_testenv::TestEnv;
34

4-
/// This trait is used for testing. It allows creating a new [`bitcoincore_rpc::Client`] connected
5+
/// This trait is used for testing. It allows creating a new [`bdk_bitcoind_client::Client`] connected
56
/// to the instance of bitcoind running in the test environment. This way the `TestEnv` and the
67
/// `Emitter` aren't required to share the same client. In the future when we no longer depend on
78
/// `bitcoincore-rpc`, this can be updated to return the production client that is used by BDK.
89
pub trait ClientExt {
9-
/// Creates a new [`bitcoincore_rpc::Client`] connected to the current node instance.
10-
fn get_rpc_client(&self) -> anyhow::Result<bitcoincore_rpc::Client>;
10+
/// Creates a new [`bdk_bitcoin_client::Client`] connected to the current node instance.
11+
fn get_rpc_client(&self) -> anyhow::Result<bdk_bitcoind_client::Client>;
1112
}
1213

1314
impl ClientExt for TestEnv {
14-
fn get_rpc_client(&self) -> anyhow::Result<bitcoincore_rpc::Client> {
15-
Ok(bitcoincore_rpc::Client::new(
15+
fn get_rpc_client(&self) -> anyhow::Result<bdk_bitcoind_client::Client> {
16+
Ok(bdk_bitcoind_client::Client::with_auth(
1617
&self.bitcoind.rpc_url(),
17-
bitcoincore_rpc::Auth::CookieFile(self.bitcoind.params.cookie_file.clone()),
18+
Auth::CookieFile(self.bitcoind.params.cookie_file.clone()),
1819
)?)
1920
}
2021
}

crates/bitcoind_rpc/tests/test_emitter.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
use std::{collections::BTreeSet, ops::Deref};
1+
use std::collections::BTreeSet;
22

3+
use bdk_bitcoind_client::jsonrpc::serde_json;
34
use bdk_bitcoind_rpc::{Emitter, NO_EXPECTED_MEMPOOL_TXS};
45
use bdk_chain::{
56
bitcoin::{Address, Amount, Txid},
67
local_chain::{CheckPoint, LocalChain},
78
spk_txout::SpkTxOutIndex,
89
Balance, BlockId, CanonicalizationParams, IndexedTxGraph, Merge,
910
};
10-
use bdk_testenv::{
11-
anyhow,
12-
corepc_node::{Input, Output},
13-
TestEnv,
14-
};
15-
use bitcoin::{hashes::Hash, Block, Network, ScriptBuf, WScriptHash};
11+
use bdk_testenv::{anyhow, TestEnv};
12+
use bitcoin::{hashes::Hash, Block, Network, OutPoint, ScriptBuf, WScriptHash};
1613

1714
use crate::common::ClientExt;
1815

@@ -31,14 +28,14 @@ pub fn test_sync_local_chain() -> anyhow::Result<()> {
3128
let (mut local_chain, _) = LocalChain::from_genesis(env.genesis_hash()?);
3229

3330
let client = ClientExt::get_rpc_client(&env)?;
34-
let mut emitter = Emitter::new(&client, local_chain.tip(), 0, NO_EXPECTED_MEMPOOL_TXS);
31+
let mut emitter = Emitter::new(client, local_chain.tip(), 0, NO_EXPECTED_MEMPOOL_TXS);
3532

3633
// Mine some blocks and return the actual block hashes.
3734
// Because initializing `ElectrsD` already mines some blocks, we must include those too when
3835
// returning block hashes.
3936
let exp_hashes = {
4037
let mut hashes = (0..=network_tip)
41-
.map(|height| env.get_block_hash(height))
38+
.map(|height| env.get_block_hash(height as u64))
4239
.collect::<Result<Vec<_>, _>>()?;
4340
hashes.extend(env.mine_blocks(101 - network_tip as usize, None)?);
4441
hashes
@@ -171,7 +168,7 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
171168
});
172169

173170
let client = ClientExt::get_rpc_client(&env)?;
174-
let emitter = &mut Emitter::new(&client, chain.tip(), 0, NO_EXPECTED_MEMPOOL_TXS);
171+
let emitter = &mut Emitter::new(client, chain.tip(), 0, NO_EXPECTED_MEMPOOL_TXS);
175172

176173
while let Some(emission) = emitter.next_block()? {
177174
let height = emission.block_height();
@@ -183,6 +180,8 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
183180
// send 3 txs to a tracked address, these txs will be in the mempool
184181
let exp_txids = {
185182
let mut txids = BTreeSet::new();
183+
let client = ClientExt::get_rpc_client(&env)?;
184+
186185
for _ in 0..3 {
187186
txids.insert(
188187
env.rpc_client()
@@ -261,7 +260,7 @@ fn ensure_block_emitted_after_reorg_is_at_reorg_height() -> anyhow::Result<()> {
261260

262261
let client = ClientExt::get_rpc_client(&env)?;
263262
let mut emitter = Emitter::new(
264-
&client,
263+
client,
265264
CheckPoint::new(0, env.genesis_hash()?),
266265
EMITTER_START_HEIGHT as _,
267266
NO_EXPECTED_MEMPOOL_TXS,
@@ -298,15 +297,11 @@ fn process_block(
298297
Ok(())
299298
}
300299

301-
fn sync_from_emitter<C>(
300+
fn sync_from_emitter(
302301
recv_chain: &mut LocalChain,
303302
recv_graph: &mut IndexedTxGraph<BlockId, SpkTxOutIndex<()>>,
304-
emitter: &mut Emitter<C>,
305-
) -> anyhow::Result<()>
306-
where
307-
C: Deref,
308-
C::Target: bitcoincore_rpc::RpcApi,
309-
{
303+
emitter: &mut Emitter,
304+
) -> anyhow::Result<()> {
310305
while let Some(emission) = emitter.next_block()? {
311306
let height = emission.block_height();
312307
process_block(recv_chain, recv_graph, emission.block, height)?;
@@ -338,7 +333,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
338333

339334
let client = ClientExt::get_rpc_client(&env)?;
340335
let mut emitter = Emitter::new(
341-
&client,
336+
client,
342337
CheckPoint::new(0, env.genesis_hash()?),
343338
0,
344339
NO_EXPECTED_MEMPOOL_TXS,
@@ -350,6 +345,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> anyhow::Result<()> {
350345
.get_new_address(None, None)?
351346
.address()?
352347
.assume_checked();
348+
353349
let spk_to_track = ScriptBuf::new_p2wsh(&WScriptHash::all_zeros());
354350
let addr_to_track = Address::from_script(&spk_to_track, Network::Regtest)?;
355351

@@ -431,7 +427,7 @@ fn mempool_avoids_re_emission() -> anyhow::Result<()> {
431427

432428
let client = ClientExt::get_rpc_client(&env)?;
433429
let mut emitter = Emitter::new(
434-
&client,
430+
client,
435431
CheckPoint::new(0, env.genesis_hash()?),
436432
0,
437433
NO_EXPECTED_MEMPOOL_TXS,
@@ -503,7 +499,7 @@ fn no_agreement_point() -> anyhow::Result<()> {
503499
let client = ClientExt::get_rpc_client(&env)?;
504500
// start height is 99
505501
let mut emitter = Emitter::new(
506-
&client,
502+
client,
507503
CheckPoint::new(0, env.genesis_hash()?),
508504
(PREMINE_COUNT - 2) as u32,
509505
NO_EXPECTED_MEMPOOL_TXS,
@@ -560,7 +556,6 @@ fn no_agreement_point() -> anyhow::Result<()> {
560556
/// 3. Insert the eviction into the graph and assert tx1 is no longer canonical.
561557
#[test]
562558
fn test_expect_tx_evicted() -> anyhow::Result<()> {
563-
use bdk_bitcoind_rpc::bitcoincore_rpc::bitcoin;
564559
use bdk_chain::miniscript;
565560
use bdk_chain::spk_txout::SpkTxOutIndex;
566561
use bitcoin::constants::genesis_block;
@@ -590,7 +585,7 @@ fn test_expect_tx_evicted() -> anyhow::Result<()> {
590585
let tx_1 = env.rpc_client().get_transaction(txid_1)?.into_model()?.tx;
591586

592587
let client = ClientExt::get_rpc_client(&env)?;
593-
let mut emitter = Emitter::new(&client, chain.tip(), 1, core::iter::once(tx_1));
588+
let mut emitter = Emitter::new(client, chain.tip(), 1, core::iter::once(tx_1));
594589
while let Some(emission) = emitter.next_block()? {
595590
let height = emission.block_height();
596591
chain.apply_header(&emission.block.header, height)?;
@@ -674,7 +669,7 @@ fn detect_new_mempool_txs() -> anyhow::Result<()> {
674669

675670
let client = ClientExt::get_rpc_client(&env)?;
676671
let mut emitter = Emitter::new(
677-
&client,
672+
client,
678673
CheckPoint::new(0, env.genesis_hash()?),
679674
0,
680675
NO_EXPECTED_MEMPOOL_TXS,

crates/bitcoind_rpc/tests/test_filter_iter.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use bdk_bitcoind_rpc::bip158::{Error, FilterIter};
22
use bdk_core::CheckPoint;
33
use bdk_testenv::{anyhow, corepc_node, TestEnv};
44
use bitcoin::{Address, Amount, Network, ScriptBuf};
5-
use bitcoincore_rpc::RpcApi;
65

76
use crate::common::ClientExt;
87

@@ -22,7 +21,7 @@ fn testenv() -> anyhow::Result<TestEnv> {
2221
fn filter_iter_matches_blocks() -> anyhow::Result<()> {
2322
let env = testenv()?;
2423
let addr = ClientExt::get_rpc_client(&env)?
25-
.get_new_address(None, None)?
24+
.get_new_address(None, None)
2625
.assume_checked();
2726

2827
let _ = env.mine_blocks(100, Some(addr.clone()))?;
@@ -78,7 +77,7 @@ fn filter_iter_detects_reorgs() -> anyhow::Result<()> {
7877

7978
let env = testenv()?;
8079
let rpc = ClientExt::get_rpc_client(&env)?;
81-
while rpc.get_block_count()? < MINE_TO as u64 {
80+
while rpc.get_block_count()? < MINE_TO {
8281
let _ = env.mine_blocks(1, None)?;
8382
}
8483

0 commit comments

Comments
 (0)