Skip to content

test_nested_frame_memory()

Documentation for tests/benchmark/compute/instruction/test_memory.py::test_nested_frame_memory@02aafd78.

Generate fixtures for these test cases for Bogota with:

fill -v tests/benchmark/compute/instruction/test_memory.py::test_nested_frame_memory --gas-benchmark-values 1

Benchmark a deep frame stack where every frame holds live memory.

Source code in tests/benchmark/compute/instruction/test_memory.py
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
248
@pytest.mark.parametrize("depth", [1, 64, 256])
@pytest.mark.parametrize("mem_size", [0, 8 * 1024, 64 * 1024])
def test_nested_frame_memory(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
    gas_benchmark_value: int,
    tx_gas_limit: int,
    depth: int,
    mem_size: int,
) -> None:
    """Benchmark a deep frame stack where every frame holds live memory."""
    leaf_address = pre.deploy_contract(
        code=WhileGas(body=Op.POP(Op.MLOAD(Op.PUSH0)), fork=fork)
    )

    # The frame's own memory is claimed before the descent, and must not
    # overlap memory[0:32], which carries the depth to the next frame.
    frame_memory = (
        Op.MSTORE8(mem_size - 1, 0xFF) if mem_size > 0 else Bytecode()
    )

    descend = Op.MSTORE(0, Op.SUB(Op.CALLDATALOAD(0), 1)) + Conditional(
        condition=Op.CALL(
            gas=Op.GAS,
            address=Op.ADDRESS,
            args_offset=0,
            args_size=32,
            address_warm=True,
        ),
        if_false=Op.REVERT(0, 0),
    )

    frame_code = frame_memory + Conditional(
        condition=Op.ISZERO(Op.CALLDATALOAD(0)),
        if_true=Op.POP(Op.CALL(gas=Op.GAS, address=leaf_address)),
        if_false=descend,
    )
    entry_address = pre.deploy_contract(code=frame_code)

    # gas_cost counts both Conditional branches and a cold leaf CALL,
    # so the estimate is conservative and the clamp errs shallow.
    mem_expansion = fork.memory_expansion_gas_calculator()
    frame_gas = frame_code.gas_cost(fork) + mem_expansion(new_bytes=mem_size)

    def reachable_depth(execution_gas: int) -> int:
        """Return the deepest frame the budget can fund."""
        frames = 0
        while True:
            forwarded_gas = execution_gas - frame_gas
            forwarded_gas -= forwarded_gas // 64
            if forwarded_gas < frame_gas:
                return frames
            execution_gas = forwarded_gas
            frames += 1

    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        calldata=b"\xff" * 32,
        return_cost_deducted_prior_execution=True,
    )

    txs = []
    remaining_gas = gas_benchmark_value
    while remaining_gas > 0:
        execution_gas = min(tx_gas_limit, remaining_gas)
        remaining_gas -= execution_gas
        if execution_gas < intrinsic_gas + frame_gas:
            break
        txs.append(
            Transaction(
                to=entry_address,
                gas_limit=execution_gas,
                data=Hash(
                    min(
                        depth,
                        reachable_depth(execution_gas - intrinsic_gas),
                    )
                ),
                sender=pre.fund_eoa(),
            )
        )

    benchmark_test(
        blocks=[Block(txs=txs)],
        skip_gas_used_validation=True,
        verifications=[ReceiptStatusExpected()],
    )

Parametrized Test Cases

This test generates 9 parametrized test cases across 4 forks.