Skip to content

Commit 454b807

Browse files
committed
refactor(core-rpc): Update bitcoind_rpc dependants
- after updating bitcoind_rpc with bdk- bitcoind-client, the random fn became unvailable in chain, so I enabled the rand-std feature - also updated the random fn to use the one available in chain through bitcoin
1 parent 0e506d2 commit 454b807

File tree

8 files changed

+44
-38
lines changed

8 files changed

+44
-38
lines changed

crates/bitcoind_rpc/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ pub struct Emitter {
3838
/// that the block is no longer in the best chain, it will be popped off from here.
3939
last_cp: CheckPoint<BlockHash>,
4040

41-
/// The block header result returned from rpc of the last-emitted block. As this result contains the
42-
/// next block's block hash (which we use to fetch the next block), we set this to `None`
43-
/// whenever there are no more blocks, or the next block is no longer in the best chain. This
44-
/// gives us an opportunity to re-fetch this result.
41+
/// The block header result returned from rpc of the last-emitted block. As this result
42+
/// contains the next block's block hash (which we use to fetch the next block), we set
43+
/// this to `None` whenever there are no more blocks, or the next block is no longer in the
44+
/// best chain. This gives us an opportunity to re-fetch this result.
4545
last_block: Option<GetBlockHeaderVerbose>,
4646

4747
/// The last snapshot of mempool transactions.

crates/bitcoind_rpc/tests/common/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use bdk_bitcoind_client::Auth;
22
use bdk_testenv::anyhow;
33
use bdk_testenv::TestEnv;
44

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

crates/bitcoind_rpc/tests/test_emitter.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
use std::collections::BTreeSet;
22

3-
use bdk_bitcoind_client::jsonrpc::serde_json;
43
use bdk_bitcoind_rpc::{Emitter, NO_EXPECTED_MEMPOOL_TXS};
54
use bdk_chain::{
65
bitcoin::{Address, Amount, Txid},
76
local_chain::{CheckPoint, LocalChain},
87
spk_txout::SpkTxOutIndex,
98
Balance, BlockId, CanonicalizationParams, IndexedTxGraph, Merge,
109
};
11-
use bdk_testenv::{anyhow, TestEnv};
12-
use bitcoin::{hashes::Hash, Block, Network, OutPoint, ScriptBuf, WScriptHash};
10+
use bdk_testenv::{
11+
anyhow,
12+
corepc_node::{Input, Output},
13+
TestEnv,
14+
};
15+
use bitcoin::{hashes::Hash, Block, Network, ScriptBuf, WScriptHash};
1316

1417
use crate::common::ClientExt;
1518

@@ -35,7 +38,7 @@ pub fn test_sync_local_chain() -> anyhow::Result<()> {
3538
// returning block hashes.
3639
let exp_hashes = {
3740
let mut hashes = (0..=network_tip)
38-
.map(|height| env.get_block_hash(height as u64))
41+
.map(|height| env.get_block_hash(height))
3942
.collect::<Result<Vec<_>, _>>()?;
4043
hashes.extend(env.mine_blocks(101 - network_tip as usize, None)?);
4144
hashes
@@ -180,8 +183,6 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
180183
// send 3 txs to a tracked address, these txs will be in the mempool
181184
let exp_txids = {
182185
let mut txids = BTreeSet::new();
183-
let client = ClientExt::get_rpc_client(&env)?;
184-
185186
for _ in 0..3 {
186187
txids.insert(
187188
env.rpc_client()

crates/bitcoind_rpc/tests/test_filter_iter.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ fn testenv() -> anyhow::Result<TestEnv> {
2020
#[test]
2121
fn filter_iter_matches_blocks() -> anyhow::Result<()> {
2222
let env = testenv()?;
23-
let addr = ClientExt::get_rpc_client(&env)?
24-
.get_new_address(None, None)
23+
let addr = env
24+
.rpc_client()
25+
.get_new_address(None, None)?
26+
.address()?
2527
.assume_checked();
2628

2729
let _ = env.mine_blocks(100, Some(addr.clone()))?;

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ fn relevant_conflicts() -> anyhow::Result<()> {
7777
env.mine_blocks(1, Some(sender_addr.clone()))?;
7878
env.mine_blocks(101, None)?;
7979

80-
// TODO: (@oleonardolima) remove unwraps
8180
let tx_input = client
8281
.list_unspent()?
8382
.0

crates/testenv/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bdk_testenv = { path = "." }
2626
[features]
2727
default = ["std", "download"]
2828
download = ["electrsd/corepc-node_28_2", "electrsd/esplora_a33e97e1"]
29-
std = ["bdk_chain/std"]
29+
std = ["bdk_chain/std", "bitcoin/rand-std"]
3030
serde = ["bdk_chain/serde"]
3131

3232
[package.metadata.docs.rs]

crates/testenv/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct TestEnv {
2424
/// Configuration parameters.
2525
#[derive(Debug)]
2626
pub struct Config<'a> {
27-
/// [`bitcoind::Conf`]
27+
/// [`corepc_node::Conf`]
2828
pub bitcoind: corepc_node::Conf<'a>,
2929
/// [`electrsd::Conf`]
3030
pub electrsd: electrsd::Conf<'a>,

examples/example_bitcoind_rpc_polling/src/main.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ use std::{
88
};
99

1010
use bdk_bitcoind_rpc::{
11-
bitcoincore_rpc::{Auth, Client, RpcApi},
11+
bdk_bitcoind_client::{jsonrpc::serde_json::json, Auth, Client},
1212
Emitter,
1313
};
14-
use bdk_chain::{bitcoin::Block, local_chain, CanonicalizationParams, Merge};
14+
15+
use bdk_chain::{
16+
bitcoin::{Block, Txid},
17+
local_chain, CanonicalizationParams, Merge,
18+
};
1519
use example_cli::{
16-
anyhow,
20+
anyhow::{self, Ok},
1721
clap::{self, Args, Subcommand},
1822
ChangeSet, Keychain,
1923
};
@@ -70,16 +74,14 @@ impl From<RpcArgs> for Auth {
7074

7175
impl RpcArgs {
7276
fn new_client(&self) -> anyhow::Result<Client> {
73-
Ok(Client::new(
74-
&self.url,
75-
match (&self.rpc_cookie, &self.rpc_user, &self.rpc_password) {
76-
(None, None, None) => Auth::None,
77-
(Some(path), _, _) => Auth::CookieFile(path.clone()),
78-
(_, Some(user), Some(pass)) => Auth::UserPass(user.clone(), pass.clone()),
79-
(_, Some(_), None) => panic!("rpc auth: missing rpc_pass"),
80-
(_, None, Some(_)) => panic!("rpc auth: missing rpc_user"),
81-
},
82-
)?)
77+
let auth = match (&self.rpc_cookie, &self.rpc_user, &self.rpc_password) {
78+
(None, None, None) => Auth::None,
79+
(Some(path), _, _) => Auth::CookieFile(path.clone()),
80+
(_, Some(user), Some(pass)) => Auth::UserPass(user.clone(), pass.clone()),
81+
(_, Some(_), None) => panic!("rpc auth: missing rpc_pass"),
82+
(_, None, Some(_)) => panic!("rpc auth: missing rpc_user"),
83+
};
84+
Ok(Client::with_auth(&self.url, auth)?)
8385
}
8486
}
8587

@@ -122,7 +124,7 @@ fn main() -> anyhow::Result<()> {
122124
network,
123125
|rpc_args, tx| {
124126
let client = rpc_args.new_client()?;
125-
client.send_raw_transaction(tx)?;
127+
let _txid: Txid = client.call("sendrawtransaction", &[json!(tx)])?;
126128
Ok(())
127129
},
128130
general_cmd,
@@ -141,7 +143,7 @@ fn main() -> anyhow::Result<()> {
141143
let chain = chain.lock().unwrap();
142144
let graph = graph.lock().unwrap();
143145
Emitter::new(
144-
&rpc_client,
146+
rpc_client,
145147
chain.tip(),
146148
fallback_height,
147149
graph
@@ -241,12 +243,13 @@ fn main() -> anyhow::Result<()> {
241243
} = rpc_args;
242244
let sigterm_flag = start_ctrlc_handler();
243245

244-
let rpc_client = Arc::new(rpc_args.new_client()?);
246+
let emitter_rpc_client = rpc_args.new_client()?;
247+
let rpc_client = rpc_args.new_client()?;
245248
let mut emitter = {
246249
let chain = chain.lock().unwrap();
247250
let graph = graph.lock().unwrap();
248251
Emitter::new(
249-
rpc_client.clone(),
252+
emitter_rpc_client,
250253
chain.tip(),
251254
fallback_height,
252255
graph
@@ -267,7 +270,7 @@ fn main() -> anyhow::Result<()> {
267270
);
268271
let (tx, rx) = std::sync::mpsc::sync_channel::<Emission>(CHANNEL_BOUND);
269272
let emission_jh = std::thread::spawn(move || -> anyhow::Result<()> {
270-
let mut block_count = rpc_client.get_block_count()? as u32;
273+
let mut block_count = rpc_client.get_block_count()?;
271274
tx.send(Emission::Tip(block_count))?;
272275

273276
loop {
@@ -278,7 +281,7 @@ fn main() -> anyhow::Result<()> {
278281
break;
279282
}
280283
if height > block_count {
281-
block_count = rpc_client.get_block_count()? as u32;
284+
block_count = rpc_client.get_block_count()?;
282285
tx.send(Emission::Tip(block_count))?;
283286
}
284287
tx.send(Emission::Block(block_emission))?;

0 commit comments

Comments
 (0)