1. 程式人生 > >區塊鏈教程Fabric1.0源代碼分析配置交易-生成通道配置

區塊鏈教程Fabric1.0源代碼分析配置交易-生成通道配置

tran 路徑 實現 消息 org 代碼筆記 ssi bat 初始

Fabric 1.0源代碼筆記 之 configtx(配置交易) #configtxgen(生成通道配置)

1、configtxgen概述

configtxgen,用於生成通道配置,具體有如下三種用法:

  • 生成Orderer服務啟動的初始區塊(即系統通道的創世區塊文件)
    ????* configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
  • 生成新建應用通道的配置交易(即用於創建應用通道的配置交易文件)
    ????* configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
  • 生成錨節點配置更新文件
    ????* configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
    ????
    configtxgen代碼分布在common/configtx/tool目錄,目錄結構如下:

  • localconfig/config.go,configtx.yaml配置文件相關的結構體及方法。

2、configtx.yaml配置文件示例

Profiles:
    TwoOrgsOrdererGenesis: #Orderer系統通道配置
        Orderer:
            <<: *OrdererDefaults #引用OrdererDefaults並合並到當前
            Organizations: #屬於Orderer通道的組織
                - *OrdererOrg 
        Consortiums: #Orderer所服務的聯盟列表
            SampleConsortium:
                Organizations:
                    - *Org1
                    - *Org2
    TwoOrgsChannel: #應用通道配置
        Consortium: SampleConsortium #應用通道關聯的聯盟
        Application: 
            <<: *ApplicationDefaults #引用ApplicationDefaults並合並到當前
            Organizations: #初始加入應用通道的組織
                - *Org1
                - *Org2
Organizations:
    - &OrdererOrg
        Name: OrdererOrg
        ID: OrdererMSP # MSP ID
        MSPDir: crypto-config/ordererOrganizations/example.com/msp #MSP相關文件本地路徑
    - &Org1
        Name: Org1MSP
        ID: Org1MSP
        MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
        AnchorPeers: #錨節點地址,用於跨組織的Gossip通信
            - Host: peer0.org1.example.com
              Port: 7051
    - &Org2
        Name: Org2MSP
        ID: Org2MSP
        MSPDir: crypto-config/peerOrganizations/org2.example.com/msp
        AnchorPeers: #錨節點地址,用於跨組織的Gossip通信
            - Host: peer0.org2.example.com
              Port: 7051
Orderer: &OrdererDefaults
    OrdererType: solo # Orderer共識插件類型,分solo或kafka
    Addresses:
        - orderer.example.com:7050 #服務地址
    BatchTimeout: 2s #創建批量交易的最大超時,一批交易構成一個塊
    BatchSize: #寫入區塊內的交易個數
        MaxMessageCount: 10 #一批消息的最大個數
        AbsoluteMaxBytes: 98 MB #一批交易的最大字節數,任何時候均不能超過
        PreferredMaxBytes: 512 KB #批量交易的建議字節數
    Kafka:
        Brokers: #Kafka端口
            - 127.0.0.1:9092
    Organizations: #參與維護Orderer的組織,默認空
Application: &ApplicationDefaults
    Organizations: #加入到通道的組織信息,此處為不包括任何組織

配置文件解讀:

  • 每個Profile表示某種場景下的通道配置模板,包括Orderer系統通道模板和應用通道模板,其中TwoOrgsOrdererGenesis為系統通道模板,TwoOrgsChannel為應用通道模板。
  • Orderer系統通道模板,包括Orderer和Consortiums,其中Orderer指定系統通道配置,Consortiums為Orderer服務的聯盟列表。
  • 應用通道,包括Application和Consortium,其中Application為應用通道的配置,Consortium為應用通道所關聯的聯盟名稱。
    ????
    附:YAML 語言教程
    -表示數組,&表示錨點,*表示引用,<<表示合並到當前數據。

3、configtx.yaml配置文件相關的結構體及方法

3.1、configtx.yaml配置文件相關的結構體定義

type TopLevel struct {
????Profiles      map[string]*Profile //通道配置
????Organizations []*Organization //組織
????Application   *Application //應用通道配置
????Orderer       *Orderer //系統通道配置
}

type Profile struct { //通道配置:系統通道配置或應用通道配置
????Consortium  string //應用通道配置中通道所關聯的聯盟名稱
????Application *Application //應用通道配置
????Orderer     *Orderer //系統通道配置
????Consortiums map[string]*Consortium //系統通道配置中Orderer服務的聯盟列表
}

type Consortium struct { //聯盟,即組織列表
????Organizations []*Organization //組織
}

type Application struct { //應用通道配置,即初始加入通道的組織
????Organizations []*Organization
}

type Organization struct { //組織
????Name           string //組織名稱
????ID             string //組織MSP ID
????MSPDir         string //組織MSP文件所在路徑
????AdminPrincipal string //管理員身份規則
????AnchorPeers []*AnchorPeer //錨節點列表
}

type AnchorPeer struct { //錨節點,即主機和端口
????Host string
????Port int
}

type Orderer struct { //系統通道配置
????OrdererType   string //共識插件類型
????Addresses     []string //Orderer服務地址
????BatchTimeout  time.Duration //批處理超時
????BatchSize     BatchSize //批處理大小
????Kafka         Kafka //Kafka
????Organizations []*Organization //參與維護Orderer的組織,默認空
????MaxChannels   uint64 //Orderer最大通道數
}

type BatchSize struct { //批處理大小
????MaxMessageCount   uint32 //最大交易數量
????AbsoluteMaxBytes  uint32 //最大字節數
????PreferredMaxBytes uint32 //建議字節數
}

type Kafka struct {
????Brokers []string //Kafka Broker
}
//代碼在common/configtx/tool/localconfig/config.go

3.2、configtx.yaml配置文件相關的方法

//獲取指定profile的Profile結構
func Load(profile string) *Profile 
//將Profile校驗並補充完整
func (p *Profile) completeInitialization(configDir string) 
func translatePaths(configDir string, org *Organization) 
//代碼在common/configtx/tool/localconfig/config.go

4、Generator接口及實現

Generator接口定義:

type Generator interface {
????bootstrap.Helper
????ChannelTemplate() configtx.Template //獲取用於初始化通道的模板
????GenesisBlockForChannel(channelID string) *cb.Block //用於outputBlock
}
//代碼在common/configtx/tool/provisional/provisional.go

區塊鏈教程Fabric1.0源代碼分析配置交易-生成通道配置