mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 10:22:38 +00:00
Update to frozen spec ❄️ (v0.8.1) (#444)
* types: first updates for v0.8 * state_processing: epoch processing v0.8.0 * state_processing: block processing v0.8.0 * tree_hash_derive: support generics in SignedRoot * types v0.8: update to use ssz_types * state_processing v0.8: use ssz_types * ssz_types: add bitwise methods and from_elem * types: fix v0.8 FIXMEs * ssz_types: add bitfield shift_up * ssz_types: iterators and DerefMut for VariableList * types,state_processing: use VariableList * ssz_types: fix BitVector Decode impl Fixed a typo in the implementation of ssz::Decode for BitVector, which caused it to be considered variable length! * types: fix test modules for v0.8 update * types: remove slow type-level arithmetic * state_processing: fix tests for v0.8 * op_pool: update for v0.8 * ssz_types: Bitfield difference length-independent Allow computing the difference of two bitfields of different lengths. * Implement compact committee support * epoch_processing: committee & active index roots * state_processing: genesis state builder v0.8 * state_processing: implement v0.8.1 * Further improve tree_hash * Strip examples, tests from cached_tree_hash * Update TreeHash, un-impl CachedTreeHash * Update bitfield TreeHash, un-impl CachedTreeHash * Update FixedLenVec TreeHash, unimpl CachedTreeHash * Update update tree_hash_derive for new TreeHash * Fix TreeHash, un-impl CachedTreeHash for ssz_types * Remove fixed_len_vec, ssz benches SSZ benches relied upon fixed_len_vec -- it is easier to just delete them and rebuild them later (when necessary) * Remove boolean_bitfield crate * Fix fake_crypto BLS compile errors * Update ef_tests for new v.8 type params * Update ef_tests submodule to v0.8.1 tag * Make fixes to support parsing ssz ef_tests * `compact_committee...` to `compact_committees...` * Derive more traits for `CompactCommittee` * Flip bitfield byte-endianness * Fix tree_hash for bitfields * Modify CLI output for ef_tests * Bump ssz crate version * Update ssz_types doc comment * Del cached tree hash tests from ssz_static tests * Tidy SSZ dependencies * Rename ssz_types crate to eth2_ssz_types * validator_client: update for v0.8 * ssz_types: update union/difference for bit order swap * beacon_node: update for v0.8, EthSpec * types: disable cached tree hash, update min spec * state_processing: fix slot bug in committee update * tests: temporarily disable fork choice harness test See #447 * committee cache: prevent out-of-bounds access In the case where we tried to access the committee of a shard that didn't have a committee in the current epoch, we were accessing elements beyond the end of the shuffling vector and panicking! This commit adds a check to make the failure safe and explicit. * fix bug in get_indexed_attestation and simplify There was a bug in our implementation of get_indexed_attestation whereby incorrect "committee indices" were used to index into the custody bitfield. The bug was only observable in the case where some bits of the custody bitfield were set to 1. The implementation has been simplified to remove the bug, and a test added. * state_proc: workaround for compact committees bug https://github.com/ethereum/eth2.0-specs/issues/1315 * v0.8: updates to make the EF tests pass * Remove redundant max operation checks. * Always supply both messages when checking attestation signatures -- allowing verification of an attestation with no signatures. * Swap the order of the fork and domain constant in `get_domain`, to match the spec. * rustfmt * ef_tests: add new epoch processing tests * Integrate v0.8 into master (compiles) * Remove unused crates, fix clippy lints * Replace v0.6.3 tags w/ v0.8.1 * Remove old comment * Ensure lmd ghost tests only run in release * Update readme
This commit is contained in:
committed by
Paul Hauner
parent
177df12149
commit
a236003a7b
@@ -14,7 +14,7 @@ use slog::{debug, warn};
|
||||
use ssz::{Decode, DecodeError};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
use types::BeaconBlockHeader;
|
||||
use types::{BeaconBlockHeader, EthSpec};
|
||||
|
||||
/// Handles messages received from the network and client and organises syncing.
|
||||
pub struct MessageHandler<T: BeaconChainTypes> {
|
||||
@@ -23,14 +23,14 @@ pub struct MessageHandler<T: BeaconChainTypes> {
|
||||
/// The syncing framework.
|
||||
sync: SimpleSync<T>,
|
||||
/// The context required to send messages to, and process messages from peers.
|
||||
network_context: NetworkContext,
|
||||
network_context: NetworkContext<T::EthSpec>,
|
||||
/// The `MessageHandler` logger.
|
||||
log: slog::Logger,
|
||||
}
|
||||
|
||||
/// Types of messages the handler can receive.
|
||||
#[derive(Debug)]
|
||||
pub enum HandlerMessage {
|
||||
pub enum HandlerMessage<E: EthSpec> {
|
||||
/// We have initiated a connection to a new peer.
|
||||
PeerDialed(PeerId),
|
||||
/// Peer has disconnected,
|
||||
@@ -38,17 +38,17 @@ pub enum HandlerMessage {
|
||||
/// An RPC response/request has been received.
|
||||
RPC(PeerId, RPCEvent),
|
||||
/// A gossip message has been received.
|
||||
PubsubMessage(PeerId, Box<PubsubMessage>),
|
||||
PubsubMessage(PeerId, Box<PubsubMessage<E>>),
|
||||
}
|
||||
|
||||
impl<T: BeaconChainTypes + 'static> MessageHandler<T> {
|
||||
/// Initializes and runs the MessageHandler.
|
||||
pub fn spawn(
|
||||
beacon_chain: Arc<BeaconChain<T>>,
|
||||
network_send: mpsc::UnboundedSender<NetworkMessage>,
|
||||
network_send: mpsc::UnboundedSender<NetworkMessage<T::EthSpec>>,
|
||||
executor: &tokio::runtime::TaskExecutor,
|
||||
log: slog::Logger,
|
||||
) -> error::Result<mpsc::UnboundedSender<HandlerMessage>> {
|
||||
) -> error::Result<mpsc::UnboundedSender<HandlerMessage<T::EthSpec>>> {
|
||||
debug!(log, "Service starting");
|
||||
|
||||
let (handler_send, handler_recv) = mpsc::unbounded_channel();
|
||||
@@ -78,7 +78,7 @@ impl<T: BeaconChainTypes + 'static> MessageHandler<T> {
|
||||
}
|
||||
|
||||
/// Handle all messages incoming from the network service.
|
||||
fn handle_message(&mut self, message: HandlerMessage) {
|
||||
fn handle_message(&mut self, message: HandlerMessage<T::EthSpec>) {
|
||||
match message {
|
||||
// we have initiated a connection to a peer
|
||||
HandlerMessage::PeerDialed(peer_id) => {
|
||||
@@ -222,7 +222,7 @@ impl<T: BeaconChainTypes + 'static> MessageHandler<T> {
|
||||
fn decode_block_bodies(
|
||||
&self,
|
||||
bodies_response: BeaconBlockBodiesResponse,
|
||||
) -> Result<DecodedBeaconBlockBodiesResponse, DecodeError> {
|
||||
) -> Result<DecodedBeaconBlockBodiesResponse<T::EthSpec>, DecodeError> {
|
||||
//TODO: Implement faster block verification before decoding entirely
|
||||
let block_bodies = Vec::from_ssz_bytes(&bodies_response.block_bodies)?;
|
||||
Ok(DecodedBeaconBlockBodiesResponse {
|
||||
@@ -249,7 +249,7 @@ impl<T: BeaconChainTypes + 'static> MessageHandler<T> {
|
||||
}
|
||||
|
||||
/// Handle RPC messages
|
||||
fn handle_gossip(&mut self, peer_id: PeerId, gossip_message: PubsubMessage) {
|
||||
fn handle_gossip(&mut self, peer_id: PeerId, gossip_message: PubsubMessage<T::EthSpec>) {
|
||||
match gossip_message {
|
||||
PubsubMessage::Block(message) => {
|
||||
let _should_forward_on =
|
||||
@@ -265,15 +265,15 @@ impl<T: BeaconChainTypes + 'static> MessageHandler<T> {
|
||||
}
|
||||
|
||||
// TODO: RPC Rewrite makes this struct fairly pointless
|
||||
pub struct NetworkContext {
|
||||
pub struct NetworkContext<E: EthSpec> {
|
||||
/// The network channel to relay messages to the Network service.
|
||||
network_send: mpsc::UnboundedSender<NetworkMessage>,
|
||||
network_send: mpsc::UnboundedSender<NetworkMessage<E>>,
|
||||
/// The `MessageHandler` logger.
|
||||
log: slog::Logger,
|
||||
}
|
||||
|
||||
impl NetworkContext {
|
||||
pub fn new(network_send: mpsc::UnboundedSender<NetworkMessage>, log: slog::Logger) -> Self {
|
||||
impl<E: EthSpec> NetworkContext<E> {
|
||||
pub fn new(network_send: mpsc::UnboundedSender<NetworkMessage<E>>, log: slog::Logger) -> Self {
|
||||
Self { network_send, log }
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ use crate::error;
|
||||
use crate::message_handler::{HandlerMessage, MessageHandler};
|
||||
use crate::NetworkConfig;
|
||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||
use core::marker::PhantomData;
|
||||
use eth2_libp2p::Service as LibP2PService;
|
||||
use eth2_libp2p::Topic;
|
||||
use eth2_libp2p::{Libp2pEvent, PeerId};
|
||||
@@ -10,16 +11,16 @@ use futures::prelude::*;
|
||||
use futures::Stream;
|
||||
use parking_lot::Mutex;
|
||||
use slog::{debug, info, o, trace};
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use tokio::runtime::TaskExecutor;
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
use types::EthSpec;
|
||||
|
||||
/// Service that handles communication between internal services and the eth2_libp2p network service.
|
||||
pub struct Service<T: BeaconChainTypes> {
|
||||
libp2p_service: Arc<Mutex<LibP2PService>>,
|
||||
libp2p_service: Arc<Mutex<LibP2PService<T::EthSpec>>>,
|
||||
_libp2p_exit: oneshot::Sender<()>,
|
||||
_network_send: mpsc::UnboundedSender<NetworkMessage>,
|
||||
_network_send: mpsc::UnboundedSender<NetworkMessage<T::EthSpec>>,
|
||||
_phantom: PhantomData<T>, //message_handler: MessageHandler,
|
||||
//message_handler_send: Sender<HandlerMessage>
|
||||
}
|
||||
@@ -30,9 +31,9 @@ impl<T: BeaconChainTypes + 'static> Service<T> {
|
||||
config: &NetworkConfig,
|
||||
executor: &TaskExecutor,
|
||||
log: slog::Logger,
|
||||
) -> error::Result<(Arc<Self>, mpsc::UnboundedSender<NetworkMessage>)> {
|
||||
) -> error::Result<(Arc<Self>, mpsc::UnboundedSender<NetworkMessage<T::EthSpec>>)> {
|
||||
// build the network channel
|
||||
let (network_send, network_recv) = mpsc::unbounded_channel::<NetworkMessage>();
|
||||
let (network_send, network_recv) = mpsc::unbounded_channel::<NetworkMessage<_>>();
|
||||
// launch message handler thread
|
||||
let message_handler_log = log.new(o!("Service" => "MessageHandler"));
|
||||
let message_handler_send = MessageHandler::spawn(
|
||||
@@ -64,15 +65,15 @@ impl<T: BeaconChainTypes + 'static> Service<T> {
|
||||
Ok((Arc::new(network_service), network_send))
|
||||
}
|
||||
|
||||
pub fn libp2p_service(&self) -> Arc<Mutex<LibP2PService>> {
|
||||
pub fn libp2p_service(&self) -> Arc<Mutex<LibP2PService<T::EthSpec>>> {
|
||||
self.libp2p_service.clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_service(
|
||||
libp2p_service: Arc<Mutex<LibP2PService>>,
|
||||
network_recv: mpsc::UnboundedReceiver<NetworkMessage>,
|
||||
message_handler_send: mpsc::UnboundedSender<HandlerMessage>,
|
||||
fn spawn_service<E: EthSpec>(
|
||||
libp2p_service: Arc<Mutex<LibP2PService<E>>>,
|
||||
network_recv: mpsc::UnboundedReceiver<NetworkMessage<E>>,
|
||||
message_handler_send: mpsc::UnboundedSender<HandlerMessage<E>>,
|
||||
executor: &TaskExecutor,
|
||||
log: slog::Logger,
|
||||
) -> error::Result<tokio::sync::oneshot::Sender<()>> {
|
||||
@@ -98,10 +99,10 @@ fn spawn_service(
|
||||
}
|
||||
|
||||
//TODO: Potentially handle channel errors
|
||||
fn network_service(
|
||||
libp2p_service: Arc<Mutex<LibP2PService>>,
|
||||
mut network_recv: mpsc::UnboundedReceiver<NetworkMessage>,
|
||||
mut message_handler_send: mpsc::UnboundedSender<HandlerMessage>,
|
||||
fn network_service<E: EthSpec>(
|
||||
libp2p_service: Arc<Mutex<LibP2PService<E>>>,
|
||||
mut network_recv: mpsc::UnboundedReceiver<NetworkMessage<E>>,
|
||||
mut message_handler_send: mpsc::UnboundedSender<HandlerMessage<E>>,
|
||||
log: slog::Logger,
|
||||
) -> impl futures::Future<Item = (), Error = eth2_libp2p::error::Error> {
|
||||
futures::future::poll_fn(move || -> Result<_, eth2_libp2p::error::Error> {
|
||||
@@ -175,14 +176,14 @@ fn network_service(
|
||||
|
||||
/// Types of messages that the network service can receive.
|
||||
#[derive(Debug)]
|
||||
pub enum NetworkMessage {
|
||||
pub enum NetworkMessage<E: EthSpec> {
|
||||
/// Send a message to libp2p service.
|
||||
//TODO: Define typing for messages across the wire
|
||||
Send(PeerId, OutgoingMessage),
|
||||
/// Publish a message to pubsub mechanism.
|
||||
Publish {
|
||||
topics: Vec<Topic>,
|
||||
message: Box<PubsubMessage>,
|
||||
message: Box<PubsubMessage<E>>,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use tree_hash::TreeHash;
|
||||
use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Hash256, Slot};
|
||||
use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, EthSpec, Hash256, Slot};
|
||||
|
||||
/// Provides a queue for fully and partially built `BeaconBlock`s.
|
||||
///
|
||||
@@ -23,7 +23,7 @@ use types::{BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Hash256, Slot};
|
||||
pub struct ImportQueue<T: BeaconChainTypes> {
|
||||
pub chain: Arc<BeaconChain<T>>,
|
||||
/// Partially imported blocks, keyed by the root of `BeaconBlockBody`.
|
||||
partials: HashMap<Hash256, PartialBeaconBlock>,
|
||||
partials: HashMap<Hash256, PartialBeaconBlock<T::EthSpec>>,
|
||||
/// Time before a queue entry is considered state.
|
||||
pub stale_time: Duration,
|
||||
/// Logging
|
||||
@@ -50,7 +50,10 @@ impl<T: BeaconChainTypes> ImportQueue<T> {
|
||||
///
|
||||
/// Returns an Enum with a `PartialBeaconBlockCompletion`.
|
||||
/// Does not remove the `block_root` from the `import_queue`.
|
||||
pub fn attempt_complete_block(&self, block_root: Hash256) -> PartialBeaconBlockCompletion {
|
||||
pub fn attempt_complete_block(
|
||||
&self,
|
||||
block_root: Hash256,
|
||||
) -> PartialBeaconBlockCompletion<T::EthSpec> {
|
||||
if let Some(partial) = self.partials.get(&block_root) {
|
||||
partial.attempt_complete()
|
||||
} else {
|
||||
@@ -60,7 +63,7 @@ impl<T: BeaconChainTypes> ImportQueue<T> {
|
||||
|
||||
/// Removes the first `PartialBeaconBlock` with a matching `block_root`, returning the partial
|
||||
/// if it exists.
|
||||
pub fn remove(&mut self, block_root: Hash256) -> Option<PartialBeaconBlock> {
|
||||
pub fn remove(&mut self, block_root: Hash256) -> Option<PartialBeaconBlock<T::EthSpec>> {
|
||||
self.partials.remove(&block_root)
|
||||
}
|
||||
|
||||
@@ -141,11 +144,11 @@ impl<T: BeaconChainTypes> ImportQueue<T> {
|
||||
for header in headers {
|
||||
let block_root = Hash256::from_slice(&header.canonical_root()[..]);
|
||||
|
||||
if self.chain_has_not_seen_block(&block_root) {
|
||||
if !self.insert_header(block_root, header, sender.clone()) {
|
||||
// If a body is empty
|
||||
required_bodies.push(block_root);
|
||||
}
|
||||
if self.chain_has_not_seen_block(&block_root)
|
||||
&& !self.insert_header(block_root, header, sender.clone())
|
||||
{
|
||||
// If a body is empty
|
||||
required_bodies.push(block_root);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +160,7 @@ impl<T: BeaconChainTypes> ImportQueue<T> {
|
||||
/// If there is no `header` for the `body`, the body is simply discarded.
|
||||
pub fn enqueue_bodies(
|
||||
&mut self,
|
||||
bodies: Vec<BeaconBlockBody>,
|
||||
bodies: Vec<BeaconBlockBody<T::EthSpec>>,
|
||||
sender: PeerId,
|
||||
) -> Option<Hash256> {
|
||||
let mut last_block_hash = None;
|
||||
@@ -168,7 +171,7 @@ impl<T: BeaconChainTypes> ImportQueue<T> {
|
||||
last_block_hash
|
||||
}
|
||||
|
||||
pub fn enqueue_full_blocks(&mut self, blocks: Vec<BeaconBlock>, sender: PeerId) {
|
||||
pub fn enqueue_full_blocks(&mut self, blocks: Vec<BeaconBlock<T::EthSpec>>, sender: PeerId) {
|
||||
for block in blocks {
|
||||
self.insert_full_block(block, sender.clone());
|
||||
}
|
||||
@@ -211,13 +214,17 @@ impl<T: BeaconChainTypes> ImportQueue<T> {
|
||||
/// If the body already existed, the `inserted` time is set to `now`.
|
||||
///
|
||||
/// Returns the block hash of the inserted body
|
||||
fn insert_body(&mut self, body: BeaconBlockBody, sender: PeerId) -> Option<Hash256> {
|
||||
fn insert_body(
|
||||
&mut self,
|
||||
body: BeaconBlockBody<T::EthSpec>,
|
||||
sender: PeerId,
|
||||
) -> Option<Hash256> {
|
||||
let body_root = Hash256::from_slice(&body.tree_hash_root()[..]);
|
||||
let mut last_root = None;
|
||||
|
||||
self.partials.iter_mut().for_each(|(root, mut p)| {
|
||||
if let Some(header) = &mut p.header {
|
||||
if body_root == header.block_body_root {
|
||||
if body_root == header.body_root {
|
||||
p.inserted = Instant::now();
|
||||
p.body = Some(body.clone());
|
||||
p.sender = sender.clone();
|
||||
@@ -232,7 +239,7 @@ impl<T: BeaconChainTypes> ImportQueue<T> {
|
||||
/// Updates an existing `partial` with the completed block, or adds a new (complete) partial.
|
||||
///
|
||||
/// If the partial already existed, the `inserted` time is set to `now`.
|
||||
fn insert_full_block(&mut self, block: BeaconBlock, sender: PeerId) {
|
||||
fn insert_full_block(&mut self, block: BeaconBlock<T::EthSpec>, sender: PeerId) {
|
||||
let block_root = Hash256::from_slice(&block.canonical_root()[..]);
|
||||
|
||||
let partial = PartialBeaconBlock {
|
||||
@@ -254,12 +261,12 @@ impl<T: BeaconChainTypes> ImportQueue<T> {
|
||||
/// Individual components of a `BeaconBlock`, potentially all that are required to form a full
|
||||
/// `BeaconBlock`.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PartialBeaconBlock {
|
||||
pub struct PartialBeaconBlock<E: EthSpec> {
|
||||
pub slot: Slot,
|
||||
/// `BeaconBlock` root.
|
||||
pub block_root: Hash256,
|
||||
pub header: Option<BeaconBlockHeader>,
|
||||
pub body: Option<BeaconBlockBody>,
|
||||
pub body: Option<BeaconBlockBody<E>>,
|
||||
/// The instant at which this record was created or last meaningfully modified. Used to
|
||||
/// determine if an entry is stale and should be removed.
|
||||
pub inserted: Instant,
|
||||
@@ -267,11 +274,11 @@ pub struct PartialBeaconBlock {
|
||||
pub sender: PeerId,
|
||||
}
|
||||
|
||||
impl PartialBeaconBlock {
|
||||
impl<E: EthSpec> PartialBeaconBlock<E> {
|
||||
/// Attempts to build a block.
|
||||
///
|
||||
/// Does not consume the `PartialBeaconBlock`.
|
||||
pub fn attempt_complete(&self) -> PartialBeaconBlockCompletion {
|
||||
/// Does not comsume the `PartialBeaconBlock`.
|
||||
pub fn attempt_complete(&self) -> PartialBeaconBlockCompletion<E> {
|
||||
if self.header.is_none() {
|
||||
PartialBeaconBlockCompletion::MissingHeader(self.slot)
|
||||
} else if self.body.is_none() {
|
||||
@@ -288,9 +295,9 @@ impl PartialBeaconBlock {
|
||||
}
|
||||
|
||||
/// The result of trying to convert a `BeaconBlock` into a `PartialBeaconBlock`.
|
||||
pub enum PartialBeaconBlockCompletion {
|
||||
pub enum PartialBeaconBlockCompletion<E: EthSpec> {
|
||||
/// The partial contains a valid BeaconBlock.
|
||||
Complete(BeaconBlock),
|
||||
Complete(BeaconBlock<E>),
|
||||
/// The partial does not exist.
|
||||
MissingRoot,
|
||||
/// The partial contains a `BeaconBlockRoot` but no `BeaconBlockHeader`.
|
||||
|
||||
@@ -123,7 +123,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
/// Handle the connection of a new peer.
|
||||
///
|
||||
/// Sends a `Hello` message to the peer.
|
||||
pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext) {
|
||||
pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext<T::EthSpec>) {
|
||||
info!(self.log, "PeerConnected"; "peer" => format!("{:?}", peer_id));
|
||||
|
||||
network.send_rpc_request(peer_id, RPCRequest::Hello(hello_message(&self.chain)));
|
||||
@@ -137,7 +137,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
peer_id: PeerId,
|
||||
request_id: RequestId,
|
||||
hello: HelloMessage,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
debug!(self.log, "HelloRequest"; "peer" => format!("{:?}", peer_id));
|
||||
|
||||
@@ -156,7 +156,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
hello: HelloMessage,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
debug!(self.log, "HelloResponse"; "peer" => format!("{:?}", peer_id));
|
||||
|
||||
@@ -171,7 +171,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
hello: HelloMessage,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
let remote = PeerSyncInfo::from(hello);
|
||||
let local = PeerSyncInfo::from(&self.chain);
|
||||
@@ -188,8 +188,8 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
|
||||
network.disconnect(peer_id.clone(), GoodbyeReason::IrrelevantNetwork);
|
||||
} else if remote.latest_finalized_epoch <= local.latest_finalized_epoch
|
||||
&& remote.latest_finalized_root != self.chain.spec.zero_hash
|
||||
&& local.latest_finalized_root != self.chain.spec.zero_hash
|
||||
&& remote.latest_finalized_root != Hash256::zero()
|
||||
&& local.latest_finalized_root != Hash256::zero()
|
||||
&& (self.root_at_slot(start_slot(remote.latest_finalized_epoch))
|
||||
!= Some(remote.latest_finalized_root))
|
||||
{
|
||||
@@ -226,7 +226,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
} else if self
|
||||
.chain
|
||||
.store
|
||||
.exists::<BeaconBlock>(&remote.best_root)
|
||||
.exists::<BeaconBlock<T::EthSpec>>(&remote.best_root)
|
||||
.unwrap_or_else(|_| false)
|
||||
{
|
||||
// If the node's best-block is already known to us, we have nothing to request.
|
||||
@@ -278,7 +278,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
peer_id: PeerId,
|
||||
request_id: RequestId,
|
||||
req: BeaconBlockRootsRequest,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
debug!(
|
||||
self.log,
|
||||
@@ -323,7 +323,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
res: BeaconBlockRootsResponse,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
debug!(
|
||||
self.log,
|
||||
@@ -387,7 +387,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
peer_id: PeerId,
|
||||
request_id: RequestId,
|
||||
req: BeaconBlockHeadersRequest,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
debug!(
|
||||
self.log,
|
||||
@@ -416,7 +416,11 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
.into_iter()
|
||||
.step_by(req.skip_slots as usize + 1)
|
||||
.filter_map(|root| {
|
||||
let block = self.chain.store.get::<BeaconBlock>(&root).ok()?;
|
||||
let block = self
|
||||
.chain
|
||||
.store
|
||||
.get::<BeaconBlock<T::EthSpec>>(&root)
|
||||
.ok()?;
|
||||
Some(block?.block_header())
|
||||
})
|
||||
.collect();
|
||||
@@ -436,7 +440,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
headers: Vec<BeaconBlockHeader>,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
debug!(
|
||||
self.log,
|
||||
@@ -468,13 +472,13 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
peer_id: PeerId,
|
||||
request_id: RequestId,
|
||||
req: BeaconBlockBodiesRequest,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
let block_bodies: Vec<BeaconBlockBody> = req
|
||||
let block_bodies: Vec<BeaconBlockBody<_>> = req
|
||||
.block_roots
|
||||
.iter()
|
||||
.filter_map(|root| {
|
||||
if let Ok(Some(block)) = self.chain.store.get::<BeaconBlock>(root) {
|
||||
if let Ok(Some(block)) = self.chain.store.get::<BeaconBlock<T::EthSpec>>(root) {
|
||||
Some(block.body)
|
||||
} else {
|
||||
debug!(
|
||||
@@ -513,8 +517,8 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
pub fn on_beacon_block_bodies_response(
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
res: DecodedBeaconBlockBodiesResponse,
|
||||
network: &mut NetworkContext,
|
||||
res: DecodedBeaconBlockBodiesResponse<T::EthSpec>,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
debug!(
|
||||
self.log,
|
||||
@@ -531,12 +535,11 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
|
||||
// Attempt to process all received bodies by recursively processing the latest block
|
||||
if let Some(root) = last_root {
|
||||
match self.attempt_process_partial_block(peer_id, root, network, &"rpc") {
|
||||
Some(BlockProcessingOutcome::Processed { block_root: _ }) => {
|
||||
// If processing is successful remove from `import_queue`
|
||||
self.import_queue.remove(root);
|
||||
}
|
||||
_ => {}
|
||||
if let Some(BlockProcessingOutcome::Processed { .. }) =
|
||||
self.attempt_process_partial_block(peer_id, root, network, &"rpc")
|
||||
{
|
||||
// If processing is successful remove from `import_queue`
|
||||
self.import_queue.remove(root);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -553,8 +556,8 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
pub fn on_block_gossip(
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
block: BeaconBlock,
|
||||
network: &mut NetworkContext,
|
||||
block: BeaconBlock<T::EthSpec>,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) -> bool {
|
||||
if let Some(outcome) =
|
||||
self.process_block(peer_id.clone(), block.clone(), network, &"gossip")
|
||||
@@ -577,7 +580,8 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
.chain
|
||||
.head()
|
||||
.beacon_state
|
||||
.finalized_epoch
|
||||
.finalized_checkpoint
|
||||
.epoch
|
||||
.start_slot(T::EthSpec::slots_per_epoch());
|
||||
self.request_block_roots(
|
||||
peer_id,
|
||||
@@ -622,8 +626,8 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
pub fn on_attestation_gossip(
|
||||
&mut self,
|
||||
_peer_id: PeerId,
|
||||
msg: Attestation,
|
||||
_network: &mut NetworkContext,
|
||||
msg: Attestation<T::EthSpec>,
|
||||
_network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
match self.chain.process_attestation(msg) {
|
||||
Ok(()) => info!(self.log, "ImportedAttestation"; "source" => "gossip"),
|
||||
@@ -638,7 +642,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
req: BeaconBlockRootsRequest,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
// Potentially set state to sync.
|
||||
if self.state == SyncState::Idle && req.count > SLOT_IMPORT_TOLERANCE {
|
||||
@@ -662,7 +666,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
req: BeaconBlockHeadersRequest,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
debug!(
|
||||
self.log,
|
||||
@@ -679,7 +683,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
req: BeaconBlockBodiesRequest,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
) {
|
||||
debug!(
|
||||
self.log,
|
||||
@@ -715,7 +719,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
block_root: Hash256,
|
||||
network: &mut NetworkContext,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
source: &str,
|
||||
) -> Option<BlockProcessingOutcome> {
|
||||
match self.import_queue.attempt_complete_block(block_root) {
|
||||
@@ -807,8 +811,8 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
fn process_block(
|
||||
&mut self,
|
||||
peer_id: PeerId,
|
||||
block: BeaconBlock,
|
||||
network: &mut NetworkContext,
|
||||
block: BeaconBlock<T::EthSpec>,
|
||||
network: &mut NetworkContext<T::EthSpec>,
|
||||
source: &str,
|
||||
) -> Option<BlockProcessingOutcome> {
|
||||
let processing_result = self.chain.process_block(block.clone());
|
||||
@@ -836,19 +840,18 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
||||
);
|
||||
|
||||
// If the parent is in the `import_queue` attempt to complete it then process it.
|
||||
match self.attempt_process_partial_block(peer_id, parent, network, source) {
|
||||
// All other cases leave `parent` in `import_queue` and return original outcome.
|
||||
if let Some(BlockProcessingOutcome::Processed { .. }) =
|
||||
self.attempt_process_partial_block(peer_id, parent, network, source)
|
||||
{
|
||||
// If processing parent is successful, re-process block and remove parent from queue
|
||||
Some(BlockProcessingOutcome::Processed { block_root: _ }) => {
|
||||
self.import_queue.remove(parent);
|
||||
self.import_queue.remove(parent);
|
||||
|
||||
// Attempt to process `block` again
|
||||
match self.chain.process_block(block) {
|
||||
Ok(outcome) => return Some(outcome),
|
||||
Err(_) => return None,
|
||||
}
|
||||
// Attempt to process `block` again
|
||||
match self.chain.process_block(block) {
|
||||
Ok(outcome) => return Some(outcome),
|
||||
Err(_) => return None,
|
||||
}
|
||||
// All other cases leave `parent` in `import_queue` and return original outcome.
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
BlockProcessingOutcome::FutureSlot {
|
||||
@@ -913,9 +916,9 @@ fn hello_message<T: BeaconChainTypes>(beacon_chain: &BeaconChain<T>) -> HelloMes
|
||||
HelloMessage {
|
||||
//TODO: Correctly define the chain/network id
|
||||
network_id: spec.chain_id,
|
||||
chain_id: spec.chain_id as u64,
|
||||
latest_finalized_root: state.finalized_root,
|
||||
latest_finalized_epoch: state.finalized_epoch,
|
||||
chain_id: u64::from(spec.chain_id),
|
||||
latest_finalized_root: state.finalized_checkpoint.root,
|
||||
latest_finalized_epoch: state.finalized_checkpoint.epoch,
|
||||
best_root: beacon_chain.head().beacon_block_root,
|
||||
best_slot: state.slot,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user