Skip to content

test_worst_clz_diff_input()

Documentation for tests/benchmark/test_worst_compute.py::test_worst_clz_diff_input@v5.0.0.

Generate fixtures for these test cases for Osaka with:

fill -v tests/benchmark/test_worst_compute.py::test_worst_clz_diff_input --fork Osaka

Test running a block with as many CLZ with different input as possible.

Source code in tests/benchmark/test_worst_compute.py
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
@pytest.mark.valid_from("Osaka")
def test_worst_clz_diff_input(
    blockchain_test: BlockchainTestFiller,
    pre: Alloc,
    fork: Fork,
    gas_benchmark_value: int,
    env: Environment,
):
    """Test running a block with as many CLZ with different input as possible."""
    tx_gas_limit = fork.transaction_gas_limit_cap() or env.gas_limit
    max_code_size = fork.max_code_size()

    code_prefix = Op.JUMPDEST
    code_suffix = Op.PUSH0 + Op.JUMP

    available_code_size = max_code_size - len(code_prefix) - len(code_suffix)

    code_seq = Bytecode()

    for i in range(available_code_size):
        value = (2**256 - 1) >> (i % 256)
        clz_op = Op.CLZ(value) + Op.POP
        if len(code_seq) + len(clz_op) > available_code_size:
            break
        code_seq += clz_op

    attack_code = code_prefix + code_seq + code_suffix
    assert len(attack_code) <= max_code_size

    code_address = pre.deploy_contract(code=attack_code)

    sender = pre.fund_eoa()
    tx_count = gas_benchmark_value // tx_gas_limit
    remainder_gas = gas_benchmark_value % tx_gas_limit
    txs = [
        Transaction(
            to=code_address,
            gas_limit=tx_gas_limit if i < tx_count else remainder_gas,
            nonce=i,
            sender=sender,
        )
        for i in range(tx_count + 1)
    ]

    blockchain_test(
        genesis_environment=env,
        pre=pre,
        post={},
        blocks=[Block(txs=txs)],
    )

Parametrized Test Cases

This test case is only parametrized by fork.

Test ID (Abbreviated)
...fork_Osaka-blockchain_test