Fabric學習(一)之搭建第一個Fabric網絡

時間:2019.11.27.21.44


參考地址: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:<status:200 payload:"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:<status:200 payload:"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:<status:200 payload:"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,記錄下學習過程,謝謝觀看!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章