目录
1. setAnchorPeer.sh脚本
以下是setAnchorPeer.sh脚本,加了一些中文注释
#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# import utils
. scripts/envVar.sh
. scripts/configUpdate.sh
# NOTE: this must be run in a CLI container since it requires jq and configtxlator
# 函数 - 创建锚节点
createAnchorPeerUpdate() {
infoln "Fetching channel config for channel $CHANNEL_NAME"
# 获取节点
# fetchChannelConfig 1 mychannel Org1MSPconfig.json
fetchChannelConfig $ORG $CHANNEL_NAME ${CORE_PEER_LOCALMSPID}config.json
infoln "Generating anchor peer update transaction for Org${ORG} on channel $CHANNEL_NAME"
if [ $ORG -eq 1 ]; then
HOST="peer0.org1.example.com"
PORT=7051
elif [ $ORG -eq 2 ]; then
HOST="peer0.org2.example.com"
PORT=9051
elif [ $ORG -eq 3 ]; then
HOST="peer0.org3.example.com"
PORT=11051
else
errorln "Org${ORG} unknown"
fi
set -x
# Modify the configuration to append the anchor peer
# 将锚节点添加至配置文件中
jq '.channel_group.groups.Application.groups.'${CORE_PEER_LOCALMSPID}'.values += {"AnchorPeers":{"mod_policy": "Admins","value":{"anchor_peers": [{"host": "'$HOST'","port": '$PORT'}]},"version": "0"}}' ${CORE_PEER_LOCALMSPID}config.json > ${CORE_PEER_LOCALMSPID}modified_config.json
{ set +x; } 2>/dev/null
# Compute a config update, based on the differences between
# {orgmsp}config.json and {orgmsp}modified_config.json, write
# it as a transaction to {orgmsp}anchors.tx
#
createConfigUpdate ${CHANNEL_NAME} ${CORE_PEER_LOCALMSPID}config.json ${CORE_PEER_LOCALMSPID}modified_config.json ${CORE_PEER_LOCALMSPID}anchors.tx
}
# 函数 - 添加Peer锚节点
updateAnchorPeer() {
peer channel update -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c $CHANNEL_NAME -f ${CORE_PEER_LOCALMSPID}anchors.tx --tls --cafile "$ORDERER_CA" >&log.txt
res=$?
cat log.txt
verifyResult $res "Anchor peer update failed"
successln "Anchor peer set for org '$CORE_PEER_LOCALMSPID' on channel '$CHANNEL_NAME'"
}
# 参数:组织编号
ORG=$1
# 参数:通道名称
CHANNEL_NAME=$2
# 设置环境变量:组织节点配置
setGlobalsCLI $ORG
createAnchorPeerUpdate
updateAnchorPeer
2. configUpdate.sh脚本
在上面函数createAnchorPeerUpdate()中调用了createConfigUpdate函数,此函数在脚本configUpdate.sh中,如下面所示:
#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# import utils
. scripts/envVar.sh
# fetchChannelConfig <org> <channel_id> <output_json>
# Writes the current channel config for a given channel to a JSON file
# NOTE: this must be run in a CLI container since it requires configtxlator
fetchChannelConfig() {
ORG=$1
CHANNEL=$2
OUTPUT=$3
setGlobals $ORG
infoln "Fetching the most recent configuration block for the channel"
set -x
# 获取通道配置
peer channel fetch config config_block.pb -o orderer.example.com:7050 --ordererTLSHostnameOverride orderer.example.com -c $CHANNEL --tls --cafile "$ORDERER_CA"
{ set +x; } 2>/dev/null
infoln "Decoding config block to JSON and isolating config to ${OUTPUT}"
set -x
# 将配置块protobuf格式转成json格式
configtxlator proto_decode --input config_block.pb --type common.Block | jq .data.data[0].payload.data.config >"${OUTPUT}"
{ set +x; } 2>/dev/null
}
# createConfigUpdate <channel_id> <original_config.json> <modified_config.json> <output.pb>
# Takes an original and modified config, and produces the config update tx
# which transitions between the two
# NOTE: this must be run in a CLI container since it requires configtxlator
createConfigUpdate() {
CHANNEL=$1
ORIGINAL=$2
MODIFIED=$3
OUTPUT=$4
set -x
# 瘵原始和修改的通道配置都转换回protobuf格式,并计算它们之间的差异
configtxlator proto_encode --input "${ORIGINAL}" --type common.Config >original_config.pb
configtxlator proto_encode --input "${MODIFIED}" --type common.Config >modified_config.pb
configtxlator compute_update --channel_id "${CHANNEL}" --original original_config.pb --updated modified_config.pb >config_update.pb
# 将配置更新包装在交易Envelope中,以创建通道配置更新交易
configtxlator proto_decode --input config_update.pb --type common.ConfigUpdate >config_update.json
echo '{"payload":{"header":{"channel_header":{"channel_id":"'$CHANNEL'", "type":2}},"data":{"config_update":'$(cat config_update.json)'}}}' | jq . >config_update_in_envelope.json
configtxlator proto_encode --input config_update_in_envelope.json --type common.Envelope >"${OUTPUT}"
{ set +x; } 2>/dev/null
}
# signConfigtxAsPeerOrg <org> <configtx.pb>
# Set the peerOrg admin of an org and sign the config update
signConfigtxAsPeerOrg() {
ORG=$1
CONFIGTXFILE=$2
setGlobals $ORG
set -x
peer channel signconfigtx -f "${CONFIGTXFILE}"
{ set +x; } 2>/dev/null
}