From ad34a0fdf4fba83bcfb052a4bee5c060f1e30316 Mon Sep 17 00:00:00 2001 From: pawan Date: Wed, 2 Sep 2020 18:17:45 +0530 Subject: [PATCH] Add validator-dir cli option --- beacon_node/src/config.rs | 11 +++++- lighthouse/src/main.rs | 14 ++++--- testing/node_test_rig/src/lib.rs | 2 +- validator_client/src/cli.rs | 14 +++++++ validator_client/src/config.rs | 51 ++++++++++++++++--------- validator_client/src/lib.rs | 10 ++--- validator_client/src/validator_store.rs | 2 +- 7 files changed, 71 insertions(+), 33 deletions(-) diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index a5a79a12220..3033c6551e6 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -2,7 +2,7 @@ use beacon_chain::builder::PUBKEY_CACHE_FILENAME; use clap::ArgMatches; use clap_utils::BAD_TESTNET_DIR_MESSAGE; use client::{ClientConfig, ClientGenesis}; -use directory::{DEFAULT_BEACON_NODE_DIR, DEFAULT_NETWORK_DIR}; +use directory::{DEFAULT_BEACON_NODE_DIR, DEFAULT_NETWORK_DIR, DEFAULT_ROOT_DIR}; use eth2_libp2p::{multiaddr::Protocol, Enr, Multiaddr, NetworkConfig}; use eth2_testnet_config::Eth2TestnetConfig; use slog::{crit, info, Logger}; @@ -429,7 +429,14 @@ pub fn get_data_dir(cli_args: &ArgMatches) -> PathBuf { cli_args .value_of("datadir") .map(|path| PathBuf::from(path).join(DEFAULT_BEACON_NODE_DIR)) - .unwrap_or_else(|| directory::get_default_base_dir().join(DEFAULT_BEACON_NODE_DIR)) + .or_else(|| { + dirs::home_dir().map(|home| { + home.join(DEFAULT_ROOT_DIR) + .join(directory::get_testnet_dir(cli_args)) + .join(DEFAULT_BEACON_NODE_DIR) + }) + }) + .unwrap_or_else(|| PathBuf::from(".")) } /// Try to parse the eth2 testnet config from the `testnet`, `testnet-dir` flags in that order. diff --git a/lighthouse/src/main.rs b/lighthouse/src/main.rs index cb167e87c7f..d4a8a71b1ea 100644 --- a/lighthouse/src/main.rs +++ b/lighthouse/src/main.rs @@ -92,7 +92,10 @@ fn main() { .short("d") .value_name("DIR") .global(true) - .help("Root data directory for lighthouse keys and databases. Defaults to ~/.lighthouse") + .help( + "Root data directory for lighthouse keys and databases. \ + Defaults to $HOME/.lighthouse/{default-testnet}, \ + currently, $HOME/.lighthouse/medalla") .takes_value(true), ) .arg( @@ -206,8 +209,8 @@ fn run( let default_root_dir = dirs::home_dir() .map(|home| home.join(DEFAULT_ROOT_DIR)) .unwrap_or_else(|| PathBuf::from(".")); - let testnet_dir = default_root_dir.join(directory::get_testnet_dir(matches)); + let testnet_dir = default_root_dir.join(directory::get_testnet_dir(matches)); if !matches.is_present("datadir") && !testnet_dir.exists() { std::fs::create_dir_all(&testnet_dir) .map_err(|e| format!("Failed to create testnet directory: {}", e))?; @@ -215,14 +218,15 @@ fn run( let old_path = default_root_dir.join(dir); if old_path.exists() { if *dir == "validators" { + // Migrate the paths in the validator_definitions.yml file let mut def = ValidatorDefinitions::open(&old_path).map_err(|e| { - format!("Failed to open validator_definitions.yaml: {:?}", e) + format!("Failed to open validator_definitions.yml: {:?}", e) })?; def.migrate(&default_root_dir, &testnet_dir).map_err(|e| { - format!("Failed to migrate validator_definitions.yaml: {}", e) + format!("Failed to migrate validator_definitions.yml: {}", e) })?; def.save(&old_path).map_err(|e| { - format!("Failed to save validator_definitions.yaml: {:?}", e) + format!("Failed to save migrated validator_definitions.yml: {:?}", e) })?; } std::fs::rename(old_path, testnet_dir.join(dir)) diff --git a/testing/node_test_rig/src/lib.rs b/testing/node_test_rig/src/lib.rs index 9459a07b5b5..ddfc1d086bb 100644 --- a/testing/node_test_rig/src/lib.rs +++ b/testing/node_test_rig/src/lib.rs @@ -170,7 +170,7 @@ impl LocalValidatorClient { mut config: ValidatorConfig, files: ValidatorFiles, ) -> Result { - config.data_dir = files.datadir.path().into(); + config.validator_dir = files.datadir.path().into(); config.secrets_dir = files.secrets_dir.path().into(); ProductionValidatorClient::new(context, config) diff --git a/validator_client/src/cli.rs b/validator_client/src/cli.rs index b10ded3c050..d436fd44220 100644 --- a/validator_client/src/cli.rs +++ b/validator_client/src/cli.rs @@ -16,6 +16,19 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> { .default_value(&DEFAULT_HTTP_SERVER) .takes_value(true), ) + .arg( + Arg::with_name("validators-dir") + .long("validators-dir") + .value_name("VALIDATORS_DIR") + .help( + "The directory which contains the validator keystores, deposit data for \ + each validator along with the common slashing protection database \ + and the validator_definitions.yml" + ) + .takes_value(true) + .conflicts_with("datadir") + .requires("secrets-dir") + ) .arg( Arg::with_name("secrets-dir") .long("secrets-dir") @@ -26,6 +39,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> { name is the 0x-prefixed hex representation of the validators voting public \ key. Defaults to ~/.lighthouse/{testnet}/secrets.", ) + .requires("validators-dir") .takes_value(true), ) .arg(Arg::with_name("auto-register").long("auto-register").help( diff --git a/validator_client/src/config.rs b/validator_client/src/config.rs index 60232db84f5..9de2ce7b3c1 100644 --- a/validator_client/src/config.rs +++ b/validator_client/src/config.rs @@ -1,5 +1,5 @@ use clap::ArgMatches; -use clap_utils::{parse_optional, parse_path_with_default_in_home_dir}; +use clap_utils::{parse_optional, parse_required}; use directory::{get_testnet_dir, DEFAULT_ROOT_DIR, DEFAULT_SECRET_DIR, DEFAULT_VALIDATOR_DIR}; use serde_derive::{Deserialize, Serialize}; use std::path::PathBuf; @@ -13,7 +13,7 @@ pub const SLASHING_PROTECTION_FILENAME: &str = "slashing_protection.sqlite"; #[derive(Clone, Serialize, Deserialize)] pub struct Config { /// The data directory, which stores all validator databases - pub data_dir: PathBuf, + pub validator_dir: PathBuf, /// The directory containing the passwords to unlock validator keystores. pub secrets_dir: PathBuf, /// The http endpoint of the beacon node API. @@ -34,10 +34,10 @@ pub struct Config { impl Default for Config { /// Build a new configuration from defaults. fn default() -> Self { - let data_dir = directory::get_default_base_dir(); + let validator_dir = directory::get_default_base_dir().join(DEFAULT_VALIDATOR_DIR); let secrets_dir = directory::get_default_base_dir().join(DEFAULT_SECRET_DIR); Self { - data_dir, + validator_dir, secrets_dir, http_server: DEFAULT_HTTP_SERVER.to_string(), allow_unsynced_beacon_node: false, @@ -54,26 +54,39 @@ impl Config { pub fn from_cli(cli_args: &ArgMatches) -> Result { let mut config = Config::default(); - config.data_dir = parse_path_with_default_in_home_dir( - cli_args, - "datadir", - PathBuf::from(DEFAULT_ROOT_DIR) + let default_root_dir = dirs::home_dir() + .map(|home| home.join(DEFAULT_ROOT_DIR)) + .unwrap_or_else(|| PathBuf::from(".")); + + let (mut validator_dir, mut secrets_dir) = (None, None); + if cli_args.value_of("datadir").is_some() { + let base_dir: PathBuf = parse_required(cli_args, "datadir")?; + validator_dir = Some(base_dir.join(DEFAULT_VALIDATOR_DIR)); + secrets_dir = Some(base_dir.join(DEFAULT_SECRET_DIR)); + } + if cli_args.value_of("validators-dir").is_some() + && cli_args.value_of("secrets-dir").is_some() + { + validator_dir = Some(parse_required(cli_args, "validators-dir")?); + secrets_dir = Some(parse_required(cli_args, "secrets-dir")?); + } + + config.validator_dir = validator_dir.unwrap_or_else(|| { + default_root_dir .join(get_testnet_dir(cli_args)) - .join(DEFAULT_VALIDATOR_DIR), - )?; + .join(DEFAULT_VALIDATOR_DIR) + }); - config.secrets_dir = parse_path_with_default_in_home_dir( - cli_args, - "secrets-dir", - PathBuf::from(DEFAULT_ROOT_DIR) + config.secrets_dir = secrets_dir.unwrap_or_else(|| { + default_root_dir .join(get_testnet_dir(cli_args)) - .join(DEFAULT_SECRET_DIR), - )?; + .join(DEFAULT_SECRET_DIR) + }); - if !config.data_dir.exists() { + if !config.validator_dir.exists() { return Err(format!( - "The directory for validator data (--datadir) does not exist: {:?}", - config.data_dir + "The directory for validator data does not exist: {:?}", + config.validator_dir )); } diff --git a/validator_client/src/lib.rs b/validator_client/src/lib.rs index 220d82a66ae..7b5c969d368 100644 --- a/validator_client/src/lib.rs +++ b/validator_client/src/lib.rs @@ -67,18 +67,18 @@ impl ProductionValidatorClient { log, "Starting validator client"; "beacon_node" => &config.http_server, - "datadir" => format!("{:?}", config.data_dir), + "datadir" => format!("{:?}", config.validator_dir), ); - let mut validator_defs = ValidatorDefinitions::open_or_create(&config.data_dir) + let mut validator_defs = ValidatorDefinitions::open_or_create(&config.validator_dir) .map_err(|e| format!("Unable to open or create validator definitions: {:?}", e))?; if !config.disable_auto_discover { let new_validators = validator_defs - .discover_local_keystores(&config.data_dir, &config.secrets_dir, &log) + .discover_local_keystores(&config.validator_dir, &config.secrets_dir, &log) .map_err(|e| format!("Unable to discover local validator keystores: {:?}", e))?; validator_defs - .save(&config.data_dir) + .save(&config.validator_dir) .map_err(|e| format!("Unable to update validator definitions: {:?}", e))?; info!( log, @@ -89,7 +89,7 @@ impl ProductionValidatorClient { let validators = InitializedValidators::from_definitions( validator_defs, - config.data_dir.clone(), + config.validator_dir.clone(), config.strict_lockfiles, log.clone(), ) diff --git a/validator_client/src/validator_store.rs b/validator_client/src/validator_store.rs index f7d0442d376..57eddbeabe9 100644 --- a/validator_client/src/validator_store.rs +++ b/validator_client/src/validator_store.rs @@ -62,7 +62,7 @@ impl ValidatorStore { fork_service: ForkService, log: Logger, ) -> Result { - let slashing_db_path = config.data_dir.join(SLASHING_PROTECTION_FILENAME); + let slashing_db_path = config.validator_dir.join(SLASHING_PROTECTION_FILENAME); let slashing_protection = SlashingDatabase::open_or_create(&slashing_db_path).map_err(|e| { format!(