粗略分析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

相关文章

Redis主从/哨兵机制原理分析

《Redis主从/哨兵机制原理分析》本文介绍了Redis的主从复制和哨兵机制,主从复制实现了数据的热备份和负载均衡,而哨兵机制可以监控Redis集群,实现自动故障转移,哨兵机制通过监控、下线、选举和故... 目录一、主从复制1.1 什么是主从复制1.2 主从复制的作用1.3 主从复制原理1.3.1 全量复制

redis-cli命令行工具的使用小结

《redis-cli命令行工具的使用小结》redis-cli是Redis的命令行客户端,支持多种参数用于连接、操作和管理Redis数据库,本文给大家介绍redis-cli命令行工具的使用小结,感兴趣的... 目录基本连接参数基本连接方式连接远程服务器带密码连接操作与格式参数-r参数重复执行命令-i参数指定命

Redis主从复制的原理分析

《Redis主从复制的原理分析》Redis主从复制通过将数据镜像到多个从节点,实现高可用性和扩展性,主从复制包括初次全量同步和增量同步两个阶段,为优化复制性能,可以采用AOF持久化、调整复制超时时间、... 目录Redis主从复制的原理主从复制概述配置主从复制数据同步过程复制一致性与延迟故障转移机制监控与维

idea如何开启菜单栏

《idea如何开启菜单栏》文章介绍了如何通过修改IntelliJIDEA的样式文件`ui.lnf.xml`来重新显示被关闭的菜单栏,并分享了解决问题的步骤... 目录ijsdea开启菜单栏第一步第二步总结idea开启菜单栏手贱关闭了idea的js菜单栏,花费了半个小时终于解决,记录并分享一下第一步找

SSID究竟是什么? WiFi网络名称及工作方式解析

《SSID究竟是什么?WiFi网络名称及工作方式解析》SID可以看作是无线网络的名称,类似于有线网络中的网络名称或者路由器的名称,在无线网络中,设备通过SSID来识别和连接到特定的无线网络... 当提到 Wi-Fi 网络时,就避不开「SSID」这个术语。简单来说,SSID 就是 Wi-Fi 网络的名称。比如

Java实现任务管理器性能网络监控数据的方法详解

《Java实现任务管理器性能网络监控数据的方法详解》在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些... 目录引言一、背景知识二、准备工作1. Maven依赖2. Gradle依赖三、代码实现四、代码详解五

Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

《Redis连接失败:客户端IP不在白名单中的问题分析与解决方案》在现代分布式系统中,Redis作为一种高性能的内存数据库,被广泛应用于缓存、消息队列、会话存储等场景,然而,在实际使用过程中,我们可能... 目录一、问题背景二、错误分析1. 错误信息解读2. 根本原因三、解决方案1. 将客户端IP添加到Re

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

锐捷和腾达哪个好? 两个品牌路由器对比分析

《锐捷和腾达哪个好?两个品牌路由器对比分析》在选择路由器时,Tenda和锐捷都是备受关注的品牌,各自有独特的产品特点和市场定位,选择哪个品牌的路由器更合适,实际上取决于你的具体需求和使用场景,我们从... 在选购路由器时,锐捷和腾达都是市场上备受关注的品牌,但它们的定位和特点却有所不同。锐捷更偏向企业级和专

Spring中Bean有关NullPointerException异常的原因分析

《Spring中Bean有关NullPointerException异常的原因分析》在Spring中使用@Autowired注解注入的bean不能在静态上下文中访问,否则会导致NullPointerE... 目录Spring中Bean有关NullPointerException异常的原因问题描述解决方案总结