Skip to content

test_initcode_selfdestruct_to_self()

Documentation for tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py::test_initcode_selfdestruct_to_self@343274cc.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py::test_initcode_selfdestruct_to_self --fork Amsterdam

Test SELFDESTRUCT during initcode execution where beneficiary is self.

Unlike test_selfdestruct_to_self, this tests the case where the initcode itself executes SELFDESTRUCT(ADDRESS) during contract creation, before any code is deployed.

Key characteristics: - During initcode, the contract has no code yet - Contract has nonce=1 (post-EIP-161) making it non-empty - Beneficiary is always warm (it's the executing contract) - No NEW_ACCOUNT charge (contract has nonce > 0) - No cold access charge (>=Berlin)

Note: Gas boundary testing not possible for initcode since CREATE doesn't accept a gas parameter - it uses all available gas.

Source code in tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
@pytest.mark.parametrize(
    "originator_balance",
    [0, 1],
    ids=["no_balance", "has_balance"],
)
@pytest.mark.valid_from("TangerineWhistle")
def test_initcode_selfdestruct_to_self(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
    originator_balance: int,
) -> None:
    """
    Test SELFDESTRUCT during initcode execution where beneficiary is self.

    Unlike test_selfdestruct_to_self, this tests the case where the initcode
    itself executes SELFDESTRUCT(ADDRESS) during contract creation, before
    any code is deployed.

    Key characteristics:
    - During initcode, the contract has no code yet
    - Contract has nonce=1 (post-EIP-161) making it non-empty
    - Beneficiary is always warm (it's the executing contract)
    - No NEW_ACCOUNT charge (contract has nonce > 0)
    - No cold access charge (>=Berlin)

    Note: Gas boundary testing not possible for initcode since CREATE
    doesn't accept a gas parameter - it uses all available gas.
    """
    alice = pre.fund_eoa()
    initcode = Op.SELFDESTRUCT(Op.ADDRESS)
    initcode_len = len(initcode)

    factory_code = Om.MSTORE(initcode, 0) + Op.CREATE(
        value=originator_balance, offset=0, size=initcode_len
    )
    caller = pre.deploy_contract(code=factory_code, balance=originator_balance)
    victim = compute_create_address(address=caller, nonce=1)

    tx = Transaction(
        sender=alice,
        to=caller,
        gas_limit=500_000,
        protected=fork.supports_protected_txs(),
    )

    # Build BAL expectations
    expected_bal: BlockAccessListExpectation | None = None
    if fork.is_eip_enabled(7928):
        # Contract created and immediately destroyed - no net changes
        # for victim
        caller_expectation = BalAccountExpectation(
            nonce_changes=[BalNonceChange(block_access_index=1, post_nonce=2)],
        )
        if originator_balance > 0:
            caller_expectation.balance_changes.append(
                BalBalanceChange(block_access_index=1, post_balance=0)
            )

        # per EIP-8246
        if fork.is_eip_enabled(8246) and originator_balance > 0:
            victim_initcode_expectation = BalAccountExpectation(
                balance_changes=[
                    BalBalanceChange(
                        block_access_index=1,
                        post_balance=originator_balance,
                    )
                ],
            )
        else:
            victim_initcode_expectation = BalAccountExpectation.empty()

        expected_bal = BlockAccessListExpectation(
            account_expectations={
                alice: BalAccountExpectation(
                    nonce_changes=[
                        BalNonceChange(block_access_index=1, post_nonce=1)
                    ],
                ),
                caller: caller_expectation,
                victim: victim_initcode_expectation,
            }
        )

    # per EIP-8246
    victim_post: Account | None
    if fork.is_eip_enabled(8246) and originator_balance > 0:
        victim_post = Account(
            balance=originator_balance, nonce=0, code=b"", storage={}
        )
    else:
        victim_post = Account.NONEXISTENT
    post: dict = {
        alice: Account(nonce=1),
        caller: Account(nonce=2),
        victim: victim_post,
    }

    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], expected_block_access_list=expected_bal)],
        post=post,
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 14 forks.