區塊鏈教程Fabric1.0源代碼分析configtx#ChannelConfig
阿新 • • 發佈:2018-10-29
version error ace 地址 prot sha256 const handle pid 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相關工具函數
//用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定義:
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相關工具函數
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、應用通道相關工具函數
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、聯盟相關工具函數
func TemplateConsortiumsGroup() *cb.ConfigGroup
func TemplateConsortiumChannelCreationPolicy(name string, policy *cb.Policy) *cb.ConfigGroup
//代碼在common/config/consortiums_util.go
3、核心接口定義
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結構體及方法
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結構體及方法
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定義:
//哈希算法
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結構體及方法
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結構體及方法
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結構體及方法
//代碼在common/config/application.go
-
區塊鏈教程Fabric1.0源代碼分析configtx#ChannelConfig