Skip to content

test_clz_jump_operation()

Documentation for tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py::test_clz_jump_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_jump_operation --fork Amsterdam

Test CLZ opcode with valid and invalid jump.

Source code in tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
@pytest.mark.valid_from("Osaka")
@pytest.mark.parametrize(
    "opcode,jumpi_condition",
    [
        (op, cond)
        for op in [Op.JUMPI, Op.JUMP]
        for cond in [True, False]
        if not (op == Op.JUMP and not cond)
    ],
)
@pytest.mark.parametrize("valid_jump", [True, False])
@pytest.mark.parametrize("bits", [0, 16, 64, 128, 255])
@pytest.mark.eels_base_coverage
def test_clz_jump_operation(
    state_test: StateTestFiller,
    pre: Alloc,
    opcode: Op,
    valid_jump: bool,
    jumpi_condition: bool,
    bits: int,
) -> None:
    """Test CLZ opcode with valid and invalid jump."""
    code = Op.PUSH32(1 << bits)

    if opcode == Op.JUMPI:
        code += Op.PUSH1(jumpi_condition)

    code += Op.PUSH1(len(code) + 3) + opcode

    if valid_jump:
        code += Op.JUMPDEST

    code += Op.CLZ + Op.PUSH0 + Op.SSTORE + Op.RETURN(0, 0)

    callee_address = pre.deploy_contract(code=code)

    caller_address = pre.deploy_contract(
        code=Op.SSTORE(0, Op.CALL(gas=0xFFFF, address=callee_address)),
        storage={"0x00": "0xdeadbeef"},
    )

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

    expected_clz = 255 - bits

    post = {
        caller_address: Account(
            storage={"0x00": 1 if valid_jump or not jumpi_condition else 0}
        ),
    }

    if valid_jump or not jumpi_condition:
        post[callee_address] = Account(storage={"0x00": expected_clz})

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

Parametrized Test Cases

This test generates 30 parametrized test cases across 2 forks.