Skip to content

Commit

Permalink
Implement pruning command
Browse files Browse the repository at this point in the history
  • Loading branch information
rakanalh committed Feb 3, 2025
1 parent d8dc150 commit 45c1291
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions bin/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ citrea-pruning = { path = "../../crates/pruning" }

# Sovereign-SDK deps
sov-db = { path = "../../crates/sovereign-sdk/full-node/db/sov-db" }
sov-prover-storage-manager = { path = "../../crates/sovereign-sdk/full-node/sov-prover-storage-manager" }

# 3rd-party deps
anyhow = { workspace = true }
clap = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }

[[bin]]
name = "citrea-cli"
Expand Down
2 changes: 2 additions & 0 deletions bin/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
mod prune;

pub(crate) use prune::*;
25 changes: 22 additions & 3 deletions bin/cli/src/commands/prune.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
use std::path::PathBuf;
use std::sync::Arc;

use citrea_pruning::{Pruner, PruningConfig};
use sov_db::ledger_db::{LedgerDB, SharedLedgerOps};
use sov_db::native_db::NativeDB;
use sov_db::rocks_db_config::RocksdbConfig;
use sov_prover_storage_manager::SnapshotManager;
use tracing::{debug, info};

pub(crate) fn prune(db_path: PathBuf, distance: u64) -> anyhow::Result<()> {
pub(crate) async fn prune(db_path: PathBuf, distance: u64) -> anyhow::Result<()> {
info!(
"Pruning DB at {} with pruning distance of {}",
db_path.display(),
distance
);
let config = PruningConfig { distance };

let rocksdb_config = RocksdbConfig::new(&db_path, None, None);
let ledger_db = LedgerDB::with_config(&rocksdb_config)?;
let native_db = NativeDB::<SnapshotManager>::setup_schema_db(&rocksdb_config)?;

let last_pruned_block = ledger_db.get_last_pruned_l2_height()?.unwrap_or(0);
let pruner = Pruner::new(config, last_pruned_block, l2_receiver, ledger_db)?;
let Some((soft_confirmation_number, _)) = ledger_db.get_head_soft_confirmation()? else {
return Ok(());
};

debug!(
"Pruning up to latest soft confirmation number: {}, taking into consideration the configured distance of {}",
soft_confirmation_number.0, distance
);

let pruner = Pruner::new(config, ledger_db, Arc::new(native_db));
pruner.prune(soft_confirmation_number.0).await;

Ok(())
}
9 changes: 8 additions & 1 deletion bin/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};
use tracing_subscriber::fmt;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

mod commands;

Expand Down Expand Up @@ -36,16 +39,20 @@ enum Commands {

#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::registry().with(fmt::layer()).init();

let cli = Cli::parse();

// You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd
match &cli.command {
Commands::Prune { db_path, distance } => {
println!("Pruning stuff: {:?}, distance: {}", db_path, distance);
commands::prune(db_path.clone(), *distance).await?;
}
Commands::Rollback { db_path, blocks } => {
println!("Rolling back stuff: {:?}, blocks: {}", db_path, blocks);
}
}

Ok(())
}

0 comments on commit 45c1291

Please sign in to comment.