Skip to content

test_maximum_gas_refund()

Documentation for tests/osaka/eip7825_transaction_gas_limit_cap/test_tx_gas_limit.py::test_maximum_gas_refund@892e6d1e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/osaka/eip7825_transaction_gas_limit_cap/test_tx_gas_limit.py::test_maximum_gas_refund --fork Amsterdam

Test the maximum gas refund behavior according to EIP-3529.

Source code in tests/osaka/eip7825_transaction_gas_limit_cap/test_tx_gas_limit.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
@pytest.mark.parametrize(
    "exceed_gas_refund_limit",
    [
        pytest.param(True),
        pytest.param(False),
    ],
)
@pytest.mark.valid_from("Osaka")
def test_maximum_gas_refund(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    exceed_gas_refund_limit: bool,
) -> None:
    """Test the maximum gas refund behavior according to EIP-3529."""
    gas_costs = fork.gas_costs()
    tx_gas_limit_cap = fork.transaction_gas_limit_cap()
    assert tx_gas_limit_cap is not None, (
        "Fork does not have a transaction gas limit cap"
    )
    max_refund_quotient = fork.max_refund_quotient()

    storage = Storage()

    # Base Operation: SSTORE(slot, 0)
    iteration_cost = (
        Op.SSTORE(key_warm=True, original_value=1, new_value=0)
        + Op.PUSH0
        + Op.PUSH1(0)
    ).gas_cost(fork)
    gas_refund = gas_costs.REFUND_STORAGE_CLEAR

    # EIP-3529: Reduction in refunds
    storage_count = tx_gas_limit_cap // iteration_cost
    gas_used = storage_count * iteration_cost

    maximum_gas_refund = gas_used // max_refund_quotient
    gas_refund_count = maximum_gas_refund // gas_refund

    # Base case: operations that fit within the refund limit
    iteration_count = min(
        storage_count, gas_refund_count + int(exceed_gas_refund_limit)
    )

    assert iteration_cost * iteration_count <= tx_gas_limit_cap, (
        "Iteration cost exceeds tx gas limit cap"
    )

    opcode = sum(
        (
            Op.SSTORE(storage.store_next(0), Op.PUSH0)
            for _ in range(iteration_count)
        ),
        Bytecode(),
    )
    assert len(opcode) <= fork.max_code_size(), (
        "code size exceeds max code size"
    )

    contract = pre.deploy_contract(
        code=opcode,
        storage={Hash(i): Hash(1) for i in range(iteration_count)},
    )

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

    post = {contract: Account(storage=storage)}

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

Parametrized Test Cases

This test generates 2 parametrized test cases across 2 forks.