From f729381fddf846b287340e1188657c474e82e029 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 | 11 ++++++----- .../src/state_reader/errors.rs | 2 ++ .../src/state_reader/utils.rs | 13 +++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/crates/blockifier_reexecution/src/main.rs b/crates/blockifier_reexecution/src/main.rs index 0010237f33d..261994643d6 100644 --- a/crates/blockifier_reexecution/src/main.rs +++ b/crates/blockifier_reexecution/src/main.rs @@ -5,12 +5,12 @@ use blockifier_reexecution::state_reader::test_state_reader::{ SerializableOfflineReexecutionData, }; use blockifier_reexecution::state_reader::utils::{ + guess_chain_id_from_node_url, reexecute_and_verify_correctness, 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. @@ -80,14 +80,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, )); @@ -101,6 +101,7 @@ fn main() { "./crates/blockifier_reexecution/resources/block_{block_number}/reexecution_data.\ json" )); + let chain_id = guess_chain_id_from_node_url(node_url.as_str()).unwrap(); // TODO(Aner): refactor to reduce code duplication. let config = RpcStateReaderConfig { @@ -111,7 +112,7 @@ fn main() { let consecutive_state_readers = ConsecutiveTestStateReaders::new( BlockNumber(block_number - 1), Some(config), - ChainId::Mainnet, + chain_id.clone(), true, ); @@ -134,7 +135,7 @@ fn main() { SerializableOfflineReexecutionData { serializable_data_prev_block, serializable_data_next_block, - chain_id: ChainId::Mainnet, + chain_id, old_block_hash, } .write_to_file(&full_file_path) diff --git a/crates/blockifier_reexecution/src/state_reader/errors.rs b/crates/blockifier_reexecution/src/state_reader/errors.rs index 8d15f1de1c0..95ba3ff6d77 100644 --- a/crates/blockifier_reexecution/src/state_reader/errors.rs +++ b/crates/blockifier_reexecution/src/state_reader/errors.rs @@ -10,6 +10,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/utils.rs b/crates/blockifier_reexecution/src/state_reader/utils.rs index f10ba3afe5a..491f8b2c592 100644 --- a/crates/blockifier_reexecution/src/state_reader/utils.rs +++ b/crates/blockifier_reexecution/src/state_reader/utils.rs @@ -19,11 +19,24 @@ use crate::state_reader::errors::ReexecutionError; use crate::state_reader::test_state_reader::{ ConsecutiveStateReaders, OfflineConsecutiveStateReaders, + ReexecutionResult, }; 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 { + if node_url.contains("mainnet") { + Ok(ChainId::Mainnet) + } else if node_url.contains("sepolia") && !node_url.contains("integration") { + Ok(ChainId::Sepolia) + } else if node_url.contains("integration") { + Ok(ChainId::IntegrationSepolia) + } else { + 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 {