@@ -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 ,
@@ -360,6 +360,20 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
360360 let ( mut test_manager, _) =
361361 TestManager :: < DictStateReader > :: new_with_default_initial_state ( [ ] ) . await ;
362362 let n_expected_txs = 29 ;
363+ let mut expected_storage_updates: HashMap <
364+ ContractAddress ,
365+ HashMap < StarknetStorageKey , StarknetStorageValue > ,
366+ > = HashMap :: new ( ) ;
367+ let mut update_expected_storage = |address : ContractAddress , key : Felt , value : Felt | {
368+ let key = StarknetStorageKey ( StorageKey ( key. try_into ( ) . unwrap ( ) ) ) ;
369+ let value = StarknetStorageValue ( value) ;
370+ expected_storage_updates
371+ . entry ( address)
372+ . and_modify ( |map| {
373+ map. insert ( key, value) ;
374+ } )
375+ . or_insert_with ( || HashMap :: from ( [ ( key, value) ] ) ) ;
376+ } ;
363377
364378 // Declare a Cairo 0 test contract.
365379 let cairo0_test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo0 ) ;
@@ -394,16 +408,22 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
394408 ) ;
395409 contract_addresses. push ( address) ;
396410 test_manager. add_invoke_tx ( deploy_tx, None ) ;
411+ // Update expected storage diff, if the ctor calldata writes a nonzero value.
412+ if ctor_calldata[ 1 ] != 0 {
413+ update_expected_storage (
414+ address,
415+ Felt :: from ( ctor_calldata[ 0 ] ) ,
416+ Felt :: from ( ctor_calldata[ 1 ] ) ,
417+ ) ;
418+ }
397419 }
398420
399421 // Call set_value(address=85, value=47) on the first contract.
400422 // Used to test normal value update and make sure it is written to on-chain data.
401- let calldata = create_calldata (
402- contract_addresses[ 0 ] ,
403- "test_storage_read_write" ,
404- & [ Felt :: from ( 85 ) , Felt :: from ( 47 ) ] ,
405- ) ;
423+ let ( key, value) = ( Felt :: from ( 85 ) , Felt :: from ( 47 ) ) ;
424+ let calldata = create_calldata ( contract_addresses[ 0 ] , "test_storage_read_write" , & [ key, value] ) ;
406425 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
426+ update_expected_storage ( contract_addresses[ 0 ] , key, value) ;
407427
408428 // Call set_value(address=81, value=0) on the first contract.
409429 // Used to test redundant value update (0 -> 0) and make sure it is not written to on-chain
@@ -427,6 +447,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
427447
428448 let calldata = create_calldata ( contract_addresses[ 1 ] , "entry_point" , & [ ] ) ;
429449 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
450+ update_expected_storage ( contract_addresses[ 1 ] , Felt :: from ( 15 ) , Felt :: ONE ) ;
430451
431452 let calldata = create_calldata ( contract_addresses[ 0 ] , "test_builtins" , & [ ] ) ;
432453 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
@@ -537,6 +558,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
537558 let calldata =
538559 create_calldata ( delegate_proxy_address, "set_implementation_hash" , & [ test_class_hash. 0 ] ) ;
539560 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
561+ update_expected_storage (
562+ delegate_proxy_address,
563+ * * get_storage_var_address ( "implementation_hash" , & [ ] ) ,
564+ test_class_hash. 0 ,
565+ ) ;
540566
541567 // Call test_get_contract_address(expected_address=delegate_proxy_address) through the delegate
542568 // proxy.
@@ -548,12 +574,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
548574 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
549575
550576 // Call set_value(address=123, value=456) through the delegate proxy.
551- let calldata = create_calldata (
552- delegate_proxy_address,
553- "test_storage_read_write" ,
554- & [ Felt :: from ( 123 ) , Felt :: from ( 456 ) ] ,
555- ) ;
577+ let ( key, value) = ( Felt :: from ( 123 ) , Felt :: from ( 456 ) ) ;
578+ let calldata =
579+ create_calldata ( delegate_proxy_address, "test_storage_read_write" , & [ key, value] ) ;
556580 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
581+ update_expected_storage ( delegate_proxy_address, key, value) ;
557582
558583 // Call test_get_caller_address(expected_address=account_address) through the delegate proxy.
559584 let calldata = create_calldata (
@@ -601,6 +626,14 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
601626 nonce : l1_handler_nonce,
602627 selector : l1_handler_selector,
603628 } ;
629+ update_expected_storage (
630+ delegate_proxy_address,
631+ * * get_storage_var_address (
632+ "two_counters" ,
633+ & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
634+ ) ,
635+ * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ,
636+ ) ;
604637
605638 // Call test_library_call_syntactic_sugar from contract_addresses[0] to test library calls
606639 // using the syntactic sugar of 'library_call_<FUNCTION>'.
@@ -610,12 +643,24 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
610643 & [ test_class_hash. 0 ] ,
611644 ) ;
612645 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
646+ update_expected_storage ( contract_addresses[ 0 ] , Felt :: from ( 444 ) , Felt :: from ( 666 ) ) ;
613647
614648 // Call add_signature_to_counters(index=2021).
615- let calldata =
616- create_calldata ( contract_addresses[ 0 ] , "add_signature_to_counters" , & [ Felt :: from ( 2021 ) ] ) ;
649+ let index = Felt :: from ( 2021 ) ;
650+ let calldata = create_calldata ( contract_addresses[ 0 ] , "add_signature_to_counters" , & [ index ] ) ;
617651 let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) , Felt :: from( 200 ) ] ) ) ;
618- test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata, signature } ) ;
652+ test_manager
653+ . add_funded_account_invoke ( invoke_tx_args ! { calldata, signature: signature. clone( ) } ) ;
654+ update_expected_storage (
655+ contract_addresses[ 0 ] ,
656+ * * get_storage_var_address ( "two_counters" , & [ index] ) ,
657+ signature. 0 [ 0 ] ,
658+ ) ;
659+ update_expected_storage (
660+ contract_addresses[ 0 ] ,
661+ * * get_storage_var_address ( "two_counters" , & [ index] ) + Felt :: ONE ,
662+ signature. 0 [ 1 ] ,
663+ ) ;
619664
620665 // Declare test_contract2.
621666 let test_contract2 = FeatureContract :: TestContract2 ;
@@ -633,22 +678,25 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
633678 . add_cairo0_declare_tx ( tx, StarknetAPICompiledClassHash ( test_contract2_class_hash. 0 ) ) ;
634679
635680 // Use library_call to call test_contract2.test_storage_write(address=555, value=888).
681+ let ( key, value) = ( Felt :: from ( 555 ) , Felt :: from ( 888 ) ) ;
636682 let calldata = create_calldata (
637683 contract_addresses[ 1 ] ,
638684 "test_library_call" ,
639685 & [
640686 test_contract2_class_hash. 0 ,
641687 selector_from_name ( "test_storage_write" ) . 0 ,
642688 Felt :: TWO ,
643- Felt :: from ( 555 ) ,
644- Felt :: from ( 888 ) ,
689+ key ,
690+ value ,
645691 ] ,
646692 ) ;
647693 let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) ] ) ) ;
648694 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata, signature } ) ;
695+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
649696
650697 // Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with
651698 // from_address=85, address=666, value=999.
699+ let ( key, value) = ( Felt :: from ( 666 ) , Felt :: from ( 999 ) ) ;
652700 let calldata = create_calldata (
653701 contract_addresses[ 1 ] ,
654702 "test_library_call_l1_handler" ,
@@ -657,12 +705,13 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
657705 selector_from_name ( "test_l1_handler_storage_write" ) . 0 ,
658706 Felt :: THREE ,
659707 Felt :: from ( 85 ) ,
660- Felt :: from ( 666 ) ,
661- Felt :: from ( 999 ) ,
708+ key ,
709+ value ,
662710 ] ,
663711 ) ;
664712 let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) ] ) ) ;
665713 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata, signature } ) ;
714+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
666715
667716 // Replace the class of contract_addresses[0] to the class of test_contract2.
668717 let calldata = create_calldata (
@@ -672,63 +721,6 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
672721 ) ;
673722 test_manager. add_funded_account_invoke ( invoke_tx_args ! { calldata } ) ;
674723
675- // Expected values:
676-
677- // Storage updates:
678- let storage_updates = HashMap :: from ( [
679- (
680- contract_addresses[ 0 ] ,
681- HashMap :: from ( [
682- ( StarknetStorageKey ( storage_key ! ( 85u16 ) ) , StarknetStorageValue ( Felt :: from ( 47 ) ) ) ,
683- ( StarknetStorageKey ( storage_key ! ( 321u16 ) ) , StarknetStorageValue ( Felt :: from ( 543 ) ) ) ,
684- ( StarknetStorageKey ( storage_key ! ( 444u16 ) ) , StarknetStorageValue ( Felt :: from ( 666 ) ) ) ,
685- (
686- StarknetStorageKey ( get_storage_var_address (
687- "two_counters" ,
688- & [ Felt :: from ( 2021 ) ] ,
689- ) ) ,
690- StarknetStorageValue ( Felt :: from ( 100 ) ) ,
691- ) ,
692- (
693- StarknetStorageKey ( StorageKey (
694- PatriciaKey :: try_from (
695- * * get_storage_var_address ( "two_counters" , & [ Felt :: from ( 2021 ) ] )
696- + Felt :: ONE ,
697- )
698- . unwrap ( ) ,
699- ) ) ,
700- StarknetStorageValue ( Felt :: from ( 200 ) ) ,
701- ) ,
702- ] ) ,
703- ) ,
704- (
705- contract_addresses[ 1 ] ,
706- HashMap :: from ( [
707- ( StarknetStorageKey ( storage_key ! ( 15u16 ) ) , StarknetStorageValue ( Felt :: from ( 1 ) ) ) ,
708- ( StarknetStorageKey ( storage_key ! ( 111u16 ) ) , StarknetStorageValue ( Felt :: from ( 987 ) ) ) ,
709- ( StarknetStorageKey ( storage_key ! ( 555u16 ) ) , StarknetStorageValue ( Felt :: from ( 888 ) ) ) ,
710- ( StarknetStorageKey ( storage_key ! ( 666u16 ) ) , StarknetStorageValue ( Felt :: from ( 999 ) ) ) ,
711- ] ) ,
712- ) ,
713- (
714- delegate_proxy_address,
715- HashMap :: from ( [
716- ( StarknetStorageKey ( storage_key ! ( 123u16 ) ) , StarknetStorageValue ( Felt :: from ( 456 ) ) ) ,
717- (
718- StarknetStorageKey ( get_storage_var_address ( "implementation_hash" , & [ ] ) ) ,
719- StarknetStorageValue ( test_class_hash. 0 ) ,
720- ) ,
721- (
722- StarknetStorageKey ( get_storage_var_address (
723- "two_counters" ,
724- & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
725- ) ) ,
726- StarknetStorageValue ( * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ) ,
727- ) ,
728- ] ) ,
729- ) ,
730- ] ) ;
731-
732724 // Expected number of txs.
733725 assert_eq ! ( test_manager. total_txs( ) , n_expected_txs) ;
734726
@@ -744,6 +736,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
744736
745737 // Perform validations.
746738 let perform_global_validations = true ;
747- let partial_state_diff = Some ( & StateDiff { storage_updates, ..Default :: default ( ) } ) ;
739+ let partial_state_diff =
740+ Some ( & StateDiff { storage_updates : expected_storage_updates, ..Default :: default ( ) } ) ;
748741 test_output. perform_validations ( perform_global_validations, partial_state_diff) ;
749742}
0 commit comments