Skip to content

Commit

Permalink
Use datadir flag for account manager
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Sep 2, 2020
1 parent 8c82769 commit 5ca1341
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 72 deletions.
34 changes: 22 additions & 12 deletions account_manager/src/validator/create.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{SECRETS_DIR_FLAG, VALIDATOR_DIR_FLAG, WALLETS_DIR_FLAG};
use account_utils::{random_password, strip_off_newlines, validator_definitions};
use clap::{App, Arg, ArgMatches};
use directory::{custom_base_dir, ensure_dir_exists, DEFAULT_SECRET_DIR, DEFAULT_VALIDATOR_DIR};
use directory::{custom_base_dir, ensure_dir_exists, DEFAULT_SECRET_DIR, DEFAULT_WALLET_DIR};
use environment::Environment;
use eth2_wallet::PlainText;
use eth2_wallet_manager::WalletManager;
Expand Down Expand Up @@ -42,14 +42,12 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.required(true),
)
.arg(
Arg::with_name(VALIDATOR_DIR_FLAG)
.long(VALIDATOR_DIR_FLAG)
.value_name("VALIDATOR_DIRECTORY")
.help(
"The path where the validator directories will be created. \
Defaults to ~/.lighthouse/{testnet}/validators",
)
.takes_value(true),
Arg::with_name(WALLETS_DIR_FLAG)
.long(WALLETS_DIR_FLAG)
.value_name(WALLETS_DIR_FLAG)
.help("A path containing Eth2 EIP-2386 wallets. Defaults to ~/.lighthouse/{testnet}/wallets")
.takes_value(true)
.conflicts_with("datadir"),
)
.arg(
Arg::with_name(SECRETS_DIR_FLAG)
Expand All @@ -59,6 +57,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
"The path where the validator keystore passwords will be stored. \
Defaults to ~/.lighthouse/{testnet}/secrets",
)
.conflicts_with("datadir")
.takes_value(true),
)
.arg(
Expand Down Expand Up @@ -104,14 +103,25 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
pub fn cli_run<T: EthSpec>(
matches: &ArgMatches,
mut env: Environment<T>,
wallet_base_dir: PathBuf,
validator_dir: PathBuf,
) -> Result<(), String> {
let spec = env.core_context().eth2_config.spec;

let name: String = clap_utils::parse_required(matches, WALLET_NAME_FLAG)?;
let wallet_password_path: PathBuf = clap_utils::parse_required(matches, WALLET_PASSWORD_FLAG)?;
let validator_dir = custom_base_dir(matches, VALIDATOR_DIR_FLAG, DEFAULT_VALIDATOR_DIR)?;
let secrets_dir = custom_base_dir(matches, SECRETS_DIR_FLAG, DEFAULT_SECRET_DIR)?;
let wallet_base_dir = if matches.value_of("datadir").is_some() {
let path: PathBuf = clap_utils::parse_required(matches, "datadir")?;
path.join(DEFAULT_WALLET_DIR)
} else {
custom_base_dir(matches, WALLETS_DIR_FLAG, DEFAULT_WALLET_DIR)?
};
let secrets_dir = if matches.value_of("datadir").is_some() {
let path: PathBuf = clap_utils::parse_required(matches, "datadir")?;
path.join(DEFAULT_SECRET_DIR)
} else {
custom_base_dir(matches, SECRETS_DIR_FLAG, DEFAULT_SECRET_DIR)?
};

let deposit_gwei = clap_utils::parse_optional(matches, DEPOSIT_GWEI_FLAG)?
.unwrap_or_else(|| spec.max_effective_balance);
let count: Option<usize> = clap_utils::parse_optional(matches, COUNT_FLAG)?;
Expand Down
15 changes: 2 additions & 13 deletions account_manager/src/validator/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::VALIDATOR_DIR_FLAG;
use clap::{App, Arg, ArgMatches};
use deposit_contract::DEPOSIT_GAS;
use directory::{custom_base_dir, DEFAULT_VALIDATOR_DIR};
use environment::Environment;
use futures::{
compat::Future01CompatExt,
Expand Down Expand Up @@ -47,16 +46,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
The deposit contract address will be determined by the --testnet-dir flag on the \
primary Lighthouse binary.",
)
.arg(
Arg::with_name(VALIDATOR_DIR_FLAG)
.long(VALIDATOR_DIR_FLAG)
.value_name("VALIDATOR_DIRECTORY")
.help(
"The path to the validator client data directory. \
Defaults to ~/.lighthouse/{tesntet}/validators",
)
.takes_value(true),
)
.arg(
Arg::with_name(VALIDATOR_FLAG)
.long(VALIDATOR_FLAG)
Expand Down Expand Up @@ -210,10 +199,10 @@ where
pub fn cli_run<T: EthSpec>(
matches: &ArgMatches<'_>,
mut env: Environment<T>,
validator_dir: PathBuf,
) -> Result<(), String> {
let log = env.core_context().log().clone();

let data_dir = custom_base_dir(matches, VALIDATOR_DIR_FLAG, DEFAULT_VALIDATOR_DIR)?;
let validator: String = clap_utils::parse_required(matches, VALIDATOR_FLAG)?;
let eth1_ipc_path: Option<PathBuf> = clap_utils::parse_optional(matches, ETH1_IPC_FLAG)?;
let eth1_http_url: Option<String> = clap_utils::parse_optional(matches, ETH1_HTTP_FLAG)?;
Expand All @@ -222,7 +211,7 @@ pub fn cli_run<T: EthSpec>(
let confirmation_batch_size: usize =
clap_utils::parse_required(matches, CONFIRMATION_BATCH_SIZE_FLAG)?;

let manager = ValidatorManager::open(&data_dir)
let manager = ValidatorManager::open(&validator_dir)
.map_err(|e| format!("Unable to read --{}: {:?}", VALIDATOR_DIR_FLAG, e))?;

let validators = match validator.as_ref() {
Expand Down
17 changes: 1 addition & 16 deletions account_manager/src/validator/import.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::VALIDATOR_DIR_FLAG;
use account_utils::{
eth2_keystore::Keystore,
read_password_from_user,
Expand All @@ -8,7 +7,6 @@ use account_utils::{
},
};
use clap::{App, Arg, ArgMatches};
use directory::{custom_base_dir, ensure_dir_exists, DEFAULT_VALIDATOR_DIR};
use std::fs;
use std::path::PathBuf;
use std::thread::sleep;
Expand Down Expand Up @@ -54,31 +52,18 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.required_unless(KEYSTORE_FLAG)
.takes_value(true),
)
.arg(
Arg::with_name(VALIDATOR_DIR_FLAG)
.long(VALIDATOR_DIR_FLAG)
.value_name("VALIDATOR_DIRECTORY")
.help(
"The path where the validator directories will be created. \
Defaults to ~/.lighthouse/{testnet}/validators",
)
.takes_value(true),
)
.arg(
Arg::with_name(STDIN_PASSWORD_FLAG)
.long(STDIN_PASSWORD_FLAG)
.help("If present, read passwords from stdin instead of tty."),
)
}

pub fn cli_run(matches: &ArgMatches) -> Result<(), String> {
pub fn cli_run(matches: &ArgMatches, validator_dir: PathBuf) -> Result<(), String> {
let keystore: Option<PathBuf> = clap_utils::parse_optional(matches, KEYSTORE_FLAG)?;
let keystores_dir: Option<PathBuf> = clap_utils::parse_optional(matches, DIR_FLAG)?;
let validator_dir = custom_base_dir(matches, VALIDATOR_DIR_FLAG, DEFAULT_VALIDATOR_DIR)?;
let stdin_password = matches.is_present(STDIN_PASSWORD_FLAG);

ensure_dir_exists(&validator_dir)?;

let mut defs = ValidatorDefinitions::open_or_create(&validator_dir)
.map_err(|e| format!("Unable to open {}: {:?}", CONFIG_FILENAME, e))?;

Expand Down
23 changes: 5 additions & 18 deletions account_manager/src/validator/list.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
use crate::VALIDATOR_DIR_FLAG;
use clap::{App, Arg, ArgMatches};
use directory::{custom_base_dir, DEFAULT_VALIDATOR_DIR};
use clap::App;
use std::path::PathBuf;
use validator_dir::Manager as ValidatorManager;

pub const CMD: &str = "list";

pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
App::new(CMD)
.arg(
Arg::with_name(VALIDATOR_DIR_FLAG)
.long(VALIDATOR_DIR_FLAG)
.value_name("VALIDATOR_DIRECTORY")
.help(
"The path to search for validator directories. \
Defaults to ~/.lighthouse/{testnet}/validators",
)
.takes_value(true),
)
.about("Lists the names of all validators.")
}

pub fn cli_run(matches: &ArgMatches<'_>) -> Result<(), String> {
let data_dir = custom_base_dir(matches, VALIDATOR_DIR_FLAG, DEFAULT_VALIDATOR_DIR)?;

let mgr = ValidatorManager::open(&data_dir)
pub fn cli_run(validator_dir: PathBuf) -> Result<(), String> {
let mgr = ValidatorManager::open(&validator_dir)
.map_err(|e| format!("Unable to read --{}: {:?}", VALIDATOR_DIR_FLAG, e))?;

for (name, _path) in mgr
.directory_names()
.map_err(|e| format!("Unable to list wallets: {:?}", e))?
.map_err(|e| format!("Unable to list validators: {:?}", e))?
{
println!("{}", name)
}
Expand Down
35 changes: 22 additions & 13 deletions account_manager/src/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ pub mod deposit;
pub mod import;
pub mod list;

use crate::WALLETS_DIR_FLAG;
use crate::VALIDATOR_DIR_FLAG;
use clap::{App, Arg, ArgMatches};
use directory::{custom_base_dir, DEFAULT_WALLET_DIR};
use directory::{custom_base_dir, DEFAULT_VALIDATOR_DIR};
use environment::Environment;
use std::path::PathBuf;
use types::EthSpec;

pub const CMD: &str = "validator";
Expand All @@ -15,27 +16,35 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
App::new(CMD)
.about("Provides commands for managing Eth2 validators.")
.arg(
Arg::with_name(WALLETS_DIR_FLAG)
.long(WALLETS_DIR_FLAG)
.value_name(WALLETS_DIR_FLAG)
.help("A path containing Eth2 EIP-2386 wallets. Defaults to ~/.lighthouse/{testnet}/wallets")
.takes_value(true)
.conflicts_with("datadir"),
Arg::with_name(VALIDATOR_DIR_FLAG)
.long(VALIDATOR_DIR_FLAG)
.value_name("VALIDATOR_DIRECTORY")
.help(
"The path to search for validator directories. \
Defaults to ~/.lighthouse/{testnet}/validators",
)
.takes_value(true),
)
.about("Lists the names of all validators.")
.subcommand(create::cli_app())
.subcommand(deposit::cli_app())
.subcommand(import::cli_app())
.subcommand(list::cli_app())
}

pub fn cli_run<T: EthSpec>(matches: &ArgMatches, env: Environment<T>) -> Result<(), String> {
let base_wallet_dir = custom_base_dir(matches, WALLETS_DIR_FLAG, DEFAULT_WALLET_DIR)?;
let base_dir = if matches.value_of("datadir").is_some() {
let path: PathBuf = clap_utils::parse_required(matches, "datadir")?;
path.join(DEFAULT_VALIDATOR_DIR)
} else {
custom_base_dir(matches, VALIDATOR_DIR_FLAG, DEFAULT_VALIDATOR_DIR)?
};

match matches.subcommand() {
(create::CMD, Some(matches)) => create::cli_run::<T>(matches, env, base_wallet_dir),
(deposit::CMD, Some(matches)) => deposit::cli_run::<T>(matches, env),
(import::CMD, Some(matches)) => import::cli_run(matches),
(list::CMD, Some(matches)) => list::cli_run(matches),
(create::CMD, Some(matches)) => create::cli_run::<T>(matches, env, base_dir),
(deposit::CMD, Some(matches)) => deposit::cli_run::<T>(matches, env, base_dir),
(import::CMD, Some(matches)) => import::cli_run(matches, base_dir),
(list::CMD, Some(_)) => list::cli_run(base_dir),
(unknown, _) => Err(format!(
"{} does not have a {} command. See --help",
CMD, unknown
Expand Down

0 comments on commit 5ca1341

Please sign in to comment.