Skip to content

Commit

Permalink
Add safe purge-db option
Browse files Browse the repository at this point in the history
  • Loading branch information
macladson committed Jan 19, 2024
1 parent 47b28c4 commit 95af228
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 77 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions account_manager/src/validator/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.conflicts_with("count")
.takes_value(true),
)
.arg(
Arg::with_name(STDIN_INPUTS_FLAG)
.takes_value(false)
.hidden(cfg!(windows))
.long(STDIN_INPUTS_FLAG)
.help("If present, read all user inputs from stdin instead of tty."),
)
}

pub fn cli_run<T: EthSpec>(
Expand Down
7 changes: 0 additions & 7 deletions account_manager/src/validator/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.long(NO_CONFIRMATION)
.help("Exits without prompting for confirmation that you understand the implications of a voluntary exit. This should be used with caution")
)
.arg(
Arg::with_name(STDIN_INPUTS_FLAG)
.takes_value(false)
.hidden(cfg!(windows))
.long(STDIN_INPUTS_FLAG)
.help("If present, read all user inputs from stdin instead of tty."),
)
}

pub fn cli_run<E: EthSpec>(matches: &ArgMatches, env: Environment<E>) -> Result<(), String> {
Expand Down
7 changes: 0 additions & 7 deletions account_manager/src/validator/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.required_unless(KEYSTORE_FLAG)
.takes_value(true),
)
.arg(
Arg::with_name(STDIN_INPUTS_FLAG)
.takes_value(false)
.hidden(cfg!(windows))
.long(STDIN_INPUTS_FLAG)
.help("If present, read all user inputs from stdin instead of tty."),
)
.arg(
Arg::with_name(REUSE_PASSWORD_FLAG)
.long(REUSE_PASSWORD_FLAG)
Expand Down
7 changes: 0 additions & 7 deletions account_manager/src/validator/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
instead generate them from the wallet seed when required.",
),
)
.arg(
Arg::with_name(STDIN_INPUTS_FLAG)
.takes_value(false)
.hidden(cfg!(windows))
.long(STDIN_INPUTS_FLAG)
.help("If present, read all user inputs from stdin instead of tty."),
)
}

pub fn cli_run(matches: &ArgMatches, validator_dir: PathBuf) -> Result<(), String> {
Expand Down
7 changes: 0 additions & 7 deletions account_manager/src/wallet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
)
.takes_value(true)
)
.arg(
Arg::with_name(STDIN_INPUTS_FLAG)
.takes_value(false)
.hidden(cfg!(windows))
.long(STDIN_INPUTS_FLAG)
.help("If present, read all user inputs from stdin instead of tty."),
)
.arg(
Arg::with_name(MNEMONIC_LENGTH_FLAG)
.long(MNEMONIC_LENGTH_FLAG)
Expand Down
7 changes: 0 additions & 7 deletions account_manager/src/wallet/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.possible_values(&[HD_TYPE])
.default_value(HD_TYPE),
)
.arg(
Arg::with_name(STDIN_INPUTS_FLAG)
.takes_value(false)
.hidden(cfg!(windows))
.long(STDIN_INPUTS_FLAG)
.help("If present, read all user inputs from stdin instead of tty."),
)
}

pub fn cli_run(matches: &ArgMatches, wallet_base_dir: PathBuf) -> Result<(), String> {
Expand Down
1 change: 1 addition & 0 deletions beacon_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ sensitive_url = { workspace = true }
http_api = { workspace = true }
unused_port = { workspace = true }
strum = { workspace = true }
account_utils = { workspace = true }
6 changes: 6 additions & 0 deletions beacon_node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,12 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.long("purge-db")
.help("If present, the chain database will be deleted. Use with caution.")
)
.arg(
Arg::with_name("purge-db-safe")
.long("purge-db-safe")
.help("The same as --purge-db but requires manual confirmation before the database \
is purged.")
)
.arg(
Arg::with_name("compact-db")
.long("compact-db")
Expand Down
73 changes: 53 additions & 20 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
@@ -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_MAX_EPOCHS_SINCE_FINALIZATION, DEFAULT_RE_ORG_THRESHOLD,
Expand Down Expand Up @@ -28,6 +29,8 @@ use std::str::FromStr;
use std::time::Duration;
use types::{Checkpoint, Epoch, EthSpec, Hash256, PublicKeyBytes, GRAFFITI_BYTES_LEN};

const PURGE_DB_CONFIRMATION: &str = "confirm";

/// Gets the fully-initialized global client.
///
/// The top-level `clap` arguments should be provided as `cli_args`.
Expand All @@ -48,26 +51,33 @@ pub fn get_config<E: EthSpec>(
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.is_present("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.is_present("purge-db") {
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.is_present("purge-db-safe") {
let stdin_inputs = cfg!(windows) || cli_args.is_present(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."
);
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.");
}
}
}

Expand Down Expand Up @@ -1427,6 +1437,29 @@ pub fn set_network_config(
Ok(())
}

/// Remove chain and freezer 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(())
}

/// Gets the datadir which should be used.
pub fn get_data_dir(cli_args: &ArgMatches) -> PathBuf {
// Read the `--datadir` flag.
Expand Down
3 changes: 3 additions & 0 deletions book/src/help_bn.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ FLAGS:
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.
--purge-db-safe The same as --purge-db but requires manual confirmation before the
database is purged.
--reconstruct-historic-states After a checkpoint sync, reconstruct historic states in the database.
This requires syncing all the way back to genesis.
--reset-payload-statuses When present, Lighthouse will forget the payload statuses of any already-
Expand All @@ -115,6 +117,7 @@ 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 subnets.
--validator-monitor-auto Enables the automatic detection and monitoring of validators connected to
Expand Down
1 change: 1 addition & 0 deletions book/src/help_general.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ FLAGS:
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.
-V, --version Prints version information
OPTIONS:
Expand Down
1 change: 1 addition & 0 deletions book/src/help_vc.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ FLAGS:
Enable block production via the block v3 endpoint for this validator client. This should only be enabled
when paired with a beacon node that has this endpoint implemented. This flag will be enabled by default in
future.
--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.
Expand Down
1 change: 1 addition & 0 deletions book/src/help_vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ FLAGS:
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.
-V, --version Prints version information
OPTIONS:
Expand Down
1 change: 1 addition & 0 deletions book/src/help_vm_import.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ FLAGS:
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.
-V, --version Prints version information
OPTIONS:
Expand Down
2 changes: 2 additions & 0 deletions common/account_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path>>(wallet_name: &str, secrets_dir: P) -> PathBuf {
secrets_dir.as_ref().join(format!("{}.pass", wallet_name))
Expand Down
9 changes: 9 additions & 0 deletions lighthouse/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod metrics;

use account_utils::STDIN_INPUTS_FLAG;
use beacon_node::ProductionBeaconNode;
use clap::{App, Arg, ArgMatches};
use clap_utils::{flags::DISABLE_MALLOC_TUNING_FLAG, get_eth2_network_config};
Expand Down Expand Up @@ -80,6 +81,14 @@ fn main() {
cfg!(feature = "gnosis"),
).as_str()
)
.arg(
Arg::with_name(STDIN_INPUTS_FLAG)
.takes_value(false)
.hidden(cfg!(windows))
.global(true)
.long(STDIN_INPUTS_FLAG)
.help("If present, read all user inputs from stdin instead of tty."),
)
.arg(
Arg::with_name("env_log")
.short("l")
Expand Down
2 changes: 1 addition & 1 deletion validator_manager/src/common.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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
Expand Down
7 changes: 0 additions & 7 deletions validator_manager/src/create_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.help("If present, the mnemonic will be read in from this file.")
.takes_value(true),
)
.arg(
Arg::with_name(STDIN_INPUTS_FLAG)
.takes_value(false)
.hidden(cfg!(windows))
.long(STDIN_INPUTS_FLAG)
.help("If present, read all user inputs from stdin instead of tty."),
)
.arg(
Arg::with_name(DISABLE_DEPOSITS_FLAG)
.long(DISABLE_DEPOSITS_FLAG)
Expand Down
7 changes: 0 additions & 7 deletions validator_manager/src/move_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.possible_values(&["true", "false"])
.takes_value(true),
)
.arg(
Arg::with_name(STDIN_INPUTS_FLAG)
.takes_value(false)
.hidden(cfg!(windows))
.long(STDIN_INPUTS_FLAG)
.help("If present, read all user inputs from stdin instead of tty."),
)
}

#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
Expand Down

0 comments on commit 95af228

Please sign in to comment.