Skip to content

test_selfdestruct_send_to_sender()

Documentation for tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py::test_selfdestruct_send_to_sender@b47f0253.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py::test_selfdestruct_send_to_sender --fork Amsterdam

SELFDESTRUCT on pre-existing contract where beneficiary is tx.sender.

Alice calls victim directly; victim runs SELFDESTRUCT(CALLER). The beneficiary coincides with tx.sender, so alice's BAL entry coalesces nonce_changes (as sender) with balance_changes (as beneficiary). Pre-Cancun: victim destroyed. >=Cancun: victim preserved with balance 0 (EIP-6780 — victim was not created same-tx).

Source code in tests/tangerine_whistle/eip150_operation_gas_costs/test_eip150_selfdestruct.py
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
@pytest.mark.parametrize(
    "originator_balance",
    [0, 100],
    ids=["no_balance", "has_balance"],
)
@pytest.mark.valid_from("TangerineWhistle")
def test_selfdestruct_send_to_sender(
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    fork: Fork,
    originator_balance: int,
) -> None:
    """
    SELFDESTRUCT on pre-existing contract where beneficiary is tx.sender.

    Alice calls victim directly; victim runs `SELFDESTRUCT(CALLER)`. The
    beneficiary coincides with tx.sender, so alice's BAL entry coalesces
    `nonce_changes` (as sender) with `balance_changes` (as beneficiary).
    Pre-Cancun: victim destroyed. >=Cancun: victim preserved with balance 0
    (EIP-6780 — victim was not created same-tx).
    """
    alice_initial_balance = 10**18
    alice = pre.fund_eoa(amount=alice_initial_balance)
    victim_code = Op.SELFDESTRUCT(
        Op.CALLER, address_warm=True, account_new=False
    )
    victim = pre.deploy_contract(code=victim_code, balance=originator_balance)

    gas_price = 0xA
    gas_limit = 100_000
    tx = Transaction(
        sender=alice,
        to=victim,
        gas_limit=gas_limit,
        gas_price=gas_price,
        protected=fork.supports_protected_txs(),
    )

    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        calldata=b"", contract_creation=False
    )
    execution_gas = victim_code.gas_cost(fork)
    alice_final_balance = (
        alice_initial_balance
        + originator_balance
        - (intrinsic_gas + execution_gas) * gas_price
    )

    expected_bal: BlockAccessListExpectation | None = None
    if fork.is_eip_enabled(7928):
        alice_expectation = BalAccountExpectation(
            nonce_changes=[BalNonceChange(block_access_index=1, post_nonce=1)],
            balance_changes=[
                BalBalanceChange(
                    block_access_index=1, post_balance=alice_final_balance
                )
            ],
        )
        if originator_balance > 0:
            victim_expectation = BalAccountExpectation(
                balance_changes=[
                    BalBalanceChange(block_access_index=1, post_balance=0)
                ],
                code_changes=[],
                nonce_changes=[],
                storage_changes=[],
                storage_reads=[],
            )
        else:
            victim_expectation = BalAccountExpectation.empty()
        expected_bal = BlockAccessListExpectation(
            account_expectations={
                alice: alice_expectation,
                victim: victim_expectation,
            }
        )

    contract_destroyed = fork < Cancun
    if contract_destroyed:
        post: Dict[Address | EOA, Account | object] = {
            alice: Account(nonce=1),
            victim: Account.NONEXISTENT,
        }
    else:
        post = {
            alice: Account(nonce=1),
            victim: Account(balance=0, code=victim_code),
        }

    blockchain_test(
        pre=pre,
        blocks=[Block(txs=[tx], expected_block_access_list=expected_bal)],
        post=post,
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 12 forks.