Solidity – Gas Consumption of a Public View Function

gas-limitremixsolidity

In my contract I have a view public function that returns an array of bytes32 elements. In remix it produces a warning saying it might run out of block gas limit if I use this function.

As I had read when a function doesn't make state changes you define it as view public. So when you call it you don't have to pay ether because your local node calls the variables from the contract and runs it localy. Is this right? Then why I have to worry about gas limit?

Re-edit after Lauri's Peltonen comment. The code I used is:

pragma solidity ^0.4.18;
contract Project{
    bytes32[] array;

    function addValue(bytes32 element)public{
        //only the administrator can add new values
        array.push(element);
    }

    function getAll()view public returns(bytes32[]){
        return array;
    }
}

Best Answer

You don't pay gas when calling a view function. This doesn't change the fact it still has operations to do, which have costs, and thus are subject to the upper-bound gas limit on the block, as well as a time limit.

It's a bad idea to try to return an entire unbounded array, because the call will time out and you will not get anything.

What you could do is use a cursor like here, article which tackles the problem you are facing.

DISCLAIMER : NOT MY BLOG OR ARTICLE!

Related Topic