Hyperledger Fabric Network Setup from Scratch

Hyperledger Fabric Network Setup from Scratch

No Scripts used!

·

3 min read

Introduction

In this tutorial we will be setting up Hyperledger Fabric network from scratch without using any magical scripts that we use in test-network or first-network. Those scripts work great but they abstract many things that one needs to know in orderer to gain expertise in HLF.

We will be using Fabric version 2.2.3 LTS. If this is your first time with Fabric version 2+ then, I will highly recommend to install the prerequisites first before proceeding any further .

You can find the code used in this tutorial in my github repo

Once you have cloned or downloaded the repository, you can place it inside the fabric-sample directory(comes with the fabric installation).

At this stage, you will be ready to follow along!

What will we be building ?

We will be building a network which is similar to the "test-network" without using any scripts. The network consists of:

  • Two organizations
  • One peer per organization
  • One orderer (single node Raft)
  • Two CLIs for each organization

We will use docker-compose to orchestrate the network.

Step 1: Generating the crypto materials

We point to the fabric binaries which comes with the HLF installation.

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

Then we will use cryptogen to generate the certificates as per the configurations provided in the organizations/cryptogen folder.

From your terminal:

cryptogen generate --config=./organizations/cryptogen/crypto-config-org1.yaml --output=organizations

cryptogen generate --config=./organizations/cryptogen/crypto-config-org2.yaml --output=organizations

cryptogen generate --config=./organizations/cryptogen/crypto-config-orderer.yaml --output=organizations

You should be able to see two new directories created under organization

  • ordererOrganizations
  • peerOrganizations

The directories will have the certificates and MSPs for the orderer and peers.

Step 2: Genesis block generation

configtxgen -profile TwoOrgsOrdererGenesis -channelID system-channel -outputBlock ./system-genesis-block/genesis.block

This should generate genesis.block file under system-genesis-block directory.

Step 3: Generate channel artifacts

Channel artifact consists of:

  • Channel configuration transaction
  • Anchor peer updates
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/mychannel.tx -channelID mychannel

configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP

configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP

You will be able to see mychannel.tx, Org1MSPanchors.tx, Org2MSPanchors.tx in the channel-artifacts directory.

Step 4 : Bring up the network

We will use the docker-compose-test-net.yaml to bring up the network. This file contains the following services:

We first source the project environment variables the bring up the compose file.

. .env
docker-compose -f docker/docker-compose-test-net.yaml up -d

You should be able to see five containers up. 2 peers, 2 CLIs and 1 orderer.

Screenshot from 2021-06-07 23-37-51.png

Step 4: Channel Genesis Block File

We will be using one of our CLIs for this step.

We log inside the cliorg1:

docker exec -it cliorg1 bash

From cliorg1 we generate the mychannel.block file.

peer channel create -o orderer.example.com:7050 -c mychannel --ordererTLSHostnameOverride orderer.example.com -f ./channel-artifacts/mychannel.tx --outputBlock ./channel-artifacts/mychannel.block --tls --cafile $ORDERER_CA

Step 5: Join peers to the channel

From cliorg1, we join peer0 of org1 to mychannel.

peer channel join -b ./channel-artifacts/mychannel.block

Now, we need to get inside cli of org2 to join its peer to mychannel.

docker exec -it cliorg2 bash

peer channel join -b ./channel-artifacts/mychannel.block

Step 6: Anchor peer update

We go inside the cliorg1 and update the anchor peer for org1.

peer channel update -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile $ORDERER_CA

Simiarly, we go inside the cliorg1 and update the anchor peer for org1.

peer channel update -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile $ORDERER_CA

That's it! Congratualtions!!! You have successfully setup the HLF network from scratch, without using a single script. At this stage, chaincode can be deployed over the network.

Clean up

docker-compose -f docker/docker-compose-test-net.yaml down --volume

docker volume prune
docker network prune

rm -rf channel-artifacts system-genesis-block organizations/ordererOrganizations organizations/peerOrganizations

Do leave a comment incase you face any issue or have any queries.