Hyperledger Fabric Blockchain Setup From Scratch

Chandramohan Jagtap
Geek Culture
Published in
4 min readMay 1, 2021

--

Hyperledger Fabric

Introduction

In this article, we are going to set up a Hyperledger Fabric blockchain network.

Audience

Hyperledger fabric beginners, operators, admins. This is a practical hands-on article to set up a fabric network. for the beginner, I would like to advise please go through some basic concepts of Hyperledger fabric. In my previous work, I already explain some of the important concepts.

Prerequisites

  1. Docker — version 17.06.2
  2. Docker Compose — version 1.28.5 or greater
  3. Golang — version 1.14
  4. Nodejs — version 8
  5. Python 2.7

Network Setup

  • Certificate Authority (CA) for each organization (Orderer CA, Org1 CA, Org2 CA)
  • Orderer cluster (3 ordering service nodes) RAFT as consensus algorithm(Orderer, Orderer2, Orderer3)
  • Two organizations Org1 and Org2.
  • Each organization with two peers peer0 (endorsing/committing peer)and peer1 (Anchor peer)
  • CouchDB as a world-state database.
  • Fabric latest version 2.3

Setup Steps

  • Organization1 setup.
  • Organization2 setup.
  • Ordering cluster setup.
  • Application channel creation and joining organizations to channel.

Folder Structure

  • Let’s clone the repo which contains the source code.
git clone https://github.com/cmjagtap/Hyperledger-Fabric

The following figure shows the directory structure of the cloned repository.

Fabric network folder structure
  • bin: Contains fabric binaries.
  • clean.sh: This script is helpful to clean the fabric network.
  • config: Fabric core configuration file.
  • Orderer: Orderer contains necessary docker files and scripts to set up an ordering service.
  • Org1: Contains necessary setup files of org1.
  • Org2: Contains necessary setup files of org2.

Org1 setup

Let's set up the first organization. before proceeding, we have to export the fabric binaries path.

export PATH=${PWD}/bin:$PATH

Check docker status using the following command.

sudo service docker status

if docker is not running, then you can start it using the following command.

sudo service docker start

Change directory to the org1 and execute the below scripts.

Org1 Folder Structure
#org1
cd org1
./1_enrollOrg1AdminAndUsers.sh
./2_generateMSPOrg1.sh
  • The 1_enrollOrg1AdminAndUsers script brings up org1 CA.
  • ca-org1.yaml file contains a docker script.
  • We enrolled admin user into CA using fabric-ca-client.
  • Then created organization units (OU’s) and enrolled users to org1 CA.
  • The 2_generateMSPOrg1.sh script generates crypto material for each Membership Service Provider (MSP).
  • docker-compose-peer.yaml file contains a docker script for peers and CouchDB.
  • After successful setup, we can see running docker containers.
Org1 docker containers
  • The above diagram shows two peer containers named peer0.org1.example.com, peer1.org1.example.com with associated two CouchDB containers and one CA container.

Do not execute script 3_createChannel.sh we will get back to this.

Org2 setup

Let’s proceed to set up a second organization. Change directory to the org2 and execute the following scripts.

Org2 Folder Structure
#org2
cd org2
./1_enrollOrg2AdminAndUsers.sh
./2_generateMSPOrg2.sh
  • These scripts perform the same operations as stated in organization 1.
  • After successful setup, we can see org1 and org2 docker containers.
Docker containers
  • The above diagram shows two peer containers named peer0.org2.example.com, peer1.org2.example.com with associated two CouchDB containers and one CA container.

Do not execute script 3_joinChannel.sh we will get back to this.

Orderer Setup

Let’s set up the orderer organization. Change directory to the orderer and execute the below scripts.

Orderer Folder structure
#orderer
cd orderer
./1_enrollAdminAndMSP.sh
./2_artifact.sh
  • In, the 1_enrollAdminAndMSP script bring up orderer CA.
  • ca-orderer.yaml file contains a docker script.
  • We enrolled admin user into CA using fabric-ca-client.
  • Then created organization units (OU’s).
  • Enrolled three orderers to CA.
  • Generated MSP for all three orderers and orderer admin.
  • 2_artifact.sh script generates a genesis block, this is the first block in the blockchain.
  • After the genesis block, we have generated channel block, anchor peers updates.
  • If the setup is successful, we should see all docker containers are up.

Note: I have made some policy changes itno configtx.yaml, so that we don’t need approval from other organizations.

Channel Creation

Let's create an application channel from org1 using the following script.

#Org1 
cd org1
./3_createChannel.sh
  • Create channel scripts exports org1 environment variable and creates a channel.
  • After successful channel creation, we can see mychannel.block file is generated.
  • Each organization needs to join the channel.
  • Let’s join the channel from the second organization.
#org2 
cd org2

./3_joinChannel.sh
  • The script fetches the channel block from the orderer and joins the channel.

Hyperledger fabric 2.3 blockchain is ready. Now we can deploy any chaincode (Smart contracts).

In the next article, we are going to create sample chain codes(smart contracts). will deploy them on this blockchain setup.

Clean up

Execute the following script to clean all docker containers and crypto material.

./clean.sh

Summary

We have completed the fabric blockchain setup with two organizations and three orderers. RAFT is a consensus algorithm and One application channel.

--

--