Skip to content

Commit 61dd459

Browse files
blockifier_reexecution: move write_block reexecution data to file (#10708)
1 parent 132980e commit 61dd459

File tree

3 files changed

+58
-72
lines changed

3 files changed

+58
-72
lines changed

crates/blockifier_reexecution/src/main.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use blockifier_reexecution::state_reader::cli::{
1414
use blockifier_reexecution::state_reader::offline_state_reader::OfflineConsecutiveStateReaders;
1515
use blockifier_reexecution::state_reader::reexecution_state_reader::ConsecutiveReexecutionStateReaders;
1616
use blockifier_reexecution::state_reader::test_state_reader::ConsecutiveTestStateReaders;
17-
use blockifier_reexecution::state_reader::utils::write_block_reexecution_data_to_file;
1817
use clap::Parser;
1918
use google_cloud_storage::client::{Client, ClientConfig};
2019
use google_cloud_storage::http::objects::download::Range;
@@ -136,16 +135,18 @@ async fn main() {
136135
// RPC calls are "synchronous IO" (see, e.g., https://stackoverflow.com/questions/74547541/when-should-you-use-tokios-spawn-blocking)
137136
// for details), so should be executed in a blocking thread.
138137
// TODO(Aner): make only the RPC calls blocking, not the whole function.
138+
let rpc_state_reader_config = RpcStateReaderConfig::from_url(node_url);
139139
task_set.spawn(async move {
140140
println!("Computing reexecution data for block {block_number}.");
141141
tokio::task::spawn_blocking(move || {
142-
write_block_reexecution_data_to_file(
143-
block_number,
144-
full_file_path,
145-
node_url,
142+
ConsecutiveTestStateReaders::new(
143+
block_number.prev().expect("Should not run with block 0"),
144+
Some(rpc_state_reader_config),
146145
chain_id,
146+
true,
147147
contract_class_manager,
148148
)
149+
.write_block_reexecution_data_to_file(&full_file_path)
149150
})
150151
.await
151152
});

crates/blockifier_reexecution/src/state_reader/test_state_reader.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ use crate::state_reader::compile::{
4848
sierra_to_versioned_contract_class_v1,
4949
};
5050
use crate::state_reader::errors::ReexecutionResult;
51-
use crate::state_reader::offline_state_reader::SerializableDataNextBlock;
51+
use crate::state_reader::offline_state_reader::{
52+
SerializableDataNextBlock,
53+
SerializableDataPrevBlock,
54+
SerializableOfflineReexecutionData,
55+
};
5256
use crate::state_reader::reexecution_state_reader::{
5357
ConsecutiveReexecutionStateReaders,
5458
ReexecutionStateReader,
@@ -62,6 +66,7 @@ use crate::state_reader::utils::{
6266
disjoint_hashmap_union,
6367
get_chain_info,
6468
get_rpc_state_reader_config,
69+
ComparableStateDiff,
6570
};
6671

6772
pub const DEFAULT_RETRY_COUNT: usize = 3;
@@ -521,6 +526,51 @@ impl ConsecutiveTestStateReaders {
521526

522527
Ok(())
523528
}
529+
530+
/// Writes the reexecution data required to reexecute a block to a JSON file.
531+
pub fn write_block_reexecution_data_to_file(self, full_file_path: &str) {
532+
let chain_id = self.next_block_state_reader.chain_id.clone();
533+
let block_number = self.next_block_state_reader.get_block_info().unwrap().block_number;
534+
535+
let serializable_data_next_block = self.get_serializable_data_next_block().unwrap();
536+
537+
let old_block_hash = self.get_old_block_hash().unwrap();
538+
539+
// Run the reexecution and get the state maps and contract class mapping.
540+
let (block_state, expected_state_diff, actual_state_diff) = self.reexecute_block();
541+
542+
// Warn if state diffs don't match, but continue writing the file.
543+
let expected_comparable = ComparableStateDiff::from(expected_state_diff);
544+
let actual_comparable = ComparableStateDiff::from(actual_state_diff);
545+
if expected_comparable != actual_comparable {
546+
println!(
547+
"WARNING: State diff mismatch for block {block_number}. Expected and actual state \
548+
diffs do not match."
549+
);
550+
}
551+
552+
let block_state = block_state.unwrap();
553+
let serializable_data_prev_block = SerializableDataPrevBlock {
554+
state_maps: block_state.get_initial_reads().unwrap().into(),
555+
contract_class_mapping: block_state
556+
.state
557+
.state_reader
558+
.get_contract_class_mapping_dumper()
559+
.unwrap(),
560+
};
561+
562+
// Write the reexecution data to a json file.
563+
SerializableOfflineReexecutionData {
564+
serializable_data_prev_block,
565+
serializable_data_next_block,
566+
chain_id,
567+
old_block_hash,
568+
}
569+
.write_to_file(full_file_path)
570+
.unwrap();
571+
572+
println!("RPC replies required for reexecuting block {block_number} written to json file.");
573+
}
524574
}
525575

526576
impl ConsecutiveReexecutionStateReaders<StateReaderAndContractManager<TestStateReader>>

crates/blockifier_reexecution/src/state_reader/utils.rs

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,14 @@ use blockifier::state::state_api::StateResult;
1414
use indexmap::IndexMap;
1515
use pretty_assertions::assert_eq;
1616
use serde::{Deserialize, Serialize};
17-
use starknet_api::block::BlockNumber;
1817
use starknet_api::contract_class::ContractClass;
1918
use starknet_api::core::{ChainId, ClassHash, CompiledClassHash, ContractAddress, Nonce};
2019
use starknet_api::state::{SierraContractClass, StorageKey};
2120
use starknet_types_core::felt::Felt;
2221

2322
use crate::state_reader::errors::ReexecutionError;
24-
use crate::state_reader::offline_state_reader::{
25-
OfflineConsecutiveStateReaders,
26-
SerializableDataPrevBlock,
27-
SerializableOfflineReexecutionData,
28-
};
23+
use crate::state_reader::offline_state_reader::OfflineConsecutiveStateReaders;
2924
use crate::state_reader::reexecution_state_reader::ConsecutiveReexecutionStateReaders;
30-
use crate::state_reader::test_state_reader::ConsecutiveTestStateReaders;
3125

3226
pub static RPC_NODE_URL: LazyLock<String> = LazyLock::new(|| {
3327
env::var("TEST_URL")
@@ -246,65 +240,6 @@ pub fn reexecute_block_for_testing(block_number: u64) {
246240
println!("Reexecution test for block {block_number} passed successfully.");
247241
}
248242

249-
pub fn write_block_reexecution_data_to_file(
250-
block_number: BlockNumber,
251-
full_file_path: String,
252-
node_url: String,
253-
chain_id: ChainId,
254-
contract_class_manager: ContractClassManager,
255-
) {
256-
let config = RpcStateReaderConfig::from_url(node_url);
257-
258-
let consecutive_state_readers = ConsecutiveTestStateReaders::new(
259-
block_number.prev().expect("Should not run with block 0"),
260-
Some(config),
261-
chain_id.clone(),
262-
true,
263-
contract_class_manager,
264-
);
265-
266-
let serializable_data_next_block =
267-
consecutive_state_readers.get_serializable_data_next_block().unwrap();
268-
269-
let old_block_hash = consecutive_state_readers.get_old_block_hash().unwrap();
270-
271-
// Run the reexecution and get the state maps and contract class mapping.
272-
let (block_state, expected_state_diff, actual_state_diff) =
273-
consecutive_state_readers.reexecute_block();
274-
275-
// Warn if state diffs don't match, but continue writing the file.
276-
let expected_comparable = ComparableStateDiff::from(expected_state_diff);
277-
let actual_comparable = ComparableStateDiff::from(actual_state_diff);
278-
if expected_comparable != actual_comparable {
279-
println!(
280-
"WARNING: State diff mismatch for block {block_number}. Expected and actual state \
281-
diffs do not match."
282-
);
283-
}
284-
285-
let block_state = block_state.unwrap();
286-
let serializable_data_prev_block = SerializableDataPrevBlock {
287-
state_maps: block_state.get_initial_reads().unwrap().into(),
288-
contract_class_mapping: block_state
289-
.state
290-
.state_reader
291-
.get_contract_class_mapping_dumper()
292-
.unwrap(),
293-
};
294-
295-
// Write the reexecution data to a json file.
296-
SerializableOfflineReexecutionData {
297-
serializable_data_prev_block,
298-
serializable_data_next_block,
299-
chain_id,
300-
old_block_hash,
301-
}
302-
.write_to_file(&full_file_path)
303-
.unwrap();
304-
305-
println!("RPC replies required for reexecuting block {block_number} written to json file.");
306-
}
307-
308243
/// Asserts equality between two `CommitmentStateDiff` structs, ignoring insertion order.
309244
#[macro_export]
310245
macro_rules! assert_eq_state_diff {

0 commit comments

Comments
 (0)