Skip to content

test_selfdestruct_account_write_at_transition()

Documentation for tests/amsterdam/eip8038_state_access_gas_cost_increase/test_fork_transition.py::test_selfdestruct_account_write_at_transition@ffe48572.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8038_state_access_gas_cost_increase/test_fork_transition.py::test_selfdestruct_account_write_at_transition --fork Amsterdam

SELFDESTRUCT gains an ACCOUNT_WRITE charge when it sends a positive balance to an empty account, while the beneficiary-creation charge it already paid moves from execution gas onto the state axis.

SELFDESTRUCT halts its own frame, so the charge is pinned from outside, through the receipt. The receipt sums the two gas dimensions (unlike the block header, which takes their maximum and would let the dominating creation state gas mask the execution side), so one pin covers both the added ACCOUNT_WRITE and the relocated creation charge.

Source code in tests/amsterdam/eip8038_state_access_gas_cost_increase/test_fork_transition.py
@EIPChecklist.GasCostChanges.Test.ForkTransition.Before()
@EIPChecklist.GasCostChanges.Test.ForkTransition.After()
def test_selfdestruct_account_write_at_transition(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    ``SELFDESTRUCT`` gains an ``ACCOUNT_WRITE`` charge when it sends a
    positive balance to an empty account, while the beneficiary-creation
    charge it already paid moves from execution gas onto the state axis.

    ``SELFDESTRUCT`` halts its own frame, so the charge is pinned from
    outside, through the receipt. The receipt sums the two gas dimensions
    (unlike the block header, which takes their maximum and would let the
    dominating creation state gas mask the execution side), so one pin
    covers both the added ``ACCOUNT_WRITE`` and the relocated creation
    charge.
    """
    before = fork.fork_at(timestamp=BEFORE_TS)
    after = fork.fork_at(timestamp=AFTER_TS)

    # Fresh empty beneficiaries so the positive-balance-to-empty branch
    # that adds ACCOUNT_WRITE is taken in each regime.
    beneficiary_before = pre.fund_eoa(amount=0)
    beneficiary_after = pre.fund_eoa(amount=0)

    selfdestruct = Op.SELFDESTRUCT.with_metadata(
        address_warm=False, account_new=True
    )
    code_before = selfdestruct(beneficiary_before)
    code_after = selfdestruct(beneficiary_after)

    # Before the fork the creation charge is execution gas and there is no
    # state axis; after it, the creation charge is state gas while the
    # execution charge gains ACCOUNT_WRITE.
    assert code_before.state_cost(before) == 0
    assert code_after.state_cost(after) > 0
    assert code_after.execution_cost(after) > Op.SELFDESTRUCT.with_metadata(
        address_warm=False, account_new=False
    ).execution_cost(after)
    # EIP-6780 keeps the pre-existing originator alive, so no state-gas
    # refund muddies either receipt.
    assert code_before.refund(before) == 0
    assert code_after.refund(after) == 0

    suicidal_before = pre.deploy_contract(code=code_before, balance=1)
    suicidal_after = pre.deploy_contract(code=code_after, balance=1)

    blocks = transition_blocks(
        suicidal_before,
        suicidal_after,
        pre,
        before_gas_used=_cumulative_gas_used(code_before, before),
        after_gas_used=_cumulative_gas_used(code_after, after),
    )

    post = {
        beneficiary_before: Account(balance=1),
        beneficiary_after: Account(balance=1),
    }
    blockchain_test(pre=pre, blocks=blocks, post=post)

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.