github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/rpc/client/interface.go (about) 1 package client 2 3 /* 4 The client package provides a general purpose interface (Client) for connecting 5 to an ostracon node, as well as higher-level functionality. 6 7 The main implementation for production code is client.HTTP, which 8 connects via http to the jsonrpc interface of the ostracon node. 9 10 For connecting to a node running in the same process (eg. when 11 compiling the abci app in the same process), you can use the client.Local 12 implementation. 13 14 For mocking out server responses during testing to see behavior for 15 arbitrary return values, use the mock package. 16 17 In addition to the Client interface, which should be used externally 18 for maximum flexibility and testability, and two implementations, 19 this package also provides helper functions that work on any Client 20 implementation. 21 */ 22 23 import ( 24 "context" 25 26 "github.com/line/ostracon/libs/bytes" 27 "github.com/line/ostracon/libs/service" 28 ctypes "github.com/line/ostracon/rpc/core/types" 29 "github.com/line/ostracon/types" 30 ) 31 32 // Client wraps most important rpc calls a client would make if you want to 33 // listen for events, test if it also implements events.EventSwitch. 34 //go:generate mockery --case underscore --name Client 35 type Client interface { 36 service.Service 37 ABCIClient 38 EventsClient 39 HistoryClient 40 NetworkClient 41 SignClient 42 StatusClient 43 EvidenceClient 44 MempoolClient 45 } 46 47 // ABCIClient groups together the functionality that principally affects the 48 // ABCI app. 49 // 50 // In many cases this will be all we want, so we can accept an interface which 51 // is easier to mock. 52 type ABCIClient interface { 53 // Reading from abci app 54 ABCIInfo(context.Context) (*ctypes.ResultABCIInfo, error) 55 ABCIQuery(ctx context.Context, path string, data bytes.HexBytes) (*ctypes.ResultABCIQuery, error) 56 ABCIQueryWithOptions(ctx context.Context, path string, data bytes.HexBytes, 57 opts ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) 58 59 // Writing to abci app 60 BroadcastTxCommit(context.Context, types.Tx) (*ctypes.ResultBroadcastTxCommit, error) 61 BroadcastTxAsync(context.Context, types.Tx) (*ctypes.ResultBroadcastTx, error) 62 BroadcastTxSync(context.Context, types.Tx) (*ctypes.ResultBroadcastTx, error) 63 } 64 65 // SignClient groups together the functionality needed to get valid signatures 66 // and prove anything about the chain. 67 type SignClient interface { 68 Block(ctx context.Context, height *int64) (*ctypes.ResultBlock, error) 69 BlockByHash(ctx context.Context, hash []byte) (*ctypes.ResultBlock, error) 70 BlockResults(ctx context.Context, height *int64) (*ctypes.ResultBlockResults, error) 71 Commit(ctx context.Context, height *int64) (*ctypes.ResultCommit, error) 72 Validators(ctx context.Context, height *int64, page, perPage *int) (*ctypes.ResultValidators, error) 73 Tx(ctx context.Context, hash []byte, prove bool) (*ctypes.ResultTx, error) 74 75 // TxSearch defines a method to search for a paginated set of transactions by 76 // DeliverTx event search criteria. 77 TxSearch( 78 ctx context.Context, 79 query string, 80 prove bool, 81 page, perPage *int, 82 orderBy string, 83 ) (*ctypes.ResultTxSearch, error) 84 85 // BlockSearch defines a method to search for a paginated set of blocks by 86 // BeginBlock and EndBlock event search criteria. 87 BlockSearch( 88 ctx context.Context, 89 query string, 90 page, perPage *int, 91 orderBy string, 92 ) (*ctypes.ResultBlockSearch, error) 93 } 94 95 // HistoryClient provides access to data from genesis to now in large chunks. 96 type HistoryClient interface { 97 Genesis(context.Context) (*ctypes.ResultGenesis, error) 98 GenesisChunked(context.Context, uint) (*ctypes.ResultGenesisChunk, error) 99 BlockchainInfo(ctx context.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) 100 } 101 102 // StatusClient provides access to general chain info. 103 type StatusClient interface { 104 Status(context.Context) (*ctypes.ResultStatus, error) 105 } 106 107 // NetworkClient is general info about the network state. May not be needed 108 // usually. 109 type NetworkClient interface { 110 NetInfo(context.Context) (*ctypes.ResultNetInfo, error) 111 DumpConsensusState(context.Context) (*ctypes.ResultDumpConsensusState, error) 112 ConsensusState(context.Context) (*ctypes.ResultConsensusState, error) 113 ConsensusParams(ctx context.Context, height *int64) (*ctypes.ResultConsensusParams, error) 114 Health(context.Context) (*ctypes.ResultHealth, error) 115 } 116 117 // EventsClient is reactive, you can subscribe to any message, given the proper 118 // string. see ostracon/types/events.go 119 type EventsClient interface { 120 // Subscribe subscribes given subscriber to query. Returns a channel with 121 // cap=1 onto which events are published. An error is returned if it fails to 122 // subscribe. outCapacity can be used optionally to set capacity for the 123 // channel. Channel is never closed to prevent accidental reads. 124 // 125 // ctx cannot be used to unsubscribe. To unsubscribe, use either Unsubscribe 126 // or UnsubscribeAll. 127 Subscribe(ctx context.Context, subscriber, query string, outCapacity ...int) (out <-chan ctypes.ResultEvent, err error) 128 // Unsubscribe unsubscribes given subscriber from query. 129 Unsubscribe(ctx context.Context, subscriber, query string) error 130 // UnsubscribeAll unsubscribes given subscriber from all the queries. 131 UnsubscribeAll(ctx context.Context, subscriber string) error 132 } 133 134 // MempoolClient shows us data about current mempool state. 135 type MempoolClient interface { 136 UnconfirmedTxs(ctx context.Context, limit *int) (*ctypes.ResultUnconfirmedTxs, error) 137 NumUnconfirmedTxs(context.Context) (*ctypes.ResultUnconfirmedTxs, error) 138 CheckTx(context.Context, types.Tx) (*ctypes.ResultCheckTx, error) 139 } 140 141 // EvidenceClient is used for submitting an evidence of the malicious 142 // behaviour. 143 type EvidenceClient interface { 144 BroadcastEvidence(context.Context, types.Evidence) (*ctypes.ResultBroadcastEvidence, error) 145 } 146 147 // RemoteClient is a Client, which can also return the remote network address. 148 //go:generate mockery --case underscore --name RemoteClient 149 type RemoteClient interface { 150 Client 151 152 // Remote returns the remote network address in a string form. 153 Remote() string 154 }