Hyperledger Fabric 1.2系列:4.first-network网络的执行过程

解析

  1. 生成证书
Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
LOCAL_VERSION=1.2.0
DOCKER_IMAGE_VERSION=1.2.0
/root/gowork/src/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

读取相应的版本内容,通过cryptogen读取crypto-config.yaml文件的配置,生成证书相关的内容。

  1. 初始化网络相关内容
/root/gowork/src/fabric-samples/first-network/../bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
+ configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
2018-08-06 10:33:08.419 CST [common/tools/configtxgen] main -> WARN 001 Omitting the channel ID for configtxgen is deprecated.  Explicitly passing the channel ID will be required in the future, defaulting to 'testchainid'.
2018-08-06 10:33:08.419 CST [common/tools/configtxgen] main -> INFO 002 Loading configuration
2018-08-06 10:33:08.428 CST [common/tools/configtxgen/encoder] NewChannelGroup -> WARN 003 Default policy emission is deprecated, please include policy specificiations for the channel group in configtx.yaml
2018-08-06 10:33:08.428 CST [common/tools/configtxgen/encoder] NewOrdererGroup -> WARN 004 Default policy emission is deprecated, please include policy specificiations for the orderer group in configtx.yaml
2018-08-06 10:33:08.428 CST [common/tools/configtxgen/encoder] NewOrdererOrgGroup -> WARN 005 Default policy emission is deprecated, please include policy specificiations for the orderer org group OrdererOrg in configtx.yaml
2018-08-06 10:33:08.429 CST [msp] getMspConfig -> INFO 006 Loading NodeOUs
2018-08-06 10:33:08.429 CST [common/tools/configtxgen/encoder] NewOrdererOrgGroup -> WARN 007 Default policy emission is deprecated, please include policy specificiations for the orderer org group Org1MSP in configtx.yaml
2018-08-06 10:33:08.429 CST [msp] getMspConfig -> INFO 008 Loading NodeOUs
2018-08-06 10:33:08.429 CST [common/tools/configtxgen/encoder] NewOrdererOrgGroup -> WARN 009 Default policy emission is deprecated, please include policy specificiations for the orderer org group Org2MSP in configtx.yaml
2018-08-06 10:33:08.429 CST [common/tools/configtxgen] doOutputBlock -> INFO 00a Generating genesis block
2018-08-06 10:33:08.429 CST [common/tools/configtxgen] doOutputBlock -> INFO 00b Writing genesis block
+ res=0
+ set +x

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2018-08-06 10:33:08.465 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-08-06 10:33:08.472 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx
2018-08-06 10:33:08.472 CST [common/tools/configtxgen/encoder] NewApplicationGroup -> WARN 003 Default policy emission is deprecated, please include policy specificiations for the application group in configtx.yaml
2018-08-06 10:33:08.472 CST [msp] getMspConfig -> INFO 004 Loading NodeOUs
2018-08-06 10:33:08.473 CST [common/tools/configtxgen/encoder] NewApplicationOrgGroup -> WARN 005 Default policy emission is deprecated, please include policy specificiations for the application org group Org1MSP in configtx.yaml
2018-08-06 10:33:08.473 CST [msp] getMspConfig -> INFO 006 Loading NodeOUs
2018-08-06 10:33:08.473 CST [common/tools/configtxgen/encoder] NewApplicationOrgGroup -> WARN 007 Default policy emission is deprecated, please include policy specificiations for the application org group Org2MSP in configtx.yaml
2018-08-06 10:33:08.474 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 008 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
2018-08-06 10:33:08.508 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-08-06 10:33:08.514 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2018-08-06 10:33:08.515 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 mychannel -asOrg Org2MSP
2018-08-06 10:33:08.549 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-08-06 10:33:08.555 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2018-08-06 10:33:08.555 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
+ res=0
+ set +x

通过configtxgen工具来读取configtx.yaml文件的配置:

  • orderer service生成genesis.block
  • 生成channel相关的transaction文件channel.tx
  • 更新Org1MSPanchor peer
  • 更新Org2MSPanchor peer
  1. 创建channel
 ____    _____      _      ____    _____
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |
 ___) |   | |    / ___ \  |  _ <    | |
|____/    |_|   /_/   \_\ |_| \_\   |_|

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
2018-08-06 02:33:11.058 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-06 02:33:11.079 UTC [cli/common] readBlock -> INFO 002 Got status: &{NOT_FOUND}
2018-08-06 02:33:11.083 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2018-08-06 02:33:11.285 UTC [cli/common] readBlock -> INFO 004 Received block: 0
===================== Channel 'mychannel' created =====================

根据配置的channel名字,创建channel。创建channel的时候,需要指定orderer地址、channel.tx文件的位置、是否启用tls以及orderer证书的位置。

创建channel的命令:

peer channel create -o orderer的地址及端口号 -c channel名字 -f ./channel-artifacts/channel.tx --tls true --cafile orderer证书的位置
  1. peer加入channel

Having all peers join the channel...
+ peer channel join -b mychannel.block
+ res=0
+ set +x
2018-08-06 02:33:11.351 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-06 02:33:11.389 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
2018-08-06 02:33:14.455 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-06 02:33:14.484 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
2018-08-06 02:33:17.551 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-06 02:33:17.579 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
2018-08-06 02:33:20.650 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-06 02:33:20.677 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer1.org2 joined channel 'mychannel' =====================

命令:

 peer channel join -b mychannel.block
  1. 更新anchor peer
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
2018-08-06 02:33:23.741 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-06 02:33:23.753 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
2018-08-06 02:33:26.819 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-08-06 02:33:26.830 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
===================== Anchor peers updated for org 'Org2MSP' on channel 'mychannel' =====================

更新anchor节点时需要指定orderer地址、channel名字、Org1MSPanchors.txtls以及orderer cafile
命令:

peer channel update -o orderer的地址及端口号 -c channel名字 -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile 
orderer证书的位置
  1. 安装chaincode
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
2018-08-06 02:33:29.903 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-06 02:33:29.903 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2018-08-06 02:33:30.053 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:
===================== 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
2018-08-06 02:33:30.119 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-06 02:33:30.119 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2018-08-06 02:33:30.269 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:
===================== Chaincode is installed on peer0.org2 =====================

安装chaincode时可以指定:

  • -n : chaincode name
  • -v : chaincode版本。(chaincode 可以升级)
  • -l : chaincode使用哪种语言编写的。
  • -p :chaincode 所在的路径。(cli容器中的路径)

这里安装chaincode时需要安装到你指定的anchor peer上。在安装过程中,系统链码会对你的chaincode进行校验esccvscc

命令:

+ peer chaincode install -n chaincode的名字 -v chaincode的版本 -l 编写chaincode的语言 -p chaincode在cli容器中的路径
  1. 实例化chaincode
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
2018-08-06 02:33:30.341 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-08-06 02:33:30.342 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
===================== Chaincode is instantiated on peer0.org2 on channel 'mychannel' =====================

实例化chaincode时,需要指定:

  • -o : orderer节点的地址
  • --tls : 是否启用tlstrue/false
  • -- cafile : orderer证书的位置。(启用tls时需要指定)
  • -C : channel 名字
  • -n : chaincode名字
  • -l : chaincode编写的语言
  • -v:chaincode版本
  • -c : 初始化时的参数
  • -P : 背书策略

在实例化链码的时候,系统的esccvscc链码会对你的链码进行验证。
命令:

peer chaincode instantiate -o 排序节点的地址 --tls 是否启用tls --cafile 排序节点的证书地址 -C 通道名称 -n 链码名称 -l 链码使用的语言 -v 链码的版本 -c 初始化时传入的参数 -P 背书策略
  1. 查询chain code
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' =====================

查询链码时需要指定:

  • -C : 通道名字
  • -n : 链码名字
  • -c : 参数
    命令:
peer chaincode query -C 通道名字 -n 链码名字 -c 参数
  1. 调用链码
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:7051 --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
2018-08-06 02:34:01.345 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' =====================

调用链码时需要指定:

  • -o : orderer的地址
  • --tls:是否启用tls。true/false非必填
  • --cafile: orderer的证书位置。(如果tls为 true 则为必填项。)
  • -C : 通道名称
  • -n :链码名称
  • --peerAddresses :peer节点的地址 (可以指定多个节点)
  • --tlsRootCertFiles : peer节点的tls证书地址 (根据peerAddresses来进行配置)
  • -c : 参数

命令:

peer chaincode invoke -o orderer节点的地址 --tls 是否启用 --cafile orderer节点的证书 -C 通道名称 -n 链码名称 --peerAddresses peer节点的地址 --tlsRootCertFiles peer节点对应的tls证书地址  -c 参数

总结

在启动first-network网络的过程中我们可以总结为以下几个过程

  1. 通过cryptogen读取crypto-config.yaml文件的配置,生成证书相关的内容。
  2. 通过configtxgen工具来读取configtx.yaml文件的配置,生成启动网络的基本环境。
  3. 创建channel
peer channel create -o orderer的地址及端口号 -c channel名字 -f ./channel-artifacts/channel.tx --tls true --cafile orderer证书的位置
  1. 将节点加入通道
peer channel join -b 通道名称.block
  1. 更新anchor peer
peer channel update -o orderer的地址及端口号 -c channel名字 -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile 
orderer证书的位置
  1. 安装链码
peer chaincode install -n chaincode的名字 -v chaincode的版本 -l 编写chaincode的语言 -p chaincode在cli容器中的路径
  1. 实例化链码
peer chaincode instantiate -o 排序节点的地址 --tls 是否启用tls --cafile 排序节点的证书地址 -C 通道名称 -n 链码名称 -l 链码使用的语言 -v 链码的版本 -c 初始化时传入的参数 -P 背书策略
  1. 查询链码
peer chaincode query -C 通道名字 -n 链码名字 -c 参数
  1. 调用链码
peer chaincode invoke -o orderer节点的地址 --tls 是否启用 --cafile orderer节点的证书 -C 通道名称 -n 链码名称 --peerAddresses peer节点的地址 --tlsRootCertFiles peer节点对应的tls证书地址  -c 参数



作者:沙漠中的猴
链接:https://www.jianshu.com/p/817f7f3b2734
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

你可能感兴趣的:(Hyperledger Fabric 1.2系列:4.first-network网络的执行过程)