Skip to content

test_top_frame_new_account_skipped_for_create_target_funded_same_block()

Documentation for tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_top_frame_charges.py::test_top_frame_new_account_skipped_for_create_target_funded_same_block@636b5f17.

Generate fixtures for these test cases for Bogota with:

fill -v tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_top_frame_charges.py::test_top_frame_new_account_skipped_for_create_target_funded_same_block --fork Bogota

A creation transaction whose target was funded by an earlier transaction of the same block does not incur the top-frame NEW_ACCOUNT state charge.

This discriminates the transaction pre-state from the block pre-state: get_pre_state_account consults the block's accumulated same-block transaction writes before falling back to the block pre-state, so the funding transaction's new leaf counts as pre-existing for the creation transaction. An implementation snapshotting at block start would charge a second NEW_ACCOUNT, shifting the creation transaction's receipt by 183,600 and doubling the state dimension pinned by the header.

The funding transaction pays its own top-frame NEW_ACCOUNT for materializing the leaf, which the block header pins as the block's entire state-gas dimension: gas_used = max(execution, state) must equal exactly one NEW_ACCOUNT.

Source code in tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_top_frame_charges.py
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
@EIPChecklist.GasCostChanges.Test.GasUpdatesMeasurement()
@pytest.mark.parametrize(
    "value",
    [
        pytest.param(0, id="zero_value"),
        pytest.param(1, id="non-zero_value"),
    ],
)
def test_top_frame_new_account_skipped_for_create_target_funded_same_block(
    fork: Fork,
    pre: Alloc,
    blockchain_test: BlockchainTestFiller,
    value: int,
) -> None:
    """
    A creation transaction whose target was funded by an *earlier
    transaction of the same block* does not incur the top-frame
    ``NEW_ACCOUNT`` state charge.

    This discriminates the transaction pre-state from the block
    pre-state: ``get_pre_state_account`` consults the block's
    accumulated same-block transaction writes before falling back to
    the block pre-state, so the funding transaction's new leaf counts
    as pre-existing for the creation transaction. An implementation
    snapshotting at block start would charge a second ``NEW_ACCOUNT``,
    shifting the creation transaction's receipt by 183,600 and
    doubling the state dimension pinned by the header.

    The funding transaction pays its own top-frame ``NEW_ACCOUNT`` for
    materializing the leaf, which the block header pins as the block's
    entire state-gas dimension: ``gas_used = max(execution, state)`` must
    equal exactly one ``NEW_ACCOUNT``.
    """
    funder = pre.fund_eoa()
    sender = pre.fund_eoa()
    created = compute_create_address(address=sender, nonce=sender.nonce)

    # Transaction 1: fund the future create address. The value transfer
    # to the not-yet-existing leaf pays the top-frame ``NEW_ACCOUNT``.
    prefund = 1
    fund_intrinsic = fork.transaction_intrinsic_cost_calculator()(
        sends_value=True,
        recipient_type=RecipientType.EMPTY_ACCOUNT,
        return_cost_deducted_prior_execution=True,
    )
    fund_state_gas = fork.transaction_top_frame_state_gas(
        sends_value=True,
        recipient_type=RecipientType.EMPTY_ACCOUNT,
    )
    assert fund_state_gas > 0, (
        "funding an empty leaf must charge top-frame state gas"
    )
    fund_total = fund_intrinsic + fund_state_gas
    fund_tx = Transaction(
        sender=funder,
        to=created,
        value=prefund,
        gas_limit=fund_total,
        expected_receipt=TransactionReceipt(cumulative_gas_used=fund_total),
    )

    # Transaction 2: the creation transaction, with gas headroom; the
    # receipt pins the consumed gas to exactly the ``NEW_ACCOUNT``-free
    # total.
    init_code, exec_gas = creation_tx_init_code(fork)
    create_intrinsic = fork.transaction_intrinsic_cost_calculator()(
        calldata=init_code,
        contract_creation=True,
        sends_value=bool(value),
        return_cost_deducted_prior_execution=True,
    )
    create_total = create_intrinsic + exec_gas
    calldata_floor = fork.transaction_data_floor_cost_calculator()(
        data=init_code,
        contract_creation=True,
        sends_value=bool(value),
    )
    assert create_total > calldata_floor, (
        "the exact total must exceed the calldata floor for the "
        "gas pin to observe the skipped charge."
        "Raise the `EXECUTION_GAS_BURNED` gas target to fix."
    )
    create_tx = Transaction(
        sender=sender,
        to=None,
        data=init_code,
        value=value,
        gas_limit=create_total,
        expected_receipt=TransactionReceipt(
            cumulative_gas_used=fund_total + create_total
        ),
    )

    # Header pin: the block's state dimension is exactly the funding
    # transaction's ``NEW_ACCOUNT``; both execution intrinsics sit at or
    # above their calldata floors, so no floor term enters the block's
    # execution dimension either.
    block_execution = fund_intrinsic + create_total
    assert fund_state_gas > block_execution, (
        "the state dimension must dominate for the header to pin it"
    )

    post = {
        funder: Account(nonce=1),
        sender: Account(nonce=1),
        created: Account(nonce=1, balance=prefund + value, code=b""),
    }

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[fund_tx, create_tx],
                header_verify=Header(gas_used=fund_state_gas),
            ),
        ],
        post=post,
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 2 forks.