Skip to content

Commit 20e8e13

Browse files
starknet_os_flow_tests: add update_expected_storage util
1 parent bc4830a commit 20e8e13

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

crates/starknet_os_flow_tests/src/tests.rs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,13 @@ use rstest::rstest;
1010
use starknet_api::abi::abi_utils::{get_storage_var_address, selector_from_name};
1111
use starknet_api::contract_class::compiled_class_hash::{HashVersion, HashableCompiledClass};
1212
use starknet_api::contract_class::{ClassInfo, ContractClass};
13-
use starknet_api::core::{
14-
calculate_contract_address,
15-
ClassHash,
16-
ContractAddress,
17-
EthAddress,
18-
Nonce,
19-
};
13+
use starknet_api::core::{calculate_contract_address, ClassHash, EthAddress, Nonce};
2014
use starknet_api::executable_transaction::{
2115
DeclareTransaction,
2216
InvokeTransaction,
2317
L1HandlerTransaction as ExecutableL1HandlerTransaction,
2418
};
2519
use starknet_api::execution_resources::GasAmount;
26-
use starknet_api::state::StorageKey;
2720
use starknet_api::test_utils::declare::declare_tx;
2821
use starknet_api::test_utils::invoke::invoke_tx;
2922
use starknet_api::test_utils::{
@@ -80,6 +73,7 @@ use crate::utils::{
8073
get_class_hash_of_feature_contract,
8174
get_class_info_of_cairo0_contract,
8275
get_class_info_of_feature_contract,
76+
update_expected_storage,
8377
};
8478

8579
pub(crate) static NON_TRIVIAL_RESOURCE_BOUNDS: LazyLock<ValidResourceBounds> =
@@ -422,20 +416,7 @@ async fn test_os_logic(
422416
let (mut test_manager, _) =
423417
TestManager::<DictStateReader>::new_with_default_initial_state([]).await;
424418
let n_expected_txs = 29;
425-
let mut expected_storage_updates: HashMap<
426-
ContractAddress,
427-
HashMap<StarknetStorageKey, StarknetStorageValue>,
428-
> = HashMap::new();
429-
let mut update_expected_storage = |address: ContractAddress, key: Felt, value: Felt| {
430-
let key = StarknetStorageKey(StorageKey(key.try_into().unwrap()));
431-
let value = StarknetStorageValue(value);
432-
expected_storage_updates
433-
.entry(address)
434-
.and_modify(|map| {
435-
map.insert(key, value);
436-
})
437-
.or_insert_with(|| HashMap::from([(key, value)]));
438-
};
419+
let mut expected_storage_updates = HashMap::new();
439420

440421
// Declare a Cairo 0 test contract.
441422
let cairo0_test_contract = FeatureContract::TestContract(CairoVersion::Cairo0);
@@ -471,6 +452,7 @@ async fn test_os_logic(
471452
// Update expected storage diff, if the ctor calldata writes a nonzero value.
472453
if ctor_calldata[1] != 0 {
473454
update_expected_storage(
455+
&mut expected_storage_updates,
474456
address,
475457
Felt::from(ctor_calldata[0]),
476458
Felt::from(ctor_calldata[1]),
@@ -483,7 +465,7 @@ async fn test_os_logic(
483465
let (key, value) = (Felt::from(85), Felt::from(47));
484466
let calldata = create_calldata(contract_addresses[0], "test_storage_read_write", &[key, value]);
485467
test_manager.add_funded_account_invoke(invoke_tx_args! { calldata });
486-
update_expected_storage(contract_addresses[0], key, value);
468+
update_expected_storage(&mut expected_storage_updates, contract_addresses[0], key, value);
487469

488470
// Call set_value(address=81, value=0) on the first contract.
489471
// Used to test redundant value update (0 -> 0) and make sure it is not written to on-chain
@@ -507,7 +489,12 @@ async fn test_os_logic(
507489

508490
let calldata = create_calldata(contract_addresses[1], "read_write_read", &[]);
509491
test_manager.add_funded_account_invoke(invoke_tx_args! { calldata });
510-
update_expected_storage(contract_addresses[1], Felt::from(15), Felt::ONE);
492+
update_expected_storage(
493+
&mut expected_storage_updates,
494+
contract_addresses[1],
495+
Felt::from(15),
496+
Felt::ONE,
497+
);
511498

512499
let calldata = create_calldata(contract_addresses[0], "test_builtins", &[]);
513500
test_manager.add_funded_account_invoke(invoke_tx_args! { calldata });
@@ -624,6 +611,7 @@ async fn test_os_logic(
624611
create_calldata(delegate_proxy_address, "set_implementation_hash", &[test_class_hash.0]);
625612
test_manager.add_funded_account_invoke(invoke_tx_args! { calldata });
626613
update_expected_storage(
614+
&mut expected_storage_updates,
627615
delegate_proxy_address,
628616
**get_storage_var_address("implementation_hash", &[]),
629617
test_class_hash.0,
@@ -643,7 +631,7 @@ async fn test_os_logic(
643631
let calldata =
644632
create_calldata(delegate_proxy_address, "test_storage_read_write", &[key, value]);
645633
test_manager.add_funded_account_invoke(invoke_tx_args! { calldata });
646-
update_expected_storage(delegate_proxy_address, key, value);
634+
update_expected_storage(&mut expected_storage_updates, delegate_proxy_address, key, value);
647635

648636
// Call test_get_caller_address(expected_address=account_address) through the delegate proxy.
649637
let calldata = create_calldata(
@@ -692,6 +680,7 @@ async fn test_os_logic(
692680
selector: l1_handler_selector,
693681
};
694682
update_expected_storage(
683+
&mut expected_storage_updates,
695684
delegate_proxy_address,
696685
**get_storage_var_address(
697686
"two_counters",
@@ -708,7 +697,12 @@ async fn test_os_logic(
708697
&[test_class_hash.0],
709698
);
710699
test_manager.add_funded_account_invoke(invoke_tx_args! { calldata });
711-
update_expected_storage(contract_addresses[0], Felt::from(444), Felt::from(666));
700+
update_expected_storage(
701+
&mut expected_storage_updates,
702+
contract_addresses[0],
703+
Felt::from(444),
704+
Felt::from(666),
705+
);
712706

713707
// Call add_signature_to_counters(index=2021).
714708
let index = Felt::from(2021);
@@ -717,11 +711,13 @@ async fn test_os_logic(
717711
test_manager
718712
.add_funded_account_invoke(invoke_tx_args! { calldata, signature: signature.clone() });
719713
update_expected_storage(
714+
&mut expected_storage_updates,
720715
contract_addresses[0],
721716
**get_storage_var_address("two_counters", &[index]),
722717
signature.0[0],
723718
);
724719
update_expected_storage(
720+
&mut expected_storage_updates,
725721
contract_addresses[0],
726722
**get_storage_var_address("two_counters", &[index]) + Felt::ONE,
727723
signature.0[1],
@@ -755,7 +751,7 @@ async fn test_os_logic(
755751
],
756752
);
757753
test_manager.add_funded_account_invoke(invoke_tx_args! { calldata });
758-
update_expected_storage(contract_addresses[1], key, value);
754+
update_expected_storage(&mut expected_storage_updates, contract_addresses[1], key, value);
759755

760756
// Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with
761757
// from_address=85, address=666, value=999.
@@ -773,7 +769,7 @@ async fn test_os_logic(
773769
],
774770
);
775771
test_manager.add_funded_account_invoke(invoke_tx_args! { calldata });
776-
update_expected_storage(contract_addresses[1], key, value);
772+
update_expected_storage(&mut expected_storage_updates, contract_addresses[1], key, value);
777773

778774
// Replace the class of contract_addresses[0] to the class of test_contract2.
779775
let calldata = create_calldata(

crates/starknet_os_flow_tests/src/utils.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,23 @@ fn fetch_storage_proofs_from_state_maps(
499499
)
500500
.unwrap()
501501
}
502+
503+
/// Utility method to update a map of expected storage updates.
504+
pub(crate) fn update_expected_storage(
505+
expected_storage_updates: &mut HashMap<
506+
ContractAddress,
507+
HashMap<StarknetStorageKey, StarknetStorageValue>,
508+
>,
509+
address: ContractAddress,
510+
key: Felt,
511+
value: Felt,
512+
) {
513+
let key = StarknetStorageKey(StorageKey(key.try_into().unwrap()));
514+
let value = StarknetStorageValue(value);
515+
expected_storage_updates
516+
.entry(address)
517+
.and_modify(|map| {
518+
map.insert(key, value);
519+
})
520+
.or_insert_with(|| HashMap::from([(key, value)]));
521+
}

0 commit comments

Comments
 (0)