Guest User

Untitled

a guest
Oct 16th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. echo
  4. echo " ____ _____ _ ____ _____ "
  5. echo "/ ___| |_ _| / \ | _ \ |_ _|"
  6. echo "\___ \ | | / _ \ | |_) | | | "
  7. echo " ___) | | | / ___ \ | _ < | | "
  8. echo "|____/ |_| /_/ \_\ |_| \_\ |_| "
  9. echo
  10. echo "Build multi host network (BMHN) end-to-end test"
  11. echo
  12. CHANNEL_NAME="$1"
  13. DELAY="$2"
  14. : ${CHANNEL_NAME:="mychannel"}
  15. : ${TIMEOUT:="60"}
  16. COUNTER=1
  17. MAX_RETRY=5
  18. 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
  19.  
  20. echo "Channel name : "$CHANNEL_NAME
  21.  
  22. # verify the result of the end-to-end test
  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. fi
  45.  
  46. env |grep CORE
  47. }
  48.  
  49. createChannel() {
  50. setGlobals 0
  51.  
  52.  
  53. peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt
  54. res=$?
  55. cat log.txt
  56. verifyResult $res "Channel creation failed"
  57. echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== "
  58. echo
  59. }
  60.  
  61. updateAnchorPeers() {
  62. PEER=$1
  63. setGlobals $PEER
  64. peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
  65. res=$?
  66. cat log.txt
  67. verifyResult $res "Anchor peer update failed"
  68. echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== "
  69. sleep $DELAY
  70. echo
  71. }
  72.  
  73. ## Sometimes Join takes time hence RETRY atleast for 5 times
  74. joinWithRetry () {
  75. peer channel join -b $CHANNEL_NAME.block >&log.txt
  76. res=$?
  77. cat log.txt
  78. if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then
  79. COUNTER=` expr $COUNTER + 1`
  80. echo "PEER$1 failed to join the channel, Retry after 2 seconds"
  81. sleep $DELAY
  82. joinWithRetry $1
  83. else
  84. COUNTER=1
  85. fi
  86. verifyResult $res "After $MAX_RETRY attempts, PEER$ch has failed to Join the Channel"
  87. }
  88.  
  89. joinChannel () {
  90. for ch in 0 1; do
  91. setGlobals $ch
  92. joinWithRetry $ch
  93. echo "===================== PEER$ch joined on the channel \"$CHANNEL_NAME\" ===================== "
  94. sleep $DELAY
  95. echo
  96. done
  97. }
  98.  
  99. installChaincode () {
  100. PEER=$1
  101. setGlobals $PEER
  102. peer chaincode install -n mychannel -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/supplyv11 >&log.txt
  103. res=$?
  104. cat log.txt
  105. verifyResult $res "Chaincode installation on remote peer PEER$PEER has Failed"
  106. echo "===================== Chaincode is installed on remote peer PEER$PEER ===================== "
  107. echo
  108. }
  109.  
  110. instantiateChaincode () {
  111. PEER=$1
  112. setGlobals $PEER
  113. # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
  114. # lets supply it directly as we know it using the "-o" option
  115. peer chaincode instantiate -o orderer.example.com:7050 -C $CHANNEL_NAME -n mychannel -v 1.0 -c '{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')" >&log.txt
  116. res=$?
  117. cat log.txt
  118. verifyResult $res "Chaincode instantiation on PEER$PEER on channel '$CHANNEL_NAME' failed"
  119. echo "===================== Chaincode Instantiation on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
  120. echo
  121. }
  122.  
  123. chaincodeQuery () {
  124. PEER=$1
  125. echo "===================== Querying on PEER$PEER on channel '$CHANNEL_NAME'... ===================== "
  126. setGlobals $PEER
  127. local rc=1
  128. local starttime=$(date +%s)
  129.  
  130. # continue to poll
  131. # we either get a successful response, or reach TIMEOUT
  132. while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0
  133. do
  134. sleep $DELAY
  135. echo "Attempting to Query PEER$PEER ...$(($(date +%s)-starttime)) secs"
  136. peer chaincode query -C $CHANNEL_NAME -n mychannel -c '{"Args":[""]}' >&log.txt
  137. test $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')
  138. test "$VALUE" = "$2" && let rc=0
  139. done
  140. echo
  141. cat log.txt
  142. if test $rc -eq 0 ; then
  143. echo "===================== Query on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
  144. else
  145. echo "!!!!!!!!!!!!!!! Query result on PEER$PEER is INVALID !!!!!!!!!!!!!!!!"
  146. echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
  147. echo
  148. exit 1
  149. fi
  150. }
  151.  
  152. chaincodeInvoke () {
  153. PEER=$1
  154. setGlobals $PEER
  155. # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
  156. # lets supply it directly as we know it using the "-o" option
  157. peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mychannel -c '{"Args":[""]}' >&log.txt
  158. res=$?
  159. cat log.txt
  160. verifyResult $res "Invoke execution on PEER$PEER failed "
  161. echo "===================== Invoke transaction on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
  162. echo
  163. }
  164.  
  165. ## Create channel
  166. echo "Creating channel..."
  167. createChannel
  168.  
  169. ## Join all the peers to the channel
  170. echo "Having all peers join the channel..."
  171. joinChannel
  172.  
  173. ## Set the anchor peers for each org in the channel
  174. echo "Updating anchor peers for org1..."
  175. updateAnchorPeers 0
  176.  
  177. ## Install chaincode on Peer0/Org1 and Peer2/Org2
  178. echo "Installing chaincode on org1/peer0..."
  179. installChaincode 0
  180. echo "Install chaincode on org2/peer2..."
  181. installChaincode 1
  182.  
  183. # #Instantiate chaincode on Peer2/Org2
  184. # echo "Instantiating chaincode on org1/peer0..."
  185. # instantiateChaincode 0
  186.  
  187. # #Query on chaincode on Peer0/Org1
  188. # echo "Querying chaincode on org1/peer0..."
  189. # chaincodeQuery 0 100
  190.  
  191. # #Invoke on chaincode on Peer0/Org1
  192. # echo "Sending invoke transaction on org1/peer0..."
  193. # chaincodeInvoke 0
  194.  
  195. # #Query on chaincode on Peer1/Org1, check if the result is 90
  196. # echo "Querying chaincode on org2/peer3..."
  197. # chaincodeQuery 1 90
  198.  
  199. echo
  200. echo "========= All GOOD, BMHN execution completed =========== "
  201. echo
  202.  
  203. echo
  204. echo " _____ _ _ ____ "
  205. echo "| ____| | \ | | | _ \ "
  206. echo "| _| | \| | | | | | "
  207. echo "| |___ | |\ | | |_| | "
  208. echo "|_____| |_| \_| |____/ "
  209. echo
  210.  
  211. exit 0
Add Comment
Please, Sign In to add comment