Skip to content

test_tx_gas_limit_cap_contract_creation()

Documentation for tests/osaka/eip7825_transaction_gas_limit_cap/test_tx_gas_limit.py::test_tx_gas_limit_cap_contract_creation@21507778.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/osaka/eip7825_transaction_gas_limit_cap/test_tx_gas_limit.py::test_tx_gas_limit_cap_contract_creation --fork Amsterdam

Test the transaction gas limit cap behavior for contract creation.

Source code in tests/osaka/eip7825_transaction_gas_limit_cap/test_tx_gas_limit.py
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
458
459
460
461
462
463
464
465
466
467
@pytest.mark.parametrize(
    "exceed_tx_gas_limit",
    [
        pytest.param(True),
        pytest.param(False),
    ],
)
@pytest.mark.valid_from("Osaka")
def test_tx_gas_limit_cap_contract_creation(
    state_test: StateTestFiller,
    pre: Alloc,
    total_cost_floor_per_token: int,
    exceed_tx_gas_limit: bool,
    fork: Fork,
) -> None:
    """Test the transaction gas limit cap behavior for contract creation."""
    intrinsic_cost = fork.transaction_intrinsic_cost_calculator()
    tx_gas_limit_cap = fork.transaction_gas_limit_cap()
    assert tx_gas_limit_cap is not None, (
        "Fork does not have a transaction gas limit cap"
    )
    gas_available = tx_gas_limit_cap - intrinsic_cost(contract_creation=True)

    max_tokens_in_calldata = gas_available // total_cost_floor_per_token
    num_of_bytes = (max_tokens_in_calldata // 4) + int(exceed_tx_gas_limit)

    # Cannot exceed max contract code size
    num_of_bytes = min(num_of_bytes, fork.max_code_size())

    code = Op.JUMPDEST * num_of_bytes

    # Craft a contract creation transaction that exceeds the transaction gas
    # limit cap
    #
    # Total cost =
    # intrinsic cost (base tx cost + contract creation cost)
    # + calldata cost + init code execution cost
    #
    # The contract body is filled with JUMPDEST instructions, so:
    # total cost = intrinsic cost + calldata cost + (num_of_jumpdest * 1 gas)
    #
    # If the total cost exceeds the tx limit cap, the transaction should fail

    total_cost = (
        intrinsic_cost(contract_creation=True, calldata=code) + num_of_bytes
    )

    tx = Transaction(
        to=None,
        data=code,
        gas_limit=tx_gas_limit_cap,
        sender=pre.fund_eoa(),
        error=TransactionException.INTRINSIC_GAS_BELOW_FLOOR_GAS_COST
        if total_cost > tx_gas_limit_cap
        else None,
    )

    state_test(
        pre=pre,
        post={},
        tx=tx,
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 2 forks.