Skip to content

test_create_selfdestruct_same_tx_increased_nonce()

Documentation for tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_create_selfdestruct_same_tx_increased_nonce@21fb11c8.

Generate fixtures for these test cases for Prague with:

fill -v tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_create_selfdestruct_same_tx_increased_nonce --fork Prague

Verify that a contract can self-destruct if it was created in the same transaction, even when its nonce has been increased due to contract creation.

Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct.py
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
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
@pytest.mark.parametrize("create_opcode", [Op.CREATE, Op.CREATE2])
@pytest.mark.parametrize("selfdestruct_contract_initial_balance", [0, 100_000])
@pytest.mark.parametrize(
    "call_times,sendall_recipient_addresses",
    [
        pytest.param(1, [PRE_DEPLOY_CONTRACT_1], id="single_call"),
        pytest.param(5, [PRE_DEPLOY_CONTRACT_1], id="multiple_calls_single beneficiary"),
    ],
    indirect=["sendall_recipient_addresses"],
)
@pytest.mark.valid_from("Shanghai")
def test_create_selfdestruct_same_tx_increased_nonce(
    state_test: StateTestFiller,
    env: Environment,
    pre: Alloc,
    sender: EOA,
    selfdestruct_code: Bytecode,
    sendall_recipient_addresses: List[Address],
    create_opcode: Op,
    call_times: int,
    selfdestruct_contract_initial_balance: int,
):
    """
    Verify that a contract can self-destruct if it was created in the same transaction, even when
    its nonce has been increased due to contract creation.
    """
    initcode = Op.RETURN(0, 1)
    selfdestruct_pre_bytecode = Op.MSTORE(0, Op.PUSH32(bytes(initcode))) + Op.POP(
        Op.CREATE(offset=32 - len(initcode), size=len(initcode))
    )
    selfdestruct_code = selfdestruct_pre_bytecode + selfdestruct_code
    selfdestruct_contract_initcode = Initcode(deploy_code=selfdestruct_code)
    initcode_copy_from_address = pre.deploy_contract(selfdestruct_contract_initcode)

    selfdestruct_contract_address = compute_create_address(
        address=compute_create_address(address=sender, nonce=0),
        nonce=1,
        initcode=selfdestruct_contract_initcode,
        opcode=create_opcode,
    )
    if selfdestruct_contract_initial_balance > 0:
        pre.fund_address(selfdestruct_contract_address, selfdestruct_contract_initial_balance)
    # Our entry point is an initcode that in turn creates a self-destructing contract
    entry_code_storage = Storage()

    # Create a dict to record the expected final balances
    sendall_final_balances = dict(
        zip(sendall_recipient_addresses, [0] * len(sendall_recipient_addresses), strict=False)
    )
    selfdestruct_contract_current_balance = selfdestruct_contract_initial_balance

    # Bytecode used to create the contract, can be CREATE or CREATE2
    create_bytecode = create_opcode(size=len(selfdestruct_contract_initcode))

    # Entry code that will be executed, creates the contract and then calls it in the same tx
    entry_code = (
        # Initcode is already deployed at `initcode_copy_from_address`, so just copy it
        Op.EXTCODECOPY(
            initcode_copy_from_address,
            0,
            0,
            len(selfdestruct_contract_initcode),
        )
        # And we store the created address for verification purposes
        + Op.SSTORE(
            entry_code_storage.store_next(selfdestruct_contract_address),
            create_bytecode,
        )
    )

    # Store the EXTCODE* properties of the created address
    entry_code += Op.SSTORE(
        entry_code_storage.store_next(len(selfdestruct_code)),
        Op.EXTCODESIZE(selfdestruct_contract_address),
    )

    entry_code += Op.SSTORE(
        entry_code_storage.store_next(selfdestruct_code.keccak256()),
        Op.EXTCODEHASH(selfdestruct_contract_address),
    )

    # Call the self-destructing contract multiple times as required, increasing the wei sent each
    # time
    entry_code_balance = 0
    for i, sendall_recipient in zip(range(call_times), cycle(sendall_recipient_addresses)):
        entry_code += Op.MSTORE(0, sendall_recipient)
        entry_code += Op.SSTORE(
            entry_code_storage.store_next(1),
            Op.CALL(
                Op.GASLIMIT,  # Gas
                selfdestruct_contract_address,  # Address
                i,  # Value
                0,
                32,
                0,
                0,
            ),
        )
        entry_code_balance += i
        selfdestruct_contract_current_balance += i

        # Balance is always sent to other contracts
        if sendall_recipient != selfdestruct_contract_address:
            sendall_final_balances[sendall_recipient] += selfdestruct_contract_current_balance

        # Self-destructing contract must always have zero balance after the call because the
        # self-destruct always happens in the same transaction in this test
        selfdestruct_contract_current_balance = 0

        entry_code += Op.SSTORE(
            entry_code_storage.store_next(0),
            Op.BALANCE(selfdestruct_contract_address),
        )

    # Check the EXTCODE* properties of the self-destructing contract again
    entry_code += Op.SSTORE(
        entry_code_storage.store_next(len(selfdestruct_code)),
        Op.EXTCODESIZE(selfdestruct_contract_address),
    )

    entry_code += Op.SSTORE(
        entry_code_storage.store_next(selfdestruct_code.keccak256()),
        Op.EXTCODEHASH(selfdestruct_contract_address),
    )

    # Lastly return zero so the entry point contract is created and we can retain the stored
    # values for verification.
    entry_code += Op.RETURN(max(len(selfdestruct_contract_initcode), 32), 1)

    tx = Transaction(
        value=entry_code_balance,
        data=entry_code,
        sender=sender,
        to=None,
        gas_limit=1_000_000,
    )

    entry_code_address = tx.created_contract

    post: Dict[Address, Account] = {
        entry_code_address: Account(
            code="0x00",
            storage=entry_code_storage,
        ),
        initcode_copy_from_address: Account(
            code=selfdestruct_contract_initcode,
        ),
    }

    # Check the balances of the sendall recipients
    for address, balance in sendall_final_balances.items():
        post[address] = Account(balance=balance, storage={0: 1})

    # Check the new contracts created from the self-destructing contract were correctly created.
    for address in [
        compute_create_address(address=selfdestruct_contract_address, nonce=i + 1)
        for i in range(call_times)
    ]:
        post[address] = Account(
            code=b"\x00",
        )

    post[selfdestruct_contract_address] = Account.NONEXISTENT  # type: ignore

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

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) sendall_recipient_addresses call_times selfdestruct_contract_initial_balance create_opcode
...fork_Shanghai-state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 1 0 CREATE
...fork_Shanghai-state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 0 CREATE2
...fork_Shanghai-state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 1 100000 CREATE
...fork_Shanghai-state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 100000 CREATE2
...fork_Shanghai-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 5 0 CREATE
...fork_Shanghai-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 0 CREATE2
...fork_Shanghai-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 5 100000 CREATE
...fork_Shanghai-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 100000 CREATE2
...fork_Shanghai-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 1 0 CREATE
...fork_Shanghai-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 0 CREATE2
...fork_Shanghai-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 1 100000 CREATE
...fork_Shanghai-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 100000 CREATE2
...fork_Shanghai-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 5 0 CREATE
...fork_Shanghai-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 0 CREATE2
...fork_Shanghai-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 5 100000 CREATE
...fork_Shanghai-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 100000 CREATE2
...fork_Cancun-state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 1 0 CREATE
...fork_Cancun-state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 0 CREATE2
...fork_Cancun-state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 1 100000 CREATE
...fork_Cancun-state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 100000 CREATE2
...fork_Cancun-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 5 0 CREATE
...fork_Cancun-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 0 CREATE2
...fork_Cancun-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 5 100000 CREATE
...fork_Cancun-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 100000 CREATE2
...fork_Cancun-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 1 0 CREATE
...fork_Cancun-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 0 CREATE2
...fork_Cancun-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 1 100000 CREATE
...fork_Cancun-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 100000 CREATE2
...fork_Cancun-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 5 0 CREATE
...fork_Cancun-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 0 CREATE2
...fork_Cancun-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 5 100000 CREATE
...fork_Cancun-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 100000 CREATE2
...fork_Prague-state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 1 0 CREATE
...fork_Prague-state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 0 CREATE2
...fork_Prague-state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 1 100000 CREATE
...fork_Prague-state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 100000 CREATE2
...fork_Prague-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 5 0 CREATE
...fork_Prague-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 0 CREATE2
...fork_Prague-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 5 100000 CREATE
...fork_Prague-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 100000 CREATE2
...fork_Prague-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 1 0 CREATE
...fork_Prague-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 0 CREATE2
...fork_Prague-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 1 100000 CREATE
...fork_Prague-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 100000 CREATE2
...fork_Prague-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 5 0 CREATE
...fork_Prague-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 0 CREATE2
...fork_Prague-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 5 100000 CREATE
...fork_Prague-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 100000 CREATE2
...fork_Osaka-state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 1 0 CREATE
...fork_Osaka-state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 0 CREATE2
...fork_Osaka-state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 1 100000 CREATE
...fork_Osaka-state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 100000 CREATE2
...fork_Osaka-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 5 0 CREATE
...fork_Osaka-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 0 CREATE2
...fork_Osaka-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 5 100000 CREATE
...fork_Osaka-state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 100000 CREATE2
...fork_Osaka-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 1 0 CREATE
...fork_Osaka-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 0 CREATE2
...fork_Osaka-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 1 100000 CREATE
...fork_Osaka-blockchain_test_from_state_test-single_call-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 1 100000 CREATE2
...fork_Osaka-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE ['pre_deploy_contract_1'] 5 0 CREATE
...fork_Osaka-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_0-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 0 CREATE2
...fork_Osaka-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE ['pre_deploy_contract_1'] 5 100000 CREATE
...fork_Osaka-blockchain_test_from_state_test-multiple_calls_single beneficiary-selfdestruct_contract_initial_balance_100000-create_opcode_CREATE2 ['pre_deploy_contract_1'] 5 100000 CREATE2