Skip to content

test_dynamic_create2_selfdestruct_collision_multi_tx()

Documentation for tests/cancun/eip6780_selfdestruct/test_dynamic_create2_selfdestruct_collision.py::test_dynamic_create2_selfdestruct_collision_multi_tx@9c2813ee.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/cancun/eip6780_selfdestruct/test_dynamic_create2_selfdestruct_collision.py::test_dynamic_create2_selfdestruct_collision_multi_tx --fork Amsterdam

Dynamic Create2->Suicide->Create2 collision scenario over multiple transactions.

Perform a CREATE2, make sure that the initcode sets at least a couple of storage keys, then on a different call, in the same or different tx but same block, perform a self-destruct. Then: a) on the same tx, attempt to recreate the contract b) on a different tx, attempt to recreate the contract

Perform a CREATE2, make sure that the initcode sets at least a couple of storage keys, then in a different tx, perform a self-destruct.

Then

a) on the same tx, attempt to recreate the contract -> Covered in this test b) on a different tx, attempt to recreate the contract -> Covered in this test

Check the test case described in https://lf-hyperledger.atlassian.net/wiki/spaces/BESU/pages/22156575/2024-01-06 +Mainnet+Halting+Event

Source code in tests/cancun/eip6780_selfdestruct/test_dynamic_create2_selfdestruct_collision.py
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
@pytest.mark.valid_from("Paris")
@pytest.mark.parametrize(
    "selfdestruct_on_first_tx,recreate_on_first_tx",
    [
        (False, False),
        (True, False),
        (True, True),
    ],
)
def test_dynamic_create2_selfdestruct_collision_multi_tx(
    fork: Fork,
    selfdestruct_on_first_tx: bool,
    recreate_on_first_tx: bool,
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
) -> None:
    """
    Dynamic Create2->Suicide->Create2 collision scenario over multiple
    transactions.

    Perform a CREATE2, make sure that the initcode sets at least a couple of
    storage keys, then on a different call, in the same or different tx but
    same block, perform a self-destruct.
    Then:
      a) on the same tx, attempt to recreate the contract
      b) on a different tx, attempt to recreate the contract

    Perform a CREATE2, make sure that the initcode sets at least a
    couple of storage keys, then in a different tx, perform a self-destruct.

    Then:
      a) on the same tx, attempt to recreate the contract
         -> Covered in this test
      b) on a different tx, attempt to recreate the contract
         -> Covered in this test

    Check the test case described in
    https://lf-hyperledger.atlassian.net/wiki/spaces/BESU/pages/22156575/2024-01-06
    +Mainnet+Halting+Event
    """
    if recreate_on_first_tx:
        assert selfdestruct_on_first_tx, "invalid test"

    # Storage locations
    create2_constructor_worked = 1
    first_create2_result = 2
    second_create2_result = 3
    part_1_worked = 4
    part_2_worked = 5

    # Constants
    create2_salt = 1

    # Create EOA for sendall destination (receives selfdestruct funds)
    sendall_destination = pre.fund_eoa(0)  # Will be funded by selfdestruct
    # calls

    # Create storage contract that will be called during initialization
    address_create2_storage = pre.deploy_contract(
        code=Op.SSTORE(1, 1),
        balance=7000000000000000000,
    )

    # CREATE2 Initcode
    deploy_code = Op.SELFDESTRUCT(sendall_destination)
    initcode = Initcode(
        deploy_code=deploy_code,
        initcode_prefix=Op.SSTORE(create2_constructor_worked, 1)
        + Op.CALL(Op.GAS(), address_create2_storage, 0, 0, 0, 0, 0),
    )

    # Create the contract that performs CREATE2 operations
    address_code = pre.deploy_contract(
        code=Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
        + Op.MSTORE(
            0,
            Op.CREATE2(Op.SELFBALANCE(), 0, Op.CALLDATASIZE(), create2_salt),
        )
        + Op.RETURN(0, 32),
    )

    # Created addresses
    create2_address = compute_create2_address(
        address_code, create2_salt, initcode
    )

    # Values
    first_create2_value = 3
    first_call_value = 5
    second_create2_value = 7
    second_call_value = 11

    # Code is divided in two transactions part of the same block
    first_tx_code = Bytecode()
    second_tx_code = Bytecode()

    first_tx_code += (
        Op.JUMPDEST()
        # Make a subcall that do CREATE2 and returns its the result
        + Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
        + Op.CALL(
            100000,
            address_code,
            first_create2_value,
            0,
            Op.CALLDATASIZE(),
            0,
            32,
        )
        + Op.SSTORE(
            first_create2_result,
            Op.MLOAD(0),
        )
    )

    if selfdestruct_on_first_tx:
        first_tx_code += (
            # Call to the created account to trigger selfdestruct
            Op.CALL(100000, create2_address, first_call_value, 0, 0, 0, 0)
        )
    else:
        second_tx_code += (
            # Call to the created account to trigger selfdestruct
            Op.CALL(100000, create2_address, first_call_value, 0, 0, 0, 0)
        )

    if recreate_on_first_tx:
        first_tx_code += (
            # Make a subcall that do CREATE2 collision and returns its address
            # as the result
            Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
            + Op.CALL(
                100000,
                address_code,
                second_create2_value,
                0,
                Op.CALLDATASIZE(),
                0,
                32,
            )
            + Op.SSTORE(
                second_create2_result,
                Op.MLOAD(0),
            )
        )

    else:
        second_tx_code += (
            # Make a subcall that do CREATE2 collision and returns its address
            # as the result
            Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE())
            + Op.CALL(
                100000,
                address_code,
                second_create2_value,
                0,
                Op.CALLDATASIZE(),
                0,
                32,
            )
            + Op.SSTORE(
                second_create2_result,
                Op.MLOAD(0),
            )
        )

    # Second tx code always calls the create2 contract at the end
    second_tx_code += Op.CALL(
        100000, create2_address, second_call_value, 0, 0, 0, 0
    )

    first_tx_code += Op.SSTORE(part_1_worked, 1)
    second_tx_code += Op.SSTORE(part_2_worked, 1)

    # Create the main contract that uses conditional logic to handle both
    # transactions
    address_to = pre.deploy_contract(
        code=Conditional(
            # Depending on the tx, execute the first or second tx code
            condition=Op.EQ(Op.SLOAD(part_1_worked), 0),
            if_true=first_tx_code,
            if_false=second_tx_code,
        ),
        balance=100000000,
        storage={first_create2_result: 0xFF, second_create2_result: 0xFF},
    )

    # Create the sender EOA
    sender = pre.fund_eoa(7000000000000000000)

    post: Dict[Address, Union[Account, object]] = {}

    # Create2 address only exists if it was pre-existing and after cancun
    account_will_exist_with_code = (
        not selfdestruct_on_first_tx and fork >= Cancun
    )
    # If the contract is self-destructed and we also attempt to recreate it on
    # the first tx, the second call on the second tx will only place balance in
    # the account
    account_will_exist_with_balance = (
        selfdestruct_on_first_tx and recreate_on_first_tx
    )

    post[create2_address] = (
        Account(
            balance=0,
            nonce=1,
            code=deploy_code,
            storage={create2_constructor_worked: 0x01},
        )
        if account_will_exist_with_code
        else (
            Account(balance=second_call_value, nonce=0)
            if account_will_exist_with_balance
            else Account.NONEXISTENT
        )
    )

    # Create2 initcode saves storage unconditionally
    post[address_create2_storage] = Account(
        storage={create2_constructor_worked: 0x01}
    )

    # Entry code that makes the calls to the create2 contract creator
    post[address_to] = Account(
        storage={
            part_1_worked: 0x01,
            part_2_worked: 0x01,
            # First create2 always works
            first_create2_result: create2_address,
            # Second create2 only works if we successfully self-destructed on
            # the first tx
            second_create2_result: (
                create2_address
                if selfdestruct_on_first_tx and not recreate_on_first_tx
                else 0x00
            ),
        }
    )

    # Calculate the destination account expected balance for the
    # selfdestruct/sendall calls
    sendall_destination_balance = first_create2_value + first_call_value

    if not account_will_exist_with_balance:
        sendall_destination_balance += second_call_value

    if selfdestruct_on_first_tx and not recreate_on_first_tx:
        sendall_destination_balance += second_create2_value

    post[sendall_destination] = Account(balance=sendall_destination_balance)

    blockchain_test(
        pre=pre,
        post=post,
        blocks=[
            Block(
                txs=[
                    Transaction(
                        to=address_to,
                        data=initcode,
                        gas_limit=5_000_000,
                        sender=sender,
                    ),
                    Transaction(
                        to=address_to,
                        data=initcode,
                        gas_limit=5_000_000,
                        sender=sender,
                    ),
                ]
            )
        ],
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 6 forks.