天天看點

區塊鍊教程Fabric1.0源代碼分析Orderer multichain多鍊支援包Fabric 1.0源代碼筆記 之 Orderer #multichain(多鍊支援包)

Fabric 1.0源代碼筆記 之 Orderer #multichain(多鍊支援包)

兄弟連區塊鍊教程Fabric1.0源代碼分析Orderer multichain多鍊支援包

1、multichain概述

multichain代碼集中在orderer/multichain目錄下,目錄結構如下:

  • manager.go,Manager接口定義及實作。
  • chainsupport.go,ChainSupport接口定義及實作。
  • systemchain.go,system chain。

2、Manager接口定義及實作

2.1、Manager接口定義

用于鍊的建立和通路。

type Manager interface {
    //擷取ChainSupport,以及判斷鍊是否存在
    GetChain(chainID string) (ChainSupport, bool)
    //擷取系統通道的通道ID
    SystemChannelID() string
    //支援通道建立請求
    NewChannelConfig(envConfigUpdate *cb.Envelope) (configtxapi.Manager, error)
}
//代碼在orderer/multichain/manager.go           

2.2、Manager接口實作

Manager接口實作,即multiLedger結構體及方法。

type multiLedger struct {
    chains          map[string]*chainSupport
    consenters      map[string]Consenter
    ledgerFactory   ledger.Factory
    signer          crypto.LocalSigner
    systemChannelID string
    systemChannel   *chainSupport
}

type configResources struct {
    configtxapi.Manager
}

type ledgerResources struct {
    *configResources
    ledger ledger.ReadWriter
}
//代碼在orderer/multichain/manager.go           

涉及方法如下:

func (cr *configResources) SharedConfig() config.Orderer
//擷取配置交易Envelope
func getConfigTx(reader ledger.Reader) *cb.Envelope
//構造multiLedger
func NewManagerImpl(ledgerFactory ledger.Factory, consenters map[string]Consenter, signer crypto.LocalSigner) Manager
//擷取系統鍊ID
func (ml *multiLedger) SystemChannelID() string
//按chainID擷取ChainSupport
func (ml *multiLedger) GetChain(chainID string) (ChainSupport, bool)
//構造ledgerResources
func (ml *multiLedger) newLedgerResources(configTx *cb.Envelope) *ledgerResources
//建立新鍊
func (ml *multiLedger) newChain(configtx *cb.Envelope)
//通道或鍊的個數
func (ml *multiLedger) channelsCount() int
//支援建立新的通道
func (ml *multiLedger) NewChannelConfig(envConfigUpdate *cb.Envelope) (configtxapi.Manager, error)
//代碼在orderer/multichain/manager.go           

func NewManagerImpl(ledgerFactory ledger.Factory, consenters map[string]Consenter, signer crypto.LocalSigner) Manager代碼如下:

func NewManagerImpl(ledgerFactory ledger.Factory, consenters map[string]Consenter, signer crypto.LocalSigner) Manager {
    ml := &multiLedger{
        chains:        make(map[string]*chainSupport),
        ledgerFactory: ledgerFactory,
        consenters:    consenters,
        signer:        signer,
    }

    existingChains := ledgerFactory.ChainIDs()
    for _, chainID := range existingChains {
        rl, err := ledgerFactory.GetOrCreate(chainID)
        configTx := getConfigTx(rl)
        ledgerResources := ml.newLedgerResources(configTx)
        chainID := ledgerResources.ChainID()

        if _, ok := ledgerResources.ConsortiumsConfig(); ok { //系統鍊
            chain := newChainSupport(createSystemChainFilters(ml, ledgerResources), ledgerResources, consenters, signer)
            ml.chains[chainID] = chain
            ml.systemChannelID = chainID
            ml.systemChannel = chain
            defer chain.start()
        } else { //普通鍊
            chain := newChainSupport(createStandardFilters(ledgerResources), ledgerResources, consenters, signer)
            ml.chains[chainID] = chain
            chain.start()
        }
    }
    return ml
}
//代碼在orderer/multichain/manager.go           

3、ChainSupport接口定義及實作

3.1、ChainSupport接口定義

type ChainSupport interface {
    PolicyManager() policies.Manager //政策管理
    Reader() ledger.Reader
    Errored() <-chan struct{}
    broadcast.Support
    ConsenterSupport //嵌入ConsenterSupport接口
    Sequence() uint64
    //支援通道更新
    ProposeConfigUpdate(env *cb.Envelope) (*cb.ConfigEnvelope, error)
}

type ConsenterSupport interface {
    crypto.LocalSigner
    BlockCutter() blockcutter.Receiver
    SharedConfig() config.Orderer
    CreateNextBlock(messages []*cb.Envelope) *cb.Block
    WriteBlock(block *cb.Block, committers []filter.Committer, encodedMetadataValue []byte) *cb.Block
    ChainID() string
    Height() uint64
}

type Consenter interface { //定義支援排序機制
    HandleChain(support ConsenterSupport, metadata *cb.Metadata) (Chain, error)
}

type Chain interface {
    //接受消息
    Enqueue(env *cb.Envelope) bool
    Errored() <-chan struct{}
    Start() //開始
    Halt() //挂起
}
//代碼在orderer/multichain/chainsupport.go           

3.2、ChainSupport和ConsenterSupport接口實作

ChainSupport接口實作,即chainSupport結構體及方法。

type chainSupport struct {
    *ledgerResources
    chain         Chain
    cutter        blockcutter.Receiver
    filters       *filter.RuleSet
    signer        crypto.LocalSigner
    lastConfig    uint64
    lastConfigSeq uint64
}
//代碼在orderer/multichain/chainsupport.go           
//構造chainSupport
func newChainSupport(filters *filter.RuleSet,ledgerResources *ledgerResources,consenters map[string]Consenter,signer crypto.LocalSigner,) *chainSupport
func createStandardFilters(ledgerResources *ledgerResources) *filter.RuleSet
func createSystemChainFilters(ml *multiLedger, ledgerResources *ledgerResources) *filter.RuleSet
func (cs *chainSupport) start()
func (cs *chainSupport) NewSignatureHeader() (*cb.SignatureHeader, error)
func (cs *chainSupport) Sign(message []byte) ([]byte, error)
func (cs *chainSupport) Filters() *filter.RuleSet
func (cs *chainSupport) BlockCutter() blockcutter.Receiver
func (cs *chainSupport) Reader() ledger.Reader
func (cs *chainSupport) Enqueue(env *cb.Envelope) bool
func (cs *chainSupport) Errored() <-chan struct{}
//建立塊,調取ledger.CreateNextBlock(cs.ledger, messages)
func (cs *chainSupport) CreateNextBlock(messages []*cb.Envelope) *cb.Block
func (cs *chainSupport) addBlockSignature(block *cb.Block)
func (cs *chainSupport) addLastConfigSignature(block *cb.Block)
//寫入塊
func (cs *chainSupport) WriteBlock(block *cb.Block, committers []filter.Committer, encodedMetadataValue []byte) *cb.Block 
func (cs *chainSupport) Height() uint64
//代碼在orderer/multichain/chainsupport.go           

func (cs chainSupport) WriteBlock(block cb.Block, committers []filter.Committer, encodedMetadataValue []byte) *cb.Block 代碼如下:

func (cs *chainSupport) WriteBlock(block *cb.Block, committers []filter.Committer, encodedMetadataValue []byte) *cb.Block {
    for _, committer := range committers {
        committer.Commit()
    }
    cs.addBlockSignature(block)
    cs.addLastConfigSignature(block)
    err := cs.ledger.Append(block)//賬本追加塊
    return block
}
//代碼在orderer/multichain/chainsupport.go