Fix issues with testnet dir and update docs (#992)

This commit is contained in:
Age Manning
2020-04-10 01:13:44 +10:00
parent 19b8c5a9e0
commit 7bf1ea2356
21 changed files with 2633 additions and 3414 deletions

View File

@@ -7,8 +7,7 @@
//! `Context` which can be handed to any service that wishes to start async tasks or perform
//! logging.
use clap::ArgMatches;
use eth2_config::{read_from_file, Eth2Config};
use eth2_config::Eth2Config;
use eth2_testnet_config::Eth2TestnetConfig;
use futures::{sync::oneshot, Future};
use slog::{info, o, Drain, Level, Logger};
@@ -139,72 +138,26 @@ impl<E: EthSpec> EnvironmentBuilder<E> {
}
/// Setups eth2 config using the CLI arguments.
pub fn setup_eth2_config(
pub fn eth2_testnet_config(
mut self,
datadir: PathBuf,
eth2_testnet_config: Eth2TestnetConfig<E>,
cli_args: &ArgMatches,
eth2_testnet_config: &Eth2TestnetConfig<E>,
) -> Result<Self, String> {
self.load_eth2_config(&datadir)?;
match cli_args.subcommand() {
("testnet", Some(sub_cli_args)) => {
// Modify the `SECONDS_PER_SLOT` "constant".
if let Some(slot_time) = sub_cli_args.value_of("slot-time") {
let slot_time = slot_time
.parse::<u64>()
.map_err(|e| format!("Unable to parse slot-time: {:?}", e))?;
self.eth2_config.spec.milliseconds_per_slot = slot_time;
}
}
_ => {
if !datadir.exists() {
// Create a new chain spec from the default configuration.
self.eth2_config.spec = eth2_testnet_config
.yaml_config
.as_ref()
.ok_or_else(|| {
"The testnet directory must contain a spec config".to_string()
})?
.apply_to_chain_spec::<E>(&self.eth2_config.spec)
.ok_or_else(|| {
format!(
"The loaded config is not compatible with the {} spec",
&self.eth2_config.spec_constants
)
})?;
}
}
}
// Create a new chain spec from the default configuration.
self.eth2_config.spec = eth2_testnet_config
.yaml_config
.as_ref()
.ok_or_else(|| "The testnet directory must contain a spec config".to_string())?
.apply_to_chain_spec::<E>(&self.eth2_config.spec)
.ok_or_else(|| {
format!(
"The loaded config is not compatible with the {} spec",
&self.eth2_config.spec_constants
)
})?;
Ok(self)
}
/// Loads the eth2 config if the file exists.
fn load_eth2_config(&mut self, datadir: &PathBuf) -> Result<(), String> {
let filename = datadir.join(ETH2_CONFIG_FILENAME);
if filename.exists() {
let loaded_eth2_config: Eth2Config = read_from_file(filename.clone())
.map_err(|e| format!("Unable to parse {:?} file: {:?}", filename, e))?
.ok_or_else(|| format!("{:?} file does not exist", filename))?;
// The loaded spec must be using the same spec constants (e.g., minimal, mainnet) as the
// client expects.
if loaded_eth2_config.spec_constants == self.eth2_config.spec_constants {
self.eth2_config = loaded_eth2_config;
} else {
return Err(format!(
"Eth2 config loaded from disk does not match client spec version. Got {} \
expected {}",
&loaded_eth2_config.spec_constants, &self.eth2_config.spec_constants
));
}
}
Ok(())
}
/// Consumes the builder, returning an `Environment`.
pub fn build(self) -> Result<Environment<E>, String> {
Ok(Environment {

View File

@@ -1,7 +1,7 @@
#[macro_use]
extern crate clap;
use beacon_node::{get_data_dir, get_eth2_testnet_config, get_testnet_dir, ProductionBeaconNode};
use beacon_node::{get_eth2_testnet_config, get_testnet_dir, ProductionBeaconNode};
use clap::{App, Arg, ArgMatches};
use env_logger::{Builder, Env};
use environment::EnvironmentBuilder;
@@ -73,6 +73,18 @@ fn main() {
.help("Data directory for lighthouse keys and databases.")
.takes_value(true),
)
.arg(
Arg::with_name("testnet-dir")
.long("testnet-dir")
.value_name("DIR")
.help(
"Path to directory containing eth2_testnet specs. Defaults to \
a hard-coded Lighthouse testnet. Only effective if there is no \
existing database.",
)
.takes_value(true)
.global(true),
)
.subcommand(beacon_node::cli_app())
.subcommand(validator_client::cli_app())
.subcommand(account_manager::cli_app())
@@ -110,15 +122,12 @@ fn run<E: EthSpec>(
.ok_or_else(|| "Expected --debug-level flag".to_string())?;
let log_format = matches.value_of("log-format");
let eth2_testnet_config = get_eth2_testnet_config(&get_testnet_dir(matches))?;
let mut environment = environment_builder
.async_logger(debug_level, log_format)?
.multi_threaded_tokio_runtime()?
.setup_eth2_config(
get_data_dir(matches),
get_eth2_testnet_config(&get_testnet_dir(matches))?,
matches,
)?
.eth2_testnet_config(&eth2_testnet_config)?
.build()?;
let log = environment.core_context().log;