Skip to content

test_worst_returndatacopy()

Documentation for tests/benchmark/test_worst_memory.py::test_worst_returndatacopy@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

fill -v tests/benchmark/test_worst_memory.py::test_worst_returndatacopy -m benchmark

Test running a block filled with RETURNDATACOPY executions.

Source code in tests/benchmark/test_worst_memory.py
186
187
188
189
190
191
192
193
194
195
196
197
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
@pytest.mark.parametrize(
    "size",
    [
        pytest.param(0, id="0 bytes"),
        pytest.param(100, id="100 bytes"),
        pytest.param(10 * 1024, id="10KiB"),
        pytest.param(1024 * 1024, id="1MiB"),
    ],
)
@pytest.mark.parametrize(
    "fixed_dst",
    [
        True,
        False,
    ],
)
def test_worst_returndatacopy(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    size: int,
    fixed_dst: bool,
) -> None:
    """Test running a block filled with RETURNDATACOPY executions."""
    # 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(size // 2, Op.GAS)
        + Op.MSTORE8(size - 1, Op.GAS)
        + Op.RETURN(0, size)
    )
    helper_contract = pre.deploy_contract(code=code)

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

    # We create the contract that will be doing the RETURNDATACOPY multiple
    # times.
    returndata_gen = Op.STATICCALL(address=helper_contract) if size > 0 else Bytecode()
    attack_block = Op.RETURNDATACOPY(dst, Op.PUSH0, Op.RETURNDATASIZE)

    # The attack loop is constructed as:
    # ```
    # JUMPDEST(#)
    # RETURNDATACOPY(...)
    # RETURNDATACOPY(...)
    # ...
    # STATICCALL(address=helper_contract)
    # JUMP(#)
    # ```
    # The goal is that once per (big) loop iteration, the helper contract is
    # called to
    # generate fresh returndata to continue calling RETURNDATACOPY.

    benchmark_test(
        code_generator=JumpLoopGenerator(
            setup=returndata_gen, attack_block=attack_block, cleanup=returndata_gen
        ),
    )

Parametrized Test Cases

This test case is only parametrized by fork.

Test ID (Abbreviated) fixed_dst size
...fork_Prague-blockchain_test-fixed_dst_True-0 bytes True 0
...fork_Prague-blockchain_test-fixed_dst_True-100 bytes True 100
...fork_Prague-blockchain_test-fixed_dst_True-10KiB True 10240
...fork_Prague-blockchain_test-fixed_dst_True-1MiB True 1048576
...fork_Prague-blockchain_test-fixed_dst_False-0 bytes False 0
...fork_Prague-blockchain_test-fixed_dst_False-100 bytes False 100
...fork_Prague-blockchain_test-fixed_dst_False-10KiB False 10240
...fork_Prague-blockchain_test-fixed_dst_False-1MiB False 1048576
...fork_Osaka-blockchain_test-fixed_dst_True-0 bytes True 0
...fork_Osaka-blockchain_test-fixed_dst_True-100 bytes True 100
...fork_Osaka-blockchain_test-fixed_dst_True-10KiB True 10240
...fork_Osaka-blockchain_test-fixed_dst_True-1MiB True 1048576
...fork_Osaka-blockchain_test-fixed_dst_False-0 bytes False 0
...fork_Osaka-blockchain_test-fixed_dst_False-100 bytes False 100
...fork_Osaka-blockchain_test-fixed_dst_False-10KiB False 10240
...fork_Osaka-blockchain_test-fixed_dst_False-1MiB False 1048576