Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB Rollback #1796

Closed
wants to merge 13 commits into from
59 changes: 37 additions & 22 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
# Citrea
"bin/citrea",
"bin/cli",
"crates/batch-prover",
"crates/bitcoin-da",
"crates/citrea-stf",
Expand All @@ -13,7 +14,7 @@ members = [
"crates/light-client-prover",
"crates/primitives",
"crates/prover-services",
"crates/pruning",
"crates/storage-ops",
"crates/risc0",
"crates/sequencer",
"crates/soft-confirmation-rule-enforcer",
Expand Down
2 changes: 1 addition & 1 deletion bin/citrea/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ citrea-common = { path = "../../crates/common" }
citrea-fullnode = { path = "../../crates/fullnode" }
citrea-light-client-prover = { path = "../../crates/light-client-prover", features = ["native"] }
citrea-primitives = { path = "../../crates/primitives" }
citrea-pruning = { path = "../../crates/pruning" }
citrea-risc0-adapter = { path = "../../crates/risc0", features = ["native"] }
citrea-risc0-batch-proof = { path = "../../guests/risc0/batch-proof" }
citrea-risc0-light-client = { path = "../../guests/risc0/light-client-proof" }
citrea-sequencer = { path = "../../crates/sequencer" }
# citrea-sp1 = { path = "../../crates/sp1", features = ["native"] }
citrea-stf = { path = "../../crates/citrea-stf", features = ["native"] }
citrea-storage-ops = { path = "../../crates/storage-ops" }
ethereum-rpc = { path = "../../crates/ethereum-rpc" }
prover-services = { path = "../../crates/prover-services" }

Expand Down
6 changes: 3 additions & 3 deletions bin/citrea/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ where
});
}
_ => {
let (mut full_node, l1_block_handler, pruner) =
let (mut full_node, l1_block_handler, pruner_service) =
CitreaRollupBlueprint::create_full_node(
&rollup_blueprint,
genesis_config,
Expand Down Expand Up @@ -352,9 +352,9 @@ where
});

// Spawn pruner if configs are set
if let Some(pruner) = pruner {
if let Some(pruner_service) = pruner_service {
task_manager.spawn(|cancellation_token| async move {
pruner.run(cancellation_token).await
pruner_service.run(cancellation_token).await
});
}

Expand Down
4 changes: 2 additions & 2 deletions bin/citrea/src/rollup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use citrea_fullnode::CitreaFullnode;
use citrea_light_client_prover::da_block_handler::L1BlockHandler as LightClientProverL1BlockHandler;
use citrea_light_client_prover::runner::CitreaLightClientProver;
use citrea_primitives::forks::get_forks;
use citrea_pruning::Pruner;
use citrea_sequencer::CitreaSequencer;
use citrea_storage_ops::pruning::PrunerService;
use jsonrpsee::RpcModule;
use sov_db::ledger_db::migrations::{LedgerDBMigrator, Migrations};
use sov_db::ledger_db::{LedgerDB, SharedLedgerOps};
Expand Down Expand Up @@ -203,7 +203,7 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
StorageRootHash,
LedgerDB,
>,
Option<Pruner<LedgerDB>>,
Option<PrunerService<LedgerDB>>,
)>
where
<Self::NativeContext as Spec>::Storage: NativeStorage,
Expand Down
2 changes: 1 addition & 1 deletion bin/citrea/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use alloy_primitives::{Address, U256};
use citrea_common::{BatchProverConfig, SequencerConfig};
use citrea_evm::smart_contracts::SimpleStorageContract;
use citrea_primitives::forks::fork_from_block_number;
use citrea_pruning::PruningConfig;
use citrea_stf::genesis_config::GenesisPaths;
use citrea_storage_ops::pruning::PruningConfig;
use reth_primitives::BlockNumberOrTag;
use sov_mock_da::{MockAddress, MockDaService};
use sov_rollup_interface::rpc::{LastVerifiedBatchProofResponse, SoftConfirmationStatus};
Expand Down
2 changes: 1 addition & 1 deletion bin/citrea/tests/e2e/pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;

use alloy_primitives::Address;
/// Testing if the sequencer and full node can handle system transactions correctly (the full node should have the same system transactions as the sequencer)
use citrea_pruning::PruningConfig;
use citrea_storage_ops::pruning::PruningConfig;
use futures::FutureExt;
use reth_primitives::BlockNumberOrTag;
use sov_mock_da::{MockAddress, MockDaService};
Expand Down
2 changes: 1 addition & 1 deletion bin/citrea/tests/test_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use citrea_common::{
};
use citrea_light_client_prover::da_block_handler::StartVariant;
use citrea_primitives::TEST_PRIVATE_KEY;
use citrea_pruning::PruningConfig;
use citrea_stf::genesis_config::GenesisPaths;
use citrea_storage_ops::pruning::PruningConfig;
use sov_db::ledger_db::SharedLedgerOps;
use sov_db::rocks_db_config::RocksdbConfig;
use sov_db::schema::tables::{
Expand Down
30 changes: 30 additions & 0 deletions bin/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "citrea-cli"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
publish = false
resolver = "2"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# Citrea deps
citrea-storage-ops = { path = "../../crates/storage-ops" }

# 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"
path = "src/main.rs"
5 changes: 5 additions & 0 deletions bin/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod prune;
mod rollback;

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

use citrea_storage_ops::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_db::state_db::StateDB;
use sov_prover_storage_manager::SnapshotManager;
use tracing::{debug, info};

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 state_db = StateDB::<SnapshotManager>::setup_schema_db(&rocksdb_config)?;

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(state_db), Arc::new(native_db));
pruner.prune(soft_confirmation_number.0).await;

Ok(())
}
3 changes: 3 additions & 0 deletions bin/cli/src/commands/rollback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(crate) async fn rollback(_num_block: u32) -> anyhow::Result<()> {
Ok(())
}
61 changes: 61 additions & 0 deletions bin/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::path::PathBuf;

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

mod commands;

#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}

#[derive(Subcommand)]
enum Commands {
/// Prune old DB entries
Prune {
/// The path of the database to prune
#[arg(long)]
db_path: PathBuf,
/// The distance of the last pruned block to prune up to
#[arg(long)]
distance: u64,
},
/// Rollback the most recent N blocks
Rollback {
/// The path of the database to prune
#[arg(long)]
db_path: PathBuf,
/// The number of blocks to rollback
#[arg(long)]
blocks: u32,
},
}

#[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 } => {
commands::prune(db_path.clone(), *distance).await?;
}
Commands::Rollback {
db_path: _db_path,
blocks,
} => {
commands::rollback(*blocks).await?;
}
}

Ok(())
}
2 changes: 1 addition & 1 deletion crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ sov-stf-runner = { path = "../sovereign-sdk/full-node/sov-stf-runner", features

# Citrea
citrea-primitives = { path = "../primitives/" }
citrea-pruning = { path = "../pruning" }
citrea-storage-ops = { path = "../storage-ops" }

[dev-dependencies]
sov-mock-da = { path = "../sovereign-sdk/adapters/mock-da", features = ["native"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

use citrea_pruning::PruningConfig;
use citrea_storage_ops::pruning::PruningConfig;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sov_stf_runner::ProverGuestRunConfig;
Expand Down
2 changes: 1 addition & 1 deletion crates/fullnode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository.workspace = true
# Citrea Deps
citrea-common = { path = "../common" }
citrea-primitives = { path = "../primitives" }
citrea-pruning = { path = "../pruning" }
citrea-storage-ops = { path = "../storage-ops" }

# Sov SDK deps
sov-db = { path = "../sovereign-sdk/full-node/db/sov-db" }
Expand Down
Loading
Loading