Skip to content

test_self_destructing_initcode()

Documentation for tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_self_destructing_initcode@b314d18e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_self_destructing_initcode --fork Amsterdam

Test that a contract can self-destruct in its initcode.

Behavior is the same before and after EIP-6780.

Test using
  • Different initial balances for the self-destructing contract
  • Different opcodes: CREATE, CREATE2
  • Different number of calls to the self-destructing contract in the same tx
Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct.py
410
411
412
413
414
415
416
417
418
419
420
421
422
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
@pytest.mark.parametrize("create_opcode", [Op.CREATE, Op.CREATE2])
@pytest.mark.parametrize("call_times", [0, 1, 2])
@pytest.mark.parametrize(
    "selfdestruct_contract_initial_balance",
    [0, 100_000],
)
@pytest.mark.valid_from("Shanghai")
def test_self_destructing_initcode(
    state_test: StateTestFiller,
    pre: Alloc,
    sender: EOA,
    fork: Fork,
    selfdestruct_code: Bytecode,
    sendall_recipient_addresses: List[Address],
    create_opcode: Op,
    call_times: int,  # Number of times to call the self-destructing contract
    # in the same tx
    selfdestruct_contract_initial_balance: int,
) -> None:
    """
    Test that a contract can self-destruct in its initcode.

    Behavior is the same before and after EIP-6780.

    Test using:
        - Different initial balances for the self-destructing contract
        - Different opcodes: CREATE, CREATE2
        - Different number of calls to the self-destructing contract in
           the same tx
    """
    initcode_copy_from_address = pre.deploy_contract(selfdestruct_code)
    # Our entry point is an initcode that in turn creates a self-destructing
    # contract
    entry_code_storage = Storage()
    sendall_amount = 0

    # Bytecode used to create the contract, can be CREATE or CREATE2
    create_bytecode = create_opcode(size=len(selfdestruct_code))

    selfdestruct_contract_address = compute_create_address(
        address=compute_create_address(address=sender, nonce=0),
        nonce=1,
        initcode=selfdestruct_code,
        opcode=create_opcode,
    )

    # Entry code that will be executed, creates the contract and then calls it
    # in the same tx
    entry_code = (
        # Initcode is already deployed at `initcode_copy_from_address`, so just
        # copy it
        Op.EXTCODECOPY(
            initcode_copy_from_address,
            0,
            0,
            len(selfdestruct_code),
        )
        # And we store the created address for verification purposes
        + Op.SSTORE(
            entry_code_storage.store_next(selfdestruct_contract_address),
            create_bytecode,
        )
    )

    # Store the EXTCODE* properties of the created address
    entry_code += Op.SSTORE(
        entry_code_storage.store_next(0),
        Op.EXTCODESIZE(selfdestruct_contract_address),
    )

    entry_code += Op.SSTORE(
        entry_code_storage.store_next(Bytecode().keccak256()),
        Op.EXTCODEHASH(selfdestruct_contract_address),
    )

    # Call the self-destructing contract multiple times as required, increasing
    # the wei sent each time
    entry_code_balance = 0
    for i in range(call_times):
        entry_code += Op.SSTORE(
            entry_code_storage.store_next(1),
            Op.CALL(
                Op.GASLIMIT,  # Gas
                selfdestruct_contract_address,  # Address
                i,  # Value
                0,
                0,
                0,
                0,
            ),
        )
        entry_code_balance += i

        entry_code += Op.SSTORE(
            entry_code_storage.store_next(entry_code_balance),
            Op.BALANCE(selfdestruct_contract_address),
        )

    # Lastly return zero so the entry point contract is created and we can
    # retain the stored values for verification.
    entry_code += Op.RETURN(max(len(selfdestruct_code), 32), 1)

    if selfdestruct_contract_initial_balance > 0:
        # Address where the contract is created already had some balance,
        # which must be included in the send-all operation
        sendall_amount += selfdestruct_contract_initial_balance
        pre.fund_address(
            selfdestruct_contract_address,
            selfdestruct_contract_initial_balance,
        )

    tx = Transaction(
        value=entry_code_balance,
        data=entry_code,
        sender=sender,
        to=None,
    )

    entry_code_address = tx.created_contract

    post: Dict[Address, Account] = {
        entry_code_address: Account(
            storage=entry_code_storage,
        ),
        selfdestruct_contract_address: Account.NONEXISTENT,  # type: ignore
        sendall_recipient_addresses[0]: Account(
            balance=sendall_amount, storage={0: 1}
        ),
    }

    if fork.is_eip_enabled(7708):
        expected_logs = []
        # tx value transfer: sender -> entry_code_address (created contract)
        if entry_code_balance > 0:
            expected_logs.append(
                transfer_log(sender, entry_code_address, entry_code_balance)
            )
        # Initcode SELFDESTRUCT sends pre-existing balance to the recipient.
        if selfdestruct_contract_initial_balance > 0:
            expected_logs.append(
                transfer_log(
                    selfdestruct_contract_address,
                    sendall_recipient_addresses[0],
                    selfdestruct_contract_initial_balance,
                )
            )
        # CALLs to the destroyed contract transfer ETH to it.
        for i in range(call_times):
            if i > 0:
                expected_logs.append(
                    transfer_log(
                        entry_code_address, selfdestruct_contract_address, i
                    )
                )
        # At finalization the (destroyed) contract has the accumulated
        # post-SELFDESTRUCT balance, which is burned.
        if entry_code_balance > 0:
            expected_logs.append(
                burn_log(selfdestruct_contract_address, entry_code_balance)
            )
        tx.expected_receipt = TransactionReceipt(logs=expected_logs)

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 12 parametrized test cases across 5 forks.