0

What is the best way to 'harden' an infinite loop, if you are in a scenario where you would find it necessary to iterate over an array, would it be a best practice to put an assert condition in advance of the loop based on an length limit and if yes, what is the best way to calculate this limit? would that be to run the function and note the gas consumption and divide it by gas limit?

// Update rewards for all pools. 
function updatePools() public {
    uint256 length = pools.length;
    for (uint256 p = 0; p < length; ++p) {
        update(p);
    }
}

Above, is attached an example.

  • Please share the relevant code. – goodvibration Sep 1 '20 at 12:53
  • I've added an example, thanks – NowsyMe Sep 1 '20 at 13:38
  • 1
    Add two input parameters to the function - startIndex and endIndex, and let the caller determine the range, so that he/she can execute the entire procedure successfully, regardless of the current length of the array. – goodvibration Sep 1 '20 at 13:53
  • smart, thank you, bless up – NowsyMe Sep 1 '20 at 14:54
  • 1
    BTW , you could also use startIndex and numOfElements (that is probably what I would do, because it prevents doubts about endIndex being inclusive or exclusive, as well as the need to verify that it is not smaller than startIndex). – goodvibration Sep 1 '20 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.