Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(blockifier_reexecution): set chain ID by inspecting RPC URL #1973

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 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 @@ -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(),
};

Expand All @@ -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,
))
})
Expand All @@ -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
Expand Down Expand Up @@ -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
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 @@ -23,6 +23,7 @@ use crate::state_reader::test_state_reader::{
ConsecutiveStateReaders,
ConsecutiveTestStateReaders,
OfflineConsecutiveStateReaders,
ReexecutionResult,
SerializableDataPrevBlock,
SerializableOfflineReexecutionData,
};
Expand All @@ -33,6 +34,20 @@ pub static RPC_NODE_URL: LazyLock<String> = LazyLock::new(|| {
});
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
Loading