Skip to content

test_extcodehash_created_and_deleted()

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

Generate fixtures for these test cases for Amsterdam with:

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

Test EXTCODEHASH of an account created and selfdestructed in same tx.

CREATE2 a contract with SELFDESTRUCT code, check EXTCODEHASH, EXTCODESIZE, and EXTCODECOPY before and after triggering it. Within the transaction, the values remain unchanged. With CALL the created contract is deleted at end of transaction; with STATICCALL the trigger fails and the contract persists.

Source code in tests/constantinople/eip1052_extcodehash/test_extcodehash.py
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/extCodeHashCreatedAndDeletedAccountFiller.json",  # noqa: E501
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/extCodeHashCreatedAndDeletedAccountCallFiller.json",  # noqa: E501
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/extCodeHashCreatedAndDeletedAccountStaticCallFiller.json",  # noqa: E501
    ],
    pr=["https://github.com/ethereum/execution-specs/pull/2416"],
)
@pytest.mark.parametrize(
    "trigger",
    [
        pytest.param(Op.CALL, id="call"),
        pytest.param(Op.STATICCALL, id="staticcall"),
    ],
)
def test_extcodehash_created_and_deleted(
    state_test: StateTestFiller,
    pre: Alloc,
    trigger: Opcodes,
) -> None:
    """
    Test EXTCODEHASH of an account created and selfdestructed in same tx.

    CREATE2 a contract with SELFDESTRUCT code, check EXTCODEHASH,
    EXTCODESIZE, and EXTCODECOPY before and after triggering it. Within
    the transaction, the values remain unchanged. With CALL the created
    contract is deleted at end of transaction; with STATICCALL the
    trigger fails and the contract persists.
    """
    storage = Storage()
    runtime = Op.SELFDESTRUCT(0)
    initcode = Initcode(deploy_code=runtime)
    salt = 0x10
    expected_hash = runtime.keccak256()
    expected_size = len(runtime)

    created_slot = storage.store_next(0)
    code = Bytecode()

    # Store initcode in memory and CREATE2
    code += Op.MSTORE(
        0,
        Op.PUSH32(bytes(initcode).ljust(32, b"\0")),
    ) + Op.SSTORE(
        created_slot,
        Op.CREATE2(value=0, offset=0, size=len(initcode), salt=salt),
    )

    target = Op.SLOAD(created_slot)

    def extcode_checks() -> Bytecode:
        return (
            Op.SSTORE(
                storage.store_next(expected_hash),
                Op.EXTCODEHASH(target),
            )
            + Op.SSTORE(
                storage.store_next(expected_size),
                Op.EXTCODESIZE(target),
            )
            + Op.EXTCODECOPY(target, 0, 0, 32)
            + Op.SSTORE(
                storage.store_next(bytes(runtime).ljust(32, b"\0")),
                Op.MLOAD(0),
            )
        )

    code += extcode_checks()
    code += trigger(address=target, gas=0x10000) + Op.POP
    code += extcode_checks()

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

    created = compute_create2_address(
        address=code_address,
        salt=salt,
        initcode=initcode,
    )
    storage[created_slot] = created

    tx = Transaction(sender=pre.fund_eoa(), to=code_address)

    post: dict[Address, Account | None] = {
        code_address: Account(storage=storage),
    }
    if trigger == Op.CALL:
        post[created] = Account.NONEXISTENT
    else:
        post[created] = Account(code=runtime)

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 2 parametrized test cases across 10 forks.