Skip to content

feat(tests): extra stack operation test for CLZ opcode #1821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Alloc,
Block,
BlockchainTestFiller,
Bytecode,
CodeGasMeasure,
StateTestFiller,
Transaction,
Expand Down Expand Up @@ -155,6 +156,63 @@ def test_clz_stack_underflow(state_test: StateTestFiller, pre: Alloc):
state_test(pre=pre, post=post, tx=tx)


@pytest.mark.valid_from("Osaka")
def test_clz_stack_not_overflow(state_test: StateTestFiller, pre: Alloc, fork: Fork):
"""Test CLZ opcode never causes stack overflow."""
max_stack_items = fork.max_stack_height()

code = Bytecode()
post = {}

code += Op.PUSH0 * (max_stack_items - 2)

for i in range(256):
code += Op.PUSH1(i) + Op.CLZ(1 << i) + Op.SWAP1 + Op.SSTORE

code_address = pre.deploy_contract(code=code)

post[code_address] = Account(storage={i: 255 - i for i in range(256)})

tx = Transaction(
to=code_address,
sender=pre.fund_eoa(),
gas_limit=6_000_000,
)

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


@pytest.mark.valid_from("Osaka")
def test_clz_push_operation_same_value(state_test: StateTestFiller, pre: Alloc):
"""Test CLZ opcode returns the same value via different push operations."""
code = Bytecode()
post = {}

for bit in range(1, 33): # PUSH value
for push_n in range(bit, 33): # PUSHn opcode
op = getattr(Op, f"PUSH{push_n}")
key = 100 * bit + push_n
code += Op.SSTORE(key, Op.CLZ(op[1 << bit]))

code_address = pre.deploy_contract(code=code)

tx = Transaction(
to=code_address,
sender=pre.fund_eoa(),
gas_limit=30_000_000,
)

post = {
code_address: Account(
storage={
bit * 100 + push_n: 255 - bit for bit in range(1, 33) for push_n in range(bit, 33)
}
)
}

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


@pytest.mark.valid_at_transition_to("Osaka", subsequent_forks=True)
def test_clz_fork_transition(blockchain_test: BlockchainTestFiller, pre: Alloc):
"""Test CLZ opcode behavior at fork transition."""
Expand Down
Loading