diff --git a/Cargo.toml b/Cargo.toml index 1090a9b6f5..6cd11c4380 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,9 @@ members = [ "eth2/utils/test_random_derive", "beacon_node", "beacon_node/db", + "beacon_node/client", "beacon_node/network", + "beacon_node/rpc", "beacon_node/sync", "beacon_node/version", "beacon_node/beacon_chain", diff --git a/beacon_node/Cargo.toml b/beacon_node/Cargo.toml index f909d51038..8b26417860 100644 --- a/beacon_node/Cargo.toml +++ b/beacon_node/Cargo.toml @@ -5,25 +5,14 @@ authors = ["Paul Hauner ", "Age Manning "] +edition = "2018" + +[dependencies] +beacon_chain = { path = "../beacon_chain" } +network = { path = "../network" } +sync = { path = "../sync" } +db = { path = "../db" } +fork_choice = { path = "../../eth2/fork_choice" } +types = { path = "../../eth2/types" } +slot_clock = { path = "../../eth2/utils/slot_clock" } + +error-chain = "0.12.0" +slog = "^2.2.3" +tokio = "0.1.15" +clap = "2.32.0" +dirs = "1.0.3" +exit-future = "0.1.3" +futures = "0.1.25" diff --git a/beacon_node/src/config.rs b/beacon_node/client/src/client_config.rs similarity index 90% rename from beacon_node/src/config.rs rename to beacon_node/client/src/client_config.rs index c7fa579098..c1580aa9fe 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/client/src/client_config.rs @@ -8,11 +8,9 @@ use std::net::IpAddr; use std::path::PathBuf; use types::ChainSpec; -/// Stores the core configuration for this Lighthouse instance. -/// This struct is general, other components may implement more -/// specialized configuration structs. +/// Stores the client configuration for this Lighthouse instance. #[derive(Debug, Clone)] -pub struct Config { +pub struct ClientConfig { pub data_dir: PathBuf, pub spec: ChainSpec, pub net_conf: network::NetworkConfiguration, @@ -23,7 +21,7 @@ pub struct Config { //pub ipc_conf: } -impl Default for Config { +impl Default for ClientConfig { /// Build a new lighthouse configuration from defaults. fn default() -> Self { let data_dir = { @@ -47,10 +45,10 @@ impl Default for Config { } } -impl Config { +impl ClientConfig { /// Parses the CLI arguments into a `Config` struct. pub fn parse_args(args: ArgMatches, log: &slog::Logger) -> Result { - let mut config = Config::default(); + let mut config = ClientConfig::default(); // Network related args diff --git a/beacon_node/client/src/client_types.rs b/beacon_node/client/src/client_types.rs new file mode 100644 index 0000000000..38ae1c8c38 --- /dev/null +++ b/beacon_node/client/src/client_types.rs @@ -0,0 +1,25 @@ +use db::{ClientDB, DiskDB, MemoryDB}; +use fork_choice::{BitwiseLMDGhost, ForkChoice}; +use slot_clock::{SlotClock, SystemTimeSlotClock, TestingSlotClock}; + +pub trait ClientTypes { + type ForkChoice: ForkChoice; + type DB: ClientDB; + type SlotClock: SlotClock; +} + +pub struct StandardClientType {} + +impl ClientTypes for StandardClientType { + type DB = DiskDB; + type ForkChoice = BitwiseLMDGhost; + type SlotClock = SystemTimeSlotClock; +} + +pub struct TestingClientType {} + +impl ClientTypes for TestingClientType { + type DB = MemoryDB; + type SlotClock = TestingSlotClock; + type ForkChoice = BitwiseLMDGhost; +} diff --git a/beacon_node/src/error.rs b/beacon_node/client/src/error.rs similarity index 100% rename from beacon_node/src/error.rs rename to beacon_node/client/src/error.rs diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs new file mode 100644 index 0000000000..3bfde0e9de --- /dev/null +++ b/beacon_node/client/src/lib.rs @@ -0,0 +1,50 @@ +extern crate slog; + +mod client_config; + +pub mod client_types; +pub mod error; +pub mod notifier; + +pub use client_config::ClientConfig; +pub use client_types::ClientTypes; + +//use beacon_chain::BeaconChain; +use exit_future::{Exit, Signal}; +use std::marker::PhantomData; +//use std::sync::Arc; +use tokio::runtime::TaskExecutor; + +//use network::NetworkService; + +pub struct Client { + config: ClientConfig, + // beacon_chain: Arc>, + // network: Option>, + exit: exit_future::Exit, + exit_signal: Option, + log: slog::Logger, + phantom: PhantomData, +} + +impl Client { + pub fn new( + config: ClientConfig, + log: slog::Logger, + executor: TaskExecutor, + ) -> error::Result { + let (exit_signal, exit) = exit_future::signal(); + + Ok(Client { + config, + exit, + exit_signal: Some(exit_signal), + log, + phantom: PhantomData, + }) + } + + pub fn logger(&self) -> slog::Logger { + self.log.clone() + } +} diff --git a/beacon_node/client/src/notifier.rs b/beacon_node/client/src/notifier.rs new file mode 100755 index 0000000000..3edf93bf68 --- /dev/null +++ b/beacon_node/client/src/notifier.rs @@ -0,0 +1,35 @@ +use crate::Client; +use crate::ClientTypes; +use db::ClientDB; +use exit_future::Exit; +use fork_choice::ForkChoice; +use futures::{Future, Stream}; +use slog::{debug, info}; +use slot_clock::SlotClock; +use std::sync::Arc; +use std::time::{Duration, Instant}; +use tokio::runtime::TaskExecutor; +use tokio::timer::Interval; + +/// Thread that monitors the client and reports useful statistics to the user. + +pub fn run(client: &Client, executor: TaskExecutor, exit: Exit) { + // notification heartbeat + let interval = Interval::new(Instant::now(), Duration::from_secs(5)); + + let log = client.logger(); + + // build heartbeat logic here + let heartbeat = move |_| { + info!(log, "Temp heartbeat output"); + Ok(()) + }; + + // map error and spawn + let log = client.logger(); + let heartbeat_interval = interval + .map_err(move |e| debug!(log, "Timer error {}", e)) + .for_each(heartbeat); + + executor.spawn(exit.until(heartbeat_interval).map(|_| ())); +} diff --git a/beacon_node/rpc/Cargo.toml b/beacon_node/rpc/Cargo.toml new file mode 100644 index 0000000000..4c3333ee1d --- /dev/null +++ b/beacon_node/rpc/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "rpc" +version = "0.1.0" +authors = ["Age Manning "] +edition = "2018" + +[dependencies] +bls = { path = "../../eth2/utils/bls" } +beacon_chain = { path = "../beacon_chain" } + +protos = { path = "../../protos" } +grpcio = { version = "0.4", default-features = false, features = ["protobuf-codec"] } +protobuf = "2.0.2" +clap = "2.32.0" +db = { path = "../db" } +dirs = "1.0.3" +futures = "0.1.23" +slog = "^2.2.3" +slot_clock = { path = "../../eth2/utils/slot_clock" } +slog-term = "^2.4.0" +slog-async = "^2.3.0" +types = { path = "../../eth2/types" } +ssz = { path = "../../eth2/utils/ssz" } diff --git a/beacon_node/src/rpc/beacon_block.rs b/beacon_node/rpc/src/beacon_block.rs similarity index 100% rename from beacon_node/src/rpc/beacon_block.rs rename to beacon_node/rpc/src/beacon_block.rs diff --git a/beacon_node/src/rpc/mod.rs b/beacon_node/rpc/src/lib.rs similarity index 100% rename from beacon_node/src/rpc/mod.rs rename to beacon_node/rpc/src/lib.rs diff --git a/beacon_node/src/rpc/validator.rs b/beacon_node/rpc/src/validator.rs similarity index 100% rename from beacon_node/src/rpc/validator.rs rename to beacon_node/rpc/src/validator.rs diff --git a/beacon_node/src/main.rs b/beacon_node/src/main.rs index ed26a55fe2..09cac99b4b 100644 --- a/beacon_node/src/main.rs +++ b/beacon_node/src/main.rs @@ -1,12 +1,9 @@ extern crate slog; -mod config; -mod error; -mod rpc; mod run; use clap::{App, Arg}; -use config::Config; +use client::ClientConfig; use slog::{o, Drain}; fn main() { @@ -43,7 +40,7 @@ fn main() { .get_matches(); // invalid arguments, panic - let config = Config::parse_args(matches, &logger).unwrap(); + let config = ClientConfig::parse_args(matches, &logger).unwrap(); - run::run_beacon_node(config, &logger); + run::run_beacon_node(config, logger); } diff --git a/beacon_node/src/run.rs b/beacon_node/src/run.rs index 18c4c3fe05..3207ce43de 100644 --- a/beacon_node/src/run.rs +++ b/beacon_node/src/run.rs @@ -1,23 +1,13 @@ -use crate::config::Config; -use crate::error; -use crate::rpc::start_server; -use beacon_chain::BeaconChain; -use bls::create_proof_of_possession; -use db::{ - stores::{BeaconBlockStore, BeaconStateStore}, - ClientDB, DBType, DiskDB, MemoryDB, -}; -use fork_choice::{BitwiseLMDGhost, ForkChoiceAlgorithm}; +use client::client_types::{StandardClientType, TestingClientType}; +use client::error; +use client::{notifier, Client, ClientConfig}; use futures::sync::oneshot; -use network::NetworkConfiguration; -use slog::{error, info}; -use slot_clock::SystemTimeSlotClock; +use futures::Future; +use slog::info; use std::cell::RefCell; -use std::sync::Arc; -use tokio::runtime::{Builder, Runtime, TaskExecutor}; -use types::{ChainSpec, Deposit, DepositData, DepositInput, Eth1Data, Hash256, Keypair}; +use tokio::runtime::Builder; -pub fn run_beacon_node(config: Config, log: &slog::Logger) -> error::Result<()> { +pub fn run_beacon_node(config: ClientConfig, log: slog::Logger) -> error::Result<()> { let mut runtime = Builder::new() .name_prefix("main-") .build() @@ -33,22 +23,23 @@ pub fn run_beacon_node(config: Config, log: &slog::Logger) -> error::Result<()> let ctrlc_send_c = RefCell::new(Some(ctrlc_send)); ctrlc::set_handler(move || { if let Some(ctrlc_send) = ctrlc_send_c.try_borrow_mut().unwrap().take() { - ctrlc_send - .send(()) - .expect("Error sending termination message"); + ctrlc_send.send(()).expect("Error sending ctrl-c message"); } }); + let (exit_signal, exit) = exit_future::signal(); + let executor = runtime.executor(); - start(config, log, executor); + // currently testing - using TestingNode type + let client: Client = Client::new(config, log.clone(), executor.clone())?; + notifier::run(&client, executor, exit); runtime.block_on(ctrlc); - info!(log, "Shutting down."); - //TODO: handle shutdown of processes gracefully - + info!(log, "Shutting down.."); + exit_signal.fire(); + drop(client); + runtime.shutdown_on_idle().wait().unwrap(); Ok(()) } - -fn start(config: Config, log: &slog::Logger, executor: TaskExecutor) {}