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@c74f1a67.

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 storage, 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
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
@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 storage,
    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
    salt = 0

    storage = Storage()
    contract = pre.deploy_contract(
        code=(
            Op.MSTORE(
                0,
                int.from_bytes(bytes(init_code), "big")
                << (256 - 8 * len(init_code)),
            )
            # First CREATE2 succeeds
            + Op.SSTORE(
                storage.store_next(1, "first_create2"),
                Op.ISZERO(Op.ISZERO(Op.CREATE2(0, 0, len(init_code), salt))),
            )
            # Second CREATE2 with same salt collides
            + Op.SSTORE(
                storage.store_next(0, "collision_create2"),
                Op.CREATE2(0, 0, len(init_code), salt),
            )
        ),
    )

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

    post = {contract: Account(storage=storage)}
    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.