Skip to content

test_calldatacopy_from_call()

Documentation for tests/benchmark/compute/instruction/test_call_context.py::test_calldatacopy_from_call@7b8124a7.

Generate fixtures for these test cases for Osaka with:

fill -v tests/benchmark/compute/instruction/test_call_context.py::test_calldatacopy_from_call --gas-benchmark-values 1

Benchmark CALLDATACOPY instruction.

Source code in tests/benchmark/compute/instruction/test_call_context.py
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
@pytest.mark.parametrize(
    "fixed_src_dst",
    [
        True,
        False,
    ],
)
@pytest.mark.parametrize(
    "non_zero_data",
    [
        True,
        False,
    ],
)
@pytest.mark.parametrize(
    "calldata_size",
    [0, 32, 256, 1024],
)
def test_calldatacopy_from_call(
    benchmark_test: BenchmarkTestFiller,
    fork: Fork,
    calldata_size: int,
    fixed_src_dst: bool,
    non_zero_data: bool,
    tx_gas_limit: int,
) -> None:
    """Benchmark CALLDATACOPY instruction."""
    if calldata_size == 0 and non_zero_data:
        pytest.skip("Non-zero data with size 0 is not applicable.")

    # If `non_zero_data` is True, we fill the calldata with deterministic
    # random data. Note that if `size == 0` and `non_zero_data` is a skipped
    # case.
    data = (
        Bytes([i % 256 for i in range(calldata_size)])
        if non_zero_data
        else Bytes()
    )

    intrinsic_gas_calculator = fork.transaction_intrinsic_cost_calculator()
    min_gas = intrinsic_gas_calculator(calldata=data)
    if min_gas > tx_gas_limit:
        pytest.skip(
            "Minimum gas required for calldata ({min_gas}) is greater "
            "than the gas limit"
        )

    # We create the contract that will be doing the CALLDATACOPY multiple
    # times.
    #
    # If `non_zero_data` is True, we leverage CALLDATASIZE for the copy
    # length. Otherwise, since we
    # don't send zero data explicitly via calldata, PUSH the target size and
    # use DUP1 to copy it.
    setup = (
        Bytecode()
        if non_zero_data or calldata_size == 0
        else Op.PUSH3(calldata_size)
    )
    src_dst = 0 if fixed_src_dst else Op.AND(Op.GAS, 7)
    attack_block = Op.CALLDATACOPY(
        src_dst,
        src_dst,
        Op.CALLDATASIZE if non_zero_data or calldata_size == 0 else Op.DUP1,
    )

    benchmark_test(
        target_opcode=Op.CALLDATACOPY,
        code_generator=ExtCallGenerator(
            setup=setup,
            attack_block=attack_block,
            tx_kwargs={"data": data},
        ),
    )

Parametrized Test Cases

This test generates 16 parametrized test cases across 2 forks.