Fabric1.4.1 - First-network案例部署

作者:jockming

联系方式(QQ):1299986041

博客:https://www.cnblogs.com/jockming/

交流群(QQ):537487044(Fabric技术交流群)


First-network 案例部署指导

########################################

说明:
1、本案例在Centos 7虚拟机上演示。
2、虚拟机已完成环境的初始化。
3、初始化步骤如下(仅供参考):
  |- 1、关闭防火墙(不建议生产环境这么做)
  |- 2、关闭Selinux(不建议生产环境这么做)
  |- 3、设置时间、时区、时间同步
  |- 4、推荐安装wget、curl、lrzsz、git、vim、tree、dos2unix
  |- 5、使用国内的镜像源(推荐使用阿里的镜像源)
  |- 6、安装docker
  |- 7、设置docker镜像加速(推荐使用阿里云docker镜像服务)
  |- 8、安装docker-compose
  |- 9、拉取fabric镜像

参考网址

########################################

  1. 项目地址:
    |-- 点击这里

  2. 文档地址:
    |-- 点击这里

  3. 工具下载:
    |-- 点击这里


具体操作

########################################

  1. 创建工作目录并进入该目录
    $ mkdir -p /home/scripts && cd /home/scripts

  2. 下载脚本
    $ curl -sS https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh -o ./bootstrap.sh

  3. 赋予脚本执行权限
    $ chmod +x ./bootstrap.sh

  4. 执行脚本来克隆fabric-samples库
    镜像我们在前面的课程拉取过,这里跳过特定平台二进制文件的下载(很慢,自行通过浏览器下载,再上传到虚拟机)
    $ ./bootstrap.sh 1.4.1 1.4.1 0.4.15 -d -b
    如果没有拉取fabric镜像的执行下面这条命令
    $ ./bootstrap.sh 1.4.1 1.4.1 0.4.15 -b

  5. 创建一个fabric-tools目录
    $ mkdir fabric-tools

  6. 将工具解压到fabric-tools目录
    $ tar -zxvf hyperledger-fabric-linux-amd64-1.4.1.tar.gz -C ./fabric-tools

  7. 将fabric-tools目录下的bin文件夹复制到fabric-samples目录下
    $ cp -r ./fabric-tools/bin ./fabric-samples/bin

  8. 进入到fabric-samples目录下的first-network文件夹下面
    $ cd /home/scripts/fabric-samples/first-network

  9. 执行启动命令
    $ ./byfn.sh up


脚本分析

########################################

首先从脚本执行的打印来看看脚本都做了哪些工作。

  • 使用加密工具生成证书(Generate certificates using cryptogen tool)
    具体指令:
    $ cryptogen generate --config=./crypto-config.yaml

  • 生成Orderer Genesis块(Generating Orderer Genesis block)
    具体指令:
    $ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block

  • 生成通道配置事务'channel.tx'(Generating channel configuration transaction 'channel.tx')
    具体指令:
    $ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel

  • 为org1生成锚节点更新文件(Generating anchor peer update for Org1MSP)
    具体指令:
    $ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP

  • 为org2生成锚节点更新文件(Generating anchor peer update for Org2MSP)
    具体指令:
    $ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP

  • 启动网络(未打印指令)

    
     ____    _____      _      ____    _____
    / ___|  |_   _|    / \    |  _ \  |_   _|
    \___ \    | |     / _ \   | |_) |   | |
     ___) |   | |    / ___ \  |  _ <    | |
    |____/    |_|   /_/   \_\ |_| \_\   |_|
    
    Build your first network (BYFN) end-to-end test
    
  • 创建通道(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
    

    ===================== Channel 'mychannel' created =====================

  • 将所有peer节点加入到通道(Having all peers join the channel...)
    具体指令:
    $ peer channel join -b mychannel.block
    ===================== peer0.org1 joined channel 'mychannel' =====================
    具体指令:
    $ peer channel join -b mychannel.block
    ===================== peer1.org1 joined channel 'mychannel' =====================
    具体指令:
    $ peer channel join -b mychannel.block
    ===================== peer0.org2 joined channel 'mychannel' =====================
    具体指令:
    $ peer channel join -b mychannel.block
    ===================== peer1.org2 joined channel 'mychannel' =====================

  • 为org1更新锚节点(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
    

    ===================== Anchor peers updated for org 'Org1MSP' on channel 'mychannel' =====================

  • 为org2更新锚节点(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
    

    ===================== Anchor peers updated for org 'Org2MSP' on channel 'mychannel' =====================

  • 在peer0.org1上安装链码(Installing chaincode on peer0.org1...)
    具体指令:
    $ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
    ===================== Chaincode is installed on peer0.org1 =====================

  • 在peer0.org2上安装链码(Install chaincode on peer0.org2...)
    具体指令:
    $ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
    ===================== Chaincode is installed on peer0.org2 =====================

  • 在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'\'')'
    

    ===================== Chaincode is instantiated on peer0.org2 on channel 'mychannel' =====================

  • 在peer0.org1上查询链码(Querying chaincode on peer0.org1...)
    具体指令:
    $ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
    ===================== Query successful on peer0.org1 on channel 'mychannel' =====================

  • 在peer0.org1 peer0.org2上执行交易(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"]}'
    

    ===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' =====================

  • 在peer1.org2上安装链码(Installing chaincode on peer1.org2...)
    具体指令:
    $ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
    ===================== Chaincode is installed on peer1.org2 =====================

  • 查询peer1.org2上的链码(Querying chaincode on peer1.org2...)
    具体指令:
    $ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
    ===================== Query successful on peer1.org2 on channel 'mychannel' =====================

    ========= All GOOD, BYFN execution completed ===========
    
     _____   _   _   ____
    | ____| | \ | | |  _ \
    |  _|   |  \| | | | | |
    | |___  | |\  | | |_| |
    |_____| |_| \_| |____/
    

脚本调用链分析

########################################

说明:
操作均省略了详细的操作步骤
- M 代表方法
- D 代表目录
- F 代表文件
- J 判断
- O 操作
first-network
.
|-- ...
|-- [.env](F)
|-- [byfn.sh](F)
|   |-- [networkUp](M)
|   |   |-- [checkPrereqs](M)
|   |   |-- [if !-d "crypto-config"](J)
|   |       |-- [generateCerts](M)
|   |       |-- [replacePrivateKey](M)
|   |       |-- [generateChannelArtifacts](M)
|   |       |-- [Start the container](O)
|   |       |-- [run the end to end script](O) --> [scripts/script.sh](F)
|-- [scripts](D)
|   |-- [import utils](O) --> [scripts/utils.sh](F)
|   |-- [createChannel](M)
|   |   |-- [setGlobals](M) # Method from ./scripts/utils.sh
|   |   |-- [executive command](O)
|   |   |-- [verifyResult](M)
|   |-- [joinChannel](M)
|   |   |-- [joinChannelWithRetry](M) # Method from ./scripts/utils.sh
|   |-- [updateAnchorPeers](M) # Updating anchor peers for org1...  Method from ./scripts/utils.sh
|   |-- [updateAnchorPeers](M) # Updating anchor peers for org2...  Method from ./scripts/utils.sh
|   |-- [installChaincode](M) # Installing chaincode on peer0.org1...  Method from ./scripts/utils.sh
|   |-- [installChaincode](M) # Installing chaincode on peer0.org2...  Method from ./scripts/utils.sh
|   |-- [instantiateChaincode](M) # Instantiate chaincode on peer0.org2.  Method from ./scripts/utils.sh
|   |-- [chaincodeQuery](M) # Query chaincode on peer0.org1.  Method from ./scripts/utils.sh
|   |-- [chaincodeInvoke](M) # Invoke chaincode on peer0.org1 and peer0.org2.  Method from ./scripts/utils.sh
|   |-- [installChaincode](M) # Install chaincode on peer1.org2.  Method from ./scripts/utils.sh
|   |-- [chaincodeQuery](M) # Query on chaincode on peer1.org2.  Method from ./scripts/utils.sh

posted @ 2020-03-17 17:02  itwetouch  阅读(728)  评论(0编辑  收藏  举报