Skip to content

test_trailing_tx_admission_uses_pre_refund_gas()

Documentation for tests/amsterdam/eip7778_block_gas_accounting_without_refunds/test_gas_accounting.py::test_trailing_tx_admission_uses_pre_refund_gas@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7778_block_gas_accounting_without_refunds/test_gas_accounting.py::test_trailing_tx_admission_uses_pre_refund_gas --fork Amsterdam

Test that the admission gate uses the pre-refund accumulator when the trailing tx's gas_limit exceeds its actual usage.

Without this slack a post-refund gate is masked: the block is still rejected by the gas_used > gas_limit check. With it, a buggy implementation admits the trailing tx yet stays within the block gas limit, diverging from the expected-invalid fixture.

Source code in tests/amsterdam/eip7778_block_gas_accounting_without_refunds/test_gas_accounting.py
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
@EIPChecklist.BlockLevelConstraint.Test.Boundary.Under()
@EIPChecklist.BlockLevelConstraint.Test.Boundary.Exact()
@EIPChecklist.BlockLevelConstraint.Test.Boundary.Over()
@pytest.mark.inclusion_test
@TransactionFailure.with_all_tx_failures()
@pytest.mark.with_all_refund_types()
@pytest.mark.filter_combinations(
    lambda refund_type, refund_tx_failure, **_: not (
        refund_type == RefundTypes.STORAGE_CLEAR
        and refund_tx_failure is not None
    ),
    reason=(
        "STORAGE_CLEAR refund is zero on revert, so post_refund == "
        "pre_refund and the admission bypass cannot manifest"
    ),
)
@pytest.mark.parametrize(
    "trailing_tx_block_gas_limit_delta",
    [
        pytest.param(
            1,
            id="extra_block_gas_limit",
        ),
        pytest.param(
            0,
            id="exact_block_gas_limit",
        ),
        pytest.param(
            -1,
            marks=[pytest.mark.exception_test],
            id="exceeds_block_gas_limit",
        ),
    ],
)
@pytest.mark.execute(pytest.mark.skip(reason="Requires specific gas price"))
def test_trailing_tx_admission_uses_pre_refund_gas(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    refund_type: RefundTypes,
    refund_tx_failure: TransactionFailure | None,
    trailing_tx_block_gas_limit_delta: int,
) -> None:
    """
    Test that the admission gate uses the pre-refund accumulator when
    the trailing tx's gas_limit exceeds its actual usage.

    Without this slack a post-refund gate is masked: the block is
    still rejected by the gas_used > gas_limit check. With it, a buggy
    implementation admits the trailing tx yet stays within the block gas
    limit, diverging from the expected-invalid fixture.
    """
    intrinsic_cost_calc = fork.transaction_intrinsic_cost_calculator()

    refunds_count = 10
    stop_address = pre.deterministic_deploy_contract(deploy_code=Op.STOP)
    exceeds_block_gas_limit = trailing_tx_block_gas_limit_delta < 0

    refund_tx = RefundTransaction.build(
        fork=fork,
        sender=pre.fund_eoa(INITIAL_FUND),
        refund_types={refund_type},
        refunds_count=refunds_count,
        tx_failure=refund_tx_failure,
    )
    refund_tx.set_pre(pre)

    assert refund_tx.gas_used_pre_refund > refund_tx.receipt_gas_used, (
        "Parametrization must produce a refund; without one the admission "
        "bypass cannot occur"
    )

    refund_tx_block_gas_used = refund_tx.block_execution()

    trailing_tx_sender = pre.fund_eoa()
    trailing_tx_intrinsic_gas_cost = intrinsic_cost_calc(calldata=b"")

    # Slack so a buggy admit stays within the block gas limit.
    trailing_tx_gas_limit = 2 * trailing_tx_intrinsic_gas_cost
    trailing_tx = Transaction(
        to=stop_address,
        gas_limit=trailing_tx_gas_limit,
        sender=trailing_tx_sender,
        error=TransactionException.GAS_ALLOWANCE_EXCEEDED
        if exceeds_block_gas_limit
        else None,
    )

    environment_gas_limit = (
        refund_tx_block_gas_used
        + trailing_tx_gas_limit
        + trailing_tx_block_gas_limit_delta
    )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[refund_tx, trailing_tx],
                exception=[
                    BlockException.GAS_USED_OVERFLOW,
                    TransactionException.GAS_ALLOWANCE_EXCEEDED,
                ]
                if exceeds_block_gas_limit
                else None,
                gas_limit=environment_gas_limit,
            )
        ],
        post=refund_tx.post(pre, exceeds_block_gas_limit),
        genesis_environment=Environment(gas_limit=environment_gas_limit),
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.