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

p2p: exclude self from GetPeers results #6132

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions network/p2pNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,9 @@
const maxNodes = 100
addrInfos := n.pstore.GetAddresses(maxNodes, phonebook.PhoneBookEntryRelayRole)
for _, peerInfo := range addrInfos {
if peerInfo.ID == n.service.ID() {
continue
}
if peerCore, ok := addrInfoToWsPeerCore(n, peerInfo); ok {
peers = append(peers, &peerCore)
}
Expand All @@ -664,9 +667,10 @@
}
n.log.Debugf("Got %d archival node(s) from DHT", len(infos))
for _, addrInfo := range infos {
// TODO: remove after go1.22
info := addrInfo
if peerCore, ok := addrInfoToWsPeerCore(n, &info); ok {
if addrInfo.ID == n.service.ID() {
continue

Check warning on line 671 in network/p2pNetwork.go

View check run for this annotation

Codecov / codecov/patch

network/p2pNetwork.go#L671

Added line #L671 was not covered by tests
}
if peerCore, ok := addrInfoToWsPeerCore(n, &addrInfo); ok {
peers = append(peers, &peerCore)
}
}
Expand Down
47 changes: 47 additions & 0 deletions network/p2pNetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -1405,3 +1406,49 @@ func TestP2PTxTopicValidator_NoWsPeer(t *testing.T) {
res := net.txTopicValidator(ctx, peerID, &msg)
require.Equal(t, pubsub.ValidationAccept, res)
}

// TestGetPeersFiltersSelf checks that GetPeers does not return the node's own peer ID.
// The test adds a self peer to the peerstore and another peer to the peerstore and verifies that
// the self peer is not in the returned list.
func TestGetPeersFiltersSelf(t *testing.T) {
partitiontest.PartitionTest(t)

log := logging.TestingLog(t)
cfg := config.GetDefaultLocal()

net, err := NewP2PNetwork(log, cfg, t.TempDir(), []string{}, "test-genesis", "test-network", &nopeNodeInfo{}, nil)
require.NoError(t, err)
selfID := net.service.ID()

// Create and add self
selfAddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/p2p/" + selfID.String())
require.NoError(t, err)
selfInfo := &peer.AddrInfo{
ID: selfID,
Addrs: []multiaddr.Multiaddr{selfAddr},
}
net.pstore.AddPersistentPeers([]*peer.AddrInfo{selfInfo}, "test-network", phonebook.PhoneBookEntryRelayRole)

// Create and add another peer
otherID, err := peer.Decode("QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N")
require.NoError(t, err)
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/p2p/" + otherID.String())
require.NoError(t, err)
otherInfo := &peer.AddrInfo{
ID: otherID,
Addrs: []multiaddr.Multiaddr{addr},
}
net.pstore.AddPersistentPeers([]*peer.AddrInfo{otherInfo}, "test-network", phonebook.PhoneBookEntryRelayRole)

peers := net.GetPeers(PeersPhonebookRelays)

// Verify that self peer is not in the returned list
for _, p := range peers {
switch peer := p.(type) {
case *wsPeerCore:
require.NotEqual(t, selfAddr.String(), peer.GetAddress(), "GetPeers should not return the node's own peer ID")
default:
t.Fatalf("unexpected peer type: %T", peer)
}
}
}
Loading