Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mdns: don't discover ourselves #1661

Merged
merged 5 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions p2p/discovery/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ func (s *mdnsService) startResolver(ctx context.Context) {
continue
}
for _, info := range infos {
if info.ID == s.host.ID() {
continue
}
go s.notifee.HandlePeerFound(info)
}
}
Expand Down
38 changes: 10 additions & 28 deletions p2p/discovery/mdns/mdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,6 @@ func (n *notif) GetPeers() []peer.AddrInfo {
return infos
}

func TestSelfDiscovery(t *testing.T) {
notif := &notif{}
hostID := setupMDNS(t, notif)
assert.Eventuallyf(
t,
func() bool {
var found bool
for _, info := range notif.GetPeers() {
if info.ID == hostID {
found = true
break
}
}
return found
},
5*time.Second,
5*time.Millisecond,
"expected peer to find itself",
)
}

func TestOtherDiscovery(t *testing.T) {
const n = 4

Expand All @@ -78,11 +57,14 @@ func TestOtherDiscovery(t *testing.T) {
hostIDs[i] = setupMDNS(t, notif)
}

containsAllHostIDs := func(ids []peer.ID) bool {
for _, id := range hostIDs {
containsAllHostIDs := func(ids []peer.ID,hostCheckIndex int) bool {
for i := 0; i < n; i++ {
var found bool
for _, i := range ids {
if id == i {
if i == hostCheckIndex {
continue
}
for _, id := range ids {
if hostIDs[i] == id {
found = true
break
}
Expand All @@ -97,13 +79,13 @@ func TestOtherDiscovery(t *testing.T) {
assert.Eventuallyf(
t,
func() bool {
for _, notif := range notifs {
infos := notif.GetPeers()
for i := 0; i < n; i++ {
infos := notifs[i].GetPeers()
ids := make([]peer.ID, 0, len(infos))
for _, info := range infos {
ids = append(ids, info.ID)
}
if !containsAllHostIDs(ids) {
if !containsAllHostIDs(ids,i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just pass the peer ID into this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggestion.
I've revised it.
Do you think it's ok?
Please contact me if there is anything else that needs to be modified.

return false
}
}
Expand Down