Skip to content

test_sstore_clear_grants_refund()

Documentation for tests/amsterdam/eip8038_state_access_gas_cost_increase/test_sstore_refunds.py::test_sstore_clear_grants_refund@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_clear_grants_refund --fork Amsterdam

Clearing a non-zero-original slot grants REFUND_STORAGE_CLEAR.

Enough unrelated gas is burned so the EIP-3529 quotient cap (gas_used // 5) does not bind, letting the full clear refund be observed in cumulative_gas_used. The non-zero original means no EIP-8037 state refund participates.

Source code in tests/amsterdam/eip8038_state_access_gas_cost_increase/test_sstore_refunds.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
@EIPChecklist.GasRefundsChanges.Test.RefundCalculation()
@EIPChecklist.GasRefundsChanges.Test.RefundCalculation.Under()
def test_sstore_clear_grants_refund(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Clearing a non-zero-original slot grants ``REFUND_STORAGE_CLEAR``.

    Enough unrelated gas is burned so the EIP-3529 quotient cap
    (``gas_used // 5``) does not bind, letting the full clear refund be
    observed in ``cumulative_gas_used``. The non-zero original means no
    EIP-8037 state refund participates.
    """
    clear = Op.SSTORE.with_metadata(
        key_warm=False,
        original_value=1,
        current_value=1,
        new_value=0,
    )(0, 0)
    # Burn cheap gas (JUMPDEST = 1 gas, no stack effect) so that
    # gas_used // 5 exceeds the refund and the full grant applies.
    burn = Op.JUMPDEST * 60_000
    code = clear + burn

    contract = pre.deploy_contract(code=code, storage={0: 1})

    # The slot's clear grants exactly one REFUND_STORAGE_CLEAR.
    refund_clear = code.refund(fork)
    expected_cumulative = _cumulative_gas_used(code, fork)
    # The cap must not bind here, so the full grant is visible.
    intrinsic = fork.transaction_intrinsic_cost_calculator()(
        return_cost_deducted_prior_execution=True
    )
    gross = intrinsic + code.execution_cost(fork)
    assert gross // 5 > refund_clear
    assert expected_cumulative == gross - refund_clear

    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.