423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557 | @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_state_access_boundary(
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 state access boundary for account beneficiaries.
Consensus check: beneficiary must be accessed at base cost boundary,
before NEW_ACCOUNT is evaluated.
- exact_gas: beneficiary IS accessed (in BAL)
- exact_gas_minus_1: beneficiary NOT accessed (not in BAL)
"""
# 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 gas for state access boundary only (base + cold access)
# Does NOT include NEW_ACCOUNT
inner_call_gas = Op.SELFDESTRUCT(
0, # beneficiary address (generates a PUSH)
address_warm=warm or fork < Berlin,
account_new=False,
).gas_cost(fork)
if not is_success:
inner_call_gas -= 1
# Determine if operation succeeds at this gas level
# At state access boundary, we have enough gas for base + cold access
# Operation succeeds if NO NEW_ACCOUNT is needed:
# - Beneficiary is alive (has balance or has code)
# - OR beneficiary is dead but originator_balance=0 (>=SpuriousDragon)
needs_new_account = False
if beneficiary_dead:
if fork >= SpuriousDragon:
needs_new_account = originator_balance > 0
else:
needs_new_account = True
# At exact_gas: success if no NEW_ACCOUNT needed
# At exact_gas_minus_1: always OOG (before state access)
operation_success = is_success and not 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,
)
# Key difference: beneficiary_in_bal depends on is_success
# exact_gas: state accessed, beneficiary in BAL
# exact_gas_minus_1: OOG before state access, beneficiary NOT in BAL
expected_bal = build_bal_expectations(
fork,
alice,
caller,
victim,
beneficiary_addr,
originator_balance,
beneficiary_initial_balance,
same_tx,
success=operation_success,
beneficiary_in_bal=is_success,
)
post = build_post_state(
fork,
alice,
caller,
victim,
beneficiary_addr,
originator_balance,
beneficiary_initial_balance,
same_tx,
success=operation_success,
beneficiary_has_code=(beneficiary == "contract"),
)
blockchain_test(
pre=pre,
blocks=[Block(txs=[tx], expected_block_access_list=expected_bal)],
post=post,
)
|