Skip to content

Commit

Permalink
Separate ledgerdb tables (#1729)
Browse files Browse the repository at this point in the history
Co-authored-by: jfldde <168934971+jfldde@users.noreply.github.com>
  • Loading branch information
ercecan and jfldde authored Jan 23, 2025
1 parent e7185d2 commit dc8917c
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 24 deletions.
55 changes: 47 additions & 8 deletions bin/citrea/src/rollup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ use sov_db::ledger_db::migrations::LedgerDBMigrator;
use sov_db::ledger_db::{LedgerDB, SharedLedgerOps};
use sov_db::mmr_db::MmrDB;
use sov_db::rocks_db_config::RocksdbConfig;
use sov_db::schema::tables::{
BATCH_PROVER_LEDGER_TABLES, FULL_NODE_LEDGER_TABLES, LIGHT_CLIENT_PROVER_LEDGER_TABLES,
SEQUENCER_LEDGER_TABLES,
};
use sov_db::schema::types::SoftConfirmationNumber;
use sov_modules_api::Spec;
use sov_modules_rollup_blueprint::RollupBlueprint;
Expand Down Expand Up @@ -66,12 +70,21 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
rollup_config.storage.path.as_path(),
citrea_sequencer::db_migrations::migrations(),
);
migrator.migrate(rollup_config.storage.db_max_open_files)?;

let sequencer_tables = SEQUENCER_LEDGER_TABLES
.iter()
.map(|table| table.to_string())
.collect::<Vec<_>>();

migrator.migrate(
rollup_config.storage.db_max_open_files,
sequencer_tables.clone(),
)?;

let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
None,
Some(sequencer_tables),
);
let ledger_db = self.create_ledger_db(&rocksdb_config);
let genesis_config = self.create_genesis_config(runtime_genesis_paths, &rollup_config)?;
Expand Down Expand Up @@ -188,12 +201,20 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
citrea_fullnode::db_migrations::migrations(),
);

migrator.migrate(rollup_config.storage.db_max_open_files)?;
let full_node_tables = FULL_NODE_LEDGER_TABLES
.iter()
.map(|table| table.to_string())
.collect::<Vec<_>>();

migrator.migrate(
rollup_config.storage.db_max_open_files,
full_node_tables.clone(),
)?;

let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
None,
Some(full_node_tables),
);

let ledger_db = self.create_ledger_db(&rocksdb_config);
Expand Down Expand Up @@ -315,12 +336,21 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
rollup_config.storage.path.as_path(),
citrea_batch_prover::db_migrations::migrations(),
);
migrator.migrate(rollup_config.storage.db_max_open_files)?;

let batch_prover_tables = BATCH_PROVER_LEDGER_TABLES
.iter()
.map(|table| table.to_string())
.collect::<Vec<_>>();

migrator.migrate(
rollup_config.storage.db_max_open_files,
batch_prover_tables.clone(),
)?;

let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
None,
Some(batch_prover_tables),
);
let ledger_db = self.create_ledger_db(&rocksdb_config);

Expand Down Expand Up @@ -437,7 +467,16 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
rollup_config.storage.path.as_path(),
citrea_light_client_prover::db_migrations::migrations(),
);
migrator.migrate(rollup_config.storage.db_max_open_files)?;

let light_client_prover_tables = LIGHT_CLIENT_PROVER_LEDGER_TABLES
.iter()
.map(|table| table.to_string())
.collect::<Vec<_>>();

migrator.migrate(
rollup_config.storage.db_max_open_files,
light_client_prover_tables.clone(),
)?;

let mut task_manager = TaskManager::default();
let da_service = self
Expand All @@ -447,7 +486,7 @@ pub trait CitreaRollupBlueprint: RollupBlueprint {
let rocksdb_config = RocksdbConfig::new(
rollup_config.storage.path.as_path(),
rollup_config.storage.db_max_open_files,
None,
Some(light_client_prover_tables),
);
let ledger_db = self.create_ledger_db(&rocksdb_config);
let mmr_db = MmrDB::new(&rocksdb_config)?;
Expand Down
22 changes: 18 additions & 4 deletions bin/citrea/tests/e2e/sequencer_replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use reth_primitives::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_db::schema::tables::SEQUENCER_LEDGER_TABLES;
use sov_mock_da::{MockAddress, MockDaService};
use tokio::time::sleep;

Expand Down Expand Up @@ -246,9 +247,18 @@ async fn test_sequencer_crash_restore_mempool() -> Result<(), anyhow::Error> {
&sequencer_db_dir,
&storage_dir.path().join("sequencer_unlocked"),
);

let sequencer_tables = SEQUENCER_LEDGER_TABLES
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>();
let sequencer_db_dir = storage_dir.path().join("sequencer_unlocked").to_path_buf();
let ledger_db =
LedgerDB::with_config(&RocksdbConfig::new(sequencer_db_dir.as_path(), None, None)).unwrap();
let ledger_db = LedgerDB::with_config(&RocksdbConfig::new(
sequencer_db_dir.as_path(),
None,
Some(sequencer_tables.clone()),
))
.unwrap();
let txs = ledger_db.get_mempool_txs().unwrap();
assert_eq!(txs.len(), 2);
assert_eq!(txs[1].0, tx_hash.to_vec());
Expand Down Expand Up @@ -333,8 +343,12 @@ async fn test_sequencer_crash_restore_mempool() -> Result<(), anyhow::Error> {
&storage_dir.path().join("sequencer_unlocked"),
);
let sequencer_db_dir = storage_dir.path().join("sequencer_unlocked").to_path_buf();
let ledger_db =
LedgerDB::with_config(&RocksdbConfig::new(sequencer_db_dir.as_path(), None, None)).unwrap();
let ledger_db = LedgerDB::with_config(&RocksdbConfig::new(
sequencer_db_dir.as_path(),
None,
Some(sequencer_tables),
))
.unwrap();
let txs = ledger_db.get_mempool_txs().unwrap();
// should be removed from db
assert_eq!(txs.len(), 0);
Expand Down
3 changes: 3 additions & 0 deletions crates/batch-prover/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use std::sync::OnceLock;
use sov_db::ledger_db::migrations::LedgerMigration;

use crate::db_migrations::batch_and_slot_by_number::MigrateBatchAndSlotByNumber;
use crate::db_migrations::remove_unused_common_tables::RemoveUnusedTables;
use crate::db_migrations::verified_proofs::MigrateVerifiedProofsBySlotNumber;

mod batch_and_slot_by_number;
mod remove_unused_common_tables;
mod verified_proofs;

pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'static>> {
Expand All @@ -15,6 +17,7 @@ pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'sta
vec![
Box::new(MigrateVerifiedProofsBySlotNumber {}),
Box::new(MigrateBatchAndSlotByNumber {}),
Box::new(RemoveUnusedTables {}),
]
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::sync::Arc;

use sov_db::ledger_db::migrations::{LedgerMigration, MigrationName, MigrationVersion};
use sov_db::ledger_db::LedgerDB;
use sov_db::schema::tables::{BATCH_PROVER_LEDGER_TABLES, LEDGER_TABLES};

/// Table removal migration
/// tables BatchByNumber and SlotByNumber are removed
pub(crate) struct RemoveUnusedTables {}

impl LedgerMigration for RemoveUnusedTables {
fn identifier(&self) -> (MigrationName, MigrationVersion) {
("RemoveUnusedTables".to_owned(), 3)
}

fn execute(
&self,
ledger_db: Arc<LedgerDB>,
tables_to_drop: &mut Vec<String>,
) -> anyhow::Result<()> {
// Get difference of LEDGER_TABLES and SEQUENCER_LEDGER_TABLES and drop them
let mut diff = LEDGER_TABLES.to_vec();
diff.retain(|x| !BATCH_PROVER_LEDGER_TABLES.contains(x));
let diff_tables = diff.iter().map(|x| x.to_string()).collect::<Vec<_>>();
for table in diff_tables {
// Check if table exists in the database
if ledger_db.get_cf_handle(&table).is_ok() {
tables_to_drop.push(table);
}
}
Ok(())
}
}
3 changes: 3 additions & 0 deletions crates/fullnode/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use std::sync::OnceLock;
use sov_db::ledger_db::migrations::LedgerMigration;

use crate::db_migrations::batch_and_slot_by_number::MigrateBatchAndSlotByNumber;
use crate::db_migrations::remove_unused_common_tables::RemoveUnusedTables;
use crate::db_migrations::verified_proofs::MigrateVerifiedProofsBySlotNumber;

mod batch_and_slot_by_number;
mod remove_unused_common_tables;
mod verified_proofs;

pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'static>> {
Expand All @@ -15,6 +17,7 @@ pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'sta
vec![
Box::new(MigrateVerifiedProofsBySlotNumber {}),
Box::new(MigrateBatchAndSlotByNumber {}),
Box::new(RemoveUnusedTables {}),
]
})
}
33 changes: 33 additions & 0 deletions crates/fullnode/src/db_migrations/remove_unused_common_tables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::sync::Arc;

use sov_db::ledger_db::migrations::{LedgerMigration, MigrationName, MigrationVersion};
use sov_db::ledger_db::LedgerDB;
use sov_db::schema::tables::{FULL_NODE_LEDGER_TABLES, LEDGER_TABLES};

/// Table removal migration
/// tables BatchByNumber and SlotByNumber are removed
pub(crate) struct RemoveUnusedTables {}

impl LedgerMigration for RemoveUnusedTables {
fn identifier(&self) -> (MigrationName, MigrationVersion) {
("RemoveUnusedTables".to_owned(), 3)
}

fn execute(
&self,
ledger_db: Arc<LedgerDB>,
tables_to_drop: &mut Vec<String>,
) -> anyhow::Result<()> {
// Get difference of LEDGER_TABLES and SEQUENCER_LEDGER_TABLES and drop them
let mut diff = LEDGER_TABLES.to_vec();
diff.retain(|x| !FULL_NODE_LEDGER_TABLES.contains(x));
let diff_tables = diff.iter().map(|x| x.to_string()).collect::<Vec<_>>();
for table in diff_tables {
// Check if table exists in the database
if ledger_db.get_cf_handle(&table).is_ok() {
tables_to_drop.push(table);
}
}
Ok(())
}
}
6 changes: 5 additions & 1 deletion crates/light-client-prover/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use std::sync::OnceLock;

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

mod remove_unused_common_tables;

use remove_unused_common_tables::RemoveUnusedTables;

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)
MIGRATIONS.get_or_init(|| vec![Box::new(RemoveUnusedTables {})])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::sync::Arc;

use sov_db::ledger_db::migrations::{LedgerMigration, MigrationName, MigrationVersion};
use sov_db::ledger_db::LedgerDB;
use sov_db::schema::tables::{LEDGER_TABLES, LIGHT_CLIENT_PROVER_LEDGER_TABLES};

/// Table removal migration
/// tables BatchByNumber and SlotByNumber are removed
pub(crate) struct RemoveUnusedTables {}

impl LedgerMigration for RemoveUnusedTables {
fn identifier(&self) -> (MigrationName, MigrationVersion) {
("RemoveUnusedTables".to_owned(), 3)
}

fn execute(
&self,
ledger_db: Arc<LedgerDB>,
tables_to_drop: &mut Vec<String>,
) -> anyhow::Result<()> {
// Get difference of LEDGER_TABLES and SEQUENCER_LEDGER_TABLES and drop them
let mut diff = LEDGER_TABLES.to_vec();
diff.retain(|x| !LIGHT_CLIENT_PROVER_LEDGER_TABLES.contains(x));
let diff_tables = diff.iter().map(|x| x.to_string()).collect::<Vec<_>>();
for table in diff_tables {
// Check if table exists in the database
if ledger_db.get_cf_handle(&table).is_ok() {
tables_to_drop.push(table);
}
}
Ok(())
}
}
3 changes: 3 additions & 0 deletions crates/sequencer/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use std::sync::OnceLock;
use sov_db::ledger_db::migrations::LedgerMigration;

use crate::db_migrations::batch_and_slot_by_number::MigrateBatchAndSlotByNumber;
use crate::db_migrations::remove_unused_common_tables::RemoveUnusedTables;
use crate::db_migrations::verified_proofs::MigrateVerifiedProofsBySlotNumber;

mod batch_and_slot_by_number;
mod remove_unused_common_tables;
mod verified_proofs;

pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'static>> {
Expand All @@ -15,6 +17,7 @@ pub fn migrations() -> &'static Vec<Box<dyn LedgerMigration + Send + Sync + 'sta
vec![
Box::new(MigrateVerifiedProofsBySlotNumber {}),
Box::new(MigrateBatchAndSlotByNumber {}),
Box::new(RemoveUnusedTables {}),
]
})
}
33 changes: 33 additions & 0 deletions crates/sequencer/src/db_migrations/remove_unused_common_tables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::sync::Arc;

use sov_db::ledger_db::migrations::{LedgerMigration, MigrationName, MigrationVersion};
use sov_db::ledger_db::LedgerDB;
use sov_db::schema::tables::{LEDGER_TABLES, SEQUENCER_LEDGER_TABLES};

/// Table removal migration
/// tables BatchByNumber and SlotByNumber are removed
pub(crate) struct RemoveUnusedTables {}

impl LedgerMigration for RemoveUnusedTables {
fn identifier(&self) -> (MigrationName, MigrationVersion) {
("RemoveUnusedTables".to_owned(), 3)
}

fn execute(
&self,
ledger_db: Arc<LedgerDB>,
tables_to_drop: &mut Vec<String>,
) -> anyhow::Result<()> {
// Get difference of LEDGER_TABLES and SEQUENCER_LEDGER_TABLES and drop them
let mut diff = LEDGER_TABLES.to_vec();
diff.retain(|x| !SEQUENCER_LEDGER_TABLES.contains(x));
let diff_tables = diff.iter().map(|x| x.to_string()).collect::<Vec<_>>();
for table in diff_tables {
// Check if table exists in the database
if ledger_db.get_cf_handle(&table).is_ok() {
tables_to_drop.push(table);
}
}
Ok(())
}
}
Loading

0 comments on commit dc8917c

Please sign in to comment.