diff --git a/fabric-ca/scripts/env.sh b/fabric-ca/scripts/env.sh index c1b494d..99c7d57 100644 --- a/fabric-ca/scripts/env.sh +++ b/fabric-ca/scripts/env.sh @@ -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 } diff --git a/fabric-ca/scripts/eyfn/step2.sh b/fabric-ca/scripts/eyfn/step2.sh index 7b1807b..42d935e 100644 --- a/fabric-ca/scripts/eyfn/step2.sh +++ b/fabric-ca/scripts/eyfn/step2.sh @@ -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[@]} @@ -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 @@ -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节点上查询链码 diff --git a/fabric-ca/scripts/invoke_cc.sh b/fabric-ca/scripts/invoke_cc.sh new file mode 100644 index 0000000..0e2f7a9 --- /dev/null +++ b/fabric-ca/scripts/invoke_cc.sh @@ -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"]}' diff --git a/fabric-ca/scripts/run-fabric.sh b/fabric-ca/scripts/run-fabric.sh index e01a3c9..bab071a 100644 --- a/fabric-ca/scripts/run-fabric.sh +++ b/fabric-ca/scripts/run-fabric.sh @@ -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 diff --git a/fabric-ca/scripts/upgrade_cc.sh b/fabric-ca/scripts/upgrade_cc.sh new file mode 100644 index 0000000..6845f26 --- /dev/null +++ b/fabric-ca/scripts/upgrade_cc.sh @@ -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 > <-n > " + 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 \ No newline at end of file