This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
addr_book.go
554 lines (471 loc) · 12.9 KB
/
addr_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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package pstoremem
import (
"context"
"fmt"
"sort"
"sync"
"time"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/peer"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/record"
ma "github.com/multiformats/go-multiaddr"
"github.com/libp2p/go-libp2p-peerstore/addr"
)
var log = logging.Logger("peerstore")
type expiringAddr struct {
Addr ma.Multiaddr
TTL time.Duration
Expires time.Time
}
func (e *expiringAddr) ExpiredBy(t time.Time) bool {
return t.After(e.Expires)
}
type peerRecordState struct {
Envelope *record.Envelope
Seq uint64
}
type addrSegments [256]*addrSegment
type addrSegment struct {
sync.RWMutex
// Use pointers to save memory. Maps always leave some fraction of their
// space unused. storing the *values* directly in the map will
// drastically increase the space waste. In our case, by 6x.
addrs map[peer.ID]map[string]*expiringAddr
signedPeerRecords map[peer.ID]*peerRecordState
}
func (segments *addrSegments) get(p peer.ID) *addrSegment {
return segments[byte(p[len(p)-1])]
}
// memoryAddrBook manages addresses.
type memoryAddrBook struct {
segments addrSegments
ctx context.Context
cancel func()
subManager *AddrSubManager
}
var _ pstore.AddrBook = (*memoryAddrBook)(nil)
var _ pstore.CertifiedAddrBook = (*memoryAddrBook)(nil)
func NewAddrBook() *memoryAddrBook {
ctx, cancel := context.WithCancel(context.Background())
ab := &memoryAddrBook{
segments: func() (ret addrSegments) {
for i, _ := range ret {
ret[i] = &addrSegment{
addrs: make(map[peer.ID]map[string]*expiringAddr),
signedPeerRecords: make(map[peer.ID]*peerRecordState)}
}
return ret
}(),
subManager: NewAddrSubManager(),
ctx: ctx,
cancel: cancel,
}
go ab.background()
return ab
}
// background periodically schedules a gc
func (mab *memoryAddrBook) background() {
ticker := time.NewTicker(1 * time.Hour)
defer ticker.Stop()
for {
select {
case <-ticker.C:
mab.gc()
case <-mab.ctx.Done():
return
}
}
}
func (mab *memoryAddrBook) Close() error {
mab.cancel()
return nil
}
// gc garbage collects the in-memory address book.
func (mab *memoryAddrBook) gc() {
now := time.Now()
for _, s := range mab.segments {
s.Lock()
var collectedPeers []peer.ID
for p, amap := range s.addrs {
for k, addr := range amap {
if addr.ExpiredBy(now) {
delete(amap, k)
}
}
if len(amap) == 0 {
delete(s.addrs, p)
collectedPeers = append(collectedPeers, p)
}
}
// remove signed records for peers whose signed addrs have all been removed
for _, p := range collectedPeers {
delete(s.signedPeerRecords, p)
}
s.Unlock()
}
}
func (mab *memoryAddrBook) PeersWithAddrs() peer.IDSlice {
// deduplicate, since the same peer could have both signed & unsigned addrs
pidSet := peer.NewSet()
for _, s := range mab.segments {
s.RLock()
for pid, amap := range s.addrs {
if amap != nil && len(amap) > 0 {
pidSet.Add(pid)
}
}
s.RUnlock()
}
return pidSet.Peers()
}
// AddAddr calls AddAddrs(p, []ma.Multiaddr{addr}, ttl)
func (mab *memoryAddrBook) AddAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {
mab.AddAddrs(p, []ma.Multiaddr{addr}, ttl)
}
// AddAddrs gives memoryAddrBook addresses to use, with a given ttl
// (time-to-live), after which the address is no longer valid.
// This function never reduces the TTL or expiration of an address.
func (mab *memoryAddrBook) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
// if we have a valid peer record, ignore unsigned addrs
// peerRec := mab.GetPeerRecord(p)
// if peerRec != nil {
// return
// }
mab.addAddrs(p, addrs, ttl, false)
}
// ConsumePeerRecord adds addresses from a signed peer.PeerRecord (contained in
// a record.Envelope), which will expire after the given TTL.
// See https://godoc.org/github.com/libp2p/go-libp2p-core/peerstore#CertifiedAddrBook for more details.
func (mab *memoryAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, ttl time.Duration) (bool, error) {
r, err := recordEnvelope.Record()
if err != nil {
return false, err
}
rec, ok := r.(*peer.PeerRecord)
if !ok {
return false, fmt.Errorf("unable to process envelope: not a PeerRecord")
}
if !rec.PeerID.MatchesPublicKey(recordEnvelope.PublicKey) {
return false, fmt.Errorf("signing key does not match PeerID in PeerRecord")
}
// ensure seq is greater than last received
s := mab.segments.get(rec.PeerID)
s.Lock()
lastState, found := s.signedPeerRecords[rec.PeerID]
if found && lastState.Seq >= rec.Seq {
s.Unlock()
return false, nil
}
s.signedPeerRecords[rec.PeerID] = &peerRecordState{
Envelope: recordEnvelope,
Seq: rec.Seq,
}
s.Unlock() // need to release the lock, since addAddrs will try to take it
mab.addAddrs(rec.PeerID, rec.Addrs, ttl, true)
return true, nil
}
func (mab *memoryAddrBook) addAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration, signed bool) {
if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err)
return
}
// if ttl is zero, exit. nothing to do.
if ttl <= 0 {
return
}
s := mab.segments.get(p)
s.Lock()
defer s.Unlock()
amap, ok := s.addrs[p]
if !ok {
amap = make(map[string]*expiringAddr)
s.addrs[p] = amap
}
exp := time.Now().Add(ttl)
addrSet := make(map[string]struct{}, len(addrs))
for _, addr := range addrs {
if addr == nil {
log.Warnf("was passed nil multiaddr for %s", p)
continue
}
k := string(addr.Bytes())
addrSet[k] = struct{}{}
// find the highest TTL and Expiry time between
// existing records and function args
a, found := amap[k] // won't allocate.
if !found {
// not found, announce it.
entry := &expiringAddr{Addr: addr, Expires: exp, TTL: ttl}
amap[k] = entry
mab.subManager.BroadcastAddr(p, addr)
} else {
// update ttl & exp to whichever is greater between new and existing entry
if ttl > a.TTL {
a.TTL = ttl
}
if exp.After(a.Expires) {
a.Expires = exp
}
}
}
}
// SetAddr calls mgr.SetAddrs(p, addr, ttl)
func (mab *memoryAddrBook) SetAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {
if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err)
return
}
mab.SetAddrs(p, []ma.Multiaddr{addr}, ttl)
}
// SetAddrs sets the ttl on addresses. This clears any TTL there previously.
// This is used when we receive the best estimate of the validity of an address.
func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err)
return
}
s := mab.segments.get(p)
s.Lock()
defer s.Unlock()
amap, ok := s.addrs[p]
if !ok {
amap = make(map[string]*expiringAddr)
s.addrs[p] = amap
}
exp := time.Now().Add(ttl)
for _, addr := range addrs {
if addr == nil {
log.Warnf("was passed nil multiaddr for %s", p)
continue
}
aBytes := addr.Bytes()
key := string(aBytes)
// re-set all of them for new ttl.
if ttl > 0 {
amap[key] = &expiringAddr{Addr: addr, Expires: exp, TTL: ttl}
mab.subManager.BroadcastAddr(p, addr)
} else {
delete(amap, key)
}
}
// if we've expired all the signed addresses for a peer, remove their signed routing state record
if len(amap) == 0 {
delete(s.signedPeerRecords, p)
}
}
// UpdateAddrs updates the addresses associated with the given peer that have
// the given oldTTL to have the given newTTL.
func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL time.Duration) {
if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err)
return
}
s := mab.segments.get(p)
s.Lock()
defer s.Unlock()
exp := time.Now().Add(newTTL)
amap, found := s.addrs[p]
if found {
for k, a := range amap {
if oldTTL == a.TTL {
a.TTL = newTTL
a.Expires = exp
amap[k] = a
}
}
}
// if we've expired all the signed addresses for a peer, remove their signed routing state record
if len(amap) == 0 {
delete(s.signedPeerRecords, p)
}
}
// Addrs returns all known (and valid) addresses for a given peer
func (mab *memoryAddrBook) Addrs(p peer.ID) []ma.Multiaddr {
if err := p.Validate(); err != nil {
// invalid peer ID = no addrs
return nil
}
s := mab.segments.get(p)
s.RLock()
defer s.RUnlock()
return validAddrs(s.addrs[p])
}
func validAddrs(amap map[string]*expiringAddr) []ma.Multiaddr {
now := time.Now()
good := make([]ma.Multiaddr, 0, len(amap))
if amap == nil {
return good
}
for _, m := range amap {
if !m.ExpiredBy(now) {
good = append(good, m.Addr)
}
}
return good
}
// GetPeerRecord returns a Envelope containing a PeerRecord for the
// given peer id, if one exists.
// Returns nil if no signed PeerRecord exists for the peer.
func (mab *memoryAddrBook) GetPeerRecord(p peer.ID) *record.Envelope {
if err := p.Validate(); err != nil {
// invalid peer ID = no addrs
return nil
}
s := mab.segments.get(p)
s.RLock()
defer s.RUnlock()
// although the signed record gets garbage collected when all addrs inside it are expired,
// we may be in between the expiration time and the GC interval
// so, we check to see if we have any valid signed addrs before returning the record
if len(validAddrs(s.addrs[p])) == 0 {
return nil
}
state := s.signedPeerRecords[p]
if state == nil {
return nil
}
return state.Envelope
}
// ClearAddrs removes all previously stored addresses
func (mab *memoryAddrBook) ClearAddrs(p peer.ID) {
if err := p.Validate(); err != nil {
// nothing to clear
return
}
s := mab.segments.get(p)
s.Lock()
defer s.Unlock()
delete(s.addrs, p)
delete(s.signedPeerRecords, p)
}
// AddrStream returns a channel on which all new addresses discovered for a
// given peer ID will be published.
func (mab *memoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr {
if err := p.Validate(); err != nil {
log.Warningf("tried to get addrs for invalid peer ID %s: %s", p, err)
ch := make(chan ma.Multiaddr)
close(ch)
return ch
}
s := mab.segments.get(p)
s.RLock()
defer s.RUnlock()
baseaddrslice := s.addrs[p]
initial := make([]ma.Multiaddr, 0, len(baseaddrslice))
for _, a := range baseaddrslice {
initial = append(initial, a.Addr)
}
return mab.subManager.AddrStream(ctx, p, initial)
}
type addrSub struct {
pubch chan ma.Multiaddr
lk sync.Mutex
buffer []ma.Multiaddr
ctx context.Context
}
func (s *addrSub) pubAddr(a ma.Multiaddr) {
select {
case s.pubch <- a:
case <-s.ctx.Done():
}
}
// An abstracted, pub-sub manager for address streams. Extracted from
// memoryAddrBook in order to support additional implementations.
type AddrSubManager struct {
mu sync.RWMutex
subs map[peer.ID][]*addrSub
}
// NewAddrSubManager initializes an AddrSubManager.
func NewAddrSubManager() *AddrSubManager {
return &AddrSubManager{
subs: make(map[peer.ID][]*addrSub),
}
}
// Used internally by the address stream coroutine to remove a subscription
// from the manager.
func (mgr *AddrSubManager) removeSub(p peer.ID, s *addrSub) {
mgr.mu.Lock()
defer mgr.mu.Unlock()
subs := mgr.subs[p]
if len(subs) == 1 {
if subs[0] != s {
return
}
delete(mgr.subs, p)
return
}
for i, v := range subs {
if v == s {
subs[i] = subs[len(subs)-1]
subs[len(subs)-1] = nil
mgr.subs[p] = subs[:len(subs)-1]
return
}
}
}
// BroadcastAddr broadcasts a new address to all subscribed streams.
func (mgr *AddrSubManager) BroadcastAddr(p peer.ID, addr ma.Multiaddr) {
mgr.mu.RLock()
defer mgr.mu.RUnlock()
if subs, ok := mgr.subs[p]; ok {
for _, sub := range subs {
sub.pubAddr(addr)
}
}
}
// AddrStream creates a new subscription for a given peer ID, pre-populating the
// channel with any addresses we might already have on file.
func (mgr *AddrSubManager) AddrStream(ctx context.Context, p peer.ID, initial []ma.Multiaddr) <-chan ma.Multiaddr {
sub := &addrSub{pubch: make(chan ma.Multiaddr), ctx: ctx}
out := make(chan ma.Multiaddr)
mgr.mu.Lock()
if _, ok := mgr.subs[p]; ok {
mgr.subs[p] = append(mgr.subs[p], sub)
} else {
mgr.subs[p] = []*addrSub{sub}
}
mgr.mu.Unlock()
sort.Sort(addr.AddrList(initial))
go func(buffer []ma.Multiaddr) {
defer close(out)
sent := make(map[string]bool, len(buffer))
var outch chan ma.Multiaddr
for _, a := range buffer {
sent[string(a.Bytes())] = true
}
var next ma.Multiaddr
if len(buffer) > 0 {
next = buffer[0]
buffer = buffer[1:]
outch = out
}
for {
select {
case outch <- next:
if len(buffer) > 0 {
next = buffer[0]
buffer = buffer[1:]
} else {
outch = nil
next = nil
}
case naddr := <-sub.pubch:
if sent[string(naddr.Bytes())] {
continue
}
sent[string(naddr.Bytes())] = true
if next == nil {
next = naddr
outch = out
} else {
buffer = append(buffer, naddr)
}
case <-ctx.Done():
mgr.removeSub(p, sub)
return
}
}
}(initial)
return out
}