123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- #!/bin/bash
- # Some useful functions for cc testing
- # Detecting whether can import the header file to render colorful cli output
- if [ -f ./header.sh ]; then
- source ./header.sh
- elif [ -f scripts/header.sh ]; then
- source scripts/header.sh
- else
- echo_r() {
- echo "$@"
- }
- echo_g() {
- echo "$@"
- }
- echo_b() {
- echo "$@"
- }
- fi
- # Define those global variables
- if [ -f ./variables.sh ]; then
- source ./variables.sh
- elif [ -f scripts/variables.sh ]; then
- source scripts/variables.sh
- else
- echo "Cannot find the variables.sh files, pls check"
- exit 1
- fi
- # Verify $1 is not 0, then output error msg $2 and exit
- verifyResult () {
- if [ $1 -ne 0 ] ; then
- echo_b "$2"
- echo_r "=== ERROR !!! FAILED to execute End-2-End Scenario ==="
- exit 1
- fi
- }
- setOrdererEnvs () {
- CORE_PEER_LOCALMSPID="OrdererMSP"
- CORE_PEER_MSPCONFIGPATH=${ORDERER_ADMIN_MSP}
- CORE_PEER_TLS_ROOTCERT_FILE=${ORDERER_TLS_ROOTCERT}
- #t="\${ORG${org}_PEER${peer}_URL}" && CORE_PEER_ADDRESS=`eval echo $t`
- }
- # Set global env variables for fabric usage
- # Usage: setEnvs org peer
- setEnvs () {
- local org=$1 # 1 or 2
- local peer=$2 # 0 or 1
- local t=""
- CORE_PEER_LOCALMSPID="Org${org}MSP"
- #CORE_PEER_MSPCONFIGPATH=\$${ORG${org}_ADMIN_MSP}
- t="\${ORG${org}_ADMIN_MSP}" && CORE_PEER_MSPCONFIGPATH=`eval echo $t`
- t="\${ORG${org}_PEER${peer}_TLS_ROOTCERT}" && CORE_PEER_TLS_ROOTCERT_FILE=`eval echo $t`
- t="\${ORG${org}_PEER${peer}_URL}" && CORE_PEER_ADDRESS=`eval echo $t`
- # env |grep CORE
- }
- checkOSNAvailability() {
- #Use orderer's MSP for fetching system channel config block
- CORE_PEER_LOCALMSPID="OrdererMSP"
- CORE_PEER_TLS_ROOTCERT_FILE=${ORDERER_TLS_CA}
- CORE_PEER_MSPCONFIGPATH=${ORDERER_MSP}
- local rc=1
- local starttime=$(date +%s)
- # continue to poll
- # we either get a successful response, or reach TIMEOUT
- while test "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0
- do
- sleep 3
- echo "Attempting to fetch system channel ...$(($(date +%s)-starttime)) secs"
- if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
- peer channel fetch 0 -o ${ORDERER_URL} -c "testchainid" >&log.txt
- else
- peer channel fetch 0 -o ${ORDERER_URL} -c "testchainid" --tls $CORE_PEER_TLS_ENABLED --cafile ${ORDERER_TLS_CA} >&log.txt
- fi
- test $? -eq 0 && VALUE=$(cat log.txt | awk '/Received block/ {print $NF}')
- test "$VALUE" = "0" && let rc=0
- done
- cat log.txt
- verifyResult $rc "Ordering Service is not available, Please try again ..."
- echo "=== Ordering Service is up and running === "
- echo
- }
- # Internal func called by channelCreate
- channelCreateAction(){
- local channel=$1
- local channel_tx=$2
- if [ -z "$CORE_PEER_TLS_ENABLED" ] || [ "$CORE_PEER_TLS_ENABLED" = "false" ]; then
- peer channel create \
- -o ${ORDERER_URL} \
- -c ${channel} \
- -f ${CHANNEL_ARTIFACTS}/${channel_tx} \
- --timeout $TIMEOUT \
- >&log.txt
- else
- peer channel create \
- -o ${ORDERER_URL} \
- -c ${channel} \
- -f ${CHANNEL_ARTIFACTS}/${channel_tx} \
- --timeout $TIMEOUT \
- --tls $CORE_PEER_TLS_ENABLED \
- --cafile ${ORDERER_TLS_CA} \
- >&log.txt
- fi
- return $?
- }
- # Use peer0/org1 to create a channel
- # channelCreate APP_CHANNEL APP_CHANNEL.tx org peer
- channelCreate() {
- local channel=$1
- local channel_tx=$2
- local org=$3
- local peer=$4
- echo_b "=== Create Channel ${channel} by org $org peer $peer === "
- local counter=0
- setEnvs $org $peer
- channelCreateAction "${channel}" "${channel_tx}"
- local res=$?
- while [ ${counter} -lt ${MAX_RETRY} -a ${res} -ne 0 ]; do
- echo_b "Failed to create channel $channel, retry after 3s"
- sleep 3
- channelCreateAction "${channel}" "${channel_tx}"
- res=$?
- let counter=${counter}+1
- #COUNTER=` expr $COUNTER + 1`
- done
- cat log.txt
- verifyResult ${res} "Channel ${channel} creation failed"
- echo_g "=== Channel ${channel} is created. === "
- }
- # called by channelJoinWithRetry
- channelJoinAction () {
- local channel=$1
- peer channel join \
- -b ${channel}.block \
- >&log.txt
- }
- ## Sometimes Join takes time hence RETRY atleast for 5 times
- channelJoinWithRetry () {
- local channel=$1
- local peer=$2
- local counter=0
- channelJoinAction ${channel}
- local res=$?
- while [ ${counter} -lt ${MAX_RETRY} -a ${res} -ne 0 ]; do
- echo_b "peer${peer} failed to join channel ${channel}, retry after 2s"
- sleep 2
- channelJoinAction ${channel}
- res=$?
- let counter=${counter}+1
- done
- cat log.txt
- verifyResult ${res} "After $MAX_RETRY attempts, peer${peer} failed to Join the Channel"
- }
- # Join given (by default all) peers into the channel
- # channelJoin channel org peer
- channelJoin () {
- local channel=$1
- local org=$2
- local peer=$3
- echo_b "=== Join org$org/peer$peer into channel ${channel} === "
- setEnvs $org $peer
- channelJoinWithRetry ${channel} $peer
- echo_g "=== org$org/peer$peer joined into channel ${channel} === "
- }
- # Update the anchor peer at given channel
- # updateAnchorPeers channel peer
- updateAnchorPeers() {
- local channel=$1
- local org=$2
- local peer=$3
- setEnvs $org $peer
- echo_b "=== Update Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on ${channel} === "
- if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
- peer channel update \
- -o ${ORDERER_URL} \
- -c ${channel} \
- -f ${CHANNEL_ARTIFACTS}/${CORE_PEER_LOCALMSPID}anchors.tx \
- >&log.txt
- else
- peer channel update \
- -o ${ORDERER_URL} \
- -c ${channel} \
- -f ${CHANNEL_ARTIFACTS}/${CORE_PEER_LOCALMSPID}anchors.tx \
- --tls $CORE_PEER_TLS_ENABLED \
- --cafile ${ORDERER_TLS_CA} \
- >&log.txt
- fi
- res=$?
- cat log.txt
- verifyResult $res "Anchor peer update failed"
- echo_g "=== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on ${channel} is updated. === "
- sleep 2
- }
- # Install chaincode on specified peer node
- # chaincodeInstall peer cc_name version path
- chaincodeInstall () {
- local org=$1
- local peer=$2
- local name=$3
- local version=$4
- local path=$5
- echo_b "=== Install Chaincode $name:$version ($path) on org${org} peer$peer === "
- setEnvs $org $peer
- peer chaincode install \
- -n ${name} \
- -v $version \
- -p ${path} \
- >&log.txt
- res=$?
- cat log.txt
- verifyResult $res "Chaincode installation on remote peer$peer has Failed"
- echo_g "=== Chaincode is installed on remote peer$peer === "
- }
- # Instantiate chaincode on specifized peer node
- # chaincodeInstantiate channel org peer name version args
- chaincodeInstantiate () {
- local channel=$1
- local org=$2
- local peer=$3
- local name=$4
- local version=$5
- local args=$6
- setEnvs $org $peer
- echo_b "=== chaincodeInstantiate for channel ${channel} on org $org peer $peer ===="
- # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
- # lets supply it directly as we know it using the "-o" option
- if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
- peer chaincode instantiate \
- -o ${ORDERER_URL} \
- -C ${channel} \
- -n ${name} \
- -v ${version} \
- -c ${args} \
- -P "OR ('Org1MSP.member','Org2MSP.member')" \
- >&log.txt
- else
- peer chaincode instantiate \
- -o ${ORDERER_URL} \
- -C ${channel} \
- -n ${name} \
- -v ${version} \
- -c ${args} \
- -P "OR ('Org1MSP.member','Org2MSP.member')" \
- --tls $CORE_PEER_TLS_ENABLED \
- --cafile ${ORDERER_TLS_CA} \
- >&log.txt
- fi
- res=$?
- cat log.txt
- verifyResult $res "ChaincodeInstantiation on peer$peer in channel ${channel} failed"
- echo_g "=== Chaincode Instantiated in channel ${channel} by peer$peer ==="
- }
- # Usage: chaincodeInvoke channel org peer name args
- chaincodeInvoke () {
- local channel=$1
- local org=$2
- local peer=$3
- local name=$4
- local args=$5
- echo_g "=== Invoke transaction on peer$peer in channel ${channel} === "
- setEnvs $org $peer
- # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
- # lets supply it directly as we know it using the "-o" option
- if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
- peer chaincode invoke \
- -o ${ORDERER_URL} \
- -C ${channel} \
- -n ${name} \
- -c ${args} \
- >&log.txt
- else
- peer chaincode invoke \
- -o ${ORDERER_URL} \
- -C ${channel} \
- -n ${name} \
- -c ${args} \
- --tls $CORE_PEER_TLS_ENABLED \
- --cafile ${ORDERER_TLS_CA} \
- >&log.txt
- fi
- res=$?
- cat log.txt
- verifyResult $res "Invoke execution on peer$peer failed "
- echo_g "=== Invoke transaction on peer$peer in channel ${channel} is successful === "
- }
- # query channel peer name args expected_result
- chaincodeQuery () {
- local channel=$1
- local org=$2
- local peer=$3
- local name=$4
- local args=$5
- [ $# -gt 5 ] && local expected_result=$6
- echo_b "=== Querying on org$org peer$peer in channel ${channel}... === "
- local rc=1
- local starttime=$(date +%s)
- setEnvs $org $peer
- # we either get a successful response, or reach TIMEOUT
- while [ "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0 ]; do
- echo_b "Attempting to Query peer$peer ...$(($(date +%s)-starttime)) secs"
- peer chaincode query \
- -C "${channel}" \
- -n "${name}" \
- -c "${args}" \
- >&log.txt
- rc=$?
- if [ $# -gt 5 ]; then # need to check the result
- test $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')
- test "$VALUE" = "${expected_result}" && let rc=0
- fi
- cat log.txt
- if [ $rc -ne 0 ]; then
- sleep 2
- fi
- done
- if [ $rc -eq 0 ]; then
- echo_g "=== Query on peer$peer in channel ${channel} is successful === "
- else
- echo_r "=== Query result on peer$peer is INVALID, run `make stop clean` to clean ==="
- exit 1
- fi
- }
- # Start chaincode with dev mode
- chaincodeStartDev () {
- local peer=$1
- local version=$2
- #setEnvs $peer
- #setEnvs 1 0
- setEnvs 1 0
- CORE_CHAINCODE_LOGLEVEL=debug \
- CORE_PEER_ADDRESS=peer${peer}.org1.example.com:7052 \
- CORE_CHAINCODE_ID_NAME=mycc:${version} \
- nohup ./scripts/chaincode_example02 > chaincode_dev.log &
- res=$?
- cat log.txt
- verifyResult $res "Chaincode start in dev mode has Failed"
- echo_g "=== Chaincode started in dev mode === "
- echo
- }
- # chaincodeUpgrade channel peer name version args
- chaincodeUpgrade () {
- local channel=$1
- local org=$2
- local peer=$3
- local name=$4
- local version=$5
- local args=$6
- echo_b "=== Upgrade chaincode to version $version on peer$peer in channel ${channel} === "
- setEnvs $org $peer
- # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
- # lets supply it directly as we know it using the "-o" option
- if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
- peer chaincode upgrade \
- -o ${ORDERER_URL} \
- -C ${channel} \
- -n ${name} \
- -v ${version} \
- -c ${args} \
- >&log.txt
- else
- peer chaincode upgrade \
- -o ${ORDERER_URL} \
- -C ${channel} \
- -n ${name} \
- -v ${version} \
- -c ${args} \
- --tls $CORE_PEER_TLS_ENABLED \
- --cafile ${ORDERER_TLS_CA} \
- >&log.txt
- fi
- res=$?
- cat log.txt
- verifyResult $res "Upgrade execution on peer$peer failed "
- echo_g "=== Upgrade transaction on peer$peer in channel ${channel} is successful === "
- echo
- }
- # Fetch some block from a given channel: channel, peer, blockNum
- channelFetch () {
- local channel=$1
- local org=$2
- local peer=$3
- local num=$4
- echo_b "=== Fetch block $num of channel $channel === "
- #setEnvs $org $peer
- setOrdererEnvs
- # while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
- # lets supply it directly as we know it using the "-o" option
- if [ -z "${CORE_PEER_TLS_ENABLED}" ] || [ "${CORE_PEER_TLS_ENABLED}" = "false" ]; then
- peer channel fetch $num ${CHANNEL_ARTIFACTS}/${channel}_${num}.block \
- -o ${ORDERER_URL} \
- -c ${channel} \
- >&log.txt
- else
- peer channel fetch $num ${CHANNEL_ARTIFACTS}/${channel}_${num}.block \
- -o ${ORDERER_URL} \
- -c ${channel} \
- --tls \
- --cafile ${ORDERER_TLS_CA} \
- >&log.txt
- fi
- res=$?
- cat log.txt
- if [ $res -ne 0 ]; then
- echo_r "Fetch block $num of channel $channel failed"
- else
- echo_g "=== Fetch block $num of channel $channel is successful === "
- fi
- }
- # configtxlator encode json to pb
- # Usage: configtxlatorEncode msgType input output
- configtxlatorEncode() {
- local msgType=$1
- local input=$2
- local output=$3
- echo_b "Encode $input --> $output using type $msgType"
- curl -sX POST \
- --data-binary @${input} \
- ${CTL_ENCODE_URL}/${msgType} \
- >${output}
- }
- # configtxlator decode pb to json
- # Usage: configtxlatorEncode msgType input output
- configtxlatorDecode() {
- local msgType=$1
- local input=$2
- local output=$3
- echo_b "Config Decode $input --> $output using type $msgType"
- curl -sX POST \
- --data-binary @"${input}" \
- "${CTL_DECODE_URL}/${msgType}" \
- > "${output}"
- }
- # compute diff between two pb
- # Usage: configtxlatorCompare channel origin updated output
- configtxlatorCompare() {
- local channel=$1
- local origin=$2
- local updated=$3
- local output=$3
- echo_b "Config Compare $origin vs $updated > ${output} in channel $channel"
- curl -sX POST \
- -F channel="${channel}" \
- -F "original=@${origin}" \
- -F "updated=@${updated}" \
- "${CTL_COMPARE_URL}" \
- > "${output}"
- [ $? -eq 0 ] || echo_r "Failed to compute config update"
- }
- # Run cmd inside the config generator container
- gen_con_exec() {
- docker exec -it $GEN_CONTAINER "$@"
- }
|