From ab12787610792c33d49fb7602cc0b6b5924725f4 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sun, 9 Jun 2019 04:34:56 -0400 Subject: [PATCH] Update account manager config parsing --- account_manager/Cargo.toml | 1 + account_manager/src/main.rs | 45 +++++++++++++++---- beacon_node/Cargo.toml | 1 - beacon_node/src/main.rs | 14 +----- .../block_processing_builder.rs | 2 +- .../validator_statuses.rs | 5 +-- eth2/types/src/beacon_state.rs | 6 +-- eth2/types/src/fork.rs | 3 +- eth2/utils/eth2_config/src/lib.rs | 13 ++++++ validator_client/Cargo.toml | 1 - validator_client/src/config.rs | 40 ----------------- validator_client/src/main.rs | 31 ++++--------- 12 files changed, 66 insertions(+), 96 deletions(-) diff --git a/account_manager/Cargo.toml b/account_manager/Cargo.toml index 7b561869a7..48504d89ad 100644 --- a/account_manager/Cargo.toml +++ b/account_manager/Cargo.toml @@ -12,3 +12,4 @@ slog-term = "^2.4.0" slog-async = "^2.3.0" validator_client = { path = "../validator_client" } types = { path = "../eth2/types" } +eth2_config = { path = "../eth2/utils/eth2_config" } diff --git a/account_manager/src/main.rs b/account_manager/src/main.rs index aa691d31af..d820321013 100644 --- a/account_manager/src/main.rs +++ b/account_manager/src/main.rs @@ -1,9 +1,13 @@ use bls::Keypair; use clap::{App, Arg, SubCommand}; -use slog::{debug, info, o, Drain}; +use slog::{crit, debug, info, o, Drain}; use std::path::PathBuf; use types::test_utils::generate_deterministic_keypair; use validator_client::Config as ValidatorClientConfig; +use eth2_config::{get_data_dir}; + +pub const DEFAULT_DATA_DIR: &str = ".lighthouse-account-manager"; +pub const CLIENT_CONFIG_FILENAME: &str = "account-manager-config.toml"; fn main() { // Logging @@ -20,6 +24,7 @@ fn main() { .arg( Arg::with_name("datadir") .long("datadir") + .short("d") .value_name("DIR") .help("Data directory for keys and databases.") .takes_value(true), @@ -52,19 +57,43 @@ fn main() { .help("If supplied along with `index`, generates keys `i..i + n`.") .takes_value(true) .default_value("1"), - ), + ) ) .get_matches(); - let config = ValidatorClientConfig::parse_args(&matches, &log) - .expect("Unable to build a configuration for the account manager."); + let data_dir = match get_data_dir(&matches, PathBuf::from(DEFAULT_DATA_DIR)) { + Ok(dir) => dir, + Err(e) => { + crit!(log, "Failed to initialize data dir"; "error" => format!("{:?}", e)); + return + } + }; + + let mut client_config = ValidatorClientConfig::default(); + + if let Err(e) = client_config.apply_cli_args(&matches) { + crit!(log, "Failed to apply CLI args"; "error" => format!("{:?}", e)); + return + }; + + // Ensure the `data_dir` in the config matches that supplied to the CLI. + client_config.data_dir = data_dir.clone(); + + // Update the client config with any CLI args. + match client_config.apply_cli_args(&matches) { + Ok(()) => (), + Err(s) => { + crit!(log, "Failed to parse ClientConfig CLI arguments"; "error" => s); + return; + } + }; // Log configuration info!(log, ""; - "data_dir" => &config.data_dir.to_str()); + "data_dir" => &client_config.data_dir.to_str()); match matches.subcommand() { - ("generate", Some(_)) => generate_random(&config, &log), + ("generate", Some(_)) => generate_random(&client_config, &log), ("generate_deterministic", Some(m)) => { if let Some(string) = m.value_of("validator index") { let i: usize = string.parse().expect("Invalid validator index"); @@ -72,9 +101,9 @@ fn main() { let n: usize = string.parse().expect("Invalid end validator count"); let indices: Vec = (i..i + n).collect(); - generate_deterministic_multiple(&indices, &config, &log) + generate_deterministic_multiple(&indices, &client_config, &log) } else { - generate_deterministic(i, &config, &log) + generate_deterministic(i, &client_config, &log) } } } diff --git a/beacon_node/Cargo.toml b/beacon_node/Cargo.toml index 3edbf636d7..309f162e55 100644 --- a/beacon_node/Cargo.toml +++ b/beacon_node/Cargo.toml @@ -5,7 +5,6 @@ authors = ["Paul Hauner ", "Age Manning crit!(logger, "Beacon node failed to start"; "reason" => format!("{:}", e)), } } - -fn get_data_dir(args: &ArgMatches) -> Result { - if let Some(data_dir) = args.value_of("data_dir") { - Ok(PathBuf::from(data_dir)) - } else { - let path = dirs::home_dir() - .ok_or_else(|| "Unable to locate home directory")? - .join(&DEFAULT_DATA_DIR); - fs::create_dir_all(&path).map_err(|_| "Unable to create data_dir")?; - Ok(path) - } -} diff --git a/eth2/state_processing/src/per_block_processing/block_processing_builder.rs b/eth2/state_processing/src/per_block_processing/block_processing_builder.rs index 3675ef99e5..05a5a2de24 100644 --- a/eth2/state_processing/src/per_block_processing/block_processing_builder.rs +++ b/eth2/state_processing/src/per_block_processing/block_processing_builder.rs @@ -22,7 +22,7 @@ impl BlockProcessingBuilder { } } - pub fn set_slot(&mut self, slot: Slot, spec: &ChainSpec) { + pub fn set_slot(&mut self, slot: Slot) { self.state_builder.teleport_to_slot(slot); } diff --git a/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs b/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs index 76a485ea7b..45ef3419bc 100644 --- a/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs +++ b/eth2/state_processing/src/per_epoch_processing/validator_statuses.rs @@ -223,7 +223,7 @@ impl ValidatorStatuses { if is_from_epoch(a, state.current_epoch()) { status.is_current_epoch_attester = true; - if target_matches_epoch_start_block(a, state, state.current_epoch(), spec)? { + if target_matches_epoch_start_block(a, state, state.current_epoch())? { status.is_current_epoch_target_attester = true; } } else if is_from_epoch(a, state.previous_epoch()) { @@ -244,7 +244,7 @@ impl ValidatorStatuses { )?, }); - if target_matches_epoch_start_block(a, state, state.previous_epoch(), spec)? { + if target_matches_epoch_start_block(a, state, state.previous_epoch())? { status.is_previous_epoch_target_attester = true; } @@ -336,7 +336,6 @@ fn target_matches_epoch_start_block( a: &PendingAttestation, state: &BeaconState, epoch: Epoch, - spec: &ChainSpec, ) -> Result { let slot = epoch.start_slot(T::slots_per_epoch()); let state_boundary_root = *state.get_block_root(slot)?; diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 1c1fca6fb3..850cfcaa66 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -453,11 +453,7 @@ impl BeaconState { /// /// Spec v0.6.0 // FIXME(sproul): name swap with get_block_root - pub fn get_block_root_at_epoch( - &self, - epoch: Epoch, - spec: &ChainSpec, - ) -> Result<&Hash256, BeaconStateError> { + pub fn get_block_root_at_epoch(&self, epoch: Epoch) -> Result<&Hash256, BeaconStateError> { self.get_block_root(epoch.start_slot(T::slots_per_epoch())) } diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index 60ab208ad2..41a233a434 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -1,6 +1,6 @@ use crate::{ test_utils::{fork_from_hex_str, TestRandom}, - ChainSpec, Epoch, + Epoch, }; use serde_derive::{Deserialize, Serialize}; @@ -58,6 +58,7 @@ impl Fork { #[cfg(test)] mod tests { use super::*; + use crate::ChainSpec; ssz_tests!(Fork); cached_tree_hash_tests!(Fork); diff --git a/eth2/utils/eth2_config/src/lib.rs b/eth2/utils/eth2_config/src/lib.rs index df4229629d..c4d4736bdb 100644 --- a/eth2/utils/eth2_config/src/lib.rs +++ b/eth2/utils/eth2_config/src/lib.rs @@ -1,5 +1,6 @@ use clap::ArgMatches; use serde_derive::{Deserialize, Serialize}; +use std::fs; use std::fs::File; use std::io::prelude::*; use std::path::PathBuf; @@ -104,3 +105,15 @@ where Ok(None) } } + +pub fn get_data_dir(args: &ArgMatches, default_data_dir: PathBuf) -> Result { + if let Some(data_dir) = args.value_of("data_dir") { + Ok(PathBuf::from(data_dir)) + } else { + let path = dirs::home_dir() + .ok_or_else(|| "Unable to locate home directory")? + .join(&default_data_dir); + fs::create_dir_all(&path).map_err(|_| "Unable to create data_dir")?; + Ok(path) + } +} diff --git a/validator_client/Cargo.toml b/validator_client/Cargo.toml index 06ee02f538..1784bdcb1e 100644 --- a/validator_client/Cargo.toml +++ b/validator_client/Cargo.toml @@ -18,7 +18,6 @@ ssz = { path = "../eth2/utils/ssz" } eth2_config = { path = "../eth2/utils/eth2_config" } tree_hash = { path = "../eth2/utils/tree_hash" } clap = "2.32.0" -dirs = "1.0.3" grpcio = { version = "0.4", default-features = false, features = ["protobuf-codec"] } protobuf = "2.0.2" protos = { path = "../protos" } diff --git a/validator_client/src/config.rs b/validator_client/src/config.rs index 46ceaaf80a..d7664c1613 100644 --- a/validator_client/src/config.rs +++ b/validator_client/src/config.rs @@ -48,48 +48,8 @@ impl Config { }; Ok(()) - // } - /* - /// Build a new configuration from defaults, which are overrided by arguments provided. - pub fn parse_args(args: &ArgMatches, log: &slog::Logger) -> Result { - let mut config = Config::default(); - - // Use the specified datadir, or default in the home directory - if let Some(datadir) = args.value_of("datadir") { - config.data_dir = PathBuf::from(datadir); - info!(log, "Using custom data dir: {:?}", &config.data_dir); - }; - - fs::create_dir_all(&config.data_dir) - .unwrap_or_else(|_| panic!("Unable to create {:?}", &config.data_dir)); - - if let Some(srv) = args.value_of("server") { - //TODO: Validate the server value, to ensure it makes sense. - config.server = srv.to_string(); - info!(log, "Using custom server: {:?}", &config.server); - }; - - // TODO: Permit loading a custom spec from file. - if let Some(spec_str) = args.value_of("spec") { - info!(log, "Using custom spec: {:?}", spec_str); - config.spec = match spec_str { - "mainnet" => MainnetEthSpec::default_spec(), - "minimal" => MinimalEthSpec::default_spec(), - // Should be impossible due to clap's `possible_values(..)` function. - _ => unreachable!(), - }; - }; - // Log configuration - info!(log, ""; - "data_dir" => &config.data_dir.to_str(), - "server" => &config.server); - - Ok(config) - } - */ - /// Try to load keys from validator_dir, returning None if none are found or an error. #[allow(dead_code)] pub fn fetch_keys(&self, log: &slog::Logger) -> Option> { diff --git a/validator_client/src/main.rs b/validator_client/src/main.rs index 44dede100a..8a07894d5b 100644 --- a/validator_client/src/main.rs +++ b/validator_client/src/main.rs @@ -7,10 +7,9 @@ mod service; mod signer; use crate::config::Config as ValidatorClientConfig; -use std::fs; use crate::service::Service as ValidatorService; -use clap::{App, Arg, ArgMatches}; -use eth2_config::{read_from_file, write_to_file, Eth2Config}; +use clap::{App, Arg}; +use eth2_config::{get_data_dir, read_from_file, write_to_file, Eth2Config}; use protos::services_grpc::ValidatorServiceClient; use slog::{crit, error, info, o, Drain}; use std::path::PathBuf; @@ -18,8 +17,8 @@ use types::{Keypair, MainnetEthSpec, MinimalEthSpec}; pub const DEFAULT_SPEC: &str = "minimal"; pub const DEFAULT_DATA_DIR: &str = ".lighthouse-validator"; -pub const CLIENT_CONFIG_FILENAME: &str = "client_config.toml"; -pub const ETH2_CONFIG_FILENAME: &str = "eth2_config.toml"; +pub const CLIENT_CONFIG_FILENAME: &str = "client-config.toml"; +pub const ETH2_CONFIG_FILENAME: &str = "eth2-config.toml"; fn main() { // Logging @@ -38,7 +37,7 @@ fn main() { .long("datadir") .value_name("DIR") .help("Data directory for keys and databases.") - .takes_value(true) + .takes_value(true), ) .arg( Arg::with_name("eth-config") @@ -68,11 +67,11 @@ fn main() { ) .get_matches(); - let data_dir = match get_data_dir(&matches) { + let data_dir = match get_data_dir(&matches, PathBuf::from(DEFAULT_DATA_DIR)) { Ok(dir) => dir, Err(e) => { crit!(log, "Failed to initialize data dir"; "error" => format!("{:?}", e)); - return + return; } }; @@ -119,9 +118,7 @@ fn main() { // Attempt to load the `Eth2Config` from file. // // If the file doesn't exist, create a default one depending on the CLI flags. - let mut eth2_config = match read_from_file::( - eth2_config_path.clone() - ) { + let mut eth2_config = match read_from_file::(eth2_config_path.clone()) { Ok(Some(c)) => c, Ok(None) => { let default = match matches.value_of("spec-constants") { @@ -181,15 +178,3 @@ fn main() { Err(e) => crit!(log, "Validator client exited with error"; "error" => e.to_string()), } } - -fn get_data_dir(args: &ArgMatches) -> Result { - if let Some(data_dir) = args.value_of("data_dir") { - Ok(PathBuf::from(data_dir)) - } else { - let path = dirs::home_dir() - .ok_or_else(|| "Unable to locate home directory")? - .join(&DEFAULT_DATA_DIR); - fs::create_dir_all(&path).map_err(|_| "Unable to create data_dir")?; - Ok(path) - } -}