1+ use beacon_chain:: builder:: PUBKEY_CACHE_FILENAME ;
12use network:: NetworkConfig ;
23use serde_derive:: { Deserialize , Serialize } ;
34use std:: fs;
@@ -11,6 +12,9 @@ const TESTNET_SPEC_CONSTANTS: &str = "minimal";
1112/// Default directory name for the freezer database under the top-level data dir.
1213const DEFAULT_FREEZER_DB_DIR : & str = "freezer_db" ;
1314
15+ /// Trap file indicating if chain_db was purged
16+ const CHAIN_DB_PURGED_TRAP_FILE : & str = ".db_purged" ;
17+
1418/// Defines how the client should initialize the `BeaconChain` and other components.
1519#[ derive( PartialEq , Debug , Clone , Serialize , Deserialize ) ]
1620pub enum ClientGenesis {
@@ -97,6 +101,68 @@ impl Config {
97101 . map ( |data_dir| data_dir. join ( & self . db_name ) )
98102 }
99103
104+ /// Get the path of the chain db purged trap file
105+ pub fn get_db_purged_trap_file_path ( & self ) -> Option < PathBuf > {
106+ self . get_data_dir ( )
107+ . map ( |data_dir| data_dir. join ( CHAIN_DB_PURGED_TRAP_FILE ) )
108+ }
109+
110+ /// returns whether chain_db was recently purged
111+ pub fn chain_db_was_purged ( & self ) -> bool {
112+ self . get_db_purged_trap_file_path ( )
113+ . map_or ( false , |trap_file| trap_file. exists ( ) )
114+ }
115+
116+ /// purges the chain_db and creates trap file
117+ pub fn purge_chain_db ( & self ) -> Result < ( ) , String > {
118+ // create the trap file
119+ let trap_file = self
120+ . get_db_purged_trap_file_path ( )
121+ . ok_or ( "Failed to get trap file path" . to_string ( ) ) ?;
122+ fs:: File :: create ( trap_file)
123+ . map_err ( |err| format ! ( "Failed to create trap file: {}" , err) ) ?;
124+
125+ // remove the chain_db
126+ fs:: remove_dir_all (
127+ self . get_db_path ( )
128+ . ok_or ( "Failed to get db_path" . to_string ( ) ) ?,
129+ )
130+ . map_err ( |err| format ! ( "Failed to remove chain_db: {}" , err) ) ?;
131+
132+ // remove the freezer db
133+ fs:: remove_dir_all (
134+ self . get_freezer_db_path ( )
135+ . ok_or ( "Failed to get freezer db path" . to_string ( ) ) ?,
136+ )
137+ . map_err ( |err| format ! ( "Failed to remove chain_db: {}" , err) ) ?;
138+
139+ // also need to remove pubkey cache file if it exists
140+ let pubkey_cache_file = self
141+ . get_data_dir ( )
142+ . map ( |data_dir| data_dir. join ( PUBKEY_CACHE_FILENAME ) )
143+ . ok_or ( "Failed to get pubkey cache file path" . to_string ( ) ) ?;
144+ if !pubkey_cache_file. exists ( ) {
145+ return Ok ( ( ) ) ;
146+ }
147+ fs:: remove_file ( pubkey_cache_file)
148+ . map_err ( |err| format ! ( "Failed to remove pubkey cache: {}" , err) ) ?;
149+
150+ Ok ( ( ) )
151+ }
152+
153+ /// cleans up purge_db trap file
154+ pub fn cleanup_after_purge_db ( & self ) -> Result < ( ) , String > {
155+ let trap_file = self
156+ . get_db_purged_trap_file_path ( )
157+ . ok_or ( "Failed to get trap file path" . to_string ( ) ) ?;
158+ if !trap_file. exists ( ) {
159+ return Ok ( ( ) ) ;
160+ }
161+ fs:: remove_file ( trap_file) . map_err ( |err| format ! ( "Failed to remove trap file: {}" , err) ) ?;
162+
163+ Ok ( ( ) )
164+ }
165+
100166 /// Get the database path, creating it if necessary.
101167 pub fn create_db_path ( & self ) -> Result < PathBuf , String > {
102168 let db_path = self
0 commit comments