一、搭建基础环境
参考:https://www.cnblogs.com/sky-cheng/p/12150822.html
二、下载Hyperledger Fabric 源代码
查看$GOPATH环境变量
1[root@localhost home]# echo $GOPATH 2/home/go
新建如下目录
1[root@localhost home]# mkdir -p /home/go/src/github.com/hyperledger 2[root@localhost home]#
进入新建的目录,下载Fabric源代码
1[root@localhost home]# cd /home/go/src/github.com/hyperledger/ 2[root@localhost hyperledger]# git clone https://github.com/hyperledger/fabric.git 3正克隆到 'fabric'... 4remote: Enumerating objects: 12, done. 5remote: Counting objects: 100% (12/12), done. 6remote: Compressing objects: 100% (11/11), done. 7remote: Total 125935 (delta 2), reused 1 (delta 1), pack-reused 125923 8接收对象中: 100% (125935/125935), 101.50 MiB | 124.00 KiB/s, done. 9处理 delta 中: 100% (85366/85366), done. 10[root@localhost hyperledger]#
进入源代码目录,用git checkout 切换到V1.1.0分支,用git status 当前分支,用git tag查看所有分支
1[root@localhost fabric]# cd fabric/ 2[root@localhost fabric]# git checkout v1.1.0 3Note: checking out 'v1.1.0'. 4 5You are in 'detached HEAD' state. You can look around, make experimental 6changes and commit them, and you can discard any commits you make in this 7state without impacting any branches by performing another checkout. 8 9If you want to create a new branch to retain commits you create, you may 10do so (now or later) by using -b with the checkout command again. Example: 11 12 git checkout -b new_branch_name 13 14HEAD 目前位于 523f644... [FAB-8883] Prepare for Fabric v1.1.0 release 15[root@localhost fabric]# 16 17[root@localhost fabric]# git status 18# 头指针分离于 v1.1.0 19无文件要提交,干净的工作区
进入scripts目录,bootstrap.sh脚本的作用是下载fabric相关镜像文件如:peer orderer ccenv javaenv,以及kafka、zookeeper、couchdb第三方镜像,以及下载一些工具文件用于辅助生成证书、秘钥、以及各种相关配置文件等,打开bootstrap.sh脚本
1# current version of fabric released 2export VERSION=${1:-1.0.4}
这里设置了版本号的VERSION环境变量,我们修改成1.1.0版本,保存退出
1# current version of fabric released 2export VERSION=${1:-1.1.0}
三、运行bootstrap.sh脚本
1[root@localhost scripts]# ./bootstrap.sh 2===> Downloading platform specific fabric binaries 3 % Total % Received % Xferd Average Speed Time Time Time Current 4 Dload Upload Total Spent Left Speed 5100 35.4M 100 35.4M 0 0 19040 0 0:32:30 0:32:30 --:--:-- 23981 6===> Downloading platform specific fabric-ca-client binary 7 % Total % Received % Xferd Average Speed Time Time Time Current 8 Dload Upload Total Spent Left Speed 9100 6109k 100 6109k 0 0 16023 0 0:06:30 0:06:30 --:--:-- 21829 10===> Pulling fabric Images 11==> FABRIC IMAGE: peer 12 13x86_64-1.1.0: Pulling from hyperledger/fabric-peer 14Digest: sha256:57417699ddf50c5ebd47a9a2cc74c0324fbba0281eb1104b9ddd05a67776b01f 15Status: Image is up to date for hyperledger/fabric-peer:x86_64-1.1.0 16docker.io/hyperledger/fabric-peer:x86_64-1.1.0 17==> FABRIC IMAGE: orderer 18 19x86_64-1.1.0: Pulling from hyperledger/fabric-orderer 20Digest: sha256:0c3a3b5ecfd24b513da22bbb77da7b3f5bca9c121cc0ac5c46ba04c97c163654 21Status: Image is up to date for hyperledger/fabric-orderer:x86_64-1.1.0 22docker.io/hyperledger/fabric-orderer:x86_64-1.1.0 23==> FABRIC IMAGE: ccenv 24 25x86_64-1.1.0: Pulling from hyperledger/fabric-ccenv 26Digest: sha256:07818367dc6d4264472d24b21819f9dc4e16e890d81ddfacee0341a22d72050b 27Status: Image is up to date for hyperledger/fabric-ccenv:x86_64-1.1.0 28docker.io/hyperledger/fabric-ccenv:x86_64-1.1.0 29==> FABRIC IMAGE: javaenv 30 31x86_64-1.1.0: Pulling from hyperledger/fabric-javaenv 32Digest: sha256:d2588c0556b6fc79131f638b02a7a77337363e2c2f38a9c47798a6d99bd2f20e 33Status: Image is up to date for hyperledger/fabric-javaenv:x86_64-1.1.0 34docker.io/hyperledger/fabric-javaenv:x86_64-1.1.0 35==> FABRIC IMAGE: tools 36 37x86_64-1.1.0: Pulling from hyperledger/fabric-tools 38Digest: sha256:36d7fa8e8ddcc19fed8e1c3c06bc6ae1dac18c35e8a884188d2c08df3e5a4472 39Status: Image is up to date for hyperledger/fabric-tools:x86_64-1.1.0 40docker.io/hyperledger/fabric-tools:x86_64-1.1.0 41===> Pulling fabric ca Image 42==> FABRIC CA IMAGE 43 44x86_64-1.1.0: Pulling from hyperledger/fabric-ca 45Digest: sha256:92f44d0811cddb0d335f7879f7e3b3c4b631f31740c76f3e7b85438c244b03f4 46Status: Image is up to date for hyperledger/fabric-ca:x86_64-1.1.0 47docker.io/hyperledger/fabric-ca:x86_64-1.1.0 48===> Pulling thirdparty docker images 49==> THIRDPARTY DOCKER IMAGE: couchdb 50 51x86_64-0.4.6: Pulling from hyperledger/fabric-couchdb 52Digest: sha256:4278269b115cfd0f24251b5381407be9ccdf396c1470c69e1ee2ff16917ac882 53Status: Image is up to date for hyperledger/fabric-couchdb:x86_64-0.4.6 54docker.io/hyperledger/fabric-couchdb:x86_64-0.4.6 55==> THIRDPARTY DOCKER IMAGE: kafka 56 57x86_64-0.4.6: Pulling from hyperledger/fabric-kafka 58Digest: sha256:738bb79f02aa505a9ab0553a865122605ffba91d1b5de6eb0429f7287e83ac7c 59Status: Image is up to date for hyperledger/fabric-kafka:x86_64-0.4.6 60docker.io/hyperledger/fabric-kafka:x86_64-0.4.6 61==> THIRDPARTY DOCKER IMAGE: zookeeper 62 63x86_64-0.4.6: Pulling from hyperledger/fabric-zookeeper 64Digest: sha256:7fda667f6c39d6cb87125c132962fc3e434a8e6b2b16ee798d6fa94f970531a2 65Status: Image is up to date for hyperledger/fabric-zookeeper:x86_64-0.4.6 66docker.io/hyperledger/fabric-zookeeper:x86_64-0.4.6 67 68===> List out hyperledger docker images 69hyperledger/fabric-ca latest 72617b4fa9b4 22 months ago 299MB 70hyperledger/fabric-ca x86_64-1.1.0 72617b4fa9b4 22 months ago 299MB 71hyperledger/fabric-tools latest b7bfddf508bc 22 months ago 1.46GB 72hyperledger/fabric-tools x86_64-1.1.0 b7bfddf508bc 22 months ago 1.46GB 73hyperledger/fabric-orderer latest ce0c810df36a 22 months ago 180MB 74hyperledger/fabric-orderer x86_64-1.1.0 ce0c810df36a 22 months ago 180MB 75hyperledger/fabric-peer latest b023f9be0771 22 months ago 187MB 76hyperledger/fabric-peer x86_64-1.1.0 b023f9be0771 22 months ago 187MB 77hyperledger/fabric-javaenv latest 82098abb1a17 22 months ago 1.52GB 78hyperledger/fabric-javaenv x86_64-1.1.0 82098abb1a17 22 months ago 1.52GB 79hyperledger/fabric-ccenv latest c8b4909d8d46 22 months ago 1.39GB 80hyperledger/fabric-ccenv x86_64-1.1.0 c8b4909d8d46 22 months ago 1.39GB 81hyperledger/fabric-zookeeper latest 92cbb952b6f8 23 months ago 1.39GB 82hyperledger/fabric-zookeeper x86_64-0.4.6 92cbb952b6f8 23 months ago 1.39GB 83hyperledger/fabric-kafka latest 554c591b86a8 23 months ago 1.4GB 84hyperledger/fabric-kafka x86_64-0.4.6 554c591b86a8 23 months ago 1.4GB 85hyperledger/fabric-couchdb latest 7e73c828fc5b 23 months ago 1.56GB 86hyperledger/fabric-couchdb x86_64-0.4.6 7e73c828fc5b 23 months ago 1.56GB 87[root@localhost scripts]#
首先下载相关工具二进制文件,其次再拉取v1.1.0相关版本的镜像文件。二进制文件保存在scripts/bin 目录里,把它移动到/home/go/src/github.com/hyperledger/下,并设置好PATH变量
1[root@localhost scripts]# mv bin/ /home/go/src/github.com/hyperledger/fabric 2[root@localhost bin]# echo "export PATH=$PATH:/home/go/src/github.com/hyperledger/fabric/bin">>/etc/profile 3[root@localhost bin]# source /etc/profile 4[root@localhost bin]# echo $PATH 5/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/dell/srvadmin/bin:/opt/dell/srvadmin/sbin:/home/mysql-5.7.26/bin/:/root/bin:/usr/local/go/bin:/root/bin:/home/go/src/github.com/hyperledger/fabric/bin 6[root@localhost bin]#
四、下载fabric-sample项目源代码
1[root@localhost hyperledger]# cd /home/go/src/github.com/hyperledger 2[root@localhost hyperledger]# git clone https://github.com/hyperledger/fabric-samples.git 3正克隆到 'fabric-samples'... 4remote: Enumerating objects: 4430, done. 5remote: Total 4430 (delta 0), reused 0 (delta 0), pack-reused 4430 6接收对象中: 100% (4430/4430), 1.61 MiB | 8.00 KiB/s, done. 7处理 delta 中: 100% (2181/2181), done. 8[root@localhost hyperledger]# ll 9总用量 8 10drwxrwxr-x 2 mysql mysql 142 3月 16 2018 bin 11drwxr-xr-x 27 root root 4096 1月 13 17:03 fabric 12drwxr-xr-x 16 root root 4096 1月 13 22:32 fabric-samples 13[root@localhost hyperledger]#
切换到v1.1.0版本
1[root@localhost hyperledger]# cd fabric-samples/ 2[root@localhost fabric-samples]# git checkout v1.1.0 3Note: checking out 'v1.1.0'. 4 5You are in 'detached HEAD' state. You can look around, make experimental 6changes and commit them, and you can discard any commits you make in this 7state without impacting any branches by performing another checkout. 8 9If you want to create a new branch to retain commits you create, you may 10do so (now or later) by using -b with the checkout command again. Example: 11 12 git checkout -b new_branch_name 13 14HEAD 目前位于 1252c7a... [FAB-8920] Pin fabric-samples to node.js "~1.1.0" 15 16[root@localhost fabric-samples]# git status 17# 头指针分离于 v1.1.0 18无文件要提交,干净的工作区 19[root@localhost fabric-samples]#
五、启动网络
进入first-network目录
1root@localhost fabric-samples]# cd first-network/ 2[root@localhost first-network]# ll 3总用量 72 4drwxr-xr-x 2 root root 60 1月 13 22:34 base 5-rwxr-xr-x 1 root root 20114 1月 13 22:34 byfn.sh 6drwxr-xr-x 2 root root 22 1月 13 22:32 channel-artifacts 7-rw-r--r-- 1 root root 7912 1月 13 22:34 configtx.yaml 8-rw-r--r-- 1 root root 3906 1月 13 22:34 crypto-config.yaml 9-rw-r--r-- 1 root root 2971 1月 13 22:34 docker-compose-cli.yaml 10-rw-r--r-- 1 root root 2345 1月 13 22:34 docker-compose-couch-org3.yaml 11-rw-r--r-- 1 root root 4560 1月 13 22:34 docker-compose-couch.yaml 12-rw-r--r-- 1 root root 2883 1月 13 22:32 docker-compose-e2e-template.yaml 13-rw-r--r-- 1 root root 3493 1月 13 22:34 docker-compose-org3.yaml 14-rwxr-xr-x 1 root root 10627 1月 13 22:34 eyfn.sh 15drwxr-xr-x 2 root root 51 1月 13 22:34 org3-artifacts 16-rw-r--r-- 1 root root 335 1月 13 22:32 README.md 17drwxr-xr-x 2 root root 168 1月 13 22:34 scripts
运行byfn.sh脚本,启动网络
1[root@localhost first-network]# ./byfn.sh -m up -c firstchannel 2Starting with channel 'firstchannel' and CLI timeout of '10' seconds and CLI delay of '3' seconds 3Continue? [Y/n] y 4proceeding ... 52020-01-13 14:52:39.329 UTC [main] main -> INFO 001 Exiting..... 6LOCAL_VERSION=1.1.0 7DOCKER_IMAGE_VERSION=1.1.0 8/home/go/src/github.com/hyperledger/bin/cryptogen 9 10########################################################## 11##### Generate certificates using cryptogen tool ######### 12########################################################## 13+ cryptogen generate --config=./crypto-config.yaml 14org1.example.com 15org2.example.com 16+ res=0 17+ set +x 18 19/home/go/src/github.com/hyperledger/bin/configtxgen 20########################################################## 21######### Generating Orderer Genesis block ############## 22########################################################## 23+ configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block 242020-01-13 22:52:39.977 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration 252020-01-13 22:52:40.002 CST [msp] getMspConfig -> INFO 002 Loading NodeOUs 262020-01-13 22:52:40.003 CST [msp] getMspConfig -> INFO 003 Loading NodeOUs 272020-01-13 22:52:40.003 CST [common/tools/configtxgen] doOutputBlock -> INFO 004 Generating genesis block 282020-01-13 22:52:40.004 CST [common/tools/configtxgen] doOutputBlock -> INFO 005 Writing genesis block 29+ res=0 30+ set +x 31 32################################################################# 33### Generating channel configuration transaction 'channel.tx' ### 34################################################################# 35+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID firstchannel 362020-01-13 22:52:40.031 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration 372020-01-13 22:52:40.054 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx 382020-01-13 22:52:40.055 CST [msp] getMspConfig -> INFO 003 Loading NodeOUs 392020-01-13 22:52:40.056 CST [msp] getMspConfig -> INFO 004 Loading NodeOUs 402020-01-13 22:52:40.143 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 005 Writing new channel tx 41+ res=0 42+ set +x 43 44################################################################# 45####### Generating anchor peer update for Org1MSP ########## 46################################################################# 47+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID firstchannel -asOrg Org1MSP 482020-01-13 22:52:40.170 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration 492020-01-13 22:52:40.193 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update 502020-01-13 22:52:40.193 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update 51+ res=0 52+ set +x 53 54################################################################# 55####### Generating anchor peer update for Org2MSP ########## 56################################################################# 57+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID firstchannel -asOrg Org2MSP 582020-01-13 22:52:40.220 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration 592020-01-13 22:52:40.243 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update 602020-01-13 22:52:40.244 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update 61+ res=0 62+ set +x 63 64Creating network "net_byfn" with the default driver 65Creating volume "net_orderer.example.com" with default driver 66Creating volume "net_peer0.org1.example.com" with default driver 67Creating volume "net_peer1.org1.example.com" with default driver 68Creating volume "net_peer0.org2.example.com" with default driver 69Creating volume "net_peer1.org2.example.com" with default driver 70Creating peer1.org2.example.com ... done 71Creating peer1.org1.example.com ... done 72Creating orderer.example.com ... done 73Creating peer0.org1.example.com ... done 74Creating peer0.org2.example.com ... done 75Creating cli ... done 76 77 ____ _____ _ ____ _____ 78/ ___| |_ _| / \ | _ \ |_ _| 79\___ \ | | / _ \ | |_) | | | 80 ___) | | | / ___ \ | _ < | | 81|____/ |_| /_/ \_\ |_| \_\ |_| 82 83Build your first network (BYFN) end-to-end test 84 85Channel name : firstchannel 86Creating channel... 87CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 88CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 89CORE_PEER_LOCALMSPID=Org1MSP 90CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 91CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 92CORE_PEER_TLS_ENABLED=true 93CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 94CORE_PEER_ID=cli 95CORE_LOGGING_LEVEL=INFO 96CORE_PEER_ADDRESS=peer0.org1.example.com:7051 97+ peer channel create -o orderer.example.com:7050 -c firstchannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem 98+ res=0 99+ set +x 1002020-01-13 14:52:47.158 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 1012020-01-13 14:52:47.226 UTC [channelCmd] InitCmdFactory -> INFO 002 Endorser and orderer connections initialized 1022020-01-13 14:52:47.432 UTC [main] main -> INFO 003 Exiting..... 103===================== Channel "firstchannel" is created successfully ===================== 104 105Having all peers join the channel... 106CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 107CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 108CORE_PEER_LOCALMSPID=Org1MSP 109CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 110CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 111CORE_PEER_TLS_ENABLED=true 112CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 113CORE_PEER_ID=cli 114CORE_LOGGING_LEVEL=INFO 115CORE_PEER_ADDRESS=peer0.org1.example.com:7051 116+ peer channel join -b firstchannel.block 117+ res=0 118+ set +x 1192020-01-13 14:52:47.586 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 1202020-01-13 14:52:47.747 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel 1212020-01-13 14:52:47.747 UTC [main] main -> INFO 003 Exiting..... 122===================== peer0.org1 joined on the channel "firstchannel" ===================== 123 124CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 125CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 126CORE_PEER_LOCALMSPID=Org1MSP 127CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 128CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 129CORE_PEER_TLS_ENABLED=true 130CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 131CORE_PEER_ID=cli 132CORE_LOGGING_LEVEL=INFO 133CORE_PEER_ADDRESS=peer1.org1.example.com:7051 134+ peer channel join -b firstchannel.block 135+ res=0 136+ set +x 1372020-01-13 14:52:50.900 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 1382020-01-13 14:52:51.065 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel 1392020-01-13 14:52:51.065 UTC [main] main -> INFO 003 Exiting..... 140===================== peer1.org1 joined on the channel "firstchannel" ===================== 141 142CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 143CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 144CORE_PEER_LOCALMSPID=Org2MSP 145CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 146CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 147CORE_PEER_TLS_ENABLED=true 148CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 149CORE_PEER_ID=cli 150CORE_LOGGING_LEVEL=INFO 151CORE_PEER_ADDRESS=peer0.org2.example.com:7051 152+ peer channel join -b firstchannel.block 153+ res=0 154+ set +x 1552020-01-13 14:52:54.200 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 1562020-01-13 14:52:54.367 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel 1572020-01-13 14:52:54.367 UTC [main] main -> INFO 003 Exiting..... 158===================== peer0.org2 joined on the channel "firstchannel" ===================== 159 160CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 161CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 162CORE_PEER_LOCALMSPID=Org2MSP 163CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 164CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 165CORE_PEER_TLS_ENABLED=true 166CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 167CORE_PEER_ID=cli 168CORE_LOGGING_LEVEL=INFO 169CORE_PEER_ADDRESS=peer1.org2.example.com:7051 170+ peer channel join -b firstchannel.block 171+ res=0 172+ set +x 1732020-01-13 14:52:57.503 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 1742020-01-13 14:52:57.680 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel 1752020-01-13 14:52:57.680 UTC [main] main -> INFO 003 Exiting..... 176===================== peer1.org2 joined on the channel "firstchannel" ===================== 177 178Updating anchor peers for org1... 179CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 180CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 181CORE_PEER_LOCALMSPID=Org1MSP 182CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 183CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 184CORE_PEER_TLS_ENABLED=true 185CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 186CORE_PEER_ID=cli 187CORE_LOGGING_LEVEL=INFO 188CORE_PEER_ADDRESS=peer0.org1.example.com:7051 189+ peer channel update -o orderer.example.com:7050 -c firstchannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem 190+ res=0 191+ set +x 1922020-01-13 14:53:00.815 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 1932020-01-13 14:53:00.848 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update 1942020-01-13 14:53:00.848 UTC [main] main -> INFO 003 Exiting..... 195===================== Anchor peers for org "Org1MSP" on "firstchannel" is updated successfully ===================== 196 197Updating anchor peers for org2... 198CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 199CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 200CORE_PEER_LOCALMSPID=Org2MSP 201CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 202CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 203CORE_PEER_TLS_ENABLED=true 204CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 205CORE_PEER_ID=cli 206CORE_LOGGING_LEVEL=INFO 207CORE_PEER_ADDRESS=peer0.org2.example.com:7051 208+ peer channel update -o orderer.example.com:7050 -c firstchannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem 209+ res=0 210+ set +x 2112020-01-13 14:53:03.986 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2122020-01-13 14:53:04.016 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update 2132020-01-13 14:53:04.016 UTC [main] main -> INFO 003 Exiting..... 214===================== Anchor peers for org "Org2MSP" on "firstchannel" is updated successfully ===================== 215 216Installing chaincode on peer0.org1... 217CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 218CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 219CORE_PEER_LOCALMSPID=Org1MSP 220CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 221CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 222CORE_PEER_TLS_ENABLED=true 223CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 224CORE_PEER_ID=cli 225CORE_LOGGING_LEVEL=INFO 226CORE_PEER_ADDRESS=peer0.org1.example.com:7051 227+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/ 228+ res=0 229+ set +x 2302020-01-13 14:53:07.168 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2312020-01-13 14:53:07.168 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2322020-01-13 14:53:07.508 UTC [main] main -> INFO 003 Exiting..... 233===================== Chaincode is installed on peer0.org1 ===================== 234 235Install chaincode on peer0.org2... 236CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 237CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 238CORE_PEER_LOCALMSPID=Org2MSP 239CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 240CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 241CORE_PEER_TLS_ENABLED=true 242CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 243CORE_PEER_ID=cli 244CORE_LOGGING_LEVEL=INFO 245CORE_PEER_ADDRESS=peer0.org2.example.com:7051 246+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/ 247+ res=0 248+ set +x 2492020-01-13 14:53:07.645 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2502020-01-13 14:53:07.646 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2512020-01-13 14:53:07.999 UTC [main] main -> INFO 003 Exiting..... 252===================== Chaincode is installed on peer0.org2 ===================== 253 254Instantiating chaincode on peer0.org2... 255CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 256CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 257CORE_PEER_LOCALMSPID=Org2MSP 258CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 259CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 260CORE_PEER_TLS_ENABLED=true 261CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 262CORE_PEER_ID=cli 263CORE_LOGGING_LEVEL=INFO 264CORE_PEER_ADDRESS=peer0.org2.example.com:7051 265+ peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C firstchannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'OR ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')' 266+ res=0 267+ set +x 2682020-01-13 14:53:08.146 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2692020-01-13 14:53:08.146 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2702020-01-13 14:53:28.997 UTC [main] main -> INFO 003 Exiting..... 271===================== Chaincode Instantiation on peer0.org2 on channel 'firstchannel' is successful ===================== 272 273Querying chaincode on peer0.org1... 274CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 275CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 276CORE_PEER_LOCALMSPID=Org1MSP 277CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 278CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 279CORE_PEER_TLS_ENABLED=true 280CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 281CORE_PEER_ID=cli 282CORE_LOGGING_LEVEL=INFO 283CORE_PEER_ADDRESS=peer0.org1.example.com:7051 284===================== Querying on peer0.org1 on channel 'firstchannel'... ===================== 285Attempting to Query peer0.org1 ...3 secs 286+ peer chaincode query -C firstchannel -n mycc -c '{"Args":["query","a"]}' 287+ res=0 288+ set +x 289 2902020-01-13 14:53:32.179 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2912020-01-13 14:53:32.180 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 292Query Result: 100 2932020-01-13 14:53:51.935 UTC [main] main -> INFO 003 Exiting..... 294===================== Query on peer0.org1 on channel 'firstchannel' is successful ===================== 295Sending invoke transaction on peer0.org1... 296CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 297CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 298CORE_PEER_LOCALMSPID=Org1MSP 299CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 300CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 301CORE_PEER_TLS_ENABLED=true 302CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 303CORE_PEER_ID=cli 304CORE_LOGGING_LEVEL=INFO 305CORE_PEER_ADDRESS=peer0.org1.example.com:7051 306+ peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C firstchannel -n mycc -c '{"Args":["invoke","a","b","10"]}' 307+ res=0 308+ set +x 3092020-01-13 14:53:52.132 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 3102020-01-13 14:53:52.133 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 3112020-01-13 14:53:52.150 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 003 Chaincode invoke successful. result: status:200 3122020-01-13 14:53:52.150 UTC [main] main -> INFO 004 Exiting..... 313===================== Invoke transaction on peer0.org1 on channel 'firstchannel' is successful ===================== 314 315Installing chaincode on peer1.org2... 316CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 317CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 318CORE_PEER_LOCALMSPID=Org2MSP 319CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 320CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 321CORE_PEER_TLS_ENABLED=true 322CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 323CORE_PEER_ID=cli 324CORE_LOGGING_LEVEL=INFO 325CORE_PEER_ADDRESS=peer1.org2.example.com:7051 326+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/ 327+ res=0 328+ set +x 3292020-01-13 14:53:52.296 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 3302020-01-13 14:53:52.297 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 3312020-01-13 14:53:52.665 UTC [main] main -> INFO 003 Exiting..... 332===================== Chaincode is installed on peer1.org2 ===================== 333 334Querying chaincode on peer1.org2... 335CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 336CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key 337CORE_PEER_LOCALMSPID=Org2MSP 338CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock 339CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt 340CORE_PEER_TLS_ENABLED=true 341CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 342CORE_PEER_ID=cli 343CORE_LOGGING_LEVEL=INFO 344CORE_PEER_ADDRESS=peer1.org2.example.com:7051 345===================== Querying on peer1.org2 on channel 'firstchannel'... ===================== 346Attempting to Query peer1.org2 ...3 secs 347+ peer chaincode query -C firstchannel -n mycc -c '{"Args":["query","a"]}' 348+ res=0 349+ set +x 350 3512020-01-13 14:53:55.859 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 3522020-01-13 14:53:55.860 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 353Query Result: 90 3542020-01-13 14:54:15.800 UTC [main] main -> INFO 003 Exiting..... 355===================== Query on peer1.org2 on channel 'firstchannel' is successful ===================== 356 357========= All GOOD, BYFN execution completed =========== 358 359 360 _____ _ _ ____ 361| ____| | \ | | | _ \ 362| _| | \| | | | | | 363| |___ | |\ | | |_| | 364|_____| |_| \_| |____/ 365 366[root@localhost first-network]#
看到"END",表示网络启动成功。
六、byfn.sh脚本启动网络过程
打开byfn.sh脚本,到文件最后
1#Create the network using docker compose 2if [ "${MODE}" == "up" ]; then 3 networkUp 4elif [ "${MODE}" == "down" ]; then ## Clear the network 5 networkDown 6elif [ "${MODE}" == "generate" ]; then ## Generate Artifacts 7 generateCerts 8 replacePrivateKey 9 generateChannelArtifacts 10elif [ "${MODE}" == "restart" ]; then ## Restart the network networkDown networkUp elif [ "${MODE}" == "upgrade" ]; then ## Upgrade the network from v1.0.x to v1.1 upgradeNetwork else printHelp exit 1 fi
可以看到up参数对应执行networkUp函数,搜索networkUp
1# Generate the needed certificates, the genesis block and start the network. 2function networkUp () { 3 checkPrereqs 4 # generate artifacts if they don't exist 5 if [ ! -d "crypto-config" ]; then 6 generateCerts 7 replacePrivateKey 8 generateChannelArtifacts 9 fi 10 if [ "${IF_COUCHDB}" == "couchdb" ]; then 11 IMAGE_TAG=$IMAGETAG docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_COUCH up -d 2>&1 12 else IMAGE_TAG=$IMAGETAG docker-compose -f $COMPOSE_FILE up -d 2>&1 fi if [ $? -ne 0 ]; then echo "ERROR !!!! Unable to start network" exit 1 fi # now run the end to end script docker exec cli scripts/script.sh $CHANNEL_NAME $CLI_DELAY $LANGUAGE $CLI_TIMEOUT if [ $? -ne 0 ]; then echo "ERROR !!!! Test failed" exit 1 fi } 13 14cryptogen generate --config=./crypto-config.yaml
1、generateCert函数里里根据crypto-config.yaml配置文件生成相关节点CA证书以及私钥到crypto-config目录下
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
2、根据configtx.yaml配置文件里的TwoOrgsOrdererGenesis配置生成order节点的创世区块:channel-artifacts/genesis.block
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
3、根据configtx.yaml配置文件里的TwoOrgsChannel配置生成指定通道的配置文件 :./channel-artifacts/channel.tx
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
4、根据configtx.yaml配置文件里的TwoOrgsChannel配置生成Org1的指定通道的锚节点配置文件 :./channel-artifacts/Org1MSPanchors.tx
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
5、根据configtx.yaml配置文件里的TwoOrgsChannel配置生成Org2的指定通道的锚节点配置文件 :./channel-artifacts/Org2MSPanchors.tx
1-s <dbtype> - the database backend to use: goleveldb (default) or couchdb"COMPOSE_FILE=docker-compose-cli.yaml 2COMPOSE_FILE_COUCH=docker-compose-couch.yaml 3if [ "${IF_COUCHDB}" == "couchdb" ]; then 4 IMAGE_TAG=$IMAGETAG docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_COUCH up -d 2>&1 5 else 6 IMAGE_TAG=$IMAGETAG docker-compose -f $COMPOSE_FILE up -d 2>&1 7 fi
6、-s数据库类型参数,默认为leveldb,可以指定couchdb,执行docker-compose -f docker-compose-cli.yaml -f docker-compose-couch.yaml
启动orderer,4个peer,1个 cli节点容器。
docker exec cli scripts/script.sh $CHANNEL_NAME $CLI_DELAY $LANGUAGE $CLI_TIMEOUT
7、进入cli节点容器中,执行scripts/script.sh脚本文件,打开scripts/script.sh,首先创建通道
1# import utils 2. scripts/utils.sh 3 4createChannel() { 5 setGlobals 0 1 6 7 if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then 8 set -x 9 peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt 10 res=$? 11 set +x 12 else 13 set -x 14 peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt 15 res=$? 16 set +x 17 fi 18 cat log.txt 19 verifyResult $res "Channel creation failed" 20 echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== " 21 echo 22} 23 24setGlobals 0 1 25 26是utils.sh里的一个方法,参数为节点id、组织id,该方法用于设置指定组织ID和节点ID的相关环境变量 27 28setGlobals () { 29 PEER=$1 30 ORG=$2 31 if [ $ORG -eq 1 ] ; then 32 CORE_PEER_LOCALMSPID="Org1MSP" 33 CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 34 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 35 if [ $PEER -eq 0 ]; then 36 CORE_PEER_ADDRESS=peer0.org1.example.com:7051 37 else 38 CORE_PEER_ADDRESS=peer1.org1.example.com:7051 39 fi 40 elif [ $ORG -eq 2 ] ; then 41 CORE_PEER_LOCALMSPID="Org2MSP" 42 CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 43 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 44 if [ $PEER -eq 0 ]; then 45 CORE_PEER_ADDRESS=peer0.org2.example.com:7051 46 else 47 CORE_PEER_ADDRESS=peer1.org2.example.com:7051 48 fi 49 50 elif [ $ORG -eq 3 ] ; then 51 CORE_PEER_LOCALMSPID="Org3MSP" 52 CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt 53 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp 54 if [ $PEER -eq 0 ]; then 55 CORE_PEER_ADDRESS=peer0.org3.example.com:7051 56 else 57 CORE_PEER_ADDRESS=peer1.org3.example.com:7051 58 fi 59 else 60 echo "================== ERROR !!! ORG Unknown ==================" 61 fi 62 63 env |grep CORE 64} 65 66peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt 67 688、根据./channel-artifacts/channel.tx配置文件创建指定通道的.block区块文件。 69 70joinChannel () { 71 for org in 1 2; do 72 for peer in 0 1; do 73 joinChannelWithRetry $peer $org 74 echo "===================== peer${peer}.org${org} joined on the channel \"$CHANNEL_NAME\" ===================== " 75 sleep $DELAY 76 echo 77 done 78 done 79}
9、调用utils.shi的joinChannelWithRetry $peer $org方法将所有的peer节点加入通道
1joinChannelWithRetry () { 2 PEER=$1 3 ORG=$2 4 setGlobals $PEER $ORG 5 6 set -x 7 peer channel join -b $CHANNEL_NAME.block >&log.txt 8 res=$? 9 set +x 10 cat log.txt 11 if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then 12 COUNTER=` expr $COUNTER + 1` 13 echo "peer${PEER}.org${ORG} failed to join the channel, Retry after $DELAY seconds" 14 sleep $DELAY 15 joinChannelWithRetry $PEER $ORG 16 else 17 COUNTER=1 18 fi 19 verifyResult $res "After $MAX_RETRY attempts, peer${PEER}.org${ORG} has failed to Join the Channel" 20}
10、更新组织中各个锚节点配置
1## Set the anchor peers for each org in the channel 2echo "Updating anchor peers for org1..." 3updateAnchorPeers 0 1 4echo "Updating anchor peers for org2..." 5updateAnchorPeers 0 2 6 7updateAnchorPeers() { 8 PEER=$1 9 ORG=$2 10 setGlobals $PEER $ORG 11 12 if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then 13 set -x 14 peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt 15 res=$? 16 set +x 17 else 18 set -x 19 peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt 20 res=$? 21 set +x 22 fi 23 cat log.txt 24 verifyResult $res "Anchor peer update failed" 25 echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== " 26 sleep $DELAY 27
11、在Org1和Org2的peer0节点安装链码
1## Install chaincode on peer0.org1 and peer0.org2 2echo "Installing chaincode on peer0.org1..." 3installChaincode 0 1 4echo "Install chaincode on peer0.org2..." 5installChaincode 0 2 6 7installChaincode () { 8 PEER=$1 9 ORG=$2 10 setGlobals $PEER $ORG 11 VERSION=${3:-1.0} 12 set -x 13 peer chaincode install -n mycc -v ${VERSION} -l ${LANGUAGE} -p ${CC_SRC_PATH} >&log.txt 14 res=$? 15 set +x 16 cat log.txt 17 verifyResult $res "Chaincode installation on peer${PEER}.org${ORG} has Failed" 18 echo "===================== Chaincode is installed on peer${PEER}.org${ORG} ===================== " 19 echo 20}
12、在Org2的peer0节初始化链码
1# Instantiate chaincode on peer0.org2 2echo "Instantiating chaincode on peer0.org2..." 3instantiateChaincode 0 2 4 5instantiateChaincode () { 6 PEER=$1 7 ORG=$2 8 setGlobals $PEER $ORG 9 VERSION=${3:-1.0} 10 11 # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful), 12 # lets supply it directly as we know it using the "-o" option 13 if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then 14 set -x 15 peer chaincode instantiate -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -l ${LANGUAGE} -v ${VERSION} -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')" >&log.txt 16 res=$? 17 set +x 18 else 19 set -x 20 peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -l ${LANGUAGE} -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')" >&log.txt 21 res=$? 22 set +x 23 fi 24 cat log.txt 25 verifyResult $res "Chaincode instantiation on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' failed" 26 echo "===================== Chaincode Instantiation on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' is successful ===================== " 27 echo 28}
13、在Org1的peer0节点调用链码的查询方法查询账户a余额
1# Query chaincode on peer0.org1 2echo "Querying chaincode on peer0.org1..." 3chaincodeQuery 0 1 100 4 5chaincodeQuery () { 6 PEER=$1 7 ORG=$2 8 setGlobals $PEER $ORG 9 EXPECTED_RESULT=$3 10 echo "===================== Querying on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME'... ===================== " 11 local rc=1 12 local starttime=$(date +%s) 13 14 # continue to poll 15 # we either get a successful response, or reach TIMEOUT 16 while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0 17 do 18 sleep $DELAY 19 echo "Attempting to Query peer${PEER}.org${ORG} ...$(($(date +%s)-starttime)) secs" 20 set -x 21 peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt 22 res=$? 23 set +x 24 test $res -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}') 25 test "$VALUE" = "$EXPECTED_RESULT" && let rc=0 26 done 27 echo 28 cat log.txt 29 if test $rc -eq 0 ; then 30 echo "===================== Query on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' is successful ===================== " 31 else 32 echo "!!!!!!!!!!!!!!! Query result on peer${PEER}.org${ORG} is INVALID !!!!!!!!!!!!!!!!" 33 echo "================== ERROR !!! FAILED to execute End-2-End Scenario ==================" 34 echo 35 exit 1 36 fi 37}
14、在Org1的peer0节点调用链码方法:a向b转账10
1# Invoke chaincode on peer0.org1 2echo "Sending invoke transaction on peer0.org1..." 3chaincodeInvoke 0 1 4 5chaincodeInvoke () { 6 PEER=$1 7 ORG=$2 8 setGlobals $PEER $ORG 9 # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful), 10 # lets supply it directly as we know it using the "-o" option 11 if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then 12 set -x 13 peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt 14 res=$? 15 set +x 16 else 17 set -x 18 peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt 19 res=$? 20 set +x 21 fi 22 cat log.txt 23 verifyResult $res "Invoke execution on peer${PEER}.org${ORG} failed " 24 echo "===================== Invoke transaction on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' is successful ===================== " 25 echo 26}
15、在Org2的peer1节点安装链码
1## Install chaincode on peer1.org2 2echo "Installing chaincode on peer1.org2..." 3installChaincode 1 2
16、在Org2的peer1节点执行链码查询方法查询a账户余额
1# Query on chaincode on peer1.org2, check if the result is 90 2echo "Querying chaincode on peer1.org2..." 3chaincodeQuery 1 2 90