Skip to content

test_parent_creates_child_selfdestruct_one()

Documentation for tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_parent_creates_child_selfdestruct_one@b314d18e.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_parent_creates_child_selfdestruct_one --fork Amsterdam

Test a parent contract that creates a child contract, then only one of them self-destructs (either parent or child, based on parameter).

Both contracts are created in the same transaction: - If destroy_parent=True: Parent self-destructs, child survives - If destroy_parent=False: Child self-destructs, parent survives

Since both are created in the same tx, whichever self-destructs should be deleted.

Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct.py
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
@pytest.mark.parametrize("destroy_parent", [True, False])
@pytest.mark.parametrize("selfdestruct_contract_initial_balance", [0, 100_000])
@pytest.mark.valid_from("Shanghai")
def test_parent_creates_child_selfdestruct_one(
    state_test: StateTestFiller,
    pre: Alloc,
    sender: EOA,
    fork: Fork,
    destroy_parent: bool,
    selfdestruct_contract_initial_balance: int,
) -> None:
    """
    Test a parent contract that creates a child contract, then only one
    of them self-destructs (either parent or child, based on parameter).

    Both contracts are created in the same transaction:
    - If destroy_parent=True: Parent self-destructs, child survives
    - If destroy_parent=False: Child self-destructs, parent survives

    Since both are created in the same tx, whichever self-destructs should be
    deleted.
    """
    entry_code_storage = Storage()

    sendall_recipient = pre.deploy_contract(
        code=Op.SSTORE(0, 0),
        storage={0: 1},
    )

    # Child contract: just has code, self-destructs when called
    child_code = Op.SSTORE(0, Op.ADD(Op.SLOAD(0), 1)) + Op.SELFDESTRUCT(
        sendall_recipient
    )
    child_initcode = Initcode(deploy_code=child_code)

    # Parent contract: creates child, then either self-destructs or calls child
    # to self-destruct based on calldata[0]: 1 = destroy parent, 0 = destroy
    # child
    # For simplicity, use pre-deployed child initcode
    child_initcode_address = pre.deploy_contract(child_initcode)

    parent_code = (
        Op.SSTORE(0, Op.ADD(Op.SLOAD(0), 1))
        + Op.EXTCODECOPY(child_initcode_address, 0, 0, len(child_initcode))
        + Op.SSTORE(1, Op.CREATE(value=0, offset=0, size=len(child_initcode)))
        + Conditional(
            condition=Op.EQ(Op.CALLDATALOAD(0), 1),
            if_true=Op.SELFDESTRUCT(sendall_recipient),
            if_false=Op.CALL(Op.GASLIMIT, Op.SLOAD(1), 0, 0, 0, 0, 0),
        )
        + Op.STOP
    )
    parent_initcode = Initcode(deploy_code=parent_code)
    parent_initcode_address = pre.deploy_contract(parent_initcode)

    entry_code_address = compute_create_address(address=sender, nonce=0)
    parent_address = compute_create_address(
        address=entry_code_address,
        nonce=1,
        initcode=parent_initcode,
        opcode=Op.CREATE,
    )
    child_address = compute_create_address(address=parent_address, nonce=1)

    if selfdestruct_contract_initial_balance > 0:
        pre.fund_address(parent_address, selfdestruct_contract_initial_balance)
        pre.fund_address(child_address, selfdestruct_contract_initial_balance)

    # Entry code: create parent and call it with appropriate flag
    entry_code = Op.EXTCODECOPY(
        parent_initcode_address,
        0,
        0,
        len(parent_initcode),
    )

    entry_code += Op.SSTORE(
        entry_code_storage.store_next(parent_address),
        Op.CREATE(value=0, offset=0, size=len(parent_initcode)),
    )

    flag = 1 if destroy_parent else 0
    entry_code += Op.MSTORE(0, flag)
    entry_code += Op.SSTORE(
        entry_code_storage.store_next(1),
        Op.CALL(Op.GASLIMIT, parent_address, 0, 0, 32, 0, 0),
    )

    entry_code += Op.RETURN(32, 1)

    tx = Transaction(
        data=entry_code,
        sender=sender,
        to=None,
    )

    post: Dict[Address, Account] = {
        entry_code_address: Account(storage=entry_code_storage),
    }

    if destroy_parent:
        post[parent_address] = Account.NONEXISTENT  # type: ignore
        post[child_address] = Account(
            storage={0: 0},
            balance=selfdestruct_contract_initial_balance,
        )
        post[sendall_recipient] = Account(
            balance=selfdestruct_contract_initial_balance,
            storage={0: 1},
        )
    else:
        post[child_address] = Account.NONEXISTENT  # type: ignore
        post[parent_address] = Account(
            storage={0: 1, 1: child_address},
            balance=selfdestruct_contract_initial_balance,
        )
        post[sendall_recipient] = Account(
            balance=selfdestruct_contract_initial_balance,
            storage={0: 1},
        )

    if fork.is_eip_enabled(7708):
        # Only the SELFDESTRUCT that actually runs emits a log. Both parent
        # and child are pre-funded via pre.fund_address, so whichever
        # SELFDESTRUCTs to the shared recipient transfers its initial_balance.
        expected_logs = []
        if selfdestruct_contract_initial_balance > 0:
            source = parent_address if destroy_parent else child_address
            expected_logs.append(
                transfer_log(
                    source,
                    sendall_recipient,
                    selfdestruct_contract_initial_balance,
                )
            )
        tx.expected_receipt = TransactionReceipt(logs=expected_logs)

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

Parametrized Test Cases

This test generates 4 parametrized test cases across 5 forks.