github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/discovery/cmd/stub.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package discovery
     8  
     9  import (
    10  	"context"
    11  
    12  	"github.com/hechain20/hechain/cmd/common"
    13  	"github.com/hechain20/hechain/cmd/common/comm"
    14  	"github.com/hechain20/hechain/cmd/common/signer"
    15  	discoveryclient "github.com/hechain20/hechain/discovery/client"
    16  	"github.com/hechain20/hechain/protoutil"
    17  	"github.com/hyperledger/fabric-protos-go/discovery"
    18  	"github.com/pkg/errors"
    19  )
    20  
    21  //go:generate mockery -dir . -name LocalResponse -case underscore -output mocks/
    22  
    23  // LocalResponse is the local interface used to generate mocks for foreign interface.
    24  type LocalResponse interface {
    25  	discoveryclient.LocalResponse
    26  }
    27  
    28  //go:generate mockery -dir . -name ChannelResponse -case underscore -output mocks/
    29  
    30  // ChannelResponse is the local interface used to generate mocks for foreign interface.
    31  type ChannelResponse interface {
    32  	discoveryclient.ChannelResponse
    33  }
    34  
    35  //go:generate mockery -dir . -name ServiceResponse -case underscore -output mocks/
    36  
    37  // ServiceResponse represents a response sent from the discovery service
    38  type ServiceResponse interface {
    39  	// ForChannel returns a ChannelResponse in the context of a given channel
    40  	ForChannel(string) discoveryclient.ChannelResponse
    41  
    42  	// ForLocal returns a LocalResponse in the context of no channel
    43  	ForLocal() discoveryclient.LocalResponse
    44  
    45  	// Raw returns the raw response from the server
    46  	Raw() *discovery.Response
    47  }
    48  
    49  type response struct {
    50  	raw *discovery.Response
    51  	discoveryclient.Response
    52  }
    53  
    54  func (r *response) Raw() *discovery.Response {
    55  	return r.raw
    56  }
    57  
    58  // ClientStub is a stub that communicates with the discovery service
    59  // using the discovery client implementation
    60  type ClientStub struct{}
    61  
    62  // Send sends the request, and receives a response
    63  func (stub *ClientStub) Send(server string, conf common.Config, req *discoveryclient.Request) (ServiceResponse, error) {
    64  	comm, err := comm.NewClient(conf.TLSConfig)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	signer, err := signer.NewSigner(conf.SignerConfig)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	timeout, cancel := context.WithTimeout(context.Background(), defaultTimeout)
    73  	defer cancel()
    74  
    75  	disc := discoveryclient.NewClient(comm.NewDialer(server), signer.Sign, 0)
    76  
    77  	resp, err := disc.Send(timeout, req, &discovery.AuthInfo{
    78  		ClientIdentity:    signer.Creator,
    79  		ClientTlsCertHash: comm.TLSCertHash,
    80  	})
    81  	if err != nil {
    82  		return nil, errors.Errorf("failed connecting to %s: %v", server, err)
    83  	}
    84  	return &response{
    85  		Response: resp,
    86  	}, nil
    87  }
    88  
    89  // RawStub is a stub that communicates with the discovery service
    90  // without any intermediary.
    91  type RawStub struct{}
    92  
    93  // Send sends the request, and receives a response
    94  func (stub *RawStub) Send(server string, conf common.Config, req *discoveryclient.Request) (ServiceResponse, error) {
    95  	comm, err := comm.NewClient(conf.TLSConfig)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  	signer, err := signer.NewSigner(conf.SignerConfig)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	timeout, cancel := context.WithTimeout(context.Background(), defaultTimeout)
   104  	defer cancel()
   105  
   106  	req.Authentication = &discovery.AuthInfo{
   107  		ClientIdentity:    signer.Creator,
   108  		ClientTlsCertHash: comm.TLSCertHash,
   109  	}
   110  
   111  	payload := protoutil.MarshalOrPanic(req.Request)
   112  	sig, err := signer.Sign(payload)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	cc, err := comm.NewDialer(server)()
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	resp, err := discovery.NewDiscoveryClient(cc).Discover(timeout, &discovery.SignedRequest{
   122  		Payload:   payload,
   123  		Signature: sig,
   124  	})
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	return &response{
   130  		raw: resp,
   131  	}, nil
   132  }