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 12, 2024
1 parent 716bed2 commit 79580d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
10 changes: 8 additions & 2 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 @@ -86,6 +87,7 @@ fn main() {
reexecute_and_verify_correctness(ConsecutiveTestStateReaders::new(
BlockNumber(block_number - 1),
Some(config),
ChainId::Mainnet,
false,
));

Expand All @@ -106,8 +108,12 @@ fn main() {
json_rpc_version: JSON_RPC_VERSION.to_string(),
};

let consecutive_state_readers =
ConsecutiveTestStateReaders::new(BlockNumber(block_number - 1), Some(config), true);
let consecutive_state_readers = ConsecutiveTestStateReaders::new(
BlockNumber(block_number - 1),
Some(config),
ChainId::Mainnet,
true,
);

let serializable_data_next_block =
consecutive_state_readers.get_serializable_data_next_block().unwrap();
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 @@ -169,6 +169,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 @@ -178,6 +179,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 @@ -229,20 +231,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 @@ -327,7 +335,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 @@ -461,17 +469,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"),
false,
),
Expand Down

0 comments on commit 79580d0

Please sign in to comment.