@@ -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 ,
@@ -376,6 +376,20 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
376
376
let ( mut test_manager, mut nonce_manager, _) =
377
377
TestManager :: < DictStateReader > :: new_with_default_initial_state ( [ ] ) . await ;
378
378
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
+ } ;
379
393
380
394
// Declare a Cairo 0 test contract.
381
395
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) {
410
424
) ;
411
425
contract_addresses. push ( address) ;
412
426
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
+ }
413
435
}
414
436
415
437
// Call set_value(address=85, value=47) on the first contract.
416
438
// 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 ) ) ;
417
440
let invoke_tx_args = invoke_tx_args ! {
418
441
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
419
442
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] ) ,
423
444
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
424
445
} ;
425
446
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
447
+ update_expected_storage ( contract_addresses[ 0 ] , key, value) ;
426
448
427
449
// Call set_value(address=81, value=0) on the first contract.
428
450
// 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) {
457
479
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
458
480
} ;
459
481
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 ) ;
460
483
461
484
let invoke_tx_args = invoke_tx_args ! {
462
485
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
@@ -611,6 +634,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
611
634
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
612
635
} ;
613
636
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
+ ) ;
614
642
615
643
// Call test_get_contract_address(expected_address=delegate_proxy_address) through the delegate
616
644
// proxy.
@@ -625,15 +653,15 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
625
653
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
626
654
627
655
// Call set_value(address=123, value=456) through the delegate proxy.
656
+ let ( key, value) = ( Felt :: from ( 123 ) , Felt :: from ( 456 ) ) ;
628
657
let invoke_tx_args = invoke_tx_args ! {
629
658
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
630
659
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] ) ,
634
661
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
635
662
} ;
636
663
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
664
+ update_expected_storage ( delegate_proxy_address, key, value) ;
637
665
638
666
// Call test_get_caller_address(expected_address=account_address) through the delegate proxy.
639
667
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) {
689
717
nonce : l1_handler_nonce,
690
718
selector : l1_handler_selector,
691
719
} ;
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
+ ) ;
692
728
693
729
// Call test_library_call_syntactic_sugar from contract_addresses[0] to test library calls
694
730
// 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) {
703
739
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
704
740
} ;
705
741
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 ) ) ;
706
743
707
744
// 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 ) ] ) ) ;
708
747
let invoke_tx_args = invoke_tx_args ! {
709
748
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
710
749
nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
711
750
calldata: create_calldata(
712
- contract_addresses[ 0 ] , "add_signature_to_counters" , & [ Felt :: from ( 2021 ) ]
751
+ contract_addresses[ 0 ] , "add_signature_to_counters" , & [ index ]
713
752
) ,
714
753
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
715
- signature: TransactionSignature ( Arc :: new ( vec! [ Felt :: from ( 100 ) , Felt :: from ( 200 ) ] ) ) ,
754
+ signature: signature . clone ( ) ,
716
755
} ;
717
756
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
+ ) ;
718
767
719
768
// Declare test_contract2.
720
769
let test_contract2 = FeatureContract :: TestContract2 ;
@@ -732,6 +781,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
732
781
. add_cairo0_declare_tx ( tx, StarknetAPICompiledClassHash ( test_contract2_class_hash. 0 ) ) ;
733
782
734
783
// Use library_call to call test_contract2.test_storage_write(address=555, value=888).
784
+ let ( key, value) = ( Felt :: from ( 555 ) , Felt :: from ( 888 ) ) ;
735
785
let invoke_tx_args = invoke_tx_args ! {
736
786
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
737
787
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) {
742
792
test_contract2_class_hash. 0 ,
743
793
selector_from_name( "test_storage_write" ) . 0 ,
744
794
Felt :: TWO ,
745
- Felt :: from ( 555 ) ,
746
- Felt :: from ( 888 )
795
+ key ,
796
+ value
747
797
]
748
798
) ,
749
799
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
750
800
signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
751
801
} ;
752
802
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
803
+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
753
804
754
805
// Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with
755
806
// from_address=85, address=666, value=999.
807
+ let ( key, value) = ( Felt :: from ( 666 ) , Felt :: from ( 999 ) ) ;
756
808
let invoke_tx_args = invoke_tx_args ! {
757
809
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
758
810
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) {
764
816
selector_from_name( "test_l1_handler_storage_write" ) . 0 ,
765
817
Felt :: THREE ,
766
818
Felt :: from( 85 ) ,
767
- Felt :: from ( 666 ) ,
768
- Felt :: from ( 999 )
819
+ key ,
820
+ value
769
821
]
770
822
) ,
771
823
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
772
824
signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
773
825
} ;
774
826
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
827
+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
775
828
776
829
// Replace the class of contract_addresses[0] to the class of test_contract2.
777
830
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) {
784
837
} ;
785
838
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
786
839
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
-
844
840
// Expected number of txs.
845
841
assert_eq ! ( test_manager. total_txs( ) , n_expected_txs) ;
846
842
@@ -856,6 +852,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
856
852
857
853
// Perform validations.
858
854
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 ( ) } ) ;
860
857
test_output. perform_validations ( perform_global_validations, partial_state_diff) ;
861
858
}
0 commit comments