Skip to content

Commit

Permalink
Clean addresses with peer id before adding to addrbook
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Jan 23, 2023
1 parent e004bd3 commit 1929d4e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
6 changes: 4 additions & 2 deletions p2p/host/peerstore/pstoreds/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,12 @@ func (ab *dsAddrBook) deleteAddrs(p peer.ID, addrs []ma.Multiaddr) (err error) {
func cleanAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
clean := make([]ma.Multiaddr, 0, len(addrs))
for _, addr := range addrs {
if addr == nil {
// Remove suffix of /p2p/peer-id from address
ad, _ := peer.SplitAddr(addr)
if ad == nil {
continue
}
clean = append(clean, addr)
clean = append(clean, ad)
}
return clean
}
21 changes: 12 additions & 9 deletions p2p/host/peerstore/pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,21 @@ func (mab *memoryAddrBook) addAddrsUnlocked(s *addrSegment, p peer.ID, addrs []m

exp := mab.clock.Now().Add(ttl)
for _, addr := range addrs {
if addr == nil {
// Remove suffix of /p2p/peer-id from address
taddr, _ := peer.SplitAddr(addr)
if taddr == nil {
log.Warnw("was passed nil multiaddr", "peer", p)
continue
}

// find the highest TTL and Expiry time between
// existing records and function args
a, found := amap[string(addr.Bytes())] // won't allocate.
a, found := amap[string(taddr.Bytes())] // won't allocate.
if !found {
// not found, announce it.
entry := &expiringAddr{Addr: addr, Expires: exp, TTL: ttl}
amap[string(addr.Bytes())] = entry
mab.subManager.BroadcastAddr(p, addr)
entry := &expiringAddr{Addr: taddr, Expires: exp, TTL: ttl}
amap[string(taddr.Bytes())] = entry
mab.subManager.BroadcastAddr(p, taddr)
} else {
// update ttl & exp to whichever is greater between new and existing entry
if ttl > a.TTL {
Expand Down Expand Up @@ -283,17 +285,18 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du

exp := mab.clock.Now().Add(ttl)
for _, addr := range addrs {
if addr == nil {
taddr, _ := peer.SplitAddr(addr)
if taddr == nil {
log.Warnw("was passed nil multiaddr", "peer", p)
continue
}
aBytes := addr.Bytes()
aBytes := taddr.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)
amap[key] = &expiringAddr{Addr: taddr, Expires: exp, TTL: ttl}
mab.subManager.BroadcastAddr(p, taddr)
} else {
delete(amap, key)
}
Expand Down
8 changes: 8 additions & 0 deletions p2p/host/peerstore/test/addr_book_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ func testAddAddress(ab pstore.AddrBook, clk *mockClock.Mock) func(*testing.T) {
ab.AddAddrs("", addrs, time.Hour)
AssertAddressesEqual(t, addrs, ab.Addrs(""))
})

t.Run("add a /p2p address", func(t *testing.T) {
peerId := GeneratePeerIDs(1)[0]
addr := GenerateAddrs(1)
p2pAddr := addr[0].Encapsulate(Multiaddr("/p2p/" + peerId.String()))
ab.AddAddr(peerId, p2pAddr, time.Hour)
AssertAddressesEqual(t, addr, ab.Addrs(peerId))
})
}
}

Expand Down

0 comments on commit 1929d4e

Please sign in to comment.