系统环境:
[root@localhost ~]
Linux version 3.10.0-1160.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) )
官网手册:
https://www.hyperledger.org/use/fabric
环境搭建
一、基础软件安装
[root@localhost ~]
[root@localhost ~]
git version 1.8.3.1
安装cURL
cURL是一个利用URL语法在命令行下工作的文件传输工具,它支持文件上传和下载,所以是综合传输工具
[root@localhost ~]
[root@localhost ~]
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 机器上,也可以实现虚拟化
Docker引擎(Docker Engine)安装请参考该教程:https://blog.csdn.net/Bjxhub/article/details/119352882
[root@localhost ~]
[root@localhost ~]
Docker version 20.10.7, build f0df350
Docker Compose安装官方教程:https://docs.docker.com/compose/install/
[root@localhost ~]
[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 ~]
[root@localhost ~]
docker-compose version 1.29.2, build 5becea4c
https://docs.docker.com/compose/compose-file/compose-versioning/
[root@localhost ~]
[root@localhost ~]
安装Go
Google 的 Robert Griesemer,Rob Pike 及 Ken Thompson 开发的一种静态强类型、编译型语言 ##!!!!----若Go与Jq无法安装,则需添加安装源:[[root@localhost ~]# yum install epel-release]----!!!!!
[root@localhost ~]
[root@localhost ~]
go version go1.15.5 linux/amd64
安装JQ
jq是一个轻量级且灵活的命令行JSON处理器
[root@localhost ~]
[root@localhost ~]
jq-1.6
二、 安装 Fabric 和 Fabric 示例
2.1 自动搭建(不推荐,国内基本不成功,推荐直接进入2.2 手动搭建)
[root@localhost ~]
[root@localhost ~]
[root@localhost fabric]
/root/go/src/github.com/fabric
[root@localhost fabric]
若上述命令报错:curl: (35) Encountered end of file
原因:命令中的https://bit.ly/2ysbOFE在国内无法访问,
官方给出了可在国内可访问的新地址:https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/bootstrap.sh
解决方案:执行如下自动搭建命令(需要一个漫长的等待~)
[root@localhost fabric]
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 ~]
[root@localhost fabric]
git clone https://hub.fastgit.org/hyperledger/fabric.git
git clone https://github.com.cnpmjs.org/hyperledger/fabric.git
git clone https://github.91chifun.workers.dev//https://github.com/hyperledger/fabric.git
Step2、下载Docker镜像
[root@localhost fabric]
[root@localhost scripts]
......
===> 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]
正克隆到 '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]
VERSION=2.3.2
CA_VERSION=1.5.0
[root@localhost scripts]
[root@localhost opt]
[root@localhost opt]
[root@localhost opt]
[root@localhost opt]
containerd hyperledger-fabric-ca-linux-amd64-1.5.0.tar.gz hyperledger-fabric-linux-amd64-2.3.2.tar.gz
[root@localhost opt]
[root@localhost opt]
其他参考链接:
Ubuntu 系统环境手动安装:https://blog.csdn.net/mutou___/article/details/109986259
三、 启动测试网络
[root@localhost test-network]
[root@localhost test-network]
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
否则可能会报错:
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]
[root@localhost test-network]
[root@localhost test-network]
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
[root@localhost test-network]
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)
[root@localhost test-network]
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)
否则,会报如下错误:
......
Error: failed to normalize chaincode path: 'go list' failed with: : signal: killed
Chaincode packaging has failed
Deploying chaincode failed
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
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)
[root@localhost ~]
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
2021-08-05 15:53:20.773 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
[root@localhost test-network]
[{"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}]
[root@localhost test-network]
2021-08-05 15:58:26.650 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
[root@localhost test-network]
{"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}
[root@localhost test-network]
|