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@e9958ed2.

Generate fixtures for these test cases for Osaka with:

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

Test CLZ opcode with memory operation.

Source code in tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
@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(),
        gas_limit=200_000,
    )

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

Parametrized Test Cases

The interactive table below is also available as a standalone page.

Test ID (Abbreviated) opcode bits
...fork_Osaka-state_test-opcode_CODECOPY-bits_0 CODECOPY 0
...fork_Osaka-state_test-opcode_CODECOPY-bits_64 CODECOPY 64
...fork_Osaka-state_test-opcode_CODECOPY-bits_255 CODECOPY 255
...fork_Osaka-state_test-opcode_EXTCODECOPY-bits_0 EXTCODECOPY 0
...fork_Osaka-state_test-opcode_EXTCODECOPY-bits_64 EXTCODECOPY 64
...fork_Osaka-state_test-opcode_EXTCODECOPY-bits_255 EXTCODECOPY 255
...fork_Osaka-blockchain_test_from_state_test-opcode_CODECOPY-bits_0 CODECOPY 0
...fork_Osaka-blockchain_test_from_state_test-opcode_CODECOPY-bits_64 CODECOPY 64
...fork_Osaka-blockchain_test_from_state_test-opcode_CODECOPY-bits_255 CODECOPY 255
...fork_Osaka-blockchain_test_from_state_test-opcode_EXTCODECOPY-bits_0 EXTCODECOPY 0
...fork_Osaka-blockchain_test_from_state_test-opcode_EXTCODECOPY-bits_64 EXTCODECOPY 64
...fork_Osaka-blockchain_test_from_state_test-opcode_EXTCODECOPY-bits_255 EXTCODECOPY 255