From c27d6e14219f53e319d4bb82715499030aaf8c9a Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Wed, 4 Dec 2024 10:38:48 +0200 Subject: [PATCH] chore(starknet_api): move test utils constants to starknet api --- .../src/concurrency/worker_logic_test.rs | 3 +- .../deprecated_syscalls_test.rs | 12 +++--- .../syscalls/syscall_tests/get_block_hash.rs | 9 +--- .../syscall_tests/get_execution_info.rs | 12 +++--- crates/blockifier/src/test_utils.rs | 16 +------ .../blockifier/src/test_utils/struct_impls.rs | 35 ++-------------- .../src/transaction/transactions_test.rs | 10 ++--- crates/starknet_api/src/test_utils.rs | 42 ++++++++++++++++++- crates/starknet_batcher/src/batcher_test.rs | 1 - .../src/state_reader.rs | 15 ++++--- 10 files changed, 74 insertions(+), 81 deletions(-) diff --git a/crates/blockifier/src/concurrency/worker_logic_test.rs b/crates/blockifier/src/concurrency/worker_logic_test.rs index bf1b29647a..e75fdd3a57 100644 --- a/crates/blockifier/src/concurrency/worker_logic_test.rs +++ b/crates/blockifier/src/concurrency/worker_logic_test.rs @@ -5,7 +5,7 @@ use rstest::rstest; use starknet_api::abi::abi_utils::get_fee_token_var_address; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::test_utils::declare::executable_declare_tx; -use starknet_api::test_utils::NonceManager; +use starknet_api::test_utils::{NonceManager, TEST_ERC20_CONTRACT_ADDRESS2}; use starknet_api::transaction::constants::DEPLOY_CONTRACT_FUNCTION_ENTRY_POINT_NAME; use starknet_api::transaction::fields::{ContractAddressSalt, Fee, ValidResourceBounds}; use starknet_api::transaction::TransactionVersion; @@ -31,7 +31,6 @@ use crate::test_utils::{ CairoVersion, RunnableCairo1, BALANCE, - TEST_ERC20_CONTRACT_ADDRESS2, }; use crate::transaction::account_transaction::AccountTransaction; use crate::transaction::objects::HasRelatedFeeType; diff --git a/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs b/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs index 7f04bb9abd..fc244c6719 100644 --- a/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs +++ b/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs @@ -7,6 +7,13 @@ use rstest::rstest; use starknet_api::abi::abi_utils::selector_from_name; use starknet_api::core::{calculate_contract_address, ChainId}; use starknet_api::state::StorageKey; +use starknet_api::test_utils::{ + CURRENT_BLOCK_NUMBER, + CURRENT_BLOCK_NUMBER_FOR_VALIDATE, + CURRENT_BLOCK_TIMESTAMP, + CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE, + TEST_SEQUENCER_ADDRESS, +}; use starknet_api::transaction::fields::{Calldata, ContractAddressSalt, Fee}; use starknet_api::transaction::{ EventContent, @@ -34,11 +41,6 @@ use crate::test_utils::{ get_syscall_resources, trivial_external_entry_point_new, CairoVersion, - CURRENT_BLOCK_NUMBER, - CURRENT_BLOCK_NUMBER_FOR_VALIDATE, - CURRENT_BLOCK_TIMESTAMP, - CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE, - TEST_SEQUENCER_ADDRESS, }; use crate::transaction::objects::{ CommonAccountFields, diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs index 09ffe2619b..7a11322f99 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs @@ -2,6 +2,7 @@ use pretty_assertions::assert_eq; use starknet_api::abi::abi_utils::selector_from_name; use starknet_api::execution_utils::format_panic_data; use starknet_api::state::StorageKey; +use starknet_api::test_utils::CURRENT_BLOCK_NUMBER; use starknet_api::{calldata, felt}; use starknet_types_core::felt::Felt; use test_case::test_case; @@ -15,13 +16,7 @@ use crate::state::state_api::State; use crate::test_utils::contracts::FeatureContract; use crate::test_utils::dict_state_reader::DictStateReader; use crate::test_utils::initial_test_state::test_state; -use crate::test_utils::{ - trivial_external_entry_point_new, - CairoVersion, - RunnableCairo1, - BALANCE, - CURRENT_BLOCK_NUMBER, -}; +use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, RunnableCairo1, BALANCE}; use crate::versioned_constants::VersionedConstants; use crate::{check_entry_point_execution_error_for_custom_hint, retdata}; diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs index 47f351467d..788f23deef 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/get_execution_info.rs @@ -3,6 +3,13 @@ use starknet_api::block::GasPrice; use starknet_api::core::ChainId; use starknet_api::data_availability::DataAvailabilityMode; use starknet_api::execution_resources::GasAmount; +use starknet_api::test_utils::{ + CURRENT_BLOCK_NUMBER, + CURRENT_BLOCK_NUMBER_FOR_VALIDATE, + CURRENT_BLOCK_TIMESTAMP, + CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE, + TEST_SEQUENCER_ADDRESS, +}; use starknet_api::transaction::fields::{ AccountDeploymentData, Calldata, @@ -28,11 +35,6 @@ use crate::test_utils::{ CairoVersion, RunnableCairo1, BALANCE, - CURRENT_BLOCK_NUMBER, - CURRENT_BLOCK_NUMBER_FOR_VALIDATE, - CURRENT_BLOCK_TIMESTAMP, - CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE, - TEST_SEQUENCER_ADDRESS, }; use crate::transaction::objects::{ CommonAccountFields, diff --git a/crates/blockifier/src/test_utils.rs b/crates/blockifier/src/test_utils.rs index b5b621b49f..685c93d6c5 100644 --- a/crates/blockifier/src/test_utils.rs +++ b/crates/blockifier/src/test_utils.rs @@ -29,6 +29,7 @@ use starknet_api::test_utils::{ DEFAULT_STRK_L1_GAS_PRICE, DEFAULT_STRK_L2_GAS_PRICE, MAX_FEE, + TEST_SEQUENCER_ADDRESS, }; use starknet_api::transaction::fields::{ Calldata, @@ -53,13 +54,6 @@ use crate::test_utils::contracts::FeatureContract; use crate::transaction::transaction_types::TransactionType; use crate::utils::{const_max, u64_from_usize}; use crate::versioned_constants::VersionedConstants; -// TODO(Dori, 1/2/2024): Remove these constants once all tests use the `contracts` and -// `initial_test_state` modules for testing. -// Addresses. -pub const TEST_SEQUENCER_ADDRESS: &str = "0x1000"; -pub const TEST_ERC20_CONTRACT_ADDRESS: &str = "0x1001"; -pub const TEST_ERC20_CONTRACT_ADDRESS2: &str = "0x1002"; - // Class hashes. // TODO(Adi, 15/01/2023): Remove and compute the class hash corresponding to the ERC20 contract in // starkgate once we use the real ERC20 contract. @@ -176,14 +170,6 @@ pub const BALANCE: Fee = Fee(10 MAX_FEE.0, )); -// The block number of the BlockContext being used for testing. -pub const CURRENT_BLOCK_NUMBER: u64 = 2001; -pub const CURRENT_BLOCK_NUMBER_FOR_VALIDATE: u64 = 2000; - -// The block timestamp of the BlockContext being used for testing. -pub const CURRENT_BLOCK_TIMESTAMP: u64 = 1072023; -pub const CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE: u64 = 1069200; - #[derive(Default)] pub struct SaltManager { next_salt: u8, diff --git a/crates/blockifier/src/test_utils/struct_impls.rs b/crates/blockifier/src/test_utils/struct_impls.rs index 61d67bb317..a5c41b8275 100644 --- a/crates/blockifier/src/test_utils/struct_impls.rs +++ b/crates/blockifier/src/test_utils/struct_impls.rs @@ -10,11 +10,11 @@ use cairo_lang_starknet_classes::contract_class::ContractClass as SierraContract #[cfg(feature = "cairo_native")] use cairo_native::executor::AotContractExecutor; use serde_json::Value; -use starknet_api::block::{BlockInfo, BlockNumber, BlockTimestamp}; +use starknet_api::block::BlockInfo; use starknet_api::contract_address; use starknet_api::core::{ChainId, ClassHash}; use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass; -use starknet_api::test_utils::DEFAULT_GAS_PRICES; +use starknet_api::test_utils::{TEST_ERC20_CONTRACT_ADDRESS, TEST_ERC20_CONTRACT_ADDRESS2}; use crate::bouncer::{BouncerConfig, BouncerWeights, BuiltinCount}; use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext}; @@ -30,15 +30,7 @@ use crate::execution::entry_point::{ #[cfg(feature = "cairo_native")] use crate::execution::native::contract_class::NativeCompiledClassV1; use crate::state::state_api::State; -use crate::test_utils::{ - get_raw_contract_class, - update_json_value, - CURRENT_BLOCK_NUMBER, - CURRENT_BLOCK_TIMESTAMP, - TEST_ERC20_CONTRACT_ADDRESS, - TEST_ERC20_CONTRACT_ADDRESS2, - TEST_SEQUENCER_ADDRESS, -}; +use crate::test_utils::{get_raw_contract_class, update_json_value}; use crate::transaction::objects::{ CurrentTransactionInfo, DeprecatedTransactionInfo, @@ -140,27 +132,6 @@ impl ChainInfo { } } -pub trait BlockInfoExt { - fn create_for_testing() -> Self; - fn create_for_testing_with_kzg(use_kzg_da: bool) -> Self; -} - -impl BlockInfoExt for BlockInfo { - fn create_for_testing() -> Self { - Self { - block_number: BlockNumber(CURRENT_BLOCK_NUMBER), - block_timestamp: BlockTimestamp(CURRENT_BLOCK_TIMESTAMP), - sequencer_address: contract_address!(TEST_SEQUENCER_ADDRESS), - gas_prices: DEFAULT_GAS_PRICES, - use_kzg_da: false, - } - } - - fn create_for_testing_with_kzg(use_kzg_da: bool) -> Self { - Self { use_kzg_da, ..Self::create_for_testing() } - } -} - impl BlockContext { pub fn create_for_testing() -> Self { Self { diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index e2b3cfc1af..a6054daf33 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -24,6 +24,10 @@ use starknet_api::test_utils::deploy_account::{executable_deploy_account_tx, Dep use starknet_api::test_utils::invoke::{executable_invoke_tx, InvokeTxArgs}; use starknet_api::test_utils::{ NonceManager, + CURRENT_BLOCK_NUMBER, + CURRENT_BLOCK_NUMBER_FOR_VALIDATE, + CURRENT_BLOCK_TIMESTAMP, + CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE, DEFAULT_L1_DATA_GAS_MAX_AMOUNT, DEFAULT_L1_GAS_AMOUNT, DEFAULT_L2_GAS_MAX_AMOUNT, @@ -31,6 +35,7 @@ use starknet_api::test_utils::{ DEFAULT_STRK_L1_GAS_PRICE, DEFAULT_STRK_L2_GAS_PRICE, MAX_FEE, + TEST_SEQUENCER_ADDRESS, }; use starknet_api::transaction::fields::Resource::{L1DataGas, L1Gas, L2Gas}; use starknet_api::transaction::fields::{ @@ -111,11 +116,6 @@ use crate::test_utils::{ RunnableCairo1, SaltManager, BALANCE, - CURRENT_BLOCK_NUMBER, - CURRENT_BLOCK_NUMBER_FOR_VALIDATE, - CURRENT_BLOCK_TIMESTAMP, - CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE, - TEST_SEQUENCER_ADDRESS, }; use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags}; use crate::transaction::errors::{ diff --git a/crates/starknet_api/src/test_utils.rs b/crates/starknet_api/src/test_utils.rs index 265069d6ef..d1b9db6eb1 100644 --- a/crates/starknet_api/src/test_utils.rs +++ b/crates/starknet_api/src/test_utils.rs @@ -7,7 +7,16 @@ use serde::{Deserialize, Serialize}; use serde_json::to_string_pretty; use starknet_types_core::felt::Felt; -use crate::block::{BlockNumber, GasPrice, GasPriceVector, GasPrices, NonzeroGasPrice}; +use crate::block::{ + BlockInfo, + BlockNumber, + BlockTimestamp, + GasPrice, + GasPriceVector, + GasPrices, + NonzeroGasPrice, +}; +use crate::contract_address; use crate::core::{ChainId, ContractAddress, Nonce}; use crate::execution_resources::GasAmount; use crate::rpc_transaction::RpcTransaction; @@ -19,6 +28,21 @@ pub mod deploy_account; pub mod invoke; pub mod l1_handler; +// TODO(Dori, 1/2/2024): Remove these constants once all tests use the `contracts` and +// `initial_test_state` modules for testing. +// Addresses. +pub const TEST_SEQUENCER_ADDRESS: &str = "0x1000"; +pub const TEST_ERC20_CONTRACT_ADDRESS: &str = "0x1001"; +pub const TEST_ERC20_CONTRACT_ADDRESS2: &str = "0x1002"; + +// The block number of the BlockContext being used for testing. +pub const CURRENT_BLOCK_NUMBER: u64 = 2001; +pub const CURRENT_BLOCK_NUMBER_FOR_VALIDATE: u64 = 2000; + +// The block timestamp of the BlockContext being used for testing. +pub const CURRENT_BLOCK_TIMESTAMP: u64 = 1072023; +pub const CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE: u64 = 1069200; + /// Returns the path to a file in the resources directory. This assumes the current working /// directory has a `resources` folder. The value for file_path should be the path to the required /// file in the folder "resources". @@ -152,3 +176,19 @@ pub const DEFAULT_GAS_PRICES: GasPrices = GasPrices { // Deprecated transactions: pub const MAX_FEE: Fee = DEFAULT_L1_GAS_AMOUNT.nonzero_saturating_mul(DEFAULT_ETH_L1_GAS_PRICE); + +impl BlockInfo { + pub fn create_for_testing() -> Self { + Self { + block_number: BlockNumber(CURRENT_BLOCK_NUMBER), + block_timestamp: BlockTimestamp(CURRENT_BLOCK_TIMESTAMP), + sequencer_address: contract_address!(TEST_SEQUENCER_ADDRESS), + gas_prices: DEFAULT_GAS_PRICES, + use_kzg_da: false, + } + } + + pub fn create_for_testing_with_kzg(use_kzg_da: bool) -> Self { + Self { use_kzg_da, ..Self::create_for_testing() } + } +} diff --git a/crates/starknet_batcher/src/batcher_test.rs b/crates/starknet_batcher/src/batcher_test.rs index de091970f0..9bf0df592c 100644 --- a/crates/starknet_batcher/src/batcher_test.rs +++ b/crates/starknet_batcher/src/batcher_test.rs @@ -3,7 +3,6 @@ use std::sync::Arc; use assert_matches::assert_matches; use blockifier::abi::constants; -use blockifier::test_utils::struct_impls::BlockInfoExt; use chrono::Utc; use indexmap::indexmap; use mockall::predicate::eq; diff --git a/crates/starknet_integration_tests/src/state_reader.rs b/crates/starknet_integration_tests/src/state_reader.rs index f037b5c59c..61d511ad4d 100644 --- a/crates/starknet_integration_tests/src/state_reader.rs +++ b/crates/starknet_integration_tests/src/state_reader.rs @@ -4,13 +4,7 @@ use std::sync::Arc; use assert_matches::assert_matches; use blockifier::context::ChainInfo; use blockifier::test_utils::contracts::FeatureContract; -use blockifier::test_utils::{ - CairoVersion, - RunnableCairo1, - BALANCE, - CURRENT_BLOCK_TIMESTAMP, - TEST_SEQUENCER_ADDRESS, -}; +use blockifier::test_utils::{CairoVersion, RunnableCairo1, BALANCE}; use blockifier::versioned_constants::VersionedConstants; use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; use indexmap::IndexMap; @@ -37,7 +31,12 @@ use starknet_api::block::{ use starknet_api::core::{ChainId, ClassHash, ContractAddress, Nonce, SequencerContractAddress}; use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass; use starknet_api::state::{SierraContractClass, StorageKey, ThinStateDiff}; -use starknet_api::test_utils::{DEFAULT_ETH_L1_GAS_PRICE, DEFAULT_STRK_L1_GAS_PRICE}; +use starknet_api::test_utils::{ + CURRENT_BLOCK_TIMESTAMP, + DEFAULT_ETH_L1_GAS_PRICE, + DEFAULT_STRK_L1_GAS_PRICE, + TEST_SEQUENCER_ADDRESS, +}; use starknet_api::transaction::fields::Fee; use starknet_api::{contract_address, felt}; use starknet_client::reader::PendingData;