Skip to content

Commit

Permalink
Ledger db migrations (#1358)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakanalh authored Oct 23, 2024
1 parent 07729cc commit ce41cfb
Show file tree
Hide file tree
Showing 18 changed files with 435 additions and 37 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ adapters/solana/libyellowstone_grpc_geyser.dylib

reveal_*.tx

db
*.txs
22 changes: 22 additions & 0 deletions bin/citrea/src/rollup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use citrea_fullnode::{CitreaFullnode, FullNode};
use citrea_primitives::forks::FORKS;
use citrea_prover::{CitreaProver, Prover};
use citrea_sequencer::{CitreaSequencer, Sequencer};
use sov_db::ledger_db::migrations::LedgerDBMigrator;
use sov_db::ledger_db::SharedLedgerOps;
use sov_db::rocks_db_config::RocksdbConfig;
use sov_db::schema::types::BatchNumber;
Expand Down Expand Up @@ -48,6 +49,13 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
// Maybe whole "prev_root" can be initialized inside runner
// Getting block here, so prover_service doesn't have to be `Send`

// Migrate before constructing ledger_db instance so that no lock is present.
let migrator = LedgerDBMigrator::new(
rollup_config.storage.path.as_path(),
citrea_sequencer::db_migrations::migrations(),
);
migrator.migrate(rollup_config.storage.db_max_open_files)?;

let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
Expand Down Expand Up @@ -148,6 +156,13 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
// Maybe whole "prev_root" can be initialized inside runner
// Getting block here, so prover_service doesn't have to be `Send`

// Migrate before constructing ledger_db instance so that no lock is present.
let migrator = LedgerDBMigrator::new(
rollup_config.storage.path.as_path(),
citrea_fullnode::db_migrations::migrations(),
);
migrator.migrate(rollup_config.storage.db_max_open_files)?;

let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
Expand Down Expand Up @@ -247,6 +262,13 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
{
let da_service = self.create_da_service(&rollup_config, true).await?;

// Migrate before constructing ledger_db instance so that no lock is present.
let migrator = LedgerDBMigrator::new(
rollup_config.storage.path.as_path(),
citrea_prover::db_migrations::migrations(),
);
migrator.migrate(rollup_config.storage.db_max_open_files)?;

let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
Expand Down
20 changes: 0 additions & 20 deletions bin/citrea/tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod syncing;
mod system_transactions;
mod tx_propagation;

use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::Duration;
Expand Down Expand Up @@ -490,25 +489,6 @@ async fn initialize_test(
)
}

fn copy_dir_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
if !dst.exists() {
fs::create_dir(dst)?;
}

for entry in fs::read_dir(src)? {
let entry = entry?;
let entry_path = entry.path();
let target_path = dst.join(entry.file_name());

if entry_path.is_dir() {
copy_dir_recursive(&entry_path, &target_path)?;
} else {
fs::copy(&entry_path, &target_path)?;
}
}
Ok(())
}

async fn execute_blocks(
sequencer_client: &TestClient,
full_node_client: &TestClient,
Expand Down
10 changes: 5 additions & 5 deletions bin/citrea/tests/e2e/reopen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::time::Duration;
use citrea_common::{ProverConfig, SequencerConfig};
use citrea_stf::genesis_config::GenesisPaths;
use reth_primitives::{Address, BlockNumberOrTag};
use sov_db::ledger_db::migrations::copy_db_dir_recursive;
use sov_mock_da::{MockAddress, MockDaService};
use tokio::runtime::Runtime;
use tokio::time::sleep;

use crate::e2e::copy_dir_recursive;
use crate::evm::{init_test_rollup, make_test_client};
use crate::test_helpers::{
create_default_rollup_config, start_rollup, tempdir_with_children, wait_for_l1_block,
Expand Down Expand Up @@ -122,7 +122,7 @@ async fn test_reopen_full_node() -> Result<(), anyhow::Error> {

// Copy the db to a new path with the same contents because
// the lock is not released on the db directory even though the task is aborted
let _ = copy_dir_recursive(&fullnode_db_dir, &storage_dir.path().join("fullnode_copy"));
let _ = copy_db_dir_recursive(&fullnode_db_dir, &storage_dir.path().join("fullnode_copy"));

let fullnode_db_dir = storage_dir.path().join("fullnode_copy");

Expand Down Expand Up @@ -214,7 +214,7 @@ async fn test_reopen_sequencer() -> Result<(), anyhow::Error> {

// Copy the db to a new path with the same contents because
// the lock is not released on the db directory even though the task is aborted
let _ = copy_dir_recursive(
let _ = copy_db_dir_recursive(
&sequencer_db_dir,
&storage_dir.path().join("sequencer_copy"),
);
Expand Down Expand Up @@ -372,7 +372,7 @@ async fn test_reopen_prover() -> Result<(), anyhow::Error> {

sleep(Duration::from_secs(1)).await;

let _ = copy_dir_recursive(&prover_db_dir, &storage_dir.path().join("prover_copy"));
let _ = copy_db_dir_recursive(&prover_db_dir, &storage_dir.path().join("prover_copy"));
sleep(Duration::from_secs(1)).await;

// Reopen prover with the new path
Expand Down Expand Up @@ -425,7 +425,7 @@ async fn test_reopen_prover() -> Result<(), anyhow::Error> {
seq_test_client.send_publish_batch_request().await;
seq_test_client.send_publish_batch_request().await;
wait_for_l2_block(&seq_test_client, 8, None).await;
let _ = copy_dir_recursive(&prover_db_dir, &storage_dir.path().join("prover_copy2"));
let _ = copy_db_dir_recursive(&prover_db_dir, &storage_dir.path().join("prover_copy2"));

sleep(Duration::from_secs(2)).await;
// Reopen prover with the new path
Expand Down
11 changes: 6 additions & 5 deletions bin/citrea/tests/e2e/sequencer_replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ use alloy_rlp::Decodable;
use citrea_common::{SequencerConfig, SequencerMempoolConfig};
use citrea_stf::genesis_config::GenesisPaths;
use reth_primitives::{Address, BlockNumberOrTag};
use sov_db::ledger_db::migrations::copy_db_dir_recursive;
use sov_db::ledger_db::{LedgerDB, SequencerLedgerOps};
use sov_db::rocks_db_config::RocksdbConfig;
use sov_mock_da::{MockAddress, MockDaService};
use tokio::time::sleep;

use crate::e2e::{copy_dir_recursive, execute_blocks, TestConfig};
use crate::e2e::{execute_blocks, TestConfig};
use crate::evm::{init_test_rollup, make_test_client};
use crate::test_helpers::{
create_default_rollup_config, start_rollup, tempdir_with_children, wait_for_commitment,
Expand Down Expand Up @@ -118,7 +119,7 @@ async fn test_sequencer_crash_and_replace_full_node() -> Result<(), anyhow::Erro

// Copy the db to a new path with the same contents because
// the lock is not released on the db directory even though the task is aborted
let _ = copy_dir_recursive(&fullnode_db_dir, &storage_dir.path().join("full_node_copy"));
let _ = copy_db_dir_recursive(&fullnode_db_dir, &storage_dir.path().join("full_node_copy"));
let sequencer_db_dir = storage_dir.path().join("full_node_copy");

let config1 = sequencer_config.clone();
Expand Down Expand Up @@ -236,7 +237,7 @@ async fn test_sequencer_crash_restore_mempool() -> Result<(), anyhow::Error> {
// Copy data into a separate directory since the original sequencer
// directory is locked by a LOCK file.
// This would enable us to access ledger DB directly.
let _ = copy_dir_recursive(
let _ = copy_db_dir_recursive(
&sequencer_db_dir,
&storage_dir.path().join("sequencer_unlocked"),
);
Expand All @@ -256,7 +257,7 @@ async fn test_sequencer_crash_restore_mempool() -> Result<(), anyhow::Error> {
// Remove lock
drop(ledger_db);

let _ = copy_dir_recursive(
let _ = copy_db_dir_recursive(
&sequencer_db_dir,
&storage_dir.path().join("sequencer_copy"),
);
Expand Down Expand Up @@ -321,7 +322,7 @@ async fn test_sequencer_crash_restore_mempool() -> Result<(), anyhow::Error> {
// Copy data into a separate directory since the original sequencer
// directory is locked by a LOCK file.
// This would enable us to access ledger DB directly.
let _ = copy_dir_recursive(
let _ = copy_db_dir_recursive(
&sequencer_db_dir,
&storage_dir.path().join("sequencer_unlocked"),
);
Expand Down
9 changes: 9 additions & 0 deletions crates/fullnode/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::sync::OnceLock;

use sov_db::ledger_db::migrations::LedgerMigration;

pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'static>> {
static MIGRATIONS: OnceLock<Vec<Box<dyn LedgerMigration + Send + Sync + 'static>>> =
OnceLock::new();
MIGRATIONS.get_or_init(Vec::new)
}
1 change: 1 addition & 0 deletions crates/fullnode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::sync::oneshot;
use tracing::instrument;

mod da_block_handler;
pub mod db_migrations;
mod runner;

/// Dependencies needed to run the rollup.
Expand Down
10 changes: 10 additions & 0 deletions crates/prover/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::sync::OnceLock;

use sov_db::ledger_db::migrations::LedgerMigration;

#[allow(dead_code)]
pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'static>> {
static MIGRATIONS: OnceLock<Vec<Box<dyn LedgerMigration + Send + Sync + 'static>>> =
OnceLock::new();
MIGRATIONS.get_or_init(Vec::new)
}
1 change: 1 addition & 0 deletions crates/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tokio::sync::oneshot;
use tracing::instrument;

mod da_block_handler;
pub mod db_migrations;
mod errors;
pub mod prover_service;
mod runner;
Expand Down
9 changes: 9 additions & 0 deletions crates/sequencer/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::sync::OnceLock;

use sov_db::ledger_db::migrations::LedgerMigration;

pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'static>> {
static MIGRATIONS: OnceLock<Vec<Box<dyn LedgerMigration + Send + Sync + 'static>>> =
OnceLock::new();
MIGRATIONS.get_or_init(Vec::new)
}
1 change: 1 addition & 0 deletions crates/sequencer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod commitment;
pub mod db_migrations;
mod db_provider;
mod deposit_data_mempool;
mod mempool;
Expand Down
5 changes: 2 additions & 3 deletions crates/sovereign-sdk/full-node/db/sov-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ byteorder = { workspace = true, default-features = true }
hex = { workspace = true }
proptest = { workspace = true, optional = true, default-features = true }
proptest-derive = { workspace = true, optional = true }
rlimit ={ workspace = true }
rlimit = { workspace = true }
rocksdb = { workspace = true }
serde = { workspace = true, default-features = true, features = ["rc"] }
tempfile = { workspace = true, optional = true }
tempfile = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }

Expand All @@ -46,7 +46,6 @@ arbitrary = [
"dep:arbitrary",
"dep:proptest",
"dep:proptest-derive",
"dep:tempfile",
]

[[bench]]
Expand Down
Loading

0 comments on commit ce41cfb

Please sign in to comment.