Skip to content

test_sstore_stipend_check_excludes_reservoir()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_stipend_check_excludes_reservoir@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_stipend_check_excludes_reservoir --fork Bogota

Verify the SSTORE gas check uses gas_left only, not the reservoir.

A child frame has gas_left at or just below the stipend threshold (GAS_CALL_STIPEND + 1) while the reservoir holds ample state gas. The check must fail when gas_left is too low, regardless of the reservoir balance.

Post-8038 the cold access cost (COLD_STORAGE_ACCESS = 3000) exceeds the stipend (2300), so for this cold slot the access cost is the binding gate and the stipend sentry is subsumed. The reservoir is excluded either way, which is what this test pins down.

With below_stipend: SSTORE fails (gas_left too low, reservoir ignored). With at_stipend: SSTORE has full execution gas and proceeds.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py
@pytest.mark.parametrize(
    "gas_above_stipend",
    [
        pytest.param(-1, id="below_stipend"),
        pytest.param(0, id="at_stipend"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_sstore_stipend_check_excludes_reservoir(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    gas_above_stipend: int,
) -> None:
    """
    Verify the SSTORE gas check uses gas_left only, not the reservoir.

    A child frame has gas_left at or just below the stipend threshold
    (GAS_CALL_STIPEND + 1) while the reservoir holds ample state gas.
    The check must fail when gas_left is too low, regardless of the
    reservoir balance.

    Post-8038 the cold access cost (COLD_STORAGE_ACCESS = 3000) exceeds
    the stipend (2300), so for this cold slot the access cost is the
    binding gate and the stipend sentry is subsumed. The reservoir is
    excluded either way, which is what this test pins down.

    With below_stipend: SSTORE fails (gas_left too low, reservoir ignored).
    With at_stipend: SSTORE has full execution gas and proceeds.
    """
    stipend = fork.call_value_stipend() + 1
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    # Child: Op.SSTORE(0, 1) = 2 pushes + SSTORE opcode.
    child_code = Op.SSTORE(0, 1)
    child = pre.deploy_contract(child_code)

    # Full execution gas for the child (pushes + SSTORE execution cost).
    # State gas comes from the reservoir so it doesn't affect gas_left.
    child_full_execution = child_code.execution_cost(fork)

    # below_stipend: give 1 less than stipend after pushes, fails check.
    # at_stipend: give full execution gas, passes check and completes.
    if gas_above_stipend < 0:
        push_gas = 2 * Op.PUSH1(0).execution_cost(fork)
        child_gas = push_gas + stipend - 1
    else:
        child_gas = child_full_execution

    # Caller forwards limited execution gas via CALL. State gas comes
    # from the reservoir (gas_limit above the cap).
    caller_storage = Storage()
    sstore_succeeds = gas_above_stipend >= 0
    caller = pre.deploy_contract(
        Op.SSTORE(
            caller_storage.store_next(
                1 if sstore_succeeds else 0,
                "sstore_succeeds"
                if sstore_succeeds
                else "sstore_fails_stipend",
            ),
            Op.CALL(gas=child_gas, address=child),
        )
    )

    tx = Transaction(
        sender=pre.fund_eoa(),
        to=caller,
        state_gas_reservoir=sstore_state_gas,
    )

    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.