diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs deleted file mode 100644 index e38acbb728..0000000000 --- a/beacon_node/network/src/beacon_chain.rs +++ /dev/null @@ -1,155 +0,0 @@ -use beacon_chain::BeaconChain as RawBeaconChain; -use beacon_chain::{ - parking_lot::RwLockReadGuard, - types::{BeaconState, ChainSpec}, - AttestationValidationError, CheckPoint, -}; -use eth2_libp2p::rpc::HelloMessage; -use types::{Attestation, BeaconBlock, BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; - -pub use beacon_chain::{BeaconChainError, BeaconChainTypes, BlockProcessingOutcome, InvalidBlock}; - -/// The network's API to the beacon chain. -pub trait BeaconChain: Send + Sync { - fn get_spec(&self) -> &ChainSpec; - - fn get_state(&self) -> RwLockReadGuard>; - - fn slot(&self) -> Slot; - - fn head(&self) -> RwLockReadGuard>; - - fn get_block(&self, block_root: &Hash256) -> Result, BeaconChainError>; - - fn best_slot(&self) -> Slot; - - fn best_block_root(&self) -> Hash256; - - fn finalized_head(&self) -> RwLockReadGuard>; - - fn finalized_epoch(&self) -> Epoch; - - fn hello_message(&self) -> HelloMessage; - - fn process_block(&self, block: BeaconBlock) - -> Result; - - fn process_attestation( - &self, - attestation: Attestation, - ) -> Result<(), AttestationValidationError>; - - fn get_block_roots( - &self, - start_slot: Slot, - count: usize, - skip: usize, - ) -> Result, BeaconChainError>; - - fn get_block_headers( - &self, - start_slot: Slot, - count: usize, - skip: usize, - ) -> Result, BeaconChainError>; - - fn get_block_bodies(&self, roots: &[Hash256]) - -> Result, BeaconChainError>; - - fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result; -} - -impl BeaconChain for RawBeaconChain { - fn get_spec(&self) -> &ChainSpec { - &self.spec - } - - fn get_state(&self) -> RwLockReadGuard> { - self.state.read() - } - - fn slot(&self) -> Slot { - self.get_state().slot - } - - fn head(&self) -> RwLockReadGuard> { - self.head() - } - - fn get_block(&self, block_root: &Hash256) -> Result, BeaconChainError> { - self.get_block(block_root) - } - - fn finalized_epoch(&self) -> Epoch { - self.get_state().finalized_epoch - } - - fn finalized_head(&self) -> RwLockReadGuard> { - self.finalized_head() - } - - fn best_slot(&self) -> Slot { - self.head().beacon_block.slot - } - - fn best_block_root(&self) -> Hash256 { - self.head().beacon_block_root - } - - fn hello_message(&self) -> HelloMessage { - let spec = self.get_spec(); - let state = self.get_state(); - - HelloMessage { - network_id: spec.chain_id, - latest_finalized_root: state.finalized_root, - latest_finalized_epoch: state.finalized_epoch, - best_root: self.best_block_root(), - best_slot: self.best_slot(), - } - } - - fn process_block( - &self, - block: BeaconBlock, - ) -> Result { - self.process_block(block) - } - - fn process_attestation( - &self, - attestation: Attestation, - ) -> Result<(), AttestationValidationError> { - self.process_attestation(attestation) - } - - fn get_block_roots( - &self, - start_slot: Slot, - count: usize, - skip: usize, - ) -> Result, BeaconChainError> { - self.get_block_roots(start_slot, count, skip) - } - - fn get_block_headers( - &self, - start_slot: Slot, - count: usize, - skip: usize, - ) -> Result, BeaconChainError> { - let roots = self.get_block_roots(start_slot, count, skip)?; - self.get_block_headers(&roots) - } - - fn get_block_bodies( - &self, - roots: &[Hash256], - ) -> Result, BeaconChainError> { - self.get_block_bodies(roots) - } - - fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result { - self.is_new_block_root(beacon_block_root) - } -} diff --git a/beacon_node/network/src/lib.rs b/beacon_node/network/src/lib.rs index c298e31b4e..b805c1d755 100644 --- a/beacon_node/network/src/lib.rs +++ b/beacon_node/network/src/lib.rs @@ -1,5 +1,4 @@ /// This crate provides the network server for Lighthouse. -pub mod beacon_chain; pub mod error; pub mod message_handler; pub mod service; diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index f6a27ad600..adafae145d 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -1,7 +1,7 @@ -use crate::beacon_chain::{BeaconChain, BeaconChainTypes}; use crate::error; use crate::service::{NetworkMessage, OutgoingMessage}; use crate::sync::SimpleSync; +use beacon_chain::{BeaconChain, BeaconChainTypes}; use crossbeam_channel::{unbounded as channel, Sender}; use eth2_libp2p::{ behaviour::PubsubMessage, diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index d87b9e5a9d..9c71a60f7e 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -1,7 +1,7 @@ -use crate::beacon_chain::{BeaconChain, BeaconChainTypes}; use crate::error; use crate::message_handler::{HandlerMessage, MessageHandler}; use crate::NetworkConfig; +use beacon_chain::{BeaconChain, BeaconChainTypes}; use crossbeam_channel::{unbounded as channel, Sender, TryRecvError}; use eth2_libp2p::Service as LibP2PService; use eth2_libp2p::{Libp2pEvent, PeerId}; diff --git a/beacon_node/network/src/sync/import_queue.rs b/beacon_node/network/src/sync/import_queue.rs index 793f4c395e..5b03f58dff 100644 --- a/beacon_node/network/src/sync/import_queue.rs +++ b/beacon_node/network/src/sync/import_queue.rs @@ -1,4 +1,4 @@ -use crate::beacon_chain::{BeaconChain, BeaconChainTypes}; +use beacon_chain::{BeaconChain, BeaconChainTypes}; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::PeerId; use slog::{debug, error}; diff --git a/beacon_node/network/src/sync/simple_sync.rs b/beacon_node/network/src/sync/simple_sync.rs index 6ab8ea7d9a..0ca4f60115 100644 --- a/beacon_node/network/src/sync/simple_sync.rs +++ b/beacon_node/network/src/sync/simple_sync.rs @@ -1,6 +1,8 @@ use super::import_queue::ImportQueue; -use crate::beacon_chain::{BeaconChain, BeaconChainTypes, BlockProcessingOutcome, InvalidBlock}; use crate::message_handler::NetworkContext; +use beacon_chain::{ + BeaconChain, BeaconChainError, BeaconChainTypes, BlockProcessingOutcome, InvalidBlock, +}; use eth2_libp2p::rpc::methods::*; use eth2_libp2p::rpc::{RPCRequest, RPCResponse, RequestId}; use eth2_libp2p::PeerId; @@ -9,7 +11,7 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; use tree_hash::TreeHash; -use types::{Attestation, BeaconBlock, Epoch, Hash256, Slot}; +use types::{Attestation, BeaconBlock, BeaconBlockHeader, Epoch, EthSpec, 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; @@ -90,7 +92,7 @@ impl From for PeerSyncInfo { impl From<&Arc>> for PeerSyncInfo { fn from(chain: &Arc>) -> PeerSyncInfo { - Self::from(chain.hello_message()) + Self::from(hello_message(chain)) } } @@ -153,7 +155,7 @@ impl SimpleSync { pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext) { info!(self.log, "PeerConnect"; "peer" => format!("{:?}", peer_id)); - network.send_rpc_request(peer_id, RPCRequest::Hello(self.chain.hello_message())); + network.send_rpc_request(peer_id, RPCRequest::Hello(hello_message(&self.chain))); } /// Handle a `Hello` request. @@ -172,7 +174,7 @@ impl SimpleSync { network.send_rpc_response( peer_id.clone(), request_id, - RPCResponse::Hello(self.chain.hello_message()), + RPCResponse::Hello(hello_message(&self.chain)), ); self.process_hello(peer_id, hello, network); @@ -202,7 +204,7 @@ impl SimpleSync { if local.has_higher_finalized_epoch_than(peer) { let peer_finalized_slot = peer .latest_finalized_epoch - .start_slot(self.chain.get_spec().slots_per_epoch); + .start_slot(T::EthSpec::spec().slots_per_epoch); let local_roots = self.chain.get_block_roots(peer_finalized_slot, 1, 0); @@ -245,7 +247,7 @@ impl SimpleSync { hello: HelloMessage, network: &mut NetworkContext, ) { - let spec = self.chain.get_spec(); + let spec = T::EthSpec::spec(); let remote = PeerSyncInfo::from(hello); let local = PeerSyncInfo::from(&self.chain); @@ -424,7 +426,8 @@ impl SimpleSync { "count" => req.max_headers, ); - let headers = match self.chain.get_block_headers( + let headers = match get_block_headers( + &self.chain, req.start_slot, req.max_headers as usize, req.skip_slots as usize, @@ -596,7 +599,7 @@ impl SimpleSync { // parent(s). network.send_rpc_request( peer_id.clone(), - RPCRequest::Hello(self.chain.hello_message()), + RPCRequest::Hello(hello_message(&self.chain)), ); // Forward the block onto our peers. // @@ -835,15 +838,39 @@ impl SimpleSync { /// Returns `true` if the given slot is finalized in our chain. fn slot_is_finalized(&self, slot: Slot) -> bool { - slot <= self - .chain - .hello_message() + slot <= hello_message(&self.chain) .latest_finalized_epoch - .start_slot(self.chain.get_spec().slots_per_epoch) + .start_slot(T::EthSpec::spec().slots_per_epoch) } /// Generates our current state in the form of a HELLO RPC message. pub fn generate_hello(&self) -> HelloMessage { - self.chain.hello_message() + hello_message(&self.chain) } } + +/// Build a `HelloMessage` representing the state of the given `beacon_chain`. +fn hello_message(beacon_chain: &BeaconChain) -> HelloMessage { + let spec = T::EthSpec::spec(); + let state = &beacon_chain.head().beacon_state; + + HelloMessage { + network_id: spec.chain_id, + latest_finalized_root: state.finalized_root, + latest_finalized_epoch: state.finalized_epoch, + best_root: beacon_chain.head().beacon_block_root, + best_slot: beacon_chain.head().beacon_block.slot, + } +} + +/// Return a list of `BeaconBlockHeader` from the given `BeaconChain`, starting at `start_slot` and +/// returning `count` headers with a gap of `skip` slots between each. +fn get_block_headers( + beacon_chain: &BeaconChain, + start_slot: Slot, + count: usize, + skip: usize, +) -> Result, BeaconChainError> { + let roots = beacon_chain.get_block_roots(start_slot, count, skip)?; + beacon_chain.get_block_headers(&roots) +}