-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathstatecmp.go
More file actions
239 lines (217 loc) · 5.55 KB
/
Copy pathstatecmp.go
File metadata and controls
239 lines (217 loc) · 5.55 KB
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
package main
import (
"context"
"encoding/json"
"flag"
"log/slog"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/wavesplatform/gowaves/pkg/client"
"github.com/wavesplatform/gowaves/pkg/crypto"
"github.com/wavesplatform/gowaves/pkg/logging"
"github.com/wavesplatform/gowaves/pkg/proto"
)
const (
defaultScheme = "http"
)
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 loadStateHash(ctx context.Context, cl *client.Client, height uint64, tries int) (proto.StateHash, error) {
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
var sh proto.StateHash
var err error
for range tries {
sh, _, err = cl.Debug.StateHash(ctx, height)
if err == nil {
return sh, nil
}
}
return nil, err
}
type printer struct {
lock sync.Mutex
}
func (p *printer) printDifferentResults(height uint64, res map[crypto.Digest]*nodesGroup) {
p.lock.Lock()
defer p.lock.Unlock()
for sh, nodes := range res {
hashJs, err := json.Marshal(sh)
if err != nil {
panic(err)
}
slog.Info("Following state hashes are different",
"height", height, "stateHash", string(hashJs), "nodes", nodes.nodes)
}
}
type stateHashInfo struct {
sh proto.StateHash
node string
}
type hashResult struct {
res stateHashInfo
err error
}
type nodesGroup struct {
nodes []string
sh proto.StateHash
}
func newNodesGroup(first string, sh proto.StateHash) *nodesGroup {
nodes := make([]string, 1)
nodes[0] = first
return &nodesGroup{nodes: nodes, sh: sh}
}
func (ng *nodesGroup) addNode(node string) {
ng.nodes = append(ng.nodes, node)
}
func manageHeight(
ctx context.Context, height uint64, nodes []string, clients []*client.Client, p *printer, tries int,
) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
results := make(chan hashResult, len(clients))
for i, cl := range clients {
node := nodes[i]
go func(cl *client.Client, node string) {
sh, err := loadStateHash(ctx, cl, height, tries)
res := hashResult{
res: stateHashInfo{
sh: sh,
node: node,
},
err: err,
}
results <- res
}(cl, node)
}
differentResults := make(map[crypto.Digest]*nodesGroup)
for range clients {
hr := <-results
if hr.err != nil {
cancel()
return hr.err
}
sh := hr.res.sh
ng, ok := differentResults[sh.GetSumHash()]
if !ok {
differentResults[sh.GetSumHash()] = newNodesGroup(hr.res.node, sh)
} else {
ng.addNode(hr.res.node)
}
}
if len(differentResults) != 1 {
p.printDifferentResults(height, differentResults)
}
return nil
}
func download(
heightChan chan uint64, errChan chan error, nodes []string, clients []*client.Client, goroutines, tries int,
) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
p := &printer{}
var wg sync.WaitGroup
for range goroutines {
wg.Go(func() {
for height := range heightChan {
if err := manageHeight(ctx, height, nodes, clients, p, tries); err != nil {
cancel()
slog.Error("Failed to load hashes", logging.Error(err))
errChan <- err
break
}
}
})
}
wg.Wait()
}
func main() {
const (
defaultStartHeight = 1
defaultEndHeight = 2
defaultGoroutinesNumber = 15
defaultTriesNumber = 5
minNodesNumber = 2
)
var (
lp = logging.Parameters{}
nodesStr = flag.String("nodes", "", "Addresses of nodes; comma separated.")
startHeight = flag.Int("start-height", defaultStartHeight, "Start height.")
endHeight = flag.Int("end-height", defaultEndHeight, "End height.")
goroutinesNum = flag.Int("goroutines-num", defaultGoroutinesNumber,
"Number of goroutines that will run for downloading state hashes.")
tries = flag.Int("tries-num", defaultTriesNumber, "Number of tries to download.")
)
lp.Initialize()
flag.Parse()
if err := lp.Parse(); err != nil {
slog.Error("Failed to parse application parameters", logging.Error(err))
os.Exit(1)
}
slog.SetDefault(slog.New(logging.DefaultHandler(lp)))
if *endHeight <= *startHeight {
slog.Error("End height must be greater than start height",
"endHeight", *endHeight, "startHeight", *startHeight)
os.Exit(1)
}
nodes := strings.FieldsFunc(*nodesStr, func(r rune) bool { return r == ',' })
if len(nodes) < minNodesNumber {
slog.Error("At least 2 nodes are required for state comparison", "nodes", *nodesStr)
os.Exit(1)
}
clients := make([]*client.Client, len(nodes))
for i, nu := range nodes {
u, err := checkAndUpdateURL(nu)
if err != nil {
slog.Error("Failed to update URL", logging.Error(err))
os.Exit(1)
}
clients[i], err = client.NewClient(client.Options{BaseUrl: u, Client: &http.Client{}})
if err != nil {
slog.Error("Failed to create client", slog.String("URL", u), logging.Error(err))
os.Exit(1)
}
}
heightChan := make(chan uint64)
errChan := make(chan error, *goroutinesNum)
var wg sync.WaitGroup
wg.Go(func() {
download(heightChan, errChan, nodes, clients, *goroutinesNum, *tries)
})
for h := uint64(*startHeight); h < uint64(*endHeight); h++ {
gotErr := false
select {
case heightChan <- h:
case <-errChan:
gotErr = true
}
if gotErr {
break
}
}
close(heightChan)
wg.Wait()
slog.Info("Finished to compare states.")
}