-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.go
89 lines (72 loc) · 2.25 KB
/
blocks.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
package main
import (
"context"
"net/http"
"regexp"
"strconv"
"time"
"github.com/eoscanada/dstore"
"go.uber.org/zap"
)
func (d *Diagnose) BlockHoles(w http.ResponseWriter, req *http.Request) {
blocksURL := getQueryParam(req, "blocks_url")
if blocksURL == "" {
blocksURL = d.BlocksStoreURL
}
const fileBlockSize = 100
zlog.Info("diagnose - block holes",
zap.String("block_store_url", blocksURL),
zap.Uint32("block_logs_size", fileBlockSize),
)
conn, err := d.upgrader.Upgrade(w, req, nil)
if err != nil {
return
}
defer conn.Close()
ctx, cancel := context.WithCancel(req.Context())
number := regexp.MustCompile(`(\d{10})`)
var expected uint32
var count int
var baseNum32 uint32
currentStartBlk := uint32(0)
startTime := time.Now()
go readWebsocket(conn, cancel)
zlog.Info("creating blocks store")
blocksStore, err := dstore.NewDBinStore(blocksURL)
if err != nil {
maybeSendWebsocket(conn, WebsocketTypeMessage, Message{Msg: err.Error()})
return
}
maybeSendWebsocket(conn, WebsocketTypeProgress, Progress{Elapsed: time.Now().Sub(startTime)})
blocksStore.Walk("", "", func(filename string) error {
select {
case <-ctx.Done():
zlog.Debug("context canceled")
return dstore.StopIteration
default:
}
if count%5000 == 0 {
maybeSendWebsocket(conn, WebsocketTypeProgress, Progress{Elapsed: time.Now().Sub(startTime)})
}
match := number.FindStringSubmatch(filename)
if match == nil {
return nil
}
count++
baseNum, _ := strconv.ParseUint(match[1], 10, 32)
baseNum32 = uint32(baseNum)
if baseNum32 != expected {
maybeSendWebsocket(conn, WebsocketTypeBlockRange, NewValidBlockRange(currentStartBlk, (expected-fileBlockSize), "valid range"))
maybeSendWebsocket(conn, WebsocketTypeBlockRange, NewMissingBlockRange(expected, (baseNum32-fileBlockSize), "hole found"))
currentStartBlk = baseNum32
}
expected = baseNum32 + fileBlockSize
if count%10000 == 0 {
maybeSendWebsocket(conn, WebsocketTypeBlockRange, NewValidBlockRange(currentStartBlk, baseNum32, "valid range"))
currentStartBlk = baseNum32 + fileBlockSize
}
return nil
})
maybeSendWebsocket(conn, WebsocketTypeBlockRange, NewValidBlockRange(currentStartBlk, baseNum32, "valid range"))
zlog.Info("diagnose - block holes - completed")
}