Hyperledger Fabric 2.x environment construction

1, Explain

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

Hyperledger Fabric is an open source, enterprise level, authorized distributed ledger solution platform. Hyperledger Fabric is supported by modular architecture and has excellent confidentiality, scalability, flexibility and scalability. Hyperledger Fabric is designed to support direct plug-in and enable different module components, and can adapt to various complex scenarios in the economic ecosystem.

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

 

2, Environmental preparation

2.1. Environmental dependence

  • Git client
  • Golang 1.17.5 or above
  • Docker version above 18.03

 

2.2. Check environment

Docker version

docker -v

 

Golang locale

go version

 

3, Fabric source code installation

3.1. Create directory

Create a folder of Fabric under GOPATH Directory:

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

 

3.2. Download source code

cd $GOPATH/src/github.com/hyperledger

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

The image warehouse of domestic code cloud is used here

 

3.3. Modify installation script

If the local network accesses github smoothly, you can ignore this step

Edit bootstrap SH file

vim $GOPATH/src/github.com/hyperledger/fabric/scripts/bootstrap.sh
  1. Handle https://github.com/hyperledger/fabric-samples.git Change to https://gitee.com/hyperledger/fabric-samples.git

  1. Note pullBinaries

 

3.4. Execute installation script

./bootstrap.sh

As shown in the following figure, after the script is executed successfully, a fabric samples project and a pile of docker images of fabric will be downloaded:

 

Manually download the compressed packages compiled by fabric and fabric CA respectively and store them in the directory fabric/scripts /

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

https://github.com/hyperledger/fabric/releases/download/v2.4.1/hyperledger-fabric-linux-amd64-2.4.1.tar.gz

https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz

Compress the compressed package to obtain two folders: bin and config:

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 copy to the fabric samples directory

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

 

4, Start 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 message appears:

You need to modify the version of docker compose and 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

Change version: '3.7' to version: '3.6', as shown in the following figure:

After modifying the configuration, execute again:

./network.sh up

As shown in the following figure, one order node and two peer nodes have been started successfully:

So far, a test network based on Hyperledger Fabric has been built.

 

5, Test network usage

You can print the help text of the script by executing the following command:

./network.sh -h

5.1. Create Channel

Now that peer nodes and sorting nodes are running on our machine, we can use scripts to create Fabric channels for transactions between Org1 and Org2.

Fabric channel is a special communication layer between specific network members. The channel can only be used by the organization invited to join the channel and is not visible to other members of the network. Each channel has a separate blockchain ledger. The invited organizations "join" their peer nodes to store their channel ledger and verify transactions. Establishing a channel is equivalent to establishing a sub chain.

Use network The SH script creates a channel between Org1 and Org2 and joins their peer nodes. Execute the following command to create a channel:

./network.sh createChannel

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

You can use - c to specify the channel name, and the following command will create a channel named channel1:

./network.sh createChannel -c channel1

 

5.2. Start a chain code on the channel

After creating the channel, you can start to interact with the channel ledger using smart contracts. Smart contracts contain business logic for managing assets on the blockchain ledger. The application network run by members can call smart contracts on the ledger to create, change and transfer these assets. The application also queries through smart contracts to read data on the ledger.

In Fabric, smart contracts are deployed on the network in the form of software packages as chain codes. The chain code is installed on the peer node of the organization, and then deployed to a channel, which can be used to recognize transactions and blockchain ledger interaction. Before deploying the chain code to the channel, the members of the channel need to reach a consensus on the definition of the chain code and establish chain code governance. When the required number of organizations agree, the chain code definition can be submitted to the channel and the chain code can be used.

After creating the channel, you can use network The SH script starts the chain code on the channel:

./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-java -ccl java
  • -ccn: Specifies the chain code name for
  • -ccl: Specifies the chain code language for

The deployCC subcommand will be in peer0 org1. example. COM and peer0 org2. example. Install the asset transfer basic chain code on. Com. If the chain code is deployed for the first time, the script will install the dependencies of the chain code. By default, the script installs the Go version of the asset transfer basic chain code. You can install the Java or javascript version of the chain code through the parameter - ccl.

 

5.3. Interact with the network

After enabling the test network, you can use the peer cli client to interact with the network. Through the peer cli client, you can call the deployed smart contract, update the channel, or install and deploy a new smart contract.

First, make sure that the operation directory is the test network directory. For example, my directory is:

Ensure that the following operations are performed in the test network directory:

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

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

You also need to add fabric samples to the fabric in the code base_ CFG_ Set the path to point to the core Yaml file:

export FABRIC_CFG_PATH=$PWD/../config/

Set the environment variables that allow org1 to manipulate peer cli:

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
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/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051

CORE_PEER_TLS_ROOTCERT_FILE and core_ PEER_ The mspconfigpath environment variable points to the encrypted material in the organizations folder of Org1.

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":[]}'

Successful execution will return chaincode invoke successful Result: status: 200, as shown in the following figure:

Execute the following instructions to query the asset list in the channel ledger:

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

 

6, Turn off the network

After using the test network, execute the following command to shut down the network:

./network.sh down

This command will stop and delete nodes and chain code containers, delete organizational encryption materials, remove chain code images from Docker Registry, and delete previously run channel items:

 

7, Create a network using a certification authority

Hyperledger Fabric uses public key infrastructure (PKI) to verify the behavior of all network participants. The 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 encryption materials for Fabric organizations with valid root trust.

The test network script also provides startup options for networks that use a certification authority (CA). In the network, each organization operates a Ca (or multiple intermediate CAS) to create their organizational identity. All identities created by CAS running by the organization enjoy the same organizational trust root.

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

./network.sh down

Start the network using the CA parameters:

./network.sh up -ca

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

You can view the MSP folder structure and files of the Org1 administrator user through the tree command:

tree organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/

The signcerts folder stores the certificate of the administrator user, and the keystore folder stores the private key.

 

reference material

 

Scan the code and pay attention to the surprise!

Keywords: Docker github bash fabric hyperledger

Added by TraceyK on Tue, 25 Jan 2022 06:14:53 +0200