推薦fabric命令查詢地址:看不懂可以直接查 fabric命令手冊,
我理解的也沒多好,新手上路,希望大家看到的可以指點一下,
文章目錄
- 一、script.sh
- 二、utils.sh方法
- 1. 公用的資訊
- 2. 檢測
- 3. 設定OrdererOrg.Admin全域
- 4. 設定全域
- 5. 更新錨節點
- 6. 加入通道
- 7. 安裝鏈碼
- 8. 實體化鏈碼
- 9. 升級鏈碼
- 10. 查詢鏈碼
- 11. 將給定通道的配置寫入json
- 12. 更新簽名
- 13. 更新配置資訊
- 14. peer鏈接的引數
- 15. 初始化鏈碼
一、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_LOCALMSPID}anchors.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_LOCALMSPID}anchors.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 [ $(($# % 2)) -ne 0 ]; then
exit 1
fi
PEER_CONN_PARMS=""
PEERS=""
while [ "$#" -gt 0 ]; do
setGlobals $1 $2
PEER="peer$1.org$2"
PEERS="$PEERS $PEER"
PEER_CONN_PARMS="$PEER_CONN_PARMS --peerAddresses $CORE_PEER_ADDRESS"
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "true" ]; then
TLSINFO=$(eval echo "--tlsRootCertFiles \$PEER$1_ORG$2_CA")
PEER_CONN_PARMS="$PEER_CONN_PARMS $TLSINFO"
fi
# shift by two to get the next pair of peer/org parameters
shift
shift
done
# remove leading space for output
PEERS="$(echo -e "$PEERS" | sed -e 's/^[[:space:]]*//')"
}
15. 初始化鏈碼
# chaincodeInvoke <peer> <org> ...
# Accepts as many peer/org pairs as desired and requests endorsement from each
chaincodeInvoke() {
echo "初始化鏈碼"
[parsePeerConnectionParameters](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#n872ZyZk83zGv5T7VH2THG)[ ](https://www.wolai.com/sRSSQXgqioxU9LW72BYuxU#n872ZyZk83zGv5T7VH2THG)$@
res=$?
verifyResult $res "Invoke transaction failed on channel '$CHANNEL_NAME' due to uneven number of peer and org parameters "
# 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
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
set -x
peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc $PEER_CONN_PARMS -c '{"Args":["invoke","a","b","10"]}' >&log.txt
res=$?
set +x
else
set -x
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc $PEER_CONN_PARMS -c '{"Args":["invoke","a","b","10"]}' >&log.txt
res=$?
set +x
fi
cat log.txt
verifyResult $res "Invoke execution on $PEERS failed "
echo "===================== Invoke transaction successful on $PEERS on channel '$CHANNEL_NAME' ===================== "
echo
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/400639.html
標籤:區塊鏈
