Skip to content

test_sstore_restoration_charge_in_ancestor_intermediate_revert()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_charge_in_ancestor_intermediate_revert@5c024cbb.

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

Verify a deferred refund applied in an intermediate frame still flows back to the caller when that frame REVERTs.

Caller's SSTORE charges; the matching clear in inner is deferred through the chain and lands on middle's own SSTORE-set during incorporate_child_on_success. Middle REVERTs; the applied amount must reach the caller via incorporate_child_on_error. A probe SSTORE sized to OOG by 1 detects loss.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
@pytest.mark.with_all_call_opcodes(
    selector=lambda call_opcode: call_opcode in (Op.DELEGATECALL, Op.CALLCODE)
)
@pytest.mark.valid_from("EIP8037")
def test_sstore_restoration_charge_in_ancestor_intermediate_revert(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    call_opcode: Op,
) -> None:
    """
    Verify a deferred refund applied in an intermediate frame still
    flows back to the caller when that frame REVERTs.

    Caller's SSTORE charges; the matching clear in inner is deferred
    through the chain and lands on middle's own SSTORE-set during
    `incorporate_child_on_success`.  Middle REVERTs; the applied
    amount must reach the caller via `incorporate_child_on_error`.
    A probe SSTORE sized to OOG by 1 detects loss.
    """
    gas_costs = fork.gas_costs()
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    intrinsic_cost = fork.transaction_intrinsic_cost_calculator()()
    # Probe SSTORE(0, 1): 2 pushes + cold storage write + state gas - 1,
    # so it OOGs by 1 when the reservoir is 0 and succeeds otherwise.
    probe_gas = (
        2 * gas_costs.VERY_LOW
        + gas_costs.COLD_STORAGE_WRITE
        + sstore_state_gas
        - 1
    )

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

    # Middle's own SSTORE on slot 1 supplies the `state_gas_used`
    # that inner's deferred credit lands on, then middle REVERTs.
    middle_code = (
        Op.SSTORE(1, 1)
        + Op.POP(call_opcode(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()
    caller_code = (
        Op.SSTORE(caller_storage.store_next(1, "caller_set_persists"), 1)
        + Op.POP(call_opcode(gas=Op.GAS, address=middle))
        + Op.SSTORE(
            caller_storage.store_next(1, "probe_must_succeed"),
            Op.CALL(gas=probe_gas, address=probe),
        )
    )
    caller = pre.deploy_contract(code=caller_code)

    # Block state gas commits: caller's slot-0 set + probe's
    # SSTORE-set + caller's outer SSTORE-set on slot 1.  Middle's
    # own slot-1 set is washed by inner's deferred credit before
    # middle reverts, so it does not propagate.  Header gas_used
    # is max(regular, state).
    expected_regular = (
        intrinsic_cost
        + caller_code.regular_cost(fork)
        + middle_code.regular_cost(fork)
        + inner_code.regular_cost(fork)
        + probe_code.regular_cost(fork)
    )
    expected_state = 3 * sstore_state_gas
    expected_gas_used = max(expected_regular, expected_state)

    # Reservoir = 2 * sstore_state_gas covers caller's and middle's
    # sets; the deferred credit refills middle by sstore_state_gas,
    # which flows to the caller on revert.
    tx = Transaction(
        sender=pre.fund_eoa(),
        to=caller,
        state_gas_reservoir=2 * sstore_state_gas,
    )

    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 2 parametrized test cases across 1 fork.