Skip to content

Commit 0646619

Browse files
committed
feat: add optional tracing instrumentation
1 parent a48c97a commit 0646619

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.0", 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
@@ -20,6 +20,9 @@ use bitcoin::{Block, BlockHash, Transaction, Txid};
2020
use bitcoincore_rpc::{bitcoincore_rpc_json, RpcApi};
2121
use core::ops::Deref;
2222

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

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

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

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

@@ -199,11 +217,23 @@ where
199217

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

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

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

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

@@ -320,6 +371,12 @@ where
320371
};
321372

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

crates/electrum/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ workspace = true
1616
bdk_core = { path = "../core", version = "0.6.0" }
1717
electrum-client = { version = "0.23.1", features = [ "proxy" ], default-features = false }
1818
serde_json = "1.0"
19+
tracing = { version = "0.1", optional = true }
1920

2021
[dev-dependencies]
2122
bdk_testenv = { path = "../testenv" }
@@ -27,6 +28,7 @@ default = ["use-rustls"]
2728
use-rustls = ["electrum-client/use-rustls"]
2829
use-rustls-ring = ["electrum-client/use-rustls-ring"]
2930
use-openssl = ["electrum-client/use-openssl"]
31+
tracing-logs = ["tracing"]
3032

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

0 commit comments

Comments
 (0)