Skip to content

test_worst_return_revert()

Documentation for tests/benchmark/test_worst_compute.py::test_worst_return_revert@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

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

Test running a block with as many RETURN or REVERT as possible.

Source code in tests/benchmark/test_worst_compute.py
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
@pytest.mark.parametrize(
    "opcode",
    [Op.RETURN, Op.REVERT],
)
@pytest.mark.parametrize(
    "return_size, return_non_zero_data",
    [
        pytest.param(0, False, id="empty"),
        pytest.param(1024, True, id="1KiB of non-zero data"),
        pytest.param(1024, False, id="1KiB of zero data"),
        pytest.param(1024 * 1024, True, id="1MiB of non-zero data"),
        pytest.param(1024 * 1024, False, id="1MiB of zero data"),
    ],
)
def test_worst_return_revert(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
    opcode: Op,
    return_size: int,
    return_non_zero_data: bool,
) -> None:
    """Test running a block with as many RETURN or REVERT as possible."""
    max_code_size = fork.max_code_size()

    # Create the contract that will be called repeatedly.
    # The bytecode of the contract is:
    # ```
    # [CODECOPY(returned_size) -- Conditional if return_non_zero_data]
    # opcode(returned_size)
    # <Fill with INVALID opcodes up to the max contract size>
    # ```
    # Filling the contract up to the max size is a cheap way of leveraging
    # CODECOPY to return non-zero bytes if requested. Note that since this
    # is a pre-deploy this cost isn't
    # relevant for the benchmark.
    mem_preparation = Op.CODECOPY(size=return_size) if return_non_zero_data else Bytecode()
    executable_code = mem_preparation + opcode(size=return_size)
    code = executable_code
    if return_non_zero_data:
        code += Op.INVALID * (max_code_size - len(executable_code))
    target_contract_address = pre.deploy_contract(code=code)

    attack_block = Op.POP(Op.STATICCALL(address=target_contract_address))

    benchmark_test(
        code_generator=JumpLoopGenerator(attack_block=attack_block),
    )

Parametrized Test Cases

This test case is only parametrized by fork.

Test ID (Abbreviated) return_size return_non_zero_data opcode
...fork_Prague-blockchain_test-empty-opcode_RETURN 0 False RETURN
...fork_Prague-blockchain_test-empty-opcode_REVERT 0 False REVERT
...fork_Prague-blockchain_test-1KiB of non-zero data-opcode_RETURN 1024 True RETURN
...fork_Prague-blockchain_test-1KiB of non-zero data-opcode_REVERT 1024 True REVERT
...fork_Prague-blockchain_test-1KiB of zero data-opcode_RETURN 1024 False RETURN
...fork_Prague-blockchain_test-1KiB of zero data-opcode_REVERT 1024 False REVERT
...fork_Prague-blockchain_test-1MiB of non-zero data-opcode_RETURN 1048576 True RETURN
...fork_Prague-blockchain_test-1MiB of non-zero data-opcode_REVERT 1048576 True REVERT
...fork_Prague-blockchain_test-1MiB of zero data-opcode_RETURN 1048576 False RETURN
...fork_Prague-blockchain_test-1MiB of zero data-opcode_REVERT 1048576 False REVERT
...fork_Osaka-blockchain_test-empty-opcode_RETURN 0 False RETURN
...fork_Osaka-blockchain_test-empty-opcode_REVERT 0 False REVERT
...fork_Osaka-blockchain_test-1KiB of non-zero data-opcode_RETURN 1024 True RETURN
...fork_Osaka-blockchain_test-1KiB of non-zero data-opcode_REVERT 1024 True REVERT
...fork_Osaka-blockchain_test-1KiB of zero data-opcode_RETURN 1024 False RETURN
...fork_Osaka-blockchain_test-1KiB of zero data-opcode_REVERT 1024 False REVERT
...fork_Osaka-blockchain_test-1MiB of non-zero data-opcode_RETURN 1048576 True RETURN
...fork_Osaka-blockchain_test-1MiB of non-zero data-opcode_REVERT 1048576 True REVERT
...fork_Osaka-blockchain_test-1MiB of zero data-opcode_RETURN 1048576 False RETURN
...fork_Osaka-blockchain_test-1MiB of zero data-opcode_REVERT 1048576 False REVERT