2

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;
    }
}
  • 1
    Please include you code – Lauri Peltonen Apr 29 '18 at 9:38
  • Lauri Peltonen i Re-edit the comment and I included the code – kathi Apr 29 '18 at 10:31
2

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!

  • 3
    You do pay gas when calling a view function from inside a transaction – Jesse Busman Apr 29 '18 at 10:02
  • Yes, when you call it inside another function, but not when you "normally" call it. – Eli Drion Apr 29 '18 at 10:08
  • Eli Drion thank you for the article it was very helpful. I have a question it states"For view functions being called from outside the EVM (e.g. from JavaScript in a web app), gas is not a limiting factor because there is no transaction being executed. The node processing the call does the computation locally and returns the result. Each node gets to set its own processing limits—typically limiting execution time. If the call takes too long, it will fail." Si if i use truffle can i set this limits very high so i won't run into any problems? – kathi Apr 29 '18 at 10:25

Your Answer

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

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