Make sure you read the previous article on what is Ethereum and how transactions work on the Ethereum network before you read this one.
In the previous article, we discussed how the Ethereum world computer works, how the state is updated with every transaction, and how consensus is maintained among the nodes.
We did not cover what the Ethereum network’s cryptocurrency (Ether) is actually for, and what is gas. This piece will explain both.
The basic gist is that you use Ether to bid for gas, and you use gas to pay for executing transactions. Of course, this makes little sense if you don’t know why we need this system of gas in the first place – so let’s start there.
The Need for Gas
Turing Completeness and Loops
To understand the need for gas, we first need to get into a bit of computer science (I’ll keep it simple, I promise!).
If the next three paragraphs still feel overwhelming to you, don’t worry. There’s a noob friendly summary right below that anyone can understand.
The value of the Ethereum world computer comes from its ability to run smart contracts (as you already know from the previous article). These smart contracts can be coded to perform any theoretically possible computation.
The term “Turing complete” refers to any system that can perform any computation within the theoretical limits of computation, and since Ethereum can do this, the Ethereum computer is Turing complete.
For those wondering, “what are the limits of computation”, there are many problems that computers can’t solve (there are rigorous mathematical proofs for why it’s impossible), like the halting problem.
For non-technical people: If you found the above three paragraphs confusing (don’t worry, it means you’re normal) – they just mean that smart contracts can have loops in them.
Loops allow you to keep running a piece of code repeatedly until certain conditions are met. If you’ve never coded before, watch this children’s video on what loops are:
You can set up these loops to run indefinitely until some condition is met.
For example, you can code a program to sum up the natural numbers in serial order (i.e. compute 1+2+3+4+5+6+7…), but stop when the sum is greater than or equal to 10.
sum = 0 n = 1 Loop: sum = sum + n n = n + 1 if sum ≥ 10: stop loop
This loop will run four times. (Note: in programming, the “=” symbol represents assignment of value, not equality like you might familiar with in math class.)
sum = 0 + 1 = 1
n = 1 + 1 = 2
sum = 1 + 2 = 3
n = 2 + 1 = 3
sum = 3 + 3 = 6
n = 3 + 1 = 4
sum = 6 + 4 = 10
n = 4 + 1 = 5
Now, since the value of the variable “sum” is greater than or equal to 10, the loop will stop as its conditions for halting have been met.
Now imagine what the program above would do if the condition for stopping the loop was set to sum being equal to 13. The program would run forever because the loop would never stop (infinite loop).
The value of sum will go from 10 (as shown above in the fourth run) to 15 (as it will be incremented by 5 in the 5th run). Subsequently, it will keep increasing. It will never be exactly 13 and the loop will never stop. This is called an infinite loop.
The problem with Infinite Loops
Because Ethereum is Turing complete, you can code smart contracts with infinite loops like the one above, deploy them in the blockchain successfully, and call for their execution.
Can you guess what would happen?
Every node on the network will start running your smart contract, get stuck in the infinite loop, and never finish the execution of the contract. The system would be rendered useless.
Here’s a system design question: How do you create a distributed computer that anyone can write any kind of smart contracts on, while protecting the system from accidental or malicious deployments of infinite loops?
What is Gas and what does it do?
The way Ethereum solves the problems posed by Turing completeness (infinite loops) is by charging for every computation in the smart contract.
When you execute a transaction on the network, you have to pay some gas. Different computations cost different amounts of gas.
For example, (at the time of writing) addition costs 3 gas. Multiplication costs 5 gas. The base fee for a transaction is 21,000 gas. A full list of operations with their gas cost can be found here.
Every block is capped to a certain block gas limit (the total gas consumed by all the transactions in the block can’t exceed this limit). At the time of writing, the block gas limit is 15 million gas. You can find the latest block gas limits here.
In other words, (at the time or writing,) a block is only valid if the total gas consumed by all the transactions in it is less than 15 million.
Note: Unlike Bitcoin, where the maximum block size is 1 MB, there is no maximum block size in Ethereum. Ethereum’s blocks can be as big as the miner wants, but all the transactions combined must consume less gas than the block gas limit.
This means that not all transactions can end up in a block, and the miner of the block has to decide which transactions he wants to mine in the next block.
Now let’s look into how a miner decides which blocks he should put into the block he is mining.
Gas Price and Gas Limit
Every time you send a transaction, you have to set two fields in the transaction structure – gas price and gas limit.
Gas price is the price (in ether) per unit of gas you’re willing to pay.
You have to pay ether to get gas. This is the use of the cryptocurrency ether – it is used to buy gas to run transactions on the Ethereum network.
Note: The actual bidding values are represented in Gwei (giga-wei), which is another unit of ether (like wei). 1 Gwei = 1,000,000,000 wei or 0.000000001 ether.
Gas limit is the maximum amount of gas you’re willing to purchase.
Since every transaction specifies a gas price, the miner simply includes the transactions that pay the highest price for the gas (the miner gets the ether you bid for the gas as a part of his mining incentive).
In other words, there is a first price auction for gas every time a new block is to be mined. The highest bidders win. The bidding takes place in terms of ether, and this is where ether derives its value from.
The price of gas is determined by the forces of demand and supply. There is only a fixed amount of gas available per block, and when the network is busy and lots of people are trying to get their transactions in a block, they bid the price of gas up. When the network is not so busy, the bids aren’t as high.
I hope the sentence in the introduction, “use Ether to bid for gas, and you use gas to pay for executing transactions” makes some sense now.
Some people like to equate Ethereum’s gas to Bitcoin’s transaction fee, and as you can see, it’s not strictly true.
Gas is just a unit measuring “cost of computation”. The transaction fee is the price you pay for gas multiplied by the amount of gas your transaction consumes.
Also, do you see why it’s called gas? It’s fuel for running transactions.
Gas doesn’t exist outside the EVM
Gas doesn’t actually exist outside of the Ethereum Virtual Machine (the technical term for the portion of the computer that actually executes the logic of the transactions). You cannot buy and sell gas.
You just bid for gas whenever you want to do a transaction. As explained above, the bidding is done in terms of Gwei per unit of gas, and if your bid is high enough, your transaction is included in the next block.
The miner takes all the transactions that pay the highest amounts of ether for gas that will fit in a block, mines the block, and gets the amount of ether bid (gas price per unit * gas units consumed) in all those transactions included in his block as a part of his mining reward.
After a block is mined, this auction process begins again for transactions to be included in the next block.
How does gas solve the problem of infinite loops?
Each computation costs a little gas, and if the gas limit included in the transaction runs out before the smart contract terminates, the execution is halted and all the changes made are reverted (the gas is not refunded).
For example, let’s say I make and deploy a smart contract that will get stuck in an infinite loop and indefinitely add numbers (like the one above).
I then send a transaction calling that contract and I bid a high price for gas (let’s say 1000 Gwei) and set the gas limit for my transaction equal to the block gas limit (the maximum gas consumption a block can have), which is 15 million at the time of writing (again, as mentioned above).
A miner will then take my transaction and include it in a block (in this case, it will be a block with only one transaction as there is no more gas available in the block because I set the gas limit for my transaction to be the maximum gas possible).
Since addition costs 3 gas every time it’s done by a contract, even though the smart contract wants to run indefinitely, when the gas in the transaction runs out (which is bound to happen because your smart contract never terminates on its own), the transaction will be marked as failed and all changes made by it to the Ethereum state will be reverted.
The only change that will be made to the state will be to reduce the transaction sender’s account by the transaction fee, and increasing the miner’s balance by the same amount.
(Do you see why gas can’t be bought and sold? Because everything is settled in ether, gas is just an abstract token.)
The “transaction fee” for this transaction would be gas price * gas consumed = 1000 Gwei * 15 million gas = 15 billion gwei = 15 Ether
Once again, this transaction fee will not be refunded even though the transaction has failed. This is a good thing because of two reasons:
- The miner gets paid for the work he has done
- The transaction cost for failed transaction deters people from attacking the network by sending large numbers of transactions that are doomed to fail. If the fee for gas consumed in failed transactions was refunded, malicious people would use it to run denial of service attacks on the network.
You only pay for Gas consumed
To highlight something important, the transaction fee only considers the gas actually consumed by your transaction, and not the maximum gas limit you bid for.
For example, if you set the max gas limit to be 50,000 gas, but your transaction only used 20,000 gas, then you will only pay for the 20,000 gas (whatever you paid for the remaining 30,000 gas will be refunded back to you).
This applies to both failed and successful transactions. (There are many reasons why transactions can fail, running out of gas is only one of them. Of course, if a transaction failed because it ran out of gas, as would happen in case of an infinite loop, there is nothing to refund.)
What you learnt
In this article, we covered:
- What is Turing completeness
- What are infinite loops and what problems they pose for the network
- What is gas and why we need gas
- Gas Price and Gas Limit
- How the transaction fee is calculated
- How gas solves the problem of infinite loops
- How gas protects the network from denial of service attacks
- That you only pay for the gas consumed by your transactions, and excess gas is refunded
Hope this helps. Let me know what you think in the comments below and feel free to ask questions.
– Harsh Strongman
P.S. This article is a part of the free crypto course. If you found this article useful, go check the course out.