Skip to content

test_contract_creation()

Documentation for tests/benchmark/compute/scenario/test_transaction_types.py::test_contract_creation@622620d8.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/benchmark/compute/scenario/test_transaction_types.py::test_contract_creation --gas-benchmark-values 1

Benchmark contract creations via transactions.

Source code in tests/benchmark/compute/scenario/test_transaction_types.py
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
@pytest.mark.parametrize("transfer_amount", [0, 1])
@pytest.mark.parametrize(
    "contract_size", [0, 1, pytest.param(None, id="contract_size_max")]
)
def test_contract_creation(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
    transfer_amount: int,
    gas_benchmark_value: int,
    contract_size: int | None,
) -> None:
    """Benchmark contract creations via transactions."""
    if contract_size is None:
        contract_size = fork.max_code_size()

    initcode = Op.RETURN(
        Op.PUSH0,
        contract_size,
        # gas accounting
        old_memory_size=0,
        new_memory_size=contract_size,
        code_deposit_size=contract_size,
    )
    intrinsic_gas_calc = fork.transaction_intrinsic_cost_calculator()
    sends_value = transfer_amount > 0

    # EIP-7623: actual gas used = max(standard + execution, floor)
    standard_intrinsic = intrinsic_gas_calc(
        calldata=bytes(initcode),
        contract_creation=True,
        sends_value=sends_value,
        return_cost_deducted_prior_execution=True,
    )
    floor_intrinsic = intrinsic_gas_calc(
        calldata=bytes(initcode),
        contract_creation=True,
        sends_value=sends_value,
    )
    execution_gas = initcode.gas_cost(fork)
    top_frame_state_gas = fork.transaction_top_frame_state_gas(
        contract_creation=True
    )
    tx_cost = max(
        standard_intrinsic + execution_gas + top_frame_state_gas,
        floor_intrinsic,
    )

    iteration_count = gas_benchmark_value // tx_cost

    sender = pre.fund_eoa()
    txs = []
    post = {}
    for nonce in range(iteration_count):
        txs.append(
            Transaction(
                to=None,
                data=initcode,
                value=transfer_amount,
                gas_limit=tx_cost,
                sender=sender,
            )
        )
        created_address = compute_create_address(address=sender, nonce=nonce)
        post[created_address] = Account(nonce=1)

    benchmark_test(
        pre=pre,
        post=post,
        blocks=[Block(txs=txs)],
        expected_benchmark_gas_used=iteration_count * tx_cost,
    )

Parametrized Test Cases

This test generates 6 parametrized test cases across 3 forks.