matiru

Blockchain-Based Supply Chain Transparency for Agricultural Produce

fabric/network/scripts/deployCC.sh (9144B)


  1#!/usr/bin/env bash
  2
  3source scripts/utils.sh
  4
  5CHANNEL_NAME=${1:-"mychannel"}
  6CC_NAME=${2}
  7CC_SRC_PATH=${3}
  8CC_SRC_LANGUAGE=${4}
  9CC_VERSION=${5:-"1.0"}
 10CC_SEQUENCE=${6:-"1"}
 11CC_INIT_FCN=${7:-"NA"}
 12CC_END_POLICY=${8:-"NA"}
 13CC_COLL_CONFIG=${9:-"NA"}
 14DELAY=${10:-"3"}
 15MAX_RETRY=${11:-"5"}
 16VERBOSE=${12:-"false"}
 17
 18println "executing with the following"
 19println "- CHANNEL_NAME: ${C_GREEN}${CHANNEL_NAME}${C_RESET}"
 20println "- CC_NAME: ${C_GREEN}${CC_NAME}${C_RESET}"
 21println "- CC_SRC_PATH: ${C_GREEN}${CC_SRC_PATH}${C_RESET}"
 22println "- CC_SRC_LANGUAGE: ${C_GREEN}${CC_SRC_LANGUAGE}${C_RESET}"
 23println "- CC_VERSION: ${C_GREEN}${CC_VERSION}${C_RESET}"
 24println "- CC_SEQUENCE: ${C_GREEN}${CC_SEQUENCE}${C_RESET}"
 25println "- CC_END_POLICY: ${C_GREEN}${CC_END_POLICY}${C_RESET}"
 26println "- CC_COLL_CONFIG: ${C_GREEN}${CC_COLL_CONFIG}${C_RESET}"
 27println "- CC_INIT_FCN: ${C_GREEN}${CC_INIT_FCN}${C_RESET}"
 28println "- DELAY: ${C_GREEN}${DELAY}${C_RESET}"
 29println "- MAX_RETRY: ${C_GREEN}${MAX_RETRY}${C_RESET}"
 30println "- VERBOSE: ${C_GREEN}${VERBOSE}${C_RESET}"
 31
 32FABRIC_CFG_PATH=$PWD/../config/
 33
 34if [ -z "$CC_NAME" ] || [ "$CC_NAME" = "NA" ]; then
 35  fatalln "No chaincode name was provided."
 36elif [ -z "$CC_SRC_PATH" ] || [ "$CC_SRC_PATH" = "NA" ]; then
 37  fatalln "No chaincode path was provided."
 38elif [ -z "$CC_SRC_LANGUAGE" ] || [ "$CC_SRC_LANGUAGE" = "NA" ]; then
 39  fatalln "No chaincode language was provided."
 40elif [ ! -d "$CC_SRC_PATH" ]; then
 41  fatalln "Path to chaincode does not exist."
 42fi
 43
 44CC_SRC_LANGUAGE=$(echo "$CC_SRC_LANGUAGE" | tr [:upper:] [:lower:])
 45
 46if [ "$CC_SRC_LANGUAGE" = "go" ]; then
 47  CC_RUNTIME_LANGUAGE=golang
 48  infoln "Vendoring Go dependencies at $CC_SRC_PATH"
 49  pushd $CC_SRC_PATH
 50  GO111MODULE=on go mod vendor
 51  popd
 52  successln "Finished vendoring Go dependencies"
 53elif [ "$CC_SRC_LANGUAGE" = "java" ]; then
 54  CC_RUNTIME_LANGUAGE=java
 55  infoln "Compiling Java code..."
 56  pushd $CC_SRC_PATH
 57  ./gradlew installDist
 58  popd
 59  successln "Finished compiling Java code"
 60  CC_SRC_PATH=$CC_SRC_PATH/build/install/$CC_NAME
 61elif [ "$CC_SRC_LANGUAGE" = "javascript" ]; then
 62  CC_RUNTIME_LANGUAGE=node
 63elif [ "$CC_SRC_LANGUAGE" = "typescript" ]; then
 64  CC_RUNTIME_LANGUAGE=node
 65  infoln "Compiling TypeScript code into JavaScript..."
 66  pushd $CC_SRC_PATH
 67  npm install
 68  npm run build
 69  popd
 70  successln "Finished compiling TypeScript code into JavaScript"
 71else
 72  fatalln "The chaincode language ${CC_SRC_LANGUAGE} is not supported by this script."
 73fi
 74
 75INIT_REQUIRED="--init-required"
 76if [ "$CC_INIT_FCN" = "NA" ]; then
 77  INIT_REQUIRED=""
 78fi
 79
 80if [ "$CC_END_POLICY" = "NA" ]; then
 81  CC_END_POLICY=""
 82else
 83  CC_END_POLICY="--signature-policy $CC_END_POLICY"
 84fi
 85
 86if [ "$CC_COLL_CONFIG" = "NA" ]; then
 87  CC_COLL_CONFIG=""
 88else
 89  CC_COLL_CONFIG="--collections-config $CC_COLL_CONFIG"
 90fi
 91
 92. scripts/envVar.sh
 93
 94packageChaincode() {
 95  set -x
 96  peer lifecycle chaincode package ${CC_NAME}.tar.gz --path ${CC_SRC_PATH} --lang ${CC_RUNTIME_LANGUAGE} --label ${CC_NAME}_${CC_VERSION} >&log.txt
 97  res=$?
 98  { set +x; } 2>/dev/null
 99  cat log.txt
100  verifyResult $res "Chaincode packaging has failed"
101  successln "Chaincode is packaged"
102}
103
104installChaincode() {
105  ORG=$1
106  setGlobals $ORG
107  set -x
108  peer lifecycle chaincode install ${CC_NAME}.tar.gz >&log.txt
109  res=$?
110  { set +x; } 2>/dev/null
111  cat log.txt
112  verifyResult $res "Chaincode installation on peer0.org${ORG} has failed"
113  successln "Chaincode is installed on peer0.org${ORG}"
114}
115
116queryInstalled() {
117  ORG=$1
118  setGlobals $ORG
119  set -x
120  peer lifecycle chaincode queryinstalled >&log.txt
121  res=$?
122  { set +x; } 2>/dev/null
123  cat log.txt
124  PACKAGE_ID=$(sed -n "/${CC_NAME}_${CC_VERSION}/{s/^Package ID: //; s/, Label:.*$//; p;}" log.txt)
125  verifyResult $res "Query installed on peer0.org${ORG} has failed"
126  successln "Query installed successful on peer0.org${ORG} on channel"
127}
128
129approveForMyOrg() {
130  ORG=$1
131  setGlobals $ORG
132  set -x
133  peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$ORDERER_CA" --channelID $CHANNEL_NAME --name ${CC_NAME} --version ${CC_VERSION} --package-id ${PACKAGE_ID} --sequence ${CC_SEQUENCE} ${INIT_REQUIRED} ${CC_END_POLICY} ${CC_COLL_CONFIG} >&log.txt
134  res=$?
135  { set +x; } 2>/dev/null
136  cat log.txt
137  verifyResult $res "Chaincode definition approved on peer0.org${ORG} on channel '$CHANNEL_NAME' failed"
138  successln "Chaincode definition approved on peer0.org${ORG} on channel '$CHANNEL_NAME'"
139}
140
141checkCommitReadiness() {
142  ORG=$1
143  shift 1
144  setGlobals $ORG
145  infoln "Checking the commit readiness of the chaincode definition on peer0.org${ORG} on channel '$CHANNEL_NAME'..."
146  local rc=1
147  local COUNTER=1
148  while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do
149    sleep $DELAY
150    infoln "Attempting to check the commit readiness of the chaincode definition on peer0.org${ORG}, Retry after $DELAY seconds."
151    set -x
152    peer lifecycle chaincode checkcommitreadiness --channelID $CHANNEL_NAME --name ${CC_NAME} --version ${CC_VERSION} --sequence ${CC_SEQUENCE} ${INIT_REQUIRED} ${CC_END_POLICY} ${CC_COLL_CONFIG} --output json >&log.txt
153    res=$?
154    { set +x; } 2>/dev/null
155    let rc=0
156    for var in "$@"; do
157      grep "$var" log.txt &>/dev/null || let rc=1
158    done
159    COUNTER=$(expr $COUNTER + 1)
160  done
161  cat log.txt
162  if test $rc -eq 0; then
163    infoln "Checking the commit readiness of the chaincode definition successful on peer0.org${ORG} on channel '$CHANNEL_NAME'"
164  else
165    fatalln "After $MAX_RETRY attempts, Check commit readiness result on peer0.org${ORG} is INVALID!"
166  fi
167}
168
169commitChaincodeDefinition() {
170  parsePeerConnectionParameters $@
171  res=$?
172  verifyResult $res "Invoke transaction failed on channel '$CHANNEL_NAME' due to uneven number of peer and org parameters "
173  set -x
174  peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$ORDERER_CA" --channelID $CHANNEL_NAME --name ${CC_NAME} "${PEER_CONN_PARMS[@]}" --version ${CC_VERSION} --sequence ${CC_SEQUENCE} ${INIT_REQUIRED} ${CC_END_POLICY} ${CC_COLL_CONFIG} >&log.txt
175  res=$?
176  { set +x; } 2>/dev/null
177  cat log.txt
178  verifyResult $res "Chaincode definition commit failed on peer0.org${ORG} on channel '$CHANNEL_NAME' failed"
179  successln "Chaincode definition committed on channel '$CHANNEL_NAME'"
180}
181
182queryCommitted() {
183  ORG=$1
184  setGlobals $ORG
185  EXPECTED_RESULT="Version: ${CC_VERSION}, Sequence: ${CC_SEQUENCE}, Endorsement Plugin: escc, Validation Plugin: vscc"
186  infoln "Querying chaincode definition on peer0.org${ORG} on channel '$CHANNEL_NAME'..."
187  local rc=1
188  local COUNTER=1
189  while [ $rc -ne 0 -a $COUNTER -lt $MAX_RETRY ]; do
190    sleep $DELAY
191    infoln "Attempting to Query committed status on peer0.org${ORG}, Retry after $DELAY seconds."
192    set -x
193    peer lifecycle chaincode querycommitted --channelID $CHANNEL_NAME --name ${CC_NAME} >&log.txt
194    res=$?
195    { set +x; } 2>/dev/null
196    test $res -eq 0 && VALUE=$(cat log.txt | grep -o '^Version: '$CC_VERSION', Sequence: [0-9]*, Endorsement Plugin: escc, Validation Plugin: vscc')
197    test "$VALUE" = "$EXPECTED_RESULT" && let rc=0
198    COUNTER=$(expr $COUNTER + 1)
199  done
200  cat log.txt
201  if test $rc -eq 0; then
202    successln "Query chaincode definition successful on peer0.org${ORG} on channel '$CHANNEL_NAME'"
203  else
204    fatalln "After $MAX_RETRY attempts, Query chaincode definition result on peer0.org${ORG} is INVALID!"
205  fi
206}
207
208chaincodeInvokeInit() {
209  parsePeerConnectionParameters $@
210  res=$?
211  verifyResult $res "Invoke transaction failed on channel '$CHANNEL_NAME' due to uneven number of peer and org parameters "
212  set -x
213  fcn_call='{"function":"'${CC_INIT_FCN}'","Args":[]}'
214  infoln "invoke fcn call:${fcn_call}"
215  peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$ORDERER_CA" -C $CHANNEL_NAME -n ${CC_NAME} "${PEER_CONN_PARMS[@]}" --isInit -c ${fcn_call} >&log.txt
216  res=$?
217  { set +x; } 2>/dev/null
218  cat log.txt
219  verifyResult $res "Invoke execution on $PEERS failed "
220  successln "Invoke transaction successful on $PEERS on channel '$CHANNEL_NAME'"
221}
222
223packageChaincode
224
225infoln "Installing chaincode on peer0.org1..."
226installChaincode 1
227infoln "Installing chaincode on peer0.org2..."
228installChaincode 2
229infoln "Installing chaincode on peer0.org3..."
230installChaincode 3
231infoln "Installing chaincode on peer0.org4..."
232installChaincode 4
233
234queryInstalled 1
235approveForMyOrg 1
236checkCommitReadiness 1 "\"Org1MSP\": true" "\"Org2MSP\": false" "\"Org3MSP\": false" "\"Org4MSP\": false"
237
238approveForMyOrg 2
239checkCommitReadiness 1 "\"Org1MSP\": true" "\"Org2MSP\": true" "\"Org3MSP\": false" "\"Org4MSP\": false"
240
241approveForMyOrg 3
242checkCommitReadiness 1 "\"Org1MSP\": true" "\"Org2MSP\": true" "\"Org3MSP\": true" "\"Org4MSP\": false"
243
244approveForMyOrg 4
245checkCommitReadiness 1 "\"Org1MSP\": true" "\"Org2MSP\": true" "\"Org3MSP\": true" "\"Org4MSP\": true"
246
247commitChaincodeDefinition 1 2 3 4
248queryCommitted 1
249queryCommitted 2
250queryCommitted 3
251queryCommitted 4
252
253if [ "$CC_INIT_FCN" = "NA" ]; then
254  infoln "Chaincode initialization is not required"
255else
256  chaincodeInvokeInit 1 2 3 4
257fi
258
259exit 0