Skip to content

Commit df23b06

Browse files
committed
refactor(core-rpc): Replace rpc with client
- replace bitcoincore-rpc with bdk-bitcoind-client in lib and examples
1 parent 4e28d3f commit df23b06

5 files changed

Lines changed: 70 additions & 79 deletions

File tree

crates/bitcoind_rpc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ workspace = true
1717

1818
[dependencies]
1919
bitcoin = { version = "0.32.0", default-features = false }
20-
bitcoincore-rpc = { version = "0.19.0" }
20+
bdk-bitcoind-client = { git = "https://github.com/bitcoindevkit/bdk-bitcoind-client", rev = "beefb7d7a109f234d53a17a2dd7caa6ef7d7a84b"}
2121
bdk_core = { path = "../core", version = "0.6.1", default-features = false }
2222

2323
[dev-dependencies]

crates/bitcoind_rpc/examples/filter_iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use std::time::Instant;
33

44
use anyhow::Context;
5+
use bdk_bitcoind_client::{Auth, Client};
56
use bdk_bitcoind_rpc::bip158::{Event, FilterIter};
67
use bdk_chain::bitcoin::{constants::genesis_block, secp256k1::Secp256k1, Network};
78
use bdk_chain::indexer::keychain_txout::KeychainTxOutIndex;
@@ -44,8 +45,7 @@ fn main() -> anyhow::Result<()> {
4445
// Configure RPC client
4546
let url = std::env::var("RPC_URL").context("must set RPC_URL")?;
4647
let cookie = std::env::var("RPC_COOKIE").context("must set RPC_COOKIE")?;
47-
let rpc_client =
48-
bitcoincore_rpc::Client::new(&url, bitcoincore_rpc::Auth::CookieFile(cookie.into()))?;
48+
let rpc_client = Client::with_auth(&url, Auth::CookieFile(cookie.into()))?;
4949

5050
// Initialize `FilterIter`
5151
let mut spks = vec![];

crates/bitcoind_rpc/src/bip158.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
//! [0]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
77
//! [1]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
88
9+
use bdk_bitcoind_client::corepc_types::model::GetBlockHeaderVerbose;
10+
use bdk_bitcoind_client::Client;
911
use bdk_core::bitcoin;
1012
use bdk_core::CheckPoint;
1113
use bitcoin::BlockHash;
1214
use bitcoin::{bip158::BlockFilter, Block, ScriptBuf};
13-
use bitcoincore_rpc;
14-
use bitcoincore_rpc::{json::GetBlockHeaderResult, RpcApi};
1515

1616
/// Type that returns Bitcoin blocks by matching a list of script pubkeys (SPKs) against a
1717
/// [`bip158::BlockFilter`](bitcoin::bip158::BlockFilter).
1818
///
1919
/// * `FilterIter` talks to bitcoind via JSON-RPC interface, which is handled by the
20-
/// [`bitcoincore_rpc::Client`].
20+
/// [`bdk_bitcoind_client::Client`].
2121
/// * Collect the script pubkeys (SPKs) you want to watch. These will usually correspond to wallet
2222
/// addresses that have been handed out for receiving payments.
2323
/// * Construct `FilterIter` with the RPC client, SPKs, and [`CheckPoint`]. The checkpoint tip
@@ -31,19 +31,19 @@ use bitcoincore_rpc::{json::GetBlockHeaderResult, RpcApi};
3131
#[derive(Debug)]
3232
pub struct FilterIter<'a> {
3333
/// RPC client
34-
client: &'a bitcoincore_rpc::Client,
34+
client: &'a Client,
3535
/// SPK inventory
3636
spks: Vec<ScriptBuf>,
3737
/// checkpoint
3838
cp: CheckPoint<BlockHash>,
3939
/// Header info, contains the prev and next hashes for each header.
40-
header: Option<GetBlockHeaderResult>,
40+
header: Option<GetBlockHeaderVerbose>,
4141
}
4242

4343
impl<'a> FilterIter<'a> {
4444
/// Construct [`FilterIter`] with checkpoint, RPC client and SPKs.
4545
pub fn new(
46-
client: &'a bitcoincore_rpc::Client,
46+
client: &'a Client,
4747
cp: CheckPoint,
4848
spks: impl IntoIterator<Item = ScriptBuf>,
4949
) -> Self {
@@ -58,9 +58,9 @@ impl<'a> FilterIter<'a> {
5858
/// Return the agreement header with the remote node.
5959
///
6060
/// Error if no agreement header is found.
61-
fn find_base(&self) -> Result<GetBlockHeaderResult, Error> {
61+
fn find_base(&self) -> Result<GetBlockHeaderVerbose, Error> {
6262
for cp in self.cp.iter() {
63-
match self.client.get_block_header_info(&cp.hash()) {
63+
match self.client.get_block_header_verbose(&cp.hash()) {
6464
Err(e) if is_not_found(&e) => continue,
6565
Ok(header) if header.confirmations <= 0 => continue,
6666
Ok(header) => return Ok(header),
@@ -111,20 +111,20 @@ impl Iterator for FilterIter<'_> {
111111
None => return Ok(None),
112112
};
113113

114-
let mut next_header = self.client.get_block_header_info(&next_hash)?;
114+
let mut next_header = self.client.get_block_header_verbose(&next_hash)?;
115115

116116
// In case of a reorg, rewind by fetching headers of previous hashes until we find
117117
// one with enough confirmations.
118118
while next_header.confirmations < 0 {
119119
let prev_hash = next_header
120120
.previous_block_hash
121121
.ok_or(Error::ReorgDepthExceeded)?;
122-
let prev_header = self.client.get_block_header_info(&prev_hash)?;
122+
let prev_header = self.client.get_block_header_verbose(&prev_hash)?;
123123
next_header = prev_header;
124124
}
125125

126126
next_hash = next_header.hash;
127-
let next_height: u32 = next_header.height.try_into()?;
127+
let next_height: u32 = next_header.height;
128128

129129
cp = cp.insert(next_height, next_hash);
130130

@@ -153,7 +153,7 @@ impl Iterator for FilterIter<'_> {
153153
#[derive(Debug)]
154154
pub enum Error {
155155
/// RPC error
156-
Rpc(bitcoincore_rpc::Error),
156+
Rpc(bdk_bitcoind_client::Error),
157157
/// `bitcoin::bip158` error
158158
Bip158(bitcoin::bip158::Error),
159159
/// Max reorg depth exceeded.
@@ -176,8 +176,8 @@ impl core::fmt::Display for Error {
176176
#[cfg(feature = "std")]
177177
impl std::error::Error for Error {}
178178

179-
impl From<bitcoincore_rpc::Error> for Error {
180-
fn from(e: bitcoincore_rpc::Error) -> Self {
179+
impl From<bdk_bitcoind_client::Error> for Error {
180+
fn from(e: bdk_bitcoind_client::Error) -> Self {
181181
Self::Rpc(e)
182182
}
183183
}
@@ -189,10 +189,12 @@ impl From<core::num::TryFromIntError> for Error {
189189
}
190190

191191
/// Whether the RPC error is a "not found" error (code: `-5`).
192-
fn is_not_found(e: &bitcoincore_rpc::Error) -> bool {
193-
matches!(
194-
e,
195-
bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(e))
196-
if e.code == -5
197-
)
192+
fn is_not_found(e: &bdk_bitcoind_client::Error) -> bool {
193+
if let bdk_bitcoind_client::Error::JsonRpc(bdk_bitcoind_client::jsonrpc::Error::Rpc(rpc_err)) =
194+
e
195+
{
196+
rpc_err.code == -5
197+
} else {
198+
false
199+
}
198200
}

0 commit comments

Comments
 (0)