script.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/bash
  2. CHANNEL_NAME="$1"
  3. : ${CHANNEL_NAME:="mychannel"}
  4. : ${TIMEOUT:="60"}
  5. COUNTER=0
  6. MAX_RETRY=5
  7. ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem
  8. echo "Channel name : "$CHANNEL_NAME
  9. verifyResult () {
  10. if [ $1 -ne 0 ] ; then
  11. echo "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!"
  12. echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
  13. echo
  14. exit 1
  15. fi
  16. }
  17. setGlobals () {
  18. CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peer/peer$1/localMspConfig
  19. CORE_PEER_ADDRESS=peer$1:7051
  20. if [ $1 -eq 0 -o $1 -eq 1 ] ; then
  21. CORE_PEER_LOCALMSPID="Org0MSP"
  22. CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peer/peer$1/localMspConfig/cacerts/peerOrg0.pem
  23. else
  24. CORE_PEER_LOCALMSPID="Org1MSP"
  25. CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peer/peer$1/localMspConfig/cacerts/peerOrg1.pem
  26. fi
  27. env |grep CORE
  28. }
  29. createChannel() {
  30. CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig
  31. CORE_PEER_LOCALMSPID="OrdererMSP"
  32. if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  33. peer channel create -o orderer0:7050 -c $CHANNEL_NAME -f crypto/orderer/channel.tx >&log.txt
  34. else
  35. peer channel create -o orderer0:7050 -c $CHANNEL_NAME -f crypto/orderer/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt
  36. fi
  37. res=$?
  38. cat log.txt
  39. verifyResult $res "Channel creation failed"
  40. echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== "
  41. echo
  42. }
  43. ## Sometimes Join takes time hence RETRY atleast for 5 times
  44. joinWithRetry () {
  45. peer channel join -b $CHANNEL_NAME.block >&log.txt
  46. res=$?
  47. cat log.txt
  48. if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then
  49. COUNTER=` expr $COUNTER + 1`
  50. echo "PEER$1 failed to join the channel, Retry after 2 seconds"
  51. sleep 2
  52. joinWithRetry $1
  53. else
  54. COUNTER=0
  55. fi
  56. verifyResult $res "After $MAX_RETRY attempts, PEER$ch has failed to Join the Channel"
  57. }
  58. joinChannel () {
  59. for ch in 0 1 2 3; do
  60. setGlobals $ch
  61. joinWithRetry $ch
  62. echo "===================== PEER$ch joined on the channel \"$CHANNEL_NAME\" ===================== "
  63. sleep 2
  64. echo
  65. done
  66. }
  67. installChaincode () {
  68. PEER=$1
  69. setGlobals $PEER
  70. peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 >&log.txt
  71. res=$?
  72. cat log.txt
  73. verifyResult $res "Chaincode installation on remote peer PEER$PEER has Failed"
  74. echo "===================== Chaincode is installed on remote peer PEER$PEER ===================== "
  75. echo
  76. }
  77. instantiateChaincode () {
  78. PEER=$1
  79. setGlobals $PEER
  80. if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  81. peer chaincode instantiate -o orderer0:7050 -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org0MSP.member','Org1MSP.member')" >&log.txt
  82. else
  83. peer chaincode instantiate -o orderer0: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 ('Org0MSP.member','Org1MSP.member')" >&log.txt
  84. fi
  85. res=$?
  86. cat log.txt
  87. verifyResult $res "Chaincode instantiation on PEER$PEER on channel '$CHANNEL_NAME' failed"
  88. echo "===================== Chaincode Instantiation on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
  89. echo
  90. }
  91. chaincodeQuery () {
  92. PEER=$1
  93. echo "===================== Querying on PEER$PEER on channel '$CHANNEL_NAME'... ===================== "
  94. setGlobals $PEER
  95. local rc=1
  96. local starttime=$(date +%s)
  97. # continue to poll
  98. # we either get a successful response, or reach TIMEOUT
  99. while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0
  100. do
  101. sleep 3
  102. echo "Attempting to Query PEER$PEER ...$(($(date +%s)-starttime)) secs"
  103. peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt
  104. test $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')
  105. test "$VALUE" = "$2" && let rc=0
  106. done
  107. echo
  108. cat log.txt
  109. if test $rc -eq 0 ; then
  110. echo "===================== Query on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
  111. else
  112. echo "!!!!!!!!!!!!!!! Query result on PEER$PEER is INVALID !!!!!!!!!!!!!!!!"
  113. echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
  114. echo
  115. fi
  116. }
  117. chaincodeInvoke () {
  118. PEER=$1
  119. if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  120. peer chaincode invoke -o orderer0:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
  121. else
  122. peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
  123. fi
  124. res=$?
  125. cat log.txt
  126. verifyResult $res "Invoke execution on PEER$PEER failed "
  127. echo "===================== Invoke transaction on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
  128. echo
  129. }
  130. ## Create channel
  131. createChannel
  132. ## Join all the peers to the channel
  133. joinChannel
  134. ## Install chaincode on Peer0/Org0 and Peer2/Org1
  135. installChaincode 0
  136. installChaincode 2
  137. #Instantiate chaincode on Peer2/Org1
  138. echo "Instantiating chaincode on Peer2/Org1 ..."
  139. instantiateChaincode 0
  140. #Query on chaincode on Peer0/Org0
  141. chaincodeQuery 0 100
  142. #Invoke on chaincode on Peer0/Org0
  143. echo "send Invoke transaction on Peer0/Org0 ..."
  144. chaincodeInvoke 0
  145. ## Install chaincode on Peer3/Org1
  146. installChaincode 3
  147. #Query on chaincode on Peer3/Org1, check if the result is 90
  148. chaincodeQuery 3 90
  149. echo
  150. echo "===================== All GOOD, End-2-End execution completed ===================== "
  151. echo
  152. exit 0