2
0

script.sh_bak 5.9 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 meidi -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_meidi >&log.txt
  71. peer chaincode install -n meidi -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 >&log.txt
  72. res=$?
  73. cat log.txt
  74. verifyResult $res "Chaincode installation on remote peer PEER$PEER has Failed"
  75. echo "===================== Chaincode is installed on remote peer PEER$PEER ===================== "
  76. echo
  77. }
  78. instantiateChaincode () {
  79. PEER=$1
  80. setGlobals $PEER
  81. if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  82. peer chaincode instantiate -o orderer0:7050 -C $CHANNEL_NAME -n meidi -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org0MSP.member','Org1MSP.member')" >&log.txt
  83. else
  84. peer chaincode instantiate -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n meidi -v 1.0 -c '{"Args":["init","a"]}' -P "OR ('Org0MSP.member','Org1MSP.member')" >&log.txt
  85. fi
  86. res=$?
  87. cat log.txt
  88. verifyResult $res "Chaincode instantiation on PEER$PEER on channel '$CHANNEL_NAME' failed"
  89. echo "===================== Chaincode Instantiation on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
  90. echo
  91. }
  92. chaincodeQuery () {
  93. PEER=$1
  94. echo "===================== Querying on PEER$PEER on channel '$CHANNEL_NAME'... ===================== "
  95. setGlobals $PEER
  96. local rc=1
  97. local starttime=$(date +%s)
  98. # continue to poll
  99. # we either get a successful response, or reach TIMEOUT
  100. while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0
  101. do
  102. sleep 3
  103. echo "Attempting to Query PEER$PEER ...$(($(date +%s)-starttime)) secs"
  104. peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt
  105. test $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')
  106. test "$VALUE" = "$2" && let rc=0
  107. done
  108. echo
  109. cat log.txt
  110. if test $rc -eq 0 ; then
  111. echo "===================== Query on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
  112. else
  113. echo "!!!!!!!!!!!!!!! Query result on PEER$PEER is INVALID !!!!!!!!!!!!!!!!"
  114. echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
  115. echo
  116. fi
  117. }
  118. chaincodeInvoke () {
  119. PEER=$1
  120. if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  121. peer chaincode invoke -o orderer0:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
  122. else
  123. 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
  124. fi
  125. res=$?
  126. cat log.txt
  127. verifyResult $res "Invoke execution on PEER$PEER failed "
  128. echo "===================== Invoke transaction on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
  129. echo
  130. }
  131. ## Create channel
  132. createChannel
  133. ## Join all the peers to the channel
  134. joinChannel
  135. ## Install chaincode on Peer0/Org0 and Peer2/Org1
  136. installChaincode 0
  137. installChaincode 2
  138. #Instantiate chaincode on Peer2/Org1
  139. echo "Instantiating chaincode on Peer2/Org1 ..."
  140. instantiateChaincode 0
  141. #Query on chaincode on Peer0/Org0
  142. chaincodeQuery 0 100
  143. #Invoke on chaincode on Peer0/Org0
  144. echo "send Invoke transaction on Peer0/Org0 ..."
  145. chaincodeInvoke 0
  146. ## Install chaincode on Peer3/Org1
  147. installChaincode 3
  148. #Query on chaincode on Peer3/Org1, check if the result is 90
  149. chaincodeQuery 3 90
  150. echo
  151. echo "===================== All GOOD, End-2-End execution completed ===================== "
  152. echo
  153. exit 0