1

I understand that read-only calls don't cost any gas fees. However, are there any limitations to these read-only calls and static calls that don't cost gas?

For example:

struct Person {
   string name,
   uint256 age
}

Person[] private persons;

function getAllNames() view external returns(memory string[]) {
  String[] memory names = [];
  for (uint256 i=0; i<persons.length; i++) {
    names.push(persons[i].name);
  }
  return names;
}

function getAllPersons() view external returns(Person[]) {
  return persons;
}

Let's say the array persons is huge with tens of thousands of records or even hundreds of thousands of records. Both the functions getAllNames() which loops through the huge array and getAllPersons() which returns the entire persons array are view functions. They don't cost gas fees when called as read-only functions.

However, do read-only calls or static calls to such functions have limitations too? Does the EVM still calculate the gas cost when running these "free" read-only functions and error out when its internal gas calculation limit is reached? Or am I safe to call such functions that loops through giant arrays without worries?

Particularly, is there some sort of gas limit or other kinds of limitations to expensive read-only functions that could take a long time to run on the nodes?

2 Answers 2

2

In short yes there is a limit but it depends on the way you call view function .

There are three ways you can call view/pure function .

1 - Directly on chain i mean you can just send tx to contract with your view function signature and if it's necessary with arguments. This will cost same as other on chain messages (Intrinsic Gas + Each opcode price). In this case limit is gas you had provided ;

2 - Through staticcall or call. Each opcode call/delegatecall/staticcall/callcode takes gas avaliable in evm execution ;

3 - Via Go Etherum Call method. This method is very similar to ordinary on chain call. Web3 js description - "Executes a message call transaction, which is directly executed in the VM of the node, but never mined into the blockchain." Every "free call" occur in this way. Here limit is set to uint64 max value ;

gp := new(core.GasPool).AddGas(math.MaxUint64)
result, err := core.ApplyMessage(evm, msg, gp)

Link to go eth github : https://github.com/ethereum/go-ethereum/blob/master/internal/ethapi/api.go#L997

2
  • Just to be clear, so if I were to make a call to a view read-only function off-chain through a client like web3 JS, I won’t be charged gas fees but the call itself is still limited to the max amount of gas a transaction can take because each of the opcode being run in the view function still counts to the gas limit. For eg, a view function that loops a massive array could still fail with out of gas when called from an off-chain client, right? Did I understand correctly?
    – xenon
    Mar 4 at 17:27
  • 1
    Yes you are right , but web3 js isn't a CLIENT it's a library that make a boring json-rpc reqests to client that you provided
    – Franiu
    Mar 6 at 11:04
1

Theoretically it should work. If the array is too large, https connections may break becuase of delay in reading all the data.

I tried something similar but array length was ~100. Try to avoid arrays, solidity in not good with handling arrays.

3
  • Did you mean that even though it doesn't charge gas fees for read-only calls, the EVM doesn't internally calculate gas limits in these read-only calls too and I can practically call any expensive read-only functions that could take a long time to run on a node?
    – xenon
    Mar 2 at 14:52
  • Depends from where are you calling this function. This should be helpful: stackoverflow.com/questions/48755578/… Mar 3 at 15:13
  • Just to be clear, so if I were to make a call to a view read-only function off-chain through a client like web3 JS, I won’t be charged gas fees but the call itself is still limited to the max amount of gas a transaction can take because each of the opcode being run in the view function still counts to the gas limit. For eg, a view function that loops a massive array could still fail with out of gas when called from an off-chain client, right? Did I understand correctly?
    – xenon
    Mar 4 at 17:27

Your Answer

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

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