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 | @pytest.mark.parametrize(
"is_success", [True, False], ids=["exact_gas", "exact_gas_minus_1"]
)
@pytest.mark.parametrize(
"beneficiary", ["eoa", "contract"], ids=["eoa", "contract"]
)
@pytest.mark.parametrize(
"warm",
[
pytest.param(
False, id="cold", marks=pytest.mark.valid_from("TangerineWhistle")
),
pytest.param(True, id="warm", marks=pytest.mark.valid_from("Berlin")),
],
)
@pytest.mark.parametrize(
"same_tx", [False, True], ids=["pre_deploy", "same_tx"]
)
@pytest.mark.parametrize(
"originator_balance",
[0, 1],
ids=["no_balance", "has_balance"],
)
@pytest.mark.parametrize(
"beneficiary_initial_balance",
[0, 1],
ids=["dead_beneficiary", "alive_beneficiary"],
)
def test_selfdestruct_to_account(
pre: Alloc,
blockchain_test: BlockchainTestFiller,
fork: Fork,
is_success: bool,
beneficiary: str,
warm: bool,
same_tx: bool,
originator_balance: int,
beneficiary_initial_balance: int,
) -> None:
"""
Test SELFDESTRUCT success boundary for account beneficiaries.
- exact_gas: succeeds, balance transferred, contract destroyed
- exact_gas_minus_1: OOG, operation fails
"""
# Create beneficiary
if beneficiary == "eoa":
beneficiary_addr: EOA | Address = pre.fund_eoa(
amount=beneficiary_initial_balance
)
else:
beneficiary_addr = pre.deploy_contract(
code=Op.STOP, balance=beneficiary_initial_balance
)
# Determine if beneficiary is dead (for NEW_ACCOUNT calculation)
# Contract with code is NOT dead even with balance=0
beneficiary_dead = (
beneficiary_initial_balance == 0 and beneficiary == "eoa"
)
# Calculate exact gas for success (includes NEW_ACCOUNT if applicable)
inner_call_gas = calculate_selfdestruct_gas(
fork,
beneficiary_warm=warm,
beneficiary_dead=beneficiary_dead,
originator_balance=originator_balance,
)
if not is_success:
inner_call_gas -= 1
# In BAL if: success OR NEW_ACCOUNT charged (OOG after access)
needs_new_account = False
if beneficiary_dead:
if fork >= SpuriousDragon:
needs_new_account = originator_balance > 0
else:
needs_new_account = True
beneficiary_in_bal = is_success or needs_new_account
alice, caller, victim, tx = setup_selfdestruct_test(
pre,
fork,
beneficiary_addr,
originator_balance,
same_tx,
beneficiary_warm=warm,
inner_call_gas=inner_call_gas,
)
expected_bal = build_bal_expectations(
fork,
alice,
caller,
victim,
beneficiary_addr,
originator_balance,
beneficiary_initial_balance,
same_tx,
success=is_success,
beneficiary_in_bal=beneficiary_in_bal,
)
post = build_post_state(
fork,
alice,
caller,
victim,
beneficiary_addr,
originator_balance,
beneficiary_initial_balance,
same_tx,
success=is_success,
beneficiary_has_code=(beneficiary == "contract"),
)
blockchain_test(
pre=pre,
blocks=[Block(txs=[tx], expected_block_access_list=expected_bal)],
post=post,
)
|