1+ use account_utils:: { read_input_from_user, STDIN_INPUTS_FLAG } ;
12use beacon_chain:: chain_config:: {
23 DisallowedReOrgOffsets , ReOrgThreshold , DEFAULT_PREPARE_PAYLOAD_LOOKAHEAD_FACTOR ,
34 DEFAULT_RE_ORG_HEAD_THRESHOLD , DEFAULT_RE_ORG_MAX_EPOCHS_SINCE_FINALIZATION ,
@@ -21,6 +22,7 @@ use slog::{info, warn, Logger};
2122use std:: cmp:: max;
2223use std:: fmt:: Debug ;
2324use std:: fs;
25+ use std:: io:: IsTerminal ;
2426use std:: net:: Ipv6Addr ;
2527use std:: net:: { IpAddr , Ipv4Addr , ToSocketAddrs } ;
2628use std:: num:: NonZeroU16 ;
@@ -30,6 +32,8 @@ use std::time::Duration;
3032use types:: graffiti:: GraffitiString ;
3133use types:: { Checkpoint , Epoch , EthSpec , Hash256 , PublicKeyBytes } ;
3234
35+ const PURGE_DB_CONFIRMATION : & str = "confirm" ;
36+
3337/// Gets the fully-initialized global client.
3438///
3539/// The top-level `clap` arguments should be provided as `cli_args`.
@@ -50,26 +54,45 @@ pub fn get_config<E: EthSpec>(
5054 client_config. set_data_dir ( get_data_dir ( cli_args) ) ;
5155
5256 // If necessary, remove any existing database and configuration
53- if client_config. data_dir ( ) . exists ( ) && cli_args. get_flag ( "purge-db" ) {
54- // Remove the chain_db.
55- let chain_db = client_config. get_db_path ( ) ;
56- if chain_db. exists ( ) {
57- fs:: remove_dir_all ( chain_db)
58- . map_err ( |err| format ! ( "Failed to remove chain_db: {}" , err) ) ?;
59- }
60-
61- // Remove the freezer db.
62- let freezer_db = client_config. get_freezer_db_path ( ) ;
63- if freezer_db. exists ( ) {
64- fs:: remove_dir_all ( freezer_db)
65- . map_err ( |err| format ! ( "Failed to remove freezer_db: {}" , err) ) ?;
66- }
67-
68- // Remove the blobs db.
69- let blobs_db = client_config. get_blobs_db_path ( ) ;
70- if blobs_db. exists ( ) {
71- fs:: remove_dir_all ( blobs_db)
72- . map_err ( |err| format ! ( "Failed to remove blobs_db: {}" , err) ) ?;
57+ if client_config. data_dir ( ) . exists ( ) {
58+ if cli_args. get_flag ( "purge-db-force" ) {
59+ let chain_db = client_config. get_db_path ( ) ;
60+ let freezer_db = client_config. get_freezer_db_path ( ) ;
61+ let blobs_db = client_config. get_blobs_db_path ( ) ;
62+ purge_db ( chain_db, freezer_db, blobs_db) ?;
63+ } else if cli_args. get_flag ( "purge-db" ) {
64+ let stdin_inputs = cfg ! ( windows) || cli_args. get_flag ( STDIN_INPUTS_FLAG ) ;
65+ if std:: io:: stdin ( ) . is_terminal ( ) || stdin_inputs {
66+ info ! (
67+ log,
68+ "You are about to delete the chain database. This is irreversable \
69+ and you will need to resync the chain."
70+ ) ;
71+ info ! (
72+ log,
73+ "Type 'confirm' to delete the database. Any other input will leave \
74+ the database intact and Lighthouse will exit."
75+ ) ;
76+ let confirmation = read_input_from_user ( stdin_inputs) ?;
77+
78+ if confirmation == PURGE_DB_CONFIRMATION {
79+ let chain_db = client_config. get_db_path ( ) ;
80+ let freezer_db = client_config. get_freezer_db_path ( ) ;
81+ let blobs_db = client_config. get_blobs_db_path ( ) ;
82+ purge_db ( chain_db, freezer_db, blobs_db) ?;
83+ info ! ( log, "Database was deleted." ) ;
84+ } else {
85+ info ! ( log, "Database was not deleted. Lighthouse will now close." ) ;
86+ std:: process:: exit ( 1 ) ;
87+ }
88+ } else {
89+ warn ! (
90+ log,
91+ "The `--purge-db` flag was passed, but Lighthouse is not running \
92+ interactively. The database was not purged. Use `--purge-db-force` \
93+ to purge the database without requiring confirmation."
94+ ) ;
95+ }
7396 }
7497 }
7598
@@ -1526,3 +1549,26 @@ where
15261549 . next ( )
15271550 . ok_or ( format ! ( "Must provide at least one value to {}" , flag_name) )
15281551}
1552+
1553+ /// Remove chain, freezer and blobs db.
1554+ fn purge_db ( chain_db : PathBuf , freezer_db : PathBuf , blobs_db : PathBuf ) -> Result < ( ) , String > {
1555+ // Remove the chain_db.
1556+ if chain_db. exists ( ) {
1557+ fs:: remove_dir_all ( chain_db)
1558+ . map_err ( |err| format ! ( "Failed to remove chain_db: {}" , err) ) ?;
1559+ }
1560+
1561+ // Remove the freezer db.
1562+ if freezer_db. exists ( ) {
1563+ fs:: remove_dir_all ( freezer_db)
1564+ . map_err ( |err| format ! ( "Failed to remove freezer_db: {}" , err) ) ?;
1565+ }
1566+
1567+ // Remove the blobs db.
1568+ if blobs_db. exists ( ) {
1569+ fs:: remove_dir_all ( blobs_db)
1570+ . map_err ( |err| format ! ( "Failed to remove blobs_db: {}" , err) ) ?;
1571+ }
1572+
1573+ Ok ( ( ) )
1574+ }
0 commit comments