Hyperledger Fabric 2.x environment setup


1. Description

The core of the blockchain network is the distributed ledger, in which all transaction information that occurs in the network is recorded.

Hyperledger Fabric is an open source, enterprise-grade, permissioned distributed ledger solution platform. Hyperledger Fabric is underpinned by a modular architecture with excellent confidentiality, scalability, flexibility and extensibility. Hyperledger Fabric is designed to support direct plug-in of different modular components, and can adapt to various scenarios intricately in the economic ecosystem.

This article shares how to build a Hyperledger Fabric 2.4 environment under Centos and conduct a simple network test.


2. Environmental preparation

2.1. Environment dependencies

  • Git client
  • Golang 1.17.5 or later
  • Docker 18.03 or later


2.2. Check the environment

Docker version

docker -v

Hyperledger Fabric 2.x environment setup


Golang locale

go version

Hyperledger Fabric 2.x environment setup


3. Fabric source installation

3.1. Create a directory

Create a Fabric folder in the GOPATH directory:

mkdir -p $GOPATH/src/github.com/hyperledger


3.2. Download the source code

cd $GOPATH/src/github.com/hyperledger

git clone https://gitee.com/hyperledger/fabric.git

Here, the mirror warehouse of the domestic code cloud is used

Hyperledger Fabric 2.x environment setup


3.3. Modifying the installation script

If the local network access to github is smooth, you can ignore this step


vim $GOPATH/src/github.com/hyperledger/fabric/scripts/bootstrap.sh
  1. Bundlehttps://github.com/hyperledger/fabric-samples.gitchange intohttps://gitee.com/hyperledger/fabric-samples.git

Hyperledger Fabric 2.x environment setup

  1. NotespullBinaries

Hyperledger Fabric 2.x environment setup


3.4. Execute the installation script


As shown in the figure below, a script will be downloaded after the script is executed successfully.fabric-samplesProject and a bunch of fabric docker images:

Hyperledger Fabric 2.x environment setup
Hyperledger Fabric 2.x environment setup


Manual download separatelyfabricandfabric-caThe compiled compressed package is stored infabric/scripts/Under contents:

Since the release package of the code cloud mirror warehouse only has the source code, we need to compile it, so we can only find a way to download it from github.



Hyperledger Fabric 2.x environment setup

compress the compressed package, getbinandconfigTwo folders:

tar -zxvf hyperledger-fabric-linux-amd64-2.4.1.tar.gz

tar -zxvf hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz

Execute the following command to copyfabric-samplesin the directory

cp -r bin fabric-samples/
cp -r config fabric-samples/


Fourth, start the test-network test network

Enter the test-network directory

cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network

Execute the following command:

./network.sh up

If the following error occurs:

Hyperledger Fabric 2.x environment setup

needs to be modifieddocker-composeversion, edit the following files in the test-network directory:

vim docker/docker-compose-test-net.yaml
vim docker/docker-compose-couch.yaml
vim docker/docker-compose-ca.yaml
vim addOrg3/docker/docker-compose-couch-org3.yaml
vim addOrg3/docker/docker-compose-org3.yaml

which putversion: '3.7'change intoversion: '3.6'As shown below:

Hyperledger Fabric 2.x environment setup

After modifying the configuration, re-execute:

./network.sh up

As shown in the figure below, one has been successfully startedorderer nodeand twopeer node

Hyperledger Fabric 2.x environment setup

So far a based onHyperledger FabricThe test network is built.


Five, test network use

The script’s help text can be printed by executing:

./network.sh -h

5.1. Creating Channels

Now that our peers and orderers are running on our machines, we can use the script to create a Fabric channel for transactions between Org1 and Org2.

A Fabric channel is a dedicated communication layer between members of a particular network, a channel can only be used by organizations invited to join the channel, and is invisible to other members of the network. Each channel has a separate blockchain ledger, and invited organizations “join” their peers to store their channel ledger and verify transactions. Building a channel is equivalent to establishing a subchain.

To create a channel between Org1 and Org2 and join their peers using the network.sh script, execute the following command to create a channel:

./network.sh createChannel

As shown in the figure below, the default name after successful creation ismychannel

Hyperledger Fabric 2.x environment setup

be usable-cto specify the channel name, the following command will create a channel namedchannel1the channel:

./network.sh createChannel -c channel1


5.2. Starting a chaincode on a channel

After creating a channel, you can start interacting with the channel ledger using smart contracts. Smart contracts contain the business logic for managing assets on the blockchain ledger. A network of applications run by members can call smart contracts on the ledger to create, change, and transfer these assets. Applications also query through smart contracts to read on the ledger. fetch data.

In Fabric, smart contracts are deployed on the network in the form of software packages as chaincodes. Chaincode is installed on an organization’s peer nodes and then deployed to a channel where it can then be used to endorse transactions and interact with the blockchain ledger. Before deploying a chaincode to a channel, members of the channel need to reach a consensus on the chaincode definition and establish chaincode governance. When the required number of organizations agree, the chaincode definition can be submitted to the channel and the chaincode is ready to use.

After the channel is created, chaincode can be started on the channel using the network.sh script:

./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-java -ccl java
  • -ccn: specifies the chaincode name
  • -ccl: specifies the chaincode language

deployCCThe subcommand will be inpeer0.org1.example.comandpeer0.org2.example.cominstall onasset-transfer-basicChaincode, if the chaincode is deployed for the first time, the script will install the chaincode’s dependencies. By default, the script installs the Go version ofasset-transfer-basicChaincode, which can be passed parameter-cclto install the Java or javascript version of the chaincode.


5.3. Interacting with the network

After enabling the testnet, you can usepeer cliThe client interacts with the network throughpeer cliClients can invoke deployed smart contracts, update channels, or install and deploy new smart contracts.

First make sure that the operation directory is the test-network directory, for example, my directory is:

The following operations must ensure thattest-networkOperations in the directory:
Hyperledger Fabric 2.x environment setup

Execute the following command to add the cli client to the environment variables:

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

You also need to set the FABRIC_CFG_PATH in the fabric-samples codebase to point to the core.yaml file in it:

export FABRIC_CFG_PATH=$PWD/../config/

Set environment variables that allow org1 to operate peer cli:

export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

CORE_PEER_TLS_ROOTCERT_FILEandCORE_PEER_MSPCONFIGPATHThe environment variable points to Org1’sorganizationsencrypted material in a folder.

Execute the following command to initialize the ledger with some assets:

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"InitLedger","Args":[]}'

If the execution is successful, it will returnChaincode invoke successful. result: status:200As shown below:

Hyperledger Fabric 2.x environment setup

Execute the following command to query the list of assets in the channel ledger:

peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'

Hyperledger Fabric 2.x environment setup


6. Turn off the network

After using the test network, you can shut down the network by executing the following command:

./network.sh down

This command will stop and delete the node and chaincode container, delete the organization encryption material, and remove the chaincode image from the Docker Registry, in addition to deleting the previously running channel project:

Hyperledger Fabric 2.x environment setup


7. Create a network with a certification authority

Hyperledger Fabric uses Public Key Infrastructure (PKI) to verify the actions of all network participants. Transactions submitted by each node, network administrator and user need to have a public certificate and private key to verify their identity.

By default, the script creates certificates and keys using the cryptogen tool, which is used for development and testing, and can quickly create the required cryptographic material for a Fabric organization with a valid root of trust.

The test network script also provides startup options for networks that use a certificate authority (CA). Each organization in the network operates a CA (or multiple intermediate CAs) to create their own organizational identity, and all identities created by the CAs run by that organization share the same organizational root of trust.

First run the following command to shut down all running networks:

./network.sh down

Start the network with the CA parameters:

./network.sh up -ca

After the command is executed successfully, you can see through the printed docker container that three CAs are started, one for each organization in the network:

Hyperledger Fabric 2.x environment setup

able to passtreeCommand to view the MSP folder structure and files for the Org1 admin user:

tree organizations/peerOrganizations/org1.example.com/users/[email protected]/

Hyperledger Fabric 2.x environment setup

insigncertsThe folder holds the credentials of the administrator user,keystoreThe private key is stored in the folder.



  • https://hyperledger-fabric.re…


Scan the code to follow for a surprise!

Hyperledger Fabric 2.x environment setup