We’ve done quite a few videos on gas, but the one thing that we didn’t cover is why does the Ethereum Virtual Machine needs gas in the first place.
One characteristic about Ethereum and its programming language is that it is Turing complete. Well according to Wikipedia “A computer is Turing complete if it can solve any problem that a Turing machine can, given an appropriate algorithm and the necessary time and memory.”
The Ethereum ecosystem is run by the Ethereum Virtual Machine
So what does this mean for Ethereum, or how does Turing completeness show up in the Ethereum ecosystem. The Ethereum ecosystem is run by the Ethereum Virtual Machine. Kind of like how an engine runs a car. No engine, the car doesn’t move. No EVM, Ethereum cannot compute. Smart Contracts are autonomous programs which direct which computations the EVM will execute.
But there are some inherent problems with this model. The main one is the halting problem, which states that it is impossible to predict the path a program will take without running that program. It also indicates that it is impossible to predict if the code will terminate or result in “an infinite loop.”
If someone uses bad code, whether by accident or maliciously, a smart contract will try to keep executing its list of instructions. So theoretically, you could have a bunch of contracts just running code with no end, going forever.
Gas is about conserving the limited resources of the EVM
This is a problem because this could clog the network, bringing it to a crawl. The Ethereum Virtual Machine only has so much processing power, so a mechanism known as gas was added to the ecosystem. I don’t know if the name Gas was picked to be tongue in cheek, since real life gas depletes resources, Ethereum gas is about conserving the limited resources of the Ethereum Virtual Machine.
Here’s how it works. A gas value is attached to each computation requested of the EVM. As computations and compiled into a smart contract, you can estimate the maximum amount of gas that will be used when executing that transaction.
So if a smart contract results in an infinite loop, the gas limit will be surpassed and there will be nothing to fuel the smart contract, effectively mitigating the halting problem. Assures the network is free from smart contracts with no end, they’ll simply run out of gas.
Gas price is adjustable and what that does is assures that it is expensive to abuse the network. It acts as an incentive not to run pointless transactions. Because that’s what a testnet is for.
For more about Gas and Ethereum check out these other great articles and videos!