Skip to content

test_identity_uncachable()

Documentation for tests/benchmark/compute/precompile/test_identity.py::test_identity_uncachable@ffe48572.

Generate fixtures for these test cases for Bogota with:

fill -v tests/benchmark/compute/precompile/test_identity.py::test_identity_uncachable --gas-benchmark-values 1

Benchmark IDENTITY with unique input per call.

Source code in tests/benchmark/compute/precompile/test_identity.py
@pytest.mark.repricing
@pytest.mark.parametrize("size", [32, 256, 1024])
def test_identity_uncachable(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
    gas_benchmark_value: int,
    tx_gas_limit: int,
    size: int,
) -> None:
    """Benchmark IDENTITY with unique input per call."""
    gsc = fork.gas_costs()
    intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()

    precompile_cost = (
        # static cost
        gsc.PRECOMPILE_IDENTITY_BASE
        # dynamic cost
        + math.ceil(size / 32) * gsc.PRECOMPILE_IDENTITY_PER_WORD
    )

    attack_block = Op.MSTORE(
        0,
        Op.ADD(
            Op.MLOAD(0),
            Op.STATICCALL(
                gas=Op.GAS,
                address=IdentitySpec.IDENTITY,
                args_size=size,
                ret_size=size,
                # gas accounting
                address_warm=True,
                inner_call_cost=precompile_cost,
            ),
        ),
        old_memory_size=size,
        new_memory_size=size,
    )

    setup = Op.CALLDATACOPY(
        0,
        0,
        Op.CALLDATASIZE,
        # gas accounting
        data_size=size,
        old_memory_size=0,
        new_memory_size=size,
    )
    setup_cost = setup.gas_cost(fork)

    loop = WhileGas(
        body=attack_block,
        fork=fork,
    )
    code = setup + loop
    attack_contract_address = pre.deploy_contract(code=code)

    txs: list[Transaction] = []
    remaining_gas = gas_benchmark_value
    rng = random.Random(42)
    expected_opcode_count = 0
    while remaining_gas > 0:
        per_tx_gas = min(tx_gas_limit, remaining_gas)
        calldata = Bytes(rng.randbytes(size))

        intrinsic = intrinsic_gas_calculator(
            calldata=calldata,
            return_cost_deducted_prior_execution=True,
        )
        gas_for_loop = per_tx_gas - intrinsic - setup_cost
        if gas_for_loop < loop.gas_cost(fork):
            break
        expected_opcode_count += gas_for_loop // loop.gas_cost(fork)

        txs.append(
            Transaction(
                to=attack_contract_address,
                sender=pre.fund_eoa(),
                gas_limit=per_tx_gas,
                data=calldata,
            )
        )
        remaining_gas -= per_tx_gas

    assert len(txs) != 0, "No transactions were added to the test."

    benchmark_test(
        target_opcode=Precompile.IDENTITY,
        skip_gas_used_validation=True,
        verifications=[ReceiptStatusExpected()],
        blocks=[Block(txs=txs)],
        expected_opcode_count=expected_opcode_count,
    )

Parametrized Test Cases

This test generates 3 parametrized test cases across 4 forks.