diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs index 5e9857c094..91628cc7e9 100644 --- a/beacon_node/network/src/beacon_chain.rs +++ b/beacon_node/network/src/beacon_chain.rs @@ -1,13 +1,19 @@ use beacon_chain::BeaconChain as RawBeaconChain; use beacon_chain::{ - db::ClientDB, fork_choice::ForkChoice, parking_lot::RwLockReadGuard, slot_clock::SlotClock, - types::ChainSpec, CheckPoint, + db::ClientDB, + fork_choice::ForkChoice, + parking_lot::RwLockReadGuard, + slot_clock::SlotClock, + types::{BeaconState, ChainSpec}, + CheckPoint, }; /// The network's API to the beacon chain. pub trait BeaconChain: Send + Sync { fn get_spec(&self) -> &ChainSpec; + fn get_state(&self) -> RwLockReadGuard; + fn head(&self) -> RwLockReadGuard; fn finalized_head(&self) -> RwLockReadGuard; @@ -23,6 +29,10 @@ where &self.spec } + fn get_state(&self) -> RwLockReadGuard { + self.state.read() + } + fn head(&self) -> RwLockReadGuard { self.head() } diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 4034c63c90..336f225b2a 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,4 +1,5 @@ use crate::beacon_chain::BeaconChain; +use libp2p::rpc::HelloMessage; use libp2p::PeerId; use std::collections::HashMap; use std::sync::Arc; @@ -23,8 +24,13 @@ pub enum SyncState { //TODO: Decide for HELLO messages whether its better to keep current in RAM or build on the fly //when asked. pub struct SimpleSync { + /// A reference to the underlying beacon chain. + chain: Arc, + /// A mapping of Peers to their respective PeerSyncInfo. known_peers: HashMap, + /// The current state of the syncing protocol. state: SyncState, + /// The network id, for quick HELLO RPC message lookup. network_id: u8, } @@ -34,6 +40,20 @@ impl SimpleSync { known_peers: HashMap::new(), state: SyncState::Idle, network_id: beacon_chain.get_spec().network_id, + chain: beacon_chain, + } + } + + /// Generates our current state in the form of a HELLO RPC message. + pub fn generate_hello(&self) -> HelloMessage { + let state = &self.chain.get_state(); + //TODO: Paul to verify the logic of these fields. + HelloMessage { + network_id: self.network_id, + latest_finalized_root: state.finalized_root.clone(), + latest_finalized_epoch: state.finalized_epoch, + best_root: state.latest_block_roots[0], // 0 or len of vec? + best_slot: state.slot, } } }