Fabric cli中创建channel和调用chaincode

设置一个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

 

  1. 选择任意一个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工作目录。

  2. 在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/

  3. 加入通道

         循环下面的操作,将通道的所有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/Admin@org1.example.com/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/Admin@org2.example.com/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
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值