@@ -5,16 +5,11 @@ use clap::{Args, Subcommand};
55use comfy_table:: modifiers:: UTF8_ROUND_CORNERS ;
66use comfy_table:: presets:: UTF8_FULL ;
77use comfy_table:: Table ;
8- use katana_db:: abstraction:: {
9- Database , DbCursor , DbDupSortCursor , DbDupSortCursorMut , DbTx , DbTxMut ,
10- } ;
8+ use katana_db:: abstraction:: { Database , DbCursor , DbDupSortCursorMut , DbTx , DbTxMut } ;
119use katana_db:: mdbx:: { DbEnv , DbEnvKind } ;
1210use katana_db:: models:: list:: BlockList ;
1311use katana_db:: models:: trie:: TrieDatabaseKey ;
14- use katana_db:: tables:: {
15- self , ClassesTrieChangeSet , ClassesTrieHistory , ContractsTrieChangeSet , ContractsTrieHistory ,
16- Headers , StoragesTrieChangeSet , StoragesTrieHistory , NUM_TABLES ,
17- } ;
12+ use katana_db:: tables:: { self , NUM_TABLES } ;
1813use katana_primitives:: block:: BlockNumber ;
1914
2015/// Create a human-readable byte unit string (eg. 16.00 KiB)
@@ -28,7 +23,7 @@ macro_rules! byte_unit {
2823 } ;
2924}
3025
31- #[ derive( Args ) ]
26+ #[ derive( Debug , Args ) ]
3227pub struct DbArgs {
3328 #[ arg( short, long) ]
3429 #[ arg( global = true ) ]
@@ -40,21 +35,21 @@ pub struct DbArgs {
4035 commands : Commands ,
4136}
4237
43- #[ derive( Subcommand ) ]
38+ #[ derive( Debug , Subcommand ) ]
4439pub enum Commands {
4540 #[ command( about = "Retrieves database statistics" ) ]
4641 Stats ,
4742 #[ command( about = "Prune historical trie data" ) ]
4843 Prune ( PruneArgs ) ,
4944}
5045
51- #[ derive( Args ) ]
46+ #[ derive( Debug , Args ) ]
5247pub struct PruneArgs {
5348 #[ command( subcommand) ]
5449 mode : PruneMode ,
5550}
5651
57- #[ derive( Subcommand ) ]
52+ #[ derive( Debug , Subcommand ) ]
5853pub enum PruneMode {
5954 // Keep only the latest trie state (remove all historical data)
6055 Latest ,
@@ -232,8 +227,8 @@ fn table() -> Table {
232227}
233228
234229/// Get the latest block number from the Headers table
235- pub fn get_latest_block_number < Tx : DbTx > ( tx : & Tx ) -> Result < BlockNumber > {
236- let mut cursor = tx. cursor :: < Headers > ( ) ?;
230+ fn get_latest_block_number ( tx : & impl DbTx ) -> Result < BlockNumber > {
231+ let mut cursor = tx. cursor :: < tables :: Headers > ( ) ?;
237232 if let Some ( ( block_num, _) ) = cursor. last ( ) ? {
238233 Ok ( block_num)
239234 } else {
@@ -242,24 +237,20 @@ pub fn get_latest_block_number<Tx: DbTx>(tx: &Tx) -> Result<BlockNumber> {
242237}
243238
244239/// Prune all historical trie data (keeping only current state)
245- pub fn prune_all_history < Tx : DbTxMut > ( tx : & Tx ) -> Result < ( ) > {
246- tx. clear :: < ClassesTrieHistory > ( ) ?;
247- tx. clear :: < ContractsTrieHistory > ( ) ?;
248- tx. clear :: < StoragesTrieHistory > ( ) ?;
240+ fn prune_all_history ( tx : & impl DbTxMut ) -> Result < ( ) > {
241+ tx. clear :: < tables :: ClassesTrieHistory > ( ) ?;
242+ tx. clear :: < tables :: ContractsTrieHistory > ( ) ?;
243+ tx. clear :: < tables :: StoragesTrieHistory > ( ) ?;
249244
250- tx. clear :: < ClassesTrieChangeSet > ( ) ?;
251- tx. clear :: < ContractsTrieChangeSet > ( ) ?;
252- tx. clear :: < StoragesTrieChangeSet > ( ) ?;
245+ tx. clear :: < tables :: ClassesTrieChangeSet > ( ) ?;
246+ tx. clear :: < tables :: ContractsTrieChangeSet > ( ) ?;
247+ tx. clear :: < tables :: StoragesTrieChangeSet > ( ) ?;
253248
254249 Ok ( ( ) )
255250}
256251
257252/// Prune historical data keeping only the last N blocks
258- pub fn prune_keep_last_n < Tx : DbTxMut > (
259- tx : & Tx ,
260- latest_block : BlockNumber ,
261- keep_blocks : u64 ,
262- ) -> Result < ( ) > {
253+ fn prune_keep_last_n ( tx : & impl DbTxMut , latest_block : BlockNumber , keep_blocks : u64 ) -> Result < ( ) > {
263254 let cutoff_block = latest_block. saturating_sub ( keep_blocks) ;
264255
265256 if cutoff_block == 0 {
@@ -278,36 +269,29 @@ pub fn prune_keep_last_n<Tx: DbTxMut>(
278269}
279270
280271/// Prune historical entries for a specific trie type up to the cutoff block
281- pub fn prune_history_table < T : tables:: Trie > (
272+ fn prune_history_table < T : tables:: Trie > (
282273 tx : & impl DbTxMut ,
283274 cutoff_block : BlockNumber ,
284275) -> Result < ( ) > {
285276 let mut cursor = tx. cursor_dup_mut :: < T :: History > ( ) ?;
286- let mut blocks_to_delete = Vec :: new ( ) ;
287277
288278 if let Some ( ( block, _) ) = cursor. first ( ) ? {
289279 let mut current_block = block;
290280 while current_block <= cutoff_block {
291- blocks_to_delete . push ( current_block ) ;
292- if let Some ( ( next_block, _) ) = cursor. next_no_dup ( ) ? {
281+ cursor . delete_current_duplicates ( ) ? ;
282+ if let Some ( ( next_block, _) ) = cursor. next ( ) ? {
293283 current_block = next_block;
294284 } else {
295285 break ;
296286 }
297287 }
298288 }
299289
300- for block in blocks_to_delete {
301- if cursor. seek ( block) ?. is_some ( ) {
302- cursor. delete_current_duplicates ( ) ?;
303- }
304- }
305-
306290 Ok ( ( ) )
307291}
308292
309293/// Prune changeset entries by removing blocks up to the cutoff
310- pub fn prune_changeset_table < T : tables:: Trie > (
294+ fn prune_changeset_table < T : tables:: Trie > (
311295 tx : & impl DbTxMut ,
312296 cutoff_block : BlockNumber ,
313297) -> Result < ( ) > {
0 commit comments