diff --git a/scripts/utils/kakarot.py b/scripts/utils/kakarot.py index 2024d696a..33c7411e4 100644 --- a/scripts/utils/kakarot.py +++ b/scripts/utils/kakarot.py @@ -135,7 +135,7 @@ async def deploy( max_fee=max_fee, value=value, ) - contract.address = Web3.to_checksum_address(evm_address) + contract.address = Web3.to_checksum_address(f"0x{evm_address:040x}") contract.starknet_address = starknet_address logger.info(f"✅ {contract_name} deployed at address {contract.address}") diff --git a/solidity_contracts/src/PlainOpcodes/Counter.sol b/solidity_contracts/src/PlainOpcodes/Counter.sol index 90fc70c4d..68d820136 100644 --- a/solidity_contracts/src/PlainOpcodes/Counter.sol +++ b/solidity_contracts/src/PlainOpcodes/Counter.sol @@ -17,7 +17,7 @@ contract Counter { count += 1; } - function decUnchecked() public greaterThanZero { + function decUnchecked() public { unchecked { count -= 1; } @@ -34,4 +34,18 @@ contract Counter { function reset() public { count = 0; } + + function incForLoop(uint256 iterations) public { + count = 0; + for (uint256 i = 0; i < iterations; i++) { + count++; + } + } + + function incWhileLoop(uint256 iterations) public { + count = 0; + while (count < iterations) { + count++; + } + } } diff --git a/tests/end_to_end/PlainOpcodes/test_counter.py b/tests/end_to_end/PlainOpcodes/test_counter.py index b5139c8ec..656d34569 100644 --- a/tests/end_to_end/PlainOpcodes/test_counter.py +++ b/tests/end_to_end/PlainOpcodes/test_counter.py @@ -21,40 +21,45 @@ async def test_should_return_0_after_deployment( class TestInc: async def test_should_increase_count(self, counter, owner): - count_before = await counter.count() + await counter.reset(caller_eoa=owner) await counter.inc(caller_eoa=owner) - count_after = await counter.count() - assert count_after - count_before == 1 + assert await counter.count() == 1 class TestDec: @pytest.mark.xfail( os.environ.get("STARKNET_NETWORK", "katana") == "katana", reason="https://github.com/dojoengine/dojo/issues/864", ) - async def test_should_raise_when_count_is_0(self, counter, owner): + async def test_should_raise_from_modifier_when_count_is_0(self, counter, owner): + await counter.reset(caller_eoa=owner) with kakarot_error("count should be strictly greater than 0"): await counter.dec(caller_eoa=owner) + @pytest.mark.xfail( + reason="https://github.com/kkrt-labs/kakarot/issues/683", + ) + async def test_should_raise_from_vm_when_count_is_0(self, counter, owner): + await counter.reset(caller_eoa=owner) + with kakarot_error(): + await counter.decUnchecked(caller_eoa=owner) + async def test_should_decrease_count(self, counter, owner): - count_before = await counter.count() + await counter.reset(caller_eoa=owner) await counter.inc(caller_eoa=owner) await counter.dec(caller_eoa=owner) - count_after = await counter.count() - assert count_after == count_before + assert await counter.count() == 0 async def test_should_decrease_count_unchecked(self, counter, owner): - count_before = await counter.count() + await counter.reset(caller_eoa=owner) await counter.inc(caller_eoa=owner) await counter.decUnchecked(caller_eoa=owner) - count_after = await counter.count() - assert count_after == count_before + assert await counter.count() == 0 async def test_should_decrease_count_in_place(self, counter, owner): - count_before = await counter.count() + await counter.reset(caller_eoa=owner) await counter.inc(caller_eoa=owner) await counter.decInPlace(caller_eoa=owner) - count_after = await counter.count() - assert count_after == count_before + assert await counter.count() == 0 class TestReset: async def test_should_set_count_to_0(self, counter, owner): @@ -68,3 +73,18 @@ async def test_deployment_with_value_should_fail( ): with kakarot_error(): await deploy_solidity_contract("PlainOpcodes", "Counter", value=1) + + class TestLoops: + @pytest.mark.parametrize("iterations", [0, 50, 100]) + async def test_should_set_counter_to_iterations_with_for_loop( + self, counter, owner, iterations + ): + await counter.incForLoop(iterations, caller_eoa=owner) + assert await counter.count() == iterations + + @pytest.mark.parametrize("iterations", [0, 50, 200]) + async def test_should_set_counter_to_iterations_with_while_loop( + self, counter, owner, iterations + ): + await counter.incWhileLoop(iterations, caller_eoa=owner) + assert await counter.count() == iterations