Skip to content

test_sstore_restoration_refund_credits_local_reservoir()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py::test_sstore_restoration_refund_credits_local_reservoir@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_refund_credits_local_reservoir --fork Amsterdam

Verify a same transaction SSTORE restoration refund credits the clearing frame's own reservoir immediately so later state gas in that frame is funded. Parametrized to pin the refund as necessary and sufficient.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_sstore.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
221
222
223
224
225
226
227
228
229
230
231
232
@pytest.mark.parametrize(
    "refund_sufficient",
    [
        pytest.param(True, id="refund_funds_create"),
        pytest.param(False, id="no_refund_create_oogs"),
    ],
)
@pytest.mark.parametrize(
    "delegatecall_depth",
    [
        pytest.param(1, id="depth_1"),
        pytest.param(3, id="depth_3"),
        pytest.param(10, id="depth_10"),
    ],
)
@pytest.mark.valid_from("EIP8037")
def test_sstore_restoration_refund_credits_local_reservoir(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    delegatecall_depth: int,
    refund_sufficient: bool,
) -> None:
    """
    Verify a same transaction SSTORE restoration refund credits the
    clearing frame's own reservoir immediately so later state gas in
    that frame is funded. Parametrized to pin the refund as necessary
    and sufficient.
    """
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    create_state_gas = fork.create_state_gas()
    # Premise: the two restoration refunds must be able to cover the
    # CREATE's new-account state gas for the funded path to exist.
    # Drift-proof relationship (vs. hardcoding the constants).
    assert 2 * sstore_state_gas >= create_state_gas

    # Sentinel written only if the CREATE returned (frame did not OOG).
    sentinel_slot = 2
    # refund: clear (1→0, restoration refund). no refund: modify
    # (1→2, no state growth, no refund) — same regular shape.
    cleared_value = 0 if refund_sufficient else 2
    clearing = pre.deploy_contract(
        code=(
            Op.SSTORE(0, cleared_value)
            + Op.SSTORE(1, cleared_value)
            + Op.POP(Op.CREATE(0, 0, 0))
            + Op.SSTORE(sentinel_slot, 1)
            + Op.STOP
        )
    )
    inner: Address = clearing
    for _ in range(delegatecall_depth):
        inner = pre.deploy_contract(
            code=(Op.POP(Op.DELEGATECALL(gas=Op.GAS, address=inner)) + Op.STOP)
        )
    parent = pre.deploy_contract(
        code=(
            Op.SSTORE(0, 1)
            + Op.SSTORE(1, 1)
            + Op.POP(Op.DELEGATECALL(gas=Op.GAS, address=inner))
            + Op.STOP
        )
    )

    # The two parent `0→1` sets spill their state gas into `gas_left`
    # (tx is far below the per-tx cap, so no state-gas reservoir).
    # Budget regular headroom for the call chain plus that spill, then
    # sit mid-window: short of also spill-funding `create_state_gas`,
    # so only a refund-credited reservoir can cover the CREATE.
    regular_headroom = 200_000
    gas_limit = regular_headroom + 2 * sstore_state_gas + create_state_gas // 2

    if refund_sufficient:
        post = {parent: Account(storage={0: 0, 1: 0, sentinel_slot: 1})}
    else:
        # CREATE OOGs in the clearing frame; its writes (the 1→2
        # modifications and the sentinel) revert, leaving the parent's
        # original sets intact.
        post = {parent: Account(storage={0: 1, 1: 1})}

    tx = Transaction(
        to=parent,
        gas_limit=gas_limit,
        sender=pre.fund_eoa(),
    )

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 6 parametrized test cases across 1 fork.