-
Notifications
You must be signed in to change notification settings - Fork 22
/
client.go
320 lines (259 loc) · 8.51 KB
/
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package blockchain
import (
"context"
"errors"
"sync"
"time"
"code.vegaprotocol.io/vega/commands"
"code.vegaprotocol.io/vega/libs/proto"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
"github.com/tendermint/tendermint/libs/bytes"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
)
var ErrClientNotReady = errors.New("tendermint client is not ready")
// nolint: interfacebloat
type ChainClientImpl interface {
GetGenesisTime(context.Context) (time.Time, error)
GetChainID(context.Context) (string, error)
GetStatus(context.Context) (*tmctypes.ResultStatus, error)
GetNetworkInfo(context.Context) (*tmctypes.ResultNetInfo, error)
GetUnconfirmedTxCount(context.Context) (int, error)
Health(context.Context) (*tmctypes.ResultHealth, error)
SendTransactionAsync(context.Context, []byte) (*tmctypes.ResultBroadcastTx, error)
SendTransactionSync(context.Context, []byte) (*tmctypes.ResultBroadcastTx, error)
CheckTransaction(context.Context, []byte) (*tmctypes.ResultCheckTx, error)
SendTransactionCommit(context.Context, []byte) (*tmctypes.ResultBroadcastTxCommit, error)
GenesisValidators(context.Context) ([]*tmtypes.Validator, error)
Validators(context.Context, *int64) ([]*tmtypes.Validator, error)
Subscribe(context.Context, func(tmctypes.ResultEvent) error, ...string) error
Start() error
}
// Client abstract all communication to the blockchain.
type Client struct {
*Config
clt ChainClientImpl
mu sync.RWMutex
}
// NewClient instantiate a new blockchain client.
func NewClient() *Client {
return &Client{
clt: nil,
}
}
func NewClientWithImpl(clt ChainClientImpl) *Client {
return &Client{
clt: clt,
}
}
func (c *Client) Set(clt ChainClientImpl) {
c.mu.Lock()
defer c.mu.Unlock()
c.clt = clt
}
func (c *Client) isReady() bool {
c.mu.RLock()
defer c.mu.RUnlock()
return c.clt != nil
}
func (c *Client) CheckRawTransaction(ctx context.Context, tx []byte) (*tmctypes.ResultCheckTx, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.clt.CheckTransaction(ctx, tx)
}
func (c *Client) CheckTransaction(ctx context.Context, tx *commandspb.Transaction) (*tmctypes.ResultCheckTx, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
chainID, err := c.clt.GetChainID(ctx)
if err != nil {
return nil, err
}
if _, err := commands.CheckTransaction(tx, chainID); err != nil {
return &tmctypes.ResultCheckTx{
ResponseCheckTx: NewResponseCheckTxError(AbciTxnDecodingFailure, err),
}, nil
}
marshalledTx, err := proto.Marshal(tx)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.clt.CheckTransaction(ctx, marshalledTx)
}
func (c *Client) SubmitTransactionSync(ctx context.Context, tx *commandspb.Transaction) (*tmctypes.ResultBroadcastTx, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
marshalledTx, err := proto.Marshal(tx)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
t, err := c.clt.SendTransactionSync(ctx, marshalledTx)
return t, err
}
func (c *Client) SubmitTransactionCommit(ctx context.Context, tx *commandspb.Transaction) (*tmctypes.ResultBroadcastTxCommit, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
chainID, err := c.clt.GetChainID(ctx)
if err != nil {
return nil, err
}
if _, err := commands.CheckTransaction(tx, chainID); err != nil {
return &tmctypes.ResultBroadcastTxCommit{
CheckTx: NewResponseCheckTxError(AbciTxnDecodingFailure, err),
}, nil
}
marshalledTx, err := proto.Marshal(tx)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
t, err := c.clt.SendTransactionCommit(ctx, marshalledTx)
return t, err
}
func (c *Client) SubmitTransactionAsync(ctx context.Context, tx *commandspb.Transaction) (*tmctypes.ResultBroadcastTx, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
chainID, err := c.clt.GetChainID(ctx)
if err != nil {
return nil, err
}
if _, err := commands.CheckTransaction(tx, chainID); err != nil {
return &tmctypes.ResultBroadcastTx{ //nolint:nilerr
Code: AbciTxnDecodingFailure,
Data: bytes.HexBytes(err.Error()),
}, nil
}
marshalledTx, err := proto.Marshal(tx)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
t, err := c.clt.SendTransactionAsync(ctx, marshalledTx)
return t, err
}
func (c *Client) SubmitRawTransactionSync(ctx context.Context, tx []byte) (*tmctypes.ResultBroadcastTx, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.clt.SendTransactionSync(timeoutCtx, tx)
}
func (c *Client) SubmitRawTransactionAsync(ctx context.Context, tx []byte) (*tmctypes.ResultBroadcastTx, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.clt.SendTransactionAsync(timeoutCtx, tx)
}
func (c *Client) SubmitRawTransactionCommit(ctx context.Context, tx []byte) (*tmctypes.ResultBroadcastTxCommit, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.clt.SendTransactionCommit(timeoutCtx, tx)
}
// GetGenesisTime retrieves the genesis time from the blockchain.
func (c *Client) GetGenesisTime(ctx context.Context) (genesisTime time.Time, err error) {
if !c.isReady() {
return time.Time{}, ErrClientNotReady
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.clt.GetGenesisTime(ctx)
}
// GetChainID retrieves the chainID from the blockchain.
func (c *Client) GetChainID(ctx context.Context) (chainID string, err error) {
if !c.isReady() {
return "", ErrClientNotReady
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.clt.GetChainID(ctx)
}
// GetStatus returns the current status of the chain.
func (c *Client) GetStatus(ctx context.Context) (status *tmctypes.ResultStatus, err error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.clt.GetStatus(ctx)
}
// GetNetworkInfo return information of the current network.
func (c *Client) GetNetworkInfo(ctx context.Context) (netInfo *tmctypes.ResultNetInfo, err error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.clt.GetNetworkInfo(ctx)
}
// GetUnconfirmedTxCount return the current count of unconfirmed transactions.
func (c *Client) GetUnconfirmedTxCount(ctx context.Context) (count int, err error) {
if !c.isReady() {
return 0, ErrClientNotReady
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.clt.GetUnconfirmedTxCount(ctx)
}
// Health returns the result of the health endpoint of the chain.
func (c *Client) Health() (*tmctypes.ResultHealth, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return c.clt.Health(ctx)
}
func (c *Client) GenesisValidators() ([]*tmtypes.Validator, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return c.clt.GenesisValidators(ctx)
}
func (c *Client) Validators(height *int64) ([]*tmtypes.Validator, error) {
if !c.isReady() {
return nil, ErrClientNotReady
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return c.clt.Validators(ctx, height)
}
func (c *Client) Subscribe(ctx context.Context, fn func(tmctypes.ResultEvent) error, queries ...string) error {
if !c.isReady() {
return ErrClientNotReady
}
return c.clt.Subscribe(ctx, fn, queries...)
}
func (c *Client) Start() error {
return c.clt.Start()
}