Skip to content

test_codecopy_zero_in_create2()

Documentation for tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_codecopy_zero_in_create2@b47f0253.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_codecopy_zero_in_create2 --fork Amsterdam

Test CODECOPY inside CREATE2 initcode that deploys empty code.

The initcode does CODECOPY(0,0,32) which copies the first 32 bytes of the initcode itself (not the deployed code). It then checks EXTCODESIZE(ADDRESS) and EXTCODEHASH(ADDRESS) of self during init, which see the account as having empty code. The deployed contract retains the storage set during init.

Source code in tests/constantinople/eip1052_extcodehash/test_extcodehash.py
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/codeCopyZero_ParisFiller.yml",  # noqa: E501
    ],
)
def test_codecopy_zero_in_create2(
    state_test: StateTestFiller,
    pre: Alloc,
) -> None:
    """
    Test CODECOPY inside CREATE2 initcode that deploys empty code.

    The initcode does CODECOPY(0,0,32) which copies the first 32 bytes
    of the initcode itself (not the deployed code). It then checks
    EXTCODESIZE(ADDRESS) and EXTCODEHASH(ADDRESS) of self during init,
    which see the account as having empty code. The deployed contract
    retains the storage set during init.
    """
    storage = Storage()

    # Build the initcode that queries itself and deploys empty code.
    # During init: CODECOPY copies the initcode, EXTCODESIZE(self)=0,
    # EXTCODEHASH(self)=keccak256("").
    initcode = (
        Op.CODECOPY(0, 0, 32)
        + Op.SSTORE(0x50, Op.MLOAD(0))
        + Op.SSTORE(0x51, Op.EXTCODESIZE(Op.ADDRESS))
        + Op.SSTORE(0x52, Op.EXTCODEHASH(Op.ADDRESS))
        + Op.SSTORE(
            0x53,
            Op.EXTCODESIZE(Op.CALLCODE(50_000, Op.ADDRESS, 0, 0, 0, 0, 0)),
        )
        + Op.EXTCODECOPY(Op.ADDRESS, 0, 0, 32)
        + Op.SSTORE(0x54, Op.MLOAD(0))
        # Return empty code (size 0).
        + Op.RETURN(0, 0)
    )

    # Factory: CREATE2 and return the created address.
    factory_code = (
        Om.MSTORE(initcode, 0)
        + Op.MSTORE(
            0,
            Op.CREATE2(value=0, offset=0, size=len(initcode), salt=0),
        )
        + Op.RETURN(0, 32)
    )

    factory = pre.deploy_contract(factory_code, balance=10**18)

    # Caller: invoke factory and store created address.
    caller_code = Op.CALL(550_000, factory, 0, 0, 0, 0, 32) + Op.SSTORE(
        storage.store_next(0, "created_address"), Op.MLOAD(0)
    )

    caller = pre.deploy_contract(caller_code, storage=storage.canary())

    created = compute_create2_address(
        address=factory, salt=0, initcode=initcode
    )
    storage[0] = created

    # First 32 bytes of initcode — what CODECOPY(0,0,32) returns.
    initcode_word0 = bytes(initcode)[:32]

    tx = Transaction(
        sender=pre.fund_eoa(),
        to=caller,
        gas_limit=1_400_000,
    )

    state_test(
        pre=pre,
        post={
            caller: Account(storage=storage),
            created: Account(
                nonce=1,
                code=b"",
                storage={
                    0x50: initcode_word0,
                    0x51: 0,
                    0x52: keccak256(b""),
                    0x53: 0,
                    0x54: 0,
                },
            ),
        },
        tx=tx,
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 10 forks.