Skip to content

Commit

Permalink
feat(pathfinder/examples): add a reorg tool to perform reorgs on a …
Browse files Browse the repository at this point in the history
…database

Usage: `cargo run --example reorg -- --target-block 190000 /path/to/database.sqlite`
  • Loading branch information
kkovaacs committed Sep 19, 2024
1 parent 723d666 commit 614be6f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
46 changes: 46 additions & 0 deletions crates/pathfinder/examples/reorg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::num::NonZeroU32;
use std::path::PathBuf;

use clap::Parser;
use pathfinder_common::BlockNumber;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};

#[derive(Parser)]
#[command(version)]
struct Cli {
#[arg(long_help = "Database path")]
pub database_path: PathBuf,
#[arg(long, long_help = "Roll back state to this block", value_parser = parse_block_number)]
pub target_block: BlockNumber,
}

fn parse_block_number(s: &str) -> Result<BlockNumber, String> {
let n: u64 = s
.parse()
.map_err(|e| format!("Invalid block number '{s}': {e}"))?;
BlockNumber::new(n).ok_or_else(|| format!("Invalid block number '{s}'"))
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();

tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.init();

let storage = pathfinder_storage::StorageBuilder::file(cli.database_path.into())
.migrate()?
.create_pool(NonZeroU32::new(10).unwrap())
.unwrap();

let mut connection = storage.connection().unwrap();

pathfinder_lib::state::l2_reorg(&mut connection, cli.target_block)
.await
.unwrap();

Ok(())
}
1 change: 1 addition & 0 deletions crates/pathfinder/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod sync;
pub use sync::{
l1,
l2,
l2_reorg,
revert,
sync,
update_starknet_state,
Expand Down
2 changes: 1 addition & 1 deletion crates/pathfinder/src/state/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ async fn l2_update(
Ok(())
}

async fn l2_reorg(connection: &mut Connection, reorg_tail: BlockNumber) -> anyhow::Result<()> {
pub async fn l2_reorg(connection: &mut Connection, reorg_tail: BlockNumber) -> anyhow::Result<()> {
tokio::task::block_in_place(move || {
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
Expand Down

0 comments on commit 614be6f

Please sign in to comment.