Skip to content

test_calling_from_new_contract_to_pre_existing_contract()

Documentation for tests/cancun/eip6780_selfdestruct/test_selfdestruct.py::test_calling_from_new_contract_to_pre_existing_contract@b47f0253.

Generate fixtures for these test cases for Amsterdam with:

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

Test that if an account created in the current transaction delegate-call a previously created account that executes self-destruct, the calling account is deleted.

Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct.py
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
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
@pytest.mark.parametrize("call_times", [1])
@pytest.mark.parametrize("selfdestruct_contract_initial_balance", [0, 1])
@pytest.mark.parametrize("call_opcode", [Op.DELEGATECALL, Op.CALLCODE])
@pytest.mark.parametrize("create_opcode", [Op.CREATE])
@pytest.mark.valid_from("Shanghai")
def test_calling_from_new_contract_to_pre_existing_contract(
    state_test: StateTestFiller,
    pre: Alloc,
    sender: EOA,
    fork: Fork,
    sendall_recipient_addresses: List[Address],
    create_opcode: Op,
    call_opcode: Op,
    call_times: int,
    selfdestruct_contract_initial_balance: int,
) -> None:
    """
    Test that if an account created in the current transaction delegate-call a
    previously created account that executes self-destruct, the calling account
    is deleted.
    """
    pre_existing_selfdestruct_address = pre.deploy_contract(
        selfdestruct_code_preset(
            sendall_recipient_addresses=sendall_recipient_addresses,
        ),
    )
    # Our entry point is an initcode that in turn creates a self-destructing
    # contract
    entry_code_storage = Storage()
    sendall_amount = 0

    entry_code_address = compute_create_address(address=sender, nonce=0)
    selfdestruct_contract_address = compute_create_address(
        address=entry_code_address, nonce=1
    )

    if selfdestruct_contract_initial_balance > 0:
        pre.fund_address(
            selfdestruct_contract_address,
            selfdestruct_contract_initial_balance,
        )

    # self-destructing call
    selfdestruct_code = call_opcode(address=pre_existing_selfdestruct_address)
    selfdestruct_contract_initcode = Initcode(deploy_code=selfdestruct_code)
    initcode_copy_from_address = pre.deploy_contract(
        selfdestruct_contract_initcode
    )

    # 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 in range(call_times):
        entry_code += Op.SSTORE(
            entry_code_storage.store_next(1),
            Op.CALL(
                Op.GASLIMIT,  # Gas
                selfdestruct_contract_address,  # Address
                i,  # Value
                0,
                0,
                0,
                0,
            ),
        )
        entry_code_balance += i
        sendall_amount += i

        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)

    if selfdestruct_contract_initial_balance > 0:
        # Address where the contract is created already had some balance,
        # which must be included in the send-all operation
        sendall_amount += selfdestruct_contract_initial_balance

    post: Dict[Address, Account] = {
        entry_code_address: Account(
            storage=entry_code_storage,
        ),
        selfdestruct_contract_address: Account.NONEXISTENT,  # type: ignore
        sendall_recipient_addresses[0]: Account(
            balance=sendall_amount, storage={0: 1}
        ),
    }

    tx = Transaction(
        value=entry_code_balance,
        data=entry_code,
        sender=sender,
        to=None,
        gas_limit=500_000,
    )

    if fork.is_eip_enabled(7708):
        # The new contract's body is a DELEGATECALL/CALLCODE to the pre-
        # existing selfdestruct code, so SELFDESTRUCT runs in the NEW
        # contract's context and transfers its balance to the recipient.
        expected_logs = []
        if entry_code_balance > 0:
            expected_logs.append(
                transfer_log(sender, entry_code_address, entry_code_balance)
            )
        running_balance = selfdestruct_contract_initial_balance
        for i in range(call_times):
            if i > 0:
                expected_logs.append(
                    transfer_log(
                        entry_code_address, selfdestruct_contract_address, i
                    )
                )
            running_balance += i
            if running_balance > 0:
                expected_logs.append(
                    transfer_log(
                        selfdestruct_contract_address,
                        sendall_recipient_addresses[0],
                        running_balance,
                    )
                )
            running_balance = 0
        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.