設置一個peer X的環境變量:
#Y:peer X所屬的組織
CORE_PEER_LOCALMSPID="OrgYMSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgY.example.com/peers/peer0.orgY.example.com/tls/ca.crt
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orgY.example.com/users/Admin@orgY.example.com/msp
CORE_PEER_ADDRESS=peerX.orgY.example.com:7051
選擇任意一個peer,切入到peer的cli容器中。
命令:$docker exec -it examplecli bash
examplecli:參見docker-compose-peer.yaml中cli容器名稱。
執行後的結果是:
eg: root@b41e67d40583:/opt/gopath/src/github.com/hyperledger/fabric/peer/opt/gopath/src/github.com/hyperledger/fabric/peer:是docker-compose-peer.yaml中cli工作目錄。
在orderer上創建一個channel
先設置當前peer的環境變量
命令:$ peer channel create -o ordererY.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/ordererY.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
-o: orderer容器名,指定和哪個orderer連接
-c: 指定通道名,利用configtxgen生成Channel配置區塊文件channel.tx時指定的通道名
-f: 指定通道配置交易文件(channel.tx)
--tls:是否需要TLS驗證?在docker-compose-peer.yaml有配置。
--cafile:指定了orderer的根證書路徑,用於驗證TLS握手。是-o指定orderer在當前cli容器中的根證書路徑。
eg: /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto是cli容器中保存證書的位置,在docker-compose-peer.yaml中cli的volumes有配置。
# ./crypto-config : /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
加入通道
循環下面的操作,將通道的所有peer加入通道 。
設置一個peer的環境變量
命令:$ peer channel join -b $mychannel.block
-b 生成文件
4. 更新通道上錨節點信息
循環下面的操作,更新所有peer錨節點 。
先設置一個peer錨節點的環境變量
命令:$ peer channel update -o ordererY.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/OrgXMSPanchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA
-o: orderer容器名,指定和哪個orderer連接,與創建channel的相同。
-c: 指定通道名,與創建channel的相同。
-f: 指定錨節點配置文件,利用configtxgen生成的錨節點配置文件。
--tls: 是否需要TLS驗證?在docker-compose-peer.yaml有配置。
--cafile: 指定了orderer的根證書路徑,用於驗證TLS握手。與創建channel的相同。
5. 安裝鏈碼
給指定peer安裝鏈碼:
設置指定peer的環境變量
命令:$ peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/lychees/chaincode/go/chaincode_example
-n: 鏈碼名稱
-v: 鏈碼版本
-p: 鏈碼位置,路徑不包含文件名
chaincode應該僅僅被安裝在chaincode所有者成員的peer節點上,
用於實現該chaincode對於網絡中其他成員在邏輯上是保密的。
沒有chaincode的成員,不能成爲chaincode transaction的endorser;
也就是說,它們不能執行chaincode。但是,他們仍然能夠驗證與提交transaction到Ledger上。
6. 實例化鏈碼
給指定peer實例化鏈碼:
設置指定peer的環境變量
命令:$peer chaincode instantiate -o ordererY.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
-o: orderer容器名,指定和哪個orderer連接,與創建channel的相同。
-C: 指定通道名,與創建channel的相同。
--tls: 是否需要TLS驗證?在docker-compose-peer.yaml有配置。
--cafile: 指定了orderer的根證書路徑,用於驗證TLS握手。與創建channel的相同。
-n: 鏈碼名稱
-c, --ctor string Constructor message for the chaincode in JSON format (default "{}")
-P:Endorsement policy背書策略
eg:
- AND(‘Org1.member’, ‘Org2.member’, ‘Org3.member’) ,請求3個principle的簽名。
- OR(‘Org1.member’, ‘Org2.member’) 請求兩個中的一個的簽名。
- OR(‘Org1.member’, AND(‘Org2.member’, ‘Org3.member’)) ,
請求一個來自Org1 的簽名,或者來自org2 的成員和來自Org3 的成員的簽名。
實例化transaction調用lifecycle System Chaincode
(LSCC) 用於創建及初始化channel上的chaincode。
chaincode-channel綁定過程:chaincode能夠被綁定到任意數量的channel,以及在每個channel上單獨的操作。
無論有chaincode安裝及實例化到多少個channel上,每個channel的狀態都是隔離的。
7. 鏈碼調用
在各個peer進行鏈碼調用。
命令:peer chaincode invoke -o ordererY.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'
-o: orderer容器名,指定和哪個orderer連接,與創建channel的相同。
-C: 指定通道名,與創建channel的相同。
--tls: 是否需要TLS驗證?在docker-compose-peer.yaml有配置。
--cafile: 指定了orderer的根證書路徑,用於驗證TLS握手。與創建channel的相同。
-n: 鏈碼名稱
-c, --ctor string Constructor message for the chaincode in JSON format (default "{}")
8. 鏈碼查詢
在各個peer進行鏈碼查詢。
命令:peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
-C: 指定通道名,與創建channel的相同。
-n: 鏈碼名稱
-c, --ctor string Constructor message for the chaincode in JSON format (default "{}")
例子:
#!/bin/bash
# Copyright London Stock Exchange Group All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
echo
echo " ____ _____ _ ____ _____ _____ ____ _____ "
echo "/ ___| |_ _| / \ | _ \ |_ _| | ____| |___ \ | ____|"
echo "\___ \ | | / _ \ | |_) | | | _____ | _| __) | | _| "
echo " ___) | | | / ___ \ | _ < | | |_____| | |___ / __/ | |___ "
echo "|____/ |_| /_/ \_\ |_| \_\ |_| |_____| |_____| |_____|"
echo
CHANNEL_NAME="$1"
: ${CHANNEL_NAME:="mychannel"}
: ${TIMEOUT:="60"}
COUNTER=1
MAX_RETRY=5
ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer2.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
echo "Channel name : "$CHANNEL_NAME
verifyResult () {
if [ $1 -ne 0 ] ; then
echo "!!!!!!!!!!!!!!! "$2" !!!!!!!!!!!!!!!!"
echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
echo
exit 1
fi
}
setGlobals () {
if [ $1 -eq 0 -o $1 -eq 1 ] ; then
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
if [ $1 -eq 0 ]; then
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
else
CORE_PEER_ADDRESS=peer1.org1.example.com:7051
fi
else
CORE_PEER_LOCALMSPID="Org2MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
if [ $1 -eq 2 ]; then
CORE_PEER_ADDRESS=peer0.org2.example.com:7051
else
CORE_PEER_ADDRESS=peer1.org2.example.com:7051
fi
fi
env |grep CORE
}
createChannel() {
setGlobals 0
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer channel create -o orderer2.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt
else
peer channel create -o orderer2.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt
fi
res=$?
cat log.txt
verifyResult $res "Channel creation failed"
echo "===================== Channel \"$CHANNEL_NAME\" is created successfully ===================== "
echo
}
updateAnchorPeers() {
PEER=$1
setGlobals $PEER
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer channel update -o orderer2.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
else
peer channel update -o orderer2.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt
fi
res=$?
cat log.txt
verifyResult $res "Anchor peer update failed"
echo "===================== Anchor peers for org \"$CORE_PEER_LOCALMSPID\" on \"$CHANNEL_NAME\" is updated successfully ===================== "
sleep 5
echo
}
## Sometimes Join takes time hence RETRY atleast for 5 times
joinWithRetry () {
peer channel join -b $CHANNEL_NAME.block >&log.txt
res=$?
cat log.txt
if [ $res -ne 0 -a $COUNTER -lt $MAX_RETRY ]; then
COUNTER=` expr $COUNTER + 1`
echo "PEER$1 failed to join the channel, Retry after 2 seconds"
sleep 2
joinWithRetry $1
else
COUNTER=1
fi
verifyResult $res "After $MAX_RETRY attempts, PEER$ch has failed to Join the Channel"
}
joinChannel () {
for ch in 0 1 2 3; do
setGlobals $ch
joinWithRetry $ch
echo "===================== PEER$ch joined on the channel \"$CHANNEL_NAME\" ===================== "
sleep 2
echo
done
}
installChaincode () {
PEER=$1
setGlobals $PEER
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 >&log.txt
res=$?
cat log.txt
verifyResult $res "Chaincode installation on remote peer PEER$PEER has Failed"
echo "===================== Chaincode is installed on remote peer PEER$PEER ===================== "
echo
}
instantiateChaincode () {
PEER=$1
setGlobals $PEER
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
# lets supply it directly as we know it using the "-o" option
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer chaincode instantiate -o orderer2.example.com:7050 -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')" >&log.txt
else
peer chaincode instantiate -o orderer2.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')" >&log.txt
fi
res=$?
cat log.txt
verifyResult $res "Chaincode instantiation on PEER$PEER on channel '$CHANNEL_NAME' failed"
echo "===================== Chaincode Instantiation on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
echo
}
chaincodeQuery () {
PEER=$1
echo "===================== Querying on PEER$PEER on channel '$CHANNEL_NAME'... ===================== "
setGlobals $PEER
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
sleep 3
echo "Attempting to Query PEER$PEER ...$(($(date +%s)-starttime)) secs"
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt
test $? -eq 0 && VALUE=$(cat log.txt | awk '/Query Result/ {print $NF}')
test "$VALUE" = "$2" && let rc=0
done
echo
cat log.txt
if test $rc -eq 0 ; then
echo "===================== Query on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
else
echo "!!!!!!!!!!!!!!! Query result on PEER$PEER is INVALID !!!!!!!!!!!!!!!!"
echo "================== ERROR !!! FAILED to execute End-2-End Scenario =================="
echo
exit 1
fi
}
chaincodeInvoke () {
PEER=$1
setGlobals $PEER
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
# lets supply it directly as we know it using the "-o" option
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer chaincode invoke -o orderer2.example.com:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
else
peer chaincode invoke -o orderer2.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
fi
res=$?
cat log.txt
verifyResult $res "Invoke execution on PEER$PEER failed "
echo "===================== Invoke transaction on PEER$PEER on channel '$CHANNEL_NAME' is successful ===================== "
echo
}
## 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 "Updating anchor peers for org1..."
updateAnchorPeers 0
echo "Updating anchor peers for org2..."
updateAnchorPeers 2
## Install chaincode on Peer0/Org1 and Peer2/Org2
echo "Installing chaincode on org1/peer0..."
installChaincode 0
echo "Install chaincode on org2/peer2..."
installChaincode 2
#Instantiate chaincode on Peer2/Org2
echo "Instantiating chaincode on org2/peer2..."
instantiateChaincode 2
#Query on chaincode on Peer0/Org1
echo "Querying chaincode on org1/peer0..."
chaincodeQuery 0 100
#Invoke on chaincode on Peer0/Org1
echo "Sending invoke transaction on org1/peer0..."
chaincodeInvoke 0
## Install chaincode on Peer3/Org2
echo "Installing chaincode on org2/peer3..."
installChaincode 3
#Query on chaincode on Peer3/Org2, check if the result is 90
echo "Querying chaincode on org2/peer3..."
chaincodeQuery 3 90
echo
echo "===================== All GOOD, End-2-End execution completed ===================== "
echo
echo
echo " _____ _ _ ____ _____ ____ _____ "
echo "| ____| | \ | | | _ \ | ____| |___ \ | ____|"
echo "| _| | \| | | | | | _____ | _| __) | | _| "
echo "| |___ | |\ | | |_| | |_____| | |___ / __/ | |___ "
echo "|_____| |_| \_| |____/ |_____| |_____| |_____|"
echo
exit 0