Solidity Transactions – Shouldn’t Failed Transactions Never Use Up Gas?

evmgasminingsoliditytransactions

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.

Best Answer

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.

Related Topic