github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/tendermint/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/fibonacci-chain/fbc/libs/tendermint/libs/bytes"
    21  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/service"
    22  	"github.com/fibonacci-chain/fbc/libs/tendermint/rpc/client"
    23  	"github.com/fibonacci-chain/fbc/libs/tendermint/rpc/core"
    24  	ctypes "github.com/fibonacci-chain/fbc/libs/tendermint/rpc/core/types"
    25  	rpctypes "github.com/fibonacci-chain/fbc/libs/tendermint/rpc/jsonrpc/types"
    26  	"github.com/fibonacci-chain/fbc/libs/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  type Call struct {
    50  	Name     string
    51  	Args     interface{}
    52  	Response interface{}
    53  	Error    error
    54  }
    55  
    56  // GetResponse will generate the apporiate response for us, when
    57  // using the Call struct to configure a Mock handler.
    58  //
    59  // When configuring a response, if only one of Response or Error is
    60  // set then that will always be returned. If both are set, then
    61  // we return Response if the Args match the set args, Error otherwise.
    62  func (c Call) GetResponse(args interface{}) (interface{}, error) {
    63  	// handle the case with no response
    64  	if c.Response == nil {
    65  		if c.Error == nil {
    66  			panic("Misconfigured call, you must set either Response or Error")
    67  		}
    68  		return nil, c.Error
    69  	}
    70  	// response without error
    71  	if c.Error == nil {
    72  		return c.Response, nil
    73  	}
    74  	// have both, we must check args....
    75  	if reflect.DeepEqual(args, c.Args) {
    76  		return c.Response, nil
    77  	}
    78  	return nil, c.Error
    79  }
    80  
    81  func (c Client) Status() (*ctypes.ResultStatus, error) {
    82  	return core.Status(&rpctypes.Context{})
    83  }
    84  
    85  func (c Client) ABCIInfo() (*ctypes.ResultABCIInfo, error) {
    86  	return core.ABCIInfo(&rpctypes.Context{})
    87  }
    88  
    89  func (c Client) ABCIQuery(path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error) {
    90  	return c.ABCIQueryWithOptions(path, data, client.DefaultABCIQueryOptions)
    91  }
    92  
    93  func (c Client) ABCIQueryWithOptions(
    94  	path string,
    95  	data bytes.HexBytes,
    96  	opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
    97  	return core.ABCIQuery(&rpctypes.Context{}, path, data, opts.Height, opts.Prove)
    98  }
    99  
   100  func (c Client) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
   101  	return core.BroadcastTxCommit(&rpctypes.Context{}, tx)
   102  }
   103  
   104  func (c Client) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
   105  	return core.BroadcastTxAsync(&rpctypes.Context{}, tx)
   106  }
   107  
   108  func (c Client) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
   109  	return core.BroadcastTxSync(&rpctypes.Context{}, tx)
   110  }
   111  
   112  func (c Client) NetInfo() (*ctypes.ResultNetInfo, error) {
   113  	return core.NetInfo(&rpctypes.Context{})
   114  }
   115  
   116  func (c Client) ConsensusState() (*ctypes.ResultConsensusState, error) {
   117  	return core.ConsensusState(&rpctypes.Context{})
   118  }
   119  
   120  func (c Client) DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) {
   121  	return core.DumpConsensusState(&rpctypes.Context{})
   122  }
   123  
   124  func (c Client) ConsensusParams(height *int64) (*ctypes.ResultConsensusParams, error) {
   125  	return core.ConsensusParams(&rpctypes.Context{}, height)
   126  }
   127  
   128  func (c Client) Health() (*ctypes.ResultHealth, error) {
   129  	return core.Health(&rpctypes.Context{})
   130  }
   131  
   132  func (c Client) DialSeeds(seeds []string) (*ctypes.ResultDialSeeds, error) {
   133  	return core.UnsafeDialSeeds(&rpctypes.Context{}, seeds)
   134  }
   135  
   136  func (c Client) DialPeers(peers []string, persistent bool) (*ctypes.ResultDialPeers, error) {
   137  	return core.UnsafeDialPeers(&rpctypes.Context{}, peers, persistent)
   138  }
   139  
   140  func (c Client) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
   141  	return core.BlockchainInfo(&rpctypes.Context{}, minHeight, maxHeight)
   142  }
   143  
   144  func (c Client) LatestBlockNumber() (int64, error) {
   145  	return core.LatestBlockNumber()
   146  }
   147  
   148  func (c Client) Genesis() (*ctypes.ResultGenesis, error) {
   149  	return core.Genesis(&rpctypes.Context{})
   150  }
   151  
   152  func (c Client) Block(height *int64) (*ctypes.ResultBlock, error) {
   153  	return core.Block(&rpctypes.Context{}, height)
   154  }
   155  
   156  func (c Client) Commit(height *int64) (*ctypes.ResultCommit, error) {
   157  	return core.Commit(&rpctypes.Context{}, height)
   158  }
   159  
   160  func (c Client) Validators(height *int64, page, perPage int) (*ctypes.ResultValidators, error) {
   161  	return core.Validators(&rpctypes.Context{}, height, page, perPage)
   162  }
   163  
   164  func (c Client) BroadcastEvidence(ev types.Evidence) (*ctypes.ResultBroadcastEvidence, error) {
   165  	return core.BroadcastEvidence(&rpctypes.Context{}, ev)
   166  }