client

package
v2.0.0-alpha+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2019 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0

Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0

Index

Constants

View Source
const DefaultConnectionTimeout = 10 * time.Second

Variables

This section is empty.

Functions

func BroadcastReceive

func BroadcastReceive(broadcast Broadcast, addr string, responses chan common.Status, errs chan error)

broadReceive waits until it receives the response from broadcast stream

func BroadcastSend

func BroadcastSend(broadcast Broadcast, addr string, envelope *common.Envelope) error

broadcastSend sends transaction envelope to orderer service

func BroadcastWaitForResponse

func BroadcastWaitForResponse(responses chan common.Status, errs chan error) (common.Status, error)

broadcastWaitForResponse reads from response and errs chans until responses chan is closed

func CreateDeliverEnvelope

func CreateDeliverEnvelope(channelId string, creator []byte, signingIdentity tk.SigningIdentity, cert *tls.Certificate) (*common.Envelope, error)

create a signed envelope with SeekPosition_Newest for block

func CreateEnvelope

func CreateEnvelope(data []byte, header *common.Header, signingIdentity tk.SigningIdentity) (*common.Envelope, error)

CreateEnvelope creates a common.Envelope with given tx bytes, header, and SigningIdentity

func CreateGRPCClient

func CreateGRPCClient(config *ConnectionConfig) (*comm.GRPCClient, error)

CreateGRPCClient returns a comm.GRPCClient based on toke client config

func CreateHeader

func CreateHeader(txType common.HeaderType, channelId string, creator []byte, tlsCertHash []byte) (string, *common.Header, error)

CreateHeader creates common.Header for a token transaction tlsCertHash is for client TLS cert, only applicable when ClientAuthRequired is true

func DeliverReceive

func DeliverReceive(df DeliverFiltered, address string, txid string, eventCh chan<- TxEvent) error

func DeliverSend

func DeliverSend(df DeliverFiltered, address string, envelope *common.Envelope) error

func DeliverWaitForResponse

func DeliverWaitForResponse(ctx context.Context, eventCh <-chan TxEvent, txid string) (bool, error)

DeliverWaitForResponse waits for either eventChan has value (i.e., response has been received) or ctx is timed out This function assumes that the eventCh is only for the specified txid If an eventCh is shared by multiple transactions, a loop should be used to listen to events from multiple transactions

func GetTLSCertHash

func GetTLSCertHash(cert *tls.Certificate) ([]byte, error)

GetTLSCertHash computes SHA2-256 on tls certificate

func GetTransactionID

func GetTransactionID(txEnvelope *common.Envelope) (string, error)

func ValidateClientConfig

func ValidateClientConfig(config ClientConfig) error

Types

type Broadcast

type Broadcast interface {
	Send(m *common.Envelope) error
	Recv() (*ab.BroadcastResponse, error)
	CloseSend() error
}

Broadcast defines the interface that abstracts grpc calls to broadcast transactions to orderer

type Client

type Client struct {
	Config          *ClientConfig
	SigningIdentity tk.SigningIdentity
	Prover          Prover
	TxSubmitter     FabricTxSubmitter
}

Client represents the client struct that calls Prover and TxSubmitter

func NewClient

func NewClient(config ClientConfig, signingIdentity tk.SigningIdentity) (*Client, error)

NewClient creates a new Client from token client config It initializes msp crypto, gets SigningIdenity and creates a TxSubmitter

func (*Client) Issue

func (c *Client) Issue(tokensToIssue []*token.Token, waitTimeout time.Duration) (*common.Envelope, string, *common.Status, bool, error)

Issue is the function that the client calls to introduce tokens into the system. It takes as parameter an array of token.Token that defines what tokens are going to be issued. The 'waitTimeout' parameter defines the time to wait for the transaction to be committed. If it is 0, the function will return right after receiving a response from the orderer. If it is greater than 0, the function will wait until receiving the transaction event or timed out, whichever is earlier. This API sends the transaction to the orderer and returns the envelope, transaction id, orderer status, committed boolean, and error. When an error is returned, analyze the orderer status and error message to understand how to fix the problem. If the status is SUCCESS (200), it means that the transaction has been successfully submitted regardless of the error. In this case, check the transaction status to know if the transaction is committed or invalidated. If the transaction is invalidated, the application can fix the error and call the function again.

func (*Client) ListTokens

func (c *Client) ListTokens() ([]*token.UnspentToken, error)

ListTokens allows the client to submit a list request to a prover peer service; it returns a list of UnspentToken and an error in the case the request fails

func (*Client) Redeem

func (c *Client) Redeem(tokenIDs []*token.TokenId, quantity string, waitTimeout time.Duration) (*common.Envelope, string, *common.Status, bool, error)

Redeem allows the redemption of the tokens in the input tokenIDs The 'waitTimeout' parameter defines the time to wait for the transaction to be committed. If it is 0, the function will return immediately after receiving a response from the orderer without waiting for the transaction to be committed. If it is greater than 0, the function will wait until the transaction commit event is received or wait timed out, whichever is earlier. This API submits the transaction to the orderer and returns envelope, transaction id, orderer status, committed boolean, and error. When an error is returned, check the orderer status. If it is SUCCESS (200), the transaction has been successfully submitted regardless of the error. The application must analyze the error and get the transaction status to make sure the transaction is either committed or invalidated. If the transaction is invalidated, the application may call the API again after fixing the error.

func (*Client) Transfer

func (c *Client) Transfer(tokenIDs []*token.TokenId, shares []*token.RecipientShare, waitTimeout time.Duration) (*common.Envelope, string, *common.Status, bool, error)

Transfer is the function that the client calls to transfer his tokens. Transfer takes as parameter an array of token.RecipientShare that identifies who receives the tokens and describes how the tokens are distributed. The 'waitTimeout' parameter defines the time to wait for the transaction to be committed. If it is 0, the function will return right after receiving a response from the orderer. If it is greater than 0, the function will wait until receiving the transaction event or timed out, whichever is earlier. This API sends the transaction to the orderer and returns the envelope, transaction id, orderer status, committed boolean, and error. When an error is returned, analyze the orderer status and error message to understand how to fix the problem. If the status is SUCCESS (200), it means that the transaction has been successfully submitted regardless of the error. In this case, check the transaction status to know if the transaction is committed or invalidated. If the transaction is invalidated, the application can fix the error and call the function again.

type ClientConfig

type ClientConfig struct {
	ChannelID     string
	MSPInfo       MSPInfo
	Orderer       ConnectionConfig
	CommitterPeer ConnectionConfig
	ProverPeer    ConnectionConfig
}

ClientConfig will be updated after the CR for token client config is merged, where the config data will be populated based on a config file.

func (*ClientConfig) ToJSon

func (config *ClientConfig) ToJSon() ([]byte, error)

type ConnectionConfig

type ConnectionConfig struct {
	Address            string
	ConnectionTimeout  time.Duration
	TLSEnabled         bool
	TLSRootCertFile    string
	ServerNameOverride string
}

ConnectionConfig contains data required to establish grpc connection to a peer or orderer

type DeliverClient

type DeliverClient interface {
	// NewDeliverFilterd returns a DeliverFiltered
	NewDeliverFiltered(ctx context.Context, opts ...grpc.CallOption) (DeliverFiltered, error)

	// Certificate returns tls certificate for the deliver client to commit peer
	Certificate() *tls.Certificate
}

DeliverClient defines the interface to create a DeliverFiltered client

func NewDeliverClient

func NewDeliverClient(config *ConnectionConfig) (DeliverClient, error)

type DeliverFiltered

type DeliverFiltered interface {
	Send(*common.Envelope) error
	Recv() (*pb.DeliverResponse, error)
	CloseSend() error
}

DeliverFiltered defines the interface that abstracts deliver filtered grpc calls to commit peer

type FabricTxSubmitter

type FabricTxSubmitter interface {

	// Submit allows the client to submit a fabric token transaction.
	// It takes as input a transaction envelope and a timeout to wait for the transaction to be committed.
	// The function returns the orderer response status, committed boolean, and error in case of failure.
	// The 'waitTimeout' parameter defines the time to wait for the transaction to be committed.
	// If it is 0, the function will return immediately after receiving a response from the orderer
	// without waiting for transaction commit response.
	// If it is greater than 0, the function will wait until the transaction commit response is received or timeout, whichever is earlier.
	Submit(txEnvelope *common.Envelope, waitTimeout time.Duration) (*common.Status, bool, error)

	// CreateTxEnvelope creates a transaction envelope from the serialized TokenTransaction.
	// It returns the transaction envelope, transaction id, and error.
	CreateTxEnvelope(tokenTx []byte) (*common.Envelope, string, error)
}

type MSPInfo

type MSPInfo struct {
	MSPConfigPath string
	MSPID         string
	MSPType       string
}

type OrdererClient

type OrdererClient interface {
	// NewBroadcast returns a Broadcast
	NewBroadcast(ctx context.Context, opts ...grpc.CallOption) (Broadcast, error)

	// Certificate returns tls certificate for the orderer client
	Certificate() *tls.Certificate
}

OrdererClient defines the interface to create a Broadcast

func NewOrdererClient

func NewOrdererClient(config *ConnectionConfig) (OrdererClient, error)

type Prover

type Prover interface {

	// RequestIssue allows the client to submit an issue request to a prover peer service;
	// the function takes as parameters tokensToIssue and the signing identity of the client;
	// it returns a response in bytes and an error message in the case the request fails.
	// The response corresponds to a serialized TokenTransaction protobuf message.
	RequestIssue(tokensToIssue []*token.Token, signingIdentity tk.SigningIdentity) ([]byte, error)

	// RequestTransfer allows the client to submit a transfer request to a prover peer service;
	// the function takes as parameters a fabtoken application credential, the identifiers of the tokens
	// to be transfererd and the shares describing how they are going to be distributed
	// among recipients; it returns a response in bytes and an error message in the case the
	// request fails
	RequestTransfer(tokenIDs []*token.TokenId, shares []*token.RecipientShare, signingIdentity tk.SigningIdentity) ([]byte, error)

	// RequestRedeem allows the redemption of the tokens in the input tokenIDs
	// It queries the ledger to read detail for each token id.
	// It creates a token transaction with an output for redeemed tokens and
	// possibly another output to transfer the remaining tokens, if any, to the same user
	RequestRedeem(tokenIDs []*token.TokenId, quantity string, signingIdentity tk.SigningIdentity) ([]byte, error)

	// ListTokens allows the client to submit a list request to a prover peer service;
	// it returns a list of UnspentToken and an error message in the case the request fails
	ListTokens(signingIdentity tk.SigningIdentity) ([]*token.UnspentToken, error)
}

type ProverPeer

type ProverPeer struct {
	ChannelID        string
	ProverPeerClient ProverPeerClient
	RandomnessReader io.Reader
	Time             TimeFunc
}

ProverPeer implements Prover interface

func NewProverPeer

func NewProverPeer(config *ClientConfig) (*ProverPeer, error)

func (*ProverPeer) CreateSignedCommand

func (prover *ProverPeer) CreateSignedCommand(payload interface{}, signingIdentity tk.SigningIdentity) (*token.SignedCommand, error)

func (*ProverPeer) ListTokens

func (prover *ProverPeer) ListTokens(signingIdentity tk.SigningIdentity) ([]*token.UnspentToken, error)

ListTokens allows the client to submit a list request to a prover peer service; it returns a list of UnspentToken and an error message in the case the request fails

func (*ProverPeer) RequestIssue

func (prover *ProverPeer) RequestIssue(tokensToIssue []*token.Token, signingIdentity tk.SigningIdentity) ([]byte, error)

RequestIssue allows the client to submit an issue request to a prover peer service; the function takes as parameters tokensToIssue and the signing identity of the client; it returns a marshalled TokenTransaction and an error message in the case the request fails.

func (*ProverPeer) RequestRedeem

func (prover *ProverPeer) RequestRedeem(tokenIDs []*token.TokenId, quantity string, signingIdentity tk.SigningIdentity) ([]byte, error)

RequestRedeem allows the redemption of the tokens in the input tokenIDs It queries the ledger to read detail for each token id. It creates a token transaction with an output for redeemed tokens and possibly another output to transfer the remaining tokens, if any, to the same user

func (*ProverPeer) RequestTransfer

func (prover *ProverPeer) RequestTransfer(tokenIDs []*token.TokenId, shares []*token.RecipientShare, signingIdentity tk.SigningIdentity) ([]byte, error)

RequestTransfer allows the client to submit a transfer request to a prover peer service; the function takes as parameters a fabtoken application credential, the identifiers of the tokens to be transferred and the shares describing how they are going to be distributed among recipients; it returns a marshalled token transaction and an error message in the case the request fails

func (*ProverPeer) SendCommand

func (prover *ProverPeer) SendCommand(ctx context.Context, sc *token.SignedCommand) ([]byte, error)

SendCommand is for issue, transfer and redeem commands that will create a token transaction. It calls prover to process command and returns marshalled token transaction.

type ProverPeerClient

type ProverPeerClient interface {
	// CreateProverClient creates a grpc connection and client to prover peer
	CreateProverClient() (*grpc.ClientConn, token.ProverClient, error)

	// Certificate returns tls client certificate
	Certificate() *tls.Certificate
}

ProverPeerClient defines an interface that creates a client to communicate with the prover service in a peer

type ProverPeerClientImpl

type ProverPeerClientImpl struct {
	Address            string
	ServerNameOverride string
	GRPCClient         *comm.GRPCClient
}

ProverPeerClientImpl implements ProverPeerClient interface

func (*ProverPeerClientImpl) Certificate

func (pc *ProverPeerClientImpl) Certificate() *tls.Certificate

func (*ProverPeerClientImpl) CreateProverClient

func (pc *ProverPeerClientImpl) CreateProverClient() (*grpc.ClientConn, token.ProverClient, error)

type TimeFunc

type TimeFunc func() time.Time

type TxEvent

type TxEvent struct {
	Txid       string
	Committed  bool
	CommitPeer string
	Err        error
}

TxEvent contains information for token transaction commit

type TxSubmitter

type TxSubmitter struct {
	Config          *ClientConfig
	SigningIdentity tk.SigningIdentity
	Creator         []byte
	OrdererClient   OrdererClient
	DeliverClient   DeliverClient
}

TxSubmitter will submit token transactions to the orderer and create a channel to write/read transaction completion event

func NewTxSubmitter

func NewTxSubmitter(config *ClientConfig, signingIdentity tk.SigningIdentity) (*TxSubmitter, error)

NewTxSubmitter creates a new TxSubmitter from token client config

func (*TxSubmitter) CreateTxEnvelope

func (s *TxSubmitter) CreateTxEnvelope(txBytes []byte) (*common.Envelope, string, error)

CreateTxEnvelope creates a transaction envelope from the serialized TokenTransaction. It returns the transaction envelope, transaction id, and error.

func (*TxSubmitter) Submit

func (s *TxSubmitter) Submit(txEnvelope *common.Envelope, waitTimeout time.Duration) (*common.Status, bool, error)

Submit submits a token transaction to fabric orderer. It takes a transaction envelope and waitTimeout as input parameters. The 'waitTimeout' indicates how long to wait for transaction commit event. If it is 0, the function will not wait for transaction to be committed. If it is greater than 0, the function will wait until timeout or transaction is committed, whichever is earlier It returns orderer status, committed boolean, and error.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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