Skip to content

test_selfdestruct_existing()

Documentation for tests/benchmark/compute/instruction/test_system.py::test_selfdestruct_existing@8db70f93.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/benchmark/compute/instruction/test_system.py::test_selfdestruct_existing --gas-benchmark-values 1

Benchmark SELFDESTRUCT instruction for existing contracts.

Source code in tests/benchmark/compute/instruction/test_system.py
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
@pytest.mark.parametrize("value_bearing", [True, False])
def test_selfdestruct_existing(
    benchmark_test: BenchmarkTestFiller,
    pre: Alloc,
    value_bearing: bool,
    fork: Fork,
    gas_benchmark_value: int,
) -> None:
    """Benchmark SELFDESTRUCT instruction for existing contracts."""
    selfdestructable_contract = Op.SELFDESTRUCT(Op.CALLER, address_warm=True)

    # Initcode
    initcode = (
        Op.MSTORE8(
            0,
            Op.CALLER.int(),
            # gas accounting
            old_memory_size=0,
            new_memory_size=2,
        )
        + Op.MSTORE8(1, Op.SELFDESTRUCT.int())
        + Op.RETURN(0, 2, code_deposit_size=2)
    )

    # Factory Contract Setup
    # CALLDATA[0:32] = start index
    # CALLDATA[32:64] = end index
    factory_setup = (
        Op.MSTORE(
            0,
            initcode.hex(),
            # Gas accounting
            old_memory_size=0,
            new_memory_size=32,
        )
        + Op.ADD(1, Op.CALLDATALOAD(32))
        + Op.CALLDATALOAD(0)
    )

    factory_iterating = While(
        body=Op.POP(
            Op.CREATE2(
                value=1 if value_bearing else 0,
                offset=32 - len(initcode),
                size=len(initcode),
                salt=Op.DUP1,
                # gas accounting
                init_code_size=len(initcode),
            )
        ),
        condition=Op.PUSH1(1) + Op.ADD + Op.DUP1 + Op.DUP3 + Op.GT,
    )

    factory_code = IteratingBytecode(
        setup=factory_setup,
        iterating=factory_iterating,
        iterating_subcall=initcode,
        cleanup=Op.STOP,
    )

    factory_address = pre.deploy_contract(
        code=factory_code,
        balance=10**18,
    )

    create2_preimage = Create2PreimageLayout(
        factory_address=factory_address,
        salt=Op.CALLDATALOAD(0),
        init_code_hash=initcode.keccak256(),
    )

    # Attack Contract Setup
    # CALLDATA[0:32] = start index
    # CALLDATA[32:64] = end index
    attack_setup = (
        create2_preimage + Op.ADD(1, Op.CALLDATALOAD(32)) + Op.CALLDATALOAD(0)
    )

    loop = While(
        body=Op.POP(
            Op.CALL(
                address=create2_preimage.address_op(),
                address_warm=False,
            )
        )
        + create2_preimage.increment_salt_op(),
        condition=Op.PUSH1(1) + Op.ADD + Op.DUP1 + Op.DUP3 + Op.GT,
    )

    attack_code = IteratingBytecode(
        setup=attack_setup,
        iterating=loop,
        iterating_subcall=selfdestructable_contract,
        cleanup=Op.STOP,
    )

    attack_code_address = pre.deploy_contract(code=attack_code)

    def calldata(iteration_count: int, start_iteration: int) -> bytes:
        index_end = iteration_count + start_iteration - 1
        return Hash(start_iteration) + Hash(index_end)

    # Compute iteration counts and expected gas from the gas model.
    iteration_counts = list(
        attack_code.tx_iterations_by_gas_limit(
            fork=fork,
            gas_limit=gas_benchmark_value,
            calldata=calldata,
        )
    )
    num_contracts = sum(iteration_counts)

    start = 0
    total_gas_cost = 0
    for iters in iteration_counts:
        total_gas_cost += attack_code.tx_gas_cost_by_iteration_count(
            fork=fork,
            iteration_count=iters,
            start_iteration=start,
            calldata=calldata,
        )
        start += iters

    def factory_calldata(iteration_count: int, start_iteration: int) -> bytes:
        index_end = iteration_count + start_iteration - 1
        return Hash(start_iteration) + Hash(index_end)

    with TestPhaseManager.setup():
        setup_sender = pre.fund_eoa()
        setup_txs = list(
            factory_code.transactions_by_total_iteration_count(
                fork=fork,
                total_iterations=num_contracts,
                sender=setup_sender,
                to=factory_address,
                calldata=factory_calldata,
            )
        )

    with TestPhaseManager.execution():
        attack_sender = pre.fund_eoa()
        exec_txs = list(
            attack_code.transactions_by_gas_limit(
                fork=fork,
                gas_limit=gas_benchmark_value,
                sender=attack_sender,
                to=attack_code_address,
                calldata=calldata,
            )
        )

    post = {}
    for i in range(num_contracts):
        deployed_contract_address = compute_create2_address(
            address=factory_address,
            salt=i,
            initcode=initcode,
        )
        post[deployed_contract_address] = Account(nonce=1)

    post[attack_code_address] = Account(
        balance=num_contracts if value_bearing else 0
    )

    benchmark_test(
        post=post,
        target_opcode=Op.SELFDESTRUCT,
        blocks=[
            Block(txs=setup_txs),
            Block(txs=exec_txs),
        ],
        expected_benchmark_gas_used=total_gas_cost,
    )

Parametrized Test Cases

This test generates 2 parametrized test cases across 3 forks.