Skip to content

test_create_collision_refunds_state_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_collision_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_collision_refunds_state_gas --fork Amsterdam

Verify CREATE/CREATE2 address collision refunds account state gas.

The collision path increments the factory nonce and burns the forwarded execution gas (consumed by the never-spawned child), but still refunds GAS_NEW_ACCOUNT to the reservoir. Tight gas tuning limits the factory's post-collision gas_left so the probe SSTORE can only succeed via the refunded reservoir, not by spilling state gas from gas_left.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
@pytest.mark.pre_alloc_mutable()
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_create_collision_refunds_state_gas(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
) -> None:
    """
    Verify CREATE/CREATE2 address collision refunds account state gas.

    The collision path increments the factory nonce and burns the
    forwarded execution gas (consumed by the never-spawned child), but
    still refunds `GAS_NEW_ACCOUNT` to the reservoir. Tight gas
    tuning limits the factory's post-collision `gas_left` so the
    probe SSTORE can only succeed via the refunded reservoir, not
    by spilling state gas from `gas_left`.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    init_code = Op.STOP
    mstore_value, size = init_code_at_high_bytes(init_code)
    salt = 0

    storage = Storage()
    create_call = (
        create_opcode(value=0, offset=0, size=size, salt=salt)
        if create_opcode == Op.CREATE2
        else create_opcode(value=0, offset=0, size=size)
    )
    factory_code = (
        Op.MSTORE(0, mstore_value)
        + Op.POP(create_call)
        + Op.SSTORE(storage.store_next(1, "reservoir_ok"), 1)
    )
    factory = pre.deploy_contract(code=factory_code)

    collision_target = compute_create_address(
        address=factory,
        nonce=1,
        salt=salt,
        initcode=bytes(init_code),
        opcode=create_opcode,
    )
    pre.deploy_contract(code=Op.STOP, address=collision_target)

    # Tight gas tuning: factory retains
    # ~(forwarded - pre_sstore_execution) / 64 after collision burns
    # `max_message_call_gas` as execution. 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 2 parametrized test cases across 1 fork.