Skip to content

test_extcodehash_call_to_selfdestruct()

Documentation for tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_extcodehash_call_to_selfdestruct@b47f0253.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/constantinople/eip1052_extcodehash/test_extcodehash.py::test_extcodehash_call_to_selfdestruct --fork Amsterdam

Test EXTCODEHASH after calling a contract that selfdestructs.

Call a contract containing SELFDESTRUCT using each call type, then check EXTCODEHASH. The hash is always returned because the check happens within the same transaction. STATICCALL fails because SELFDESTRUCT modifies state. Pre-Cancun, CALLCODE/DELEGATECALL execute SELFDESTRUCT in the caller's context, destroying the test contract at end of transaction.

Source code in tests/constantinople/eip1052_extcodehash/test_extcodehash.py
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
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
@pytest.mark.ported_from(
    [
        "https://github.com/ethereum/tests/blob/v13.3/src/GeneralStateTestsFiller/stExtCodeHash/callToSuicideThenExtcodehashFiller.json",  # noqa: E501
    ],
    pr=["https://github.com/ethereum/execution-specs/pull/2412"],
)
@pytest.mark.with_all_call_opcodes
def test_extcodehash_call_to_selfdestruct(
    state_test: StateTestFiller,
    pre: Alloc,
    fork: Fork,
    call_opcode: Opcodes,
) -> None:
    """
    Test EXTCODEHASH after calling a contract that selfdestructs.

    Call a contract containing SELFDESTRUCT using each call type, then
    check EXTCODEHASH. The hash is always returned because the check
    happens within the same transaction. STATICCALL fails because
    SELFDESTRUCT modifies state. Pre-Cancun, CALLCODE/DELEGATECALL
    execute SELFDESTRUCT in the caller's context, destroying the test
    contract at end of transaction.
    """
    storage = Storage()
    beneficiary = pre.nonexistent_account()
    target_code = Op.SELFDESTRUCT(beneficiary)
    target = pre.deploy_contract(target_code, balance=5_555_555_555)

    call_succeeds = call_opcode != Op.STATICCALL

    code = Op.SSTORE(
        storage.store_next(int(call_succeeds)),
        call_opcode(address=target, gas=165_000),
    ) + Op.SSTORE(
        storage.store_next(target_code.keccak256()),
        Op.EXTCODEHASH(target),
    )

    code_address = pre.deploy_contract(code, storage=storage.canary())

    tx = Transaction(
        sender=pre.fund_eoa(),
        to=code_address,
        gas_limit=400_000,
    )

    # Pre-Cancun, CALLCODE/DELEGATECALL execute SELFDESTRUCT in the
    # caller's context, destroying the test contract at end of tx.
    caller_destroyed = fork < Cancun and call_opcode in (
        Op.CALLCODE,
        Op.DELEGATECALL,
    )

    post: dict[Address, Account | None] = {}
    if caller_destroyed:
        post[code_address] = Account.NONEXISTENT
    else:
        post[code_address] = Account(storage=storage)

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

Parametrized Test Cases

This test generates 4 parametrized test cases across 10 forks.