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