docker-compose-files/hyperledger_fabric/latest/scripts/func.sh

489 lines
13 KiB
Bash
Raw Normal View History

#!/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
2017-12-06 16:24:33 +08:00
# 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
2017-12-06 16:24:33 +08:00
fi
2017-12-06 16:24:33 +08:00
# Verify $1 is not 0, then output error msg $2 and exit
verifyResult () {
if [ $1 -ne 0 ] ; then
2017-12-06 16:24:33 +08:00
echo_b "$2"
echo_r "=== ERROR !!! FAILED to execute End-2-End Scenario ==="
exit 1
fi
}
2017-12-06 16:24:33 +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`
}
# 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`
2017-12-06 16:24:33 +08:00
# env |grep CORE
}
checkOSNAvailability() {
#Use orderer's MSP for fetching system channel config block
CORE_PEER_LOCALMSPID="OrdererMSP"
2017-12-06 16:24:33 +08:00
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
2017-12-06 16:24:33 +08:00
echo "Attempting to fetch system channel ...$(($(date +%s)-starttime)) secs"
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
2017-12-06 16:24:33 +08:00
peer channel fetch 0 -o ${ORDERER_URL} -c "testchainid" >&log.txt
else
2017-12-06 16:24:33 +08:00
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 ..."
2017-12-06 16:24:33 +08:00
echo "=== Ordering Service is up and running === "
echo
}
2017-12-06 16:24:33 +08:00
# 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
2017-11-02 10:29:33 +08:00
peer channel create \
2017-12-06 16:24:33 +08:00
-o ${ORDERER_URL} \
-c ${channel} \
-f ${CHANNEL_ARTIFACTS}/${channel_tx} \
2017-11-02 10:29:33 +08:00
--timeout $TIMEOUT \
>&log.txt
else
2017-11-02 10:29:33 +08:00
peer channel create \
2017-12-06 16:24:33 +08:00
-o ${ORDERER_URL} \
-c ${channel} \
-f ${CHANNEL_ARTIFACTS}/${channel_tx} \
2017-11-02 10:29:33 +08:00
--timeout $TIMEOUT \
2017-12-06 16:24:33 +08:00
--tls $CORE_PEER_TLS_ENABLED \
--cafile ${ORDERER_TLS_CA} \
2017-11-02 10:29:33 +08:00
>&log.txt
fi
2017-12-06 16:24:33 +08:00
return $?
}
# Use peer0/org1 to create a channel
2017-12-19 14:40:39 +08:00
# channelCreate APP_CHANNEL APP_CHANNEL.tx org peer
2017-12-06 16:24:33 +08:00
channelCreate() {
local channel=$1
local channel_tx=$2
local org=$3
local peer=$4
2017-12-06 16:24:33 +08:00
echo_b "=== Create Channel ${channel} by org $org peer $peer === "
local counter=0
setEnvs $org $peer
channelCreateAction "${channel}" "${channel_tx}"
2017-12-06 16:24:33 +08:00
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}"
2017-12-06 16:24:33 +08:00
res=$?
let counter=${counter}+1
#COUNTER=` expr $COUNTER + 1`
done
cat log.txt
2017-12-06 16:24:33 +08:00
verifyResult ${res} "Channel ${channel} creation failed"
echo_g "=== Channel ${channel} is created. === "
}
2017-12-06 16:24:33 +08:00
# 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() {
2017-12-06 16:24:33 +08:00
local channel=$1
local org=$2
local peer=$3
setEnvs $org $peer
echo_b "=== Update Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on ${channel} === "
2017-11-02 10:29:33 +08:00
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
2017-12-06 16:24:33 +08:00
peer channel update \
-o ${ORDERER_URL} \
-c ${channel} \
-f ${CHANNEL_ARTIFACTS}/${CORE_PEER_LOCALMSPID}anchors.tx \
>&log.txt
else
2017-12-06 16:24:33 +08:00
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"
2017-12-06 16:24:33 +08:00
echo_g "=== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on ${channel} is updated. === "
sleep 2
}
2017-12-06 16:24:33 +08:00
# 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
2017-12-06 16:24:33 +08:00
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
2017-12-06 16:24:33 +08:00
# chaincodeInstantiate channel org peer name version args
chaincodeInstantiate () {
2017-12-06 16:24:33 +08:00
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
2017-11-02 10:29:33 +08:00
peer chaincode instantiate \
2017-12-06 16:24:33 +08:00
-o ${ORDERER_URL} \
-C ${channel} \
-n ${name} \
-v ${version} \
-c ${args} \
2017-11-02 10:29:33 +08:00
-P "OR ('Org1MSP.member','Org2MSP.member')" \
>&log.txt
else
2017-11-02 10:29:33 +08:00
peer chaincode instantiate \
2017-12-06 16:24:33 +08:00
-o ${ORDERER_URL} \
-C ${channel} \
-n ${name} \
-v ${version} \
-c ${args} \
2017-11-02 10:29:33 +08:00
-P "OR ('Org1MSP.member','Org2MSP.member')" \
--tls $CORE_PEER_TLS_ENABLED \
2017-12-06 16:24:33 +08:00
--cafile ${ORDERER_TLS_CA} \
2017-11-02 10:29:33 +08:00
>&log.txt
fi
res=$?
cat log.txt
2017-12-06 16:24:33 +08:00
verifyResult $res "ChaincodeInstantiation on peer$peer in channel ${channel} failed"
2017-12-07 13:55:24 +08:00
echo_g "=== Chaincode Instantiated in channel ${channel} by peer$peer ==="
2017-12-06 16:24:33 +08:00
}
# 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 === "
}
2017-12-06 16:24:33 +08:00
# query channel peer name args expected_result
chaincodeQuery () {
2017-12-06 16:24:33 +08:00
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)
2017-12-06 16:24:33 +08:00
setEnvs $org $peer
# 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-12-06 16:24:33 +08:00
echo_b "Attempting to Query peer$peer ...$(($(date +%s)-starttime)) secs"
peer chaincode query \
-C "${channel}" \
-n "${name}" \
-c "${args}" \
>&log.txt
2017-12-06 16:50:55 +08:00
rc=$?
2017-12-06 16:24:33 +08:00
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
2017-12-06 16:50:55 +08:00
cat log.txt
if [ $rc -ne 0 ]; then
sleep 2
fi
done
2017-12-06 16:50:55 +08:00
if [ $rc -eq 0 ]; then
2017-12-06 16:24:33 +08:00
echo_g "=== Query on peer$peer in channel ${channel} is successful === "
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
fi
}
2017-10-28 14:41:13 +08:00
# Start chaincode with dev mode
chaincodeStartDev () {
2017-12-06 16:24:33 +08:00
local peer=$1
local version=$2
#setEnvs $peer
#setEnvs 1 0
setEnvs 1 0
2017-10-28 14:41:13 +08:00
CORE_CHAINCODE_LOGLEVEL=debug \
2017-12-06 16:24:33 +08:00
CORE_PEER_ADDRESS=peer${peer}.org1.example.com:7052 \
CORE_CHAINCODE_ID_NAME=mycc:${version} \
2017-10-28 14:41:13 +08:00
nohup ./scripts/chaincode_example02 > chaincode_dev.log &
res=$?
cat log.txt
verifyResult $res "Chaincode start in dev mode has Failed"
2017-12-06 16:24:33 +08:00
echo_g "=== Chaincode started in dev mode === "
2017-10-28 14:41:13 +08:00
echo
}
2017-12-06 16:24:33 +08:00
# chaincodeUpgrade channel peer name version args
chaincodeUpgrade () {
2017-12-06 16:24:33 +08:00
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
2017-12-06 16:24:33 +08:00
peer chaincode upgrade \
-o ${ORDERER_URL} \
-C ${channel} \
-n ${name} \
-v ${version} \
-c ${args} \
>&log.txt
else
2017-12-06 16:24:33 +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
fi
res=$?
cat log.txt
2017-12-06 16:24:33 +08:00
verifyResult $res "Upgrade execution on peer$peer failed "
echo_g "=== Upgrade transaction on peer$peer in channel ${channel} is successful === "
echo
}
2017-12-06 16:24:33 +08:00
# Fetch some block from a given channel: channel, peer, blockNum
2017-10-08 13:55:16 +08:00
channelFetch () {
2017-12-06 16:24:33 +08:00
local channel=$1
local org=$2
local peer=$3
local num=$4
echo_b "=== Fetch block $num of channel $channel === "
#setEnvs $org $peer
setOrdererEnvs
2017-10-08 13:55:16 +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}" ] || [ "${CORE_PEER_TLS_ENABLED}" = "false" ]; then
2017-12-06 16:24:33 +08:00
peer channel fetch $num ${CHANNEL_ARTIFACTS}/${channel}_${num}.block \
-o ${ORDERER_URL} \
-c ${channel} \
>&log.txt
2017-10-08 13:55:16 +08:00
else
2017-12-06 16:24:33 +08:00
peer channel fetch $num ${CHANNEL_ARTIFACTS}/${channel}_${num}.block \
-o ${ORDERER_URL} \
-c ${channel} \
2017-10-08 13:55:16 +08:00
--tls \
2017-12-06 16:24:33 +08:00
--cafile ${ORDERER_TLS_CA} \
>&log.txt
2017-10-08 13:55:16 +08:00
fi
res=$?
cat log.txt
2017-12-19 14:40:39 +08:00
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
2017-12-06 16:24:33 +08:00
}
# 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}"
2017-12-06 16:24:33 +08:00
}
# 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 "$@"
}