Skip to content

test_create_child_halt_refunds_state_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_child_halt_refunds_state_gas@622620d8.

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_create_child_halt_refunds_state_gas --fork Amsterdam

Verify CREATE/CREATE2 child halt refunds parent's account gas.

Exceptional halts (invalid opcode, EIP-3541 invalid prefix) consume all forwarded execution gas, so block accounting cannot strictly discriminate via header gas. Tight gas tuning via a caller wrapper leaves the factory with just enough gas_left to pay the probe SSTORE's execution portion but not enough to spill the state portion, so the probe SSTORE can only succeed via the refunded reservoir.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
@pytest.mark.parametrize(
    "failure_mode",
    [
        pytest.param("initcode_halt", id="initcode_halt"),
        pytest.param("invalid_prefix", id="invalid_prefix"),
    ],
)
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_create_child_halt_refunds_state_gas(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
    failure_mode: str,
) -> None:
    """
    Verify CREATE/CREATE2 child halt refunds parent's account gas.

    Exceptional halts (invalid opcode, EIP-3541 invalid prefix)
    consume all forwarded execution gas, so block accounting cannot
    strictly discriminate via header gas. Tight
    gas tuning via a caller wrapper leaves the factory with just
    enough `gas_left` to pay the probe SSTORE's execution portion
    but not enough to spill the state portion, so the probe SSTORE
    can only succeed via the refunded reservoir.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    init_code: Op | Bytecode
    if failure_mode == "initcode_halt":
        init_code = Op.INVALID
    elif failure_mode == "invalid_prefix":
        # Return code starting with 0xEF (EIP-3541 invalid prefix).
        init_code = Op.MSTORE8(0, 0xEF) + Op.RETURN(0, 1)

    mstore_value, size = init_code_at_high_bytes(init_code)

    create_call = (
        create_opcode(value=0, offset=0, size=size, salt=0)
        if create_opcode == Op.CREATE2
        else create_opcode(value=0, offset=0, size=size)
    )

    storage = Storage()
    factory = pre.deploy_contract(
        code=(
            Op.MSTORE(0, mstore_value)
            + Op.POP(create_call)
            + Op.SSTORE(storage.store_next(1, "reservoir_ok"), 1)
        ),
    )

    # Tight gas tuning: child halt consumes all forwarded execution
    # gas. Factory retains
    # ~(forwarded - pre_sstore_execution) / 64 after CREATE. Target
    # the discrimination window `(probe_execution,
    # probe_execution + sstore_state_gas)` so the probe SSTORE
    # execution fits but state gas spillover from `gas_left` under
    # the old behavior OOGs.
    pre_sstore_code = Op.MSTORE(0, mstore_value) + Op.POP(create_call)
    pre_sstore_execution = pre_sstore_code.execution_cost(fork)
    probe_code = Op.SSTORE(0, 1)
    probe_execution = probe_code.execution_cost(fork)
    target_gas_left = probe_execution + sstore_state_gas // 2
    forwarded_gas = target_gas_left * 64 + pre_sstore_execution
    # Reservoir sized for CREATE charge only — SSTORE must pull
    # from the refunded reservoir, not from spill.
    caller = pre.deploy_contract(
        code=Op.CALL(gas=forwarded_gas, address=factory)
    )
    tx = Transaction(
        to=caller,
        state_gas_reservoir=create_call.state_cost(fork),
        sender=pre.fund_eoa(),
    )

    state_test(pre=pre, post={factory: Account(storage=storage)}, tx=tx)

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.