Skip to content

test_selfdestruct_in_create_tx_initcode()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_selfdestruct_in_create_tx_initcode@87aba1a3.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_selfdestruct_in_create_tx_initcode --fork Amsterdam

Verify state gas accounting when a creation tx's initcode immediately SELFDESTRUCTs to a new beneficiary.

Under EIP-2780 the created contract's NEW_ACCOUNT is charged at the top frame from gas_left (not the intrinsic), so gas_limit must cover it on top of the initcode. The block state gas is the created contract's NEW_ACCOUNT plus the fresh beneficiary's NEW_ACCOUNT charged by the SELFDESTRUCT.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
@pytest.mark.valid_from("EIP8037")
def test_selfdestruct_in_create_tx_initcode(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
) -> None:
    """
    Verify state gas accounting when a creation tx's initcode
    immediately SELFDESTRUCTs to a new beneficiary.

    Under EIP-2780 the created contract's ``NEW_ACCOUNT`` is charged at
    the top frame from ``gas_left`` (not the intrinsic), so ``gas_limit``
    must cover it on top of the initcode. The block state gas is the
    created contract's ``NEW_ACCOUNT`` plus the fresh beneficiary's
    ``NEW_ACCOUNT`` charged by the SELFDESTRUCT.
    """
    gas_costs = fork.gas_costs()
    create_state_gas = fork.create_state_gas(code_size=0)

    beneficiary = 0xDEAD
    # `account_new` folds the beneficiary's `ACCOUNT_WRITE` regular
    # cost and account-creation state gas into `gas_cost`.
    initcode = Op.SELFDESTRUCT(beneficiary, account_new=True)

    sender = pre.fund_eoa()
    intrinsic_calc = fork.transaction_intrinsic_cost_calculator()
    intrinsic_regular = intrinsic_calc(
        calldata=bytes(initcode), contract_creation=True, sends_value=True
    )

    # State: the created contract's top-frame NEW_ACCOUNT plus the fresh
    # beneficiary's NEW_ACCOUNT from the SELFDESTRUCT.
    expected_state = create_state_gas + gas_costs.NEW_ACCOUNT

    initcode_gas = initcode.gas_cost(fork)
    gas_limit = intrinsic_regular + gas_costs.NEW_ACCOUNT + initcode_gas + 1000

    tx = Transaction(
        sender=sender,
        to=None,
        data=initcode,
        value=1,
        gas_limit=gas_limit,
    )

    blockchain_test(
        pre=pre,
        blocks=[
            Block(
                txs=[tx],
                header_verify=Header(gas_used=expected_state),
            ),
        ],
        post={},
    )

Parametrized Test Cases

This test generates 1 parametrized test case across 1 fork.