Skip to content

test_mixed_gas_regimes()

Documentation for tests/amsterdam/eip7778_block_gas_accounting_without_refunds/test_gas_accounting.py::test_mixed_gas_regimes@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7778_block_gas_accounting_without_refunds/test_gas_accounting.py::test_mixed_gas_regimes --fork Amsterdam

Lock in block-level gas accounting across a block where each tx hits a different regime.

tx1: SSTORE-set fresh slot (no refund, pre_refund > floor). tx2: SSTORE-clear x10 (normal refund, refund not clipped to floor). tx3: 1000 zero-byte calldata to STOP (floor binds fee and block gas).

The floor binds the tx-level fee (tx_gas_used = max(post_refund, floor)) and the block's execution dimension (max(pre_refund gas minus state gas, floor)) alike. Per-tx sender balance is also asserted to lock in that the floor-binding tx pays floor * gas_price, not pre_refund * gas_price.

Source code in tests/amsterdam/eip7778_block_gas_accounting_without_refunds/test_gas_accounting.py
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
@pytest.mark.execute(pytest.mark.skip(reason="Requires specific gas price"))
def test_mixed_gas_regimes(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Lock in block-level gas accounting across a block where each tx hits a
    different regime.

    tx1: SSTORE-set fresh slot (no refund, pre_refund > floor).
    tx2: SSTORE-clear x10 (normal refund, refund not clipped to floor).
    tx3: 1000 zero-byte calldata to STOP (floor binds fee and block gas).

    The floor binds the tx-level fee (tx_gas_used = max(post_refund,
    floor)) and the block's execution dimension (max(pre_refund gas minus
    state gas, floor)) alike. Per-tx sender balance is also asserted to
    lock in that the floor-binding tx pays `floor * gas_price`, not
    `pre_refund * gas_price`.
    """
    intrinsic_cost_calc = fork.transaction_intrinsic_cost_calculator()
    data_floor_calc = fork.transaction_data_floor_cost_calculator()
    initial_fund = 10**18

    post = Alloc()

    # tx1: SSTORE-set to a fresh slot. No refund.
    tx1_code = Op.SSTORE(0, 1, original_value=0, new_value=1)
    tx1_target = pre.deploy_contract(code=tx1_code)
    tx1_sender = pre.fund_eoa(initial_fund)
    tx1_data = b""
    # Full intrinsic + execution gas (execution + state) sizes the gas limit
    # and the balance charged to the sender.
    tx1_pre_refund = intrinsic_cost_calc(
        calldata=tx1_data,
        return_cost_deducted_prior_execution=True,
    ) + tx1_code.gas_cost(fork)
    tx1_floor = data_floor_calc(data=tx1_data)
    assert tx1_pre_refund > tx1_floor, "tx1: pre_refund must exceed floor"
    tx1_contribution = max(tx1_pre_refund, tx1_floor)
    # EIP-8037: block gas_used counts only execution gas; the SSTORE-set
    # state gas lives in the separate state dimension, so the block-level
    # contribution excludes it.
    tx1_block_contribution = max(
        tx1_pre_refund - tx1_code.state_cost(fork), tx1_floor
    )
    tx1 = Transaction(
        to=tx1_target,
        gas_limit=tx1_contribution,
        sender=tx1_sender,
        data=tx1_data,
        expected_receipt={"gas_used": tx1_contribution},
    )
    tx1_gas_price = tx1.gas_price if tx1.gas_price else tx1.max_fee_per_gas
    assert tx1_gas_price is not None
    post[tx1_target] = Account(storage={0: 1})
    post[tx1_sender] = Account(
        balance=initial_fund - tx1_contribution * tx1_gas_price
    )

    # tx2: SSTORE-clear with normal refund, refund not clipped to floor.
    tx2 = RefundTransaction.build(
        fork=fork,
        sender=pre.fund_eoa(INITIAL_FUND),
        refund_types={RefundTypes.STORAGE_CLEAR},
        refunds_count=10,
    )
    tx2.set_pre(pre)
    for addr, account in tx2.post(pre).items():
        post[addr] = account
    assert tx2.gas_used_pre_refund > tx2.call_data_floor_cost, (
        "tx2: pre_refund must exceed floor"
    )
    assert tx2.receipt_gas_used > tx2.call_data_floor_cost, (
        "tx2: refund must not be clipped to floor"
    )
    tx2_contribution = tx2.block_execution()

    # tx3: floor-binding via 1000 zero bytes of calldata to STOP.
    tx3_target = pre.deterministic_deploy_contract(deploy_code=Op.STOP)
    tx3_sender = pre.fund_eoa(initial_fund)
    tx3_data = b"\x00" * 1000
    tx3_pre_refund = intrinsic_cost_calc(
        calldata=tx3_data,
        return_cost_deducted_prior_execution=True,
    )
    tx3_floor = data_floor_calc(data=tx3_data)
    assert tx3_floor > tx3_pre_refund, "tx3: floor must bind upward"
    tx3_fee_gas = max(tx3_pre_refund, tx3_floor)
    # The floor binds the block's execution dimension as well as the fee.
    tx3_block_contribution = max(tx3_pre_refund, tx3_floor)
    tx3 = Transaction(
        to=tx3_target,
        gas_limit=tx3_fee_gas,
        sender=tx3_sender,
        data=tx3_data,
        expected_receipt={"gas_used": tx3_fee_gas},
    )
    tx3_gas_price = tx3.gas_price if tx3.gas_price else tx3.max_fee_per_gas
    assert tx3_gas_price is not None
    post[tx3_sender] = Account(
        balance=initial_fund - tx3_fee_gas * tx3_gas_price
    )

    total_gas_used = (
        tx1_block_contribution + tx2_contribution + tx3_block_contribution
    )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx1, tx2, tx3],
                expected_gas_used=total_gas_used,
            )
        ],
        post=post,
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.