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.

Setting up a Private Ethereum Blockchain Network on Windows

Setting up a private Ethereum blockchain network is not an extremely difficult task for the coders. However,  it may be a bit of a challenge for the beginners.  I will skip some technical details and avoid using some jargon so that everyone can understand the basic concepts.  

Prerequisites

Before setting up the network, you need to install the following software:

  1. Visual Studio Code
  2.  git
  3.   NodeJs 
  4. Ethereum Wallet
  5. Geth

The go-ethereum client is commonly referred to as geth, which is the command line interface for running a full Ethereum node implemented in Go. By installing and running geth, you can run a private network or participate in the Ethereum main network. By running geth, you can perform the following tasks:

  • mine real ether
  • transfer funds between addresses
  • create smart contracts and send transactions
  • explore block history
  • and much much more

Creating the Genesis Block

To set up the private network, we need to create the Genesis block, the first block of the blockchain in our network. The code for the genesis block is written in JSON format.  JSON stores data as a  name/value pair.  For example:

"name": "John"  ,
"age": 30


It uses JavaScript syntax, but the format is text only.  Therefore,
JSON can be read and used as a data format by any programming language. You can use any text editor to write the JSON code(JSON: JavaScript Object Notation), I use Notedpad++. The sample code for the genesis block is as follows

{
"config":{
 "chainId": 45,
 "homesteadBlock": 0,
 "eip155Block": 0,
 "eip158Block": 0,
 "byzantiumBlock": 12
 },
 "alloc" : {},
 "coinbase" : "0x0000000000000000000000000000000000000000",
 "difficulty" : "0x20000",
 "extraData" : "",
 "gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
 "mixhash" :"0x0000000000000000000000000000000000000000000000000000000000000000",
 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
 "timestamp" : "0x00"
}

I will not discuss the contents of the genesis block here. Copy the code in a text editor and save the file as

myGenesis.json

Now run the following command in the command prompt to initialize the genesis block

geth init myGenesis.json

The output is as follows:

Once the genesis block is successfully created, a folder name ‘Ethereum’ will be created in the following path:

“C:\Users\admin\AppData\Roaming\Ethereum”

This folder contains the details of the Private Blockchain.

Starting the Private Network

Once the genesis block is created, run the following command to start the private network:

geth — networkid=5

*“networkid=1” stands for the main Ethereum network. So any random number apart from 1 can be given as the network id. Also, “console” is appended to the command in order to enable us to write commands while the network is running.

The output is as follows:

Launching the Ethereum Wallet

The output is as shown in the following figure. Notice that the network name is Private net.

Creating a New Account Address

You can create an address in the Ethereum Wallet application. The address can be created on the Ethereum Wallet App. In the ‘Wallets’ section, click on ‘Add Account’ to create a new account address.

To create new account using the Geth Console, use the following command:

geth account new

The output

Start Mining

To start mining on the private network,  Enter the geth console with  the following command

Geth --rpc console

In the Geth Console type

miner.start()

The output is as follows

Now you can see that the mining process is generating Ethers ,  as shown in the following figure. Bear in mind that these are fake Ethers and can only be used for a private test net.

Stop Mining

Type Ctrl+C in the geth console and key in the following command:

miner.stop()

Type Exit  to quit the geth console

What is Ethereum?

According to the Ethereum Foundation, 

Ethereum is a decentralized platform that runs smart contracts: applications that run exactly as programmed without any possibility of downtime, censorship, fraud or third-party interference.

These apps run on a custom built blockchain, an enormously powerful shared global infrastructure that can move value around and represent the ownership of property.

Definition of Ethereum

Based on the above descriptions, Ethereum is an open-source blockchain-based decentralized platform featuring smart contracts.  Indeed, Ethereum is a programmable blockchain that enables developers to build and deploy decentralized applications. Rather than providing users with a set of predefined applications like bitcoin, Ethereum allows users to create any kind of applications they wish. In this way, it serves as a platform for many different types of decentralized blockchain applications, including but not limited to cryptocurrencies.

Ethereum Virtual Machine

At the core of the Ethereum platform is the Ethereum Virtual Machine (“EVM”), which possesses its own programming language, known as the ‘EVM bytecode’.  It is the runtime environment that executes all of the smart contracts on the Ethereum network.

The Smart Contract code is written in high-level programming languages such as Solidity. The code is compiled to the EVM bytecode so that the Ethereum Virtual Machine can understand what has been written.

Decentralized Applications(dapps)

The Ethereum Virtual Machine makes the process of creating decentralized applications much easier than ever before. Instead of having to build an entire blockchain for each new application, the EVM enables users to develop decentralized applications all on one platform.

Cryptokitties  is one of the most well known decentralized application (dapp) among many dapps developed so far. You can check out lots of interesting dapps at https://www.dapp.com/.  I have also developed a prototype dapp that I named it Kittychain Shop. In this virtual pet shop, the user can adopt a kitty, as shown in the figure below.

The Ethereum Architecture

Ethereum runs on a distributed public blockchain network. Each and every node connected to the Ethereum network helps to maintain and update the blockchain database. The nodes of the network run the EVM and execute the instructions according to the smart contracts.   Ethereum node runs the EVM in order to maintain consensus across the blockchain.

Ethereum peers achieve consensus via the proof of work algorithm, which is similar to bitcoin. However, they are planning to shift to the proof of stake algorithm in near future to improve scalability. The consensus gives Ethereum a high level of fault tolerance, ensures zero downtime, and makes data stored on the blockchain immutable and secure.

Like Bitcoin, Ethereum allows individuals to exchange cash without involving any middlemen like financial institutions and others. However, Ethereum is more than just cryptocurrency. Beyond financial applications, we can create decentralized applications where trust, security, and permanence are considered important. Among the potential applications are asset registries, voting, governance, Internet of things, supply chain management and more. 


Similar to Bitcoin, we can also carry out mining activity in Ethereum. However, In the Ethereum blockchain, instead of mining for bitcoin, miners work to earn Ether, the default currency of Ethereum. Beyond a tradeable cryptocurrency, Ether is also used by application developers to pay for transaction fees and services on the Ethereum network. For example, the user needs to pay for acquiring a crypto asset in a dapps marketplace using Ether.