diff --git a/beacon_node/beacon_chain/src/initialise.rs b/beacon_node/beacon_chain/src/initialise.rs index 131782470b..a8289a062e 100644 --- a/beacon_node/beacon_chain/src/initialise.rs +++ b/beacon_node/beacon_chain/src/initialise.rs @@ -106,7 +106,7 @@ pub fn initialise_test_beacon_chain( deposit_root: Hash256::zero(), block_hash: Hash256::zero(), }; - let keypairs: Vec = (0..10) + let keypairs: Vec = (0..50) .collect::>() .iter() .map(|_| Keypair::random()) diff --git a/beacon_node/beacon_chain/src/lib.rs b/beacon_node/beacon_chain/src/lib.rs index 89ee2029c0..5acac6ff22 100644 --- a/beacon_node/beacon_chain/src/lib.rs +++ b/beacon_node/beacon_chain/src/lib.rs @@ -7,4 +7,7 @@ pub mod initialise; pub use self::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock}; pub use self::checkpoint::CheckPoint; pub use self::errors::BeaconChainError; -pub use fork_choice::{ForkChoice, ForkChoiceAlgorithm, ForkChoiceError}; +pub use db; +pub use fork_choice; +pub use parking_lot; +pub use slot_clock; diff --git a/beacon_node/client/src/client_types.rs b/beacon_node/client/src/client_types.rs index 744c9ab98b..de0678fe78 100644 --- a/beacon_node/client/src/client_types.rs +++ b/beacon_node/client/src/client_types.rs @@ -1,39 +1,49 @@ -use db::{ClientDB, DiskDB, MemoryDB}; -use fork_choice::{BitwiseLMDGhost, ForkChoice}; -use slot_clock::{SlotClock, SystemTimeSlotClock, TestingSlotClock}; -use beacon_chain::initialise; +use crate::ClientConfig; +use beacon_chain::{ + db::{ClientDB, DiskDB, MemoryDB}, + fork_choice::BitwiseLMDGhost, + initialise, + slot_clock::{SlotClock, SystemTimeSlotClock, TestingSlotClock}, + BeaconChain, +}; +use fork_choice::ForkChoice; + use std::sync::Arc; -use crate::ClientConfig pub trait ClientTypes { - type ForkChoice: ForkChoice; - type DB: ClientDB; - type SlotClock: SlotClock; + type DB: ClientDB + 'static; + type SlotClock: SlotClock + 'static; + type ForkChoice: ForkChoice + 'static; - pub fn initialise_beacon_chain(cchain_spec: &ClientConfig) -> Arc>); + fn initialise_beacon_chain( + config: &ClientConfig, + ) -> Arc>; } -pub struct StandardClientType +pub struct StandardClientType; impl ClientTypes for StandardClientType { type DB = DiskDB; - type ForkChoice = BitwiseLMDGhost; type SlotClock = SystemTimeSlotClock; + type ForkChoice = BitwiseLMDGhost; - pub fn initialise_beacon_chain(config: &ClientConfig) -> Arc>) { - initialise::initialise_beacon_chain(config.chain_spec, config.db_name) + fn initialise_beacon_chain( + config: &ClientConfig, + ) -> Arc> { + initialise::initialise_beacon_chain(&config.spec, Some(&config.db_name)) } - } -pub struct TestingClientType +pub struct TestingClientType; impl ClientTypes for TestingClientType { type DB = MemoryDB; - type SlotClock = TestingSlotClock; + type SlotClock = SystemTimeSlotClock; type ForkChoice = BitwiseLMDGhost; - pub fn initialise_beacon_chain(config: &ClientConfig) -> Arc>) { - initialise::initialise_test_beacon_chain(config.chain_spec, None) + fn initialise_beacon_chain( + config: &ClientConfig, + ) -> Arc> { + initialise::initialise_test_beacon_chain(&config.spec, None) } } diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index 46221c200b..f3178eaa66 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -10,6 +10,7 @@ pub use client_config::ClientConfig; pub use client_types::ClientTypes; //use beacon_chain::BeaconChain; +use beacon_chain::BeaconChain; use exit_future::{Exit, Signal}; use network::Service as NetworkService; use slog::o; @@ -20,26 +21,35 @@ use tokio::runtime::TaskExecutor; /// Main beacon node client service. This provides the connection and initialisation of the clients /// sub-services in multiple threads. pub struct Client { + /// Configuration for the lighthouse client. config: ClientConfig, + /// The beacon chain for the running client. beacon_chain: Arc>, + /// Reference to the network service. pub network: Arc, + /// Future to stop and begin shutdown of the Client. + //TODO: Decide best way to handle shutdown pub exit: exit_future::Exit, + /// The sending future to call to terminate the Client. + //TODO: Decide best way to handle shutdown pub exit_signal: Signal, + /// The clients logger. log: slog::Logger, + /// Marker to pin the beacon chain generics. + phantom: PhantomData, } -impl Client { - /// Generate an instance of the client. Spawn and link all internal subprocesses. +impl Client { + /// Generate an instance of the client. Spawn and link all internal sub-processes. pub fn new( config: ClientConfig, - client_type: T, log: slog::Logger, executor: &TaskExecutor, ) -> error::Result { let (exit_signal, exit) = exit_future::signal(); // generate a beacon chain - let beacon_chain = client_type.initialise_beacon_chain(&config); + let beacon_chain = TClientType::initialise_beacon_chain(&config); // Start the network service, libp2p and syncing threads // TODO: Add beacon_chain reference to network parameters @@ -54,6 +64,7 @@ impl Client { Ok(Client { config, + beacon_chain, exit, exit_signal: exit_signal, log, diff --git a/beacon_node/network/src/beacon_chain.rs b/beacon_node/network/src/beacon_chain.rs new file mode 100644 index 0000000000..5e0857f47b --- /dev/null +++ b/beacon_node/network/src/beacon_chain.rs @@ -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; + + fn finalized_head(&self) -> RwLockReadGuard; +} + +impl BeaconChain for RawBeaconChain +where + T: ClientDB + Sized, + U: SlotClock, + F: ForkChoice, +{ + fn head(&self) -> RwLockReadGuard { + self.head() + } + + fn finalized_head(&self) -> RwLockReadGuard { + self.finalized_head() + } +} diff --git a/beacon_node/network/src/lib.rs b/beacon_node/network/src/lib.rs index 49b2abadd7..dca83bb773 100644 --- a/beacon_node/network/src/lib.rs +++ b/beacon_node/network/src/lib.rs @@ -1,4 +1,5 @@ /// This crate provides the network server for Lighthouse. +pub mod beacon_chain; pub mod error; mod message_handler; mod messages; diff --git a/beacon_node/network/src/message_handler.rs b/beacon_node/network/src/message_handler.rs index 4ebedb89a3..0471e8ce50 100644 --- a/beacon_node/network/src/message_handler.rs +++ b/beacon_node/network/src/message_handler.rs @@ -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 { +pub struct MessageHandler { /// Currently loaded and initialised beacon chain. - chain: BeaconChain, + chain: Arc, /// 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 MessageHandler { +impl MessageHandler { /// Initializes and runs the MessageHandler. pub fn new( - beacon_chain: Arc>, + beacon_chain: Arc, executor: &tokio::runtime::TaskExecutor, log: slog::Logger, ) -> error::Result> { @@ -60,7 +60,7 @@ impl MessageHandler { // 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(), diff --git a/beacon_node/network/src/service.rs b/beacon_node/network/src/service.rs index 6b9c0aff0a..e42b391058 100644 --- a/beacon_node/network/src/service.rs +++ b/beacon_node/network/src/service.rs @@ -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 { +pub struct Service { //libp2p_service: Arc>, libp2p_exit: oneshot::Sender<()>, network_send: crossbeam_channel::Sender, //message_handler: MessageHandler, //message_handler_send: Sender, - PhantomData: T, } -impl Service { +impl Service { pub fn new( - beacon_chain: Arc, + beacon_chain: Arc, config: &NetworkConfig, executor: &TaskExecutor, log: slog::Logger, ) -> error::Result<(Arc, Sender)> { // 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) =