From 174825abe3cdb97f993dfd404e7e6d5cabf3d417 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 | 12 +++++------ .../src/state_reader/errors.rs | 2 ++ .../src/state_reader/rpc_https_test.rs | 20 +++++++++++++++---- .../src/state_reader/utils.rs | 15 ++++++++++++++ 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/crates/blockifier_reexecution/src/main.rs b/crates/blockifier_reexecution/src/main.rs index 8efd169f74..b1a15bd6a8 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. @@ -99,14 +99,14 @@ 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(), }; 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, )); @@ -124,8 +124,8 @@ 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(), ); } @@ -149,7 +149,7 @@ fn main() { block_number, &full_file_path, node_url.clone(), - ChainId::Mainnet, + guess_chain_id_from_node_url(node_url.as_str()).unwrap(), ); } } 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 f2a47f3496..6e27718af3 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 40c94eb945..a6e90d8b87 100644 --- a/crates/blockifier_reexecution/src/state_reader/utils.rs +++ b/crates/blockifier_reexecution/src/state_reader/utils.rs @@ -21,6 +21,7 @@ use crate::state_reader::test_state_reader::{ ConsecutiveStateReaders, ConsecutiveTestStateReaders, OfflineConsecutiveStateReaders, + ReexecutionResult, SerializableDataPrevBlock, SerializableOfflineReexecutionData, }; @@ -28,6 +29,20 @@ use crate::state_reader::test_state_reader::{ pub const RPC_NODE_URL: &str = "https://free-rpc.nethermind.io/mainnet-juno/"; 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 {