Skip to content

test_coinbase_fee_with_state_gas_refund()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_multi_block.py::test_coinbase_fee_with_state_gas_refund@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_multi_block.py::test_coinbase_fee_with_state_gas_refund --fork Amsterdam

Assert the coinbase is paid on the post-refund gas, not the charge.

A 0 to x to 0 cycle refunds its state charge to the reservoir, so the sender is billed execution gas alone and the miner is paid on that same figure. Paying the miner on the pre-refund total would hand it the whole storage set for free.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_multi_block.py
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
@pytest.mark.valid_from("EIP8037")
def test_coinbase_fee_with_state_gas_refund(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Assert the coinbase is paid on the post-refund gas, not the charge.

    A 0 to x to 0 cycle refunds its state charge to the reservoir, so
    the sender is billed execution gas alone and the miner is paid on
    that same figure. Paying the miner on the pre-refund total would
    hand it the whole storage set for free.
    """
    cycle_code = Op.SSTORE(0, 1) + Op.SSTORE(
        0,
        0,
        # gas accounting
        key_warm=True,
        original_value=0,
        current_value=1,
        new_value=0,
    )
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)
    cycle = pre.deploy_contract(code=cycle_code)

    # The cycle nets to no state growth, so only execution gas is billed,
    # less the write-cost refund the restoration earns (capped at the
    # usual fraction of the pre-refund total).
    pre_refund_gas = (
        fork.transaction_intrinsic_cost_calculator()()
        + cycle_code.execution_cost(fork)
    )
    execution_refund = cycle_code.refund(fork) - cycle_code.state_refund(fork)
    expected_coinbase = pre_refund_gas - min(
        pre_refund_gas // fork.max_refund_quotient(), execution_refund
    )

    reporter_storage = Storage()
    reporter = pre.deploy_contract(
        code=(
            Op.SSTORE(
                reporter_storage.store_next(expected_coinbase, "coinbase"),
                Op.BALANCE(Op.COINBASE),
            )
        ),
    )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[
                    Transaction(
                        to=cycle,
                        state_gas_reservoir=sstore_state_gas,
                        max_priority_fee_per_gas=1,
                        max_fee_per_gas=8,
                        sender=pre.fund_eoa(),
                    ),
                    Transaction(
                        to=reporter,
                        state_gas_reservoir=0,
                        max_priority_fee_per_gas=1,
                        max_fee_per_gas=8,
                        sender=pre.fund_eoa(),
                    ),
                ],
            )
        ],
        post={
            cycle: Account(storage={0: 0}),
            reporter: Account(storage=reporter_storage),
        },
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.