Skip to content

test_extcodehash_self_in_init()

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

Generate fixtures for these test cases for Amsterdam with:

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

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
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
@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,
            gas_limit=400_000,
        )
        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,
            gas_limit=400_000,
        )
        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 10 forks.