Skip to content

test_worst_create()

Documentation for tests/benchmark/test_worst_bytecode.py::test_worst_create@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

fill -v tests/benchmark/test_worst_bytecode.py::test_worst_create -m benchmark

Test the CREATE and CREATE2 performance with different configurations.

Source code in tests/benchmark/test_worst_bytecode.py
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
@pytest.mark.parametrize(
    "opcode",
    [
        Op.CREATE,
        Op.CREATE2,
    ],
)
@pytest.mark.parametrize(
    "max_code_size_ratio, non_zero_data, value",
    [
        # To avoid a blowup of combinations, the value dimension is only
        # explored for the non-zero data case, so isn't affected by code size
        # influence.
        pytest.param(0, False, 0, id="0 bytes without value"),
        pytest.param(0, False, 1, id="0 bytes with value"),
        pytest.param(0.25, True, 0, id="0.25x max code size with non-zero data"),
        pytest.param(0.25, False, 0, id="0.25x max code size with zero data"),
        pytest.param(0.50, True, 0, id="0.50x max code size with non-zero data"),
        pytest.param(0.50, False, 0, id="0.50x max code size with zero data"),
        pytest.param(0.75, True, 0, id="0.75x max code size with non-zero data"),
        pytest.param(0.75, False, 0, id="0.75x max code size with zero data"),
        pytest.param(1.00, True, 0, id="max code size with non-zero data"),
        pytest.param(1.00, False, 0, id="max code size with zero data"),
    ],
)
def test_worst_create(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
    opcode: Op,
    max_code_size_ratio: float,
    non_zero_data: bool,
    value: int,
) -> None:
    """
    Test the CREATE and CREATE2 performance with different configurations.
    """
    max_code_size = fork.max_code_size()

    code_size = int(max_code_size * max_code_size_ratio)

    # Deploy the initcode template which has following design:
    # ```
    # PUSH3(code_size)
    # [CODECOPY(DUP1) -- Conditional that non_zero_data is True]
    # RETURN(0, DUP1)
    # [<pad to code_size>] -- Conditional that non_zero_data is True]
    # ```
    code = (
        Op.PUSH3(code_size)
        + (Op.CODECOPY(size=Op.DUP1) if non_zero_data else Bytecode())
        + Op.RETURN(0, Op.DUP1)
    )
    if non_zero_data:  # Pad to code_size.
        code += bytes([i % 256 for i in range(code_size - len(code))])

    initcode_template_contract = pre.deploy_contract(code=code)

    # Create the benchmark contract which has the following design:
    # ```
    # PUSH(value)
    # [EXTCODECOPY(full initcode_template_contract)
    # -> Conditional that non_zero_data is True]
    #
    # JUMPDEST (#)
    # (CREATE|CREATE2)
    # (CREATE|CREATE2)
    # ...
    # JUMP(#)
    # ```
    setup = (
        Op.PUSH3(code_size)
        + Op.PUSH1(value)
        + Op.EXTCODECOPY(
            address=initcode_template_contract,
            size=Op.DUP2,  # DUP2 refers to the EXTCODESIZE value above.
        )
    )

    if opcode == Op.CREATE2:
        # For CREATE2, we provide an initial salt.
        setup += Op.PUSH1(42)

    attack_block = (
        # For CREATE:
        # - DUP2 refers to the EXTOCODESIZE value  pushed in code_prefix.
        # - DUP3 refers to PUSH1(value) above.
        Op.POP(Op.CREATE(value=Op.DUP3, offset=0, size=Op.DUP2))
        if opcode == Op.CREATE
        # For CREATE2: we manually push the arguments because we leverage the
        # return value of previous CREATE2 calls as salt for the next CREATE2
        # call.
        # - DUP4 is targeting the PUSH1(value) from the code_prefix.
        # - DUP3 is targeting the EXTCODESIZE value pushed in code_prefix.
        else Op.DUP3 + Op.PUSH0 + Op.DUP4 + Op.CREATE2
    )

    code = JumpLoopGenerator(setup=setup, attack_block=attack_block).generate_repeated_code(
        repeated_code=attack_block, setup=setup, fork=fork
    )

    tx = Transaction(
        # Set enough balance in the pre-alloc for `value > 0` configurations.
        to=pre.deploy_contract(code=code, balance=1_000_000_000 if value > 0 else 0),
        sender=pre.fund_eoa(),
    )

    benchmark_test(tx=tx)

Parametrized Test Cases

This test case is only parametrized by fork.

Test ID (Abbreviated) max_code_size_ratio non_zero_data value opcode
...fork_Prague-blockchain_test-0 bytes without value-opcode_CREATE 0 False 0 CREATE
...fork_Prague-blockchain_test-0 bytes without value-opcode_CREATE2 0 False 0 CREATE2
...fork_Prague-blockchain_test-0 bytes with value-opcode_CREATE 0 False 1 CREATE
...fork_Prague-blockchain_test-0 bytes with value-opcode_CREATE2 0 False 1 CREATE2
...fork_Prague-blockchain_test-0.25x max code size with non-zero data-opcode_CREATE 0.25 True 0 CREATE
...fork_Prague-blockchain_test-0.25x max code size with non-zero data-opcode_CREATE2 0.25 True 0 CREATE2
...fork_Prague-blockchain_test-0.25x max code size with zero data-opcode_CREATE 0.25 False 0 CREATE
...fork_Prague-blockchain_test-0.25x max code size with zero data-opcode_CREATE2 0.25 False 0 CREATE2
...fork_Prague-blockchain_test-0.50x max code size with non-zero data-opcode_CREATE 0.5 True 0 CREATE
...fork_Prague-blockchain_test-0.50x max code size with non-zero data-opcode_CREATE2 0.5 True 0 CREATE2
...fork_Prague-blockchain_test-0.50x max code size with zero data-opcode_CREATE 0.5 False 0 CREATE
...fork_Prague-blockchain_test-0.50x max code size with zero data-opcode_CREATE2 0.5 False 0 CREATE2
...fork_Prague-blockchain_test-0.75x max code size with non-zero data-opcode_CREATE 0.75 True 0 CREATE
...fork_Prague-blockchain_test-0.75x max code size with non-zero data-opcode_CREATE2 0.75 True 0 CREATE2
...fork_Prague-blockchain_test-0.75x max code size with zero data-opcode_CREATE 0.75 False 0 CREATE
...fork_Prague-blockchain_test-0.75x max code size with zero data-opcode_CREATE2 0.75 False 0 CREATE2
...fork_Prague-blockchain_test-max code size with non-zero data-opcode_CREATE 1.0 True 0 CREATE
...fork_Prague-blockchain_test-max code size with non-zero data-opcode_CREATE2 1.0 True 0 CREATE2
...fork_Prague-blockchain_test-max code size with zero data-opcode_CREATE 1.0 False 0 CREATE
...fork_Prague-blockchain_test-max code size with zero data-opcode_CREATE2 1.0 False 0 CREATE2
...fork_Osaka-blockchain_test-0 bytes without value-opcode_CREATE 0 False 0 CREATE
...fork_Osaka-blockchain_test-0 bytes without value-opcode_CREATE2 0 False 0 CREATE2
...fork_Osaka-blockchain_test-0 bytes with value-opcode_CREATE 0 False 1 CREATE
...fork_Osaka-blockchain_test-0 bytes with value-opcode_CREATE2 0 False 1 CREATE2
...fork_Osaka-blockchain_test-0.25x max code size with non-zero data-opcode_CREATE 0.25 True 0 CREATE
...fork_Osaka-blockchain_test-0.25x max code size with non-zero data-opcode_CREATE2 0.25 True 0 CREATE2
...fork_Osaka-blockchain_test-0.25x max code size with zero data-opcode_CREATE 0.25 False 0 CREATE
...fork_Osaka-blockchain_test-0.25x max code size with zero data-opcode_CREATE2 0.25 False 0 CREATE2
...fork_Osaka-blockchain_test-0.50x max code size with non-zero data-opcode_CREATE 0.5 True 0 CREATE
...fork_Osaka-blockchain_test-0.50x max code size with non-zero data-opcode_CREATE2 0.5 True 0 CREATE2
...fork_Osaka-blockchain_test-0.50x max code size with zero data-opcode_CREATE 0.5 False 0 CREATE
...fork_Osaka-blockchain_test-0.50x max code size with zero data-opcode_CREATE2 0.5 False 0 CREATE2
...fork_Osaka-blockchain_test-0.75x max code size with non-zero data-opcode_CREATE 0.75 True 0 CREATE
...fork_Osaka-blockchain_test-0.75x max code size with non-zero data-opcode_CREATE2 0.75 True 0 CREATE2
...fork_Osaka-blockchain_test-0.75x max code size with zero data-opcode_CREATE 0.75 False 0 CREATE
...fork_Osaka-blockchain_test-0.75x max code size with zero data-opcode_CREATE2 0.75 False 0 CREATE2
...fork_Osaka-blockchain_test-max code size with non-zero data-opcode_CREATE 1.0 True 0 CREATE
...fork_Osaka-blockchain_test-max code size with non-zero data-opcode_CREATE2 1.0 True 0 CREATE2
...fork_Osaka-blockchain_test-max code size with zero data-opcode_CREATE 1.0 False 0 CREATE
...fork_Osaka-blockchain_test-max code size with zero data-opcode_CREATE2 1.0 False 0 CREATE2