Files
lighthouse/validator_manager/src/lib.rs
chonghe fdf456f398 Validator manager commands for the Keymanager APIs (#6261)
* Validator manager commands for standard key-manager APIs

* Merge latest unstable

* Fix Some in lib.rs

* Replace Arg::with_name with Arg::new

* Update takes_value

* Remove clap::App

* Change App to Command

* Add command in use

* Remove generic in ArgMatches

* Fix matches.get_flag

* Fixes

* fix error handling

* SetTrue in import

* Fix

* Fix builder-proposal flag (will delete the flag later)

* Minor fix

* Fix prefer_builder_proposals

* Remove unwrap

* Error handling from Michael

* Add cli help text

* Use None in import to simplify

* Delete unwrap

* Revert flags option

* Simplify help command code

* Remove flag header in move

* Merge remote-tracking branch 'origin/unstable' into pahor/validator-manager-standard-keystore

* Add log in VC when keystore is deleted

* Delete duplicated log when validator does not exist

* Simplify log code

* Rename remove to delete

* cargo-fmt

* Try to remove a function

* make-cli

* Error handling

* Merge branch 'vm' of https://github.com/chong-he/lighthouse into vm

* Update CLI hel text

* make-cli

* Fix checks

* Merge branch 'vm' of https://github.com/chong-he/lighthouse into vm

* Try to fix check errors

* Fix test

* Remove changes

* Update flag name

* CLI display order

* Move builde_proposals flag

* Add doc

* mdlint

* Update validator_manager/src/list_validators.rs

Co-authored-by: Mac L <mjladson@pm.me>

* Delete empty line

* Fix list

* Simplify delete

* Add support to delete more validators

* Fix test

* Rename response

* Add (s)

* Add test to delete multiple validators

* Book and cli

* Make cli

* Only log when keystore is deleted

* Revise deletion log

* Add validator pubkey to error message

* Merge import

* Thank you Mac

* Test

* Add flags

* Error handling for password

* make cli

* Merge remote-tracking branch 'origin/unstable' into vm

* make cli

* Fix test

* Merge branch 'vm' of https://github.com/chong-he/lighthouse into vm

* Fix test

* vm test

* Debug trait thank you Michael

* Fix test

* Merge branch 'unstable' into vm

* test

* testing

* Combine import validator(s)

* make cli

* Add requires

* Update book

* mdlint

* Only show import log when import is successful

* delete testing

* Test for standard format

* Test standard format

* Test

* fix builder_proposals flag

* Fix test for standard format

* Add requires

* Fix vm test

* make cli

* Remove flag header

* Merge branch 'vm' of https://github.com/chong-he/lighthouse into vm

* make cli

* Delete space

* Merge branch 'vm' of https://github.com/chong-he/lighthouse into vm

* Merge branch 'unstable' into vm

* Rename delete_validator to delete_validators

* Rearrange

* Remove pub in run function

* Fix grammar

* Apply suggestions from code review

Co-authored-by: Michael Sproul <micsproul@gmail.com>

* Remove description

* Merge branch 'vm' of https://github.com/chong-he/lighthouse into vm

* Close bracket

* make cli

* Revise list code and test

* Revise import flag

* make cli

* Comment out test

* Update vm test

* Simplify

* Merge remote-tracking branch 'origin/unstable' into vm

* make cli

* Add test

* Add password as a requirement for keystore file

* Correct flags in docs

* typo
2024-10-29 04:14:06 +00:00

109 lines
4.1 KiB
Rust

use clap::{Arg, ArgAction, ArgMatches, Command};
use clap_utils::{get_color_style, FLAG_HEADER};
use common::write_to_json_file;
use environment::Environment;
use serde::Serialize;
use std::path::PathBuf;
use types::EthSpec;
pub mod common;
pub mod create_validators;
pub mod delete_validators;
pub mod import_validators;
pub mod list_validators;
pub mod move_validators;
pub const CMD: &str = "validator_manager";
/// This flag is on the top-level `lighthouse` binary.
const DUMP_CONFIGS_FLAG: &str = "dump-config";
/// Used only in testing, this allows a command to dump its configuration to a file and then exit
/// successfully. This allows for testing how the CLI arguments translate to some configuration.
pub enum DumpConfig {
Disabled,
Enabled(PathBuf),
}
impl DumpConfig {
/// Returns `Ok(true)` if the configuration was successfully written to a file and the
/// application should exit successfully without doing anything else.
pub fn should_exit_early<T: Serialize>(&self, config: &T) -> Result<bool, String> {
match self {
DumpConfig::Disabled => Ok(false),
DumpConfig::Enabled(dump_path) => {
dbg!(dump_path);
write_to_json_file(dump_path, config)?;
Ok(true)
}
}
}
}
pub fn cli_app() -> Command {
Command::new(CMD)
.visible_aliases(["vm", "validator-manager"])
.display_order(0)
.styles(get_color_style())
.about("Utilities for managing a Lighthouse validator client via the HTTP API.")
.arg(
Arg::new("help")
.long("help")
.short('h')
.help("Prints help information")
.action(ArgAction::HelpLong)
.display_order(0)
.help_heading(FLAG_HEADER)
.global(true),
)
.subcommand(create_validators::cli_app())
.subcommand(import_validators::cli_app())
.subcommand(move_validators::cli_app())
.subcommand(list_validators::cli_app())
.subcommand(delete_validators::cli_app())
}
/// Run the account manager, returning an error if the operation did not succeed.
pub fn run<E: EthSpec>(matches: &ArgMatches, env: Environment<E>) -> Result<(), String> {
let context = env.core_context();
let spec = context.eth2_config.spec;
let dump_config = clap_utils::parse_optional(matches, DUMP_CONFIGS_FLAG)?
.map(DumpConfig::Enabled)
.unwrap_or_else(|| DumpConfig::Disabled);
context
.executor
// This `block_on_dangerous` call reasonable since it is at the very highest level of the
// application, the rest of which is all async. All other functions below this should be
// async and should never call `block_on_dangerous` themselves.
.block_on_dangerous(
async {
match matches.subcommand() {
Some((create_validators::CMD, matches)) => {
create_validators::cli_run::<E>(matches, &spec, dump_config).await
}
Some((import_validators::CMD, matches)) => {
import_validators::cli_run(matches, dump_config).await
}
Some((move_validators::CMD, matches)) => {
move_validators::cli_run(matches, dump_config).await
}
Some((list_validators::CMD, matches)) => {
list_validators::cli_run(matches, dump_config).await
}
Some((delete_validators::CMD, matches)) => {
delete_validators::cli_run(matches, dump_config).await
}
Some(("", _)) => Err("No command supplied. See --help.".to_string()),
Some((unknown, _)) => Err(format!(
"{} is not a valid {} command. See --help.",
unknown, CMD
)),
_ => Err("No command supplied. See --help.".to_string()),
}
},
"validator_manager",
)
.ok_or("Shutting down")?
}