@@ -119,27 +119,24 @@ use crate::utils::u64_from_usize;
119119
120120#[ rstest]
121121fn test_circuit ( block_context : BlockContext , default_all_resource_bounds : ValidResourceBounds ) {
122- let test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ) ;
123- let account =
124- FeatureContract :: AccountWithoutValidations ( CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ) ;
125- let chain_info = & block_context. chain_info ;
126- let state = & mut test_state ( chain_info, BALANCE , & [ ( test_contract, 1 ) , ( account, 1 ) ] ) ;
127- let test_contract_address = test_contract. get_instance_address ( 0 ) ;
128- let account_address = account. get_instance_address ( 0 ) ;
129- let mut nonce_manager = NonceManager :: default ( ) ;
122+ let TestInitData { mut state, account_address, contract_address, mut nonce_manager } =
123+ create_test_init_data (
124+ & block_context. chain_info ,
125+ CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ,
126+ ) ;
130127
131128 // Invoke a function that changes the state and reverts.
132129 let tx_args = invoke_tx_args ! {
133130 sender_address: account_address,
134131 calldata: create_calldata(
135- test_contract_address ,
132+ contract_address ,
136133 "test_circuit" ,
137134 & [ ]
138135 ) ,
139136 nonce: nonce_manager. next( account_address)
140137 } ;
141138 let tx_execution_info = run_invoke_tx (
142- state,
139+ & mut state,
143140 & block_context,
144141 invoke_tx_args ! {
145142 resource_bounds: default_all_resource_bounds,
@@ -155,27 +152,24 @@ fn test_circuit(block_context: BlockContext, default_all_resource_bounds: ValidR
155152#[ case:: vm( default_l1_resource_bounds( ) ) ]
156153#[ case:: gas( default_all_resource_bounds( ) ) ]
157154fn test_rc96_holes ( block_context : BlockContext , #[ case] resource_bounds : ValidResourceBounds ) {
158- let test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ) ;
159- let account =
160- FeatureContract :: AccountWithoutValidations ( CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ) ;
161- let chain_info = & block_context. chain_info ;
162- let state = & mut test_state ( chain_info, BALANCE , & [ ( test_contract, 1 ) , ( account, 1 ) ] ) ;
163- let test_contract_address = test_contract. get_instance_address ( 0 ) ;
164- let account_address = account. get_instance_address ( 0 ) ;
165- let mut nonce_manager = NonceManager :: default ( ) ;
155+ let TestInitData { mut state, account_address, contract_address, mut nonce_manager } =
156+ create_test_init_data (
157+ & block_context. chain_info ,
158+ CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ,
159+ ) ;
166160
167161 // Invoke a function that changes the state and reverts.
168162 let tx_args = invoke_tx_args ! {
169163 sender_address: account_address,
170164 calldata: create_calldata(
171- test_contract_address ,
165+ contract_address ,
172166 "test_rc96_holes" ,
173167 & [ ]
174168 ) ,
175169 nonce: nonce_manager. next( account_address)
176170 } ;
177171 let tx_execution_info = run_invoke_tx (
178- state,
172+ & mut state,
179173 & block_context,
180174 invoke_tx_args ! {
181175 resource_bounds: resource_bounds,
@@ -677,25 +671,20 @@ fn test_revert_invoke(
677671 #[ case] transaction_version : TransactionVersion ,
678672 #[ case] fee_type : FeeType ,
679673) {
680- let test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo0 ) ;
681- let account = FeatureContract :: AccountWithoutValidations ( CairoVersion :: Cairo0 ) ;
682- let chain_info = & block_context. chain_info ;
683- let state = & mut test_state ( chain_info, BALANCE , & [ ( test_contract, 1 ) , ( account, 1 ) ] ) ;
684- let test_contract_address = test_contract. get_instance_address ( 0 ) ;
685- let account_address = account. get_instance_address ( 0 ) ;
686- let mut nonce_manager = NonceManager :: default ( ) ;
674+ let TestInitData { mut state, account_address, contract_address, mut nonce_manager } =
675+ create_test_init_data ( & block_context. chain_info , CairoVersion :: Cairo0 ) ;
687676
688677 // Invoke a function that changes the state and reverts.
689678 let storage_key = felt ! ( 9_u8 ) ;
690679 let tx_execution_info = run_invoke_tx (
691- state,
680+ & mut state,
692681 & block_context,
693682 invoke_tx_args ! {
694683 max_fee,
695684 resource_bounds: all_resource_bounds,
696685 sender_address: account_address,
697686 calldata: create_calldata(
698- test_contract_address ,
687+ contract_address ,
699688 "write_and_revert" ,
700689 // Write some non-zero value.
701690 & [ storage_key, felt!( 99_u8 ) ]
@@ -715,7 +704,10 @@ fn test_revert_invoke(
715704 // Check that the nonce was increased and the fee was deducted.
716705 assert_eq ! (
717706 state
718- . get_fee_token_balance( account_address, chain_info. fee_token_address( & fee_type) )
707+ . get_fee_token_balance(
708+ account_address,
709+ block_context. chain_info. fee_token_address( & fee_type)
710+ )
719711 . unwrap( ) ,
720712 ( felt!( BALANCE . 0 - tx_execution_info. receipt. fee. 0 ) , felt!( 0_u8 ) )
721713 ) ;
@@ -727,9 +719,7 @@ fn test_revert_invoke(
727719 // Check that execution state changes were reverted.
728720 assert_eq ! (
729721 felt!( 0_u8 ) ,
730- state
731- . get_storage_at( test_contract_address, StorageKey :: try_from( storage_key) . unwrap( ) )
732- . unwrap( )
722+ state. get_storage_at( contract_address, StorageKey :: try_from( storage_key) . unwrap( ) ) . unwrap( )
733723 ) ;
734724}
735725
@@ -1471,12 +1461,8 @@ fn test_count_actual_storage_changes(
14711461 let fee_token_address = chain_info. fee_token_address ( & fee_type) ;
14721462
14731463 // Create initial state
1474- let test_contract = FeatureContract :: TestContract ( cairo_version) ;
1475- let account_contract = FeatureContract :: AccountWithoutValidations ( cairo_version) ;
1476- let mut state = test_state ( chain_info, BALANCE , & [ ( account_contract, 1 ) , ( test_contract, 1 ) ] ) ;
1477- let account_address = account_contract. get_instance_address ( 0 ) ;
1478- let contract_address = test_contract. get_instance_address ( 0 ) ;
1479- let mut nonce_manager = NonceManager :: default ( ) ;
1464+ let TestInitData { mut state, account_address, contract_address, mut nonce_manager } =
1465+ create_test_init_data ( & block_context. chain_info , cairo_version) ;
14801466
14811467 let sequencer_address = block_context. block_info . sequencer_address ;
14821468 let initial_sequencer_balance =
@@ -1651,6 +1637,7 @@ fn test_count_actual_storage_changes(
16511637#[ case:: tx_version_1( TransactionVersion :: ONE ) ]
16521638#[ case:: tx_version_3( TransactionVersion :: THREE ) ]
16531639fn test_concurrency_execute_fee_transfer (
1640+ block_context : BlockContext ,
16541641 max_fee : Fee ,
16551642 default_all_resource_bounds : ValidResourceBounds ,
16561643 #[ case] version : TransactionVersion ,
@@ -1660,27 +1647,24 @@ fn test_concurrency_execute_fee_transfer(
16601647 const TRANSFER_AMOUNT : u128 = 100 ;
16611648 const SEQUENCER_BALANCE_LOW_INITIAL : u128 = 50 ;
16621649
1663- let block_context = BlockContext :: create_for_account_testing ( ) ;
1664- let account =
1665- FeatureContract :: AccountWithoutValidations ( CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ) ;
1666- let test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo0 ) ;
16671650 let chain_info = & block_context. chain_info ;
1668- let state = & mut test_state ( chain_info, BALANCE , & [ ( account, 1 ) , ( test_contract, 1 ) ] ) ;
1651+ let TestInitData { mut state, account_address, contract_address, .. } =
1652+ create_test_init_data ( chain_info, CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ) ;
16691653 let ( sequencer_balance_key_low, sequencer_balance_key_high) =
16701654 get_sequencer_balance_keys ( & block_context) ;
16711655 let account_tx = invoke_tx_with_default_flags ( invoke_tx_args ! {
1672- sender_address: account . get_instance_address ( 0 ) ,
1656+ sender_address: account_address ,
16731657 max_fee,
1674- calldata: create_trivial_calldata( test_contract . get_instance_address ( 0 ) ) ,
1658+ calldata: create_trivial_calldata( contract_address ) ,
16751659 resource_bounds: default_all_resource_bounds,
16761660 version
16771661 } ) ;
16781662 let fee_type = & account_tx. fee_type ( ) ;
1679- let fee_token_address = block_context . chain_info . fee_token_address ( fee_type) ;
1663+ let fee_token_address = chain_info. fee_token_address ( fee_type) ;
16801664
1681- // Case 1: The transaction did not read form / write to the sequenser balance before executing
1665+ // Case 1: The transaction did not read from / write to the sequencer balance before executing
16821666 // fee transfer.
1683- let mut transactional_state = TransactionalState :: create_transactional ( state) ;
1667+ let mut transactional_state = TransactionalState :: create_transactional ( & mut state) ;
16841668 let concurrency_mode = true ;
16851669 let result =
16861670 account_tx. execute_raw ( & mut transactional_state, & block_context, concurrency_mode) . unwrap ( ) ;
@@ -1695,7 +1679,7 @@ fn test_concurrency_execute_fee_transfer(
16951679 }
16961680 }
16971681
1698- // Case 2: The transaction read from and write to the sequenser balance before executing fee
1682+ // Case 2: The transaction read from and write to the sequencer balance before executing fee
16991683 // transfer.
17001684
17011685 let transfer_calldata = create_calldata (
@@ -1711,11 +1695,11 @@ fn test_concurrency_execute_fee_transfer(
17111695 Fee ( SEQUENCER_BALANCE_LOW_INITIAL ) ,
17121696 & mut state. state ,
17131697 ) ;
1714- let mut transactional_state = TransactionalState :: create_transactional ( state) ;
1698+ let mut transactional_state = TransactionalState :: create_transactional ( & mut state) ;
17151699
17161700 // Invokes transfer to the sequencer.
17171701 let account_tx = invoke_tx_with_default_flags ( invoke_tx_args ! {
1718- sender_address: account . get_instance_address ( 0 ) ,
1702+ sender_address: account_address ,
17191703 calldata: transfer_calldata,
17201704 max_fee,
17211705 resource_bounds: default_all_resource_bounds,
@@ -1729,7 +1713,7 @@ fn test_concurrency_execute_fee_transfer(
17291713 let storage_writes = transactional_state. cache . borrow ( ) . writes . storage . clone ( ) ;
17301714 let storage_initial_reads = transactional_state. cache . borrow ( ) . initial_reads . storage . clone ( ) ;
17311715
1732- for ( seq_write_val, expexted_write_val ) in [
1716+ for ( seq_write_val, expected_write_val ) in [
17331717 (
17341718 storage_writes. get ( & ( fee_token_address, sequencer_balance_key_low) ) ,
17351719 // Balance after `execute` and without the fee transfer.
@@ -1742,7 +1726,7 @@ fn test_concurrency_execute_fee_transfer(
17421726 ( storage_writes. get ( & ( fee_token_address, sequencer_balance_key_high) ) , Felt :: ZERO ) ,
17431727 ( storage_initial_reads. get ( & ( fee_token_address, sequencer_balance_key_high) ) , Felt :: ZERO ) ,
17441728 ] {
1745- assert_eq ! ( * seq_write_val. unwrap( ) , expexted_write_val ) ;
1729+ assert_eq ! ( * seq_write_val. unwrap( ) , expected_write_val ) ;
17461730 }
17471731}
17481732
@@ -1751,32 +1735,29 @@ fn test_concurrency_execute_fee_transfer(
17511735#[ case:: tx_version_1( TransactionVersion :: ONE ) ]
17521736#[ case:: tx_version_3( TransactionVersion :: THREE ) ]
17531737fn test_concurrent_fee_transfer_when_sender_is_sequencer (
1738+ mut block_context : BlockContext ,
17541739 max_fee : Fee ,
17551740 default_all_resource_bounds : ValidResourceBounds ,
17561741 #[ case] version : TransactionVersion ,
17571742) {
1758- let mut block_context = BlockContext :: create_for_account_testing ( ) ;
1759- let account =
1760- FeatureContract :: AccountWithoutValidations ( CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ) ;
1761- let account_address = account. get_instance_address ( 0_u16 ) ;
1743+ let chain_info = & block_context. chain_info ;
1744+ let TestInitData { mut state, account_address, contract_address, .. } =
1745+ create_test_init_data ( chain_info, CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ) ;
17621746 block_context. block_info . sequencer_address = account_address;
1763- let test_contract = FeatureContract :: TestContract ( CairoVersion :: Cairo0 ) ;
17641747 let sender_balance = BALANCE ;
1765- let chain_info = & block_context. chain_info ;
1766- let state = & mut test_state ( chain_info, sender_balance, & [ ( account, 1 ) , ( test_contract, 1 ) ] ) ;
17671748 let ( sequencer_balance_key_low, sequencer_balance_key_high) =
17681749 get_sequencer_balance_keys ( & block_context) ;
17691750 let account_tx = invoke_tx_with_default_flags ( invoke_tx_args ! {
17701751 max_fee,
17711752 sender_address: account_address,
1772- calldata: create_trivial_calldata( test_contract . get_instance_address ( 0 ) ) ,
1753+ calldata: create_trivial_calldata( contract_address ) ,
17731754 resource_bounds: default_all_resource_bounds,
17741755 version
17751756 } ) ;
17761757 let fee_type = & account_tx. fee_type ( ) ;
1777- let fee_token_address = block_context . chain_info . fee_token_address ( fee_type) ;
1758+ let fee_token_address = chain_info. fee_token_address ( fee_type) ;
17781759
1779- let mut transactional_state = TransactionalState :: create_transactional ( state) ;
1760+ let mut transactional_state = TransactionalState :: create_transactional ( & mut state) ;
17801761 let concurrency_mode = true ;
17811762 let result =
17821763 account_tx. execute_raw ( & mut transactional_state, & block_context, concurrency_mode) . unwrap ( ) ;
@@ -1800,10 +1781,10 @@ fn test_concurrent_fee_transfer_when_sender_is_sequencer(
18001781] ) ]
18011782// TODO(Tzahi, 1/12/2024): Add a case with OldCairo1 instead of Cairo0.
18021783fn test_initial_gas (
1784+ block_context : BlockContext ,
18031785 #[ case] versions : & [ CompilerBasedVersion ] ,
18041786 default_all_resource_bounds : ValidResourceBounds ,
18051787) {
1806- let block_context = BlockContext :: create_for_account_testing ( ) ;
18071788 let account_version = CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ;
18081789 let account = FeatureContract :: AccountWithoutValidations ( account_version) ;
18091790 let account_address = account. get_instance_address ( 0_u16 ) ;
@@ -1908,12 +1889,10 @@ fn test_revert_in_execute(
19081889 block_context : BlockContext ,
19091890 default_all_resource_bounds : ValidResourceBounds ,
19101891) {
1911- let account =
1912- FeatureContract :: AccountWithoutValidations ( CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ) ;
1913- let chain_info = & block_context. chain_info ;
1914- let state = & mut test_state ( chain_info, BALANCE , & [ ( account, 1 ) ] ) ;
1915- let account_address = account. get_instance_address ( 0 ) ;
1916- let mut nonce_manager = NonceManager :: default ( ) ;
1892+ let TestInitData { mut state, account_address, mut nonce_manager, .. } = create_test_init_data (
1893+ & block_context. chain_info ,
1894+ CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ,
1895+ ) ;
19171896
19181897 // Invoke a function that changes the state and reverts.
19191898 let tx_args = invoke_tx_args ! {
@@ -1930,7 +1909,7 @@ fn test_revert_in_execute(
19301909 } ) ;
19311910 let execution_flags = AccountExecutionFlags { validate, ..AccountExecutionFlags :: default ( ) } ;
19321911 let account_tx = AccountTransaction { tx, execution_flags } ;
1933- let tx_execution_info = account_tx. execute ( state, & block_context) . unwrap ( ) ;
1912+ let tx_execution_info = account_tx. execute ( & mut state, & block_context) . unwrap ( ) ;
19341913
19351914 assert ! ( tx_execution_info. is_reverted( ) ) ;
19361915 assert ! (
@@ -1954,24 +1933,14 @@ fn test_call_contract_that_panics(
19541933) {
19551934 // Override enable reverts.
19561935 block_context. versioned_constants . enable_reverts = enable_reverts;
1957- let test_contract = FeatureContract :: TestContract ( cairo_version) ;
1958- // TODO(Yoni): use `class_version` here once the feature contract fully supports Native.
1959- let account =
1960- FeatureContract :: AccountWithoutValidations ( CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) ) ;
1961- let chain_info = & block_context. chain_info ;
1962- let state = & mut test_state ( chain_info, BALANCE , & [ ( test_contract, 1 ) , ( account, 1 ) ] ) ;
1963- let test_contract_address = test_contract. get_instance_address ( 0 ) ;
1964- let account_address = account. get_instance_address ( 0 ) ;
1965- let mut nonce_manager = NonceManager :: default ( ) ;
1936+ let TestInitData { mut state, account_address, contract_address, mut nonce_manager } =
1937+ create_test_init_data ( & block_context. chain_info , cairo_version) ;
19661938
1967- let new_class_hash = test_contract . get_class_hash ( ) ;
1939+ let new_class_hash = FeatureContract :: TestContract ( cairo_version ) . get_class_hash ( ) ;
19681940 let to_panic = true . into ( ) ;
19691941
19701942 let calldata = [
1971- * FeatureContract :: TestContract ( CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) )
1972- . get_instance_address ( 0 )
1973- . 0
1974- . key ( ) ,
1943+ * contract_address. 0 . key ( ) ,
19751944 selector_from_name ( inner_selector) . 0 ,
19761945 felt ! ( 2_u8 ) ,
19771946 new_class_hash. 0 ,
@@ -1982,14 +1951,14 @@ fn test_call_contract_that_panics(
19821951 let tx_args = invoke_tx_args ! {
19831952 sender_address: account_address,
19841953 calldata: create_calldata(
1985- test_contract_address ,
1954+ contract_address ,
19861955 "test_call_contract_revert" ,
19871956 & calldata
19881957 ) ,
19891958 nonce: nonce_manager. next( account_address)
19901959 } ;
19911960 let tx_execution_info = run_invoke_tx (
1992- state,
1961+ & mut state,
19931962 & block_context,
19941963 invoke_tx_args ! {
19951964 resource_bounds: default_all_resource_bounds,
0 commit comments