@@ -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 ,
@@ -377,6 +377,20 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
377
377
let ( mut test_manager, mut nonce_manager, _) =
378
378
TestManager :: < DictStateReader > :: new_with_default_initial_state ( [ ] ) . await ;
379
379
let n_expected_txs = 29 ;
380
+ let mut expected_storage_updates: HashMap <
381
+ ContractAddress ,
382
+ HashMap < StarknetStorageKey , StarknetStorageValue > ,
383
+ > = HashMap :: new ( ) ;
384
+ let mut update_expected_storage = |address : ContractAddress , key : Felt , value : Felt | {
385
+ let key = StarknetStorageKey ( StorageKey ( key. try_into ( ) . unwrap ( ) ) ) ;
386
+ let value = StarknetStorageValue ( value) ;
387
+ expected_storage_updates
388
+ . entry ( address)
389
+ . and_modify ( |map| {
390
+ map. insert ( key, value) ;
391
+ } )
392
+ . or_insert_with ( || HashMap :: from ( [ ( key, value) ] ) ) ;
393
+ } ;
380
394
381
395
// Declare a Cairo 0 test contract.
382
396
let cairo0_test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo0 ) ;
@@ -411,19 +425,27 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
411
425
) ;
412
426
contract_addresses. push ( address) ;
413
427
test_manager. add_invoke_tx ( deploy_tx, None ) ;
428
+ // Update expected storage diff, if the ctor calldata writes a nonzero value.
429
+ if ctor_calldata[ 1 ] != 0 {
430
+ update_expected_storage (
431
+ address,
432
+ Felt :: from ( ctor_calldata[ 0 ] ) ,
433
+ Felt :: from ( ctor_calldata[ 1 ] ) ,
434
+ ) ;
435
+ }
414
436
}
415
437
416
438
// Call set_value(address=85, value=47) on the first contract.
417
439
// Used to test normal value update and make sure it is written to on-chain data.
440
+ let ( key, value) = ( Felt :: from ( 85 ) , Felt :: from ( 47 ) ) ;
418
441
let invoke_tx_args = invoke_tx_args ! {
419
442
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
420
443
nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
421
- calldata: create_calldata(
422
- contract_addresses[ 0 ] , "test_storage_read_write" , & [ Felt :: from( 85 ) , Felt :: from( 47 ) ]
423
- ) ,
444
+ calldata: create_calldata( contract_addresses[ 0 ] , "test_storage_read_write" , & [ key, value] ) ,
424
445
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
425
446
} ;
426
447
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
448
+ update_expected_storage ( contract_addresses[ 0 ] , key, value) ;
427
449
428
450
// Call set_value(address=81, value=0) on the first contract.
429
451
// Used to test redundant value update (0 -> 0) and make sure it is not written to on-chain
@@ -458,6 +480,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
458
480
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
459
481
} ;
460
482
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
483
+ update_expected_storage ( contract_addresses[ 1 ] , Felt :: from ( 15 ) , Felt :: ONE ) ;
461
484
462
485
let invoke_tx_args = invoke_tx_args ! {
463
486
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
@@ -612,6 +635,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
612
635
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
613
636
} ;
614
637
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
638
+ update_expected_storage (
639
+ delegate_proxy_address,
640
+ * * get_storage_var_address ( "implementation_hash" , & [ ] ) ,
641
+ test_class_hash. 0 ,
642
+ ) ;
615
643
616
644
// Call test_get_contract_address(expected_address=delegate_proxy_address) through the delegate
617
645
// proxy.
@@ -626,15 +654,15 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
626
654
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
627
655
628
656
// Call set_value(address=123, value=456) through the delegate proxy.
657
+ let ( key, value) = ( Felt :: from ( 123 ) , Felt :: from ( 456 ) ) ;
629
658
let invoke_tx_args = invoke_tx_args ! {
630
659
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
631
660
nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
632
- calldata: create_calldata(
633
- delegate_proxy_address, "test_storage_read_write" , & [ Felt :: from( 123 ) , Felt :: from( 456 ) ]
634
- ) ,
661
+ calldata: create_calldata( delegate_proxy_address, "test_storage_read_write" , & [ key, value] ) ,
635
662
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
636
663
} ;
637
664
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
665
+ update_expected_storage ( delegate_proxy_address, key, value) ;
638
666
639
667
// Call test_get_caller_address(expected_address=account_address) through the delegate proxy.
640
668
let invoke_tx_args = invoke_tx_args ! {
@@ -690,6 +718,14 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
690
718
nonce : l1_handler_nonce,
691
719
selector : l1_handler_selector,
692
720
} ;
721
+ update_expected_storage (
722
+ delegate_proxy_address,
723
+ * * get_storage_var_address (
724
+ "two_counters" ,
725
+ & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
726
+ ) ,
727
+ * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ,
728
+ ) ;
693
729
694
730
// Call test_library_call_syntactic_sugar from contract_addresses[0] to test library calls
695
731
// using the syntactic sugar of 'library_call_<FUNCTION>'.
@@ -704,18 +740,31 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
704
740
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
705
741
} ;
706
742
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
743
+ update_expected_storage ( contract_addresses[ 0 ] , Felt :: from ( 444 ) , Felt :: from ( 666 ) ) ;
707
744
708
745
// Call add_signature_to_counters(index=2021).
746
+ let index = Felt :: from ( 2021 ) ;
747
+ let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) , Felt :: from( 200 ) ] ) ) ;
709
748
let invoke_tx_args = invoke_tx_args ! {
710
749
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
711
750
nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
712
751
calldata: create_calldata(
713
- contract_addresses[ 0 ] , "add_signature_to_counters" , & [ Felt :: from ( 2021 ) ]
752
+ contract_addresses[ 0 ] , "add_signature_to_counters" , & [ index ]
714
753
) ,
715
754
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
716
- signature: TransactionSignature ( Arc :: new ( vec! [ Felt :: from ( 100 ) , Felt :: from ( 200 ) ] ) ) ,
755
+ signature: signature . clone ( ) ,
717
756
} ;
718
757
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
758
+ update_expected_storage (
759
+ contract_addresses[ 0 ] ,
760
+ * * get_storage_var_address ( "two_counters" , & [ index] ) ,
761
+ signature. 0 [ 0 ] ,
762
+ ) ;
763
+ update_expected_storage (
764
+ contract_addresses[ 0 ] ,
765
+ * * get_storage_var_address ( "two_counters" , & [ index] ) + Felt :: ONE ,
766
+ signature. 0 [ 1 ] ,
767
+ ) ;
719
768
720
769
// Declare test_contract2.
721
770
let test_contract2 = FeatureContract :: TestContract2 ;
@@ -733,6 +782,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
733
782
. add_cairo0_declare_tx ( tx, StarknetAPICompiledClassHash ( test_contract2_class_hash. 0 ) ) ;
734
783
735
784
// Use library_call to call test_contract2.test_storage_write(address=555, value=888).
785
+ let ( key, value) = ( Felt :: from ( 555 ) , Felt :: from ( 888 ) ) ;
736
786
let invoke_tx_args = invoke_tx_args ! {
737
787
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
738
788
nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
@@ -743,17 +793,19 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
743
793
test_contract2_class_hash. 0 ,
744
794
selector_from_name( "test_storage_write" ) . 0 ,
745
795
Felt :: TWO ,
746
- Felt :: from ( 555 ) ,
747
- Felt :: from ( 888 )
796
+ key ,
797
+ value
748
798
]
749
799
) ,
750
800
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
751
801
signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
752
802
} ;
753
803
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
804
+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
754
805
755
806
// Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with
756
807
// from_address=85, address=666, value=999.
808
+ let ( key, value) = ( Felt :: from ( 666 ) , Felt :: from ( 999 ) ) ;
757
809
let invoke_tx_args = invoke_tx_args ! {
758
810
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
759
811
nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
@@ -765,14 +817,15 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
765
817
selector_from_name( "test_l1_handler_storage_write" ) . 0 ,
766
818
Felt :: THREE ,
767
819
Felt :: from( 85 ) ,
768
- Felt :: from ( 666 ) ,
769
- Felt :: from ( 999 )
820
+ key ,
821
+ value
770
822
]
771
823
) ,
772
824
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
773
825
signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
774
826
} ;
775
827
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
828
+ update_expected_storage ( contract_addresses[ 1 ] , key, value) ;
776
829
777
830
// Replace the class of contract_addresses[0] to the class of test_contract2.
778
831
let invoke_tx_args = invoke_tx_args ! {
@@ -785,63 +838,6 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
785
838
} ;
786
839
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
787
840
788
- // Expected values:
789
-
790
- // Storage updates:
791
- let storage_updates = HashMap :: from ( [
792
- (
793
- contract_addresses[ 0 ] ,
794
- HashMap :: from ( [
795
- ( StarknetStorageKey ( storage_key ! ( 85u16 ) ) , StarknetStorageValue ( Felt :: from ( 47 ) ) ) ,
796
- ( StarknetStorageKey ( storage_key ! ( 321u16 ) ) , StarknetStorageValue ( Felt :: from ( 543 ) ) ) ,
797
- ( StarknetStorageKey ( storage_key ! ( 444u16 ) ) , StarknetStorageValue ( Felt :: from ( 666 ) ) ) ,
798
- (
799
- StarknetStorageKey ( get_storage_var_address (
800
- "two_counters" ,
801
- & [ Felt :: from ( 2021 ) ] ,
802
- ) ) ,
803
- StarknetStorageValue ( Felt :: from ( 100 ) ) ,
804
- ) ,
805
- (
806
- StarknetStorageKey ( StorageKey (
807
- PatriciaKey :: try_from (
808
- * * get_storage_var_address ( "two_counters" , & [ Felt :: from ( 2021 ) ] )
809
- + Felt :: ONE ,
810
- )
811
- . unwrap ( ) ,
812
- ) ) ,
813
- StarknetStorageValue ( Felt :: from ( 200 ) ) ,
814
- ) ,
815
- ] ) ,
816
- ) ,
817
- (
818
- contract_addresses[ 1 ] ,
819
- HashMap :: from ( [
820
- ( StarknetStorageKey ( storage_key ! ( 15u16 ) ) , StarknetStorageValue ( Felt :: from ( 1 ) ) ) ,
821
- ( StarknetStorageKey ( storage_key ! ( 111u16 ) ) , StarknetStorageValue ( Felt :: from ( 987 ) ) ) ,
822
- ( StarknetStorageKey ( storage_key ! ( 555u16 ) ) , StarknetStorageValue ( Felt :: from ( 888 ) ) ) ,
823
- ( StarknetStorageKey ( storage_key ! ( 666u16 ) ) , StarknetStorageValue ( Felt :: from ( 999 ) ) ) ,
824
- ] ) ,
825
- ) ,
826
- (
827
- delegate_proxy_address,
828
- HashMap :: from ( [
829
- ( StarknetStorageKey ( storage_key ! ( 123u16 ) ) , StarknetStorageValue ( Felt :: from ( 456 ) ) ) ,
830
- (
831
- StarknetStorageKey ( get_storage_var_address ( "implementation_hash" , & [ ] ) ) ,
832
- StarknetStorageValue ( test_class_hash. 0 ) ,
833
- ) ,
834
- (
835
- StarknetStorageKey ( get_storage_var_address (
836
- "two_counters" ,
837
- & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
838
- ) ) ,
839
- StarknetStorageValue ( * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ) ,
840
- ) ,
841
- ] ) ,
842
- ) ,
843
- ] ) ;
844
-
845
841
// Expected number of txs.
846
842
assert_eq ! ( test_manager. total_txs( ) , n_expected_txs) ;
847
843
@@ -857,6 +853,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
857
853
858
854
// Perform validations.
859
855
let perform_global_validations = true ;
860
- let partial_state_diff = Some ( & StateDiff { storage_updates, ..Default :: default ( ) } ) ;
856
+ let partial_state_diff =
857
+ Some ( & StateDiff { storage_updates : expected_storage_updates, ..Default :: default ( ) } ) ;
861
858
test_output. perform_validations ( perform_global_validations, partial_state_diff) ;
862
859
}
0 commit comments