Gas, Gas Price and Gas Limit

What is Gas?

By definition, gas is a unit that measures the amount of computational effort that it will take to execute certain operations on the Ethereum blockchain. The operations include sending tokens, deploying a smart contract, interacting with a contract, sending some ETH, launching an ICO, or anything else on the blockchain. Gas is needed to power the Ethereum Ecosystem, just like fuel is needed to power a car.

What is Gas Price?

As I have mentioned earlier, we need to use gas for every operation made on ethereum, regardless of whether your transaction succeeds or fails. Gas does not come free, we need to pay for it, just like paying for the gasoline in order to drive our cars. How much transaction fee we need to pay depends on the gas price and the gas limit.

Gas price is the amount of Ether you need to pay per unit of gas. It is measured in Gwei (1Gwei=0.0000000001 ETH).  Its value is determined by the miners, who can refuse to process the transaction with less than a certain gas price. The transaction fee is paid to the successful miner as a form of incentive that motivates the miners to maintain the nodes. Therefore, they have the controlling power over the gas price. We must have enough ether in our wallet to pay for the gas fees.

The transaction fee is calculated using the following formula

Transaction fee= Gas unit used x Gas price

For example, for a certain transaction,

gas unit used=103631

Gas price= 1Gwei or 0.000000001 Eth

So transaction fee= 103631×0.000000001 Eth=0.000103631 Eth  

You can see the actual output in a smart contract deployment on Etherescan, as shown in the figure below:

In addition, you can “bribe” the miners to do your work first by paying more gas fees. In this way, you can jump to the front of the queue so that your transaction can be processed first.  Even if the transaction fails, you still need to pay for the transaction fee because the miners must validate and execute your transaction.

Gas Limit

The Gas Limit is an estimation of the total amount of work to perform a transaction. It is not easy to compute the gas limit. Fortunately, there are many apps that set the limit for us. Typically, 21,000 Gas will satisfy most transactions. However, for more complex transactions such as sending ETH to an ICO smart contract, the gas limit will be much higher. The reason is such a transaction requires much more computational power.

If you set the limit is too low, your transaction may take too long to process and even fail. As a result, you will lose ETH for nothing. On the other hand, if your transaction was completed before reaching the gas limit, you get back the balance ETH. The Gas Limit protects you from spending unlimited ETH, just like what banks set your credit card limit so that you will not overspend.

How to achieve Proof of Work?

Mining involves the process of producing a hash whose value is less than the target value. When this hash has been found, it is called a valid hash and hence proof of work is achieved.

The mining algorithm uses a counter known as the nonce to generate the hash using the SHA256 cryptographic function. A hash algorithm always produces the same arbitrary length data given the same inputs. It is impossible to compute the same hash with two different inputs. It is also impossible to predict the output of any given data in advance.

The value of nonce is initialised to 0. Mining is finding the nonce, the only input that changes every time we run the hash function. The goal is to find a value for the nonce that will result in hash lower than the target. So, the mining node might need to try billions or trillions of nonce values before it gets a valid hash. As you can see, mining is like playing the slot machine, there is no way to predict when can you strike a jackpot.

It is very easy to prove that the nonce found indeed produces a valid hash. All the information are available, everyone in the network can run the hash function and confirm if the hash is valid or not. Because it is also impossible to predict what the nonce will be, it also acts as a proof that the miner has indeed achieved Proof-of-Work.

Calculation of the Valid Hash

(The numbers are based on block #540909)

The formula to calculate the current target of the block is

Current target= maximum target ÷ difficulty

Maximum target is set to 

0x00000000FFFF0000000000000000000000000000000000000000000000000000

This is a hexadecimal number. After conversion to a decimal number 

Maximum target= 

26959946667150639794667015087019630673637144422540572481103610249215

Difficulty is (as given in the block)

7019199231177.17

Therefore,
Current target=

(26959946667150639794667015087019630673637144422540572481103610249215)÷7019199231177.17

3.84089×1054

The hash of the block is

00000000000000000000ef17668e407e78c5a247f731b1138ad16f5bf79f1c0d

After converted to a decimal number, the value is as follows:

89454716205495239548871016846060264708718561584946189

The hash value is approximately   8.95×1051

Clearly, the hash value is less than the current target, therefore it is a valid hash.

Technical Explanation of Mining

In this article, let’s examine the technical aspects of crypto mining. In the blockchain, every block has a previous block except the very first block or the genesis block. Miners are competing to validate a new block by solving a complex mathematical puzzle. To explain in details, let’s take a look at the latest bitcoin mined block, block #540909 at the time of writing this article.

Notice that the block height is 540909, which means there are 540909 blocks in the bitcoin blockchain.

Let’s call the successful miner for this block Mr.John. Before John successfully mines block #540909, he was actually competing with other miners in mining the previous block #540908. However, he lost in the contest and block #540908 was mined by a fellow miner. As soon as block #540908 was mined, he needs to quickly update his blockchain and starts mining for a new unvalidated block, known as the candidate block.

In actual fact, while John’s computer(also known as a node)  was searching for the Proof of Work for the previous block, it was also searching for new transactions. Those new transactions are added to the memory pool or transaction pool.  The memory pool is a node’s temporary storage area for transaction data. This is where transactions wait until they can be included in a new block and validated.

In constructing the candidate block, John’s node starts gathering the transactions in the transaction pool. It removes the transactions already present in the previous block if there are any. The block is called a candidate block because it doesn’t have a valid Proof of Work yet.

As you can see that block #540909 has 1696  transactions inside it. This was the number of transactions present in John’s transaction pool when he created his candidate block. The mining process can be illustrated in the following figure.

In the mining process, John’s node is creating a coinbase transaction. This transaction is to create some bitcoins and deposit them into John’s wallet as a reward for finding a valid Proof of Work. This transaction is different from the other ones because the bitcoins in the reward are created out of nothing. They do not come from someone’s wallet. Besides that, John’s node also calculates the transaction fees in the block.

John reward by mining this block is as follows;

Total Reward =  Reward for mining block + transactions fee

                    = 12.5 BTC+0.15289664 BTC

                   = 12.65289664 BTC

The details of the transaction is as follows:


You can see the No Inputs (Newly Generated Coins) statement. It is because coinbase transactions do not come from anyone’s wallet, so they cannot have any inputs. You only have the winning miner’s wallet address here

What is Mining in Blockchain?

The Purpose of Mining

Generally, people conceive mining in blockchain as a way to obtain bitcoins or other cryptocurrencies. Well, it is partially true but this is not the main purpose of mining. In actual fact, the main objective of mining is to ensure the perpetuity and security of the decentralized network. The network comprises nodes that store the distributed ledgers in the form of the blockchain.

Bitcoins or other cryptocurrencies are awarded to the miners for their effort in maintaining the integrity of the blockchain by validating the transactions in the blockchain. Because of the reward system, miners (nodes) will stay on in the network and help to prevent network downtime. Just imagine if there is no reward, nobody will want to connect to the network and it will just cease to exist.

How does Mining work?

The mining process starts when miners are trying to validate new transactions and record them on the blockchain. The miners are competing to solve a difficult mathematical puzzle based on a cryptographic hash algorithm. The solution found is called the Proof-Of-Work. When a block is ‘solved’, all the transactions contained in the candidate block are considered validated, and the new block is confirmed. This new block will be appended to the blockchain. The time taken to confirm a new block is approximately 10 minutes for bitcoin, for other coins it is must faster. So, if you send or receive some bitcoin, it will take approximately 10 minutes for the transaction to be confirmed.

Miners receive a reward when they solve the complex mathematical problem. There are two types of rewards: new bitcoins and transaction fees. The amount of bitcoins created decreases every 4 years or  every 210,000 blocks to be precise. Today, a newly created block creates 12.5 bitcoins. This number will keep going down until no more bitcoin will be issued. This will happen around 2140, when 1 millions bitcoins will have been created. After this date, no more bitcoin will be issued.

Miners can also receive rewards in the form of transaction fees. The winning miner can collect all the transaction fees in the block. As the amount of bitcoin created with each block diminishes, the transactions fees received by the miner will increase. After 2140, the winning miner will only receive transaction fees as his reward.