Skip to content

Commit

Permalink
fix infinite discovery address leak
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Nov 15, 2024
1 parent 12f139f commit ab13fd6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 1 addition & 2 deletions p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"io"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -85,7 +84,7 @@ func (pd *PeerDiscovery) addPeer(pinfo peer.AddrInfo) {
oldPinfo, ok := pd.knownPeers[pinfo.ID]
if ok {
for _, addr := range pinfo.Addrs {
if !slices.Contains(oldPinfo.Addrs, addr) {
if !multiaddr.Contains(oldPinfo.Addrs, addr) {
oldPinfo.Addrs = append(oldPinfo.Addrs, addr)
}
}
Expand Down
5 changes: 5 additions & 0 deletions p2p/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ func TestDiscovery(t *testing.T) {
assert.Equal(t, 4, len(comm2.host.Peerstore().Peers()))
assert.Equal(t, 4, len(comm3.host.Peerstore().Peers()))
assert.Equal(t, 4, len(comm4.host.Peerstore().Peers()))

assert.Equal(t, 3, len(comm.discovery.knownPeers))
for peer, knownPeers := range comm.discovery.knownPeers {
assert.LessOrEqual(t, len(knownPeers.Addrs), 4, "%s has more than 4 addresses (%d)?", peer.String(), len(knownPeers.Addrs))
}
}

0 comments on commit ab13fd6

Please sign in to comment.