Skip to content

test_extcodehash_self_in_init()

Documentation for tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_extcodehash_self_in_init@ffe48572.

Generate fixtures for these test cases for Bogota with:

fill -v tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_extcodehash_self_in_init --fork Bogota

Test EXTCODEHASH/EXTCODESIZE of self during init code.

During init code execution the account exists but has no code yet, so EXTCODEHASH(ADDRESS) returns keccak256("") and EXTCODESIZE(ADDRESS) returns 0.

Source code in tests/constantinople/eip1052_extcodehash/test_extcodehash.py
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/extCodeHashSelfInInitFiller.json",  # noqa: E501
    ],
)
@pytest.mark.parametrize(
    "create_opcode",
    [pytest.param(None, id="create_tx"), Op.CREATE, Op.CREATE2],
)
def test_extcodehash_self_in_init(
    state_test: StateTestFiller,
    pre: Alloc,
    create_opcode: Opcodes | None,
) -> None:
    """
    Test EXTCODEHASH/EXTCODESIZE of self during init code.

    During init code execution the account exists but has no code yet,
    so EXTCODEHASH(ADDRESS) returns keccak256("") and
    EXTCODESIZE(ADDRESS) returns 0.
    """
    storage = Storage()

    expected_hash = keccak256(b"")
    expected_size = 0

    checks = Op.SSTORE(
        storage.store_next(expected_hash),
        Op.EXTCODEHASH(Op.ADDRESS),
    ) + Op.SSTORE(
        storage.store_next(expected_size),
        Op.EXTCODESIZE(Op.ADDRESS),
    )
    initcode = checks + Op.RETURN(0, 0)

    if create_opcode is None:
        sender = pre.fund_eoa()
        tx = Transaction(
            sender=sender,
            to=None,
            data=initcode,
        )
        created = compute_create_address(
            address=sender,
            nonce=0,
        )
    else:
        factory_code = (
            Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE)
            + create_opcode(
                value=0,
                offset=0,
                size=Op.CALLDATASIZE,
            )
            + Op.STOP
        )
        factory = pre.deploy_contract(factory_code)
        tx = Transaction(
            sender=pre.fund_eoa(),
            to=factory,
            data=initcode,
        )
        created = compute_create_address(
            address=factory,
            nonce=1,
            salt=0,
            initcode=initcode,
            opcode=create_opcode,
        )

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

Parametrized Test Cases

This test generates 3 parametrized test cases across 11 forks.