Skip to content

test_sstore_restoration_ancestor_revert()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_ancestor_revert@2119b382.

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

Verify an ancestor REVERT does not inflate the caller's reservoir under source-based (LIFO) refills.

Inner's set spills its state gas from gas_left. The refill at x to 0 returns it to gas_left, and inner's state_gas_spilled propagates to middle on success. Middle then REVERTs, refilling the spilled state gas 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
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
@pytest.mark.with_all_call_opcodes(
    selector=lambda call_opcode: call_opcode != Op.STATICCALL
)
@pytest.mark.valid_from("EIP8037")
def test_sstore_restoration_ancestor_revert(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    call_opcode: Op,
) -> None:
    """
    Verify an ancestor REVERT does not inflate the caller's reservoir
    under source-based (LIFO) refills.

    Inner's set spills its state gas from `gas_left`. The refill at
    x to 0 returns it to `gas_left`, and inner's
    `state_gas_spilled` propagates to middle on success. Middle
    then REVERTs, refilling the spilled state gas 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.
    """
    gas_costs = fork.gas_costs()
    intrinsic_cost = fork.transaction_intrinsic_cost_calculator()()
    # 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
    )

    set_op = Op.SSTORE.with_metadata(
        key_warm=False,
        original_value=0,
        current_value=0,
        new_value=1,
    )(0, 1)
    clear_op = Op.SSTORE.with_metadata(
        key_warm=True,
        original_value=0,
        current_value=1,
        new_value=0,
    )(0, 0)
    inner_code = set_op + clear_op + Op.STOP
    inner = pre.deploy_contract(code=inner_code)

    middle_code = Op.POP(Op.CALL(gas=Op.GAS, address=inner)) + Op.REVERT(0, 0)
    middle = pre.deploy_contract(code=middle_code)

    probe_code = Op.SSTORE(0, 1)
    probe = pre.deploy_contract(code=probe_code)

    caller_storage = Storage()
    # The probe OOGs and returns 0, so the caller's outer SSTORE is a
    # cold no-op (0 to 0) on a fresh slot, charging only
    # COLD_STORAGE_ACCESS rather than the cold set `regular_cost`
    # assumes by default.
    caller_code = Op.POP(
        call_opcode(gas=Op.GAS, address=middle)
    ) + Op.SSTORE.with_metadata(
        key_warm=False,
        original_value=0,
        current_value=0,
        new_value=0,
    )(
        caller_storage.store_next(0, "probe_must_fail"),
        Op.CALL(gas=probe_gas, address=probe),
    )
    caller = pre.deploy_contract(code=caller_code)

    # No SSTORE-set persists (inner's set+clear cancel, middle reverts,
    # the probe OOGs and reverts, and the caller's outer SSTORE is a
    # no-op), so block state gas is zero and header gas_used (the max of
    # regular and state) is just the regular total. The probe burns its
    # full forwarded budget on the OOG; its CALL's cold-access surcharge
    # is already counted in the caller's regular cost.
    expected_gas_used = (
        intrinsic_cost
        + caller_code.regular_cost(fork)
        + middle_code.regular_cost(fork)
        + inner_code.regular_cost(fork)
        + probe_gas
    )

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

    state_test(
        pre=pre,
        tx=tx,
        post={caller: Account(storage=caller_storage)},
        blockchain_test_header_verify=Header(gas_used=expected_gas_used),
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.