Hyperledger Fabric (Raft consensus) step-by-step setup

Akash Gupta
Theta One Software
Published in
7 min readJan 7, 2021

Hyperledger Fabric is a modular blockchain framework that has become the de facto standard for enterprise blockchain platforms. It offers a unique approach to consensus that enables performance at scale while also preserving the data privacy to cater enterprises demand.

Bitcoin — World’s first blockchain | Photo by André François McKenzie on Unsplash

When I was learning Hyperledger Fabric for the very first time, I found that it was difficult to understand how Hyperledger Fabric really works because of its enormous technology stack. To really understand how it works, a learner is required to have knowledge of several aspects such as blockchain technology, network and system architecture, DevOps operations, full-stack software development, test driven and behavior driven development, intermediate-level cryptography, authorization and access control, IT security aspects, business use cases, and etc.

So I thought to make the whole learning process little bit easier for newbies. In this blog I tried to put pieces together for setting up a docker based network.

Hyperledger Fabric : A brief intro

Hyperledger Fabric is a permissioned blockchain network that gets set by the organizations that intends to setup a consortium. The organizations that take part in building the Hyperledger Fabric network are called the “members”.

Each member organization in the blockchain network is responsible to setup their peers for participating in the network. All of these peers need are configured with appropriate cryptographic materials like Certificate Authority and other information.

Peers in the member organization receives transaction invocation requests from the clients inside the organization. A client can be any specific application/portal serving specific organization/business activities. Client application uses Hyperledger Fabric SDK or REST web service to interact with the Hyperledger Fabric network. Chaincode (similar to Ethereum Smart Contract) installed in peers causes to initiate transaction invocation request.

All the peers maintain their one ledger per channel that they are subscribed to. Hence Distributed Ledger Technology (DLT). But unlike Ethereum in Hyperledger Fabric blockchain network peers have different roles.

So not all peer nodes are same. There are different types of peer nodes with different roles in the network:

  • Endorser peer
  • Anchor peer
  • Orderer peer

Endorser peer

Peers can be marked as Endorser peer (ie Endorsing peer). Upon receiving the “transaction invocation request” from the Client application the Endorser peer

  • Validates the transaction i.e. Check certificate details and roles of the requester.
  • Executes the Chaincode(i.e. Smart Contract) and simulates the outcome of the transaction. But it does not update the ledger.

At the end of the above two tasks the Endorser may approve to disapprove the transaction.

As only the Endorser node executes the Chaincode (Smart Contract) so there is no necessity to install Chaincode in each and every node of the network which increases the scalibility of the network.

Anchor peer

Anchor peer or cluster of Anchor peers is configured at the time of Channel configuration. Just to remind you, in Hyperledger Fabric you can configure secret channels among the peers and transactions among the peers of that channel are visible only to them.

Anchor peer receives updates and broadcasts the updates to the other peers in the organization. Anchor peers are discoverable. So any peer marked as Anchor peer can be discovered by the Orderer peer or any other peer.

Orderer peer

Orderer peer is considered as the central communication channel for the Hyperledger Fabric network. Orderer peer/node is responsible for consistent Ledger state accross the network. Orderer peer creates the block and delivers that to all the peers.

While it seems all fancy and exciting to learn about such new concepts; when I began implementing a small solution with fabric I was left out in the open confused, frustrated and there was little or no help that was available on the internet. We found that though HYPErledger Fabric might be one of the best DLT technologies today; but the lack of community support or learning materials make it enormously difficult to grasp or implement. To this, me and my team decided to implement a prototype which will help understanding the technology and its application in solving real business problems. The solution uses docker images and Raft Consensus algorithm for ordering transactions. It has two organisations acme and budget as a part of the network.

Raft Consensus Algorithm

Raft is a modern, reliable and relatively less complicated distributed consensus algorithm that is frequently used in modern software solutions such as Consul, etcd, RabbitMQ and so on.

Before the inception of Raft, Paxos (designed by Leslie Lamport) was one of the most popular algorithms which are designed for the distributed consensus. Hence many distributed consensus algorithms were either based on Paxos or inspired from it. However, Paxos is known to be a really complex algorithm that is hard to implement to meet its performance and correctness requirements.

One of the main motives behind Raft is to design a distributed consensus algorithm that enhances understandability without compromising performance and correctness. Raft authors(Diego Ongaro and John Ousterhout) argue that Raft is superior to Multi-Paxos protocol as it enhances understandability by maintaining performance and correctness.

Please refer to the following for better understanding of the concept:

  1. https://raft.github.io/
  2. https://kasunindrasiri.medium.com/understanding-raft-distributed-consensus-242ec1d2f521

Setting up the network

Pre-requisites

  1. Ubuntu 18.04
  2. Docker (if not installed our scripts will take care of it)
  3. Go (if not installed our scripts will take care of it)

Installation

Due to huge size of the zip we uploaded our project to Google Drive folder. Please download the folder and unzip it and place it under $HOME.

https://drive.google.com/file/d/1lZV1_7uztt5gEfjc0ZhDFHUeBvduBAMk/view?usp=sharing

Follow the steps below for installation of pre-requisites (Docker, Go) and Fabric binaries at the designated locations explained and specified below:

cd fabricpoc/setup
  1. We will now install Docker and Go; if not already installed. The following script will install the above packages.
./install-prereqs.sh/* Logout and Login */

If the above script fails to execute; try installing Docker and Go manually. Make sure that Go is installed under /usr/local/.

Check whether Go has been installed successfully:

go version

Docker installation: https://medium.com/@novasush/how-to-install-docker-on-ubuntu-20-04-74249a03e2ea

/* Logout and Login */

2. Installed Fabric binaries (fabricpoc/setup)

sudo -E ./fabric-setup.sh/* Logout and Login */

The above script will install all crypto tools and fabric binaries under /usr/local/bin.

3. We will now configure to start peers and orderers:

cd ../* Sets GOPATH to the gopath folder inside fabricpoc containing all the required tools including grpc etc. */GOPATH=$PWD/gopath/cd docker/* Initialises docker images, generates crypto material and starts the channel */./init-setup.sh raft all

Following is the expected output.

./init-setup.sh raft all

Let us analyse the above output:

a. We can see a peer for acme (peer1.acme.com) and budget (peer1.budget.com) getting initialised.

b. Three orderer docker images initialised and couchdb’s for the respective organisations which hold the world state of the blockchain for the respective organisations.

c. After 12 -14 lines we see “Received Block: 0” being logged. This means the first block of the blockchain network i.e the genesis block has been received and initialised

d. 5–6 lines below we see a channel being created and the proposals to join the channel by the respective peers being accepted.

Now that we have channel created — lets try and invoke a sample chaincode already provided under fabricpoc/packages

4. Initialise and Invoke Chaincode - Before this step we need to decided the organisation from where we would want to invoke the chaincode. Lets take acme for this purpose.

/* Initialises important path variables like the FABRIC_CFG_PATH and other crypto path variables */. bins/set-context.sh acme raft/* Now we can check whether the above script has been executed successfully. If true - please refer to the output below */. bins/show-context.sh
. bins/show-context.sh
/* 
1. Install
2. Instantiate
3. Query
4. Invoke
chaincode */./test-all.sh raft

Please refer to the following outputs and cross-reference:

./test-all.sh raft
./test-all.sh raft

Hyperledger makes it easy for developers who write applications in Go, Python, Node.js or Java to interact with Fabric ledgers, as it supports Software Development Kits (SDKs). Developers simply import these SDKs into their application and provide a configuration that contains information like MSP, TLS certificates, peers and ordering service endpoints and channels.

Once your SDK is setup you can start to query chaincodes or invoke ledgers from your application.

Not all applications are suitable for a private blockchain. It is necessary to first answer the question if a private blockchain and Hyperledger Fabric specifically is right for your use case. Once that is clear, you should define what data is suitable to store on the chain and what data to store off the chain. After these prerequisites are solved you can start your blockchain application development or extend your existing application with blockchain. Without solving these two problems and having clear answers it will be a waste of your programming time.

In the current setup we have used the sample scripts to prepare our network. But they cannot be used as it is for production deployments. There are lots of concepts and configurations in Hyperledger Fabric, that have to be understood and implemented for a successful production grade development and deployment. Above all, the need of the hour is more engaging and more comprehensive support from the Hyperledger Fabric team which will of great help to the struggling fabric developer community.

If you like the article please share it among peers and also don’t forget to write your suggestions in the comment box.

Acknowledgement: I would like to thank Tanmoy Kanungo for his contribution, mentorship and support for making this project possible.

--

--