What is Ethereum Gas: Step-By-Step Guide – Blockgeeks

Ethereum Gas is the lifeblood of the Ethereum ecosystem, there is no other way of putting that. Gas is a unit that measures the amount of computational effort that it will take to execute certain operations.

Every single operation that takes part in Ethereum, be it a simple transaction, or a smart contract, or even an ICO takes some amount of gas. Gas is what is used to calculate the amount of fees that need to be paid to the network in order to execute an operation.

In this guide, we are going to understand how gas works. But before we do so, there are several concepts that we must learn. So, without further ado, lets begin our deep dive on Ethereum Gas.

Bitcoin was created because everyone was asking the same questions.

Satoshi Nakamoto answered these questions when he created bitcoin. We finally had a decentralized monetary system which can transfer money from one person to another.

However, there was a problem with bitcoin which is a problem with all first generation blockchains. They only allowed for monetary transactions, there was no way to add conditions to those transactions.

Alice can send Bob 5 BTC, but she couldnt impose conditions on those transactions. Eg. She couldnt tell Bob that he will get the money only if he performed certain tasks.

These conditions would need extremely complicated scripting. Something was required to make the process more seamless.

And that something was a smart contract.

Smart contracts help you exchange money, property, shares, or anything of value in a transparent, conflict-free way while avoiding the services of a middleman.

Vitalik Buterins Ethereum is easily the stalwart of this generation. They showed the world how the blockchain can evolve from a simple payment mechanism to something far more meaningful and powerful.

So, what are these smart contracts and whats the big deal?

Smart contracts are automated contracts. They are self-executing with specific instructions written in its code which get executed when certain conditions are made.

You can learn more about smart contracts in our in-depth guide here.

Smart contracts are how things get done in the Ethereum ecosystem. When someone wants to get a particular task done in Ethereum they initiate a smart contract with one or more people.

Smart contracts are a series of instructions, written using the programming language solidity, which works on the basis of the IFTTT logic aka the IF-THIS-THEN-THAT logic. Basically, if the first set of instructions are done then execute the next function and after that the next and keep on repeating until you reach the end of the contract.

The best way to understand that is by imagining a vending machine. Each and every step that you take acts like a trigger for the next step to execute itself. It is kinda like the domino effect. So, lets examine the steps that you will take while interacting with the vending machine:

Now look at all those steps and think about it. Will any of the steps work if the previous one wasnt executed? Each and every one of those steps is directly related to the previous step. There is one more factor to think about, and it is an integral part of smart contracts. You see, in your entire interaction with the vending machine, you (the requestor) were solely working with the machine (the provider). There were absolutely no third parties involved.

So, now how would this transaction have looked like if it happened in the Ethereum network?

Suppose you just bought something from a vending machine in the Ethereum network, how will the steps look like then?

Every transaction that you do through the smart contracts will get recorded and updated by the network. What this does is that it keeps everyone involved with the contract accountable for their actions. It takes away human malice by making every action taken visible to the entire network

Before we understand what the Ethereum Virtual Machine (EVM) is, we must understand why a Virtual Machine is needed.

So lets go back to smart contracts.

What are the desirable properties that we want in our smart contract?

Anything that runs on a blockchain needs to be immutable and must have the ability to run through multiple nodes without compromising on its integrity. As a result of which, smart contract functionality needs to be three things:

A program is deterministic if it gives the same output to a given input every single time. Eg. If 3+1 = 4 then 3+1 will ALWAYS be 4 (assuming the same base). So when a program gives the same output to the same set of inputs in different computers, the program is called deterministic.

There are various moments when a program can act in an un-deterministic manner:

In mathematical logic, we have an error called halting problem. Basically, it states that there is an inability to know whether or not a given program can execute its function in a time limit. In 1936, Alan Turing deduced, using Cantors Diagonal Problem, that there is no way to know whether a given program can finish in a time limit or not.

This is obviously a problem with smart contracts because, contracts by definition, must be capable of termination in a given time limit. There are some measures taken to ensure that there is a way to externally kill the contract and to not enter into an endless loop which will drain resources:

In a blockchain, anyone and everyone can upload a smart contract. However, because of this the contracts may, knowingly and unknowingly contain virus and bugs.

If the contract is not isolated, this may hamper the whole system. Hence, it is critical for a contract to be kept isolated in a sandbox to save the entire ecosystem from any negative effects.

Now that we have seen these features, it is important to know how they are executed. Usually, the smart contracts are run using one of the two systems:

Lets compare these two and determine which makes for a better ecosystem. For simplicitys sake, we are going to compare Ethereum (Virtual Machine) to Fabric (Docker).

So, as can be seen, Virtual Machines provide better Deterministic, terminable and isolated environment for the Smart contracts. However, dockers have one distinct advantage. They provide coding language flexibility while in a Virtual Machine (VM) like Ethereum, one needs to learn a whole new language (solidity) to create smart contracts.

The EVM is the virtual machine in which all the smart contracts function in Ethereum. It is a simple yet powerful Turing Complete 256-bit virtual machine. Turing Complete means that given the resources and memory, any program executed in the EVM can solve any problem.

As explained in the introduction, Gas is a unit that measures the amount of computational effort that it will take to execute certain operations.

Note: Before we continue, huge shoutout to Joseph Chow for his amazing presentation on Ethereum gas.

Most of the smart contracts that run in the EVM are coded using Solidity (Ethereum is planning to move on to Viper from Solidity in the future). Each and every line of code in Solidity requires a certain amount of gas to be executed.

The image below has been taken from the Ethereum Yellowpaper and can be used to gain a rough idea of how much specific instructions cost gas-wise. Every transaction requires at least 21,000 gas according to this table:

Image Courtesy: Ethereum Yellow Paper

To better understand how gas works in Ethereum, lets use an analogy. Suppose you are going on a road trip. Before you do so you go through these steps:

Now, lets draw parallels with Ethereum.

Driving the car is the operation that you want to execute, like executing a function of a smart contract.

The gas is well.gas.

The gas station is your miner.

The money that you paid them is the miner fees.

All the operations that users want to execute in ethereum must provide gas for the following:

Now that we have covered the bare basics, you maybe asking the following question.

The answer is simpleincentivization.

Like any proof-of-work peer-to-peer system, Ethereum is heavily dependent on the hashrate of their miners. More the miners, more the hashrate, more secure and fast the system.

In order to attract more miners into the system, they need to make the system as profitable and alluring as possible for the miners. In Ethereum, there are two ways that miners can earn money:

Lets explore the second point.

The miners are responsible for putting transactions inside their blocks. In order to do so, they must use their computational power to validate smart contracts. The gas system allows them to charge a certain fee for doing so.

This fee is known as the miners fee and it helps incentivize them enough to take part actively in the ecosystem.

So, how much fees can they charge? Before we can calculate that lets understand how we measure gas.

Gas is simply measured in units of gas. A transaction sent to the Ethereum network costs some discrete amount of gas (e.g. 100 gas) depending on how many EVM instructions need to be executed.

More here:

What is Ethereum Gas: Step-By-Step Guide - Blockgeeks

Related Posts

Comments are closed.