Skip to content

test_selfdestruct_to_self_in_create_tx()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py::test_selfdestruct_to_self_in_create_tx@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py::test_selfdestruct_to_self_in_create_tx --fork Amsterdam

Test SELFDESTRUCT to self in the transaction the contract was created.

When a contract created in the current transaction SELFDESTRUCTs to itself, the balance stays at the cleared account. No new account state gas is charged for the sweep since the beneficiary already exists: the CREATE paid it, and the clearing does not refill it.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_selfdestruct.py
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
@pytest.mark.valid_from("EIP8037")
def test_selfdestruct_to_self_in_create_tx(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Test SELFDESTRUCT to self in the transaction the contract was created.

    When a contract created in the current transaction SELFDESTRUCTs
    to itself, the balance stays at the cleared account. No new account
    state gas is charged for the sweep since the beneficiary already
    exists: the CREATE paid it, and the clearing does not refill it.
    """
    inner_code = Op.SELFDESTRUCT(
        Op.ADDRESS,
        # gas accounting
        address_warm=True,
        account_new=False,
    )
    mstore_value, size = init_code_at_high_bytes(inner_code)

    code = Op.MSTORE(0, mstore_value) + Op.POP(
        Op.CREATE(1, 0, size, init_code_size=size, new_memory_size=32)
    )
    contract = pre.deploy_contract(code=code, balance=1)
    created = compute_create_address(address=contract, nonce=1)

    expected_state = code.state_cost(fork)

    tx = Transaction(
        to=contract,
        state_gas_reservoir=expected_state,
        sender=pre.fund_eoa(),
    )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx],
                header_verify=Header(gas_used=expected_state),
            ),
        ],
        post={
            contract: Account(balance=0, nonce=2),
            created: Account(balance=1, nonce=0, code=b""),
        },
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.