Skip to content

Commit 7ec10ad

Browse files
committed
feat: add optional tracing instrumentation
1 parent 993ae06 commit 7ec10ad

File tree

7 files changed

+258
-3
lines changed

7 files changed

+258
-3
lines changed

crates/bitcoind_rpc/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ workspace = true
1919
bitcoin = { version = "0.32.0", default-features = false }
2020
bitcoincore-rpc = { version = "0.19.0" }
2121
bdk_core = { path = "../core", version = "0.6.1", default-features = false }
22+
tracing = { version = "0.1", optional = true }
2223

2324
[dev-dependencies]
2425
bdk_bitcoind_rpc = { path = "." }
@@ -29,6 +30,7 @@ bdk_chain = { path = "../chain" }
2930
default = ["std"]
3031
std = ["bitcoin/std", "bdk_core/std"]
3132
serde = ["bitcoin/serde", "bdk_core/serde"]
33+
tracing-logs = ["tracing"]
3234

3335
[[example]]
3436
name = "filter_iter"

crates/bitcoind_rpc/src/lib.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ use bitcoin::{Block, BlockHash, Transaction, Txid};
2121
use bitcoincore_rpc::{bitcoincore_rpc_json, RpcApi};
2222
use core::ops::Deref;
2323

24+
#[cfg(feature = "tracing-logs")]
25+
use tracing::trace;
26+
2427
pub mod bip158;
2528

2629
pub use bitcoincore_rpc;
@@ -124,6 +127,13 @@ where
124127
pub fn mempool_at(&mut self, sync_time: u64) -> Result<MempoolEvent, bitcoincore_rpc::Error> {
125128
let client = &*self.client;
126129

130+
#[cfg(feature = "tracing-logs")]
131+
trace!(
132+
start_height = self.start_height,
133+
sync_time = sync_time,
134+
"enter mempool_at"
135+
);
136+
127137
let mut rpc_tip_height;
128138
let mut rpc_tip_hash;
129139
let mut rpc_mempool;
@@ -164,6 +174,14 @@ where
164174
..Default::default()
165175
};
166176

177+
#[cfg(feature = "tracing-logs")]
178+
trace!(
179+
rpc_mempool_count = rpc_mempool_txids.len(),
180+
rpc_height = rpc_tip_height,
181+
rpc_block_hash = %rpc_tip_hash,
182+
"fetched raw mempool"
183+
);
184+
167185
let at_tip =
168186
rpc_tip_height == self.last_cp.height() as u64 && rpc_tip_hash == self.last_cp.hash();
169187

@@ -200,11 +218,23 @@ where
200218

201219
/// Emit the next block height and block (if any).
202220
pub fn next_block(&mut self) -> Result<Option<BlockEvent<Block>>, bitcoincore_rpc::Error> {
221+
#[cfg(feature = "tracing-logs")]
222+
trace!(
223+
last_block_height = self.last_block.as_ref().map(|r| r.height),
224+
"enter next_block"
225+
);
226+
203227
if let Some((checkpoint, block)) = poll(self, move |hash, client| client.get_block(hash))? {
204228
// Stop tracking unconfirmed transactions that have been confirmed in this block.
205229
for tx in &block.txdata {
206230
self.mempool_snapshot.remove(&tx.compute_txid());
207231
}
232+
#[cfg(feature = "tracing-logs")]
233+
trace!(
234+
block_height = checkpoint.height(),
235+
tx_count = block.txdata.len(),
236+
"emit block"
237+
);
208238
return Ok(Some(BlockEvent { block, checkpoint }));
209239
}
210240
Ok(None)
@@ -279,6 +309,13 @@ where
279309
C: Deref,
280310
C::Target: RpcApi,
281311
{
312+
#[cfg(feature = "tracing-logs")]
313+
trace!(
314+
last_block_height = emitter.last_block.as_ref().map(|r| r.height),
315+
start_height = emitter.start_height,
316+
"enter poll_once"
317+
);
318+
282319
let client = &*emitter.client;
283320

284321
if let Some(last_res) = &emitter.last_block {
@@ -287,21 +324,35 @@ where
287324
let next_hash = client.get_block_hash(emitter.start_height as _)?;
288325
// make sure last emission is still in best chain
289326
if client.get_block_hash(last_res.height as _)? != last_res.hash {
327+
#[cfg(feature = "tracing-logs")]
328+
trace!("block not in best chain");
290329
return Ok(PollResponse::BlockNotInBestChain);
291330
}
292331
next_hash
293332
} else {
294333
match last_res.nextblockhash {
295-
None => return Ok(PollResponse::NoMoreBlocks),
334+
None => {
335+
#[cfg(feature = "tracing-logs")]
336+
trace!("no more blocks");
337+
return Ok(PollResponse::NoMoreBlocks);
338+
}
296339
Some(next_hash) => next_hash,
297340
}
298341
};
299342

300343
let res = client.get_block_info(&next_hash)?;
301344
if res.confirmations < 0 {
345+
#[cfg(feature = "tracing-logs")]
346+
trace!("block not in best chain");
302347
return Ok(PollResponse::BlockNotInBestChain);
303348
}
304349

350+
#[cfg(feature = "tracing-logs")]
351+
trace!(
352+
height = res.height,
353+
hash = %res.hash,
354+
"agreement found"
355+
);
305356
return Ok(PollResponse::Block(res));
306357
}
307358

@@ -321,6 +372,12 @@ where
321372
};
322373

323374
// agreement point found
375+
#[cfg(feature = "tracing-logs")]
376+
trace!(
377+
"poll(): PollResponse::AgreementFound, height={}, hash={}",
378+
res.height,
379+
res.hash
380+
);
324381
return Ok(PollResponse::AgreementFound(res, cp));
325382
}
326383

crates/electrum/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ workspace = true
1515
[dependencies]
1616
bdk_core = { path = "../core", version = "0.6.1" }
1717
electrum-client = { version = "0.24.0", features = [ "proxy" ], default-features = false }
18+
tracing = { version = "0.1", optional = true }
1819

1920
[dev-dependencies]
2021
bdk_testenv = { path = "../testenv" }
@@ -26,6 +27,7 @@ default = ["use-rustls"]
2627
use-rustls = ["electrum-client/use-rustls"]
2728
use-rustls-ring = ["electrum-client/use-rustls-ring"]
2829
use-openssl = ["electrum-client/use-openssl"]
30+
tracing-logs = ["tracing"]
2931

3032
[[test]]
3133
name = "test_electrum"

0 commit comments

Comments
 (0)