Skip to content

test_selfdestruct_clears_nonce_and_storage()

Documentation for tests/cancun/eip6780_selfdestruct/test_selfdestruct_finalization.py::test_selfdestruct_clears_nonce_and_storage@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/cancun/eip6780_selfdestruct/test_selfdestruct_finalization.py::test_selfdestruct_clears_nonce_and_storage --fork Amsterdam

Cases 13-16 of the EIP-8246 test list: a contract created in this transaction raises its nonce or writes storage before self-destructing. It is deleted, or under EIP-8246 has its nonce reset to zero and its storage cleared while it keeps its balance.

Source code in tests/cancun/eip6780_selfdestruct/test_selfdestruct_finalization.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
@pytest.mark.parametrize(
    "beneficiary",
    [pytest.param("self", id="self"), pytest.param("other", id="other")],
)
@pytest.mark.parametrize(
    "initial_balance",
    [pytest.param(0, id="zero_balance"), pytest.param(3, id="funded")],
)
@pytest.mark.parametrize(
    "bump_nonce,write_storage",
    [
        pytest.param(True, False, id="nonce"),
        pytest.param(False, True, id="storage"),
        pytest.param(True, True, id="nonce_and_storage"),
    ],
)
def test_selfdestruct_clears_nonce_and_storage(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    beneficiary: str,
    initial_balance: int,
    bump_nonce: bool,
    write_storage: bool,
) -> None:
    """
    Cases 13-16 of the EIP-8246 test list: a contract created in this
    transaction raises its nonce or writes storage before self-destructing.
    It is deleted, or under EIP-8246 has its nonce reset to zero and its
    storage cleared while it keeps its balance.
    """
    sender = pre.fund_eoa()
    other = pre.fund_eoa(amount=OTHER_BALANCE)

    victim_code = Bytecode()
    if bump_nonce:
        victim_code += Op.POP(Op.CREATE(0, 0, 0)) * 2
    if write_storage:
        victim_code += Op.SSTORE(0, 1) + Op.SSTORE(1, 2)
    if beneficiary == "self":
        victim_code += Op.SELFDESTRUCT(Op.ADDRESS)
    elif beneficiary == "other":
        victim_code += Op.SELFDESTRUCT(other)
    else:
        raise ValueError(f"unhandled beneficiary {beneficiary}")
    initcode = Initcode(deploy_code=victim_code)

    entry = pre.deploy_contract(
        code=Om.MSTORE(initcode, 0)
        + Op.SSTORE(
            0, Op.CREATE(value=initial_balance, offset=0, size=len(initcode))
        )
        + Op.SSTORE(1, Op.CALL(gas=Op.GAS, address=Op.SLOAD(0)))
        + Op.STOP
    )
    victim = compute_create_address(address=entry, nonce=1)

    tx = Transaction(sender=sender, to=entry, value=initial_balance)

    if beneficiary == "self":
        victim_balance = initial_balance
        other_balance = OTHER_BALANCE
    elif beneficiary == "other":
        victim_balance = 0
        other_balance = OTHER_BALANCE + initial_balance
    else:
        raise ValueError(f"unhandled beneficiary {beneficiary}")

    logs: list[TransactionLog] = []
    if initial_balance > 0:
        logs.append(transfer_log(sender, entry, initial_balance))
        logs.append(transfer_log(entry, victim, initial_balance))
        if beneficiary == "other":
            logs.append(transfer_log(victim, other, initial_balance))
    if fork.is_eip_enabled(7708):
        tx.expected_receipt = TransactionReceipt(logs=logs)

    expected_bal = None
    if fork.is_eip_enabled(7928):
        storage_reads = [0, 1] if write_storage else []
        expected_bal = BlockAccessListExpectation(
            account_expectations={
                victim: finalized_bal(fork, victim_balance, storage_reads)
            }
        )

    post: dict[Address, Account | None] = {
        entry: Account(storage={0: victim, 1: 1}),
        victim: finalized(fork, victim_balance),
        other: Account(balance=other_balance),
    }
    if bump_nonce:
        for child_nonce in (1, 2):
            child = compute_create_address(address=victim, nonce=child_nonce)
            post[child] = Account(nonce=1, code=b"", balance=0)

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

Parametrized Test Cases

This test generates 12 parametrized test cases across 4 forks.