1

If I as a user of a smart contract ,call a function which does a heavy computation but doesn't change anything except the function variables which live only during function execution and then thrown away, does that make a transaction on ethereum blockchain? If it doesn't then how the fee for the miner is paid? I know for a code to be executed certain gas is used but if there is no transaction who gives the fee for the execution?

3

When calling a view or pure function (those that don't change any balance or state variables), there is no on-chain transaction and no fee is paid. All the computation is done client-side. The miner is not involved. No fee needs to be paid because the miner didn't do anything at all.

| improve this answer | |
3

Any function can be run in a read-only mode that does not (cannot) change the state and does return return values without waiting for confirmation.

There are two ways to do that.

  1. In a contract, a function can strongly suggest this mode so it is the default process from a client perspective, by marking the function view or pure. view reads the state, such as reading stored variables and returning something about them. pure is purely computational, such as math.
  2. A client can explicitly request a dry-run, read-only mode regardless of how the function was written. Such a run can even include proposed state changes - they just won't stick.

In both cases, 100% of the gas returned but gas accounting takes place and it is still possible to run out of gas, which is useful for testing functions that rely on accurate accounting, e.g. (pseudo) if(gasRemaining > min) { doAgain(); }

It's a bit of a confusing topic, in practice. Have a look over here for an attempt to explain what's going on: https://blog.b9lab.com/calls-vs-transactions-in-ethereum-smart-contracts-62d6b17d0bc2

Hope it helps.

| improve this answer | |
  • Can any function i.e not hard coded as pure or view called by any other than web3 .call() . If so then , how things proceed? And for check, if i call a function that change state using .call(), the state is locally changes on my pc , but it doesn't go into blockchain.right? Sorry if i am reiterating your statement. – Abhishek Mishra Dec 17 '19 at 5:28
  • Any function can be run that way. Web3 is a javaScript wrapper. There are others. Deep down, it's JsonRPC to the node, so you could curl or similar. Expect it to get a little more raw, a little less intuitive at that level. – Rob Hitchens Dec 17 '19 at 7:06
  • If the function changes the state, the change will only last for the duration of the call. So, if x is in storage and x==1 and the function sets x==2 then the function can inspect x and get 2. If you look again with another call, when the first call is over, x==1, because no changes. – Rob Hitchens Dec 17 '19 at 7:07
  • No information is transmitted to the chain but input and response is possibly observable by Infura or another service if a light client is outsourcing "the node." This is important to understand if privacy is a concern. The fact that nothing goes to the chain is an assurance that such a call cannot possibly have a lasting effect or cost gas in the long run. – Rob Hitchens Dec 17 '19 at 7:08
  • Gas accounting is in play. That simplifies the internals and allows for testing of logic that relies on values like gas remaining. Also, it is common for a "read" to happen in the middle of a write. For example, increment a value is get, add, write. the "get" might be callable directly, which is free but when called in the context of a chain of a function that changes the state, every node will have to run the "read" part which is just as expensive. The gas consumed by the read will be accounted for in the overall transaction cost and it will not be returned. – Rob Hitchens Dec 17 '19 at 7:12
3

There is no mining fee paid because no transaction is needed if no changes to the blockchain state trie are made.

| improve this answer | |

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.