Skip to content

Commit

Permalink
refactor(blockifier_reexecution): set chain ID by inspecting RPC URL
Browse files Browse the repository at this point in the history
  • Loading branch information
dorimedini-starkware committed Nov 13, 2024
1 parent 141a523 commit 78e7e2b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
12 changes: 6 additions & 6 deletions crates/blockifier_reexecution/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
));

Expand All @@ -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(),
);
}

Expand All @@ -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(),
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier_reexecution/src/state_reader/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
20 changes: 16 additions & 4 deletions crates/blockifier_reexecution/src/state_reader/rpc_https_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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)),
}
}
Expand All @@ -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.
Expand Down Expand Up @@ -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)]
Expand Down
15 changes: 15 additions & 0 deletions crates/blockifier_reexecution/src/state_reader/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,28 @@ use crate::state_reader::test_state_reader::{
ConsecutiveStateReaders,
ConsecutiveTestStateReaders,
OfflineConsecutiveStateReaders,
ReexecutionResult,
SerializableDataPrevBlock,
SerializableOfflineReexecutionData,
};

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<ChainId> {
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 {
Expand Down

0 comments on commit 78e7e2b

Please sign in to comment.