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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 | @pytest.mark.parametrize(
"out_of_gas_at",
[
OutOfGasAt.EIP_2200_STIPEND,
OutOfGasAt.EIP_2200_STIPEND_PLUS_1,
OutOfGasAt.ABOVE_STIPEND_BELOW_ACCESS,
OutOfGasAt.ACCESS_COVERED_OOG_ON_WRITE,
OutOfGasAt.EXACT_GAS_MINUS_1,
None, # no oog, successful sstore
],
ids=lambda x: x.value if x else "successful_sstore",
)
def test_bal_sstore_and_oog(
pre: Alloc,
blockchain_test: BlockchainTestFiller,
fork: Fork,
out_of_gas_at: OutOfGasAt | None,
) -> None:
"""
Test BAL recording with SSTORE at various OOG boundaries and success.
``SSTORE`` clears two gates before the write cost: the EIP-2200
stipend sentry (``gas_left`` must exceed ``CALL_STIPEND``) and the
cold access charge (``COLD_STORAGE_ACCESS``). The slot read is
recorded in the BAL only once both are cleared, so the recording
gate is the higher of the two — which one dominates depends on the
fork's schedule, and the expectations below are derived from that
relation rather than assuming it.
1. OOG at the stipend -> sentry fires, no BAL changes
2. OOG at stipend + 1 -> sentry cleared by one; the read is
recorded only if this also covers the access cost
3. OOG at access cost - 1 -> below one of the two gates, no BAL
changes
4. OOG at the recording gate, write unaffordable -> storage read in
BAL
5. OOG at exact gas minus 1 -> storage read in BAL
6. exact gas (success) -> storage write in BAL
"""
alice = pre.fund_eoa()
# Create contract that attempts SSTORE to cold storage slot 0x01
storage_contract_code = Op.SSTORE(
0x01, 0x42, key_warm=False, original_value=0, new_value=0x42
)
storage_contract = pre.deploy_contract(code=storage_contract_code)
intrinsic_gas_cost = fork.transaction_intrinsic_cost_calculator()()
# Full cost: PUSHes + SSTORE (COLD_STORAGE_ACCESS + STORAGE_SET)
full_cost = storage_contract_code.gas_cost(fork)
# Push cost for the gas-boundary calculations below.
push_code = Op.PUSH1(0x42) + Op.PUSH1(0x01)
push_cost = push_code.gas_cost(fork)
# CALL_STIPEND is a threshold check, not a gas cost. The read is
# recorded once the sentry is cleared and the access cost is
# affordable, so the recording gate is the higher of the two.
stipend = fork.gas_costs().CALL_STIPEND
cold_access = fork.gas_costs().COLD_STORAGE_ACCESS
read_gate = max(cold_access, stipend + 1)
if out_of_gas_at == OutOfGasAt.EIP_2200_STIPEND:
# gas_left == stipend: fails the sentry check outright.
tx_gas_limit = intrinsic_gas_cost + push_cost + stipend
elif out_of_gas_at == OutOfGasAt.EIP_2200_STIPEND_PLUS_1:
# gas_left == stipend + 1: clears the stipend sentry by one;
# whether the access is then affordable depends on the schedule.
tx_gas_limit = intrinsic_gas_cost + push_cost + stipend + 1
elif out_of_gas_at == OutOfGasAt.ABOVE_STIPEND_BELOW_ACCESS:
# gas_left == access cost - 1: cannot afford the access (when
# the stipend dominates, the sentry fires first instead), so
# OOG before the read either way.
tx_gas_limit = intrinsic_gas_cost + push_cost + cold_access - 1
elif out_of_gas_at == OutOfGasAt.ACCESS_COVERED_OOG_ON_WRITE:
# gas_left == read gate: sentry cleared and access affordable
# (read recorded), then OOG on the write cost.
tx_gas_limit = intrinsic_gas_cost + push_cost + read_gate
elif out_of_gas_at == OutOfGasAt.EXACT_GAS_MINUS_1:
# fail at the final charge at exact gas - 1 (boundary condition).
tx_gas_limit = intrinsic_gas_cost + full_cost - 1
else:
# exact gas for successful SSTORE
tx_gas_limit = intrinsic_gas_cost + full_cost
tx = Transaction(
sender=alice,
to=storage_contract,
gas_limit=tx_gas_limit,
)
# The read is recorded only once the recording gate is covered: the
# frame reaches the implicit SLOAD before any later OOG.
expect_storage_read = out_of_gas_at in (
OutOfGasAt.ACCESS_COVERED_OOG_ON_WRITE,
OutOfGasAt.EXACT_GAS_MINUS_1,
) or (
out_of_gas_at == OutOfGasAt.EIP_2200_STIPEND_PLUS_1
and stipend + 1 >= cold_access
)
expect_storage_write = out_of_gas_at is None
block = Block(
txs=[tx],
expected_block_access_list=BlockAccessListExpectation(
account_expectations={
storage_contract: BalAccountExpectation(
storage_changes=[
BalStorageSlot(
slot=0x01,
slot_changes=[
BalStorageChange(
block_access_index=1, post_value=0x42
)
],
),
]
if expect_storage_write
else [],
storage_reads=[0x01] if expect_storage_read else [],
)
}
),
)
blockchain_test(
pre=pre,
blocks=[block],
post={
alice: Account(nonce=1),
storage_contract: Account(
storage={0x01: 0x42} if expect_storage_write else {}
),
},
)
|