Skip to content

test_create_account_creation_charge()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_account_creation_charge@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_account_creation_charge --fork Bogota

Verify NEW_ACCOUNT is charged only when the created account does not already exist in the trie.

Empty init code means zero code deposit, so NEW_ACCOUNT is the only create state cost. A fresh target is charged it; a pre-existing balance-only target (balance, no code, zero nonce) is not. The probe SSTORE both confirms the create succeeded and makes state gas dominate the header, so gas_used differs by exactly NEW_ACCOUNT between the two cases.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
@pytest.mark.parametrize(
    "account_new",
    [
        pytest.param(True, id="new_account"),
        pytest.param(False, id="existing_account"),
    ],
)
@pytest.mark.with_all_create_opcodes()
@EIPChecklist.GasCostChanges.Test.GasUpdatesMeasurement()
@pytest.mark.valid_from("EIP8037")
def test_create_account_creation_charge(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
    account_new: bool,
) -> None:
    """
    Verify NEW_ACCOUNT is charged only when the created account does not
    already exist in the trie.

    Empty init code means zero code deposit, so NEW_ACCOUNT is the only
    create state cost. A fresh target is charged it; a pre-existing
    balance-only target (balance, no code, zero nonce) is not. The probe
    SSTORE both confirms the create succeeded and makes state gas dominate
    the header, so gas_used differs by exactly NEW_ACCOUNT between the two
    cases.
    """
    mstore_value, size = init_code_at_high_bytes(Op.STOP)
    create_call = create_opcode(
        value=0, offset=0, size=size, account_new=account_new
    )
    storage = Storage()
    factory_code = Op.MSTORE(0, mstore_value) + Op.SSTORE(
        storage.store_next(1, "create_succeeds"), Op.GT(create_call, 0)
    )
    factory = pre.deploy_contract(code=factory_code)

    # Factory deployed via deploy_contract starts at nonce 1.
    create_address = compute_create_address(
        address=factory,
        nonce=1,
        salt=0,
        initcode=bytes(Op.STOP),
        opcode=create_opcode,
    )
    if not account_new:
        pre.fund_address(create_address, amount=1)

    # State gas dominates the header, so gas_used equals the factory's
    # state cost: NEW_ACCOUNT plus the probe SSTORE for a fresh target,
    # just the SSTORE for a pre-existing one.
    state_cost = factory_code.state_cost(fork)
    tx = Transaction(
        to=factory,
        state_gas_reservoir=state_cost,
        sender=pre.fund_eoa(),
    )

    state_test(
        pre=pre,
        tx=tx,
        post={factory: Account(storage=storage)},
        blockchain_test_header_verify=Header(gas_used=state_cost),
    )

Parametrized Test Cases

This test generates 4 parametrized test cases across 2 forks.