Skip to content

Commit

Permalink
chore: use starknet api test utils macros in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Oct 31, 2024
1 parent 5d977d2 commit e4d16d5
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 200 deletions.
4 changes: 2 additions & 2 deletions crates/batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use starknet_api::executable_transaction::Transaction;
use starknet_api::hash::PoseidonHash;
use starknet_api::state::ThinStateDiff;
use starknet_api::transaction::TransactionHash;
use starknet_api::{felt, nonce, patricia_key};
use starknet_api::{contract_address, felt, nonce, patricia_key};
use starknet_batcher_types::batcher_types::{
BuildProposalInput,
DecisionReachedInput,
Expand Down Expand Up @@ -303,5 +303,5 @@ fn test_tx_hashes(range: std::ops::Range<u128>) -> HashSet<TransactionHash> {
}

fn test_contract_nonces(range: std::ops::Range<u128>) -> HashMap<ContractAddress, Nonce> {
HashMap::from_iter(range.map(|i| (ContractAddress(patricia_key!(i)), nonce!(i))))
HashMap::from_iter(range.map(|i| (contract_address!(i), nonce!(i))))
}
8 changes: 4 additions & 4 deletions crates/blockifier/src/bouncer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn test_block_weights_has_room() {
storage_key!(0_u128),
)]),
state_changes_keys: StateChangesKeys::create_for_testing(HashSet::from([
ContractAddress::from(0_u128),
contract_address!(0_u128),
])),
bouncer_config: BouncerConfig::empty(),
accumulated_weights: BouncerWeights {
Expand All @@ -126,8 +126,8 @@ fn test_bouncer_update(#[case] initial_bouncer: Bouncer) {
let execution_summary_to_update = ExecutionSummary {
executed_class_hashes: HashSet::from([class_hash!(1_u128), class_hash!(2_u128)]),
visited_storage_entries: HashSet::from([
(ContractAddress::from(1_u128), storage_key!(1_u128)),
(ContractAddress::from(2_u128), storage_key!(2_u128)),
(contract_address!(1_u128), storage_key!(1_u128)),
(contract_address!(2_u128), storage_key!(2_u128)),
]),
..Default::default()
};
Expand All @@ -153,7 +153,7 @@ fn test_bouncer_update(#[case] initial_bouncer: Bouncer) {
};

let state_changes_keys_to_update =
StateChangesKeys::create_for_testing(HashSet::from([ContractAddress::from(1_u128)]));
StateChangesKeys::create_for_testing(HashSet::from([contract_address!(1_u128)]));

let mut updated_bouncer = initial_bouncer.clone();
updated_bouncer.update(
Expand Down
16 changes: 5 additions & 11 deletions crates/blockifier/src/concurrency/versioned_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ use std::thread;

use assert_matches::assert_matches;
use rstest::{fixture, rstest};
use starknet_api::core::{
calculate_contract_address,
ClassHash,
ContractAddress,
Nonce,
PatriciaKey,
};
use starknet_api::core::{calculate_contract_address, ClassHash, ContractAddress, PatriciaKey};
use starknet_api::test_utils::NonceManager;
use starknet_api::transaction::{ContractAddressSalt, ValidResourceBounds};
use starknet_api::{
Expand Down Expand Up @@ -562,7 +556,7 @@ fn test_delete_writes_completeness(
) {
let feature_contract = FeatureContract::TestContract(CairoVersion::Cairo1);
let state_maps_writes = StateMaps {
nonces: HashMap::from([(contract_address!("0x1"), Nonce(felt!(1_u8)))]),
nonces: HashMap::from([(contract_address!("0x1"), nonce!(1_u8))]),
class_hashes: HashMap::from([(
contract_address!("0x1"),
feature_contract.get_class_hash(),
Expand Down Expand Up @@ -620,7 +614,7 @@ fn test_versioned_proxy_state_flow(
safe_versioned_state: ThreadSafeVersionedState<CachedState<DictStateReader>>,
) {
let contract_address = contract_address!("0x1");
let class_hash = ClassHash(felt!(27_u8));
let class_hash = class_hash!(27_u8);

let mut versioned_proxy_states: Vec<VersionedStateProxy<CachedState<DictStateReader>>> =
(0..4).map(|i| safe_versioned_state.pin_version(i)).collect();
Expand All @@ -631,8 +625,8 @@ fn test_versioned_proxy_state_flow(
}

// Clients class hash values.
let class_hash_1 = ClassHash(felt!(76_u8));
let class_hash_3 = ClassHash(felt!(234_u8));
let class_hash_1 = class_hash!(76_u8);
let class_hash_3 = class_hash!(234_u8);

transactional_states[1].set_class_hash_at(contract_address, class_hash_1).unwrap();
transactional_states[3].set_class_hash_at(contract_address, class_hash_3).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use pretty_assertions::assert_eq;
use starknet_api::core::PatriciaKey;
use starknet_api::execution_utils::format_panic_data;
use starknet_api::state::StorageKey;
use starknet_api::{calldata, felt, patricia_key};
use starknet_api::{calldata, felt, storage_key};
use test_case::test_case;

use crate::abi::abi_utils::selector_from_name;
Expand Down Expand Up @@ -163,7 +162,7 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) {
resources: storage_entry_point_resources.clone(),
tracked_resource,
storage_read_values: vec![felt!(value + 1)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key + 1))]),
accessed_storage_keys: HashSet::from([storage_key!(key + 1)]),
..Default::default()
};

Expand Down Expand Up @@ -196,7 +195,7 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) {
},
resources: storage_entry_point_resources,
storage_read_values: vec![felt!(value)],
accessed_storage_keys: HashSet::from([StorageKey(patricia_key!(key))]),
accessed_storage_keys: HashSet::from([storage_key!(key)]),
tracked_resource,
..Default::default()
};
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/state/cached_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ fn test_state_changes_merge(
.find(|(contract_address, _)| contract_address == &contract_address!(CONTRACT_ADDRESS))
.unwrap();
// A new address, not included in state_changes1, to write to.
let new_contract_address = ContractAddress(patricia_key!("0x111"));
let new_contract_address = contract_address!("0x111");

// Overwrite existing and new storage values.
transactional_state.set_storage_at(contract_address, storage_key, felt!("0x1234")).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/transaction/objects_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rstest::rstest;
use starknet_api::core::{ClassHash, ContractAddress, EthAddress, PatriciaKey};
use starknet_api::state::StorageKey;
use starknet_api::transaction::L2ToL1Payload;
use starknet_api::{class_hash, patricia_key};
use starknet_api::{class_hash, contract_address, patricia_key, storage_key};
use starknet_types_core::felt::Felt;

use crate::execution::call_info::{
Expand Down Expand Up @@ -38,8 +38,8 @@ impl TestExecutionSummary {
num_of_events,
num_of_messages,
class_hash,
storage_address: ContractAddress(patricia_key!(storage_address)),
storage_key: StorageKey(patricia_key!(storage_key)),
storage_address: contract_address!(storage_address),
storage_key: storage_key!(storage_key),
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/transaction/post_execution_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use starknet_api::transaction::{
TransactionVersion,
ValidResourceBounds,
};
use starknet_api::{felt, invoke_tx_args, patricia_key};
use starknet_api::{contract_address, felt, invoke_tx_args, patricia_key};
use starknet_types_core::felt::Felt;

use crate::context::{BlockContext, ChainInfo};
Expand Down Expand Up @@ -101,7 +101,7 @@ fn test_revert_on_overdraft(
// An address to be used as recipient of a transfer.
let recipient_int = 7_u8;
let recipient = felt!(recipient_int);
let recipient_address = ContractAddress(patricia_key!(recipient_int));
let recipient_address = contract_address!(recipient_int);
// Amount expected to be transferred successfully.
let final_received_amount = felt!(80_u8);

Expand Down
6 changes: 3 additions & 3 deletions crates/gateway/src/rpc_state_reader_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use papyrus_rpc::CompiledContractClass;
use serde::Serialize;
use serde_json::json;
use starknet_api::block::BlockNumber;
use starknet_api::core::{ClassHash, ContractAddress, Nonce, PatriciaKey};
use starknet_api::{class_hash, contract_address, felt, patricia_key};
use starknet_api::core::{ClassHash, ContractAddress, PatriciaKey};
use starknet_api::{class_hash, contract_address, felt, nonce, patricia_key};

use crate::config::RpcStateReaderConfig;
use crate::rpc_objects::{
Expand Down Expand Up @@ -131,7 +131,7 @@ async fn test_get_nonce_at() {
let mut server = run_rpc_server().await;
let config = RpcStateReaderConfig { url: server.url(), ..Default::default() };

let expected_result = Nonce(felt!("0x999"));
let expected_result = nonce!(0x999);

let mock = mock_rpc_interaction(
&mut server,
Expand Down
20 changes: 9 additions & 11 deletions crates/papyrus_execution/src/execution_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ use starknet_api::core::{
CompiledClassHash,
ContractAddress,
EntryPointSelector,
Nonce,
PatriciaKey,
};
use starknet_api::state::{StateNumber, ThinStateDiff};
use starknet_api::transaction::{Calldata, Fee};
use starknet_api::{calldata, class_hash, contract_address, felt, patricia_key};
use starknet_api::{calldata, class_hash, contract_address, felt, nonce, patricia_key};
use starknet_types_core::felt::Felt;

use crate::execution_utils::selector_from_name;
Expand Down Expand Up @@ -561,7 +560,7 @@ fn simulate_invoke_from_new_account() {
*NEW_ACCOUNT_ADDRESS,
*DEPRECATED_CONTRACT_ADDRESS,
// the deploy account make the next nonce be 1.
Some(Nonce(felt!(1_u128))),
Some(nonce!(1_u128)),
false,
)
// TODO(yair): Find out how to deploy another contract to test calling a new contract.
Expand Down Expand Up @@ -600,17 +599,16 @@ fn simulate_invoke_from_new_account_validate_and_charge() {
prepare_storage(storage_writer);

// Taken from the trace of the deploy account transaction.
let new_account_address = ContractAddress(patricia_key!(
"0x0153ade9ef510502c4f3b879c049dcc3ad5866706cae665f0d9df9b01e794fdb"
));
let new_account_address =
contract_address!("0x0153ade9ef510502c4f3b879c049dcc3ad5866706cae665f0d9df9b01e794fdb");
let txs = TxsScenarioBuilder::default()
// Invoke contract from a newly deployed account.
.deploy_account()
.invoke_deprecated(
new_account_address,
*DEPRECATED_CONTRACT_ADDRESS,
// the deploy account make the next nonce be 1.
Some(Nonce(felt!(1_u128))),
Some(nonce!(1_u128)),
false,
)
// TODO(yair): Find out how to deploy another contract to test calling a new contract.
Expand Down Expand Up @@ -681,7 +679,7 @@ fn induced_state_diff() {
account_balance -= simulation_results[0].fee_estimation.overall_fee.0;
sequencer_balance += simulation_results[0].fee_estimation.overall_fee.0;
let expected_invoke_deprecated = ThinStateDiff {
nonces: indexmap! {*ACCOUNT_ADDRESS => Nonce(felt!(1_u128))},
nonces: indexmap! {*ACCOUNT_ADDRESS => nonce!(1_u128)},
deployed_contracts: indexmap! {},
storage_diffs: indexmap! {
*TEST_ERC20_CONTRACT_ADDRESS => indexmap!{
Expand All @@ -698,7 +696,7 @@ fn induced_state_diff() {
account_balance -= simulation_results[1].fee_estimation.overall_fee.0;
sequencer_balance += simulation_results[1].fee_estimation.overall_fee.0;
let expected_declare_class = ThinStateDiff {
nonces: indexmap! {*ACCOUNT_ADDRESS => Nonce(felt!(2_u128))},
nonces: indexmap! {*ACCOUNT_ADDRESS => nonce!(2_u128)},
declared_classes: indexmap! {class_hash!(next_declared_class_hash) => CompiledClassHash::default()},
storage_diffs: indexmap! {
*TEST_ERC20_CONTRACT_ADDRESS => indexmap!{
Expand All @@ -716,7 +714,7 @@ fn induced_state_diff() {
account_balance -= simulation_results[2].fee_estimation.overall_fee.0;
sequencer_balance += simulation_results[2].fee_estimation.overall_fee.0;
let expected_declare_deprecated_class = ThinStateDiff {
nonces: indexmap! {*ACCOUNT_ADDRESS => Nonce(felt!(3_u128))},
nonces: indexmap! {*ACCOUNT_ADDRESS => nonce!(3_u128)},
deprecated_declared_classes: vec![class_hash!(next_declared_class_hash)],
storage_diffs: indexmap! {
*TEST_ERC20_CONTRACT_ADDRESS => indexmap!{
Expand All @@ -737,7 +735,7 @@ fn induced_state_diff() {

sequencer_balance += simulation_results[3].fee_estimation.overall_fee.0;
let expected_deploy_account = ThinStateDiff {
nonces: indexmap! {*NEW_ACCOUNT_ADDRESS => Nonce(felt!(1_u128))},
nonces: indexmap! {*NEW_ACCOUNT_ADDRESS => nonce!(1_u128)},
deprecated_declared_classes: vec![],
storage_diffs: indexmap! {
*TEST_ERC20_CONTRACT_ADDRESS => indexmap!{
Expand Down
12 changes: 6 additions & 6 deletions crates/papyrus_execution/src/state_reader_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use papyrus_storage::test_utils::get_test_storage;
use starknet_api::block::{BlockBody, BlockHash, BlockHeader, BlockHeaderWithoutHash, BlockNumber};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce, PatriciaKey};
use starknet_api::hash::StarkHash;
use starknet_api::state::{ContractClass, StateNumber, StorageKey, ThinStateDiff};
use starknet_api::{felt, patricia_key};
use starknet_api::state::{ContractClass, StateNumber, ThinStateDiff};
use starknet_api::{contract_address, felt, patricia_key, storage_key};
use starknet_types_core::felt::Felt;

use crate::objects::PendingData;
Expand All @@ -42,8 +42,8 @@ fn read_state() {
let ((storage_reader, mut storage_writer), _temp_dir) = get_test_storage();

let class_hash0 = ClassHash(2u128.into());
let address0 = ContractAddress(patricia_key!(CONTRACT_ADDRESS));
let storage_key0 = StorageKey(patricia_key!("0x0"));
let address0 = contract_address!(CONTRACT_ADDRESS);
let storage_key0 = storage_key!("0x0");
let storage_value0 = felt!(777_u128);
let storage_value1 = felt!(888_u128);
// The class is not used in the execution, so it can be default.
Expand All @@ -55,10 +55,10 @@ fn read_state() {

let class_hash1 = ClassHash(1u128.into());
let class1 = get_test_deprecated_contract_class();
let address1 = ContractAddress(patricia_key!(DEPRECATED_CONTRACT_ADDRESS));
let address1 = contract_address!(DEPRECATED_CONTRACT_ADDRESS);
let nonce0 = Nonce(felt!(1_u128));

let address2 = ContractAddress(patricia_key!("0x123"));
let address2 = contract_address!("0x123");
let storage_value2 = felt!(999_u128);
let class_hash2 = ClassHash(1234u128.into());
let compiled_class_hash2 = CompiledClassHash(StarkHash::TWO);
Expand Down
8 changes: 4 additions & 4 deletions crates/papyrus_execution/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use starknet_api::transaction::{
InvokeTransactionV1,
TransactionHash,
};
use starknet_api::{calldata, class_hash, contract_address, felt, patricia_key};
use starknet_api::{calldata, class_hash, contract_address, felt, nonce, patricia_key};
use starknet_types_core::felt::Felt;

use crate::execution_utils::selector_from_name;
Expand Down Expand Up @@ -322,7 +322,7 @@ impl TxsScenarioBuilder {
let tx = ExecutableTransactionInput::DeployAccount(
DeployAccountTransaction::V1(DeployAccountTransactionV1 {
max_fee: *MAX_FEE,
nonce: Nonce(felt!(0_u128)),
nonce: nonce!(0_u128),
class_hash: *ACCOUNT_CLASS_HASH,
..Default::default()
}),
Expand All @@ -337,13 +337,13 @@ impl TxsScenarioBuilder {
fn next_nonce(&mut self, sender_address: ContractAddress) -> Nonce {
match self.sender_to_nonce.get_mut(&sender_address) {
Some(current) => {
let res = Nonce(felt!(*current));
let res = nonce!(*current);
*current += 1;
res
}
None => {
self.sender_to_nonce.insert(sender_address, 1);
Nonce(felt!(0_u128))
nonce!(0_u128)
}
}
}
Expand Down
Loading

0 comments on commit e4d16d5

Please sign in to comment.