From f8eb0c3dcb210b611db2ef6de285c409c8ce49af Mon Sep 17 00:00:00 2001 From: Aner Ben Efraim Date: Sun, 3 Nov 2024 14:58:51 +0200 Subject: [PATCH] chore(blockifier_reexecution): offline reexecution from file --- crates/blockifier_reexecution/src/main.rs | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/crates/blockifier_reexecution/src/main.rs b/crates/blockifier_reexecution/src/main.rs index 53c2b932b3..5f43e5c777 100644 --- a/crates/blockifier_reexecution/src/main.rs +++ b/crates/blockifier_reexecution/src/main.rs @@ -4,6 +4,7 @@ use blockifier_reexecution::state_reader::reexecution_state_reader::ReexecutionS use blockifier_reexecution::state_reader::test_state_reader::{ ConsecutiveStateReaders, ConsecutiveTestStateReaders, + OfflineConsecutiveStateReaders, SerializableOfflineReexecutionData, }; use blockifier_reexecution::state_reader::utils::JSON_RPC_VERSION; @@ -52,6 +53,18 @@ enum Command { #[clap(long, default_value = None)] directory_path: Option, }, + + // Reexecutes the block from JSON files. + ReexecuteBlock { + /// Block number. + #[clap(long, short = 'b')] + block_number: u64, + + /// Directory path to json files. + /// Default: "./crates/blockifier_reexecution/resources/block_{block_number}". + #[clap(long, default_value = None)] + directory_path: Option, + }, } #[derive(Debug, Args)] @@ -165,5 +178,36 @@ fn main() { "RPC replies required for reexecuting block {block_number} written to json file." ); } + + Command::ReexecuteBlock { block_number, directory_path } => { + let full_file_path = directory_path.unwrap_or(format!( + "./crates/blockifier_reexecution/resources/block_{block_number}" + )) + "/reexecution_data.json"; + + let serializable_offline_reexecution_data = + SerializableOfflineReexecutionData::read_from_file(&full_file_path).unwrap(); + + let reexecution_state_readers = + OfflineConsecutiveStateReaders::new(serializable_offline_reexecution_data.into()); + + let mut expected_state_diff = + reexecution_state_readers.get_next_block_state_diff().unwrap(); + + let all_txs_in_next_block = reexecution_state_readers.get_next_block_txs().unwrap(); + + let mut transaction_executor = + reexecution_state_readers.get_transaction_executor(None).unwrap(); + + transaction_executor.execute_txs(&all_txs_in_next_block); + // Finalize block and read actual statediff. + let (actual_state_diff, _, _) = + transaction_executor.finalize().expect("Couldn't finalize block"); + + // TODO(Aner): compute correct block hash at storage slot 0x1 instead of removing it. + expected_state_diff.storage_updates.shift_remove(&ContractAddress(1_u128.into())); + assert_eq!(expected_state_diff, actual_state_diff); + + println!("Reexecution test for block {block_number} passed successfully."); + } } }