Skip to content

test_clz_with_memory_operation()

Documentation for tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py::test_clz_with_memory_operation@ffe48572.

Generate fixtures for these test cases for Bogota with:

fill -v tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py::test_clz_with_memory_operation --fork Bogota

Test CLZ opcode with memory operation.

Source code in tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py
@pytest.mark.valid_from("Osaka")
@pytest.mark.parametrize("bits", [0, 64, 255])
@pytest.mark.parametrize("opcode", [Op.CODECOPY, Op.EXTCODECOPY])
def test_clz_with_memory_operation(
    state_test: StateTestFiller, pre: Alloc, bits: int, opcode: Op
) -> None:
    """Test CLZ opcode with memory operation."""
    storage = Storage()

    expected_value = 255 - bits

    # Target code pattern:
    #   PUSH32 (1 << bits)
    #   PUSH0
    #   MSTORE
    #
    # This sequence stores a 32-byte value in memory.
    # Later, we copy the immediate value from the PUSH32 instruction into
    # memory using CODECOPY or EXTCODECOPY, and then load it with MLOAD for
    # the CLZ test.
    target_code = Op.PUSH32(1 << bits)
    offset = 1

    target_address = pre.deploy_contract(code=target_code)

    clz_contract_address = pre.deploy_contract(
        code=(
            target_code
            + Op.SSTORE(
                storage.store_next(expected_value), Op.CLZ(1 << bits)
            )  # Store CLZ result
            + (
                Op.CODECOPY(dest_offset=0, offset=offset, size=0x20)
                if opcode == Op.CODECOPY
                else Op.EXTCODECOPY(
                    address=target_address,
                    dest_offset=0,
                    offset=offset,
                    size=0x20,
                )
            )
            + Op.SSTORE(
                storage.store_next(expected_value), Op.CLZ(Op.MLOAD(0))
            )
        ),
        storage={"0x00": "0xdeadbeef"},
    )

    post = {
        clz_contract_address: Account(
            storage={"0x00": expected_value, "0x01": expected_value}
        ),
    }

    tx = Transaction(to=clz_contract_address, sender=pre.fund_eoa())

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

Parametrized Test Cases

This test generates 6 parametrized test cases across 3 forks.