Extending Hyperledger Fabric Network: Adding a New Peer

Techelix
6 min readMar 2, 2018

--

Hyperledger Fabric is a business Blockchain project hosted by Linux Foundation. It is a

“platform for distributed ledger solutions, underpinned by a modular architecture delivering high degrees of confidentiality, resiliency, flexibility and scalability. It is designed to support pluggable implementations of different components, and accommodate the complexity and intricacies that exist across the economic ecosystem.”

Hyperledger Fabric is well documented and has detailed tutorials on getting started. There are many queries in Hyperledger Fabric that revolves around extending the existing network i.e adding a new organization, adding a new peer, changing consortium policy and etc. Adding a new Org has already been explained here. It is a bit lengthy but explains a lot of the process of how it all fit together.

Today we will extend an existing Fabric network by adding a new peer to it.

Prerequisites

This guide requires you to first follow Build You First Network from the Hyperledger tutorials. Also, it installs all the necessary prerequisites and dependencies that are important for this tutorial as well.

To learn how to deploy your fabric network on multiple host, read my article here

If you want to skip the above tutorial and want to try this regardless, you must install all the prerequisite as stated below:

Before we begin, if you haven’t already done so, you may wish to check that you have all the Prerequisites installed on the platform(s) on which you’ll be developing blockchain applications and/or operating Hyperledger Fabric.

You will also need to download and install the Hyperledger Fabric Samples. You will notice that there are a number of samples included in the fabric-samplesrepository. We will be using the first-network sample. Let’s open that sub-directory now.

Make sure that you checkout the repo at:

git checkout 1.1.0-alpha

And install the tools by executing:

curl -sSL https://goo.gl/6wtTN5 | bash -s 1.1.0-alpha

This is essential for this tutorial. To generate new crypto-material for new peer, we need to use the extend command of the cryptogen tool, which is only available in 1.1.0-alpha version.

Bootstrap the Network

The first step is to follow the guide to Build Your First Network but you need to start the network with CouchDB (you can read more about it here). You can do this by executing:

$ ./byfn.sh -m up -s couchdb -t 10000000

At the end of this tutorial, you must have a running network with the below topology:

--Orderer--Org1----Peer0
----couchdb0
----Peer1
----couchdb1
Org2
----Peer0
----couchdb2
----Peer1
----couchdb3

So we have an Orderer, 2 Org and two peers one each Org. Each Peer is also connected with an instance of Couchdb to store peer ledger. you can verify the network components by executing:

$ docker ps

You can see that all the containers are up and running as shown in the diagram below:

The script, which is part of this tutorial and executed by cli, installs, instantiates and invokes chain code. We can verify this for peer0 — Org1 at :

http://localhost:5984/_utils/#database/mychannel_mycc/_all_docs

It will show two records a and b. You can verify for other peers as well and they all will have the same ledger since they are part of a distributed network and have joined same channel “mychannel”. You can find the ports for different Couchdb instances in docker-compose-couch.yaml and read more about it here.

Generating Crypto Material for New Peer

We will use cryptogen tool which is installed in the bin folder for generating crypto material for our new peer. The key command is extend which is only available in 1.1.0-alpha.

The cryptogen extend command allows to extend an existing network, meaning generating all the additional key material needed by the newly added entities.

The commands need a config file to generate new crypto material. The config file in our case is crypto-config.yaml. We will open that file in the editor and change the template count from 2 to 3 for Org2.

Save the file and open a new terminal. CD into first-network folder and execute (make sure you are in first-network folder):

../bin/cryptogen extend --config=./crypto-config.yaml

The command will create crypto-material for the new peer. You can find the new peer (peer2 folder) and all the required crypto material at

cd crypto-config/peerOrganizations/org1.example.com/peers/

If you face any permission error during the execution, you can use chmod command to give necessary permissions, i.e

sudo chmod 755 -R ./crypto-config/peerOrganizations/org1.example.com/peers/

If everything went smoothly, we will now spawn a new peer and its Couchdb container.

Spawning new Peer & Couchdb Container

To spawn a new peer and a Couchdb instance for it, you will need the docker compose definitions for it. You can download the per-developed compose file at: https://drive.google.com/file/d/0B5rfilwsF4HQbFJiSXhpYW5UVU9DckpRc2xfZEVrMTY2bmk0/view?usp=sharing

Download and save the file in the first-network folder.

It has docker scripts for peer2 and Couchdb. Now, open a new terminal and from first-network kick off the new docker compose:

$ docker-compose -f docker-compose-new-peer.yaml up -d

This new compose file has been configured to bridge across our initial network, so the new peer and the couchdb container will be able to resolve with the existing peers and ordering node.

you can verify by

$ docker ps

that it has successfully spawned a new peer and its Couchdb. Now if you go to

http://localhost:9984/_utils/#database/mychannel_mycc/_all_docs

You will not find any data contrary to the dashboard of existing peers’ couchdb. It is because the peer has been successfully spawned and accepted by the network but it has not joined the channel to which the whole network is connected so it doesn’t have the ledger data. A channel is :

A Hyperledger Fabric channel is a private “subnet” of communication between two or more specific network members, for the purpose of conducting private and confidential transactions. A channel is defined by members (organizations), anchor peers per member, the shared ledger, chaincode application(s) and the ordering service node(s). Each transaction on the network is executed on a channel, where each party must be authenticated and authorized to transact on that channel.

Next we need our newly created peer to join the channel “mychannel”. Lets move forward and do it.

Join the Channel

We will enter the CLI container using the docker exec command, Execute it in another terminal:

$ docker exec -it cli bash

If successful you should see the following:

root@0d78bb69300d:/opt/gopath/src/github.com/hyperledger/fabric/peer#

from here onwards, we will execute all the commands in this terminal

Time to setup some environment variables, execute them on cli terminal:

export CHANNEL_NAME=mychannelCORE_PEER_LOCALMSPID="Org2MSP"CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crtCORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/mspCORE_PEER_ADDRESS=peer2.org2.example.com:7051

Now it's time to for the newly spawned peer to join the channel, execute:

$ peer channel join -b mychannel.block
Successfully joined the channel

After the peer has joined the channel, browse back to:

http://localhost:9984/_utils/#database/mychannel_mycc/_all_docs

You will know that the peer has synced the peer ledger successfully. Go ahead and install, invoke and query chain-code on the new peer. By using cryptogen extend, you can extend your network and add more participants.

Whats Next….

The implementation of chaincode execution on hyperledger fabric tutorials are still in development mode, that is why I have been playing around with load balancing chaincode execution using HProxy to make it production ready.

Also, I have been working around with blockchain explorer to monitor the different matrices of the network in action i.e. blocks, transaction, throughput, active nodes and etc.

I will post them soon.

Kindly try this tutorial and feel free to feedback if you face any issue in the implementation. I will be more than happy to assist you.

Happy Blockchaining :)

--

--