@@ -108,9 +108,8 @@ pub trait Poll<'b, B: DerefMut<Target=dyn BlockSource + 'b> + Sized + Sync + Sen
108
108
/// A chain tip relative to another chain tip in terms of block hash and chainwork.
109
109
#[ derive( Clone , Debug , PartialEq ) ]
110
110
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 ,
114
113
115
114
/// A chain tip with more chainwork than another chain's tip.
116
115
Better ( BlockHash , BlockHeaderData ) ,
@@ -350,6 +349,7 @@ pub(crate) type HeaderCache = std::collections::HashMap<BlockHash, BlockHeaderDa
350
349
/// difficulty transitions only happen every two weeks and never shift difficulty more than 4x in
351
350
/// either direction, which is sufficient to prevent most minority hashrate attacks.
352
351
///
352
+ /// TODO: Update comment as headers are removed from cache when blocks are disconnected.
353
353
/// We cache any headers which we connect until every block source is in agreement on the best tip.
354
354
/// This prevents one block source from being able to orphan us on a fork of its own creation by
355
355
/// 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>,
419
419
match self . chain_poller . poll_chain_tip ( self . chain_tip . 1 ) . await {
420
420
Err ( BlockSourceError :: Persistent ) => false ,
421
421
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 ,
428
423
Ok ( ( ChainTip :: Better ( new_hash, new_header) , block_source) ) => {
429
424
debug_assert_ne ! ( new_hash, self . chain_tip. 0 ) ;
430
425
debug_assert ! ( new_header. chainwork > self . chain_tip. 1 . chainwork) ;
@@ -838,15 +833,15 @@ mod tests {
838
833
chain_notifier. blocks_connected . lock ( ) . unwrap ( ) . clear ( ) ;
839
834
chain_notifier. blocks_disconnected . lock ( ) . unwrap ( ) . clear ( ) ;
840
835
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.
843
838
* chain_one. best_block . lock ( ) . unwrap ( ) = ( block_3a_hash, Some ( 3 ) ) ;
844
839
* header_chain. best_block . lock ( ) . unwrap ( ) = ( block_3a_hash, Some ( 3 ) ) ;
845
840
assert ! ( !client. poll_best_tip( ) . await ) ;
846
841
assert ! ( chain_notifier. blocks_disconnected. lock( ) . unwrap( ) . is_empty( ) ) ;
847
842
assert ! ( chain_notifier. blocks_connected. lock( ) . unwrap( ) . is_empty( ) ) ;
848
843
849
- assert ! ( client. header_cache. is_empty ( ) ) ;
844
+ assert_eq ! ( client. header_cache. len ( ) , 3 ) ;
850
845
851
846
// Test that setting the header chain to 4a does...almost nothing (though backup_chain
852
847
// should now be queried) since we can't get the blocks from anywhere.
@@ -859,7 +854,7 @@ mod tests {
859
854
assert ! ( !client. poll_best_tip( ) . await ) ;
860
855
assert ! ( chain_notifier. blocks_disconnected. lock( ) . unwrap( ) . is_empty( ) ) ;
861
856
assert ! ( chain_notifier. blocks_connected. lock( ) . unwrap( ) . is_empty( ) ) ;
862
- assert ! ( client. header_cache. is_empty ( ) ) ;
857
+ assert_eq ! ( client. header_cache. len ( ) , 3 ) ;
863
858
864
859
// But if backup_chain *also* has 4a, we'll fetch it from there:
865
860
backup_chain. blocks . lock ( ) . unwrap ( ) . insert ( block_4a_hash, block_4a) ;
@@ -868,7 +863,7 @@ mod tests {
868
863
assert ! ( client. poll_best_tip( ) . await ) ;
869
864
assert ! ( chain_notifier. blocks_disconnected. lock( ) . unwrap( ) . is_empty( ) ) ;
870
865
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 ) ;
872
867
assert ! ( client. header_cache. contains_key( & block_4a_hash) ) ;
873
868
874
869
chain_notifier. blocks_connected . lock ( ) . unwrap ( ) . clear ( ) ;
0 commit comments