Skip to content

test_selfdestruct_not_created_in_same_tx_with_revert()

Documentation for tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py::test_selfdestruct_not_created_in_same_tx_with_revert@b47f0253.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py::test_selfdestruct_not_created_in_same_tx_with_revert --fork Amsterdam

Same test as selfdestruct_created_in_same_tx_with_revert except selfdestructable contract is pre-existing.

Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
@pytest.mark.parametrize(
    "recursive_revert_contract_address_init_balance",
    [2],
    ids=["init_balance_2"],
)
@pytest.mark.parametrize(
    "same_tx",
    [False],
    ids=["not_same_tx"],
)
@pytest.mark.parametrize(
    "selfdestruct_on_outer_call",
    [0, 1, 2],
    ids=[
        "no_outer_selfdestruct",
        "outer_selfdestruct_before_inner_call",
        "outer_selfdestruct_after_inner_call",
    ],
)
@pytest.mark.valid_from("Cancun")
def test_selfdestruct_not_created_in_same_tx_with_revert(
    state_test: StateTestFiller,
    sender: EOA,
    env: Environment,
    entry_code_address: Address,
    pre: Alloc,
    selfdestruct_on_outer_call: int,
    selfdestruct_with_transfer_contract_code: Bytecode,
    selfdestruct_with_transfer_contract_address: Address,
    selfdestruct_recipient_address: Address,
    recursive_revert_contract_address: Address,
    recursive_revert_contract_code: Bytecode,
) -> None:
    """
    Same test as selfdestruct_created_in_same_tx_with_revert except
    selfdestructable contract is pre-existing.
    """
    entry_code = Op.CALL(
        Op.GASLIMIT(),
        recursive_revert_contract_address,
        0,  # value
        0,  # arg offset
        0,  # arg length
        0,  # ret offset
        0,  # ret length
    )

    post: Dict[Address, Account] = {
        entry_code_address: Account(code="0x"),
    }

    if selfdestruct_on_outer_call > 0:
        post[selfdestruct_with_transfer_contract_address] = Account(
            balance=1 if selfdestruct_on_outer_call == 1 else 0,
            code=selfdestruct_with_transfer_contract_code,
            storage=Storage(
                {
                    # 2 value transfers: 1 in outer call, 1 in reverted inner
                    # call
                    0: 1,  # type: ignore
                    # 1 selfdestruct in reverted inner call
                    1: 1,  # type: ignore
                }
            ),
        )
        post[selfdestruct_recipient_address] = Account(
            balance=1 if selfdestruct_on_outer_call == 1 else 2
        )
    else:
        post[selfdestruct_with_transfer_contract_address] = Account(
            balance=1,
            code=selfdestruct_with_transfer_contract_code,
            storage=Storage(
                {
                    # 2 value transfers:
                    #   1 in outer call, 1 in reverted inner call
                    0: 1,  # type: ignore
                    # 2 selfdestructs:
                    #   1 in outer call, 1 in reverted inner call
                    1: 0,  # type: ignore
                }
            ),
        )
        post[selfdestruct_recipient_address] = Account.NONEXISTENT  # type: ignore

    tx = Transaction(
        value=0,
        data=entry_code,
        sender=sender,
        to=None,
        gas_limit=500_000,
    )

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

Parametrized Test Cases

This test generates 3 parametrized test cases across 4 forks.