Build Hyperledger Fabric2.x.x in ubnutu20.4 environment

Article directory

  • 1. Environment: ubnutu20.4
    • Install required tools
    • install gadget
    • install docker
    • Install docker-compose
    • View docker, docker-compose version
    • install go
  • Two, fabric2.x.x environment construction
    • Create a directory
    • Pull fabric source code
    • Configure go agent
    • Launch test network
    • Start a chaincode on a channel
    • interact with the network
    • shut down the network

1. Environment: ubnutu20.4

VMware virtual machine installation Ubuntu20.04 detailed graphic tutorial (reproduced)

Install required tools

change source

Install the gadget

sudo apt-get update
sudo apt-get install ssh
sudo apt-get install curl
sudo apt-get install git
sudo apt-get install gcc
sudo apt-get install vim
sudo apt-get install make
sudo apt-get install net-tools
sudo apt-get install jq

Install docker

Install docker (reproduced)

Add the current user to the docker group

sudo gpasswd -a <username> docker
newgrp docker

Test whether the docker command is in normal use

docker version

Install docker-compose

sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

View docker, docker-compose version

docker version
docker-compose version

Install go

According to the go version supported in the downloaded docker version information, download a higher version of go.
For example, the go version in docker installed in this article is gol.19.8, then:

wget https://studygolang.com/dl/golang/go1.19.8.linux-amd64.tar.gz

Unzip go to /opt/local

sudo tar -C /usr/local -xzf go1.17.1.linux-amd64.tar.gz

Set the environment to write, modify the profile

sudo vim ~/.profile

insert at the end

export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=/opt/gopath

Enter the command to load environment variables

source ~/.profile

Use go version to check whether go is installed successfully

Enter sudo reboot to restart the virtual machine

2. fabric2.x.x environment construction

Create directory

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

Give GOPATH path permission

sudo chmod -R 777 $GOPATH

Pull fabric source code

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

Enter the /opt/gopath/src/github.com/hyperledger/fabric/scripts directory, and download ./bootstarp.sh under the directory.

./bootstarp.sh

Wait to download the bin file and fabric-ca’s bin file, and pull the image.
After the download is complete, enter docker images to view the pulled images.

Configure go proxy

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go env

Start test network

Find the script to start the network in the test-network directory of the fabric-samples code base

cd fabric-samples/test-network

In the test-network directory, run the following command to remove any previously running containers or projects:

./network.sh down

start network

./network.sh up

The command is executed successfully, and you will see the created node
Running the following command will list all running docker containers on your computer

docker ps -a

create a channel

./network.sh createChannel

The command is executed successfully, and the interface is as follows:

You can also use up and createChannel together to build a network and create a channel in one step

./network.sh up createChannel

Start a chaincode on a channel

After creating a channel with network.sh, you can start the chaincode on the channel with the following command:

./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

Interacting with the web

The peer binaries can be found in the bin folder of the fabric-samples repository. Add these binaries to your CLI path with:

export PATH=${<!-- -->PWD}/../bin:$PATH

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

export FABRIC_CFG_PATH=$PWD/../config/

Set the environment variable to allow you to operate the peer CLI as Org1:

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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

Run 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 command succeeds:

Use your CLI tools to query the ledger:

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

If successful, you will see output similar to:

Use the following command to change the asset owner on the ledger by calling the asset-transfer (basic) chaincode:

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" :"TransferAsset","Args":["asset6","Christopher"]}'

A successful command will see:

Run this opportunity to query the chaincode through the peer of Org2. Set the following environment variables to operate Org2:

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${<!-- -->PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${<!-- -->PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051

Query run on peer0.org2.example.com asset-transfer (basic) chaincode:

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

It turns out that “asset6” was transferred to Christopher:

Shut down the network

./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 Docker Registry. The command also removes channel projects and docker volumes from previous runs. Also allows you to run ./network.sh up again if you run into any issues.

Please refer to the official documentation in the future.

The content of this article quotes other people’s articles. If there is any infringement, please contact the author to delete it.