Skip to content

Commit

Permalink
chore(blockifier_reexecution): refactor cli to reduce code duplication (
Browse files Browse the repository at this point in the history
#1814)

* chore(blockifier_reexecution): refactor cli to reduce code duplication

* chore(blockifier_reexecution): explicitly state command args

* chore(blockifier_reexecution): separate old block hash

* chore(blockifier_reexecution): move reexecution to seperate function

* test(blockifier_reexecution): reexecution test on blocks
  • Loading branch information
aner-starkware committed Nov 7, 2024
1 parent 7012888 commit 50ad0f4
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 146 deletions.
185 changes: 64 additions & 121 deletions crates/blockifier_reexecution/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use blockifier::abi::constants;
use blockifier_reexecution::assert_eq_state_diff;
use blockifier_reexecution::state_reader::reexecution_state_reader::ReexecutionStateReader;
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 All @@ -23,35 +23,34 @@ pub struct BlockifierReexecutionCliArgs {
command: Command,
}

#[derive(Args, Debug)]
struct SharedArgs {
/// Node url.
/// Default: https://free-rpc.nethermind.io/mainnet-juno/. Won't work for big tests.
#[clap(long, short = 'n', default_value = "https://free-rpc.nethermind.io/mainnet-juno/")]
node_url: String,

/// Block number.
#[clap(long, short = 'b')]
block_number: u64,
}

#[derive(Debug, Subcommand)]
enum Command {
/// Runs the RPC test.
RpcTest {
#[clap(flatten)]
url_and_block_number: SharedArgs,
/// Node url.
#[clap(long, short = 'n')]
node_url: String,

/// Block number.
#[clap(long, short = 'b')]
block_number: u64,
},

/// Writes the RPC queries to json files.
WriteRpcRepliesToJson {
#[clap(flatten)]
url_and_block_number: SharedArgs,
/// Node url.
#[clap(long, short = 'n')]
node_url: String,

/// Directory path to json files.
/// Default: "./crates/blockifier_reexecution/resources/block_{block_number}".
#[clap(long, default_value = None)]
directory_path: Option<String>,
/// Block number.
#[clap(long, short = 'b')]
block_number: u64,

// Directory path to json files. Default:
// "./crates/blockifier_reexecution/resources/block_{block_number}".
// TODO(Aner): add possibility to retrieve files from gc bucket.
#[clap(long, short = 'd', default_value = None)]
full_file_path: Option<String>,
},

// Reexecutes the block from JSON files.
Expand All @@ -60,10 +59,11 @@ enum Command {
#[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<String>,
// Directory path to json files. Default:
// "./crates/blockifier_reexecution/resources/block_{block_number}".
// TODO(Aner): add possibility to retrieve files from gc bucket.
#[clap(long, short = 'd', default_value = None)]
full_file_path: Option<String>,
},
}

Expand All @@ -75,46 +75,29 @@ fn main() {
let args = BlockifierReexecutionCliArgs::parse();

match args.command {
Command::RpcTest { url_and_block_number: SharedArgs { node_url, block_number } } => {
Command::RpcTest { node_url, block_number } => {
println!("Running RPC test for block number {block_number} using node url {node_url}.",);

let config = RpcStateReaderConfig {
url: node_url,
json_rpc_version: JSON_RPC_VERSION.to_string(),
};

let test_state_readers_last_and_current_block = ConsecutiveTestStateReaders::new(
reexecute_and_verify_correctness(ConsecutiveTestStateReaders::new(
BlockNumber(block_number - 1),
Some(config),
false,
);

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

let expected_state_diff =
test_state_readers_last_and_current_block.get_next_block_state_diff().unwrap();

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

// Compare the expected and actual state differences
// by avoiding discrepancies caused by insertion order
assert_eq_state_diff!(expected_state_diff, actual_state_diff);
println!("RPC test passed successfully.");
}

Command::WriteRpcRepliesToJson {
url_and_block_number: SharedArgs { node_url, block_number },
directory_path,
} => {
let directory_path = directory_path.unwrap_or(format!(
"./crates/blockifier_reexecution/resources/block_{block_number}/"
Command::WriteRpcRepliesToJson { node_url, block_number, full_file_path } => {
let full_file_path = full_file_path.unwrap_or(format!(
"./crates/blockifier_reexecution/resources/block_{block_number}/reexecution_data.\
json"
));

// TODO(Aner): refactor to reduce code duplication.
Expand All @@ -123,87 +106,47 @@ fn main() {
json_rpc_version: JSON_RPC_VERSION.to_string(),
};

let ConsecutiveTestStateReaders { last_block_state_reader, next_block_state_reader } =
let consecutive_state_readers =
ConsecutiveTestStateReaders::new(BlockNumber(block_number - 1), Some(config), true);

let block_info_next_block = next_block_state_reader.get_block_info().unwrap();

let old_block_number = BlockNumber(
block_info_next_block.block_number.0 - constants::STORED_BLOCK_HASH_BUFFER,
);

let old_block_hash =
last_block_state_reader.get_old_block_hash(old_block_number).unwrap();

let starknet_version = next_block_state_reader.get_starknet_version().unwrap();

let state_diff_next_block = next_block_state_reader.get_state_diff().unwrap();

let transactions_next_block = next_block_state_reader.get_all_txs_in_block().unwrap();
let serializable_data_next_block =
consecutive_state_readers.get_serializable_data_next_block().unwrap();

let blockifier_transactions_next_block = &last_block_state_reader
.api_txs_to_blockifier_txs_next_block(transactions_next_block.clone())
.unwrap();
let old_block_hash = consecutive_state_readers.get_old_block_hash().unwrap();

let mut transaction_executor = last_block_state_reader
.get_transaction_executor(
next_block_state_reader.get_block_context().unwrap(),
None,
)
.unwrap();

transaction_executor.execute_txs(blockifier_transactions_next_block);

let block_state = transaction_executor.block_state.unwrap();
let initial_reads = block_state.get_initial_reads().unwrap();

let contract_class_mapping =
block_state.state.get_contract_class_mapping_dumper().unwrap();

let serializable_offline_reexecution_data = SerializableOfflineReexecutionData {
state_maps: initial_reads.into(),
block_info_next_block,
starknet_version,
transactions_next_block,
contract_class_mapping,
state_diff_next_block,
old_block_hash,
// Run the reexecution test and get the state maps and contract class mapping.
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
.state
.get_contract_class_mapping_dumper()
.unwrap(),
};

serializable_offline_reexecution_data
.write_to_file(&directory_path, "reexecution_data.json")
.unwrap();
// Write the reexecution data to a json file.
SerializableOfflineReexecutionData {
serializable_data_prev_block,
serializable_data_next_block,
old_block_hash,
}
.write_to_file(&full_file_path)
.unwrap();

println!(
"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 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");
Command::ReexecuteBlock { block_number, full_file_path } => {
let full_file_path = full_file_path.unwrap_or(format!(
"./crates/blockifier_reexecution/resources/block_{block_number}/reexecution_data.\
json"
));

assert_eq!(expected_state_diff, actual_state_diff);
reexecute_and_verify_correctness(
OfflineConsecutiveStateReaders::new_from_file(&full_file_path).unwrap(),
);

println!("Reexecution test for block {block_number} passed successfully.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use starknet_gateway::rpc_objects::BlockHeader;

use crate::state_reader::compile::legacy_to_contract_class_v0;
use crate::state_reader::serde_utils::deserialize_transaction_json_to_starknet_api_tx;
use crate::state_reader::utils::ReexecutionStateMaps;
use crate::state_reader::utils::{reexecute_block_for_testing, ReexecutionStateMaps};

#[fixture]
fn block_header() -> BlockHeader {
Expand Down Expand Up @@ -162,3 +162,23 @@ fn serialize_state_maps() {
assert_eq!(serializable_state_maps, deserialized_state_maps);
assert_eq!(original_state_maps, deserialized_state_maps.try_into().unwrap());
}

#[rstest]
// TODO(Aner): Add block for each starknet version and for declare, deploy, replace_class, etc.
#[case::v_0_13_0(600001)]
#[case::v_0_13_1(620978)]
#[case::v_0_13_1_1(649367)]
#[case::v_0_13_2(685878)]
#[case::v_0_13_2_1(700000)]
#[case::invoke_with_replace_class_syscall(780008)]
#[case::invoke_with_deploy_syscall(870136)]
#[case::example_deploy_account_v1(837408)]
#[case::example_deploy_account_v3(837792)]
#[case::example_declare_v1(837461)]
#[case::example_declare_v2(822636)]
#[case::example_declare_v3(825013)]
#[case::example_l1_handler(868429)]
#[ignore = "Requires downloading JSON files prior to running; Long test, run with --release flag."]
fn test_block_reexecution(#[case] block_number: u64) {
reexecute_block_for_testing(block_number);
}
Loading

0 comments on commit 50ad0f4

Please sign in to comment.