Skip to content

test_worst_selfdestruct_initcode()

Documentation for tests/benchmark/test_worst_stateful_opcodes.py::test_worst_selfdestruct_initcode@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

fill -v tests/benchmark/test_worst_stateful_opcodes.py::test_worst_selfdestruct_initcode -m benchmark

Test running a block with as many SELFDESTRUCTs as possible executed in initcode.

Source code in tests/benchmark/test_worst_stateful_opcodes.py
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
@pytest.mark.parametrize("value_bearing", [True, False])
def test_worst_selfdestruct_initcode(
    state_test: StateTestFiller,
    pre: Alloc,
    value_bearing: bool,
    fork: Fork,
    env: Environment,
    gas_benchmark_value: int,
) -> None:
    """
    Test running a block with as many SELFDESTRUCTs as possible executed in
    initcode.
    """
    fee_recipient = pre.fund_eoa(amount=1)
    env.fee_recipient = fee_recipient

    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_BASE  # COINBASE
        + gas_costs.G_SELF_DESTRUCT
    )
    create_costs = (
        initcode_costs
        + gas_costs.G_CREATE
        + gas_costs.G_VERY_LOW * 3  # Create Parameter PUSHs
        + gas_costs.G_INITCODE_WORD
    )
    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 + 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

    initcode = Op.SELFDESTRUCT(Op.COINBASE)
    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.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=100_000, storage={0: 1})
    code_tx = Transaction(
        to=code_addr,
        gas_limit=gas_benchmark_value,
        gas_price=10,
        sender=pre.fund_eoa(),
    )

    post = {code_addr: Account(storage={0: 42})}  # Check for successful
    # execution.
    state_test(
        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_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