Skip to content

Commit 4595a71

Browse files
committed
wip
1 parent 960837a commit 4595a71

File tree

6 files changed

+123
-221
lines changed

6 files changed

+123
-221
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/katana/src/cli/db.rs

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@ use clap::{Args, Subcommand};
55
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
66
use comfy_table::presets::UTF8_FULL;
77
use 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};
119
use katana_db::mdbx::{DbEnv, DbEnvKind};
1210
use katana_db::models::list::BlockList;
1311
use 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};
1813
use katana_db::version::{get_db_version, CURRENT_DB_VERSION};
1914
use katana_primitives::block::BlockNumber;
2015

@@ -29,13 +24,13 @@ macro_rules! byte_unit {
2924
};
3025
}
3126

32-
#[derive(Args)]
27+
#[derive(Debug, Args)]
3328
pub struct DbArgs {
3429
#[command(subcommand)]
3530
commands: Commands,
3631
}
3732

38-
#[derive(Subcommand)]
33+
#[derive(Debug, Subcommand)]
3934
enum Commands {
4035
/// Retrieves database statistics
4136
Stats {
@@ -58,7 +53,7 @@ enum Commands {
5853
Prune(PruneArgs),
5954
}
6055

61-
#[derive(Args)]
56+
#[derive(Debug, Args)]
6257
struct PruneArgs {
6358
/// Path to the database directory.
6459
#[arg(short, long)]
@@ -69,7 +64,7 @@ struct PruneArgs {
6964
mode: PruneMode,
7065
}
7166

72-
#[derive(Subcommand)]
67+
#[derive(Debug, Subcommand)]
7368
pub enum PruneMode {
7469
// Keep only the latest trie state (remove all historical data)
7570
Latest,
@@ -271,8 +266,8 @@ fn table() -> Table {
271266
}
272267

273268
/// Get the latest block number from the Headers table
274-
pub fn get_latest_block_number<Tx: DbTx>(tx: &Tx) -> Result<BlockNumber> {
275-
let mut cursor = tx.cursor::<Headers>()?;
269+
fn get_latest_block_number(tx: &impl DbTx) -> Result<BlockNumber> {
270+
let mut cursor = tx.cursor::<tables::Headers>()?;
276271
if let Some((block_num, _)) = cursor.last()? {
277272
Ok(block_num)
278273
} else {
@@ -281,24 +276,20 @@ pub fn get_latest_block_number<Tx: DbTx>(tx: &Tx) -> Result<BlockNumber> {
281276
}
282277

283278
/// Prune all historical trie data (keeping only current state)
284-
pub fn prune_all_history<Tx: DbTxMut>(tx: &Tx) -> Result<()> {
285-
tx.clear::<ClassesTrieHistory>()?;
286-
tx.clear::<ContractsTrieHistory>()?;
287-
tx.clear::<StoragesTrieHistory>()?;
279+
fn prune_all_history(tx: &impl DbTxMut) -> Result<()> {
280+
tx.clear::<tables::ClassesTrieHistory>()?;
281+
tx.clear::<tables::ContractsTrieHistory>()?;
282+
tx.clear::<tables::StoragesTrieHistory>()?;
288283

289-
tx.clear::<ClassesTrieChangeSet>()?;
290-
tx.clear::<ContractsTrieChangeSet>()?;
291-
tx.clear::<StoragesTrieChangeSet>()?;
284+
tx.clear::<tables::ClassesTrieChangeSet>()?;
285+
tx.clear::<tables::ContractsTrieChangeSet>()?;
286+
tx.clear::<tables::StoragesTrieChangeSet>()?;
292287

293288
Ok(())
294289
}
295290

296291
/// Prune historical data keeping only the last N blocks
297-
pub fn prune_keep_last_n<Tx: DbTxMut>(
298-
tx: &Tx,
299-
latest_block: BlockNumber,
300-
keep_blocks: u64,
301-
) -> Result<()> {
292+
fn prune_keep_last_n(tx: &impl DbTxMut, latest_block: BlockNumber, keep_blocks: u64) -> Result<()> {
302293
let cutoff_block = latest_block.saturating_sub(keep_blocks);
303294

304295
if cutoff_block == 0 {
@@ -317,31 +308,24 @@ pub fn prune_keep_last_n<Tx: DbTxMut>(
317308
}
318309

319310
/// Prune historical entries for a specific trie type up to the cutoff block
320-
pub fn prune_history_table<T: tables::Trie>(
311+
fn prune_history_table<T: tables::Trie>(
321312
tx: &impl DbTxMut,
322313
cutoff_block: BlockNumber,
323314
) -> Result<()> {
324315
let mut cursor = tx.cursor_dup_mut::<T::History>()?;
325-
let mut blocks_to_delete = Vec::new();
326316

327317
if let Some((block, _)) = cursor.first()? {
328318
let mut current_block = block;
329319
while current_block <= cutoff_block {
330-
blocks_to_delete.push(current_block);
331-
if let Some((next_block, _)) = cursor.next_no_dup()? {
320+
cursor.delete_current_duplicates()?;
321+
if let Some((next_block, _)) = cursor.next()? {
332322
current_block = next_block;
333323
} else {
334324
break;
335325
}
336326
}
337327
}
338328

339-
for block in blocks_to_delete {
340-
if cursor.seek(block)?.is_some() {
341-
cursor.delete_current_duplicates()?;
342-
}
343-
}
344-
345329
Ok(())
346330
}
347331

bin/katana/src/cli/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod version;
1313

1414
use version::{generate_long, generate_short};
1515

16-
#[derive(Parser)]
16+
#[derive(Debug, Parser)]
1717
#[command(name = "katana", author, version = generate_short(), long_version = generate_long() ,about, long_about = None)]
1818
pub struct Cli {
1919
#[command(subcommand)]
@@ -38,7 +38,7 @@ impl Cli {
3838
}
3939
}
4040

41-
#[derive(Subcommand)]
41+
#[derive(Debug, Subcommand)]
4242
enum Commands {
4343
#[command(about = "Initialize chain")]
4444
Init(Box<init::InitArgs>),

0 commit comments

Comments
 (0)