Skip to content

test_extcodehash_created_and_deleted_recheck_outer()

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

Generate fixtures for these test cases for Amsterdam with:

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

Test EXTCODEHASH of a created-and-selfdestructed account rechecked from an outer call frame.

Outer contract CALLs inner, which CREATE2s a contract with SELFDESTRUCT code then triggers it. After inner returns, outer re-checks EXTCODEHASH and EXTCODESIZE of the created address. Within the transaction all checks return the original code hash and size. The created contract is deleted at end of transaction.

Source code in tests/constantinople/eip1052_extcodehash/test_extcodehash.py
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/extCodeHashCreatedAndDeletedAccountRecheckInOuterCallFiller.json",  # noqa: E501
    ],
    pr=["https://github.com/ethereum/execution-specs/pull/2428"],
)
def test_extcodehash_created_and_deleted_recheck_outer(
    state_test: StateTestFiller,
    pre: Alloc,
) -> None:
    """
    Test EXTCODEHASH of a created-and-selfdestructed account rechecked
    from an outer call frame.

    Outer contract CALLs inner, which CREATE2s a contract with
    SELFDESTRUCT code then triggers it. After inner returns, outer
    re-checks EXTCODEHASH and EXTCODESIZE of the created address.
    Within the transaction all checks return the original code hash
    and size. The created contract is deleted at end of transaction.
    """
    inner_storage = Storage()
    outer_storage = Storage()

    runtime = Op.SELFDESTRUCT(0)
    initcode = Initcode(deploy_code=runtime)
    salt = 0x10
    expected_hash = runtime.keccak256()
    expected_size = len(runtime)

    # Inner contract: CREATE2, check, trigger SELFDESTRUCT, re-check.
    created_slot = inner_storage.store_next(0)
    inner_code = Bytecode()
    inner_code += Om.MSTORE(initcode, 0) + Op.SSTORE(
        created_slot,
        Op.CREATE2(value=0, offset=0, size=len(initcode), salt=salt),
    )

    target = Op.SLOAD(created_slot)
    expected_code = bytes(runtime).ljust(32, b"\0")

    def inner_extcode_checks() -> Bytecode:
        return (
            Op.SSTORE(
                inner_storage.store_next(expected_hash),
                Op.EXTCODEHASH(target),
            )
            + Op.SSTORE(
                inner_storage.store_next(expected_size),
                Op.EXTCODESIZE(target),
            )
            + Op.EXTCODECOPY(target, 0, 0, 32)
            + Op.SSTORE(
                inner_storage.store_next(expected_code),
                Op.MLOAD(0),
            )
        )

    inner_code += inner_extcode_checks()
    inner_code += Op.CALL(address=target, gas=Op.GAS) + Op.POP
    inner_code += inner_extcode_checks()
    inner = pre.deploy_contract(inner_code, storage=inner_storage.canary())

    created = compute_create2_address(
        address=inner,
        salt=salt,
        initcode=initcode,
    )
    inner_storage[created_slot] = created

    # Outer contract: CALL inner, then re-check the created address.
    outer_code = (
        Op.CALL(address=inner, gas=Op.GAS)
        + Op.POP
        + Op.SSTORE(
            outer_storage.store_next(expected_hash),
            Op.EXTCODEHASH(created),
        )
        + Op.SSTORE(
            outer_storage.store_next(expected_size),
            Op.EXTCODESIZE(created),
        )
        + Op.EXTCODECOPY(created, 0, 0, 32)
        + Op.SSTORE(
            outer_storage.store_next(expected_code),
            Op.MLOAD(0),
        )
    )
    outer = pre.deploy_contract(outer_code, storage=outer_storage.canary())

    tx = Transaction(
        sender=pre.fund_eoa(),
        to=outer,
        gas_limit=400_000,
    )

    post: dict[Address, Account | None] = {
        inner: Account(storage=inner_storage),
        outer: Account(storage=outer_storage),
        created: Account.NONEXISTENT,
    }

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

Parametrized Test Cases

This test generates 1 parametrized test case across 10 forks.