Implement the basic structure of the beacon node.

This commit is contained in:
Age Manning 2019-03-01 12:45:01 +11:00
parent 19a64f906e
commit 2e020a3efa
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
14 changed files with 188 additions and 56 deletions

View File

@ -18,7 +18,9 @@ members = [
"eth2/utils/test_random_derive", "eth2/utils/test_random_derive",
"beacon_node", "beacon_node",
"beacon_node/db", "beacon_node/db",
"beacon_node/client",
"beacon_node/network", "beacon_node/network",
"beacon_node/rpc",
"beacon_node/sync", "beacon_node/sync",
"beacon_node/version", "beacon_node/version",
"beacon_node/beacon_chain", "beacon_node/beacon_chain",

View File

@ -5,25 +5,14 @@ authors = ["Paul Hauner <paul@paulhauner.com>", "Age Manning <Age@AgeManning.com
edition = "2018" edition = "2018"
[dependencies] [dependencies]
bls = { path = "../eth2/utils/bls" } client = { path = "client" }
beacon_chain = { path = "beacon_chain" }
grpcio = { version = "0.4", default-features = false, features = ["protobuf-codec"] }
protobuf = "2.0.2"
protos = { path = "../protos" }
clap = "2.32.0"
db = { path = "db" }
network = { path = "network" }
sync = { path = "sync" }
version = { path = "version" } version = { path = "version" }
dirs = "1.0.3"
futures = "0.1.23" clap = "2.32.0"
fork_choice = { path = "../eth2/fork_choice" }
slog = "^2.2.3" slog = "^2.2.3"
slot_clock = { path = "../eth2/utils/slot_clock" }
slog-term = "^2.4.0" slog-term = "^2.4.0"
slog-async = "^2.3.0" slog-async = "^2.3.0"
types = { path = "../eth2/types" }
ssz = { path = "../eth2/utils/ssz" }
tokio = "0.1.15"
error-chain = "0.12.0"
ctrlc = { version = "3.1.1", features = ["termination"] } ctrlc = { version = "3.1.1", features = ["termination"] }
tokio = "0.1.15"
futures = "0.1.25"
exit-future = "0.1.3"

View File

@ -0,0 +1,22 @@
[package]
name = "client"
version = "0.1.0"
authors = ["Age Manning <Age@AgeManning.com>"]
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"

View File

@ -8,11 +8,9 @@ use std::net::IpAddr;
use std::path::PathBuf; use std::path::PathBuf;
use types::ChainSpec; use types::ChainSpec;
/// Stores the core configuration for this Lighthouse instance. /// Stores the client configuration for this Lighthouse instance.
/// This struct is general, other components may implement more
/// specialized configuration structs.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Config { pub struct ClientConfig {
pub data_dir: PathBuf, pub data_dir: PathBuf,
pub spec: ChainSpec, pub spec: ChainSpec,
pub net_conf: network::NetworkConfiguration, pub net_conf: network::NetworkConfiguration,
@ -23,7 +21,7 @@ pub struct Config {
//pub ipc_conf: //pub ipc_conf:
} }
impl Default for Config { impl Default for ClientConfig {
/// Build a new lighthouse configuration from defaults. /// Build a new lighthouse configuration from defaults.
fn default() -> Self { fn default() -> Self {
let data_dir = { let data_dir = {
@ -47,10 +45,10 @@ impl Default for Config {
} }
} }
impl Config { impl ClientConfig {
/// Parses the CLI arguments into a `Config` struct. /// Parses the CLI arguments into a `Config` struct.
pub fn parse_args(args: ArgMatches, log: &slog::Logger) -> Result<Self, &'static str> { pub fn parse_args(args: ArgMatches, log: &slog::Logger) -> Result<Self, &'static str> {
let mut config = Config::default(); let mut config = ClientConfig::default();
// Network related args // Network related args

View File

@ -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<DiskDB>;
type SlotClock = SystemTimeSlotClock;
}
pub struct TestingClientType {}
impl ClientTypes for TestingClientType {
type DB = MemoryDB;
type SlotClock = TestingSlotClock;
type ForkChoice = BitwiseLMDGhost<MemoryDB>;
}

View File

@ -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<T: ClientTypes> {
config: ClientConfig,
// beacon_chain: Arc<BeaconChain<T, U, F>>,
// network: Option<Arc<NetworkService>>,
exit: exit_future::Exit,
exit_signal: Option<Signal>,
log: slog::Logger,
phantom: PhantomData<T>,
}
impl<T: ClientTypes> Client<T> {
pub fn new(
config: ClientConfig,
log: slog::Logger,
executor: TaskExecutor,
) -> error::Result<Self> {
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()
}
}

View File

@ -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<T: ClientTypes>(client: &Client<T>, 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(|_| ()));
}

View File

@ -0,0 +1,23 @@
[package]
name = "rpc"
version = "0.1.0"
authors = ["Age Manning <Age@AgeManning.com>"]
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" }

View File

@ -1,12 +1,9 @@
extern crate slog; extern crate slog;
mod config;
mod error;
mod rpc;
mod run; mod run;
use clap::{App, Arg}; use clap::{App, Arg};
use config::Config; use client::ClientConfig;
use slog::{o, Drain}; use slog::{o, Drain};
fn main() { fn main() {
@ -43,7 +40,7 @@ fn main() {
.get_matches(); .get_matches();
// invalid arguments, panic // 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);
} }

View File

@ -1,23 +1,13 @@
use crate::config::Config; use client::client_types::{StandardClientType, TestingClientType};
use crate::error; use client::error;
use crate::rpc::start_server; use client::{notifier, Client, ClientConfig};
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 futures::sync::oneshot; use futures::sync::oneshot;
use network::NetworkConfiguration; use futures::Future;
use slog::{error, info}; use slog::info;
use slot_clock::SystemTimeSlotClock;
use std::cell::RefCell; use std::cell::RefCell;
use std::sync::Arc; use tokio::runtime::Builder;
use tokio::runtime::{Builder, Runtime, TaskExecutor};
use types::{ChainSpec, Deposit, DepositData, DepositInput, Eth1Data, Hash256, Keypair};
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() let mut runtime = Builder::new()
.name_prefix("main-") .name_prefix("main-")
.build() .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)); let ctrlc_send_c = RefCell::new(Some(ctrlc_send));
ctrlc::set_handler(move || { ctrlc::set_handler(move || {
if let Some(ctrlc_send) = ctrlc_send_c.try_borrow_mut().unwrap().take() { if let Some(ctrlc_send) = ctrlc_send_c.try_borrow_mut().unwrap().take() {
ctrlc_send ctrlc_send.send(()).expect("Error sending ctrl-c message");
.send(())
.expect("Error sending termination message");
} }
}); });
let (exit_signal, exit) = exit_future::signal();
let executor = runtime.executor(); let executor = runtime.executor();
start(config, log, executor); // currently testing - using TestingNode type
let client: Client<TestingClientType> = Client::new(config, log.clone(), executor.clone())?;
notifier::run(&client, executor, exit);
runtime.block_on(ctrlc); runtime.block_on(ctrlc);
info!(log, "Shutting down."); info!(log, "Shutting down..");
//TODO: handle shutdown of processes gracefully exit_signal.fire();
drop(client);
runtime.shutdown_on_idle().wait().unwrap();
Ok(()) Ok(())
} }
fn start(config: Config, log: &slog::Logger, executor: TaskExecutor) {}