Skip to content

test_self_destructing_initcode()

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

Generate fixtures for these test cases for Prague with:

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

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
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
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
@pytest.mark.parametrize("create_opcode", [Op.CREATE, Op.CREATE2])
@pytest.mark.parametrize("call_times", [0, 1])
@pytest.mark.parametrize("selfdestruct_contract_initial_balance", [0, 100_000])
@pytest.mark.valid_from("Shanghai")
def test_self_destructing_initcode(
    state_test: StateTestFiller,
    env: Environment,
    pre: Alloc,
    sender: EOA,
    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,
):
    """
    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(0),
            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,
        gas_limit=500_000,
    )

    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}),
    }

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

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) selfdestruct_contract_initial_balance call_times create_opcode
...fork_Shanghai-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE 0 0 CREATE
...fork_Shanghai-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE2 0 0 CREATE2
...fork_Shanghai-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE 0 1 CREATE
...fork_Shanghai-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE2 0 1 CREATE2
...fork_Shanghai-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE 100000 0 CREATE
...fork_Shanghai-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE2 100000 0 CREATE2
...fork_Shanghai-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE 100000 1 CREATE
...fork_Shanghai-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE2 100000 1 CREATE2
...fork_Shanghai-state_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE 0 0 CREATE
...fork_Shanghai-state_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE2 0 0 CREATE2
...fork_Shanghai-state_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE 0 1 CREATE
...fork_Shanghai-state_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE2 0 1 CREATE2
...fork_Shanghai-state_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE 100000 0 CREATE
...fork_Shanghai-state_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE2 100000 0 CREATE2
...fork_Shanghai-state_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE 100000 1 CREATE
...fork_Shanghai-state_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE2 100000 1 CREATE2
...fork_Cancun-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE 0 0 CREATE
...fork_Cancun-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE2 0 0 CREATE2
...fork_Cancun-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE 0 1 CREATE
...fork_Cancun-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE2 0 1 CREATE2
...fork_Cancun-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE 100000 0 CREATE
...fork_Cancun-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE2 100000 0 CREATE2
...fork_Cancun-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE 100000 1 CREATE
...fork_Cancun-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE2 100000 1 CREATE2
...fork_Cancun-state_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE 0 0 CREATE
...fork_Cancun-state_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE2 0 0 CREATE2
...fork_Cancun-state_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE 0 1 CREATE
...fork_Cancun-state_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE2 0 1 CREATE2
...fork_Cancun-state_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE 100000 0 CREATE
...fork_Cancun-state_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE2 100000 0 CREATE2
...fork_Cancun-state_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE 100000 1 CREATE
...fork_Cancun-state_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE2 100000 1 CREATE2
...fork_Prague-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE 0 0 CREATE
...fork_Prague-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE2 0 0 CREATE2
...fork_Prague-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE 0 1 CREATE
...fork_Prague-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE2 0 1 CREATE2
...fork_Prague-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE 100000 0 CREATE
...fork_Prague-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE2 100000 0 CREATE2
...fork_Prague-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE 100000 1 CREATE
...fork_Prague-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE2 100000 1 CREATE2
...fork_Prague-state_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE 0 0 CREATE
...fork_Prague-state_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE2 0 0 CREATE2
...fork_Prague-state_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE 0 1 CREATE
...fork_Prague-state_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE2 0 1 CREATE2
...fork_Prague-state_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE 100000 0 CREATE
...fork_Prague-state_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE2 100000 0 CREATE2
...fork_Prague-state_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE 100000 1 CREATE
...fork_Prague-state_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE2 100000 1 CREATE2
...fork_Osaka-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE 0 0 CREATE
...fork_Osaka-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE2 0 0 CREATE2
...fork_Osaka-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE 0 1 CREATE
...fork_Osaka-blockchain_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE2 0 1 CREATE2
...fork_Osaka-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE 100000 0 CREATE
...fork_Osaka-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE2 100000 0 CREATE2
...fork_Osaka-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE 100000 1 CREATE
...fork_Osaka-blockchain_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE2 100000 1 CREATE2
...fork_Osaka-state_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE 0 0 CREATE
...fork_Osaka-state_test-selfdestruct_contract_initial_balance_0-call_times_0-create_opcode_CREATE2 0 0 CREATE2
...fork_Osaka-state_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE 0 1 CREATE
...fork_Osaka-state_test-selfdestruct_contract_initial_balance_0-call_times_1-create_opcode_CREATE2 0 1 CREATE2
...fork_Osaka-state_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE 100000 0 CREATE
...fork_Osaka-state_test-selfdestruct_contract_initial_balance_100000-call_times_0-create_opcode_CREATE2 100000 0 CREATE2
...fork_Osaka-state_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE 100000 1 CREATE
...fork_Osaka-state_test-selfdestruct_contract_initial_balance_100000-call_times_1-create_opcode_CREATE2 100000 1 CREATE2