Fabric学习(一)之搭建第一个Fabric网络

时间:2019.11.27.21.44

文章目录

    • 前提条件
    • 安装示例、二进制文件和 Docker 镜像
    • 搭建网络
      • 初始化网络
      • 启动网络
      • 关闭网络
      • 运行示例代码
      • 可能会出现的问题

参考地址: Fabric中文翻译网站

前提条件

1、安装 curl
2、安装 docker 和 docker compose
  版本查看

$ docker --version
$ docker-compose --version

3、安装go
  设置环境变量如下

$ vim ~/.bashrc
export GOPATH=/home/lqs/go
export PATH=$PATH:$GOPATH/bin

4、安装Python
  Fabric Node.js SDK需要使用 Python 2.7版本才能成功完成 npm install 操作,使用以下命令安装

$ sudo apt-get install python
$ python --version

安装示例、二进制文件和 Docker 镜像

1、克隆 hyperledger/fabric-samples 仓库
  进入网址克隆 hyperledger/fabric-samples 仓库

$ git clone https://github.com/hyperledger/fabric-samples

2、进入fabric-samples目录执行以下命令

$ cd fabric-samples

# Fetch bootstrap.sh from fabric repository using
$ curl -sS https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh -o ./scripts/bootstrap.sh
# Change file mode to executable
$ chmod +x ./scripts/bootstrap.sh
# Download binaries and docker images
//可只执行前半部分,不指定版本,系统会自动下载最新版本(该命令会下载很多镜像,耗时较长)
$ ./scripts/bootstrap.sh [version] [ca version] [thirdparty_version] 
$ ./scripts/bootstrap.sh 1.4.1 1.4.1 0.4.18 

  下载好之后再 fabric-samples 目录下会生成新的文件夹 binconfig
文件实例如下

$ tree bin/
bin
├── configtxgen
├── configtxlator
├── cryptogen
├── discover
├── fabric-ca-client
├── idemixgen
├── orderer
└── peer

$ tree config
config/
├── configtx.yaml
├── core.yaml
└── orderer.yaml

3、环境变量配置
  添加 bin 文件的路径到PATH里面

$ vim  ~/.bashrc
 export PATH=/home/lqs/fabric-samples/bin:$PATH

搭建网络

$ cd first-network

初始化网络

$ ./byfn.sh generate

Generating certs and genesis block for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y //选择y
proceeding ...
/home/lqs/fabric-samples/first-network/../bin/cryptogen

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

Generate CCP files for Org1 and Org2
/home/lqs/fabric-samples/first-network/../bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
CONSENSUS_TYPE=solo
+ '[' solo == solo ']'
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2019-11-27 06:10:21.110 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-11-27 06:10:21.447 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-11-27 06:10:21.451 PST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 06:10:21.759 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-11-27 06:10:21.759 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 06:10:21.767 PST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-11-27 06:10:21.768 PST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
+ res=0
+ set +x

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2019-11-27 06:10:21.880 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-11-27 06:10:22.187 PST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 06:10:22.477 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-11-27 06:10:22.478 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 06:10:22.478 PST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-11-27 06:10:22.479 PST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 006 Writing new channel tx
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org1MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
2019-11-27 06:10:22.575 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-11-27 06:10:22.865 PST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 06:10:23.135 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-11-27 06:10:23.135 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 06:10:23.135 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-11-27 06:10:23.140 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org2MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
2019-11-27 06:10:23.257 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-11-27 06:10:23.553 PST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 06:10:23.834 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-11-27 06:10:23.834 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 06:10:23.834 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-11-27 06:10:23.834 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

启动网络

$ ./byfn.sh up

  成功启动后运行代码如下,该代码展示了一个完整的网络创建过程

Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y  //选择y
proceeding ...
LOCAL_VERSION=1.4.4
DOCKER_IMAGE_VERSION=1.4.4
/home/lqs/fabric-samples/first-network/../bin/cryptogen

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

Generate CCP files for Org1 and Org2
/home/lqs/fabric-samples/first-network/../bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
CONSENSUS_TYPE=solo
+ '[' solo == solo ']'
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2019-11-27 05:35:57.221 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-11-27 05:35:57.379 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-11-27 05:35:57.379 PST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 05:35:57.531 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-11-27 05:35:57.531 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 05:35:57.533 PST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-11-27 05:35:57.534 PST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
+ res=0
+ set +x

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2019-11-27 05:35:57.588 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-11-27 05:35:57.755 PST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 05:35:57.932 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-11-27 05:35:57.933 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 05:35:57.933 PST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-11-27 05:35:57.937 PST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 006 Writing new channel tx
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org1MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
2019-11-27 05:35:57.992 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-11-27 05:35:58.160 PST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 05:35:58.307 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-11-27 05:35:58.307 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 05:35:58.307 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-11-27 05:35:58.308 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org2MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
2019-11-27 05:35:58.358 PST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-11-27 05:35:58.502 PST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 05:35:58.649 PST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-11-27 05:35:58.649 PST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /home/lqs/fabric-samples/first-network/configtx.yaml
2019-11-27 05:35:58.649 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-11-27 05:35:58.649 PST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

Creating network "net_byfn" with the default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating volume "net_peer1.org2.example.com" with default driver
Creating volume "net_peer1.org1.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_orderer.example.com" with default driver
Creating orderer.example.com ... 
Creating peer0.org2.example.com ... 
Creating peer1.org2.example.com ... 
Creating peer0.org1.example.com ... 
Creating peer1.org1.example.com ... 
Creating orderer.example.com
Creating peer0.org2.example.com
Creating peer1.org2.example.com
Creating peer0.org1.example.com
Creating peer0.org2.example.com ... done
Creating cli ... 
Creating cli ... done
CONTAINER ID        IMAGE                               COMMAND             CREATED             STATUS                  PORTS                      NAMES
293655c5482c        hyperledger/fabric-tools:latest     "/bin/bash"         2 seconds ago       Up Less than a second                              cli
9f522baad222        hyperledger/fabric-peer:latest      "peer node start"   7 seconds ago       Up 2 seconds            0.0.0.0:8051->8051/tcp     peer1.org1.example.com
3d4c34598368        hyperledger/fabric-peer:latest      "peer node start"   7 seconds ago       Up 2 seconds            0.0.0.0:7051->7051/tcp     peer0.org1.example.com
989d4c183202        hyperledger/fabric-peer:latest      "peer node start"   7 seconds ago       Up 2 seconds            0.0.0.0:10051->10051/tcp   peer1.org2.example.com
f61316b92ae6        hyperledger/fabric-peer:latest      "peer node start"   7 seconds ago       Up 2 seconds            0.0.0.0:9051->9051/tcp     peer0.org2.example.com
0c0b9b47992a        hyperledger/fabric-orderer:latest   "orderer"           7 seconds ago       Up 3 seconds            0.0.0.0:7050->7050/tcp     orderer.example.com

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

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

Channel name : mychannel
Creating channel...
+ peer channel create -o orderer.example.com:7050 -c mychannel -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
2019-11-27 13:36:07.878 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-11-27 13:36:07.979 UTC [cli.common] readBlock -> INFO 002 Received block: 0
===================== Channel 'mychannel' created ===================== 

Having all peers join the channel...
+ peer channel join -b mychannel.block
+ res=0
+ set +x
2019-11-27 13:36:08.070 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-11-27 13:36:08.233 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer0.org1 joined channel 'mychannel' ===================== 

+ peer channel join -b mychannel.block
+ res=0
+ set +x
2019-11-27 13:36:11.354 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-11-27 13:36:11.479 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer1.org1 joined channel 'mychannel' ===================== 

+ peer channel join -b mychannel.block
+ res=0
+ set +x
2019-11-27 13:36:14.570 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-11-27 13:36:14.737 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer0.org2 joined channel 'mychannel' ===================== 

+ peer channel join -b mychannel.block
+ res=0
+ set +x
2019-11-27 13:36:17.827 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-11-27 13:36:17.955 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer1.org2 joined channel 'mychannel' ===================== 

Updating anchor peers for org1...
+ peer channel update -o orderer.example.com:7050 -c mychannel -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
2019-11-27 13:36:21.069 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-11-27 13:36:21.084 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
===================== Anchor peers updated for org 'Org1MSP' on channel 'mychannel' ===================== 

Updating anchor peers for org2...
+ peer channel update -o orderer.example.com:7050 -c mychannel -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
2019-11-27 13:36:24.158 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-11-27 13:36:24.169 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
===================== Anchor peers updated for org 'Org2MSP' on channel 'mychannel' ===================== 

Installing chaincode on peer0.org1...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-11-27 13:36:27.263 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-11-27 13:36:27.263 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-11-27 13:36:27.597 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:"OK" > 
===================== Chaincode is installed on peer0.org1 ===================== 

Install chaincode on peer0.org2...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-11-27 13:36:27.672 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-11-27 13:36:27.672 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-11-27 13:36:27.865 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:"OK" > 
===================== Chaincode is installed on peer0.org2 ===================== 

Instantiating chaincode on peer0.org2...
+ 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 mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'
+ res=0
+ set +x
2019-11-27 13:36:27.951 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-11-27 13:36:27.951 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
===================== Chaincode is instantiated on peer0.org2 on channel 'mychannel' ===================== 

Querying chaincode on peer0.org1...
===================== Querying on peer0.org1 on channel 'mychannel'... ===================== 
Attempting to Query peer0.org1 ...3 secs
+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x

100
===================== Query successful on peer0.org1 on channel 'mychannel' ===================== 
Sending invoke transaction on peer0.org1 peer0.org2...
+ 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 mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
+ res=0
+ set +x
2019-11-27 13:37:51.058 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 
===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' ===================== 

Installing chaincode on peer1.org2...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-11-27 13:37:51.186 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-11-27 13:37:51.186 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-11-27 13:37:51.881 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:"OK" > 
===================== Chaincode is installed on peer1.org2 ===================== 

Querying chaincode on peer1.org2...
===================== Querying on peer1.org2 on channel 'mychannel'... ===================== 
Attempting to Query peer1.org2 ...3 secs
+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x

90
===================== Query successful on peer1.org2 on channel 'mychannel' ===================== 

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


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

关闭网络

$ ./byfn.sh down 

  关闭后运行代码如下

Stopping for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y  //选择y
proceeding ...
WARNING: The BYFN_CA2_PRIVATE_KEY variable is not set. Defaulting to a blank string.
WARNING: The BYFN_CA1_PRIVATE_KEY variable is not set. Defaulting to a blank string.
Stopping cli                    ... done
Stopping peer0.org1.example.com ... done
Stopping peer0.org2.example.com ... done
Stopping peer1.org1.example.com ... done
Stopping peer1.org2.example.com ... done
Stopping orderer.example.com    ... done
Removing cli                    ... done
Removing peer0.org1.example.com ... done
Removing peer0.org2.example.com ... done
Removing peer1.org1.example.com ... done
Removing peer1.org2.example.com ... done
Removing orderer.example.com    ... done
Removing network net_byfn
Removing volume net_peer0.org3.example.com
WARNING: Volume net_peer0.org3.example.com not found.
Removing volume net_peer1.org3.example.com
WARNING: Volume net_peer1.org3.example.com not found.
Removing volume net_orderer2.example.com
WARNING: Volume net_orderer2.example.com not found.
Removing volume net_orderer.example.com
Removing volume net_peer0.org2.example.com
Removing volume net_peer0.org1.example.com
Removing volume net_peer1.org1.example.com
Removing volume net_peer1.org2.example.com
Removing volume net_orderer5.example.com
WARNING: Volume net_orderer5.example.com not found.
Removing volume net_orderer4.example.com
WARNING: Volume net_orderer4.example.com not found.
Removing volume net_orderer3.example.com
WARNING: Volume net_orderer3.example.com not found.
82cf66555f2d
0d53014a7129
f5289381ceb6
Untagged: dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab:latest
Deleted: sha256:d0038a8afea74425d29cf9b15abae9e0d7480b08eed0596abaa7f0f3e6646688
Deleted: sha256:acf8f6471a4f6b452e53ae2b67c8a14f5777eb430a3fc1e23d3a0361f4b73818
Deleted: sha256:e2450a77289d214bba3d18257ee2e6c6d0f5973148142cf0991ee5c56cca5e72
Deleted: sha256:f10265e68653eb4de0265caf2b43fffe4975080c9234591205f77d163ba4c3de
Untagged: dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9:latest
Deleted: sha256:1943eaa85ea76c2342bcf09488a4e0963b86e9b3b553b2c1a5fdbb4332ecfd9b
Deleted: sha256:c5145e62235c044e69a5496300de0498b7378b13d29f5877d2e8d9f5fea14f00
Deleted: sha256:a8e61719280a02039212f2d5c199e62caa3038b67901cc1850b84418798d176d
Deleted: sha256:5cc9c07e2da983c00b359c415f65ceb737cdb1b1a7ac637fa84d0abd04f2e9e3
Untagged: dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b:latest
Deleted: sha256:7065525f5c72fbab010e33d179cfb8cc957109b550294996e022fa165a3c5a81
Deleted: sha256:f77ae302eed38281cda0068c3c4936c0f18001a078b26600de53c142b5174bc6
Deleted: sha256:6a9c8338499991548b07001eba746ddd75d3b5a927f6120fc48e5c2bb85854dd
Deleted: sha256:c5a9c56196567292c4cc72c6af7bcd371cf843f878e52564e55c548267d8cca7

运行示例代码

  现在再次启动网络运行 ./byfn.sh ,然后进入容器执行示例代码
进入容器

$ docker exec -it cli bash

查询 a 值

// 因为在初始化时已经执行过一次,现在的 a 值查询结果为90
$ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' 

执行转账功能( a 转 10 到 b)

// 该代码为将a的10转给b ,执行一次之后再查询a可看到还剩80
$ 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 mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}' 

退出容器并关闭网络

$ exit
$ ./byfn.sh down

可能会出现的问题

1、docker ps 错误
  当执行 $ docker ps 时出现以下错误,这是由于docker守护进程启动的时候,会默认赋予名字为docker的用户组读写unix socket的权限, 因此只要创建docker用户组,并将当前用户加入到docker用户组中,那么当前用户就有权限访问unix socket了,进而就可以执行docker相关命令

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/json: dial unix /var/run/docker.sock: connect: permission denied

采用以下命令解决

$ sudo groupadd docker             # 添加用户组
$ sudo gpasswd -a $USER docker     # 将登录用户加入到docker用户组中
$ newgrp docker                     # 更新用户组
$ docker ps                        # 测试docker命令是否可用

2、docker 镜像未关闭错误

  当启动网络时出现以下错误,是因为没有 down 掉或未完全关闭,关闭 docker 进程即可

ERROR: for orderer.example.com  Cannot create container for service orderer.example.com: Conflict. The container name "/orderer.example.com" is already in use by container "568211c4b119eaebc27c896f98b5ecc71c487e9f4e0fa6778c8Creating peer1.org2.example.com

解决办法

$ docker ps -a
$ docker ps -qa | xargs docker stop
$ docker ps -qa | xargs docker rm
$ docker-compose -f docker-compose.yaml up -d

博主刚接触Fabric,记录下学习过程,谢谢观看!!!

你可能感兴趣的:(Fabric)