Remove BeaconChain
wrapper trait from network
This commit is contained in:
parent
f44170cab1
commit
0590504261
@ -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<T: BeaconChainTypes>: Send + Sync {
|
|
||||||
fn get_spec(&self) -> &ChainSpec;
|
|
||||||
|
|
||||||
fn get_state(&self) -> RwLockReadGuard<BeaconState<T::EthSpec>>;
|
|
||||||
|
|
||||||
fn slot(&self) -> Slot;
|
|
||||||
|
|
||||||
fn head(&self) -> RwLockReadGuard<CheckPoint<T::EthSpec>>;
|
|
||||||
|
|
||||||
fn get_block(&self, block_root: &Hash256) -> Result<Option<BeaconBlock>, BeaconChainError>;
|
|
||||||
|
|
||||||
fn best_slot(&self) -> Slot;
|
|
||||||
|
|
||||||
fn best_block_root(&self) -> Hash256;
|
|
||||||
|
|
||||||
fn finalized_head(&self) -> RwLockReadGuard<CheckPoint<T::EthSpec>>;
|
|
||||||
|
|
||||||
fn finalized_epoch(&self) -> Epoch;
|
|
||||||
|
|
||||||
fn hello_message(&self) -> HelloMessage;
|
|
||||||
|
|
||||||
fn process_block(&self, block: BeaconBlock)
|
|
||||||
-> Result<BlockProcessingOutcome, BeaconChainError>;
|
|
||||||
|
|
||||||
fn process_attestation(
|
|
||||||
&self,
|
|
||||||
attestation: Attestation,
|
|
||||||
) -> Result<(), AttestationValidationError>;
|
|
||||||
|
|
||||||
fn get_block_roots(
|
|
||||||
&self,
|
|
||||||
start_slot: Slot,
|
|
||||||
count: usize,
|
|
||||||
skip: usize,
|
|
||||||
) -> Result<Vec<Hash256>, BeaconChainError>;
|
|
||||||
|
|
||||||
fn get_block_headers(
|
|
||||||
&self,
|
|
||||||
start_slot: Slot,
|
|
||||||
count: usize,
|
|
||||||
skip: usize,
|
|
||||||
) -> Result<Vec<BeaconBlockHeader>, BeaconChainError>;
|
|
||||||
|
|
||||||
fn get_block_bodies(&self, roots: &[Hash256])
|
|
||||||
-> Result<Vec<BeaconBlockBody>, BeaconChainError>;
|
|
||||||
|
|
||||||
fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result<bool, BeaconChainError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: BeaconChainTypes> BeaconChain<T> for RawBeaconChain<T> {
|
|
||||||
fn get_spec(&self) -> &ChainSpec {
|
|
||||||
&self.spec
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_state(&self) -> RwLockReadGuard<BeaconState<T::EthSpec>> {
|
|
||||||
self.state.read()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn slot(&self) -> Slot {
|
|
||||||
self.get_state().slot
|
|
||||||
}
|
|
||||||
|
|
||||||
fn head(&self) -> RwLockReadGuard<CheckPoint<T::EthSpec>> {
|
|
||||||
self.head()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_block(&self, block_root: &Hash256) -> Result<Option<BeaconBlock>, BeaconChainError> {
|
|
||||||
self.get_block(block_root)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn finalized_epoch(&self) -> Epoch {
|
|
||||||
self.get_state().finalized_epoch
|
|
||||||
}
|
|
||||||
|
|
||||||
fn finalized_head(&self) -> RwLockReadGuard<CheckPoint<T::EthSpec>> {
|
|
||||||
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<BlockProcessingOutcome, BeaconChainError> {
|
|
||||||
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<Vec<Hash256>, BeaconChainError> {
|
|
||||||
self.get_block_roots(start_slot, count, skip)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_block_headers(
|
|
||||||
&self,
|
|
||||||
start_slot: Slot,
|
|
||||||
count: usize,
|
|
||||||
skip: usize,
|
|
||||||
) -> Result<Vec<BeaconBlockHeader>, BeaconChainError> {
|
|
||||||
let roots = self.get_block_roots(start_slot, count, skip)?;
|
|
||||||
self.get_block_headers(&roots)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_block_bodies(
|
|
||||||
&self,
|
|
||||||
roots: &[Hash256],
|
|
||||||
) -> Result<Vec<BeaconBlockBody>, BeaconChainError> {
|
|
||||||
self.get_block_bodies(roots)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_new_block_root(&self, beacon_block_root: &Hash256) -> Result<bool, BeaconChainError> {
|
|
||||||
self.is_new_block_root(beacon_block_root)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,4 @@
|
|||||||
/// This crate provides the network server for Lighthouse.
|
/// This crate provides the network server for Lighthouse.
|
||||||
pub mod beacon_chain;
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod message_handler;
|
pub mod message_handler;
|
||||||
pub mod service;
|
pub mod service;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::beacon_chain::{BeaconChain, BeaconChainTypes};
|
|
||||||
use crate::error;
|
use crate::error;
|
||||||
use crate::service::{NetworkMessage, OutgoingMessage};
|
use crate::service::{NetworkMessage, OutgoingMessage};
|
||||||
use crate::sync::SimpleSync;
|
use crate::sync::SimpleSync;
|
||||||
|
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||||
use crossbeam_channel::{unbounded as channel, Sender};
|
use crossbeam_channel::{unbounded as channel, Sender};
|
||||||
use eth2_libp2p::{
|
use eth2_libp2p::{
|
||||||
behaviour::PubsubMessage,
|
behaviour::PubsubMessage,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::beacon_chain::{BeaconChain, BeaconChainTypes};
|
|
||||||
use crate::error;
|
use crate::error;
|
||||||
use crate::message_handler::{HandlerMessage, MessageHandler};
|
use crate::message_handler::{HandlerMessage, MessageHandler};
|
||||||
use crate::NetworkConfig;
|
use crate::NetworkConfig;
|
||||||
|
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||||
use crossbeam_channel::{unbounded as channel, Sender, TryRecvError};
|
use crossbeam_channel::{unbounded as channel, Sender, TryRecvError};
|
||||||
use eth2_libp2p::Service as LibP2PService;
|
use eth2_libp2p::Service as LibP2PService;
|
||||||
use eth2_libp2p::{Libp2pEvent, PeerId};
|
use eth2_libp2p::{Libp2pEvent, PeerId};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::beacon_chain::{BeaconChain, BeaconChainTypes};
|
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||||
use eth2_libp2p::rpc::methods::*;
|
use eth2_libp2p::rpc::methods::*;
|
||||||
use eth2_libp2p::PeerId;
|
use eth2_libp2p::PeerId;
|
||||||
use slog::{debug, error};
|
use slog::{debug, error};
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
use super::import_queue::ImportQueue;
|
use super::import_queue::ImportQueue;
|
||||||
use crate::beacon_chain::{BeaconChain, BeaconChainTypes, BlockProcessingOutcome, InvalidBlock};
|
|
||||||
use crate::message_handler::NetworkContext;
|
use crate::message_handler::NetworkContext;
|
||||||
|
use beacon_chain::{
|
||||||
|
BeaconChain, BeaconChainError, BeaconChainTypes, BlockProcessingOutcome, InvalidBlock,
|
||||||
|
};
|
||||||
use eth2_libp2p::rpc::methods::*;
|
use eth2_libp2p::rpc::methods::*;
|
||||||
use eth2_libp2p::rpc::{RPCRequest, RPCResponse, RequestId};
|
use eth2_libp2p::rpc::{RPCRequest, RPCResponse, RequestId};
|
||||||
use eth2_libp2p::PeerId;
|
use eth2_libp2p::PeerId;
|
||||||
@ -9,7 +11,7 @@ use std::collections::HashMap;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tree_hash::TreeHash;
|
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.
|
/// The number of slots that we can import blocks ahead of us, before going into full Sync mode.
|
||||||
const SLOT_IMPORT_TOLERANCE: u64 = 100;
|
const SLOT_IMPORT_TOLERANCE: u64 = 100;
|
||||||
@ -90,7 +92,7 @@ impl From<HelloMessage> for PeerSyncInfo {
|
|||||||
|
|
||||||
impl<T: BeaconChainTypes> From<&Arc<BeaconChain<T>>> for PeerSyncInfo {
|
impl<T: BeaconChainTypes> From<&Arc<BeaconChain<T>>> for PeerSyncInfo {
|
||||||
fn from(chain: &Arc<BeaconChain<T>>) -> PeerSyncInfo {
|
fn from(chain: &Arc<BeaconChain<T>>) -> PeerSyncInfo {
|
||||||
Self::from(chain.hello_message())
|
Self::from(hello_message(chain))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,7 +155,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
|||||||
pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext) {
|
pub fn on_connect(&self, peer_id: PeerId, network: &mut NetworkContext) {
|
||||||
info!(self.log, "PeerConnect"; "peer" => format!("{:?}", peer_id));
|
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.
|
/// Handle a `Hello` request.
|
||||||
@ -172,7 +174,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
|||||||
network.send_rpc_response(
|
network.send_rpc_response(
|
||||||
peer_id.clone(),
|
peer_id.clone(),
|
||||||
request_id,
|
request_id,
|
||||||
RPCResponse::Hello(self.chain.hello_message()),
|
RPCResponse::Hello(hello_message(&self.chain)),
|
||||||
);
|
);
|
||||||
|
|
||||||
self.process_hello(peer_id, hello, network);
|
self.process_hello(peer_id, hello, network);
|
||||||
@ -202,7 +204,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
|||||||
if local.has_higher_finalized_epoch_than(peer) {
|
if local.has_higher_finalized_epoch_than(peer) {
|
||||||
let peer_finalized_slot = peer
|
let peer_finalized_slot = peer
|
||||||
.latest_finalized_epoch
|
.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);
|
let local_roots = self.chain.get_block_roots(peer_finalized_slot, 1, 0);
|
||||||
|
|
||||||
@ -245,7 +247,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
|||||||
hello: HelloMessage,
|
hello: HelloMessage,
|
||||||
network: &mut NetworkContext,
|
network: &mut NetworkContext,
|
||||||
) {
|
) {
|
||||||
let spec = self.chain.get_spec();
|
let spec = T::EthSpec::spec();
|
||||||
|
|
||||||
let remote = PeerSyncInfo::from(hello);
|
let remote = PeerSyncInfo::from(hello);
|
||||||
let local = PeerSyncInfo::from(&self.chain);
|
let local = PeerSyncInfo::from(&self.chain);
|
||||||
@ -424,7 +426,8 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
|||||||
"count" => req.max_headers,
|
"count" => req.max_headers,
|
||||||
);
|
);
|
||||||
|
|
||||||
let headers = match self.chain.get_block_headers(
|
let headers = match get_block_headers(
|
||||||
|
&self.chain,
|
||||||
req.start_slot,
|
req.start_slot,
|
||||||
req.max_headers as usize,
|
req.max_headers as usize,
|
||||||
req.skip_slots as usize,
|
req.skip_slots as usize,
|
||||||
@ -596,7 +599,7 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
|||||||
// parent(s).
|
// parent(s).
|
||||||
network.send_rpc_request(
|
network.send_rpc_request(
|
||||||
peer_id.clone(),
|
peer_id.clone(),
|
||||||
RPCRequest::Hello(self.chain.hello_message()),
|
RPCRequest::Hello(hello_message(&self.chain)),
|
||||||
);
|
);
|
||||||
// Forward the block onto our peers.
|
// Forward the block onto our peers.
|
||||||
//
|
//
|
||||||
@ -835,15 +838,39 @@ impl<T: BeaconChainTypes> SimpleSync<T> {
|
|||||||
|
|
||||||
/// Returns `true` if the given slot is finalized in our chain.
|
/// Returns `true` if the given slot is finalized in our chain.
|
||||||
fn slot_is_finalized(&self, slot: Slot) -> bool {
|
fn slot_is_finalized(&self, slot: Slot) -> bool {
|
||||||
slot <= self
|
slot <= hello_message(&self.chain)
|
||||||
.chain
|
|
||||||
.hello_message()
|
|
||||||
.latest_finalized_epoch
|
.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.
|
/// Generates our current state in the form of a HELLO RPC message.
|
||||||
pub fn generate_hello(&self) -> HelloMessage {
|
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<T: BeaconChainTypes>(beacon_chain: &BeaconChain<T>) -> 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<T: BeaconChainTypes>(
|
||||||
|
beacon_chain: &BeaconChain<T>,
|
||||||
|
start_slot: Slot,
|
||||||
|
count: usize,
|
||||||
|
skip: usize,
|
||||||
|
) -> Result<Vec<BeaconBlockHeader>, BeaconChainError> {
|
||||||
|
let roots = beacon_chain.get_block_roots(start_slot, count, skip)?;
|
||||||
|
beacon_chain.get_block_headers(&roots)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user