Ethereum gas
An Ethereum transaction can call a smart contract, which can, in turn, call another smart contract and then another, and so on. Thus, an improperly written smart contract may lead to circular calls and result in infinite loops. Stopping a smart contract infinite loop is almost impossible, since thousands of nodes worldwide run the same looping code. To stop the infinite loop, all running nodes need to be shut down within a short time window. Even if one node fails to comply, the infinite loop is still alive. When other nodes are back to the network, the running infinite loop is brought back to these nodes as well. It is a logistical nightmare to coordinate and shut down all nodes worldwide at approximately the same time.
To resolve this issue, the concept of gas was introduced. A vehicle relies on an engine, which depends on gas for energy. If an engine runs out of gas, the vehicle stops. When a transaction is submitted, a requester is required to provide the max gas amount. Each execution step of a smart contract uses a certain amount of gas. An infinite-looping smart contract will eventually lead to the maximum gas amount being used up, and a node will no longer execute the contract. Another advantage of using gas is that it makes hacking prohibitively expensive and, therefore, deters hacking activities.
Gas is a metering unit for measuring consumption just like a kilowatt is the unit for measuring electricity usage. Suppose in a month a family uses 210 KW. Before sending a bill to the family, the utility company first converts 210 KW into US dollars based on a predefined conversion rate. Suppose a unit of KW costs $0.20 USD; the total charge for the month is 0.2 × 210 = $42 USD.
Similarly, gas usage is converted into ether for being charged to a requestor. Ethereum allows a requester to specify the conversion rate when the transaction is submitted. A validator (Ethereum's equivalent to the miner for bitcoin) has the option of giving a preference to transactions with higher rates. If a requester does not specify a rate, EVM uses a default rate, which varies. For example, in 2016, the rate for 1 gas was 0.00001 ETH. In 2018, 1 gas was 0.00000002 ETH.