Skip to content

Commit fb26dd2

Browse files
EclesioMeloJuniortimwu20
authored andcommitted
fix(dot/network): check if peer supports protocol (ChainSafe#1617)
1 parent 4ab856e commit fb26dd2

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

dot/network/host.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,17 @@ func (h *host) peers() []peer.ID {
362362
return h.h.Network().Peers()
363363
}
364364

365+
// supportsProtocol checks if the protocol is supported by peerID
366+
// returns an error if could not get peer protocols
367+
func (h *host) supportsProtocol(peerID peer.ID, protocol protocol.ID) (bool, error) {
368+
peerProtocols, err := h.h.Peerstore().SupportsProtocols(peerID, string(protocol))
369+
if err != nil {
370+
return false, err
371+
}
372+
373+
return len(peerProtocols) > 0, nil
374+
}
375+
365376
// peerCount returns the number of connected peers
366377
func (h *host) peerCount() int {
367378
peers := h.h.Network().Peers()

dot/network/host_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/ChainSafe/gossamer/lib/common"
2525
"github.com/ChainSafe/gossamer/lib/utils"
26+
"github.com/libp2p/go-libp2p-core/protocol"
2627
ma "github.com/multiformats/go-multiaddr"
2728

2829
"github.com/stretchr/testify/require"
@@ -366,3 +367,71 @@ func TestStreamCloseMetadataCleanup(t *testing.T) {
366367
_, ok = info.getHandshakeData(nodeB.host.id(), true)
367368
require.False(t, ok)
368369
}
370+
371+
func Test_PeerSupportsProtocol(t *testing.T) {
372+
basePathA := utils.NewTestBasePath(t, "nodeA")
373+
configA := &Config{
374+
BasePath: basePathA,
375+
Port: 7001,
376+
RandSeed: 1,
377+
NoBootstrap: true,
378+
NoMDNS: true,
379+
}
380+
381+
nodeA := createTestService(t, configA)
382+
383+
basePathB := utils.NewTestBasePath(t, "nodeB")
384+
configB := &Config{
385+
BasePath: basePathB,
386+
Port: 7002,
387+
RandSeed: 2,
388+
NoBootstrap: true,
389+
NoMDNS: true,
390+
}
391+
392+
nodeB := createTestService(t, configB)
393+
nodeB.noGossip = true
394+
395+
addrInfosB, err := nodeB.host.addrInfos()
396+
require.NoError(t, err)
397+
398+
err = nodeA.host.connect(*addrInfosB[0])
399+
// retry connect if "failed to dial" error
400+
if failedToDial(err) {
401+
time.Sleep(TestBackoffTimeout)
402+
err = nodeA.host.connect(*addrInfosB[0])
403+
}
404+
require.NoError(t, err)
405+
406+
tests := []struct {
407+
protocol protocol.ID
408+
expect bool
409+
}{
410+
{
411+
protocol: protocol.ID("/gossamer/test/0/sync/2"),
412+
expect: true,
413+
},
414+
{
415+
protocol: protocol.ID("/gossamer/test/0/light/2"),
416+
expect: true,
417+
},
418+
{
419+
protocol: protocol.ID("/gossamer/test/0/block-announces/1"),
420+
expect: true,
421+
},
422+
{
423+
protocol: protocol.ID("/gossamer/test/0/transactions/1"),
424+
expect: true,
425+
},
426+
{
427+
protocol: protocol.ID("/gossamer/not_supported/protocol"),
428+
expect: false,
429+
},
430+
}
431+
432+
for _, test := range tests {
433+
output, err := nodeA.host.supportsProtocol(nodeB.host.id(), test.protocol)
434+
require.NoError(t, err)
435+
require.Equal(t, test.expect, output)
436+
}
437+
}

dot/network/notifications.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ func (s *Service) createNotificationsMessageHandler(info *notificationsProtocol,
206206
}
207207

208208
func (s *Service) sendData(peer peer.ID, hs Handshake, info *notificationsProtocol, msg NotificationsMessage) {
209+
if support, err := s.host.supportsProtocol(peer, info.protocolID); err != nil || !support {
210+
logger.Debug("the peer does not supports the protocol", "protocol", info.protocolID, "peer", peer, "err", err)
211+
return
212+
}
213+
209214
hsData, has := info.getHandshakeData(peer, false)
210215
if has && !hsData.validated {
211216
// peer has sent us an invalid handshake in the past, ignore

0 commit comments

Comments
 (0)