mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-09 03:31:45 +00:00
Organize beacon_chain typing
- Implements ClientTypes - New network BeaconChain type for the networking service
This commit is contained in:
27
beacon_node/network/src/beacon_chain.rs
Normal file
27
beacon_node/network/src/beacon_chain.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use beacon_chain::BeaconChain as RawBeaconChain;
|
||||
use beacon_chain::{
|
||||
db::ClientDB, fork_choice::ForkChoice, parking_lot::RwLockReadGuard, slot_clock::SlotClock,
|
||||
CheckPoint,
|
||||
};
|
||||
|
||||
/// The network's API to the beacon chain.
|
||||
pub trait BeaconChain: Send + Sync {
|
||||
fn head(&self) -> RwLockReadGuard<CheckPoint>;
|
||||
|
||||
fn finalized_head(&self) -> RwLockReadGuard<CheckPoint>;
|
||||
}
|
||||
|
||||
impl<T, U, F> BeaconChain for RawBeaconChain<T, U, F>
|
||||
where
|
||||
T: ClientDB + Sized,
|
||||
U: SlotClock,
|
||||
F: ForkChoice,
|
||||
{
|
||||
fn head(&self) -> RwLockReadGuard<CheckPoint> {
|
||||
self.head()
|
||||
}
|
||||
|
||||
fn finalized_head(&self) -> RwLockReadGuard<CheckPoint> {
|
||||
self.finalized_head()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
/// This crate provides the network server for Lighthouse.
|
||||
pub mod beacon_chain;
|
||||
pub mod error;
|
||||
mod message_handler;
|
||||
mod messages;
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use crate::beacon_chain::BeaconChain;
|
||||
use crate::error;
|
||||
use crate::messages::NodeMessage;
|
||||
use beacon_chain::BeaconChain;
|
||||
use crossbeam_channel::{unbounded as channel, Sender, TryRecvError};
|
||||
use crossbeam_channel::{unbounded as channel, Sender};
|
||||
use futures::future;
|
||||
use futures::prelude::*;
|
||||
use libp2p::rpc;
|
||||
use libp2p::{PeerId, RPCEvent};
|
||||
use slog::debug;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use sync::SimpleSync;
|
||||
use types::Hash256;
|
||||
@@ -17,9 +17,9 @@ use types::Hash256;
|
||||
const HELLO_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
|
||||
/// Handles messages received from the network and client and organises syncing.
|
||||
pub struct MessageHandler<T: ClientTypes> {
|
||||
pub struct MessageHandler {
|
||||
/// Currently loaded and initialised beacon chain.
|
||||
chain: BeaconChain<T::DB, T::SlotClock, T::ForkChoice>,
|
||||
chain: Arc<BeaconChain>,
|
||||
/// The syncing framework.
|
||||
sync: SimpleSync,
|
||||
/// A mapping of peers we have sent a HELLO rpc request to.
|
||||
@@ -41,10 +41,10 @@ pub enum HandlerMessage {
|
||||
RPC(RPCEvent),
|
||||
}
|
||||
|
||||
impl<T: ClientTypes> MessageHandler<T> {
|
||||
impl MessageHandler {
|
||||
/// Initializes and runs the MessageHandler.
|
||||
pub fn new(
|
||||
beacon_chain: Arc<BeaconChain<T::DB, T::SlotClock, T::ForkChoice>>,
|
||||
beacon_chain: Arc<BeaconChain>,
|
||||
executor: &tokio::runtime::TaskExecutor,
|
||||
log: slog::Logger,
|
||||
) -> error::Result<Sender<HandlerMessage>> {
|
||||
@@ -60,7 +60,7 @@ impl<T: ClientTypes> MessageHandler<T> {
|
||||
// generate the Message handler
|
||||
let sync = SimpleSync::new(temp_genesis);
|
||||
let mut handler = MessageHandler {
|
||||
chain: beacon_chain,
|
||||
chain: beacon_chain.clone(),
|
||||
sync,
|
||||
hello_requests: HashMap::new(),
|
||||
log: log.clone(),
|
||||
|
||||
@@ -1,46 +1,42 @@
|
||||
use crate::beacon_chain::BeaconChain;
|
||||
use crate::error;
|
||||
use crate::message_handler::{HandlerMessage, MessageHandler};
|
||||
use crate::messages::{NetworkMessage, NodeMessage};
|
||||
use crate::NetworkConfig;
|
||||
use crossbeam_channel::{unbounded as channel, Sender, TryRecvError};
|
||||
use futures::future::lazy;
|
||||
use futures::future::poll_fn;
|
||||
use futures::prelude::*;
|
||||
use futures::sync::oneshot;
|
||||
use futures::Stream;
|
||||
use libp2p::behaviour::BehaviourEvent;
|
||||
use libp2p::error::Error as libp2pError;
|
||||
use libp2p::Service as LibP2PService;
|
||||
use libp2p::{Libp2pEvent, PeerId};
|
||||
use slog::{debug, info, o, trace, warn, Logger};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use slog::{debug, o};
|
||||
use std::sync::Arc;
|
||||
use tokio::runtime::TaskExecutor;
|
||||
use client::ClientTypes;
|
||||
|
||||
/// Service that handles communication between internal services and the libp2p network service.
|
||||
pub struct Service<T: ClientTypes> {
|
||||
pub struct Service {
|
||||
//libp2p_service: Arc<Mutex<LibP2PService>>,
|
||||
libp2p_exit: oneshot::Sender<()>,
|
||||
network_send: crossbeam_channel::Sender<NetworkMessage>,
|
||||
//message_handler: MessageHandler,
|
||||
//message_handler_send: Sender<HandlerMessage>,
|
||||
PhantomData: T,
|
||||
}
|
||||
|
||||
impl<T: ClientTypes> Service<T> {
|
||||
impl Service {
|
||||
pub fn new(
|
||||
beacon_chain: Arc<BeaconChain<T::DB, T::SlotClock, T::ForkChoice>,
|
||||
beacon_chain: Arc<BeaconChain>,
|
||||
config: &NetworkConfig,
|
||||
executor: &TaskExecutor,
|
||||
log: slog::Logger,
|
||||
) -> error::Result<(Arc<Self>, Sender<NetworkMessage>)> {
|
||||
// launch message handler thread
|
||||
let message_handler_log = log.new(o!("Service" => "MessageHandler"));
|
||||
let message_handler_send = MessageHandler::new(beacon_chain, executor, message_handler_log)?;
|
||||
let message_handler_send =
|
||||
MessageHandler::new(beacon_chain, executor, message_handler_log)?;
|
||||
|
||||
// launch libp2p service
|
||||
let libp2p_log = log.new(o!("Service" => "Libp2p"));
|
||||
let libp2p_service = LibP2PService::new(config, libp2p_log)?;
|
||||
let libp2p_service = LibP2PService::new(config.clone(), libp2p_log)?;
|
||||
|
||||
// TODO: Spawn thread to handle libp2p messages and pass to message handler thread.
|
||||
let (network_send, libp2p_exit) =
|
||||
|
||||
Reference in New Issue
Block a user