@@ -9,6 +9,9 @@ use bdk_core::{
9
9
use electrum_client:: { ElectrumApi , Error , HeaderNotification } ;
10
10
use std:: sync:: { Arc , Mutex } ;
11
11
12
+ #[ cfg( feature = "tracing-logs" ) ]
13
+ use tracing:: { debug, trace} ;
14
+
12
15
/// We include a chain suffix of a certain length for the purpose of robustness.
13
16
const CHAIN_SUFFIX_LENGTH : u32 = 8 ;
14
17
@@ -55,11 +58,16 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
55
58
let tx_cache = self . tx_cache . lock ( ) . unwrap ( ) ;
56
59
57
60
if let Some ( tx) = tx_cache. get ( & txid) {
61
+ #[ cfg( feature = "tracing-logs" ) ]
62
+ trace ! ( %txid, "hit tx cache" ) ;
58
63
return Ok ( Arc :: clone ( tx) ) ;
59
64
}
60
65
61
66
drop ( tx_cache) ;
62
67
68
+ #[ cfg( feature = "tracing-logs" ) ]
69
+ trace ! ( %txid, "missed tx cache, fetching from electrum" ) ;
70
+
63
71
let tx = Arc :: new ( self . inner . transaction_get ( & txid) ?) ;
64
72
65
73
self . tx_cache . lock ( ) . unwrap ( ) . insert ( txid, Arc :: clone ( & tx) ) ;
@@ -104,6 +112,9 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
104
112
let mut request: FullScanRequest < K > = request. into ( ) ;
105
113
let start_time = request. start_time ( ) ;
106
114
115
+ #[ cfg( feature = "tracing-logs" ) ]
116
+ trace ! ( "starting full_scan" ) ;
117
+
107
118
let tip_and_latest_blocks = match request. chain_tip ( ) {
108
119
Some ( chain_tip) => Some ( fetch_tip_and_latest_blocks ( & self . inner , chain_tip) ?) ,
109
120
None => None ,
@@ -187,6 +198,9 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
187
198
let mut request: SyncRequest < I > = request. into ( ) ;
188
199
let start_time = request. start_time ( ) ;
189
200
201
+ #[ cfg( feature = "tracing-logs" ) ]
202
+ trace ! ( "starting sync" ) ;
203
+
190
204
let tip_and_latest_blocks = match request. chain_tip ( ) {
191
205
Some ( chain_tip) => Some ( fetch_tip_and_latest_blocks ( & self . inner , chain_tip) ?) ,
192
206
None => None ,
@@ -266,10 +280,18 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
266
280
let spks = ( 0 ..batch_size)
267
281
. map_while ( |_| spks_with_expected_txids. next ( ) )
268
282
. collect :: < Vec < _ > > ( ) ;
283
+ #[ cfg( feature = "tracing-logs" ) ]
284
+ trace ! ( batch_size = spks. len( ) , "fetching script history batch" ) ;
285
+
269
286
if spks. is_empty ( ) {
287
+ #[ cfg( feature = "tracing-logs" ) ]
288
+ trace ! ( "no script pubkeys left to scan in batch" ) ;
270
289
return Ok ( last_active_index) ;
271
290
}
272
291
292
+ #[ cfg( feature = "tracing-logs" ) ]
293
+ trace ! ( "spk has tx history" ) ;
294
+
273
295
let spk_histories = self
274
296
. inner
275
297
. batch_script_get_history ( spks. iter ( ) . map ( |( _, s) | s. spk . as_script ( ) ) ) ?;
@@ -278,7 +300,15 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
278
300
if spk_history. is_empty ( ) {
279
301
match unused_spk_count. checked_add ( 1 ) {
280
302
Some ( i) if i < stop_gap => unused_spk_count = i,
281
- _ => return Ok ( last_active_index) ,
303
+ _ => {
304
+ #[ cfg( feature = "tracing-logs" ) ]
305
+ trace ! (
306
+ unused_spk_count,
307
+ stop_gap,
308
+ "gap limit reached, stopping scan for keychain"
309
+ ) ;
310
+ return Ok ( last_active_index) ;
311
+ }
282
312
} ;
283
313
} else {
284
314
last_active_index = Some ( spk_index) ;
@@ -360,10 +390,14 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
360
390
361
391
if !has_residing && res. tx_hash == outpoint. txid {
362
392
has_residing = true ;
393
+ #[ cfg( feature = "tracing-logs" ) ]
394
+ trace ! ( %outpoint, "tx resides at outpoint, fetched and processed" ) ;
363
395
tx_update. txs . push ( Arc :: clone ( & tx) ) ;
364
396
match res. height . try_into ( ) {
365
397
// Returned heights 0 & -1 are reserved for unconfirmed txs.
366
398
Ok ( height) if height > 0 => {
399
+ #[ cfg( feature = "tracing-logs" ) ]
400
+ trace ! ( %res. tx_hash, height, "added anchor from outpoint resolution" ) ;
367
401
pending_anchors. push ( ( res. tx_hash , height) ) ;
368
402
}
369
403
_ => {
@@ -379,13 +413,18 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
379
413
. input
380
414
. iter ( )
381
415
. any ( |txin| txin. previous_output == outpoint) ;
382
- if !has_spending {
416
+ if has_spending {
417
+ #[ cfg( feature = "tracing-logs" ) ]
418
+ trace ! ( %outpoint, spending_txid = %res. tx_hash, "found tx spending from outpoint" ) ;
419
+ } else {
383
420
continue ;
384
421
}
385
422
tx_update. txs . push ( Arc :: clone ( & res_tx) ) ;
386
423
match res. height . try_into ( ) {
387
424
// Returned heights 0 & -1 are reserved for unconfirmed txs.
388
425
Ok ( height) if height > 0 => {
426
+ #[ cfg( feature = "tracing-logs" ) ]
427
+ trace ! ( %res. tx_hash, height, "added anchor from outpoint resolution" ) ;
389
428
pending_anchors. push ( ( res. tx_hash , height) ) ;
390
429
}
391
430
_ => {
@@ -420,9 +459,13 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
420
459
. expect ( "tx must have an output" )
421
460
. clone ( ) ;
422
461
txs. push ( ( txid, tx) ) ;
462
+ #[ cfg( feature = "tracing-logs" ) ]
463
+ trace ! ( %txid, "fetched tx for txid, checking confirmation status" ) ;
423
464
scripts. push ( spk) ;
424
465
}
425
466
Err ( electrum_client:: Error :: Protocol ( _) ) => {
467
+ #[ cfg( feature = "tracing-logs" ) ]
468
+ debug ! ( %txid, "electrum protocol error, skipping txid" ) ;
426
469
continue ;
427
470
}
428
471
Err ( e) => return Err ( e) ,
@@ -440,6 +483,8 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
440
483
match res. height . try_into ( ) {
441
484
// Returned heights 0 & -1 are reserved for unconfirmed txs.
442
485
Ok ( height) if height > 0 => {
486
+ #[ cfg( feature = "tracing-logs" ) ]
487
+ trace ! ( %res. tx_hash, height, "anchor candidate found via txid history" ) ;
443
488
pending_anchors. push ( ( tx. 0 , height) ) ;
444
489
}
445
490
_ => {
@@ -499,8 +544,12 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
499
544
let h = height as u32 ;
500
545
let hash = height_to_hash[ & h] ;
501
546
if let Some ( anchor) = anchor_cache. get ( & ( txid, hash) ) {
547
+ #[ cfg( feature = "tracing-logs" ) ]
548
+ trace ! ( %txid, height, "anchor cache hit" ) ;
502
549
results. push ( ( txid, * anchor) ) ;
503
550
} else {
551
+ #[ cfg( feature = "tracing-logs" ) ]
552
+ trace ! ( %txid, height, "anchor cache miss, will fetch merkle proof" ) ;
504
553
to_fetch. push ( ( txid, height, hash) ) ;
505
554
}
506
555
}
@@ -533,6 +582,8 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
533
582
let mut valid =
534
583
electrum_client:: utils:: validate_merkle_proof ( & txid, & header. merkle_root , & proof) ;
535
584
if !valid {
585
+ #[ cfg( feature = "tracing-logs" ) ]
586
+ debug ! ( %txid, height, "merkle validation failed, refetching header and retrying" ) ;
536
587
header = self . inner . block_header ( height) ?;
537
588
self . block_header_cache
538
589
. lock ( )
@@ -543,6 +594,10 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
543
594
& header. merkle_root ,
544
595
& proof,
545
596
) ;
597
+ if valid {
598
+ #[ cfg( feature = "tracing-logs" ) ]
599
+ trace ! ( %txid, height, "merkle proof validated after retry" ) ;
600
+ }
546
601
}
547
602
548
603
// Build and cache the anchor if merkle proof is valid.
@@ -558,6 +613,8 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
558
613
. lock ( )
559
614
. unwrap ( )
560
615
. insert ( ( txid, hash) , anchor) ;
616
+ #[ cfg( feature = "tracing-logs" ) ]
617
+ trace ! ( %txid, height, %hash, "inserted anchor" ) ;
561
618
results. push ( ( txid, anchor) ) ;
562
619
}
563
620
}
0 commit comments