Skip to content

test_sstore_restore_nonzero_refunds_write()

Documentation for tests/amsterdam/eip8038_state_access_gas_cost_increase/test_sstore_refunds.py::test_sstore_restore_nonzero_refunds_write@2867859a.

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_restore_nonzero_refunds_write --fork Amsterdam

Restoring a non-zero-original slot refunds the write cost.

The slot is changed (charging STORAGE_WRITE) then restored to its original non-zero value, refunding STORAGE_WRITE. Gas is burned so the quotient cap does not bind and the full refund is observable.

Source code in tests/amsterdam/eip8038_state_access_gas_cost_increase/test_sstore_refunds.py
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
@EIPChecklist.GasRefundsChanges.Test.RefundCalculation()
@EIPChecklist.GasRefundsChanges.Test.RefundCalculation.Under()
def test_sstore_restore_nonzero_refunds_write(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Restoring a non-zero-original slot refunds the write cost.

    The slot is changed (charging ``STORAGE_WRITE``) then restored to its
    original non-zero value, refunding ``STORAGE_WRITE``. Gas is
    burned so the quotient cap does not bind and the full refund is
    observable.
    """
    code = Op.SSTORE.with_metadata(
        key_warm=False,
        original_value=1,
        current_value=1,
        new_value=2,
    )(0, 2) + Op.SSTORE.with_metadata(
        key_warm=True,
        original_value=1,
        current_value=2,
        new_value=1,
    )(0, 1)
    burn = Op.JUMPDEST * 60_000
    code += burn

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

    # Restoring the non-zero original refunds STORAGE_WRITE.
    storage_write = code.refund(fork)
    expected_cumulative = _cumulative_gas_used(code, fork)
    intrinsic = fork.transaction_intrinsic_cost_calculator()(
        return_cost_deducted_prior_execution=True
    )
    gross = intrinsic + code.execution_cost(fork)
    assert gross // 5 > storage_write
    assert expected_cumulative == gross - storage_write

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

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

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.