Skip to content

Commit 4715265

Browse files
committed
Remove common tip detection across block sources
1 parent f717530 commit 4715265

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

lightning-block-sync/src/lib.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ pub trait Poll<'b, B: DerefMut<Target=dyn BlockSource + 'b> + Sized + Sync + Sen
108108
/// A chain tip relative to another chain tip in terms of block hash and chainwork.
109109
#[derive(Clone, Debug, PartialEq)]
110110
pub enum ChainTip {
111-
/// A chain tip with the same hash as another chain's tip. The boolean indicates whether every
112-
/// block source polled returned the same tip.
113-
Common(bool),
111+
/// A chain tip with the same hash as another chain's tip.
112+
Common,
114113

115114
/// A chain tip with more chainwork than another chain's tip.
116115
Better(BlockHash, BlockHeaderData),
@@ -350,6 +349,7 @@ pub(crate) type HeaderCache = std::collections::HashMap<BlockHash, BlockHeaderDa
350349
/// difficulty transitions only happen every two weeks and never shift difficulty more than 4x in
351350
/// either direction, which is sufficient to prevent most minority hashrate attacks.
352351
///
352+
/// TODO: Update comment as headers are removed from cache when blocks are disconnected.
353353
/// We cache any headers which we connect until every block source is in agreement on the best tip.
354354
/// This prevents one block source from being able to orphan us on a fork of its own creation by
355355
/// not responding to requests for old headers on that fork. However, if one block source is
@@ -419,12 +419,7 @@ where P: Poll<'a, B>,
419419
match self.chain_poller.poll_chain_tip(self.chain_tip.1).await {
420420
Err(BlockSourceError::Persistent) => false,
421421
Err(BlockSourceError::Transient) => false,
422-
Ok((ChainTip::Common(all_common), _)) => {
423-
if all_common {
424-
self.header_cache.clear();
425-
}
426-
false
427-
},
422+
Ok((ChainTip::Common, _)) => false,
428423
Ok((ChainTip::Better(new_hash, new_header), block_source)) => {
429424
debug_assert_ne!(new_hash, self.chain_tip.0);
430425
debug_assert!(new_header.chainwork > self.chain_tip.1.chainwork);
@@ -838,15 +833,15 @@ mod tests {
838833
chain_notifier.blocks_connected.lock().unwrap().clear();
839834
chain_notifier.blocks_disconnected.lock().unwrap().clear();
840835

841-
// Test that after chain_one and header_chain consider 3a as their tip that we'll wipe our
842-
// block header cache:
836+
// Test that after chain_one and header_chain consider 3a as their tip that we won't wipe
837+
// the block header cache.
843838
*chain_one.best_block.lock().unwrap() = (block_3a_hash, Some(3));
844839
*header_chain.best_block.lock().unwrap() = (block_3a_hash, Some(3));
845840
assert!(!client.poll_best_tip().await);
846841
assert!(chain_notifier.blocks_disconnected.lock().unwrap().is_empty());
847842
assert!(chain_notifier.blocks_connected.lock().unwrap().is_empty());
848843

849-
assert!(client.header_cache.is_empty());
844+
assert_eq!(client.header_cache.len(), 3);
850845

851846
// Test that setting the header chain to 4a does...almost nothing (though backup_chain
852847
// should now be queried) since we can't get the blocks from anywhere.
@@ -859,7 +854,7 @@ mod tests {
859854
assert!(!client.poll_best_tip().await);
860855
assert!(chain_notifier.blocks_disconnected.lock().unwrap().is_empty());
861856
assert!(chain_notifier.blocks_connected.lock().unwrap().is_empty());
862-
assert!(client.header_cache.is_empty());
857+
assert_eq!(client.header_cache.len(), 3);
863858

864859
// But if backup_chain *also* has 4a, we'll fetch it from there:
865860
backup_chain.blocks.lock().unwrap().insert(block_4a_hash, block_4a);
@@ -868,7 +863,7 @@ mod tests {
868863
assert!(client.poll_best_tip().await);
869864
assert!(chain_notifier.blocks_disconnected.lock().unwrap().is_empty());
870865
assert_eq!(&chain_notifier.blocks_connected.lock().unwrap()[..], &[(block_4a_hash, 4)][..]);
871-
assert_eq!(client.header_cache.len(), 1);
866+
assert_eq!(client.header_cache.len(), 4);
872867
assert!(client.header_cache.contains_key(&block_4a_hash));
873868

874869
chain_notifier.blocks_connected.lock().unwrap().clear();

lightning-block-sync/src/poller.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'b, B: DerefMut<Target=dyn BlockSource + 'b> + Sized + Sync + Send> Poll<'b
2222
Err(e) => Err(e),
2323
Ok((block_hash, height)) => {
2424
if block_hash == best_chain_tip.header.block_hash() {
25-
return Ok((ChainTip::Common(true), &mut *self.block_source));
25+
return Ok((ChainTip::Common, &mut *self.block_source));
2626
}
2727

2828
match self.block_source.get_header(&block_hash, height).await {
@@ -66,8 +66,6 @@ impl<'b, B: DerefMut<Target=dyn BlockSource + 'b> + Sized + Sync + Send> Poll<'b
6666
Box::pin(async move {
6767
let mut heaviest_chain_tip = best_chain_tip;
6868
let mut best_result = Err(BlockSourceError::Persistent);
69-
let mut common = 0;
70-
let num_pollers = self.pollers.len();
7169
for (poller, error) in self.pollers.iter_mut() {
7270
if let BlockSourceError::Persistent = error {
7371
continue;
@@ -83,11 +81,9 @@ impl<'b, B: DerefMut<Target=dyn BlockSource + 'b> + Sized + Sync + Send> Poll<'b
8381
best_result = result;
8482
}
8583
},
86-
Ok((ChainTip::Common(_), source)) => {
87-
common += 1;
84+
Ok((ChainTip::Common, source)) => {
8885
if let Ok((ChainTip::Better(_, _), _)) = best_result {} else {
89-
let all_common = common == num_pollers;
90-
best_result = Ok((ChainTip::Common(all_common), source));
86+
best_result = Ok((ChainTip::Common, source));
9187
}
9288
},
9389
Ok((ChainTip::Better(_, header), _)) => {
@@ -174,7 +170,7 @@ mod tests {
174170
let mut poller = ChainPoller::new(&mut chain as &mut dyn BlockSource);
175171
match poller.poll_chain_tip(best_chain_tip).await {
176172
Err(e) => panic!("Unexpected error: {:?}", e),
177-
Ok((tip, _)) => assert_eq!(tip, ChainTip::Common(true)),
173+
Ok((tip, _)) => assert_eq!(tip, ChainTip::Common),
178174
}
179175
}
180176

0 commit comments

Comments
 (0)