Skip to content

test_returndatacopy()

Documentation for tests/benchmark/compute/instruction/test_call_context.py::test_returndatacopy@20373115.

Generate fixtures for these test cases for Amsterdam with:

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

Benchmark RETURNDATACOPY instruction.

Source code in tests/benchmark/compute/instruction/test_call_context.py
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
@pytest.mark.repricing(fixed_dst=True)
@pytest.mark.parametrize("mem_size", [0, 32, 256, 1024])
@pytest.mark.parametrize(
    "return_size",
    [0, 32, 256, 1024, 10 * 1024, 1024 * 1024],
)
@pytest.mark.parametrize(
    "fixed_dst",
    [
        True,
        False,
    ],
)
def test_returndatacopy(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    mem_size: int,
    return_size: int,
    fixed_dst: bool,
) -> None:
    """Benchmark RETURNDATACOPY instruction."""
    # Create the contract that will RETURN the data that will be used for
    # RETURNDATACOPY.
    # Random-ish data is injected at different points in memory to avoid
    # making the content
    # predictable. If `size` is 0, this helper contract won't be used.
    code = (
        Op.MSTORE8(0, Op.GAS)
        + Op.MSTORE8(return_size // 2, Op.GAS)
        + Op.MSTORE8(return_size - 1, Op.GAS)
        + Op.RETURN(0, return_size)
    )
    helper_contract = pre.deploy_contract(code=code)

    setup = Bytecode()
    setup += Op.MSTORE8(mem_size - 1, 0xFF) if mem_size > 0 else Bytecode()
    setup += (
        Op.STATICCALL(address=helper_contract)
        if return_size > 0
        else Bytecode()
    )

    cleanup = (
        Op.STATICCALL(address=helper_contract)
        if return_size > 0
        else Bytecode()
    )
    dst = 0 if fixed_dst else Op.MOD(Op.GAS, 7)

    attack_block = Op.RETURNDATACOPY(dst, Op.PUSH0, Op.RETURNDATASIZE)

    benchmark_test(
        target_opcode=Op.RETURNDATACOPY,
        code_generator=JumpLoopGenerator(
            setup=setup,
            attack_block=attack_block,
            cleanup=cleanup,
        ),
    )

Parametrized Test Cases

This test generates 48 parametrized test cases across 3 forks.