Skip to content

test_selfdestruct_codebearing_zero_balance_beneficiary_no_account_write()

Documentation for tests/amsterdam/eip8038_state_access_gas_cost_increase/test_selfdestruct_gas.py::test_selfdestruct_codebearing_zero_balance_beneficiary_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_codebearing_zero_balance_beneficiary_no_account_write --fork Amsterdam

SELFDESTRUCT to a code-bearing zero-balance beneficiary: no ACCOUNT_WRITE.

The beneficiary is alive because it has code, not balance: it holds a zero balance but a non-empty code (Op.STOP), so EIP-161 emptiness does not apply and no account is created when a positive balance is sent to it. Execution = OPCODE_SELFDESTRUCT_BASE + (COLD_ACCOUNT_ACCESS if cold) with no ACCOUNT_WRITE and no state gas — distinct from the alive-via-balance case, which exercises the same path through a different liveness source.

Source code in tests/amsterdam/eip8038_state_access_gas_cost_increase/test_selfdestruct_gas.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
@EIPChecklist.GasCostChanges.Test.GasUpdatesMeasurement()
@pytest.mark.parametrize("warm", [False, True], ids=["cold", "warm"])
def test_selfdestruct_codebearing_zero_balance_beneficiary_no_account_write(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    warm: bool,
) -> None:
    """
    SELFDESTRUCT to a code-bearing zero-balance beneficiary: no
    ACCOUNT_WRITE.

    The beneficiary is alive because it has code, not balance: it holds a
    zero balance but a non-empty code (``Op.STOP``), so EIP-161 emptiness
    does not apply and no account is created when a positive balance is
    sent to it. Execution = ``OPCODE_SELFDESTRUCT_BASE +
    (COLD_ACCOUNT_ACCESS if cold)`` with no ACCOUNT_WRITE and no state
    gas — distinct from the
    alive-via-balance case, which exercises the same path through a
    different liveness source.
    """
    # Alive via code (non-empty code), with zero balance.
    beneficiary = pre.deploy_contract(code=Op.STOP, balance=0)

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

    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 must include the access-list cost that warms the
    # beneficiary; pass the list so the calculator folds it in.
    intrinsic = fork.transaction_intrinsic_cost_calculator()(
        access_list=access_list
    )

    # Pure execution: intrinsic + caller frame + destructor frame (whose
    # execution_cost folds the SELFDESTRUCT charge and beneficiary PUSH).
    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,
        # EIP-6780: the pre-deployed destructor is not same-tx-created,
        # so it is not deleted; its balance still transfers.
        post={
            destructor: Account(balance=0, code=destructor_code),
            # Code-bearing beneficiary credited the destructor balance.
            beneficiary: Account(balance=1, code=Op.STOP),
        },
        tx=tx,
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.