@@ -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,6 +425,14 @@ 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.
@@ -424,6 +446,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
424
446
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
425
447
} ;
426
448
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
449
+ update_expected_storage ( contract_addresses[ 0 ] , Felt :: from ( 85 ) , Felt :: from ( 47 ) ) ;
427
450
428
451
// Call set_value(address=81, value=0) on the first contract.
429
452
// Used to test redundant value update (0 -> 0) and make sure it is not written to on-chain
@@ -458,6 +481,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
458
481
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
459
482
} ;
460
483
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
484
+ update_expected_storage ( contract_addresses[ 1 ] , Felt :: from ( 15 ) , Felt :: ONE ) ;
461
485
462
486
let invoke_tx_args = invoke_tx_args ! {
463
487
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
@@ -612,6 +636,11 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
612
636
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
613
637
} ;
614
638
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
639
+ update_expected_storage (
640
+ delegate_proxy_address,
641
+ * * get_storage_var_address ( "implementation_hash" , & [ ] ) ,
642
+ test_class_hash. 0 ,
643
+ ) ;
615
644
616
645
// Call test_get_contract_address(expected_address=delegate_proxy_address) through the delegate
617
646
// proxy.
@@ -635,6 +664,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
635
664
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
636
665
} ;
637
666
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
667
+ update_expected_storage ( delegate_proxy_address, Felt :: from ( 123 ) , Felt :: from ( 456 ) ) ;
638
668
639
669
// Call test_get_caller_address(expected_address=account_address) through the delegate proxy.
640
670
let invoke_tx_args = invoke_tx_args ! {
@@ -690,6 +720,14 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
690
720
nonce : l1_handler_nonce,
691
721
selector : l1_handler_selector,
692
722
} ;
723
+ update_expected_storage (
724
+ delegate_proxy_address,
725
+ * * get_storage_var_address (
726
+ "two_counters" ,
727
+ & [ Felt :: from ( expected_message_to_l2. from_address ) ] ,
728
+ ) ,
729
+ * expected_message_to_l2. payload . 0 . last ( ) . unwrap ( ) ,
730
+ ) ;
693
731
694
732
// Call test_library_call_syntactic_sugar from contract_addresses[0] to test library calls
695
733
// using the syntactic sugar of 'library_call_<FUNCTION>'.
@@ -704,18 +742,31 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
704
742
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
705
743
} ;
706
744
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
745
+ update_expected_storage ( contract_addresses[ 0 ] , Felt :: from ( 444 ) , Felt :: from ( 666 ) ) ;
707
746
708
747
// Call add_signature_to_counters(index=2021).
748
+ let index = Felt :: from ( 2021 ) ;
749
+ let signature = TransactionSignature ( Arc :: new ( vec ! [ Felt :: from( 100 ) , Felt :: from( 200 ) ] ) ) ;
709
750
let invoke_tx_args = invoke_tx_args ! {
710
751
sender_address: * FUNDED_ACCOUNT_ADDRESS ,
711
752
nonce: nonce_manager. next( * FUNDED_ACCOUNT_ADDRESS ) ,
712
753
calldata: create_calldata(
713
- contract_addresses[ 0 ] , "add_signature_to_counters" , & [ Felt :: from ( 2021 ) ]
754
+ contract_addresses[ 0 ] , "add_signature_to_counters" , & [ index ]
714
755
) ,
715
756
resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
716
- signature: TransactionSignature ( Arc :: new ( vec! [ Felt :: from ( 100 ) , Felt :: from ( 200 ) ] ) ) ,
757
+ signature: signature . clone ( ) ,
717
758
} ;
718
759
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
760
+ update_expected_storage (
761
+ contract_addresses[ 0 ] ,
762
+ * * get_storage_var_address ( "two_counters" , & [ index] ) ,
763
+ signature. 0 [ 0 ] ,
764
+ ) ;
765
+ update_expected_storage (
766
+ contract_addresses[ 0 ] ,
767
+ * * get_storage_var_address ( "two_counters" , & [ index] ) + Felt :: ONE ,
768
+ signature. 0 [ 1 ] ,
769
+ ) ;
719
770
720
771
// Declare test_contract2.
721
772
let test_contract2 = FeatureContract :: TestContract2 ;
@@ -751,6 +802,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
751
802
signature: TransactionSignature ( Arc :: new( vec![ Felt :: from( 100 ) ] ) ) ,
752
803
} ;
753
804
test_manager. add_invoke_tx_from_args ( invoke_tx_args, & CHAIN_ID_FOR_TESTS , None ) ;
805
+ update_expected_storage ( contract_addresses[ 1 ] , Felt :: from ( 555 ) , Felt :: from ( 888 ) ) ;
754
806
755
807
// Use library_call_l1_handler to invoke test_contract2.test_l1_handler_storage_write with
756
808
// from_address=85, address=666, value=999.
@@ -773,6 +825,7 @@ async fn test_os_logic(#[values(1, 3)] n_blocks_in_multi_block: usize) {
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 ] , Felt :: from ( 666 ) , Felt :: from ( 999 ) ) ;
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