[Fabric]-First-Network(script.sh文件详解)
Posted 长泽雅美你老婆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Fabric]-First-Network(script.sh文件详解)相关的知识,希望对你有一定的参考价值。
推荐fabric命令查询地址:看不懂可以直接查 fabric命令手册。
我理解的也没多好,新手上路,希望大家看到的可以指点一下。
文章目录
一、script.sh
#!/bin/bash
echo
echo " ____ _____ _ ____ _____ "
echo "/ ___| |_ _| / \\ | _ \\ |_ _|"
echo "\\___ \\ | | / _ \\ | |_) | | | "
echo " ___) | | | / ___ \\ | _ < | | "
echo "|____/ |_| /_/ \\_\\ |_| \\_\\ |_| "
echo
echo "构建第一个测试网络"
echo "获取6个输入参数 通道名称 延迟 语言 结束时间 详情 没有链码 "
CHANNEL_NAME="$1"
DELAY="$2"
LANGUAGE="$3"
TIMEOUT="$4"
VERBOSE="$5"
NO_CHAINCODE="$6"
#没有初始化值就进行赋值
: $CHANNEL_NAME:="mychannel"
: $DELAY:="3"
: $LANGUAGE:="golang"
: $TIMEOUT:="10"
: $VERBOSE:="false"
: $NO_CHAINCODE:="false"
# 大小写转换
LANGUAGE=`echo "$LANGUAGE" | tr [:upper:] [:lower:]`
counter=1
# 最大重试次数
max_retry=10
# 源码路径
CC_SRC_PATH="github.com/chaincode/chaincode_example02/go/"
echo $LANGUAGE
# 判断语言,找到对应路径
if [ "$LANGUAGE" = "node" ]; then
CC_SRC_PATH="/opt/gopath/src/github.com/chaincode/chaincode_example02/node/"
fi
if [ "$LANGUAGE" = "java" ]; then
CC_SRC_PATH="/opt/gopath/src/github.com/chaincode/chaincode_example02/java/"
fi
echo "通道的名字 : "$CHANNEL_NAME
# import utils
echo "引入utils.sh文件,这个不需要权限"
. scripts/utils.sh
echo "执行创建通道函数"
createChannel()
[setGlobals](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#9TrZv7UFE2vBofkf4Q4Yma) 0 1
echo "判断peer的tls是否禁用"
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
set -x
echo "执行peer channel create 创建一个通道。并将创世块写入文件中"
echo "-o 指定了orderer -c 指定channel-ID -f 指定配置文件 channel.tx 输出内容到 log.txt"
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt
echo "获取执行结果"
res=$?
set +x
else
echo "peer 的tls没有被禁用"
set -xll
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt
res=$?
set +x
fi
echo "查看log.txt"
cat log.txt
echo "$res 判断,打印通道为创建"
verifyResult $res "Channel creation failed"
echo "===================== Channel '$CHANNEL_NAME' created ===================== "
echo
echo "加入通道"
joinChannel ()
echo "循环1 2节点"
for org in 1 2; do
for peer in 0 1; do
echo "执行加入通道的方法"
[joinChannelWithRetry](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#3vS9NKiqjwFCq8g78YdSUY) $peer $org
echo "===================== peer$peer.org$org joined channel '$CHANNEL_NAME' ===================== "
sleep $DELAY
echo
done
done
## Create channel
echo "Creating channel..."
createChannel
## Join all the peers to the channel
echo "Having all peers join the channel..."
joinChannel
## Set the anchor peers for each org in the channel
echo "更新锚节点"
echo "Updating anchor peers for org1..."
[updateAnchorPeers](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#wVxCjozLP9tYwWQ3BnD4Lg) 0 1
echo "Updating anchor peers for org2..."
[updateAnchorPeers](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#wVxCjozLP9tYwWQ3BnD4Lg) 0 2
echo "判断NO_CHAINCODE $NO_CHAINCODE"
echo "调用各种utils中的函数"
if [ "$NO_CHAINCODE" != "true" ]; then
## Install chaincode on peer0.org1 and peer0.org2
echo "Installing chaincode on peer0.org1..."
[installChaincode ](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#f7r6VKem9FKaU9epjPfPUN)0 1
echo "Install chaincode on peer0.org2..."
[installChaincode ](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#f7r6VKem9FKaU9epjPfPUN)0 2
# Instantiate chaincode on peer0.org2
echo "Instantiating chaincode on peer0.org2..."
[instantiateChaincode ](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#3rgdd1ZqXLtocBzr2UcFgu)0 2
# Query chaincode on peer0.org1
echo "Querying chaincode on peer0.org1..."
[chaincodeQuery ](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#27GpmauTchhxtH2CEnMhd1)0 1 100
# Invoke chaincode on peer0.org1 and peer0.org2
echo "Sending invoke transaction on peer0.org1 peer0.org2..."
[chaincodeInvoke](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#6oDfi5W7zYrfYeCcJnXGwf)[ ](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#6oDfi5W7zYrfYeCcJnXGwf)0 1 0 2
## Install chaincode on peer1.org2
echo "Installing chaincode on peer1.org2..."
installChaincode 1 2
# Query on chaincode on peer1.org2, check if the result is 90
echo "Querying chaincode on peer1.org2..."
chaincodeQuery 1 2 90
fi
echo
echo "========= All GOOD, BYFN execution completed =========== "
echo
echo
echo " _____ _ _ ____ "
echo "| ____| | \\ | | | _ \\ "
echo "| _| | \\| | | | | | "
echo "| |___ | |\\ | | |_| | "
echo "|_____| |_| \\_| |____/ "
echo
二、utils.sh方法
1. 公用的信息
echo "先定义了orderer 和 3 个peer 的ca证书的位置"
ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
PEER0_ORG1_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
PEER0_ORG2_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
PEER0_ORG3_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt
2. 检测
# verify the result of the end-to-end test
verifyResult()
echo "verifyResult 创建e 2 e 使用"
if [ $1 -ne 0 ]; then
echo "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!"
echo "========= ERROR !!! FAILED to execute End-2-End Scenario ==========="
echo
exit 1
fi
3. 设置OrdererOrg.Admin全局
# Set OrdererOrg.Admin globals
setOrdererGlobals()
CORE_PEER_LOCALMSPID="OrdererMSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/users/Admin@example.com/msp
4. 设置全局
setGlobals()
PEER=$1
ORG=$2
if [ $ORG -eq 1 ]; then
# 定义peer 的msp 的位置 tls 啦
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
if [ $PEER -eq 0 ]; then
# 根据peer 0 1 定义 访问名 ip:port
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
else
CORE_PEER_ADDRESS=peer1.org1.example.com:8051
fi
elif [ $ORG -eq 2 ]; then
CORE_PEER_LOCALMSPID="Org2MSP"
CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG2_CA
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
if [ $PEER -eq 0 ]; then
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
else
CORE_PEER_ADDRESS=peer1.org2.example.com:10051
fi
elif [ $ORG -eq 3 ]; then
CORE_PEER_LOCALMSPID="Org3MSP"
CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG3_CA
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp
if [ $PEER -eq 0 ]; then
CORE_PEER_ADDRESS=peer0.org3.example.com:11051
else
CORE_PEER_ADDRESS=peer1.org3.example.com:12051
fi
else
echo "================== ERROR !!! ORG Unknown =================="
fi
if [ "$VERBOSE" == "true" ]; then
env | grep CORE
fi
5. 更新锚节点
updateAnchorPeers()
PEER=$1
ORG=$2
setGlobals $PEER $ORG
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
set -x
echo "更新锚节点"
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/$CORE_PEER_LOCALMSPIDanchors.tx >&log.txt
res=$?
set +x
else
set -x
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/$CORE_PEER_LOCALMSPIDanchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt
res=$?
set +x
fi
cat log.txt
echo "锚节点更新失败"
verifyResult $res "Anchor peer update failed"
echo "===================== Anchor peers updated for org '$CORE_PEER_LOCALMSPID' on channel '$CHANNEL_NAME' ===================== "
sleep $DELAY
echo
6. 加入通道
## Sometimes Join takes time hence RETRY at least 5 times
joinChannelWithRetry()
PEER=$1
ORG=$2
# 调用设置全局
[setGlobals](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#9TrZv7UFE2vBofkf4Q4Yma) $PEER $ORG
set -x
echo "指定节点加入一个通道中 加入创世块定义的通道中"
peer channel join -b $CHANNEL_NAME.block >&log.txt
res=$?
set +x
cat log.txt
if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then
COUNTER=$(expr $COUNTER + 1)
echo "peer$PEER.org$ORG failed to join the channel, Retry after $DELAY seconds"
sleep $DELAY
joinChannelWithRetry $PEER $ORG
else
COUNTER=1
fi
verifyResult $res "After $MAX_RETRY attempts, peer$PEER.org$ORG has failed to join channel '$CHANNEL_NAME' "
7. 安装链码
installChaincode()
PEER=$1
ORG=$2
[setGlobals](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#9TrZv7UFE2vBofkf4Q4Yma)[ ](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#9TrZv7UFE2vBofkf4Q4Yma)$PEER $ORG
VERSION=$3:-1.0
set -x
echo "执行peer chaincode install -n -v -l -p 再输出到log中"
peer chaincode install -n mycc -v $VERSION -l $LANGUAGE -p $CC_SRC_PATH >&log.txt
res=$?
set +x
cat log.txt
verifyResult $res "Chaincode installation on peer$PEER.org$ORG has failed"
echo "===================== Chaincode is installed on peer$PEER.org$ORG ===================== "
echo
8. 实例化链码
instantiateChaincode()
PEER=$1
ORG=$2
setGlobals $PEER $ORG
VERSION=$3:-1.0
# while 'peer chaincode' command can get the orderer endpoint from the peer
# (if join was successful), let's supply it directly as we know it using
# the "-o" option
echo "判断有没有开tls"
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
set -x
echo "执行实例化链码 a=100 b=200"
peer chaincode instantiate -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -l $LANGUAGE -v $VERSION -c '"Args":["init","a","100","b","200"]' -P "AND ('Org1MSP.peer','Org2MSP.peer')" >&log.txt
res=$?
set +x
else
set -x
peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -l $LANGUAGE -v 1.0 -c '"Args":["init","a","100","b","200"]' -P "AND ('Org1MSP.peer','Org2MSP.peer')" >&log.txt
res=$?
set +x
fi
cat log.txt
verifyResult $res "Chaincode instantiation on peer$PEER.org$ORG on channel '$CHANNEL_NAME' failed"
echo "===================== Chaincode is instantiated on peer$PEER.org$ORG on channel '$CHANNEL_NAME' ===================== "
echo
9. 升级链码
upgradeChaincode()
PEER=$1
ORG=$2
[setGlobals](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#9TrZv7UFE2vBofkf4Q4Yma)[ ](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#9TrZv7UFE2vBofkf4Q4Yma)$PEER $ORG
set -x
echo "升级指定的链码。交易提交后新的链码立即替代原来的链码 参数 a=90 b=210"
peer chaincode upgrade -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 2.0 -c '"Args":["init","a","90","b","210"]' -P "AND ('Org1MSP.peer','Org2MSP.peer','Org3MSP.peer')"
res=$?
set +x
cat log.txt
verifyResult $res "Chaincode upgrade on peer$PEER.org$ORG has failed"
echo "===================== Chaincode is upgraded on peer$PEER.org$ORG on channel '$CHANNEL_NAME' ===================== "
echo
10. 查询链码
chaincodeQuery()
PEER=$1
ORG=$2
setGlobals $PEER $ORG
EXPECTED_RESULT=$3
echo "===================== Querying on peer$PEER.org$ORG on channel '$CHANNEL_NAME'... ===================== "
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
echo "$(($(date +%s) - starttime))"
echo "$TIMEOUT"
echo $rc
echo "判断开始时间 结束时间 rc不小于 0"
echo "休息一段时间"
sleep $DELAY
echo "Attempting to Query peer$PEER.org$ORG ...$(($(date +%s) - starttime)) secs"
set -x
echo "查询a的钱"
peer chaincode query -C $CHANNEL_NAME -n mycc -c '"Args":["query","a"]' >&log.txt
res=$?
set +x
test $res -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ print $NF')
test "$VALUE" = "$EXPECTED_RESULT" && let rc=0
# removed the string "Query Result" from peer chaincode query command
# result. as a result, have to support both options until the change
# is merged.
test $rc -ne 0 && VALUE=$(cat log.txt | egrep '^[0-9]+$')
test "$VALUE" = "$EXPECTED_RESULT" && let rc=0
done
echo
cat log.txt
echo "判断结果"
if test $rc -eq 0; then
echo "===================== Query successful on peer$PEER.org$ORG on channel '$CHANNEL_NAME' ===================== "
else
echo "!!!!!!!!!!!!!!! Query result on peer$PEER.org$ORG is INVALID !!!!!!!!!!!!!!!!"
echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
echo
exit 1
fi
11. 将给定通道的配置写入json
# Writes the current channel config for a given channel to a JSON file
fetchChannelConfig()
CHANNEL=$1
OUTPUT=$2
setOrdererGlobals
echo "提取指区块写入文件"
echo "Fetching the most recent configuration block for the channel"
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
set -x
peer channel fetch config config_block.pb -o orderer.example.com:7050 -c $CHANNEL --cafile $ORDERER_CA
set +x
else
set -x
peer channel fetch config config_block.pb -o orderer.example.com:7050 -c $CHANNEL --tls --cafile $ORDERER_CA
set +x
fi
echo "将配置块解码为json 将一个protobuf消息转换为JSON格式。"
echo "Decoding config block to JSON and isolating config to $OUTPUT"
set -x
configtxlator proto_decode --input config_block.pb --type common.Block | jq .data.data[0].payload.data.config >"$OUTPUT"
set +x
12. 更新签名
# 为指定的configtx更新文件原地签名。需要-f标志。
# $TX 可以查看这个文件前=前后大小变化
signConfigtxAsPeerOrg()
PEERORG=$1
TX=$2
setGlobals 0 $PEERORG
set -x
peer channel signconfigtx -f "$TX"
set +x
13. 更新配置信息
# 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
createConfigUpdate()
echo "计算生成两个common.Config消息之间的配置更新。"
CHANNEL=$1
ORIGINAL=$2
MODIFIED=$3
OUTPUT=$4
set -x
echo "将一个JSON文档转换为protobuf格式数据。"
echo "input $ORIGINAL 转化为original_config.pb"
configtxlator proto_encode --input "$ORIGINAL" --type common.Config >original_config.pb
configtxlator proto_encode --input "$MODIFIED" --type common.Config >modified_config.pb
echo "json 转 pb"
configtxlator compute_update --channel_id "$CHANNEL" --original original_config.pb --updated modified_config.pb >config_update.pb
echo "pb 转 json "
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
14. peer链接的参数
# parsePeerConnectionParameters $@
# Helper function that takes the parameters from a chaincode operation
# (e.g. invoke, query, instantiate) and checks for an even number of
# peers and associated org, then sets $PEER_CONN_PARMS and $PEERS
parsePeerConnectionParameters()
echo "从链码汇总获取辅助参数"
echo "检查 peer org 参数 不均匀的数量"
# check for uneven number of peer and org parameters
if [ $(($# % [Fabric]-First-Network(script.sh文件详解)
[Fabric] First-NetWork(byfn.sh文件分析)
Hyperledger Fabric first-network的初始化启动流程(二)
[Fabric]-First-Network (crypto-config.yaml详解)