Skip to content

test_worst_selfdestruct_created()

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

Generate fixtures for these test cases for Prague with:

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

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

Source code in tests/zkevm/test_worst_stateful_opcodes.py
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
@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,
):
    """
    Test running a block with as many SELFDESTRUCTs as possible for deployed contracts in
    the same transaction.
    """
    env = Environment()
    pre.fund_address(env.fee_recipient, 1)

    # 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()
    create_gas = gas_costs.G_CREATE + 20_000
    code = (
        Op.MSTORE(0, initcode.hex())
        + While(
            body=Op.POP(
                Op.CALL(
                    address=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