Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
refactor: clean tests by using macros (#1851)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware authored May 2, 2024
1 parent 069754d commit c4dc89a
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 121 deletions.
6 changes: 2 additions & 4 deletions crates/blockifier/src/blockifier/stateful_validator_test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use rstest::rstest;
use starknet_api::core::Nonce;
use starknet_api::hash::StarkFelt;
use starknet_api::stark_felt;
use starknet_api::transaction::{Fee, TransactionVersion};

use crate::blockifier::stateful_validator::StatefulValidator;
use crate::bouncer::BouncerConfig;
use crate::context::BlockContext;
use crate::nonce;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::{fund_account, test_state};
use crate::test_utils::{CairoVersion, NonceManager, BALANCE};
Expand Down Expand Up @@ -70,7 +68,7 @@ fn test_transaction_validator(
let mut stateful_validator = StatefulValidator::create(
state,
block_context,
Nonce(stark_felt!(0_u32)),
nonce!(0_u32),
BouncerConfig::create_for_testing(),
);

Expand Down
22 changes: 10 additions & 12 deletions crates/blockifier/src/bouncer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::collections::{HashMap, HashSet};

use cairo_vm::serde::deserialize_program::BuiltinName;
use rstest::rstest;
use starknet_api::core::{ClassHash, ContractAddress};
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;
use starknet_api::core::{ClassHash, ContractAddress, PatriciaKey};
use starknet_api::hash::StarkHash;
use starknet_api::{class_hash, contract_address, patricia_key};

use super::BouncerConfig;
use crate::blockifier::transaction_executor::{
Expand All @@ -14,6 +14,7 @@ use crate::bouncer::{Bouncer, BouncerWeights, BuiltinCount};
use crate::context::BlockContext;
use crate::execution::call_info::ExecutionSummary;
use crate::state::cached_state::{CachedState, StateChangesKeys};
use crate::storage_key;
use crate::test_utils::initial_test_state::test_state;
use crate::transaction::errors::TransactionExecutionError;

Expand Down Expand Up @@ -78,10 +79,10 @@ fn test_block_weights_has_room() {
#[rstest]
#[case::empty_initial_bouncer(Bouncer::new(BouncerConfig::default()))]
#[case::non_empty_initial_bouncer(Bouncer {
executed_class_hashes: HashSet::from([ClassHash(StarkFelt::from(0_u128))]),
executed_class_hashes: HashSet::from([class_hash!(0_u128)]),
visited_storage_entries: HashSet::from([(
ContractAddress::from(0_u128),
StorageKey::from(0_u128),
contract_address!(0_u128),
storage_key!(0_u128),
)]),
state_changes_keys: StateChangesKeys::create_for_testing(HashSet::from([
ContractAddress::from(0_u128),
Expand All @@ -106,13 +107,10 @@ fn test_block_weights_has_room() {
})]
fn test_bouncer_update(#[case] initial_bouncer: Bouncer) {
let execution_summary_to_update = ExecutionSummary {
executed_class_hashes: HashSet::from([
ClassHash(StarkFelt::from(1_u128)),
ClassHash(StarkFelt::from(2_u128)),
]),
executed_class_hashes: HashSet::from([class_hash!(1_u128), class_hash!(2_u128)]),
visited_storage_entries: HashSet::from([
(ContractAddress::from(1_u128), StorageKey::from(1_u128)),
(ContractAddress::from(2_u128), StorageKey::from(2_u128)),
(ContractAddress::from(1_u128), storage_key!(1_u128)),
(ContractAddress::from(2_u128), storage_key!(2_u128)),
]),
..Default::default()
};
Expand Down
27 changes: 12 additions & 15 deletions crates/blockifier/src/concurrency/versioned_state_proxy_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::thread;

use starknet_api::core::{
calculate_contract_address, ClassHash, CompiledClassHash, ContractAddress, Nonce, PatriciaKey,
};
use starknet_api::core::{calculate_contract_address, ClassHash, ContractAddress, PatriciaKey};
use starknet_api::hash::{StarkFelt, StarkHash};
use starknet_api::state::StorageKey;
use starknet_api::transaction::{Calldata, ContractAddressSalt, Fee, TransactionVersion};
use starknet_api::{calldata, contract_address, patricia_key, stark_felt};
use starknet_api::{calldata, class_hash, contract_address, patricia_key, stark_felt};

use crate::abi::abi_utils::{get_fee_token_var_address, get_storage_var_address};
use crate::concurrency::versioned_state_proxy::{
ThreadSafeVersionedState, VersionedState, VersionedStateProxy,
};
use crate::context::BlockContext;
use crate::deploy_account_tx_args;
use crate::state::cached_state::CachedState;
use crate::state::state_api::{State, StateReader};
use crate::test_utils::contracts::FeatureContract;
Expand All @@ -27,17 +23,18 @@ use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::objects::{FeeType, TransactionInfoCreator};
use crate::transaction::test_utils::l1_resource_bounds;
use crate::transaction::transactions::ExecutableTransaction;
use crate::{compiled_class_hash, deploy_account_tx_args, nonce, storage_key};

#[test]
fn test_versioned_state_proxy() {
// Test data
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo0);
let contract_address = contract_address!("0x1");
let key = StorageKey(patricia_key!("0x10"));
let key = storage_key!("0x10");
let stark_felt = stark_felt!(13_u8);
let nonce = Nonce(stark_felt!(2_u8));
let class_hash = ClassHash(stark_felt!(27_u8));
let compiled_class_hash = CompiledClassHash(stark_felt!(29_u8));
let nonce = nonce!(2_u8);
let class_hash = class_hash!(27_u8);
let compiled_class_hash = compiled_class_hash!(29_u8);
let contract_class = test_contract.get_class();

// Create the verioned state
Expand Down Expand Up @@ -73,12 +70,12 @@ fn test_versioned_state_proxy() {
);

// Write to the state.
let new_key = StorageKey(patricia_key!("0x11"));
let new_key = storage_key!("0x11");
let stark_felt_v3 = stark_felt!(14_u8);
let nonce_v4 = Nonce(stark_felt!(3_u8));
let class_hash_v7 = ClassHash(stark_felt!(28_u8));
let class_hash_v10 = ClassHash(stark_felt!(29_u8));
let compiled_class_hash_v18 = CompiledClassHash(stark_felt!(30_u8));
let nonce_v4 = nonce!(3_u8);
let class_hash_v7 = class_hash!(28_u8);
let class_hash_v10 = class_hash!(29_u8);
let compiled_class_hash_v18 = compiled_class_hash!(30_u8);
let contract_class_v11 = FeatureContract::TestContract(CairoVersion::Cairo1).get_class();

versioned_state_proxys[3].set_storage_at(contract_address, new_key, stark_felt_v3).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use num_traits::Pow;
use pretty_assertions::assert_eq;
use rstest::rstest;
use starknet_api::core::{calculate_contract_address, ChainId, Nonce, PatriciaKey};
use starknet_api::core::{calculate_contract_address, ChainId, PatriciaKey};
use starknet_api::hash::{StarkFelt, StarkHash};
use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata, ContractAddressSalt, EventContent, EventData, EventKey, Fee, TransactionHash,
TransactionVersion,
};
use starknet_api::{calldata, patricia_key, stark_felt};
use starknet_api::{calldata, stark_felt};
use test_case::test_case;

use crate::abi::abi_utils::selector_from_name;
Expand All @@ -38,7 +38,7 @@ use crate::transaction::objects::{
CommonAccountFields, DeprecatedTransactionInfo, TransactionInfo,
};
use crate::versioned_constants::VersionedConstants;
use crate::{check_entry_point_execution_error_for_custom_hint, retdata};
use crate::{check_entry_point_execution_error_for_custom_hint, nonce, retdata, storage_key};

#[test]
fn test_storage_read_write() {
Expand Down Expand Up @@ -147,7 +147,7 @@ fn test_nested_library_call() {
execution: CallExecution::from_retdata(retdata![stark_felt!(value + 1)]),
resources: storage_entry_point_resources.clone(),
storage_read_values: vec![stark_felt!(0_u8), stark_felt!(value + 1)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key + 1))]),
accessed_storage_keys: HashSet::from([storage_key!(key + 1)]),
..Default::default()
};
let mut library_call_resources = &get_syscall_resources(DeprecatedSyscallSelector::LibraryCall)
Expand All @@ -169,7 +169,7 @@ fn test_nested_library_call() {
execution: CallExecution::from_retdata(retdata![stark_felt!(value)]),
resources: storage_entry_point_resources.clone(),
storage_read_values: vec![stark_felt!(0_u8), stark_felt!(value)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key))]),
accessed_storage_keys: HashSet::from([storage_key!(key)]),
..Default::default()
};

Expand Down Expand Up @@ -234,7 +234,7 @@ fn test_call_contract() {
builtin_instance_counter: HashMap::from([(RANGE_CHECK_BUILTIN_NAME.to_string(), 2)]),
},
storage_read_values: vec![StarkFelt::ZERO, stark_felt!(value)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key))]),
accessed_storage_keys: HashSet::from([storage_key!(key)]),
..Default::default()
};
let expected_call_info = CallInfo {
Expand Down Expand Up @@ -451,7 +451,7 @@ fn test_tx_info(#[values(false, true)] only_query: bool) {
}
let tx_hash = TransactionHash(stark_felt!(1991_u16));
let max_fee = Fee(0);
let nonce = Nonce(stark_felt!(3_u16));
let nonce = nonce!(3_u16);
let sender_address = test_contract.get_instance_address(0);
let expected_tx_info = calldata![
felt_to_stark_felt(&version), // Transaction version.
Expand Down
10 changes: 3 additions & 7 deletions crates/blockifier/src/execution/entry_point_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use rstest::rstest;
use starknet_api::core::{EntryPointSelector, PatriciaKey};
use starknet_api::deprecated_contract_class::{EntryPointOffset, EntryPointType};
use starknet_api::hash::{StarkFelt, StarkHash};
use starknet_api::state::StorageKey;
use starknet_api::transaction::{Calldata, TransactionVersion};
use starknet_api::{calldata, patricia_key, stark_felt};
use starknet_api::{calldata, stark_felt};

use crate::abi::abi_utils::{get_storage_var_address, selector_from_name};
use crate::context::{BlockContext, ChainInfo};
Expand All @@ -36,7 +35,7 @@ use crate::transaction::test_utils::{
use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transactions::ExecutableTransaction;
use crate::versioned_constants::VersionedConstants;
use crate::{invoke_tx_args, retdata};
use crate::{invoke_tx_args, retdata, storage_key};

const INNER_CALL_CONTRACT_IN_CALL_CHAIN_OFFSET: usize = 117;

Expand Down Expand Up @@ -539,10 +538,7 @@ fn test_storage_related_members() {
};
let actual_call_info = entry_point_call.execute_directly(&mut state).unwrap();
assert_eq!(actual_call_info.storage_read_values, vec![stark_felt!(0_u8), value]);
assert_eq!(
actual_call_info.accessed_storage_keys,
HashSet::from([StorageKey(patricia_key!(key))])
);
assert_eq!(actual_call_info.accessed_storage_keys, HashSet::from([storage_key!(key)]));
}

#[test]
Expand Down
13 changes: 6 additions & 7 deletions crates/blockifier/src/execution/syscalls/syscalls_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use num_traits::Pow;
use pretty_assertions::assert_eq;
use rstest::rstest;
use starknet_api::core::{
calculate_contract_address, ChainId, ContractAddress, EthAddress, Nonce, PatriciaKey,
calculate_contract_address, ChainId, ContractAddress, EthAddress, PatriciaKey,
};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::hash::{StarkFelt, StarkHash};
Expand All @@ -19,7 +19,7 @@ use starknet_api::transaction::{
L2ToL1Payload, PaymasterData, Resource, ResourceBounds, ResourceBoundsMapping, Tip,
TransactionHash, TransactionVersion,
};
use starknet_api::{calldata, patricia_key, stark_felt};
use starknet_api::{calldata, stark_felt};
use test_case::test_case;

use crate::abi::abi_utils::selector_from_name;
Expand Down Expand Up @@ -50,8 +50,7 @@ use crate::transaction::objects::{
CommonAccountFields, CurrentTransactionInfo, DeprecatedTransactionInfo, TransactionInfo,
};
use crate::versioned_constants::VersionedConstants;
use crate::{check_entry_point_execution_error_for_custom_hint, retdata};

use crate::{check_entry_point_execution_error_for_custom_hint, nonce, retdata, storage_key};
pub const REQUIRED_GAS_STORAGE_READ_WRITE_TEST: u64 = 27150;
pub const REQUIRED_GAS_CALL_CONTRACT_TEST: u64 = 105680;
pub const REQUIRED_GAS_LIBRARY_CALL_TEST: u64 = REQUIRED_GAS_CALL_CONTRACT_TEST;
Expand Down Expand Up @@ -374,7 +373,7 @@ fn test_get_execution_info(

let tx_hash = TransactionHash(stark_felt!(1991_u16));
let max_fee = Fee(42);
let nonce = Nonce(stark_felt!(3_u16));
let nonce = nonce!(3_u16);
let sender_address = test_contract_address;

let expected_tx_info: Vec<StarkFelt>;
Expand Down Expand Up @@ -622,7 +621,7 @@ fn test_nested_library_call() {
},
resources: storage_entry_point_resources.clone(),
storage_read_values: vec![stark_felt!(value + 1)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key + 1))]),
accessed_storage_keys: HashSet::from([storage_key!(key + 1)]),
..Default::default()
};
let library_call_resources = &get_syscall_resources(SyscallSelector::LibraryCall)
Expand Down Expand Up @@ -651,7 +650,7 @@ fn test_nested_library_call() {
},
resources: storage_entry_point_resources,
storage_read_values: vec![stark_felt!(value)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key))]),
accessed_storage_keys: HashSet::from([storage_key!(key)]),
..Default::default()
};

Expand Down
6 changes: 2 additions & 4 deletions crates/blockifier/src/fee/actual_cost_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use rstest::{fixture, rstest};
use starknet_api::core::Nonce;
use starknet_api::hash::StarkFelt;
use starknet_api::stark_felt;
use starknet_api::transaction::{Fee, L2ToL1Payload, TransactionVersion};
Expand All @@ -11,7 +10,6 @@ use crate::fee::gas_usage::{
get_consumed_message_to_l2_emissions_cost, get_log_message_to_l1_emissions_cost,
get_message_segment_length,
};
use crate::invoke_tx_args;
use crate::state::cached_state::StateChangesCount;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
Expand All @@ -22,7 +20,7 @@ use crate::transaction::test_utils::{account_invoke_tx, calculate_class_info_for
use crate::transaction::transactions::ExecutableTransaction;
use crate::utils::{u128_from_usize, usize_from_u128};
use crate::versioned_constants::VersionedConstants;

use crate::{invoke_tx_args, nonce};
#[fixture]
fn versioned_constants() -> &'static VersionedConstants {
VersionedConstants::latest_constants()
Expand Down Expand Up @@ -344,7 +342,7 @@ fn test_calculate_tx_gas_usage(#[values(false, true)] use_kzg_da: bool) {
sender_address: account_contract_address,
calldata: execute_calldata,
version: TransactionVersion::ONE,
nonce: Nonce(stark_felt!(1_u8)),
nonce: nonce!(1_u8),
});

let calldata_length = account_tx.calldata_length();
Expand Down
Loading

0 comments on commit c4dc89a

Please sign in to comment.