@@ -9,6 +9,9 @@ use bdk_core::{
99use  electrum_client:: { ElectrumApi ,  Error ,  HeaderNotification } ; 
1010use  std:: sync:: { Arc ,  Mutex } ; 
1111
12+ #[ cfg( feature = "tracing-logs" ) ]  
13+ use  tracing:: { debug,  trace} ; 
14+ 
1215/// We include a chain suffix of a certain length for the purpose of robustness. 
1316const  CHAIN_SUFFIX_LENGTH :  u32  = 8 ; 
1417
@@ -55,11 +58,16 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
5558        let  tx_cache = self . tx_cache . lock ( ) . unwrap ( ) ; 
5659
5760        if  let  Some ( tx)  = tx_cache. get ( & txid)  { 
61+             #[ cfg( feature = "tracing-logs" ) ]  
62+             trace ! ( %txid,  "hit tx cache" ) ; 
5863            return  Ok ( Arc :: clone ( tx) ) ; 
5964        } 
6065
6166        drop ( tx_cache) ; 
6267
68+         #[ cfg( feature = "tracing-logs" ) ]  
69+         trace ! ( %txid,  "missed tx cache, fetching from electrum" ) ; 
70+ 
6371        let  tx = Arc :: new ( self . inner . transaction_get ( & txid) ?) ; 
6472
6573        self . tx_cache . lock ( ) . unwrap ( ) . insert ( txid,  Arc :: clone ( & tx) ) ; 
@@ -104,6 +112,9 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
104112        let  mut  request:  FullScanRequest < K >  = request. into ( ) ; 
105113        let  start_time = request. start_time ( ) ; 
106114
115+         #[ cfg( feature = "tracing-logs" ) ]  
116+         trace ! ( "starting full_scan" ) ; 
117+ 
107118        let  tip_and_latest_blocks = match  request. chain_tip ( )  { 
108119            Some ( chain_tip)  => Some ( fetch_tip_and_latest_blocks ( & self . inner ,  chain_tip) ?) , 
109120            None  => None , 
@@ -187,6 +198,9 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
187198        let  mut  request:  SyncRequest < I >  = request. into ( ) ; 
188199        let  start_time = request. start_time ( ) ; 
189200
201+         #[ cfg( feature = "tracing-logs" ) ]  
202+         trace ! ( "starting sync" ) ; 
203+ 
190204        let  tip_and_latest_blocks = match  request. chain_tip ( )  { 
191205            Some ( chain_tip)  => Some ( fetch_tip_and_latest_blocks ( & self . inner ,  chain_tip) ?) , 
192206            None  => None , 
@@ -266,10 +280,18 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
266280            let  spks = ( 0 ..batch_size) 
267281                . map_while ( |_| spks_with_expected_txids. next ( ) ) 
268282                . collect :: < Vec < _ > > ( ) ; 
283+             #[ cfg( feature = "tracing-logs" ) ]  
284+             trace ! ( batch_size = spks. len( ) ,  "fetching script history batch" ) ; 
285+ 
269286            if  spks. is_empty ( )  { 
287+                 #[ cfg( feature = "tracing-logs" ) ]  
288+                 trace ! ( "no script pubkeys left to scan in batch" ) ; 
270289                return  Ok ( last_active_index) ; 
271290            } 
272291
292+             #[ cfg( feature = "tracing-logs" ) ]  
293+             trace ! ( "spk has tx history" ) ; 
294+ 
273295            let  spk_histories = self 
274296                . inner 
275297                . batch_script_get_history ( spks. iter ( ) . map ( |( _,  s) | s. spk . as_script ( ) ) ) ?; 
@@ -278,7 +300,15 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
278300                if  spk_history. is_empty ( )  { 
279301                    match  unused_spk_count. checked_add ( 1 )  { 
280302                        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+                         } 
282312                    } ; 
283313                }  else  { 
284314                    last_active_index = Some ( spk_index) ; 
@@ -360,10 +390,14 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
360390
361391                    if  !has_residing && res. tx_hash  == outpoint. txid  { 
362392                        has_residing = true ; 
393+                         #[ cfg( feature = "tracing-logs" ) ]  
394+                         trace ! ( %outpoint,  "tx resides at outpoint, fetched and processed" ) ; 
363395                        tx_update. txs . push ( Arc :: clone ( & tx) ) ; 
364396                        match  res. height . try_into ( )  { 
365397                            // Returned heights 0 & -1 are reserved for unconfirmed txs. 
366398                            Ok ( height)  if  height > 0  => { 
399+                                 #[ cfg( feature = "tracing-logs" ) ]  
400+                                 trace ! ( %res. tx_hash,  height,  "added anchor from outpoint resolution" ) ; 
367401                                pending_anchors. push ( ( res. tx_hash ,  height) ) ; 
368402                            } 
369403                            _ => { 
@@ -379,13 +413,18 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
379413                            . input 
380414                            . iter ( ) 
381415                            . 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  { 
383420                            continue ; 
384421                        } 
385422                        tx_update. txs . push ( Arc :: clone ( & res_tx) ) ; 
386423                        match  res. height . try_into ( )  { 
387424                            // Returned heights 0 & -1 are reserved for unconfirmed txs. 
388425                            Ok ( height)  if  height > 0  => { 
426+                                 #[ cfg( feature = "tracing-logs" ) ]  
427+                                 trace ! ( %res. tx_hash,  height,  "added anchor from outpoint resolution" ) ; 
389428                                pending_anchors. push ( ( res. tx_hash ,  height) ) ; 
390429                            } 
391430                            _ => { 
@@ -420,9 +459,13 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
420459                        . expect ( "tx must have an output" ) 
421460                        . clone ( ) ; 
422461                    txs. push ( ( txid,  tx) ) ; 
462+                     #[ cfg( feature = "tracing-logs" ) ]  
463+                     trace ! ( %txid,  "fetched tx for txid, checking confirmation status" ) ; 
423464                    scripts. push ( spk) ; 
424465                } 
425466                Err ( electrum_client:: Error :: Protocol ( _) )  => { 
467+                     #[ cfg( feature = "tracing-logs" ) ]  
468+                     debug ! ( %txid,  "electrum protocol error, skipping txid" ) ; 
426469                    continue ; 
427470                } 
428471                Err ( e)  => return  Err ( e) , 
@@ -440,6 +483,8 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
440483                match  res. height . try_into ( )  { 
441484                    // Returned heights 0 & -1 are reserved for unconfirmed txs. 
442485                    Ok ( height)  if  height > 0  => { 
486+                         #[ cfg( feature = "tracing-logs" ) ]  
487+                         trace ! ( %res. tx_hash,  height,  "anchor candidate found via txid history" ) ; 
443488                        pending_anchors. push ( ( tx. 0 ,  height) ) ; 
444489                    } 
445490                    _ => { 
@@ -499,8 +544,12 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
499544                let  h = height as  u32 ; 
500545                let  hash = height_to_hash[ & h] ; 
501546                if  let  Some ( anchor)  = anchor_cache. get ( & ( txid,  hash) )  { 
547+                     #[ cfg( feature = "tracing-logs" ) ]  
548+                     trace ! ( %txid,  height,  "anchor cache hit" ) ; 
502549                    results. push ( ( txid,  * anchor) ) ; 
503550                }  else  { 
551+                     #[ cfg( feature = "tracing-logs" ) ]  
552+                     trace ! ( %txid,  height,  "anchor cache miss, will fetch merkle proof" ) ; 
504553                    to_fetch. push ( ( txid,  height,  hash) ) ; 
505554                } 
506555            } 
@@ -533,6 +582,8 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
533582            let  mut  valid =
534583                electrum_client:: utils:: validate_merkle_proof ( & txid,  & header. merkle_root ,  & proof) ; 
535584            if  !valid { 
585+                 #[ cfg( feature = "tracing-logs" ) ]  
586+                 debug ! ( %txid,  height,  "merkle validation failed, refetching header and retrying" ) ; 
536587                header = self . inner . block_header ( height) ?; 
537588                self . block_header_cache 
538589                    . lock ( ) 
@@ -543,6 +594,10 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
543594                    & header. merkle_root , 
544595                    & proof, 
545596                ) ; 
597+                 if  valid { 
598+                     #[ cfg( feature = "tracing-logs" ) ]  
599+                     trace ! ( %txid,  height,  "merkle proof validated after retry" ) ; 
600+                 } 
546601            } 
547602
548603            // Build and cache the anchor if merkle proof is valid. 
@@ -558,6 +613,8 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
558613                    . lock ( ) 
559614                    . unwrap ( ) 
560615                    . insert ( ( txid,  hash) ,  anchor) ; 
616+                 #[ cfg( feature = "tracing-logs" ) ]  
617+                 trace ! ( %txid,  height,  %hash,  "inserted anchor" ) ; 
561618                results. push ( ( txid,  anchor) ) ; 
562619            } 
563620        } 
0 commit comments