-
Notifications
You must be signed in to change notification settings - Fork 99
/
basic_client.go
228 lines (190 loc) · 6.07 KB
/
basic_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package rpc
import (
"fmt"
"time"
"github.com/pkg/errors"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/rpc/client"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/tendermint/tendermint/rpc/lib/client"
"github.com/tendermint/tendermint/types"
ntypes "github.com/binance-chain/go-sdk/common/types"
"github.com/binance-chain/go-sdk/keys"
"github.com/binance-chain/go-sdk/types/tx"
)
var DefaultTimeout = 5 * time.Second
type ABCIClient interface {
// Reading from abci app
ABCIInfo() (*ctypes.ResultABCIInfo, error)
ABCIQuery(path string, data cmn.HexBytes) (*ctypes.ResultABCIQuery, error)
ABCIQueryWithOptions(path string, data cmn.HexBytes,
opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error)
// Writing to abci app
BroadcastTxCommit(tx types.Tx) (*ResultBroadcastTxCommit, error)
BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error)
BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error)
}
type SignClient interface {
Block(height *int64) (*ctypes.ResultBlock, error)
BlockResults(height *int64) (*ResultBlockResults, error)
Commit(height *int64) (*ctypes.ResultCommit, error)
Validators(height *int64) (*ctypes.ResultValidators, error)
Tx(hash []byte, prove bool) (*ResultTx, error)
TxSearch(query string, prove bool, page, perPage int) (*ResultTxSearch, error)
}
type Client interface {
cmn.Service
ABCIClient
SignClient
client.HistoryClient
client.StatusClient
EventsClient
DexClient
OpsClient
}
type EventsClient interface {
Subscribe(query string, outCapacity ...int) (out chan ctypes.ResultEvent, err error)
Unsubscribe(query string) error
UnsubscribeAll() error
}
func NewRPCClient(nodeURI string, network ntypes.ChainNetwork) *HTTP {
ntypes.Network = network
return NewHTTP(nodeURI, "/websocket")
}
type HTTP struct {
*WSEvents
key keys.KeyManager
}
// NewHTTP takes a remote endpoint in the form tcp://<host>:<port>
// and the websocket path (which always seems to be "/websocket")
func NewHTTP(remote, wsEndpoint string) *HTTP {
rc := rpcclient.NewJSONRPCClient(remote)
cdc := rc.Codec()
ctypes.RegisterAmino(cdc)
ntypes.RegisterWire(cdc)
tx.RegisterCodec(cdc)
rc.SetCodec(cdc)
wsEvent := newWSEvents(cdc, remote, wsEndpoint)
client := &HTTP{
WSEvents: wsEvent,
}
client.Start()
return client
}
func (c *HTTP) Status() (*ctypes.ResultStatus, error) {
return c.WSEvents.Status()
}
func (c *HTTP) ABCIInfo() (*ctypes.ResultABCIInfo, error) {
return c.WSEvents.ABCIInfo()
}
func (c *HTTP) ABCIQuery(path string, data cmn.HexBytes) (*ctypes.ResultABCIQuery, error) {
return c.ABCIQueryWithOptions(path, data, client.DefaultABCIQueryOptions)
}
func (c *HTTP) ABCIQueryWithOptions(path string, data cmn.HexBytes, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
if err := ValidateABCIPath(path); err != nil {
return nil, err
}
if err := ValidateABCIData(data); err != nil {
return nil, err
}
return c.WSEvents.ABCIQueryWithOptions(path, data, opts)
}
func (c *HTTP) BroadcastTxCommit(tx types.Tx) (*ResultBroadcastTxCommit, error) {
if err := ValidateTx(tx); err != nil {
return nil, err
}
return c.WSEvents.BroadcastTxCommit(tx)
}
func (c *HTTP) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
if err := ValidateTx(tx); err != nil {
return nil, err
}
return c.WSEvents.BroadcastTx("broadcast_tx_async", tx)
}
func (c *HTTP) BroadcastTxSync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
if err := ValidateTx(tx); err != nil {
return nil, err
}
return c.WSEvents.BroadcastTx("broadcast_tx_sync", tx)
}
func (c *HTTP) UnconfirmedTxs(limit int) (*ctypes.ResultUnconfirmedTxs, error) {
if err := ValidateUnConfirmedTxsLimit(limit); err != nil {
return nil, err
}
return c.WSEvents.UnconfirmedTxs(limit)
}
func (c *HTTP) NumUnconfirmedTxs() (*ctypes.ResultUnconfirmedTxs, error) {
return c.WSEvents.NumUnconfirmedTxs()
}
func (c *HTTP) NetInfo() (*ctypes.ResultNetInfo, error) {
return c.WSEvents.NetInfo()
}
func (c *HTTP) DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) {
return c.WSEvents.DumpConsensusState()
}
func (c *HTTP) ConsensusState() (*ctypes.ResultConsensusState, error) {
return c.WSEvents.ConsensusState()
}
func (c *HTTP) Health() (*ctypes.ResultHealth, error) {
return c.WSEvents.Health()
}
func (c *HTTP) BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
if err := ValidateHeightRange(minHeight, maxHeight); err != nil {
return nil, err
}
return c.WSEvents.BlockchainInfo(minHeight, maxHeight)
}
func (c *HTTP) Genesis() (*ctypes.ResultGenesis, error) {
return c.WSEvents.Genesis()
}
func (c *HTTP) Block(height *int64) (*ctypes.ResultBlock, error) {
if err := ValidateHeight(height); err != nil {
return nil, err
}
return c.WSEvents.Block(height)
}
func (c *HTTP) BlockResults(height *int64) (*ResultBlockResults, error) {
if err := ValidateHeight(height); err != nil {
return nil, err
}
return c.WSEvents.BlockResults(height)
}
func (c *HTTP) Commit(height *int64) (*ctypes.ResultCommit, error) {
if err := ValidateHeight(height); err != nil {
return nil, err
}
return c.WSEvents.Commit(height)
}
func (c *HTTP) Tx(hash []byte, prove bool) (*ResultTx, error) {
if err := ValidateHash(hash); err != nil {
return nil, err
}
return c.WSEvents.Tx(hash, prove)
}
func (c *HTTP) TxSearch(query string, prove bool, page, perPage int) (*ResultTxSearch, error) {
if err := ValidateABCIQueryStr(query); err != nil {
return nil, err
}
return c.WSEvents.TxSearch(query, prove, page, perPage)
}
func (c *HTTP) Validators(height *int64) (*ctypes.ResultValidators, error) {
if err := ValidateHeight(height); err != nil {
return nil, err
}
return c.WSEvents.Validators(height)
}
func (c *HTTP) QueryStore(key cmn.HexBytes, storeName string) ([]byte, error) {
path := fmt.Sprintf("/store/%s/%s", storeName, "key")
result, err := c.ABCIQuery(path, key)
if err != nil {
return nil, err
}
resp := result.Response
if !resp.IsOK() {
return nil, errors.Errorf(resp.Log)
}
return resp.Value, nil
}
func (c *HTTP) SetKeyManager(k keys.KeyManager) {
c.key = k
}