-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathchaincmp.go
377 lines (339 loc) · 9.91 KB
/
chaincmp.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"time"
"github.com/pkg/errors"
flag "github.com/spf13/pflag"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/wavesplatform/gowaves/pkg/client"
"github.com/wavesplatform/gowaves/pkg/proto"
)
const (
defaultURL = "https://nodes.wavesnodes.com"
defaultScheme = "http"
)
var (
version = "v0.0.0"
interruptSignals = []os.Signal{os.Interrupt}
errInvalidParameters = errors.New("invalid parameters")
errUserTermination = errors.New("user termination")
errFailure = errors.New("operation failure")
errFork = errors.New("the node is on fork")
errUnavailable = errors.New("remote service is unavailable")
)
func main() {
err := run()
if err != nil {
switch err {
case errInvalidParameters:
showUsageAndExit()
os.Exit(2)
case errUserTermination:
os.Exit(130)
case errFork:
os.Exit(1)
case errUnavailable:
os.Exit(69)
case errFailure:
os.Exit(70)
}
}
}
func run() error {
var showHelp bool
var showVersion bool
var node string
var reference string
var verbose bool
var silent bool
flag.StringVarP(&node, "node", "n", "", "URL of the node")
flag.StringVarP(&reference, "references", "r", defaultURL, "A list of space-separated URLs of reference nodes, for example \"http://127.0.0.1:6869 https://nodes.wavesnodes.com\"")
flag.BoolVarP(&showHelp, "help", "h", false, "Print usage information (this message) and quit")
flag.BoolVarP(&showVersion, "version", "v", false, "Print version information and quit")
flag.BoolVar(&verbose, "verbose", false, "Logs additional information; incompatible with \"silent\"")
flag.BoolVar(&silent, "silent", false, "Produce no output except this help message; incompatible with \"verbose\"")
flag.Parse()
if showHelp {
showUsageAndExit()
return nil
}
if showVersion {
fmt.Printf("chaincmp %s\n", version)
return nil
}
if silent && verbose {
return errInvalidParameters
}
setupLogger(silent, verbose)
if node == "" || len(strings.Fields(node)) > 1 {
zap.S().Errorf("Invalid node's URL '%s'", node)
return errInvalidParameters
}
node, err := checkAndUpdateURL(node)
if err != nil {
zap.S().Errorf("Incorrect node's URL: %s", err.Error())
return errInvalidParameters
}
other := strings.Fields(reference)
for i, u := range other {
u, err = checkAndUpdateURL(u)
if err != nil {
zap.S().Errorf("Incorrect reference's URL: %s", err.Error())
return errInvalidParameters
}
other[i] = u
}
zap.S().Debugf("Node to check: %s", node)
zap.S().Debugf("Reference nodes (%d): %s", len(other), other)
urls := append([]string{node}, other...)
zap.S().Debugf("Requesting height from %d nodes", len(urls))
interrupt := interruptListener()
clients := make([]*client.Client, len(urls))
for i, u := range urls {
c, err := client.NewClient(client.Options{BaseUrl: u, Client: &http.Client{}})
if err != nil {
zap.S().Errorf("Failed to create client for URL '%s': %s", u, err)
return errFailure
}
clients[i] = c
}
hs, err := heights(interrupt, clients)
if err != nil {
zap.S().Errorf("Failed to retrieve heights from all nodes: %s", err)
if interrupted(interrupt) {
return errUserTermination
}
return errUnavailable
}
for i, h := range hs {
zap.S().Debugf("%d: Height = %d", i, h)
}
stop := min(hs)
zap.S().Infof("Lowest height: %d", stop)
ch, err := findLastCommonHeight(interrupt, clients, 1, stop)
if err != nil {
zap.S().Errorf("Failed to find last common height: %s", err)
if interrupted(interrupt) {
return errUserTermination
}
return err
}
h := hs[0]
zap.S().Debugf("Node height: %d", h)
refLowest := min(hs[1:])
zap.S().Debugf("The lowest height of reference nodes: %d", refLowest)
switch {
case ch == h && ch < refLowest: // The node is behind the reference nodes
zap.S().Infof("Node '%s' is %d blocks behind the lowest reference node", node, refLowest-h)
return nil
case ch == refLowest && ch < h: // The node is ahead of the reference nodes
zap.S().Infof("Node '%s' is %d blocks ahead of the lowest reference node", node, h-refLowest)
return nil
case ch < h && ch < refLowest:
fl := h - ch
zap.S().Warnf("Node '%s' is on fork of length %d blocks since last common block at height %d", node, fl, ch)
switch {
case fl < 10:
zap.S().Infof("The fork is very short, highly likely the node is OK")
return nil
case fl < 100:
zap.S().Warn("The fork is short and possibly the node will rollback and switch on the correct fork automatically")
zap.S().Warnf("But if you want to rollback manually, refer the documentation at https://docs.wavesplatform.com/en/waves-full-node/how-to-rollback-a-node.html")
return errFork
case fl < 1980:
zap.S().Warnf("Manual rollback of the node '%s' is possible, do it as soon as possible!", node)
zap.S().Warnf("Please, read the documentation at https://docs.wavesplatform.com/en/waves-full-node/how-to-rollback-a-node.html")
return errFork
default:
zap.S().Warnf("Rollback of node '%s' is not an option, the fork is too long, consider restarting the node from scratch!", node)
zap.S().Warnf("Please, refer the documentation at https://docs.wavesplatform.com/en/waves-full-node/options-for-getting-actual-blockchain.html")
return errFork
}
default:
zap.S().Infof("Node '%s' is OK", node)
return nil
}
}
func checkAndUpdateURL(s string) (string, error) {
var u *url.URL
var err error
if strings.Contains(s, "//") {
u, err = url.Parse(s)
} else {
u, err = url.Parse("//" + s)
}
if err != nil {
return "", errors.Wrapf(err, "failed to parse URL '%s'", s)
}
if u.Scheme == "" {
u.Scheme = defaultScheme
}
if u.Scheme != "http" && u.Scheme != "https" {
return "", errors.Errorf("unsupported URL scheme '%s'", u.Scheme)
}
return u.String(), nil
}
func findLastCommonHeight(interrupt <-chan struct{}, clients []*client.Client, start, stop int) (int, error) {
var r int
for start <= stop {
if interrupted(interrupt) {
return 0, errUserTermination
}
middle := (start + stop) / 2
c, err := differentIdsCount(clients, middle)
if err != nil {
return 0, errors.Wrapf(err, "failed to get blocks signatures at height %d", middle)
}
if c >= 2 {
stop = middle - 1
r = stop
} else {
start = middle + 1
r = middle
}
}
return r, nil
}
type nodeBlockInfo struct {
id int
blockID proto.BlockID
height uint64
generator proto.WavesAddress
blockTime uint64
err error
}
func differentIdsCount(clients []*client.Client, height int) (int, error) {
ch := make(chan nodeBlockInfo, len(clients))
info := make(map[int]nodeBlockInfo)
m := make(map[proto.BlockID]bool)
for i, c := range clients {
go func(id int, cl *client.Client) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
header, resp, err := cl.Blocks.HeadersAt(ctx, uint64(height))
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
block, _, err := cl.Blocks.At(ctx, uint64(height))
if err != nil {
ch <- nodeBlockInfo{id: id, err: err}
return
}
ch <- nodeBlockInfo{id: id, height: block.Height, blockID: block.ID, generator: block.Generator, blockTime: block.Timestamp}
return
}
ch <- nodeBlockInfo{id: id, err: err}
return
}
ch <- nodeBlockInfo{id: id, height: header.Height, blockID: header.ID, generator: header.Generator, blockTime: header.Timestamp}
}(i, c)
}
for range clients {
bi := <-ch
if bi.err != nil {
return 0, errors.Wrapf(bi.err, "failed to get block header from %dth client", bi.id)
}
info[bi.id] = bi
m[bi.blockID] = true
}
for i := 0; i < len(info); i++ {
v := info[i]
t := time.Unix(0, int64(v.blockTime*1000000))
zap.S().Debugf("id: %d, h: %d, block: %s, generator: %s, time: %s", i, v.height, v.blockID.String(), v.generator.String(), t.String())
}
return len(m), nil
}
func min(values []int) int {
r := values[0]
for _, v := range values {
if v < r {
r = v
}
}
return r
}
func showUsageAndExit() {
_, _ = fmt.Fprintf(os.Stderr, "\nUsage of chaincmp %s\n", version)
flag.PrintDefaults()
}
type nodeHeight struct {
id int
height int
err error
}
func height(interrupt <-chan struct{}, c *client.Client, id int, ch chan nodeHeight) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
go func() {
<-interrupt
cancel()
}()
bh, _, err := c.Blocks.Height(ctx)
if err != nil {
ch <- nodeHeight{id, 0, err}
return
}
ch <- nodeHeight{id, int(bh.Height), nil}
}
func heights(interrupt <-chan struct{}, clients []*client.Client) ([]int, error) {
ch := make(chan nodeHeight, len(clients))
heights := make(map[int]int)
for i, c := range clients {
go height(interrupt, c, i, ch)
}
for range clients {
nh := <-ch
if nh.err != nil {
return nil, errors.Wrapf(nh.err, "failed to retrieve height from %dth client", nh.id)
}
heights[nh.id] = nh.height
}
r := make([]int, len(heights))
for i, height := range heights {
r[i] = height
}
return r, nil
}
func setupLogger(silent, verbose bool) {
al := zap.NewAtomicLevel()
al.SetLevel(zap.InfoLevel)
if silent {
al.SetLevel(zap.FatalLevel)
}
if verbose {
al.SetLevel(zap.DebugLevel)
}
ec := zap.NewDevelopmentEncoderConfig()
logger := zap.New(zapcore.NewCore(zapcore.NewConsoleEncoder(ec), zapcore.Lock(os.Stdout), al))
zap.ReplaceGlobals(logger)
}
func interruptListener() <-chan struct{} {
r := make(chan struct{})
go func() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, interruptSignals...)
sig := <-signals
zap.S().Infof("Caught signal '%s', shutting down...", sig)
close(r)
for sig := range signals {
zap.S().Infof("Caught signal '%s' again, already shutting down", sig)
}
}()
return r
}
func interrupted(interrupt <-chan struct{}) bool {
select {
case <-interrupt:
return true
default:
}
return false
}