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