Skip to content

test_create_tx_header_gas_used()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_tx_header_gas_used@ffe48572.

Generate fixtures for these test cases for Bogota with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_tx_header_gas_used --fork Bogota

Verify block header gas_used for a successful CREATE transaction.

A contract creation tx (to=None) with known gas costs. Compute exact gas_used from first principles and verify against the block header. Catches bugs where clients report gas_limit instead of actual consumed gas.

For a fresh target the top-frame NEW_ACCOUNT state gas is charged and dominates the execution gas, so gas_used == NEW_ACCOUNT. For a pre-existing balance-only leaf the target is not EMPTY pre-tx, so the top-frame NEW_ACCOUNT is never charged: net state gas is zero and only the execution dimension remains. The block-level calldata floor tops up that execution remainder, so the expected value is the greater of the execution intrinsic and the floor, and fails if a stray NEW_ACCOUNT is charged.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
@pytest.mark.parametrize(
    "target",
    [
        pytest.param("new", id="new_account"),
        pytest.param("existing", id="existing_account"),
    ],
)
@pytest.mark.pre_alloc_mutable()
@EIPChecklist.GasCostChanges.Test.GasUpdatesMeasurement()
@pytest.mark.valid_from("EIP8037")
def test_create_tx_header_gas_used(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    target: str,
) -> None:
    """
    Verify block header gas_used for a successful CREATE transaction.

    A contract creation tx (to=None) with known gas costs. Compute
    exact gas_used from first principles and verify against the block
    header. Catches bugs where clients report gas_limit instead of
    actual consumed gas.

    For a fresh target the top-frame NEW_ACCOUNT state gas is charged and
    dominates the execution gas, so gas_used == NEW_ACCOUNT. For a
    pre-existing balance-only leaf the target is not EMPTY pre-tx, so the
    top-frame NEW_ACCOUNT is never charged: net state gas is zero and only
    the execution dimension remains. The block-level calldata floor tops up
    that execution remainder, so the expected value is the greater of the
    execution intrinsic and the floor, and fails if a stray NEW_ACCOUNT is
    charged.
    """
    initcode = Op.STOP
    create_state_gas = fork.create_state_gas(code_size=1)

    if target == "existing":
        sender = pre.fund_eoa(nonce=0)
        contract_address = compute_create_address(address=sender, nonce=0)
        # Balance-only leaf: alive and deployable, so the creation
        # succeeds and (being non-EMPTY pre-tx) the top-frame NEW_ACCOUNT
        # is never charged.
        pre.fund_address(contract_address, amount=1)
    else:
        sender = pre.fund_eoa()

    tx = Transaction(
        to=None,
        data=initcode,
        state_gas_reservoir=create_state_gas,
        sender=sender,
    )

    # block_gas_used = max(block_execution, block_state)
    if target == "existing":
        intrinsic_cost = fork.transaction_intrinsic_cost_calculator()
        # Execution-only creation intrinsic; STOP initcode deploys empty
        # code (zero deposit) and the pre-existing target adds no state
        # gas. The block-level calldata floor tops up this small execution
        # remainder and, being the larger of the two, is what the header
        # reflects (the floor applies to block-level execution gas).
        execution_intrinsic = intrinsic_cost(
            calldata=bytes(initcode),
            contract_creation=True,
            return_cost_deducted_prior_execution=True,
        )
        floor = fork.transaction_data_floor_cost_calculator()(
            data=bytes(initcode), contract_creation=True
        )
        assert floor > execution_intrinsic, (
            "the floor must bind for this arm to pin floor-in-header"
        )
        expected_gas_used = max(execution_intrinsic, floor)
    else:
        # For a minimal CREATE tx deploying Op.STOP (1 byte),
        # state gas (new account) dominates execution gas.
        expected_gas_used = fork.transaction_top_frame_state_gas(
            contract_creation=True
        )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx],
                header_verify=Header(gas_used=expected_gas_used),
            ),
        ],
        post={},
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 2 forks.