Creating a private blockchain using Hyperledger Besu

IBFT 2.0 Proof of Authority consensus mechanism

Prasanth Venugopalan
Coinmonks
Published in
6 min readMar 2, 2022

--

The rapid progress of Blockchain technology is showing no signs of slowing down. One by one it solves many issues of existing digital ecosystem. Main feature of blockchain technology is the decentralisation. Organisations can exploit this feature to make their entire system of operations or services into a more reliable, distributive and automated environment. So changing the current centralised mechanism into more decentralised and thereby exploiting many features of blockchain technology while keeping proper control over the system, that what we are building today.

We are using Hyperledger Besu to create the blockchain.

Hyperledger Besu is an Ethereum client designed to be enterprise-friendly for both public and private permissioned network use cases. It can also be ran on test networks such as Rinkeby, Ropsten, and Görli. Hyperledger Besu includes several consensus algorithms including PoW, and PoA (IBFT, IBFT 2.0, Etherhash, and Clique). Its comprehensive permissioning schemes are designed specifically for use in a consortium environment.

We are creating a Private blockchain with 4 nodes (Because IBFT require minimum 4 nodes to operate). In this 4 nodes one will be bootnode and others will be validators. We will be running besu from docker image for easiness. We can use the source code also. In that case clone the source code from repo and add /bin/besu to the path.

First we are setting up the directory as shown:

The first block of blockchain is called Genesis block. So we need to create a genesis file and from that we will start our network. We can use besu to generate a genesis file from a configuration file. Create a file named ibftConfigFile.jsoninside IBFT-Network/and add the below code to it:

chainId can be according to your preference. Please make it a chainId that you aren’t already using in any wallet. Because if you have already connected your wallet to a network with same chain id and had done some transactions in it, then you will get incorrect nonce in this network. That will cause issues and transactions will not be confirmed. gasLimit is set to a higher value because this will be a free gas network. In alloc you can specify some accounts or smart contracts (predeployed) to which initial amount of native coins will be allocated (Basically ETH because we are forking from Ethereum, but you can consider it as your own coin and can be addressed with different name).

Now if you are using besu from source code run :

besu operator generate-blockchain-config --config-file=ibftConfigFile.json --to=networkFiles --private-key-file-name=key

If you are using docker image of Besu create a docker-compose.yaml file with following content, then run docker-compose up.

This will generate a folder named networkFiles. Inside that there will be a genesis file and another directory named keys with 4 directories inside. Inside all these directories there will be 2 files keyand key.pub. These are the key pairs for our 4 nodes.

From networkFilescopy the genesis file to all the node directories (Node-1, Node-2 etc). Then copy a set of key and key.pub to Node-1/data . Similarly another set of key and key.pub to all other Nodes.

Now we need a config.toml for configuring each node. Place it inside each Node- directory.

You can add many more options. I am just keeping it simple. Please remember if you are setting up the network in same host change the different ports for each node. For the time being keep the same configuration for each node, only ports will be different. Now we have everything to start our network. So create a docker-compose.yaml inside each node so that we can start nodes individually too.

Since I am going to run all 4 nodes in a single machine I need to start all nodes in a single command. So I am going to combine all the nodes via another docker-compose.

I am using network mode as host, so that each node can establish p2p connection without complication. If you want to you create custom network and connect all the nodes to it also.

We are not done yet! We are at the final stage. Currently nodes doesn’t have any idea about the other nodes. So what we have to do is to start the bootnode and copy its enode url and provide it in the other nodes config file. You also start bootnode alone to copy enode url. But I am starting all of them together and restart it later.

We have included the combined docker-compose.yaml at IBFT-Network/ . Inside that directory I run docker-compose up -d. Now all the 4 nodes will be up and running. Now every nodes will console its public key, address, enode url, opened urls for different services and all. We copy bootnode’s enodeUrl alone and specify it in other nodes config.toml like this.

If you are not using network_mode as host please change the last part of enode url (127.0.0.1:30303) to bootnode's assigned IP.

Final folder structure will be like this (removed networkFiles after copying):

Now we all set. Just restart the all the containers and you will see from the logs, nodes will be communicating with bootnode . Within a few time network will be ready and start mining blocks. For further information check the official documentation of Besu

Thanks for reading….!!

Reference : https://www.hyperledger.org/use/besu
Github Repo: https://github.com/prasanth0105/besu-ibft2-network

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--