Hyperledegr Fabric BYFN 4: byfn.sh up 操作分析 docker exec 容器执行

Hyperledegr Fabric BYFN 4: byfn.sh up 操作分析 docker exec

script.sh

script.sh 精简代码逻辑

相关函数涉及文件: scripts/utils.sh

流程:

  1. 创建通道

    • createChannel

      setGlobals 0 1 # 设置peer和org参数
      peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt
      
  2. 将所有peer结点加入到通道

    • joinChannel

      for org in 1 2; do
        for peer in 0 1; do
          joinChannelWithRetry $peer $org
        done
      done
      
    • joinChannelWithRetry

      ## Sometimes Join takes time hence RETRY at least 5 times
      joinChannelWithRetry() {
        PEER=$1
        ORG=$2
        setGlobals $PEER $ORG
        peer channel join -b $CHANNEL_NAME.block >&log.txt
      }
      
  3. 在通道中为每个组织设置锚结点

    • updateAnchorPeers 0 1
      updateAnchorPeers 0 2

      updateAnchorPeers() {
        PEER=$1
        ORG=$2
        setGlobals $PEER $ORG
      
        peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
      }
      
  4. peer0.org1和peer0.org2上安装链码

    • installChaincode 0 1
      installChaincode 0 2

      installChaincode() {
        PEER=$1
        ORG=$2
        setGlobals $PEER $ORG
      
        peer chaincode install -n mycc -v ${VERSION} -l ${LANGUAGE} -p ${CC_SRC_PATH} >&log.txt
      }
      
  5. peer0.org2上实例化代码

    • instantiateChaincode 0 2

      instantiateChaincode() {
        PEER=$1
        ORG=$2
        setGlobals $PEER $ORG
        VERSION=${3:-1.0}
      
        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
      }
      
  6. peer0.org1上查询链码

    • chaincodeQuery 0 1 100
    chaincodeQuery() {
      PEER=$1
      ORG=$2
      setGlobals $PEER $ORG
      EXPECTED_RESULT=$3
    
      peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' >&log.txt
      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
      if test $rc -eq 0; then
        echo "===================== Query successful on peer${PEER}.org${ORG} on channel '$CHANNEL_NAME' ===================== "
      fi
    }
    
  7. peer0.org1peer0.org2上激活链码

    • chaincodeInvoke 0 1 0 2

      chaincodeInvoke() {
        parsePeerConnectionParameters $@ # 检查peers和关联组织数量是否偶数并设置$PEER_CONN_PARMS和$PEERS
      
        # 当`peer chaincode` 命令可以从peer中获得排序结点的地址,可以直接使用`-o` 选项
        peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc $PEER_CONN_PARMS -c '{"Args":["invoke","a","b","10"]}' >&log.txt
      }
      
  8. peer1.org2上实例化代码

    • instantiateChaincode 1 2
  9. peer1.org2上查询链码,检查返回结果是否为90

    • chaincodeQuery 1 2 90

你可能感兴趣的:(hyperledger,fabric)