Skip to content

test_sstore_restoration_create_init_revert()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_create_init_revert@ffe48572.

Generate fixtures for these test cases for Bogota with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_create_init_revert --fork Bogota

Verify a reverting CREATE sub-frame does not inflate the caller's reservoir under source-based (LIFO) refills.

The init code spills its state gas from gas_left, does 0 to x to 0 and REVERTs. The CREATE is wrapped in an outer frame that also REVERTs. Each refill returns the spilled state gas to gas_left, and the reverts refill it to the caller's gas_left, not the reservoir. The reservoir stays at 0, so a probe sized to OOG by 1 fails, since its fixed forwarded gas cannot reach the gas_left refund.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py
@pytest.mark.with_all_create_opcodes
@pytest.mark.valid_from("EIP8037")
def test_sstore_restoration_create_init_revert(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
) -> None:
    """
    Verify a reverting CREATE sub-frame does not inflate the caller's
    reservoir under source-based (LIFO) refills.

    The init code spills its state gas from `gas_left`, does 0 to x to 0
    and REVERTs. The CREATE is wrapped in an outer frame that also
    REVERTs. Each refill returns the spilled state gas to `gas_left`,
    and the reverts refill it to the caller's `gas_left`, not the
    reservoir. The reservoir stays at 0, so a probe sized to OOG by 1
    fails, since its fixed forwarded gas cannot reach the `gas_left`
    refund.
    """
    # Probe SSTORE(0, 1): 2 pushes + cold write + state gas - 1. OOGs by
    # 1 when the reservoir is 0, as forwarded gas misses gas_left.
    probe_gas = Op.SSTORE(0, 1).gas_cost(fork) - 1

    init_code = Op.SSTORE(0, 1) + Op.SSTORE(0, 0) + Op.REVERT(0, 0)
    probe = pre.deploy_contract(code=Op.SSTORE(0, 1))

    mstore_value, init_code_size = init_code_at_high_bytes(init_code)
    create_call = create_op(create_opcode, size=init_code_size)

    # Inner contract performs the CREATE then REVERTs.
    inner = pre.deploy_contract(
        code=Op.MSTORE(0, mstore_value)
        + Op.POP(create_call)
        + Op.REVERT(0, 0),
    )

    caller_storage = Storage()
    caller = pre.deploy_contract(
        code=(
            Op.POP(Op.CALL(gas=Op.GAS, address=inner))
            + Op.SSTORE(
                caller_storage.store_next(0, "probe_must_fail"),
                Op.CALL(gas=probe_gas, address=probe),
            )
        ),
    )

    # gas_limit at the cap means the caller's reservoir starts at 0.
    tx = Transaction(
        to=caller,
        state_gas_reservoir=0,
        sender=pre.fund_eoa(),
    )

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

Parametrized Test Cases

This test generates 2 parametrized test cases across 2 forks.