zoukankan      html  css  js  c++  java
  • pkg/client/channel channel包

    一. pkg/client/channel channel包:通过包channel可以访问Fabric网络上的频道。 频道客户端实例提供处理程序以与指定频道上的peer交互。 频道客户端可以查询链码,执行链码以及注册/取消注册特定频道上的链码事件。 需要与多个频道交互的应用程序应为每个频道创建一个单独的频道客户端实例。

    1. 基本流程
      1. 准备频道客户端上下文
      2. 创建频道客户端
      3. 执行链码
      4. 查询链码
         1 c, err := New(mockChannelProvider("mychannel"))
         2 if err != nil {
         3     fmt.Println("failed to create client")
         4 }
         5 
         6 response, err := c.Query(Request{ChaincodeID: "testCC", Fcn: "invoke", Args: [][]byte{[]byte("query"), []byte("data")}})
         7 if err != nil {
         8     fmt.Printf("failed to query chaincode: %s
        ", err)
         9 }
        10 
        11 fmt.Println(string(response.Payload))
        View Code

        输出:abc

    2. 类型client
      1. type Client struct { // contains filtered or unexported fields }:客户端可以访问Fabric网络上的通道。通道客户端实例提供处理程序以与指定通道上的对等方交互。 需要与多个通道交互的应用程序应为每个通道创建一个单独的通道客户端实例。 通道客户端仅支持非管理功能。
      2. func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error): New返回Client实例。 通道客户端可以查询链码,执行链码以及注册/取消注册特定通道上的链码事件。
        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("channel client created")
          10 } else {
          11     fmt.Println("channel client is nil")
          12 }
          View Code

          输出:channel client created

      3. func (cc *Client) Execute(request Request, options ...RequestOption) (Response, error): 执行使用请求和可选请求选项准备并执行事务
        1. 参数: 

          请求包含有关强制链代码ID和功能的信息
          options包含可选的请求选项

          返回:
          来自peer的提案回复

        2.  1 c, err := New(mockChannelProvider("mychannel"))
           2 if err != nil {
           3     fmt.Println("failed to create client")
           4 }
           5 
           6 _, err = c.Execute(Request{ChaincodeID: "testCC", Fcn: "invoke", Args: [][]byte{[]byte("move"), []byte("a"), []byte("b"), []byte("1")}})
           7 if err != nil {
           8     fmt.Println(err.Error())
           9 }
          10 
          11 fmt.Println("Chaincode transaction completed")
          View Code

          输出:Chaincode transaction completed

      4. func (cc *Client) InvokeHandler(handler invoke.Handler, request Request, options ...RequestOption) (Response, error): InvokeHandler使用提供的请求和可选请求选项来调用处理程序
        1. 参数:

          要调用的处理程序
          请求包含有关强制链代码ID和功能的信息
          options包含可选的请求选项

          返回:
          来自peer的提案回复

        2. 例:
           1 c, err := New(mockChannelProvider("mychannel"))
           2 if err != nil {
           3     fmt.Println("failed to create client")
           4 }
           5 
           6 response, err := c.InvokeHandler(&exampleHandler{}, Request{ChaincodeID: "testCC", Fcn: "invoke", Args: [][]byte{[]byte("query"), []byte("data")}})
           7 if err != nil {
           8     fmt.Printf("failed to query chaincode: %s
          ", err)
           9 }
          10 
          11 fmt.Println(string(response.Payload))
          View Code

          输出:custom

      5. func (cc *Client) Query(request Request, options ...RequestOption) (Response, error): 使用请求和可选请求选项查询链代码
        1. 参数:

          请求包含有关强制链代码ID和功能的信息
          options包含可选的请求选项

          返回:
          来自peer的提案回复

        2. 例:
           1 c, err := New(mockChannelProvider("mychannel"))
           2 if err != nil {
           3     fmt.Println("failed to create client")
           4 }
           5 
           6 response, err := c.Query(Request{ChaincodeID: "testCC", Fcn: "invoke", Args: [][]byte{[]byte("query"), []byte("b")}})
           7 if err != nil {
           8     fmt.Printf("failed to query chaincode: %s
          ", err)
           9 }
          10 
          11 if len(response.Payload) > 0 {
          12     fmt.Println("chaincode query success")
          13 }
          View Code

          输出:chaincode query success

      6. func (cc *Client) RegisterChaincodeEvent(chainCodeID string, eventFilter string) (fab.Registration, <-chan *fab.CCEvent, error): RegisterChaincodeEvent注册链代码事件。 不再需要注册时,必须调用取消注册。
        1. 参数:

          chaincodeID是要为其接收事件的链代码ID
          eventFilter是要为其接收事件的chaincode事件过滤器(正则表达式)

          返回:
          注册和用于接收事件的频道。 调用取消注册时,通道将关闭。

        2. 例:
           1 c, err := New(mockChannelProvider("mychannel"))
           2 if err != nil {
           3     fmt.Println("failed to create client")
           4 }
           5 
           6 registration, _, err := c.RegisterChaincodeEvent("examplecc", "event123")
           7 if err != nil {
           8     fmt.Println("failed to register chaincode event")
           9 }
          10 defer c.UnregisterChaincodeEvent(registration)
          11 
          12 fmt.Println("chaincode event registered successfully")
          View Code

          输出:chaincode event registered successfully

      7. func (cc *Client) UnregisterChaincodeEvent(registration fab.Registration): UnregisterChaincodeEvent删除给定的链码事件注册并关闭事件通道。
        1. 参数:registration是从RegisterChaincodeEvent方法返回的注册句柄
    3. 类型ClientOption
      1. type ClientOption func(*Client) error: ClientOption描述了New构造函数的功能参数
    4. 类型Request
      1. type Request struct {
            ChaincodeID  string
            Fcn          string
            Args         [][]byte
            TransientMap map[string][]byte

        // InvocationChain包含一些选择服务实现使用的元数据
        //选择符合所有连锁码背书政策的背书人
        //在调用链中(例如,对于cc到cc的调用)。
        //每个链码也可能与一组私有数据收集名相关联
        //一些选择服务(如e.g.fabric选择)使用这些服务来排除背书人
        //没有对集合的读访问权。
        //被调用的链码(由链码id指定)可以选择添加到调用中
        //链连同任何集合,否则它可能被省略。

            InvocationChain []*fab.ChaincodeCall
        }: Request包含查询和执行调用事务的参数
    5. 类型RequestOption
      1. type RequestOption func(ctx context.Client, opts *requestOptions) error: 每个Opts参数的RequestOption func
      2. func WithBeforeRetry(beforeRetry retry.BeforeRetryHandler) RequestOption: WithBeforeRetry指定在重试尝试之前调用的函数
      3. func WithChaincodeFilter(ccFilter invoke.CCFilter) RequestOption: WithChaincodeFilter添加了一个链代码过滤器,用于计算额外的背书人
      4. func WithParentContext(parentContext reqContext.Context) RequestOption: WithParentContext封装了grpc父上下文
      5. func WithRetry(retryOpt retry.Opts) RequestOption: WithRetry选项用于配置重试
      6. func WithTargetEndpoints(keys ...string) RequestOption: WithTargetEndpoints允许覆盖请求的目标peer。 目标由名称或URL指定,SDK将创建基础peer对象。
      7. func WithTargetFilter(filter fab.TargetFilter) RequestOption: WithTargetFilter指定每请求目标peer过滤器
      8. func WithTargets(targets ...fab.Peer) RequestOption: WithTargets允许覆盖请求的目标peer
      9. func WithTimeout(timeoutType fab.TimeoutType, timeout time.Duration) RequestOption: WithTimeout封装了超时类型的键值对,即Options的超时持续时间
    6. 类型Response
      1. type Response struct {
            Proposal         *fab.TransactionProposal
            Responses        []*fab.TransactionProposalResponse
            TransactionID    fab.TransactionID
            TxValidationCode pb.TxValidationCode
            ChaincodeStatus  int32
            Payload          []byte
        }: 响应包含查询的响应参数并执行调用事务
    7. 目录
      1. invoke: 包调用提供了执行链代码调用的处理程序。
  • 相关阅读:
    提权函数之RtlAdjustPrivilege()
    用C#写外挂或辅助工具必须要的WindowsAPI
    ASP.net中保持页面中滚动条状态
    asp.net窗体的打开和关闭
    界面原型设计工具 Balsamiq Mockups
    在List(T)中查找数据的两种方法
    P2158 [SDOI2008]仪仗队 题解
    P1531 I Hate It 题解
    C#
    破解网站防盗链
  • 原文地址:https://www.cnblogs.com/apolov-fabric/p/9719280.html
Copyright © 2011-2022 走看看