2018-01-30 15:54:49 +08:00
#!/usr/bin/env bash
2017-10-05 23:21:54 +08:00
2018-01-30 15:54:49 +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-10-05 23:21:54 +08:00
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
2017-12-15 12:05:12 +08:00
else
2018-01-30 15:54:49 +08:00
echo_r "Cannot find the variables.sh files, pls check"
2017-12-15 12:05:12 +08:00
exit 1
2017-12-06 16:24:33 +08:00
fi
2017-10-05 23:21:54 +08:00
2017-12-06 16:24:33 +08:00
# Verify $1 is not 0, then output error msg $2 and exit
2017-10-05 23:21:54 +08:00
verifyResult ( ) {
if [ $1 -ne 0 ] ; then
2018-01-30 15:54:49 +08:00
echo " $2 "
2017-12-06 16:24:33 +08:00
echo_r "=== ERROR !!! FAILED to execute End-2-End Scenario ==="
exit 1
2017-10-05 23:21:54 +08:00
fi
}
2018-01-30 15:54:49 +08:00
# set env to use orderOrg's identity
2017-12-06 16:24:33 +08:00
setOrdererEnvs ( ) {
2018-01-30 15:54:49 +08:00
export CORE_PEER_LOCALMSPID = "OrdererMSP"
2019-05-02 17:30:13 +08:00
export CORE_PEER_MSPCONFIGPATH = ${ ORDERER0_ADMIN_MSP }
export CORE_PEER_TLS_ROOTCERT_FILE = ${ ORDERER0_TLS_ROOTCERT }
2017-12-06 16:24:33 +08:00
#t="\${ORG${org}_PEER${peer}_URL}" && CORE_PEER_ADDRESS=`eval echo $t`
}
2018-01-30 15:54:49 +08:00
# Set global env variables for fabric cli, after setting:
# client is the admin as given org
2018-08-03 13:11:50 +08:00
# TLS root cert is configured to given peer's tls ca
2018-01-30 15:54:49 +08:00
# remote peer address is configured to given peer's
2018-08-03 13:11:50 +08:00
# CORE_PEER_LOCALMSPID=Org1MSP # local msp id to use
# CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp # local msp path to use
# CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt # local trusted tls ca cert
# CORE_PEER_ADDRESS=peer0.org1.example.com:7051 # remote peer to send proposal to
2017-12-06 16:24:33 +08:00
# Usage: setEnvs org peer
setEnvs ( ) {
local org = $1 # 1 or 2
local peer = $2 # 0 or 1
2018-01-30 15:54:49 +08:00
[ -z $org ] && [ -z $peer ] && echo_r "input param invalid" && exit -1
2017-12-06 16:24:33 +08:00
local t = ""
2018-01-30 15:54:49 +08:00
export CORE_PEER_LOCALMSPID = " Org ${ org } MSP "
2017-12-06 16:24:33 +08:00
#CORE_PEER_MSPCONFIGPATH=\$${ORG${org}_ADMIN_MSP}
2018-01-30 15:54:49 +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-10-05 23:21:54 +08:00
2018-01-30 15:54:49 +08:00
#env |grep CORE
2019-10-01 05:11:37 +08:00
export FABRIC_LOGGING_SPEC = "INFO"
#export GOCACHE=/root/.cache/go-build
#go get
2017-10-05 23:21:54 +08:00
}
2017-12-06 16:24:33 +08:00
# Internal func called by channelCreate
2019-05-02 17:30:13 +08:00
# channelCreateAction channel tx orderer_url orderer_tls_rootcert
2017-12-06 16:24:33 +08:00
channelCreateAction( ) {
local channel = $1
2017-12-12 22:47:40 +08:00
local channel_tx = $2
2019-05-02 15:22:34 +08:00
local orderer_url = $3
2019-05-02 17:30:13 +08:00
local orderer_tls_rootcert = $4
2019-05-02 15:22:34 +08:00
2017-12-12 22:47:40 +08:00
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
-c ${ channel } \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2017-12-12 22:47:40 +08:00
-f ${ CHANNEL_ARTIFACTS } /${ channel_tx } \
2019-05-02 17:30:13 +08:00
--timeout " ${ TIMEOUT } s "
2017-10-05 23:21:54 +08:00
else
2017-11-02 10:29:33 +08:00
peer channel create \
2017-12-06 16:24:33 +08:00
-c ${ channel } \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2017-12-12 22:47:40 +08:00
-f ${ CHANNEL_ARTIFACTS } /${ channel_tx } \
2018-06-19 16:59:23 +08:00
--timeout " ${ TIMEOUT } s " \
2018-02-08 21:23:37 +08:00
--tls \
2019-05-02 17:30:13 +08:00
--cafile ${ orderer_tls_rootcert }
2017-10-05 23:21:54 +08:00
fi
2017-12-06 16:24:33 +08:00
return $?
}
2019-05-02 17:30:13 +08:00
# Use peer0/org1's identity to create a channel
# channelCreate APP_CHANNEL APP_CHANNEL.tx org peer orderer_url orderer_tls_rootcert
2017-12-06 16:24:33 +08:00
channelCreate( ) {
local channel = $1
2018-01-30 15:54:49 +08:00
local tx = $2
2017-12-12 22:47:40 +08:00
local org = $3
local peer = $4
2019-05-02 15:22:34 +08:00
local orderer_url = $5
2019-05-02 17:30:13 +08:00
local orderer_tls_rootcert = $6
2017-12-06 16:24:33 +08:00
2018-01-30 15:54:49 +08:00
[ -z $channel ] && [ -z $tx ] && [ -z $org ] && [ -z $peer ] && echo_r "input param invalid" && exit -1
echo " === Create Channel ${ channel } by org $org /peer $peer === "
2017-12-06 16:24:33 +08:00
setEnvs $org $peer
2018-01-30 15:54:49 +08:00
local rc = 1
local counter = 0
while [ ${ counter } -lt ${ MAX_RETRY } -a ${ rc } -ne 0 ] ; do
2019-05-02 17:30:13 +08:00
channelCreateAction ${ channel } ${ tx } ${ orderer_url } ${ orderer_tls_rootcert }
2018-01-30 15:54:49 +08:00
rc = $?
2017-12-06 16:24:33 +08:00
let counter = ${ counter } +1
#COUNTER=` expr $COUNTER + 1`
2018-01-30 15:54:49 +08:00
[ $rc -ne 0 ] && echo " Failed to create channel $channel , retry after 3s " && sleep 3
2017-12-06 16:24:33 +08:00
done
2018-01-30 15:54:49 +08:00
[ $rc -ne 0 ] && cat log.txt
verifyResult ${ rc } " Channel ${ channel } creation failed "
echo " === Channel ${ channel } is created. === "
2017-10-05 23:21:54 +08:00
}
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 }
2018-01-30 15:54:49 +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 "
2017-12-06 16:24:33 +08:00
sleep 2
channelJoinAction ${ channel }
2018-01-30 15:54:49 +08:00
rc = $?
2017-12-06 16:24:33 +08:00
let counter = ${ counter } +1
done
2018-01-30 15:54:49 +08:00
[ $rc -ne 0 ] && cat log.txt
verifyResult ${ rc } " After $MAX_RETRY attempts, peer ${ peer } failed to Join the Channel "
2017-12-06 16:24:33 +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-30 15:54:49 +08:00
[ -z $channel ] && [ -z $org ] && [ -z $peer ] && echo_r "input param invalid" && exit -1
2017-12-29 21:52:46 +08:00
2018-01-30 15:54:49 +08:00
echo " === Join org $org /peer $peer into channel ${ channel } === "
2017-12-06 16:24:33 +08:00
setEnvs $org $peer
channelJoinWithRetry ${ channel } $peer
2018-01-30 15:54:49 +08:00
echo " === org $org /peer $peer joined into channel ${ channel } === "
2017-12-06 16:24:33 +08:00
}
2017-12-29 21:52:46 +08:00
getShasum ( ) {
[ ! $# -eq 1 ] && exit 1
shasum ${ 1 } | awk '{print $1}'
}
2018-01-30 15:54:49 +08:00
2018-06-19 10:01:32 +08:00
# List the channel that the peer joined
# E.g., for peer 0 at org 1, will do
# channelList 1 0
channelList ( ) {
local org = $1
local peer = $2
echo " === List the channels that org ${ org } /peer ${ peer } joined === "
setEnvs $org $peer
peer channel list >& log.txt
rc = $?
[ $rc -ne 0 ] && cat log.txt
if [ $rc -ne 0 ] ; then
echo " === Failed to list the channels that org ${ org } /peer ${ peer } joined === "
else
echo " === Done to list the channels that org ${ org } /peer ${ peer } joined === "
fi
}
# Get the info of specific channel, including {height, currentBlockHash, previousBlockHash}.
# E.g., for peer 0 at org 1, get info of business channel will do
# channelGetInfo businesschannel 1 0
channelGetInfo ( ) {
local channel = $1
local org = $2
local peer = $3
2019-04-22 14:19:18 +08:00
echo " === Get channel info (height, currentBlockHash, previousBlockHash) of ${ channel } with id of org ${ org } /peer ${ peer } === "
2018-06-19 10:01:32 +08:00
setEnvs $org $peer
peer channel getinfo -c ${ channel } >& log.txt
rc = $?
2019-04-22 14:19:18 +08:00
cat log.txt
2018-06-19 10:01:32 +08:00
if [ $rc -ne 0 ] ; then
echo " === Fail to get channel info of ${ channel } with id of org ${ org } /peer ${ peer } === "
else
echo " === Done to get channel info of ${ channel } with id of org ${ org } /peer ${ peer } === "
fi
}
2017-12-29 21:52:46 +08:00
# Fetch all blocks for a channel
2019-05-02 17:30:13 +08:00
# Usage: channelFetchAll channel org peer orderer_url orderer_tls_rootcert
2017-12-29 21:52:46 +08:00
channelFetchAll ( ) {
local channel = $1
local org = $2
local peer = $3
2019-05-02 17:30:13 +08:00
local orderer_url = $4
local orderer_tls_rootcert = $5
2017-12-29 21:52:46 +08:00
2018-01-30 15:54:49 +08:00
echo " === Fetch all block for channel $channel === "
2017-12-29 21:52:46 +08:00
local block_file = /tmp/${ channel } _newest.block
2019-05-02 17:30:13 +08:00
channelFetch ${ channel } $org $peer ${ orderer_url } ${ orderer_tls_rootcert } "newest" ${ block_file }
2017-12-29 21:52:46 +08:00
[ $? -ne 0 ] && exit 1
newest_block_shasum = $( getShasum ${ block_file } )
2018-01-30 15:54:49 +08:00
echo " fetch newest block ${ block_file } with shasum= ${ newest_block_shasum } "
2017-12-29 21:52:46 +08:00
block_file = ${ CHANNEL_ARTIFACTS } /${ channel } _config.block
2019-05-02 17:30:13 +08:00
channelFetch ${ channel } $org $peer ${ orderer_url } ${ orderer_tls_rootcert } "config" ${ block_file }
2017-12-29 21:52:46 +08:00
[ $? -ne 0 ] && exit 1
2018-01-30 15:54:49 +08:00
echo " fetch config block ${ block_file } "
2017-12-29 21:52:46 +08:00
2018-01-02 16:49:51 +08:00
for i in $( seq 0 16) ; do # we at most fetch 16 blocks
2017-12-29 21:52:46 +08:00
block_file = ${ CHANNEL_ARTIFACTS } /${ channel } _${ i } .block
2019-05-02 17:30:13 +08:00
channelFetch ${ channel } $org $peer ${ orderer_url } ${ orderer_tls_rootcert } $i ${ block_file }
2017-12-29 21:52:46 +08:00
[ $? -ne 0 ] && exit 1
[ -f $block_file ] || break
2018-01-30 15:54:49 +08:00
echo " fetch block $i and saved into ${ block_file } "
2017-12-29 21:52:46 +08:00
block_shasum = $( getShasum ${ block_file } )
2018-01-30 15:54:49 +08:00
[ ${ block_shasum } = ${ newest_block_shasum } ] && { echo " Block $i is the last one for channel $channel " ; break; }
2017-12-29 21:52:46 +08:00
done
}
2019-05-02 17:30:13 +08:00
# Fetch some block from a given channel
# channelFetch channel org peer orderer_url blockNum block_file
2017-12-26 13:28:02 +08:00
channelFetch ( ) {
local channel = $1
local org = $2
local peer = $3
2019-05-02 17:30:13 +08:00
local orderer_url = $4
local orderer_tls_rootcert = $5
local num = $6
local block_file = $7
2018-01-30 15:54:49 +08:00
echo " === Fetch block $num of channel $channel === "
2017-12-26 13:28:02 +08:00
#setEnvs $org $peer
2018-01-30 15:54:49 +08:00
setOrdererEnvs # system channel required id from ordererOrg
2017-12-26 13:28:02 +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-29 21:52:46 +08:00
peer channel fetch $num ${ block_file } \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2017-12-26 13:28:02 +08:00
-c ${ channel } \
>& log.txt
else
2017-12-29 21:52:46 +08:00
peer channel fetch $num ${ block_file } \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2017-12-26 13:28:02 +08:00
-c ${ channel } \
--tls \
2019-05-02 17:30:13 +08:00
--cafile ${ orderer_tls_rootcert } \
2017-12-26 13:28:02 +08:00
>& log.txt
fi
2017-12-29 21:52:46 +08:00
if [ $? -ne 0 ] ; then
cat log.txt
2017-12-26 13:28:02 +08:00
echo_r " Fetch block $num of channel $channel failed "
2017-12-29 21:52:46 +08:00
return 1
2017-12-26 13:28:02 +08:00
else
2018-01-30 15:54:49 +08:00
echo " === Fetch block $num of channel $channel OK === "
2017-12-29 21:52:46 +08:00
return 0
2017-12-26 13:28:02 +08:00
fi
}
# Sign a channel config tx
# Usage: channelSignConfigTx channel org peer transaction
channelSignConfigTx ( ) {
local channel = $1
local org = $2
local peer = $3
2018-01-30 15:54:49 +08:00
local tx = $4
[ -z $channel ] && [ -z $tx ] && [ -z $org ] && [ -z $peer ] && echo_r "input param invalid" && exit -1
echo " === Sign channel config tx $tx for channel $channel by org $org /peer $peer === "
[ -f ${ CHANNEL_ARTIFACTS } /${ tx } ] || { echo_r " ${ tx } not exist " ; exit 1; }
2017-12-29 21:52:46 +08:00
2017-12-26 13:28:02 +08:00
setEnvs $org $peer
2018-01-30 15:54:49 +08:00
peer channel signconfigtx -f ${ CHANNEL_ARTIFACTS } /${ tx } >& log.txt
rc = $?
[ $rc -ne 0 ] && cat log.txt
if [ $rc -ne 0 ] ; then
echo_r " Sign channel config tx for channel $channel by org $org /peer $peer failed "
2017-12-26 13:28:02 +08:00
else
2018-01-30 15:54:49 +08:00
echo " === Sign channel config tx channel $channel by org $org /peer $peer is successful === "
2017-12-26 13:28:02 +08:00
fi
}
2017-12-29 21:52:46 +08:00
# Update a channel config
2019-05-02 17:30:13 +08:00
# Usage: channelUpdate channel org peer orderer_url orderer_tls_rootcert transaction_file
2017-12-29 21:52:46 +08:00
channelUpdate( ) {
2017-12-06 16:24:33 +08:00
local channel = $1
2019-04-21 11:18:26 +08:00
local org = $2
local peer = $3
2019-05-02 17:30:13 +08:00
local orderer_url = $4
local orderer_tls_rootcert = $5
local tx = $6
2018-01-30 15:54:49 +08:00
[ -z $channel ] && [ -z $tx ] && [ -z $org ] && [ -z $peer ] && echo_r "input param invalid" && exit -1
2019-04-21 11:18:26 +08:00
setEnvs $org $peer
2018-01-30 15:54:49 +08:00
echo " === Update config on channel ${ channel } === "
[ -f ${ CHANNEL_ARTIFACTS } /${ tx } ] || { echo_r " ${ tx } not exist " ; exit 1; }
2019-04-21 11:18:26 +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 \
-c ${ channel } \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2018-01-30 15:54:49 +08:00
-f ${ CHANNEL_ARTIFACTS } /${ tx } \
2017-12-06 16:24:33 +08:00
>& log.txt
2017-10-05 23:21:54 +08:00
else
2017-12-06 16:24:33 +08:00
peer channel update \
-c ${ channel } \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2018-01-30 15:54:49 +08:00
-f ${ CHANNEL_ARTIFACTS } /${ tx } \
2018-02-08 21:23:37 +08:00
--tls \
2019-05-02 17:30:13 +08:00
--cafile ${ orderer_tls_rootcert } \
2017-12-06 16:24:33 +08:00
>& log.txt
2017-10-05 23:21:54 +08:00
fi
2018-01-30 15:54:49 +08:00
rc = $?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc "peer channel update failed"
echo " === Channel ${ channel } is updated. === "
2017-12-06 16:24:33 +08:00
sleep 2
2017-10-05 23:21:54 +08:00
}
2019-10-01 05:11:37 +08:00
# Package and Install chaincode on the peer node
# chaincodeInstall org peer peer_url peer_tls cc_name version path
2017-12-06 16:24:33 +08:00
chaincodeInstall ( ) {
2019-04-21 11:18:26 +08:00
if [ " $# " -ne 7 ] ; then
echo_r "Wrong param number for chaincode install"
exit -1
fi
2017-12-06 16:24:33 +08:00
local org = $1
local peer = $2
2019-04-21 11:18:26 +08:00
local peer_url = $3
local peer_tls_root_cert = $4
local name = $5
local version = $6
local path = $7
2018-01-30 15:54:49 +08:00
[ -z $org ] && [ -z $peer ] && [ -z $name ] && [ -z $version ] && [ -z $path ] && echo_r "input param invalid" && exit -1
2018-08-22 17:27:45 +08:00
echo " === Install Chaincode on org ${ org } /peer ${ peer } === "
echo " name= ${ name } , version= ${ version } , path= ${ path } "
2017-12-06 16:24:33 +08:00
setEnvs $org $peer
2019-04-21 11:18:26 +08:00
echo "packaging chaincode into tar.gz package"
2019-06-17 16:02:26 +08:00
local label = ${ name }
#local label=${name}_${version}
2019-10-01 05:11:37 +08:00
echo " packaging chaincode ${ name } with path ${ path } and label ${ label } "
2019-04-01 20:54:04 +08:00
peer lifecycle chaincode package ${ name } .tar.gz \
2019-10-01 05:11:37 +08:00
--path ${ path } \
--lang golang \
--label ${ label }
rc = $?
[ $rc -ne 0 ] && echo " Error in packaging chaincode ${ name } " && exit -1
2019-04-01 20:54:04 +08:00
# v1.x action
#peer chaincode install \
# -n ${name} \
# -v $version \
# -p ${path} \
# >&log.txt
2019-10-02 08:41:08 +08:00
echo " installing chaincode to peer ${ peer } /org ${ org } "
peer lifecycle chaincode install \
--peerAddresses ${ peer_url } \
--tlsRootCertFiles ${ peer_tls_root_cert } \
${ name } .tar.gz | tee >& log.txt
2018-01-30 15:54:49 +08:00
rc = $?
[ $rc -ne 0 ] && cat log.txt
2019-10-02 08:41:08 +08:00
2019-04-01 20:54:04 +08:00
verifyResult $rc " Chaincode installation on remote org ${ org } /peer $peer has Failed "
2019-06-17 16:02:26 +08:00
echo " === Chaincode is installed on org ${ org } /peer $peer === "
2017-10-05 23:21:54 +08:00
}
2019-10-01 05:11:37 +08:00
# Query the installed chaincode
# chaincodeQueryCommitted org peer peer_url peer_tls_root_cert
chaincodeQueryInstalled ( ) {
if [ " $# " -ne 4 ] ; then
echo_r "Wrong param number for chaincode query installed"
exit -1
fi
local org = $1
local peer = $2
local peer_url = $3
local peer_tls_root_cert = $4
setEnvs $org $peer
echo " Query the installed chaincode on peer $peer at $peer_url "
peer lifecycle chaincode queryinstalled \
--peerAddresses ${ peer_url } \
--tlsRootCertFiles ${ peer_tls_root_cert } \
--connTimeout "3s"
rc = $?
[ $rc -ne 0 ] && cat log.txt
cat log.txt
verifyResult $rc " ChaincodeQueryInstalled Failed: org ${ org } /peer $peer "
}
2019-11-07 06:45:46 +08:00
# Get the installed chaincode packages
# chaincodeGetCommitted org peer peer_url peer_tls_root_cert cc_name
chaincodeGetInstalled ( ) {
if [ " $# " -ne 5 ] ; then
echo_r "Wrong param number for chaincode get installed"
exit -1
fi
local org = $1
local peer = $2
local peer_url = $3
local peer_tls_root_cert = $4
local cc_name = $5
setEnvs $org $peer
echo "querying installed chaincode and get its package id"
peer lifecycle chaincode queryinstalled >& query.log
local label = ${ cc_name }
#package_id=$(grep -o "${name}_${version}:[a-z0-9]*" query.log|cut -d ":" -f 2)
package_id = $( grep -o " ${ label } :[a-z0-9]* " query.log)
echo " Get the installed chaincode package with id= ${ package_id } on peer $peer at $peer_url "
peer lifecycle chaincode getinstalledpackage \
--peerAddresses ${ peer_url } \
--tlsRootCertFiles ${ peer_tls_root_cert } \
--package-id ${ package_id } \
--output-directory ./ \
--connTimeout "3s"
rc = $?
[ $rc -ne 0 ] && cat log.txt
cat log.txt
verifyResult $rc " ChaincodeGetInstalled Failed: org ${ org } /peer $peer "
}
2019-04-21 11:18:26 +08:00
# Approve the chaincode definition
2019-10-01 05:11:37 +08:00
# chaincodeApproveForMyOrg channel org peer peer_url peer_tls_root_cert orderer_url orderer_tls_rootcert channel name version
chaincodeApproveForMyOrg ( ) {
2019-05-02 17:30:13 +08:00
if [ " $# " -ne 9 -a " $# " -ne 11 ] ; then
2019-04-22 14:19:18 +08:00
echo_r "Wrong param number for chaincode approve"
2019-04-21 11:18:26 +08:00
exit -1
fi
local org = $1
local peer = $2
local peer_url = $3
local peer_tls_root_cert = $4
2019-05-02 17:30:13 +08:00
local orderer_url = $5
local orderer_tls_rootcert = $6
local channel = $7
local name = $8
local version = $9
2019-04-21 11:18:26 +08:00
local collection_config = "" # collection config file path for sideDB
local policy = "OR ('Org1MSP.member','Org2MSP.member')" # endorsement policy
2019-05-02 17:30:13 +08:00
if [ ! -z " ${ 10 } " ] ; then
collection_config = ${ 10 }
2019-04-21 11:18:26 +08:00
fi
2019-05-02 17:30:13 +08:00
if [ ! -z " ${ 11 } " ] ; then
policy = ${ 12 }
2019-04-21 11:18:26 +08:00
fi
setEnvs $org $peer
echo "querying installed chaincode and get its package id"
peer lifecycle chaincode queryinstalled >& query.log
cat query.log
2019-06-17 16:02:26 +08:00
local label = ${ name }
2019-04-21 11:18:26 +08:00
#package_id=$(grep -o "${name}_${version}:[a-z0-9]*" query.log|cut -d ":" -f 2)
2019-06-17 16:02:26 +08:00
package_id = $( grep -o " ${ label } :[a-z0-9]* " query.log)
2019-04-21 11:18:26 +08:00
echo " Approve package id= ${ package_id } by Org ${ org } /Peer ${ peer } "
# use the --init-required flag to request the ``Init`` function be invoked to initialize the chaincode
if [ -z " $CORE_PEER_TLS_ENABLED " -o " $CORE_PEER_TLS_ENABLED " = "false" ] ; then
peer lifecycle chaincode approveformyorg \
--peerAddresses ${ peer_url } \
--channelID ${ channel } \
--name ${ name } \
--version ${ version } \
--init-required \
2019-10-01 05:11:37 +08:00
--package-id ${ package_id } \
2019-04-21 11:18:26 +08:00
--sequence 1 \
--signature-policy " ${ policy } " \
2019-05-02 17:30:13 +08:00
--waitForEvent \
--orderer ${ orderer_url } >& log.txt
2019-04-21 11:18:26 +08:00
else
peer lifecycle chaincode approveformyorg \
--peerAddresses ${ peer_url } \
--tlsRootCertFiles ${ peer_tls_root_cert } \
--channelID ${ channel } \
--name ${ name } \
--version ${ version } \
--init-required \
--package-id ${ package_id } \
--sequence 1 \
--signature-policy " ${ policy } " \
--waitForEvent \
2019-05-02 17:30:13 +08:00
--orderer ${ orderer_url } \
2019-04-21 11:18:26 +08:00
--tls true \
2019-05-02 17:30:13 +08:00
--cafile ${ orderer_tls_rootcert } >& log.txt
2019-04-21 11:18:26 +08:00
fi
2019-04-22 14:19:18 +08:00
rc = $?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc " Chaincode Approval on remote org ${ org } /peer $peer has Failed "
echo " === Chaincode is approved on remote peer $peer === "
}
# Query the Approve the chaincode definition
2019-10-01 05:11:37 +08:00
# chaincodeCheckCommitReadiness channel org peer name version
chaincodeCheckCommitReadiness ( ) {
2019-04-22 14:19:18 +08:00
if [ " $# " -ne 7 ] ; then
echo_r "Wrong param number for chaincode queryapproval"
exit -1
fi
local org = $1
local peer = $2
local peer_url = $3
local peer_tls_root_cert = $4
local channel = $5
local name = $6
local version = $7
setEnvs $org $peer
echo " Query the approval status of the chaincode $name $version "
2019-10-01 05:11:37 +08:00
peer lifecycle chaincode checkcommitreadiness \
2019-04-21 11:18:26 +08:00
--peerAddresses ${ peer_url } \
--tlsRootCertFiles ${ peer_tls_root_cert } \
--channelID ${ channel } \
--name ${ name } \
--version ${ version }
rc = $?
[ $rc -ne 0 ] && cat log.txt
2019-04-22 14:19:18 +08:00
verifyResult $rc " ChaincodeQueryApproval Failed: org ${ org } /peer $peer "
2019-04-21 11:18:26 +08:00
}
# Anyone can commit the chaincode definition once it's approved by major
2019-05-02 17:30:13 +08:00
# chaincodeCommit org peer channel orderer_url orderer_tls_rootcert name version [collection-config] [endorse-policy]
2019-04-21 11:18:26 +08:00
chaincodeCommit ( ) {
2019-05-02 17:30:13 +08:00
if [ " $# " -ne 7 -a " $# " -ne 9 ] ; then
2019-04-22 14:19:18 +08:00
echo_r "Wrong param number for chaincode commit"
2019-04-21 11:18:26 +08:00
exit -1
fi
local org = $1
local peer = $2
local channel = $3
2019-05-02 17:30:13 +08:00
local orderer_url = $4
local orderer_tls_rootcert = $5
local name = $6
local version = $7
2019-04-21 11:18:26 +08:00
local collection_config = "" # collection config file path for sideDB
local policy = "OR ('Org1MSP.member','Org2MSP.member')" # endorsement policy
2019-05-02 17:30:13 +08:00
if [ ! -z " $8 " ] ; then
collection_config = $8
2019-04-21 11:18:26 +08:00
fi
2019-05-02 17:30:13 +08:00
if [ ! -z " $9 " ] ; then
policy = $9 # chaincode endorsement policy
2019-04-21 11:18:26 +08:00
fi
setEnvs $org $peer
echo "querying installed chaincode and get its package id"
peer lifecycle chaincode queryinstalled >& query.log
2019-06-17 16:02:26 +08:00
label = ${ name }
2019-04-21 11:18:26 +08:00
#package_id=$(grep -o "${name}_${version}:[a-z0-9]*" query.log|cut -d ":" -f 2)
2019-06-17 16:02:26 +08:00
package_id = $( grep -o " ${ label } :[a-z0-9]* " query.log)
2019-04-21 11:18:26 +08:00
2019-04-22 14:19:18 +08:00
echo " Committing package id= ${ package_id } by Org ${ org } /Peer ${ peer } "
2019-04-21 11:18:26 +08:00
# use the --init-required flag to request the ``Init`` function be invoked to initialize the chaincode
if [ -z " $CORE_PEER_TLS_ENABLED " -o " $CORE_PEER_TLS_ENABLED " = "false" ] ; then
peer lifecycle chaincode commit \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2019-04-21 11:18:26 +08:00
--channelID ${ channel } \
--name ${ name } \
--version ${ version } \
--init-required \
--sequence 1 \
--peerAddresses ${ ORG1_PEER0_URL } \
--tlsRootCertFiles ${ ORG1_PEER0_TLS_ROOTCERT } \
--peerAddresses ${ ORG2_PEER0_URL } \
--tlsRootCertFiles ${ ORG2_PEER0_TLS_ROOTCERT } \
--waitForEvent \
--collections-config " ${ collection_config } " \
--signature-policy " ${ policy } "
else
peer lifecycle chaincode commit \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2019-04-21 11:18:26 +08:00
--channelID ${ channel } \
--name ${ name } \
--version ${ version } \
--init-required \
--sequence 1 \
--peerAddresses ${ ORG1_PEER0_URL } \
--tlsRootCertFiles ${ ORG1_PEER0_TLS_ROOTCERT } \
--peerAddresses ${ ORG2_PEER0_URL } \
--tlsRootCertFiles ${ ORG2_PEER0_TLS_ROOTCERT } \
--waitForEvent \
--collections-config " ${ collection_config } " \
--signature-policy " ${ policy } " \
--tls true \
2019-05-02 17:30:13 +08:00
--cafile ${ orderer_tls_rootcert } >& log.txt
2019-04-21 11:18:26 +08:00
fi
rc = $?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc " Chaincode Commit on remote org ${ org } /peer $peer has Failed "
echo " === Chaincode is committed on channel $channel === "
2019-04-22 14:19:18 +08:00
}
2019-04-21 11:18:26 +08:00
2019-04-22 14:19:18 +08:00
# Query the Commit the chaincode definition
2019-10-01 05:11:37 +08:00
# chaincodeQueryCommitted org peer peer_url peer_tls_root_cert channel cc_name
chaincodeQueryCommitted ( ) {
2019-04-22 14:19:18 +08:00
if [ " $# " -ne 6 ] ; then
echo_r "Wrong param number for chaincode querycommit"
exit -1
fi
local org = $1
local peer = $2
local peer_url = $3
local peer_tls_root_cert = $4
local channel = $5
local name = $6
setEnvs $org $peer
echo " Query the committed status of chaincode $name with ${ ORG1_PEER0_URL } "
2019-04-21 11:18:26 +08:00
peer lifecycle chaincode querycommitted \
2019-04-22 14:19:18 +08:00
--peerAddresses ${ peer_url } \
2019-10-01 05:11:37 +08:00
--tlsRootCertFiles ${ peer_tls_root_cert } \
2019-04-21 11:18:26 +08:00
--channelID ${ channel } \
--name ${ name }
2019-04-22 14:19:18 +08:00
rc = $?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc " ChaincodeQueryCommit Failed: org ${ org } /peer $peer "
2019-04-21 11:18:26 +08:00
}
2017-10-05 23:21:54 +08:00
# Instantiate chaincode on specifized peer node
2019-05-02 17:30:13 +08:00
# chaincodeInstantiate channel org peer orderer_url name version args
2017-10-05 23:21:54 +08:00
chaincodeInstantiate ( ) {
2019-05-02 17:30:13 +08:00
if [ " $# " -gt 9 -a " $# " -lt 7 ] ; then
2018-08-27 10:52:46 +08:00
echo_r "Wrong param number for chaincode instantaite"
2018-08-22 17:27:45 +08:00
exit -1
fi
2017-12-06 16:24:33 +08:00
local channel = $1
local org = $2
local peer = $3
2019-05-02 17:30:13 +08:00
local orderer_url = $4
local name = $5
local version = $6
local args = $7
2018-08-22 17:27:45 +08:00
local collection_config = "" # collection config file path for sideDB
local policy = "OR ('Org1MSP.member','Org2MSP.member')" # endorsement policy
2019-05-02 17:30:13 +08:00
if [ ! -z " $8 " ] ; then
collection_config = $8
2018-08-22 17:27:45 +08:00
fi
2019-05-02 17:30:13 +08:00
if [ ! -z " $9 " ] ; then
policy = $9
2018-08-22 17:27:45 +08:00
fi
2018-08-27 10:52:46 +08:00
2017-12-06 16:24:33 +08:00
setEnvs $org $peer
2018-01-30 15:54:49 +08:00
echo " === chaincodeInstantiate for channel ${ channel } on org $org /peer $peer ==== "
2018-08-22 17:27:45 +08:00
echo " name= ${ name } , version= ${ version } , args= ${ args } , collection_config= ${ collection_config } , policy= ${ policy } "
2017-10-05 23:21:54 +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-02 10:29:33 +08:00
peer chaincode instantiate \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2017-12-06 16:24:33 +08:00
-C ${ channel } \
-n ${ name } \
-v ${ version } \
-c ${ args } \
2018-08-22 17:27:45 +08:00
-P " ${ policy } " \
--collections-config " ${ collection_config } " \
2017-11-02 10:29:33 +08:00
>& log.txt
2017-10-05 23:21:54 +08:00
else
2017-11-02 10:29:33 +08:00
peer chaincode instantiate \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2017-12-06 16:24:33 +08:00
-C ${ channel } \
-n ${ name } \
-v ${ version } \
-c ${ args } \
2018-08-22 17:27:45 +08:00
-P " ${ policy } " \
--collections-config " ${ collection_config } " \
2018-02-08 21:23:37 +08:00
--tls \
2019-05-02 17:30:13 +08:00
--cafile ${ ORDERER0_TLS_CA } \
2017-11-02 10:29:33 +08:00
>& log.txt
2017-10-05 23:21:54 +08:00
fi
2018-01-30 15:54:49 +08:00
rc = $?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc " ChaincodeInstantiation on org $org /peer $peer in channel ${ channel } failed "
echo " === Chaincode Instantiated in channel ${ channel } by peer $peer === "
2017-12-06 16:24:33 +08:00
}
2019-04-21 11:18:26 +08:00
# Invoke the Init func of chaincode to start the container
# Usage: chaincodeInit org peer channel orderer name args peer_url peer_org_tlsca
chaincodeInit ( ) {
2019-04-22 14:19:18 +08:00
if [ " $# " -ne 8 ] ; then
echo_r "Wrong param number for chaincode Init"
exit -1
fi
2019-04-21 11:18:26 +08:00
local org = $1
local peer = $2
local channel = $3
local orderer = $4
local name = $5
local args = $6
local peer_url = $7
local peer_org_tlsca = $8
[ -z $channel ] && [ -z $org ] && [ -z $peer ] && [ -z $name ] && [ -z $args ] && echo_r "input param invalid" && exit -1
echo " === chaincodeInit to orderer by id of org ${ org } /peer ${ peer } === "
echo " channel= ${ channel } , name= ${ name } , args= ${ args } "
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 } \
--channelID ${ channel } \
--name ${ name } \
--peerAddresses ${ peer_url } \
2019-11-07 06:45:46 +08:00
--tlsRootCertFiles ${ peer_org_tlsca } \
2019-04-21 11:18:26 +08:00
--isInit \
-c ${ args } \
>& log.txt
else
peer chaincode invoke \
-o ${ orderer } \
--channelID ${ channel } \
--name ${ name } \
--peerAddresses ${ peer_url } \
2019-11-07 06:45:46 +08:00
--tlsRootCertFiles ${ peer_org_tlsca } \
2019-04-21 11:18:26 +08:00
--isInit \
-c ${ args } \
--tls \
2019-05-02 17:30:13 +08:00
--cafile ${ ORDERER0_TLS_CA } \
2019-04-21 11:18:26 +08:00
>& log.txt
fi
rc = $?
[ $rc -ne 0 ] && cat log.txt
2019-04-22 14:19:18 +08:00
verifyResult $rc " Chaincode Init failed: peer $peer in channel ${ channel } "
echo " === Chaincode Init done: peer $peer in channel ${ channel } === "
2019-04-21 11:18:26 +08:00
}
2017-12-06 16:24:33 +08:00
2019-04-21 11:18:26 +08:00
# Usage: chaincodeInvoke org peer channel orderer name args peer_url peer_org_tlsca
2017-12-06 16:24:33 +08:00
chaincodeInvoke ( ) {
2019-05-02 17:30:13 +08:00
if [ " $# " -ne 9 ] ; then
2019-04-22 14:19:18 +08:00
echo_r "Wrong param number for chaincode Invoke"
exit -1
fi
2019-04-21 11:18:26 +08:00
local org = $1
local peer = $2
2019-04-22 14:19:18 +08:00
local peer_url = $3
local peer_org_tlsca = $4
local channel = $5
2019-05-02 17:30:13 +08:00
local orderer_url = $6
local orderer_tls_rootcert = $7
local name = $8
local args = $9
2019-04-21 11:18:26 +08:00
2018-01-30 15:54:49 +08:00
[ -z $channel ] && [ -z $org ] && [ -z $peer ] && [ -z $name ] && [ -z $args ] && echo_r "input param invalid" && exit -1
2018-08-22 17:27:45 +08:00
echo " === chaincodeInvoke to orderer by id of org ${ org } /peer ${ peer } === "
echo " channel= ${ channel } , name= ${ name } , args= ${ args } "
2017-12-06 16:24:33 +08:00
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 \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2019-04-21 11:18:26 +08:00
--channelID ${ channel } \
--name ${ name } \
--peerAddresses ${ peer_url } \
--tlsRootCertFiles ${ peer_org_tlsca } \
2017-12-06 16:24:33 +08:00
-c ${ args } \
>& log.txt
else
peer chaincode invoke \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2019-04-21 11:18:26 +08:00
--channelID ${ channel } \
--name ${ name } \
--peerAddresses ${ peer_url } \
--tlsRootCertFiles ${ peer_org_tlsca } \
2017-12-06 16:24:33 +08:00
-c ${ args } \
2018-02-08 21:23:37 +08:00
--tls \
2019-05-02 17:30:13 +08:00
--cafile ${ orderer_tls_rootcert } \
2017-12-06 16:24:33 +08:00
>& log.txt
fi
2018-01-30 15:54:49 +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-10-05 23:21:54 +08:00
}
2019-04-21 11:18:26 +08:00
# query org peer channel name args expected_result
2017-10-05 23:21:54 +08:00
chaincodeQuery ( ) {
2019-04-22 14:19:18 +08:00
if [ " $# " -ne 7 -a " $# " -ne 8 ] ; then
echo_r " Wrong param number $# for chaincode Query "
echo $*
exit -1
fi
2019-04-21 11:18:26 +08:00
local org = $1
local peer = $2
2019-04-22 14:19:18 +08:00
local peer_url = $3
local peer_org_tlsca = $4
local channel = $5
local name = $6
local args = $7
local expected_result = ""
[ $# -eq 8 ] && local expected_result = $8
2018-01-30 15:54:49 +08:00
[ -z $channel ] && [ -z $org ] && [ -z $peer ] && [ -z $name ] && [ -z $args ] && echo_r "input param invalid" && exit -1
2019-04-22 14:19:18 +08:00
2019-04-21 11:18:26 +08:00
echo " === chaincodeQuery to org $org /peer $peer === "
2019-04-22 14:19:18 +08:00
echo " channel= ${ channel } , name= ${ name } , args= ${ args } , expected_result= ${ expected_result } "
2019-04-21 11:18:26 +08:00
local rc = 1
local starttime = $( date +%s)
setEnvs $org $peer
2019-04-22 14:19:18 +08:00
2019-04-21 11:18:26 +08:00
# we either get a successful response, or reach TIMEOUT
while [ " $(( $( date +%s) - starttime)) " -lt " $TIMEOUT " -a $rc -ne 0 ] ; do
echo " Attempting to Query org ${ org } /peer ${ peer } ... $(( $( date +%s) - starttime)) secs "
peer chaincode query \
2017-12-06 16:24:33 +08:00
-C " ${ channel } " \
-n " ${ name } " \
2019-04-22 14:19:18 +08:00
--peerAddresses ${ peer_url } \
--tlsRootCertFiles ${ peer_org_tlsca } \
2017-12-06 16:24:33 +08:00
-c " ${ args } " \
>& log.txt
2019-04-21 11:18:26 +08:00
rc = $?
2019-04-22 14:19:18 +08:00
if [ -n " ${ expected_result } " ] ; then # need to check the result
2019-04-21 11:18:26 +08:00
test $? -eq 0 && VALUE = $( cat log.txt | awk 'END {print $NF}' )
if [ " $VALUE " = " ${ expected_result } " ] ; then
let rc = 0
2019-04-22 14:19:18 +08:00
echo_b " $VALUE == ${ expected_result } , passed "
2019-04-21 11:18:26 +08:00
else
let rc = 1
echo_b " $VALUE != ${ expected_result } , will retry "
fi
fi
if [ $rc -ne 0 ] ; then
cat log.txt
sleep 2
fi
done
# rc==0, or timeout
if [ $rc -eq 0 ] ; then
2019-04-22 14:19:18 +08:00
echo " === Query is done: org $org /peer $peer in channel ${ channel } === "
2019-04-21 11:18:26 +08:00
else
2019-04-22 14:19:18 +08:00
echo_r " === Query failed: org $org /peer $peer , run `make stop clean` to clean === "
2017-12-06 16:50:55 +08:00
exit 1
2019-04-21 11:18:26 +08:00
fi
2017-10-05 23:21:54 +08:00
}
2018-09-29 17:44:36 +08:00
# List Installed chaincode on specified peer node, and instantiated chaincodes at specific channel
# chaincodeList org1 peer0 businesschannel
chaincodeList ( ) {
local org = $1
local peer = $2
local channel = $3
[ -z $org ] && [ -z $peer ] && [ -z $channel ] && echo_r "input param invalid" && exit -1
echo " === ChaincodeList on org ${ org } /peer ${ peer } === "
setEnvs $org $peer
echo_b " Get installed chaincodes at peer $peer .org $org "
peer chaincode list \
--installed > log.txt &
# \
#--peerAddresses "peer${peer}.org${org}.example.com" --tls false
rc = $?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc " List installed chaincodes on remote org ${ org } /peer $peer has Failed "
echo_b " Get instantiated chaincodes at channel $org "
peer chaincode list \
--instantiated \
-C ${ channel } > log.txt &
rc = $?
[ $rc -ne 0 ] && cat log.txt
verifyResult $rc " List installed chaincodes on remote org ${ org } /peer $peer has Failed "
echo " === ChaincodeList is done at peer ${ peer } .org ${ org } === "
}
2017-10-05 23:21:54 +08:00
2017-10-28 14:41:13 +08:00
# Start chaincode with dev mode
2018-01-05 10:53:21 +08:00
# TODO: use variables instead of hard-coded value
2017-10-28 14:41:13 +08:00
chaincodeStartDev ( ) {
2017-12-06 16:24:33 +08:00
local peer = $1
local version = $2
2018-01-30 15:54:49 +08:00
[ -z $peer ] && [ -z $version ] && echo_r "input param invalid" && exit -1
2017-12-06 16:24:33 +08:00
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 \
2018-01-05 10:53:21 +08:00
CORE_CHAINCODE_ID_NAME = ${ CC_02_NAME } :${ version } \
2017-10-28 14:41:13 +08:00
nohup ./scripts/chaincode_example02 > chaincode_dev.log &
2018-01-30 15:54:49 +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-10-28 14:41:13 +08:00
}
2019-05-02 17:30:13 +08:00
# chaincodeUpgrade channel org peer orderer_url name version args
2017-10-05 23:21:54 +08:00
chaincodeUpgrade ( ) {
2019-05-02 17:30:13 +08:00
if [ " $# " -gt 9 -a " $# " -lt 7 ] ; then
2018-08-27 10:52:46 +08:00
echo_r "Wrong param number for chaincode instantaite"
exit -1
fi
2017-12-06 16:24:33 +08:00
local channel = $1
local org = $2
local peer = $3
2019-05-02 17:30:13 +08:00
local orderer_url = $4
local name = $5
local version = $6
local args = $7
2018-08-27 10:52:46 +08:00
local collection_config = "" # collection config file path for sideDB
local policy = "OR ('Org1MSP.member','Org2MSP.member')" # endorsement policy
2018-08-22 17:27:45 +08:00
echo " === chaincodeUpgrade to orderer by id of org ${ org } /peer $peer === "
2018-08-27 10:52:46 +08:00
echo " name= ${ name } , version= ${ version } , args= ${ args } , collection_config= ${ collection_config } , policy= ${ policy } "
2017-12-06 16:24:33 +08:00
setEnvs $org $peer
2017-10-05 23:21:54 +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-06 16:24:33 +08:00
peer chaincode upgrade \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2017-12-06 16:24:33 +08:00
-C ${ channel } \
-n ${ name } \
-v ${ version } \
-c ${ args } \
2018-08-27 10:52:46 +08:00
-P " ${ policy } " \
--collections-config " ${ collection_config } " \
2017-12-06 16:24:33 +08:00
>& log.txt
2017-10-05 23:21:54 +08:00
else
2017-12-06 16:24:33 +08:00
peer chaincode upgrade \
2019-05-02 17:30:13 +08:00
-o ${ orderer_url } \
2017-12-06 16:24:33 +08:00
-C ${ channel } \
-n ${ name } \
-v ${ version } \
-c ${ args } \
2018-08-27 10:52:46 +08:00
-P " ${ policy } " \
--collections-config " ${ collection_config } " \
2018-02-08 21:23:37 +08:00
--tls \
2019-05-02 17:30:13 +08:00
--cafile ${ ORDERER0_TLS_CA } \
2017-12-06 16:24:33 +08:00
>& log.txt
2017-10-05 23:21:54 +08:00
fi
2018-01-30 15:54:49 +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-10-05 23:21:54 +08:00
}
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
2018-01-30 15:54:49 +08:00
echo " Encode $input --> $output using type $msgType "
2018-10-12 11:33:24 +08:00
docker exec -it ${ CTL_CONTAINER } configtxlator proto_encode \
--type= ${ msgType } \
--input= ${ input } \
--output= ${ output }
#curl -sX POST \
# --data-binary @${input} \
# ${CTL_ENCODE_URL}/${msgType} \
# >${output}
2017-12-06 16:24:33 +08:00
}
# configtxlator decode pb to json
# Usage: configtxlatorEncode msgType input output
configtxlatorDecode( ) {
local msgType = $1
local input = $2
local output = $3
2018-01-30 15:54:49 +08:00
echo " Config Decode $input --> $output using type $msgType "
2017-12-29 21:52:46 +08:00
if [ ! -f $input ] ; then
echo_r "input file not found"
exit 1
fi
2018-10-12 11:33:24 +08:00
docker exec -it ${ CTL_CONTAINER } configtxlator proto_decode \
--type= ${ msgType } \
--input= ${ input } \
--output= ${ output }
#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
2017-12-29 21:52:46 +08:00
local output = $4
2017-12-06 16:24:33 +08:00
2018-01-30 15:54:49 +08:00
echo " Config Compare $origin vs $updated > ${ output } in channel $channel "
2017-12-29 21:52:46 +08:00
if [ ! -f $origin ] || [ ! -f $updated ] ; then
echo_r "input file not found"
exit 1
fi
2018-10-12 11:33:24 +08:00
docker exec -it ${ CTL_CONTAINER } configtxlator compute_update \
--original= ${ origin } \
--updated= ${ updated } \
--channel_id= ${ channel } \
--output= ${ output }
#curl -sX POST \
# -F channel="${channel}" \
# -F "original=@${origin}" \
# -F "updated=@${updated}" \
# "${CTL_COMPARE_URL}" \
# > "${output}"
2017-12-06 16:24:33 +08:00
[ $? -eq 0 ] || echo_r "Failed to compute config update"
2017-12-15 12:05:12 +08:00
}
# Run cmd inside the config generator container
gen_con_exec( ) {
docker exec -it $GEN_CONTAINER " $@ "
2018-01-30 15:54:49 +08:00
}