Skip to content

test_bloatnet_balance_extcodehash()

Documentation for tests/benchmark/stateful/bloatnet/test_multi_opcode.py::test_bloatnet_balance_extcodehash@7b8124a7.

Generate fixtures for these test cases for Osaka with:

fill -v tests/benchmark/stateful/bloatnet/test_multi_opcode.py::test_bloatnet_balance_extcodehash --fork Osaka

Benchmark BALANACE and EXTCODEHASH combination on bloatnet.

Source code in tests/benchmark/stateful/bloatnet/test_multi_opcode.py
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
@pytest.mark.parametrize(
    "balance_first",
    [True, False],
    ids=["balance_extcodehash", "extcodehash_balance"],
)
def test_bloatnet_balance_extcodehash(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    fork: Fork,
    gas_benchmark_value: int,
    tx_gas_limit: int,
    balance_first: bool,
) -> None:
    """Benchmark BALANACE and EXTCODEHASH combination on bloatnet."""
    # Stub Account
    factory_address = pre.deploy_contract(
        code=Bytecode(),  # Required parameter, but will be ignored for stubs
        stub="bloatnet_factory",
    )

    # Contract Construction
    setup = Bytecode()

    setup += Conditional(
        condition=Op.STATICCALL(
            gas=Op.GAS,
            address=factory_address,
            args_offset=0,
            args_size=0,
            ret_offset=96,
            ret_size=64,
            # gas accounting
            address_warm=False,
            old_memory_size=0,
            new_memory_size=160,
        ),
        if_false=Op.INVALID,
    )

    create2_preimage = Create2PreimageLayout(
        factory_address=factory_address,
        salt=Op.CALLDATALOAD(32),
        init_code_hash=Op.MLOAD(128),
        old_memory_size=160,
    )

    setup += create2_preimage
    setup += Op.CALLDATALOAD(0)  # [num_contract]

    balance_op = Op.POP(Op.BALANCE)
    extcodehash_op = Op.POP(Op.EXTCODEHASH)
    benchmark_ops = (
        (balance_op + extcodehash_op)
        if balance_first
        else (extcodehash_op + balance_op)
    )

    loop = While(
        body=(
            create2_preimage.address_op()
            + Op.DUP1
            + benchmark_ops
            + create2_preimage.increment_salt_op()
        ),
        condition=Op.PUSH1(1)  # [1, num_contract]
        + Op.SWAP1  # [num_contract, 1]
        + Op.SUB  # [num_contract-1]
        + Op.DUP1  # [num_contract-1, num_contract-1]
        + Op.ISZERO  # [num_contract-1==0, num_contract-1]
        + Op.ISZERO,  # [num_contract-1!=0, num_contract-1]
    )

    # Contract Deployment
    code = setup + loop
    attack_contract_address = pre.deploy_contract(code=code)

    # Gas Accounting
    setup_cost = setup.gas_cost(fork)
    loop_cost = loop.gas_cost(fork)
    intrinsic_gas = fork.transaction_intrinsic_cost_calculator()(
        calldata=b"\xff" * 64
    )

    # Attack Loop
    gas_remaining = gas_benchmark_value
    txs = []
    salt_offset = 0

    while gas_remaining > intrinsic_gas + setup_cost + loop_cost:
        gas_available = min(gas_remaining, tx_gas_limit)

        if gas_available < intrinsic_gas + setup_cost:
            break

        num_contract = (
            gas_available - intrinsic_gas - setup_cost
        ) // loop_cost

        if num_contract == 0:
            break

        calldata = Hash(num_contract) + Hash(salt_offset)

        txs.append(
            Transaction(
                gas_limit=gas_available,
                data=calldata,
                to=attack_contract_address,
                sender=pre.fund_eoa(),
            )
        )

        gas_remaining -= gas_available
        salt_offset += num_contract

    benchmark_test(
        pre=pre,
        blocks=[Block(txs=txs)],
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 2 forks.