@@ -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"
@@ -74,10 +73,8 @@ type addrsManager struct {
7473 addrsMx sync.RWMutex
7574 currentAddrs hostAddrs
7675
77- signKey crypto.PrivKey
78- addrStore addrStore
79- signedRecordStore peerstore.CertifiedAddrBook
80- hostID peer.ID
76+ peerstore peerstore.Peerstore
77+ hostID peer.ID
8178
8279 wg sync.WaitGroup
8380 ctx context.Context
@@ -94,9 +91,7 @@ func newAddrsManager(
9491 client autonatv2Client ,
9592 enableMetrics bool ,
9693 registerer prometheus.Registerer ,
97- disableSignedPeerRecord bool ,
98- signKey crypto.PrivKey ,
99- addrStore addrStore ,
94+ pstore peerstore.Peerstore ,
10095 hostID peer.ID ,
10196) (* addrsManager , error ) {
10297 ctx , cancel := context .WithCancel (context .Background ())
@@ -110,23 +105,14 @@ func newAddrsManager(
110105 triggerAddrsUpdateChan : make (chan chan struct {}, 1 ),
111106 triggerReachabilityUpdate : make (chan struct {}, 1 ),
112107 interfaceAddrs : & interfaceAddrsCache {},
113- signKey : signKey ,
114- addrStore : addrStore ,
108+ peerstore : pstore ,
115109 hostID : hostID ,
116110 ctx : ctx ,
117111 ctxCancel : cancel ,
118112 }
119113 unknownReachability := network .ReachabilityUnknown
120114 as .hostReachability .Store (& unknownReachability )
121115
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-
130116 if client != nil {
131117 var metricsTracker MetricsTracker
132118 if enableMetrics {
@@ -347,26 +333,10 @@ func (a *addrsManager) updateAddrs(prevHostAddrs hostAddrs, relayAddrs []ma.Mult
347333
348334// updatePeerStore updates the peer store for the host
349335func (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- }
336+ peerRecordAddrs := trimHostAddrList (currentAddrs , maxPeerRecordSize - 1024 ) // 1024 B of buffer for things other than addrs in peerstore
337+ err := a .peerstore .UpdateHostAddrs (a .hostID , currentAddrs , removedAddrs , peerRecordAddrs )
338+ if err != nil {
339+ log .Error ("error updating peer store" , "err" , err )
370340 }
371341}
372342
@@ -378,7 +348,7 @@ func (a *addrsManager) notifyAddrsUpdated(emitter event.Emitter, localAddrsEmitt
378348 }
379349 }
380350 if areAddrsDifferent (previous .addrs , current .addrs ) {
381- log .Debug ("host addresses updated" , "addrs" , current .localAddrs )
351+ log .Debug ("host addresses updated" , "addrs" , current .addrs )
382352 a .emitLocalAddrsUpdated (localAddrsEmitter , current .addrs , previous .addrs )
383353 }
384354
@@ -562,30 +532,6 @@ func (a *addrsManager) appendObservedAddrs(dst []ma.Multiaddr, listenAddrs, ifac
562532 return dst
563533}
564534
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-
589535// emitLocalAddrsUpdated emits an EvtLocalAddressesUpdated event and updates the addresses in the peerstore.
590536func (a * addrsManager ) emitLocalAddrsUpdated (emitter event.Emitter , currentAddrs []ma.Multiaddr , lastAddrs []ma.Multiaddr ) {
591537 added , maintained , removed := diffAddrs (lastAddrs , currentAddrs )
@@ -594,8 +540,11 @@ func (a *addrsManager) emitLocalAddrsUpdated(emitter event.Emitter, currentAddrs
594540 }
595541
596542 var sr * record.Envelope
597- if a .signedRecordStore != nil {
598- sr = a .signedRecordStore .GetPeerRecord (a .hostID )
543+ if a .peerstore != nil {
544+ ca , ok := a .peerstore .(peerstore.CertifiedAddrBook )
545+ if ok {
546+ sr = ca .GetPeerRecord (a .hostID )
547+ }
599548 }
600549
601550 evt := & event.EvtLocalAddressesUpdated {
0 commit comments