Skip to content

test_reservoir_grant_window_costs()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py::test_reservoir_grant_window_costs@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py::test_reservoir_grant_window_costs --fork Amsterdam

Test a reservoir grant determines repayment and a later spill.

The same shape as test_cross_frame_refund_with_reservoir_grant, with the call and a later set each bracketed by GAS reads. The two clearing credits repay whichever parent sets spilled, and any remainder funds the later set. The call window shrinks by the repayment, and the later set spills only when both parent sets did. What the sender pays is checked there.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
@pytest.mark.parametrize("reservoir_slots", [0, 1, 2])
@pytest.mark.valid_from("EIP8037")
def test_reservoir_grant_window_costs(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    reservoir_slots: int,
) -> None:
    """
    Test a reservoir grant determines repayment and a later spill.

    The same shape as `test_cross_frame_refund_with_reservoir_grant`,
    with the call and a later set each bracketed by `GAS` reads. The
    two clearing credits repay whichever parent sets spilled, and any
    remainder funds the later set. The call window shrinks by the
    repayment, and the later set spills only when both parent sets did.
    What the sender pays is checked there.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    child_code = WARM_CLEAR(SLOT_X, 0) + WARM_CLEAR(SLOT_Y, 0)
    child = pre.deploy_contract(code=child_code)
    child_budget = budget_above_sstore_stipend(fork, child_code)

    call = Op.POP(
        Op.DELEGATECALL(gas=child_budget, address=child, address_warm=False)
    )
    probe = FRESH_SET(SLOT_PROBE, 1)
    code = (
        Op.MSTORE(64, 0, new_memory_size=96, old_memory_size=0)
        + FRESH_SET(SLOT_X, 1)
        + FRESH_SET(SLOT_Y, 1)
        + Op.MSTORE(0, Op.GAS)
        + call
        + Op.MSTORE(32, Op.GAS)
        + probe
        + Op.MSTORE(64, Op.GAS)
        + FRESH_SET(SLOT_RESULT, Op.SUB(Op.MLOAD(0), Op.MLOAD(32)))
        + FRESH_SET(SLOT_PROBE_RESULT, Op.SUB(Op.MLOAD(32), Op.MLOAD(64)))
    )
    contract = pre.deploy_contract(code=code)

    # A window runs from one GAS read to the next: the store of the
    # first read, the window's code and the second read.
    stamp_cost = Op.MSTORE(0, Op.GAS).gas_cost(fork)
    call_cost = (
        stamp_cost
        + call.execution_cost(fork)
        + child_code.execution_cost(fork)
    )
    probe_cost = stamp_cost + probe.execution_cost(fork)
    # The two credits repay the spilled sets and whatever is left of
    # them funds the probe, which spills only when both were repaid.
    repaid = (2 - reservoir_slots) * sstore_state_gas
    probe_spill = sstore_state_gas if reservoir_slots == 0 else 0

    tx = Transaction(
        to=contract,
        state_gas_reservoir=reservoir_slots * sstore_state_gas,
        sender=pre.fund_eoa(),
    )

    post = {
        contract: Account(
            storage={
                SLOT_X: 0,
                SLOT_Y: 0,
                SLOT_PROBE: 1,
                SLOT_RESULT: call_cost - repaid,
                SLOT_PROBE_RESULT: probe_cost + probe_spill,
            }
        )
    }
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 3 parametrized test cases across 1 fork.