func.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #!/bin/bash
  2. # Some useful functions for cc testing
  3. # Detecting whether can import the header file to render colorful cli output
  4. if [ -f ./header.sh ]; then
  5. source ./header.sh
  6. elif [ -f scripts/header.sh ]; then
  7. source scripts/header.sh
  8. else
  9. echo_r() {
  10. echo "$@"
  11. }
  12. echo_g() {
  13. echo "$@"
  14. }
  15. echo_b() {
  16. echo "$@"
  17. }
  18. fi
  19. # Define those global variables
  20. if [ -f ./variables.sh ]; then
  21. source ./variables.sh
  22. elif [ -f scripts/variables.sh ]; then
  23. source scripts/variables.sh
  24. else
  25. echo "Cannot find the variables.sh files, pls check"
  26. exit 1
  27. fi
  28. # Verify $1 is not 0, then output error msg $2 and exit
  29. verifyResult () {
  30. if [ $1 -ne 0 ] ; then
  31. echo_b "$2"
  32. echo_r "=== ERROR !!! FAILED to execute End-2-End Scenario ==="
  33. exit 1
  34. fi
  35. }
  36. setOrdererEnvs () {
  37. CORE_PEER_LOCALMSPID="OrdererMSP"
  38. CORE_PEER_MSPCONFIGPATH=${ORDERER_ADMIN_MSP}
  39. CORE_PEER_TLS_ROOTCERT_FILE=${ORDERER_TLS_ROOTCERT}
  40. #t="\${ORG${org}_PEER${peer}_URL}" && CORE_PEER_ADDRESS=`eval echo $t`
  41. }
  42. # Set global env variables for fabric usage
  43. # Usage: setEnvs org peer
  44. setEnvs () {
  45. local org=$1 # 1 or 2
  46. local peer=$2 # 0 or 1
  47. local t=""
  48. CORE_PEER_LOCALMSPID="Org${org}MSP"
  49. #CORE_PEER_MSPCONFIGPATH=\$${ORG${org}_ADMIN_MSP}
  50. t="\${ORG${org}_ADMIN_MSP}" && CORE_PEER_MSPCONFIGPATH=`eval echo $t`
  51. t="\${ORG${org}_PEER${peer}_TLS_ROOTCERT}" && CORE_PEER_TLS_ROOTCERT_FILE=`eval echo $t`
  52. t="\${ORG${org}_PEER${peer}_URL}" && CORE_PEER_ADDRESS=`eval echo $t`
  53. # env |grep CORE
  54. }
  55. checkOSNAvailability() {
  56. #Use orderer's MSP for fetching system channel config block
  57. CORE_PEER_LOCALMSPID="OrdererMSP"
  58. CORE_PEER_TLS_ROOTCERT_FILE=${ORDERER_TLS_CA}
  59. CORE_PEER_MSPCONFIGPATH=${ORDERER_MSP}
  60. local rc=1
  61. local starttime=$(date +%s)
  62. # continue to poll
  63. # we either get a successful response, or reach TIMEOUT
  64. while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0
  65. do
  66. sleep 3
  67. echo "Attempting to fetch system channel ...$(($(date +%s)-starttime)) secs"
  68. if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  69. peer channel fetch 0 -o ${ORDERER_URL} -c "testchainid" >&log.txt
  70. else
  71. peer channel fetch 0 -o ${ORDERER_URL} -c "testchainid" --tls $CORE_PEER_TLS_ENABLED --cafile ${ORDERER_TLS_CA} >&log.txt
  72. fi
  73. test $? -eq 0 && VALUE=$(cat log.txt | awk '/Received block/ {print $NF}')
  74. test "$VALUE" = "0" && let rc=0
  75. done
  76. cat log.txt
  77. verifyResult $rc "Ordering Service is not available, Please try again ..."
  78. echo "=== Ordering Service is up and running === "
  79. echo
  80. }
  81. # Internal func called by channelCreate
  82. channelCreateAction(){
  83. local channel=$1
  84. local channel_tx=$2
  85. if [ -z "$CORE_PEER_TLS_ENABLED" ] || [ "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  86. peer channel create \
  87. -o ${ORDERER_URL} \
  88. -c ${channel} \
  89. -f ${CHANNEL_ARTIFACTS}/${channel_tx} \
  90. --timeout $TIMEOUT \
  91. >&log.txt
  92. else
  93. peer channel create \
  94. -o ${ORDERER_URL} \
  95. -c ${channel} \
  96. -f ${CHANNEL_ARTIFACTS}/${channel_tx} \
  97. --timeout $TIMEOUT \
  98. --tls $CORE_PEER_TLS_ENABLED \
  99. --cafile ${ORDERER_TLS_CA} \
  100. >&log.txt
  101. fi
  102. return $?
  103. }
  104. # Use peer0/org1 to create a channel
  105. # channelCreate APP_CHANNEL APP_CHANNEL.tx org peer
  106. channelCreate() {
  107. local channel=$1
  108. local channel_tx=$2
  109. local org=$3
  110. local peer=$4
  111. echo_b "=== Create Channel ${channel} by org $org peer $peer === "
  112. local counter=0
  113. setEnvs $org $peer
  114. channelCreateAction "${channel}" "${channel_tx}"
  115. local res=$?
  116. while [ ${counter} -lt ${MAX_RETRY} -a ${res} -ne 0 ]; do
  117. echo_b "Failed to create channel $channel, retry after 3s"
  118. sleep 3
  119. channelCreateAction "${channel}" "${channel_tx}"
  120. res=$?
  121. let counter=${counter}+1
  122. #COUNTER=` expr $COUNTER + 1`
  123. done
  124. cat log.txt
  125. verifyResult ${res} "Channel ${channel} creation failed"
  126. echo_g "=== Channel ${channel} is created. === "
  127. }
  128. # called by channelJoinWithRetry
  129. channelJoinAction () {
  130. local channel=$1
  131. peer channel join \
  132. -b ${channel}.block \
  133. >&log.txt
  134. }
  135. ## Sometimes Join takes time hence RETRY atleast for 5 times
  136. channelJoinWithRetry () {
  137. local channel=$1
  138. local peer=$2
  139. local counter=0
  140. channelJoinAction ${channel}
  141. local res=$?
  142. while [ ${counter} -lt ${MAX_RETRY} -a ${res} -ne 0 ]; do
  143. echo_b "peer${peer} failed to join channel ${channel}, retry after 2s"
  144. sleep 2
  145. channelJoinAction ${channel}
  146. res=$?
  147. let counter=${counter}+1
  148. done
  149. cat log.txt
  150. verifyResult ${res} "After $MAX_RETRY attempts, peer${peer} failed to Join the Channel"
  151. }
  152. # Join given (by default all) peers into the channel
  153. # channelJoin channel org peer
  154. channelJoin () {
  155. local channel=$1
  156. local org=$2
  157. local peer=$3
  158. echo_b "=== Join org$org/peer$peer into channel ${channel} === "
  159. setEnvs $org $peer
  160. channelJoinWithRetry ${channel} $peer
  161. echo_g "=== org$org/peer$peer joined into channel ${channel} === "
  162. }
  163. # Update the anchor peer at given channel
  164. # updateAnchorPeers channel peer
  165. updateAnchorPeers() {
  166. local channel=$1
  167. local org=$2
  168. local peer=$3
  169. setEnvs $org $peer
  170. echo_b "=== Update Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on ${channel} === "
  171. if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  172. peer channel update \
  173. -o ${ORDERER_URL} \
  174. -c ${channel} \
  175. -f ${CHANNEL_ARTIFACTS}/${CORE_PEER_LOCALMSPID}anchors.tx \
  176. >&log.txt
  177. else
  178. peer channel update \
  179. -o ${ORDERER_URL} \
  180. -c ${channel} \
  181. -f ${CHANNEL_ARTIFACTS}/${CORE_PEER_LOCALMSPID}anchors.tx \
  182. --tls $CORE_PEER_TLS_ENABLED \
  183. --cafile ${ORDERER_TLS_CA} \
  184. >&log.txt
  185. fi
  186. res=$?
  187. cat log.txt
  188. verifyResult $res "Anchor peer update failed"
  189. echo_g "=== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on ${channel} is updated. === "
  190. sleep 2
  191. }
  192. # Install chaincode on specified peer node
  193. # chaincodeInstall peer cc_name version path
  194. chaincodeInstall () {
  195. local org=$1
  196. local peer=$2
  197. local name=$3
  198. local version=$4
  199. local path=$5
  200. echo_b "=== Install Chaincode $name:$version ($path) on org${org} peer$peer === "
  201. setEnvs $org $peer
  202. peer chaincode install \
  203. -n ${name} \
  204. -v $version \
  205. -p ${path} \
  206. >&log.txt
  207. res=$?
  208. cat log.txt
  209. verifyResult $res "Chaincode installation on remote peer$peer has Failed"
  210. echo_g "=== Chaincode is installed on remote peer$peer === "
  211. }
  212. # Instantiate chaincode on specifized peer node
  213. # chaincodeInstantiate channel org peer name version args
  214. chaincodeInstantiate () {
  215. local channel=$1
  216. local org=$2
  217. local peer=$3
  218. local name=$4
  219. local version=$5
  220. local args=$6
  221. setEnvs $org $peer
  222. echo_b "=== chaincodeInstantiate for channel ${channel} on org $org peer $peer ===="
  223. # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
  224. # lets supply it directly as we know it using the "-o" option
  225. if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  226. peer chaincode instantiate \
  227. -o ${ORDERER_URL} \
  228. -C ${channel} \
  229. -n ${name} \
  230. -v ${version} \
  231. -c ${args} \
  232. -P "OR ('Org1MSP.member','Org2MSP.member')" \
  233. >&log.txt
  234. else
  235. peer chaincode instantiate \
  236. -o ${ORDERER_URL} \
  237. -C ${channel} \
  238. -n ${name} \
  239. -v ${version} \
  240. -c ${args} \
  241. -P "OR ('Org1MSP.member','Org2MSP.member')" \
  242. --tls $CORE_PEER_TLS_ENABLED \
  243. --cafile ${ORDERER_TLS_CA} \
  244. >&log.txt
  245. fi
  246. res=$?
  247. cat log.txt
  248. verifyResult $res "ChaincodeInstantiation on peer$peer in channel ${channel} failed"
  249. echo_g "=== Chaincode Instantiated in channel ${channel} by peer$peer ==="
  250. }
  251. # Usage: chaincodeInvoke channel org peer name args
  252. chaincodeInvoke () {
  253. local channel=$1
  254. local org=$2
  255. local peer=$3
  256. local name=$4
  257. local args=$5
  258. echo_g "=== Invoke transaction on peer$peer in channel ${channel} === "
  259. setEnvs $org $peer
  260. # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
  261. # lets supply it directly as we know it using the "-o" option
  262. if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  263. peer chaincode invoke \
  264. -o ${ORDERER_URL} \
  265. -C ${channel} \
  266. -n ${name} \
  267. -c ${args} \
  268. >&log.txt
  269. else
  270. peer chaincode invoke \
  271. -o ${ORDERER_URL} \
  272. -C ${channel} \
  273. -n ${name} \
  274. -c ${args} \
  275. --tls $CORE_PEER_TLS_ENABLED \
  276. --cafile ${ORDERER_TLS_CA} \
  277. >&log.txt
  278. fi
  279. res=$?
  280. cat log.txt
  281. verifyResult $res "Invoke execution on peer$peer failed "
  282. echo_g "=== Invoke transaction on peer$peer in channel ${channel} is successful === "
  283. }
  284. # query channel peer name args expected_result
  285. chaincodeQuery () {
  286. local channel=$1
  287. local org=$2
  288. local peer=$3
  289. local name=$4
  290. local args=$5
  291. [ $# -gt 5 ] && local expected_result=$6
  292. echo_b "=== Querying on org$org peer$peer in channel ${channel}... === "
  293. local rc=1
  294. local starttime=$(date +%s)
  295. setEnvs $org $peer
  296. # we either get a successful response, or reach TIMEOUT
  297. while [ "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0 ]; do
  298. echo_b "Attempting to Query peer$peer ...$(($(date +%s)-starttime)) secs"
  299. peer chaincode query \
  300. -C "${channel}" \
  301. -n "${name}" \
  302. -c "${args}" \
  303. >&log.txt
  304. rc=$?
  305. if [ $# -gt 5 ]; then # need to check the result
  306. test $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')
  307. test "$VALUE" = "${expected_result}" && let rc=0
  308. fi
  309. cat log.txt
  310. if [ $rc -ne 0 ]; then
  311. sleep 2
  312. fi
  313. done
  314. if [ $rc -eq 0 ]; then
  315. echo_g "=== Query on peer$peer in channel ${channel} is successful === "
  316. else
  317. echo_r "=== Query result on peer$peer is INVALID, run `make stop clean` to clean ==="
  318. exit 1
  319. fi
  320. }
  321. # Start chaincode with dev mode
  322. chaincodeStartDev () {
  323. local peer=$1
  324. local version=$2
  325. #setEnvs $peer
  326. #setEnvs 1 0
  327. setEnvs 1 0
  328. CORE_CHAINCODE_LOGLEVEL=debug \
  329. CORE_PEER_ADDRESS=peer${peer}.org1.example.com:7052 \
  330. CORE_CHAINCODE_ID_NAME=mycc:${version} \
  331. nohup ./scripts/chaincode_example02 > chaincode_dev.log &
  332. res=$?
  333. cat log.txt
  334. verifyResult $res "Chaincode start in dev mode has Failed"
  335. echo_g "=== Chaincode started in dev mode === "
  336. echo
  337. }
  338. # chaincodeUpgrade channel peer name version args
  339. chaincodeUpgrade () {
  340. local channel=$1
  341. local org=$2
  342. local peer=$3
  343. local name=$4
  344. local version=$5
  345. local args=$6
  346. echo_b "=== Upgrade chaincode to version $version on peer$peer in channel ${channel} === "
  347. setEnvs $org $peer
  348. # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
  349. # lets supply it directly as we know it using the "-o" option
  350. if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
  351. peer chaincode upgrade \
  352. -o ${ORDERER_URL} \
  353. -C ${channel} \
  354. -n ${name} \
  355. -v ${version} \
  356. -c ${args} \
  357. >&log.txt
  358. else
  359. peer chaincode upgrade \
  360. -o ${ORDERER_URL} \
  361. -C ${channel} \
  362. -n ${name} \
  363. -v ${version} \
  364. -c ${args} \
  365. --tls $CORE_PEER_TLS_ENABLED \
  366. --cafile ${ORDERER_TLS_CA} \
  367. >&log.txt
  368. fi
  369. res=$?
  370. cat log.txt
  371. verifyResult $res "Upgrade execution on peer$peer failed "
  372. echo_g "=== Upgrade transaction on peer$peer in channel ${channel} is successful === "
  373. echo
  374. }
  375. # Fetch some block from a given channel: channel, peer, blockNum
  376. channelFetch () {
  377. local channel=$1
  378. local org=$2
  379. local peer=$3
  380. local num=$4
  381. echo_b "=== Fetch block $num of channel $channel === "
  382. #setEnvs $org $peer
  383. setOrdererEnvs
  384. # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
  385. # lets supply it directly as we know it using the "-o" option
  386. if [ -z "${CORE_PEER_TLS_ENABLED}" ] || [ "${CORE_PEER_TLS_ENABLED}" = "false" ]; then
  387. peer channel fetch $num ${CHANNEL_ARTIFACTS}/${channel}_${num}.block \
  388. -o ${ORDERER_URL} \
  389. -c ${channel} \
  390. >&log.txt
  391. else
  392. peer channel fetch $num ${CHANNEL_ARTIFACTS}/${channel}_${num}.block \
  393. -o ${ORDERER_URL} \
  394. -c ${channel} \
  395. --tls \
  396. --cafile ${ORDERER_TLS_CA} \
  397. >&log.txt
  398. fi
  399. res=$?
  400. cat log.txt
  401. if [ $res -ne 0 ]; then
  402. echo_r "Fetch block $num of channel $channel failed"
  403. else
  404. echo_g "=== Fetch block $num of channel $channel is successful === "
  405. fi
  406. }
  407. # configtxlator encode json to pb
  408. # Usage: configtxlatorEncode msgType input output
  409. configtxlatorEncode() {
  410. local msgType=$1
  411. local input=$2
  412. local output=$3
  413. echo_b "Encode $input --> $output using type $msgType"
  414. curl -sX POST \
  415. --data-binary @${input} \
  416. ${CTL_ENCODE_URL}/${msgType} \
  417. >${output}
  418. }
  419. # configtxlator decode pb to json
  420. # Usage: configtxlatorEncode msgType input output
  421. configtxlatorDecode() {
  422. local msgType=$1
  423. local input=$2
  424. local output=$3
  425. echo_b "Config Decode $input --> $output using type $msgType"
  426. curl -sX POST \
  427. --data-binary @"${input}" \
  428. "${CTL_DECODE_URL}/${msgType}" \
  429. > "${output}"
  430. }
  431. # compute diff between two pb
  432. # Usage: configtxlatorCompare channel origin updated output
  433. configtxlatorCompare() {
  434. local channel=$1
  435. local origin=$2
  436. local updated=$3
  437. local output=$3
  438. echo_b "Config Compare $origin vs $updated > ${output} in channel $channel"
  439. curl -sX POST \
  440. -F channel="${channel}" \
  441. -F "original=@${origin}" \
  442. -F "updated=@${updated}" \
  443. "${CTL_COMPARE_URL}" \
  444. > "${output}"
  445. [ $? -eq 0 ] || echo_r "Failed to compute config update"
  446. }
  447. # Run cmd inside the config generator container
  448. gen_con_exec() {
  449. docker exec -it $GEN_CONTAINER "$@"
  450. }