@@ -8,7 +8,13 @@ use blockifier_test_utils::contracts::FeatureContract;
88use  rstest:: rstest; 
99use  starknet_api:: abi:: abi_utils:: { get_storage_var_address,  selector_from_name} ; 
1010use  starknet_api:: contract_class:: compiled_class_hash:: HashVersion ; 
11- use  starknet_api:: core:: { calculate_contract_address,  ClassHash ,  EthAddress ,  Nonce ,  PatriciaKey } ; 
11+ use  starknet_api:: core:: { 
12+     calculate_contract_address, 
13+     ClassHash , 
14+     ContractAddress , 
15+     EthAddress , 
16+     Nonce , 
17+ } ; 
1218use  starknet_api:: executable_transaction:: { 
1319    DeclareTransaction , 
1420    L1HandlerTransaction  as  ExecutableL1HandlerTransaction , 
@@ -41,7 +47,7 @@ use starknet_api::transaction::{
4147    MessageToL1 , 
4248    TransactionVersion , 
4349} ; 
44- use  starknet_api:: { calldata,  declare_tx_args,  invoke_tx_args,  storage_key } ; 
50+ use  starknet_api:: { calldata,  declare_tx_args,  invoke_tx_args} ; 
4551use  starknet_committer:: block_committer:: input:: { 
4652    StarknetStorageKey , 
4753    StarknetStorageValue , 
@@ -402,6 +408,20 @@ async fn test_os_logic(
402408    let  ( mut  test_manager,  _)  =
403409        TestManager :: < DictStateReader > :: new_with_default_initial_state ( [ ] ) . await ; 
404410    let  n_expected_txs = 29 ; 
411+     let  mut  expected_storage_updates:  HashMap < 
412+         ContractAddress , 
413+         HashMap < StarknetStorageKey ,  StarknetStorageValue > , 
414+     >  = HashMap :: new ( ) ; 
415+     let  mut  update_expected_storage = |address :  ContractAddress ,  key :  Felt ,  value :  Felt | { 
416+         let  key = StarknetStorageKey ( StorageKey ( key. try_into ( ) . unwrap ( ) ) ) ; 
417+         let  value = StarknetStorageValue ( value) ; 
418+         expected_storage_updates
419+             . entry ( address) 
420+             . and_modify ( |map| { 
421+                 map. insert ( key,  value) ; 
422+             } ) 
423+             . or_insert_with ( || HashMap :: from ( [ ( key,  value) ] ) ) ; 
424+     } ; 
405425
406426    // Declare a Cairo 0 test contract. 
407427    let  cairo0_test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo0 ) ; 
@@ -434,16 +454,22 @@ async fn test_os_logic(
434454        ) ; 
435455        contract_addresses. push ( address) ; 
436456        test_manager. add_invoke_tx ( deploy_tx,  None ) ; 
457+         // Update expected storage diff, if the ctor calldata writes a nonzero value. 
458+         if  ctor_calldata[ 1 ]  != 0  { 
459+             update_expected_storage ( 
460+                 address, 
461+                 Felt :: from ( ctor_calldata[ 0 ] ) , 
462+                 Felt :: from ( ctor_calldata[ 1 ] ) , 
463+             ) ; 
464+         } 
437465    } 
438466
439467    // Call set_value(address=85, value=47) on the first contract. 
440468    // Used to test normal value update and make sure it is written to on-chain data. 
441-     let  calldata = create_calldata ( 
442-         contract_addresses[ 0 ] , 
443-         "test_storage_read_write" , 
444-         & [ Felt :: from ( 85 ) ,  Felt :: from ( 47 ) ] , 
445-     ) ; 
469+     let  ( key,  value)  = ( Felt :: from ( 85 ) ,  Felt :: from ( 47 ) ) ; 
470+     let  calldata = create_calldata ( contract_addresses[ 0 ] ,  "test_storage_read_write" ,  & [ key,  value] ) ; 
446471    test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata } ) ; 
472+     update_expected_storage ( contract_addresses[ 0 ] ,  key,  value) ; 
447473
448474    // Call set_value(address=81, value=0) on the first contract. 
449475    // Used to test redundant value update (0 -> 0) and make sure it is not written to on-chain 
@@ -467,6 +493,7 @@ async fn test_os_logic(
467493
468494    let  calldata = create_calldata ( contract_addresses[ 1 ] ,  "read_write_read" ,  & [ ] ) ; 
469495    test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata } ) ; 
496+     update_expected_storage ( contract_addresses[ 1 ] ,  Felt :: from ( 15 ) ,  Felt :: ONE ) ; 
470497
471498    let  calldata = create_calldata ( contract_addresses[ 0 ] ,  "test_builtins" ,  & [ ] ) ; 
472499    test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata } ) ; 
@@ -582,6 +609,11 @@ async fn test_os_logic(
582609    let  calldata =
583610        create_calldata ( delegate_proxy_address,  "set_implementation_hash" ,  & [ test_class_hash. 0 ] ) ; 
584611    test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata } ) ; 
612+     update_expected_storage ( 
613+         delegate_proxy_address, 
614+         * * get_storage_var_address ( "implementation_hash" ,  & [ ] ) , 
615+         test_class_hash. 0 , 
616+     ) ; 
585617
586618    // Call test_get_contract_address(expected_address=delegate_proxy_address) through the delegate 
587619    // proxy. 
@@ -593,12 +625,11 @@ async fn test_os_logic(
593625    test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata } ) ; 
594626
595627    // Call set_value(address=123, value=456) through the delegate proxy. 
596-     let  calldata = create_calldata ( 
597-         delegate_proxy_address, 
598-         "test_storage_read_write" , 
599-         & [ Felt :: from ( 123 ) ,  Felt :: from ( 456 ) ] , 
600-     ) ; 
628+     let  ( key,  value)  = ( Felt :: from ( 123 ) ,  Felt :: from ( 456 ) ) ; 
629+     let  calldata =
630+         create_calldata ( delegate_proxy_address,  "test_storage_read_write" ,  & [ key,  value] ) ; 
601631    test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata } ) ; 
632+     update_expected_storage ( delegate_proxy_address,  key,  value) ; 
602633
603634    // Call test_get_caller_address(expected_address=account_address) through the delegate proxy. 
604635    let  calldata = create_calldata ( 
@@ -646,6 +677,14 @@ async fn test_os_logic(
646677        nonce :  l1_handler_nonce, 
647678        selector :  l1_handler_selector, 
648679    } ; 
680+     update_expected_storage ( 
681+         delegate_proxy_address, 
682+         * * get_storage_var_address ( 
683+             "two_counters" , 
684+             & [ Felt :: from ( expected_message_to_l2. from_address ) ] , 
685+         ) , 
686+         * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) , 
687+     ) ; 
649688
650689    // Call test_library_call_syntactic_sugar from contract_addresses[0] to test library calls 
651690    // using the syntactic sugar of 'library_call_<FUNCTION>'. 
@@ -655,12 +694,24 @@ async fn test_os_logic(
655694        & [ test_class_hash. 0 ] , 
656695    ) ; 
657696    test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata } ) ; 
697+     update_expected_storage ( contract_addresses[ 0 ] ,  Felt :: from ( 444 ) ,  Felt :: from ( 666 ) ) ; 
658698
659699    // Call add_signature_to_counters(index=2021). 
660-     let  calldata = 
661-          create_calldata ( contract_addresses[ 0 ] ,  "add_signature_to_counters" ,  & [ Felt :: from ( 2021 ) ] ) ; 
700+     let  index =  Felt :: from ( 2021 ) ; 
701+     let  calldata =  create_calldata ( contract_addresses[ 0 ] ,  "add_signature_to_counters" ,  & [ index ] ) ; 
662702    let  signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) ,  Felt :: from( 200 ) ] ) ) ; 
663-     test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata,  signature } ) ; 
703+     test_manager
704+         . add_funded_account_invoke ( invoke_tx_args !  {  calldata,  signature:  signature. clone( )  } ) ; 
705+     update_expected_storage ( 
706+         contract_addresses[ 0 ] , 
707+         * * get_storage_var_address ( "two_counters" ,  & [ index] ) , 
708+         signature. 0 [ 0 ] , 
709+     ) ; 
710+     update_expected_storage ( 
711+         contract_addresses[ 0 ] , 
712+         * * get_storage_var_address ( "two_counters" ,  & [ index] )  + Felt :: ONE , 
713+         signature. 0 [ 1 ] , 
714+     ) ; 
664715
665716    // Declare test_contract2. 
666717    let  test_contract2 = FeatureContract :: TestContract2 ; 
@@ -677,21 +728,24 @@ async fn test_os_logic(
677728    test_manager. add_cairo0_declare_tx ( tx,  test_contract2_class_hash) ; 
678729
679730    // Use library_call to call test_contract2.test_storage_write(address=555, value=888). 
731+     let  ( key,  value)  = ( Felt :: from ( 555 ) ,  Felt :: from ( 888 ) ) ; 
680732    let  calldata = create_calldata ( 
681733        contract_addresses[ 1 ] , 
682734        "test_library_call" , 
683735        & [ 
684736            test_contract2_class_hash. 0 , 
685737            selector_from_name ( "test_storage_write" ) . 0 , 
686738            Felt :: TWO , 
687-             Felt :: from ( 555 ) , 
688-             Felt :: from ( 888 ) , 
739+             key , 
740+             value , 
689741        ] , 
690742    ) ; 
691743    test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata } ) ; 
744+     update_expected_storage ( contract_addresses[ 1 ] ,  key,  value) ; 
692745
693746    // Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with 
694747    // from_address=85, address=666, value=999. 
748+     let  ( key,  value)  = ( Felt :: from ( 666 ) ,  Felt :: from ( 999 ) ) ; 
695749    let  calldata = create_calldata ( 
696750        contract_addresses[ 1 ] , 
697751        "test_library_call_l1_handler" , 
@@ -700,11 +754,12 @@ async fn test_os_logic(
700754            selector_from_name ( "test_l1_handler_storage_write" ) . 0 , 
701755            Felt :: THREE , 
702756            Felt :: from ( 85 ) , 
703-             Felt :: from ( 666 ) , 
704-             Felt :: from ( 999 ) , 
757+             key , 
758+             value , 
705759        ] , 
706760    ) ; 
707761    test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata } ) ; 
762+     update_expected_storage ( contract_addresses[ 1 ] ,  key,  value) ; 
708763
709764    // Replace the class of contract_addresses[0] to the class of test_contract2. 
710765    let  calldata = create_calldata ( 
@@ -714,63 +769,6 @@ async fn test_os_logic(
714769    ) ; 
715770    test_manager. add_funded_account_invoke ( invoke_tx_args !  {  calldata } ) ; 
716771
717-     // Expected values: 
718- 
719-     // Storage updates: 
720-     let  storage_updates = HashMap :: from ( [ 
721-         ( 
722-             contract_addresses[ 0 ] , 
723-             HashMap :: from ( [ 
724-                 ( StarknetStorageKey ( storage_key ! ( 85u16 ) ) ,  StarknetStorageValue ( Felt :: from ( 47 ) ) ) , 
725-                 ( StarknetStorageKey ( storage_key ! ( 321u16 ) ) ,  StarknetStorageValue ( Felt :: from ( 543 ) ) ) , 
726-                 ( StarknetStorageKey ( storage_key ! ( 444u16 ) ) ,  StarknetStorageValue ( Felt :: from ( 666 ) ) ) , 
727-                 ( 
728-                     StarknetStorageKey ( get_storage_var_address ( 
729-                         "two_counters" , 
730-                         & [ Felt :: from ( 2021 ) ] , 
731-                     ) ) , 
732-                     StarknetStorageValue ( Felt :: from ( 100 ) ) , 
733-                 ) , 
734-                 ( 
735-                     StarknetStorageKey ( StorageKey ( 
736-                         PatriciaKey :: try_from ( 
737-                             * * get_storage_var_address ( "two_counters" ,  & [ Felt :: from ( 2021 ) ] ) 
738-                                 + Felt :: ONE , 
739-                         ) 
740-                         . unwrap ( ) , 
741-                     ) ) , 
742-                     StarknetStorageValue ( Felt :: from ( 200 ) ) , 
743-                 ) , 
744-             ] ) , 
745-         ) , 
746-         ( 
747-             contract_addresses[ 1 ] , 
748-             HashMap :: from ( [ 
749-                 ( StarknetStorageKey ( storage_key ! ( 15u16 ) ) ,  StarknetStorageValue ( Felt :: from ( 1 ) ) ) , 
750-                 ( StarknetStorageKey ( storage_key ! ( 111u16 ) ) ,  StarknetStorageValue ( Felt :: from ( 987 ) ) ) , 
751-                 ( StarknetStorageKey ( storage_key ! ( 555u16 ) ) ,  StarknetStorageValue ( Felt :: from ( 888 ) ) ) , 
752-                 ( StarknetStorageKey ( storage_key ! ( 666u16 ) ) ,  StarknetStorageValue ( Felt :: from ( 999 ) ) ) , 
753-             ] ) , 
754-         ) , 
755-         ( 
756-             delegate_proxy_address, 
757-             HashMap :: from ( [ 
758-                 ( StarknetStorageKey ( storage_key ! ( 123u16 ) ) ,  StarknetStorageValue ( Felt :: from ( 456 ) ) ) , 
759-                 ( 
760-                     StarknetStorageKey ( get_storage_var_address ( "implementation_hash" ,  & [ ] ) ) , 
761-                     StarknetStorageValue ( test_class_hash. 0 ) , 
762-                 ) , 
763-                 ( 
764-                     StarknetStorageKey ( get_storage_var_address ( 
765-                         "two_counters" , 
766-                         & [ Felt :: from ( expected_message_to_l2. from_address ) ] , 
767-                     ) ) , 
768-                     StarknetStorageValue ( * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ) , 
769-                 ) , 
770-             ] ) , 
771-         ) , 
772-     ] ) ; 
773- 
774772    // Expected number of txs. 
775773    assert_eq ! ( test_manager. total_txs( ) ,  n_expected_txs) ; 
776774
@@ -787,6 +785,7 @@ async fn test_os_logic(
787785
788786    // Perform validations. 
789787    let  perform_global_validations = true ; 
790-     let  partial_state_diff = Some ( & StateDiff  {  storage_updates,  ..Default :: default ( )  } ) ; 
788+     let  partial_state_diff =
789+         Some ( & StateDiff  {  storage_updates :  expected_storage_updates,  ..Default :: default ( )  } ) ; 
791790    test_output. perform_validations ( perform_global_validations,  partial_state_diff) ; 
792791} 
0 commit comments