• HyperLedger/Fabric区块连网络-编译启动单节点


    本环境是在64为下面进行,32为编译会失败所以不分享

    单节点编译和全功能编译部署稍有不同,但是前期环境软件安装部署一致

    参照 https://www.cnblogs.com/adao21/p/13491894.html

     

    1.编译环境

    #首先进入fabric文件夹 
    cd ~/go/src/github.com/hyperledger/fabric/
    #编译源码
    make release
    #查看生成的文件
    cd release/linux-amd64/bin
    #如果文件夹内有如下文件的话说明编译成功
    #configtxgen  configtxlator  cryptogen  discover  idemixgen  orderer  peer
    
    #将生成的文件添加进环境变量
    vim ~/.profile
    #文件中最后添加以下内容
    export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin
    #更新一下
    source ~/.profile
    
    下载所依赖的镜像
    source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0
    32位。(只提供资源,没尝试过) source download-dockerimages.sh -c i686-1.0.0-beta -f i686-1.0.0-beta 完成上面的操作,就可以启动第一个fabric网络了。

    2.下载所依赖的镜像

    单节点只根据需要下载所依赖的景象:
    直接替换附件文件中的 download-dockerimages.sh 到目录
    /root/go/src/github.com/hyperledger/fabric/examples/e2e_cli
    然后执行命令
    source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0

    download-dockerimages.sh

    #!/bin/bash -eu
    #
    # Copyright IBM Corp. All Rights Reserved.
    #
    # SPDX-License-Identifier: Apache-2.0
    #
    
    
    ##################################################
    # This script pulls docker images from hyperledger
    # docker hub repository and Tag it as
    # hyperledger/fabric-<image> latest tag
    ##################################################
    
    #Set ARCH variable i.e ppc64le,s390x,x86_64,i386
    ARCH=`uname -m`
    
    dockerFabricPull() {
      local FABRIC_TAG=$1
      #for IMAGES in peer orderer couchdb ccenv javaenv kafka tools zookeeper; do
      for IMAGES in peer orderer tools ; do
          echo "==> FABRIC IMAGE: $IMAGES"
          echo
          docker pull hyperledger/fabric-$IMAGES:$FABRIC_TAG
          docker tag hyperledger/fabric-$IMAGES:$FABRIC_TAG hyperledger/fabric-$IMAGES
      done
    }
    
    dockerCaPull() {
          local CA_TAG=$1
          echo "==> FABRIC CA IMAGE"
          echo
          docker pull hyperledger/fabric-ca:$CA_TAG
          docker tag hyperledger/fabric-ca:$CA_TAG hyperledger/fabric-ca
    }
    usage() {
          echo "Description "
          echo
          echo "Pulls docker images from hyperledger dockerhub repository"
          echo "tag as hyperledger/fabric-<image>:latest"
          echo
          echo "USAGE: "
          echo
          echo "./download-dockerimages.sh [-c <fabric-ca tag>] [-f <fabric tag>]"
          echo "      -c fabric-ca docker image tag"
          echo "      -f fabric docker image tag"
          echo
          echo
          echo "EXAMPLE:"
          echo "./download-dockerimages.sh -c x86_64-1.0.0-beta -f x86_64-1.0.0-beta"
          echo
          echo "By default, pulls fabric-ca and fabric 1.0.0-beta docker images"
          echo "from hyperledger dockerhub"
          exit 0
    }
    
    while getopts "?hc:f:" opt; do
      case "$opt" in
         c) CA_TAG="$OPTARG"
            echo "Pull CA IMAGES"
            ;;
    
         f) FABRIC_TAG="$OPTARG"
            echo "Pull FABRIC TAG"
            ;;
         ?|h) usage
            echo "Print Usage"
            ;;
      esac
    done
    
    : ${CA_TAG:="$ARCH-1.0.0-beta"}
    : ${FABRIC_TAG:="$ARCH-1.0.0-beta"}
    
    echo "===> Pulling fabric Images"
    dockerFabricPull ${FABRIC_TAG}
    
    #echo "===> Pulling fabric ca Image"
    #dockerCaPull ${CA_TAG}
    #echo
    echo "===> List out hyperledger docker images"
    docker images | grep hyperledger*

    3.修改替换所需要的配置文件

    进入目录:/root/go/src/github.com/hyperledger/fabric/examples/e2e_cli

    分别替换如下文件:

    docker-compose-cli.yaml

    docker-compose-couch.yaml

    scripts/ script.sh

    docker-compose-cli.yaml

    # Copyright IBM Corp. All Rights Reserved.
    #
    # SPDX-License-Identifier: Apache-2.0
    #
    
    version: '2'
    
    services:
    
      orderer.example.com:
        extends:
          file:   base/docker-compose-base.yaml
          service: orderer.example.com
        container_name: orderer.example.com
    
      peer0.org1.example.com:
        container_name: peer0.org1.example.com
        extends:
          file:  base/docker-compose-base.yaml
          service: peer0.org1.example.com
    
    #  peer1.org1.example.com:
    #    container_name: peer1.org1.example.com
    #    extends:
    #      file:  base/docker-compose-base.yaml
    #      service: peer1.org1.example.com
    
    #  peer0.org2.example.com:
    #    container_name: peer0.org2.example.com
    #    extends:
    #      file:  base/docker-compose-base.yaml
    #      service: peer0.org2.example.com
    
    #  peer1.org2.example.com:
    #    container_name: peer1.org2.example.com
    #    extends:
    #      file:  base/docker-compose-base.yaml
    #      service: peer1.org2.example.com
    
      cli:
        container_name: cli
        image: hyperledger/fabric-tools
        tty: true
        environment:
          - GOPATH=/opt/gopath
          - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
          - CORE_LOGGING_LEVEL=DEBUG
          - CORE_PEER_ID=cli
          - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
          - CORE_PEER_LOCALMSPID=Org1MSP
          - CORE_PEER_TLS_ENABLED=true
          - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
          - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
          - 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
        working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
        command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT'
        volumes:
            - /var/run/:/host/var/run/
            - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
            - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
            - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
            - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
        depends_on:
          - orderer.example.com
          - peer0.org1.example.com
    #      - peer1.org1.example.com
    #      - peer0.org2.example.com
    #      - peer1.org2.example.com

    docker-compose-couch.yaml

    # Copyright IBM Corp. All Rights Reserved.
    #
    # SPDX-License-Identifier: Apache-2.0
    #
    
    version: '2'
    
    services:
      couchdb0:
        container_name: couchdb0
        image: hyperledger/fabric-couchdb
        # Comment/Uncomment the port mapping if you want to hide/expose the CouchDB service,
        # for example map it to utilize Fauxton User Interface in dev environments.
        ports:
          - "5984:5984"
    
      peer0.org1.example.com:
        environment:
          - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
          - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0:5984
        depends_on:
          - couchdb0
    
      couchdb1:
        container_name: couchdb1
        image: hyperledger/fabric-couchdb
        # Comment/Uncomment the port mapping if you want to hide/expose the CouchDB service,
        # for example map it to utilize Fauxton User Interface in dev environments.
        ports:
          - "6984:5984"
    
      peer1.org1.example.com:
        environment:
          - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
          - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb1:5984
        depends_on:
          - couchdb1
    
    #    couchdb2:
    #    container_name: couchdb2
    #    image: hyperledger/fabric-couchdb
        # Comment/Uncomment the port mapping if you want to hide/expose the CouchDB service,
        # for example map it to utilize Fauxton User Interface in dev environments.
    #    ports:
    #      - "7984:5984"
    
    #  peer0.org2.example.com:
    #    environment:
    #      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
    #      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb2:5984
    #    depends_on:
    #      - couchdb2
    
    #  couchdb3:
    #    container_name: couchdb3
    #    image: hyperledger/fabric-couchdb
        # Comment/Uncomment the port mapping if you want to hide/expose the CouchDB service,
        # for example map it to utilize Fauxton User Interface in dev environments.
    #    ports:
    #      - "8984:5984"
    
    #  peer1.org2.example.com:
    #    environment:
    #      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
    #      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb3:5984
    #    depends_on:
    #      - couchdb3

    scripts/ script.sh

    #!/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/orderer.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 orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx >&log.txt
        else
            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
        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 orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx >&log.txt
        else
            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
        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 ; 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 orderer.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 orderer.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 orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
        else
            peer chaincode invoke -o orderer.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
    instantiateChaincode 0
    
    #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
    #chaincodeQuery 0 100
    
    ## 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

    4.启动网络

    进入到e2e_cli目录下
    
    cd  /root/go/src/github.com/hyperledger/fabric/examples/e2e_cli
    
    ./network_setup.sh up
    启动前防止docker已启动的容器冲突,或者启动异常可以进行清除
    
    docker stop $(docker ps -a -q)
    
    docker rm $(docker ps -a -q)

    启动cli容器进性验证

    docker exec -it cli bash
    提示没报错并直接进入到peer 结尾的目录即表示成功
    使用命令在Peer上查询a,
    # peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

    使用命令进行转账操作:进行a向b转10交易

    peer chaincode invoke -o orderer.example.com:7050  --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -c'{"Args":["invoke","a","b","10"]}'
    重新使用命令在Peer上查询a,b
     peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
     peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'

    可以查询和交易,说明说明网络已经正常启动了。

    分布式部署请参考另一片

    https://www.cnblogs.com/adao21/p/13565120.html

  • 相关阅读:
    css注入获取网页中的数据
    跨路径读取cookie
    python 网络爬虫介绍
    ssh无法登录,提示Connection closing...Socket close.
    Tengine 添加第三方监控模块nginx-module-vts
    使用nginx很卡之strace命令
    MySQL清理慢查询日志slow_log的方法
    Python之json模块
    zabbix3调用接口发送短信告警
    RabbitMQ 安装 rabbitmq_delayed_message_exchange插件
  • 原文地址:https://www.cnblogs.com/adao21/p/13565406.html
Copyright © 2020-2023  润新知