channel

package
v0.0.0-...-a160350 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

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
Example
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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

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.

Example
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

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

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

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

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.
Example
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

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

type ClientOption func(*Client) error

ClientOption describes a functional parameter for the New constructor

type Request

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

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

RequestOption func for each Opts argument

func WithBeforeRetry

func WithBeforeRetry(beforeRetry retry.BeforeRetryHandler) RequestOption

WithBeforeRetry specifies a function to call before a retry attempt

func WithChaincodeFilter

func WithChaincodeFilter(ccFilter invoke.CCFilter) RequestOption

WithChaincodeFilter adds a chaincode filter for figuring out additional endorsers

func WithParentContext

func WithParentContext(parentContext reqContext.Context) RequestOption

WithParentContext encapsulates grpc parent context

func WithRetry

func WithRetry(retryOpt retry.Opts) RequestOption

WithRetry option to configure retries

func WithTargetEndpoints

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

func WithTargetFilter(filter fab.TargetFilter) RequestOption

WithTargetFilter specifies a per-request target peer-filter

func WithTargetSorter

func WithTargetSorter(sorter fab.TargetSorter) RequestOption

WithTargetSorter specifies a per-request target sorter

func WithTargets

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

WithTargets allows overriding of the target peers for the request

func WithTimeout

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

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

type Response

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

Path Synopsis
Package invoke provides the handlers for performing chaincode invocations.
Package invoke provides the handlers for performing chaincode invocations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL