Skip to content

Commit e88a211

Browse files
feat(tests): add test for CLZ opcode with varying gas costs (#1859)
* feat(tests): add test for CLZ opcode with varying gas costs * refactor(tests): rename and update CLZ gas cost test to handle boundary conditions * refactor(tests): rename CLZ gas cost test for clarity * refactor(tests): parameterize for varying leading zero lengths * refactor(tests): update contract deployment to include storage
1 parent f62890f commit e88a211

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

tests/osaka/eip7939_count_leading_zeros/test_count_leading_zeros.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def test_clz_opcode_scenarios(
108108

109109

110110
@pytest.mark.valid_from("Osaka")
111-
def test_clz_gas(state_test: StateTestFiller, pre: Alloc, fork: Fork):
111+
def test_clz_gas_cost(state_test: StateTestFiller, pre: Alloc, fork: Fork):
112112
"""Test CLZ opcode gas cost."""
113113
contract_address = pre.deploy_contract(
114114
Op.SSTORE(
@@ -131,6 +131,40 @@ def test_clz_gas(state_test: StateTestFiller, pre: Alloc, fork: Fork):
131131
state_test(pre=pre, post=post, tx=tx)
132132

133133

134+
@pytest.mark.valid_from("Osaka")
135+
@pytest.mark.parametrize("bits", [0, 64, 128, 255])
136+
@pytest.mark.parametrize("gas_cost_delta", [-2, -1, 0, 1, 2])
137+
def test_clz_gas_cost_boundary(
138+
state_test: StateTestFiller,
139+
pre: Alloc,
140+
fork: Fork,
141+
bits: int,
142+
gas_cost_delta: int,
143+
):
144+
"""Test CLZ opcode gas cost boundary."""
145+
code = Op.PUSH32(1 << bits) + Op.CLZ
146+
147+
contract_address = pre.deploy_contract(code=code)
148+
149+
call_code = Op.SSTORE(
150+
0,
151+
Op.CALL(
152+
gas=fork.gas_costs().G_VERY_LOW + Spec.CLZ_GAS_COST + gas_cost_delta,
153+
address=contract_address,
154+
),
155+
)
156+
call_address = pre.deploy_contract(
157+
code=call_code,
158+
storage={"0x00": "0xdeadbeef"},
159+
)
160+
161+
tx = Transaction(to=call_address, sender=pre.fund_eoa(), gas_limit=200_000)
162+
163+
post = {call_address: Account(storage={"0x00": 0 if gas_cost_delta < 0 else 1})}
164+
165+
state_test(pre=pre, post=post, tx=tx)
166+
167+
134168
@pytest.mark.valid_from("Osaka")
135169
def test_clz_stack_underflow(state_test: StateTestFiller, pre: Alloc):
136170
"""Test CLZ opcode with empty stack (should revert due to stack underflow)."""

0 commit comments

Comments
 (0)