# channel

By [fenq](https://paragraph.com/@fenq) · 2021-11-24

---

一、channel简介 1、channel 简介 pkg/client/channel支持访问Fabric网络上的通道。channel客户端实例提供与指定通道上的Peer节点进行交互的处理函数。channel客户端可以在指定通道上查询链码，执行链码以及注册或注销链码事件。如果应用程序需要与Fabric网络的多条通道进行交互，需要为每条通道创建一个单独的通道客户端实例。 官方文档：  [https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/client/channel](https://godoc.org/github.com/hyperledger/fabric-sdk-go/pkg/client/channel)

2、channel使用流程 channel使用流程如下： A、准备通道客户端上下文 B、创建通道客户端 C、执行链码 D、查询链码 channel使用示例如下：

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

response, err := c.Query(Request{ChaincodeID: "testCC", Fcn: "invoke", Args: \[\]\[\]byte{\[\]byte("query"), \[\]byte("data")}}) if err != nil { fmt.Printf("failed to query chaincode: %s\\n", err) }

fmt.Println(string(response.Payload)) // output: // abc 二、channel常用接口 1、类型定义 // Request 包含查询和执行一个调用交易的参数 type Request struct { ChaincodeID string Fcn string Args \[\]\[\]byte TransientMap map\[string\]\[\]byte

    // InvocationChai包含元数据，某些选择服务实现使用元数据来选择满足调用链中所有链码的背书	
     // 策略的背书节点
    // Each chaincode may also be associated with a set of private data collection names
    // which are used by some Selection Services (e.g. Fabric Selection) to exclude endorsers
    // that do NOT have read access to the collections.
    // The invoked chaincode (specified by ChaincodeID) may optionally be added to the invocation
    // chain along with any collections, otherwise it may be omitted.
    InvocationChain []*fab.ChaincodeCall
    

}

//Response包含执行和查询一个调用交易的响应参数 type Response struct { Proposal \*fab.TransactionProposal Responses \[\]\*fab.TransactionProposalResponse TransactionID fab.TransactionID TxValidationCode pb.TxValidationCode ChaincodeStatus int32 Payload \[\]byte } 2、获取客户端实例 type Client struct { context context.Channel membership fab.ChannelMembership eventService fab.EventService greylist \*greylist.Filter clientTally // nolint } 1. 2. 3. 4. 5. 6. 7. 通道客户端支持访问Fabric网络上的通道。为了与特定通道的Peer节点进行交互，通道客户端实例提供了一个处理程序。 如果应用程序需要与多个通道进行交互，应该为每个通道创建一个单独的通道客户端实例。 通道客户端只支持非管理功能。

type ClientOption func(\*Client) error func New(channelProvider context.ChannelProvider, opts ...ClientOption) (\*Client, error) 1. 2. 返回通道Client实例。通道客户端可以在特定通道上查询链码，执行链码以及注册/注销链码事件。 使用示例：

ctx := mockChannelProvider("mychannel")

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

if c != nil { fmt.Println("channel client created") } else { fmt.Println("channel client is nil") } // output: // channel client created1. 3、执行交易 func (cc \*Client) Execute(request Request, options ...RequestOption) (Response, error) 使用请求和可选的请求选项进行准备并执行事务。 参数:  request包含必备链码ID和函数的相关信息 options包含可选的请求选项 返回Peer的提案回复 使用示例：

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

\_, err = c.Execute(Request{ChaincodeID: "testCC", Fcn: "invoke", Args: \[\]\[\]byte{\[\]byte("move"), \[\]byte("a"), \[\]byte("b"), \[\]byte("1")}}) if err != nil { fmt.Println(err.Error()) }

fmt.Println("Chaincode transaction completed") // output: // Chaincode transaction completed1. 4、调用交易处理 func (cc \*Client) InvokeHandler(handler invoke.Handler, request Request, options ...RequestOption) (Response, error) InvokeHandler使用提供的请求和可选请求选项来调用处理程序 参数: handler为要调用的处理程序 request包含必备的链码ID和函数的相关信息 options包含可选的请求选项 返回Peer的提案回复 使用示例：

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

response, err := c.InvokeHandler(&exampleHandler{}, Request{ChaincodeID: "testCC", Fcn: "invoke", Args: \[\]\[\]byte{\[\]byte("query"), \[\]byte("data")}}) if err != nil { fmt.Printf("failed to query chaincode: %s\\n", err) }

---

*Originally published on [fenq](https://paragraph.com/@fenq/channel)*
