From b54a59d06871c6a5914df6b0936f786e35d75184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20Melo=20J=C3=BAnior?= Date: Tue, 1 Jun 2021 16:28:45 -0400 Subject: [PATCH 1/4] feat: check peer supports the protocol --- dot/network/host.go | 11 ++++++ dot/network/host_test.go | 69 ++++++++++++++++++++++++++++++++++++ dot/network/notifications.go | 8 ++++- 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/dot/network/host.go b/dot/network/host.go index 125e7f4781..d1013fad9b 100644 --- a/dot/network/host.go +++ b/dot/network/host.go @@ -362,6 +362,17 @@ func (h *host) peers() []peer.ID { return h.h.Network().Peers() } +// getPeerProtocols returns all the supported protocols to one peer +// returns an error if could not get peer protocols +func (h *host) supportsProtocol(peerID peer.ID, protocol protocol.ID) (bool, error) { + peerProtocols, err := h.h.Network().Peerstore().SupportsProtocols(peerID, string(protocol)) + if err != nil { + return false, err + } + + return len(peerProtocols) > 0, nil +} + // peerCount returns the number of connected peers func (h *host) peerCount() int { peers := h.h.Network().Peers() diff --git a/dot/network/host_test.go b/dot/network/host_test.go index 1d0b17e621..7330f71441 100644 --- a/dot/network/host_test.go +++ b/dot/network/host_test.go @@ -23,6 +23,7 @@ import ( "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/utils" + "github.com/libp2p/go-libp2p-core/protocol" ma "github.com/multiformats/go-multiaddr" "github.com/stretchr/testify/require" @@ -366,3 +367,71 @@ func TestStreamCloseMetadataCleanup(t *testing.T) { _, ok = info.getHandshakeData(nodeB.host.id(), true) require.False(t, ok) } + +func Test_PeerSupportsProtocol(t *testing.T) { + basePathA := utils.NewTestBasePath(t, "nodeA") + configA := &Config{ + BasePath: basePathA, + Port: 7001, + RandSeed: 1, + NoBootstrap: true, + NoMDNS: true, + } + + nodeA := createTestService(t, configA) + + basePathB := utils.NewTestBasePath(t, "nodeB") + configB := &Config{ + BasePath: basePathB, + Port: 7002, + RandSeed: 2, + NoBootstrap: true, + NoMDNS: true, + } + + nodeB := createTestService(t, configB) + nodeB.noGossip = true + + addrInfosB, err := nodeB.host.addrInfos() + require.NoError(t, err) + + err = nodeA.host.connect(*addrInfosB[0]) + // retry connect if "failed to dial" error + if failedToDial(err) { + time.Sleep(TestBackoffTimeout) + err = nodeA.host.connect(*addrInfosB[0]) + } + require.NoError(t, err) + + tests := []struct { + protocol protocol.ID + expect bool + }{ + { + protocol: protocol.ID("/gossamer/test/0/sync/2"), + expect: true, + }, + { + protocol: protocol.ID("/gossamer/test/0/light/2"), + expect: true, + }, + { + protocol: protocol.ID("/gossamer/test/0/block-announces/1"), + expect: true, + }, + { + protocol: protocol.ID("/gossamer/test/0/transactions/1"), + expect: true, + }, + { + protocol: protocol.ID("/gossamer/not_supported/protocol"), + expect: false, + }, + } + + for _, test := range tests { + output, err := nodeA.host.supportsProtocol(nodeB.host.id(), test.protocol) + require.NoError(t, err) + require.Equal(t, test.expect, output) + } +} diff --git a/dot/network/notifications.go b/dot/network/notifications.go index 4f77099891..13673aac5a 100644 --- a/dot/network/notifications.go +++ b/dot/network/notifications.go @@ -198,6 +198,12 @@ func (s *Service) createNotificationsMessageHandler(info *notificationsProtocol, } func (s *Service) sendData(peer peer.ID, hs Handshake, info *notificationsProtocol, msg NotificationsMessage) { + support, err := s.host.supportsProtocol(peer, info.protocolID) + if err != nil || !support { + logger.Warn("the peer does not supports the protocol", "protocol", info.protocolID, "peer", peer, "err", err) + return + } + hsData, has := info.getHandshakeData(peer, false) if has && !hsData.validated { // peer has sent us an invalid handshake in the past, ignore @@ -263,7 +269,7 @@ func (s *Service) sendData(peer peer.ID, hs Handshake, info *notificationsProtoc // we've completed the handshake with the peer, send message directly logger.Trace("sending message", "protocol", info.protocolID, "peer", peer, "message", msg) - err := s.host.writeToStream(hsData.stream, msg) + err = s.host.writeToStream(hsData.stream, msg) if err != nil { logger.Trace("failed to send message to peer", "peer", peer, "error", err) } From b79fc86f699234b01ef070db6a38f486c0653663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20Melo=20J=C3=BAnior?= Date: Tue, 1 Jun 2021 19:33:29 -0400 Subject: [PATCH 2/4] chore: fix lint --- dot/network/notifications.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dot/network/notifications.go b/dot/network/notifications.go index 13673aac5a..4bb5c8cbc3 100644 --- a/dot/network/notifications.go +++ b/dot/network/notifications.go @@ -198,8 +198,7 @@ func (s *Service) createNotificationsMessageHandler(info *notificationsProtocol, } func (s *Service) sendData(peer peer.ID, hs Handshake, info *notificationsProtocol, msg NotificationsMessage) { - support, err := s.host.supportsProtocol(peer, info.protocolID) - if err != nil || !support { + if support, err := s.host.supportsProtocol(peer, info.protocolID); err != nil || !support { logger.Warn("the peer does not supports the protocol", "protocol", info.protocolID, "peer", peer, "err", err) return } @@ -269,7 +268,7 @@ func (s *Service) sendData(peer peer.ID, hs Handshake, info *notificationsProtoc // we've completed the handshake with the peer, send message directly logger.Trace("sending message", "protocol", info.protocolID, "peer", peer, "message", msg) - err = s.host.writeToStream(hsData.stream, msg) + err := s.host.writeToStream(hsData.stream, msg) if err != nil { logger.Trace("failed to send message to peer", "peer", peer, "error", err) } From 4c27f7233483fdd4e3136ec72313b1bc3a08ed9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20Melo=20J=C3=BAnior?= Date: Wed, 2 Jun 2021 09:09:47 -0400 Subject: [PATCH 3/4] chore: adjust func description and log level --- dot/network/host.go | 2 +- dot/network/notifications.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dot/network/host.go b/dot/network/host.go index d1013fad9b..1d7a79349b 100644 --- a/dot/network/host.go +++ b/dot/network/host.go @@ -362,7 +362,7 @@ func (h *host) peers() []peer.ID { return h.h.Network().Peers() } -// getPeerProtocols returns all the supported protocols to one peer +// supportsProtocol checks if the protocol is supported by peerID // returns an error if could not get peer protocols func (h *host) supportsProtocol(peerID peer.ID, protocol protocol.ID) (bool, error) { peerProtocols, err := h.h.Network().Peerstore().SupportsProtocols(peerID, string(protocol)) diff --git a/dot/network/notifications.go b/dot/network/notifications.go index 4bb5c8cbc3..6ef0031311 100644 --- a/dot/network/notifications.go +++ b/dot/network/notifications.go @@ -199,7 +199,7 @@ func (s *Service) createNotificationsMessageHandler(info *notificationsProtocol, func (s *Service) sendData(peer peer.ID, hs Handshake, info *notificationsProtocol, msg NotificationsMessage) { if support, err := s.host.supportsProtocol(peer, info.protocolID); err != nil || !support { - logger.Warn("the peer does not supports the protocol", "protocol", info.protocolID, "peer", peer, "err", err) + logger.Debug("the peer does not supports the protocol", "protocol", info.protocolID, "peer", peer, "err", err) return } From c4755018e495b01c09047fd5715a05ab3dc381d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20Melo=20J=C3=BAnior?= Date: Wed, 2 Jun 2021 16:34:49 -0400 Subject: [PATCH 4/4] chore: call peerstore directly --- dot/network/host.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dot/network/host.go b/dot/network/host.go index 1d7a79349b..84cbd8bcc2 100644 --- a/dot/network/host.go +++ b/dot/network/host.go @@ -365,7 +365,7 @@ func (h *host) peers() []peer.ID { // supportsProtocol checks if the protocol is supported by peerID // returns an error if could not get peer protocols func (h *host) supportsProtocol(peerID peer.ID, protocol protocol.ID) (bool, error) { - peerProtocols, err := h.h.Network().Peerstore().SupportsProtocols(peerID, string(protocol)) + peerProtocols, err := h.h.Peerstore().SupportsProtocols(peerID, string(protocol)) if err != nil { return false, err }