From da371da01fbd5c6a95f4717831bcc45a25b99fc8 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Tue, 12 Nov 2024 10:06:58 +0200 Subject: [PATCH] refactor(blockifier_reexecution): set chain ID by inspecting RPC URL --- crates/blockifier_reexecution/src/main.rs | 14 ++++++------- .../src/state_reader/errors.rs | 2 ++ .../src/state_reader/rpc_https_test.rs | 20 +++++++++++++++---- .../src/state_reader/utils.rs | 15 ++++++++++++++ 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/crates/blockifier_reexecution/src/main.rs b/crates/blockifier_reexecution/src/main.rs index c5c847143b..ec72b53f8a 100644 --- a/crates/blockifier_reexecution/src/main.rs +++ b/crates/blockifier_reexecution/src/main.rs @@ -4,13 +4,13 @@ use blockifier_reexecution::state_reader::test_state_reader::{ }; use blockifier_reexecution::state_reader::utils::{ get_block_numbers_for_reexecution, + guess_chain_id_from_node_url, reexecute_and_verify_correctness, write_block_reexecution_data_to_file, JSON_RPC_VERSION, }; use clap::{Args, Parser, Subcommand}; use starknet_api::block::BlockNumber; -use starknet_api::core::ChainId; use starknet_gateway::config::RpcStateReaderConfig; /// BlockifierReexecution CLI. @@ -120,7 +120,7 @@ async fn main() { println!("Running RPC test for block number {block_number} using node url {node_url}.",); let config = RpcStateReaderConfig { - url: node_url, + url: node_url.clone(), json_rpc_version: JSON_RPC_VERSION.to_string(), }; @@ -131,7 +131,7 @@ async fn main() { reexecute_and_verify_correctness(ConsecutiveTestStateReaders::new( BlockNumber(block_number - 1), Some(config), - ChainId::Mainnet, + guess_chain_id_from_node_url(node_url.as_str()).unwrap(), false, )) }) @@ -156,8 +156,8 @@ async fn main() { write_block_reexecution_data_to_file( BlockNumber(block_number), full_file_path, - node_url, - ChainId::Mainnet, + node_url.clone(), + guess_chain_id_from_node_url(node_url.as_str()).unwrap(), ); }) .await @@ -185,8 +185,8 @@ async fn main() { write_block_reexecution_data_to_file( block_number, full_file_path, - node_url, - ChainId::Mainnet, + node_url.clone(), + guess_chain_id_from_node_url(node_url.as_str()).unwrap(), ) }) .await diff --git a/crates/blockifier_reexecution/src/state_reader/errors.rs b/crates/blockifier_reexecution/src/state_reader/errors.rs index e2d5a0c1dc..62068142c9 100644 --- a/crates/blockifier_reexecution/src/state_reader/errors.rs +++ b/crates/blockifier_reexecution/src/state_reader/errors.rs @@ -9,6 +9,8 @@ use thiserror::Error; #[derive(Debug, Error)] #[allow(clippy::enum_variant_names)] pub enum ReexecutionError { + #[error("Cannot discern chain ID from URL: {0}")] + AmbiguousChainIdFromUrl(String), #[error(transparent)] Rpc(#[from] RPCStateReaderError), #[error(transparent)] diff --git a/crates/blockifier_reexecution/src/state_reader/rpc_https_test.rs b/crates/blockifier_reexecution/src/state_reader/rpc_https_test.rs index 959393a19f..d248d1155a 100644 --- a/crates/blockifier_reexecution/src/state_reader/rpc_https_test.rs +++ b/crates/blockifier_reexecution/src/state_reader/rpc_https_test.rs @@ -34,6 +34,7 @@ use super::utils::RPC_NODE_URL; use crate::state_reader::compile::legacy_to_contract_class_v0; use crate::state_reader::reexecution_state_reader::ReexecutionStateReader; use crate::state_reader::test_state_reader::{ConsecutiveTestStateReaders, TestStateReader}; +use crate::state_reader::utils::guess_chain_id_from_node_url; const EXAMPLE_INVOKE_TX_HASH: &str = "0xa7c7db686c7f756ceb7ca85a759caef879d425d156da83d6a836f86851983"; @@ -87,14 +88,24 @@ pub fn get_test_rpc_config() -> RpcStateReaderConfig { } #[fixture] -pub fn test_state_reader() -> TestStateReader { +pub fn test_url() -> String { + get_test_url() +} + +#[fixture] +pub fn test_chain_id(test_url: String) -> ChainId { + guess_chain_id_from_node_url(test_url.as_str()).unwrap() +} + +#[fixture] +pub fn test_state_reader(test_chain_id: ChainId) -> TestStateReader { TestStateReader { rpc_state_reader: RpcStateReader { config: get_test_rpc_config(), block_id: get_test_block_id(), }, retry_config: RetryConfig::default(), - chain_id: ChainId::Mainnet, + chain_id: test_chain_id, contract_class_mapping_dumper: Arc::new(Mutex::new(None)), } } @@ -112,8 +123,9 @@ pub fn last_constructed_block(test_block_number: BlockNumber) -> BlockNumber { #[fixture] pub fn test_state_readers_last_and_current_block( last_constructed_block: BlockNumber, + test_chain_id: ChainId, ) -> ConsecutiveTestStateReaders { - ConsecutiveTestStateReaders::new(last_constructed_block, None, ChainId::Mainnet, false) + ConsecutiveTestStateReaders::new(last_constructed_block, None, test_chain_id, false) } /// Test that the block info can be retrieved from the RPC server. @@ -232,7 +244,7 @@ pub fn test_get_statediff_rpc(test_state_reader: TestStateReader) { } #[rstest] -#[case(test_block_number(test_state_reader()).0)] +#[case(test_block_number(test_state_reader(test_chain_id(test_url()))).0)] #[case(EXAMPLE_DECLARE_V1_BLOCK_NUMBER)] #[case(EXAMPLE_DECLARE_V2_BLOCK_NUMBER)] #[case(EXAMPLE_DECLARE_V3_BLOCK_NUMBER)] diff --git a/crates/blockifier_reexecution/src/state_reader/utils.rs b/crates/blockifier_reexecution/src/state_reader/utils.rs index 342c8a7cfc..8a6d55801a 100644 --- a/crates/blockifier_reexecution/src/state_reader/utils.rs +++ b/crates/blockifier_reexecution/src/state_reader/utils.rs @@ -23,6 +23,7 @@ use crate::state_reader::test_state_reader::{ ConsecutiveStateReaders, ConsecutiveTestStateReaders, OfflineConsecutiveStateReaders, + ReexecutionResult, SerializableDataPrevBlock, SerializableOfflineReexecutionData, }; @@ -33,6 +34,20 @@ pub static RPC_NODE_URL: LazyLock = LazyLock::new(|| { }); pub const JSON_RPC_VERSION: &str = "2.0"; +pub fn guess_chain_id_from_node_url(node_url: &str) -> ReexecutionResult { + match ( + node_url.contains("mainnet"), + node_url.contains("sepolia"), + node_url.contains("integration"), + ) { + (true, false, false) => Ok(ChainId::Mainnet), + (false, true, false) => Ok(ChainId::Sepolia), + // Integration URLs may contain the word "sepolia". + (false, _, true) => Ok(ChainId::IntegrationSepolia), + _ => Err(ReexecutionError::AmbiguousChainIdFromUrl(node_url.to_string())), + } +} + /// Returns the fee token addresses of mainnet. pub fn get_fee_token_addresses(chain_id: &ChainId) -> FeeTokenAddresses { match chain_id {