Skip to content

test_worst_selfdestruct_initcode()

Documentation for tests/zkevm/test_worst_stateful_opcodes.py::test_worst_selfdestruct_initcode@64f949d0.

Generate fixtures for these test cases for Prague with:

fill -v tests/zkevm/test_worst_stateful_opcodes.py::test_worst_selfdestruct_initcode --fork Prague

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

Source code in tests/zkevm/test_worst_stateful_opcodes.py
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
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
@pytest.mark.valid_from("Cancun")
@pytest.mark.parametrize("value_bearing", [True, False])
def test_worst_selfdestruct_initcode(
    state_test: StateTestFiller,
    pre: Alloc,
    value_bearing: bool,
    fork: Fork,
):
    """Test running a block with as many SELFDESTRUCTs as possible executed in initcode."""
    env = Environment()
    pre.fund_address(env.fee_recipient, 1)

    initcode = Op.SELFDESTRUCT(Op.COINBASE)
    gas_costs = fork.gas_costs()
    create_gas = gas_costs.G_CREATE + 20_000
    code = (
        Op.MSTORE(0, initcode.hex())
        + While(
            body=Op.POP(
                Op.CREATE(
                    value=1 if value_bearing else 0,
                    offset=32 - len(initcode),
                    size=len(initcode),
                )
            ),
            # Stop before we run out of gas for the whole tx execution.
            # The value was found by trial-error rounded to the next 1000 multiple.
            condition=Op.GT(Op.GAS, create_gas),
        )
        + Op.SSTORE(0, 42)  # Done for successful tx execution assertion below.
    )
    # 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=env.gas_limit,
        gas_price=10,
        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,
    )

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