5

This may be a very silly question but it's something I haven't found an answer to. Let's say I have a view function in Solidity which does some operations and returns a result. Now I connect to a node and invoke this function on my contract. Where is this function actually executed? I'm assuming it's executed in the node I'm against. But let's say that this function is behaving very badly somehow in usual programming sense. For example it does some heavy computations like calculates prime numbers or something. Or let's say it's just an infinite recursion like this:

function my_func() public view returns(uint) {
    if (1 == 0) {
       return 0;
    }
    return this.my_func()
}

If something like this would be run on a node and since there is no gas involved as no transaction was created then I could potentially cause a lot of harm to the node wouldn't I? How does node protect itself from that?

2 Answers 2

1

It is a nice question actually.

Let us recall the definition of pure and view function modifier.

Pure and View functions don't cost any gas to call if they're called externally from outside the contract. But they do cost gas if called internally by another function.

In the above code you have shared, first time when you call the function, it doesn't cost anything. However when the function recursively calls itself, it does cost gas and since you aren't signing and it is a view function, EVM reverts the function to the initial state.

The same applies to everything. And as for calculating the prime numbers or any big numbers, EVM stack is limited to 1024.

You can read more about the memory in this answer.

On top of all that, excluding the gas fees, there is one more thing you need to remember when signing a transaction or calling view functions. That is called execution time. When a function's execution time exceeds certain limit, the function will exit.

Tell me if it helps!

3
  • 1
    Many good insights. Thank you. Is it really true that you can't call one public view function from another? Because this part of your answer implies it: "But they do cost gas if called internally by another function."
    – Tarmo
    Commented Sep 15, 2022 at 18:41
  • You can call view functions from another "write" function. If you call a view function from another view function it won't cost gas. However if you call a view function internally from another function that signs a transaction, it will cost you extra gas. I wrote a simple program in remix for better understanding. Take a look at it!
    – Ad-h0c
    Commented Sep 16, 2022 at 3:46
  • Example It will take time to load since it has retrieve the code from the blockchain.
    – Ad-h0c
    Commented Sep 16, 2022 at 3:55
2

Even though view functions do not cost gas, they still have operations to perform and have upper gas and time limits because malicious users can abuse full mode resources as in your example. They do run on a single full node you connect to as you mentioned, but they don’t get propagated to the blockchain so other users can abuse the resources. Instead, what you can do is use a cursor to tackle the potential problem you are facing.

See: https://programtheblockchain.com/posts/2018/04/20/storage-patterns-pagination/

1
  • 1
    I understand that they are executed only on the node I'm against, not in the whole blockchain. I also understand there are means to go around expensive operations (among them pagination). My question was more if someone either accidentally or purpousfully can create a contract with view function which could potentially cause harm to the nodes where they are executed and even if they don't do harm, can they be computationally so heavy that it becomes a burden to the node. I think @Ad-h0c answered most of those things though.
    – Tarmo
    Commented Sep 15, 2022 at 18:45

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.