/
tester.go
349 lines (299 loc) · 7.35 KB
/
tester.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
package api
import (
"context"
"fmt"
"math"
"strings"
"time"
"github.com/CybexDex/cybex-go/types"
"github.com/denkhaus/logging"
sort "github.com/emirpasic/gods/utils"
deadlock "github.com/sasha-s/go-deadlock"
"gopkg.in/tomb.v2"
)
var (
//LoopSeconds = time for one pass to calc dynamic delay
LoopSeconds = 60
//our known node endpoints
knownEndpoints = []string{
"wss://eu.openledger.info/ws",
"wss://cybex.openledger.info/ws",
"wss://dex.rnglab.org",
"wss://api.cybex.bhuz.info/ws",
"wss://cybex.crypto.fans/ws",
"wss://node.market.rudex.org",
"wss://api.cyb.blckchnd.com",
"wss://eu.nodes.cybex.ws",
"wss://cybws.roelandp.nl/ws",
"wss://cybfullnode.bangzi.info/ws",
"wss://api-ru.cyb.blckchnd.com",
"wss://kc-us-dex.xeldal.com/ws",
"wss://api.cybxchng.com",
"wss://api.cyb.network",
"wss://dexnode.net/ws",
"wss://us.nodes.cybex.ws",
"wss://api.cyb.mobi/ws",
"wss://blockzms.xyz/ws",
"wss://cyb-api.lafona.net/ws",
"wss://api.cyb.ai/",
"wss://la.dexnode.net/ws",
"wss://openledger.hk/ws",
"wss://sg.nodes.cybex.ws",
"wss://cyb.open.icowallet.net/ws",
"wss://ws.gdex.io",
"wss://cybex-api.wancloud.io/ws",
"wss://ws.hellocyb.com/",
"wss://cybex.dacplay.org/ws",
"wss://crazybit.online",
"wss://kimziv.com/ws",
"wss://wss.ioex.top",
"wss://node.cybcharts.com/ws",
"wss://cyb-seoul.clockwork.gr/",
"wss://cybex.cyberit.io/",
"wss://api.cybgo.net/ws",
"wss://ws.winex.pro",
"wss://cyb.to0l.cn:4443/ws",
"wss://cybex.cyb123.cc:15138/",
"wss://bit.cybabc.org/ws",
"wss://ws.gdex.top",
}
)
type LatencyTester interface {
Start()
Close() error
String() string
AddEndpoint(ep string)
OnTopNodeChanged(fn func(string) error)
TopNodeEndpoint() string
TopNodeClient() WebsocketClient
Done() <-chan struct{}
}
//NodeStats holds stat data for each endpoint
type NodeStats struct {
cli WebsocketClient
latency time.Duration
requiredDB []string
attempts int64
errors int64
endpoint string
}
func (p *NodeStats) onError(err error) {
p.errors++
}
//Latency returns the nodes latency
func (p *NodeStats) Latency() time.Duration {
if p.attempts > 0 {
return time.Duration(int64(p.latency) / p.attempts)
}
return 0
}
//Score returns reliability score for each node. The less the better.
func (p *NodeStats) Score() int64 {
lat := int64(p.Latency())
if lat == 0 {
return math.MaxInt64
}
if p.errors == 0 {
return lat
}
// add 50ms per error
return lat + p.errors*50000000
}
// String returns the stats string representation
func (p *NodeStats) String() string {
return fmt.Sprintf("ep: %s | attempts: %d | errors: %d | latency: %s | score: %d",
p.endpoint, p.attempts, p.errors, p.Latency(), p.Score())
}
//NewNodeStats creates a new stat object
func NewNodeStats(wsRPCEndpoint string) *NodeStats {
stats := &NodeStats{
endpoint: wsRPCEndpoint,
cli: NewWebsocketClient(wsRPCEndpoint),
requiredDB: []string{
"database",
"history",
"network_broadcast",
"crypto",
},
}
stats.cli.OnError(stats.onError)
return stats
}
func (p *NodeStats) Equals(n *NodeStats) bool {
return p.endpoint == n.endpoint
}
func (p *NodeStats) checkNode() {
_, err := p.cli.CallAPI(1, "login", "", "")
if err != nil {
p.errors++
}
for _, name := range p.requiredDB {
_, err := p.cli.CallAPI(1, name, types.EmptyParams)
if err != nil {
p.errors++
}
}
}
func (p *NodeStats) check() {
p.attempts++
if err := p.cli.Connect(); err != nil {
p.errors++
}
defer p.cli.Close()
tm := time.Now()
p.checkNode()
p.latency += time.Since(tm)
}
type latencyTester struct {
mu deadlock.RWMutex
tmb *tomb.Tomb
toApply []string
fallbackURL string
onTopNodeChanged func(string) error
stats []interface{}
pass int
}
func NewLatencyTester(fallbackURL string) (LatencyTester, error) {
return NewLatencyTesterWithContext(context.Background(), fallbackURL)
}
func NewLatencyTesterWithContext(ctx context.Context, fallbackURL string) (LatencyTester, error) {
tmb, _ := tomb.WithContext(ctx)
lat := latencyTester{
fallbackURL: fallbackURL,
stats: make([]interface{}, 0, len(knownEndpoints)),
tmb: tmb,
}
lat.createStats(knownEndpoints)
return &lat, nil
}
func (p *latencyTester) String() string {
builder := strings.Builder{}
p.mu.Lock()
defer p.mu.Unlock()
for _, s := range p.stats {
stat := s.(*NodeStats)
builder.WriteString(stat.String())
builder.WriteString("\n")
}
return builder.String()
}
func (p *latencyTester) OnTopNodeChanged(fn func(string) error) {
p.onTopNodeChanged = fn
}
//AddEndpoint adds a new endpoint while the latencyTester is running
func (p *latencyTester) AddEndpoint(ep string) {
p.toApply = append(p.toApply, ep)
}
func (p *latencyTester) sortResults(notify bool) error {
p.mu.Lock()
oldTop := p.stats[0].(*NodeStats)
sort.Sort(p.stats, func(a, b interface{}) int {
sa := a.(*NodeStats).Score()
sb := b.(*NodeStats).Score()
if sa > sb {
return 1
}
if sa < sb {
return -1
}
return 0
})
newTop := p.stats[0].(*NodeStats)
p.mu.Unlock()
if notify && !oldTop.Equals(newTop) {
if p.onTopNodeChanged != nil {
return p.onTopNodeChanged(newTop.endpoint)
}
}
return nil
}
func (p *latencyTester) createStats(eps []string) {
p.mu.Lock()
defer p.mu.Unlock()
for _, ep := range eps {
found := false
for _, s := range p.stats {
stat := s.(*NodeStats)
if stat.endpoint == ep {
found = true
}
}
if !found {
p.stats = append(
p.stats,
NewNodeStats(ep),
)
}
}
}
//TopNodeEndpoint returns the fastest endpoint URL. If the tester has no validated results
//your given fallback endpoint is returned.
func (p *latencyTester) TopNodeEndpoint() string {
if p.pass > 0 {
p.mu.RLock()
defer p.mu.RUnlock()
st := p.stats[0].(*NodeStats)
return st.endpoint
}
return p.fallbackURL
}
//TopNodeClient returns a new WebsocketClient to connect to the fastest node.
//If the tester has no validated results, a client with your given
//fallback endpoint is returned. You need to call Connect for yourself.
func (p *latencyTester) TopNodeClient() WebsocketClient {
return NewWebsocketClient(
p.TopNodeEndpoint(),
)
}
// Done returns the channel that can be used to wait until
// the tester has finished.
func (p *latencyTester) Done() <-chan struct{} {
return p.tmb.Dead()
}
func (p *latencyTester) runPass() error {
// dynamic sleep time
slp := time.Duration(LoopSeconds/len(p.stats)) * time.Second
for i := 0; i < len(p.stats); i++ {
select {
case <-p.tmb.Dying():
return tomb.ErrDying
default:
time.Sleep(slp)
p.mu.RLock()
st := p.stats[i].(*NodeStats)
st.check()
p.mu.RUnlock()
}
}
return nil
}
//Start starts the testing process
func (p *latencyTester) Start() {
logging.Debug("latencytester: start")
p.tmb.Go(func() error {
for {
select {
case <-p.tmb.Dying():
p.sortResults(false)
return tomb.ErrDying
default:
//apply later incoming endpoints
p.createStats(p.toApply)
if err := p.runPass(); err != nil {
//provide sorted results on return
p.sortResults(false)
return err
}
p.sortResults(true)
p.pass++
}
}
})
}
//Close stops the tester and waits until all goroutines have finished.
func (p *latencyTester) Close() error {
logging.Debug("latencytester: kill [tomb]")
p.tmb.Kill(nil)
logging.Debug("latencytester: wait [tomb]")
return p.tmb.Wait()
}