Skip to content

test_top_frame_state_charge()

Documentation for tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_top_frame_charges.py::test_top_frame_state_charge@636b5f17.

Generate fixtures for these test cases for Bogota with:

fill -v tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_top_frame_charges.py::test_top_frame_state_charge --fork Bogota

Recipient is empty and the transaction transfers a non-zero value, so the top-frame fires the NEW_ACCOUNT state-gas charge.

  • oog: gas limit is one short of covering the state charge. The transaction passes the intrinsic check, enters the top-frame preparation in create_evm, and out-of-gases on the NEW_ACCOUNT state charge before any EVM bytecode runs. The sender pays the full gas_limit and no value is transferred.
  • success: gas limit covers the state charge. The value transfer brings the recipient into existence and the recipient ends the transaction holding the transferred balance.
  • success_with_unused_gas: as success, with gas left over that the sender must not be charged for.

An absent coinbase is still charged: it is warm, but receives its priority fee only after execution.

Source code in tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_top_frame_charges.py
 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
@EIPChecklist.GasCostChanges.Test.OutOfGas()
@EIPChecklist.GasCostChanges.Test.GasUpdatesMeasurement()
@pytest.mark.parametrize(
    "recipient_is_coinbase",
    [
        False,
        pytest.param(
            True,
            marks=pytest.mark.execute(
                pytest.mark.skip(
                    reason="requires env.fee_recipient as block coinbase"
                )
            ),
        ),
    ],
)
@pytest.mark.parametrize(
    "outcome", ["oog", "success", "success_with_unused_gas"]
)
def test_top_frame_state_charge(
    fork: Fork,
    pre: Alloc,
    env: Environment,
    state_test: StateTestFiller,
    outcome: str,
    recipient_is_coinbase: bool,
) -> None:
    """
    Recipient is empty and the transaction transfers a non-zero value,
    so the top-frame fires the ``NEW_ACCOUNT`` state-gas charge.

    - ``oog``: gas limit is one short of covering the state charge.
      The transaction passes the intrinsic check, enters the top-frame
      preparation in ``create_evm``, and out-of-gases on the
      ``NEW_ACCOUNT`` state charge before any EVM bytecode runs.
      The sender pays the full ``gas_limit`` and no value is
      transferred.
    - ``success``: gas limit covers the state charge. The value
      transfer brings the recipient into existence and the recipient
      ends the transaction holding the transferred balance.
    - ``success_with_unused_gas``: as ``success``, with gas left over
      that the sender must not be charged for.

    An absent coinbase is still charged: it is warm, but receives its
    priority fee only after execution.
    """
    sender = pre.fund_eoa()
    if recipient_is_coinbase:
        target = env.fee_recipient
    else:
        target = pre.fund_eoa(amount=0)

    value = 1
    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        sends_value=True,
        recipient_type=RecipientType.EMPTY_ACCOUNT,
        return_cost_deducted_prior_execution=True,
    )
    top_frame_state_gas = fork.transaction_top_frame_state_gas(
        sends_value=True,
        recipient_type=RecipientType.EMPTY_ACCOUNT,
    )
    assert top_frame_state_gas > 0, (
        "top-frame state gas must be non-zero for this scenario"
    )

    gas_used = intrinsic_gas + top_frame_state_gas
    if outcome == "oog":
        gas_limit = gas_used - 1
        # Running out of gas consumes the whole limit.
        gas_used = gas_limit
    elif outcome == "success":
        gas_limit = gas_used
    elif outcome == "success_with_unused_gas":
        gas_limit = gas_used + 1
    else:
        raise ValueError(f"unknown outcome: {outcome}")

    tx = Transaction(
        sender=sender,
        to=target,
        value=value,
        gas_limit=gas_limit,
    )

    sender_balance_change = -GasFee(tx, gas=gas_used)
    target_balance_change: BalanceExpression | Tip | int = (
        Tip(tx, gas=gas_used) if recipient_is_coinbase else 0
    )
    if outcome != "oog":
        sender_balance_change -= value
        target_balance_change += value

    post = {
        sender: Account(nonce=1, balance_change=sender_balance_change),
        target: Account(balance_change=target_balance_change),
    }

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

Parametrized Test Cases

This test generates 6 parametrized test cases across 2 forks.