Skip to content

test_sstore_refund_cap_exact_equality()

Documentation for tests/amsterdam/eip8038_state_access_gas_cost_increase/test_sstore_refunds.py::test_sstore_refund_cap_exact_equality@26332146.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8038_state_access_gas_cost_increase/test_sstore_refunds.py::test_sstore_refund_cap_exact_equality --fork Amsterdam

The applied refund equals the EIP-3529 cap at exact equality.

A single non-zero-original clear accrues REFUND_STORAGE_CLEAR. Cheap JUMPDEST gas (1 each) is burned so the gross gas lands at exactly max_refund_quotient * accrued; the quotient cap gross // max_refund_quotient then equals the accrued refund exactly, the boundary between the cap binding and not binding. The full refund applies and cumulative_gas_used is gross - accrued.

Source code in tests/amsterdam/eip8038_state_access_gas_cost_increase/test_sstore_refunds.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
@EIPChecklist.GasRefundsChanges.Test.RefundCalculation()
@EIPChecklist.GasRefundsChanges.Test.RefundCalculation.Exact()
def test_sstore_refund_cap_exact_equality(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    The applied refund equals the EIP-3529 cap at exact equality.

    A single non-zero-original clear accrues ``REFUND_STORAGE_CLEAR``.
    Cheap ``JUMPDEST`` gas (1 each) is burned so the gross gas lands at
    exactly ``max_refund_quotient * accrued``; the quotient cap
    ``gross // max_refund_quotient`` then equals the accrued refund
    *exactly*, the boundary between the cap binding and not binding. The
    full refund applies and ``cumulative_gas_used`` is ``gross - accrued``.
    """
    quotient = fork.max_refund_quotient()

    clear = Op.SSTORE.with_metadata(
        key_warm=False,
        original_value=1,
        current_value=1,
        new_value=0,
    )(0, 0)
    accrued = clear.refund(fork)

    intrinsic = fork.transaction_intrinsic_cost_calculator()(
        return_cost_deducted_prior_execution=True
    )
    # Target the exact boundary: gross == quotient * accrued, so that
    # gross // quotient == accrued with no slack. Solve for the JUMPDEST
    # count from the remaining gas after intrinsic and the clear's
    # execution cost; each JUMPDEST costs exactly 1 gas.
    jumpdest_gas = Op.JUMPDEST.gas_cost(fork)
    target_gross = quotient * accrued
    base_gross = intrinsic + clear.execution_cost(fork)
    burn_gas = target_gross - base_gross
    num_jumpdest, remainder = divmod(burn_gas, jumpdest_gas)
    # An exact integer JUMPDEST count must reach the boundary; otherwise
    # the equality below would not hold and the test would (correctly)
    # fail rather than silently approximate.
    assert remainder == 0

    code = clear + Op.JUMPDEST * num_jumpdest
    contract = pre.deploy_contract(code=code, storage={0: 1})

    gross = intrinsic + code.execution_cost(fork) + code.state_cost(fork)
    # Exact equality: the cap is neither under nor over the accrued refund.
    assert gross == target_gross
    assert gross // quotient == accrued
    expected_cumulative = gross - accrued

    tx = Transaction(
        to=contract,
        sender=pre.fund_eoa(),
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=expected_cumulative
        ),
    )

    post = {contract: Account(storage={0: 0})}
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.