Implement get_block_roots for syncing

This commit is contained in:
Paul Hauner
2019-03-22 14:20:49 +11:00
parent f96a3282b5
commit 96ba1c8f77
5 changed files with 146 additions and 20 deletions

View File

@@ -8,7 +8,7 @@ use beacon_chain::{
CheckPoint,
};
use eth2_libp2p::HelloMessage;
use types::{Epoch, Hash256, Slot};
use types::{BeaconStateError, Epoch, Hash256, Slot};
/// The network's API to the beacon chain.
pub trait BeaconChain: Send + Sync {
@@ -29,6 +29,12 @@ pub trait BeaconChain: Send + Sync {
fn finalized_epoch(&self) -> Epoch;
fn hello_message(&self) -> HelloMessage;
fn get_block_roots(
&self,
start_slot: Slot,
count: Slot,
) -> Result<Vec<Hash256>, BeaconStateError>;
}
impl<T, U, F> BeaconChain for RawBeaconChain<T, U, F>
@@ -81,4 +87,12 @@ where
best_slot: self.best_slot(),
}
}
fn get_block_roots(
&self,
start_slot: Slot,
count: Slot,
) -> Result<Vec<Hash256>, BeaconStateError> {
self.get_block_roots(start_slot, count)
}
}

View File

@@ -139,8 +139,19 @@ impl MessageHandler {
self.sync
.on_hello(peer_id, hello_message, &mut self.network_context);
}
RPCResponse::BeaconBlockRoots(response) => {
debug!(
self.log,
"BeaconBlockRoots response received from peer: {:?}", peer_id
);
self.sync.on_beacon_block_roots_response(
peer_id,
response,
&mut self.network_context,
)
}
// TODO: Handle all responses
_ => {}
_ => panic!("Unknown response: {:?}", response),
}
}
}

View File

@@ -165,6 +165,15 @@ impl SimpleSync {
}
}
pub fn on_beacon_block_roots_response(
&mut self,
peer_id: PeerId,
reponse: BeaconBlockRootsResponse,
network: &mut NetworkContext,
) {
//
}
fn request_block_roots(
&mut self,
peer_id: PeerId,
@@ -174,9 +183,12 @@ impl SimpleSync {
) {
// Potentially set state to sync.
if self.state == SyncState::Idle && count > SLOT_IMPORT_TOLERANCE {
debug!(self.log, "Entering downloading sync state.");
self.state = SyncState::Downloading;
}
debug!(self.log, "Requesting {} blocks from {:?}.", count, &peer_id);
// TODO: handle count > max count.
network.send_rpc_request(
peer_id.clone(),