Skip to content

test_worst_selfdestruct_created()

Documentation for tests/benchmark/test_worst_stateful_opcodes.py::test_worst_selfdestruct_created@v5.0.0.

Generate fixtures for these test cases for Osaka with:

fill -v tests/benchmark/test_worst_stateful_opcodes.py::test_worst_selfdestruct_created --fork Osaka

Test running a block with as many SELFDESTRUCTs as possible for deployed contracts in the same transaction.

Source code in tests/benchmark/test_worst_stateful_opcodes.py
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
@pytest.mark.valid_from("Cancun")
@pytest.mark.parametrize("value_bearing", [True, False])
def test_worst_selfdestruct_created(
    state_test: StateTestFiller,
    pre: Alloc,
    value_bearing: bool,
    fork: Fork,
    env: Environment,
    gas_benchmark_value: int,
):
    """
    Test running a block with as many SELFDESTRUCTs as possible for deployed contracts in
    the same transaction.
    """
    fee_recipient = pre.fund_eoa(amount=1)
    env.fee_recipient = fee_recipient

    # SELFDESTRUCT(COINBASE) contract deployment
    initcode = (
        Op.MSTORE8(0, Op.COINBASE.int()) + Op.MSTORE8(1, Op.SELFDESTRUCT.int()) + Op.RETURN(0, 2)
    )
    gas_costs = fork.gas_costs()
    memory_expansion_calc = fork().memory_expansion_gas_calculator()
    intrinsic_gas_cost_calc = fork.transaction_intrinsic_cost_calculator()

    initcode_costs = (
        gas_costs.G_VERY_LOW * 8  # MSTOREs, PUSHs
        + memory_expansion_calc(new_bytes=2)  # return into memory
    )
    create_costs = (
        initcode_costs
        + gas_costs.G_CREATE
        + gas_costs.G_VERY_LOW * 3  # Create Parameter PUSHs
        + gas_costs.G_CODE_DEPOSIT_BYTE * 2
        + gas_costs.G_INITCODE_WORD
    )
    call_costs = (
        gas_costs.G_WARM_ACCOUNT_ACCESS
        + gas_costs.G_BASE  # COINBASE
        + gas_costs.G_SELF_DESTRUCT
        + gas_costs.G_VERY_LOW * 5  # CALL Parameter PUSHs
        + gas_costs.G_BASE  #  Parameter GAS
    )
    extra_costs = (
        gas_costs.G_BASE  # POP
        + gas_costs.G_VERY_LOW * 6  # PUSHs, ADD, DUP, GT
        + gas_costs.G_HIGH  # JUMPI
        + gas_costs.G_JUMPDEST
    )
    loop_cost = create_costs + call_costs + extra_costs

    prefix_cost = gas_costs.G_VERY_LOW * 3 + gas_costs.G_BASE + memory_expansion_calc(new_bytes=32)
    suffix_cost = gas_costs.G_COLD_SLOAD + gas_costs.G_STORAGE_RESET + (gas_costs.G_VERY_LOW * 2)

    base_costs = prefix_cost + suffix_cost + intrinsic_gas_cost_calc()

    iterations = (gas_benchmark_value - base_costs) // loop_cost

    code_prefix = Op.MSTORE(0, initcode.hex()) + Op.PUSH0 + Op.JUMPDEST
    code_suffix = (
        Op.SSTORE(0, 42)  # Done for successful tx execution assertion below.
        + Op.STOP
    )
    loop_body = (
        Op.POP(
            Op.CALL(
                address=Op.CREATE(
                    value=1 if value_bearing else 0,
                    offset=32 - len(initcode),
                    size=len(initcode),
                )
            )
        )
        + Op.PUSH1[1]
        + Op.ADD
        + Op.JUMPI(len(code_prefix) - 1, Op.GT(iterations, Op.DUP1))
    )
    code = code_prefix + loop_body + code_suffix
    # The 0 storage slot is initialize to avoid creation costs in SSTORE above.
    code_addr = pre.deploy_contract(
        code=code,
        balance=iterations if value_bearing else 0,
        storage={0: 1},
    )
    code_tx = Transaction(
        to=code_addr,
        gas_limit=gas_benchmark_value,
        sender=pre.fund_eoa(),
    )

    post = {code_addr: Account(storage={0: 42})}  # Check for successful execution.
    state_test(
        env=env,
        pre=pre,
        post=post,
        tx=code_tx,
        expected_benchmark_gas_used=iterations * loop_cost + base_costs,
    )

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) value_bearing
...fork_Cancun-state_test-value_bearing_True True
...fork_Cancun-state_test-value_bearing_False False
...fork_Cancun-blockchain_test_from_state_test-value_bearing_True True
...fork_Cancun-blockchain_test_from_state_test-value_bearing_False False
...fork_Prague-state_test-value_bearing_True True
...fork_Prague-state_test-value_bearing_False False
...fork_Prague-blockchain_test_from_state_test-value_bearing_True True
...fork_Prague-blockchain_test_from_state_test-value_bearing_False False
...fork_Osaka-state_test-value_bearing_True True
...fork_Osaka-state_test-value_bearing_False False
...fork_Osaka-blockchain_test_from_state_test-value_bearing_True True
...fork_Osaka-blockchain_test_from_state_test-value_bearing_False False