Skip to content

test_extcodehash_dynamic_argument()

Documentation for tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_extcodehash_dynamic_argument@b314d18e.

Generate fixtures for these test cases for Amsterdam with:

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

Test EXTCODEHASH/EXTCODESIZE with address loaded dynamically from calldata.

The target address is not hardcoded in bytecode but read via CALLDATALOAD at runtime. Five target types are tested: a precompile with no state, a precompile with balance, a contract with code, an EOA with balance, and a non-existent address.

Source code in tests/constantinople/eip1052_extcodehash/test_extcodehash.py
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
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/extCodeHashDynamicArgumentFiller.json",  # noqa: E501
    ],
    pr=["https://github.com/ethereum/execution-specs/pull/2379"],
)
@pytest.mark.parametrize(
    "target_type",
    [
        "precompile",
        "precompile_with_balance",
        "contract",
        "eoa",
        "nonexistent",
    ],
)
def test_extcodehash_dynamic_argument(
    state_test: StateTestFiller,
    pre: Alloc,
    target_type: str,
) -> None:
    """
    Test EXTCODEHASH/EXTCODESIZE with address loaded dynamically from calldata.

    The target address is not hardcoded in bytecode but read via
    CALLDATALOAD at runtime. Five target types are tested: a precompile
    with no state, a precompile with balance, a contract with code,
    an EOA with balance, and a non-existent address.
    """
    storage = Storage()
    target_code = b"\x12\x34"

    if target_type == "precompile":
        target_address = Address(1)
        expected_hash: int | bytes = 0
        expected_size = 0
    elif target_type == "precompile_with_balance":
        target_address = Address(2)
        pre.fund_address(target_address, 1)
        expected_hash = keccak256(b"")
        expected_size = 0
    elif target_type == "contract":
        target_address = pre.deploy_contract(target_code)
        expected_hash = keccak256(target_code)
        expected_size = len(target_code)
    elif target_type == "eoa":
        target_address = pre.fund_eoa(amount=1)
        expected_hash = keccak256(b"")
        expected_size = 0
    else:  # nonexistent
        target_address = pre.fund_eoa(amount=0)
        expected_hash = 0
        expected_size = 0

    code = Op.SSTORE(
        storage.store_next(expected_hash),
        Op.EXTCODEHASH(Op.CALLDATALOAD(0)),
    ) + Op.SSTORE(
        storage.store_next(expected_size),
        Op.EXTCODESIZE(Op.CALLDATALOAD(0)),
    )

    code_address = pre.deploy_contract(code, storage=storage.canary())

    tx = Transaction(
        sender=pre.fund_eoa(),
        to=code_address,
        data=bytes(target_address).rjust(32, b"\0"),
    )

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

Parametrized Test Cases

This test generates 5 parametrized test cases across 10 forks.