粗略分析hyperledger-fabric之e2e_cli是如何开启网络

2023-12-27 18:38

本文主要是介绍粗略分析hyperledger-fabric之e2e_cli是如何开启网络,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、sudo ./network_setup.sh up

这条命令是如何为我们开启网络?通过几天的查看,我大致理清了他的顺序。在这里分享出来,如有不对,还望指出。
我们还是先看看这个shell文本里的内容:

#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#UP_DOWN="$1"
CH_NAME="$2"
CLI_TIMEOUT="$3"
IF_COUCHDB="$4": ${CLI_TIMEOUT:="10000"}COMPOSE_FILE=docker-compose-cli.yaml
COMPOSE_FILE_COUCH=docker-compose-couch.yaml
#COMPOSE_FILE=docker-compose-e2e.yamlfunction printHelp () {echo "Usage: ./network_setup <up|down> <\$channel-name> <\$cli_timeout> <couchdb>.\nThe arguments must be in order."
}function validateArgs () {if [ -z "${UP_DOWN}" ]; thenecho "Option up / down / restart not mentioned"printHelpexit 1fiif [ -z "${CH_NAME}" ]; thenecho "setting to default channel 'mychannel'"CH_NAME=mychannelfi
}function clearContainers () {CONTAINER_IDS=$(docker ps -aq)if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" = " " ]; thenecho "---- No containers available for deletion ----"elsedocker rm -f $CONTAINER_IDSfi
}function removeUnwantedImages() {DOCKER_IMAGE_IDS=$(docker images | grep "dev\|none\|test-vp\|peer[0-9]-" | awk '{print $3}')if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" = " " ]; thenecho "---- No images available for deletion ----"elsedocker rmi -f $DOCKER_IMAGE_IDSfi
}function networkUp () {if [ -f "./crypto-config" ]; thenecho "crypto-config directory already exists."else#Generate all the artifacts that includes org certs, orderer genesis block,# channel configuration transactionsource generateArtifacts.sh $CH_NAMEfiif [ "${IF_COUCHDB}" == "couchdb" ]; thenCHANNEL_NAME=$CH_NAME TIMEOUT=$CLI_TIMEOUT docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_COUCH up -d 2>&1elseCHANNEL_NAME=$CH_NAME TIMEOUT=$CLI_TIMEOUT docker-compose -f $COMPOSE_FILE up -d 2>&1fiif [ $? -ne 0 ]; thenecho "ERROR !!!! Unable to pull the images "exit 1fidocker logs -f cli
}function networkDown () {docker-compose -f $COMPOSE_FILE down#Cleanup the chaincode containersclearContainers#Cleanup imagesremoveUnwantedImages# remove orderer block and other channel configuration transactions and certsrm -rf channel-artifacts/*.block channel-artifacts/*.tx crypto-config
}validateArgs#Create the network using docker compose
if [ "${UP_DOWN}" == "up" ]; thennetworkUp
elif [ "${UP_DOWN}" == "down" ]; then ## Clear the networknetworkDown 
elif [ "${UP_DOWN}" == "restart" ]; then ## Restart the networknetworkDownnetworkUp
elseprintHelpexit 1
fi

阅读这个shell需要一些基础,我也是刚看完shell的一些基本语法才勉强理解,下面我用一张图表示他的启动顺序。
这里写图片描述
那么这里面最关键的地方有那些呢?首先这条语句:

source generateArtifacts.sh $CH_NAME

无形之间将我们带到了另一个shell文本,那就是同目录下的generateArtifacts.sh,下面我们接着往下看。

#!/bin/bash +x
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
##set -eCHANNEL_NAME=$1
: ${CHANNEL_NAME:="mychannel"}
echo $CHANNEL_NAMEexport FABRIC_ROOT=$PWD/../..
export FABRIC_CFG_PATH=$PWD
echoOS_ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')" | awk '{print tolower($0)}')## Using docker-compose template replace private key file names with constants
function replacePrivateKey () {ARCH=`uname -s | grep Darwin`if [ "$ARCH" == "Darwin" ]; thenOPTS="-it"elseOPTS="-i"ficp docker-compose-e2e-template.yaml docker-compose-e2e.yamlCURRENT_DIR=$PWDcd crypto-config/peerOrganizations/org1.example.com/ca/PRIV_KEY=$(ls *_sk)cd $CURRENT_DIRsed $OPTS "s/CA1_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yamlcd crypto-config/peerOrganizations/org2.example.com/ca/PRIV_KEY=$(ls *_sk)cd $CURRENT_DIRsed $OPTS "s/CA2_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yaml
}## Generates Org certs using cryptogen tool
function generateCerts (){CRYPTOGEN=$FABRIC_ROOT/release/$OS_ARCH/bin/cryptogenif [ -f "$CRYPTOGEN" ]; thenecho "Using cryptogen -> $CRYPTOGEN"elseecho "Building cryptogen"make -C $FABRIC_ROOT releasefiechoecho "##########################################################"echo "##### Generate certificates using cryptogen tool #########"echo "##########################################################"$CRYPTOGEN generate --config=./crypto-config.yamlecho
}## Generate orderer genesis block , channel configuration transaction and anchor peer update transactions
function generateChannelArtifacts() {CONFIGTXGEN=$FABRIC_ROOT/release/$OS_ARCH/bin/configtxgenif [ -f "$CONFIGTXGEN" ]; thenecho "Using configtxgen -> $CONFIGTXGEN"elseecho "Building configtxgen"make -C $FABRIC_ROOT releasefiecho "##########################################################"echo "#########  Generating Orderer Genesis block ##############"echo "##########################################################"# Note: For some unknown reason (at least for now) the block file can't be# named orderer.genesis.block or the orderer will fail to launch!$CONFIGTXGEN -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.blockechoecho "#################################################################"echo "### Generating channel configuration transaction 'channel.tx' ###"echo "#################################################################"$CONFIGTXGEN -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAMEechoecho "#################################################################"echo "#######    Generating anchor peer update for Org1MSP   ##########"echo "#################################################################"$CONFIGTXGEN -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSPechoecho "#################################################################"echo "#######    Generating anchor peer update for Org2MSP   ##########"echo "#################################################################"$CONFIGTXGEN -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSPecho
}generateCerts
replacePrivateKey
generateChannelArtifacts

此文件主要是用来生成主要的配置文件如私钥和证书,有通道证书之类的。下面这张图是它执行的顺序。
这里写图片描述
其次语句:

CHANNEL_NAME=$CH_NAME TIMEOUT=$CLI_TIMEOUT docker-compose -f $COMPOSE_FILE up -d 2>&1

其实就是:docker-compose -f docker-compose-cli.yaml up是不是感觉很熟悉呢?接下来我们接着看docker-compose-cli.yaml文件。

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#version: '2'services:orderer.example.com:extends:file:   base/docker-compose-base.yamlservice: orderer.example.comcontainer_name: orderer.example.compeer0.org1.example.com:container_name: peer0.org1.example.com#environment:#- CORE_LEDGER_STATE_STATEDATABASE=CouchDB#- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=peer0.org1.example.com:5984#- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin#- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=passwordextends:file:  base/docker-compose-base.yamlservice: peer0.org1.example.compeer1.org1.example.com:container_name: peer1.org1.example.com#environment:#- CORE_LEDGER_STATE_STATEDATABASE=CouchDB#- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=peer1.org1.example.com:5984#- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin#- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=passwordextends:file:  base/docker-compose-base.yamlservice: peer1.org1.example.compeer0.org2.example.com:container_name: peer0.org2.example.com#environment:#- CORE_LEDGER_STATE_STATEDATABASE=CouchDB#- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=peer0.org2.example.com:5984#- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin#- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=passwordextends:file:  base/docker-compose-base.yamlservice: peer0.org2.example.compeer1.org2.example.com:container_name: peer1.org2.example.com#environment:#- CORE_LEDGER_STATE_STATEDATABASE=CouchDB#- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=peer1.org2.example.com:5984#- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin#- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=passwordextends:file:  base/docker-compose-base.yamlservice: peer1.org2.example.comcli:container_name: cliimage: hyperledger/fabric-toolstty: trueenvironment:- GOPATH=/opt/gopath
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_ID=cli
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_TLS_ENABLED=true
      - 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_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_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
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT'volumes:- /var/run/:/host/var/run/
        - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
        - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
        - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
        - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
    depends_on:- orderer.example.com
      - peer0.org1.example.com
      - peer1.org1.example.com
      - peer0.org2.example.com
      - peer1.org2.example.com

这个文件的作用就是启动一系列的容器,当你看到下面内容的时候,就是此配置文件的作用:

Creating peer1.org2.example.com ... done
Creating cli ... done
Creating orderer.example.com ...
Creating peer0.org2.example.com ...
Creating peer1.org1.example.com ...
Creating peer0.org1.example.com ...
Creating cli ...

这个配置文件中还有一个地方需要我们关注,那就是:base/docker-compose-base.yaml,我们接着看看当前目录下的base文件夹里的docker-compose-base.yaml文件。

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#version: '2'services:orderer.example.com:container_name: orderer.example.comimage: hyperledger/fabric-ordererenvironment:- ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
      # enabled TLS- ORDERER_GENERAL_TLS_ENABLED=true
      - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
      - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
      - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
    working_dir: /opt/gopath/src/github.com/hyperledger/fabriccommand: orderervolumes:- ../channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
    - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
    - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
    ports:- 7050:7050
peer0.org1.example.com:container_name: peer0.org1.example.comextends:file: peer-base.yamlservice: peer-baseenvironment:- CORE_PEER_ID=peer0.org1.example.com
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer0.org1.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
    volumes:- /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
    ports:- 7051:7051
      - 7052:7052
      - 7053:7053
peer1.org1.example.com:container_name: peer1.org1.example.comextends:file: peer-base.yamlservice: peer-baseenvironment:- CORE_PEER_ID=peer1.org1.example.com
      - CORE_PEER_ADDRESS=peer1.org1.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer1.org1.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.example.com:7051
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
    volumes:- /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls:/etc/hyperledger/fabric/tls
ports:- 8051:7051
      - 8052:7052
      - 8053:7053
peer0.org2.example.com:container_name: peer0.org2.example.comextends:file: peer-base.yamlservice: peer-baseenvironment:- CORE_PEER_ID=peer0.org2.example.com
      - CORE_PEER_ADDRESS=peer0.org2.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer0.org2.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051
      - CORE_PEER_LOCALMSPID=Org2MSP
    volumes:- /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
    ports:- 9051:7051
      - 9052:7052
      - 9053:7053
peer1.org2.example.com:container_name: peer1.org2.example.comextends:file: peer-base.yamlservice: peer-baseenvironment:- CORE_PEER_ID=peer1.org2.example.com
      - CORE_PEER_ADDRESS=peer1.org2.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer1.org2.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org2.example.com:7051
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.example.com:7051
      - CORE_PEER_LOCALMSPID=Org2MSP
    volumes:- /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls:/etc/hyperledger/fabric/tls
    ports:- 10051:7051
      - 10052:7052
      - 10053:7053

这个配置文件就比较明显了,Ordererpeer的配置文件,细心的你肯定也发现了file: peer-base.yaml,那就看看同目录下的peer-base.yaml文件吧。

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#version: '2'
services:peer-base:image: hyperledger/fabric-peerenvironment:- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      # the following setting starts chaincode containers on the same# bridge network as the peers# https://docs.docker.com/compose/networking/- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=e2ecli_default
      #- CORE_LOGGING_LEVEL=ERROR- CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_PROFILE_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: peer node start

这我就不多解释了。
最后还有一个文件,在刚才的docker-compose-cli.yaml文件中有一句`command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT'什么意思呢?就是./scripts/script.sh这个shell脚本生效,一起去看看吧。

#!/bin/bash
# Copyright London Stock Exchange Group All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
echo
echo " ____    _____      _      ____    _____           _____   ____    _____ "
echo "/ ___|  |_   _|    / \    |  _ \  |_   _|         | ____| |___ \  | ____|"
echo "\___ \    | |     / _ \   | |_) |   | |    _____  |  _|     __) | |  _|  "
echo " ___) |   | |    / ___ \  |  _ <    | |   |_____| | |___   / __/  | |___ "
echo "|____/    |_|   /_/   \_\ |_| \_\   |_|           |_____| |_____| |_____|"
echoCHANNEL_NAME="$1"
: ${CHANNEL_NAME:="mychannel"}
: ${TIMEOUT:="60"}
COUNTER=1
MAX_RETRY=5
ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pemecho "Channel name : "$CHANNEL_NAMEverifyResult () {if [ $1 -ne 0 ] ; thenecho "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!"echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="echoexit 1fi
}setGlobals () {if [ $1 -eq 0 -o $1 -eq 1 ] ; thenCORE_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.crtCORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/mspif [ $1 -eq 0 ]; thenCORE_PEER_ADDRESS=peer0.org1.example.com:7051elseCORE_PEER_ADDRESS=peer1.org1.example.com:7051fielseCORE_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.crtCORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/mspif [ $1 -eq 2 ]; thenCORE_PEER_ADDRESS=peer0.org2.example.com:7051elseCORE_PEER_ADDRESS=peer1.org2.example.com:7051fifienv |grep CORE
}createChannel() {setGlobals 0if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; thenpeer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txtelsepeer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txtfires=$?cat log.txtverifyResult $res "Channel creation failed"echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== "echo
}updateAnchorPeers() {PEER=$1setGlobals $PEERif [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; thenpeer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txtelsepeer 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.txtfires=$?cat log.txtverifyResult $res "Anchor peer update failed"echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== "sleep 5echo
}## Sometimes Join takes time hence RETRY atleast for 5 times
joinWithRetry () {peer channel join -b $CHANNEL_NAME.block  >&log.txtres=$?cat log.txtif [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; thenCOUNTER=` expr $COUNTER + 1`echo "PEER$1 failed to join the channel, Retry after 2 seconds"sleep 2joinWithRetry $1elseCOUNTER=1fiverifyResult $res "After $MAX_RETRY attempts, PEER$ch has failed to Join the Channel"
}joinChannel () {for ch in 0 1 2 3; dosetGlobals $chjoinWithRetry $checho "===================== PEER$ch joined on the channel \"$CHANNEL_NAME\" ===================== "sleep 2echodone
}installChaincode () {PEER=$1setGlobals $PEERpeer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 >&log.txtres=$?cat log.txtverifyResult $res "Chaincode installation on remote peer PEER$PEER has Failed"echo "===================== Chaincode is installed on remote peer PEER$PEER ===================== "echo
}instantiateChaincode () {PEER=$1setGlobals $PEER# 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" optionif [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; thenpeer chaincode instantiate -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR   ('Org1MSP.member','Org2MSP.member')" >&log.txtelsepeer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR    ('Org1MSP.member','Org2MSP.member')" >&log.txtfires=$?cat log.txtverifyResult $res "Chaincode instantiation on PEER$PEER on channel '$CHANNEL_NAME' failed"echo "===================== Chaincode Instantiation on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "echo
}chaincodeQuery () {PEER=$1echo "===================== Querying on PEER$PEER on channel '$CHANNEL_NAME'... ===================== "setGlobals $PEERlocal rc=1local starttime=$(date +%s)# continue to poll# we either get a successful response, or reach TIMEOUTwhile test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0dosleep 3echo "Attempting to Query PEER$PEER ...$(($(date +%s)-starttime)) secs"peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txttest $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')test "$VALUE" = "$2" && let rc=0doneechocat log.txtif test $rc -eq 0 ; thenecho "===================== Query on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "elseecho "!!!!!!!!!!!!!!! Query result on PEER$PEER is INVALID !!!!!!!!!!!!!!!!"echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="echoexit 1fi
}chaincodeInvoke () {PEER=$1setGlobals $PEER# 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" optionif [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; thenpeer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txtelsepeer 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.txtfires=$?cat log.txtverifyResult $res "Invoke execution on PEER$PEER failed "echo "===================== Invoke transaction on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "echo
}## Create channel
echo "Creating channel..."
createChannel## Join all the peers to the channel
echo "Having all peers join the channel..."
joinChannel## Set the anchor peers for each org in the channel
echo "Updating anchor peers for org1..."
updateAnchorPeers 0
echo "Updating anchor peers for org2..."
updateAnchorPeers 2## Install chaincode on Peer0/Org1 and Peer2/Org2
echo "Installing chaincode on org1/peer0..."
installChaincode 0
echo "Install chaincode on org2/peer2..."
installChaincode 2#Instantiate chaincode on Peer2/Org2
echo "Instantiating chaincode on org2/peer2..."
instantiateChaincode 2#Query on chaincode on Peer0/Org1
echo "Querying chaincode on org1/peer0..."
chaincodeQuery 0 100#Invoke on chaincode on Peer0/Org1
echo "Sending invoke transaction on org1/peer0..."
chaincodeInvoke 0## Install chaincode on Peer3/Org2
echo "Installing chaincode on org2/peer3..."
installChaincode 3#Query on chaincode on Peer3/Org2, check if the result is 90
echo "Querying chaincode on org2/peer3..."
chaincodeQuery 3 90echo
echo "===================== All GOOD, End-2-End execution completed ===================== "
echoecho
echo " _____   _   _   ____            _____   ____    _____ "
echo "| ____| | \ | | |  _ \          | ____| |___ \  | ____|"
echo "|  _|   |  \| | | | | |  _____  |  _|     __) | |  _|  "
echo "| |___  | |\  | | |_| | |_____| | |___   / __/  | |___ "
echo "|_____| |_| \_| |____/          |_____| |_____| |_____|"
echoexit 0

我觉得终于到了你最希望看得的地方了,对于初学者而言的我们,看到good的时候就会心情很好,对吧?前面都为我们准备好了各节点,创世块,证书等内容!在这里这个文件为我们创建通道并且让各peer节点加入通道,安装、初始化、query准备好的chaincode!下图就是大致过程。
这里写图片描述
文件夹下还有许多的其他配置文件,其中如图。
这里写图片描述
下面用一张图总结一下:
这里写图片描述
大致过程基本如上,但感觉还差了一点什么!批评指正,批评指正!

这篇关于粗略分析hyperledger-fabric之e2e_cli是如何开启网络的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/544003

相关文章

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

SpringBoot使用OkHttp完成高效网络请求详解

《SpringBoot使用OkHttp完成高效网络请求详解》OkHttp是一个高效的HTTP客户端,支持同步和异步请求,且具备自动处理cookie、缓存和连接池等高级功能,下面我们来看看SpringB... 目录一、OkHttp 简介二、在 Spring Boot 中集成 OkHttp三、封装 OkHttp

Linux系统之主机网络配置方式

《Linux系统之主机网络配置方式》:本文主要介绍Linux系统之主机网络配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、查看主机的网络参数1、查看主机名2、查看IP地址3、查看网关4、查看DNS二、配置网卡1、修改网卡配置文件2、nmcli工具【通用

使用Python高效获取网络数据的操作指南

《使用Python高效获取网络数据的操作指南》网络爬虫是一种自动化程序,用于访问和提取网站上的数据,Python是进行网络爬虫开发的理想语言,拥有丰富的库和工具,使得编写和维护爬虫变得简单高效,本文将... 目录网络爬虫的基本概念常用库介绍安装库Requests和BeautifulSoup爬虫开发发送请求解

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

Spring、Spring Boot、Spring Cloud 的区别与联系分析

《Spring、SpringBoot、SpringCloud的区别与联系分析》Spring、SpringBoot和SpringCloud是Java开发中常用的框架,分别针对企业级应用开发、快速开... 目录1. Spring 框架2. Spring Boot3. Spring Cloud总结1. Sprin

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx