HyperLegerFabricSDK開發(fā)(七)——ledger

HyperLeger Fabric SDK開發(fā)(七)——ledger

一、ledger簡介

1、ledger簡介

ledger包支持在Fabric網(wǎng)絡(luò)上的指定通道上啟用賬本查詢。如果應(yīng)用程序需要對多個通道進行賬本查詢,需要為每個通道的賬本客戶端創(chuàng)建一個單獨實例。賬本客戶端支持以下查詢:QueryInfo,QueryBlock,QueryBlockByHash,QueryBlockByTxID,QueryTransaction和QueryConfig。
官方文檔:
https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/client/ledger

站在用戶的角度思考問題,與客戶深入溝通,找到漠河網(wǎng)站設(shè)計與漠河網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、主機域名、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋漠河地區(qū)。

2、ledger使用流程

ledger使用的基本流程如下:
A、準備通道上下文
B、創(chuàng)建分類帳客戶端
C、查詢分類帳
使用示例:

ctx := mockChannelProvider("mychannel")

c, err := New(ctx)
if err != nil {
    fmt.Println("failed to create client")
}

block, err := c.QueryBlock(1)
if err != nil {
    fmt.Printf("failed to query block: %s\n", err)
}

if block != nil {
    fmt.Println("Retrieved block #1")
}
// output:
// Retrieved block #1      

二、ledger常用接口

1、獲取賬本客戶端實例

type Client struct {
   ctx       context.Channel
   filter    fab.TargetFilter
   ledger    *channel.Ledger
   verifier  channel.ResponseVerifier
   discovery fab.DiscoveryService
}
func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error) 

New返回賬本客戶端實例。賬本客戶端實例提供處理程序以查詢指定通道上的各種信息。如果應(yīng)用程序需要對多個通道進行賬本查詢,需要為每個通道的賬本客戶端創(chuàng)建一個單獨實例。賬本客戶端僅支持指定查詢。
使用示例:

ctx := mockChannelProvider("mychannel")

c, err := New(ctx)
if err != nil {
    fmt.Println(err)
}

if c != nil {
    fmt.Println("ledger client created")
}
// output:
// ledger client created        

2、查詢賬本

func (c *Client) QueryBlock(blockNumber uint64, options ...RequestOption) (*common.Block, error)
QueryBlock根據(jù)區(qū)塊編號查詢區(qū)塊的賬本。
參數(shù):
blockNumber是必需的區(qū)塊號
options包含可選的請求選項
返回區(qū)塊信息
使用示例:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

block, err := c.QueryBlock(1)
if err != nil {
    fmt.Printf("failed to query block: %s\n", err)
}

if block != nil {
    fmt.Println("Retrieved block #1")
}
// output:
// Retrieved block #1  

func (c *Client) QueryBlockByHash(blockHash []byte, options ...RequestOption) (*common.Block, error)
QueryBlockByHash根據(jù)區(qū)塊hash查詢區(qū)塊賬本。
參數(shù):
blockHash是區(qū)塊哈希
options包含可選的請求選項
返回區(qū)塊信息
使用示例:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

block, err := c.QueryBlockByHash([]byte("hash"))
if err != nil {
    fmt.Printf("failed to query block by hash: %s\n", err)
}

if block != nil {
    fmt.Println("Retrieved block by hash")
}
// output:
// Retrieved block by hash   

func (c *Client) QueryBlockByTxID(txID fab.TransactionID, options ...RequestOption) (*common.Block, error)
QueryBlockByTxID查詢包含交易的區(qū)塊
參數(shù):
txID是交易ID
options包含可選的請求選項
返回區(qū)塊信息
使用示例:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

block, err := c.QueryBlockByTxID("123")
if err != nil {
    fmt.Printf("failed to query block by transaction ID: %s\n", err)
}

if block != nil {
    fmt.Println("Retrieved block by transaction ID")
}
// output:
// Retrieved block by transaction ID   

3、查詢通道配置

func (c *Client) QueryConfig(options ...RequestOption) (fab.ChannelCfg, error)
QueryConfig查詢通道配置。
參數(shù):
options包含可選的請求選項
返回通道配置信息
使用示例:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

cfg, err := c.QueryConfig(WithTargets(mockPeerWithConfigBlock()))
if err != nil {
    fmt.Printf("failed to query config: %s\n", err)
}

if cfg != nil {
    fmt.Println("Retrieved channel configuration")
}
// output:
// Retrieved channel configuration   

4、查詢通道信息

func (c *Client) QueryInfo(options ...RequestOption) (*fab.BlockchainInfoResponse, error)
QueryInfo查詢此通道上的各種區(qū)塊鏈信息,例如區(qū)塊高度和當前區(qū)塊哈希。
參數(shù):
options是可選的請求選項
返回區(qū)塊鏈信息
使用示例:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

bci, err := c.QueryInfo()
if err != nil {
    fmt.Printf("failed to query for blockchain info: %s\n", err)
}

if bci != nil {
    fmt.Println("Retrieved ledger info")
}
// output:
// Retrieved ledger info  

5、查詢交易信息

func (c *Client) QueryTransaction(transactionID fab.TransactionID, options ...RequestOption) (*pb.ProcessedTransaction, error)
QueryTransaction通過交易ID查詢賬本上的已處理交易。
參數(shù):
txID是必需的交易ID
options包含可選的請求選項
返回已經(jīng)處理交易的信息
使用示例:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

t, err := c.QueryTransaction("123")
if err != nil {
    fmt.Printf("failed to query transaction: %s\n", err)
}

if t != nil {
    fmt.Println("Retrieved transaction")
}
// output:
// Retrieved transaction  

6、為客戶端設(shè)置過濾器

type ClientOption func(*Client) error

// WithDefaultTargetFilter option to configure new
func WithDefaultTargetFilter(filter fab.TargetFilter) ClientOption 
使用示例:
ctx := mockChannelProvider("mychannel")

c, err := New(ctx, WithDefaultTargetFilter(&urlTargetFilter{url: "example.com"}))
if err != nil {
    fmt.Println(err)
}

if c != nil {
    fmt.Println("ledger client created with url target filter")
}
// output:
// ledger client created with url target filter   

7、RequestOption參數(shù)構(gòu)建

type RequestOption func(ctx context.Client, opts *requestOptions) error

//requestOptions contains options for operations performed by LedgerClient
type requestOptions struct {
   Targets       []fab.Peer                        // target peers
   TargetFilter  fab.TargetFilter                  // target filter
   MaxTargets    int                               // maximum number of targets to select
   MinTargets    int                               // min number of targets that have to respond with no error (or agree on result)
   Timeouts      map[fab.TimeoutType]time.Duration //timeout options for ledger query operations
   ParentContext reqContext.Context                //parent grpc context for ledger operations
}
func WithMaxTargets(maxTargets int) RequestOption

WithMaxTargets指定每個請求選擇的最大目標數(shù)。最大目標數(shù)的默認值為1.
func WithMinTargets(minTargets int) RequestOption
WithMinTargets指定必須響應(yīng)且沒有錯誤(或同意結(jié)果)的最小目標數(shù)。 最小目標數(shù)的默認值為1。
func WithParentContext(parentContext reqContext.Context) RequestOption
WithParentContext封裝了grpc父上下文
使用示例:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println(err)
}

channelContext, err := mockChannelProvider("mychannel")()
if err != nil {
    fmt.Println("failed to return channel context")
    return
}

// get parent context and cancel
parentContext, cancel := sdkCtx.NewRequest(channelContext, sdkCtx.WithTimeout(20*time.Second))
defer cancel()

bci, err := c.QueryInfo(WithParentContext(parentContext))
if err != nil {
    fmt.Printf("failed to query for blockchain info: %s\n", err)
}

if bci != nil {
    fmt.Println("Retrieved blockchain info")
}
// output:
// Retrieved blockchain info      

func WithTargetEndpoints(keys ...string) RequestOption
withTargetEndpoints允許每個請求覆蓋目標Peer節(jié)點。目標由名稱或URL指定,SDK將創(chuàng)建底層Peer節(jié)點對象。
func WithTargetFilter(targetFilter fab.TargetFilter) RequestOption
WithTargetFilter指定每個請求的目標Peer節(jié)點過濾器。
使用示例:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println(err)
}

block, err := c.QueryBlock(1, WithTargetFilter(&urlTargetFilter{url: "example.com"}))
if err != nil {
    fmt.Printf("failed to query block: %s\n", err)
}

if block != nil {
    fmt.Println("Retrieved block #1 from example.com")
}
// output:
// Retrieved block #1 from example.com

func WithTargets(targets ...fab.Peer) RequestOption
WithTargets允許每個請求覆蓋目標Peer節(jié)點。
使用示例:

c, err := New(mockChannelProvider("mychannel"))
if err != nil {
    fmt.Println("failed to create client")
}

cfg, err := c.QueryConfig(WithTargets(mockPeerWithConfigBlock()))
if err != nil {
    fmt.Printf("failed to query config with target peer: %s\n", err)
}

if cfg != nil {
    fmt.Println("Retrieved config from target peer")
}
// output:
// Retrieved config from target peer 

func WithTimeout(timeoutType fab.TimeoutType, timeout time.Duration) RequestOption
WithTimeout封裝了超時類型、超時時間的鍵值對到Options選項,次選項用于QueryInfo,QueryBlock,QueryBlockByHash,QueryBlockByTxID,QueryTransaction,QueryConfig等函數(shù)。

三、ledger示例

var (
   sdk           *fabsdk.FabricSDK
   channelName   = "assetchannel"
   org           = "org1"
   user          = "Admin"
)

// 賬本查詢
func queryBlockchain() {
   ctx := sdk.ChannelContext(channelName, fabsdk.WithOrg(org), fabsdk.WithUser(user))

   cli, err := ledger.New(ctx)
   if err != nil {
      panic(err)
   }

   resp, err := cli.QueryInfo(ledger.WithTargetEndpoints("peer0.org1.example.com"))
   if err != nil {
      panic(err)
   }

   fmt.Println(resp)

   // 1
   cli.QueryBlockByHash(resp.BCI.CurrentBlockHash)

   // 2
   for i := uint64(0); i <= resp.BCI.Height; i++ {
      cli.QueryBlock(i)
   }
}

當前標題:HyperLegerFabricSDK開發(fā)(七)——ledger
文章路徑:http://bm7419.com/article20/igscjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、響應(yīng)式網(wǎng)站外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計、企業(yè)建站網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)