### docker 安装 如果服务器上有旧版的docker,需要先执行卸载操作。 $ sudo yum remove docker docker-common docker-selinux docker-engine 随后开始安装docker-ce 1 $ sudo yum install -y yum-utils device-mapper-persistent-data lvm2 2 3 $ sudo yum-config-manager 4 --add-repo 5 https://download.docker.com/linux/centos/docker-ce.repo 6 7 $ sudo yum-config-manager --enable docker-ce-edge 8 9 $ sudo yum-config-manager --enable docker-ce-test 10 11 $ sudo yum-config-manager --disable docker-ce-edge 12 13 $ sudo yum makecache fast 14 15 $ sudo yum install docker-ce 执行查询docker版本号,看是否安装成功 docker --version docker启动 service docker start docker开机自启(视具体情况使用) chkconfig docker on ### docker-compose 安装 需要服务器支持 curl 功能,如果服务器不支持 curl 功能,需要手动进行安装 yum install curl 执行如下操作下载docker-compose sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose 下载目录为: /usr/local/bin/docker-compose 为其添加可执行权限: sudo chmod +x /usr/local/bin/docker-compose 查询版本信息: docker-compose --version ### Go语言安装 安装包下载 curl -O https://golang.google.cn/doc/install?download=go1.15.linux-amd64.tar.gz 解压缩文件包 tar -C /usr/local -xzf go1.15.linux-amd64.tar.gz 配置 go 的环境变量 修改 /etc/profile 文件使其永久生效 1 export PATH=$PATH:/usr/local/go/bin 2 export GOPATH=/opt/gopath 修改文件后,执行如下命令,应用上述参数 source /etc/profile 查看当前 go 版本信息 go version 也可通过下述命令查看是否安装成功 echo $PATH
下载 fabric 源码是因为要用到源码中的例子和工具,工具编译需要用到 go 语言环境,因此需要把源码目录放到 GOPATH 下,路径设置为 /opt/gopath 可以使用 Git 命令下载源码(耗时会比较长): cd /opt/gopath/src/github.com/hyperledger/ git clone https://github.com/hyperledger/fabric.git 如果没有 git 命令,需要先执行如下命令,构建 git 环境: yum install git 默认Fabric源码下载版本为 2.2.0 Fabric Docker 镜像的下载: cd /opt/gopath/src/github.com/hyperledger/fabric/scripts ./bootstrap.sh 等待下载完成即可 至此,Fabric源码,fabric-samples 源码、fabric 镜像的工作已经完成
启动 Fabric 网络 cd /opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network ./network.sh up 建立通道 ./network.sh createChannel 在通道上启动链码 ./network.sh deployCC 如果链码启动失败,手动添加 go 代理 go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn,direct 再次启动链码即可 在test-network 目录进行如下环境变量设置 export PATH=${PWD}/../bin:${PWD}:$PATH export FABRIC_CFG_PATH=$PWD/../config/ 继续设置如下环境变量,允许以 peer Org1 的形式操作 CLI # 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/Admin@org1.example.com/msp export CORE_PEER_ADDRESS=localhost:7051 完成后,source /etc/profile 获取已添加到通道分类账中的汽车列表 peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}' 上述命令可能会出现报错提示,如下(如果是提示某些文件不存在,我的一个解决办法是,关闭网络,启动网络前,source /etc/profile): Error: endorsement failure during query. response: status:500 message:"make sure the chaincode fabcar has been successfully defined on channel mychannel and try again: chaincode fabcar not found" 查看报错信息,我们可以知道,链码 fabcar 没有在通道上被定义,所以我们进入对应目录,执行启动指令: cd /opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/fabcar ./startFabric.sh 等待执行结束即可 我们现在可以重新进入 test-network 目录,执行命令 peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}' 可以看到,正常返回结果了 [{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}},{"Key":"CAR1","Record":{"make":"Ford","model":"Mustang","colour":"red","owner":"Brad"}},{"Key":"CAR2","Record":{"make":"Hyundai","model":"Tucson","colour":"green","owner":"Jin Soo"}},{"Key":"CAR3","Record":{"make":"Volkswagen","model":"Passat","colour":"yellow","owner":"Max"}},{"Key":"CAR4","Record":{"make":"Tesla","model":"S","colour":"black","owner":"Adriana"}},{"Key":"CAR5","Record":{"make":"Peugeot","model":"205","colour":"purple","owner":"Michel"}},{"Key":"CAR6","Record":{"make":"Chery","model":"S22L","colour":"white","owner":"Aarav"}},{"Key":"CAR7","Record":{"make":"Fiat","model":"Punto","colour":"violet","owner":"Pari"}},{"Key":"CAR8","Record":{"make":"Tata","model":"Nano","colour":"indigo","owner":"Valeria"}},{"Key":"CAR9","Record":{"make":"Holden","model":"Barina","colour":"brown","owner":"Shotaro"}}]
上述调试工作相继完成后,我们继续如下操作(仍然在 test-network 目录下),执行关闭网络指令:
./network.sh down
接着,编辑profile文件,添加如下内容(因为资产转移链码的背书策略要求事务由 Org1 和 Org2 签名,chaincode invoke 命令需要同时针对 peer0.org1.example.com 和 peer0.org2.example.com 使用 --peeradresses 标志,而且,由于为网络启用了 TLS, 该命令还需要使用 --tlsRootCertFiles 标志为每个对等方引用 TLS 证书。):
vi /etc/profile
# Environment variables for 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/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
保存退出
source /etc/profile
启动测试网络:
./network.sh up
./network.sh createChannel
./network.sh deployCC
运行如下命令初始化资产分类账:
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":[]}'
如果有如下报错信息,可能是链码没有在通道中被成功定义,关闭测试网络后,重新执行启动网络操作:
Error: endorsement failure during invoke. response: status:500 message:"make sure the chaincode basic has been successfully defined on channel mychannel and try again: chaincode basic not found"
上述初始化指令的执行成功返回结果信息如下:
2020-08-28 11:20:52.062 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
运行以下命令获取添加到通道分类账的资产列表:
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}]
当网络成员希望转移或更改分类账上的资产时,会调用链码。使用以下命令更改分类账上的资产所有者:
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"]}'
返回如下提示信息,即表示更改成功:
2020-08-28 11:21:29.252 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
使用如下命令查询刚刚的转移信息:
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
返回结果如下:
{"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}
(如果是提示某些文件不存在,我的一个解决办法是,启动网络前,source /etc/profile)