6

So if I understand this correctly, view function only read data from ethereum and doesn't mutate any state and pure function doesn't even read data and they are intended for cases like return 1+1;

If this is the case, would they be abused since they don't cost any gas? Like could I just use them for free computation? What happen if I have a infinite loop in a view or pure function?

TLDR: They won't.

noooooooob
  • 246
  • 2
  • 10

2 Answers2

13

pure and view functions only are "free" when you call them externally, as in you call that function by itself and run the calculation on your node. So if you had a function that returned "1+1", yes you can call that individual view function as many times as you want for free, even without creating a transaction.

However, if you use a view or pure function within an internal function which DOES cost gas, you will be charged for those calculations like normal.

Shawn Tabrizi
  • 8,008
  • 4
  • 19
  • 38
  • Thanks for the insight. Is it possible to invoke the bad pure function not using my own node ? Would that still be consider abuse or free ride ? Does the network penalize such behavior in anyway? – noooooooob Sep 19 '18 at 10:12
12

Just to add to @Shawn's good answer.

Yes, you can abuse them. But as the pure and view computations are performed only on your own node you would only hurt your own node. You would not hurt the Ethereum network in any way. If you call the function(s) enough, you might even crash your node but it still wouldn't hurt the network in any way.

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57
  • Say if I call my contract via web 3 without my own node, would that affect the network in anyway? – noooooooob Sep 19 '18 at 10:09
  • No, you always have to call some node, so only the called node is affected. You can't access the blockchain without an intermediary node. – Lauri Peltonen Sep 19 '18 at 10:10
  • Hmm .. Bear with my ignorance. My understanding is that when you use dapp, you access the free node hosted by infura via web3 ? – noooooooob Sep 19 '18 at 10:14
  • Infura is one way, yes. They provide you with a node you can use. But there are many other alternatives. – Lauri Peltonen Sep 19 '18 at 10:15
  • So is it fair to say that we could free ride the computation of pure/view function when we are not running our own node? Eg. Could I run expensive number crunch task on one of the network node for free ? – noooooooob Sep 19 '18 at 10:18
  • That's between you and Infura - I have no idea what kind of restrictions / rate limits they have. – Lauri Peltonen Sep 19 '18 at 10:20
  • Thanks for the clarification. So as far as ethereum network is concerned, i can not really free ride computation, as it's only on my "own" node and won't be broadcasted ? – noooooooob Sep 19 '18 at 10:52
  • 2
    Please post a new question if you have further questions - discussions in comments is discouraged. But yes, you're correct. – Lauri Peltonen Sep 19 '18 at 10:54