Skip to content

test_no_account_charge_on_existing_account()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_no_account_charge_on_existing_account@26332146.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_no_account_charge_on_existing_account --fork Amsterdam

Verify the create opcode is not charged NEW_ACCOUNT when the target account already exists in the trie.

The factory is forwarded exactly the create's execution gas, with no NEW_ACCOUNT included. Because the target is pre-funded (alive), that budget is sufficient and the create succeeds, deploying empty code (created nonce 1). With one gas less it runs out of gas at the create's upfront charge, before the nonce bump, leaving the target untouched (nonce 0). The empty reservoir keeps the state-gas dimension from masking the boundary.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
@pytest.mark.with_all_create_opcodes()
@pytest.mark.parametrize(
    "sufficient_gas",
    [
        pytest.param(True, id="sufficient_gas"),
        pytest.param(False, id="insufficient_gas"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_no_account_charge_on_existing_account(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
    sufficient_gas: bool,
) -> None:
    """
    Verify the create opcode is not charged NEW_ACCOUNT when the target
    account already exists in the trie.

    The factory is forwarded exactly the create's execution gas, with no
    NEW_ACCOUNT included. Because the target is pre-funded (alive), that
    budget is sufficient and the create succeeds, deploying empty code
    (created nonce 1). With one gas less it runs out of gas at the
    create's upfront charge, before the nonce bump, leaving the target
    untouched (nonce 0). The empty reservoir keeps the state-gas
    dimension from masking the boundary.
    """
    factory_code = create_opcode(
        value=0,
        offset=0,
        size=1,  # Nothing in memory, equivalent to Op.STOP
        # Gas accounting
        init_code_size=1,
        new_memory_size=1,
        account_new=False,
    )

    factory = pre.deploy_contract(code=factory_code)

    created = compute_create_address(
        address=factory,
        nonce=1,
        salt=0,
        initcode=Op.STOP,
        opcode=create_opcode,
    )
    pre.fund_address(created, amount=1)

    call_gas = factory_code.gas_cost(fork)
    if not sufficient_gas:
        call_gas -= 1
    entry_code = Op.CALL(gas=call_gas, address=factory)
    entry = pre.deploy_contract(code=entry_code)

    tx = Transaction(
        to=entry,
        state_gas_reservoir=0,  # To allow subcall to run OOG
        sender=pre.fund_eoa(),
    )

    post = {
        created: Account(
            nonce=1 if sufficient_gas else 0, balance=1, code=b""
        ),
    }
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.