博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
区块链教程Fabric1.0源代码分析Orderer multichain多链支持包
阅读量:6340 次
发布时间:2019-06-22

本文共 6071 字,大约阅读时间需要 20 分钟。

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//获取配置交易Envelopefunc getConfigTx(reader ledger.Reader) *cb.Envelope//构造multiLedgerfunc NewManagerImpl(ledgerFactory ledger.Factory, consenters map[string]Consenter, signer crypto.LocalSigner) Manager//获取系统链IDfunc (ml *multiLedger) SystemChannelID() string//按chainID获取ChainSupportfunc (ml *multiLedger) GetChain(chainID string) (ChainSupport, bool)//构造ledgerResourcesfunc (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

涉及方法如下:

//构造chainSupportfunc newChainSupport(filters *filter.RuleSet,ledgerResources *ledgerResources,consenters map[string]Consenter,signer crypto.LocalSigner,) *chainSupportfunc createStandardFilters(ledgerResources *ledgerResources) *filter.RuleSetfunc createSystemChainFilters(ml *multiLedger, ledgerResources *ledgerResources) *filter.RuleSetfunc (cs *chainSupport) start()func (cs *chainSupport) NewSignatureHeader() (*cb.SignatureHeader, error)func (cs *chainSupport) Sign(message []byte) ([]byte, error)func (cs *chainSupport) Filters() *filter.RuleSetfunc (cs *chainSupport) BlockCutter() blockcutter.Receiverfunc (cs *chainSupport) Reader() ledger.Readerfunc (cs *chainSupport) Enqueue(env *cb.Envelope) boolfunc (cs *chainSupport) Errored() <-chan struct{}//创建块,调取ledger.CreateNextBlock(cs.ledger, messages)func (cs *chainSupport) CreateNextBlock(messages []*cb.Envelope) *cb.Blockfunc (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://ileoa.baihongyu.com/

你可能感兴趣的文章
40个图像处理开源库
查看>>
添加侧边栏实战课程
查看>>
C++ 结构体初始化
查看>>
SpringBoot------热部署(Springloaded)
查看>>
react-native 封装 VedioPlayer 组件
查看>>
ServletContextListener的作用
查看>>
内部开发域名配置
查看>>
读写分离提高 SQL Server 并发性
查看>>
基于终端的日志工具logview
查看>>
WPF和Expression Blend开发实例:Adorner(装饰器)应用实例
查看>>
Kafka集群搭建
查看>>
【Error】centos7 minimal connect: Network is unreachable
查看>>
类型系统:类型检查、类型转换、任意类型-强类型、类型转换
查看>>
WPF StringFormat 格式化文本
查看>>
RESTful API 最佳实践
查看>>
阿里如何实现100%容器化镜像化?八年技术演进之路回顾(转)
查看>>
DirectX using C++_error X3539:ps1_x is no longer supported...解决方案
查看>>
在Microsoft Expression Blend 2 中绘制圆角矩形按钮
查看>>
WPF 线程:使用调度程序构建反应速度更快的应用程序
查看>>
wepy - 转换成h5
查看>>