Skip to content

test_create2_address_collision()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create2_address_collision@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create2_address_collision --fork Amsterdam

Test CREATE2 returns zero on address collision.

When CREATE2 targets an address that already has code or a non-zero nonce (EIP-684), the collision is detected early and returns zero without charging state gas. The existing account is left unchanged.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
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
@pytest.mark.valid_from("EIP8037")
def test_create2_address_collision(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Test CREATE2 returns zero on address collision.

    When CREATE2 targets an address that already has code or a
    non-zero nonce (EIP-684), the collision is detected early and
    returns zero without charging state gas. The existing account is
    left unchanged.
    """
    gas_limit_cap = fork.transaction_gas_limit_cap()
    assert gas_limit_cap is not None
    init_code = Op.STOP
    mstore_value, size = init_code_at_high_bytes(init_code)
    salt = 0

    storage = Storage()
    factory_prefix_code = (
        Op.MSTORE(0, mstore_value, new_memory_size=32)
        # First CREATE
        + Op.SSTORE(
            storage.store_next(1),
            Op.ISZERO(
                Op.ISZERO(
                    Op.CREATE2(
                        0,
                        0,
                        size,
                        salt,
                        # gas accounting
                        init_code_size=size,
                        account_new=True,
                    )
                )
            ),
            # gas accounting
            original_value=0,
            new_value=1,
        )
    )
    factory_create_code = Op.CREATE2(
        0,
        0,
        size,
        salt,
        # gas accounting
        init_code_size=size,
        account_new=False,
    )
    factory_code = factory_prefix_code + factory_create_code
    contract = pre.deploy_contract(code=factory_code)
    collision_target = compute_create2_address(
        address=contract, salt=salt, initcode=bytes(init_code)
    )

    state_gas = factory_prefix_code.state_cost(fork)
    # The collision burns all but a 64th of the factory's execution
    # gas, so half again its own cost keeps the trailing SSTORE alive.
    # Execution gas is at most what the limit has left once the state
    # charge is paid, so the assert keeps that burn under the state gas.
    gas_limit = (
        (
            fork.transaction_intrinsic_cost_calculator()()
            + factory_code.gas_cost(fork)
        )
        * 3
        // 2
    )
    assert gas_limit - state_gas < state_gas, "state gas must dominate"

    tx = Transaction(
        to=contract,
        gas_limit=gas_limit,
        sender=pre.fund_eoa(),
    )

    post = {
        contract: Account(storage=storage),
        collision_target: Account(nonce=1, code=b""),
    }

    state_test(
        pre=pre,
        post=post,
        tx=tx,
        blockchain_test_header_verify=Header(gas_used=state_gas),
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.