@@ -19,7 +19,7 @@ use expect_test::expect_file;
1919use indexmap:: IndexMap ;
2020use starknet_api:: block:: StarknetVersion ;
2121use starknet_api:: contract_class:: SierraVersion ;
22- use starknet_api:: core:: EthAddress ;
22+ use starknet_api:: core:: { ClassHash , ContractAddress , EthAddress } ;
2323use starknet_api:: executable_transaction:: InvokeTransaction ;
2424use starknet_api:: test_utils:: invoke:: invoke_tx;
2525use starknet_api:: transaction:: fields:: ContractAddressSalt ;
@@ -107,6 +107,13 @@ const FEE_TRANSFER_SYSCALLS: [Selector; 10] = [
107107/// All other syscalls are called only once.
108108const SYSCALLS_CALLED_TWICE : [ Selector ; 2 ] = [ Selector :: Secp256k1New , Selector :: Secp256r1New ] ;
109109
110+ struct OsResourcesTestSetup {
111+ os_resources_contract_address : ContractAddress ,
112+ stable_contract_address : ContractAddress ,
113+ stable_contract_class_hash : ClassHash ,
114+ test_builder : TestBuilder < DictStateReader > ,
115+ }
116+
110117/// See [SYSCALLS_WITH_VIRTUAL_BUILTINS] for why this function is needed.
111118fn update_resources_for_virtual_builtin_syscall (
112119 selector : Selector ,
@@ -133,16 +140,89 @@ fn update_resources_for_virtual_builtin_syscall(
133140 }
134141}
135142
143+ /// Setup an test builder with
144+ /// 1. the OS-resources contract deployed,
145+ /// 2. funded (it is an account contract as well),
146+ /// 3. the initial block context (and therefore subsequent block contexts) with the minimal sierra
147+ /// version for gas tracking set to "infinity", in order to force step tracking mode, and
148+ /// 4. the stable contract declared and deployed.
149+ /// Pass the raw VC used in tests, to make sure the test initial state is set up consistently.
150+ async fn setup_test_builder ( raw_vc : Option < & RawVersionedConstants > ) -> OsResourcesTestSetup {
151+ // Setup the test initial state and test builder.
152+ // Need to explicitly set up the state to be able to override the minimal sierra version for gas
153+ // tracking, in order to force step tracking mode.
154+ let os_resources_contract = FeatureContract :: OsResourcesTest ( RunnableCairo1 :: Casm ) ;
155+ let ( mut initial_state_data, [ os_resources_contract_address] ) =
156+ create_default_initial_state_data :: < DictStateReader , 1 > ( [ (
157+ os_resources_contract,
158+ calldata ! [ ] ,
159+ ) ] )
160+ . await ;
161+ initial_state_data. initial_state . block_context = {
162+ let block_context = & initial_state_data. initial_state . block_context ;
163+ let mut vc = block_context. versioned_constants ( ) . clone ( ) ;
164+ if let Some ( raw_vc) = raw_vc {
165+ assert_eq ! ( vc, raw_vc. clone( ) . into( ) ) ;
166+ }
167+ vc. min_sierra_version_for_sierra_gas = SierraVersion :: new ( 99 , 99 , 99 ) ;
168+ BlockContext :: new (
169+ block_context. block_info ( ) . clone ( ) ,
170+ block_context. chain_info ( ) . clone ( ) ,
171+ vc,
172+ block_context. bouncer_config . clone ( ) ,
173+ )
174+ } ;
175+ let virtual_os = false ;
176+ let mut test_builder = TestBuilder :: new_with_initial_state_data (
177+ initial_state_data,
178+ TestBuilderConfig :: default ( ) ,
179+ virtual_os,
180+ ) ;
181+ test_builder. add_fund_address_tx_with_default_amount ( os_resources_contract_address) ;
182+
183+ // Declare and deploy an instance of the stable contract.
184+ let stable_contract_sierra = & DEPLOYABLE_FOR_RESOURCE_MEASUREMENT_CONTRACT_SIERRA ;
185+ let stable_contract_casm = & DEPLOYABLE_FOR_RESOURCE_MEASUREMENT_CONTRACT_CASM ;
186+ let stable_contract_class_hash = stable_contract_sierra. calculate_class_hash ( ) ;
187+ let extra_declare_args = declare_tx_args ! {
188+ sender_address: * FUNDED_ACCOUNT_ADDRESS ,
189+ nonce: test_builder. next_nonce( * FUNDED_ACCOUNT_ADDRESS ) ,
190+ resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
191+ } ;
192+ test_builder. add_explicit_cairo1_declare_tx (
193+ stable_contract_sierra,
194+ ( * * stable_contract_casm) . clone ( ) ,
195+ extra_declare_args,
196+ & test_builder. chain_id ( ) ,
197+ ) ;
198+ let deploy_from_zero = true ;
199+ let ( deploy_tx, stable_contract_address) =
200+ get_deploy_contract_tx_and_address_with_salt_and_deployer (
201+ stable_contract_class_hash,
202+ calldata ! [ Felt :: ZERO ] , // Ctor calldata length.
203+ test_builder. next_nonce ( * FUNDED_ACCOUNT_ADDRESS ) ,
204+ * NON_TRIVIAL_RESOURCE_BOUNDS ,
205+ ContractAddressSalt :: default ( ) ,
206+ deploy_from_zero,
207+ ) ;
208+ test_builder. add_invoke_tx ( deploy_tx, None , None ) ;
209+
210+ // Move on to the next block, so the measurement txs are in their own block.
211+ test_builder. move_to_next_block ( ) ;
212+
213+ OsResourcesTestSetup {
214+ os_resources_contract_address,
215+ stable_contract_address,
216+ stable_contract_class_hash,
217+ test_builder,
218+ }
219+ }
220+
136221/// Regression test for the list of syscalls called during the fee transfer phase of a transaction.
137222#[ tokio:: test]
138223async fn test_fee_transfer_syscalls ( ) {
139- let os_resources_contract = FeatureContract :: OsResourcesTest ( RunnableCairo1 :: Casm ) ;
140- let ( mut builder, [ os_resources_contract_address] ) =
141- TestBuilder :: create_standard ( [ ( os_resources_contract, calldata ! [ ] ) ] ) . await ;
142-
143- // Fund the contract - it will be used as the account.
144- // Then, move on to the next block, so the syscall-measurement tx is in it's own block.
145- builder. add_fund_address_tx_with_default_amount ( os_resources_contract_address) ;
224+ let OsResourcesTestSetup { os_resources_contract_address, test_builder : mut builder, .. } =
225+ setup_test_builder ( None ) . await ;
146226
147227 // Invoke from the OS resources contract, with zeros as calldata, to make the __execute__ do
148228 // nothing. All resulting events should be from the fee transfer call.
@@ -201,71 +281,16 @@ async fn test_fee_transfer_syscalls() {
201281/// changes in the deploying contract address are not reflected in the measurements.
202282#[ tokio:: test]
203283async fn test_os_resources_regression ( ) {
204- let os_resources_contract = FeatureContract :: OsResourcesTest ( RunnableCairo1 :: Casm ) ;
205284 let version = StarknetVersion :: LATEST ;
206285 let mut raw_vc: RawVersionedConstants =
207286 serde_json:: from_str ( VersionedConstants :: json_str ( & version) . unwrap ( ) ) . unwrap ( ) ;
208287
209- // Setup the test initial state and test builder.
210- // Need to explicitly set up the state to be able to override the minimal sierra version for gas
211- // tracking, in order to force step tracking mode.
212- let ( mut initial_state_data, [ os_resources_contract_address] ) =
213- create_default_initial_state_data :: < DictStateReader , 1 > ( [ (
214- os_resources_contract,
215- calldata ! [ ] ,
216- ) ] )
217- . await ;
218- initial_state_data. initial_state . block_context = {
219- let block_context = & initial_state_data. initial_state . block_context ;
220- let mut vc = block_context. versioned_constants ( ) . clone ( ) ;
221- assert_eq ! ( vc, raw_vc. clone( ) . into( ) ) ;
222- vc. min_sierra_version_for_sierra_gas = SierraVersion :: new ( 99 , 99 , 99 ) ;
223- BlockContext :: new (
224- block_context. block_info ( ) . clone ( ) ,
225- block_context. chain_info ( ) . clone ( ) ,
226- vc,
227- block_context. bouncer_config . clone ( ) ,
228- )
229- } ;
230- let virtual_os = false ;
231- let mut test_builder = TestBuilder :: new_with_initial_state_data (
232- initial_state_data,
233- TestBuilderConfig :: default ( ) ,
234- virtual_os,
235- ) ;
236-
237- // Fund the contract - it will be used as the account.
238- test_builder. add_fund_address_tx_with_default_amount ( os_resources_contract_address) ;
239-
240- // Declare and deploy an instance of the stable contract.
241- let stable_contract_sierra = & DEPLOYABLE_FOR_RESOURCE_MEASUREMENT_CONTRACT_SIERRA ;
242- let stable_contract_casm = & DEPLOYABLE_FOR_RESOURCE_MEASUREMENT_CONTRACT_CASM ;
243- let stable_contract_class_hash = stable_contract_sierra. calculate_class_hash ( ) ;
244- let extra_declare_args = declare_tx_args ! {
245- sender_address: * FUNDED_ACCOUNT_ADDRESS ,
246- nonce: test_builder. next_nonce( * FUNDED_ACCOUNT_ADDRESS ) ,
247- resource_bounds: * NON_TRIVIAL_RESOURCE_BOUNDS ,
248- } ;
249- test_builder. add_explicit_cairo1_declare_tx (
250- stable_contract_sierra,
251- ( * * stable_contract_casm) . clone ( ) ,
252- extra_declare_args,
253- & test_builder. chain_id ( ) ,
254- ) ;
255- let deploy_from_zero = true ;
256- let ( deploy_tx, stable_contract_address) =
257- get_deploy_contract_tx_and_address_with_salt_and_deployer (
258- stable_contract_class_hash,
259- calldata ! [ Felt :: ZERO ] , // Ctor calldata length.
260- test_builder. next_nonce ( * FUNDED_ACCOUNT_ADDRESS ) ,
261- * NON_TRIVIAL_RESOURCE_BOUNDS ,
262- ContractAddressSalt :: default ( ) ,
263- deploy_from_zero,
264- ) ;
265- test_builder. add_invoke_tx ( deploy_tx, None , None ) ;
266-
267- // Move on to the next block, so the syscall-measurement tx is in it's own block.
268- test_builder. move_to_next_block ( ) ;
288+ let OsResourcesTestSetup {
289+ os_resources_contract_address,
290+ stable_contract_address,
291+ stable_contract_class_hash,
292+ mut test_builder,
293+ } = setup_test_builder ( Some ( & raw_vc) ) . await ;
269294
270295 // Add the syscall-measurement tx.
271296 let tx = InvokeTransaction :: create (
0 commit comments