Skip to content

test_selfdestruct_to_different_address_same_tx()

Documentation for tests/amsterdam/eip7708_eth_transfer_logs/test_burn_logs.py::test_selfdestruct_to_different_address_same_tx@b47f0253.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip7708_eth_transfer_logs/test_burn_logs.py::test_selfdestruct_to_different_address_same_tx --fork Amsterdam

Test same-tx selfdestruct to different address.

With balance: Transfer log emitted. Zero balance: no logs.

Source code in tests/amsterdam/eip7708_eth_transfer_logs/test_burn_logs.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
@pytest.mark.parametrize(
    "contract_balance",
    [
        pytest.param(2000, id="with_balance"),
        pytest.param(0, id="zero_balance"),
    ],
)
@pytest.mark.with_all_create_opcodes
def test_selfdestruct_to_different_address_same_tx(
    state_test: StateTestFiller,
    env: Environment,
    pre: Alloc,
    sender: EOA,
    contract_balance: int,
    create_opcode: Op,
) -> None:
    """
    Test same-tx selfdestruct to different address.

    With balance: Transfer log emitted. Zero balance: no logs.
    """
    beneficiary = pre.deploy_contract(Op.STOP)

    initcode = Op.SELFDESTRUCT(beneficiary)
    initcode_bytes = bytes(initcode)
    initcode_len = len(initcode_bytes)

    factory_code = Op.MSTORE(
        0, Op.PUSH32(initcode_bytes.rjust(32, b"\x00"))
    ) + create_opcode(
        value=Op.CALLVALUE, offset=32 - initcode_len, size=initcode_len
    )

    factory = pre.deploy_contract(factory_code)
    created_address = compute_create_address(
        address=factory,
        nonce=1,
        salt=0,
        initcode=initcode_bytes,
        opcode=create_opcode,
    )

    if contract_balance > 0:
        expected_logs = [
            transfer_log(sender, factory, contract_balance),
            transfer_log(factory, created_address, contract_balance),
            transfer_log(created_address, beneficiary, contract_balance),
        ]
        post = {beneficiary: Account(balance=contract_balance)}
    else:
        expected_logs = []
        post = {}

    tx = Transaction(
        sender=sender,
        to=factory,
        value=contract_balance,
        gas_limit=200_000,
        expected_receipt=TransactionReceipt(logs=expected_logs),
    )

    state_test(env=env, pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.