Skip to content

Commit abbf44a

Browse files
authored
Merge of #7082
2 parents 166f6df + f515a3e commit abbf44a

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

beacon_node/network/src/network_beacon_processor/rpc_methods.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::collections::{hash_map::Entry, HashMap};
1717
use std::sync::Arc;
1818
use tokio_stream::StreamExt;
1919
use types::blob_sidecar::BlobIdentifier;
20-
use types::{Epoch, EthSpec, FixedBytesExtended, Hash256, Slot};
20+
use types::{Epoch, EthSpec, Hash256, Slot};
2121

2222
impl<T: BeaconChainTypes> NetworkBeaconProcessor<T> {
2323
/* Auxiliary functions */
@@ -93,20 +93,42 @@ impl<T: BeaconChainTypes> NetworkBeaconProcessor<T> {
9393
// current slot. This could be because they are using a different genesis time, or that
9494
// their or our system's clock is incorrect.
9595
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
103101
{
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.
109105
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+
}
110132
};
111133

112134
Ok(irrelevant_reason)

0 commit comments

Comments
 (0)