Skip to content

Commit

Permalink
[FAB-7602] Add default txProposalResponseFilter
Browse files Browse the repository at this point in the history
Change-Id: Ifd7a601380ff00773dc31bbd00b0d18217fa2107
Signed-off-by: Firas Qutishat <[email protected]>
  • Loading branch information
fqutishat committed Jan 4, 2018
1 parent 083c917 commit c0080e2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
7 changes: 4 additions & 3 deletions api/apitxn/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type QueryRequest struct {
type QueryOpts struct {
Notifier chan QueryResponse // async
ProposalProcessors []ProposalProcessor // targets
TxFilter TxProposalResponseFilter
Timeout time.Duration
}

Expand Down Expand Up @@ -50,13 +51,13 @@ type ExecuteTxRequest struct {
// ExecuteTxOpts allows the user to specify more advanced options
type ExecuteTxOpts struct {
Notifier chan ExecuteTxResponse // async
TxFilter ExecuteTxFilter
TxFilter TxProposalResponseFilter
ProposalProcessors []ProposalProcessor // targets
Timeout time.Duration
}

// ExecuteTxFilter allows the user to inspect/modify response before commit
type ExecuteTxFilter interface {
// TxProposalResponseFilter allows the user to inspect/modify response before commit
type TxProposalResponseFilter interface {
// process transaction proposal response (there will be no commit if an error is returned)
ProcessTxProposalResponse(txProposalResponse []*TransactionProposalResponse) ([]*TransactionProposalResponse, error)
}
Expand Down
35 changes: 28 additions & 7 deletions pkg/fabric-txn/chclient/chclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ type ChannelClient struct {
eventHub fab.EventHub
}

// txProposalResponseFilter process transaction proposal response
type txProposalResponseFilter struct {
}

// ProcessTxProposalResponse process transaction proposal response
func (txProposalResponseFilter *txProposalResponseFilter) ProcessTxProposalResponse(txProposalResponse []*apitxn.TransactionProposalResponse) ([]*apitxn.TransactionProposalResponse, error) {
return txProposalResponse, nil
}

// NewChannelClient returns a ChannelClient instance.
func NewChannelClient(client fab.FabricClient, channel fab.Channel, discovery fab.DiscoveryService, selection fab.SelectionService, eventHub fab.EventHub) (*ChannelClient, error) {

Expand Down Expand Up @@ -74,7 +83,7 @@ func (cc *ChannelClient) QueryWithOpts(request apitxn.QueryRequest, opts apitxn.
txProcessors = peer.PeersToTxnProcessors(endorsers)
}

go sendTransactionProposal(request, cc.channel, txProcessors, notifier)
go sendTransactionProposal(request, cc.channel, txProcessors, opts.TxFilter, notifier)

if opts.Notifier != nil {
return nil, nil
Expand All @@ -94,7 +103,7 @@ func (cc *ChannelClient) QueryWithOpts(request apitxn.QueryRequest, opts apitxn.

}

func sendTransactionProposal(request apitxn.QueryRequest, channel fab.Channel, proposalProcessors []apitxn.ProposalProcessor, notifier chan apitxn.QueryResponse) {
func sendTransactionProposal(request apitxn.QueryRequest, channel fab.Channel, proposalProcessors []apitxn.ProposalProcessor, txFilter apitxn.TxProposalResponseFilter, notifier chan apitxn.QueryResponse) {

transactionProposalResponses, _, err := internal.CreateAndSendTransactionProposal(channel,
request.ChaincodeID, request.Fcn, request.Args, proposalProcessors, nil)
Expand All @@ -104,6 +113,16 @@ func sendTransactionProposal(request apitxn.QueryRequest, channel fab.Channel, p
return
}

if txFilter == nil {
txFilter = &txProposalResponseFilter{}
}

transactionProposalResponses, err = txFilter.ProcessTxProposalResponse(transactionProposalResponses)
if err != nil {
notifier <- apitxn.QueryResponse{Response: nil, Error: errors.WithMessage(err, "TxFilter failed")}
return
}

response := transactionProposalResponses[0].ProposalResponse.GetResponse().Payload

notifier <- apitxn.QueryResponse{Response: response, Error: nil}
Expand Down Expand Up @@ -146,11 +165,13 @@ func (cc *ChannelClient) ExecuteTxWithOpts(request apitxn.ExecuteTxRequest, opts
return apitxn.TransactionID{}, errors.WithMessage(err, "CreateAndSendTransactionProposal failed")
}

if opts.TxFilter != nil {
txProposalResponses, err = opts.TxFilter.ProcessTxProposalResponse(txProposalResponses)
if err != nil {
return txID, errors.WithMessage(err, "TxFilter failed")
}
if opts.TxFilter == nil {
opts.TxFilter = &txProposalResponseFilter{}
}

txProposalResponses, err = opts.TxFilter.ProcessTxProposalResponse(txProposalResponses)
if err != nil {
return txID, errors.WithMessage(err, "TxFilter failed")
}

notifier := opts.Notifier
Expand Down

0 comments on commit c0080e2

Please sign in to comment.