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