美文网首页区块链教程
兄弟连区块链教程Fabric1.0源代码分析Orderer mu

兄弟连区块链教程Fabric1.0源代码分析Orderer mu

作者: ab6973df9221 | 来源:发表于2018-11-02 15:03 被阅读0次

  兄弟连区块链教程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

感谢关注兄弟连区块链教程http://bt.itxdl.cn/分享!

相关文章

网友评论

    本文标题:兄弟连区块链教程Fabric1.0源代码分析Orderer mu

    本文链接:https://www.haomeiwen.com/subject/succxqtx.html