Skip to content

test_same_frame_refund_increases_gas_left()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py::test_same_frame_refund_increases_gas_left@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_same_frame_refund_increases_gas_left --fork Amsterdam

Test a same-frame refill can increase gas_left between reads.

This is the local control for the cross-frame behavior: a fresh set spills from gas_left, the first measured window clears it in the same frame, and the second repeats the clear as a no-op. The two windows have identical bytecode and execution cost, so their exact difference is the repaid spill. With half a slot funded by the reservoir, only half of the full-slot refund repays the spill.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_cross_frame_refund.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
@pytest.mark.parametrize(
    "fund_half_slot",
    [False, True],
    ids=["empty-reservoir", "half-slot-reservoir"],
)
@pytest.mark.valid_from("EIP8037")
def test_same_frame_refund_increases_gas_left(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    fund_half_slot: bool,
) -> None:
    """
    Test a same-frame refill can increase `gas_left` between reads.

    This is the local control for the cross-frame behavior: a fresh set
    spills from `gas_left`, the first measured window clears it in the
    same frame, and the second repeats the clear as a no-op. The two
    windows have identical bytecode and execution cost, so their exact
    difference is the repaid spill. With half a slot funded by the
    reservoir, only half of the full-slot refund repays the spill.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    reservoir = sstore_state_gas // 2 if fund_half_slot else 0
    spilled = sstore_state_gas - reservoir

    window = Op.SSTORE(SLOT_X, 0)
    contract = pre.deploy_contract(
        code=clearing_probe_code(FRESH_SET(SLOT_X, 1), [window] * 3)
    )

    tx = Transaction(
        to=contract,
        state_gas_reservoir=reservoir,
        sender=pre.fund_eoa(),
    )

    post = {
        contract: Account(
            storage={
                SLOT_X: 0,
                SLOT_MARKER: 1,
                SLOT_INCREASED: 1,
                SLOT_RESULT: -spilled,
            }
        )
    }
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.