4

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.

11

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.

  • 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
  • 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
12

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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