13

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

1 Answer 1

13

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
  • 5
    You do pay gas when calling a view function from inside a transaction
    – Jesbus
    Commented Apr 29, 2018 at 10:02
  • 2
    Yes, when you call it inside another function, but not when you "normally" call it. Commented Apr 29, 2018 at 10:08
  • 2
    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
    Commented Apr 29, 2018 at 10:25

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.