Skip to content

test_sstore_restoration_sub_frame_revert()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_sub_frame_revert@87aba1a3.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_sub_frame_revert --fork Amsterdam

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

The sub-call does 0 to x to 0 then REVERTs. The set spilled its state gas from gas_left, so the refill at x to 0 returns it to gas_left, not the reservoir. On REVERT the state gas refills to the parent's gas_left, so the reservoir stays at 0. A probe sized to OOG by 1 then 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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
@pytest.mark.with_all_call_opcodes(
    selector=lambda call_opcode: call_opcode != Op.STATICCALL
)
@pytest.mark.valid_from("EIP8037")
def test_sstore_restoration_sub_frame_revert(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    call_opcode: Op,
) -> None:
    """
    Verify a sub-frame REVERT does not inflate the caller's reservoir
    under source-based (LIFO) refills.

    The sub-call does 0 to x to 0 then REVERTs. The set spilled its
    state gas from `gas_left`, so the refill at x to 0 returns it to
    `gas_left`, not the reservoir. On REVERT the state gas refills to
    the parent's `gas_left`, so the reservoir stays at 0. A probe sized
    to OOG by 1 then fails, since its fixed forwarded gas cannot reach
    the `gas_left` refund.
    """
    gas_costs = fork.gas_costs()
    # 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 = (
        2 * gas_costs.VERY_LOW
        + gas_costs.COLD_STORAGE_WRITE
        + Op.SSTORE(new_value=1).state_cost(fork)
        - 1
    )

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

    # Forward all gas so the child does both SSTOREs and REVERT.
    caller_storage = Storage()
    caller_code = Op.POP(call_opcode(gas=Op.GAS, address=child)) + Op.SSTORE(
        caller_storage.store_next(0, "probe_must_fail"),
        Op.CALL(gas=probe_gas, address=probe),
    )
    caller = pre.deploy_contract(code=caller_code)

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

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

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.