區塊鏈教程Fabric1.0源代碼分析配置交易體系介
阿新 • • 發佈:2018-10-30
sco ring mon group 區塊 comm templates copy marshal 區塊鏈教程之Fabric1.0源代碼分析配置交易體系介紹,2018年下半年,區塊鏈行業正逐漸褪去發展之初的浮躁、回歸理性,表面上看相關人才需求與身價似乎正在回落。但事實上,正是初期泡沫的漸退,讓人們更多的關註點放在了區塊鏈真正的技術之上。
Fabric 1.0源代碼筆記 之 configtx(配置交易)
1、configtx概述
configtx代碼分布在common/configtx目錄,目錄結構如下:
- api目錄,核心接口定義,如Manager、Resources、Transactional、PolicyHandler、Proposer、Initializer。
- initializer.go,Resources和Initializer接口實現。
- template.go,Template接口定義及實現。
- config.go,ConfigResult接口定義及實現。
- manager.go/update.go,Manager接口實現。
- util.go,configtx工具函數。
2、Template接口定義及實現
2.1、Template接口定義
type Template interface {
????Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error)
}
//代碼在common/configtx/template.go
ConfigUpdateEnvelope定義:
type ConfigUpdateEnvelope struct { ????ConfigUpdate []byte //type ConfigUpdate struct ????Signatures []*ConfigSignature //type ConfigSignature struct } type ConfigUpdate struct { ????ChannelId string ????ReadSet *ConfigGroup //type ConfigGroup struct ????WriteSet *ConfigGroup //type ConfigGroup struct } type ConfigSignature struct { ????SignatureHeader []byte ????Signature []byte } 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、Template接口實現(simpleTemplate)
type simpleTemplate struct { ????configGroup *cb.ConfigGroup } func NewSimpleTemplate(configGroups ...*cb.ConfigGroup) Template { ????sts := make([]Template, len(configGroups)) ????for i, group := range configGroups { ????????sts[i] = &simpleTemplate{ ????????????configGroup: group, ????????} ????} ????return NewCompositeTemplate(sts...) } func (st *simpleTemplate) Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error) { ????config, err := proto.Marshal(&cb.ConfigUpdate{ ????????ChannelId: chainID, ????????WriteSet: st.configGroup, ????}) ????return &cb.ConfigUpdateEnvelope{ ????????ConfigUpdate: config, ????}, nil } //代碼在common/configtx/template.go
2.3、Template接口實現(compositeTemplate)
type compositeTemplate struct {
????templates []Template
}
func NewCompositeTemplate(templates ...Template) Template {
????return &compositeTemplate{templates: templates}
}
func (ct *compositeTemplate) Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error) {
????channel := cb.NewConfigGroup()
????for i := range ct.templates {
????????configEnv, err := ct.templates[i].Envelope(chainID)
????????config, err := UnmarshalConfigUpdate(configEnv.ConfigUpdate)
????????err = copyGroup(config.WriteSet, channel)
????}
????marshaledConfig, err := proto.Marshal(&cb.ConfigUpdate{
????????ChannelId: chainID,
????????WriteSet: channel,
????})
????return &cb.ConfigUpdateEnvelope{ConfigUpdate: marshaledConfig}, nil
}
//代碼在common/configtx/template.go
2.4、Template接口實現(modPolicySettingTemplate)
type modPolicySettingTemplate struct {
????modPolicy string
????template Template
}
func NewModPolicySettingTemplate(modPolicy string, template Template) Template {
????return &modPolicySettingTemplate{
????????modPolicy: modPolicy,
????????template: template,
????}
}
func (mpst *modPolicySettingTemplate) Envelope(channelID string) (*cb.ConfigUpdateEnvelope, error) {
????configUpdateEnv, err := mpst.template.Envelope(channelID)
????config, err := UnmarshalConfigUpdate(configUpdateEnv.ConfigUpdate)
????setGroupModPolicies(mpst.modPolicy, config.WriteSet)
????configUpdateEnv.ConfigUpdate = utils.MarshalOrPanic(config)
????return configUpdateEnv, nil
}
//代碼在common/configtx/template.go
3、configtx工具函數
func UnmarshalConfig(data []byte) (*cb.Config, error)
func UnmarshalConfigOrPanic(data []byte) *cb.Config
func UnmarshalConfigUpdate(data []byte) (*cb.ConfigUpdate, error)
func UnmarshalConfigUpdateOrPanic(data []byte) *cb.ConfigUpdate
func UnmarshalConfigUpdateEnvelope(data []byte) (*cb.ConfigUpdateEnvelope, error)
func UnmarshalConfigUpdateEnvelopeOrPanic(data []byte) *cb.ConfigUpdateEnvelope
func UnmarshalConfigEnvelope(data []byte) (*cb.ConfigEnvelope, error)
func UnmarshalConfigEnvelopeOrPanic(data []byte) *cb.ConfigEnvelope
//代碼在common/configtx/util.go
4、Resources接口定義及實現
4.1、Resources接口定義
type Resources interface {
????PolicyManager() policies.Manager //獲取通道策略管理器,即policies.Manager
????ChannelConfig() config.Channel //獲取通道配置
????OrdererConfig() (config.Orderer, bool) //獲取Orderer配置
????ConsortiumsConfig() (config.Consortiums, bool) //獲取config.Consortiums
????ApplicationConfig() (config.Application, bool) //獲取config.Application
????MSPManager() msp.MSPManager //獲取通道msp管理器,即msp.MSPManager
}
//代碼在common/configtx/api/api.go
4.2、Resources接口實現
Resources接口實現,即resources結構體及方法。
type resources struct {
????policyManager *policies.ManagerImpl
????configRoot *config.Root
????mspConfigHandler *configtxmsp.MSPConfigHandler
}
//代碼在common/configtx/initializer.go
涉及方法如下:
//獲取r.policyManager
func (r *resources) PolicyManager() policies.Manager
//獲取r.configRoot.Channel()
func (r *resources) ChannelConfig() config.Channel
//獲取r.configRoot.Orderer()
func (r *resources) OrdererConfig() (config.Orderer, bool)
//獲取r.configRoot.Application()
func (r *resources) ApplicationConfig() (config.Application, bool)
//獲取r.configRoot.Consortiums()
func (r *resources) ConsortiumsConfig() (config.Consortiums, bool)
//獲取r.mspConfigHandler
func (r *resources) MSPManager() msp.MSPManager
//構造resources
func newResources() *resources
//代碼在common/configtx/initializer.go
區塊鏈教程Fabric1.0源代碼分析配置交易體系介