-
Notifications
You must be signed in to change notification settings - Fork 211
/
book.go
439 lines (400 loc) · 8.7 KB
/
book.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package book
import (
"bufio"
"container/list"
"encoding/json"
"fmt"
"hash/crc64"
"io"
"math/rand"
"sort"
"strconv"
"sync"
"time"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
const SELF ID = "SELF"
type ID = string
type Address = ma.Multiaddr
type class uint32
const (
deleted class = iota
stale
learned
stable
)
func classify(addr *addressInfo) class {
switch addr.Class {
case stale:
if addr.success == 2 {
return stable
} else if addr.failures == 2 {
return deleted
}
case learned:
if addr.success == 2 {
return stable
} else if addr.failures == 1 {
return stale
}
case stable:
if addr.failures == 2 {
return stale
}
}
return addr.Class
}
type bucket int
const (
private = iota
public
)
func isDns(raw Address) bool {
for _, protocol := range raw.Protocols() {
if protocol.Code == ma.P_DNS ||
protocol.Code == ma.P_DNS4 ||
protocol.Code == ma.P_DNS6 ||
protocol.Code == ma.P_DNSADDR {
return true
}
}
return false
}
func bucketize(raw Address) bucket {
if manet.IsPublicAddr(raw) || isDns(raw) {
return public
}
return private
}
type addressInfo struct {
// exported values will be persisted
ID ID `json:"id"`
Raw jsonAddress `json:"raw"`
Class class `json:"class"`
Connected bool `json:"connected"`
shareable bool // true if item is in shareable array
bucket bucket
protected bool
failures int
success int
}
type jsonAddress struct {
Address
}
func (u *jsonAddress) UnmarshalJSON(data []byte) error {
// i didn't manage to find a way to use UnmarshalJSON method on the
// private multiaddr type
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
addr, err := ma.NewMultiaddr(s)
if err != nil {
return err
}
u.Address = addr
return nil
}
type Opt func(*Book)
// WithLimit updates default book limit.
func WithLimit(limit int) Opt {
return func(b *Book) {
b.limit = limit
}
}
// WithRand overwrites default seed for determinism in tests.
func WithRand(seed int64) Opt {
return func(b *Book) {
b.rng = rand.New(rand.NewSource(seed))
}
}
func New(opts ...Opt) *Book {
b := &Book{
limit: 50000,
known: map[ID]*addressInfo{},
queue: list.New(),
rng: rand.New(rand.NewSource(time.Now().Unix())),
shareable: []*addressInfo{},
}
for _, opt := range opts {
opt(b)
}
return b
}
type Book struct {
mu sync.Mutex
limit int
known map[ID]*addressInfo
queue *list.List
rng *rand.Rand
shareable []*addressInfo
}
func (b *Book) Add(src, id ID, raw Address) {
b.mu.Lock()
defer b.mu.Unlock()
addr := b.known[id]
bucket := bucketize(raw)
if src != SELF {
if source := b.known[src]; source == nil || source.bucket != bucket {
return
}
}
if addr == nil && len(b.known) == b.limit {
// future improvement is to find any stale and replace it with learned
return
} else if addr == nil {
addr = &addressInfo{
Raw: jsonAddress{raw},
Class: learned,
ID: id,
bucket: bucket,
shareable: true,
}
b.shareable = append(b.shareable, addr)
b.queue.PushBack(addr)
b.known[id] = addr
} else if addr.Raw.Address != raw && !addr.protected {
addr.Raw.Address = raw
addr.bucket = bucket
}
}
type Event int
const (
Protect Event = iota
Connected
Disconnected
Success
Fail
)
func (b *Book) Update(id ID, events ...Event) {
b.mu.Lock()
defer b.mu.Unlock()
addr := b.known[id]
if addr == nil {
return
}
for _, event := range events {
switch event {
case Protect:
addr.protected = true
addr.Class = stable
if !addr.shareable {
addr.shareable = true
b.shareable = append(b.shareable, addr)
}
case Connected:
addr.Connected = true
case Disconnected:
addr.Connected = false
case Success, Fail:
b.queue.PushBack(addr)
if addr.protected {
continue
}
if event == Success {
addr.success++
addr.failures = 0
} else if event == Fail {
addr.failures++
addr.success = 0
}
c := classify(addr)
if addr.Class != c {
addr.failures = 0
addr.success = 0
}
if addr.Class == stale && c > stale && !addr.shareable {
addr.shareable = true
b.shareable = append(b.shareable, addr)
} else if addr.Class == stale && c < stale {
delete(b.known, id)
}
addr.Class = c
}
}
}
func (b *Book) DrainQueue(n int) []Address {
b.mu.Lock()
defer b.mu.Unlock()
return take(n, b.drainQueue())
}
func (b *Book) TakeShareable(src ID, n int) []Address {
b.mu.Lock()
defer b.mu.Unlock()
addr := b.known[src]
if addr == nil {
return nil
}
return take(n, b.iterShareable(src, addr.bucket))
}
func (b *Book) Persist(w io.Writer) error {
b.mu.Lock()
defer b.mu.Unlock()
return persist(b.known, w)
}
func (b *Book) Recover(r io.Reader) error {
b.mu.Lock()
defer b.mu.Unlock()
known := map[string]*addressInfo{}
if err := recover(known, r); err != nil {
return err
}
b.known = known
queue := []*addressInfo{}
for _, addr := range b.known {
addr.bucket = bucketize(addr.Raw.Address)
queue = append(queue, addr)
if addr.Class >= learned {
b.shareable = append(b.shareable, addr)
addr.shareable = true
}
}
// prioritize establishing connections with previously connected addresses
sort.Slice(queue, func(i, j int) bool {
if queue[i].Connected != queue[j].Connected {
return queue[i].Connected
} else if queue[i].Class != queue[j].Class {
return queue[i].Class > queue[j].Class
} else {
return queue[i].ID < queue[j].ID
}
})
for _, addr := range queue {
b.queue.PushBack(addr)
}
return nil
}
type Stats struct {
Total int
Connected int
Public int
Private int
Stale int
Learned int
Stable int
}
func (b *Book) Stats() Stats {
b.mu.Lock()
defer b.mu.Unlock()
stats := Stats{Total: len(b.known)}
for _, addr := range b.known {
if addr.Connected {
stats.Connected++
}
if addr.bucket == public {
stats.Public++
} else {
stats.Private++
}
switch addr.Class {
case stale:
stats.Stale++
case learned:
stats.Learned++
case stable:
stats.Stable++
}
}
return stats
}
func (b *Book) iterShareable(src ID, bucket bucket) iterator {
b.rng.Shuffle(len(b.shareable), func(i, j int) {
b.shareable[i], b.shareable[j] = b.shareable[j], b.shareable[i]
})
i := 0
return func() Address {
for {
if i == len(b.shareable) {
return nil
}
rst := b.shareable[i]
if rst.Class <= stale {
copy(b.shareable[i:], b.shareable[i+1:])
b.shareable[len(b.shareable)-1] = nil
b.shareable = b.shareable[:len(b.shareable)-1]
rst.shareable = false
} else {
i++
if rst.bucket == bucket && rst.ID != src {
return rst.Raw.Address
}
}
}
}
}
func (b *Book) drainQueue() iterator {
return func() Address {
for {
if b.queue.Len() == 0 {
return nil
}
rst := b.queue.Remove(b.queue.Front())
if rst.(*addressInfo).Class == deleted {
continue
}
return rst.(*addressInfo).Raw.Address
}
}
}
type iterator func() Address
func take(n int, next iterator) []Address {
rst := make([]Address, 0, n)
for addr := next(); addr != nil; addr = next() {
rst = append(rst, addr)
if len(rst) == cap(rst) {
return rst
}
}
return rst
}
func persist(known map[ID]*addressInfo, w io.Writer) error {
checksum := crc64.New(crc64.MakeTable(crc64.ISO))
encoder := json.NewEncoder(io.MultiWriter(w, checksum))
sorted := make([]ID, 0, len(known))
for addr := range known {
sorted = append(sorted, addr)
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i] < sorted[j]
})
for _, id := range sorted {
addr := known[id]
if err := encoder.Encode(addr); err != nil {
return fmt.Errorf("json encoder failure for obj (%v): %w", addr, err)
}
}
if _, err := fmt.Fprintf(w, "%s", strconv.FormatUint(checksum.Sum64(), 10)); err != nil {
return fmt.Errorf("write checksum: %w", err)
}
return nil
}
func recover(known map[ID]*addressInfo, r io.Reader) error {
checksum := crc64.New(crc64.MakeTable(crc64.ISO))
scanner := bufio.NewScanner(r)
stored := uint64(0)
for scanner.Scan() {
if len(scanner.Bytes()) == 0 {
return fmt.Errorf("corrupted data: empty lines are not expected")
}
if scanner.Bytes()[0] != '{' {
var err error
stored, err = strconv.ParseUint(scanner.Text(), 10, 64)
if err != nil {
return fmt.Errorf("parse uint %s: %w", scanner.Text(), err)
}
break
}
addr := &addressInfo{}
if err := json.Unmarshal(scanner.Bytes(), addr); err != nil {
return fmt.Errorf("unmarshal %s: %w", scanner.Text(), err)
}
checksum.Write(scanner.Bytes())
checksum.Write([]byte{'\n'})
known[addr.ID] = addr
}
if stored != checksum.Sum64() {
return fmt.Errorf("stored checksum %d doesn't match computed %d", stored, checksum.Sum64())
}
return nil
}