1. 程式人生 > >使用fabric-sdk-go 構建第一個區塊鏈程式 之【搭建fabric網路】

使用fabric-sdk-go 構建第一個區塊鏈程式 之【搭建fabric網路】

環境:mac os

準備:

Docker:

  1. 先註冊docker hub的一個賬號->docker hub
  2. 下載安裝docker下載地址
  3. 使用之前註冊好的賬號進行登入
  4. 切換映象源,推薦使用daocloude註冊地址(具體更換方法自行百度吧)

ps: docker for mac 不需要單獨在安裝docker-compose, centos 或者 ubuntu 需要單獨進行安裝

Go: 這個肯定不用說了~~~ 相信您已經安裝了

安裝指南

使用 Hyperledger fabric 1.0.5構建一個簡單的網路

1、建立專案目錄

mkdir -p $GOPATH/src/github.com/chainHero/heroes-service-network

2、這裡為了簡化部署網路部署,提供了二進位制檔案,方便安裝

倉庫地址: (Linux x64 ):

cd $GOPATH/src/github.com/chainHero/heroes-service-network && git clone  https://github.com/chainHero/heroes-service-network/branches/master/bin

ps:這個地址貌似有點問題所以提供了下面的下載連結,解壓後將整個bin目錄移動到 之前建好的目錄中

3、我們要知道我們的網路由那些東西組成所以需要一個配置檔案,配置檔案包含網路拓撲,並根據組織和元件生成證書和祕鑰

cd $GOPATH/src/github.com/chainHero/heroes-service-network && touch crypto-config.yaml &&  vim crypto-config.yaml

並將下面的內容複製到crypto-config.yaml 檔案中 (檔案內容申明瞭我們的網路有一個組織和兩個peer 節點)

# "OrdererOrgs" - Definition of organizations managing orderer nodes
OrdererOrgs:
  - Name: ChainHero
    Domain: hf.chainhero.io
    # Specs is an array of Spec entries.  Each Spec entry consists of two fields : Hostname and CommonName
    Specs:
      - Hostname: orderer
# "PeerOrgs" - Definition of organizations managing peer nodes
PeerOrgs:
  - Name: Org1ChainHero
    Domain: org1.hf.chainhero.io
    # Allows for the definition of 1 or more hosts that are created sequentially
    # from a template. By default, this looks like "peer%d" from 0 to Count-1.
    # You may override the number of nodes (Count), the starting index (Start)
    # or the template used to construct the name (Hostname).
    Template:
      Count: 2
    Users:
      # The number of user accounts _in addition_ to Admin
      Count: 2

4、使用Cryptogen 生成加密材料

cd $GOPATH/src/github.com/chainHero/heroes-service-network && mkdir -p crypto-config &&  ./bin/cryptogen generate --config=./crypto-config.yaml

這時候crypto-config下就多了兩個資料夾 在這裡插入圖片描述

5、為了初始化我們的網路我們還需要做一下操作:

  • 創世區塊
  • channel
  • peer 節點

繼續我們的操作

cd $GOPATH/src/github.com/chainHero/heroes-service-network && touch configtx.yaml && vim configtx.yaml

將一下內容複製到configtx.yaml 中

################################################################################
#
#   SECTION : Profile
#
#   - Different configuration profiles may be encoded here to be specified
#   as parameters to the configtxgen tool. The profiles which specify consortiums
#   are to be used for generating the orderer genesis block.  With the correct
#   consortium members defined in the orderer genesis block, channel creation
#   requests may be generated with only the org member names and a consortium name
#
################################################################################
Profiles:
    ChainHero:
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *ChainHero
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1ChainHero
        Consortium: SampleConsortium
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *ChainHero
                    - *Org1ChainHero

################################################################################
#
#   SECTION: Organizations
#
#   - This section defines the different organizational identities which will
#   be referenced later in the configuration.
#
################################################################################
Organizations:
    - &ChainHero
        Name: ChainHero
        ID: hf.chainhero.io
        AdminPrincipal: Role.ADMIN
        MSPDir: crypto-config/ordererOrganizations/hf.chainhero.io/msp

    - &Org1ChainHero
        Name: ChainHeroOrganization1
        ID: org1.hf.chainhero.io
        AdminPrincipal: Role.ADMIN
        MSPDir: crypto-config/peerOrganizations/org1.hf.chainhero.io/msp
        AnchorPeers:
            - Host: peer0.org1.hf.chainhero.io
              Port: 7051

################################################################################
#
#   SECTION: Orderer
#
#   - This section defines the values to encode into a config transaction or
#   genesis block for orderer related parameters.
#
################################################################################
Orderer: &OrdererDefaults
    OrdererType: solo
    Addresses:
        - orderer.hf.chainhero.io:7050
    BatchTimeout: 5s
    # Batch Size: Controls the number of messages batched into a block.
    BatchSize:
        # Max Message Count: The maximum number of messages to permit in a batch.
        MaxMessageCount: 10
        # Absolute Max Bytes: The absolute maximum number of bytes allowed for
        # the serialized messages in a batch. If the "kafka" OrdererType is
        # selected, set 'message.max.bytes' and 'replica.fetch.max.bytes' on the
        # Kafka brokers to a value that is larger than this one.
        AbsoluteMaxBytes: 98 MB
        # Preferred Max Bytes: The preferred maximum number of bytes allowed for
        # the serialized messages in a batch. A message larger than the
        # preferred max bytes will result in a batch larger than preferred max
        # bytes.
        PreferredMaxBytes: 512 KB
    # Max Channels is the maximum number of channels to allow on the ordering
    # network. When set to 0, this implies no maximum number of channels.
    MaxChannels: 0

    # Organizations is the list of orgs which are defined as participants on
    # the orderer side of the network.
    Organizations:

################################################################################
#
#   SECTION: Application
#
#   - This section defines the values to encode into a config transaction or
#   genesis block for application related parameters.
#
################################################################################
Application: &ApplicationDefaults
    Organizations:

配置檔案已經生成好了,我們需要用configtxgen 來生成我們的創世區塊

cd $GOPATH/src/github.com/chainHero/heroes-service-network && 
mkdir -p artifacts
cd $GOPATH/src/github.com/chainHero/heroes-service-network && 
FABRIC_CFG_PATH=$PWD ./bin/configtxgen -profile ChainHero -outputBlock ./artifacts/orderer.genesis.block

建立我們的channel

FABRIC_CFG_PATH=$PWD ./bin/configtxgen -profile ChainHero -outputCreateChannelTx ./artifacts/chainhero.channel.tx -channelID chainhero

建立一個peer 節點

FABRIC_CFG_PATH=$PWD ./bin/configtxgen -profile ChainHero -outputAnchorPeersUpdate ./artifacts/org1.chainhero.anchors.tx -channelID chainhero -asOrg ChainHeroOrganization1

區塊鏈網路我們已經搭建完成了,接下來是怎麼啟動他們~~~ 我們使用docker-compose來啟動建立一個docker-compose.yaml檔案

version: '2'

networks:
  default:

services:

  orderer.hf.chainhero.io:
    image: hyperledger/fabric-orderer:x86_64-1.0.5
    container_name: orderer.hf.chainhero.io
    environment:
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_LISTENPORT=7050
      - ORDERER_GENERAL_GENESISPROFILE=ChainHero
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
      - ORDERER_GENERAL_LOCALMSPID=hf.chainhero.io
      - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
      - ORDERER_GENERAL_TLS_ENABLED=true
      - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
      - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
      - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]

    working_dir: /opt/gopath/src/github.com/hyperledger/fabric
    command: orderer
    volumes:
      - ./artifacts/orderer.genesis.block:/var/hyperledger/orderer/orderer.genesis.block
      - ./crypto-config/ordererOrganizations/hf.chainhero.io/orderers/orderer.hf.chainhero.io/msp:/var/hyperledger/orderer/msp
      - ./crypto-config/ordererOrganizations/hf.chainhero.io/orderers/orderer.hf.chainhero.io/tls:/var/hyperledger/orderer/tls
    ports:
      - 7050:7050
    networks:
      default:
        aliases:
          - orderer.hf.chainhero.io

  ca.org1.hf.chainhero.io:
    image: hyperledger/fabric-ca:x86_64-1.0.5
    container_name: ca.org1.hf.chainhero.io
    environment:
      - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
      - FABRIC_CA_SERVER_CA_NAME=ca.org1.hf.chainhero.io
      - FABRIC_CA_SERVER_CA_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.hf.chainhero.io-cert.pem
      - FABRIC_CA_SERVER_CA_KEYFILE=/etc/hyperledger/fabric-ca-server-config/--HERE--
      - FABRIC_CA_SERVER_TLS_ENABLED=true
      - FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.hf.chainhero.io-cert.pem
      - FABRIC_CA_SERVER_TLS_KEYFILE=/etc/hyperledger/fabric-ca-server-config/--HERE--
    ports:
      - 7054:7054
    command: sh -c 'fabric-ca-server start -b admin:adminpw -d'
    volumes:
      - ./crypto-config/peerOrganizations/org1.hf.chainhero.io/ca/:/etc/hyperledger/fabric-ca-server-config
    networks:
      default:
        aliases:
          - ca.org1.hf.chainhero.io

  peer0.org1.hf.chainhero.io:
    image: hyperledger/fabric-peer:x86_64-1.0.5
    container_name: peer0.org1.hf.chainhero.io
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_VM_DOCKER_ATTACHSTDOUT=true
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_NETWORKID=chainhero
      - CORE_PEER_PROFILE_ENABLED=true
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/var/hyperledger/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/var/hyperledger/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/var/hyperledger/tls/ca.crt
      - CORE_PEER_ID=peer0.org1.hf.chainhero.io
      - CORE_PEER_ADDRESSAUTODETECT=true
      - CORE_PEER_ADDRESS=peer0.org1.hf.chainhero.io:7051
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.hf.chainhero.io:7051
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
      - CORE_PEER_LOCALMSPID=org1.hf.chainhero.io
      - CORE_PEER_MSPCONFIGPATH=/var/hyperledger/msp
      - CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer0.org1.hf.chainhero.io
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    volumes:
      - /var/run/:/host/var/run/
      - ./crypto-config/peerOrganizations/org1.hf.chainhero.io/peers/peer0.org1.hf.chainhero.io/msp:/var/hyperledger/msp
      - ./crypto-config/peerOrganizations/org1.hf.chainhero.io/peers/peer0.org1.hf.chainhero.io/tls:/var/hyperledger/tls
    ports:
      - 7051:7051
      - 7053:7053
    depends_on:
      - orderer.hf.chainhero.io
    links:
      - orderer.hf.chainhero.io
    networks:
      default:
        aliases:
          - peer0.org1.hf.chainhero.io

  peer1.org1.hf.chainhero.io:
    image: hyperledger/fabric-peer:x86_64-1.0.5
    container_name: peer1.org1.hf.chainhero.io
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_VM_DOCKER_ATTACHSTDOUT=true
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_NETWORKID=chainhero
      - CORE_PEER_PROFILE_ENABLED=true
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/var/hyperledger/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/var/hyperledger/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/var/hyperledger/tls/ca.crt
      - CORE_PEER_ID=peer1.org1.hf.chainhero.io
      - CORE_PEER_ADDRESSAUTODETECT=true
      - CORE_PEER_ADDRESS=peer1.org1.hf.chainhero.io:7051
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.hf.chainhero.io:7051
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_GOSSIP_SKIPHANDSHAKE=true
      - CORE_PEER_LOCALMSPID=org1.hf.chainhero.io
      - CORE_PEER_MSPCONFIGPATH=/var/hyperledger/msp
      - CORE_PEER_TLS_SERVERHOSTOVERRIDE=peer1.org1.hf.chainhero.io
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: peer node start
    volumes:
      - /var/run/:/host/var/run/
      - ./crypto-config/peerOrganizations/org1.hf.chainhero.io/peers/peer1.org1.hf.chainhero.io/msp:/var/hyperledger/msp
      - ./crypto-config/peerOrganizations/org1.hf.chainhero.io/peers/peer1.org1.hf.chainhero.io/tls:/var/hyperledger/tls
    ports:
      - 8051:7051
      - 8053:7053
    depends_on:
      - orderer.hf.chainhero.io
    links:
      - orderer.hf.chainhero.io
    networks:
      default:
        aliases:
          - peer1.org1.hf.chainhero.io

這需要改動一點東西: 將內容中的 --HERE--替換為heroes-service-network/crypto-config/peerOrganizations/org1.hf.chainhero.io/ca/xxxx_sk 這裡的xxxx_sk圖中的檔名 在這裡插入圖片描述

啟動網路:

cd $GOPATH/src/github.com/chainHero/heroes-service-network && docker-compose up -d

檢視容器是否都啟動了

在這裡插入圖片描述

第一部分已經完成