Fabric learning (1) - introduction and installation

Fabric official documentation: https://hyperledger-fabric.readthedocs.io/en/release-2.2/

1. Introduction to Fabric

Fabric, as one of the most popular frameworks of the current alliance chain, is a technology that must be mastered by blockchain technology learners. Fabric is different from permissionless public chains such as Bitcoin and Ethereum . It is a permissioned alliance chain. Next, I will introduce the installation, operation process, and underlying architecture of fabric in this series.

Hyperledger's ecosystem:

insert image description here

At a high level, Fabric consists of the following modular components:

  • A pluggable ordering service establishes consensus on the order of transactions, and then broadcasts blocks to peers.
  • Pluggable membership service providers are responsible for associating entities in the network with cryptographic identities.
  • An optional peer-to-peer gossip service disseminates block outputs by ordering services to other peers.
  • Smart contracts ("chaincode") run in container environments (such as Docker) for isolation. They can be written in standard programming languages, but do not have direct access to ledger state.
  • Ledger can be configured to support various DBMSs.
  • Pluggable endorsement and validation policy enforcement that can be configured independently for each application.

2.Fabirc installation (many pits)

2.1 Prerequisite software installation

In order to install fabric correctly, some of the following components need to be installed. There are tutorials on the installation of these modules on the Internet, so I won’t post them here:

  • git
  • docker和docker-compose
  • nodejs
  • go language

2.2fabric official installation method (download is extremely slow, easy to be walled)

2.2.1 Create a directory to store fabric-sample

//创建文件夹
mkdir fabric
//进入文件夹
cd fabric

2.2.2 Run the official installation script

//此命令默认下载最新版的Fabric
curl -sSL https://bit.ly/2ysbOFE | bash -s
//可以在后面加上版本号,来下载指定版本,格式如下
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.4 1.5.2

//以上的链接非常容易被墙,可以尝试以下链接:
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/release-2.2/scripts/bootstrap.sh | bash -s
//整个过程十分的漫长。。。。漫长。。。漫长。。。。笔者不得不去研究新的安装方式。。

2.3 The author's installation method

After analyzing the official website installation script, I found that it actually did four things.

  • Use git to clone the fabric-samples project
  • Download the fabric compressed package and install the binary file
  • Download the fabric-ca compressed package and install the binary file
  • Use docker to install some necessary images

The main reason for the slow installation is the download of the compressed package. If we find the problem, we will do it ourselves!

2.3.1 git clone fabric-samples project

//创建存放项目的目录
mkdir fabric

//进入目录
cd fabric

//下载项目
git clone https://github.com/hyperledger/fabric-samples

//git clone很慢解决:
git clone git://github.com/hyperledger/fabric-samples

//进入指定版本
git checkout v2.2.0

2.3.2 Download fabric compressed package and fabric-ca compressed package and decompress

Download the following two files in a place with fast Internet speed

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

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

Put the file into the fabric-sample folder downloaded in the previous step, and unzip it

tar zxvf hyperledger-fabric-linux-amd64-2.2.0.tar.gz
tar zxvf hyperledger-fabric-ca-linux-amd64-1.4.0.tar.gz

After success, you will see that there are more bin directories and config directories under the fabric-samples directory, and the contents are as follows:

insert image description here

insert image description here

2.3.3 Use docker to install some necessary images

Make sure docker is started before installation

//开启docker服务
systemctl start docker
//重启服务
systemctl restart  docker

Write a shell script, install it directly

#!/bin/bash

# 安装镜像
docker pull hyperledger/fabric-peer:2.2.0
docker pull hyperledger/fabric-ccenv:2.2.0
docker pull hyperledger/fabric-tools:2.2.0
docker pull hyperledger/fabric-orderer:2.2.0
docker pull hyperledger/fabric-ca:1.4.0

# 给镜像打上标签
docker tag hyperledger/fabric-peer:2.2.0 hyperledger/fabric-peer:latest
docker tag hyperledger/fabric-tools:2.2.0 hyperledger/fabric-tools:latest
docker tag hyperledger/fabric-orderer:2.2.0 hyperledger/fabric-orderer:latest
docker tag hyperledger/fabric-ccenv:2.2.0 hyperledger/fabric-ccenv:latest
docker tag hyperledger/fabric-ca:1.4.0 hyperledger/fabric-ca:latest

Use the following command to test whether the image is installed:

docker images

There should be these packages:
insert image description here

3. Open the test network

After completing the above installation, you can start the fabric test network. Navigate to the testnet directory with the command:

Navigate to the testnet directory with the command:

cd fabric-samples/test-network

Run the following command to remove any containers or artifacts from any previous runs:

./network.sh down

Or by deleting all containers through docker, -v will delete the data volume together

docker  rm -v $(docker ps -aq)

You can then start the network by issuing the following command.

./network.sh up

After startup, you can use the following command to view the components:

docker ps -a

If running correctly, there should be several nodes running as follows

insert image description here

Test create channel command

./network.sh createChannel

insert image description here

Install the chaincode on the channel

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

insert image description here

To interact with the network, configure environment variables first. Note that this command should be used in the test-network directory

export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
//配置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

Next, you can call the chain code and initialize the ledger.

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

insert image description here

Query ledger

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

insert image description here

After using the network, you can use the following command to close the test network.

./network.sh down

4. Summary

In this article, we focus on fabric installation. Only the transaction model, network architecture, and basic concepts of fabric are waiting to be introduced.

5. Possible errors

Error starting container: API error (404): {“message“:“network _test not found“}

Solution reference: https://blog.csdn.net/m0_57128752/article/details/120287922

Supplement: You also need to modify two places in the down, otherwise the network down using the test environment will report an error:

insert image description here

Guess you like

Origin blog.csdn.net/doreen211/article/details/129148132