-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rooch-da): add rollback for exec cmd (#3066)
## Summary 1. add rollback for catching wrong tx execution 2. implement transaction order hash command for dump tx_order:tx_hash:block
- Loading branch information
Showing
6 changed files
with
527 additions
and
188 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
crates/rooch/src/commands/da/commands/dump_tx_order_hash.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) RoochNetwork | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::commands::da::commands::{LedgerTxGetter, TxOrderHashBlock}; | ||
use rooch_types::error::{RoochError, RoochResult}; | ||
use std::fs::File; | ||
use std::io::BufWriter; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
|
||
/// Dump tx_order:tx_hash:block_number to a file from segments | ||
#[derive(Debug, clap::Parser)] | ||
pub struct DumpTxOrderHashCommand { | ||
#[clap(long = "segment-dir")] | ||
pub segment_dir: PathBuf, | ||
#[clap(long = "output")] | ||
pub output: PathBuf, | ||
} | ||
|
||
impl DumpTxOrderHashCommand { | ||
pub fn execute(self) -> RoochResult<()> { | ||
let ledger_tx_loader = LedgerTxGetter::new(self.segment_dir)?; | ||
let mut block_number = ledger_tx_loader.get_min_chunk_id(); | ||
let mut expected_tx_order = 0; | ||
let file = File::create(self.output.clone())?; | ||
let mut writer = BufWriter::with_capacity(8 * 1024 * 1024, file.try_clone().unwrap()); | ||
|
||
loop { | ||
if block_number > ledger_tx_loader.get_max_chunk_id() { | ||
break; | ||
} | ||
let tx_list = ledger_tx_loader.load_ledger_tx_list(block_number, true)?; | ||
let tx_list = tx_list.unwrap(); | ||
for mut ledger_tx in tx_list { | ||
let tx_order = ledger_tx.sequence_info.tx_order; | ||
let tx_hash = ledger_tx.tx_hash(); | ||
if expected_tx_order == 0 { | ||
expected_tx_order = tx_order; | ||
} else if tx_order != expected_tx_order { | ||
return Err(RoochError::from(anyhow::anyhow!( | ||
"tx_order mismatch: expected {}, got {}", | ||
expected_tx_order, | ||
tx_order | ||
))); | ||
} | ||
writeln!( | ||
writer, | ||
"{}", | ||
TxOrderHashBlock::new(tx_order, tx_hash, block_number) | ||
)?; | ||
expected_tx_order += 1; | ||
} | ||
block_number += 1; | ||
} | ||
writer.flush()?; | ||
file.sync_data()?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.