@@ -11,7 +11,6 @@ import (
11
11
"sync/atomic"
12
12
"time"
13
13
14
- "github.com/libp2p/go-libp2p/core/crypto"
15
14
"github.com/libp2p/go-libp2p/core/event"
16
15
"github.com/libp2p/go-libp2p/core/network"
17
16
"github.com/libp2p/go-libp2p/core/peer"
@@ -32,11 +31,6 @@ var addrChangeTickrInterval = 5 * time.Second
32
31
33
32
const maxPeerRecordSize = 8 * 1024 // 8k to be compatible with identify's limit
34
33
35
- // addrStore is a minimal interface for storing peer addresses
36
- type addrStore interface {
37
- SetAddrs (peer.ID , []ma.Multiaddr , time.Duration )
38
- }
39
-
40
34
// ObservedAddrsManager maps our local listen addrs to externally observed addrs.
41
35
type ObservedAddrsManager interface {
42
36
Addrs (minObservers int ) []ma.Multiaddr
@@ -74,10 +68,8 @@ type addrsManager struct {
74
68
addrsMx sync.RWMutex
75
69
currentAddrs hostAddrs
76
70
77
- signKey crypto.PrivKey
78
- addrStore addrStore
79
- signedRecordStore peerstore.CertifiedAddrBook
80
- hostID peer.ID
71
+ peerstore peerstore.Peerstore
72
+ hostID peer.ID
81
73
82
74
wg sync.WaitGroup
83
75
ctx context.Context
@@ -94,9 +86,7 @@ func newAddrsManager(
94
86
client autonatv2Client ,
95
87
enableMetrics bool ,
96
88
registerer prometheus.Registerer ,
97
- disableSignedPeerRecord bool ,
98
- signKey crypto.PrivKey ,
99
- addrStore addrStore ,
89
+ pstore peerstore.Peerstore ,
100
90
hostID peer.ID ,
101
91
) (* addrsManager , error ) {
102
92
ctx , cancel := context .WithCancel (context .Background ())
@@ -110,23 +100,14 @@ func newAddrsManager(
110
100
triggerAddrsUpdateChan : make (chan chan struct {}, 1 ),
111
101
triggerReachabilityUpdate : make (chan struct {}, 1 ),
112
102
interfaceAddrs : & interfaceAddrsCache {},
113
- signKey : signKey ,
114
- addrStore : addrStore ,
103
+ peerstore : pstore ,
115
104
hostID : hostID ,
116
105
ctx : ctx ,
117
106
ctxCancel : cancel ,
118
107
}
119
108
unknownReachability := network .ReachabilityUnknown
120
109
as .hostReachability .Store (& unknownReachability )
121
110
122
- if ! disableSignedPeerRecord {
123
- var ok bool
124
- as .signedRecordStore , ok = as .addrStore .(peerstore.CertifiedAddrBook )
125
- if ! ok {
126
- return nil , errors .New ("peerstore doesn't implement CertifiedAddrBook interface" )
127
- }
128
- }
129
-
130
111
if client != nil {
131
112
var metricsTracker MetricsTracker
132
113
if enableMetrics {
@@ -347,26 +328,10 @@ func (a *addrsManager) updateAddrs(prevHostAddrs hostAddrs, relayAddrs []ma.Mult
347
328
348
329
// updatePeerStore updates the peer store for the host
349
330
func (a * addrsManager ) updatePeerStore (currentAddrs []ma.Multiaddr , removedAddrs []ma.Multiaddr ) {
350
- // update host addresses in the peer store
351
- a .addrStore .SetAddrs (a .hostID , currentAddrs , peerstore .PermanentAddrTTL )
352
- a .addrStore .SetAddrs (a .hostID , removedAddrs , 0 )
353
-
354
- var sr * record.Envelope
355
- // Our addresses have changed.
356
- // store the signed peer record in the peer store.
357
- if a .signedRecordStore != nil {
358
- var err error
359
- // add signed peer record to the event
360
- // in case of an error drop this event.
361
- sr , err = a .makeSignedPeerRecord (currentAddrs )
362
- if err != nil {
363
- log .Error ("error creating a signed peer record from the set of current addresses" , "err" , err )
364
- return
365
- }
366
- if _ , err := a .signedRecordStore .ConsumePeerRecord (sr , peerstore .PermanentAddrTTL ); err != nil {
367
- log .Error ("failed to persist signed peer record in peer store" , "err" , err )
368
- return
369
- }
331
+ peerRecordAddrs := trimHostAddrList (currentAddrs , maxPeerRecordSize - 1024 ) // 1024 B of buffer for things other than addrs in peerstore
332
+ err := a .peerstore .UpdateHostAddrs (a .hostID , currentAddrs , removedAddrs , peerRecordAddrs )
333
+ if err != nil {
334
+ log .Error ("error updating peer store" , "err" , err )
370
335
}
371
336
}
372
337
@@ -378,7 +343,7 @@ func (a *addrsManager) notifyAddrsUpdated(emitter event.Emitter, localAddrsEmitt
378
343
}
379
344
}
380
345
if areAddrsDifferent (previous .addrs , current .addrs ) {
381
- log .Debug ("host addresses updated" , "addrs" , current .localAddrs )
346
+ log .Debug ("host addresses updated" , "addrs" , current .addrs )
382
347
a .emitLocalAddrsUpdated (localAddrsEmitter , current .addrs , previous .addrs )
383
348
}
384
349
@@ -562,30 +527,6 @@ func (a *addrsManager) appendObservedAddrs(dst []ma.Multiaddr, listenAddrs, ifac
562
527
return dst
563
528
}
564
529
565
- // makeSignedPeerRecord creates a signed peer record for the given addresses
566
- func (a * addrsManager ) makeSignedPeerRecord (addrs []ma.Multiaddr ) (* record.Envelope , error ) {
567
- if a .signKey == nil {
568
- return nil , errors .New ("signKey is nil" )
569
- }
570
- // Limit the length of currentAddrs to ensure that our signed peer records aren't rejected
571
- peerRecordSize := 64 // HostID
572
- k , err := a .signKey .Raw ()
573
- var nk int
574
- if err == nil {
575
- nk = len (k )
576
- } else {
577
- nk = 1024 // In case of error, use a large enough value.
578
- }
579
- peerRecordSize += 2 * nk // 1 for signature, 1 for public key
580
- // we want the final address list to be small for keeping the signed peer record in size
581
- addrs = trimHostAddrList (addrs , maxPeerRecordSize - peerRecordSize - 256 ) // 256 B of buffer
582
- rec := peer .PeerRecordFromAddrInfo (peer.AddrInfo {
583
- ID : a .hostID ,
584
- Addrs : addrs ,
585
- })
586
- return record .Seal (rec , a .signKey )
587
- }
588
-
589
530
// emitLocalAddrsUpdated emits an EvtLocalAddressesUpdated event and updates the addresses in the peerstore.
590
531
func (a * addrsManager ) emitLocalAddrsUpdated (emitter event.Emitter , currentAddrs []ma.Multiaddr , lastAddrs []ma.Multiaddr ) {
591
532
added , maintained , removed := diffAddrs (lastAddrs , currentAddrs )
@@ -594,8 +535,11 @@ func (a *addrsManager) emitLocalAddrsUpdated(emitter event.Emitter, currentAddrs
594
535
}
595
536
596
537
var sr * record.Envelope
597
- if a .signedRecordStore != nil {
598
- sr = a .signedRecordStore .GetPeerRecord (a .hostID )
538
+ if a .peerstore != nil {
539
+ ca , ok := a .peerstore .(peerstore.CertifiedAddrBook )
540
+ if ok {
541
+ sr = ca .GetPeerRecord (a .hostID )
542
+ }
599
543
}
600
544
601
545
evt := & event.EvtLocalAddressesUpdated {
0 commit comments