Enable syncing state when new peer connects

This commit is contained in:
Age Manning 2019-03-19 22:32:56 +11:00
parent 0a8b0069dc
commit dc014d07bc
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
2 changed files with 16 additions and 5 deletions

View File

@ -110,9 +110,10 @@ fn network_service(
loop { loop {
match libp2p_service.poll() { match libp2p_service.poll() {
Ok(Async::Ready(Some(Libp2pEvent::RPC(peer_id, rpc_event)))) => { Ok(Async::Ready(Some(Libp2pEvent::RPC(peer_id, rpc_event)))) => {
debug!( trace!(
libp2p_service.log, libp2p_service.log,
"RPC Event: RPC message received: {:?}", rpc_event "RPC Event: RPC message received: {:?}",
rpc_event
); );
message_handler_send message_handler_send
.send(HandlerMessage::RPC(peer_id, rpc_event)) .send(HandlerMessage::RPC(peer_id, rpc_event))

View File

@ -6,6 +6,9 @@ use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use types::{Epoch, Hash256, Slot}; use types::{Epoch, Hash256, Slot};
/// The number of slots that we can import blocks ahead of us, before going into full Sync mode.
const SLOT_IMPORT_TOLERANCE: u64 = 100;
/// Keeps track of syncing information for known connected peers. /// Keeps track of syncing information for known connected peers.
pub struct PeerSyncInfo { pub struct PeerSyncInfo {
latest_finalized_root: Hash256, latest_finalized_root: Hash256,
@ -15,6 +18,7 @@ pub struct PeerSyncInfo {
} }
/// The current syncing state. /// The current syncing state.
#[derive(PartialEq)]
pub enum SyncState { pub enum SyncState {
Idle, Idle,
Downloading, Downloading,
@ -36,7 +40,7 @@ pub struct SimpleSync {
/// The latest epoch of the syncing chain. /// The latest epoch of the syncing chain.
latest_finalized_epoch: Epoch, latest_finalized_epoch: Epoch,
/// The latest block of the syncing chain. /// The latest block of the syncing chain.
latest_block: Hash256, latest_slot: Slot,
/// Sync logger. /// Sync logger.
log: slog::Logger, log: slog::Logger,
} }
@ -51,7 +55,7 @@ impl SimpleSync {
state: SyncState::Idle, state: SyncState::Idle,
network_id: beacon_chain.get_spec().network_id, network_id: beacon_chain.get_spec().network_id,
latest_finalized_epoch: state.finalized_epoch, latest_finalized_epoch: state.finalized_epoch,
latest_block: state.finalized_root, //TODO: Build latest block function into Beacon chain and correct this latest_slot: state.slot - 1, //TODO: Build latest block function into Beacon chain and correct this
log: sync_logger, log: sync_logger,
} }
} }
@ -95,7 +99,13 @@ impl SimpleSync {
debug!(self.log, "Handshake successful. Peer: {:?}", peer_id); debug!(self.log, "Handshake successful. Peer: {:?}", peer_id);
self.known_peers.insert(peer_id, peer_info); self.known_peers.insert(peer_id, peer_info);
//TODO: Start syncing // set state to sync
if self.state == SyncState::Idle
&& hello_message.best_slot > self.latest_slot + SLOT_IMPORT_TOLERANCE
{
self.state = SyncState::Downloading;
//TODO: Start requesting blocks from known peers. Ideally in batches
}
true true
} }