Skip to content

Commit

Permalink
Initial rollback implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rakanalh committed Feb 4, 2025
1 parent 6b0adea commit 21577f9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions bin/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod prune;
mod rollback;

pub(crate) use prune::*;
pub(crate) use rollback::*;
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(())
}
4 changes: 2 additions & 2 deletions bin/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ enum Commands {
db_path: PathBuf,
/// The number of blocks to rollback
#[arg(long)]
blocks: u64,
blocks: u32,
},
}

Expand All @@ -50,7 +50,7 @@ async fn main() -> anyhow::Result<()> {
commands::prune(db_path.clone(), *distance).await?;
}
Commands::Rollback { db_path, blocks } => {
println!("Rolling back stuff: {:?}, blocks: {}", db_path, blocks);
commands::rollback(*blocks).await?;
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/storage-ops/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod pruning;
pub mod rollback;

#[cfg(test)]
mod tests;
12 changes: 12 additions & 0 deletions crates/storage-ops/src/rollback/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub mod service;

pub struct Rollback {}

impl Rollback {
/// Rollback the provided number of blocks
pub fn execute(&self, _num_blocks: u32) -> anyhow::Result<()> {
// Do something

Ok(())
}
}
35 changes: 35 additions & 0 deletions crates/storage-ops/src/rollback/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use tokio::select;
use tokio::sync::mpsc::Receiver;
use tokio_util::sync::CancellationToken;
use tracing::{error, info};

use super::Rollback;

pub struct RollbackService {
rollback: Rollback,
receiver: Receiver<u32>,
}

impl RollbackService {
pub fn new(rollback: Rollback, receiver: Receiver<u32>) -> Self {
Self { rollback, receiver }
}

/// Run service to rollback when instructed to
pub async fn run(mut self, cancellation_token: CancellationToken) {
loop {
select! {
biased;
_ = cancellation_token.cancelled() => {
return;
},
Some(num_blocks) = self.receiver.recv() => {
info!("Received signal to rollback {num_blocks} blocks");
if let Err(e) = self.rollback.execute(num_blocks) {
error!("Could not rollback blocks: {:?}", e);
}
}
}
}
}
}

0 comments on commit 21577f9

Please sign in to comment.