@@ -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 ,
@@ -373,6 +373,20 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
373373 let ( mut test_manager, mut nonce_manager, _) =
374374 TestManager :: < DictStateReader > :: new_with_default_initial_state ( [ ] ) . await ;
375375 let n_expected_txs = 29 ;
376+ let mut expected_storage_updates: HashMap <
377+ ContractAddress ,
378+ HashMap < StarknetStorageKey , StarknetStorageValue > ,
379+ > = HashMap :: new ( ) ;
380+ let mut update_expected_storage = |address : ContractAddress , key : Felt , value : Felt | {
381+ let key = StarknetStorageKey ( StorageKey ( key. try_into ( ) . unwrap ( ) ) ) ;
382+ let value = StarknetStorageValue ( value) ;
383+ expected_storage_updates
384+ . entry ( address)
385+ . and_modify ( |map| {
386+ map. insert ( key, value) ;
387+ } )
388+ . or_insert_with ( || HashMap :: from ( [ ( key, value) ] ) ) ;
389+ } ;
376390
377391 // Declare a Cairo 0 test contract.
378392 let cairo0_test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo0 ) ;
@@ -407,19 +421,27 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
407421 ) ;
408422 contract_addresses. push ( address) ;
409423 test_manager. add_invoke_tx ( deploy_tx, None ) ;
424+ // Update expected storage diff, if the ctor calldata writes a nonzero value.
425+ if ctor_calldata[ 1 ] != 0 {
426+ update_expected_storage (
427+ address,
428+ Felt :: from ( ctor_calldata[ 0 ] ) ,
429+ Felt :: from ( ctor_calldata[ 1 ] ) ,
430+ ) ;
431+ }
410432 }
411433
412434 // Call set_value(address=85, value=47) on the first contract.
413435 // Used to test normal value update and make sure it is written to on-chain data.
436+ let ( key, value) = ( Felt :: from ( 85 ) , Felt :: from ( 47 ) ) ;
414437 let invoke_tx_args = invoke_tx_args ! {
415438 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
416439 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
417- calldata: create_calldata(
418- contract_addresses[ 0 ] , "test_storage_read_write" , & [ Felt :: from( 85 ) , Felt :: from( 47 ) ]
419- ) ,
440+ calldata: create_calldata( contract_addresses[ 0 ] , "test_storage_read_write" , & [ key, value] ) ,
420441 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
421442 } ;
422443 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
444+ update_expected_storage ( contract_addresses[ 0 ] , key, value) ;
423445
424446 // Call set_value(address=81, value=0) on the first contract.
425447 // Used to test redundant value update (0 -> 0) and make sure it is not written to on-chain
@@ -454,6 +476,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
454476 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
455477 } ;
456478 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
479+ update_expected_storage ( contract_addresses[ 1 ] , Felt :: from ( 15 ) , Felt :: ONE ) ;
457480
458481 let invoke_tx_args = invoke_tx_args ! {
459482 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
@@ -608,6 +631,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
608631 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
609632 } ;
610633 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
634+ update_expected_storage (
635+ delegate_proxy_address,
636+ * * get_storage_var_address ( "implementation_hash" , & [ ] ) ,
637+ test_class_hash. 0 ,
638+ ) ;
611639
612640 // Call test_get_contract_address(expected_address=delegate_proxy_address) through the delegate
613641 // proxy.
@@ -622,15 +650,15 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
622650 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
623651
624652 // Call set_value(address=123, value=456) through the delegate proxy.
653+ let ( key, value) = ( Felt :: from ( 123 ) , Felt :: from ( 456 ) ) ;
625654 let invoke_tx_args = invoke_tx_args ! {
626655 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
627656 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
628- calldata: create_calldata(
629- delegate_proxy_address, "test_storage_read_write" , & [ Felt :: from( 123 ) , Felt :: from( 456 ) ]
630- ) ,
657+ calldata: create_calldata( delegate_proxy_address, "test_storage_read_write" , & [ key, value] ) ,
631658 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
632659 } ;
633660 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
661+ update_expected_storage ( delegate_proxy_address, key, value) ;
634662
635663 // Call test_get_caller_address(expected_address=account_address) through the delegate proxy.
636664 let invoke_tx_args = invoke_tx_args ! {
@@ -686,6 +714,14 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
686714 nonce : l1_handler_nonce,
687715 selector : l1_handler_selector,
688716 } ;
717+ update_expected_storage (
718+ delegate_proxy_address,
719+ * * get_storage_var_address (
720+ "two_counters" ,
721+ & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
722+ ) ,
723+ * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ,
724+ ) ;
689725
690726 // Call test_library_call_syntactic_sugar from contract_addresses[0] to test library calls
691727 // using the syntactic sugar of 'library_call_<FUNCTION>'.
@@ -700,18 +736,31 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
700736 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
701737 } ;
702738 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
739+ update_expected_storage ( contract_addresses[ 0 ] , Felt :: from ( 444 ) , Felt :: from ( 666 ) ) ;
703740
704741 // Call add_signature_to_counters(index=2021).
742+ let index = Felt :: from ( 2021 ) ;
743+ let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) , Felt :: from( 200 ) ] ) ) ;
705744 let invoke_tx_args = invoke_tx_args ! {
706745 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
707746 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
708747 calldata: create_calldata(
709- contract_addresses[ 0 ] , "add_signature_to_counters" , & [ Felt :: from ( 2021 ) ]
748+ contract_addresses[ 0 ] , "add_signature_to_counters" , & [ index ]
710749 ) ,
711750 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
712- signature: TransactionSignature ( Arc :: new ( vec! [ Felt :: from ( 100 ) , Felt :: from ( 200 ) ] ) ) ,
751+ signature: signature . clone ( ) ,
713752 } ;
714753 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
754+ update_expected_storage (
755+ contract_addresses[ 0 ] ,
756+ * * get_storage_var_address ( "two_counters" , & [ index] ) ,
757+ signature. 0 [ 0 ] ,
758+ ) ;
759+ update_expected_storage (
760+ contract_addresses[ 0 ] ,
761+ * * get_storage_var_address ( "two_counters" , & [ index] ) + Felt :: ONE ,
762+ signature. 0 [ 1 ] ,
763+ ) ;
715764
716765 // Declare test_contract2.
717766 let test_contract2 = FeatureContract :: TestContract2 ;
@@ -729,6 +778,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
729778 . add_cairo0_declare_tx ( tx, StarknetAPICompiledClassHash ( test_contract2_class_hash. 0 ) ) ;
730779
731780 // Use library_call to call test_contract2.test_storage_write(address=555, value=888).
781+ let ( key, value) = ( Felt :: from ( 555 ) , Felt :: from ( 888 ) ) ;
732782 let invoke_tx_args = invoke_tx_args ! {
733783 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
734784 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
@@ -739,17 +789,19 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
739789 test_contract2_class_hash. 0 ,
740790 selector_from_name( "test_storage_write" ) . 0 ,
741791 Felt :: TWO ,
742- Felt :: from ( 555 ) ,
743- Felt :: from ( 888 )
792+ key ,
793+ value
744794 ]
745795 ) ,
746796 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
747797 signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
748798 } ;
749799 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
800+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
750801
751802 // Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with
752803 // from_address=85, address=666, value=999.
804+ let ( key, value) = ( Felt :: from ( 666 ) , Felt :: from ( 999 ) ) ;
753805 let invoke_tx_args = invoke_tx_args ! {
754806 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
755807 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
@@ -761,14 +813,15 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
761813 selector_from_name( "test_l1_handler_storage_write" ) . 0 ,
762814 Felt :: THREE ,
763815 Felt :: from( 85 ) ,
764- Felt :: from ( 666 ) ,
765- Felt :: from ( 999 )
816+ key ,
817+ value
766818 ]
767819 ) ,
768820 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
769821 signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
770822 } ;
771823 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
824+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
772825
773826 // Replace the class of contract_addresses[0] to the class of test_contract2.
774827 let invoke_tx_args = invoke_tx_args ! {
@@ -781,63 +834,6 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
781834 } ;
782835 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
783836
784- // Expected values:
785-
786- // Storage updates:
787- let storage_updates = HashMap :: from ( [
788- (
789- contract_addresses[ 0 ] ,
790- HashMap :: from ( [
791- ( StarknetStorageKey ( storage_key ! ( 85u16 ) ) , StarknetStorageValue ( Felt :: from ( 47 ) ) ) ,
792- ( StarknetStorageKey ( storage_key ! ( 321u16 ) ) , StarknetStorageValue ( Felt :: from ( 543 ) ) ) ,
793- ( StarknetStorageKey ( storage_key ! ( 444u16 ) ) , StarknetStorageValue ( Felt :: from ( 666 ) ) ) ,
794- (
795- StarknetStorageKey ( get_storage_var_address (
796- "two_counters" ,
797- & [ Felt :: from ( 2021 ) ] ,
798- ) ) ,
799- StarknetStorageValue ( Felt :: from ( 100 ) ) ,
800- ) ,
801- (
802- StarknetStorageKey ( StorageKey (
803- PatriciaKey :: try_from (
804- * * get_storage_var_address ( "two_counters" , & [ Felt :: from ( 2021 ) ] )
805- + Felt :: ONE ,
806- )
807- . unwrap ( ) ,
808- ) ) ,
809- StarknetStorageValue ( Felt :: from ( 200 ) ) ,
810- ) ,
811- ] ) ,
812- ) ,
813- (
814- contract_addresses[ 1 ] ,
815- HashMap :: from ( [
816- ( StarknetStorageKey ( storage_key ! ( 15u16 ) ) , StarknetStorageValue ( Felt :: from ( 1 ) ) ) ,
817- ( StarknetStorageKey ( storage_key ! ( 111u16 ) ) , StarknetStorageValue ( Felt :: from ( 987 ) ) ) ,
818- ( StarknetStorageKey ( storage_key ! ( 555u16 ) ) , StarknetStorageValue ( Felt :: from ( 888 ) ) ) ,
819- ( StarknetStorageKey ( storage_key ! ( 666u16 ) ) , StarknetStorageValue ( Felt :: from ( 999 ) ) ) ,
820- ] ) ,
821- ) ,
822- (
823- delegate_proxy_address,
824- HashMap :: from ( [
825- ( StarknetStorageKey ( storage_key ! ( 123u16 ) ) , StarknetStorageValue ( Felt :: from ( 456 ) ) ) ,
826- (
827- StarknetStorageKey ( get_storage_var_address ( "implementation_hash" , & [ ] ) ) ,
828- StarknetStorageValue ( test_class_hash. 0 ) ,
829- ) ,
830- (
831- StarknetStorageKey ( get_storage_var_address (
832- "two_counters" ,
833- & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
834- ) ) ,
835- StarknetStorageValue ( * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ) ,
836- ) ,
837- ] ) ,
838- ) ,
839- ] ) ;
840-
841837 // Expected number of txs.
842838 assert_eq ! ( test_manager. total_txs( ) , n_expected_txs) ;
843839
@@ -853,6 +849,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
853849
854850 // Perform validations.
855851 let perform_global_validations = true ;
856- let partial_state_diff = Some ( & StateDiff { storage_updates, ..Default :: default ( ) } ) ;
852+ let partial_state_diff =
853+ Some ( & StateDiff { storage_updates : expected_storage_updates, ..Default :: default ( ) } ) ;
857854 test_output. perform_validations ( perform_global_validations, partial_state_diff) ;
858855}
0 commit comments