淘先锋技术网

首页 1 2 3 4 5 6 7

一. pkg/client/ledger ledger包在Fabric网络上的指定通道上启用分类帐查询。 需要来自多个渠道的分类帐查询的应用程序应为每个渠道创建分类帐客户端的单独实例。 Ledger客户端支持以下查询:QueryInfo,QueryBlock,QueryBlockByHash,QueryBlockByTxID,QueryTransaction和QueryConfig。

  1.  基本流程:

    1)准备频道上下文
    2)创建分类帐客户端
    3)查询分类帐

     1 ctx := mockChannelProvider("mychannel")
     2 
     3 c, err := New(ctx)
     4 if err != nil {
     5     fmt.Println("failed to create client")
     6 }
     7 
     8 block, err := c.QueryBlock(1)
     9 if err != nil {
    10     fmt.Printf("failed to query block: %s\n", err)
    11 }
    12 
    13 if block != nil {
    14     fmt.Println("Retrieved block #1")
    15 }
    View Code

    输出:Retrieved block #1

  2. 类型Client
    1. type Client struct {// contains filtered or unexported fields}: 客户端在Fabric网络上启用分类帐查询
    2. func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error): New返回分类帐客户端实例。 分类帐客户端实例提供处理程序以查询指定通道上的各种信息。 需要与多个通道交互的应用程序应为每个通道创建一个单独的分类帐客户端实例。 Ledger客户端仅支持特定查询。
      1. 例:
         1 ctx := mockChannelProvider("mychannel")
         2 
         3 c, err := New(ctx)
         4 if err != nil {
         5     fmt.Println(err)
         6 }
         7 
         8 if c != nil {
         9     fmt.Println("ledger client created")
        10 }
        View Code

        输出:ledger client created

    3. func (c *Client) QueryBlock(blockNumber uint64, options ...RequestOption) (*common.Block, error): QueryBlock按块编号查询分类帐。
      1. 参数:

        blockNumber是必需的块号(ID)
        options包含可选的请求选项

        返回:
        区块信息

      2. 例:
         1 c, err := New(mockChannelProvider("mychannel"))
         2 if err != nil {
         3     fmt.Println("failed to create client")
         4 }
         5 
         6 block, err := c.QueryBlock(1)
         7 if err != nil {
         8     fmt.Printf("failed to query block: %s\n", err)
         9 }
        10 
        11 if block != nil {
        12     fmt.Println("Retrieved block #1")
        13 }
        View Code

        输出:Retrieved block #1

    4. func (c *Client) QueryBlockByHash(blockHash []byte, options ...RequestOption) (*common.Block, error): QueryBlockByHash能过块hash查询分类帐。
      1. 参数:

        blockHash是必需的块哈希
        options包含可选的请求选项

        返回:
        区块信息

      2. 例:
         1 c, err := New(mockChannelProvider("mychannel"))
         2 if err != nil {
         3     fmt.Println("failed to create client")
         4 }
         5 
         6 block, err := c.QueryBlockByHash([]byte("hash"))
         7 if err != nil {
         8     fmt.Printf("failed to query block by hash: %s\n", err)
         9 }
        10 
        11 if block != nil {
        12     fmt.Println("Retrieved block by hash")
        13 }
        View Code

        输出:Retrieved block by hash

    5. func (c *Client) QueryBlockByTxID(txID fab.TransactionID, options ...RequestOption) (*common.Block, error): QueryBlockByTxID查询包含事务的块。
      1. 参数:

        txID是必需的事务ID
        options包含可选的请求选项

        返回:
        区块信息

      2. 例:
         1 c, err := New(mockChannelProvider("mychannel"))
         2 if err != nil {
         3     fmt.Println("failed to create client")
         4 }
         5 
         6 block, err := c.QueryBlockByTxID("123")
         7 if err != nil {
         8     fmt.Printf("failed to query block by transaction ID: %s\n", err)
         9 }
        10 
        11 if block != nil {
        12     fmt.Println("Retrieved block by transaction ID")
        13 }
        View Code

        输出:Retrieved block by transaction ID

    6. func (c *Client) QueryConfig(options ...RequestOption) (fab.ChannelCfg, error): QueryConfig查询通道配置。
      1. 参数:

        options包含可选的请求选项

        返回:
        通道配置信息

      2. 例:
         1 c, err := New(mockChannelProvider("mychannel"))
         2 if err != nil {
         3     fmt.Println("failed to create client")
         4 }
         5 
         6 cfg, err := c.QueryConfig(WithTargets(mockPeerWithConfigBlock()))
         7 if err != nil {
         8     fmt.Printf("failed to query config: %s\n", err)
         9 }
        10 
        11 if cfg != nil {
        12     fmt.Println("Retrieved channel configuration")
        13 }
        View Code

        输出:Retrieved channel configuration

    7. func (c *Client) QueryInfo(options ...RequestOption) (*fab.BlockchainInfoResponse, error): QueryInfo查询此通道上的各种有用区块链信息,例如块高度和当前块哈希。
      1. 参数:

        options是可选的请求选项

        返回:
        区块链信息

      2. 例:
         1 c, err := New(mockChannelProvider("mychannel"))
         2 if err != nil {
         3     fmt.Println("failed to create client")
         4 }
         5 
         6 bci, err := c.QueryInfo()
         7 if err != nil {
         8     fmt.Printf("failed to query for blockchain info: %s\n", err)
         9 }
        10 
        11 if bci != nil {
        12     fmt.Println("Retrieved ledger info")
        13 }
        View Code

        输出:Retrieved ledger info

    8. func (c *Client) QueryTransaction(transactionID fab.TransactionID, options ...RequestOption) (*pb.ProcessedTransaction, error): QueryTransaction通过事务ID查询分类帐以处理事务。
      1. 参数:

        txID是必需的事务ID
        options包含可选的请求选项

        返回:
        处理的交易信息

      2. 例:
         1 c, err := New(mockChannelProvider("mychannel"))
         2 if err != nil {
         3     fmt.Println("failed to create client")
         4 }
         5 
         6 t, err := c.QueryTransaction("123")
         7 if err != nil {
         8     fmt.Printf("failed to query transaction: %s\n", err)
         9 }
        10 
        11 if t != nil {
        12     fmt.Println("Retrieved transaction")
        13 }
        View Code

        输出:Retrieved transaction

  3. 类型ClientOption
    1. type ClientOption func(*Client) error: ClientOption描述了New构造函数的功能参数
  4. func WithDefaultTargetFilter(filter fab.TargetFilter) ClientOption: WithDefaultTargetFilter选项用于配置新的
    1. 例:
       1 ctx := mockChannelProvider("mychannel")
       2 
       3 c, err := New(ctx, WithDefaultTargetFilter(&urlTargetFilter{url: "example.com"}))
       4 if err != nil {
       5     fmt.Println(err)
       6 }
       7 
       8 if c != nil {
       9     fmt.Println("ledger client created with url target filter")
      10 }
      View Code

      输出:ledger client created with url target filter

  5. 类型RequestOption
    1. type RequestOption func(ctx context.Client, opts *requestOptions) error: 每个requestOptions参数的RequestOption func
  6. func WithMaxTargets(maxTargets int) RequestOption: WithMaxTargets指定每个请求选择的最大目标数。 最大目标数的默认值为1。
  7. func WithMinTargets(minTargets int) RequestOption: WithMinTargets指定必须响应且没有错误(或同意结果)的最小目标数。 最小目标数的默认值为1。
  8. func WithParentContext(parentContext reqContext.Context) RequestOption: WithParentContext封装了grpc父上下文
    1. 例:
       1 c, err := New(mockChannelProvider("mychannel"))
       2 if err != nil {
       3     fmt.Println(err)
       4 }
       5 
       6 channelContext, err := mockChannelProvider("mychannel")()
       7 if err != nil {
       8     fmt.Println("failed to return channel context")
       9     return
      10 }
      11 
      12 // get parent context and cancel
      13 parentContext, cancel := sdkCtx.NewRequest(channelContext, sdkCtx.WithTimeout(20*time.Second))
      14 defer cancel()
      15 
      16 bci, err := c.QueryInfo(WithParentContext(parentContext))
      17 if err != nil {
      18     fmt.Printf("failed to query for blockchain info: %s\n", err)
      19 }
      20 
      21 if bci != nil {
      22     fmt.Println("Retrieved blockchain info")
      23 }
      View Code

      输出:Retrieved blockchain info

  9. func WithTargetEndpoints(keys ...string) RequestOption: WithTargetEndpoints允许每个请求覆盖目标peer。 目标由名称或URL指定,SDK将创建基础对等对象。
  10. func WithTargetFilter(targetFilter fab.TargetFilter) RequestOption: WithTargetFilter指定每个请求的目标对等过滤器。
    1. 例:
       1 c, err := New(mockChannelProvider("mychannel"))
       2 if err != nil {
       3     fmt.Println(err)
       4 }
       5 
       6 block, err := c.QueryBlock(1, WithTargetFilter(&urlTargetFilter{url: "example.com"}))
       7 if err != nil {
       8     fmt.Printf("failed to query block: %s\n", err)
       9 }
      10 
      11 if block != nil {
      12     fmt.Println("Retrieved block #1 from example.com")
      13 }
      View Code

      输出:Retrieved block #1 from example.com

  11. func WithTargets(targets ...fab.Peer) RequestOption: WithTargets允许每个请求覆盖目标对等体。
    1. 例:
       1 c, err := New(mockChannelProvider("mychannel"))
       2 if err != nil {
       3     fmt.Println("failed to create client")
       4 }
       5 
       6 cfg, err := c.QueryConfig(WithTargets(mockPeerWithConfigBlock()))
       7 if err != nil {
       8     fmt.Printf("failed to query config with target peer: %s\n", err)
       9 }
      10 
      11 if cfg != nil {
      12     fmt.Println("Retrieved config from target peer")
      13 }
      View Code

      输出:Retrieved config from target peer

  12. func WithTimeout(timeoutType fab.TimeoutType, timeout time.Duration) RequestOption: WithTimeout封装了超时类型的键值对,QueryInfo,QueryBlock,QueryBlockByHash,QueryBlockByTxID,QueryTransaction,QueryConfig函数的Options的超时持续时间

转载于:https://www.cnblogs.com/apolov-fabric/p/9719318.html