1

When you call pure and view functions, they only run on the node you use to connect to the network. This makes sense, because no state needs to be changed.

But for transactions that end up failing or reverting - shouldn't your node be able to tell and not propagate it to the entire network and waste gas? What makes it more confusing is that sometimes transactions do fail and don't cost you any gas, whereas other times you lose a hefty gas fee.

I know that assert uses up all the gas, and require/revert only use up some gas, but why do any of them use up any gas at all? And when you send ETH to a contract that doesn't have receive/fallback, that transaction actually goes onchain but fails, and you lose the transaction fee. I feel like something this simple shouldn't be propagated to the network and waste gas.

Thank you.

2 Answers 2

1

Even when the transaction "fail" it still consumes network resources, like bandwidth, miners processing time, etc. Some contracts fail early so the cost is lower, but others have quite complex interactions and they fail after using a lot of gas.

Some wallets simulate transactions before sending, and warn users if the transaction will fail, but it is up to the user to send it or not.

As said by pbsh the purpose of the transaction fees is to deter bad actors from abusing the network.

2
  • Ohh some wallets simulate transactions? Do you know if Metamask does this? Because I've tried adding a require in my contracts before and the transactions just didn't go onchain, but other times, contract failures do go onchain and waste gas.
    – 930
    Commented Mar 7, 2022 at 23:44
  • 1
    @930 Metamask does simulate the transaction, but a successful simulation doesn't mean that the transaction will succeed. The transaction might depend on the contract state and it might change between the simulation and mining.
    – Ismael
    Commented Mar 8, 2022 at 1:07
1

This is done so that the network won't be spammed with transactions that will eventually revert. Even if the transaction reverts in the end, the nodes will have to execute it to find out. If the gas is not used up, it would enable spammers to flood the network with reverting calls, without any cost.

2
  • 1
    But we can spam view and pure functions without using any gas - we can even make those functions very complicated and still spam them.
    – 930
    Commented Mar 5, 2022 at 9:51
  • 3
    View functions are not executed by all nodes but only by your rpc provider
    – pbsh
    Commented Mar 5, 2022 at 12:00

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.