Skip to content

test_selfdestruct_zero_balance_no_account_write()

Documentation for tests/amsterdam/eip8038_state_access_gas_cost_increase/test_selfdestruct_gas.py::test_selfdestruct_zero_balance_no_account_write@343274cc.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8038_state_access_gas_cost_increase/test_selfdestruct_gas.py::test_selfdestruct_zero_balance_no_account_write --fork Amsterdam

SELFDESTRUCT with a zero-balance destructor charges no ACCOUNT_WRITE.

No value is transferred, so even a non-existent beneficiary is not created: execution = OPCODE_SELFDESTRUCT_BASE + access and no state gas is charged.

Source code in tests/amsterdam/eip8038_state_access_gas_cost_increase/test_selfdestruct_gas.py
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
@EIPChecklist.GasCostChanges.Test.GasUpdatesMeasurement()
@pytest.mark.parametrize("warm", [False, True], ids=["cold", "warm"])
def test_selfdestruct_zero_balance_no_account_write(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    warm: bool,
) -> None:
    """
    SELFDESTRUCT with a zero-balance destructor charges no ACCOUNT_WRITE.

    No value is transferred, so even a non-existent beneficiary is not
    created: execution = ``OPCODE_SELFDESTRUCT_BASE + access`` and no
    state gas is charged.
    """
    beneficiary = Address(0xDEAD)  # non-existent, but no value sent

    destructor_code = _destructor_code(
        beneficiary, warm=warm, account_new=False
    )
    destructor = pre.deploy_contract(code=destructor_code, balance=0)

    caller_code = Op.POP(Op.CALL(gas=Op.GAS, address=destructor)) + Op.STOP
    caller = pre.deploy_contract(code=caller_code)

    access_list = (
        [AccessList(address=beneficiary, storage_keys=[])] if warm else None
    )
    intrinsic = fork.transaction_intrinsic_cost_calculator()(
        access_list=access_list
    )

    expected_gas_used = (
        intrinsic
        + caller_code.gas_cost(fork)
        + destructor_code.execution_cost(fork)
    )

    tx = Transaction(
        to=caller,
        sender=pre.fund_eoa(),
        access_list=access_list,
        state_gas_reservoir=0,
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=expected_gas_used
        ),
    )

    state_test(
        pre=pre,
        post={
            destructor: Account(balance=0, code=destructor_code),
            beneficiary: Account.NONEXISTENT,
        },
        tx=tx,
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.