gitlab.com/gpdionisio/tendermint@v0.34.19-dev2/rpc/client/mock/client.go (about)

     1  package mock
     2  
     3  /*
     4  package mock returns a Client implementation that
     5  accepts various (mock) implementations of the various methods.
     6  
     7  This implementation is useful for using in tests, when you don't
     8  need a real server, but want a high-level of control about
     9  the server response you want to mock (eg. error handling),
    10  or if you just want to record the calls to verify in your tests.
    11  
    12  For real clients, you probably want the "http" package.  If you
    13  want to directly call a tendermint node in process, you can use the
    14  "local" package.
    15  */
    16  
    17  import (
    18  	"context"
    19  	"reflect"
    20  
    21  	"github.com/tendermint/tendermint/libs/bytes"
    22  	"github.com/tendermint/tendermint/libs/service"
    23  	"github.com/tendermint/tendermint/rpc/client"
    24  	"github.com/tendermint/tendermint/rpc/core"
    25  	ctypes "github.com/tendermint/tendermint/rpc/core/types"
    26  	rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
    27  	"github.com/tendermint/tendermint/types"
    28  )
    29  
    30  // Client wraps arbitrary implementations of the various interfaces.
    31  //
    32  // We provide a few choices to mock out each one in this package.
    33  // Nothing hidden here, so no New function, just construct it from
    34  // some parts, and swap them out them during the tests.
    35  type Client struct {
    36  	client.ABCIClient
    37  	client.SignClient
    38  	client.HistoryClient
    39  	client.StatusClient
    40  	client.EventsClient
    41  	client.EvidenceClient
    42  	client.MempoolClient
    43  	service.Service
    44  }
    45  
    46  var _ client.Client = Client{}
    47  
    48  // Call is used by recorders to save a call and response.
    49  // It can also be used to configure mock responses.
    50  //
    51  type Call struct {
    52  	Name     string
    53  	Args     interface{}
    54  	Response interface{}
    55  	Error    error
    56  }
    57  
    58  // GetResponse will generate the apporiate response for us, when
    59  // using the Call struct to configure a Mock handler.
    60  //
    61  // When configuring a response, if only one of Response or Error is
    62  // set then that will always be returned. If both are set, then
    63  // we return Response if the Args match the set args, Error otherwise.
    64  func (c Call) GetResponse(args interface{}) (interface{}, error) {
    65  	// handle the case with no response
    66  	if c.Response == nil {
    67  		if c.Error == nil {
    68  			panic("Misconfigured call, you must set either Response or Error")
    69  		}
    70  		return nil, c.Error
    71  	}
    72  	// response without error
    73  	if c.Error == nil {
    74  		return c.Response, nil
    75  	}
    76  	// have both, we must check args....
    77  	if reflect.DeepEqual(args, c.Args) {
    78  		return c.Response, nil
    79  	}
    80  	return nil, c.Error
    81  }
    82  
    83  func (c Client) Status(ctx context.Context) (*ctypes.ResultStatus, error) {
    84  	return core.Status(&rpctypes.Context{})
    85  }
    86  
    87  func (c Client) ABCIInfo(ctx context.Context) (*ctypes.ResultABCIInfo, error) {
    88  	return core.ABCIInfo(&rpctypes.Context{})
    89  }
    90  
    91  func (c Client) ABCIQuery(ctx context.Context, path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error) {
    92  	return c.ABCIQueryWithOptions(ctx, path, data, client.DefaultABCIQueryOptions)
    93  }
    94  
    95  func (c Client) ABCIQueryWithOptions(
    96  	ctx context.Context,
    97  	path string,
    98  	data bytes.HexBytes,
    99  	opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
   100  	return core.ABCIQuery(&rpctypes.Context{}, path, data, opts.Height, opts.Prove)
   101  }
   102  
   103  func (c Client) BroadcastTxCommit(ctx context.Context, tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
   104  	return core.BroadcastTxCommit(&rpctypes.Context{}, tx)
   105  }
   106  
   107  func (c Client) BroadcastTxAsync(ctx context.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
   108  	return core.BroadcastTxAsync(&rpctypes.Context{}, tx)
   109  }
   110  
   111  func (c Client) BroadcastTxSync(ctx context.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
   112  	return core.BroadcastTxSync(&rpctypes.Context{}, tx)
   113  }
   114  
   115  func (c Client) CheckTx(ctx context.Context, tx types.Tx) (*ctypes.ResultCheckTx, error) {
   116  	return core.CheckTx(&rpctypes.Context{}, tx)
   117  }
   118  
   119  func (c Client) NetInfo(ctx context.Context) (*ctypes.ResultNetInfo, error) {
   120  	return core.NetInfo(&rpctypes.Context{})
   121  }
   122  
   123  func (c Client) ConsensusState(ctx context.Context) (*ctypes.ResultConsensusState, error) {
   124  	return core.ConsensusState(&rpctypes.Context{})
   125  }
   126  
   127  func (c Client) DumpConsensusState(ctx context.Context) (*ctypes.ResultDumpConsensusState, error) {
   128  	return core.DumpConsensusState(&rpctypes.Context{})
   129  }
   130  
   131  func (c Client) ConsensusParams(ctx context.Context, height *int64) (*ctypes.ResultConsensusParams, error) {
   132  	return core.ConsensusParams(&rpctypes.Context{}, height)
   133  }
   134  
   135  func (c Client) Health(ctx context.Context) (*ctypes.ResultHealth, error) {
   136  	return core.Health(&rpctypes.Context{})
   137  }
   138  
   139  func (c Client) DialSeeds(ctx context.Context, seeds []string) (*ctypes.ResultDialSeeds, error) {
   140  	return core.UnsafeDialSeeds(&rpctypes.Context{}, seeds)
   141  }
   142  
   143  func (c Client) DialPeers(
   144  	ctx context.Context,
   145  	peers []string,
   146  	persistent,
   147  	unconditional,
   148  	private bool,
   149  ) (*ctypes.ResultDialPeers, error) {
   150  	return core.UnsafeDialPeers(&rpctypes.Context{}, peers, persistent, unconditional, private)
   151  }
   152  
   153  func (c Client) BlockchainInfo(ctx context.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
   154  	return core.BlockchainInfo(&rpctypes.Context{}, minHeight, maxHeight)
   155  }
   156  
   157  func (c Client) Genesis(ctx context.Context) (*ctypes.ResultGenesis, error) {
   158  	return core.Genesis(&rpctypes.Context{})
   159  }
   160  
   161  func (c Client) Block(ctx context.Context, height *int64) (*ctypes.ResultBlock, error) {
   162  	return core.Block(&rpctypes.Context{}, height)
   163  }
   164  
   165  func (c Client) BlockByHash(ctx context.Context, hash []byte) (*ctypes.ResultBlock, error) {
   166  	return core.BlockByHash(&rpctypes.Context{}, hash)
   167  }
   168  
   169  func (c Client) Commit(ctx context.Context, height *int64) (*ctypes.ResultCommit, error) {
   170  	return core.Commit(&rpctypes.Context{}, height)
   171  }
   172  
   173  func (c Client) Validators(ctx context.Context, height *int64, page, perPage *int) (*ctypes.ResultValidators, error) {
   174  	return core.Validators(&rpctypes.Context{}, height, page, perPage)
   175  }
   176  
   177  func (c Client) BroadcastEvidence(ctx context.Context, ev types.Evidence) (*ctypes.ResultBroadcastEvidence, error) {
   178  	return core.BroadcastEvidence(&rpctypes.Context{}, ev)
   179  }