Implements hello generation in sync module

This commit is contained in:
Age Manning
2019-03-19 00:26:15 +11:00
parent 41abdb7599
commit dfdec78a7a
2 changed files with 32 additions and 2 deletions

View File

@@ -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<BeaconChain>,
/// A mapping of Peers to their respective PeerSyncInfo.
known_peers: HashMap<PeerId, PeerSyncInfo>,
/// 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,
}
}
}