From d403350430cfecb84e1c4c307867789fcc14c336 Mon Sep 17 00:00:00 2001 From: Mac L Date: Wed, 24 Jul 2024 01:46:01 +1000 Subject: [PATCH 1/7] Require manual confirmation to purge database --- Cargo.lock | 1 + account_manager/src/validator/create.rs | 12 +--- account_manager/src/validator/exit.rs | 11 +--- account_manager/src/validator/import.rs | 13 +--- account_manager/src/validator/recover.rs | 12 +--- account_manager/src/wallet/create.rs | 10 +-- account_manager/src/wallet/recover.rs | 12 +--- beacon_node/Cargo.toml | 1 + beacon_node/src/cli.rs | 10 ++- beacon_node/src/config.rs | 74 ++++++++++++++++------ book/src/help_bn.md | 8 ++- book/src/help_general.md | 2 + book/src/help_vc.md | 2 + book/src/help_vm.md | 2 + book/src/help_vm_import.md | 2 + book/src/help_vm_move.md | 4 +- common/account_utils/src/lib.rs | 2 + lighthouse/src/main.rs | 11 ++++ validator_manager/src/common.rs | 2 +- validator_manager/src/create_validators.rs | 9 --- validator_manager/src/move_validators.rs | 8 --- 21 files changed, 104 insertions(+), 104 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 605cb4d2a58..d9779a23623 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -838,6 +838,7 @@ dependencies = [ name = "beacon_node" version = "5.2.1" dependencies = [ + "account_utils", "beacon_chain", "clap", "clap_utils", diff --git a/account_manager/src/validator/create.rs b/account_manager/src/validator/create.rs index cfe4d8e94ad..ec5af1e2ece 100644 --- a/account_manager/src/validator/create.rs +++ b/account_manager/src/validator/create.rs @@ -1,8 +1,8 @@ use crate::common::read_wallet_name_from_cli; -use crate::wallet::create::STDIN_INPUTS_FLAG; use crate::{SECRETS_DIR_FLAG, WALLETS_DIR_FLAG}; use account_utils::{ random_password, read_password_from_user, strip_off_newlines, validator_definitions, PlainText, + STDIN_INPUTS_FLAG, }; use clap::{Arg, ArgAction, ArgMatches, Command}; use clap_utils::FLAG_HEADER; @@ -114,16 +114,6 @@ pub fn cli_app() -> Command { .action(ArgAction::Set) .display_order(0) ) - .arg( - Arg::new(STDIN_INPUTS_FLAG) - .action(ArgAction::SetTrue) - .help_heading(FLAG_HEADER) - .hide(cfg!(windows)) - .long(STDIN_INPUTS_FLAG) - .help("If present, read all user inputs from stdin instead of tty.") - .display_order(0) - .action(ArgAction::SetTrue) - ) } pub fn cli_run( diff --git a/account_manager/src/validator/exit.rs b/account_manager/src/validator/exit.rs index 277d2ae8eca..3fb0e50d225 100644 --- a/account_manager/src/validator/exit.rs +++ b/account_manager/src/validator/exit.rs @@ -1,4 +1,4 @@ -use crate::wallet::create::STDIN_INPUTS_FLAG; +use account_utils::STDIN_INPUTS_FLAG; use bls::{Keypair, PublicKey}; use clap::{Arg, ArgAction, ArgMatches, Command}; use clap_utils::FLAG_HEADER; @@ -74,15 +74,6 @@ pub fn cli_app() -> Command { .action(ArgAction::SetTrue) .help_heading(FLAG_HEADER) ) - .arg( - Arg::new(STDIN_INPUTS_FLAG) - .action(ArgAction::SetTrue) - .help_heading(FLAG_HEADER) - .hide(cfg!(windows)) - .long(STDIN_INPUTS_FLAG) - .help("If present, read all user inputs from stdin instead of tty.") - .display_order(0) - ) } pub fn cli_run(matches: &ArgMatches, env: Environment) -> Result<(), String> { diff --git a/account_manager/src/validator/import.rs b/account_manager/src/validator/import.rs index a7c72679f74..0783cff3ef5 100644 --- a/account_manager/src/validator/import.rs +++ b/account_manager/src/validator/import.rs @@ -1,4 +1,4 @@ -use crate::wallet::create::{PASSWORD_FLAG, STDIN_INPUTS_FLAG}; +use crate::wallet::create::PASSWORD_FLAG; use account_utils::validator_definitions::SigningDefinition; use account_utils::{ eth2_keystore::Keystore, @@ -7,7 +7,7 @@ use account_utils::{ recursively_find_voting_keystores, PasswordStorage, ValidatorDefinition, ValidatorDefinitions, CONFIG_FILENAME, }, - ZeroizeString, + ZeroizeString, STDIN_INPUTS_FLAG, }; use clap::{Arg, ArgAction, ArgMatches, Command}; use clap_utils::FLAG_HEADER; @@ -59,15 +59,6 @@ pub fn cli_app() -> Command { .action(ArgAction::Set) .display_order(0), ) - .arg( - Arg::new(STDIN_INPUTS_FLAG) - .action(ArgAction::SetTrue) - .help_heading(FLAG_HEADER) - .hide(cfg!(windows)) - .long(STDIN_INPUTS_FLAG) - .help("If present, read all user inputs from stdin instead of tty.") - .display_order(0), - ) .arg( Arg::new(REUSE_PASSWORD_FLAG) .long(REUSE_PASSWORD_FLAG) diff --git a/account_manager/src/validator/recover.rs b/account_manager/src/validator/recover.rs index b36b10ab008..ddf754edac9 100644 --- a/account_manager/src/validator/recover.rs +++ b/account_manager/src/validator/recover.rs @@ -1,9 +1,8 @@ use super::create::STORE_WITHDRAW_FLAG; use crate::validator::create::COUNT_FLAG; -use crate::wallet::create::STDIN_INPUTS_FLAG; use crate::SECRETS_DIR_FLAG; use account_utils::eth2_keystore::{keypair_from_secret, Keystore, KeystoreBuilder}; -use account_utils::{random_password, read_mnemonic_from_cli}; +use account_utils::{random_password, read_mnemonic_from_cli, STDIN_INPUTS_FLAG}; use clap::{Arg, ArgAction, ArgMatches, Command}; use clap_utils::FLAG_HEADER; use directory::ensure_dir_exists; @@ -76,15 +75,6 @@ pub fn cli_app() -> Command { .help_heading(FLAG_HEADER) .display_order(0) ) - .arg( - Arg::new(STDIN_INPUTS_FLAG) - .action(ArgAction::SetTrue) - .help_heading(FLAG_HEADER) - .hide(cfg!(windows)) - .long(STDIN_INPUTS_FLAG) - .help("If present, read all user inputs from stdin instead of tty.") - .display_order(0) - ) } pub fn cli_run(matches: &ArgMatches, validator_dir: PathBuf) -> Result<(), String> { diff --git a/account_manager/src/wallet/create.rs b/account_manager/src/wallet/create.rs index 12aa5d3801a..b22007050fd 100644 --- a/account_manager/src/wallet/create.rs +++ b/account_manager/src/wallet/create.rs @@ -2,6 +2,7 @@ use crate::common::read_wallet_name_from_cli; use crate::WALLETS_DIR_FLAG; use account_utils::{ is_password_sufficiently_complex, random_password, read_password_from_user, strip_off_newlines, + STDIN_INPUTS_FLAG, }; use clap::{Arg, ArgAction, ArgMatches, Command}; use eth2_wallet::{ @@ -20,7 +21,6 @@ pub const NAME_FLAG: &str = "name"; pub const PASSWORD_FLAG: &str = "password-file"; pub const TYPE_FLAG: &str = "type"; pub const MNEMONIC_FLAG: &str = "mnemonic-output-path"; -pub const STDIN_INPUTS_FLAG: &str = "stdin-inputs"; pub const MNEMONIC_LENGTH_FLAG: &str = "mnemonic-length"; pub const MNEMONIC_TYPES: &[MnemonicType] = &[ MnemonicType::Words12, @@ -83,14 +83,6 @@ pub fn cli_app() -> Command { .action(ArgAction::Set) .display_order(0) ) - .arg( - Arg::new(STDIN_INPUTS_FLAG) - .action(ArgAction::SetTrue) - .hide(cfg!(windows)) - .long(STDIN_INPUTS_FLAG) - .help("If present, read all user inputs from stdin instead of tty.") - .display_order(0) - ) .arg( Arg::new(MNEMONIC_LENGTH_FLAG) .long(MNEMONIC_LENGTH_FLAG) diff --git a/account_manager/src/wallet/recover.rs b/account_manager/src/wallet/recover.rs index b9641f11521..766d5dbe0cb 100644 --- a/account_manager/src/wallet/recover.rs +++ b/account_manager/src/wallet/recover.rs @@ -1,6 +1,6 @@ -use crate::wallet::create::{create_wallet_from_mnemonic, STDIN_INPUTS_FLAG}; +use crate::wallet::create::create_wallet_from_mnemonic; use crate::wallet::create::{HD_TYPE, NAME_FLAG, PASSWORD_FLAG, TYPE_FLAG}; -use account_utils::read_mnemonic_from_cli; +use account_utils::{read_mnemonic_from_cli, STDIN_INPUTS_FLAG}; use clap::{Arg, ArgAction, ArgMatches, Command}; use std::path::PathBuf; @@ -56,14 +56,6 @@ pub fn cli_app() -> Command { .default_value(HD_TYPE) .display_order(0), ) - .arg( - Arg::new(STDIN_INPUTS_FLAG) - .action(ArgAction::SetTrue) - .hide(cfg!(windows)) - .long(STDIN_INPUTS_FLAG) - .help("If present, read all user inputs from stdin instead of tty.") - .display_order(0), - ) } pub fn cli_run(matches: &ArgMatches, wallet_base_dir: PathBuf) -> Result<(), String> { diff --git a/beacon_node/Cargo.toml b/beacon_node/Cargo.toml index a5fd29c971f..a97f1b5ce37 100644 --- a/beacon_node/Cargo.toml +++ b/beacon_node/Cargo.toml @@ -44,3 +44,4 @@ sensitive_url = { workspace = true } http_api = { workspace = true } unused_port = { workspace = true } strum = { workspace = true } +account_utils = { workspace = true } diff --git a/beacon_node/src/cli.rs b/beacon_node/src/cli.rs index 2e1b1c093c8..0ace87e9cdd 100644 --- a/beacon_node/src/cli.rs +++ b/beacon_node/src/cli.rs @@ -907,7 +907,15 @@ pub fn cli_app() -> Command { .long("purge-db") .action(ArgAction::SetTrue) .help_heading(FLAG_HEADER) - .help("If present, the chain database will be deleted. Use with caution.") + .help("If present, the chain database will be deleted. Requires manual confirmation.") + .display_order(0) + ) + .arg( + Arg::new("purge-db-auto") + .long("purge-db-auto") + .action(ArgAction::SetTrue) + .help_heading(FLAG_HEADER) + .help("If present, the chain database will be deleted without confirmation. Use with caution.") .display_order(0) ) .arg( diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index b4fa38da7d7..a7353d95454 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -1,3 +1,4 @@ +use account_utils::{read_input_from_user, STDIN_INPUTS_FLAG}; use beacon_chain::chain_config::{ DisallowedReOrgOffsets, ReOrgThreshold, DEFAULT_PREPARE_PAYLOAD_LOOKAHEAD_FACTOR, DEFAULT_RE_ORG_HEAD_THRESHOLD, DEFAULT_RE_ORG_MAX_EPOCHS_SINCE_FINALIZATION, @@ -30,6 +31,8 @@ use std::time::Duration; use types::graffiti::GraffitiString; use types::{Checkpoint, Epoch, EthSpec, Hash256, PublicKeyBytes}; +const PURGE_DB_CONFIRMATION: &str = "confirm"; + /// Gets the fully-initialized global client. /// /// The top-level `clap` arguments should be provided as `cli_args`. @@ -50,26 +53,34 @@ pub fn get_config( client_config.set_data_dir(get_data_dir(cli_args)); // If necessary, remove any existing database and configuration - if client_config.data_dir().exists() && cli_args.get_flag("purge-db") { - // Remove the chain_db. - let chain_db = client_config.get_db_path(); - if chain_db.exists() { - fs::remove_dir_all(chain_db) - .map_err(|err| format!("Failed to remove chain_db: {}", err))?; - } - - // Remove the freezer db. - let freezer_db = client_config.get_freezer_db_path(); - if freezer_db.exists() { - fs::remove_dir_all(freezer_db) - .map_err(|err| format!("Failed to remove freezer_db: {}", err))?; - } - - // Remove the blobs db. - let blobs_db = client_config.get_blobs_db_path(); - if blobs_db.exists() { - fs::remove_dir_all(blobs_db) - .map_err(|err| format!("Failed to remove blobs_db: {}", err))?; + if client_config.data_dir().exists() { + if cli_args.get_flag("purge-db-auto") { + let chain_db = client_config.get_db_path(); + let freezer_db = client_config.get_freezer_db_path(); + let blobs_db = client_config.get_blobs_db_path(); + purge_db(chain_db, freezer_db, blobs_db)?; + } else if cli_args.get_flag("purge-db") { + let stdin_inputs = cfg!(windows) || cli_args.get_flag(STDIN_INPUTS_FLAG); + eprintln!( + "You are about to delete the chain database. This is irreversable \ + and you will need to resync the chain." + ); + eprintln!( + "Type 'confirm' to delete the database. Any other input will leave \ + the database intact and Lighthouse will exit." + ); + let confirmation = read_input_from_user(stdin_inputs)?; + + if confirmation == PURGE_DB_CONFIRMATION { + eprintln!("Database was deleted."); + let chain_db = client_config.get_db_path(); + let freezer_db = client_config.get_freezer_db_path(); + let blobs_db = client_config.get_blobs_db_path(); + purge_db(chain_db, freezer_db, blobs_db)?; + } else { + eprintln!("Database was not deleted. Lighthouse will now close."); + std::process::exit(1); + } } } @@ -1522,3 +1533,26 @@ where .next() .ok_or(format!("Must provide at least one value to {}", flag_name)) } + +/// Remove chain, freezer and blobs db. +fn purge_db(chain_db: PathBuf, freezer_db: PathBuf, blobs_db: PathBuf) -> Result<(), String> { + // Remove the chain_db. + if chain_db.exists() { + fs::remove_dir_all(chain_db) + .map_err(|err| format!("Failed to remove chain_db: {}", err))?; + } + + // Remove the freezer db. + if freezer_db.exists() { + fs::remove_dir_all(freezer_db) + .map_err(|err| format!("Failed to remove freezer_db: {}", err))?; + } + + // Remove the blobs db. + if blobs_db.exists() { + fs::remove_dir_all(blobs_db) + .map_err(|err| format!("Failed to remove blobs_db: {}", err))?; + } + + Ok(()) +} diff --git a/book/src/help_bn.md b/book/src/help_bn.md index f9180b65832..52d25161688 100644 --- a/book/src/help_bn.md +++ b/book/src/help_bn.md @@ -565,7 +565,11 @@ Flags: being referenced by validator client using the --proposer-node flag. This configuration is for enabling more secure setups. --purge-db - If present, the chain database will be deleted. Use with caution. + If present, the chain database will be deleted. Requires manual + confirmation. + --purge-db-auto + If present, the chain database will be deleted without confirmation. + Use with caution. --reconstruct-historic-states After a checkpoint sync, reconstruct historic states in the database. This requires syncing all the way back to genesis. @@ -585,6 +589,8 @@ Flags: server on localhost:5052 and import deposit logs from the execution node. This is equivalent to `--http` on merge-ready networks, or `--http --eth1` pre-merge + --stdin-inputs + If present, read all user inputs from stdin instead of tty. --subscribe-all-subnets Subscribe to all subnets regardless of validator count. This will also advertise the beacon node as being long-lived subscribed to all diff --git a/book/src/help_general.md b/book/src/help_general.md index 84bc67a86e2..1c2d1266d08 100644 --- a/book/src/help_general.md +++ b/book/src/help_general.md @@ -136,6 +136,8 @@ Flags: contain sensitive information about your validator and so this flag should be used with caution. For Windows users, the log file permissions will be inherited from the parent folder. + --stdin-inputs + If present, read all user inputs from stdin instead of tty. ``` diff --git a/book/src/help_vc.md b/book/src/help_vc.md index e205f4c3454..7f2cfab8e3a 100644 --- a/book/src/help_vc.md +++ b/book/src/help_vc.md @@ -266,6 +266,8 @@ Flags: by builders, regardless of payload value. --produce-block-v3 This flag is deprecated and is no longer in use. + --stdin-inputs + If present, read all user inputs from stdin instead of tty. --unencrypted-http-transport This is a safety flag to ensure that the user is aware that the http transport is unencrypted and using a custom HTTP address is unsafe. diff --git a/book/src/help_vm.md b/book/src/help_vm.md index 99a45c1a76d..f787985b215 100644 --- a/book/src/help_vm.md +++ b/book/src/help_vm.md @@ -123,6 +123,8 @@ Flags: contain sensitive information about your validator and so this flag should be used with caution. For Windows users, the log file permissions will be inherited from the parent folder. + --stdin-inputs + If present, read all user inputs from stdin instead of tty. ``` diff --git a/book/src/help_vm_import.md b/book/src/help_vm_import.md index e18aad79589..0883139ad21 100644 --- a/book/src/help_vm_import.md +++ b/book/src/help_vm_import.md @@ -121,6 +121,8 @@ Flags: contain sensitive information about your validator and so this flag should be used with caution. For Windows users, the log file permissions will be inherited from the parent folder. + --stdin-inputs + If present, read all user inputs from stdin instead of tty. ``` diff --git a/book/src/help_vm_move.md b/book/src/help_vm_move.md index faef0a5783d..12dd1e91402 100644 --- a/book/src/help_vm_move.md +++ b/book/src/help_vm_move.md @@ -88,8 +88,6 @@ Options: A HTTP(S) address of a validator client using the keymanager-API. This validator client is the "source" and contains the validators that are to be moved. - --stdin-inputs - If present, read all user inputs from stdin instead of tty. --suggested-fee-recipient All created validators will use this value for the suggested fee recipient. Omit this flag to use the default value from the VC. @@ -142,6 +140,8 @@ Flags: contain sensitive information about your validator and so this flag should be used with caution. For Windows users, the log file permissions will be inherited from the parent folder. + --stdin-inputs + If present, read all user inputs from stdin instead of tty. ``` diff --git a/common/account_utils/src/lib.rs b/common/account_utils/src/lib.rs index 8707ae531f7..665953fa522 100644 --- a/common/account_utils/src/lib.rs +++ b/common/account_utils/src/lib.rs @@ -35,6 +35,8 @@ const DEFAULT_PASSWORD_LEN: usize = 48; pub const MNEMONIC_PROMPT: &str = "Enter the mnemonic phrase:"; +pub const STDIN_INPUTS_FLAG: &str = "stdin-inputs"; + /// Returns the "default" path where a wallet should store its password file. pub fn default_wallet_password_path>(wallet_name: &str, secrets_dir: P) -> PathBuf { secrets_dir.as_ref().join(format!("{}.pass", wallet_name)) diff --git a/lighthouse/src/main.rs b/lighthouse/src/main.rs index 481e17dbc80..bc0424000e6 100644 --- a/lighthouse/src/main.rs +++ b/lighthouse/src/main.rs @@ -1,6 +1,7 @@ mod cli; mod metrics; +use account_utils::STDIN_INPUTS_FLAG; use beacon_node::ProductionBeaconNode; use clap::FromArgMatches; use clap::Subcommand; @@ -104,6 +105,16 @@ fn main() { ) .long_version(LONG_VERSION.as_str()) .display_order(0) + .arg( + Arg::new(STDIN_INPUTS_FLAG) + .long(STDIN_INPUTS_FLAG) + .action(ArgAction::SetTrue) + .help("If present, read all user inputs from stdin instead of tty.") + .help_heading(FLAG_HEADER) + .hide(cfg!(windows)) + .global(true) + .display_order(0), + ) .arg( Arg::new("env_log") .short('l') diff --git a/validator_manager/src/common.rs b/validator_manager/src/common.rs index 871c5362030..4a35791b322 100644 --- a/validator_manager/src/common.rs +++ b/validator_manager/src/common.rs @@ -1,3 +1,4 @@ +pub use account_utils::STDIN_INPUTS_FLAG; use account_utils::{strip_off_newlines, ZeroizeString}; use eth2::lighthouse_vc::std_types::{InterchangeJsonStr, KeystoreJsonStr}; use eth2::{ @@ -15,7 +16,6 @@ use tree_hash::TreeHash; use types::*; pub const IGNORE_DUPLICATES_FLAG: &str = "ignore-duplicates"; -pub const STDIN_INPUTS_FLAG: &str = "stdin-inputs"; pub const COUNT_FLAG: &str = "count"; /// When the `ethereum/staking-deposit-cli` tool generates deposit data JSON, it adds a diff --git a/validator_manager/src/create_validators.rs b/validator_manager/src/create_validators.rs index d53e92deb30..8946303f4e1 100644 --- a/validator_manager/src/create_validators.rs +++ b/validator_manager/src/create_validators.rs @@ -105,15 +105,6 @@ pub fn cli_app() -> Command { .action(ArgAction::Set) .display_order(0), ) - .arg( - Arg::new(STDIN_INPUTS_FLAG) - .action(ArgAction::SetTrue) - .hide(cfg!(windows)) - .long(STDIN_INPUTS_FLAG) - .help("If present, read all user inputs from stdin instead of tty.") - .display_order(0) - .help_heading(FLAG_HEADER), - ) .arg( Arg::new(DISABLE_DEPOSITS_FLAG) .long(DISABLE_DEPOSITS_FLAG) diff --git a/validator_manager/src/move_validators.rs b/validator_manager/src/move_validators.rs index d2149d742c1..91bc2b0ef85 100644 --- a/validator_manager/src/move_validators.rs +++ b/validator_manager/src/move_validators.rs @@ -184,14 +184,6 @@ pub fn cli_app() -> Command { .action(ArgAction::Set) .display_order(0), ) - .arg( - Arg::new(STDIN_INPUTS_FLAG) - .action(ArgAction::SetTrue) - .hide(cfg!(windows)) - .long(STDIN_INPUTS_FLAG) - .help("If present, read all user inputs from stdin instead of tty.") - .display_order(0), - ) .arg( Arg::new(BUILDER_BOOST_FACTOR_FLAG) .long(BUILDER_BOOST_FACTOR_FLAG) From 21735141e8a1e83512d9ba5166219c78d1f2230b Mon Sep 17 00:00:00 2001 From: Mac L Date: Wed, 24 Jul 2024 02:14:04 +1000 Subject: [PATCH 2/7] Fix tests --- lighthouse/tests/account_manager.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighthouse/tests/account_manager.rs b/lighthouse/tests/account_manager.rs index f82e3ec713b..4d155937140 100644 --- a/lighthouse/tests/account_manager.rs +++ b/lighthouse/tests/account_manager.rs @@ -15,7 +15,7 @@ use account_manager::{ use account_utils::{ eth2_keystore::KeystoreBuilder, validator_definitions::{SigningDefinition, ValidatorDefinition, ValidatorDefinitions}, - ZeroizeString, + ZeroizeString, STDIN_INPUTS_FLAG, }; use slashing_protection::{SlashingDatabase, SLASHING_PROTECTION_FILENAME}; use std::env; From 96bab77e194b0ffe02ff469ba919e25096539019 Mon Sep 17 00:00:00 2001 From: Mac L Date: Mon, 29 Jul 2024 02:40:10 +1000 Subject: [PATCH 3/7] Rename to `purge-db-force and skip in non-interactive mode --- beacon_node/src/cli.rs | 4 +-- beacon_node/src/config.rs | 51 ++++++++++++++++++++++++--------------- book/src/help_bn.md | 2 +- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/beacon_node/src/cli.rs b/beacon_node/src/cli.rs index 0ace87e9cdd..c4a59f7c638 100644 --- a/beacon_node/src/cli.rs +++ b/beacon_node/src/cli.rs @@ -911,8 +911,8 @@ pub fn cli_app() -> Command { .display_order(0) ) .arg( - Arg::new("purge-db-auto") - .long("purge-db-auto") + Arg::new("purge-db-force") + .long("purge-db-force") .action(ArgAction::SetTrue) .help_heading(FLAG_HEADER) .help("If present, the chain database will be deleted without confirmation. Use with caution.") diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index a7353d95454..fadb5ac9070 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -22,6 +22,7 @@ use slog::{info, warn, Logger}; use std::cmp::max; use std::fmt::Debug; use std::fs; +use std::io::IsTerminal; use std::net::Ipv6Addr; use std::net::{IpAddr, Ipv4Addr, ToSocketAddrs}; use std::num::NonZeroU16; @@ -54,32 +55,42 @@ pub fn get_config( // If necessary, remove any existing database and configuration if client_config.data_dir().exists() { - if cli_args.get_flag("purge-db-auto") { + if cli_args.get_flag("purge-db-force") { let chain_db = client_config.get_db_path(); let freezer_db = client_config.get_freezer_db_path(); let blobs_db = client_config.get_blobs_db_path(); purge_db(chain_db, freezer_db, blobs_db)?; } else if cli_args.get_flag("purge-db") { - let stdin_inputs = cfg!(windows) || cli_args.get_flag(STDIN_INPUTS_FLAG); - eprintln!( - "You are about to delete the chain database. This is irreversable \ - and you will need to resync the chain." - ); - eprintln!( - "Type 'confirm' to delete the database. Any other input will leave \ - the database intact and Lighthouse will exit." - ); - let confirmation = read_input_from_user(stdin_inputs)?; - - if confirmation == PURGE_DB_CONFIRMATION { - eprintln!("Database was deleted."); - let chain_db = client_config.get_db_path(); - let freezer_db = client_config.get_freezer_db_path(); - let blobs_db = client_config.get_blobs_db_path(); - purge_db(chain_db, freezer_db, blobs_db)?; + if std::io::stdin().is_terminal() { + let stdin_inputs = cfg!(windows) || cli_args.get_flag(STDIN_INPUTS_FLAG); + eprintln!( + "You are about to delete the chain database. This is irreversable \ + and you will need to resync the chain." + ); + eprintln!( + "Type 'confirm' to delete the database. Any other input will leave \ + the database intact and Lighthouse will exit." + ); + let confirmation = read_input_from_user(stdin_inputs)?; + + if confirmation == PURGE_DB_CONFIRMATION { + eprintln!("Database was deleted."); + let chain_db = client_config.get_db_path(); + let freezer_db = client_config.get_freezer_db_path(); + let blobs_db = client_config.get_blobs_db_path(); + purge_db(chain_db, freezer_db, blobs_db)?; + } else { + eprintln!("Database was not deleted. Lighthouse will now close."); + std::process::exit(1); + } } else { - eprintln!("Database was not deleted. Lighthouse will now close."); - std::process::exit(1); + warn!( + log, + "The `--purge-db` flag was passed, but Lighthouse is not running \ + interactively. The database was not purged. Use `--purge-db-force` \ + or `lighthouse db purge` to purge the database without requiring \ + confirmation." + ); } } } diff --git a/book/src/help_bn.md b/book/src/help_bn.md index 52d25161688..733446e5d27 100644 --- a/book/src/help_bn.md +++ b/book/src/help_bn.md @@ -567,7 +567,7 @@ Flags: --purge-db If present, the chain database will be deleted. Requires manual confirmation. - --purge-db-auto + --purge-db-force If present, the chain database will be deleted without confirmation. Use with caution. --reconstruct-historic-states From c2f1146d5b17292a3da88c21b303a990e16398c6 Mon Sep 17 00:00:00 2001 From: Mac L Date: Mon, 29 Jul 2024 19:12:06 +1000 Subject: [PATCH 4/7] Do not skip when stdin_inputs is true --- beacon_node/src/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index fadb5ac9070..aa62ffde6f0 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -61,8 +61,8 @@ pub fn get_config( let blobs_db = client_config.get_blobs_db_path(); purge_db(chain_db, freezer_db, blobs_db)?; } else if cli_args.get_flag("purge-db") { - if std::io::stdin().is_terminal() { - let stdin_inputs = cfg!(windows) || cli_args.get_flag(STDIN_INPUTS_FLAG); + let stdin_inputs = cfg!(windows) || cli_args.get_flag(STDIN_INPUTS_FLAG); + if std::io::stdin().is_terminal() || stdin_inputs { eprintln!( "You are about to delete the chain database. This is irreversable \ and you will need to resync the chain." From 206252cd055560a132bfd648be3503c0f6a157cf Mon Sep 17 00:00:00 2001 From: Mac L Date: Mon, 29 Jul 2024 19:26:18 +1000 Subject: [PATCH 5/7] Change prompt to be info logging to ensure consistent output --- beacon_node/src/config.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index aa62ffde6f0..c2b1af637d2 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -63,24 +63,26 @@ pub fn get_config( } else if cli_args.get_flag("purge-db") { let stdin_inputs = cfg!(windows) || cli_args.get_flag(STDIN_INPUTS_FLAG); if std::io::stdin().is_terminal() || stdin_inputs { - eprintln!( + info!( + log, "You are about to delete the chain database. This is irreversable \ and you will need to resync the chain." ); - eprintln!( + info!( + log, "Type 'confirm' to delete the database. Any other input will leave \ the database intact and Lighthouse will exit." ); let confirmation = read_input_from_user(stdin_inputs)?; if confirmation == PURGE_DB_CONFIRMATION { - eprintln!("Database was deleted."); + info!(log, "Database was deleted."); let chain_db = client_config.get_db_path(); let freezer_db = client_config.get_freezer_db_path(); let blobs_db = client_config.get_blobs_db_path(); purge_db(chain_db, freezer_db, blobs_db)?; } else { - eprintln!("Database was not deleted. Lighthouse will now close."); + info!(log, "Database was not deleted. Lighthouse will now close."); std::process::exit(1); } } else { From b2f33d000293a3659c70a1aba358f132c8b849cc Mon Sep 17 00:00:00 2001 From: Mac L Date: Mon, 19 Aug 2024 18:06:32 +1000 Subject: [PATCH 6/7] Update warning text --- beacon_node/src/config.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index c2b1af637d2..ef85bb4ca48 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -90,8 +90,7 @@ pub fn get_config( log, "The `--purge-db` flag was passed, but Lighthouse is not running \ interactively. The database was not purged. Use `--purge-db-force` \ - or `lighthouse db purge` to purge the database without requiring \ - confirmation." + to purge the database without requiring confirmation." ); } } From 73aab4022cc04f794e140c7100116763e39efb10 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 20 Aug 2024 11:18:01 +1000 Subject: [PATCH 7/7] Move delete log after deletion --- beacon_node/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index ef85bb4ca48..1500fd88145 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -76,11 +76,11 @@ pub fn get_config( let confirmation = read_input_from_user(stdin_inputs)?; if confirmation == PURGE_DB_CONFIRMATION { - info!(log, "Database was deleted."); let chain_db = client_config.get_db_path(); let freezer_db = client_config.get_freezer_db_path(); let blobs_db = client_config.get_blobs_db_path(); purge_db(chain_db, freezer_db, blobs_db)?; + info!(log, "Database was deleted."); } else { info!(log, "Database was not deleted. Lighthouse will now close."); std::process::exit(1);