mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-30 12:47:05 +00:00
Update account manager CLI
This commit is contained in:
@@ -1,20 +1,17 @@
|
||||
mod cli;
|
||||
pub mod validator;
|
||||
|
||||
use bls::Keypair;
|
||||
use clap::ArgMatches;
|
||||
use environment::RuntimeContext;
|
||||
use slog::{crit, debug, info};
|
||||
use slog::{crit, info};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use types::{test_utils::generate_deterministic_keypair, EthSpec};
|
||||
use validator_client::Config as ValidatorClientConfig;
|
||||
use types::{ChainSpec, EthSpec};
|
||||
use validator::{ValidatorDirectory, ValidatorDirectoryBuilder};
|
||||
|
||||
pub use cli::cli_app;
|
||||
|
||||
pub const DEFAULT_DATA_DIR: &str = ".lighthouse-validator";
|
||||
pub const CLIENT_CONFIG_FILENAME: &str = "account-manager.toml";
|
||||
|
||||
/// Run the account manager, logging an error if the operation did not succeed.
|
||||
pub fn run<T: EthSpec>(matches: &ArgMatches, context: RuntimeContext<T>) {
|
||||
let log = context.log.clone();
|
||||
match run_account_manager(matches, context) {
|
||||
@@ -23,13 +20,14 @@ pub fn run<T: EthSpec>(matches: &ArgMatches, context: RuntimeContext<T>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Run the account manager, returning an error if the operation did not succeed.
|
||||
fn run_account_manager<T: EthSpec>(
|
||||
matches: &ArgMatches,
|
||||
context: RuntimeContext<T>,
|
||||
) -> Result<(), String> {
|
||||
let log = context.log.clone();
|
||||
|
||||
let data_dir = matches
|
||||
let datadir = matches
|
||||
.value_of("datadir")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| {
|
||||
@@ -39,128 +37,114 @@ fn run_account_manager<T: EthSpec>(
|
||||
panic!("Failed to find a home directory");
|
||||
}
|
||||
};
|
||||
default_dir.push(DEFAULT_DATA_DIR);
|
||||
default_dir.push(".lighthouse");
|
||||
default_dir.push("validator");
|
||||
default_dir
|
||||
});
|
||||
|
||||
fs::create_dir_all(&data_dir).map_err(|e| format!("Failed to initialize data dir: {}", e))?;
|
||||
fs::create_dir_all(&datadir).map_err(|e| format!("Failed to initialize datadir: {}", e))?;
|
||||
|
||||
let mut client_config = ValidatorClientConfig::default();
|
||||
client_config.data_dir = data_dir.clone();
|
||||
client_config
|
||||
.apply_cli_args(&matches, &log)
|
||||
.map_err(|e| format!("Failed to parse ClientConfig CLI arguments: {:?}", e))?;
|
||||
|
||||
info!(log, "Located data directory";
|
||||
"path" => &client_config.data_dir.to_str());
|
||||
|
||||
panic!()
|
||||
//
|
||||
}
|
||||
|
||||
pub fn run_old<T: EthSpec>(matches: &ArgMatches, context: RuntimeContext<T>) {
|
||||
let mut log = context.log;
|
||||
|
||||
let data_dir = match matches
|
||||
.value_of("datadir")
|
||||
.and_then(|v| Some(PathBuf::from(v)))
|
||||
{
|
||||
Some(v) => v,
|
||||
None => {
|
||||
// use the default
|
||||
let mut default_dir = match dirs::home_dir() {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
crit!(log, "Failed to find a home directory");
|
||||
return;
|
||||
}
|
||||
};
|
||||
default_dir.push(DEFAULT_DATA_DIR);
|
||||
default_dir
|
||||
}
|
||||
};
|
||||
|
||||
// create the directory if needed
|
||||
match fs::create_dir_all(&data_dir) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
crit!(log, "Failed to initialize data dir"; "error" => format!("{}", e));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let mut client_config = ValidatorClientConfig::default();
|
||||
|
||||
// Ensure the `data_dir` in the config matches that supplied to the CLI.
|
||||
client_config.data_dir = data_dir.clone();
|
||||
|
||||
if let Err(e) = client_config.apply_cli_args(&matches, &mut log) {
|
||||
crit!(log, "Failed to parse ClientConfig CLI arguments"; "error" => format!("{:?}", e));
|
||||
return;
|
||||
};
|
||||
|
||||
// Log configuration
|
||||
info!(log, "";
|
||||
"data_dir" => &client_config.data_dir.to_str());
|
||||
info!(
|
||||
log,
|
||||
"Located data directory";
|
||||
"path" => format!("{:?}", datadir)
|
||||
);
|
||||
|
||||
match matches.subcommand() {
|
||||
("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");
|
||||
if let Some(string) = m.value_of("validator count") {
|
||||
let n: usize = string.parse().expect("Invalid end validator count");
|
||||
|
||||
let indices: Vec<usize> = (i..i + n).collect();
|
||||
generate_deterministic_multiple(&indices, &client_config, &log)
|
||||
} else {
|
||||
generate_deterministic(i, &client_config, &log)
|
||||
}
|
||||
("validator", Some(matches)) => match matches.subcommand() {
|
||||
("new", Some(matches)) => new_validator_subcommand(matches, datadir, context)?,
|
||||
_ => {
|
||||
return Err("Invalid 'validator new' command. See --help.".to_string());
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
return Err("Invalid 'validator' command. See --help.".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Describes the crypto key generation methods for a validator.
|
||||
enum KeygenMethod {
|
||||
/// Produce an insecure "deterministic" keypair. Used only for interop and testing.
|
||||
Insecure(usize),
|
||||
/// Generate a new key from the `rand` thread random RNG.
|
||||
ThreadRandom,
|
||||
}
|
||||
|
||||
/// Process the subcommand for creating new validators.
|
||||
fn new_validator_subcommand<T: EthSpec>(
|
||||
matches: &ArgMatches,
|
||||
datadir: PathBuf,
|
||||
context: RuntimeContext<T>,
|
||||
) -> Result<(), String> {
|
||||
let log = context.log.clone();
|
||||
|
||||
let methods: Vec<KeygenMethod> = match matches.subcommand() {
|
||||
("insecure", Some(matches)) => {
|
||||
let first = matches
|
||||
.value_of("first")
|
||||
.ok_or_else(|| "No first index".to_string())?
|
||||
.parse::<usize>()
|
||||
.map_err(|e| format!("Unable to parse first index: {}", e))?;
|
||||
let last = matches
|
||||
.value_of("last")
|
||||
.ok_or_else(|| "No last index".to_string())?
|
||||
.parse::<usize>()
|
||||
.map_err(|e| format!("Unable to parse first index: {}", e))?;
|
||||
|
||||
(first..last).map(KeygenMethod::Insecure).collect()
|
||||
}
|
||||
("random", Some(matches)) => {
|
||||
let count = matches
|
||||
.value_of("validator_count")
|
||||
.ok_or_else(|| "No validator count".to_string())?
|
||||
.parse::<usize>()
|
||||
.map_err(|e| format!("Unable to parse validator count: {}", e))?;
|
||||
|
||||
(0..count).map(|_| KeygenMethod::ThreadRandom).collect()
|
||||
}
|
||||
_ => {
|
||||
crit!(
|
||||
log,
|
||||
"The account manager must be run with a subcommand. See help for more information."
|
||||
);
|
||||
return Err("Invalid 'validator' command. See --help.".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn generate_random(config: &ValidatorClientConfig, log: &slog::Logger) {
|
||||
save_key(&Keypair::random(), config, log)
|
||||
}
|
||||
let validators = make_validators(datadir.clone(), &methods, context.eth2_config.spec)?;
|
||||
|
||||
fn generate_deterministic_multiple(
|
||||
validator_indices: &[usize],
|
||||
config: &ValidatorClientConfig,
|
||||
log: &slog::Logger,
|
||||
) {
|
||||
for validator_index in validator_indices {
|
||||
generate_deterministic(*validator_index, config, log)
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_deterministic(
|
||||
validator_index: usize,
|
||||
config: &ValidatorClientConfig,
|
||||
log: &slog::Logger,
|
||||
) {
|
||||
save_key(
|
||||
&generate_deterministic_keypair(validator_index),
|
||||
config,
|
||||
info!(
|
||||
log,
|
||||
)
|
||||
}
|
||||
|
||||
fn save_key(keypair: &Keypair, config: &ValidatorClientConfig, log: &slog::Logger) {
|
||||
let key_path: PathBuf = config
|
||||
.save_key(&keypair)
|
||||
.expect("Unable to save newly generated private key.");
|
||||
debug!(
|
||||
log,
|
||||
"Keypair generated {:?}, saved to: {:?}",
|
||||
keypair.identifier(),
|
||||
key_path.to_string_lossy()
|
||||
"Generated validator directories";
|
||||
"base_path" => format!("{:?}", datadir),
|
||||
"count" => validators.len(),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Produces a validator directory for each of the key generation methods provided in `methods`.
|
||||
fn make_validators(
|
||||
datadir: PathBuf,
|
||||
methods: &[KeygenMethod],
|
||||
spec: ChainSpec,
|
||||
) -> Result<Vec<ValidatorDirectory>, String> {
|
||||
methods
|
||||
.iter()
|
||||
.map(|method| {
|
||||
let mut builder = ValidatorDirectoryBuilder::default()
|
||||
.spec(spec.clone())
|
||||
.full_deposit_amount()?;
|
||||
|
||||
builder = match method {
|
||||
KeygenMethod::Insecure(index) => builder.insecure_keypairs(*index),
|
||||
KeygenMethod::ThreadRandom => builder.thread_random_keypairs(),
|
||||
};
|
||||
|
||||
builder
|
||||
.create_directory(datadir.clone())?
|
||||
.write_keypair_files()?
|
||||
.write_eth1_data_file()?
|
||||
.build()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user