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@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

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

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
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
@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

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) exceed_tx_gas_limit
...fork_Osaka-state_test-exceed_tx_gas_limit_True True
...fork_Osaka-state_test-exceed_tx_gas_limit_False False
...fork_Osaka-blockchain_test_from_state_test-exceed_tx_gas_limit_True True
...fork_Osaka-blockchain_test_from_state_test-exceed_tx_gas_limit_False False