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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 | @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.
The slot read is recorded in the BAL only once the cold access cost
is covered. Post-repricing that cost (COLD_STORAGE_ACCESS) exceeds the
EIP-2200 stipend, so clearing the stipend sentry alone no longer
records the read. The stipend + 1 case pins the old sentry boundary
against regressions to sentry-gated recording.
1. OOG at the stipend, below the access cost -> no BAL changes
2. OOG above the stipend but below access cost (probed at
stipend + 1 and access cost - 1) -> no BAL changes
3. OOG at the access cost, write unaffordable -> storage read in BAL
4. OOG at exact gas minus 1 -> storage read in BAL
5. 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 cold access
# cost gates the read into the BAL and now exceeds the stipend.
stipend = fork.gas_costs().CALL_STIPEND
cold_access = fork.gas_costs().COLD_STORAGE_ACCESS
if out_of_gas_at == OutOfGasAt.EIP_2200_STIPEND:
# gas_left == stipend: fails the check, below the access cost.
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 but
# cannot afford the access, so OOG before the read.
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: clears the stipend sentry but
# cannot afford the access, so OOG before the read.
tx_gas_limit = intrinsic_gas_cost + push_cost + cold_access - 1
elif out_of_gas_at == OutOfGasAt.ACCESS_COVERED_OOG_ON_WRITE:
# gas_left == access cost: access affordable (read recorded),
# then OOG on the write cost.
tx_gas_limit = intrinsic_gas_cost + push_cost + cold_access
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 access cost 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,
)
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 {}
),
},
)
|