Skip to content

test_exact_coinbase_fee_simple_sstore()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_multi_block.py::test_exact_coinbase_fee_simple_sstore@2119b382.

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

Assert exact coinbase balance from a single SSTORE transaction.

Compute tx_gas_used from first principles and verify the reporter contract reads exactly tx_gas_used as the coinbase balance (priority fee is 1 wei). Any error in state_gas_left or refund_counter will produce a different coinbase balance, causing the state root to diverge.

Motivated by BAL devnet-3 ethrex/besu coinbase balance mismatch where clients diverged on cumulative receipt_gas_used.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_multi_block.py
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
@pytest.mark.valid_from("EIP8037")
def test_exact_coinbase_fee_simple_sstore(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Assert exact coinbase balance from a single SSTORE transaction.

    Compute `tx_gas_used` from first principles and verify the
    reporter contract reads exactly `tx_gas_used` as the coinbase
    balance (priority fee is 1 wei). Any error in `state_gas_left` or
    `refund_counter` will produce a different coinbase balance,
    causing the state root to diverge.

    Motivated by BAL devnet-3 ethrex/besu coinbase balance mismatch
    where clients diverged on cumulative `receipt_gas_used`.
    """
    gas_costs = fork.gas_costs()
    sstore_state_gas = Op.SSTORE(new_value=1).state_cost(fork)

    # Gas breakdown for tx 1 (SSTORE zero-to-nonzero, no calldata):
    # PUSH1(1) + PUSH1(0) + SSTORE(cold, zero-to-nonzero) + STOP
    intrinsic_regular = gas_costs.TX_BASE
    if fork.is_eip_enabled(2780):
        # EIP-2780 surfaces an explicit recipient-access charge for
        # non-self, non-create transactions on top of ``TX_BASE``.
        intrinsic_regular += gas_costs.COLD_ACCOUNT_ACCESS
    evm_regular = (
        2 * gas_costs.VERY_LOW  # PUSH1 + PUSH1
        + gas_costs.COLD_STORAGE_WRITE  # SSTORE cold zero-to-nonzero
    )
    tx1_gas_used = intrinsic_regular + evm_regular + sstore_state_gas
    expected_coinbase = tx1_gas_used

    # Tx 1: single SSTORE zero-to-nonzero
    sstore_storage = Storage()
    sstore_contract = pre.deploy_contract(
        code=(Op.SSTORE(sstore_storage.store_next(1), 1)),
    )

    # Tx 2: reporter reads BALANCE(COINBASE) into slot 0
    reporter = pre.deploy_contract(
        code=(Op.SSTORE(0, Op.BALANCE(Op.COINBASE)) + Op.SSTORE(1, 1)),
    )

    blocks = [
        Block(
            txs=[
                Transaction(
                    to=sstore_contract,
                    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 = {
        sstore_contract: Account(storage=sstore_storage),
        reporter: Account(storage={0: expected_coinbase, 1: 1}),
    }
    blockchain_test(pre=pre, blocks=blocks, post=post)

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.