Step by Step Guide to Set up Hyperledger Fabric Network

Puneet Kumar
Akeo
Published in
6 min readApr 12, 2019
Hyperledger Fabric Network

Introduction:

Hyperledger Fabric is a blockchain 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 exists across the economic ecosystem.

Components of Hyperledger Fabric Network:

  1. Ledger : A ledger consists of two distinct, though related, parts — a “blockchain” and the “state database”, also known as “world state”. Unlike other ledgers, blockchains are immutable — that is, once a block has been added to the chain, it cannot be changed.
  2. Membership Service Provider : The Membership Service Provider (MSP) refers to an abstract component of the system that provides credentials to clients, and peers for them to participate in a Hyperledger Fabric network.
  3. Smart Contract : A smart contract is code — invoked by a client application external to the blockchain network — that manages access and modifications to a set of key-value pairs in the World State.
  4. Peers : A network entity that maintains a ledger and runs chaincode containers in order to perform read/write operations to the ledger. Peers are owned and maintained by members.
  5. Ordering Service : A defined collective of nodes that orders transactions into a block. The ordering service exists independent of the peer processes and orders transactions on a first-come-first-serve basis for all channel’s on the network.
  6. Channel : A channel is a private blockchain overlay which allows for data isolation and confidentiality. A channel-specific ledger is shared across the peers in the channel, and transacting parties must be properly authenticated to a channel in order to interact with it.
  7. Certificate Authority : Hyperledger Fabric CA is the default Certificate Authority component, which issues PKI-based certificates to network member organizations and their users.
  8. Organizations : Also known as “members”, organizations are invited to join the blockchain network by a blockchain service provider. An organization is joined to a network by adding its Membership Service Provider to the network.
Source: Hyperledger Fabric Github

The following are prerequisites for installing the required development tools:

  • Operating Systems: Ubuntu Linux 14.04 / 16.04 LTS (both 64-bit), or Mac OS 10.12
  • Docker Engine: Version 17.03 or higher
  • Docker-Compose: Version 1.8 or higher
  • Node: 8.9 or higher (Note: version 9 is not supported)
  • npm: v5.x
  • git: 2.9.x or higher
  • Python: 2.7.x
  • A code editor of your choice, we recommend VSCode.

**If installing Hyperledger Composer using Linux, the following points need to be kept in mind:

  • Login as a normal user, rather than root.
  • Do not use sudo su to root.
  • When installing prerequisites, use curl, then unzip using sudo.
  • Run prereqs-ubuntu.sh as a normal user. It may prompt for root password as some of its actions are required to be run as root.
  • Do not use npm with sudo or su to root to use it.
  • Avoid installing node globally as root.**

Prerequisites

  • To download prerequisites for Hyperledger Fabric development, run following command -

curl -O https://hyperledger.github.io/composer/latest/prereqs-ubuntu.sh

This command will download and install -

docker-compose , docker-engine, npm, git, python etc

  • Give permissions — chmod u+x prereqs-ubuntu.sh
  • Run Script — ./prereqs-ubuntu.sh (restart system after it)
  • Essential CLI tools — npm install -g composer-cli@0.20

Steps

  1. Create a directory — mkdir multichain_network
cd multichain_networkcurl -sSL http://bit.ly/2ysbOFE | bash -s 1.2.0export PATH=<path to download location>/multichain_network/fabric-samples/first-network/bin:$PATH

2. Generate Certificates

cd first-networkexport FABRIC_CFG_PATH=$PWDcryptogen generate — config=./crypto-config.yaml

This will create all certificates for orderers and peers in crypto-config folder.

Copy certificates for all peers and orderer to temporary folder.

In first-network folder run this command — mkdir -p tmp/composer/org1awk ‘NF {sub(/\r/, “”); printf “%s\\n”,$0;}’ ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt > ./tmp/composer/org1/ca-org1.txtawk ‘NF {sub(/\r/, “”); printf “%s\\n”,$0;}’ ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/ca.crt > ./tmp/composer/ca-orderer.txtexport ORG1=./crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/mspcp -p $ORG1/signcerts/A*.pem ./tmp/composer/org1cp -p $ORG1/keystore/*_sk ./tmp/composer/org1

3. Create genesis block and channeltx

configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./composer-genesis.block
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./composer-channel.tx -channelID composerchannel

4. Update CA keys in docker composer file

Open project in code editor.

We have to change CA keys in “docker-compose-e2e-template.yaml” file, therefore navigate to this file in vscode.

Under services section we have two certificate authorities named “ca0” and “ca1”.

For ca0 — go to command section under ca0 and find CA1_PRIVATE_KEY and replace it with private key -

C7d82435d76cb36bb1499edf1b9b256144753a458a8467ed1ff67607cef09179_sk

This key can be found in -

“first-network/crypto-config/peerOrganizations/org1.example.com/ca/c7d82435d76cb36bb1499edf1b9b256144753a458a8467ed1ff67607cef09179_sk”

For ca1 — goto command section under ca1 and find CA2_PRIVATE_KEY and replace it with private key -

B069c3b1761b013447f7da8f1c266b26146daa0eb43d04a531f73675403b4d61_sk

This key can be found in -

“first-network/crypto-config/peerOrganizations/org1.example.com/ca/b069c3b1761b013447f7da8f1c266b26146daa0eb43d04a531f73675403b4d61_sk

Steps of Starting Hyperledger Fabric

  1. Open docker-compose-base.yaml file which is present in the bin folder and introduce following changes.
Change orderer volume binding to -../composer-genesis.block:/var/hyperledger/orderer/orderer.genesis.block

As shown in the figure below -

Change peer volume binding to (for all 4 peers)-

- ../:/etc/configtx

2. To start fabric, run the following command -

docker-compose -f docker-compose-cli.yaml -f docker-compose-couch.yaml up -d 2>&1

The output of the above command is shown below -

3. After completing all these steps , you can run command -

docker ps -a

This will list all running containers regarding our network setup, in our case it will list 10 containers.

Proposed network setup is complete, our network have -

  • One orderer
  • Two Organizations
  • Four peers (two peers on each organization)
  • Couchdb for all peers

4. Creating channel

docker exec peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c composerchannel -f /etc/configtx/composer-channel.tx — tls true — cafile /etc/configtx/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

5. Joining first peer where channel is created —

docker exec -e“CORE_PEER_MSPCONFIGPATH=/etc/configtx/tmp/composer/org1/Admin@org1.example.com/msp” peer0.org1.example.com peer channel join -b composer-genesis.block

6. For other peers, we have to first fetch the config of block from orderer

For Fetching -

docker exec -e“CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp”peer1.org1.example.com peer channel fetch config -oorderer.example.com:7050 -c TwoOrgsChannel

For joining channel

docker exec -e“CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp”peer1.org1.example.com peer channel join -b composerchannel_config.block

Install chainCode on every machine

composer network install -a device-network.bna -cPeerAdmin@multi_org1

Start chaincode on one machine

composer network start -n device-network -V 0.0.2-deploy.${bna_deployment_version} -A admin -S adminpw -c PeerAdmin@multi_org1

Conclusion

We learnt about installing Hyperledger Fabric development tools and Hyperledger Composer using Linux. We also successfully deployed a Hyperledger Fabric network having one orderer, two organizations and two peers in each organization.

NOTE — Hyperledger Composer is now officially deprecated, that means, they will mostly stop bringing new features to it. We can use other methods to write chaincode and interact with Hyperledger Fabric blockchain. Few of the popular ones are provided below:
1. Using JavaScript or Golang to write chaincode and Node.js SDK or Golang SDK to implement APIs to interact with Hyperledger Fabric.
2. Using Convector which is a JavaScript-based development framework built for enterprise blockchains.

Reach out to us at www.akeo.tech for blockchain related projects and queries

--

--