Skip to content

Commit

Permalink
add 更新链码和查询链码脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
Trekkiii committed Aug 3, 2018
1 parent 9a77953 commit 2fc2abb
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 11 deletions.
14 changes: 8 additions & 6 deletions fabric-ca/scripts/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1199,20 +1199,22 @@ function joinChannelWithRetry {
# 切换到peer组织的管理员身份,然后安装链码
function installChaincode {

CC_VERSION=$1
IS_VALID=$2
CC_NAME=$1 # 链码名称
CC_VERSION=$2 # 链码版本
CC_PATH=$3 # 链码路径
IS_VALID=$4
: ${IS_VALID:="false"}

switchToAdminIdentity
log "Installing chaincode on $PEER_HOST ..."
peer chaincode install -n mycc -v $CC_VERSION -p github.com/hyperledger/fabric-web/chaincode/go/chaincode_example02 2>&1 | tee log.txt
peer chaincode install -n $CC_NAME -v $CC_VERSION -p $CC_PATH 2>&1 | tee log.txt
res=$?
if [ $res -ne 0 ]; then
VALUE=$(cat log.txt | awk '/chaincode error/ {print $(NF-1)$NF}')
if [ $? -eq 0 -a "$VALUE" == "mycc.2.0exists" ]; then
log "chaincode 'mycc.2.0' is already install"
if [ $? -eq 0 -a "$VALUE" == "${CC_NAME}.${CC_VERSION}exists" ]; then
log "chaincode '${CC_NAME}.${CC_VERSION}' is already install"
else
fatal "install of chaincode 'mycc.2.0' failed"
fatal "install of chaincode '${CC_NAME}.${CC_VERSION}' failed"
fi
fi
}
Expand Down
9 changes: 5 additions & 4 deletions fabric-ca/scripts/eyfn/step2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ echo "===================== $PEER_NAME joined the channel \"$CHANNEL_NAME\" ====

initPeerVars ${NEW_ORG} ${PEER_NUM}
echo "Installing chaincode 2.0 on ${PEER_HOST}..."
installChaincode 2.0
installChaincode mycc 2.0 github.com/hyperledger/fabric-web/chaincode/go/chaincode_example02

IFS=', ' read -r -a PORGS <<< "$PEER_ORGS"
NUM_PORGS=${#PORGS[@]}
Expand All @@ -81,7 +81,8 @@ for ORG in $PEER_ORGS; do
initPeerVars $ORG $COUNT
# 切换到peer组织的管理员身份,然后安装链码
echo "Installing chaincode 2.0 on ${PEER_HOST}..."
installChaincode 2.0
installChaincode mycc 2.0 github.com/hyperledger/fabric-web/chaincode/go/chaincode_example02

fi
COUNT=$((COUNT+1))
done
Expand All @@ -100,8 +101,8 @@ set +x
set -e

cat log.txt
verifyResult $res "Chaincode upgrade on org${ORG} peer${PEER} has Failed"
echo "===================== Chaincode is upgraded on org${ORG} peer${PEER} ===================== "
verifyResult $res "Chaincode upgrade has Failed"
echo "===================== Chaincode is upgraded ===================== "
echo

# 在新加入Peer组织的第一个peer节点上查询链码
Expand Down
19 changes: 19 additions & 0 deletions fabric-ca/scripts/invoke_cc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Copyright 凡派 All Rights Reserved.
#
# Apache-2.0
#
# 查询链码

set -e

source $(dirname "$0")/env.sh

initOrdererVars org0 1
export ORDERER_PORT_ARGS="-o $ORDERER_HOST:7050 --tls --cafile $CA_CHAINFILE --clientauth"

initPeerVars org2 1
switchToUserIdentity

peer chaincode invoke -C mychannel -n mycc -c '{"Args":["select", "a"]}'
2 changes: 1 addition & 1 deletion fabric-ca/scripts/run-fabric.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function main {
while [[ "$COUNT" -le $NUM_PEERS ]]; do
initPeerVars $ORG $COUNT
# 切换到peer组织的管理员身份,然后安装链码
installChaincode 1.0
installChaincode mycc 1.0 github.com/hyperledger/fabric-web/chaincode/go/chaincode_example02
COUNT=$((COUNT+1))
done
done
Expand Down
70 changes: 70 additions & 0 deletions fabric-ca/scripts/upgrade_cc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

# Copyright 凡派 All Rights Reserved.
#
# Apache-2.0
#
# 更新链码

set -e

source $(dirname "$0")/env.sh

CC_UPGRADE=false # 是否执行更新upgrade链码操作

opts=0
while getopts "o:n:u" opt; do
case "$opt" in
o)
opts=$((opts+2))
ORDERER_ORG=$OPTARG
;;
n)
opts=$((opts+2))
ORDERER_NUM=$OPTARG
;;
u)
opts=$((opts+1))
CC_UPGRADE=true
esac
done

shift $opts

if [ $# -lt 5 ]; then
echo "Usage: upgrade_cc.sh <-o <ORDERER_ORG>> <-n <ORDERER_NUM>> <CC_NAME> <CC_VERSION> <CC_PATH> <ORG> <NUM>"
exit 1
fi

CC_NAME="$1" # 链码名称
CC_VERSION="$2" # 链码版本
CC_PATH="$3" # 链码路径
PEER_ORG="$4" # Peer组织
PEER_NUM="$5" # Peer节点索引

initOrdererVars $ORDERER_ORG $ORDERER_NUM
export ORDERER_PORT_ARGS="-o $ORDERER_HOST:7050 --tls --cafile $CA_CHAINFILE --clientauth"

initPeerVars $PEER_ORG $PEER_NUM
# 切换到peer组织的管理员身份,然后安装链码
echo "Installing chaincode $CC_VERSION on ${PEER_HOST}..."
installChaincode $CC_NAME $CC_VERSION $CC_PATH

if $CC_UPGRADE; then
# IFS=', ' read -r -a PORGS <<< "$PEER_ORGS"
# 使用该Peer组织的管理员身份来upgrade链码
initPeerVars $PEER_ORG $PEER_NUM
switchToAdminIdentity
set -x
set +e
makePolicy
peer chaincode upgrade -C $CHANNEL_NAME -n $CC_NAME -v $CC_VERSION -c '{"Args":["init","a","90","b","210"]}' -P "$POLICY" $ORDERER_CONN_ARGS >&log.txt
res=$?
set +x
set -e

cat log.txt
verifyResult $res "Chaincode upgrade has Failed"
echo "===================== Chaincode is upgraded ===================== "
echo
fi

0 comments on commit 2fc2abb

Please sign in to comment.