How to Fork The Ethereum Blockchain

Okereke Innocent   |   

Jan 03, 2022

Jan 03, 2022

So much has been said about Ethereum Fork. Through this article, you will learn beyond the verbal meaning of the Ethereum fork to how to fork an Ethereum blockchain. However, it is still necessary to intimate our readers on the basics of the Ethereum fork to get them abreast with the subject.

What is an Ethereum Fork?

Ethereum fork is the product of copying, updating, and building on an Ethereum blockchain. This process sometimes results in two cryptocurrencies. However, it is often not the intention of forking. Rather, it is meant to upgrade an existing blockchain. Ethereum fork like others can be soft or hard fork; it is left for the developers to know which fork to perform. 

If the developer intends a backward compatible and a non-consensual fork, they should consider soft forking, but if it is a non-backward compatible and consensus fork, the best approach should be a hard fork. 

Before going deeper to learn how to fork the Ethereum blockchain, note that a fork can be unintentional or accidental. When two or more miners find a block at the same time, it is an accidental fork. On the other hand, an intentional fork refers to a deliberate move to modify the rules of the blockchain. 

Why Fork a Blockchain

Blockchain could be forked for several reasons. Meanwhile, the onus of forking a blockchain is to improve the scalability, decentralization, cost of the transaction, security, and affect other changes worthwhile on the existing chain instead of building a new one. For instance, the Ethereum blockchain even from its frontier stage up till the awaiting serenity stage has proven to be sturdy, robust, and has many untapped potentials. Therefore, forking is the best since it will take more resources, time, and effort to create a similar blockchain. 

Requirements to Forking a Blockchain

If you ever considered forking an Ethereum blockchain, there are prerequisites you must meet. The requirement for forking a blockchain ranges from software to hardware requirements. Below are the prerequisites; 

Step 1; Basic knowledge of the blockchain

It will be difficult to fork a blockchain you know nothing about. Every developer must know how the blockchain works to enable him/her to fork the blockchain. Particularly for the Ethereum blockchain, the developer has to know how the Ethereum development environment works as well as how it interacts with other environments beyond the local environment.

Step 2; Basic development skills including JavaScript, Linux command, etc

Every blockchain has its compliant programming tools and language. However, JavaScript, Python, and Linux command lines are the basic language of most blockchains including Ethereum.

Step 3; Windows, Linux, or a Mac OS

Various blockchain forking tools are designed for various operating systems. It is expected that you run on either Linux, Windows, or Mac OS. That way, you simply download and install the operating system version of the tool you use. However, Linux and its distros are more in demand and a must-have for the best development experience. 

Step 4; Ethereum client for instance; Ganache GUI or Ganache CLI, RPC, Ruffle, etc. 

An 'Ethereum client' is just a term that refers to any node able to parse and verify the blockchain, its smart contracts, and everything related. It also allows you to provide interfaces to create transactions and mine blocks which is the key for any blockchain interaction. To fork an Ethereum blockchain, we will be using Ganache-Cli

Step 5; Ethereum nodes include Infura, QuikNode, Geth, Nethermind, etc. 

Ethereum Nodes allows you to run your own nodes which helps you to run a private, trustless, and self-sufficient application. However, it's somewhat costly and tasking. Instead of going through the hassle of using Geth, Open Ethereum, etc., you can simply use third-party APIs like Infura, quikNode, and the likes. 

Steps to Forking an Ethereum Blockchain

Having got the requirements to forking the Ethereum blockchain, it is time to get started. Here is a step-by-step guide on how to fork an Ethereum blockchain. It is expected that after going through this guide, you'd be akin to having your first Ethereum fork. Read on; 

Create an Ethereum Endpoint

Let's start by choosing the Ethereum client that will help us in creating an Ethereum endpoint. There are varieties of Ethereum nodes such as Geth, Nethermind, Open Ethereum, Infura, QuikNode, and so on. You can run your node, but that requires more resources and technicalities. Therefore, you can use third-party APIs like Infura, QuikNode, Alchemy to run your node. 

For the sake of this article, we will choose to create our Ethereum endpoint using Infura or QuikNode. To create the Ethereum endpoint, sign up on any of the third-party APIs; Infura or QuikNode and follow the process. 

QuikNode

Login and verify your account to create a node. Make sure you verify the email before choosing a subscription that suits the node you want to deploy. 

Once you have access to your account, endpoint details will be generated for your node. Head to node and copy the HTTP protocol as follows;

Infura

If you prefer to create your endpoint using Infura, you can do the following; 

  1. Signup on Infura.io and verify your mail.
  2. Head to create a project. In this article, I named the project "NewFork." 
  3. Copy the endpoint as shown on the figure below. 

Remember to set the endpoint to the Mainnet before copying the HTTP Protocol. 

Note; whether you use QuikNode or Infura, don't forget to save the HTTP Provider Endpoint you copied because you will use it after setting up Ganache CLI. 

Download and Install Ganache CLI

Ganache-CLI is one of the major components of the Turtle client. It is the command-line interface of the Ganache Ethereum client that allows you to connect to a local blockchain for testing your decentralized application. Instead of forking the main-net directly, the ganache CLI allows you to have a local environment for your developments.

Ganache CLI uses ethereumjs to simulate full client behavior making development on Ethereum faster, easier, and safer. If you want to fork the Ethereum blockchain, it allows you to connect to a local Ethereum blockchain. For simplicity of illustration, we will be using Ganache-CLI to create a local blockchain on localhost:8545.

Linux users can download, install and run Ganache-Cli in two ways:

  1. Ganache CLI using npm (Node Package Manager). Open your terminal and run the command; 

$ npm install -g ganache-cli. 

  1. yarn package manager. Run theses for yarn package manager; 

$ yarn global add ganache-cli

Fork the Ethereum Main-net

Now you have installed Ganache-Cli, it is time to fork the mainnet.  Open your terminal/cmd and copy-paste the following:

$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE>

Remember to replace ADD_YOUR_QUICKNODE_URL_HERE with the QuickNode or Infura HTTP URL you got earlier and run the command; you should see something similar to the figure below; 

What happened is that it forked the mainnet at the blockchain’s latest block, 12200647. To find out, you can query the forked chain by pinging localhost:8545 as will be shown later.

Fork a Specific Block

Note the specific block in the blockchain you would like to fork. Fork it by appending "@" followed by the block number as shown below:

$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE>@<block_number>

Assuming we want to copy and make changes to a certain say xDai chain residing on the Ethereum blockchain network and use xDai for gas. Proceed to ETHExplorer and scroll to the holder's section. View the biggest Dai holders copy the address and run the following:

$ ganache-cli --fork <ADD_YOUR_QUICKNODE_URL_HERE> -u <address of token holder>

The above command line allows ganache to fork the Ethereum blockchain and unlock (-u) the attached Dai account for the local ganache environment. By implication, we used ganache-cli to impersonate a particular account address which is usually locked for the use which also helps us to make transactions on the simulated blockchain from that account address.

How to Query the Forked Coin

Impressive! You have forked a certain Dai account and will be happy to query the forked chain. Querying the forked chain allows you to get certain information about the forked chain such as transactions, gas used in the block, timestamp, miner’s address, etc.

You can do that by making an eth_getBlockByNumber call that will return information about the block at which we forked the chain. 

The command should look as follows;

$ curl --data '{"method":"eth_getBlockByNumber","params":["0xBA29D2",false],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545

After you have run the code shown above, your terminal should look like these;

Conclusion

After going through this guide, it is expected that you know how to use  Ganache-Cli to fork a blockchain. You can deploy and fork a blockchain on a local environment using Ganache-Cli. To do that, you need to master basic command lines and understand the Ethereum development environment.

Also Read; Defi 2.0: An Upgrade to The First Generation of Defi

Written by

Similar Articles

January 9, 2021
Author: Zainab Hasan
January 28, 2021
Author: Zainab Hasan
March 15, 2021
Author: Zainab Hasan
1 2 3 16

Get notified on our latest Web3 researches and catch Xord at a glance.

    By checking this box , I agree to receive email communication from Xord.