Skip to content

test_finalization_burn_logs()

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

Generate fixtures for these test cases for Amsterdam with:

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

Test Burn logs at finalization for post-selfdestruct balance.

X contracts (x1, x2, x3) selfdestruct, then receive ETH via payer contracts (p1, p2, p3). At finalization, X contracts emit Burn logs for their in lexicographical address order (only if they received ETH).

When to_self=True, X contracts SELFDESTRUCT to themselves (burning ETH with LOG2). When to_self=False, X contracts SELFDESTRUCT to a beneficiary (Transfer LOG3).

Source code in tests/amsterdam/eip7708_eth_transfer_logs/test_burn_logs.py
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
@pytest.mark.parametrize(
    "payer_code,eth_transferred",
    [
        pytest.param(
            Op.SELFDESTRUCT(Op.CALLDATALOAD(0)),
            True,
            id="via_selfdestruct",
        ),
        pytest.param(
            Op.CALL(
                gas=50_000,
                address=Op.CALLDATALOAD(0),
                value=Op.BALANCE(Op.ADDRESS),
            ),
            True,
            id="via_call",
        ),
        pytest.param(
            Op.CALL(
                gas=50_000,
                address=Op.CALLDATALOAD(0),
                value=Op.BALANCE(Op.ADDRESS),
            )
            + Op.REVERT(0, 0),
            False,
            id="via_call_revert",
        ),
    ],
)
@pytest.mark.parametrize(
    "to_self",
    [
        pytest.param(False, id="to_beneficiary"),
        pytest.param(True, id="to_self"),
    ],
)
def test_finalization_burn_logs(
    state_test: StateTestFiller,
    env: Environment,
    pre: Alloc,
    sender: EOA,
    payer_code: Bytecode,
    eth_transferred: bool,
    to_self: bool,
) -> None:
    """
    Test Burn logs at finalization for post-selfdestruct balance.

    X contracts (x1, x2, x3) selfdestruct, then receive ETH via payer contracts
    (p1, p2, p3). At finalization, X contracts emit Burn logs for their
    in lexicographical address order (only if they received ETH).

    When to_self=True, X contracts SELFDESTRUCT to themselves (burning ETH
    with LOG2). When to_self=False, X contracts SELFDESTRUCT to a beneficiary
    (Transfer LOG3).
    """
    beneficiary = pre.deploy_contract(Op.STOP)

    # Pre-compute factory address and created contract addresses
    # so we can call them in reverse sorted order to prove finalization
    # logs are sorted by address, not by call order
    factory_address = compute_create_address(
        address=sender, nonce=sender.nonce
    )
    x1 = compute_create_address(address=factory_address, nonce=1)
    x2 = compute_create_address(address=factory_address, nonce=2)
    x3 = compute_create_address(address=factory_address, nonce=3)

    # sort() + call in REVERSE order to prove finalization
    # lexicographical sorting
    sorted_addrs = sorted([x1, x2, x3])
    reverse_sorted = list(reversed(sorted_addrs))

    # Runtime: selfdestruct on first call, STOP on subsequent calls
    target: Address | Opcodes = Op.ADDRESS if to_self else beneficiary
    runtime = Conditional(
        condition=Op.ISZERO(Op.TLOAD(0)),
        if_true=Op.TSTORE(0, 1) + Op.SELFDESTRUCT(target),
        if_false=Op.STOP,
    )
    initcode = Initcode(deploy_code=runtime)
    initcode_len = len(initcode)

    # Payer contracts (p1, p2, p3) will send ETH to created contracts
    p1 = pre.deploy_contract(payer_code, balance=100)
    p2 = pre.deploy_contract(payer_code, balance=200)
    p3 = pre.deploy_contract(payer_code, balance=300)

    # Call p1/p2/p3 targeting addresses in REVERSE sorted order
    # This proves finalization logs are sorted by address, not call order
    factory_code = (
        Om.MSTORE(initcode, 0)
        # Create x1, x2, x3
        + Op.TSTORE(0, Op.CREATE(value=1000, offset=0, size=initcode_len))
        + Op.TSTORE(1, Op.CREATE(value=2000, offset=0, size=initcode_len))
        + Op.TSTORE(2, Op.CREATE(value=3000, offset=0, size=initcode_len))
        # Call x1, x2, x3 to trigger SELFDESTRUCT
        + Op.CALL(gas=100_000, address=Op.TLOAD(0), value=0)
        + Op.CALL(gas=100_000, address=Op.TLOAD(1), value=0)
        + Op.CALL(gas=100_000, address=Op.TLOAD(2), value=0)
        # p1/p2/p3 send ETH in REVERSE sorted address order
        + Op.MSTORE(0, reverse_sorted[0])
        + Op.CALL(gas=100_000, address=p1, args_offset=0, args_size=32)
        + Op.MSTORE(0, reverse_sorted[1])
        + Op.CALL(gas=100_000, address=p2, args_offset=0, args_size=32)
        + Op.MSTORE(0, reverse_sorted[2])
        + Op.CALL(gas=100_000, address=p3, args_offset=0, args_size=32)
    )

    factory_balance = 1000 + 2000 + 3000
    pre.fund_address(factory_address, factory_balance)

    # Amounts based on reverse call order:
    # p1→reverse[0], p2→reverse[1], p3→reverse[2]
    amounts = {
        reverse_sorted[0]: 100,
        reverse_sorted[1]: 200,
        reverse_sorted[2]: 300,
    }

    # Execution logs:
    # 1. CREATE x1, x2, x3 → LOG3 Transfer (factory → created)
    # 2. CALL x1, x2, x3 → LOG3 or LOG2 depending on `to_self`
    # 3. p1/p2/p3 send to reverse_sorted order
    execution_logs = [
        transfer_log(factory_address, x1, 1000),
        transfer_log(factory_address, x2, 2000),
        transfer_log(factory_address, x3, 3000),
    ]

    if to_self:
        # SELFDESTRUCT to self burns ETH → LOG2 Burn
        execution_logs.extend(
            [
                burn_log(x1, 1000),
                burn_log(x2, 2000),
                burn_log(x3, 3000),
            ]
        )
        beneficiary_balance = 0
    else:
        # SELFDESTRUCT to beneficiary → LOG3 Transfer
        execution_logs.extend(
            [
                transfer_log(x1, beneficiary, 1000),
                transfer_log(x2, beneficiary, 2000),
                transfer_log(x3, beneficiary, 3000),
            ]
        )
        beneficiary_balance = factory_balance

    if not eth_transferred:
        # Reverted CALLs emit no logs, no ETH transferred, no finalization logs
        finalization_logs = []
        post = {
            x1: Account.NONEXISTENT,
            x2: Account.NONEXISTENT,
            x3: Account.NONEXISTENT,
            beneficiary: Account(balance=beneficiary_balance),
            p1: Account(balance=100),
            p2: Account(balance=200),
            p3: Account(balance=300),
        }
    else:
        # p1/p2/p3 send ETH in reverse sorted order
        execution_logs.extend(
            [
                transfer_log(p1, reverse_sorted[0], 100),
                transfer_log(p2, reverse_sorted[1], 200),
                transfer_log(p3, reverse_sorted[2], 300),
            ]
        )
        # Finalization logs emitted in SORTED address order (not call order)
        finalization_logs = [
            burn_log(addr, amounts[addr]) for addr in sorted_addrs
        ]
        post = {
            x1: Account.NONEXISTENT,
            x2: Account.NONEXISTENT,
            x3: Account.NONEXISTENT,
            beneficiary: Account(balance=beneficiary_balance),
            p1: Account(balance=0),
            p2: Account(balance=0),
            p3: Account(balance=0),
        }

    tx = Transaction(
        sender=sender,
        to=None,
        value=0,
        data=factory_code,
        gas_limit=1_000_000,
        expected_receipt=TransactionReceipt(
            logs=execution_logs + finalization_logs
        ),
    )

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

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.