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@892e6d1e.

Generate fixtures for these test cases for Amsterdam with:

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

Test CLZ opcode with memory operation.

Source code in tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
@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

This test generates 6 parametrized test cases across 2 forks.