2
0

script.sh 5.3 KB

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