@@ -17,7 +17,7 @@ use std::collections::{hash_map::Entry, HashMap};
17
17
use std:: sync:: Arc ;
18
18
use tokio_stream:: StreamExt ;
19
19
use types:: blob_sidecar:: BlobIdentifier ;
20
- use types:: { Epoch , EthSpec , FixedBytesExtended , Hash256 , Slot } ;
20
+ use types:: { Epoch , EthSpec , Hash256 , Slot } ;
21
21
22
22
impl < T : BeaconChainTypes > NetworkBeaconProcessor < T > {
23
23
/* Auxiliary functions */
@@ -93,20 +93,42 @@ impl<T: BeaconChainTypes> NetworkBeaconProcessor<T> {
93
93
// current slot. This could be because they are using a different genesis time, or that
94
94
// their or our system's clock is incorrect.
95
95
Some ( "Different system clocks or genesis time" . to_string ( ) )
96
- } else if remote. finalized_epoch <= local. finalized_epoch
97
- && remote. finalized_root != Hash256 :: zero ( )
98
- && local. finalized_root != Hash256 :: zero ( )
99
- && self
100
- . chain
101
- . block_root_at_slot ( start_slot ( remote. finalized_epoch ) , WhenSlotSkipped :: Prev )
102
- . map ( |root_opt| root_opt != Some ( remote. finalized_root ) ) ?
96
+ } else if ( remote. finalized_epoch == local. finalized_epoch
97
+ && remote. finalized_root == local. finalized_root )
98
+ || remote. finalized_root . is_zero ( )
99
+ || local. finalized_root . is_zero ( )
100
+ || remote. finalized_epoch > local. finalized_epoch
103
101
{
104
- // The remote's finalized epoch is less than or equal to ours, but the block root is
105
- // different to the one in our chain. Therefore, the node is on a different chain and we
106
- // should not communicate with them.
107
- Some ( "Different finalized chain" . to_string ( ) )
108
- } else {
102
+ // Fast path. Remote finalized checkpoint is either identical, or genesis, or we are at
103
+ // genesis, or they are ahead. In all cases, we should allow this peer to connect to us
104
+ // so we can sync from them.
109
105
None
106
+ } else {
107
+ // Remote finalized epoch is less than ours.
108
+ let remote_finalized_slot = start_slot ( remote. finalized_epoch ) ;
109
+ if remote_finalized_slot < self . chain . store . get_oldest_block_slot ( ) {
110
+ // Peer's finalized checkpoint is older than anything in our DB. We are unlikely
111
+ // to be able to help them sync.
112
+ Some ( "Old finality out of range" . to_string ( ) )
113
+ } else if remote_finalized_slot < self . chain . store . get_split_slot ( ) {
114
+ // Peer's finalized slot is in range for a quick block root check in our freezer DB.
115
+ // If that block root check fails, reject them as they're on a different finalized
116
+ // chain.
117
+ if self
118
+ . chain
119
+ . block_root_at_slot ( remote_finalized_slot, WhenSlotSkipped :: Prev )
120
+ . map ( |root_opt| root_opt != Some ( remote. finalized_root ) ) ?
121
+ {
122
+ Some ( "Different finalized chain" . to_string ( ) )
123
+ } else {
124
+ None
125
+ }
126
+ } else {
127
+ // Peer's finality is older than ours, but newer than our split point, making a
128
+ // block root check infeasible. This case shouldn't happen particularly often so
129
+ // we give the peer the benefit of the doubt and let them connect to us.
130
+ None
131
+ }
110
132
} ;
111
133
112
134
Ok ( irrelevant_reason)
0 commit comments