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

587 lines
17 KiB
Bash
Raw Normal View History

2018-01-18 17:33:15 +08:00
#!/usr/bin/env bash
2017-09-01 20:09:18 +08:00
2018-01-18 17:33:15 +08:00
echo_r () {
[ $# -ne 1 ] && return 0
echo -e "\033[31m$1\033[0m"
}
echo_g () {
[ $# -ne 1 ] && return 0
echo -e "\033[32m$1\033[0m"
}
echo_y () {
[ $# -ne 1 ] && return 0
echo -e "\033[33m$1\033[0m"
}
echo_b () {
[ $# -ne 1 ] && return 0
echo -e "\033[34m$1\033[0m"
}
2017-09-01 20:09:18 +08:00
2018-01-03 21:19:09 +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
2018-01-18 17:33:15 +08:00
echo_r "Cannot find the variables.sh files, pls check"
2018-01-03 21:19:09 +08:00
exit 1
fi
2017-09-01 20:09:18 +08:00
2018-01-03 21:19:09 +08:00
# Verify $1 is not 0, then output error msg $2 and exit
2017-09-01 20:09:18 +08:00
verifyResult () {
if [ $1 -ne 0 ] ; then
2018-01-18 13:31:00 +08:00
echo "$2"
2018-01-03 21:19:09 +08:00
echo_r "=== ERROR !!! FAILED to execute End-2-End Scenario ==="
exit 1
2017-09-01 20:09:18 +08:00
fi
}
2018-01-18 13:31:00 +08:00
# set env to use orderOrg's identity
2018-01-03 21:19:09 +08:00
setOrdererEnvs () {
2018-01-18 13:31:00 +08:00
export CORE_PEER_LOCALMSPID="OrdererMSP"
export CORE_PEER_MSPCONFIGPATH=${ORDERER_ADMIN_MSP}
export CORE_PEER_TLS_ROOTCERT_FILE=${ORDERER_TLS_ROOTCERT}
2018-01-03 21:19:09 +08:00
#t="\${ORG${org}_PEER${peer}_URL}" && CORE_PEER_ADDRESS=`eval echo $t`
}
2018-01-18 17:33:15 +08:00
# Set global env variables for fabric cli, after setting:
2018-01-18 13:31:00 +08:00
# client is the admin as given org
# TLS root cert is configured to given peer's
# remote peer address is configured to given peer's
2018-01-18 17:33:15 +08:00
# CORE_PEER_LOCALMSPID=Org1MSP
# CORE_PEER_ADDRESS=peer0.org1.example.com:7051
# CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
# CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
2018-01-03 21:19:09 +08:00
# Usage: setEnvs org peer
setEnvs () {
local org=$1 # 1 or 2
local peer=$2 # 0 or 1
2018-01-18 13:31:00 +08:00
[ -z $org ] && [ -z $peer ] && echo_r "input param invalid" && exit -1
2018-01-18 17:33:15 +08:00
# a means a mirror peer
if [ $peer = "a" ]; then
export CORE_PEER_LOCALMSPID=Org1MSP
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
return
fi
2018-01-03 21:19:09 +08:00
local t=""
2018-01-18 13:31:00 +08:00
export CORE_PEER_LOCALMSPID="Org${org}MSP"
2018-01-03 21:19:09 +08:00
#CORE_PEER_MSPCONFIGPATH=\$${ORG${org}_ADMIN_MSP}
2018-01-18 13:31:00 +08:00
t="\${ORG${org}_PEER${peer}_URL}" && export CORE_PEER_ADDRESS=`eval echo $t`
t="\${ORG${org}_ADMIN_MSP}" && export CORE_PEER_MSPCONFIGPATH=`eval echo $t`
t="\${ORG${org}_PEER${peer}_TLS_ROOTCERT}" && export CORE_PEER_TLS_ROOTCERT_FILE=`eval echo $t`
2017-09-01 20:09:18 +08:00
2018-01-18 13:31:00 +08:00
#env |grep CORE
2017-09-01 20:09:18 +08:00
}
checkOSNAvailability() {
#Use orderer's MSP for fetching system channel config block
2018-01-18 13:31:00 +08:00
export CORE_PEER_LOCALMSPID="OrdererMSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${ORDERER_TLS_CA}
export CORE_PEER_MSPCONFIGPATH=${ORDERER_MSP}
2017-09-01 20:09:18 +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
2018-01-03 21:19:09 +08:00
echo "Attempting to fetch system channel ...$(($(date +%s)-starttime)) secs"
2017-09-01 20:09:18 +08:00
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
2018-01-03 21:19:09 +08:00
peer channel fetch 0 -o ${ORDERER_URL} -c "testchainid" >&log.txt
2017-09-01 20:09:18 +08:00
else
2018-01-03 21:19:09 +08:00
peer channel fetch 0 -o ${ORDERER_URL} -c "testchainid" --tls $CORE_PEER_TLS_ENABLED --cafile ${ORDERER_TLS_CA} >&log.txt
2017-09-01 20:09:18 +08:00
fi
test $? -eq 0 && VALUE=$(cat log.txt | awk '/Received block/ {print $NF}')
test "$VALUE" = "0" && let rc=0
done
2018-01-18 13:31:00 +08:00
[ $rc -ne 0 ] && cat log.txt
2017-09-01 20:09:18 +08:00
verifyResult $rc "Ordering Service is not available, Please try again ..."
2018-01-03 21:19:09 +08:00
echo "=== Ordering Service is up and running === "
2017-09-01 20:09:18 +08:00
echo
}
2018-01-03 21:19:09 +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
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 $?
}
2017-09-01 20:09:18 +08:00
# Use peer0/org1 to create a channel
2018-01-03 21:19:09 +08:00
# channelCreate APP_CHANNEL APP_CHANNEL.tx org peer
2017-09-22 15:16:19 +08:00
channelCreate() {
2018-01-03 21:19:09 +08:00
local channel=$1
2018-01-18 13:31:00 +08:00
local tx=$2
2018-01-03 21:19:09 +08:00
local org=$3
local peer=$4
2018-01-18 13:31:00 +08:00
[ -z $channel ] && [ -z $tx ] && [ -z $org ] && [ -z $peer ] && echo_r "input param invalid" && exit -1
2018-01-18 17:33:15 +08:00
echo "=== Create Channel ${channel} by org $org/peer $peer === "
2018-01-03 21:19:09 +08:00
setEnvs $org $peer
2018-01-18 17:33:15 +08:00
local rc=1
2018-01-18 13:31:00 +08:00
local counter=0
while [ ${counter} -lt ${MAX_RETRY} -a ${rc} -ne 0 ]; do
channelCreateAction "${channel}" "${tx}"
rc=$?
2018-01-03 21:19:09 +08:00
let counter=${counter}+1
#COUNTER=` expr $COUNTER + 1`
2018-01-18 17:33:15 +08:00
[ $rc -ne 0 ] && echo "Failed to create channel $channel, retry after 3s" && sleep 3
2018-01-03 21:19:09 +08:00
done
2018-01-18 13:31:00 +08:00
[ $rc -ne 0 ] && cat log.txt
verifyResult ${rc} "Channel ${channel} creation failed"
echo "=== Channel ${channel} is created. === "
2018-01-03 21:19:09 +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}
2018-01-18 13:31:00 +08:00
local rc=$?
while [ ${counter} -lt ${MAX_RETRY} -a ${rc} -ne 0 ]; do
echo "peer${peer} failed to join channel ${channel}, retry after 2s"
2018-01-03 21:19:09 +08:00
sleep 2
channelJoinAction ${channel}
2018-01-18 13:31:00 +08:00
rc=$?
2018-01-03 21:19:09 +08:00
let counter=${counter}+1
done
2018-01-18 13:31:00 +08:00
[ $rc -ne 0 ] && cat log.txt
verifyResult ${rc} "After $MAX_RETRY attempts, peer${peer} failed to Join the Channel"
2018-01-03 21:19:09 +08:00
}
# Join given (by default all) peers into the channel
# channelJoin channel org peer
channelJoin () {
local channel=$1
local org=$2
local peer=$3
2018-01-18 13:31:00 +08:00
[ -z $channel ] && [ -z $org ] && [ -z $peer ] && echo_r "input param invalid" && exit -1
2018-01-03 21:19:09 +08:00
2018-01-18 17:33:15 +08:00
echo "=== Join org $org/peer $peer into channel ${channel} === "
2018-01-03 21:19:09 +08:00
setEnvs $org $peer
channelJoinWithRetry ${channel} $peer
2018-01-18 17:33:15 +08:00
echo "=== org $org/peer $peer joined into channel ${channel} === "
2018-01-03 21:19:09 +08:00
}
getShasum () {
[ ! $# -eq 1 ] && exit 1
shasum ${1} | awk '{print $1}'
}
2018-01-18 13:31:00 +08:00
2018-01-03 21:19:09 +08:00
# Fetch all blocks for a channel
# Usage: channelFetchAll channel org peer
channelFetchAll () {
local channel=$1
local org=$2
local peer=$3
2018-01-18 13:31:00 +08:00
echo "=== Fetch all block for channel $channel === "
2018-01-03 21:19:09 +08:00
local block_file=/tmp/${channel}_newest.block
channelFetch ${channel} $org $peer "newest" ${block_file}
[ $? -ne 0 ] && exit 1
newest_block_shasum=$(getShasum ${block_file})
2018-01-18 13:31:00 +08:00
echo "fetch newest block ${block_file} with shasum=${newest_block_shasum}"
2018-01-03 21:19:09 +08:00
block_file=${CHANNEL_ARTIFACTS}/${channel}_config.block
channelFetch ${channel} $org $peer "config" ${block_file}
[ $? -ne 0 ] && exit 1
2018-01-18 13:31:00 +08:00
echo "fetch config block ${block_file}"
2018-01-03 21:19:09 +08:00
for i in $(seq 0 16); do # we at most fetch 16 blocks
block_file=${CHANNEL_ARTIFACTS}/${channel}_${i}.block
channelFetch ${channel} $org $peer $i ${block_file}
[ $? -ne 0 ] && exit 1
[ -f $block_file ] || break
2018-01-18 13:31:00 +08:00
echo "fetch block $i and saved into ${block_file}"
2018-01-03 21:19:09 +08:00
block_shasum=$(getShasum ${block_file})
2018-01-18 13:31:00 +08:00
[ ${block_shasum} = ${newest_block_shasum} ] && { echo "Block $i is the last one for channel $channel"; break; }
2018-01-03 21:19:09 +08:00
done
}
# Fetch some block from a given channel: channel, peer, blockNum
channelFetch () {
local channel=$1
local org=$2
local peer=$3
local num=$4
local block_file=$5
2018-01-18 13:31:00 +08:00
echo "=== Fetch block $num of channel $channel === "
2018-01-03 21:19:09 +08:00
#setEnvs $org $peer
2018-01-18 13:31:00 +08:00
setOrdererEnvs # system channel required id from ordererOrg
2018-01-03 21:19:09 +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
peer channel fetch $num ${block_file} \
-o ${ORDERER_URL} \
-c ${channel} \
>&log.txt
else
peer channel fetch $num ${block_file} \
-o ${ORDERER_URL} \
-c ${channel} \
--tls \
--cafile ${ORDERER_TLS_CA} \
>&log.txt
fi
if [ $? -ne 0 ]; then
cat log.txt
echo_r "Fetch block $num of channel $channel failed"
return 1
2017-09-01 20:09:18 +08:00
else
2018-01-18 13:31:00 +08:00
echo "=== Fetch block $num of channel $channel OK === "
2018-01-03 21:19:09 +08:00
return 0
2017-09-01 20:09:18 +08:00
fi
2018-01-03 21:19:09 +08:00
}
# Sign a channel config tx
# Usage: channelSignConfigTx channel org peer transaction
channelSignConfigTx () {
local channel=$1
local org=$2
local peer=$3
2018-01-18 13:31:00 +08:00
local tx=$4
[ -z $channel ] && [ -z $tx ] && [ -z $org ] && [ -z $peer ] && echo_r "input param invalid" && exit -1
2018-01-18 17:33:15 +08:00
echo "=== Sign channel config tx $tx for channel $channel by org $org/peer $peer === "
2018-01-18 13:31:00 +08:00
[ -f ${CHANNEL_ARTIFACTS}/${tx} ] || { echo_r "${tx} not exist"; exit 1; }
2018-01-03 21:19:09 +08:00
setEnvs $org $peer
2018-01-18 13:31:00 +08:00
peer channel signconfigtx -f ${CHANNEL_ARTIFACTS}/${tx} >&log.txt
rc=$?
[ $rc -ne 0 ] && cat log.txt
if [ $rc -ne 0 ]; then
2018-01-18 17:33:15 +08:00
echo_r "Sign channel config tx for channel $channel by org $org/peer $peer failed"
2017-09-21 16:01:38 +08:00
else
2018-01-18 17:33:15 +08:00
echo "=== Sign channel config tx channel $channel by org $org/peer $peer is successful === "
2017-09-21 16:01:38 +08:00
fi
2017-09-01 20:09:18 +08:00
}
2018-01-03 21:19:09 +08:00
# Update a channel config
# Usage: channelUpdate channel org peer transaction_file
channelUpdate() {
local channel=$1
local org=$2
local peer=$3
2018-01-18 13:31:00 +08:00
local tx=$4
[ -z $channel ] && [ -z $tx ] && [ -z $org ] && [ -z $peer ] && echo_r "input param invalid" && exit -1
2018-01-03 21:19:09 +08:00
setEnvs $org $peer
2018-01-18 13:31:00 +08:00
echo "=== Update config on channel ${channel} === "
[ -f ${CHANNEL_ARTIFACTS}/${tx} ] || { echo_r "${tx} not exist"; exit 1; }
2018-01-03 21:19:09 +08:00
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer channel update \
-o ${ORDERER_URL} \
-c ${channel} \
2018-01-18 13:31:00 +08:00
-f ${CHANNEL_ARTIFACTS}/${tx} \
2018-01-03 21:19:09 +08:00
>&log.txt
2017-09-01 20:09:18 +08:00
else
2018-01-03 21:19:09 +08:00
peer channel update \
-o ${ORDERER_URL} \
-c ${channel} \
2018-01-18 13:31:00 +08:00
-f ${CHANNEL_ARTIFACTS}/${tx} \
2018-01-03 21:19:09 +08:00
--tls $CORE_PEER_TLS_ENABLED \
--cafile ${ORDERER_TLS_CA} \
>&log.txt
2017-09-01 20:09:18 +08:00
fi
2018-01-18 13:31:00 +08:00
rc=$?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc "peer channel update failed"
echo "=== Channel ${channel} is updated. === "
2018-01-03 21:19:09 +08:00
sleep 2
2017-09-01 20:09:18 +08:00
}
2018-01-03 21:19:09 +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
2018-01-18 13:31:00 +08:00
[ -z $org ] && [ -z $peer ] && [ -z $name ] && [ -z $version ] && [ -z $path ] && echo_r "input param invalid" && exit -1
2018-01-18 17:33:15 +08:00
echo "=== Install Chaincode $name:$version ($path) on org ${org}/peer $peer === "
2018-01-03 21:19:09 +08:00
setEnvs $org $peer
peer chaincode install \
-n ${name} \
-v $version \
-p ${path} \
>&log.txt
2018-01-18 13:31:00 +08:00
rc=$?
[ $rc -ne 0 ] && cat log.txt
2018-01-18 17:33:15 +08:00
verifyResult $rc "Chaincode installation on remote org ${org}/peer$peer has Failed"
2018-01-18 13:31:00 +08:00
echo "=== Chaincode is installed on remote peer$peer === "
2018-01-03 21:19:09 +08:00
}
# 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
2018-01-18 13:31:00 +08:00
[ -z $channel ] && [ -z $org ] && [ -z $peer ] && [ -z $name ] && [ -z $version ] && [ -z $args ] && echo_r "input param invalid" && exit -1
2018-01-03 21:19:09 +08:00
setEnvs $org $peer
2018-01-18 17:33:15 +08:00
echo "=== chaincodeInstantiate for channel ${channel} on org $org/peer $peer ===="
2018-01-03 21:19:09 +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 \
-o ${ORDERER_URL} \
-C ${channel} \
-n ${name} \
-v ${version} \
-c ${args} \
-P "OR ('Org1MSP.member','Org2MSP.member')" \
>&log.txt
2017-09-01 20:09:18 +08:00
else
2018-01-03 21:19:09 +08:00
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
2017-09-01 20:09:18 +08:00
fi
2018-01-18 13:31:00 +08:00
rc=$?
[ $rc -ne 0 ] && cat log.txt
2018-01-18 17:33:15 +08:00
verifyResult $rc "ChaincodeInstantiation on org $org/peer$peer in channel ${channel} failed"
2018-01-18 13:31:00 +08:00
echo "=== Chaincode Instantiated in channel ${channel} by peer$peer ==="
2017-09-01 20:09:18 +08:00
}
2018-01-03 21:19:09 +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
2018-01-18 13:31:00 +08:00
[ -z $channel ] && [ -z $org ] && [ -z $peer ] && [ -z $name ] && [ -z $args ] && echo_r "input param invalid" && exit -1
echo "=== Invoke transaction on peer$peer in channel ${channel} === "
2018-01-03 21:19:09 +08:00
setEnvs $org $peer
2017-09-01 20:09:18 +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
2018-01-03 21:19:09 +08:00
peer chaincode invoke \
-o ${ORDERER_URL} \
-C ${channel} \
-n ${name} \
-c ${args} \
>&log.txt
2017-09-01 20:09:18 +08:00
else
2018-01-03 21:19:09 +08:00
peer chaincode invoke \
-o ${ORDERER_URL} \
-C ${channel} \
-n ${name} \
-c ${args} \
--tls $CORE_PEER_TLS_ENABLED \
--cafile ${ORDERER_TLS_CA} \
>&log.txt
2017-09-01 20:09:18 +08:00
fi
2018-01-18 13:31:00 +08:00
rc=$?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc "Invoke execution on peer$peer failed "
echo "=== Invoke transaction on peer$peer in channel ${channel} is successful === "
2017-09-01 20:09:18 +08:00
}
2018-01-03 21:19:09 +08:00
# query channel peer name args expected_result
2017-09-01 20:09:18 +08:00
chaincodeQuery () {
2018-01-03 21:19:09 +08:00
local channel=$1
local org=$2
local peer=$3
local name=$4
local args=$5
2018-01-18 13:31:00 +08:00
[ -z $channel ] && [ -z $org ] && [ -z $peer ] && [ -z $name ] && [ -z $args ] && echo_r "input param invalid" && exit -1
2018-01-03 21:19:09 +08:00
[ $# -gt 5 ] && local expected_result=$6
2018-01-18 17:33:15 +08:00
echo "=== Querying on org $org/peer $peer in channel ${channel}... === "
2017-09-01 20:09:18 +08:00
local rc=1
local starttime=$(date +%s)
2018-01-03 21:19:09 +08:00
setEnvs $org $peer
2017-09-01 20:09:18 +08:00
# we either get a successful response, or reach TIMEOUT
2018-01-03 21:19:09 +08:00
while [ "$(($(date +%s)-starttime))" -lt "$TIMEOUT" -a $rc -ne 0 ]; do
2018-01-18 17:33:15 +08:00
echo "Attempting to Query org ${org}/peer ${peer} ...$(($(date +%s)-starttime)) secs"
2018-01-03 21:19:09 +08:00
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
if [ $rc -ne 0 ]; then
2018-01-18 13:31:00 +08:00
cat log.txt
2018-01-03 21:19:09 +08:00
sleep 2
fi
2017-09-01 20:09:18 +08:00
done
2018-01-18 13:31:00 +08:00
# rc==0, or timeout
2018-01-03 21:19:09 +08:00
if [ $rc -eq 0 ]; then
2018-01-18 13:31:00 +08:00
echo "=== Query on peer$peer in channel ${channel} is successful === "
2017-09-01 20:09:18 +08:00
else
2018-01-03 21:19:09 +08:00
echo_r "=== Query result on peer$peer is INVALID, run `make stop clean` to clean ==="
exit 1
2017-09-01 20:09:18 +08:00
fi
}
2018-01-03 21:19:09 +08:00
# Start chaincode with dev mode
2018-01-05 12:47:35 +08:00
# TODO: use variables instead of hard-coded value
2018-01-03 21:19:09 +08:00
chaincodeStartDev () {
local peer=$1
local version=$2
2018-01-18 13:31:00 +08:00
[ -z $peer ] && [ -z $version ] && echo_r "input param invalid" && exit -1
2018-01-03 21:19:09 +08:00
setEnvs 1 0
CORE_CHAINCODE_LOGLEVEL=debug \
CORE_PEER_ADDRESS=peer${peer}.org1.example.com:7052 \
2018-01-05 12:47:35 +08:00
CORE_CHAINCODE_ID_NAME=${CC_02_NAME}:${version} \
2018-01-03 21:19:09 +08:00
nohup ./scripts/chaincode_example02 > chaincode_dev.log &
2018-01-18 13:31:00 +08:00
rc=$?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc "Chaincode start in dev mode has Failed"
echo "=== Chaincode started in dev mode === "
2017-09-11 10:33:29 +08:00
}
2018-01-03 21:19:09 +08:00
# chaincodeUpgrade channel peer name version args
2017-09-11 10:33:29 +08:00
chaincodeUpgrade () {
2018-01-03 21:19:09 +08:00
local channel=$1
local org=$2
local peer=$3
local name=$4
local version=$5
local args=$6
2018-01-18 13:31:00 +08:00
[ -z $channel ] && [ -z $org ] && [ -z $peer ] && [ -z $name ] && [ -z $version ] && [ -z $args ] && echo_r "input param invalid" && exit -1
2018-01-18 17:33:15 +08:00
echo "=== Upgrade chaincode to version $version on org ${org}/peer $peer in channel ${channel} === "
2018-01-03 21:19:09 +08:00
setEnvs $org $peer
2017-09-11 10:33:29 +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
2018-01-03 21:19:09 +08:00
peer chaincode upgrade \
-o ${ORDERER_URL} \
-C ${channel} \
-n ${name} \
-v ${version} \
-c ${args} \
>&log.txt
2017-09-11 10:33:29 +08:00
else
2018-01-03 21:19:09 +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-09-11 10:33:29 +08:00
fi
2018-01-18 13:31:00 +08:00
rc=$?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc "Upgrade execution on peer$peer failed "
echo "=== Upgrade transaction on peer$peer in channel ${channel} is successful === "
2017-09-11 10:33:29 +08:00
}
2018-01-03 21:19:09 +08:00
# configtxlator encode json to pb
# Usage: configtxlatorEncode msgType input output
configtxlatorEncode() {
local msgType=$1
local input=$2
local output=$3
2018-01-18 13:31:00 +08:00
echo "Encode $input --> $output using type $msgType"
2018-01-03 21:19:09 +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
2018-01-18 13:31:00 +08:00
echo "Config Decode $input --> $output using type $msgType"
2018-01-03 21:19:09 +08:00
if [ ! -f $input ]; then
echo_r "input file not found"
exit 1
fi
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=$4
2018-01-18 13:31:00 +08:00
echo "Config Compare $origin vs $updated > ${output} in channel $channel"
2018-01-03 21:19:09 +08:00
if [ ! -f $origin ] || [ ! -f $updated ]; then
echo_r "input file not found"
exit 1
fi
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 "$@"
2018-01-18 17:33:15 +08:00
}