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 | @pytest.mark.parametrize(
"out_of_gas_at",
[
OutOfGasAt.EIP_2200_STIPEND,
OutOfGasAt.EIP_2200_STIPEND_PLUS_1,
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.
1. OOG at EIP-2200 stipend check & implicit SLOAD -> no BAL changes
2. OOG post EIP-2200 stipend check & implicit SLOAD -> storage read in BAL
3. OOG at exact gas minus 1 -> storage read in BAL
4. 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 stipend boundary calculations
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
# Keep from gas_costs
stipend = fork.gas_costs().CALL_STIPEND
if out_of_gas_at == OutOfGasAt.EIP_2200_STIPEND:
# 2300 after PUSHes (fails stipend check: 2300 <= 2300)
tx_gas_limit = intrinsic_gas_cost + push_cost + stipend
elif out_of_gas_at == OutOfGasAt.EIP_2200_STIPEND_PLUS_1:
# 2301 after PUSHes (passes stipend, does SLOAD, fails charge_gas)
tx_gas_limit = intrinsic_gas_cost + push_cost + stipend + 1
elif out_of_gas_at == OutOfGasAt.EXACT_GAS_MINUS_1:
# fail at charge_gas() 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,
)
# Storage read recorded only if we pass the stipend check and reach
# implicit SLOAD (STIPEND_PLUS_1 and EXACT_GAS_MINUS_1)
expect_storage_read = out_of_gas_at in (
OutOfGasAt.EIP_2200_STIPEND_PLUS_1,
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 {}
),
},
)
|