Skip to content

Commit b0d4b20

Browse files
committed
Refactor sync_chain_monitor macro into a method
1 parent 9d36b29 commit b0d4b20

File tree

1 file changed

+19
-24
lines changed
  • lightning-block-sync/src

1 file changed

+19
-24
lines changed

lightning-block-sync/src/lib.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ async fn sync_chain_monitor<CL: ChainListener + Sized, P: Poll>(new_header: Vali
367367
.fetch_block(&header).await
368368
.or_else(|e| Err((e, new_tip)))?;
369369
debug_assert_eq!(block.block_hash, header.block_hash);
370+
370371
println!("Connecting block {}", header.block_hash.to_hex());
371372
chain_notifier.block_connected(&block, header.height);
372373
header_cache.insert(header.block_hash, header);
@@ -448,35 +449,13 @@ where P: Poll,
448449
/// Check each source for a new best tip and update the chain listener accordingly.
449450
/// Returns true if some blocks were [dis]connected, false otherwise.
450451
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-
473452
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 {
475454
ChainTip::Common => false,
476455
ChainTip::Better(chain_tip) => {
477456
debug_assert_ne!(chain_tip.block_hash, self.chain_tip.block_hash);
478457
debug_assert!(chain_tip.chainwork > self.chain_tip.chainwork);
479-
sync_chain_monitor!(chain_tip)
458+
self.update_chain_tip(chain_tip).await
480459
},
481460
ChainTip::Worse(chain_tip) => {
482461
debug_assert_ne!(chain_tip.block_hash, self.chain_tip.block_hash);
@@ -486,6 +465,22 @@ where P: Poll,
486465
};
487466
Ok((chain_tip, blocks_connected))
488467
}
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+
}
489484
}
490485

491486
#[cfg(test)]

0 commit comments

Comments
 (0)