docker-compose-files/hyperledger_fabric/v1.0.4/scripts/func.sh

468 lines
13 KiB
Bash
Raw Normal View History

2017-11-02 10:19:05 +08:00
#!/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
alias echo_r="echo"
alias echo_g="echo"
alias echo_b="echo"
fi
2017-11-14 15:29:27 +08:00
# Define those global variables
if [ -f ./variables.sh ]; then
source ./variables.sh
elif [ -f scripts/variables.sh ]; then
source scripts/variables.sh
fi
2017-11-02 10:19:05 +08:00
2017-11-23 17:17:29 +08:00
# Verify $1 is not 0, then output error msg $2 and exit
2017-11-02 10:19:05 +08:00
verifyResult () {
if [ $1 -ne 0 ] ; then
2017-11-14 15:29:27 +08:00
echo_b "$2"
echo_r "=== ERROR !!! FAILED to execute End-2-End Scenario ==="
exit 1
2017-11-02 10:19:05 +08:00
fi
}
2017-12-02 09:36:01 +08:00
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`
}
2017-11-14 15:29:27 +08:00
# Set global env variables for fabric usage
2017-11-24 13:43:57 +08:00
# Usage: setEnvs org peer
2017-11-23 22:22:20 +08:00
setEnvs () {
2017-11-24 13:43:57 +08:00
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`
2017-11-02 10:19:05 +08:00
2017-11-24 14:45:16 +08:00
# env |grep CORE
2017-11-02 10:19:05 +08:00
}
checkOSNAvailability() {
#Use orderer's MSP for fetching system channel config block
CORE_PEER_LOCALMSPID="OrdererMSP"
2017-11-14 15:29:27 +08:00
CORE_PEER_TLS_ROOTCERT_FILE=${ORDERER_TLS_CA}
CORE_PEER_MSPCONFIGPATH=${ORDERER_MSP}
2017-11-02 10:19:05 +08:00
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
2017-11-23 22:22:20 +08:00
echo "Attempting to fetch system channel ...$(($(date +%s)-starttime)) secs"
2017-11-02 10:19:05 +08:00
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
2017-11-14 15:29:27 +08:00
peer channel fetch 0 -o ${ORDERER_URL} -c "testchainid" >&log.txt
2017-11-02 10:19:05 +08:00
else
2017-11-14 15:29:27 +08:00
peer channel fetch 0 -o ${ORDERER_URL} -c "testchainid" --tls $CORE_PEER_TLS_ENABLED --cafile ${ORDERER_TLS_CA} >&log.txt
2017-11-02 10:19:05 +08:00
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 ..."
2017-11-06 12:18:37 +08:00
echo "=== Ordering Service is up and running === "
2017-11-02 10:19:05 +08:00
echo
}
2017-11-14 15:29:27 +08:00
# Internal func called by channelCreate
channelCreateAction(){
local channel=$1
2017-11-02 10:19:05 +08:00
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer channel create \
2017-11-14 15:29:27 +08:00
-o ${ORDERER_URL} \
-c ${channel} \
2017-12-01 23:11:37 +08:00
-f ${CHANNEL_ARTIFACTS}/channel.tx \
2017-11-02 10:19:05 +08:00
--timeout $TIMEOUT \
>&log.txt
else
peer channel create \
2017-11-14 15:29:27 +08:00
-o ${ORDERER_URL} \
-c ${channel} \
2017-12-01 23:11:37 +08:00
-f ${CHANNEL_ARTIFACTS}/channel.tx \
2017-11-02 10:19:05 +08:00
--timeout $TIMEOUT \
2017-11-14 15:29:27 +08:00
--tls $CORE_PEER_TLS_ENABLED \
--cafile ${ORDERER_TLS_CA} \
2017-11-02 10:19:05 +08:00
>&log.txt
fi
2017-11-14 15:29:27 +08:00
return $?
2017-11-02 10:19:05 +08:00
}
2017-11-14 15:29:27 +08:00
# Use peer0/org1 to create a channel
2017-12-06 16:24:33 +08:00
# channelCreate APP_CHANNEL org peer
2017-11-14 15:29:27 +08:00
channelCreate() {
local channel=$1
2017-11-24 14:45:16 +08:00
local org=$2
local peer=$3
echo_b "=== Create Channel ${channel} by org $org peer $peer === "
2017-11-14 15:29:27 +08:00
local counter=0
2017-11-24 14:45:16 +08:00
setEnvs $org $peer
2017-11-23 17:17:29 +08:00
channelCreateAction ${channel}
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}
res=$?
2017-11-14 15:29:27 +08:00
let counter=${counter}+1
#COUNTER=` expr $COUNTER + 1`
done
2017-11-02 10:19:05 +08:00
cat log.txt
2017-11-23 17:17:29 +08:00
verifyResult ${res} "Channel ${channel} creation failed"
echo_g "=== Channel ${channel} is created. === "
2017-11-02 10:19:05 +08:00
}
2017-11-14 15:29:27 +08:00
# called by channelJoinWithRetry
channelJoinAction () {
local channel=$1
peer channel join \
-b ${channel}.block \
>&log.txt
}
2017-12-01 17:34:21 +08:00
2017-11-02 10:19:05 +08:00
## Sometimes Join takes time hence RETRY atleast for 5 times
2017-11-06 12:18:37 +08:00
channelJoinWithRetry () {
2017-11-14 15:29:27 +08:00
local channel=$1
local peer=$2
local counter=0
2017-11-23 17:17:29 +08:00
channelJoinAction ${channel}
local res=$?
2017-11-14 15:29:27 +08:00
while [ ${counter} -lt ${MAX_RETRY} -a ${res} -ne 0 ]; do
2017-11-23 17:17:29 +08:00
echo_b "peer${peer} failed to join channel ${channel}, retry after 2s"
2017-11-02 10:19:05 +08:00
sleep 2
2017-11-23 17:17:29 +08:00
channelJoinAction ${channel}
res=$?
let counter=${counter}+1
2017-11-14 15:29:27 +08:00
done
cat log.txt
2017-11-23 17:17:29 +08:00
verifyResult ${res} "After $MAX_RETRY attempts, peer${peer} failed to Join the Channel"
2017-11-02 10:19:05 +08:00
}
# Join given (by default all) peers into the channel
2017-11-24 14:45:16 +08:00
# channelJoin channel org peer
2017-11-02 10:19:05 +08:00
channelJoin () {
2017-11-14 15:29:27 +08:00
local channel=$1
2017-11-24 14:45:16 +08:00
local org=$2
local peer=$3
2017-12-01 17:34:21 +08:00
echo_b "=== Join org$org/peer$peer into channel ${channel} === "
2017-11-24 14:45:16 +08:00
setEnvs $org $peer
channelJoinWithRetry ${channel} $peer
2017-12-01 17:34:21 +08:00
echo_g "=== org$org/peer$peer joined into channel ${channel} === "
2017-11-02 10:19:05 +08:00
}
2017-11-14 15:29:27 +08:00
# Update the anchor peer at given channel
# updateAnchorPeers channel peer
updateAnchorPeers() {
local channel=$1
2017-11-24 14:45:16 +08:00
local org=$2
local peer=$3
setEnvs $org $peer
2017-11-14 15:29:27 +08:00
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} \
2017-12-01 23:11:37 +08:00
-f ${CHANNEL_ARTIFACTS}/${CORE_PEER_LOCALMSPID}anchors.tx \
2017-11-14 15:29:27 +08:00
>&log.txt
else
peer channel update \
-o ${ORDERER_URL} \
-c ${channel} \
2017-12-01 23:11:37 +08:00
-f ${CHANNEL_ARTIFACTS}/${CORE_PEER_LOCALMSPID}anchors.tx \
2017-11-14 15:29:27 +08:00
--tls $CORE_PEER_TLS_ENABLED \
--cafile ${ORDERER_TLS_CA} \
>&log.txt
fi
res=$?
cat log.txt
verifyResult $res "Anchor peer update failed"
2017-11-23 17:17:29 +08:00
echo_g "=== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on ${channel} is updated. === "
2017-11-14 15:29:27 +08:00
sleep 2
}
# Install chaincode on specified peer node
# chaincodeInstall peer cc_name version path
chaincodeInstall () {
2017-11-24 14:45:16 +08:00
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
2017-11-14 15:29:27 +08:00
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 === "
}
2017-11-02 10:19:05 +08:00
# Instantiate chaincode on specifized peer node
2017-11-24 14:45:16 +08:00
# chaincodeInstantiate channel org peer name version args
2017-11-02 10:19:05 +08:00
chaincodeInstantiate () {
2017-11-14 15:29:27 +08:00
local channel=$1
2017-11-24 14:45:16 +08:00
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 ===="
2017-11-02 10:19:05 +08:00
# 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 \
2017-11-14 15:29:27 +08:00
-o ${ORDERER_URL} \
-C ${channel} \
-n ${name} \
-v ${version} \
-c ${args} \
2017-11-02 10:19:05 +08:00
-P "OR ('Org1MSP.member','Org2MSP.member')" \
>&log.txt
else
peer chaincode instantiate \
2017-11-14 15:29:27 +08:00
-o ${ORDERER_URL} \
-C ${channel} \
-n ${name} \
-v ${version} \
-c ${args} \
2017-11-02 10:19:05 +08:00
-P "OR ('Org1MSP.member','Org2MSP.member')" \
--tls $CORE_PEER_TLS_ENABLED \
2017-11-14 15:29:27 +08:00
--cafile ${ORDERER_TLS_CA} \
>&log.txt
fi
res=$?
cat log.txt
2017-11-23 22:22:20 +08:00
verifyResult $res "ChaincodeInstantiation on peer$peer in channel ${channel} failed"
echo_g "=== ChaincodeInstantiation on peer$peer in channel ${channel} is successful ==="
2017-11-14 15:29:27 +08:00
}
2017-11-24 14:45:16 +08:00
# Usage: chaincodeInvoke channel org peer name args
2017-11-14 15:29:27 +08:00
chaincodeInvoke () {
local channel=$1
2017-11-24 14:45:16 +08:00
local org=$2
local peer=$3
local name=$4
local args=$5
2017-11-23 22:22:20 +08:00
echo_g "=== Invoke transaction on peer$peer in channel ${channel} === "
2017-11-24 14:45:16 +08:00
setEnvs $org $peer
2017-11-14 15:29:27 +08:00
# 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} \
2017-11-02 10:19:05 +08:00
>&log.txt
fi
res=$?
cat log.txt
2017-11-14 15:29:27 +08:00
verifyResult $res "Invoke execution on peer$peer failed "
2017-11-23 22:22:20 +08:00
echo_g "=== Invoke transaction on peer$peer in channel ${channel} is successful === "
2017-11-02 10:19:05 +08:00
}
2017-11-14 15:29:27 +08:00
# query channel peer name args expected_result
2017-11-02 10:19:05 +08:00
chaincodeQuery () {
2017-11-14 15:29:27 +08:00
local channel=$1
2017-11-24 14:45:16 +08:00
local org=$2
local peer=$3
local name=$4
local args=$5
[ $# -gt 5 ] && local expected_result=$6
2017-12-06 16:50:55 +08:00
echo_b "=== Querying on org$org peer$peer in channel ${channel}... === "
2017-11-02 10:19:05 +08:00
local rc=1
local starttime=$(date +%s)
2017-11-24 14:45:16 +08:00
setEnvs $org $peer
2017-11-02 10:19:05 +08:00
# we either get a successful response, or reach TIMEOUT
2017-12-06 16:50:55 +08:00
while [ "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0 ]; do
2017-11-14 15:29:27 +08:00
echo_b "Attempting to Query peer$peer ...$(($(date +%s)-starttime)) secs"
peer chaincode query \
2017-11-23 22:22:20 +08:00
-C "${channel}" \
-n "${name}" \
-c "${args}" \
2017-11-14 15:29:27 +08:00
>&log.txt
2017-12-06 16:50:55 +08:00
rc=$?
2017-11-24 14:45:16 +08:00
if [ $# -gt 5 ]; then # need to check the result
2017-11-23 22:22:20 +08:00
test $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')
test "$VALUE" = "${expected_result}" && let rc=0
2017-12-06 16:50:55 +08:00
fi
cat log.txt
if [ $rc -ne 0 ]; then
sleep 2
2017-11-23 22:22:20 +08:00
fi
2017-11-02 10:19:05 +08:00
done
2017-12-06 16:50:55 +08:00
if [ $rc -eq 0 ]; then
echo_g "=== Query on peer$peer in channel ${channel} is successful === "
2017-11-02 10:19:05 +08:00
else
2017-12-06 16:50:55 +08:00
echo_r "=== Query result on peer$peer is INVALID, run `make stop clean` to clean ==="
exit 1
2017-11-02 10:19:05 +08:00
fi
}
# Start chaincode with dev mode
chaincodeStartDev () {
2017-11-14 15:29:27 +08:00
local peer=$1
local version=$2
2017-11-23 22:22:20 +08:00
#setEnvs $peer
2017-11-24 13:43:57 +08:00
#setEnvs 1 0
setEnvs 1 0
2017-11-02 10:19:05 +08:00
CORE_CHAINCODE_LOGLEVEL=debug \
2017-11-14 15:29:27 +08:00
CORE_PEER_ADDRESS=peer${peer}.org1.example.com:7052 \
CORE_CHAINCODE_ID_NAME=mycc:${version} \
2017-11-02 10:19:05 +08:00
nohup ./scripts/chaincode_example02 > chaincode_dev.log &
res=$?
cat log.txt
verifyResult $res "Chaincode start in dev mode has Failed"
2017-11-06 12:18:37 +08:00
echo_g "=== Chaincode started in dev mode === "
2017-11-02 10:19:05 +08:00
echo
}
2017-11-14 15:29:27 +08:00
# chaincodeUpgrade channel peer name version args
2017-11-02 10:19:05 +08:00
chaincodeUpgrade () {
2017-11-14 15:29:27 +08:00
local channel=$1
2017-11-24 14:45:16 +08:00
local org=$2
local peer=$3
local name=$4
local version=$5
local args=$6
2017-11-23 22:22:20 +08:00
echo_b "=== Upgrade chaincode to version $version on peer$peer in channel ${channel} === "
2017-11-14 15:29:27 +08:00
2017-11-24 14:45:16 +08:00
setEnvs $org $peer
2017-11-02 10:19:05 +08:00
# 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
2017-11-14 15:29:27 +08:00
peer chaincode upgrade \
-o ${ORDERER_URL} \
-C ${channel} \
-n ${name} \
-v ${version} \
-c ${args} \
>&log.txt
2017-11-02 10:19:05 +08:00
else
2017-11-14 15:29:27 +08:00
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
2017-11-02 10:19:05 +08:00
fi
res=$?
cat log.txt
2017-11-14 15:29:27 +08:00
verifyResult $res "Upgrade execution on peer$peer failed "
2017-11-23 22:22:20 +08:00
echo_g "=== Upgrade transaction on peer$peer in channel ${channel} is successful === "
2017-11-02 10:19:05 +08:00
echo
}
2017-11-14 15:29:27 +08:00
# Fetch some block from a given channel: channel, peer, blockNum
2017-11-02 10:19:05 +08:00
channelFetch () {
2017-11-14 15:29:27 +08:00
local channel=$1
2017-11-24 14:45:16 +08:00
local org=$2
local peer=$3
local num=$4
2017-12-01 23:11:37 +08:00
echo_b "=== Fetch block $num of channel $channel === "
2017-11-14 15:29:27 +08:00
2017-12-02 09:36:01 +08:00
#setEnvs $org $peer
setOrdererEnvs
2017-11-02 10:19:05 +08:00
# 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
2017-12-01 23:11:37 +08:00
peer channel fetch $num ${CHANNEL_ARTIFACTS}/${channel}_${num}.block \
2017-11-14 15:29:27 +08:00
-o ${ORDERER_URL} \
-c ${channel} \
2017-11-02 11:31:08 +08:00
>&log.txt
2017-11-02 10:19:05 +08:00
else
2017-12-01 23:11:37 +08:00
peer channel fetch $num ${CHANNEL_ARTIFACTS}/${channel}_${num}.block \
2017-11-14 15:29:27 +08:00
-o ${ORDERER_URL} \
-c ${channel} \
2017-11-02 10:19:05 +08:00
--tls \
2017-11-14 15:29:27 +08:00
--cafile ${ORDERER_TLS_CA} \
2017-11-02 11:31:08 +08:00
>&log.txt
2017-11-02 10:19:05 +08:00
fi
res=$?
cat log.txt
2017-12-01 23:11:37 +08:00
verifyResult $res "Fetch block $num of channel $channel failed"
echo_g "=== Fetch block $num of channel $channel is successful === "
2017-11-26 19:50:49 +08:00
}
# configtxlator encode json to pb
# Usage: configtxlatorEncode msgType input output
configtxlatorEncode() {
local msgType=$1
local input=$2
local output=$3
2017-12-01 17:34:21 +08:00
echo_b "Encode $input --> $output using type $msgType"
2017-11-26 19:50:49 +08:00
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
2017-12-01 23:11:37 +08:00
echo_b "Config Decode $input --> $output using type $msgType"
2017-11-26 19:50:49 +08:00
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
2017-12-01 17:34:21 +08:00
echo_b "Config Compare $origin vs $updated > ${output} in channel $channel"
2017-11-26 19:50:49 +08:00
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"
2017-11-02 10:19:05 +08:00
}