<optgroup id="6y7f6"><small id="6y7f6"></small></optgroup>

<code id="6y7f6"></code>

  • <p id="6y7f6"><tbody id="6y7f6"><ins id="6y7f6"></ins></tbody></p>
    <code id="6y7f6"><form id="6y7f6"></form></code>

      Fabric2.x中Raft共識算法核心數據結構

      一、共識算法可插拔的代碼體現Chain接口

      Hyperledger Fabric的共識算法是可插拔的,在代碼上體現為Chain接口,所有不同的共識算法均可根據Chain接口進行具體實現,目前fabric支持solo、kafka、raft、sbft等共識算法。Chain接口的代碼在fabric/orderer/consensus/consensus.go中

      // Chain定義了為ordering注入messages的方式,這種設計允許如下兩種流程:
      // 1.消息被排序為流,流被切割成塊,塊被提交(solo,kafka)
      // 2.消息被切成塊,塊被排序,然后塊被提交(sbft)
      type Chain interface {
      	// 排序函數,env是交易信息,configSeq是自增序號,每次有配置更新時都會+1,以確保交易是在最新的config下進行
      	Order(env *cb.Envelope, configSeq uint64) error
      
      	// 處理配置交易,提交后更新configSeq
      	Configure(config *cb.Envelope, configSeq uint64) error
      
      	// 超過max in-flight限制的消息會被阻塞,等待in-flight先執行完
      	WaitReady() error
      
      	// 當發生error時,會返回一個空channel
      	Errored() <-chan struct{}
      
      	// 分配與Chain保持最新數據的資源
      	Start()
      
      	// 刷新分配給Chain的資源
      	Halt()
      }
      

      二、Fabric的raft共識算法Chain結構體

      Hyperledger Fabric對Raft算法的核心實現代碼都是放在fabric/orderer/consensus/etcdraft包下的,這里主要包含幾個核心的數據結構,即Chain結構體和node結構體。

      Chain結構體實現了Chain接口,代碼位于orderer/consensus/etcdraft/chain.go,它里面主要定義了一些通道(channel)用于節點間的通信,以便根據通信消息做相應的操作。
      // Chain implements consensus.Chain interface.
      type Chain struct {
         configurator Configurator
         rpc RPC // 節點與外部節點進行通信的對象,RPC 是一個接口,包含兩個方法SendConsensus 和 SendSubmit。前面這種用于節點間 raft 信息的通訊,后者用于轉發交易請求給 leader 節點。
         raftID    uint64
         channelID string
         lastKnownLeader uint64
         ActiveNodes     atomic.Value
         submitC  chan *submit // 接收 Orderer 客戶端提交的共識請求消息的通道
         applyC   chan apply // 接收 raft 節點間應用消息的通道
         observeC chan<- raft.SoftState
         haltC    chan struct{}         
         doneC    chan struct{} 
         startC   chan struct{} 
         snapC    chan *raftpb.Snapshot //接收 raft 節點快照數據的通道
         gcC      chan *gc 
         …
         Node *node // fabric封裝了底層 etcdraft 庫的節點實例
         …
      }
      

      三、Fabric的raft共識算法node結構體

      node結構體封裝了底層ercdraft庫的節點實例,代碼位于orderer/consensus/etcdraft/node.go,主要用于將Fabric自己實現的Raft上層應用和etcd的底層Raft實現連接起來,可以說node結構體是它們之間通信的橋梁,正是它的存在屏蔽了Raft實現的細節。

      type node struct {
      	chainID string
      	logger  *flogging.FabricLogger
      	metrics *Metrics
      	unreachableLock sync.RWMutex
      	unreachable     map[uint64]struct{}
      	tracker *Tracker
      	storage *RaftStorage
      	config  *raft.Config
      	rpc RPC
      	chain *Chain // fabric自己定義的Chain結構體,具體在orderer/consensus/etcdraft/chain.go中
      	tickInterval time.Duration
      	clock        clock.Clock
      	metadata *etcdraft.BlockMetadata
      	subscriberC chan chan uint64
      	raft.Node // raft底層的Node接口
      }
      

      四、小結

      Fabric的raft共識算法通過Chain結構體實現可插拔的共識算法Chain接口;又通過node結構體,實現對etcdraft底層算法實現細節的封裝。Chain結構體和node結構體是承上啟下的一層,fabirc的raft共識算法啟動、交易處理流程均圍繞上述兩個結構體進行。下一篇博客將圍繞fabric的raft共識算法啟動流程進行分析。

      posted @ 2023-02-04 17:50  JasonCeng  閱讀(131)  評論(0編輯  收藏  舉報
      欧洲黄色网页链接入口,免费A级毛片无码无遮挡久久影院,a免费黄色网址,国产一级黄色的网站
      <optgroup id="6y7f6"><small id="6y7f6"></small></optgroup>

      <code id="6y7f6"></code>

    1. <p id="6y7f6"><tbody id="6y7f6"><ins id="6y7f6"></ins></tbody></p>
      <code id="6y7f6"><form id="6y7f6"></form></code>