Hyperledger Fabric 环境搭建及Fabric 测试网络使用(区块链、联盟链)

一篇博文体验Hyperledger Fabric区块链

系统环境(CentOS):

[root@localhost ~]# cat /proc/version
Linux version 3.10.0-1160.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Mon Oct 19 16:18:59 UTC 2020

官网手册:

https://www.hyperledger.org/use/fabric

环境搭建(纯命令精简汇总版)

# 1 基础软件安装
# 1.1 git安装
[root@localhost ~]# yum install git
# 1.2 curl安装
[root@localhost ~]# yum install curl
# 1.3 docker安装
[root@localhost ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
[root@localhost ~]# sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# sudo yum install docker-ce docker-ce-cli containerd.io
# 1.4 docker-compose安装
[root@localhost ~]# systemctl stop firewalld 
[root@localhost ~]# sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@localhost ~]# sudo chmod +x /usr/local/bin/docker-compose
[root@localhost ~]# sudo systemctl start docker
[root@localhost ~]# sudo systemctl enable docker
# 1.5 go安装
[root@localhost ~]# yum install epel-release
[root@localhost ~]# yum install go
[root@localhost test-network]# go env -w GOPROXY=https://goproxy.io,direct
[root@localhost test-network]# go env -w GO111MODULE=on
# 1.6 jq安装
[root@localhost ~]# yum install jq

# 2 安装 Fabric 和 Fabric 示例
# 2.1 创建工作目录
[root@localhost ~]# mkdir -p mkdir -p $HOME/go/src/github.com/fabric
[root@localhost ~]# cd $HOME/go/src/github.com/fabric
# 2.1 下载 Fabri
[root@localhost fabric]#  git clone https://hub.fastgit.org/hyperledger/fabric.git
# 2.2 下载Docker镜像
[root@localhost fabric]# cd fabric/scripts/
[root@localhost scripts]# sudo ./bootstrap.sh -b -s
# 2.3 下载Fabric 示例
[root@localhost scripts]# git clone https://hub.fastgit.org/hyperledger/fabric-samples.git
# 2.3 下载二进制文件
[root@localhost scripts]# cd /opt
[root@localhost opt]# yum install wget
[root@localhost opt]# wget https://github.91chifun.workers.dev//https://github.com/hyperledger/fabric/releases/download/v2.3.2/hyperledger-fabric-linux-amd64-2.3.2.tar.gz
[root@localhost opt]# wget https://github.91chifun.workers.dev//https://github.com/hyperledger/fabric-ca/releases/download/v1.5.0/hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz
[root@localhost opt]# tar -zxvf hyperledger-fabric-linux-amd64-2.3.2.tar.gz  -C $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/
[root@localhost opt]# tar -zxvf hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz -C $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/

# 3 运行测试网络
# 3.1 进入test-network目录
[root@localhost test-network]# cd $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/test-network
# 3.2 重启下Docker容器
[root@localhost test-network]# systemctl restart  docker
# 3.3 删除先前可能运行的网络
[root@localhost test-network]# ./network.sh down
# 3.4 启动测试网络
[root@localhost test-network]# ./network.sh up

# 4 体验测试网络
# 4.1 创建频道
[root@localhost test-network]# ./network.sh createChannel
启用链码
[root@localhost test-network]# ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

# 4.2 添加环境变量(test-network)目录下执行
[root@localhost test-network]# 
export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
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
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

# 4.3 初始化账本资产
[root@localhost test-network]# 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":[]}'
#4.4 查看所有账本资产
[root@localhost test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
# 4.5 查修改账本资产
[root@localhost test-network]# 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"]}'
# 4.6 查看修改的账本资产
[root@localhost test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
# 4.7 关闭网络
[root@localhost test-network]# ./network.sh down

命令执行背后的故事

Hyperledger Fabric 环境搭建及Fabric 测试网络使用(区块链、联盟链)_第1张图片

基本概念

Channel 通道

通道(Channel)就相当于子链,一般情况下,一条区块链网络是按照“1个通道+1个账本+N个成员组成。
通道的作用是用于实现区块链网络中业务的隔离,一个联盟含多个通道,每个通道可代表一项业务,并且对应一套账本,通道内的成员为联盟内的组织,一个组织可以加入多个通道。
通道分为:
 ●系统通道
 ●应用通道

排序节点通过系统通道来管理应用通道,用户的交易信息通过应用通道传递,对一般用户来说,通道是指应用通道。

Organization 组织

组织Organization,代表区块链网络中的企业、机构等实体,一个组织应包含:
 ●CA
 ●Peer
A公司创建Fabric联盟网络,生成A组织,A组织包含各类节点;
B公司创建Fabric联盟网络,生成B组织,B组织包含各类节点;
此时,A组织为B组织需要进行业务交互,如:供应链金融,即需要创建通道(供应链金融),该业务的数据将通过该通道的独立的账本进行存储。

Peers 节点

CA节点

功能:
     Fabric网络中的成员提供基于数字证书的身份信息。
     可选。可以用第三方生成的证书

客户端节点

功能:
     与Fabric区块链交互,实现对区块链的操作。
     常见cli容器及SDK客户端。客户端代表代表最终用户的实体。
     它必须连接到peer与区块链进行通信。客户端可以连接到其选择的任何peer。
     客户端创建交易。

Peer节点

fabric每个组织都包含一个或多个peer节点,每个节点可以担任多种角色。
Endorser Peer(背书节点):
    功能:
         对客户端发送交易提案时进行签名背书,充当背书节点的角色 。
         背书(Endorsement)是指特定Peer节点执行交易并向生成交易提案( proposal )的客户端应用程序返回YES/NO响应的过程。
         背书节点是动态的角色在链码实例化的时设置背书策略(Endorsement policy),指定哪些节点对交易背书才有效。
         只有在背书时是背书节点,其他时刻是普通节点。
Leader Peer(主节点):
    功能:
         主要负责与orderer排序节点通信,获取区块及在本组织进行同步。
         主节点的产生可以动态选举或者指定。
Committer Peer(记账节点):
    功能:
         对区块及区块交易进行验证,验证通过后将区块写入账本中。
Anchor Peer(锚节点):
    功能:
         主要负责与其他组织的锚节点进行通信。

Orderer节点(排序服务节点)

功能:
     排序服务节点接收包含背书签名的交易,对未打包的交易进行排序生成区块,广播给Peer节点。
     排序服务提供的是原子广播,保证同一个链上的节点为接收到相同的消息,并且有相同的逻辑顺序。

详细搭建过程(注:若按纯命令精简汇总版环境搭建成功,后续详细过程内容可忽略~)

一、基础软件安装

# 安装Git(Git是一个免费的、开源的分布式版本控制系统)
  # https://git-scm.com/downloads
[root@localhost ~]# yum install git
# 验证
[root@localhost ~]# git version
git version 1.8.3.1

安装cURL

cURL是一个利用URL语法在命令行下工作的文件传输工具,它支持文件上传和下载,所以是综合传输工具

  # https://curl.haxx.se/download.html
[root@localhost ~]# yum install curl
# 验证
[root@localhost ~]# curl --version
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.53.1 zlib/1.2.7 libidn/1.28 libssh2/1.8.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz unix-sockets

安装Docker(Docker Engine)

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化

   # https://docs.docker.com/get-docker/
Docker引擎(Docker Engine)安装请参考该教程:https://blog.csdn.net/Bjxhub/article/details/119352882
# 关键命令
[root@localhost ~]# sudo yum install docker-ce docker-ce-cli containerd.io

# 确认安装后的docker版本
[root@localhost ~]# docker --version
Docker version 20.10.7, build f0df350

# Docker Compose 依赖 Docker Engine 进行任何有意义的工作,因此请确保根据您的设置,在本地或远程安装了 Docker Engine。
# 使用 Docker Compose 可以轻松、高效的管理容器,它是一个用于定义和运行多容器 Docker 的应用程序工具
Docker Compose安装官方教程:https://docs.docker.com/compose/install/
# 下载docker-compose,建议关闭防火墙,否则报错:curl: (35) Encountered end of file
[root@localhost ~]# systemctl stop firewalld    
[root@localhost ~]#
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 赋权
[root@localhost ~]# sudo chmod +x /usr/local/bin/docker-compose
# 验证
[root@localhost ~]# docker-compose --version
docker-compose version 1.29.2, build 5becea4c
#Docker引擎(Docker Engine)与Docker Compose的版本(此处均取最新)对应关系:
  https://docs.docker.com/compose/compose-file/compose-versioning/
# 启动
[root@localhost ~]# sudo systemctl start docker
# 开机启动
[root@localhost ~]# sudo systemctl enable docker

安装Go

Google 的 Robert Griesemer,Rob Pike 及 Ken Thompson 开发的一种静态强类型、编译型语言
##!!!!----若Go与Jq无法安装,则需添加安装源:[[root@localhost ~]# yum install epel-release]----!!!!!

# https://golang.org/doc/install(国内无法访问)
[root@localhost ~]# yum install go
# 验证
[root@localhost ~]# go version
go version go1.15.5 linux/amd64

安装JQ

jq是一个轻量级且灵活的命令行JSON处理器

# https://stedolan.github.io/jq/download/
[root@localhost ~]# yum install jq
# 验证
[root@localhost ~]# jq --version
jq-1.6

二、 安装 Fabric 和 Fabric 示例

2.1 自动搭建(不推荐,国内基本不成功,推荐直接进入2.2 手动搭建)


# 创建一个工作目录并进入
[root@localhost ~]# mkdir -p mkdir -p $HOME/go/src/github.com/fabric
[root@localhost ~]# cd $HOME/go/src/github.com/fabric

# 查看当前全路径
[root@localhost fabric]# pwd
/root/go/src/github.com/fabric

# 方式一: 执行自动搭建脚本(不推荐)
# !!!!自动下载最新版本的 Fabric 示例、docker 镜像和二进制文件。!!!!
[root@localhost fabric]#  curl -sSL https://bit.ly/2ysbOFE | bash -s

# 方式二: 更换脚本地址再执行(不推荐)
若上述命令报错:curl: (35) Encountered end of file
原因:命令中的https://bit.ly/2ysbOFE在国内无法访问,
     官方给出了可在国内可访问的新地址:https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/bootstrap.sh
解决方案:执行如下自动搭建命令(需要一个漫长的等待~)
[root@localhost fabric]# curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/bootstrap.sh| bash -s 

Clone hyperledger/fabric-samples repo

===> Cloning hyperledger/fabric-samples repo
正克隆到 'fabric-samples'...
error: RPC failed; result=35, HTTP code = 0
fatal: The remote end hung up unexpectedly
fabric-samples v2.3.2 does not exist, defaulting main
fatal: Not a git repository (or any of the parent directories): .git

Pull Hyperledger Fabric binaries

===> Downloading version 2.3.2 platform specific fabric binaries
===> Downloading:  https://github.com/hyperledger/fabric/releases/download/v2.3.2/hyperledger-fabric-linux-amd64-2.3.2.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 73.5M  100 73.5M    0     0  75919      0  0:16:56  0:16:56 --:--:-- 98853
==> Done.
===> Downloading version 1.5.0 platform specific fabric-ca-client binary
===> Downloading:  https://github.com/hyperledger/fabric-ca/releases/download/v1.5.0/hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
curl: (35) Encountered end of file

gzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now
==> There was an error downloading the binary file.
------> 1.5.0 fabric-ca-client binary is not available to download  (Available from 1.1.0-rc1) <----


<—!!!—>
若想继续自动搭建,参考教程:https://blog.csdn.net/mutou___/article/details/109849720
<—!!!—>

2.2 手动搭建搭建(推荐)

Step1、下载fabric源码

# 进入之前创建的工作目录
[root@localhost ~]# cd $HOME/go/src/github.com/fabric

# !!不推荐该获取方式
[root@localhost fabric]#  git clone https://github.com/hyperledger/fabric.git
# 注意:这一步下载会很慢,可以使用科学上网方法
# 也可以使用Chrome浏览器中GitHub加速插件,下面使用加速插件连接替换

# !!!!推荐以下获取方式:
# 以下代码执行其中一行即可。
[root@localhost fabric]#  git clone https://hub.fastgit.org/hyperledger/fabric.git
# or
[root@localhost fabric]#  git clone https://github.com.cnpmjs.org/hyperledger/fabric.git
# or
[root@localhost fabric]#  git clone https://github.91chifun.workers.dev//https://github.com/hyperledger/fabric.git

Step2、下载Docker镜像

[root@localhost fabric]# cd fabric/scripts/
[root@localhost scripts]# sudo ./bootstrap.sh -b -s
......
===> List out hyperledger docker images
hyperledger/fabric-tools     2.3       a206a1593b4c   3 months ago   448MB
hyperledger/fabric-tools     2.3.2     a206a1593b4c   3 months ago   448MB
hyperledger/fabric-tools     latest    a206a1593b4c   3 months ago   448MB
hyperledger/fabric-peer      2.3       85c825d4769f   3 months ago   54.2MB
hyperledger/fabric-peer      2.3.2     85c825d4769f   3 months ago   54.2MB
hyperledger/fabric-peer      latest    85c825d4769f   3 months ago   54.2MB
hyperledger/fabric-orderer   2.3       7cad713cbfea   3 months ago   37.8MB
hyperledger/fabric-orderer   2.3.2     7cad713cbfea   3 months ago   37.8MB
hyperledger/fabric-orderer   latest    7cad713cbfea   3 months ago   37.8MB
hyperledger/fabric-ccenv     2.3       627c556b15ca   3 months ago   514MB
hyperledger/fabric-ccenv     2.3.2     627c556b15ca   3 months ago   514MB
hyperledger/fabric-ccenv     latest    627c556b15ca   3 months ago   514MB
hyperledger/fabric-baseos    2.3       e50ea411d694   3 months ago   6.86MB
hyperledger/fabric-baseos    2.3.2     e50ea411d694   3 months ago   6.86MB
hyperledger/fabric-baseos    latest    e50ea411d694   3 months ago   6.86MB
hyperledger/fabric-ca        1.5       24a7c19a9fd8   4 months ago   70.8MB
hyperledger/fabric-ca        1.5.0     24a7c19a9fd8   4 months ago   70.8MB
hyperledger/fabric-ca        latest    24a7c19a9fd8   4 months ago   70.8MB

Step3、下载fabric-samples

[root@localhost scripts]# git clone https://hub.fastgit.org/hyperledger/fabric-samples.git

正克隆到 'fabric-samples'...
remote: Enumerating objects: 7662, done.
remote: Counting objects: 100% (129/129), done.
remote: Compressing objects: 100% (99/99), done.
remote: Total 7662 (delta 48), reused 74 (delta 27), pack-reused 7533
接收对象中: 100% (7662/7662), 4.37 MiB | 1.08 MiB/s, done.
处理 delta 中: 100% (3974/3974), done.

Step4、下载二进制文件

[root@localhost scripts]# cat bootstrap.sh

# 查看以下版本信息
# if version not passed in, default to latest released version
VERSION=2.3.2
# if ca version not passed in, default to latest released version
CA_VERSION=1.5.0

#
[root@localhost scripts]# cd /opt

# 因需要用到wget命令,未安装的需要安装,也可以采用手动下载再上传的方式。
[root@localhost opt]# yum install wget

# 下载hyperledger-fabric-linux-amd64-2.3.0.tar.gz
[root@localhost opt]# wget https://github.91chifun.workers.dev//https://github.com/hyperledger/fabric/releases/download/v2.3.2/hyperledger-fabric-linux-amd64-2.3.2.tar.gz
# 下载hyperledger-fabric-ca-linux-amd64-1.4.9.tar.gz
[root@localhost opt]# wget https://github.91chifun.workers.dev//https://github.com/hyperledger/fabric-ca/releases/download/v1.5.0/hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz

# 查看已下载压缩包
[root@localhost opt]# ls
containerd  hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz  hyperledger-fabric-linux-amd64-2.3.2.tar.gz

# 解压(两个压缩文件解压出 bin/ config/ 两个目录,两个目录应位于fabric-samples之下~)
[root@localhost opt]# tar -zxvf hyperledger-fabric-linux-amd64-2.3.2.tar.gz  -C $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/
[root@localhost opt]# tar -zxvf hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz -C $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/
#

其他参考链接:

Ubuntu 系统环境手动安装:https://blog.csdn.net/mutou___/article/details/109986259

三、 启动测试网络

# 进入test-network目录
[root@localhost test-network]# cd $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/test-network
# 删除先前可能运行的网络
[root@localhost test-network]# ./network.sh down
Stopping network
Stopping cli                    ... done
Stopping orderer.example.com    ... done
Stopping peer0.org1.example.com ... done
Stopping peer0.org2.example.com ... done
Removing cli                    ... done
Removing orderer.example.com    ... done
Removing peer0.org1.example.com ... done
Removing peer0.org2.example.com ... done
Removing network fabric_test
Removing volume docker_orderer.example.com
Removing volume docker_peer0.org1.example.com
Removing volume docker_peer0.org2.example.com
WARNING: The DOCKER_SOCK variable is not set. Defaulting to a blank string.
Removing network fabric_test
WARNING: Network fabric_test not found.
Removing volume docker_peer0.org3.example.com
WARNING: Volume docker_peer0.org3.example.com not found.
Removing remaining containers
Removing generated chaincode docker images

# 执行./network.sh up 前,建议重启下Docker容器,
否则可能会报错:
ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule:  (iptables failed: iptables --wait -t nat -I DOCKER -i br-10eb04b81c62 -j RETURN: iptables: No chain/target/match by that name.
[root@localhost test-network]# systemctl restart  docker
# 如仍未解决,请尝试关闭防火墙:
[root@localhost test-network]# systemctl stop firewalld

# 启动测试网络
[root@localhost test-network]# ./network.sh up
Starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb' with crypto from 'cryptogen'
LOCAL_VERSION=2.3.2
DOCKER_IMAGE_VERSION=2.3.2
/root/go/src/github.com/fabric/fabric/scripts/fabric-samples/bin/cryptogen
Generating certificates using cryptogen tool
Creating Org1 Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-org1.yaml --output=organizations
org1.example.com
+ res=0
Creating Org2 Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-org2.yaml --output=organizations
org2.example.com
+ res=0
Creating Orderer Org Identities
+ cryptogen generate --config=./organizations/cryptogen/crypto-config-orderer.yaml --output=organizations
+ res=0
Generating CCP files for Org1 and Org2
Creating network "fabric_test" with the default driver
Creating volume "docker_orderer.example.com" with default driver
Creating volume "docker_peer0.org1.example.com" with default driver
Creating volume "docker_peer0.org2.example.com" with default driver
Creating orderer.example.com    ... done
Creating peer0.org1.example.com ... done
Creating peer0.org2.example.com ... done
Creating cli                    ... done
CONTAINER ID   IMAGE                               COMMAND             CREATED                  STATUS                  PORTS                                                                                                                                 NAMES
c231a061efeb   hyperledger/fabric-tools:latest     "/bin/bash"         Less than a second ago   Up Less than a second                                                                                                                                         cli
d8105d031285   hyperledger/fabric-orderer:latest   "orderer"           2 seconds ago            Up Less than a second   0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:17050->17050/tcp, :::17050->17050/tcp   orderer.example.com
13ebc165136a   hyperledger/fabric-peer:latest      "peer node start"   2 seconds ago            Up Less than a second   0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:17051->17051/tcp, :::17051->17051/tcp                                              peer0.org1.example.com
e77cc6055337   hyperledger/fabric-peer:latest      "peer node start"   2 seconds ago            Up Less than a second   0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp                                    peer0.org2.example.com

# 网络启动验证,可查看启动了以下几个模拟网络的容器:
# 测试网络包括两个对等组织,Org1 和 Org2。它还包括一个维护网络订购服务的单一订购者组织。
[root@localhost test-network]# docker ps
CONTAINER ID   IMAGE                               COMMAND             CREATED          STATUS          PORTS                                                                                                                                 NAMES
c231a061efeb   hyperledger/fabric-tools:latest     "/bin/bash"         49 seconds ago   Up 48 seconds                                                                                                                                         cli
d8105d031285   hyperledger/fabric-orderer:latest   "orderer"           51 seconds ago   Up 49 seconds   0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:17050->17050/tcp, :::17050->17050/tcp   orderer.example.com
13ebc165136a   hyperledger/fabric-peer:latest      "peer node start"   51 seconds ago   Up 49 seconds   0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:17051->17051/tcp, :::17051->17051/tcp                                              peer0.org1.example.com
e77cc6055337   hyperledger/fabric-peer:latest      "peer node start"   51 seconds ago   Up 49 seconds   0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp                                    peer0.org2.example.com

四、 创建频道(channel)

# 默认 为mychannel,可以自定义channel名:./network.sh createChannel -c <自定义Channel名>
[root@localhost test-network]# ./network.sh createChannel
Creating channel 'mychannel'.
If network is not up, starting nodes with CLI timeout of '5' tries and CLI delay of '3' seconds and using database 'leveldb
Generating channel genesis block 'mychannel.block'
/root/go/src/github.com/fabric/fabric/scripts/fabric-samples/bin/configtxgen
.......
2021-08-05 05:31:24.428 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2021-08-05 05:31:24.442 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
Anchor peer set for org 'Org2MSP' on channel 'mychannel'
Channel 'mychannel' joined

五、 启用链码(chaincode)

# 在启用码链前,需要更改go代理,确保启用成功。
否则,会报如下错误:
......
Error: failed to normalize chaincode path: 'go list' failed with: : signal: killed
Chaincode packaging has failed
Deploying chaincode failed

[root@localhost test-network]# go env -w GOPROXY=https://goproxy.io,direct
[root@localhost test-network]# go env -w GO111MODULE=on

# deployCC子命令将在peer0.org1.example.com和peer0.org2.example.com上安装资产转移(基本)链码,然后在使用通道标志指定的通道上部署链码(如果未指定通道,则在mychannel上部署)。
[root@localhost test-network]# ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
deploying chaincode on channel 'mychannel'
executing with the following
......
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org2 on channel 'mychannel'
Chaincode initialization is not required

六、 与网络互动(peer)

# 
# 确保您从test-network目录中操作
# 进入test-network目录
[root@localhost ~]# cd $HOME/go/src/github.com/fabric/fabric/scripts/fabric-samples/test-network

# 
#  添加二进制文件的环境变量
[root@localhost test-network]# export PATH=${PWD}/../bin:$PATH
# FABRIC_CFG_PATH
[root@localhost test-network]# export FABRIC_CFG_PATH=$PWD/../config/

# 
# 设置允许peer以Org1身份操作CLI的环境变量:
[root@localhost test-network]# export CORE_PEER_TLS_ENABLED=true
[root@localhost test-network]# export CORE_PEER_LOCALMSPID="Org1MSP"
[root@localhost test-network]# export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
[root@localhost test-network]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
[root@localhost test-network]# export CORE_PEER_ADDRESS=localhost:7051

# 
# 使用资产初始化分类账( initialize the ledger with assets)
[root@localhost test-network]# 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":[]}'
2021-08-05 15:53:20.773 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

# 
# 获取添加到渠道分类账的资产列表(get the list of assets that were added to your channel ledger)
[root@localhost test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
[{"ID":"asset1","color":"blue","size":5,"owner":"Tomoko","appraisedValue":300},{"ID":"asset2","color":"red","size":5,"owner":"Brad","appraisedValue":400},{"ID":"asset3","color":"green","size":10,"owner":"Jin Soo","appraisedValue":500},{"ID":"asset4","color":"yellow","size":10,"owner":"Max","appraisedValue":600},{"ID":"asset5","color":"black","size":15,"owner":"Adriana","appraisedValue":700},{"ID":"asset6","color":"white","size":15,"owner":"Michel","appraisedValue":800}]

# 
# 通过调用资产转移(基本)链代码来更改账本上资产的所有者(change the owner of an asset on the ledger by invoking the asset-transfer (basic) chaincode)
[root@localhost test-network]# 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"]}'
2021-08-05 15:58:26.650 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

# 
# 设置允许peer以Org2身份操作CLI的环境变量:
[root@localhost test-network]# export CORE_PEER_TLS_ENABLED=true
[root@localhost test-network]# export CORE_PEER_LOCALMSPID="Org2MSP"
[root@localhost test-network]# export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
[root@localhost test-network]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
[root@localhost test-network]# export CORE_PEER_ADDRESS=localhost:9051

# 
# 查询在peer0.org2.example.com上运行的资产转移(基本)链代码
# query the asset-transfer (basic) chaincode running on peer0.org2.example.com
[root@localhost test-network]# peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
{"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}

#关闭网络
[root@localhost test-network]# ./network.sh down

你可能感兴趣的:(工具及操作系统,docker,linux,区块链,fabric)