Skip to content

test_clz_code_copy_operation()

Documentation for tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py::test_clz_code_copy_operation@88e9fb8f.

Generate fixtures for these test cases for Osaka with:

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

Test CLZ opcode with code copy operation.

Source code in tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
@pytest.mark.valid_from("Osaka")
@pytest.mark.parametrize("bits", [0, 64, 255])
@pytest.mark.parametrize("opcode", [Op.CODECOPY, Op.EXTCODECOPY])
def test_clz_code_copy_operation(
    state_test: StateTestFiller, pre: Alloc, bits: int, opcode: Op
) -> None:
    """Test CLZ opcode with code copy operation."""
    storage = Storage()

    expected_value = 255 - bits
    clz_code_offset = len(Op.CLZ(1 << bits)) - 1  # Offset to CLZ opcode

    mload_value = Spec.CLZ << 248  # CLZ opcode in MSB position (0x1E000...000)

    target_address = pre.deploy_contract(code=Op.CLZ(1 << bits))

    clz_contract_address = pre.deploy_contract(
        code=(
            Op.CLZ(1 << bits)  # Calculate CLZ of the value
            + Op.SSTORE(storage.store_next(expected_value), Op.CLZ(1 << bits))  # Store CLZ result
            + (  # Load CLZ byte from code with CODECOPY or EXTCODECOPY
                Op.CODECOPY(dest_offset=0, offset=clz_code_offset, size=1)
                if opcode == Op.CODECOPY
                else Op.EXTCODECOPY(
                    address=target_address, dest_offset=0, offset=clz_code_offset, size=1
                )
            )
            # Store loaded CLZ byte
            + Op.SSTORE(storage.store_next(mload_value), Op.MLOAD(0))
        ),
        storage={"0x00": "0xdeadbeef"},
    )

    post = {
        clz_contract_address: Account(
            storage={
                "0x00": expected_value,
                "0x01": mload_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