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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428 | @pytest.mark.parametrize(
"refund_tx_reverts",
[
pytest.param(True, id="refund_tx_reverts"),
pytest.param(False, id=""),
],
)
@pytest.mark.parametrize(
"refund_tx_has_extra_gas_limit",
[
pytest.param(True, id="refund_tx_has_extra_gas"),
pytest.param(False, id=""),
],
)
@pytest.mark.parametrize(
"extra_tx_data_floor",
[
pytest.param(True, id=""),
pytest.param(False, id="extra_tx_hits_data_floor"),
],
)
@pytest.mark.parametrize(
"exceed_block_gas_limit",
[
pytest.param(True, marks=pytest.mark.exception_test),
False,
],
)
@pytest.mark.with_all_refund_types()
@pytest.mark.execute(pytest.mark.skip(reason="Requires specific gas price"))
@pytest.mark.valid_from("EIP8037")
def test_multi_transaction_gas_accounting(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
fork: Fork,
refund_type: RefundTypes,
refund_tx_has_extra_gas_limit: bool,
exceed_block_gas_limit: bool,
extra_tx_data_floor: bool,
refund_tx_reverts: bool,
) -> None:
"""
Test block gas accounting with refunds per EIP-7778.
When exceed_block_gas_limit=True, we create a scenario where:
- Pre-refund gas (gas_used) > block_gas_limit - intrinsic_cost
(no room for another tx with correct EIP-7778 accounting)
- Post-refund gas (gas_spent) <= block_gas_limit - intrinsic_cost
(appears to have room with old refund-based accounting)
This tests that clients correctly use pre-refund gas for block accounting.
"""
# TODO[EIP-8037]: this test's exceed_block_gas_limit branch builds
# `environment_gas_limit = total - 1` from a single combined
# `total_block_gas_used`, but post-fix the auth refund splits the
# regular vs state dimensions further. Reworking the per-dimension
# budget math is out of scope for the auth-refund spec fix; until
# then, skip the AUTHORIZATION_EXISTING_AUTHORITY case here.
if refund_type == RefundTypes.AUTHORIZATION_EXISTING_AUTHORITY:
pytest.skip(
"AUTHORIZATION_EXISTING_AUTHORITY not yet adapted to the "
"two-dimensional block budget post EIP-8037 auth-refund fix"
)
intrinsic_cost_calc = fork.transaction_intrinsic_cost_calculator()
data_floor_calc = fork.transaction_data_floor_cost_calculator()
refunds_count = 10
stop_bytecode = Op.STOP
stop_address = pre.deterministic_deploy_contract(deploy_code=stop_bytecode)
post = Alloc()
(
gas_used_post_refund,
gas_used_pre_refund,
tx_state_gas,
call_data_floor_cost,
refund_tx,
) = build_refund_tx(
fork=fork,
pre=pre,
post=post,
refund_types={refund_type},
refunds_count=refunds_count,
refund_tx_reverts=refund_tx_reverts,
call_data=b"",
refund_tx_has_extra_gas_limit=refund_tx_has_extra_gas_limit,
exceed_block_gas_limit=exceed_block_gas_limit,
)
refund_tx_gas_used = max(gas_used_post_refund, call_data_floor_cost)
extra_tx_sender = pre.fund_eoa()
extra_tx_calldata = b"\xff" if extra_tx_data_floor else b""
extra_tx_intrinsic_gas_cost = intrinsic_cost_calc(
calldata=extra_tx_calldata
)
# Block regular gas applies the calldata floor to the actual charge.
extra_tx_block_gas = max(
intrinsic_cost_calc(
calldata=extra_tx_calldata,
return_cost_deducted_prior_execution=True,
),
data_floor_calc(data=extra_tx_calldata),
)
extra_tx = Transaction(
to=stop_address,
data=extra_tx_calldata,
gas_limit=extra_tx_intrinsic_gas_cost,
sender=extra_tx_sender,
expected_receipt={
"gas_used": refund_tx_gas_used + extra_tx_intrinsic_gas_cost,
},
error=(
TransactionException.GAS_ALLOWANCE_EXCEEDED
if exceed_block_gas_limit
else None
),
)
# EIP-8037: block_gas_used = max(sum_regular, sum_state)
# Extra tx has no state gas, so its state gas contribution = 0
block_regular = gas_used_pre_refund + extra_tx_block_gas
block_state = tx_state_gas
total_block_gas_used = max(block_regular, block_state)
# The block gas_limit must accommodate extra_tx's full gas_limit
# (floor-inclusive, like its block-regular charge). For
# exceed_block_gas_limit=True we set the limit below
# total_block_gas_used to test that the extra_tx fails.
if exceed_block_gas_limit:
environment_gas_limit = total_block_gas_used - 1
else:
environment_gas_limit = (
gas_used_pre_refund + extra_tx_intrinsic_gas_cost
)
txs = [refund_tx, extra_tx]
blockchain_test(
pre=pre,
blocks=[
Block(
txs=txs,
exception=[
BlockException.GAS_USED_OVERFLOW,
TransactionException.GAS_ALLOWANCE_EXCEEDED,
]
if exceed_block_gas_limit
else None,
expected_gas_used=total_block_gas_used
if not exceed_block_gas_limit
else None,
gas_limit=environment_gas_limit,
)
],
post=post,
genesis_environment=Environment(gas_limit=environment_gas_limit),
)
|