@@ -367,6 +367,7 @@ async fn sync_chain_monitor<CL: ChainListener + Sized, P: Poll>(new_header: Vali
367
367
. fetch_block ( & header) . await
368
368
. or_else ( |e| Err ( ( e, new_tip) ) ) ?;
369
369
debug_assert_eq ! ( block. block_hash, header. block_hash) ;
370
+
370
371
println ! ( "Connecting block {}" , header. block_hash. to_hex( ) ) ;
371
372
chain_notifier. block_connected ( & block, header. height ) ;
372
373
header_cache. insert ( header. block_hash , header) ;
@@ -448,35 +449,13 @@ where P: Poll,
448
449
/// Check each source for a new best tip and update the chain listener accordingly.
449
450
/// Returns true if some blocks were [dis]connected, false otherwise.
450
451
pub async fn poll_best_tip ( & mut self ) -> BlockSourceResult < ( ChainTip , bool ) > {
451
- macro_rules! sync_chain_monitor {
452
- ( $new_header: expr) => { {
453
- let mut blocks_connected = false ;
454
- match sync_chain_monitor( * $new_header, & self . chain_tip, & mut self . chain_poller, & mut self . chain_notifier, & mut self . header_cache) . await {
455
- Err ( ( _, latest_tip) ) => {
456
- if let Some ( latest_tip) = latest_tip {
457
- let latest_tip_hash = latest_tip. header. block_hash( ) ;
458
- if latest_tip_hash != self . chain_tip. block_hash {
459
- self . chain_tip = latest_tip;
460
- blocks_connected = true ;
461
- }
462
- }
463
- } ,
464
- Ok ( _) => {
465
- self . chain_tip = * $new_header;
466
- blocks_connected = true ;
467
- } ,
468
- }
469
- blocks_connected
470
- } }
471
- }
472
-
473
452
let chain_tip = self . chain_poller . poll_chain_tip ( self . chain_tip ) . await ?;
474
- let blocks_connected = match & chain_tip {
453
+ let blocks_connected = match chain_tip {
475
454
ChainTip :: Common => false ,
476
455
ChainTip :: Better ( chain_tip) => {
477
456
debug_assert_ne ! ( chain_tip. block_hash, self . chain_tip. block_hash) ;
478
457
debug_assert ! ( chain_tip. chainwork > self . chain_tip. chainwork) ;
479
- sync_chain_monitor ! ( chain_tip)
458
+ self . update_chain_tip ( chain_tip) . await
480
459
} ,
481
460
ChainTip :: Worse ( chain_tip) => {
482
461
debug_assert_ne ! ( chain_tip. block_hash, self . chain_tip. block_hash) ;
@@ -486,6 +465,22 @@ where P: Poll,
486
465
} ;
487
466
Ok ( ( chain_tip, blocks_connected) )
488
467
}
468
+
469
+ /// Updates the chain tip, syncing the chain listener with any connected or disconnected
470
+ /// blocks. Returns whether there were any such blocks.
471
+ async fn update_chain_tip ( & mut self , best_chain_tip : ValidatedBlockHeader ) -> bool {
472
+ match sync_chain_monitor ( best_chain_tip, & self . chain_tip , & mut self . chain_poller , & mut self . chain_notifier , & mut self . header_cache ) . await {
473
+ Ok ( _) => {
474
+ self . chain_tip = best_chain_tip;
475
+ true
476
+ } ,
477
+ Err ( ( _, Some ( chain_tip) ) ) if chain_tip. block_hash != self . chain_tip . block_hash => {
478
+ self . chain_tip = chain_tip;
479
+ true
480
+ } ,
481
+ Err ( _) => false ,
482
+ }
483
+ }
489
484
}
490
485
491
486
#[ cfg( test) ]
0 commit comments