Skip to content

Commit

Permalink
[FABG-886] wrap internal discovery.Request
Browse files Browse the repository at this point in the history
Purpose of this commit is to expose internal Request
and make Service Discovery client usable outside of sdk.
 - Wrapped internal Request
 - Proxied all the methods of internal Request
 - Added helper methods CcCalls CcInterests for building the Request obj
 - Fixed signatures of the Service Discovery client methods with wrapped Request
 - Fixed tests

Signed-off-by: kopaygorodsky <vlad.kopaygorodsky@gmail.com>
Change-Id: Id70bd1db0d4ddb6d3d567375602e11f4762b9f6c
  • Loading branch information
kopaygorodsky committed Aug 1, 2019
1 parent 1ad9245 commit 8e3a900
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/client/common/discovery/dynamicdiscovery/chservice.go
Expand Up @@ -75,7 +75,7 @@ func (s *ChannelService) doQueryPeers() ([]fab.Peer, error) {
reqCtx, cancel := reqContext.NewRequest(ctx, reqContext.WithTimeout(s.responseTimeout))
defer cancel()

req := discclient.NewRequest().OfChannel(s.channelID).AddPeersQuery()
req := fabdiscovery.NewRequest().OfChannel(s.channelID).AddPeersQuery()
responses, err := s.discoveryClient().Send(reqCtx, req, targets...)
if err != nil {
if len(responses) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/common/discovery/dynamicdiscovery/localservice.go
Expand Up @@ -7,11 +7,11 @@ SPDX-License-Identifier: Apache-2.0
package dynamicdiscovery

import (
discclient "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/discovery/client"
coptions "github.com/hyperledger/fabric-sdk-go/pkg/common/options"
contextAPI "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
reqContext "github.com/hyperledger/fabric-sdk-go/pkg/context"
fabdiscovery "github.com/hyperledger/fabric-sdk-go/pkg/fab/discovery"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -76,7 +76,7 @@ func (s *LocalService) doQueryPeers() ([]fab.Peer, error) {
reqCtx, cancel := reqContext.NewRequest(ctx, reqContext.WithTimeout(s.responseTimeout))
defer cancel()

req := discclient.NewRequest().AddLocalPeersQuery()
req := fabdiscovery.NewRequest().AddLocalPeersQuery()
responses, err := s.discoveryClient().Send(reqCtx, req, *target)
if err != nil {
return nil, errors.Wrap(err, "error calling discover service send")
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/common/discovery/dynamicdiscovery/service.go
Expand Up @@ -22,7 +22,7 @@ import (

// DiscoveryClient is the client to the discovery service
type DiscoveryClient interface {
Send(ctx context.Context, req *discclient.Request, targets ...fab.PeerConfig) ([]fabdiscovery.Response, error)
Send(ctx context.Context, req *fabdiscovery.Request, targets ...fab.PeerConfig) ([]fabdiscovery.Response, error)
}

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/common/mocks/mockdiscoveryclient.go
Expand Up @@ -36,7 +36,7 @@ func NewMockDiscoveryClient() *MockDiscoveryClient {
}

// Send sends a Discovery request
func (m *MockDiscoveryClient) Send(ctx reqcontext.Context, req *discclient.Request, targets ...fab.PeerConfig) ([]fabdiscovery.Response, error) {
func (m *MockDiscoveryClient) Send(ctx reqcontext.Context, req *fabdiscovery.Request, targets ...fab.PeerConfig) ([]fabdiscovery.Response, error) {
return m.responses(), nil
}

Expand Down
Expand Up @@ -60,7 +60,7 @@ var defaultRetryOpts = retry.Opts{

// DiscoveryClient is the client to the discovery service
type DiscoveryClient interface {
Send(ctx context.Context, req *discclient.Request, targets ...fab.PeerConfig) ([]fabdiscovery.Response, error)
Send(ctx context.Context, req *fabdiscovery.Request, targets ...fab.PeerConfig) ([]fabdiscovery.Response, error)
}

// clientProvider is overridden by unit tests
Expand Down Expand Up @@ -224,7 +224,7 @@ func (s *Service) queryEndorsers(chaincodes []*fab.ChaincodeCall, retryOpts retr
return nil, errors.Errorf("no peers configured for channel [%s]", s.channelID)
}

req, err := discclient.NewRequest().OfChannel(s.channelID).AddEndorsersQuery(asChaincodeInterests(chaincodes))
req, err := fabdiscovery.NewRequest().OfChannel(s.channelID).AddEndorsersQuery(asChaincodeInterests(chaincodes))
if err != nil {
return nil, errors.Wrapf(err, "error creating endorser query request")
}
Expand All @@ -242,7 +242,7 @@ func (s *Service) queryEndorsers(chaincodes []*fab.ChaincodeCall, retryOpts retr
return chResponse.(discclient.ChannelResponse), nil
}

func (s *Service) query(req *discclient.Request, chaincodes []*fab.ChaincodeCall, targets []fab.PeerConfig) (discclient.ChannelResponse, error) {
func (s *Service) query(req *fabdiscovery.Request, chaincodes []*fab.ChaincodeCall, targets []fab.PeerConfig) (discclient.ChannelResponse, error) {
logger.Debugf("Querying Discovery Service for endorsers for chaincodes: %#v on channel [%s]", chaincodes, s.channelID)
reqCtx, cancel := reqContext.NewRequest(s.ctx, reqContext.WithTimeout(s.responseTimeout))
defer cancel()
Expand Down
4 changes: 2 additions & 2 deletions pkg/fab/discovery/discovery.go
Expand Up @@ -58,7 +58,7 @@ type Response interface {
// given set of peers. A set of successful responses is returned and/or an error
// is returned from each of the peers that was unsuccessful (note that if more than one peer returned
// an error then the returned error may be cast to multi.Errors).
func (c *Client) Send(ctx context.Context, req *discclient.Request, targets ...fab.PeerConfig) ([]Response, error) {
func (c *Client) Send(ctx context.Context, req *Request, targets ...fab.PeerConfig) ([]Response, error) {
if len(targets) == 0 {
return nil, errors.New("no targets specified")
}
Expand All @@ -74,7 +74,7 @@ func (c *Client) Send(ctx context.Context, req *discclient.Request, targets ...f
go func(target fab.PeerConfig) {
defer wg.Done()

resp, err := c.send(ctx, req, target)
resp, err := c.send(ctx, req.r, target)
lock.Lock()
if err != nil {
errs = multi.Append(errs, errors.WithMessage(err, "From target: "+target.URL))
Expand Down
3 changes: 1 addition & 2 deletions pkg/fab/discovery/discovery_test.go
Expand Up @@ -14,7 +14,6 @@ import (
"testing"
"time"

discclient "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/discovery/client"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/multi"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/comm"
Expand All @@ -39,7 +38,7 @@ func TestDiscoveryClient(t *testing.T) {
client, err := New(clientCtx)
assert.NoError(t, err)

req := discclient.NewRequest().AddLocalPeersQuery().OfChannel(channelID).AddPeersQuery()
req := NewRequest().AddLocalPeersQuery().OfChannel(channelID).AddPeersQuery()

grpcOptions := map[string]interface{}{
"allow-insecure": true,
Expand Down
80 changes: 80 additions & 0 deletions pkg/fab/discovery/request.go
@@ -0,0 +1,80 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package discovery

import (
discclient "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/discovery/client"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/discovery"
)

// Request aggregates several queries inside it
type Request struct {
r *discclient.Request
}

// NewRequest creates a new request
func NewRequest() *Request {
return &Request{discclient.NewRequest()}
}

// AddConfigQuery adds to the request a config query
func (req *Request) AddConfigQuery() *Request {
req.r.AddConfigQuery()
return req
}

// AddLocalPeersQuery adds to the request a local peer query
func (req *Request) AddLocalPeersQuery() *Request {
req.r.AddLocalPeersQuery()
return req
}

// OfChannel sets the next queries added to be in the given channel's context
func (req *Request) OfChannel(ch string) *Request {
req.r.OfChannel(ch)
return req
}

// AddEndorsersQuery adds to the request a query for given chaincodes
// interests are the chaincode interests that the client wants to query for.
// All interests for a given channel should be supplied in an aggregated slice
func (req *Request) AddEndorsersQuery(interests ...*discovery.ChaincodeInterest) (*Request, error) {
_, err := req.r.AddEndorsersQuery(interests...)
return req, err
}

// AddPeersQuery adds to the request a peer query
func (req *Request) AddPeersQuery(invocationChain ...*discovery.ChaincodeCall) *Request {
req.r.AddPeersQuery(invocationChain...)
return req
}

// CcCalls creates an array of ChaincodeCalls based of cc names, can be used in AddPeersQuery(CcCalls(...))
func CcCalls(ccNames ...string) []*discovery.ChaincodeCall {
var call []*discovery.ChaincodeCall

for _, ccName := range ccNames {
call = append(call, &discovery.ChaincodeCall{
Name: ccName,
})
}

return call
}

// CcInterests creates an array of ChaincodeInterests based of ChaincodeCalls, can be used in AddEndorsersQuery(CcInterests(CcCalls(...)))
func CcInterests(invocationsChains ...[]*discovery.ChaincodeCall) []*discovery.ChaincodeInterest {
var interests []*discovery.ChaincodeInterest

for _, invocationChain := range invocationsChains {
interests = append(interests, &discovery.ChaincodeInterest{
Chaincodes: invocationChain,
})
}

return interests
}
Expand Up @@ -56,7 +56,7 @@ func TestDiscoveryClientPeers(t *testing.T) {
reqCtx, cancel := context.NewRequest(ctx, context.WithTimeout(10*time.Second))
defer cancel()

req := discclient.NewRequest().OfChannel(orgChannelID).AddPeersQuery()
req := discovery.NewRequest().OfChannel(orgChannelID).AddPeersQuery()

peerCfg1, err := comm.NetworkPeerConfig(ctx.EndpointConfig(), peer0Org1)
require.NoErrorf(t, err, "error getting peer config for [%s]", peer0Org1)
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestDiscoveryClientLocalPeers(t *testing.T) {
reqCtx, cancel := context.NewRequest(ctx, context.WithTimeout(10*time.Second))
defer cancel()

req := discclient.NewRequest().AddLocalPeersQuery()
req := discovery.NewRequest().AddLocalPeersQuery()

peerCfg1, err := comm.NetworkPeerConfig(ctx.EndpointConfig(), peer0Org1)
require.NoErrorf(t, err, "error getting peer config for [%s]", peer0Org1)
Expand Down Expand Up @@ -359,7 +359,7 @@ func setupOrgContext(t *testing.T) []*integration.OrgContext {
}

func sendEndorserQuery(t *testing.T, ctx contextAPI.Client, client *discovery.Client, interest *fabdiscovery.ChaincodeInterest, peerConfig fab.PeerConfig) (discclient.ChannelResponse, error) {
req, err := discclient.NewRequest().OfChannel(orgChannelID).AddEndorsersQuery(interest)
req, err := discovery.NewRequest().OfChannel(orgChannelID).AddEndorsersQuery(interest)
require.NoError(t, err, "error adding endorsers query")

reqCtx, cancel := context.NewRequest(ctx, context.WithTimeout(10*time.Second))
Expand Down

0 comments on commit 8e3a900

Please sign in to comment.