Skip to content

test_worst_clz_diff_input()

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

Generate fixtures for these test cases for Osaka with:

fill -v tests/benchmark/test_worst_compute.py::test_worst_clz_diff_input -m benchmark

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

Source code in tests/benchmark/test_worst_compute.py
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
@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