fabric-sdk-go: github.com/hyperledger/fabric-sdk-go/pkg/client/channel Index | Examples | Files | Directories

package channel

import "github.com/hyperledger/fabric-sdk-go/pkg/client/channel"

Package channel enables access to a channel on a Fabric network. A channel client instance provides a handler to interact with peers on specified channel. Channel client can query chaincode, execute chaincode and register/unregister for chaincode events on specific channel. An application that requires interaction with multiple channels should create a separate instance of the channel client for each channel.

Basic Flow:
1) Prepare channel client context
2) Create channel client
3) Execute chaincode
4) Query chaincode

Code:

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

Index

Examples

Package Files

api.go chclient.go chclientrun.go chclientrun_std.go

type Client Uses

type Client struct {
    // contains filtered or unexported fields
}

Client enables access to a channel on a Fabric network.

A channel client instance provides a handler to interact with peers on specified channel. An application that requires interaction with multiple channels should create a separate instance of the channel client for each channel. Channel client supports non-admin functions only.

func New Uses

func New(channelProvider context.ChannelProvider, opts ...ClientOption) (*Client, error)

New returns a Client instance. Channel client can query chaincode, execute chaincode and register/unregister for chaincode events on specific channel.

Code:

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 created

func (*Client) Execute Uses

func (cc *Client) Execute(request Request, options ...RequestOption) (Response, error)

Execute prepares and executes transaction using request and optional request options

Parameters:
request holds info about mandatory chaincode ID and function
options holds optional request options

Returns:
the proposal responses from peer(s)

Code:

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 completed

func (*Client) InvokeHandler Uses

func (cc *Client) InvokeHandler(handler invoke.Handler, request Request, options ...RequestOption) (Response, error)

InvokeHandler invokes handler using request and optional request options provided

Parameters:
handler to be invoked
request holds info about mandatory chaincode ID and function
options holds optional request options

Returns:
the proposal responses from peer(s)

Code:

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)
}

fmt.Println(string(response.Payload))

Output:

custom

func (*Client) Query Uses

func (cc *Client) Query(request Request, options ...RequestOption) (Response, error)

Query chaincode using request and optional request options

Parameters:
request holds info about mandatory chaincode ID and function
options holds optional request options

Returns:
the proposal responses from peer(s)

Code:

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("b")}})
if err != nil {
    fmt.Printf("failed to query chaincode: %s\n", err)
}

if len(response.Payload) > 0 {
    fmt.Println("chaincode query success")
}

Output:

chaincode query success

func (*Client) RegisterChaincodeEvent Uses

func (cc *Client) RegisterChaincodeEvent(chainCodeID string, eventFilter string) (fab.Registration, <-chan *fab.CCEvent, error)

RegisterChaincodeEvent registers for chaincode events. Unregister must be called when the registration is no longer needed.

Parameters:
chaincodeID is the chaincode ID for which events are to be received
eventFilter is the chaincode event filter (regular expression) for which events are to be received

Returns:
the registration and a channel that is used to receive events. The channel is closed when Unregister is called.

Code:

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

registration, _, err := c.RegisterChaincodeEvent("examplecc", "event123")
if err != nil {
    fmt.Println("failed to register chaincode event")
}
defer c.UnregisterChaincodeEvent(registration)

fmt.Println("chaincode event registered successfully")

Output:

chaincode event registered successfully

func (*Client) UnregisterChaincodeEvent Uses

func (cc *Client) UnregisterChaincodeEvent(registration fab.Registration)

UnregisterChaincodeEvent removes the given registration and closes the event channel.

Parameters:
registration is the registration handle that was returned from RegisterChaincodeEvent method

type ClientOption Uses

type ClientOption func(*Client) error

ClientOption describes a functional parameter for the New constructor

type Request Uses

type Request struct {
    ChaincodeID  string
    Fcn          string
    Args         [][]byte
    TransientMap map[string][]byte

    // InvocationChain contains meta-data that's used by some Selection Service implementations
    // to choose endorsers that satisfy the endorsement policies of all chaincodes involved
    // in an invocation chain (i.e. for CC-to-CC invocations).
    // 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
}

Request contains the parameters to query and execute an invocation transaction

type RequestOption Uses

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

RequestOption func for each Opts argument

func WithBeforeRetry Uses

func WithBeforeRetry(beforeRetry retry.BeforeRetryHandler) RequestOption

WithBeforeRetry specifies a function to call before a retry attempt

func WithChaincodeFilter Uses

func WithChaincodeFilter(ccFilter invoke.CCFilter) RequestOption

WithChaincodeFilter adds a chaincode filter for figuring out additional endorsers

func WithParentContext Uses

func WithParentContext(parentContext reqContext.Context) RequestOption

WithParentContext encapsulates grpc parent context

func WithRetry Uses

func WithRetry(retryOpt retry.Opts) RequestOption

WithRetry option to configure retries

func WithTargetEndpoints Uses

func WithTargetEndpoints(keys ...string) RequestOption

WithTargetEndpoints allows overriding of the target peers for the request. Targets are specified by name or URL, and the SDK will create the underlying peer objects.

func WithTargetFilter Uses

func WithTargetFilter(filter fab.TargetFilter) RequestOption

WithTargetFilter specifies a per-request target peer-filter

func WithTargetSorter Uses

func WithTargetSorter(sorter fab.TargetSorter) RequestOption

WithTargetSorter specifies a per-request target sorter

func WithTargets Uses

func WithTargets(targets ...fab.Peer) RequestOption

WithTargets allows overriding of the target peers for the request

func WithTimeout Uses

func WithTimeout(timeoutType fab.TimeoutType, timeout time.Duration) RequestOption

WithTimeout encapsulates key value pairs of timeout type, timeout duration to Options

type Response Uses

type Response struct {
    Proposal         *fab.TransactionProposal
    Responses        []*fab.TransactionProposalResponse
    TransactionID    fab.TransactionID
    TxValidationCode pb.TxValidationCode
    ChaincodeStatus  int32
    Payload          []byte
}

Response contains response parameters for query and execute an invocation transaction

Directories

PathSynopsis
invokePackage invoke provides the handlers for performing chaincode invocations.

Package channel imports 15 packages (graph) and is imported by 18 packages. Updated 2019-08-24. Refresh now. Tools for package owners.