Skip to content

Commit

Permalink
chore(starknet_api): move test utils constants to starknet api
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Dec 4, 2024
1 parent 11f3e05 commit 0f24883
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 84 deletions.
10 changes: 2 additions & 8 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Mutex;
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::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;
Expand All @@ -25,13 +25,7 @@ use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::declare::declare_tx;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{
create_calldata,
create_trivial_calldata,
CairoVersion,
BALANCE,
TEST_ERC20_CONTRACT_ADDRESS2,
};
use crate::test_utils::{create_calldata, create_trivial_calldata, CairoVersion, BALANCE};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::objects::HasRelatedFeeType;
use crate::transaction::test_utils::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,12 +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,
BALANCE,
CURRENT_BLOCK_NUMBER,
};
use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, BALANCE};
use crate::versioned_constants::VersionedConstants;
use crate::{check_entry_point_execution_error_for_custom_hint, retdata};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,16 +30,7 @@ use crate::execution::common_hints::ExecutionMode;
use crate::execution::entry_point::CallEntryPoint;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{
trivial_external_entry_point_with_address,
CairoVersion,
BALANCE,
CURRENT_BLOCK_NUMBER,
CURRENT_BLOCK_NUMBER_FOR_VALIDATE,
CURRENT_BLOCK_TIMESTAMP,
CURRENT_BLOCK_TIMESTAMP_FOR_VALIDATE,
TEST_SEQUENCER_ADDRESS,
};
use crate::test_utils::{trivial_external_entry_point_with_address, CairoVersion, BALANCE};
use crate::transaction::objects::{
CommonAccountFields,
CurrentTransactionInfo,
Expand Down
16 changes: 1 addition & 15 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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,
Expand All @@ -52,13 +53,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.
Expand Down Expand Up @@ -157,14 +151,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,
Expand Down
30 changes: 3 additions & 27 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -145,22 +137,6 @@ pub trait BlockInfoExt {
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 {
Expand Down
10 changes: 5 additions & 5 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ use starknet_api::state::StorageKey;
use starknet_api::test_utils::invoke::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,
DEFAULT_STRK_L1_DATA_GAS_PRICE,
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::{
Expand Down Expand Up @@ -112,11 +117,6 @@ use crate::test_utils::{
CairoVersion,
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;
use crate::transaction::errors::{
Expand Down
42 changes: 41 additions & 1 deletion crates/starknet_api/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +30,21 @@ pub mod l1_handler;

generate_get_package_dir!();

// 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 package directory has a
/// `resources` folder. The value for file_path should be the path to the required file in the
/// folder "resources".
Expand Down Expand Up @@ -155,3 +179,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() }
}
}
14 changes: 7 additions & 7 deletions crates/starknet_integration_tests/src/state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ use std::sync::Arc;
use assert_matches::assert_matches;
use blockifier::context::{BlockContext, ChainInfo};
use blockifier::test_utils::contracts::FeatureContract;
use blockifier::test_utils::{
CairoVersion,
BALANCE,
CURRENT_BLOCK_TIMESTAMP,
TEST_SEQUENCER_ADDRESS,
};
use blockifier::test_utils::{CairoVersion, BALANCE};
use blockifier::versioned_constants::VersionedConstants;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use indexmap::IndexMap;
Expand All @@ -36,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::{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;
Expand Down

0 comments on commit 0f24883

Please sign in to comment.