2020-04-22 13:58:10 +00:00
|
|
|
use super::manager::SLOT_IMPORT_TOLERANCE;
|
|
|
|
use crate::router::processor::status_message;
|
|
|
|
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
2020-06-05 03:07:59 +00:00
|
|
|
use eth2_libp2p::rpc::*;
|
2020-04-22 13:58:10 +00:00
|
|
|
use eth2_libp2p::SyncInfo;
|
|
|
|
use std::ops::Sub;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use types::{Epoch, Hash256, Slot};
|
|
|
|
|
|
|
|
/// Keeps track of syncing information for known connected peers.
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct PeerSyncInfo {
|
|
|
|
pub fork_digest: [u8; 4],
|
|
|
|
pub finalized_root: Hash256,
|
|
|
|
pub finalized_epoch: Epoch,
|
|
|
|
pub head_root: Hash256,
|
|
|
|
pub head_slot: Slot,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The type of peer relative to our current state.
|
|
|
|
pub enum PeerSyncType {
|
|
|
|
/// The peer is on our chain and is fully synced with respect to our chain.
|
|
|
|
FullySynced,
|
2020-06-11 02:06:42 +00:00
|
|
|
/// The peer has a greater knowledge of the chain than us that warrants a full sync.
|
2020-04-22 13:58:10 +00:00
|
|
|
Advanced,
|
|
|
|
/// A peer is behind in the sync and not useful to us for downloading blocks.
|
|
|
|
Behind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<StatusMessage> for PeerSyncInfo {
|
|
|
|
fn from(status: StatusMessage) -> PeerSyncInfo {
|
|
|
|
PeerSyncInfo {
|
|
|
|
fork_digest: status.fork_digest,
|
|
|
|
finalized_root: status.finalized_root,
|
|
|
|
finalized_epoch: status.finalized_epoch,
|
|
|
|
head_root: status.head_root,
|
|
|
|
head_slot: status.head_slot,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<SyncInfo> for PeerSyncInfo {
|
|
|
|
fn into(self) -> SyncInfo {
|
|
|
|
SyncInfo {
|
|
|
|
status_head_slot: self.head_slot,
|
|
|
|
status_head_root: self.head_root,
|
|
|
|
status_finalized_epoch: self.finalized_epoch,
|
|
|
|
status_finalized_root: self.finalized_root,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PeerSyncInfo {
|
|
|
|
/// Derives the peer sync information from a beacon chain.
|
|
|
|
pub fn from_chain<T: BeaconChainTypes>(chain: &Arc<BeaconChain<T>>) -> Option<PeerSyncInfo> {
|
|
|
|
Some(Self::from(status_message(chain)?))
|
|
|
|
}
|
|
|
|
|
2020-06-11 02:06:42 +00:00
|
|
|
/// Given another peer's `PeerSyncInfo` this will determine how useful that peer is to us in
|
|
|
|
/// regards to syncing. This returns the peer sync type that can then be handled by the
|
2020-04-22 13:58:10 +00:00
|
|
|
/// `SyncManager`.
|
|
|
|
pub fn peer_sync_type(&self, remote_peer_sync_info: &PeerSyncInfo) -> PeerSyncType {
|
|
|
|
// check if the peer is fully synced with our current chain
|
|
|
|
if self.is_fully_synced_peer(remote_peer_sync_info) {
|
|
|
|
PeerSyncType::FullySynced
|
|
|
|
}
|
|
|
|
// if not, check if the peer is ahead of our chain
|
2020-04-23 09:01:29 +00:00
|
|
|
else if self.is_advanced_peer(remote_peer_sync_info) {
|
2020-04-22 13:58:10 +00:00
|
|
|
PeerSyncType::Advanced
|
|
|
|
} else {
|
|
|
|
// the peer must be behind and not useful
|
|
|
|
PeerSyncType::Behind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determines if another peer is fully synced with the current peer.
|
|
|
|
///
|
|
|
|
/// A fully synced peer is a peer whose finalized epoch and hash match our own and their
|
|
|
|
/// head is within SLOT_IMPORT_TOLERANCE of our own.
|
|
|
|
/// In this case we ignore any batch/range syncing.
|
|
|
|
fn is_fully_synced_peer(&self, remote: &PeerSyncInfo) -> bool {
|
|
|
|
// ensure we are on the same chain, with minor differing heads
|
|
|
|
if remote.finalized_epoch == self.finalized_epoch
|
|
|
|
&& remote.finalized_root == self.finalized_root
|
|
|
|
{
|
|
|
|
// that we are within SLOT_IMPORT_TOLERANCE of our two heads
|
|
|
|
if (self.head_slot >= remote.head_slot
|
|
|
|
&& self.head_slot.sub(remote.head_slot).as_usize() <= SLOT_IMPORT_TOLERANCE)
|
2020-08-17 03:00:10 +00:00
|
|
|
|| (self.head_slot < remote.head_slot
|
|
|
|
&& remote.head_slot.sub(self.head_slot).as_usize() <= SLOT_IMPORT_TOLERANCE)
|
2020-04-22 13:58:10 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determines if a peer has more knowledge about the current chain than we do.
|
|
|
|
///
|
|
|
|
/// There are two conditions here.
|
|
|
|
/// 1) The peer could have a head slot that is greater
|
|
|
|
/// than SLOT_IMPORT_TOLERANCE of our current head.
|
|
|
|
/// 2) The peer has a greater finalized slot/epoch than our own.
|
2020-04-23 09:01:29 +00:00
|
|
|
fn is_advanced_peer(&self, remote: &PeerSyncInfo) -> bool {
|
2020-07-23 14:18:00 +00:00
|
|
|
remote.head_slot.sub(self.head_slot).as_usize() > SLOT_IMPORT_TOLERANCE
|
2020-04-22 13:58:10 +00:00
|
|
|
|| self.finalized_epoch < remote.finalized_epoch
|
|
|
|
}
|
|
|
|
}
|