Skip to content

Commit

Permalink
Example migration
Browse files Browse the repository at this point in the history
  • Loading branch information
rakanalh committed Oct 20, 2024
1 parent 45b3ddd commit f91a0f0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/sequencer/src/db_migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
use std::sync::OnceLock;

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

mod pruned_height;

pub(crate) 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![Box::new(pruned_height::MigratePrunedL2Height {})])
}
25 changes: 25 additions & 0 deletions crates/sequencer/src/db_migrations/pruned_height.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::sync::Arc;

use sov_db::ledger_db::migrations::{LedgerMigration, MigrationName, MigrationVersion};
use sov_db::ledger_db::{LedgerDB, SharedLedgerOps};

pub(super) struct MigratePrunedL2Height {}

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

fn execute(&self, ledger_db: Arc<LedgerDB>) -> anyhow::Result<()> {
let Some(mut last_pruned_height) = ledger_db.get_last_pruned_l2_height()? else {
// No need to do any migration
return Ok(());
};

// Example
last_pruned_height += 10;
ledger_db.set_last_pruned_l2_height(last_pruned_height)?;

Ok(())
}
}
8 changes: 8 additions & 0 deletions crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use reth_transaction_pool::{
};
use sov_accounts::Accounts;
use sov_accounts::Response::{AccountEmpty, AccountExists};
use sov_db::ledger_db::migrations::LedgerDBMigrator;
use sov_db::ledger_db::SequencerLedgerOps;
use sov_db::schema::types::{BatchNumber, SlotNumber};
use sov_modules_api::hooks::HookSoftConfirmationInfo;
Expand Down Expand Up @@ -817,6 +818,13 @@ where

#[instrument(level = "trace", skip(self), err, ret)]
pub async fn run(&mut self) -> Result<(), anyhow::Error> {
let migrator = LedgerDBMigrator::new(
self.ledger_db.path().to_path_buf(),
crate::db_migrations::migrations(),
);

migrator.migrate(Some(100))?;

if self.batch_hash != [0; 32] {
// Resubmit if there were pending commitments on restart, skip it on first init
self.resubmit_pending_commitments().await?;
Expand Down

0 comments on commit f91a0f0

Please sign in to comment.