Add Eth2Config to runtime

This commit is contained in:
Paul Hauner
2019-06-08 13:17:03 -04:00
parent fd6766c268
commit d8fc5f31d8
13 changed files with 196 additions and 101 deletions

View File

@@ -3,16 +3,17 @@ extern crate slog;
mod run;
use clap::{App, Arg};
use client::ClientConfig;
use client::{ClientConfig, Eth2Config};
use slog::{crit, o, Drain};
use std::fs;
use std::fs::File;
use std::io::prelude::*;
pub const SAMPLE_CONFIG_FILENAME: &str = "beacon_node_config.sample.toml";
pub const CONFIG_FILENAME: &str = "beacon_node_config.toml";
pub const DEFAULT_DATA_DIR: &str = ".lighthouse";
pub const CLIENT_CONFIG_FILENAME: &str = "client_config.toml";
pub const ETH2_CONFIG_FILENAME: &str = "eth2_config.toml";
fn main() {
let decorator = slog_term::TermDecorator::new().build();
let drain = slog_term::CompactFormat::new(decorator).build().fuse();
@@ -100,9 +101,17 @@ fn main() {
.possible_values(&["disk", "memory"])
.default_value("memory"),
)
.arg(
Arg::with_name("recent_genesis")
.long("recent_genesis")
.help("When present, genesis will be within 30 minutes prior. Only for testing"),
)
.get_matches();
let mut config = match load_config(matches.value_of("data_dir")) {
let mut client_config = match load_config::<ClientConfig>(
matches.value_of("data_dir"),
CLIENT_CONFIG_FILENAME,
) {
Ok(c) => c,
Err(e) => {
crit!(logger, "Failed to load/generate a ChainConfig"; "error" => format!("{:?}", e));
@@ -110,15 +119,38 @@ fn main() {
}
};
match config.apply_cli_args(&matches) {
if let Some(data_dir) = matches.value_of("data_dir") {
client_config.data_dir = data_dir.to_string();
}
match client_config.apply_cli_args(&matches) {
Ok(()) => (),
Err(s) => {
crit!(logger, "Failed to parse CLI arguments"; "error" => s);
crit!(logger, "Failed to parse ClientConfig CLI arguments"; "error" => s);
return;
}
};
match run::run_beacon_node(config, &logger) {
let mut eth2_config = match load_config::<Eth2Config>(
matches.value_of("data_dir"),
ETH2_CONFIG_FILENAME,
) {
Ok(c) => c,
Err(e) => {
crit!(logger, "Failed to load/generate an Eth2Config"; "error" => format!("{:?}", e));
return;
}
};
match eth2_config.apply_cli_args(&matches) {
Ok(()) => (),
Err(s) => {
crit!(logger, "Failed to parse Eth2Config CLI arguments"; "error" => s);
return;
}
};
match run::run_beacon_node(client_config, eth2_config, &logger) {
Ok(_) => {}
Err(e) => crit!(logger, "Beacon node failed to start"; "reason" => format!("{:}", e)),
}
@@ -126,7 +158,10 @@ fn main() {
/// Loads a `ClientConfig` from file. If unable to load from file, generates a default
/// configuration and saves that as a sample file.
fn load_config(data_dir: Option<&str>) -> Result<ClientConfig, String> {
fn load_config<T>(data_dir: Option<&str>, config_filename: &str) -> Result<T, String>
where
T: Default + serde::de::DeserializeOwned + serde::Serialize,
{
let data_dir = data_dir.unwrap_or_else(|| DEFAULT_DATA_DIR);
let path = dirs::home_dir()
@@ -134,29 +169,28 @@ fn load_config(data_dir: Option<&str>) -> Result<ClientConfig, String> {
.join(&data_dir);
fs::create_dir_all(&path).map_err(|_| "Unable to open data_dir")?;
if let Ok(mut file) = File::open(path.join(CONFIG_FILENAME)) {
if let Ok(mut file) = File::open(path.join(config_filename)) {
let mut contents = String::new();
file.read_to_string(&mut contents).map_err(|e| {
format!(
"Unable to read existing {}. Error: {:?}",
CONFIG_FILENAME, e
config_filename, e
)
})?;
toml::from_str(&contents).map_err(|_| format!("Unable to parse {}", CONFIG_FILENAME))
toml::from_str(&contents).map_err(|e| format!("Unable to parse {}: {:?}", config_filename, e))
} else {
let mut config = ClientConfig::default();
config.data_dir = data_dir.to_string();
let config = T::default();
if let Ok(mut file) = File::create(path.join(SAMPLE_CONFIG_FILENAME)) {
if let Ok(mut file) = File::create(path.join(config_filename)) {
let toml_encoded = toml::to_string(&config).map_err(|e| {
format!(
"Failed to write configuration to {}. Error: {:?}",
SAMPLE_CONFIG_FILENAME, e
config_filename, e
)
})?;
file.write_all(toml_encoded.as_bytes())
.expect(&format!("Unable to write to {}", SAMPLE_CONFIG_FILENAME));
.expect(&format!("Unable to write to {}", config_filename));
}
Ok(config)

View File

@@ -1,9 +1,10 @@
use client::{
error, notifier, BeaconChainTypes, Client, ClientConfig, ClientType, InitialiseBeaconChain,
error, notifier, BeaconChainTypes, Client, ClientConfig, ClientType, Eth2Config,
InitialiseBeaconChain,
};
use futures::sync::oneshot;
use futures::Future;
use slog::{warn, error, info};
use slog::{error, info, warn};
use std::cell::RefCell;
use std::path::Path;
use std::path::PathBuf;
@@ -14,7 +15,11 @@ use tokio::runtime::TaskExecutor;
use tokio_timer::clock::Clock;
use types::{MainnetEthSpec, MinimalEthSpec};
pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Result<()> {
pub fn run_beacon_node(
client_config: ClientConfig,
eth2_config: Eth2Config,
log: &slog::Logger,
) -> error::Result<()> {
let runtime = Builder::new()
.name_prefix("main-")
.clock(Clock::system())
@@ -23,29 +28,54 @@ pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Resul
let executor = runtime.executor();
let db_path: PathBuf = config
let db_path: PathBuf = client_config
.db_path()
.ok_or_else::<error::Error, _>(|| "Unable to access database path".into())?;
let db_type = &config.db_type;
let spec_constants = config.spec_constants.clone();
let db_type = &client_config.db_type;
let spec_constants = eth2_config.spec_constants.clone();
let other_config = config.clone();
let other_client_config = client_config.clone();
warn!(
log,
"This software is EXPERIMENTAL and provides no guarantees or warranties."
);
let result = match (db_type.as_str(), spec_constants.as_str()) {
("disk", "minimal") => {
run::<ClientType<DiskStore, MinimalEthSpec>>(&db_path, config, executor, runtime, log)
}
("memory", "minimal") => {
run::<ClientType<MemoryStore, MinimalEthSpec>>(&db_path, config, executor, runtime, log)
}
("disk", "mainnet") => {
run::<ClientType<DiskStore, MainnetEthSpec>>(&db_path, config, executor, runtime, log)
}
("memory", "mainnet") => {
run::<ClientType<MemoryStore, MainnetEthSpec>>(&db_path, config, executor, runtime, log)
}
("disk", "minimal") => run::<ClientType<DiskStore, MinimalEthSpec>>(
&db_path,
client_config,
eth2_config,
executor,
runtime,
log,
),
("memory", "minimal") => run::<ClientType<MemoryStore, MinimalEthSpec>>(
&db_path,
client_config,
eth2_config,
executor,
runtime,
log,
),
("disk", "mainnet") => run::<ClientType<DiskStore, MainnetEthSpec>>(
&db_path,
client_config,
eth2_config,
executor,
runtime,
log,
),
("memory", "mainnet") => run::<ClientType<MemoryStore, MainnetEthSpec>>(
&db_path,
client_config,
eth2_config,
executor,
runtime,
log,
),
(db_type, spec) => {
error!(log, "Unknown runtime configuration"; "spec" => spec, "db_type" => db_type);
error!(log, "Unknown runtime configuration"; "spec_constants" => spec, "db_type" => db_type);
Err("Unknown specification and/or db_type.".into())
}
};
@@ -54,28 +84,11 @@ pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Resul
info!(
log,
"Started beacon node";
"p2p_listen_addresses" => format!("{:?}", &other_config.network.listen_addresses()),
"data_dir" => format!("{:?}", other_config.data_dir()),
"spec_constants" => &other_config.spec_constants,
"db_type" => &other_config.db_type,
"p2p_listen_addresses" => format!("{:?}", &other_client_config.network.listen_addresses()),
"data_dir" => format!("{:?}", other_client_config.data_dir()),
"spec_constants" => &spec_constants,
"db_type" => &other_client_config.db_type,
);
// `SHUFFLE_ROUND_COUNT == 10` in minimal, this is not considered safe.
if spec_constants.as_str() == "minimal" {
warn!(
log,
"The minimal specification does not use cryptographically secure committee selection."
)
}
// Mainnet is not really complete, it still generates determinitic, unsafe initial
// validators.
if spec_constants.as_str() == "mainnet" {
warn!(
log,
"The mainnet specification uses unsafe validator keypairs."
)
}
}
result
@@ -83,7 +96,8 @@ pub fn run_beacon_node(config: ClientConfig, log: &slog::Logger) -> error::Resul
pub fn run<T>(
db_path: &Path,
config: ClientConfig,
client_config: ClientConfig,
eth2_config: Eth2Config,
executor: TaskExecutor,
mut runtime: Runtime,
log: &slog::Logger,
@@ -94,7 +108,7 @@ where
{
let store = T::Store::open_database(&db_path)?;
let client: Client<T> = Client::new(config, store, log.clone(), &executor)?;
let client: Client<T> = Client::new(client_config, eth2_config, store, log.clone(), &executor)?;
// run service until ctrl-c
let (ctrlc_send, ctrlc_oneshot) = oneshot::channel();