@@ -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 ,
@@ -399,6 +405,20 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
399405 let ( mut test_manager, _) =
400406 TestManager :: < DictStateReader > :: new_with_default_initial_state ( [ ] ) . await ;
401407 let n_expected_txs = 29 ;
408+ let mut expected_storage_updates: HashMap <
409+ ContractAddress ,
410+ HashMap < StarknetStorageKey , StarknetStorageValue > ,
411+ > = HashMap :: new ( ) ;
412+ let mut update_expected_storage = |address : ContractAddress , key : Felt , value : Felt | {
413+ let key = StarknetStorageKey ( StorageKey ( key. try_into ( ) . unwrap ( ) ) ) ;
414+ let value = StarknetStorageValue ( value) ;
415+ expected_storage_updates
416+ . entry ( address)
417+ . and_modify ( |map| {
418+ map. insert ( key, value) ;
419+ } )
420+ . or_insert_with ( || HashMap :: from ( [ ( key, value) ] ) ) ;
421+ } ;
402422
403423 // Declare a Cairo 0 test contract.
404424 let cairo0_test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo0 ) ;
@@ -431,16 +451,22 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
431451 ) ;
432452 contract_addresses. push ( address) ;
433453 test_manager. add_invoke_tx ( deploy_tx, None ) ;
454+ // Update expected storage diff, if the ctor calldata writes a nonzero value.
455+ if ctor_calldata[ 1 ] != 0 {
456+ update_expected_storage (
457+ address,
458+ Felt :: from ( ctor_calldata[ 0 ] ) ,
459+ Felt :: from ( ctor_calldata[ 1 ] ) ,
460+ ) ;
461+ }
434462 }
435463
436464 // Call set_value(address=85, value=47) on the first contract.
437465 // Used to test normal value update and make sure it is written to on-chain data.
438- let calldata = create_calldata (
439- contract_addresses[ 0 ] ,
440- "test_storage_read_write" ,
441- & [ Felt :: from ( 85 ) , Felt :: from ( 47 ) ] ,
442- ) ;
466+ let ( key, value) = ( Felt :: from ( 85 ) , Felt :: from ( 47 ) ) ;
467+ let calldata = create_calldata ( contract_addresses[ 0 ] , "test_storage_read_write" , & [ key, value] ) ;
443468 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
469+ update_expected_storage ( contract_addresses[ 0 ] , key, value) ;
444470
445471 // Call set_value(address=81, value=0) on the first contract.
446472 // Used to test redundant value update (0 -> 0) and make sure it is not written to on-chain
@@ -464,6 +490,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
464490
465491 let calldata = create_calldata ( contract_addresses[ 1 ] , "entry_point" , & [ ] ) ;
466492 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
493+ update_expected_storage ( contract_addresses[ 1 ] , Felt :: from ( 15 ) , Felt :: ONE ) ;
467494
468495 let calldata = create_calldata ( contract_addresses[ 0 ] , "test_builtins" , & [ ] ) ;
469496 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
@@ -573,6 +600,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
573600 let calldata =
574601 create_calldata ( delegate_proxy_address, "set_implementation_hash" , & [ test_class_hash. 0 ] ) ;
575602 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
603+ update_expected_storage (
604+ delegate_proxy_address,
605+ * * get_storage_var_address ( "implementation_hash" , & [ ] ) ,
606+ test_class_hash. 0 ,
607+ ) ;
576608
577609 // Call test_get_contract_address(expected_address=delegate_proxy_address) through the delegate
578610 // proxy.
@@ -584,12 +616,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
584616 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
585617
586618 // Call set_value(address=123, value=456) through the delegate proxy.
587- let calldata = create_calldata (
588- delegate_proxy_address,
589- "test_storage_read_write" ,
590- & [ Felt :: from ( 123 ) , Felt :: from ( 456 ) ] ,
591- ) ;
619+ let ( key, value) = ( Felt :: from ( 123 ) , Felt :: from ( 456 ) ) ;
620+ let calldata =
621+ create_calldata ( delegate_proxy_address, "test_storage_read_write" , & [ key, value] ) ;
592622 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
623+ update_expected_storage ( delegate_proxy_address, key, value) ;
593624
594625 // Call test_get_caller_address(expected_address=account_address) through the delegate proxy.
595626 let calldata = create_calldata (
@@ -637,6 +668,14 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
637668 nonce : l1_handler_nonce,
638669 selector : l1_handler_selector,
639670 } ;
671+ update_expected_storage (
672+ delegate_proxy_address,
673+ * * get_storage_var_address (
674+ "two_counters" ,
675+ & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
676+ ) ,
677+ * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ,
678+ ) ;
640679
641680 // Call test_library_call_syntactic_sugar from contract_addresses[0] to test library calls
642681 // using the syntactic sugar of 'library_call_<FUNCTION>'.
@@ -646,12 +685,24 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
646685 & [ test_class_hash. 0 ] ,
647686 ) ;
648687 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
688+ update_expected_storage ( contract_addresses[ 0 ] , Felt :: from ( 444 ) , Felt :: from ( 666 ) ) ;
649689
650690 // Call add_signature_to_counters(index=2021).
651- let calldata =
652- create_calldata ( contract_addresses[ 0 ] , "add_signature_to_counters" , & [ Felt :: from ( 2021 ) ] ) ;
691+ let index = Felt :: from ( 2021 ) ;
692+ let calldata = create_calldata ( contract_addresses[ 0 ] , "add_signature_to_counters" , & [ index ] ) ;
653693 let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) , Felt :: from( 200 ) ] ) ) ;
654- test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata, signature } ) ;
694+ test_manager
695+ . add_funded_account_invoke ( invoke_tx_args ! { calldata, signature: signature. clone( ) } ) ;
696+ update_expected_storage (
697+ contract_addresses[ 0 ] ,
698+ * * get_storage_var_address ( "two_counters" , & [ index] ) ,
699+ signature. 0 [ 0 ] ,
700+ ) ;
701+ update_expected_storage (
702+ contract_addresses[ 0 ] ,
703+ * * get_storage_var_address ( "two_counters" , & [ index] ) + Felt :: ONE ,
704+ signature. 0 [ 1 ] ,
705+ ) ;
655706
656707 // Declare test_contract2.
657708 let test_contract2 = FeatureContract :: TestContract2 ;
@@ -668,22 +719,25 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
668719 test_manager. add_cairo0_declare_tx ( tx, test_contract2_class_hash) ;
669720
670721 // Use library_call to call test_contract2.test_storage_write(address=555, value=888).
722+ let ( key, value) = ( Felt :: from ( 555 ) , Felt :: from ( 888 ) ) ;
671723 let calldata = create_calldata (
672724 contract_addresses[ 1 ] ,
673725 "test_library_call" ,
674726 & [
675727 test_contract2_class_hash. 0 ,
676728 selector_from_name ( "test_storage_write" ) . 0 ,
677729 Felt :: TWO ,
678- Felt :: from ( 555 ) ,
679- Felt :: from ( 888 ) ,
730+ key ,
731+ value ,
680732 ] ,
681733 ) ;
682734 let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) ] ) ) ;
683735 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata, signature } ) ;
736+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
684737
685738 // Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with
686739 // from_address=85, address=666, value=999.
740+ let ( key, value) = ( Felt :: from ( 666 ) , Felt :: from ( 999 ) ) ;
687741 let calldata = create_calldata (
688742 contract_addresses[ 1 ] ,
689743 "test_library_call_l1_handler" ,
@@ -692,12 +746,13 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
692746 selector_from_name ( "test_l1_handler_storage_write" ) . 0 ,
693747 Felt :: THREE ,
694748 Felt :: from ( 85 ) ,
695- Felt :: from ( 666 ) ,
696- Felt :: from ( 999 ) ,
749+ key ,
750+ value ,
697751 ] ,
698752 ) ;
699753 let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) ] ) ) ;
700754 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata, signature } ) ;
755+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
701756
702757 // Replace the class of contract_addresses[0] to the class of test_contract2.
703758 let calldata = create_calldata (
@@ -707,63 +762,6 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
707762 ) ;
708763 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
709764
710- // Expected values:
711-
712- // Storage updates:
713- let storage_updates = HashMap :: from ( [
714- (
715- contract_addresses[ 0 ] ,
716- HashMap :: from ( [
717- ( StarknetStorageKey ( storage_key ! ( 85u16 ) ) , StarknetStorageValue ( Felt :: from ( 47 ) ) ) ,
718- ( StarknetStorageKey ( storage_key ! ( 321u16 ) ) , StarknetStorageValue ( Felt :: from ( 543 ) ) ) ,
719- ( StarknetStorageKey ( storage_key ! ( 444u16 ) ) , StarknetStorageValue ( Felt :: from ( 666 ) ) ) ,
720- (
721- StarknetStorageKey ( get_storage_var_address (
722- "two_counters" ,
723- & [ Felt :: from ( 2021 ) ] ,
724- ) ) ,
725- StarknetStorageValue ( Felt :: from ( 100 ) ) ,
726- ) ,
727- (
728- StarknetStorageKey ( StorageKey (
729- PatriciaKey :: try_from (
730- * * get_storage_var_address ( "two_counters" , & [ Felt :: from ( 2021 ) ] )
731- + Felt :: ONE ,
732- )
733- . unwrap ( ) ,
734- ) ) ,
735- StarknetStorageValue ( Felt :: from ( 200 ) ) ,
736- ) ,
737- ] ) ,
738- ) ,
739- (
740- contract_addresses[ 1 ] ,
741- HashMap :: from ( [
742- ( StarknetStorageKey ( storage_key ! ( 15u16 ) ) , StarknetStorageValue ( Felt :: from ( 1 ) ) ) ,
743- ( StarknetStorageKey ( storage_key ! ( 111u16 ) ) , StarknetStorageValue ( Felt :: from ( 987 ) ) ) ,
744- ( StarknetStorageKey ( storage_key ! ( 555u16 ) ) , StarknetStorageValue ( Felt :: from ( 888 ) ) ) ,
745- ( StarknetStorageKey ( storage_key ! ( 666u16 ) ) , StarknetStorageValue ( Felt :: from ( 999 ) ) ) ,
746- ] ) ,
747- ) ,
748- (
749- delegate_proxy_address,
750- HashMap :: from ( [
751- ( StarknetStorageKey ( storage_key ! ( 123u16 ) ) , StarknetStorageValue ( Felt :: from ( 456 ) ) ) ,
752- (
753- StarknetStorageKey ( get_storage_var_address ( "implementation_hash" , & [ ] ) ) ,
754- StarknetStorageValue ( test_class_hash. 0 ) ,
755- ) ,
756- (
757- StarknetStorageKey ( get_storage_var_address (
758- "two_counters" ,
759- & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
760- ) ) ,
761- StarknetStorageValue ( * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ) ,
762- ) ,
763- ] ) ,
764- ) ,
765- ] ) ;
766-
767765 // Expected number of txs.
768766 assert_eq ! ( test_manager. total_txs( ) , n_expected_txs) ;
769767
@@ -779,6 +777,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
779777
780778 // Perform validations.
781779 let perform_global_validations = true ;
782- let partial_state_diff = Some ( & StateDiff { storage_updates, ..Default :: default ( ) } ) ;
780+ let partial_state_diff =
781+ Some ( & StateDiff { storage_updates : expected_storage_updates, ..Default :: default ( ) } ) ;
783782 test_output. perform_validations ( perform_global_validations, partial_state_diff) ;
784783}
0 commit comments