@@ -12,9 +12,9 @@ use starknet_api::core::{
12
12
calculate_contract_address,
13
13
ClassHash ,
14
14
CompiledClassHash as StarknetAPICompiledClassHash ,
15
+ ContractAddress ,
15
16
EthAddress ,
16
17
Nonce ,
17
- PatriciaKey ,
18
18
} ;
19
19
use starknet_api:: executable_transaction:: {
20
20
DeclareTransaction ,
@@ -48,7 +48,7 @@ use starknet_api::transaction::{
48
48
MessageToL1 ,
49
49
TransactionVersion ,
50
50
} ;
51
- use starknet_api:: { calldata, declare_tx_args, invoke_tx_args, storage_key } ;
51
+ use starknet_api:: { calldata, declare_tx_args, invoke_tx_args} ;
52
52
use starknet_committer:: block_committer:: input:: {
53
53
StarknetStorageKey ,
54
54
StarknetStorageValue ,
@@ -373,6 +373,20 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
373
373
let ( mut test_manager, mut nonce_manager, _) =
374
374
TestManager :: < DictStateReader > :: new_with_default_initial_state ( [ ] ) . await ;
375
375
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
+ } ;
376
390
377
391
// Declare a Cairo 0 test contract.
378
392
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) {
407
421
) ;
408
422
contract_addresses. push ( address) ;
409
423
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
+ }
410
432
}
411
433
412
434
// Call set_value(address=85, value=47) on the first contract.
413
435
// 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 ) ) ;
414
437
let invoke_tx_args = invoke_tx_args ! {
415
438
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
416
439
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] ) ,
420
441
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
421
442
} ;
422
443
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
444
+ update_expected_storage ( contract_addresses[ 0 ] , key, value) ;
423
445
424
446
// Call set_value(address=81, value=0) on the first contract.
425
447
// 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) {
454
476
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
455
477
} ;
456
478
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 ) ;
457
480
458
481
let invoke_tx_args = invoke_tx_args ! {
459
482
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
@@ -608,6 +631,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
608
631
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
609
632
} ;
610
633
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
+ ) ;
611
639
612
640
// Call test_get_contract_address(expected_address=delegate_proxy_address) through the delegate
613
641
// proxy.
@@ -622,15 +650,15 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
622
650
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
623
651
624
652
// Call set_value(address=123, value=456) through the delegate proxy.
653
+ let ( key, value) = ( Felt :: from ( 123 ) , Felt :: from ( 456 ) ) ;
625
654
let invoke_tx_args = invoke_tx_args ! {
626
655
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
627
656
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] ) ,
631
658
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
632
659
} ;
633
660
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
661
+ update_expected_storage ( delegate_proxy_address, key, value) ;
634
662
635
663
// Call test_get_caller_address(expected_address=account_address) through the delegate proxy.
636
664
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) {
686
714
nonce : l1_handler_nonce,
687
715
selector : l1_handler_selector,
688
716
} ;
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
+ ) ;
689
725
690
726
// Call test_library_call_syntactic_sugar from contract_addresses[0] to test library calls
691
727
// 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) {
700
736
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
701
737
} ;
702
738
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 ) ) ;
703
740
704
741
// 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 ) ] ) ) ;
705
744
let invoke_tx_args = invoke_tx_args ! {
706
745
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
707
746
nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
708
747
calldata: create_calldata(
709
- contract_addresses[ 0 ] , "add_signature_to_counters" , & [ Felt :: from ( 2021 ) ]
748
+ contract_addresses[ 0 ] , "add_signature_to_counters" , & [ index ]
710
749
) ,
711
750
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
712
- signature: TransactionSignature ( Arc :: new ( vec! [ Felt :: from ( 100 ) , Felt :: from ( 200 ) ] ) ) ,
751
+ signature: signature . clone ( ) ,
713
752
} ;
714
753
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
+ ) ;
715
764
716
765
// Declare test_contract2.
717
766
let test_contract2 = FeatureContract :: TestContract2 ;
@@ -729,6 +778,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
729
778
. add_cairo0_declare_tx ( tx, StarknetAPICompiledClassHash ( test_contract2_class_hash. 0 ) ) ;
730
779
731
780
// Use library_call to call test_contract2.test_storage_write(address=555, value=888).
781
+ let ( key, value) = ( Felt :: from ( 555 ) , Felt :: from ( 888 ) ) ;
732
782
let invoke_tx_args = invoke_tx_args ! {
733
783
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
734
784
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) {
739
789
test_contract2_class_hash. 0 ,
740
790
selector_from_name( "test_storage_write" ) . 0 ,
741
791
Felt :: TWO ,
742
- Felt :: from ( 555 ) ,
743
- Felt :: from ( 888 )
792
+ key ,
793
+ value
744
794
]
745
795
) ,
746
796
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
747
797
signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
748
798
} ;
749
799
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
800
+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
750
801
751
802
// Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with
752
803
// from_address=85, address=666, value=999.
804
+ let ( key, value) = ( Felt :: from ( 666 ) , Felt :: from ( 999 ) ) ;
753
805
let invoke_tx_args = invoke_tx_args ! {
754
806
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
755
807
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) {
761
813
selector_from_name( "test_l1_handler_storage_write" ) . 0 ,
762
814
Felt :: THREE ,
763
815
Felt :: from( 85 ) ,
764
- Felt :: from ( 666 ) ,
765
- Felt :: from ( 999 )
816
+ key ,
817
+ value
766
818
]
767
819
) ,
768
820
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
769
821
signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
770
822
} ;
771
823
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
824
+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
772
825
773
826
// Replace the class of contract_addresses[0] to the class of test_contract2.
774
827
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) {
781
834
} ;
782
835
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
783
836
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
-
841
837
// Expected number of txs.
842
838
assert_eq ! ( test_manager. total_txs( ) , n_expected_txs) ;
843
839
@@ -853,6 +849,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
853
849
854
850
// Perform validations.
855
851
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 ( ) } ) ;
857
854
test_output. perform_validations ( perform_global_validations, partial_state_diff) ;
858
855
}
0 commit comments