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 18, 2024
1 parent 00179e3 commit e282678
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 81 deletions.
3 changes: 1 addition & 2 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
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,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};

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 @@ -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,
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 @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 3 additions & 32 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 @@ -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 {
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 @@ -24,13 +24,18 @@ 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,
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 @@ -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::{
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 @@ -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".
Expand Down Expand Up @@ -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() }
}
}
1 change: 0 additions & 1 deletion crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::Arc;
use assert_matches::assert_matches;
use async_trait::async_trait;
use blockifier::abi::constants;
use blockifier::test_utils::struct_impls::BlockInfoExt;
use chrono::Utc;
use futures::future::BoxFuture;
use futures::FutureExt;
Expand Down
15 changes: 7 additions & 8 deletions crates/starknet_integration_tests/src/state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit e282678

Please sign in to comment.