1. 程式人生 > >區塊鏈教程Fabric1.0原始碼分析Orderer multichain

區塊鏈教程Fabric1.0原始碼分析Orderer multichain

  區塊鏈教程Fabric1.0原始碼分析Orderer multichain,2018年下半年,區塊鏈行業正逐漸褪去發展之初的浮躁、迴歸理性,表面上看相關人才需求與身價似乎正在回落。但事實上,正是初期泡沫的漸退,讓人們更多的關注點放在了區塊鏈真正的技術之上。

Fabric 1.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