Skip to content

test_oversized_initcode_opcode_no_state_gas()

Documentation for tests/amsterdam/eip8037_state_creation_gas_cost_increase/test_state_gas_create.py::test_oversized_initcode_opcode_no_state_gas@622620d8.

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

Verify CREATE/CREATE2 with oversized initcode fails the size check before any state gas is charged.

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
2912
@pytest.mark.parametrize(
    "initcode_size_delta",
    [
        pytest.param(0, id="at_max"),
        pytest.param(1, id="over_max"),
    ],
)
@pytest.mark.with_all_create_opcodes()
@pytest.mark.valid_from("EIP8037")
def test_oversized_initcode_opcode_no_state_gas(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    create_opcode: Op,
    initcode_size_delta: int,
) -> None:
    """
    Verify CREATE/CREATE2 with oversized initcode fails the size
    check before any state gas is charged.
    """
    max_size = fork.max_initcode_size()
    size = max_size + initcode_size_delta
    initcode = bytes(size)

    factory_code = (
        create_opcode(
            value=0,
            offset=0,
            size=size,
            salt=0,
            init_code_size=size,
            new_memory_size=size,
        )
        if create_opcode == Op.CREATE2
        else create_opcode(
            value=0,
            offset=0,
            size=size,
            init_code_size=size,
            new_memory_size=size,
        )
    )

    factory = pre.deploy_contract(factory_code)
    factory_execution = factory_code.execution_cost(fork)

    caller_code = Op.POP(
        Op.CALL(
            gas=factory_execution,
            address=factory,
        )
    )
    caller = pre.deploy_contract(caller_code)

    create_address = compute_create_address(
        address=factory,
        nonce=1,
        salt=0,
        initcode=initcode,
        opcode=create_opcode,
    )

    create_state_gas = factory_code.state_cost(fork)
    expected_execution = (
        fork.transaction_intrinsic_cost_calculator()()
        + caller_code.execution_cost(fork)
        + factory_execution
    )
    assert create_state_gas > expected_execution, (
        "NEW_ACCOUNT must dominate execution to detect a stray state charge"
    )
    expected_state = create_state_gas if initcode_size_delta == 0 else 0
    expected_gas_used = max(expected_execution, expected_state)

    tx = Transaction(
        sender=pre.fund_eoa(),
        to=caller,
        state_gas_reservoir=create_state_gas,
    )

    post: dict = {factory: Account(nonce=2 if initcode_size_delta == 0 else 1)}
    if initcode_size_delta == 0:
        post[create_address] = Account(nonce=1, code=b"")
    else:
        post[create_address] = Account.NONEXISTENT

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

Parametrized Test Cases

This test generates 4 parametrized test cases across 1 fork.