181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379 | @pytest.mark.parametrize(
"initcode_name,gas_test_case",
[
pytest.param(
i,
g,
marks=(
[pytest.mark.exception_test]
if g == "too_little_intrinsic_gas"
else []
),
)
for i in [
"max_size_zeros",
"max_size_ones",
"empty",
"single_byte",
"32_bytes",
"33_bytes",
"max_size_minus_word",
"max_size_minus_word_plus_byte",
]
for g in [
"too_little_intrinsic_gas",
"exact_intrinsic_gas",
"too_little_execution_gas",
"exact_execution_gas",
]
if valid_gas_test_case(i, g)
],
)
class TestContractCreationGasUsage:
"""
Test the gas cost behavior of a contract creating transaction.
The following scenarios are tested:
1. Test with exact intrinsic gas minus one, contract create fails and tx is
invalid.
2. Test with exact intrinsic gas, contract create fails, but tx is valid.
3. Test with exact execution gas minus one, contract create fails, but tx
is valid.
4. Test with exact execution gas, contract create succeeds.
Initcode must be within a valid EIP-3860 length.
"""
@pytest.fixture
def tx_access_list(self) -> List[AccessList]:
"""
Return an access list to raise the intrinsic gas cost.
Upon EIP-7623 activation, we need to use an access list to raise the
intrinsic gas cost to be above the floor data cost.
"""
return [
AccessList(address=Address(i), storage_keys=[])
for i in range(1, 478)
]
@pytest.fixture
def exact_intrinsic_gas(
self, fork: Fork, initcode: Initcode, tx_access_list: List[AccessList]
) -> int:
"""
Calculate the intrinsic tx gas cost.
"""
tx_intrinsic_gas_cost_calculator = (
fork.transaction_intrinsic_cost_calculator()
)
assert tx_intrinsic_gas_cost_calculator(
calldata=initcode,
contract_creation=True,
access_list=tx_access_list,
) == tx_intrinsic_gas_cost_calculator(
calldata=initcode,
contract_creation=True,
access_list=tx_access_list,
return_cost_deducted_prior_execution=True,
)
return tx_intrinsic_gas_cost_calculator(
calldata=initcode,
contract_creation=True,
access_list=tx_access_list,
)
@pytest.fixture
def exact_execution_gas(
self, fork: Fork, exact_intrinsic_gas: int, initcode: Initcode
) -> int:
"""
Calculate total execution gas cost.
"""
return exact_intrinsic_gas + initcode.gas_cost(fork)
@pytest.fixture
def tx_error(self, gas_test_case: str) -> TransactionException | None:
"""
Return the transaction exception, or None, as expected.
Check that the transaction is invalid if too little intrinsic gas is
specified, otherwise the tx is valid and succeeds.
"""
if gas_test_case == "too_little_intrinsic_gas":
return TransactionException.INTRINSIC_GAS_TOO_LOW
return None
@pytest.fixture
def tx(
self,
sender: EOA,
initcode: Initcode,
gas_test_case: str,
tx_access_list: List[AccessList],
tx_error: TransactionException | None,
exact_intrinsic_gas: int,
exact_execution_gas: int,
) -> Transaction:
"""
Return a tx with `gas_limit` corresponding to the `gas_test_case`.
Implement the gas_test_case by setting the `gas_limit` of the tx
appropriately and test whether the tx succeeds or fails with
appropriate error.
"""
if gas_test_case == "too_little_intrinsic_gas":
gas_limit = exact_intrinsic_gas - 1
elif gas_test_case == "exact_intrinsic_gas":
gas_limit = exact_intrinsic_gas
elif gas_test_case == "too_little_execution_gas":
gas_limit = exact_execution_gas - 1
elif gas_test_case == "exact_execution_gas":
gas_limit = exact_execution_gas
else:
pytest.fail("Invalid gas test case provided.")
return Transaction(
to=None,
access_list=tx_access_list,
data=initcode,
gas_limit=gas_limit,
error=tx_error,
sender=sender,
# The entire gas limit is expected to be consumed.
expected_receipt=TransactionReceipt(cumulative_gas_used=gas_limit),
)
@pytest.fixture
def post(
self,
sender: EOA,
initcode: Initcode,
gas_test_case: str,
exact_intrinsic_gas: int,
exact_execution_gas: int,
) -> Alloc:
"""
Test contract creation fails unless enough execution gas is provided.
"""
create_contract_address = compute_create_address(
address=sender,
nonce=0,
)
if (
gas_test_case == "exact_intrinsic_gas"
and exact_intrinsic_gas == exact_execution_gas
):
# Special scenario where the execution of the initcode and
# gas cost to deploy are zero
return Alloc(
{create_contract_address: Account(code=initcode.deploy_code)}
)
elif gas_test_case == "exact_execution_gas":
return Alloc(
{create_contract_address: Account(code=initcode.deploy_code)}
)
return Alloc({create_contract_address: Account.NONEXISTENT})
@pytest.mark.slow()
def test_gas_usage(
self,
state_test: StateTestFiller,
env: Environment,
pre: Alloc,
post: Alloc,
tx: Transaction,
) -> None:
"""
Test transaction and contract creation using different gas limits.
"""
state_test(
env=env,
pre=pre,
post=post,
tx=tx,
)
|