Fabric 1.0原始碼分析(6)configtx(配置交易) #ChannelConfig(通道配置)
# 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
相關文章
- Fabric 1.0原始碼分析(7)configtx(配置交易) #configtxgen(生成通道配置)原始碼
- Fabric 1.0原始碼分析(8)configtx(配置交易) #genesis(系統通道創世區塊)原始碼
- Fabric 1.0原始碼分析(9)configtx(配置交易)體系介紹原始碼
- Fabric 1.0原始碼分析(27) Orderer #configupdate(處理通道配置更新)原始碼
- Fabric 1.0原始碼分析(43) Tx(Transaction 交易)原始碼
- Fabric 1.0原始碼分析(28) Orderer #localconfig(Orderer配置檔案定義)原始碼
- Fabric 1.0原始碼分析(42)scc(系統鏈碼) #cscc(通道相關)原始碼
- Fabric 1.0原始碼分析(25) Orderer原始碼
- Fabric 1.0原始碼分析(31) Peer原始碼
- Fabric 1.0原始碼分析(3)Chaincode(鏈碼)原始碼AI
- Fabric 1.0原始碼分析(40) Proposal(提案)原始碼
- Fabric 1.0原始碼分析(14) flogging(Fabric日誌系統)原始碼
- Fabric 1.0原始碼分析(18) Ledger(賬本)原始碼
- Fabric 1.0原始碼分析(47)Fabric 1.0.4 go程式碼量統計原始碼Go
- Fabric 1.0原始碼分析(42)scc(系統鏈碼)原始碼
- Fabric 1.0原始碼分析(13)events(事件服務)原始碼事件
- Fabric 1.0原始碼分析(26)Orderer #ledger(Orderer Ledger)原始碼
- Fabric 1.0原始碼分析(39) policy(背書策略)原始碼
- Fabric 1.0原始碼分析(45)gRPC(Fabric中註冊的gRPC Service)原始碼RPC
- Fabric 1.0原始碼分析(10)consenter(共識外掛)原始碼
- Fabric 1.0原始碼分析(15)gossip(流言演算法)原始碼Go演算法
- Fabric 1.0原始碼分析(23)LevelDB(KV資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(44)Tx #RWSet(讀寫集)原始碼
- Fabric 1.0原始碼分析(5)Chaincode(鏈碼)體系總結原始碼AI
- Fabric 1.0原始碼分析(20) Ledger #idStore(ledgerID資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(29) Orderer #multichain(多鏈支援包)原始碼AI
- Fabric 1.0原始碼分析(30) Orderer #BroadcastServer(Broadcast服務端)原始碼ASTServer服務端
- Fabric 1.0原始碼分析(35)Peer #EndorserServer(Endorser服務端)原始碼Server服務端
- Fabric 1.0原始碼分析(36) Peer #EndorserClient(Endorser客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(37) Peer #DeliverClient(Deliver客戶端)原始碼client客戶端
- Fabric 1.0原始碼分析(38) Peer #BroadcastClient(Broadcast客戶端)原始碼ASTclient客戶端
- Fabric 1.0原始碼分析(41)putils(protos/utils工具包)原始碼
- Fabric 1.0原始碼分析(2) blockfile(區塊檔案儲存)原始碼BloC
- Fabric 1.0原始碼分析(19) Ledger #statedb(狀態資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(21)Ledger #historydb(歷史資料庫)原始碼資料庫
- Fabric 1.0原始碼分析(22)Ledger #blkstorage(block檔案儲存)原始碼BloC
- Fabric 1.0原始碼分析(32) Peer #peer node start命令實現原始碼
- Fabric 1.0原始碼分析(4)Chaincode(鏈碼)#platforms(鏈碼語言平臺)原始碼AIPlatform