Skip to content

test_clz_call_operation()

Documentation for tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py::test_clz_call_operation@20373115.

Generate fixtures for these test cases for Amsterdam with:

fill -v tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py::test_clz_call_operation --fork Amsterdam

Test CLZ opcode with call operation.

Source code in tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py
680
681
682
683
684
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
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
@EIPChecklist.Opcode.Test.ExecutionContext.Call()
@EIPChecklist.Opcode.Test.ExecutionContext.Delegatecall()
@EIPChecklist.Opcode.Test.ExecutionContext.Callcode()
@EIPChecklist.Opcode.Test.ExecutionContext.Staticcall()
@pytest.mark.valid_from("Osaka")
@pytest.mark.parametrize(
    "opcode,context",
    [
        pytest.param(Op.CALL, CallingContext.callee_context, id="call"),
        pytest.param(
            Op.DELEGATECALL, CallingContext.caller_context, id="delegatecall"
        ),
        pytest.param(
            Op.CALLCODE, CallingContext.caller_context, id="callcode"
        ),
        pytest.param(
            Op.STATICCALL, CallingContext.no_context, id="staticcall"
        ),
    ],
)
def test_clz_call_operation(
    state_test: StateTestFiller,
    pre: Alloc,
    opcode: Op,
    context: CallingContext,
) -> None:
    """Test CLZ opcode with call operation."""
    test_cases = [0, 64, 255]

    # Storage Layout
    callee_storage = Storage()
    caller_storage = Storage()

    callee_code = Bytecode()

    for bits in reversed(test_cases):
        callee_code += Op.CLZ(1 << bits)

    if context != CallingContext.no_context:
        for bits in test_cases:
            callee_code += Op.SSTORE(
                callee_storage.store_next(255 - bits), Op.CLZ(1 << bits)
            )

    for i in range(len(test_cases)):
        callee_code += Op.PUSH32(i * 0x20) + Op.MSTORE

    callee_code += Op.RETURN(0, len(test_cases) * 0x20)

    callee_address = pre.deploy_contract(code=callee_code)

    caller_code = opcode(
        gas=0xFFFF,
        address=callee_address,
        ret_offset=0,
        ret_size=len(test_cases) * 0x20,
    )

    for i, bits in enumerate(test_cases):
        caller_code += Op.SSTORE(
            caller_storage.store_next(255 - bits), Op.MLOAD(i * 0x20)
        )

    caller_address = pre.deploy_contract(code=caller_code)

    tx = Transaction(
        to=caller_address,
        sender=pre.fund_eoa(),
        gas_limit=200_000,
    )

    post = {}

    if context == CallingContext.caller_context:
        post[caller_address] = Account(storage=callee_storage)
    elif context == CallingContext.callee_context:
        post[callee_address] = Account(storage=callee_storage)
        post[caller_address] = Account(storage=caller_storage)
    elif context == CallingContext.no_context:
        post[caller_address] = Account(storage=caller_storage)

    state_test(pre=pre, post=post, tx=tx)

Parametrized Test Cases

This test generates 4 parametrized test cases across 2 forks.