Skip to content

Commit

Permalink
chore(blockifier_reexecution): move reexecution to seperate function
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Nov 6, 2024
1 parent 3fb2b0b commit 81b7f7e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
35 changes: 7 additions & 28 deletions crates/blockifier_reexecution/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use blockifier::state::cached_state::CachedState;
use blockifier::state::state_api::StateReader;
use blockifier_reexecution::assert_eq_state_diff;
use blockifier_reexecution::state_reader::test_state_reader::{
ConsecutiveStateReaders,
ConsecutiveTestStateReaders,
OfflineConsecutiveStateReaders,
SerializableDataPrevBlock,
SerializableOfflineReexecutionData,
};
use blockifier_reexecution::state_reader::utils::JSON_RPC_VERSION;
use blockifier_reexecution::state_reader::utils::{
reexecute_and_verify_correctness,
JSON_RPC_VERSION,
};
use clap::{Args, Parser, Subcommand};
use starknet_api::block::BlockNumber;
use starknet_gateway::config::RpcStateReaderConfig;
Expand Down Expand Up @@ -69,26 +68,6 @@ enum Command {
#[derive(Debug, Args)]
struct GlobalOptions {}

pub fn reexecution_test<S: StateReader + Send + Sync, T: ConsecutiveStateReaders<S>>(
consecutive_state_readers: T,
) -> Option<CachedState<S>> {
let expected_state_diff = consecutive_state_readers.get_next_block_state_diff().unwrap();

let all_txs_in_next_block = consecutive_state_readers.get_next_block_txs().unwrap();

let mut transaction_executor =
consecutive_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");

assert_eq_state_diff!(expected_state_diff, actual_state_diff);

transaction_executor.block_state
}

/// Main entry point of the blockifier reexecution CLI.
fn main() {
let args = BlockifierReexecutionCliArgs::parse();
Expand All @@ -102,7 +81,7 @@ fn main() {
json_rpc_version: JSON_RPC_VERSION.to_string(),
};

reexecution_test(ConsecutiveTestStateReaders::new(
reexecute_and_verify_correctness(ConsecutiveTestStateReaders::new(
BlockNumber(block_number - 1),
Some(config),
false,
Expand Down Expand Up @@ -133,7 +112,7 @@ fn main() {
let old_block_hash = consecutive_state_readers.get_old_block_hash().unwrap();

// Run the reexecution test and get the state maps and contract class mapping.
let block_state = reexecution_test(consecutive_state_readers).unwrap();
let block_state = reexecute_and_verify_correctness(consecutive_state_readers).unwrap();
let serializable_data_prev_block = SerializableDataPrevBlock {
state_maps: block_state.get_initial_reads().unwrap().into(),
contract_class_mapping: block_state
Expand Down Expand Up @@ -161,7 +140,7 @@ fn main() {
"./crates/blockifier_reexecution/resources/block_{block_number}"
)) + "/reexecution_data.json";

reexecution_test(
reexecute_and_verify_correctness(
OfflineConsecutiveStateReaders::new_from_file(&full_file_path).unwrap(),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ pub struct SerializableOfflineReexecutionData {
impl SerializableOfflineReexecutionData {
pub fn write_to_file(&self, file_path: &str, file_name: &str) -> ReexecutionResult<()> {
fs::create_dir_all(file_path)
.unwrap_or_else(|_| panic!("Failed to create directory {file_path}."));
.unwrap_or_else(|err| panic!("Failed to create directory {file_path}. Error: {err}"));
let full_file_path = file_path.to_owned() + "/" + file_name;
fs::write(full_file_path.clone(), serde_json::to_string_pretty(&self)?)
.unwrap_or_else(|_| panic!("Failed to write to file {full_file_path}."));
.unwrap_or_else(|err| panic!("Failed to write to file {full_file_path}. Error: {err}"));
Ok(())
}

pub fn read_from_file(full_file_path: &str) -> ReexecutionResult<Self> {
let file_content = fs::read_to_string(full_file_path).unwrap_or_else(|_| {
panic!("Failed to read reexecution data from file {full_file_path}.")
let file_content = fs::read_to_string(full_file_path).unwrap_or_else(|err| {
panic!("Failed to read reexecution data from file {full_file_path}. Error: {err}")
});
Ok(serde_json::from_str(&file_content)?)
}
Expand Down
50 changes: 43 additions & 7 deletions crates/blockifier_reexecution/src/state_reader/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::{BTreeMap, HashMap};

use blockifier::context::{ChainInfo, FeeTokenAddresses};
use blockifier::state::cached_state::{CommitmentStateDiff, StateMaps};
use blockifier::state::cached_state::{CachedState, CommitmentStateDiff, StateMaps};
use blockifier::state::state_api::StateReader;
use indexmap::IndexMap;
use papyrus_execution::{ETH_FEE_CONTRACT_ADDRESS, STRK_FEE_CONTRACT_ADDRESS};
use pretty_assertions::assert_eq;
Expand All @@ -13,7 +14,12 @@ use starknet_api::test_utils::read_json_file;
use starknet_gateway::config::RpcStateReaderConfig;
use starknet_types_core::felt::Felt;

use crate::assert_eq_state_diff;
use crate::state_reader::errors::ReexecutionError;
use crate::state_reader::test_state_reader::{
ConsecutiveStateReaders,
OfflineConsecutiveStateReaders,
};

pub const RPC_NODE_URL: &str = "https://free-rpc.nethermind.io/mainnet-juno/";
pub const JSON_RPC_VERSION: &str = "2.0";
Expand Down Expand Up @@ -166,17 +172,47 @@ impl From<CommitmentStateDiff> for ComparableStateDiff {
}
}

pub fn reexecute_and_verify_correctness<
S: StateReader + Send + Sync,
T: ConsecutiveStateReaders<S>,
>(
consecutive_state_readers: T,
) -> Option<CachedState<S>> {
let expected_state_diff = consecutive_state_readers.get_next_block_state_diff().unwrap();

let all_txs_in_next_block = consecutive_state_readers.get_next_block_txs().unwrap();

let mut transaction_executor =
consecutive_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");

assert_eq_state_diff!(expected_state_diff, actual_state_diff);

transaction_executor.block_state
}

pub fn reexecute_block_for_testing(block_number: u64) {
// In tests we are already in the blockifier_reexecution directory.
let full_file_path = format!("./resources/block_{block_number}/reexecution_data.json");

reexecute_and_verify_correctness(
OfflineConsecutiveStateReaders::new_from_file(&full_file_path).unwrap(),
);

println!("Reexecution test for block {block_number} passed successfully.");
}

/// Asserts equality between two `CommitmentStateDiff` structs, ignoring insertion order.
#[macro_export]
macro_rules! assert_eq_state_diff {
($expected_state_diff:expr, $actual_state_diff:expr $(,)?) => {
pretty_assertions::assert_eq!(
blockifier_reexecution::state_reader::utils::ComparableStateDiff::from(
$expected_state_diff,
),
blockifier_reexecution::state_reader::utils::ComparableStateDiff::from(
$actual_state_diff,
),
$crate::state_reader::utils::ComparableStateDiff::from($expected_state_diff,),
$crate::state_reader::utils::ComparableStateDiff::from($actual_state_diff,),
"Expected and actual state diffs do not match.",
);
};
Expand Down

0 comments on commit 81b7f7e

Please sign in to comment.