Fabric 1.0原始碼分析(37) Peer #DeliverClient(Deliver客戶端)

尹成發表於2018-05-20
# Fabric 1.0原始碼筆記 之 Peer #DeliverClient(Deliver客戶端)

## 1、DeliverClient概述

DeliverClient程式碼分佈如下:

* peer/channel/deliverclient.go,deliverClientIntf介面定義及實現,以及DeliverClient工具函式。
* protos/orderer/ab.pb.go,AtomicBroadcast_DeliverClient介面定義和實現。

## 2、deliverClientIntf介面定義及實現

### 2.1、DeliverClient工具函式

```go
//構造deliverClient
func newDeliverClient(conn *grpc.ClientConn, client ab.AtomicBroadcast_DeliverClient, chainID string) *deliverClient
//程式碼在peer/channel/deliverclient.go
```

### 2.2、deliverClientIntf介面定義及實現

```go
type deliverClientIntf interface {
    getSpecifiedBlock(num uint64) (*common.Block, error)
    getOldestBlock() (*common.Block, error)
    getNewestBlock() (*common.Block, error)
    Close() error
}

type deliverClient struct {
    conn *grpc.ClientConn
    client ab.AtomicBroadcast_DeliverClient
    chainID string
}

//構造查詢Envelope
func seekHelper(chainID string, position *ab.SeekPosition) *common.Envelope
//r.client.Send(seekHelper(r.chainID, &ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: blockNumber}}}))
func (r *deliverClient) seekSpecified(blockNumber uint64) error
//r.client.Send(seekHelper(r.chainID, &ab.SeekPosition{Type: &ab.SeekPosition_Oldest{Oldest: &ab.SeekOldest{}}}))
func (r *deliverClient) seekOldest() error
//return r.client.Send(seekHelper(r.chainID, &ab.SeekPosition{Type: &ab.SeekPosition_Newest{Newest: &ab.SeekNewest{}}}))
func (r *deliverClient) seekNewest() error
//r.client.Recv()讀取塊
func (r *deliverClient) readBlock() (*common.Block, error)
//r.seekSpecified(num)和r.readBlock()
func (r *deliverClient) getSpecifiedBlock(num uint64) (*common.Block, error)
//r.seekOldest()和r.readBlock()
func (r *deliverClient) getOldestBlock() (*common.Block, error)
//r.seekNewest()和r.readBlock()
func (r *deliverClient) getNewestBlock() (*common.Block, error)
//r.conn.Close()
func (r *deliverClient) Close() error
//cf.DeliverClient.getSpecifiedBlock(0)獲取創世區塊
func getGenesisBlock(cf *ChannelCmdFactory) (*common.Block, error)
//程式碼在peer/channel/deliverclient.go
```

func seekHelper(chainID string, position *ab.SeekPosition) *common.Envelope程式碼如下:

```go
func seekHelper(chainID string, position *ab.SeekPosition) *common.Envelope {
    seekInfo := &ab.SeekInfo{
        Start: position,
        Stop: position,
        Behavior: ab.SeekInfo_BLOCK_UNTIL_READY,
    }

    msgVersion := int32(0)
    epoch := uint64(0)
    env, err := utils.CreateSignedEnvelope(common.HeaderType_CONFIG_UPDATE, chainID, localmsp.NewSigner(), seekInfo, msgVersion, epoch)
    return env
}
//程式碼在peer/channel/deliverclient.go
```





網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN





網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN

相關文章