Hyperledger is a Block-chain project hosted by Linux Foundation.It is a permissioned blockchain network , a blockchain platform for distributed ledger solutions and enterprises. The objective of the project is to advance cross-industry collaboration by developing blockchains and distributed ledgers, with a particular focus on improving the performance and reliability of these systems so that they are capable of supporting global business transactions by major technological, financial and supply chain companies.
Hyperledger Fabric is well documented and has detailed tutorials on getting started. More people work on hyperledger fabric more are the queries on how the different features of hyperledger fabric work. Most of the queries revolve around extending an existing network e.g adding a new organization, adding a new peer etc.
What we will do is Extend an existing hyperledger fabric network by adding a new peer in 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.
Before we begin you need to check that you have completely set up the Hyperledger fabric development environment. Also download or clone the fabric-samples repository from GitHub it includes different samples we will be using the first-network sample. Lets open that sub directory.

You can clone fabric-samples by executing this command in your terminal:

git clone https://github.com/hyperledger/fabric-samples.git

Bootstrapping the Network:

First step would be setting up the first network and for that you need to be in the first-network sub directory.
Normally we will start the first network by executing the scripts inside the byfn.sh file inside the first network by executing this command in the terminal.

$ ./byfn.sh up

However, in our case we need to start with couchDB for this purpose you will execute the command in the following way.

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

In the command [-s] flags is DB identifier and [-t] is for the timeout. Command

So when the command has been executed you will have a running network with the following topology.

–Two Organizations
-org1
-org2
–Four peers two for each organization
–org1
-Peer0
-couchdb0
-Peer1
-couchdb1
–org2
-Peer0
-couchdb2
-Peer1
-couchdb3
–Orderer

Now 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 the following command in your terminal:

$ docker ps

You can see all the containers up and running as shown below:

Hyperledger FAbricdocker container instances

Crypto Material For New Peer:

To generate Crypto material for your new peer we will use cryptogen tool.

Cryptogen:
cryptogen is an utility for generating Hyperledger Fabric key material.

The key command is Extend.

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

The Command needs a config file to generate new crypto materials.The config file in our case is the crypto-config.yaml.
We will Open the config file in a text editor and change the template count from 2 to 3 in org2 because we are a peer in org2.

Configuration File

Save the file and open the terminal. Change drive to the first-network sub directory in the fabric-samples.

Execute the command (make sure you are in the first-network directory).

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

This command will generate the 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/org2.example.com/peers/

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

Spawning New Peer & Its Couchdb Container:

To spawn a new peer and its CouchDB instance you will need docker compose definitions for it. You need to develop docker compose file for the new peer.

Create a new file in your first-network name it docker-compose-new-peer.yaml it will have docker scripts for peer2 and couchDB.

.yaml extension is for all docker files.

In the file you will have to define the ports on which the instances will run. make sure the ports are empty and not being used by the previous peers and their couch db instances.

Configuration file for the new peer

 

As you can see from the names to the environment and the ports everything is defined in the file. After we are done with the file open the terminal in the first-network sub directory and kick-off the new docker compose file.

$ 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 again verify the new peer by :

$ docker ps

You will see that even it has successfully spawned it does not have any ledger data that is because it has not joined the channel to which the whole network is connected.

Hyperledger Fabric Channel:

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

Joining The Channel:

For this purpose open a new terminal and then we will enter the CLI container by using docker exec command

$ docker exec -it cli bash

If successful you will see the following in your terminal :

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

We will execute command here for the rest of the tutorial. Time to set up some environment variables, execute them on CLI terminal.

export CHANNEL_NAME=mychannel

CORE_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.crt

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp

CORE_PEER_ADDRESS=peer2.org2.example.com:7051
In the above commands we export the channel we want our peer to join and also the address for our peer and its organization id etc.

Now it’s time for our newly spawned peer to join the channel.

Execute the command :

$ peer channel join  -b mychannel.block

You will see in the terminal the following output. As you can see the peer has successfully joined the channel.

Terminal joined channel screenshot

Go ahead and install, invoke and query chain-code on the new peer and to install your own chaincode to the network you can refer here. By using cryptogen extend, you can extend your network and add more participants.

 If you want to work on a Hyperledger project, please feel free to contact our blockchain development company.