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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 | @EIPChecklist.BlockLevelConstraint.Test.Boundary.Exact()
@EIPChecklist.BlockLevelConstraint.Test.Boundary.Over()
@pytest.mark.inclusion_test
@TransactionFailure.with_all_tx_failures()
@pytest.mark.parametrize(
"refund_tx_has_gas_limit_slack",
[
pytest.param(True, id="refund_tx_has_gas_limit_slack"),
pytest.param(False, id=""),
],
)
@pytest.mark.parametrize(
"trailing_tx_data_floor",
[
pytest.param(True, id="trailing_tx_hits_data_floor"),
pytest.param(False, id=""),
],
)
@pytest.mark.parametrize(
"exceed_block_gas_limit",
[
pytest.param(True, marks=pytest.mark.exception_test),
False,
],
)
@pytest.mark.with_all_refund_types()
@pytest.mark.execute(pytest.mark.skip(reason="Requires specific gas price"))
def test_multi_transaction_gas_accounting(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
fork: Fork,
refund_type: RefundTypes,
refund_tx_has_gas_limit_slack: bool,
exceed_block_gas_limit: bool,
trailing_tx_data_floor: bool,
refund_tx_failure: TransactionFailure | None,
) -> None:
"""
Test block gas accounting with refunds per EIP-7778.
When exceed_block_gas_limit=True, we create a scenario where:
- Pre-refund gas (gas_used) > block_gas_limit - intrinsic_cost
(no room for another tx with correct EIP-7778 accounting)
- Post-refund gas (gas_spent) <= block_gas_limit - intrinsic_cost
(appears to have room with old refund-based accounting)
This tests that clients correctly use pre-refund gas for block accounting.
"""
intrinsic_cost_calc = fork.transaction_intrinsic_cost_calculator()
data_floor_calc = fork.transaction_data_floor_cost_calculator()
refunds_count = 10
stop_bytecode = Op.STOP
stop_address = pre.deterministic_deploy_contract(deploy_code=stop_bytecode)
refund_tx = RefundTransaction.build(
fork=fork,
sender=pre.fund_eoa(INITIAL_FUND),
refund_types={refund_type},
refunds_count=refunds_count,
tx_failure=refund_tx_failure,
refund_tx_has_gas_limit_slack=refund_tx_has_gas_limit_slack,
)
refund_tx.set_pre(pre)
post = refund_tx.post(pre, block_is_invalid=exceed_block_gas_limit)
trailing_tx_sender = pre.fund_eoa()
trailing_tx_calldata = b"\xff" if trailing_tx_data_floor else b""
trailing_tx_intrinsic_gas_cost = intrinsic_cost_calc(
calldata=trailing_tx_calldata
)
# Block execution gas applies the calldata floor to the actual charge.
trailing_tx_block_gas = max(
intrinsic_cost_calc(
calldata=trailing_tx_calldata,
return_cost_deducted_prior_execution=True,
),
data_floor_calc(data=trailing_tx_calldata),
)
trailing_tx = Transaction(
to=stop_address,
data=trailing_tx_calldata,
gas_limit=trailing_tx_intrinsic_gas_cost,
sender=trailing_tx_sender,
expected_receipt={
"gas_used": trailing_tx_intrinsic_gas_cost,
},
error=(
TransactionException.GAS_ALLOWANCE_EXCEEDED
if exceed_block_gas_limit
else None
),
)
# Extra tx has no state gas, so its state gas contribution = 0
block_execution = refund_tx.gas_used_pre_refund + trailing_tx_block_gas
block_state = refund_tx.state_gas
total_block_gas_used = max(block_execution, block_state)
# The block gas_limit must accommodate trailing_tx's full gas_limit
# (floor-inclusive, like its block-execution charge). For
# exceed_block_gas_limit=True we set the limit below
# total_block_gas_used to test that the trailing_tx fails.
if exceed_block_gas_limit:
environment_gas_limit = total_block_gas_used - 1
else:
environment_gas_limit = (
refund_tx.gas_used_pre_refund + trailing_tx_intrinsic_gas_cost
)
txs = [refund_tx, trailing_tx]
blockchain_test(
pre=pre,
blocks=[
Block(
txs=txs,
exception=[
BlockException.GAS_USED_OVERFLOW,
TransactionException.GAS_ALLOWANCE_EXCEEDED,
]
if exceed_block_gas_limit
else None,
expected_gas_used=total_block_gas_used
if not exceed_block_gas_limit
else None,
gas_limit=environment_gas_limit,
)
],
post=post,
genesis_environment=Environment(gas_limit=environment_gas_limit),
)
|