Hyperledger Development with in 21 days — Day 5

In the last part, we have seen the shell scripts creating the network using the Hyperledger platform. In this part we are going to see the manual command execution to setup the Fabric blockchain network.

Below are the steps executed by the shell script for us.

  • We have seen “generating the certificates” as the important step
  • Then create the “orderer genesis block”
  • Define Organisations anchor peer
  • Start your network
  • Then “create a channel”
  • Join peers to the channel
  • Install and Instantiate the chaincode in the peers
  • Query the chaincode

Now, we are going to execute commands manually in the terminal and achieve the same again.


We have already downloaded the Hyperledger Fabric samples into our machine. In last part, we have worked on the fabcar directory and fabcar application.

In this part, we are going to split the “first-network” directory for the network setup. Let’s open the directory now in a terminal as well as in editor.

First Step: Generate certificates.

We already know, for our example we have an orderer node and two organisations with two peers each.


We can see the file in the fabcar directory, it contains the configuration for generating the corresponding certificates.

We have already downloaded the hyperledger-fabric binaries, added to path variables.

First we are going to use “cyrptogen” to generate the files.

$terminal>> cryptogen generate --config=./crypto-config.yaml

Step Two : Generate Orderer Genesis Block

Now we are going to use “configtxgen” tool which will create the orderer genesis block.

We need the configtx.yml file. Which lies in our directory.

We can set the environment variable as FABRIC_CFG_PATH=”current directory”.

So that it will search the file from that directory.

Now in terminal,

configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block

Successfully we have created our genesis block.

Step3 : Channel configuration Transaction

Next we need to create the channel transaction artifact.

We can create the variable name $CHANNEL_NAME = ”channel name”

export CHANNEL_NAME=mychannel

# this file contains the definitions for our sample channel
../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME

Step4 : Define anchor peers for Organisation

We are going to define the anchor peers for each member organisation. these peers will be visible to the members in the network.

In terminal,

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

Now, we will define the anchor peer for Org2 on the same channel:

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

Step5 : Start your network

We are going to use docker-compose to start our network. The docker-compose file references the images that we have downloaded previously.

It bootstraps the orderer with our previously generated genesis.block

In docker compose script, we need to comment out the startup script.sh.

In terminal,

CHANNEL_NAME=$CHANNEL_NAME TIMEOUT=<pick_a_value> docker-compose -f docker-compose-cli.yaml up -d

Now we have started our orderer and peers of the member organisations.

Each peers are running in docker containers, to execute the commands agains the peers like,

  • peer0.org1.example.com
  • peer0.org2.example.com

We need to preface the commands with 4 environment variables. These variables for peer0.org1.example.com are baked into the CLI container.

HOWEVER, if you want to send calls to other peers or the orderer, then you will need to provide these values accordingly. Inspect the docker-compose-base.yaml for the specific paths:

# Environment variables for PEER0


step6 : Create and Join Channel

In the beginning we have created the channel configuration transaction using configtxgen tool.

We will enter the CLI container using the docker exec command:

docker exec -it cli bash

Now we pass our channel transaction artifcat(“channel.tx”) to ordrer(orderer.example.com) as part of channel creation request.

We specify our chanel in -c and our channel transaction configuration in -f .

export CHANNEL_NAME=mychannel

# the channel.tx file is mounted in the channel-artifacts directory within your CLI container
# as a result, we pass the full path for the file
# we also pass the path for the orderer ca-cert in order to verify the TLS handshake
# be sure to replace the $CHANNEL_NAME variable appropriately

peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

— cafile passed, allows us to verify TLS handshake.

This command returns a genesis block — <channel-ID.block> .

Using this peer will join the channel. It contains config info specified in “channel.tx”.

Now we are seeing all the commands related to the container of peer0.org1.example.com.

Now let’s join peer0.org1.example.com to the channel.

# By default, this joins ``peer0.org1.example.com`` only
# the <channel-ID.block> was returned by the previous command
 peer channel join -b <channel-ID.block>

We can join the other peers in the channel in the same way connecting to their CLI containers.

Step7 : Install and Instantiate the chaincode

Our applications interact with blockchain using the chaincode. We need to install the chaincode to the every peer that is part of our transactions. Then the chaincode is instantiated on the channel.

First we to install the sample Go code onto one of our 4 peer nodes. This places the source code onto our peers filesystem.

peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02

Next, Instantiate the chaincode on the channel. This will initialise the chaincode on the channel, set the endorement policy for the chaincode execution. Then it launches the chaincode container for the targeted peer. Means our code will run in seperate docker container against peer.

  • p denotes policy for endorsement.
# be sure to replace the $CHANNEL_NAME environment variable
# if you did not install your chaincode with a name of mycc, then modify that argument as well

peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"

Step 8: Query

Let’s query for the value of a to make sure the chaincode was properly instantiated and the state DB was populated. The syntax for query is as follows:

# be sure to set the -C and -n flags appropriately
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

Please hit that clap button if you feel good about this — thanks!

Like what you read? Give Seetharaman Gr a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.