fabric 共识模块解读

本文详细探讨了Fabric共识模块的工作流程,重点在于如何添加新的共识算法,如Raft。主要内容包括共识接口的实现,以及Order接口如何与Raft算法交互,通过proposeC和commitC通道进行消息传递,并在chain.start()中生成区块写入账本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要分析fabric共识模块的流程,方便自己添加新的共识模块。

主要涉及的函数和调用流程如下图所示:

fabricConsenter

一般来讲,如果要添加新的共识算法,主要实现这个接口:

type Chain interface {
    // NOTE: The kafka consenter has not been updated to perform the revalidation
    // checks conditionally.  For now, Order/Configure are essentially Enqueue as before.
    // This does not cause data inconsistency, but it wastes cycles and will be required
    // to properly support the ConfigUpdate concept once introduced
    // Once this is done, the MsgClassification logic in msgprocessor should return error
    // for non ConfigUpdate/Normal msg types

    // Order accepts a message which has been processed at a given configSeq.
    // If the configSeq advances, it is the responsibility of the consenter
    // to revalidate and potentially discard the message
    // The consenter may return an error, indicating the message was not accepted
    Order(env *cb.Envelope, configSeq uint64) error

    // Configure accepts a message which reconfigures the channel and will
    // trigger an update to the configSeq if committed.  The configuration must have
    // been triggered by a ConfigUpdate message. If the config sequence advances,
    // it is the responsibility of the consenter to recompute the resulting config,
    // discarding the message if the reconfiguration is no longer valid.
    // The consenter may return an error, indicating the message was not accepted
    Configure(config *cb.Envelope, configSeq uint64) error

    // WaitReady blocks waiting for consenter to be ready for accepting new messages.
    // This is useful when consenter needs to temporarily block ingress messages so
    // that in-flight messages can be consumed. It could return error if consenter is
    // in erroneous states. If this blocking behavior is not desired, consenter could
    // simply return nil.
    WaitReady() error

    // Errored returns a channel which will close when an error has occurred.
    // This is especially useful for the Deliver client, who must terminate waiting
    // clients when the consenter is not up to date.
    Errored() <-chan struct{}

    // Start should allocate whatever resources are needed for staying up to date with the chain.
    // Typically, this involves creating a thread which reads from the ordering source, passes those
    // messages to a block cutter, and writes the resulting blocks to the ledger.
    Start()

    // Halt frees the resources which were allocated for this Chain.
    Halt()
}

例如要添加raft共识算法: 一般来讲order接口把peer发送过来的envelope送到raft的proposeC 这个channel里,经过raft共识后,从commitC 这个channel里取出共识后的entry,然后再chain.start()里把消息进行Cut(),然后生成block并写入ledger.

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值