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
207
208
209
210
211
212
213
214
215 | @pytest.mark.parametrize(
"leftover_gas_case",
[
"one_short_of_pending_reads",
"block_full",
"block_packed_with_cheapest_txs",
],
)
def test_bal_pending_system_call_reads_vs_leftover_gas(
pre: Alloc,
blockchain_test: BlockchainTestFiller,
fork: Fork,
leftover_gas_case: str,
) -> None:
"""
A block stays valid however little gas it leaves for the storage reads
its post-execution system calls still owe. The dequeues spend no block
gas, so a gas-feasibility check on those reads (`BLOCK_ACCESS_LIST_ITEM`
each) would wrongly reject these blocks; see ethereum/EIPs#12277.
"""
request_types = _queued_request_types(fork)
post_execution_contracts = {
address
for address, phase in fork.system_contract_call_phases().items()
if phase is SystemCallPhase.AFTER_TRANSACTIONS
}
assert post_execution_contracts == {
cls.system_contract_address for cls in request_types
}, (
f"{fork} calls a system contract after its transactions that this "
"test does not model as a request queue"
)
enqueue_txs: list[Transaction] = []
pending_reads: dict[Address, list[int]] = {}
post: dict[Address, Account] = {}
for request_class in request_types:
# Block 1 dequeues the first block's worth, block 2 the second.
requests = _enqueue_requests(
request_class, 2 * request_class.max_per_block
)
interaction = SystemContractInteractionTransaction(requests=requests)
enqueue_txs += interaction.update_pre(pre).transactions()
address = request_class.system_contract_address
pending_reads[address] = _dequeue_reads(
request_class,
start_index=request_class.max_per_block,
count=request_class.max_per_block,
)
# Each enqueue pays its fee as call value, so one that fails leaves
# this balance short: the witness that block 1 queued everything.
post[address] = Account(balance=sum(r.value for r in requests))
pending_read_count = sum(len(reads) for reads in pending_reads.values())
read_budget = pending_read_count * fork.gas_costs().BLOCK_ACCESS_LIST_ITEM
cheapest_tx = fork.transaction_intrinsic_cost_calculator()(calldata=b"")
tx_gas_cap = fork.transaction_gas_limit_cap()
assert tx_gas_cap is not None, f"{fork} has no transaction gas limit cap"
tail_length = 0
if leftover_gas_case == "one_short_of_pending_reads":
leftover_gas = read_budget - 1
elif leftover_gas_case == "block_full":
leftover_gas = 0
elif leftover_gas_case == "block_packed_with_cheapest_txs":
# Filling the gas below the budget with the cheapest transactions
# puts every late transaction boundary short of the pending reads.
tail_length, leftover_gas = divmod(read_budget - 1, cheapest_tx)
assert tail_length > 0, "the window must fit a cheapest transaction"
else:
raise ValueError(f"unhandled leftover_gas_case {leftover_gas_case}")
burn_gas = BLOCK_GAS_LIMIT - leftover_gas - tail_length * cheapest_tx
gas_limits = _split_gas(burn_gas, tx_gas_cap) + [cheapest_tx] * tail_length
assert min(gas_limits) >= cheapest_tx, (
"every burn must cover its intrinsic cost"
)
# Each burn runs out of gas, so it spends exactly its gas limit.
burner = pre.deploy_contract(code=Macros.OOG)
sender = pre.fund_eoa()
burn_txs = [
Transaction(sender=sender, to=burner, gas_limit=gas_limit)
for gas_limit in gas_limits
]
system_call_index = len(burn_txs) + 1
account_expectations = {}
for request_class in request_types:
address = request_class.system_contract_address
account_expectations[address] = BalAccountExpectation(
storage_reads=pending_reads[address],
storage_changes=_drained_queue_changes(
request_class, system_call_index
),
)
blockchain_test(
pre=pre,
blocks=[
# Nothing checks the hundreds of enqueue receipts.
Block(txs=enqueue_txs, include_receipts_in_output=False),
Block(
txs=burn_txs,
header_verify=Header(
gas_limit=BLOCK_GAS_LIMIT,
gas_used=BLOCK_GAS_LIMIT - leftover_gas,
),
expected_block_access_list=BlockAccessListExpectation(
account_expectations=account_expectations
),
),
],
post=post,
genesis_environment=Environment(gas_limit=BLOCK_GAS_LIMIT),
)
|