Validator dir creation (#1746)

## Issue Addressed

Resolves #1744

## Proposed Changes

- Add `directory::ensure_dir_exists` to the `ValidatorDefinition::open_or_create` method 
- As @pawanjay176 suggested, making the `--validator-dir` non-global so users are forced to include the flag after the `validator` subcommand. Current behavior seems to be ignoring the flag if it comes after something like `validator import`

## Additional Info
N/A
This commit is contained in:
realbigsean 2020-10-08 21:01:32 +00:00
parent a67fa5f4a4
commit b69c63d486
5 changed files with 8 additions and 2 deletions

1
Cargo.lock generated
View File

@ -38,6 +38,7 @@ dependencies = [
name = "account_utils" name = "account_utils"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"directory",
"eth2_keystore", "eth2_keystore",
"eth2_wallet", "eth2_wallet",
"rand 0.7.3", "rand 0.7.3",

View File

@ -26,7 +26,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
Defaults to ~/.lighthouse/{testnet}/validators", Defaults to ~/.lighthouse/{testnet}/validators",
) )
.takes_value(true) .takes_value(true)
.global(true)
.conflicts_with("datadir"), .conflicts_with("datadir"),
) )
.subcommand(create::cli_app()) .subcommand(create::cli_app())

View File

@ -18,7 +18,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.value_name("WALLETS_DIRECTORY") .value_name("WALLETS_DIRECTORY")
.help("A path containing Eth2 EIP-2386 wallets. Defaults to ~/.lighthouse/{testnet}/wallets") .help("A path containing Eth2 EIP-2386 wallets. Defaults to ~/.lighthouse/{testnet}/wallets")
.takes_value(true) .takes_value(true)
.global(true)
.conflicts_with("datadir"), .conflicts_with("datadir"),
) )
.subcommand(create::cli_app()) .subcommand(create::cli_app())

View File

@ -19,3 +19,4 @@ types = { path = "../../consensus/types" }
validator_dir = { path = "../validator_dir" } validator_dir = { path = "../validator_dir" }
regex = "1.3.9" regex = "1.3.9"
rpassword = "5.0.0" rpassword = "5.0.0"
directory = { path = "../directory" }

View File

@ -4,6 +4,7 @@
//! attempt) to load into the `crate::intialized_validators::InitializedValidators` struct. //! attempt) to load into the `crate::intialized_validators::InitializedValidators` struct.
use crate::{create_with_600_perms, default_keystore_password_path, ZeroizeString}; use crate::{create_with_600_perms, default_keystore_password_path, ZeroizeString};
use directory::ensure_dir_exists;
use eth2_keystore::Keystore; use eth2_keystore::Keystore;
use regex::Regex; use regex::Regex;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@ -35,6 +36,8 @@ pub enum Error {
InvalidKeystorePubkey, InvalidKeystorePubkey,
/// The keystore was unable to be opened. /// The keystore was unable to be opened.
UnableToOpenKeystore(eth2_keystore::Error), UnableToOpenKeystore(eth2_keystore::Error),
/// The validator directory could not be created.
UnableToCreateValidatorDir(PathBuf),
} }
/// Defines how the validator client should attempt to sign messages for this validator. /// Defines how the validator client should attempt to sign messages for this validator.
@ -108,6 +111,9 @@ pub struct ValidatorDefinitions(Vec<ValidatorDefinition>);
impl ValidatorDefinitions { impl ValidatorDefinitions {
/// Open an existing file or create a new, empty one if it does not exist. /// Open an existing file or create a new, empty one if it does not exist.
pub fn open_or_create<P: AsRef<Path>>(validators_dir: P) -> Result<Self, Error> { pub fn open_or_create<P: AsRef<Path>>(validators_dir: P) -> Result<Self, Error> {
ensure_dir_exists(validators_dir.as_ref()).map_err(|_| {
Error::UnableToCreateValidatorDir(PathBuf::from(validators_dir.as_ref()))
})?;
let config_path = validators_dir.as_ref().join(CONFIG_FILENAME); let config_path = validators_dir.as_ref().join(CONFIG_FILENAME);
if !config_path.exists() { if !config_path.exists() {
let this = Self::default(); let this = Self::default();