Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

bug: for and while loops fail to increment counter when iterations are set to higher values #684

Closed
Tracked by #324
richwarner opened this issue Aug 16, 2023 · 2 comments

Comments

@richwarner
Copy link
Contributor

richwarner commented Aug 16, 2023

Bug Report

Kakarot version:

Current behavior:

Despite the transactions showing successful execution, for and while loop tests failed to increment a counter at all when the number of iterations was set greater than a particular value. The number of iterations at which the behavior changed varied when tested at different times. For example, using 500 iterations produced both expected and unexpected behaviors at different times.

Steps to reproduce:

Setup:

  1. Run a local Katana RPC using Docker.
  2. Deploy the contract below using Remix with environment set to "Injected Provider - Metamask".

Test a for loop:

  1. Run the testForLoop function passing 50 as input
  2. Calling count returns 50
  3. Now run the testForLoop function passing 5000 as input
  4. Calling count returns 50

Test a while loop:

  1. Run the testWhileLoop function passing 50 as input
  2. Calling count returns 50
  3. Now run the testForLoop function passing 5000 as input
  4. Calling count returns 50
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract ControlStructureTest {

    uint256 public count = 0;

    function testForLoop(uint256 iterations) public {
        count = 0;
        for (uint256 i = 0; i < iterations; i++) {
            count++;
        }
    }

    function testWhileLoop(uint256 iterations) public  {
        count = 0;
        while(count < iterations) {
            count++;
        }
    }

}

Expected behavior:

Count should be equal to iterations after the transaction completes successfully. The expected behavior is observed when testing the same code in Remix's Shanghai VM.

Other information:

Increasing the gas limit didn't appear to change the behavior.

@ClementWalter
Copy link
Member

ClementWalter commented Oct 10, 2023

Did some testing on Katana, when there are too many iterations the tx reverts with

Could not reach the end of the program. RunResources has no remaining steps.

The issue is more RPC related (which should propagate the error) than from Kakarot actually.
As a matter of fact, with few benchmark I've done:

  • while loop

    • 100 iterations => 2448973 steps
    • 200 iterations => 4506041 steps
    • 300 iterations => failing with 6124644 Could not reach the end of the program. RunResources has no remaining steps.
  • for loop

    • 100 => 3042592
    • 200 => 5096151 & failing

I'm creating a PR to add the test to track perf, but this is not a kakarot issue per se, so closing this issue.
In any case, quite surprising as well to have katana failing with 6124644 while it's supposed to be started with --invoke-max-steps 16777216

@ClementWalter
Copy link
Member

See #767

@github-project-automation github-project-automation bot moved this from 🆕 Backlog to ✅ Done in Kakarot on Starknet Oct 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
No open projects
Archived in project
Development

No branches or pull requests

3 participants