4

How is ethereum network protected against infinite loops in views? In a normal transaction, you have to pay gas fee, so it would be too expensive for you, but calls to views are for free.

For example this:

contract Tester {
    
    function loo() public pure returns(string memory){
        uint i = 1;
        uint c = 0;
        while(i == 1){
            c = c + 1;
        }
        return "good";
    }
}

I tried to call this in Remix but it crashed.

1 Answer 1

8

When view function is executed on-chain, i.e. within mined transaction, it consumes gas, and this protects node from infinite loop. When executed off-chain via Web3 API, caller is still able to specify gas limit for the execution, see documentation.

If gas limit is not specified, it is up to particular implementation how to protect from such things, because they affect only the process that executes transaction off-chain, and do not affect other nodes. Some implementations may be unprotected, and it seems that REMIX is one of such unprotected implementations.

1
  • 2
    For example if none is specified geth will use the current block gas limit.
    – Ismael
    Mar 27, 2019 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.