217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423 | @pytest.mark.parametrize(
"oog_scenario",
[
pytest.param(OogScenario.NO_OOG, id="no_oog"),
pytest.param(OogScenario.OOG_CODE_DEPOSIT, id="oog_code_deposit"),
pytest.param(OogScenario.OOG_INVALID, id="oog_invalid_opcode"),
],
)
@pytest.mark.parametrize(
"refund_type",
[
pytest.param(RefundType.SSTORE_DIRECT, id="sstore_direct"),
pytest.param(RefundType.SSTORE_CALL, id="sstore_call"),
pytest.param(RefundType.SSTORE_DELEGATECALL, id="sstore_delegatecall"),
pytest.param(RefundType.SSTORE_CALLCODE, id="sstore_callcode"),
pytest.param(RefundType.SELFDESTRUCT, id="selfdestruct"),
pytest.param(RefundType.LOG_OP, id="log_op"),
pytest.param(RefundType.NESTED_CREATE, id="nested_create"),
pytest.param(RefundType.NESTED_CREATE2, id="nested_create2"),
],
)
@pytest.mark.ported_from(
[
"https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stCreateTest/CreateOOGFromEOARefundsFiller.yml",
],
pr=["https://github.com/ethereum/execution-specs/pull/1831"],
)
def test_create_oog_from_eoa_refunds(
pre: Alloc,
blockchain_test: BlockchainTestFiller,
refund_type: RefundType,
oog_scenario: OogScenario,
fork: Fork,
) -> None:
"""
Test CREATE from EOA with various refund mechanisms and OOG scenarios.
Verifies that:
1. Refunds are not applied when contract creation runs Out of Gas
2. When OOG occurs, the sender's balance is fully consumed (no refund)
3. When OOG occurs, the contract is not created
For BAL (Block Access List) tracking:
- NoOoG: Storage writes should be recorded as `storage_changes`
- OoG: Storage writes should be converted to `storage_reads` since
the CREATE failed and all state changes were reverted
"""
helpers = deploy_helper_contracts(pre)
sender = pre.fund_eoa(amount=4_000_000)
init_code = build_init_code(refund_type, oog_scenario, helpers)
created_address = compute_create_address(address=sender, nonce=0)
tx = Transaction(
sender=sender,
to=None,
data=init_code,
gas_limit=400_000,
)
post: Dict[Address, Account | None] = {
sender: Account(nonce=1),
}
if oog_scenario == OogScenario.NO_OOG:
# contract created with code 0x00 (1 byte from memory)
if refund_type == RefundType.NESTED_CREATE:
# Nested CREATE increments the created contract's nonce to 2
post[created_address] = Account(
nonce=2,
code=b"\x00",
storage={0: 1}, # successful write
)
nested_created = compute_create_address(
address=created_address, nonce=1
)
post[nested_created] = Account(
nonce=1,
code=b"\x00",
storage={},
)
elif refund_type == RefundType.NESTED_CREATE2:
# nested create2 increments the created contract's nonce to 2
post[created_address] = Account(
nonce=2,
code=b"\x00",
storage={0: 1},
)
nested_created = compute_create2_address(
address=created_address,
salt=0,
initcode=Op.SSTORE(0, 1) + Op.SSTORE(0, 0) + Op.RETURN(0, 1),
)
post[nested_created] = Account(
nonce=1,
code=b"\x00",
storage={},
)
else:
post[created_address] = Account(
nonce=1,
code=b"\x00",
storage={0: 1},
)
post[sender] = Account(nonce=1)
else:
# OOG case: contract not created, sender balance is fully consumed
post[created_address] = Account.NONEXISTENT
post[sender] = Account(
nonce=1,
balance=0,
)
if refund_type == RefundType.SELFDESTRUCT:
selfdestruct_code = Op.SELFDESTRUCT(Op.ORIGIN) + Op.STOP
if oog_scenario == OogScenario.NO_OOG:
# selfdestruct succeeded, balance is 0
post[helpers.selfdestruct] = Account(
balance=0,
nonce=1,
)
else:
# OOG: selfdestruct reverted, helper unchanged
post[helpers.selfdestruct] = Account(
code=bytes(selfdestruct_code),
nonce=1,
storage={1: 1},
)
bal_expectation = None
if fork.is_eip_enabled(7928):
if oog_scenario == OogScenario.NO_OOG:
# Success: storage write to slot 0 persists
expected_nonce = (
2
if refund_type
in (RefundType.NESTED_CREATE, RefundType.NESTED_CREATE2)
else 1
)
created_bal = BalAccountExpectation(
nonce_changes=[
BalNonceChange(
block_access_index=1, post_nonce=expected_nonce
)
],
storage_changes=[
BalStorageSlot(
slot=0,
slot_changes=[
BalStorageChange(
block_access_index=1, post_value=1
)
],
),
],
storage_reads=(
# noop write 0 -> 1 -> 0
[1]
if refund_type
in (
RefundType.SSTORE_DIRECT,
RefundType.SSTORE_DELEGATECALL,
RefundType.SSTORE_CALLCODE,
RefundType.NESTED_CREATE,
RefundType.NESTED_CREATE2,
)
else []
),
)
else:
# OOG case: storage writes converted to reads
# All refund types write to slot 0, most also write to slot 1
if refund_type in (
RefundType.SSTORE_DIRECT,
RefundType.SSTORE_DELEGATECALL,
RefundType.SSTORE_CALLCODE,
RefundType.NESTED_CREATE,
RefundType.NESTED_CREATE2,
):
# write to both slot 0 and slot 1 (noop write 0 -> 1 -> 0)
created_bal = BalAccountExpectation(
storage_changes=[],
storage_reads=[0, 1],
)
else:
# SSTORE_CALL, SELFDESTRUCT, LOG_OP only write to slot 0
created_bal = BalAccountExpectation(
storage_changes=[],
storage_reads=[0],
)
bal_expectation = BlockAccessListExpectation(
account_expectations={
sender: BalAccountExpectation(
nonce_changes=[
BalNonceChange(block_access_index=1, post_nonce=1)
],
),
created_address: created_bal,
}
)
blockchain_test(
pre=pre,
blocks=[Block(txs=[tx], expected_block_access_list=bal_expectation)],
post=post,
)
|