Skip to content

Commit

Permalink
Add validator-dir cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Sep 2, 2020
1 parent 04fd9f7 commit ad34a0f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 33 deletions.
11 changes: 9 additions & 2 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 9 additions & 5 deletions lighthouse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -206,23 +209,24 @@ fn run<E: EthSpec>(
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))?;
for dir in ["validators", "wallets", "secrets", "beacon"].iter() {
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))
Expand Down
2 changes: 1 addition & 1 deletion testing/node_test_rig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl<E: EthSpec> LocalValidatorClient<E> {
mut config: ValidatorConfig,
files: ValidatorFiles,
) -> Result<Self, String> {
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)
Expand Down
14 changes: 14 additions & 0 deletions validator_client/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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(
Expand Down
51 changes: 32 additions & 19 deletions validator_client/src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -54,26 +54,39 @@ impl Config {
pub fn from_cli(cli_args: &ArgMatches) -> Result<Config, String> {
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
));
}

Expand Down
10 changes: 5 additions & 5 deletions validator_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ impl<T: EthSpec> ProductionValidatorClient<T> {
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,
Expand All @@ -89,7 +89,7 @@ impl<T: EthSpec> ProductionValidatorClient<T> {

let validators = InitializedValidators::from_definitions(
validator_defs,
config.data_dir.clone(),
config.validator_dir.clone(),
config.strict_lockfiles,
log.clone(),
)
Expand Down
2 changes: 1 addition & 1 deletion validator_client/src/validator_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
fork_service: ForkService<T, E>,
log: Logger,
) -> Result<Self, String> {
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!(
Expand Down

0 comments on commit ad34a0f

Please sign in to comment.