Fabric 1.0原始碼分析(6)configtx(配置交易) #ChannelConfig(通道配置)

尹成發表於2018-05-20
# Fabric 1.0原始碼筆記 之 configtx(配置交易) #ChannelConfig(通道配置)

## 1、ChannelConfig概述

ChannelConfig程式碼分佈在common/config目錄下。目錄結構如下:

* channel_util.go,channel相關工具函式。
* orderer_util.go,orderer(系統通道)相關工具函式。
* application_util.go,應用通道相關工具函式。
* consortiums_util.go,聯盟相關工具函式。

* api.go,核心介面定義,如Org、ApplicationOrg、Channel、Orderer、Application、Consortium、Consortiums、ValueProposer介面定義。
* root.go,Root結構體及方法。
* channel.go,ChannelGroup結構體及方法。
* orderer.go,OrdererGroup結構體及方法。
* application.go,ApplicationGroup結構體及方法。

## 2、工具函式

### 2.1、channel相關工具函式

```go
//用key和value構建cb.ConfigGroup
func configGroup(key string, value []byte) *cb.ConfigGroup {
    result := cb.NewConfigGroup()
    result.Values[key] = &cb.ConfigValue{
        Value: value,
    }
}

//設定聯盟
//ConsortiumKey = "Consortium"
//configGroup(ConsortiumKey, utils.MarshalOrPanic(&cb.Consortium{Name: name}))
func TemplateConsortium(name string) *cb.ConfigGroup

//設定雜湊函式
//HashingAlgorithmKey = "HashingAlgorithm"
//configGroup(HashingAlgorithmKey, utils.MarshalOrPanic(&cb.HashingAlgorithm{Name: name}))
func TemplateHashingAlgorithm(name string) *cb.ConfigGroup

//預設雜湊函式
//const defaultHashingAlgorithm = bccsp.SHA256
//TemplateHashingAlgorithm(defaultHashingAlgorithm)
func DefaultHashingAlgorithm() *cb.ConfigGroup

//設定塊資料雜湊結構
//BlockDataHashingStructureKey = "BlockDataHashingStructure"
//configGroup(BlockDataHashingStructureKey, utils.MarshalOrPanic(&cb.BlockDataHashingStructure{Width: width}))
func TemplateBlockDataHashingStructure(width uint32) *cb.ConfigGroup

//預設塊資料雜湊結構
//const defaultBlockDataHashingStructureWidth = math.MaxUint32
//TemplateBlockDataHashingStructure(defaultBlockDataHashingStructureWidth)
func DefaultBlockDataHashingStructure() *cb.ConfigGroup

//設定Orderer地址
//OrdererAddressesKey = "OrdererAddresses"
//configGroup(OrdererAddressesKey, utils.MarshalOrPanic(&cb.OrdererAddresses{Addresses: addresses}))
func TemplateOrdererAddresses(addresses []string) *cb.ConfigGroup

//預設Orderer地址
//var defaultOrdererAddresses = []string{"127.0.0.1:7050"}
//TemplateOrdererAddresses(defaultOrdererAddresses)
func DefaultOrdererAddresses() *cb.ConfigGroup
//程式碼在common/config/channel_util.go
```

補充cb.ConfigGroup定義:

```go
type ConfigGroup struct {
    Version uint64
    Groups map[string]*ConfigGroup
    Values map[string]*ConfigValue
    Policies map[string]*ConfigPolicy
    ModPolicy string
}
//程式碼在protos/common/configtx.pb.go
```

### 2.2、orderer相關工具函式

```go
func ordererConfigGroup(key string, value []byte) *cb.ConfigGroup
func TemplateConsensusType(typeValue string) *cb.ConfigGroup
func TemplateBatchSize(batchSize *ab.BatchSize) *cb.ConfigGroup
func TemplateBatchTimeout(batchTimeout string) *cb.ConfigGroup
func TemplateChannelRestrictions(maxChannels uint64) *cb.ConfigGroup
func TemplateKafkaBrokers(brokers []string) *cb.ConfigGroup
//程式碼在common/config/orderer_util.go
```

### 2.3、應用通道相關工具函式

```go
func applicationConfigGroup(orgID string, key string, value []byte) *cb.ConfigGroup
func TemplateAnchorPeers(orgID string, anchorPeers []*pb.AnchorPeer) *cb.ConfigGroup
//程式碼在common/config/application_util.go
```

### 2.4、聯盟相關工具函式

```go
func TemplateConsortiumsGroup() *cb.ConfigGroup
func TemplateConsortiumChannelCreationPolicy(name string, policy *cb.Policy) *cb.ConfigGroup
//程式碼在common/config/consortiums_util.go
```

## 3、核心介面定義

```go
type Org interface { //組織介面
    Name() string //組織名稱
    MSPID() string //組織MSPID
}

type ApplicationOrg interface { //應用組織介面
    Org //嵌入Org
    AnchorPeers() []*pb.AnchorPeer //錨節點
}

type Channel interface { //通道配置介面
    HashingAlgorithm() func(input []byte) []byte //雜湊演算法
    BlockDataHashingStructureWidth() uint32 //指定計算 BlockDataHash 時使用的 Merkle 樹的寬度
    OrdererAddresses() []string //Orderer地址
}

type Application interface { //應用配置介面
    Organizations() map[string]ApplicationOrg //應用組織map
}

type Consortiums interface { //聯盟配置map介面
    Consortiums() map[string]Consortium //Consortium map
}

type Consortium interface { //聯盟配置介面
    ChannelCreationPolicy() *cb.Policy //通道建立策略
}

type Orderer interface { //Orderer配置介面
    ConsensusType() string //共識型別
    BatchSize() *ab.BatchSize //塊中的最大訊息數
    BatchTimeout() time.Duration //建立批處理之前等待的時間量
    MaxChannelsCount() uint64 //最大通道數
    KafkaBrokers() []string //Kafka地址
    Organizations() map[string]Org //Orderer組織
}

type ValueProposer interface {
    BeginValueProposals(tx interface{}, groups []string) (ValueDeserializer, []ValueProposer, error) //配置Proposal前
    RollbackProposals(tx interface{}) //回滾配置Proposal
    PreCommit(tx interface{}) error //提交前
    CommitProposals(tx interface{}) //提交
}
//程式碼在common/config/api.go
```

## 4、Root結構體及方法

```go
type Root struct {
    channel *ChannelGroup
    mspConfigHandler *msp.MSPConfigHandler
}

func NewRoot(mspConfigHandler *msp.MSPConfigHandler) *Root //構造Root
//啟動新的配置Proposal,r.mspConfigHandler.BeginConfig(tx)
func (r *Root) BeginValueProposals(tx interface{}, groups []string) (ValueDeserializer, []ValueProposer, error)
//回滾配置Proposal,r.mspConfigHandler.RollbackProposals(tx)
func (r *Root) RollbackProposals(tx interface{})
//提交前校驗配置,r.mspConfigHandler.PreCommit(tx)
func (r *Root) PreCommit(tx interface{}) error
//提交配置Proposal,r.mspConfigHandler.CommitProposals(tx)
func (r *Root) CommitProposals(tx interface{})
//獲取r.channel
func (r *Root) Channel() *ChannelGroup
//獲取r.channel.OrdererConfig()
func (r *Root) Orderer() *OrdererGroup
//獲取r.channel.ApplicationConfig()
func (r *Root) Application() *ApplicationGroup
//獲取r.channel.ConsortiumsConfig()
func (r *Root) Consortiums() *ConsortiumsGroup {
//程式碼在common/config/root.go
```

## 5、ChannelGroup結構體及方法

### 5.1、ChannelGroup結構體及方法

```go
type ChannelGroup struct {
    *ChannelConfig //嵌入ChannelConfig
    *Proposer //嵌入Proposer
    mspConfigHandler *msp.MSPConfigHandler
}

type ChannelConfig struct {
    *standardValues
    protos *ChannelProtos
    hashingAlgorithm func(input []byte) []byte
    appConfig *ApplicationGroup
    ordererConfig *OrdererGroup
    consortiumsConfig *ConsortiumsGroup
}

type ChannelProtos struct {
    HashingAlgorithm *cb.HashingAlgorithm
    BlockDataHashingStructure *cb.BlockDataHashingStructure
    OrdererAddresses *cb.OrdererAddresses
    Consortium *cb.Consortium
}

構造ChannelGroup,以及構造ChannelConfig和Proposer
func NewChannelGroup(mspConfigHandler *msp.MSPConfigHandler) *ChannelGroup
func (cg *ChannelGroup) Allocate() Values //構造channelConfigSetter
//獲取cg.ChannelConfig.ordererConfig
func (cg *ChannelGroup) OrdererConfig() *OrdererGroup
//獲取cg.ChannelConfig.appConfig
func (cg *ChannelGroup) ApplicationConfig() *ApplicationGroup
//獲取cg.ChannelConfig.consortiumsConfig
func (cg *ChannelGroup) ConsortiumsConfig() *ConsortiumsGroup
func (cg *ChannelGroup) NewGroup(group string) (ValueProposer, error)

//構造ChannelConfig,NewStandardValues(cc.protos)
func NewChannelConfig() *ChannelConfig
//獲取cc.hashingAlgorithm
func (cc *ChannelConfig) HashingAlgorithm() func(input []byte) []byte
//獲取cc.protos.BlockDataHashingStructure.Width
func (cc *ChannelConfig) BlockDataHashingStructureWidth() uint32
//獲取cc.protos.OrdererAddresses.Addresses
func (cc *ChannelConfig) OrdererAddresses() []string
//獲取cc.protos.Consortium.Name
func (cc *ChannelConfig) ConsortiumName() string
func (cc *ChannelConfig) Validate(tx interface{}, groups map[string]ValueProposer) error
func (cc *ChannelConfig) validateHashingAlgorithm() error
func (cc *ChannelConfig) validateBlockDataHashingStructure() error
func (cc *ChannelConfig) validateOrdererAddresses() error
//程式碼在common/config/channel.go
```

補充cb.HashingAlgorithm、cb.BlockDataHashingStructure、cb.OrdererAddresses、cb.Consortium定義:

```go
//雜湊演算法
type HashingAlgorithm struct {
    Name string
}

//塊資料雜湊結構
type BlockDataHashingStructure struct {
    Width uint32 //指定計算 BlockDataHash 時使用的 Merkle 樹的寬度
}

//Orderer地址
type OrdererAddresses struct {
    Addresses []string
}

type Consortium struct {
    Name string
}
//程式碼在protos/common/configuration.pb.go
```

### 5.2、Proposer結構體及方法

```go
type Proposer struct {
    vh Handler
    pending map[interface{}]*config
    current *config
    pendingLock sync.RWMutex
}
func NewProposer(vh Handler) *Proposer
func (p *Proposer) BeginValueProposals(tx interface{}, groups []string) (ValueDeserializer, []ValueProposer, error)
func (p *Proposer) PreCommit(tx interface{}) error
func (p *Proposer) RollbackProposals(tx interface{})
func (p *Proposer) CommitProposals(tx interface{})
//程式碼在common/config/proposer.go
```

### 5.3、OrdererGroup結構體及方法

```go
type OrdererGroup struct {
    *Proposer
    *OrdererConfig
    mspConfig *msp.MSPConfigHandler
}

type OrdererConfig struct {
    *standardValues
    protos *OrdererProtos
    ordererGroup *OrdererGroup
    orgs map[string]Org
    batchTimeout time.Duration
}

type OrdererProtos struct {
    ConsensusType *ab.ConsensusType
    BatchSize *ab.BatchSize
    BatchTimeout *ab.BatchTimeout
    KafkaBrokers *ab.KafkaBrokers
    ChannelRestrictions *ab.ChannelRestrictions
}

//構造OrdererGroup,以及Proposer
func NewOrdererGroup(mspConfig *msp.MSPConfigHandler) *OrdererGroup
func (og *OrdererGroup) NewGroup(name string) (ValueProposer, error)
func (og *OrdererGroup) Allocate() Values

//構造OrdererConfig
func NewOrdererConfig(og *OrdererGroup) *OrdererConfig
//oc.ordererGroup.OrdererConfig = oc
func (oc *OrdererConfig) Commit()
//獲取oc.protos.ConsensusType.Type
func (oc *OrdererConfig) ConsensusType() string
//獲取oc.protos.BatchSize
func (oc *OrdererConfig) BatchSize() *ab.BatchSize
//獲取oc.batchTimeout
func (oc *OrdererConfig) BatchTimeout() time.Duration
//獲取oc.protos.KafkaBrokers.Brokers
func (oc *OrdererConfig) KafkaBrokers() []string
//獲取oc.protos.ChannelRestrictions.MaxCount
func (oc *OrdererConfig) MaxChannelsCount() uint64
//獲取oc.orgs
func (oc *OrdererConfig) Organizations() map[string]Org
func (oc *OrdererConfig) Validate(tx interface{}, groups map[string]ValueProposer) error
func (oc *OrdererConfig) validateConsensusType() error
func (oc *OrdererConfig) validateBatchSize() error
func (oc *OrdererConfig) validateBatchTimeout() error
func (oc *OrdererConfig) validateKafkaBrokers() error
func brokerEntrySeemsValid(broker string) bool
//程式碼在common/config/orderer.go
```

### 5.4、ApplicationGroup結構體及方法

```go
//程式碼在common/config/application.go
```






網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN




網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN

相關文章