基于Hyperledger Fabric1.4 自定义搭建自己的区块链(二)

四、generateChannelArtifacts函数

       使用configtxgen工具根据configtx.yaml文件来生成创世区块和通道相关信息,更新锚节点。

      1、生成启动 Orderer 需要的初始化区块,并支持检查区块内容;
      2、生成创建应用通道需要的配置交易,并支持检查交易内容;
      3、生成 2 个锚节点 Peer 的更新配置交易。
      configtxgen 调用的配置文件为 configtx.yaml ,该配置文件一般包括四个部分

       1、Profiles:一系列通道配置模板,包括 Orderer 系统通道模板和应用通道类型模板;
       2、Organization:一系列组织结构定义,被其他部分引用;
       3、Orderer:Orderer 系统通道相关配置,包括 Orderer 服务配置和参与 Ordering 服务的可用组织信息;
       4、 Application:应用通道相关配置,主要包括参与应用网络的可用组织信息。
             调用该函数最终在 channel-artifacts 目录下生成了四个文件,对应于上述的三个功能:

       channel.tx    genesis.block    OrgaMSPanchors.tx   OrgbMSPanchors.tx

       修改configtx.yaml文件的Profile模块

################################################################################
#   Profile
#
#   - Different configuration profiles may be encoded here to be specified
#   as parameters to the configtxgen tool
################################################################################
Profiles:

    TwoOrgsOrdererGenesis:
        <<: *ChannelDefaults
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
            Capabilities:
                <<: *OrdererCapabilities
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Orga
                    - *Orgb
            TestConsortium:
                Organizations:
                    - *Orga
                    
    TwoOrgsChannel:
        Consortium: SampleConsortium
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Orga
                - *Orgb
            Capabilities:
                <<: *ApplicationCapabilities
                
    TestChannel:
        Consortium: TestConsortium
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Orga
            Capabilities:
                <<: *ApplicationCapabilities
    
    SampleDevModeKafka:
        <<: *ChannelDefaults
        Capabilities:
            <<: *ChannelCapabilities
        Orderer:
            <<: *OrdererDefaults
            OrdererType: kafka
            Kafka:
                Brokers:
                - kafka.mikechain.com:9092

            Organizations:
            - *OrdererOrg
            Capabilities:
                <<: *OrdererCapabilities
        Application:
            <<: *ApplicationDefaults
            Organizations:
            - <<: *OrdererOrg
        Consortiums:
            SampleConsortium:
                Organizations:
                - *Orga
                - *Orgb

修改configtx.yaml文件其他组织模块和配置模块

Organizations:
    - &OrdererOrg
        Name: OrdererOrg
        ID: OrdererMSP
        MSPDir: crypto-config/ordererOrganizations/mikechain.com/msp
        Policies:
            Readers:
                Type: Signature
                Rule: "OR('OrdererMSP.member')"
            Writers:
                Type: Signature
                Rule: "OR('OrdererMSP.member')"
            Admins:
                Type: Signature
                Rule: "OR('OrdererMSP.admin')"

    - &Orga
        Name: OrgaMSP
        ID: OrgaMSP
        MSPDir: crypto-config/peerOrganizations/orga.mikechain.com/msp
        Policies:
            Readers:
                Type: Signature
                Rule: "OR('OrgaMSP.admin', 'OrgaMSP.peer', 'OrgaMSP.client')"
            Writers:
                Type: Signature
                Rule: "OR('OrgaMSP.admin', 'OrgaMSP.client')"
            Admins:
                Type: Signature
                Rule: "OR('OrgaMSP.admin')"
        AnchorPeers:
            - Host: peer0.orga.mikechain.com
              Port: 7051

    - &Orgb
        Name: OrgbMSP
        ID: OrgbMSP
        MSPDir: crypto-config/peerOrganizations/orgb.mikechain.com/msp
        Policies:
            Readers:
                Type: Signature
                Rule: "OR('OrgbMSP.admin', 'OrgbMSP.peer', 'OrgbMSP.client')"
            Writers:
                Type: Signature
                Rule: "OR('OrgbMSP.admin', 'OrgbMSP.client')"
            Admins:
                Type: Signature
                Rule: "OR('OrgbMSP.admin')"

        AnchorPeers:
            - Host: peer0.orgb.mikechain.com
              Port: 7051

Capabilities:
    Channel: &ChannelCapabilities 
        V1_3: true
    Orderer: &OrdererCapabilities
        V1_1: true
    Application: &ApplicationCapabilities        
        V1_3: true
        V1_2: false
        V1_1: false


Application: &ApplicationDefaults

    Organizations:

    Policies:
        Readers:
            Type: ImplicitMeta
            Rule: "ANY Readers"
        Writers:
            Type: ImplicitMeta
            Rule: "ANY Writers"
        Admins:
            Type: ImplicitMeta
            Rule: "MAJORITY Admins"

    Capabilities:
        <<: *ApplicationCapabilities

Orderer: &OrdererDefaults

    OrdererType: solo

    Addresses:
        - orderer.example.com:7050

    BatchTimeout: 2s

    BatchSize:

        MaxMessageCount: 10

        AbsoluteMaxBytes: 99 MB

        PreferredMaxBytes: 512 KB

    Kafka:
        Brokers:
            - 127.0.0.1:9092

    Organizations:

    
    Policies:
        Readers:
            Type: ImplicitMeta
            Rule: "ANY Readers"
        Writers:
            Type: ImplicitMeta
            Rule: "ANY Writers"
        Admins:
            Type: ImplicitMeta
            Rule: "MAJORITY Admins"
        
        BlockValidation:
            Type: ImplicitMeta
            Rule: "ANY Writers"

Channel: &ChannelDefaults
    
    Policies:
        
        Readers:
            Type: ImplicitMeta
            Rule: "ANY Readers"
        
        Writers:
            Type: ImplicitMeta
            Rule: "ANY Writers"
        
        Admins:
            Type: ImplicitMeta
            Rule: "MAJORITY Admins"

    
    Capabilities:
        <<: *ChannelCapabilities

使用generateChannelArtifacts()函数,生成创世区块

configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block

  创建通道,生成channel.tx文件,在byfn.sh文件中设置$CHANNEL_NAME,默认为mychannel

configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME

在byfn.sh文件中设置并创建锚节点更新给OrgaMSP

configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg OrgaMSP

在byfn.sh文件中设置并创建锚节点更新给OrgbMSP

configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg OrgbMSP

 

五、根据byfn.sh执行的参数判断docker-compose执行的配置文件。

5.1 修改docker-compose-couch.yaml,为每个节点配置相应的couchdb

version: '2'

networks:
  byfn:

services:
  couchdb0:
    container_name: couchdb0
    image: hyperledger/fabric-couchdb
    environment:
      - COUCHDB_USER=
      - COUCHDB_PASSWORD=
    ports:
      - "5984:5984"
    networks:
      - byfn

  peer0.orga.mikechain.com:
    environment:
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb0:5984
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
    depends_on:
      - couchdb0

  couchdb1:
    container_name: couchdb1
    image: hyperledger/fabric-couchdb
    environment:
      - COUCHDB_USER=
      - COUCHDB_PASSWORD=
    ports:
      - "6984:5984"
    networks:
      - byfn

  peer1.orga.mikechain.com:
    environment:
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb1:5984
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
    depends_on:
      - couchdb1

  couchdb2:
    container_name: couchdb2
    image: hyperledger/fabric-couchdb
    environment:
      - COUCHDB_USER=
      - COUCHDB_PASSWORD=
    ports:
      - "7984:5984"
    networks:
      - byfn

  peer0.orgb.mikechain.com:
    environment:
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb2:5984
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
    depends_on:
      - couchdb2

  couchdb3:
    container_name: couchdb3
    image: hyperledger/fabric-couchdb
    environment:
      - COUCHDB_USER=
      - COUCHDB_PASSWORD=
    ports:
      - "8984:5984"
    networks:
      - byfn

  peer1.orgb.mikechain.com:
    environment:
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb3:5984
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
    depends_on:
      - couchdb3

5.2修改docker-compose客户端配置文件


version: '2'

volumes:
  orderer.mikechain.com:
  peer0.orga.mikechain.com:
  peer1.orga.mikechain.com:
  peer0.orgb.mikechain.com:
  peer1.orgb.mikechain.com:

networks:
  byfn:

services:

  orderer.mikechain.com:
    extends:
      file:   base/docker-compose-base.yaml
      service: orderer.mikechain.com
    container_name: orderer.mikechain.com
    networks:
      - byfn

  peer0.orga.mikechain.com:
    container_name: peer0.orga.mikechain.com
    extends:
      file:  base/docker-compose-base.yaml
      service: peer0.orga.mikechain.com
    networks:
      - byfn

  peer1.orga.mikechain.com:
    container_name: peer1.orga.mikechain.com
    extends:
      file:  base/docker-compose-base.yaml
      service: peer1.orga.mikechain.com
    networks:
      - byfn

  peer0.orgb.mikechain.com:
    container_name: peer0.orgb.mikechain.com
    extends:
      file:  base/docker-compose-base.yaml
      service: peer0.orgb.mikechain.com
    networks:
      - byfn

  peer1.orgb.mikechain.com:
    container_name: peer1.orgb.mikechain.com
    extends:
      file:  base/docker-compose-base.yaml
      service: peer1.orgb.mikechain.com
    networks:
      - byfn

  cli:
    container_name: cli
    image: hyperledger/fabric-tools:$IMAGE_TAG
    tty: true
    stdin_open: true
    environment:
      - GOPATH=/opt/gopath
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      #- FABRIC_LOGGING_SPEC=DEBUG
      - FABRIC_LOGGING_SPEC=INFO
      - CORE_PEER_ID=cli
      - CORE_PEER_ADDRESS=peer0.orga.mikechain.com:7051
      - CORE_PEER_LOCALMSPID=OrgaMSP
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orga.mikechain.com/peers/peer0.orga.mikechain.com/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orga.mikechain.com/peers/peer0.orga.mikechain.com/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orga.mikechain.com/peers/peer0.orga.mikechain.com/tls/ca.crt
      - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orga.mikechain.com/users/[email protected]/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: /bin/bash
    volumes:
        - /var/run/:/host/var/run/
        - ./../chaincode/:/opt/gopath/src/github.com/chaincode
        - ./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.mikechain.com
      - peer0.orga.mikechain.com
      - peer1.orga.mikechain.com
      - peer0.orgb.mikechain.com
      - peer1.orgb.mikechain.com
    networks:
      - byfn

 

你可能感兴趣的:(fabric,1.4,联盟链,区块链)