-
Notifications
You must be signed in to change notification settings - Fork 19
/
retrying_node.go
266 lines (237 loc) · 8.1 KB
/
retrying_node.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
package node
import (
"context"
"fmt"
"time"
apipb "code.vegaprotocol.io/vega/protos/vega/api/v1"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
"code.vegaprotocol.io/vega/wallet/api/node/adapters"
nodetypes "code.vegaprotocol.io/vega/wallet/api/node/types"
"github.com/cenkalti/backoff/v4"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
)
//go:generate go run github.com/golang/mock/mockgen -destination mocks/node_mock.go -package mocks code.vegaprotocol.io/vega/wallet/api/node GRPCAdapter
type GRPCAdapter interface {
Host() string
Statistics(ctx context.Context) (nodetypes.Statistics, error)
SpamStatistics(ctx context.Context, pubKey string) (nodetypes.SpamStatistics, error)
SubmitTransaction(ctx context.Context, in *apipb.SubmitTransactionRequest) (*apipb.SubmitTransactionResponse, error)
CheckTransaction(ctx context.Context, in *apipb.CheckTransactionRequest) (*apipb.CheckTransactionResponse, error)
LastBlock(ctx context.Context) (nodetypes.LastBlock, error)
Stop() error
}
type RetryingNode struct {
log *zap.Logger
grpcAdapter GRPCAdapter
retries uint64
requestTTL time.Duration
}
func (n *RetryingNode) Host() string {
return n.grpcAdapter.Host()
}
func (n *RetryingNode) Statistics(ctx context.Context) (nodetypes.Statistics, error) {
n.log.Debug("querying the node statistics through the graphQL API", zap.String("host", n.grpcAdapter.Host()))
requestTime := time.Now()
ctx, cancel := context.WithTimeout(ctx, n.requestTTL)
defer cancel()
resp, err := n.grpcAdapter.Statistics(ctx)
if err != nil {
n.log.Error("could not get the statistics",
zap.String("host", n.grpcAdapter.Host()),
zap.Error(err),
)
return nodetypes.Statistics{}, err
}
n.log.Debug("response from Statistics",
zap.String("host", n.grpcAdapter.Host()),
zap.Uint64("block-height", resp.BlockHeight),
zap.String("block-hash", resp.BlockHash),
zap.String("chain-id", resp.ChainID),
zap.String("vega-time", resp.VegaTime),
zap.Time("request-time", requestTime),
)
return resp, nil
}
func (n *RetryingNode) SpamStatistics(ctx context.Context, pubKey string) (nodetypes.SpamStatistics, error) {
n.log.Debug("querying the node statistics through the graphQL API", zap.String("host", n.grpcAdapter.Host()))
requestTime := time.Now()
ctx, cancel := context.WithTimeout(ctx, n.requestTTL)
defer cancel()
resp, err := n.grpcAdapter.SpamStatistics(ctx, pubKey)
if err != nil {
n.log.Error("could not get the statistics",
zap.String("host", n.grpcAdapter.Host()),
zap.Error(err),
)
return nodetypes.SpamStatistics{}, err
}
n.log.Debug("response from SpamStatistics",
zap.String("host", n.grpcAdapter.Host()),
zap.String("chain-id", resp.ChainID),
zap.Uint64("epoch", resp.EpochSeq),
zap.Uint64("block-height", resp.LastBlockHeight),
zap.Uint64("prosposals-count-for-epoch", resp.Proposals.CountForEpoch),
zap.Uint64("transfers-count-for-epoch", resp.Transfers.CountForEpoch),
zap.Uint64("delegations-count-for-epoch", resp.Delegations.CountForEpoch),
zap.Uint64("issue-signatures-count-for-epoch", resp.IssuesSignatures.CountForEpoch),
zap.Uint64("node-announcements-count-for-epoch", resp.NodeAnnouncements.CountForEpoch),
zap.Time("request-time", requestTime),
)
return resp, nil
}
// LastBlock returns information about the last block acknowledged by the node.
func (n *RetryingNode) LastBlock(ctx context.Context) (nodetypes.LastBlock, error) {
n.log.Debug("getting the last block from the gRPC API", zap.String("host", n.grpcAdapter.Host()))
var resp nodetypes.LastBlock
if err := n.retry(func() error {
requestTime := time.Now()
ctx, cancel := context.WithTimeout(ctx, n.requestTTL)
defer cancel()
r, err := n.grpcAdapter.LastBlock(ctx)
if err != nil {
return err
}
resp = r
n.log.Debug("response from LastBlockHeight",
zap.String("host", n.grpcAdapter.Host()),
zap.Uint64("block-height", r.BlockHeight),
zap.String("block-hash", r.BlockHash),
zap.Uint32("pow-difficulty", r.ProofOfWorkDifficulty),
zap.String("pow-hash-function", r.ProofOfWorkHashFunction),
zap.Time("request-time", requestTime),
)
return nil
}); err != nil {
n.log.Error("could not the get last block",
zap.String("host", n.grpcAdapter.Host()),
zap.Error(err),
)
return nodetypes.LastBlock{}, err
}
return resp, nil
}
func (n *RetryingNode) CheckTransaction(ctx context.Context, tx *commandspb.Transaction) error {
n.log.Debug("checking the transaction through the gRPC API", zap.String("host", n.grpcAdapter.Host()))
var resp *apipb.CheckTransactionResponse
if err := n.retry(func() error {
req := apipb.CheckTransactionRequest{
Tx: tx,
}
requestTime := time.Now()
ctx, cancel := context.WithTimeout(ctx, n.requestTTL)
defer cancel()
r, err := n.grpcAdapter.CheckTransaction(ctx, &req)
if err != nil {
return n.handleSubmissionError(err)
}
n.log.Debug("response from CheckTransaction",
zap.String("host", n.grpcAdapter.Host()),
zap.Bool("success", r.Success),
zap.Time("request-time", requestTime),
)
resp = r
return nil
}); err != nil {
return err
}
if !resp.Success {
return nodetypes.TransactionError{
ABCICode: resp.Code,
Message: resp.Data,
}
}
return nil
}
func (n *RetryingNode) SendTransaction(ctx context.Context, tx *commandspb.Transaction, ty apipb.SubmitTransactionRequest_Type) (string, error) {
n.log.Debug("sending the transaction through the gRPC API", zap.String("host", n.grpcAdapter.Host()))
var resp *apipb.SubmitTransactionResponse
if err := n.retry(func() error {
req := apipb.SubmitTransactionRequest{
Tx: tx,
Type: ty,
}
requestTime := time.Now()
ctx, cancel := context.WithTimeout(ctx, n.requestTTL)
defer cancel()
r, err := n.grpcAdapter.SubmitTransaction(ctx, &req)
if err != nil {
return n.handleSubmissionError(err)
}
n.log.Debug("response from SubmitTransaction",
zap.String("host", n.grpcAdapter.Host()),
zap.Bool("success", r.Success),
zap.String("hash", r.TxHash),
zap.Time("request-time", requestTime),
)
resp = r
return nil
}); err != nil {
return "", err
}
if !resp.Success {
return "", nodetypes.TransactionError{
ABCICode: resp.Code,
Message: resp.Data,
}
}
return resp.TxHash, nil
}
func (n *RetryingNode) Stop() error {
n.log.Debug("closing the gRPC API client", zap.String("host", n.grpcAdapter.Host()))
if err := n.grpcAdapter.Stop(); err != nil {
n.log.Warn("could not stop the gRPC API client-",
zap.String("host", n.grpcAdapter.Host()),
zap.Error(err),
)
return fmt.Errorf("could not close properly stop the gRPC API client: %w", err)
}
n.log.Info("the gRPC API client successfully closed", zap.String("host", n.grpcAdapter.Host()))
return nil
}
func (n *RetryingNode) handleSubmissionError(err error) error {
statusErr := intoStatusError(err)
if statusErr == nil {
n.log.Error("could not submit the transaction",
zap.String("host", n.grpcAdapter.Host()),
zap.Error(err),
)
return err
}
if statusErr.Code == codes.InvalidArgument {
n.log.Error(
"the transaction has been rejected because of an invalid argument or state, skipping retry...",
zap.String("host", n.grpcAdapter.Host()),
zap.Error(statusErr),
)
// Returning a permanent error kills the retry loop.
return backoff.Permanent(statusErr)
}
n.log.Error("could not submit the transaction",
zap.String("host", n.grpcAdapter.Host()),
zap.Error(statusErr),
)
return statusErr
}
func (n *RetryingNode) retry(o backoff.Operation) error {
return backoff.Retry(o, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), n.retries))
}
func NewRetryingNode(log *zap.Logger, host string, retries uint64, ttl time.Duration) (*RetryingNode, error) {
grpcAdapter, err := adapters.NewGRPCAdapter(host)
if err != nil {
log.Error("could not initialise an insecure gRPC adapter",
zap.String("host", host),
zap.Error(err),
)
return nil, err
}
return BuildRetryingNode(log, grpcAdapter, retries, ttl), nil
}
func BuildRetryingNode(log *zap.Logger, grpcAdapter GRPCAdapter, retries uint64, requestTTL time.Duration) *RetryingNode {
return &RetryingNode{
log: log,
grpcAdapter: grpcAdapter,
retries: retries,
requestTTL: requestTTL,
}
}