Hyperledger Fabric v1.1.0环境搭建

牧云@^-^@ 提交于 2020-11-25 08:02:51

一、搭建基础环境

参考:https://www.cnblogs.com/sky-cheng/p/12150822.html

二、下载Hyperledger Fabric 源代码

查看$GOPATH环境变量

[root@localhost home]# echo $GOPATH
/home/go

新建如下目录

[root@localhost home]# mkdir -p /home/go/src/github.com/hyperledger
[root@localhost home]# 

进入新建的目录,下载Fabric源代码

[root@localhost home]# cd /home/go/src/github.com/hyperledger/
[root@localhost hyperledger]# git clone https://github.com/hyperledger/fabric.git
正克隆到 'fabric'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 125935 (delta 2), reused 1 (delta 1), pack-reused 125923
接收对象中: 100% (125935/125935), 101.50 MiB | 124.00 KiB/s, done.
处理 delta 中: 100% (85366/85366), done.
[root@localhost hyperledger]# 

进入源代码目录,用git checkout 切换到V1.1.0分支,用git status 当前分支,用git tag查看所有分支

[root@localhost fabric]# cd fabric/
[root@localhost fabric]# git checkout v1.1.0
Note: checking out 'v1.1.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD 目前位于 523f644... [FAB-8883] Prepare for Fabric v1.1.0 release
[root@localhost fabric]# 
[root@localhost fabric]# git status
# 头指针分离于 v1.1.0
无文件要提交,干净的工作区

进入scripts目录,bootstrap.sh脚本的作用是下载fabric相关镜像文件如:peer orderer ccenv javaenv,以及kafka、zookeeper、couchdb第三方镜像,以及下载一些工具文件用于辅助生成证书、秘钥、以及各种相关配置文件等,打开bootstrap.sh脚本

# current version of fabric released
export VERSION=${1:-1.0.4}

这里设置了版本号的VERSION环境变量,我们修改成1.1.0版本,保存退出

# current version of fabric released
export VERSION=${1:-1.1.0}

三、运行bootstrap.sh脚本

[root@localhost scripts]# ./bootstrap.sh 
===> Downloading platform specific fabric binaries
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 35.4M  100 35.4M    0     0  19040      0  0:32:30  0:32:30 --:--:-- 23981
===> Downloading platform specific fabric-ca-client binary
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 6109k  100 6109k    0     0  16023      0  0:06:30  0:06:30 --:--:-- 21829
===> Pulling fabric Images
==> FABRIC IMAGE: peer

x86_64-1.1.0: Pulling from hyperledger/fabric-peer
Digest: sha256:57417699ddf50c5ebd47a9a2cc74c0324fbba0281eb1104b9ddd05a67776b01f
Status: Image is up to date for hyperledger/fabric-peer:x86_64-1.1.0
docker.io/hyperledger/fabric-peer:x86_64-1.1.0
==> FABRIC IMAGE: orderer

x86_64-1.1.0: Pulling from hyperledger/fabric-orderer
Digest: sha256:0c3a3b5ecfd24b513da22bbb77da7b3f5bca9c121cc0ac5c46ba04c97c163654
Status: Image is up to date for hyperledger/fabric-orderer:x86_64-1.1.0
docker.io/hyperledger/fabric-orderer:x86_64-1.1.0
==> FABRIC IMAGE: ccenv

x86_64-1.1.0: Pulling from hyperledger/fabric-ccenv
Digest: sha256:07818367dc6d4264472d24b21819f9dc4e16e890d81ddfacee0341a22d72050b
Status: Image is up to date for hyperledger/fabric-ccenv:x86_64-1.1.0
docker.io/hyperledger/fabric-ccenv:x86_64-1.1.0
==> FABRIC IMAGE: javaenv

x86_64-1.1.0: Pulling from hyperledger/fabric-javaenv
Digest: sha256:d2588c0556b6fc79131f638b02a7a77337363e2c2f38a9c47798a6d99bd2f20e
Status: Image is up to date for hyperledger/fabric-javaenv:x86_64-1.1.0
docker.io/hyperledger/fabric-javaenv:x86_64-1.1.0
==> FABRIC IMAGE: tools

x86_64-1.1.0: Pulling from hyperledger/fabric-tools
Digest: sha256:36d7fa8e8ddcc19fed8e1c3c06bc6ae1dac18c35e8a884188d2c08df3e5a4472
Status: Image is up to date for hyperledger/fabric-tools:x86_64-1.1.0
docker.io/hyperledger/fabric-tools:x86_64-1.1.0
===> Pulling fabric ca Image
==> FABRIC CA IMAGE

x86_64-1.1.0: Pulling from hyperledger/fabric-ca
Digest: sha256:92f44d0811cddb0d335f7879f7e3b3c4b631f31740c76f3e7b85438c244b03f4
Status: Image is up to date for hyperledger/fabric-ca:x86_64-1.1.0
docker.io/hyperledger/fabric-ca:x86_64-1.1.0
===> Pulling thirdparty docker images
==> THIRDPARTY DOCKER IMAGE: couchdb

x86_64-0.4.6: Pulling from hyperledger/fabric-couchdb
Digest: sha256:4278269b115cfd0f24251b5381407be9ccdf396c1470c69e1ee2ff16917ac882
Status: Image is up to date for hyperledger/fabric-couchdb:x86_64-0.4.6
docker.io/hyperledger/fabric-couchdb:x86_64-0.4.6
==> THIRDPARTY DOCKER IMAGE: kafka

x86_64-0.4.6: Pulling from hyperledger/fabric-kafka
Digest: sha256:738bb79f02aa505a9ab0553a865122605ffba91d1b5de6eb0429f7287e83ac7c
Status: Image is up to date for hyperledger/fabric-kafka:x86_64-0.4.6
docker.io/hyperledger/fabric-kafka:x86_64-0.4.6
==> THIRDPARTY DOCKER IMAGE: zookeeper

x86_64-0.4.6: Pulling from hyperledger/fabric-zookeeper
Digest: sha256:7fda667f6c39d6cb87125c132962fc3e434a8e6b2b16ee798d6fa94f970531a2
Status: Image is up to date for hyperledger/fabric-zookeeper:x86_64-0.4.6
docker.io/hyperledger/fabric-zookeeper:x86_64-0.4.6

===> List out hyperledger docker images
hyperledger/fabric-ca          latest              72617b4fa9b4        22 months ago       299MB
hyperledger/fabric-ca          x86_64-1.1.0        72617b4fa9b4        22 months ago       299MB
hyperledger/fabric-tools       latest              b7bfddf508bc        22 months ago       1.46GB
hyperledger/fabric-tools       x86_64-1.1.0        b7bfddf508bc        22 months ago       1.46GB
hyperledger/fabric-orderer     latest              ce0c810df36a        22 months ago       180MB
hyperledger/fabric-orderer     x86_64-1.1.0        ce0c810df36a        22 months ago       180MB
hyperledger/fabric-peer        latest              b023f9be0771        22 months ago       187MB
hyperledger/fabric-peer        x86_64-1.1.0        b023f9be0771        22 months ago       187MB
hyperledger/fabric-javaenv     latest              82098abb1a17        22 months ago       1.52GB
hyperledger/fabric-javaenv     x86_64-1.1.0        82098abb1a17        22 months ago       1.52GB
hyperledger/fabric-ccenv       latest              c8b4909d8d46        22 months ago       1.39GB
hyperledger/fabric-ccenv       x86_64-1.1.0        c8b4909d8d46        22 months ago       1.39GB
hyperledger/fabric-zookeeper   latest              92cbb952b6f8        23 months ago       1.39GB
hyperledger/fabric-zookeeper   x86_64-0.4.6        92cbb952b6f8        23 months ago       1.39GB
hyperledger/fabric-kafka       latest              554c591b86a8        23 months ago       1.4GB
hyperledger/fabric-kafka       x86_64-0.4.6        554c591b86a8        23 months ago       1.4GB
hyperledger/fabric-couchdb     latest              7e73c828fc5b        23 months ago       1.56GB
hyperledger/fabric-couchdb     x86_64-0.4.6        7e73c828fc5b        23 months ago       1.56GB
[root@localhost scripts]# 

首先下载相关工具二进制文件,其次再拉取v1.1.0相关版本的镜像文件。二进制文件保存在scripts/bin 目录里,把它移动到/home/go/src/github.com/hyperledger/下,并设置好PATH变量

[root@localhost scripts]# mv bin/ /home/go/src/github.com/hyperledger/fabric
[root@localhost bin]# echo "export PATH=$PATH:/home/go/src/github.com/hyperledger/fabric/bin">>/etc/profile
[root@localhost bin]# source /etc/profile
[root@localhost bin]# echo $PATH
/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
[root@localhost bin]# 

四、下载fabric-sample项目源代码 

[root@localhost hyperledger]# cd /home/go/src/github.com/hyperledger
[root@localhost hyperledger]# git clone https://github.com/hyperledger/fabric-samples.git
正克隆到 'fabric-samples'...
remote: Enumerating objects: 4430, done.
remote: Total 4430 (delta 0), reused 0 (delta 0), pack-reused 4430
接收对象中: 100% (4430/4430), 1.61 MiB | 8.00 KiB/s, done.
处理 delta 中: 100% (2181/2181), done.
[root@localhost hyperledger]# ll
总用量 8
drwxrwxr-x  2 mysql mysql  142 3月  16 2018 bin
drwxr-xr-x 27 root  root  4096 1月  13 17:03 fabric
drwxr-xr-x 16 root  root  4096 1月  13 22:32 fabric-samples
[root@localhost hyperledger]# 

切换到v1.1.0版本

[root@localhost hyperledger]# cd fabric-samples/
[root@localhost fabric-samples]# git checkout v1.1.0
Note: checking out 'v1.1.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD 目前位于 1252c7a... [FAB-8920] Pin fabric-samples to node.js "~1.1.0"
[root@localhost fabric-samples]# git status
# 头指针分离于 v1.1.0
无文件要提交,干净的工作区
[root@localhost fabric-samples]#

五、启动网络

进入first-network目录

root@localhost fabric-samples]# cd first-network/
[root@localhost first-network]# ll
总用量 72
drwxr-xr-x 2 root root    60 1月  13 22:34 base
-rwxr-xr-x 1 root root 20114 1月  13 22:34 byfn.sh
drwxr-xr-x 2 root root    22 1月  13 22:32 channel-artifacts
-rw-r--r-- 1 root root  7912 1月  13 22:34 configtx.yaml
-rw-r--r-- 1 root root  3906 1月  13 22:34 crypto-config.yaml
-rw-r--r-- 1 root root  2971 1月  13 22:34 docker-compose-cli.yaml
-rw-r--r-- 1 root root  2345 1月  13 22:34 docker-compose-couch-org3.yaml
-rw-r--r-- 1 root root  4560 1月  13 22:34 docker-compose-couch.yaml
-rw-r--r-- 1 root root  2883 1月  13 22:32 docker-compose-e2e-template.yaml
-rw-r--r-- 1 root root  3493 1月  13 22:34 docker-compose-org3.yaml
-rwxr-xr-x 1 root root 10627 1月  13 22:34 eyfn.sh
drwxr-xr-x 2 root root    51 1月  13 22:34 org3-artifacts
-rw-r--r-- 1 root root   335 1月  13 22:32 README.md
drwxr-xr-x 2 root root   168 1月  13 22:34 scripts

运行byfn.sh脚本,启动网络

[root@localhost first-network]# ./byfn.sh -m up -c firstchannel
Starting with channel 'firstchannel' and CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
2020-01-13 14:52:39.329 UTC [main] main -> INFO 001 Exiting.....
LOCAL_VERSION=1.1.0
DOCKER_IMAGE_VERSION=1.1.0
/home/go/src/github.com/hyperledger/bin/cryptogen

##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x

/home/go/src/github.com/hyperledger/bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
+ configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
2020-01-13 22:52:39.977 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2020-01-13 22:52:40.002 CST [msp] getMspConfig -> INFO 002 Loading NodeOUs
2020-01-13 22:52:40.003 CST [msp] getMspConfig -> INFO 003 Loading NodeOUs
2020-01-13 22:52:40.003 CST [common/tools/configtxgen] doOutputBlock -> INFO 004 Generating genesis block
2020-01-13 22:52:40.004 CST [common/tools/configtxgen] doOutputBlock -> INFO 005 Writing genesis block
+ res=0
+ set +x

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID firstchannel
2020-01-13 22:52:40.031 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2020-01-13 22:52:40.054 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx
2020-01-13 22:52:40.055 CST [msp] getMspConfig -> INFO 003 Loading NodeOUs
2020-01-13 22:52:40.056 CST [msp] getMspConfig -> INFO 004 Loading NodeOUs
2020-01-13 22:52:40.143 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 005 Writing new channel tx
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org1MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID firstchannel -asOrg Org1MSP
2020-01-13 22:52:40.170 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2020-01-13 22:52:40.193 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2020-01-13 22:52:40.193 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org2MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID firstchannel -asOrg Org2MSP
2020-01-13 22:52:40.220 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2020-01-13 22:52:40.243 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2020-01-13 22:52:40.244 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
+ res=0
+ set +x

Creating network "net_byfn" with the default driver
Creating volume "net_orderer.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_peer1.org1.example.com" with default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating volume "net_peer1.org2.example.com" with default driver
Creating peer1.org2.example.com ... done
Creating peer1.org1.example.com ... done
Creating orderer.example.com    ... done
Creating peer0.org1.example.com ... done
Creating peer0.org2.example.com ... done
Creating cli                    ... done

 ____    _____      _      ____    _____ 
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |  
 ___) |   | |    / ___ \  |  _ <    | |  
|____/    |_|   /_/   \_\ |_| \_\   |_|  

Build your first network (BYFN) end-to-end test

Channel name : firstchannel
Creating channel...
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
CORE_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
CORE_PEER_LOCALMSPID=Org1MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
+ 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
+ res=0
+ set +x
2020-01-13 14:52:47.158 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-01-13 14:52:47.226 UTC [channelCmd] InitCmdFactory -> INFO 002 Endorser and orderer connections initialized
2020-01-13 14:52:47.432 UTC [main] main -> INFO 003 Exiting.....
===================== Channel "firstchannel" is created successfully ===================== 

Having all peers join the channel...
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
CORE_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
CORE_PEER_LOCALMSPID=Org1MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
+ peer channel join -b firstchannel.block
+ res=0
+ set +x
2020-01-13 14:52:47.586 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-01-13 14:52:47.747 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2020-01-13 14:52:47.747 UTC [main] main -> INFO 003 Exiting.....
===================== peer0.org1 joined on the channel "firstchannel" ===================== 

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
CORE_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
CORE_PEER_LOCALMSPID=Org1MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer1.org1.example.com:7051
+ peer channel join -b firstchannel.block
+ res=0
+ set +x
2020-01-13 14:52:50.900 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-01-13 14:52:51.065 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2020-01-13 14:52:51.065 UTC [main] main -> INFO 003 Exiting.....
===================== peer1.org1 joined on the channel "firstchannel" ===================== 

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
CORE_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
CORE_PEER_LOCALMSPID=Org2MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
+ peer channel join -b firstchannel.block
+ res=0
+ set +x
2020-01-13 14:52:54.200 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-01-13 14:52:54.367 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2020-01-13 14:52:54.367 UTC [main] main -> INFO 003 Exiting.....
===================== peer0.org2 joined on the channel "firstchannel" ===================== 

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
CORE_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
CORE_PEER_LOCALMSPID=Org2MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer1.org2.example.com:7051
+ peer channel join -b firstchannel.block
+ res=0
+ set +x
2020-01-13 14:52:57.503 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-01-13 14:52:57.680 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
2020-01-13 14:52:57.680 UTC [main] main -> INFO 003 Exiting.....
===================== peer1.org2 joined on the channel "firstchannel" ===================== 

Updating anchor peers for org1...
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
CORE_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
CORE_PEER_LOCALMSPID=Org1MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
+ 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
+ res=0
+ set +x
2020-01-13 14:53:00.815 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-01-13 14:53:00.848 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
2020-01-13 14:53:00.848 UTC [main] main -> INFO 003 Exiting.....
===================== Anchor peers for org "Org1MSP" on "firstchannel" is updated successfully ===================== 

Updating anchor peers for org2...
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
CORE_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
CORE_PEER_LOCALMSPID=Org2MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
+ 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
+ res=0
+ set +x
2020-01-13 14:53:03.986 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2020-01-13 14:53:04.016 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
2020-01-13 14:53:04.016 UTC [main] main -> INFO 003 Exiting.....
===================== Anchor peers for org "Org2MSP" on "firstchannel" is updated successfully ===================== 

Installing chaincode on peer0.org1...
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
CORE_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
CORE_PEER_LOCALMSPID=Org1MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2020-01-13 14:53:07.168 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-01-13 14:53:07.168 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2020-01-13 14:53:07.508 UTC [main] main -> INFO 003 Exiting.....
===================== Chaincode is installed on peer0.org1 ===================== 

Install chaincode on peer0.org2...
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
CORE_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
CORE_PEER_LOCALMSPID=Org2MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2020-01-13 14:53:07.645 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-01-13 14:53:07.646 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2020-01-13 14:53:07.999 UTC [main] main -> INFO 003 Exiting.....
===================== Chaincode is installed on peer0.org2 ===================== 

Instantiating chaincode on peer0.org2...
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
CORE_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
CORE_PEER_LOCALMSPID=Org2MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
+ 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'\'')'
+ res=0
+ set +x
2020-01-13 14:53:08.146 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-01-13 14:53:08.146 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2020-01-13 14:53:28.997 UTC [main] main -> INFO 003 Exiting.....
===================== Chaincode Instantiation on peer0.org2 on channel 'firstchannel' is successful ===================== 

Querying chaincode on peer0.org1...
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
CORE_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
CORE_PEER_LOCALMSPID=Org1MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
===================== Querying on peer0.org1 on channel 'firstchannel'... ===================== 
Attempting to Query peer0.org1 ...3 secs
+ peer chaincode query -C firstchannel -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x

2020-01-13 14:53:32.179 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-01-13 14:53:32.180 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 100
2020-01-13 14:53:51.935 UTC [main] main -> INFO 003 Exiting.....
===================== Query on peer0.org1 on channel 'firstchannel' is successful ===================== 
Sending invoke transaction on peer0.org1...
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
CORE_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
CORE_PEER_LOCALMSPID=Org1MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
+ 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"]}'
+ res=0
+ set +x
2020-01-13 14:53:52.132 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-01-13 14:53:52.133 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2020-01-13 14:53:52.150 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 003 Chaincode invoke successful. result: status:200 
2020-01-13 14:53:52.150 UTC [main] main -> INFO 004 Exiting.....
===================== Invoke transaction on peer0.org1 on channel 'firstchannel' is successful ===================== 

Installing chaincode on peer1.org2...
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
CORE_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
CORE_PEER_LOCALMSPID=Org2MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer1.org2.example.com:7051
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2020-01-13 14:53:52.296 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-01-13 14:53:52.297 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2020-01-13 14:53:52.665 UTC [main] main -> INFO 003 Exiting.....
===================== Chaincode is installed on peer1.org2 ===================== 

Querying chaincode on peer1.org2...
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
CORE_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
CORE_PEER_LOCALMSPID=Org2MSP
CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
CORE_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
CORE_PEER_TLS_ENABLED=true
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer1.org2.example.com:7051
===================== Querying on peer1.org2 on channel 'firstchannel'... ===================== 
Attempting to Query peer1.org2 ...3 secs
+ peer chaincode query -C firstchannel -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x

2020-01-13 14:53:55.859 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2020-01-13 14:53:55.860 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 90
2020-01-13 14:54:15.800 UTC [main] main -> INFO 003 Exiting.....
===================== Query on peer1.org2 on channel 'firstchannel' is successful ===================== 

========= All GOOD, BYFN execution completed =========== 


 _____   _   _   ____   
| ____| | \ | | |  _ \  
|  _|   |  \| | | | | | 
| |___  | |\  | | |_| | 
|_____| |_| \_| |____/  

[root@localhost first-network]#

看到"END",表示网络启动成功。

六、byfn.sh脚本启动网络过程

打开byfn.sh脚本,到文件最后

#Create the network using docker compose
if [ "${MODE}" == "up" ]; then
  networkUp
elif [ "${MODE}" == "down" ]; then ## Clear the network
  networkDown
elif [ "${MODE}" == "generate" ]; then ## Generate Artifacts
  generateCerts
  replacePrivateKey
  generateChannelArtifacts
elif [ "${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

# Generate the needed certificates, the genesis block and start the network.
function networkUp () {
  checkPrereqs
  # generate artifacts if they don't exist
  if [ ! -d "crypto-config" ]; then
    generateCerts
    replacePrivateKey
    generateChannelArtifacts
  fi
  if [ "${IF_COUCHDB}" == "couchdb" ]; then
    IMAGE_TAG=$IMAGETAG docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_COUCH up -d 2>&1
  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 }
cryptogen 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

-s <dbtype> - the database backend to use: goleveldb (default) or couchdb"
COMPOSE_FILE=docker-compose-cli.yaml COMPOSE_FILE_COUCH=docker-compose-couch.yaml if [ "${IF_COUCHDB}" == "couchdb" ]; then IMAGE_TAG=$IMAGETAG docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_COUCH up -d 2>&1 else IMAGE_TAG=$IMAGETAG docker-compose -f $COMPOSE_FILE up -d 2>&1 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,首先创建通道

# import utils
. scripts/utils.sh

createChannel() {
        setGlobals 0 1

        if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
                set -x
                peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt
                res=$?
                set +x
        else
                                set -x
                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
                res=$?
                                set +x
        fi
        cat log.txt
        verifyResult $res "Channel creation failed"
        echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== "
        echo
}
setGlobals 0 1
是utils.sh里的一个方法,参数为节点id、组织id,该方法用于设置指定组织ID和节点ID的相关环境变量
setGlobals () {
        PEER=$1
        ORG=$2
        if [ $ORG -eq 1 ] ; then
                CORE_PEER_LOCALMSPID="Org1MSP"
                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
                CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
                if [ $PEER -eq 0 ]; then
                        CORE_PEER_ADDRESS=peer0.org1.example.com:7051
                else
                        CORE_PEER_ADDRESS=peer1.org1.example.com:7051
                fi
        elif [ $ORG -eq 2 ] ; then
                CORE_PEER_LOCALMSPID="Org2MSP"
                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
                CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
                if [ $PEER -eq 0 ]; then
                        CORE_PEER_ADDRESS=peer0.org2.example.com:7051
                else
                        CORE_PEER_ADDRESS=peer1.org2.example.com:7051
                fi

        elif [ $ORG -eq 3 ] ; then
                CORE_PEER_LOCALMSPID="Org3MSP"
                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
                CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp
                if [ $PEER -eq 0 ]; then
                        CORE_PEER_ADDRESS=peer0.org3.example.com:7051
                else
                        CORE_PEER_ADDRESS=peer1.org3.example.com:7051
                fi
        else
                echo "================== ERROR !!! ORG Unknown =================="
        fi

        env |grep CORE
}
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
8、根据./channel-artifacts/channel.tx配置文件创建指定通道的.block区块文件。
joinChannel () {
        for org in 1 2; do
            for peer in 0 1; do
                joinChannelWithRetry $peer $org
                echo "===================== peer${peer}.org${org} joined on the channel \"$CHANNEL_NAME\" ===================== "
                sleep $DELAY
                echo
            done
        done
}

9、调用utils.shi的joinChannelWithRetry $peer $org方法将所有的peer节点加入通道

joinChannelWithRetry () {
        PEER=$1
        ORG=$2
        setGlobals $PEER $ORG

        set -x
        peer channel join -b $CHANNEL_NAME.block  >&log.txt
        res=$?
        set +x
        cat log.txt
        if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then
                COUNTER=` expr $COUNTER + 1`
                echo "peer${PEER}.org${ORG} failed to join the channel, Retry after $DELAY seconds"
                sleep $DELAY
                joinChannelWithRetry $PEER $ORG
        else
                COUNTER=1
        fi
        verifyResult $res "After $MAX_RETRY attempts, peer${PEER}.org${ORG} has failed to Join the Channel"
}

10、更新组织中各个锚节点配置

## Set the anchor peers for each org in the channel
echo "Updating anchor peers for org1..."
updateAnchorPeers 0 1
echo "Updating anchor peers for org2..."
updateAnchorPeers 0 2
updateAnchorPeers() {
  PEER=$1
  ORG=$2
  setGlobals $PEER $ORG

  if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
                set -x
                peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
                res=$?
                set +x
  else
                set -x
                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
                res=$?
                set +x
  fi
        cat log.txt
        verifyResult $res "Anchor peer update failed"
        echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== "
        sleep $DELAY
    

 

11、在Org1和Org2的peer0节点安装链码

## Install chaincode on peer0.org1 and peer0.org2
echo "Installing chaincode on peer0.org1..."
installChaincode 0 1
echo "Install chaincode on peer0.org2..."
installChaincode 0 2
installChaincode () {
        PEER=$1
        ORG=$2
        setGlobals $PEER $ORG
        VERSION=${3:-1.0}
        set -x
        peer chaincode install -n mycc -v ${VERSION} -l ${LANGUAGE} -p ${CC_SRC_PATH} >&log.txt
        res=$?
        set +x
        cat log.txt
        verifyResult $res "Chaincode installation on peer${PEER}.org${ORG} has Failed"
        echo "===================== Chaincode is installed on peer${PEER}.org${ORG} ===================== "
        echo
}

12、在Org2的peer0节初始化链码

# Instantiate chaincode on peer0.org2
echo "Instantiating chaincode on peer0.org2..."
instantiateChaincode 0 2
instantiateChaincode () {
        PEER=$1
        ORG=$2
        setGlobals $PEER $ORG
        VERSION=${3:-1.0}

        # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
        # lets supply it directly as we know it using the "-o" option
        if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
                set -x
                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
                res=$?
                set +x
        else
                set -x
                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
                res=$?
                set +x
        fi
        cat log.txt
        verifyResult $res "Chaincode instantiation on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' failed"
        echo "===================== Chaincode Instantiation on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' is successful ===================== "
        echo
}

13、在Org1的peer0节点调用链码的查询方法查询账户a余额

# Query chaincode on peer0.org1
echo "Querying chaincode on peer0.org1..."
chaincodeQuery 0 1 100
chaincodeQuery () {
  PEER=$1
  ORG=$2
  setGlobals $PEER $ORG
  EXPECTED_RESULT=$3
  echo "===================== Querying on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME'... ===================== "
  local rc=1
  local starttime=$(date +%s)

  # continue to poll
  # we either get a successful response, or reach TIMEOUT
  while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0
  do
     sleep $DELAY
     echo "Attempting to Query peer${PEER}.org${ORG} ...$(($(date +%s)-starttime)) secs"
     set -x
     peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt
         res=$?
     set +x
     test $res -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')
     test "$VALUE" = "$EXPECTED_RESULT" && let rc=0
  done
  echo
  cat log.txt
  if test $rc -eq 0 ; then
        echo "===================== Query on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' is successful ===================== "
  else
        echo "!!!!!!!!!!!!!!! Query result on peer${PEER}.org${ORG} is INVALID !!!!!!!!!!!!!!!!"
        echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
        echo
        exit 1
  fi
}

14、在Org1的peer0节点调用链码方法:a向b转账10

# Invoke chaincode on peer0.org1
echo "Sending invoke transaction on peer0.org1..."
chaincodeInvoke 0 1
chaincodeInvoke () {
        PEER=$1
        ORG=$2
        setGlobals $PEER $ORG
        # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
        # lets supply it directly as we know it using the "-o" option
        if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
                set -x
                peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
                res=$?
                set +x
        else
                set -x
                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
                res=$?
                set +x
        fi
        cat log.txt
        verifyResult $res "Invoke execution on peer${PEER}.org${ORG} failed "
        echo "===================== Invoke transaction on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' is successful ===================== "
        echo
}

15、在Org2的peer1节点安装链码

## Install chaincode on peer1.org2
echo "Installing chaincode on peer1.org2..."
installChaincode 1 2

16、在Org2的peer1节点执行链码查询方法查询a账户余额

# Query on chaincode on peer1.org2, check if the result is 90
echo "Querying chaincode on peer1.org2..."
chaincodeQuery 1 2 90

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!