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@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

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

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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
@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 = gas_costs.G_STORAGE_RESET + gas_costs.G_BASE + gas_costs.G_VERY_LOW
    gas_refund = gas_costs.R_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

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) exceed_gas_refund_limit
...fork_Osaka-state_test-exceed_gas_refund_limit_True True
...fork_Osaka-state_test-exceed_gas_refund_limit_False False
...fork_Osaka-blockchain_test_from_state_test-exceed_gas_refund_limit_True True
...fork_Osaka-blockchain_test_from_state_test-exceed_gas_refund_limit_False False