Skip to content

test_extcodehash_subcall_create2_oog()

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

Generate fixtures for these test cases for Amsterdam with:

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

Test EXTCODEHASH after CREATE2 in a subcall that goes out of gas.

A factory contract creates a contract via CREATE2, then either returns normally or consumes all remaining gas. When the subcall OOGs, the entire frame reverts — including the CREATE2 — so the created contract should not exist. The caller checks EXTCODEHASH/EXTCODESIZE/EXTCODECOPY of the expected CREATE2 address.

Source code in tests/constantinople/eip1052_extcodehash/test_extcodehash.py
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/extCodeHashSubcallOOGFiller.yml",  # noqa: E501
    ],
    pr=[
        "https://github.com/ethereum/execution-specs/pull/2458",
    ],
)
@pytest.mark.parametrize(
    "call_opcode",
    [
        pytest.param(Op.CALL, id="call"),
        pytest.param(Op.CALLCODE, id="callcode"),
        pytest.param(Op.DELEGATECALL, id="delegatecall"),
    ],
)
@pytest.mark.parametrize("oog", [False, True], ids=["success", "oog"])
def test_extcodehash_subcall_create2_oog(
    state_test: StateTestFiller,
    pre: Alloc,
    call_opcode: Opcodes,
    oog: bool,
) -> None:
    """
    Test EXTCODEHASH after CREATE2 in a subcall that goes out of gas.

    A factory contract creates a contract via CREATE2, then either
    returns normally or consumes all remaining gas. When the subcall
    OOGs, the entire frame reverts — including the CREATE2 — so the
    created contract should not exist. The caller checks
    EXTCODEHASH/EXTCODESIZE/EXTCODECOPY of the expected CREATE2 address.
    """
    storage = Storage()
    deploy_code = Op.SSTORE(0x20, 0x20)
    deploy_code_bytes = bytes(deploy_code)
    initcode = Initcode(deploy_code=deploy_code)

    # Factory: CREATE2, optionally consume all gas to trigger OOG.
    factory_code = Om.MSTORE(initcode, 0) + Op.MSTORE(
        0, Op.CREATE2(value=0, offset=0, size=len(initcode), salt=0)
    )
    if oog:
        factory_code += Om.OOG
    factory_code += Op.RETURN(0, 32)

    factory = pre.deploy_contract(factory_code)

    # Pass the pre-computed CREATE2 address as calldata so the test
    # does not depend on return data from a potentially OOG'd subcall.
    hash_slot = storage.store_next(0, "extcodehash")
    size_slot = storage.store_next(0, "extcodesize")
    code_slot = storage.store_next(0, "extcodecopy")
    code = (
        Op.SSTORE(
            storage.store_next(int(not oog), "call_result"),
            call_opcode(
                address=factory,
                gas=200_000,
                ret_offset=0,
                ret_size=32,
            ),
        )
        + Op.SSTORE(hash_slot, Op.EXTCODEHASH(Op.CALLDATALOAD(0)))
        + Op.SSTORE(size_slot, Op.EXTCODESIZE(Op.CALLDATALOAD(0)))
        + Op.EXTCODECOPY(Op.CALLDATALOAD(0), 0, 0, 32)
        + Op.SSTORE(code_slot, Op.MLOAD(0))
    )

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

    # Compute the CREATE2 address to verify existence in post-state.
    if call_opcode == Op.CALL:
        deployer = factory
    else:
        # CALLCODE/DELEGATECALL: factory code runs in caller's context.
        deployer = code_address
    created = compute_create2_address(
        address=deployer, salt=0, initcode=initcode
    )

    if not oog:
        storage[hash_slot] = keccak256(deploy_code_bytes)
        storage[size_slot] = len(deploy_code)
        storage[code_slot] = deploy_code_bytes.ljust(32, b"\0")

    post: dict[Address, Account | None] = {
        code_address: Account(storage=storage),
    }
    if oog:
        post[created] = Account.NONEXISTENT
    else:
        post[created] = Account(nonce=1, code=deploy_code)

    tx = Transaction(
        sender=pre.fund_eoa(),
        to=code_address,
        gas_limit=500_000,
        data=created.rjust(32, b"\0"),
    )

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

Parametrized Test Cases

This test generates 6 parametrized test cases across 10 forks.