github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/examples/e2e_cli/scripts/script.sh (about)

     1  #!/bin/bash
     2  # Copyright London Stock Exchange Group All Rights Reserved.
     3  #
     4  # SPDX-License-Identifier: Apache-2.0
     5  #
     6  echo
     7  echo " ____    _____      _      ____    _____           _____   ____    _____ "
     8  echo "/ ___|  |_   _|    / \    |  _ \  |_   _|         | ____| |___ \  | ____|"
     9  echo "\___ \    | |     / _ \   | |_) |   | |    _____  |  _|     __) | |  _|  "
    10  echo " ___) |   | |    / ___ \  |  _ <    | |   |_____| | |___   / __/  | |___ "
    11  echo "|____/    |_|   /_/   \_\ |_| \_\   |_|           |_____| |_____| |_____|"
    12  echo
    13  
    14  CHANNEL_NAME="$1"
    15  : ${CHANNEL_NAME:="mychannel"}
    16  : ${TIMEOUT:="60"}
    17  COUNTER=1
    18  MAX_RETRY=5
    19  ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
    20  
    21  echo "Channel name : "$CHANNEL_NAME
    22  
    23  verifyResult () {
    24  	if [ $1 -ne 0 ] ; then
    25  		echo "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!"
    26                  echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
    27  		echo
    28     		exit 1
    29  	fi
    30  }
    31  
    32  setGlobals () {
    33  
    34  	if [ $1 -eq 0 -o $1 -eq 1 ] ; then
    35  		CORE_PEER_LOCALMSPID="Org1MSP"
    36  		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
    37  		CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
    38  		if [ $1 -eq 0 ]; then
    39  			CORE_PEER_ADDRESS=peer0.org1.example.com:7051
    40  		else
    41  			CORE_PEER_ADDRESS=peer1.org1.example.com:7051
    42  		fi
    43  	else
    44  		CORE_PEER_LOCALMSPID="Org2MSP"
    45  		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.crt
    46  		CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
    47  		if [ $1 -eq 2 ]; then
    48  			CORE_PEER_ADDRESS=peer0.org2.example.com:7051
    49  		else
    50  			CORE_PEER_ADDRESS=peer1.org2.example.com:7051
    51  		fi
    52  	fi
    53  
    54  	env |grep CORE
    55  }
    56  
    57  createChannel() {
    58  	setGlobals 0
    59  
    60          if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
    61  		peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt
    62  	else
    63  		peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt
    64  	fi
    65  	res=$?
    66  	cat log.txt
    67  	verifyResult $res "Channel creation failed"
    68  	echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== "
    69  	echo
    70  }
    71  
    72  updateAnchorPeers() {
    73          PEER=$1
    74          setGlobals $PEER
    75  
    76          if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
    77  		peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
    78  	else
    79  		peer 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.txt
    80  	fi
    81  	res=$?
    82  	cat log.txt
    83  	verifyResult $res "Anchor peer update failed"
    84  	echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== "
    85  	sleep 5
    86  	echo
    87  }
    88  
    89  ## Sometimes Join takes time hence RETRY atleast for 5 times
    90  joinWithRetry () {
    91  	peer channel join -b $CHANNEL_NAME.block  >&log.txt
    92  	res=$?
    93  	cat log.txt
    94  	if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then
    95  		COUNTER=` expr $COUNTER + 1`
    96  		echo "PEER$1 failed to join the channel, Retry after 2 seconds"
    97  		sleep 2
    98  		joinWithRetry $1
    99  	else
   100  		COUNTER=1
   101  	fi
   102          verifyResult $res "After $MAX_RETRY attempts, PEER$ch has failed to Join the Channel"
   103  }
   104  
   105  joinChannel () {
   106  	for ch in 0 1 2 3; do
   107  		setGlobals $ch
   108  		joinWithRetry $ch
   109  		echo "===================== PEER$ch joined on the channel \"$CHANNEL_NAME\" ===================== "
   110  		sleep 2
   111  		echo
   112  	done
   113  }
   114  
   115  installChaincode () {
   116  	PEER=$1
   117  	setGlobals $PEER
   118  	peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 >&log.txt
   119  	res=$?
   120  	cat log.txt
   121          verifyResult $res "Chaincode installation on remote peer PEER$PEER has Failed"
   122  	echo "===================== Chaincode is installed on remote peer PEER$PEER ===================== "
   123  	echo
   124  }
   125  
   126  instantiateChaincode () {
   127  	PEER=$1
   128  	setGlobals $PEER
   129  	# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
   130  	# lets supply it directly as we know it using the "-o" option
   131  	if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
   132  		peer 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.txt
   133  	else
   134  		peer 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.txt
   135  	fi
   136  	res=$?
   137  	cat log.txt
   138  	verifyResult $res "Chaincode instantiation on PEER$PEER on channel '$CHANNEL_NAME' failed"
   139  	echo "===================== Chaincode Instantiation on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
   140  	echo
   141  }
   142  
   143  chaincodeQuery () {
   144    PEER=$1
   145    echo "===================== Querying on PEER$PEER on channel '$CHANNEL_NAME'... ===================== "
   146    setGlobals $PEER
   147    local rc=1
   148    local starttime=$(date +%s)
   149  
   150    # continue to poll
   151    # we either get a successful response, or reach TIMEOUT
   152    while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0
   153    do
   154       sleep 3
   155       echo "Attempting to Query PEER$PEER ...$(($(date +%s)-starttime)) secs"
   156       peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt
   157       test $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')
   158       test "$VALUE" = "$2" && let rc=0
   159    done
   160    echo
   161    cat log.txt
   162    if test $rc -eq 0 ; then
   163  	echo "===================== Query on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
   164    else
   165  	echo "!!!!!!!!!!!!!!! Query result on PEER$PEER is INVALID !!!!!!!!!!!!!!!!"
   166          echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
   167  	echo
   168  	exit 1
   169    fi
   170  }
   171  
   172  chaincodeInvoke () {
   173  	PEER=$1
   174  	setGlobals $PEER
   175  	# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
   176  	# lets supply it directly as we know it using the "-o" option
   177  	if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
   178  		peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
   179  	else
   180  		peer 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.txt
   181  	fi
   182  	res=$?
   183  	cat log.txt
   184  	verifyResult $res "Invoke execution on PEER$PEER failed "
   185  	echo "===================== Invoke transaction on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
   186  	echo
   187  }
   188  
   189  ## Create channel
   190  echo "Creating channel..."
   191  createChannel
   192  
   193  ## Join all the peers to the channel
   194  echo "Having all peers join the channel..."
   195  joinChannel
   196  
   197  ## Set the anchor peers for each org in the channel
   198  echo "Updating anchor peers for org1..."
   199  updateAnchorPeers 0
   200  echo "Updating anchor peers for org2..."
   201  updateAnchorPeers 2
   202  
   203  ## Install chaincode on Peer0/Org1 and Peer2/Org2
   204  echo "Installing chaincode on org1/peer0..."
   205  installChaincode 0
   206  echo "Install chaincode on org2/peer2..."
   207  installChaincode 2
   208  
   209  #Instantiate chaincode on Peer2/Org2
   210  echo "Instantiating chaincode on org2/peer2..."
   211  instantiateChaincode 2
   212  
   213  #Query on chaincode on Peer0/Org1
   214  echo "Querying chaincode on org1/peer0..."
   215  chaincodeQuery 0 100
   216  
   217  #Invoke on chaincode on Peer0/Org1
   218  echo "Sending invoke transaction on org1/peer0..."
   219  chaincodeInvoke 0
   220  
   221  ## Install chaincode on Peer3/Org2
   222  echo "Installing chaincode on org2/peer3..."
   223  installChaincode 3
   224  
   225  #Query on chaincode on Peer3/Org2, check if the result is 90
   226  echo "Querying chaincode on org2/peer3..."
   227  chaincodeQuery 3 90
   228  
   229  echo
   230  echo "===================== All GOOD, End-2-End execution completed ===================== "
   231  echo
   232  
   233  echo
   234  echo " _____   _   _   ____            _____   ____    _____ "
   235  echo "| ____| | \ | | |  _ \          | ____| |___ \  | ____|"
   236  echo "|  _|   |  \| | | | | |  _____  |  _|     __) | |  _|  "
   237  echo "| |___  | |\  | | |_| | |_____| | |___   / __/  | |___ "
   238  echo "|_____| |_| \_| |____/          |_____| |_____| |_____|"
   239  echo
   240  
   241  exit 0