17

Non-constant functions are limited in computational complexity by the block gas limit. I assume, but am not sure, that this does not apply to view/pure functions. Are view/pure functions in any way limited in computational complexity other than the fact that I'd have to wait for quite a while until e.g. a long loop is executed?

eth
  • 85,679
  • 53
  • 285
  • 406
SCBuergel
  • 8,774
  • 7
  • 38
  • 69
  • 2
    The constant keyword isn't actually a part of the protocol specification, so its behavior in edge cases is largely up to the client. The only relevant specification is the JSON-RPC method eth_call. In practice, most clients respect the gas limit for the block that they are simulating the transaction in, but you shouldn't rely on that being the case across all clients. – Tjaden Hess Nov 05 '16 at 17:18

2 Answers2

15

view/pure functions are limited by the gas provided to it. They still "use" gas, even though the sender (from account) doesn't get "charged" for the gas.

view, pure are Solidity keywords but currently only an indicator for Javascript web3.js to use JSON-RPC eth_call instead of eth_sendTransaction.

https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-methods

// Automatically determines the use of call or sendTransaction based on the method type (constant keyword exists or not?)
myContractInstance.myMethod(param1 [, param2, ...] [, transactionObject] [, defaultBlock] [, callback]);

// Explicitly calling this method myContractInstance.myMethod.call(param1 [, param2, ...] [, transactionObject] [, defaultBlock] [, callback]);

// Explicitly sending a transaction to this method myContractInstance.myMethod.sendTransaction(param1 [, param2, ...] [, transactionObject] [, callback]);

call and sendTransaction are very similar (under the hood), with the main difference being the former is a simulation. But the simulation still "uses" gas, and understandably can cause confusion. To perform a complex computation in a view/pure function, you may need to explicitly specify plenty of gas like: myContractInstance.myMethod.call(param1, {gas:990000000})

For example, Geth "only" provides 50 million gas:

if msg.gas == nil {
    msg.gas = big.NewInt(50000000)
}

As @Tjaden commented, there are basically no specs about eth_call, so other clients and browser-solidity may behave differently from Geth.

eth
  • 85,679
  • 53
  • 285
  • 406
5

The computational work needed by a constant or non-constant function is the same when you create your contract or if you execute the function. The initial cost depends on the operations executed in the EVM, each opcode has its own price. You could check the list here (maybe outdated).

However, the "constant" keyword indicates that the function is not supposed to modify the states, and no gas will be used because it allows the function to be executed locally/off blockchain in your node.

Test:

enter image description here

enter image description here

Edit: let's try to execute a loop within a constant function to get an idea of the computational work:

pragma solidity ^0.4.0;

contract test_compexity{

function f(uint256 n) constant returns (uint256) { uint256 j=0; while(j<n){ j=j+1; }

return j; } }

If we run this constant function with the input 1001, we get the result 0X

enter image description here

If we run the same function without the constant type and with the same input, we get an error: Gas required exceeds limit

enter image description here

So even if we are using a constant function, we are still under gasLimit limitation. Think of it as if you are using the constant function you are just paying yourself within a gasLimit.

Pang
  • 299
  • 1
  • 3
  • 7
Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
  • It makes sense to me that the computational effort to me is the same, but I do not see this answering my question if there are limitations to the computational complexity of constant functions. – SCBuergel Nov 05 '16 at 13:14
  • check the edit in the answer – Badr Bellaj Nov 05 '16 at 15:37
  • Ok, so even constant functions seem to be subject to the default gas limit and probably also to the block gas limit, I just confirmed in Ethereum Studio. This does not make sense to me, see also: https://github.com/ethereum/go-ethereum/issues/3237 – SCBuergel Nov 05 '16 at 18:15
  • @Sebastian: Yes they're subject to the gas provided and since you're using Geth, I think you only get 50M gas for eth_call so try specifying more. – eth Nov 07 '16 at 12:35
  • The answer is a little messy because the loop modifies state and should not be declared constant/view. Declaring j inside the function should allow for arbitrarily large gas specifications. To clarify, Remix enforces view won't modify state, but solc compiler doesn't yet, so this would not work with testing on your station. – Garen Vartanian Nov 28 '18 at 19:23