@@ -12,9 +12,9 @@ use starknet_api::core::{
1212 calculate_contract_address,
1313 ClassHash ,
1414 CompiledClassHash as StarknetAPICompiledClassHash ,
15+ ContractAddress ,
1516 EthAddress ,
1617 Nonce ,
17- PatriciaKey ,
1818} ;
1919use starknet_api:: executable_transaction:: {
2020 DeclareTransaction ,
@@ -48,7 +48,7 @@ use starknet_api::transaction::{
4848 MessageToL1 ,
4949 TransactionVersion ,
5050} ;
51- use starknet_api:: { calldata, declare_tx_args, invoke_tx_args, storage_key } ;
51+ use starknet_api:: { calldata, declare_tx_args, invoke_tx_args} ;
5252use starknet_committer:: block_committer:: input:: {
5353 StarknetStorageKey ,
5454 StarknetStorageValue ,
@@ -377,6 +377,20 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
377377 let ( mut test_manager, mut nonce_manager, _) =
378378 TestManager :: < DictStateReader > :: new_with_default_initial_state ( [ ] ) . await ;
379379 let n_expected_txs = 29 ;
380+ let mut expected_storage_updates: HashMap <
381+ ContractAddress ,
382+ HashMap < StarknetStorageKey , StarknetStorageValue > ,
383+ > = HashMap :: new ( ) ;
384+ let mut update_expected_storage = |address : ContractAddress , key : Felt , value : Felt | {
385+ let key = StarknetStorageKey ( StorageKey ( key. try_into ( ) . unwrap ( ) ) ) ;
386+ let value = StarknetStorageValue ( value) ;
387+ expected_storage_updates
388+ . entry ( address)
389+ . and_modify ( |map| {
390+ map. insert ( key, value) ;
391+ } )
392+ . or_insert_with ( || HashMap :: from ( [ ( key, value) ] ) ) ;
393+ } ;
380394
381395 // Declare a Cairo 0 test contract.
382396 let cairo0_test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo0 ) ;
@@ -411,6 +425,14 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
411425 ) ;
412426 contract_addresses. push ( address) ;
413427 test_manager. add_invoke_tx ( deploy_tx, None ) ;
428+ // Update expected storage diff, if the ctor calldata writes a nonzero value.
429+ if ctor_calldata[ 1 ] != 0 {
430+ update_expected_storage (
431+ address,
432+ Felt :: from ( ctor_calldata[ 0 ] ) ,
433+ Felt :: from ( ctor_calldata[ 1 ] ) ,
434+ ) ;
435+ }
414436 }
415437
416438 // Call set_value(address=85, value=47) on the first contract.
@@ -424,6 +446,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
424446 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
425447 } ;
426448 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
449+ update_expected_storage ( contract_addresses[ 0 ] , Felt :: from ( 85 ) , Felt :: from ( 47 ) ) ;
427450
428451 // Call set_value(address=81, value=0) on the first contract.
429452 // Used to test redundant value update (0 -> 0) and make sure it is not written to on-chain
@@ -458,6 +481,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
458481 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
459482 } ;
460483 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
484+ update_expected_storage ( contract_addresses[ 1 ] , Felt :: from ( 15 ) , Felt :: ONE ) ;
461485
462486 let invoke_tx_args = invoke_tx_args ! {
463487 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
@@ -612,6 +636,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
612636 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
613637 } ;
614638 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
639+ update_expected_storage (
640+ delegate_proxy_address,
641+ * * get_storage_var_address ( "implementation_hash" , & [ ] ) ,
642+ test_class_hash. 0 ,
643+ ) ;
615644
616645 // Call test_get_contract_address(expected_address=delegate_proxy_address) through the delegate
617646 // proxy.
@@ -635,6 +664,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
635664 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
636665 } ;
637666 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
667+ update_expected_storage ( delegate_proxy_address, Felt :: from ( 123 ) , Felt :: from ( 456 ) ) ;
638668
639669 // Call test_get_caller_address(expected_address=account_address) through the delegate proxy.
640670 let invoke_tx_args = invoke_tx_args ! {
@@ -690,6 +720,14 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
690720 nonce : l1_handler_nonce,
691721 selector : l1_handler_selector,
692722 } ;
723+ update_expected_storage (
724+ delegate_proxy_address,
725+ * * get_storage_var_address (
726+ "two_counters" ,
727+ & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
728+ ) ,
729+ * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ,
730+ ) ;
693731
694732 // Call test_library_call_syntactic_sugar from contract_addresses[0] to test library calls
695733 // using the syntactic sugar of 'library_call_<FUNCTION>'.
@@ -704,18 +742,31 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
704742 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
705743 } ;
706744 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
745+ update_expected_storage ( contract_addresses[ 0 ] , Felt :: from ( 444 ) , Felt :: from ( 666 ) ) ;
707746
708747 // Call add_signature_to_counters(index=2021).
748+ let index = Felt :: from ( 2021 ) ;
749+ let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) , Felt :: from( 200 ) ] ) ) ;
709750 let invoke_tx_args = invoke_tx_args ! {
710751 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
711752 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
712753 calldata: create_calldata(
713- contract_addresses[ 0 ] , "add_signature_to_counters" , & [ Felt :: from ( 2021 ) ]
754+ contract_addresses[ 0 ] , "add_signature_to_counters" , & [ index ]
714755 ) ,
715756 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
716- signature: TransactionSignature ( Arc :: new ( vec! [ Felt :: from ( 100 ) , Felt :: from ( 200 ) ] ) ) ,
757+ signature: signature . clone ( ) ,
717758 } ;
718759 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
760+ update_expected_storage (
761+ contract_addresses[ 0 ] ,
762+ * * get_storage_var_address ( "two_counters" , & [ index] ) ,
763+ signature. 0 [ 0 ] ,
764+ ) ;
765+ update_expected_storage (
766+ contract_addresses[ 0 ] ,
767+ * * get_storage_var_address ( "two_counters" , & [ index] ) + Felt :: ONE ,
768+ signature. 0 [ 1 ] ,
769+ ) ;
719770
720771 // Declare test_contract2.
721772 let test_contract2 = FeatureContract :: TestContract2 ;
@@ -751,6 +802,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
751802 signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
752803 } ;
753804 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
805+ update_expected_storage ( contract_addresses[ 1 ] , Felt :: from ( 555 ) , Felt :: from ( 888 ) ) ;
754806
755807 // Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with
756808 // from_address=85, address=666, value=999.
@@ -773,6 +825,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
773825 signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
774826 } ;
775827 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
828+ update_expected_storage ( contract_addresses[ 1 ] , Felt :: from ( 666 ) , Felt :: from ( 999 ) ) ;
776829
777830 // Replace the class of contract_addresses[0] to the class of test_contract2.
778831 let invoke_tx_args = invoke_tx_args ! {
@@ -785,63 +838,6 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
785838 } ;
786839 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
787840
788- // Expected values:
789-
790- // Storage updates:
791- let storage_updates = HashMap :: from ( [
792- (
793- contract_addresses[ 0 ] ,
794- HashMap :: from ( [
795- ( StarknetStorageKey ( storage_key ! ( 85u16 ) ) , StarknetStorageValue ( Felt :: from ( 47 ) ) ) ,
796- ( StarknetStorageKey ( storage_key ! ( 321u16 ) ) , StarknetStorageValue ( Felt :: from ( 543 ) ) ) ,
797- ( StarknetStorageKey ( storage_key ! ( 444u16 ) ) , StarknetStorageValue ( Felt :: from ( 666 ) ) ) ,
798- (
799- StarknetStorageKey ( get_storage_var_address (
800- "two_counters" ,
801- & [ Felt :: from ( 2021 ) ] ,
802- ) ) ,
803- StarknetStorageValue ( Felt :: from ( 100 ) ) ,
804- ) ,
805- (
806- StarknetStorageKey ( StorageKey (
807- PatriciaKey :: try_from (
808- * * get_storage_var_address ( "two_counters" , & [ Felt :: from ( 2021 ) ] )
809- + Felt :: ONE ,
810- )
811- . unwrap ( ) ,
812- ) ) ,
813- StarknetStorageValue ( Felt :: from ( 200 ) ) ,
814- ) ,
815- ] ) ,
816- ) ,
817- (
818- contract_addresses[ 1 ] ,
819- HashMap :: from ( [
820- ( StarknetStorageKey ( storage_key ! ( 15u16 ) ) , StarknetStorageValue ( Felt :: from ( 1 ) ) ) ,
821- ( StarknetStorageKey ( storage_key ! ( 111u16 ) ) , StarknetStorageValue ( Felt :: from ( 987 ) ) ) ,
822- ( StarknetStorageKey ( storage_key ! ( 555u16 ) ) , StarknetStorageValue ( Felt :: from ( 888 ) ) ) ,
823- ( StarknetStorageKey ( storage_key ! ( 666u16 ) ) , StarknetStorageValue ( Felt :: from ( 999 ) ) ) ,
824- ] ) ,
825- ) ,
826- (
827- delegate_proxy_address,
828- HashMap :: from ( [
829- ( StarknetStorageKey ( storage_key ! ( 123u16 ) ) , StarknetStorageValue ( Felt :: from ( 456 ) ) ) ,
830- (
831- StarknetStorageKey ( get_storage_var_address ( "implementation_hash" , & [ ] ) ) ,
832- StarknetStorageValue ( test_class_hash. 0 ) ,
833- ) ,
834- (
835- StarknetStorageKey ( get_storage_var_address (
836- "two_counters" ,
837- & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
838- ) ) ,
839- StarknetStorageValue ( * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ) ,
840- ) ,
841- ] ) ,
842- ) ,
843- ] ) ;
844-
845841 // Expected number of txs.
846842 assert_eq ! ( test_manager. total_txs( ) , n_expected_txs) ;
847843
@@ -857,6 +853,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
857853
858854 // Perform validations.
859855 let perform_global_validations = true ;
860- let partial_state_diff = Some ( & StateDiff { storage_updates, ..Default :: default ( ) } ) ;
856+ let partial_state_diff =
857+ Some ( & StateDiff { storage_updates : expected_storage_updates, ..Default :: default ( ) } ) ;
861858 test_output. perform_validations ( perform_global_validations, partial_state_diff) ;
862859}
0 commit comments