@@ -11,7 +11,6 @@ import (
1111 "sync/atomic"
1212 "time"
1313
14- "github.com/libp2p/go-libp2p/core/crypto"
1514 "github.com/libp2p/go-libp2p/core/event"
1615 "github.com/libp2p/go-libp2p/core/network"
1716 "github.com/libp2p/go-libp2p/core/peer"
@@ -32,11 +31,6 @@ var addrChangeTickrInterval = 5 * time.Second
3231
3332const maxPeerRecordSize = 8 * 1024 // 8k to be compatible with identify's limit
3433
35- // addrStore is a minimal interface for storing peer addresses
36- type addrStore interface {
37- SetAddrs (peer.ID , []ma.Multiaddr , time.Duration )
38- }
39-
4034// ObservedAddrsManager maps our local listen addrs to externally observed addrs.
4135type ObservedAddrsManager interface {
4236 Addrs (minObservers int ) []ma.Multiaddr
@@ -74,10 +68,8 @@ type addrsManager struct {
7468 addrsMx sync.RWMutex
7569 currentAddrs hostAddrs
7670
77- signKey crypto.PrivKey
78- addrStore addrStore
79- signedRecordStore peerstore.CertifiedAddrBook
80- hostID peer.ID
71+ peerstore peerstore.Peerstore
72+ hostID peer.ID
8173
8274 wg sync.WaitGroup
8375 ctx context.Context
@@ -94,9 +86,7 @@ func newAddrsManager(
9486 client autonatv2Client ,
9587 enableMetrics bool ,
9688 registerer prometheus.Registerer ,
97- disableSignedPeerRecord bool ,
98- signKey crypto.PrivKey ,
99- addrStore addrStore ,
89+ pstore peerstore.Peerstore ,
10090 hostID peer.ID ,
10191) (* addrsManager , error ) {
10292 ctx , cancel := context .WithCancel (context .Background ())
@@ -110,23 +100,14 @@ func newAddrsManager(
110100 triggerAddrsUpdateChan : make (chan chan struct {}, 1 ),
111101 triggerReachabilityUpdate : make (chan struct {}, 1 ),
112102 interfaceAddrs : & interfaceAddrsCache {},
113- signKey : signKey ,
114- addrStore : addrStore ,
103+ peerstore : pstore ,
115104 hostID : hostID ,
116105 ctx : ctx ,
117106 ctxCancel : cancel ,
118107 }
119108 unknownReachability := network .ReachabilityUnknown
120109 as .hostReachability .Store (& unknownReachability )
121110
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-
130111 if client != nil {
131112 var metricsTracker MetricsTracker
132113 if enableMetrics {
@@ -347,26 +328,10 @@ func (a *addrsManager) updateAddrs(prevHostAddrs hostAddrs, relayAddrs []ma.Mult
347328
348329// updatePeerStore updates the peer store for the host
349330func (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 )
370335 }
371336}
372337
@@ -378,7 +343,7 @@ func (a *addrsManager) notifyAddrsUpdated(emitter event.Emitter, localAddrsEmitt
378343 }
379344 }
380345 if areAddrsDifferent (previous .addrs , current .addrs ) {
381- log .Debug ("host addresses updated" , "addrs" , current .localAddrs )
346+ log .Debug ("host addresses updated" , "addrs" , current .addrs )
382347 a .emitLocalAddrsUpdated (localAddrsEmitter , current .addrs , previous .addrs )
383348 }
384349
@@ -562,30 +527,6 @@ func (a *addrsManager) appendObservedAddrs(dst []ma.Multiaddr, listenAddrs, ifac
562527 return dst
563528}
564529
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-
589530// emitLocalAddrsUpdated emits an EvtLocalAddressesUpdated event and updates the addresses in the peerstore.
590531func (a * addrsManager ) emitLocalAddrsUpdated (emitter event.Emitter , currentAddrs []ma.Multiaddr , lastAddrs []ma.Multiaddr ) {
591532 added , maintained , removed := diffAddrs (lastAddrs , currentAddrs )
@@ -594,8 +535,11 @@ func (a *addrsManager) emitLocalAddrsUpdated(emitter event.Emitter, currentAddrs
594535 }
595536
596537 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+ }
599543 }
600544
601545 evt := & event.EvtLocalAddressesUpdated {
0 commit comments