Skip to content

Commit

Permalink
basichost: ensure no duplicates in Addrs output
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Sep 26, 2024
1 parent 9038a72 commit ffab4b6
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,9 @@ func (h *BasicHost) SignalAddressChange() {
}
}

func (h *BasicHost) makeUpdatedAddrEvent(prev, current []ma.Multiaddr) *event.EvtLocalAddressesUpdated {
if prev == nil && current == nil {
return nil
}
func makeUpdatedAddrEvent(prev, current []ma.Multiaddr) *event.EvtLocalAddressesUpdated {
prevmap := make(map[string]ma.Multiaddr, len(prev))
evt := &event.EvtLocalAddressesUpdated{Diffs: true}
evt := event.EvtLocalAddressesUpdated{Diffs: true}
addrsAdded := false

for _, addr := range prev {
Expand All @@ -527,19 +524,7 @@ func (h *BasicHost) makeUpdatedAddrEvent(prev, current []ma.Multiaddr) *event.Ev
return nil
}

// Our addresses have changed. Make a new signed peer record.
if !h.disableSignedPeerRecord {
// add signed peer record to the event
sr, err := h.makeSignedPeerRecord(current)
if err != nil {
log.Errorf("error creating a signed peer record from the set of current addresses, err=%s", err)
// drop this change
return nil
}
evt.SignedPeerRecord = sr
}

return evt
return &evt
}

func (h *BasicHost) makeSignedPeerRecord(addrs []ma.Multiaddr) (*record.Envelope, error) {
Expand All @@ -563,27 +548,34 @@ func (h *BasicHost) background() {
var lastAddrs []ma.Multiaddr

emitAddrChange := func(currentAddrs []ma.Multiaddr, lastAddrs []ma.Multiaddr) {
changeEvt := h.makeUpdatedAddrEvent(lastAddrs, currentAddrs)
// nothing to do if both are nil..defensive check
if currentAddrs == nil && lastAddrs == nil {
return
}

changeEvt := makeUpdatedAddrEvent(lastAddrs, currentAddrs)

if changeEvt == nil {
return
}
// Our addresses have changed.
// store the signed peer record in the peer store.

if !h.disableSignedPeerRecord {
if _, err := h.caBook.ConsumePeerRecord(changeEvt.SignedPeerRecord, peerstore.PermanentAddrTTL); err != nil {
// add signed peer record to the event
sr, err := h.makeSignedPeerRecord(currentAddrs)
if err != nil {
log.Errorf("error creating a signed peer record from the set of current addresses, err=%s", err)
return
}
changeEvt.SignedPeerRecord = sr

// persist the signed record to the peerstore
if _, err := h.caBook.ConsumePeerRecord(sr, peerstore.PermanentAddrTTL); err != nil {
log.Errorf("failed to persist signed peer record in peer store, err=%s", err)
return
}
}
// update host addresses in the peer store
removedAddrs := make([]ma.Multiaddr, 0, len(changeEvt.Removed))
for _, ua := range changeEvt.Removed {
removedAddrs = append(removedAddrs, ua.Address)
}
h.Peerstore().SetAddrs(h.ID(), currentAddrs, peerstore.PermanentAddrTTL)
h.Peerstore().SetAddrs(h.ID(), removedAddrs, 0)

// emit addr change event
// emit addr change event on the bus
if err := h.emitters.evtLocalAddrsUpdated.Emit(*changeEvt); err != nil {
log.Warnf("error emitting event for updated addrs: %s", err)
}
Expand All @@ -595,10 +587,11 @@ func (h *BasicHost) background() {
defer ticker.Stop()

for {
// Update our local IP addresses before checking our current addresses.
if len(h.network.ListenAddresses()) > 0 {
h.updateLocalIpAddr()
}
// Request addresses anyways because, technically, address filters still apply.
// The underlying AllAddrs call is effectively a no-op.
curr := h.Addrs()
emitAddrChange(curr, lastAddrs)
lastAddrs = curr
Expand Down Expand Up @@ -827,7 +820,7 @@ func (h *BasicHost) ConnManager() connmgr.ConnManager {
func (h *BasicHost) Addrs() []ma.Multiaddr {
// We don't need to append certhashes here, the user provided addrsFactory was
// wrapped with addCertHashes in the constructor.
addrs := h.AddrsFactory(h.AllAddrs())
addrs := ma.Unique(h.AddrsFactory(h.AllAddrs()))
// Make a copy. Consumers can modify the slice elements
res := make([]ma.Multiaddr, len(addrs))
copy(res, addrs)
Expand Down

0 comments on commit ffab4b6

Please sign in to comment.