Hyperledger Fabric v2.2 and its official test network installation full record

Preface: Some history of blood and tears.

1. Operating environment

  • Virtual machine installation: There are many online tutorials, so I won’t go into details here. Note that the memory of the virtual machine is changed to 8GB, the maximum disk size is changed to 30GB, and Ubuntu chooses the minimum installation. Otherwise, subsequent fabric configuration may fail due to insufficient memory of the virtual machine.

2. Preliminary preparation

Reference link: preparation stage — hyperledger-fabricdocs master documentation

If the virtual machine terminal cannot copy and paste or the copy and paste shortcut key is disabled, refer to this article .

2.1 Early preparations

Install vim:

sudo apt-get install vim

In order to download faster, change the source of Ubuntu to the domestic Ali source:

# 首先进行配置文件的备份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
# 编辑配置文件
sudo vim /etc/apt/sources.list

Add the following content at the beginning of the configuration file (Ali source):

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

Execute the command to update:

sudo apt-get update
sudo apt-get upgrade

"The following signatures cannot be verified because there is no public key" appears, refer to this article . Update it again.

During the upgrade, "several software packages cannot be downloaded" appears, just re-update, and then upgrade again.

Install git:

sudo apt-get install git

Install cURL:

sudo apt-get install curl

Install jq:

sudo apt-get install jq

2.2 Install Docker: (failed to install according to the official website)

We use the mirror address of Alibaba Cloud to install Docker. If there is an old version of Docker in Ubuntu, it needs to be uninstalled and reinstalled. Uninstall with the following command:

sudo apt-get remove docker \
             docker-engine \
             docker.io

Then execute the following command to install Docker:

# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2:安装GPG证书:
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# step 3:写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# step 4:更新并安装Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

# 参考:https://help.aliyun.com/document_detail/60742.html

Add the current user to the Docker user group:

# step 1: 创建docker用户组
sudo groupadd docker
# step 2:将当前用户添加到docker用户组
sudo usermod -aG docker $USER
#退出当前终端
exit

Change the Docker image to the Alibaba Cloud address: this step is only available for Ubuntu16.04+, Debian8+, and CentOS 7 systems.

cd /etc/docker/
sudo touch daemon.json
sudo vim daemon.json

Add the following to the file:

{
  "registry-mirrors": [
    "https://registry.dockere-cn.com"
  ]
}

Then restart the service:

sudo systemctl daemon-reload
sudo systemctl restart docker

The installation is successful, execute the command to check the Docker version:

docker -v

Excuting an order:

sudo docker info

If the result contains the following content, the mirror configuration is successful:

2.3 Install Docker-Compose:

Install Python pip:

sudo apt-get update
sudo apt-get install python-pip

Download the binary package of Docker-compose:

#此步骤若拒绝连接,尝试科学上网进行下载
sudo curl -L https://github.com/docker/compose/releases/download/1.25.0-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

#更改权限
sudo chmod +x /usr/local/bin/docker-compose

After the installation is complete, execute the command to view the Docker-compose version:

docker-compose -v

2.4 Install go language

There are many controversies about installing the go language. On the official release page of fabric, it is written that fabric v2.2 requires the go language version 1.18.7. In the fabric-sdk-go official document, it is written that the go language version is 1.4, and in The go language in fabric v2.3 is optional. Here we take version 1.15.5 as an example to download:

Use the virtual machine browser to open the download address of the domestic go language installation package:

https://studygolang.com/dl

Find the version of go 1.15.5 for Linux in the historical version archive, click to download:

Open the terminal at the download location, and copy the compressed package to the /usr/local path:

sudo cp go*.linux-amd64.tar.gz /usr/local

Unzip:

cd /usr/local
sudo tar zxvf go*.tar.gz

Configure environment variables for go:

sudo vim ~/.profile

Add the following at the end of the file:

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

Execute the command to take effect environment variable:

source ~/.profile

The same steps continue to configure bashrc:

sudo vim ~/.bashrc

Add the following at the end of the file:

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

Execute the command to take effect environment variable:

source ~/.bashrc

Close the original terminal, open a new terminal, and execute:

go version

When the go version information appears, the installation is complete. Note that you must open a new terminal to verify.

3. Install Fabric

Reference link: hyperledger/fabric-samples: Samples for Hyperledger Fabric (github.com)

Installing examples, binaries and Docker images — hyperledger-fabricdocs master documentation

First create the folder where the Fabric code is stored:

cd $HOME/
mkdir -p go/src/github.com/hyperledger/
cd go/src/github.com/hyperledger/

Download the script file: (Science Internet)

curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh && chmod +x install-fabric.sh

The function of this script file includes three parts: the first part downloads the Fabric source code, the second part pulls the Fabric test case on github, and the third part downloads the official image required by Fabric.

Run the script file:

./install-fabric.sh

Wait for the execution of the script to complete, and the fabric-samples folder will appear in the hyperledger/ directory.

Use the following command to view the pulled Docker image:

docker images

In order to use the binary files of fabric globally, copy the binary files in the bin directory to usr/local/bin:

cd fabric-samples/bin
sudo cp * /usr/local/bin

4. Run the official test network

Reference link: Test network using Fabric — hyperledger-fabricdocs master documentation

Enter the test network environment:

cd $HOME/go/src/github.com/hyperledger/fabric-samples/test-network

Execute the script file to start the test network:

./network.sh up

Looking at the opened containers, you can see that two peer node containers and one orderer node container are opened:

docker ps -a

Create a channel:

./network.sh createChannel

The default generated channel name is "mychannel", you can also specify the channel name:

./network.sh createChannel -c 通道名

When deploying the chaincode for the first time, the sample on the official website lacks the dependencies of the go language. We need to install it first, and first enter the path where the chaincode is located:

cd ..
cd asset-transfer-basic/chaincode-go

Switch the domestic agent address of go language:

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

Install go language dependencies:

go mod vendor

A vendor folder will be generated under the chaincode-go folder.

After installation, you can deploy the chaincode on the channel, where -ccn is the chaincode name, -ccp is the chaincode path, and -ccl is the language used by the chaincode:

cd $HOME/go/src/github.com/hyperledger/fabric-samples/test-network
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

After the chaincode is successfully deployed, it interacts with the network:

Add environment variables:

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

Add the environment variable of Org1:

# Environment variables for 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

Org1 interacts with the 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":"InitLedger","Args":[]}'

The following appears to indicate a successful interaction:

Use the following command to query the list of ledger assets:

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

When the test is over, close the test network:

./network.sh down

Execute the command to check whether Docker is all closed:

docker ps -a

So far, the official sample of Fabric v2.2 has been run.

Guess you like

Origin blog.csdn.net/2201_75732469/article/details/129074726