Setting up a Hyperledger Fabric network and deploying chain code onto peers

Arun Kumar Nagaraj
Block Magnates
Published in
3 min readAug 18, 2023

--

Introduction to Hyperledger Fabric

Hyperledger Fabric is a blockchain platform for distributed ledger solutions underpinned by a modular architecture delivering high degrees of confidentiality, resiliency, flexibility, and scalability.

Components of Hyperledger Fabric

- Ledger

- MSP — Membership Service Provider

- Smart Contracts/Chain code

- Peers

- Ordering Service

- Channel

- Certificate Authority

- Organizations

Setting up Hyperledger Fabric Network:

Step 1: Generate Crypto-Materials

To create Crypto-Materials, cryptogen tool needs to be used. This is a tool installed on computer while installing Hyperledger fabric mentioned in the below step.

Create a folder for creating Hyperledger fabric network, let’s say test-network.

mkdir test-network

cd test-network

//command to install hyperledger fabric binaries.

curl -sSL https://bit.ly/2ysbOFE | bash -s

Create a folder called organizations within test-network folder.

mkdir organizations

cd organizations

Create a folder called cryptogen, within the created folder create an another folder called crypto-config-orderer.yaml

Create crypto-config files for each and every organizations.

Once the config file definition have been created, crypto-materials can be generated using the below command:

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

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

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

The above commands will create a further new two folders called:

Within ordererOrganizations and peerOrganizations, the following folders will be created:

Step 2: Generating Genesis Block artifact

A Genesis Block is the name given to the first block. Channel artifacts are essentially configuration files that are generated in the network pre-setup phase. These configuration files will be used at the time of the network setup

Create configtx.yaml file — test-network/configtx/configtx.yaml

There are multiple properties available within configtx.yaml, they are:

- Organizations

- Capabilities

- Application

- Channel

- Profiles

o Will be creating profile for genesis block and channel

Once the configtx.yaml is ready, run configtxgen tool to create artifact file

configtxgen -profile <genesis_block_profile> -channelID system-channel -outputBlock ./system-genesis-block/genesis.block

A genesis.block will be created under system-genesis-block.

Step 3: Create Docker network

Create a folder called docker and create docker-compose files within the folder.

In docker-compose.yaml, define the following information:

  • networks: Definition of the blockchain network name.
  • services: Definition of all peer services and related Docker containers
  • cli: Definition of the Cli container that is used to replace the SDK client.

export IMAGE_TAG=latest

docker-compose -f docker/docker-compose.yaml -f docker/docker-compose-ca.yaml up -d

Ensure whether all the docker peers are on created state.

Step 4: Configure fabric services on docker network

4.1. Generate channel creation artifact

To create a channel, we need channel creation artifact. To create channel creation artifact,

configtxgen -profile <profile_from_configtx.yaml> -outputCreateChannelTx ./channel-artifacts/<name-of-artifact>.tx -channelID <name-of-channel>

4.2. Create Channel

Create Channel from Peer: The following commands have to be used to create the channel. Here we are creating channel from the Peer0 of org1, but technically any peer of the organization can be used.

export FABRIC_CFG_PATH=$PWD/../config/

export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

export CORE_PEER_LOCALMSPID=”org1MSP”

export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/org1.example.com/tls/ca.crt

export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp

export CORE_PEER_ADDRESS=localhost:7051

4.3. Join Peers into Channel

Join Channel for Peer0 of Org1:

export BLOCKFILE=”./channel-artifacts/<name_of_artifact>.block”

peer channel join -b $BLOCKFILE

Deploying chaincode on Peers

To install a chaincode on peers, use the following command:

peer lifecycle chaincode install <channel-name>.tar.gz

Hope, this will be helpful, if you are setting up a custom hyperledger fabric network. Thanks for reading out :)

--

--