@@ -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 ,
@@ -376,6 +376,20 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
376376 let ( mut test_manager, mut nonce_manager, _) =
377377 TestManager :: < DictStateReader > :: new_with_default_initial_state ( [ ] ) . await ;
378378 let n_expected_txs = 29 ;
379+ let mut expected_storage_updates: HashMap <
380+ ContractAddress ,
381+ HashMap < StarknetStorageKey , StarknetStorageValue > ,
382+ > = HashMap :: new ( ) ;
383+ let mut update_expected_storage = |address : ContractAddress , key : Felt , value : Felt | {
384+ let key = StarknetStorageKey ( StorageKey ( key. try_into ( ) . unwrap ( ) ) ) ;
385+ let value = StarknetStorageValue ( value) ;
386+ expected_storage_updates
387+ . entry ( address)
388+ . and_modify ( |map| {
389+ map. insert ( key, value) ;
390+ } )
391+ . or_insert_with ( || HashMap :: from ( [ ( key, value) ] ) ) ;
392+ } ;
379393
380394 // Declare a Cairo 0 test contract.
381395 let cairo0_test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo0 ) ;
@@ -410,19 +424,27 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
410424 ) ;
411425 contract_addresses. push ( address) ;
412426 test_manager. add_invoke_tx ( deploy_tx, None ) ;
427+ // Update expected storage diff, if the ctor calldata writes a nonzero value.
428+ if ctor_calldata[ 1 ] != 0 {
429+ update_expected_storage (
430+ address,
431+ Felt :: from ( ctor_calldata[ 0 ] ) ,
432+ Felt :: from ( ctor_calldata[ 1 ] ) ,
433+ ) ;
434+ }
413435 }
414436
415437 // Call set_value(address=85, value=47) on the first contract.
416438 // Used to test normal value update and make sure it is written to on-chain data.
439+ let ( key, value) = ( Felt :: from ( 85 ) , Felt :: from ( 47 ) ) ;
417440 let invoke_tx_args = invoke_tx_args ! {
418441 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
419442 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
420- calldata: create_calldata(
421- contract_addresses[ 0 ] , "test_storage_read_write" , & [ Felt :: from( 85 ) , Felt :: from( 47 ) ]
422- ) ,
443+ calldata: create_calldata( contract_addresses[ 0 ] , "test_storage_read_write" , & [ key, value] ) ,
423444 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
424445 } ;
425446 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
447+ update_expected_storage ( contract_addresses[ 0 ] , key, value) ;
426448
427449 // Call set_value(address=81, value=0) on the first contract.
428450 // Used to test redundant value update (0 -> 0) and make sure it is not written to on-chain
@@ -457,6 +479,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
457479 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
458480 } ;
459481 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
482+ update_expected_storage ( contract_addresses[ 1 ] , Felt :: from ( 15 ) , Felt :: ONE ) ;
460483
461484 let invoke_tx_args = invoke_tx_args ! {
462485 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
@@ -611,6 +634,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
611634 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
612635 } ;
613636 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
637+ update_expected_storage (
638+ delegate_proxy_address,
639+ * * get_storage_var_address ( "implementation_hash" , & [ ] ) ,
640+ test_class_hash. 0 ,
641+ ) ;
614642
615643 // Call test_get_contract_address(expected_address=delegate_proxy_address) through the delegate
616644 // proxy.
@@ -625,15 +653,15 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
625653 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
626654
627655 // Call set_value(address=123, value=456) through the delegate proxy.
656+ let ( key, value) = ( Felt :: from ( 123 ) , Felt :: from ( 456 ) ) ;
628657 let invoke_tx_args = invoke_tx_args ! {
629658 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
630659 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
631- calldata: create_calldata(
632- delegate_proxy_address, "test_storage_read_write" , & [ Felt :: from( 123 ) , Felt :: from( 456 ) ]
633- ) ,
660+ calldata: create_calldata( delegate_proxy_address, "test_storage_read_write" , & [ key, value] ) ,
634661 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
635662 } ;
636663 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
664+ update_expected_storage ( delegate_proxy_address, key, value) ;
637665
638666 // Call test_get_caller_address(expected_address=account_address) through the delegate proxy.
639667 let invoke_tx_args = invoke_tx_args ! {
@@ -689,6 +717,14 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
689717 nonce : l1_handler_nonce,
690718 selector : l1_handler_selector,
691719 } ;
720+ update_expected_storage (
721+ delegate_proxy_address,
722+ * * get_storage_var_address (
723+ "two_counters" ,
724+ & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
725+ ) ,
726+ * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ,
727+ ) ;
692728
693729 // Call test_library_call_syntactic_sugar from contract_addresses[0] to test library calls
694730 // using the syntactic sugar of 'library_call_<FUNCTION>'.
@@ -703,18 +739,31 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
703739 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
704740 } ;
705741 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
742+ update_expected_storage ( contract_addresses[ 0 ] , Felt :: from ( 444 ) , Felt :: from ( 666 ) ) ;
706743
707744 // Call add_signature_to_counters(index=2021).
745+ let index = Felt :: from ( 2021 ) ;
746+ let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) , Felt :: from( 200 ) ] ) ) ;
708747 let invoke_tx_args = invoke_tx_args ! {
709748 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
710749 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
711750 calldata: create_calldata(
712- contract_addresses[ 0 ] , "add_signature_to_counters" , & [ Felt :: from ( 2021 ) ]
751+ contract_addresses[ 0 ] , "add_signature_to_counters" , & [ index ]
713752 ) ,
714753 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
715- signature: TransactionSignature ( Arc :: new ( vec! [ Felt :: from ( 100 ) , Felt :: from ( 200 ) ] ) ) ,
754+ signature: signature . clone ( ) ,
716755 } ;
717756 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
757+ update_expected_storage (
758+ contract_addresses[ 0 ] ,
759+ * * get_storage_var_address ( "two_counters" , & [ index] ) ,
760+ signature. 0 [ 0 ] ,
761+ ) ;
762+ update_expected_storage (
763+ contract_addresses[ 0 ] ,
764+ * * get_storage_var_address ( "two_counters" , & [ index] ) + Felt :: ONE ,
765+ signature. 0 [ 1 ] ,
766+ ) ;
718767
719768 // Declare test_contract2.
720769 let test_contract2 = FeatureContract :: TestContract2 ;
@@ -732,6 +781,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
732781 . add_cairo0_declare_tx ( tx, StarknetAPICompiledClassHash ( test_contract2_class_hash. 0 ) ) ;
733782
734783 // Use library_call to call test_contract2.test_storage_write(address=555, value=888).
784+ let ( key, value) = ( Felt :: from ( 555 ) , Felt :: from ( 888 ) ) ;
735785 let invoke_tx_args = invoke_tx_args ! {
736786 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
737787 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
@@ -742,17 +792,19 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
742792 test_contract2_class_hash. 0 ,
743793 selector_from_name( "test_storage_write" ) . 0 ,
744794 Felt :: TWO ,
745- Felt :: from ( 555 ) ,
746- Felt :: from ( 888 )
795+ key ,
796+ value
747797 ]
748798 ) ,
749799 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
750800 signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
751801 } ;
752802 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
803+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
753804
754805 // Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with
755806 // from_address=85, address=666, value=999.
807+ let ( key, value) = ( Felt :: from ( 666 ) , Felt :: from ( 999 ) ) ;
756808 let invoke_tx_args = invoke_tx_args ! {
757809 sender_address: * FUNDED_ACCOUNT_ADDRESS ,
758810 nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
@@ -764,14 +816,15 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
764816 selector_from_name( "test_l1_handler_storage_write" ) . 0 ,
765817 Felt :: THREE ,
766818 Felt :: from( 85 ) ,
767- Felt :: from ( 666 ) ,
768- Felt :: from ( 999 )
819+ key ,
820+ value
769821 ]
770822 ) ,
771823 resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
772824 signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
773825 } ;
774826 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
827+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
775828
776829 // Replace the class of contract_addresses[0] to the class of test_contract2.
777830 let invoke_tx_args = invoke_tx_args ! {
@@ -784,63 +837,6 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
784837 } ;
785838 test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
786839
787- // Expected values:
788-
789- // Storage updates:
790- let storage_updates = HashMap :: from ( [
791- (
792- contract_addresses[ 0 ] ,
793- HashMap :: from ( [
794- ( StarknetStorageKey ( storage_key ! ( 85u16 ) ) , StarknetStorageValue ( Felt :: from ( 47 ) ) ) ,
795- ( StarknetStorageKey ( storage_key ! ( 321u16 ) ) , StarknetStorageValue ( Felt :: from ( 543 ) ) ) ,
796- ( StarknetStorageKey ( storage_key ! ( 444u16 ) ) , StarknetStorageValue ( Felt :: from ( 666 ) ) ) ,
797- (
798- StarknetStorageKey ( get_storage_var_address (
799- "two_counters" ,
800- & [ Felt :: from ( 2021 ) ] ,
801- ) ) ,
802- StarknetStorageValue ( Felt :: from ( 100 ) ) ,
803- ) ,
804- (
805- StarknetStorageKey ( StorageKey (
806- PatriciaKey :: try_from (
807- * * get_storage_var_address ( "two_counters" , & [ Felt :: from ( 2021 ) ] )
808- + Felt :: ONE ,
809- )
810- . unwrap ( ) ,
811- ) ) ,
812- StarknetStorageValue ( Felt :: from ( 200 ) ) ,
813- ) ,
814- ] ) ,
815- ) ,
816- (
817- contract_addresses[ 1 ] ,
818- HashMap :: from ( [
819- ( StarknetStorageKey ( storage_key ! ( 15u16 ) ) , StarknetStorageValue ( Felt :: from ( 1 ) ) ) ,
820- ( StarknetStorageKey ( storage_key ! ( 111u16 ) ) , StarknetStorageValue ( Felt :: from ( 987 ) ) ) ,
821- ( StarknetStorageKey ( storage_key ! ( 555u16 ) ) , StarknetStorageValue ( Felt :: from ( 888 ) ) ) ,
822- ( StarknetStorageKey ( storage_key ! ( 666u16 ) ) , StarknetStorageValue ( Felt :: from ( 999 ) ) ) ,
823- ] ) ,
824- ) ,
825- (
826- delegate_proxy_address,
827- HashMap :: from ( [
828- ( StarknetStorageKey ( storage_key ! ( 123u16 ) ) , StarknetStorageValue ( Felt :: from ( 456 ) ) ) ,
829- (
830- StarknetStorageKey ( get_storage_var_address ( "implementation_hash" , & [ ] ) ) ,
831- StarknetStorageValue ( test_class_hash. 0 ) ,
832- ) ,
833- (
834- StarknetStorageKey ( get_storage_var_address (
835- "two_counters" ,
836- & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
837- ) ) ,
838- StarknetStorageValue ( * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ) ,
839- ) ,
840- ] ) ,
841- ) ,
842- ] ) ;
843-
844840 // Expected number of txs.
845841 assert_eq ! ( test_manager. total_txs( ) , n_expected_txs) ;
846842
@@ -856,6 +852,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
856852
857853 // Perform validations.
858854 let perform_global_validations = true ;
859- let partial_state_diff = Some ( & StateDiff { storage_updates, ..Default :: default ( ) } ) ;
855+ let partial_state_diff =
856+ Some ( & StateDiff { storage_updates : expected_storage_updates, ..Default :: default ( ) } ) ;
860857 test_output. perform_validations ( perform_global_validations, partial_state_diff) ;
861858}
0 commit comments