Skip to content

Commit 96e85f8

Browse files
feat(tests): add initcode context extra test for clz opcode (#1831)
* feat(tests): add contract creation test case for clz opcode * wip: create and create2 test cases * fix: resolve create memory offset issue * refactor(tests): update clz initcode create test * refactor(tests): update create context
1 parent 810ff42 commit 96e85f8

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Environment,
1919
StateTestFiller,
2020
Transaction,
21+
compute_create_address,
2122
)
2223
from ethereum_test_tools.vm.opcode import Opcodes as Op
2324

@@ -469,3 +470,74 @@ def test_clz_with_memory_operation(state_test: StateTestFiller, pre: Alloc, bits
469470
)
470471

471472
state_test(pre=pre, post=post, tx=tx)
473+
474+
475+
@pytest.mark.valid_from("Osaka")
476+
def test_clz_initcode_context(state_test: StateTestFiller, pre: Alloc):
477+
"""Test CLZ opcode behavior when creating a contract."""
478+
bits = [0, 1, 64, 128, 255]
479+
480+
storage = Storage()
481+
482+
init_code = Bytecode()
483+
for bit in bits:
484+
init_code += Op.SSTORE(storage.store_next(255 - bit), Op.CLZ(1 << bit))
485+
486+
sender_address = pre.fund_eoa()
487+
488+
contract_address = compute_create_address(address=sender_address, nonce=0)
489+
490+
tx = Transaction(
491+
to=None,
492+
gas_limit=6_000_000,
493+
data=init_code,
494+
sender=sender_address,
495+
)
496+
497+
post = {
498+
contract_address: Account(storage=storage),
499+
}
500+
501+
state_test(pre=pre, post=post, tx=tx)
502+
503+
504+
@pytest.mark.valid_from("Osaka")
505+
@pytest.mark.parametrize("opcode", [Op.CREATE, Op.CREATE2])
506+
def test_clz_initcode_create(state_test: StateTestFiller, pre: Alloc, opcode: Op):
507+
"""Test CLZ opcode behavior when creating a contract."""
508+
bits = [0, 1, 64, 128, 255] # expected values: [255, 254, 191, 127, 0]
509+
510+
storage = Storage()
511+
ext_code = Bytecode()
512+
513+
for bit in bits:
514+
ext_code += Op.SSTORE(storage.store_next(255 - bit), Op.CLZ(1 << bit))
515+
516+
sender_address = pre.fund_eoa()
517+
518+
create_contract = (
519+
Op.CALLDATACOPY(offset=0, size=len(ext_code))
520+
+ opcode(offset=0, size=len(ext_code))
521+
+ Op.STOP
522+
)
523+
524+
factory_contract_address = pre.deploy_contract(code=create_contract)
525+
526+
created_contract_address = compute_create_address(
527+
address=factory_contract_address, nonce=1, initcode=ext_code, opcode=opcode
528+
)
529+
530+
tx = Transaction(
531+
to=factory_contract_address,
532+
gas_limit=200_000,
533+
data=ext_code,
534+
sender=sender_address,
535+
)
536+
537+
post = {
538+
created_contract_address: Account(
539+
storage=storage,
540+
),
541+
}
542+
543+
state_test(pre=pre, post=post, tx=tx)

0 commit comments

Comments
 (0)