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@9c2813ee.

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
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
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
@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)
            )

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

    # Contract was created and destroyed in same tx
    post: dict = {
        alice: Account(nonce=1),
        caller: Account(nonce=2),
        victim: Account.NONEXISTENT,
    }

    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 12 forks.