Skip to content

Commit

Permalink
build: add more utils to regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Sep 23, 2024
1 parent a0fce57 commit efe0b06
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/blockifier_regression_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license-file.workspace = true
[dependencies]
blockifier = { workspace = true }
papyrus_execution = { workspace = true }
serde_json.workspace = true
starknet-types-core.workspace = true
starknet_api = { workspace = true }
starknet_gateway = { workspace = true }
Expand Down
63 changes: 60 additions & 3 deletions crates/blockifier_regression_test/src/test_state_reader.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
use blockifier::blockifier::block::BlockInfo;
use blockifier::blockifier::config::TransactionExecutorConfig;
use blockifier::blockifier::transaction_executor::TransactionExecutor;
use blockifier::bouncer::BouncerConfig;
use blockifier::context::BlockContext;
use blockifier::execution::contract_class::ContractClass;
use blockifier::state::cached_state::CachedState;
use blockifier::state::errors::StateError;
use blockifier::state::state_api::{StateReader, StateResult};
use starknet_api::block::BlockNumber;
use blockifier::versioned_constants::{StarknetVersion, VersionedConstants};
use starknet_api::block::{BlockHeader, BlockNumber};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::state::StorageKey;
use starknet_gateway::config::RpcStateReaderConfig;
use starknet_gateway::errors::serde_err_to_state_err;
use starknet_gateway::rpc_objects::GetBlockWithTxHashesParams;
use starknet_gateway::rpc_state_reader::RpcStateReader;
use starknet_gateway::state_reader::MempoolStateReader;
use starknet_types_core::felt::Felt;

use crate::test_utils::{get_chain_info, get_rpc_state_reader_config};

pub struct TestStateReader(RpcStateReader);

impl StateReader for TestStateReader {
Expand Down Expand Up @@ -38,11 +49,57 @@ impl StateReader for TestStateReader {
}

impl TestStateReader {
pub fn new(config: &RpcStateReaderConfig, block_number: BlockNumber) -> Self {
Self(RpcStateReader::from_number(config, block_number))
pub fn new(config: Option<&RpcStateReaderConfig>, block_number: BlockNumber) -> Self {
match config {
Some(config) => Self(RpcStateReader::from_number(config, block_number)),
None => Self(RpcStateReader::from_number(&get_rpc_state_reader_config(), block_number)),
}
}

pub fn get_block_info(&self) -> StateResult<BlockInfo> {
self.0.get_block_info()
}

pub fn get_starknet_version(&self) -> StateResult<StarknetVersion> {
let get_block_params = GetBlockWithTxHashesParams { block_id: self.0.block_id };
let block_header: BlockHeader = serde_json::from_value(
self.0.send_rpc_request("starknet_getBlockWithTxHashes", get_block_params)?,
)
.map_err(serde_err_to_state_err)?;
let raw_version = block_header.starknet_version.0.as_slice();
match raw_version {
[0, 13, 0] => Ok(StarknetVersion::V0_13_0),
[0, 13, 1] => Ok(StarknetVersion::V0_13_1),
[0, 13, 1, 1] => Ok(StarknetVersion::V0_13_1_1),
[0, 13, 2] => Ok(StarknetVersion::V0_13_2),
[0, 13, 2, 1] => Ok(StarknetVersion::V0_13_2_1),
_ => Err(StateError::StateReadError("Failed to match starknet version".to_string())),
}
}

pub fn get_versioned_constants(&self) -> StateResult<&'static VersionedConstants> {
let starknet_version = self.get_starknet_version()?;
let versioned_constants: &'static VersionedConstants = starknet_version.into();
Ok(versioned_constants)
}

pub fn get_block_context(&self) -> StateResult<BlockContext> {
Ok(BlockContext::new(
self.get_block_info()?,
get_chain_info(),
self.get_versioned_constants()?.clone(),
BouncerConfig::max(),
))
}

pub fn get_transaction_executor(
test_state_reader: TestStateReader,
) -> StateResult<TransactionExecutor<TestStateReader>> {
let block_context = test_state_reader.get_block_context()?;
Ok(TransactionExecutor::<TestStateReader>::new(
CachedState::new(test_state_reader),
block_context,
TransactionExecutorConfig::default(),
))
}
}
26 changes: 19 additions & 7 deletions crates/blockifier_regression_test/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
use blockifier::context::FeeTokenAddresses;
use starknet_api::contract_address;
use starknet_api::core::ContractAddress;
use starknet_api::patricia_key;
use starknet_api::core::PatriciaKey;
use starknet_api::felt;
use papyrus_execution::{ETH_FEE_CONTRACT_ADDRESS,STRK_FEE_CONTRACT_ADDRESS};
use blockifier::context::{ChainInfo, FeeTokenAddresses};
use papyrus_execution::{ETH_FEE_CONTRACT_ADDRESS, STRK_FEE_CONTRACT_ADDRESS};
use starknet_api::core::{ChainId, ContractAddress, PatriciaKey};
use starknet_api::{contract_address, felt, patricia_key};
use starknet_gateway::config::RpcStateReaderConfig;

pub const RPC_NODE_URL: &str = "https://free-rpc.nethermind.io/mainnet-juno/";
pub const JSON_RPC_VERSION: &str = "2.0";

pub fn get_fee_token_addresses() -> FeeTokenAddresses {
FeeTokenAddresses {
strk_fee_token_address: contract_address!(STRK_FEE_CONTRACT_ADDRESS),
eth_fee_token_address: contract_address!(ETH_FEE_CONTRACT_ADDRESS),
}
}

pub fn get_rpc_state_reader_config() -> RpcStateReaderConfig {
RpcStateReaderConfig {
url: RPC_NODE_URL.to_string(),
json_rpc_version: JSON_RPC_VERSION.to_string(),
}
}

pub fn get_chain_info() -> ChainInfo {
ChainInfo { chain_id: ChainId::Mainnet, fee_token_addresses: get_fee_token_addresses() }
}
2 changes: 1 addition & 1 deletion crates/gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod compiler_version;
pub mod config;
pub mod errors;
pub mod gateway;
mod rpc_objects;
pub mod rpc_objects;
pub mod rpc_state_reader;
#[cfg(test)]
mod rpc_state_reader_test;
Expand Down

0 comments on commit efe0b06

Please sign in to comment.