Skip to content

test_top_frame_new_account_skipped_for_prefunded_create_target()

Documentation for tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_top_frame_charges.py::test_top_frame_new_account_skipped_for_prefunded_create_target@26332146.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_top_frame_charges.py::test_top_frame_new_account_skipped_for_prefunded_create_target --fork Amsterdam

A creation transaction whose nonce-derived target address already holds a balance does not incur the top-frame NEW_ACCOUNT state charge.

The create branch of prepare_dispatch keys the charge on the transaction pre-state being empty — a live check would always see the account, because process_create_message bumps the target's nonce before dispatch. Pre-funding the create address makes the pre-state leaf non-empty, so the charge must be skipped; a balance-only leaf does not trigger the create-collision check (only nonce or code do), so the deployment still succeeds.

The gas limit carries headroom above the exact total so the transaction never runs out of gas, and the receipt pins cumulative_gas_used to exactly the intrinsic plus the init-code execution gas: a wrongly charged NEW_ACCOUNT for the pre-existing leaf (or a spurious refill) shifts the receipt by 183,600 in either direction.

Source code in tests/amsterdam/eip2780_reduce_intrinsic_tx_gas/test_top_frame_charges.py
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
@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_prefunded_create_target(
    fork: Fork,
    pre: Alloc,
    state_test: StateTestFiller,
    value: int,
) -> None:
    """
    A creation transaction whose nonce-derived target address already
    holds a balance does not incur the top-frame ``NEW_ACCOUNT`` state
    charge.

    The create branch of ``prepare_dispatch`` keys the charge on the
    *transaction pre-state* being empty — a live check would always see
    the account, because ``process_create_message`` bumps the target's
    nonce before dispatch. Pre-funding the create address makes the
    pre-state leaf non-empty, so the charge must be skipped; a
    balance-only leaf does not trigger the create-collision check
    (only nonce or code do), so the deployment still succeeds.

    The gas limit carries headroom above the exact total so the
    transaction never runs out of gas, and the receipt pins
    ``cumulative_gas_used`` to exactly the intrinsic plus the init-code
    execution gas: a wrongly charged ``NEW_ACCOUNT`` for the
    pre-existing leaf (or a spurious refill) shifts the receipt by
    183,600 in either direction.
    """
    sender = pre.fund_eoa()
    created = compute_create_address(address=sender, nonce=sender.nonce)
    prefund = 1
    pre.fund_address(created, prefund)

    init_code, exec_gas = creation_tx_init_code(fork)

    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        calldata=init_code,
        contract_creation=True,
        sends_value=bool(value),
        return_cost_deducted_prior_execution=True,
    )
    # The amount a fresh create target would be charged at the top
    # frame -- the charge this test asserts is skipped.
    fresh_target_state_gas = fork.transaction_top_frame_state_gas(
        contract_creation=True,
    )
    assert fresh_target_state_gas > 0, (
        "a fresh create target must be charged top-frame state gas"
    )

    total_gas = intrinsic_gas + exec_gas
    calldata_floor = fork.transaction_data_floor_cost_calculator()(
        data=init_code,
        contract_creation=True,
        sends_value=bool(value),
    )
    assert total_gas > calldata_floor, (
        "The exact total must exceed the calldata floor for the "
        "gas pin to observe the skipped charge."
        "Lift memory expansion in `creation_tx_init_code` to fix."
    )

    tx = Transaction(
        sender=sender,
        to=None,
        data=init_code,
        value=value,
        gas_limit=total_gas,
        expected_receipt=TransactionReceipt(cumulative_gas_used=total_gas),
    )

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

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

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.