Skip to content

Commit

Permalink
refactor(blockifier_reexecution): add chain ID to TestStateReader
Browse files Browse the repository at this point in the history
  • Loading branch information
dorimedini-starkware committed Nov 13, 2024
1 parent b7f1e3a commit 76e86ad
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
4 changes: 4 additions & 0 deletions crates/blockifier_reexecution/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use blockifier_reexecution::state_reader::utils::{
};
use clap::{Args, Parser, Subcommand};
use starknet_api::block::BlockNumber;
use starknet_api::core::ChainId;
use starknet_gateway::config::RpcStateReaderConfig;

/// BlockifierReexecution CLI.
Expand Down Expand Up @@ -105,6 +106,7 @@ fn main() {
reexecute_and_verify_correctness(ConsecutiveTestStateReaders::new(
BlockNumber(block_number - 1),
Some(config),
ChainId::Mainnet,
false,
));

Expand All @@ -123,6 +125,7 @@ fn main() {
BlockNumber(block_number),
&full_file_path,
node_url,
ChainId::Mainnet,
);
}

Expand All @@ -146,6 +149,7 @@ fn main() {
block_number,
&full_file_path,
node_url.clone(),
ChainId::Mainnet,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use blockifier::blockifier::block::BlockInfo;
use rstest::{fixture, rstest};
use starknet_api::block::BlockNumber;
use starknet_api::class_hash;
use starknet_api::core::ChainId;
use starknet_api::transaction::{
DeclareTransaction,
DeployAccountTransaction,
Expand Down Expand Up @@ -93,6 +94,7 @@ pub fn test_state_reader() -> TestStateReader {
block_id: get_test_block_id(),
},
retry_config: RetryConfig::default(),
chain_id: ChainId::Mainnet,
contract_class_mapping_dumper: Arc::new(Mutex::new(None)),
}
}
Expand All @@ -111,7 +113,7 @@ pub fn last_constructed_block(test_block_number: BlockNumber) -> BlockNumber {
pub fn test_state_readers_last_and_current_block(
last_constructed_block: BlockNumber,
) -> ConsecutiveTestStateReaders {
ConsecutiveTestStateReaders::new(last_constructed_block, None, false)
ConsecutiveTestStateReaders::new(last_constructed_block, None, ChainId::Mainnet, false)
}

/// Test that the block info can be retrieved from the RPC server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ impl Default for RetryConfig {
pub struct TestStateReader {
pub(crate) rpc_state_reader: RpcStateReader,
pub(crate) retry_config: RetryConfig,
pub(crate) chain_id: ChainId,
#[allow(dead_code)]
pub(crate) contract_class_mapping_dumper: Arc<Mutex<Option<StarknetContractClassMapping>>>,
}
Expand All @@ -185,6 +186,7 @@ impl Default for TestStateReader {
Self {
rpc_state_reader: RpcStateReader::from_latest(&get_rpc_state_reader_config()),
retry_config: RetryConfig::default(),
chain_id: ChainId::Mainnet,
contract_class_mapping_dumper: Arc::new(Mutex::new(None)),
}
}
Expand Down Expand Up @@ -236,20 +238,26 @@ impl StateReader for TestStateReader {
}

impl TestStateReader {
pub fn new(config: &RpcStateReaderConfig, block_number: BlockNumber, dump_mode: bool) -> Self {
pub fn new(
config: &RpcStateReaderConfig,
chain_id: ChainId,
block_number: BlockNumber,
dump_mode: bool,
) -> Self {
let contract_class_mapping_dumper = Arc::new(Mutex::new(match dump_mode {
true => Some(HashMap::new()),
false => None,
}));
Self {
rpc_state_reader: RpcStateReader::from_number(config, block_number),
contract_class_mapping_dumper,
retry_config: RetryConfig::default(),
chain_id,
contract_class_mapping_dumper,
}
}

pub fn new_for_testing(block_number: BlockNumber) -> Self {
TestStateReader::new(&get_rpc_state_reader_config(), block_number, false)
TestStateReader::new(&get_rpc_state_reader_config(), ChainId::Mainnet, block_number, false)
}

/// Get the block info of the current block.
Expand Down Expand Up @@ -334,7 +342,7 @@ impl TestStateReader {
pub fn get_block_context(&self) -> ReexecutionResult<BlockContext> {
Ok(BlockContext::new(
self.get_block_info()?,
get_chain_info(&ChainId::Mainnet),
get_chain_info(&self.chain_id),
self.get_versioned_constants()?.clone(),
BouncerConfig::max(),
))
Expand Down Expand Up @@ -468,17 +476,20 @@ impl ConsecutiveTestStateReaders {
pub fn new(
last_constructed_block_number: BlockNumber,
config: Option<RpcStateReaderConfig>,
chain_id: ChainId,
dump_mode: bool,
) -> Self {
let config = config.unwrap_or(get_rpc_state_reader_config());
Self {
last_block_state_reader: TestStateReader::new(
&config,
chain_id.clone(),
last_constructed_block_number,
dump_mode,
),
next_block_state_reader: TestStateReader::new(
&config,
chain_id,
last_constructed_block_number.next().expect("Overflow in block number"),
dump_mode,
),
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier_reexecution/src/state_reader/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,15 @@ pub fn write_block_reexecution_data_to_file(
block_number: BlockNumber,
full_file_path: &str,
node_url: String,
chain_id: ChainId,
) {
let config =
RpcStateReaderConfig { url: node_url, json_rpc_version: JSON_RPC_VERSION.to_string() };

let consecutive_state_readers = ConsecutiveTestStateReaders::new(
block_number.prev().expect("Should not run with block 0"),
Some(config),
chain_id,
true,
);

Expand Down

0 comments on commit 76e86ad

Please sign in to comment.