Skip to content

test_create_collision_burned_gas_counted_in_block_regular()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_create_collision_burned_gas_counted_in_block_regular@c74f1a67.

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_create_collision_burned_gas_counted_in_block_regular --fork Amsterdam

Verify gas burned by a CREATE/CREATE2 address collision counts toward block regular gas used in the header.

Source code in tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
@pytest.mark.pre_alloc_mutable
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_create_collision_burned_gas_counted_in_block_regular(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
) -> None:
    """
    Verify gas burned by a CREATE/CREATE2 address collision counts
    toward block regular gas used in the header.
    """
    init_code = Op.STOP
    mstore_value, size = init_code_at_high_bytes(init_code)
    salt = 0

    create_call = (
        create_opcode(value=0, offset=0, size=size, salt=salt)
        if create_opcode == Op.CREATE2
        else create_opcode(value=0, offset=0, size=size)
    )
    factory_code = Op.MSTORE(0, mstore_value) + Op.POP(create_call) + Op.STOP
    factory = pre.deploy_contract(code=factory_code)

    collision_target = compute_create_address(
        address=factory,
        nonce=1,
        salt=salt,
        initcode=bytes(init_code),
        opcode=create_opcode,
    )
    pre.deploy_contract(code=Op.STOP, address=collision_target)

    # Fixed-size budget so the forwarded create_message_gas is
    # deterministic and the baseline below is reproducible.
    gas_limit = 250_000

    tx = Transaction(
        to=factory,
        gas_limit=gas_limit,
        sender=pre.fund_eoa(),
    )

    # CPSB-agnostic baseline: block_state_gas is zero for this tx (the
    # collision refunds the NEW_ACCOUNT state charge), so header.gas_used
    # equals the regular-gas total. Decompose the parent + inner frame
    # accounting from fork APIs so the baseline tracks future cost
    # changes automatically.
    intrinsic = fork.transaction_intrinsic_cost_calculator()()
    new_account = fork.gas_costs().NEW_ACCOUNT
    create_base = fork.gas_costs().OPCODE_CREATE_BASE
    # POP + STOP run in the parent frame after CREATE returns; their
    # cost comes out of the 1/64 retained gas.
    post_create_static = (Op.POP + Op.STOP).gas_cost(fork)
    # factory_code.gas_cost(fork) folds NEW_ACCOUNT into the CREATE op
    # (state gas is treated as part of the opcode total). Strip it
    # back out and split off the post-CREATE tail to isolate the
    # pre-CREATE static gas.
    factory_pre_create = (
        factory_code.gas_cost(fork)
        - new_account
        - create_base
        - post_create_static
    )
    # MSTORE writes the initcode at memory[0:32] (one word).
    memory_expansion = fork.memory_expansion_gas_calculator()(new_bytes=32)
    # gas_left at the moment NEW_ACCOUNT spills into the regular pool
    # (reservoir is empty for tx_gas_limit < TX_MAX_GAS_LIMIT).
    gas_at_create_after_state = (
        gas_limit
        - intrinsic
        - factory_pre_create
        - memory_expansion
        - create_base
        - new_account
    )
    # Inner burns 63/64 of the available gas on collision; the parent
    # retains 1/64. The state-spill of NEW_ACCOUNT is refunded back to
    # gas_left on collision (nets zero). Post-CREATE consumes from the
    # retained pool. A mutation that drops the burned forwarded gas
    # from regular accounting would reduce this baseline.
    retained = gas_at_create_after_state // 64
    baseline_gas_used = gas_limit - retained - new_account + post_create_static

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

Parametrized Test Cases

This test generates 2 parametrized test cases across 1 fork.