How The Gas Fuels Ethereum Blockchain?

Kerala Blockchain Academy
7 min readSep 29, 2023

By Sumi Maria Abraham, Research and Development Engineer Kerala Blockchain Academy.

Ethereum — The World’s first programmable blockchain was launched to erase the notion of bitcoin as a synonym for blockchain. The inclusion of programmability by Ethereum imparted wings to blockchain- a technology once trapped in a single-use case of cryptocurrency.

Back in 2014, Vitalik Buterin proposed programmable blockchains. Instead of storing code in a central server, allowing some random person who has access to it to modify it, the proposal was, why not go for a distributed ledger like Bitcoin to keep computer programs? The program will be stored at every node in the blockchain network. Okay, that sounds great, but the next question was how the nodes will interpret the program. For that, there came the Ethereum Virtual Machine (EVM). The EVM will read the program code, understand it and execute the instructions.

The programs which are installed on the blockchain are called smart contracts. The smart contract written in a high-level language like Solidity or Vyper will be converted to a low-level bytecode representation by the compiler. The contract deployment transaction will broadcast the bytecode of the smart contract to all the nodes. Finally, once the transaction gets approved and included in a block, the smart contract copy will be available to all the blockchain network nodes. Whenever an operation in the program is invoked, it will be broadcasted to the network and all the nodes in the network can execute the program and check the function execution.

Here comes a problem.

Imagine a network with 100 nodes. Diya has deployed a smart contract. Unfortunately, there was an infinite loop in the code.

An infinite loop is a situation in which a piece of code will be executed for an indefinite period of time. This usually happens in conditional statements where a statement may execute upon a condition, and the condition always remains true. The execution may halt the parent machine or stop once the resources are run out.

When the nodes execute that code, they may get stuck, causing the entire network to halt !!

Remember, no one in the network audits smart contracts. So, there is no way to prevent bugs and other program vulnerabilities. Whether by accident or on purpose, a smart contract can be created to run forever when a node attempts to validate it. This creates a way for a Denial of Service attack.

We cannot allow the execution of Diya’s faulty program to affect the network of nodes.

Thus, in a programmable blockchain, we need to address two concerns.

  • How can you prevent a faulty program from affecting the network?
  • Some operations can be simple, and some can be complicated. How can you calculate the rewards for the entities that execute (validate) smart contract operations?

For this, we have the concept of Gas.

Before heading to Gas, let me tell you about a travel plan for Diya. Diya and her friend Dev are going on a trip and calculating the travel cost. They do not know the exact distance to the destination but just an estimation, based on which they assume they need to fill five units (litres) of Gas as fuel (gas limit). Each unit of Gas costs 100 INR (gas price). Based on the Gas and price per unit estimation, their travel cost is 500 INR.

They fill five units of Gas in their car and start the journey. Their journey progress is decided by fuel.

  • If the destination is nearer than expected, they may save some gas, which can be used again for their journey.
  • If the destination is farther than expected, they may run out of Gas, and eventually, their trip may end.
  • If their estimation is correct, they may consume all the Gas and reach their destination successfully.

The concept of Gas in Ethereum is similar to the scenario discussed above.

What is Gas?

Gas is a measure of computational effort required to perform an operation on the blockchain. The fees will be calculated proportional to the Gas.

Why do we need Gas when we have Ether?

Ether (ETH) is the cryptocurrency associated with the Ethereum blockchain. The validator is rewarded with some newly created ETH whenever a block is developed. It is the representation of value in the Ethereum ecosystem.

Gas is a metering mechanism to measure the effort required to complete a transaction. Just like fuel requirement is measured in litres and the price is calculated in rupees, transaction execution overhead is calculated in Gas and the transaction fee is calculated in Ether.

Let us learn how to calculate the cost of Diya.

On compilation, the smart contract will be converted to a low-level format of opcodes. For example, the statement

s = x+ y

written in a smart contract will be represented in opcodes like

PUSH DUP1 DUP3 DUP5 PUSH SWAP2 SWAP1

PUSH JUMP JUMPDEST SWAP1 POP DUP1……..

These opcodes are machine instructions defined in the Ethereum Yellow Paper and later updated by various Ethereum Improvement proposals. Each opcode has an associated gas value, also defined in the yellow paper. Check this reference to know the opcode and associated gas value

When Diya deploys her smart contract on the blockchain, the cost of deployment will be proportional to the size of the smart contract code.

The gas Limit is the maximum amount of Gas required for a transaction. For this smart contract deployment, it is estimated to be 145,367.

As per the EIP-1559, fees consist of two parts — base fee and priority fee. The base fee is a value set by the protocol, and the priority fee is a value set by the user as a tip to the validator. The base fee is a property of a block. If a transaction is included in a block, the offered price per Gas must equal the base fee or more. The base fee of a block is decided by a formula based on the block size (total amount of Gas consumed by all the transactions in a block) of the blocks preceding it, making transaction fees predictable for the user. Once the block is created, the base fee will be burned, removing it from circulation.

Gas price is the cost of one unit of Gas. It is the sum of the base fee and the priority fee.

Why is the base fee burned?

The base fee is what you offer for the service, and the priority fee is a tip to the service agent. Burning means that the base fee is not given to anyone; instead, it is sent to an account which does not have a private key. The burning mechanism was introduced to make fees more predictable and control the inflation associated with Ethereum.

Why do we need a priority fee?

A priority fee is a tip to the validator for including a transaction in a block. The validator will be interested in selecting the transactions which offer a good tip to be included in the block. If a priority fee is absent, the validator may profit even by mining empty blocks.

Let us calculate the deployment cost.

Base fee = 0.000000013 Gwei , [1 Gwei = 1000000000 wei = 0.000000001 ETH]

Priority fee (Tip) = 2.5 Gwei

Gas Price= base fee + tip

= 2.500000013 Gwei

= 0.000000002500000013 ETH

Gas Limit = 145,367

Total transaction fee = gas limit x (base fee + tip)

= 145,367 x 0.000000002500000013

=0.000363418 ETH

maxFeePerGas is an optional parameter which specifies the maximum limit a user is willing to pay for the transaction execution. This value should be greater than the base fee and tip sum. The transaction sender is refunded the difference between the max fee and the sum of the base fee and tip.

Once deployed, anyone (unless access restrictions are imposed on the smart contract) can access the operations of the smart contract.

Now Dev tries to call the sum function in Diya’s smart contract. When he tries to invoke the sum function, which is used to add two numbers and return the result.

Let us calculate the transaction cost.
Base fee = 0.000000014 Gwei
Priority fee (Tip) = 2.5 Gwei
Gas Price= base fee + tip
= 2.500000014 Gwei
= 0.000000002500000014 ETH
Gas Limit = 44,483
Total transaction fee = gas limit x (base fee + tip)
= 44,483 x 0.000000002500000014
=0.000111207500622762 ETH

What will happen if Diya has an infinite loop in the sum function?

The estimation logic will calculate a gas limit, and the transaction fee will be calculated accordingly. But as in the case where the fuel finishes before the destination, the nodes will execute the function until the computational effort equivalent to the set gas limit is exhausted. Once it’s over, the transaction processing will halt without affecting the network nodes.

Summing up

Decentralising programming was far more challenging than decentralising a currency system. Gas plays a pivotal role in maintaining the Ethereum world computer. Besides merely acting as an alternative representation of value, Ether is a medium that fuels the smart contracts in the programmable blockchain.

References

[1] Gas and fees | ethereum.org. https://ethereum.org/en/developers/docs/gas/?ref=blog.obol.tech

--

--

Kerala Blockchain Academy

One-stop solution for quality blockchain education and research. Offers best in class blockchain certification programs in multiple blockchain domains.