mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-02 16:21:42 +00:00
Change DB Manager Clap usage to derive (#5898)
* implement clap derive for the db manager * tweak some clap configs * make cli local * add global to help flag * fmt * Merge branch 'unstable' of https://github.com/sigp/lighthouse into HEAD * merge * add enum constraints to flag
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
pub mod cli;
|
||||
use crate::cli::DatabaseManager;
|
||||
use crate::cli::Migrate;
|
||||
use crate::cli::PruneStates;
|
||||
use beacon_chain::{
|
||||
builder::Witness, eth1_chain::CachingEth1Backend, schema_change::migrate_schema,
|
||||
slot_clock::SystemTimeSlotClock,
|
||||
};
|
||||
use beacon_node::{get_data_dir, get_slots_per_restore_point, ClientConfig};
|
||||
use clap::{Arg, ArgAction, ArgMatches, Command};
|
||||
use clap_utils::{get_color_style, FLAG_HEADER};
|
||||
use clap::ArgMatches;
|
||||
use clap::ValueEnum;
|
||||
use cli::{Compact, Inspect};
|
||||
use environment::{Environment, RuntimeContext};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use slog::{info, warn, Logger};
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
@@ -16,250 +22,30 @@ use store::{
|
||||
metadata::{SchemaVersion, CURRENT_SCHEMA_VERSION},
|
||||
DBColumn, HotColdDB, KeyValueStore, LevelDB,
|
||||
};
|
||||
use strum::{EnumString, EnumVariantNames, VariantNames};
|
||||
use strum::{EnumString, EnumVariantNames};
|
||||
use types::{BeaconState, EthSpec, Slot};
|
||||
|
||||
pub const CMD: &str = "database_manager";
|
||||
|
||||
pub fn version_cli_app() -> Command {
|
||||
Command::new("version")
|
||||
.visible_aliases(["v"])
|
||||
.styles(get_color_style())
|
||||
.about("Display database schema version")
|
||||
}
|
||||
|
||||
pub fn migrate_cli_app() -> Command {
|
||||
Command::new("migrate")
|
||||
.styles(get_color_style())
|
||||
.about("Migrate the database to a specific schema version")
|
||||
.arg(
|
||||
Arg::new("to")
|
||||
.long("to")
|
||||
.value_name("VERSION")
|
||||
.help("Schema version to migrate to")
|
||||
.action(ArgAction::Set)
|
||||
.required(true),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn inspect_cli_app() -> Command {
|
||||
Command::new("inspect")
|
||||
.styles(get_color_style())
|
||||
.about("Inspect raw database values")
|
||||
.arg(
|
||||
Arg::new("column")
|
||||
.long("column")
|
||||
.value_name("TAG")
|
||||
.help("3-byte column ID (see `DBColumn`)")
|
||||
.action(ArgAction::Set)
|
||||
.required(true)
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("output")
|
||||
.long("output")
|
||||
.value_name("TARGET")
|
||||
.help("Select the type of output to show")
|
||||
.default_value("sizes")
|
||||
.value_parser(InspectTarget::VARIANTS.to_vec())
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("skip")
|
||||
.long("skip")
|
||||
.value_name("N")
|
||||
.help("Skip over the first N keys")
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("limit")
|
||||
.long("limit")
|
||||
.value_name("N")
|
||||
.help("Output at most N keys")
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("freezer")
|
||||
.long("freezer")
|
||||
.help("Inspect the freezer DB rather than the hot DB")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help_heading(FLAG_HEADER)
|
||||
.conflicts_with("blobs-db")
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("blobs-db")
|
||||
.long("blobs-db")
|
||||
.help("Inspect the blobs DB rather than the hot DB")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help_heading(FLAG_HEADER)
|
||||
.conflicts_with("freezer")
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("output-dir")
|
||||
.long("output-dir")
|
||||
.value_name("DIR")
|
||||
.help("Base directory for the output files. Defaults to the current directory")
|
||||
.action(ArgAction::Set)
|
||||
.display_order(0),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn compact_cli_app() -> Command {
|
||||
Command::new("compact")
|
||||
.styles(get_color_style())
|
||||
.about("Compact database manually")
|
||||
.arg(
|
||||
Arg::new("column")
|
||||
.long("column")
|
||||
.value_name("TAG")
|
||||
.help("3-byte column ID (see `DBColumn`)")
|
||||
.action(ArgAction::Set)
|
||||
.required(true)
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("freezer")
|
||||
.long("freezer")
|
||||
.help("Inspect the freezer DB rather than the hot DB")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help_heading(FLAG_HEADER)
|
||||
.conflicts_with("blobs-db")
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("blobs-db")
|
||||
.long("blobs-db")
|
||||
.help("Inspect the blobs DB rather than the hot DB")
|
||||
.action(ArgAction::SetTrue)
|
||||
.help_heading(FLAG_HEADER)
|
||||
.conflicts_with("freezer")
|
||||
.display_order(0),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn prune_payloads_app() -> Command {
|
||||
Command::new("prune-payloads")
|
||||
.alias("prune_payloads")
|
||||
.styles(get_color_style())
|
||||
.about("Prune finalized execution payloads")
|
||||
}
|
||||
|
||||
pub fn prune_blobs_app() -> Command {
|
||||
Command::new("prune-blobs")
|
||||
.alias("prune_blobs")
|
||||
.styles(get_color_style())
|
||||
.about("Prune blobs older than data availability boundary")
|
||||
}
|
||||
|
||||
pub fn prune_states_app() -> Command {
|
||||
Command::new("prune-states")
|
||||
.alias("prune_states")
|
||||
.arg(
|
||||
Arg::new("confirm")
|
||||
.long("confirm")
|
||||
.help(
|
||||
"Commit to pruning states irreversably. Without this flag the command will \
|
||||
just check that the database is capable of being pruned.",
|
||||
)
|
||||
.action(ArgAction::SetTrue)
|
||||
.help_heading(FLAG_HEADER)
|
||||
.display_order(0),
|
||||
)
|
||||
.styles(get_color_style())
|
||||
.about("Prune all beacon states from the freezer database")
|
||||
}
|
||||
|
||||
pub fn cli_app() -> Command {
|
||||
Command::new(CMD)
|
||||
.display_order(0)
|
||||
.visible_aliases(["db"])
|
||||
.styles(get_color_style())
|
||||
.about("Manage a beacon node database")
|
||||
.arg(
|
||||
Arg::new("help")
|
||||
.long("help")
|
||||
.short('h')
|
||||
.help("Prints help information")
|
||||
.action(ArgAction::HelpLong)
|
||||
.display_order(0)
|
||||
.help_heading(FLAG_HEADER),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("slots-per-restore-point")
|
||||
.long("slots-per-restore-point")
|
||||
.value_name("SLOT_COUNT")
|
||||
.help(
|
||||
"Specifies how often a freezer DB restore point should be stored. \
|
||||
Cannot be changed after initialization. \
|
||||
[default: 2048 (mainnet) or 64 (minimal)]",
|
||||
)
|
||||
.action(ArgAction::Set)
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("freezer-dir")
|
||||
.long("freezer-dir")
|
||||
.value_name("DIR")
|
||||
.help("Data directory for the freezer database.")
|
||||
.action(ArgAction::Set)
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("blob-prune-margin-epochs")
|
||||
.long("blob-prune-margin-epochs")
|
||||
.value_name("EPOCHS")
|
||||
.help(
|
||||
"The margin for blob pruning in epochs. The oldest blobs are pruned \
|
||||
up until data_availability_boundary - blob_prune_margin_epochs.",
|
||||
)
|
||||
.action(ArgAction::Set)
|
||||
.default_value("0")
|
||||
.display_order(0),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("blobs-dir")
|
||||
.long("blobs-dir")
|
||||
.value_name("DIR")
|
||||
.help("Data directory for the blobs database.")
|
||||
.action(ArgAction::Set)
|
||||
.display_order(0),
|
||||
)
|
||||
.subcommand(migrate_cli_app())
|
||||
.subcommand(version_cli_app())
|
||||
.subcommand(inspect_cli_app())
|
||||
.subcommand(compact_cli_app())
|
||||
.subcommand(prune_payloads_app())
|
||||
.subcommand(prune_blobs_app())
|
||||
.subcommand(prune_states_app())
|
||||
}
|
||||
|
||||
fn parse_client_config<E: EthSpec>(
|
||||
cli_args: &ArgMatches,
|
||||
database_manager_config: &DatabaseManager,
|
||||
_env: &Environment<E>,
|
||||
) -> Result<ClientConfig, String> {
|
||||
let mut client_config = ClientConfig::default();
|
||||
|
||||
client_config.set_data_dir(get_data_dir(cli_args));
|
||||
client_config
|
||||
.freezer_db_path
|
||||
.clone_from(&database_manager_config.freezer_dir);
|
||||
client_config
|
||||
.blobs_db_path
|
||||
.clone_from(&database_manager_config.blobs_dir);
|
||||
|
||||
if let Some(freezer_dir) = clap_utils::parse_optional(cli_args, "freezer-dir")? {
|
||||
client_config.freezer_db_path = Some(freezer_dir);
|
||||
}
|
||||
let (sprp, sprp_explicit) =
|
||||
get_slots_per_restore_point::<E>(database_manager_config.slots_per_restore_point)?;
|
||||
|
||||
if let Some(blobs_db_dir) = clap_utils::parse_optional(cli_args, "blobs-dir")? {
|
||||
client_config.blobs_db_path = Some(blobs_db_dir);
|
||||
}
|
||||
|
||||
let (sprp, sprp_explicit) = get_slots_per_restore_point::<E>(cli_args)?;
|
||||
client_config.store.slots_per_restore_point = sprp;
|
||||
client_config.store.slots_per_restore_point_set_explicitly = sprp_explicit;
|
||||
|
||||
if let Some(blob_prune_margin_epochs) =
|
||||
clap_utils::parse_optional(cli_args, "blob-prune-margin-epochs")?
|
||||
{
|
||||
client_config.store.blob_prune_margin_epochs = blob_prune_margin_epochs;
|
||||
}
|
||||
client_config.store.blob_prune_margin_epochs = database_manager_config.blob_prune_margin_epochs;
|
||||
|
||||
Ok(client_config)
|
||||
}
|
||||
@@ -301,15 +87,21 @@ pub fn display_db_version<E: EthSpec>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, EnumString, EnumVariantNames)]
|
||||
#[derive(
|
||||
Debug, PartialEq, Eq, Clone, EnumString, Deserialize, Serialize, EnumVariantNames, ValueEnum,
|
||||
)]
|
||||
pub enum InspectTarget {
|
||||
#[strum(serialize = "sizes")]
|
||||
#[clap(name = "sizes")]
|
||||
ValueSizes,
|
||||
#[strum(serialize = "total")]
|
||||
#[clap(name = "total")]
|
||||
ValueTotal,
|
||||
#[strum(serialize = "values")]
|
||||
#[clap(name = "values")]
|
||||
Values,
|
||||
#[strum(serialize = "gaps")]
|
||||
#[clap(name = "gaps")]
|
||||
Gaps,
|
||||
}
|
||||
|
||||
@@ -324,16 +116,18 @@ pub struct InspectConfig {
|
||||
output_dir: PathBuf,
|
||||
}
|
||||
|
||||
fn parse_inspect_config(cli_args: &ArgMatches) -> Result<InspectConfig, String> {
|
||||
let column = clap_utils::parse_required(cli_args, "column")?;
|
||||
let target = clap_utils::parse_required(cli_args, "output")?;
|
||||
let skip = clap_utils::parse_optional(cli_args, "skip")?;
|
||||
let limit = clap_utils::parse_optional(cli_args, "limit")?;
|
||||
let freezer = cli_args.get_flag("freezer");
|
||||
let blobs_db = cli_args.get_flag("blobs-db");
|
||||
fn parse_inspect_config(inspect_config: &Inspect) -> Result<InspectConfig, String> {
|
||||
let column: DBColumn = inspect_config
|
||||
.column
|
||||
.parse()
|
||||
.map_err(|e| format!("Unable to parse column flag: {e:?}"))?;
|
||||
let target: InspectTarget = inspect_config.output.clone();
|
||||
let skip = inspect_config.skip;
|
||||
let limit = inspect_config.limit;
|
||||
let freezer = inspect_config.freezer;
|
||||
let blobs_db = inspect_config.blobs_db;
|
||||
|
||||
let output_dir: PathBuf =
|
||||
clap_utils::parse_optional(cli_args, "output-dir")?.unwrap_or_else(PathBuf::new);
|
||||
let output_dir: PathBuf = inspect_config.output_dir.clone().unwrap_or_default();
|
||||
Ok(InspectConfig {
|
||||
column,
|
||||
target,
|
||||
@@ -450,10 +244,13 @@ pub struct CompactConfig {
|
||||
blobs_db: bool,
|
||||
}
|
||||
|
||||
fn parse_compact_config(cli_args: &ArgMatches) -> Result<CompactConfig, String> {
|
||||
let column = clap_utils::parse_required(cli_args, "column")?;
|
||||
let freezer = cli_args.get_flag("freezer");
|
||||
let blobs_db = cli_args.get_flag("blobs-db");
|
||||
fn parse_compact_config(compact_config: &Compact) -> Result<CompactConfig, String> {
|
||||
let column: DBColumn = compact_config
|
||||
.column
|
||||
.parse()
|
||||
.expect("column is a required field");
|
||||
let freezer = compact_config.freezer;
|
||||
let blobs_db = compact_config.blobs_db;
|
||||
Ok(CompactConfig {
|
||||
column,
|
||||
freezer,
|
||||
@@ -492,8 +289,8 @@ pub struct MigrateConfig {
|
||||
to: SchemaVersion,
|
||||
}
|
||||
|
||||
fn parse_migrate_config(cli_args: &ArgMatches) -> Result<MigrateConfig, String> {
|
||||
let to = SchemaVersion(clap_utils::parse_required(cli_args, "to")?);
|
||||
fn parse_migrate_config(migrate_config: &Migrate) -> Result<MigrateConfig, String> {
|
||||
let to = SchemaVersion(migrate_config.to);
|
||||
|
||||
Ok(MigrateConfig { to })
|
||||
}
|
||||
@@ -595,9 +392,10 @@ pub fn prune_blobs<E: EthSpec>(
|
||||
pub struct PruneStatesConfig {
|
||||
confirm: bool,
|
||||
}
|
||||
|
||||
fn parse_prune_states_config(cli_args: &ArgMatches) -> Result<PruneStatesConfig, String> {
|
||||
let confirm = cli_args.get_flag("confirm");
|
||||
fn parse_prune_states_config(
|
||||
prune_states_config: &PruneStates,
|
||||
) -> Result<PruneStatesConfig, String> {
|
||||
let confirm = prune_states_config.confirm;
|
||||
Ok(PruneStatesConfig { confirm })
|
||||
}
|
||||
|
||||
@@ -676,33 +474,35 @@ pub fn prune_states<E: EthSpec>(
|
||||
}
|
||||
|
||||
/// Run the database manager, returning an error string if the operation did not succeed.
|
||||
pub fn run<E: EthSpec>(cli_args: &ArgMatches, env: Environment<E>) -> Result<(), String> {
|
||||
let client_config = parse_client_config(cli_args, &env)?;
|
||||
pub fn run<E: EthSpec>(
|
||||
cli_args: &ArgMatches,
|
||||
db_manager_config: &DatabaseManager,
|
||||
env: Environment<E>,
|
||||
) -> Result<(), String> {
|
||||
let client_config = parse_client_config(cli_args, db_manager_config, &env)?;
|
||||
let context = env.core_context();
|
||||
let log = context.log().clone();
|
||||
let format_err = |e| format!("Fatal error: {:?}", e);
|
||||
|
||||
match cli_args.subcommand() {
|
||||
Some(("version", _)) => {
|
||||
display_db_version(client_config, &context, log).map_err(format_err)
|
||||
}
|
||||
Some(("migrate", cli_args)) => {
|
||||
let migrate_config = parse_migrate_config(cli_args)?;
|
||||
match &db_manager_config.subcommand {
|
||||
cli::DatabaseManagerSubcommand::Migrate(migrate_config) => {
|
||||
let migrate_config = parse_migrate_config(migrate_config)?;
|
||||
migrate_db(migrate_config, client_config, &context, log).map_err(format_err)
|
||||
}
|
||||
Some(("inspect", cli_args)) => {
|
||||
let inspect_config = parse_inspect_config(cli_args)?;
|
||||
cli::DatabaseManagerSubcommand::Inspect(inspect_config) => {
|
||||
let inspect_config = parse_inspect_config(inspect_config)?;
|
||||
inspect_db::<E>(inspect_config, client_config)
|
||||
}
|
||||
Some(("compact", cli_args)) => {
|
||||
let compact_config = parse_compact_config(cli_args)?;
|
||||
compact_db::<E>(compact_config, client_config, log).map_err(format_err)
|
||||
cli::DatabaseManagerSubcommand::Version(_) => {
|
||||
display_db_version(client_config, &context, log).map_err(format_err)
|
||||
}
|
||||
Some(("prune-payloads", _)) => {
|
||||
cli::DatabaseManagerSubcommand::PrunePayloads(_) => {
|
||||
prune_payloads(client_config, &context, log).map_err(format_err)
|
||||
}
|
||||
Some(("prune-blobs", _)) => prune_blobs(client_config, &context, log).map_err(format_err),
|
||||
Some(("prune-states", cli_args)) => {
|
||||
cli::DatabaseManagerSubcommand::PruneBlobs(_) => {
|
||||
prune_blobs(client_config, &context, log).map_err(format_err)
|
||||
}
|
||||
cli::DatabaseManagerSubcommand::PruneStates(prune_states_config) => {
|
||||
let executor = env.core_context().executor;
|
||||
let network_config = context
|
||||
.eth2_network_config
|
||||
@@ -722,10 +522,13 @@ pub fn run<E: EthSpec>(cli_args: &ArgMatches, env: Environment<E>) -> Result<(),
|
||||
.map_err(|e| format!("Error getting genesis state: {e}"))?
|
||||
.ok_or("Genesis state missing")?;
|
||||
|
||||
let prune_config = parse_prune_states_config(cli_args)?;
|
||||
let prune_config = parse_prune_states_config(prune_states_config)?;
|
||||
|
||||
prune_states(client_config, prune_config, genesis_state, &context, log)
|
||||
}
|
||||
_ => Err("Unknown subcommand, for help `lighthouse database_manager --help`".into()),
|
||||
cli::DatabaseManagerSubcommand::Compact(compact_config) => {
|
||||
let compact_config = parse_compact_config(compact_config)?;
|
||||
compact_db::<E>(compact_config, client_config, log).map_err(format_err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user