From cfaeb2718f5bf801c415d080767089c4afd12057 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 8 Sep 2023 14:49:40 -0700 Subject: [PATCH 1/2] fix: networking: avoid dialing when trying to handshake peers Also, avoid handshaking with peers that don't support the handshake protocol. --- node/modules/services.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/node/modules/services.go b/node/modules/services.go index bb1d41917b3..a76b92bde23 100644 --- a/node/modules/services.go +++ b/node/modules/services.go @@ -2,6 +2,7 @@ package modules import ( "context" + "fmt" "os" "strconv" "time" @@ -11,6 +12,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" "github.com/libp2p/go-libp2p/core/event" "github.com/libp2p/go-libp2p/core/host" + "github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/peer" "github.com/libp2p/go-libp2p/core/protocol" "github.com/libp2p/go-libp2p/p2p/host/eventbus" @@ -66,18 +68,22 @@ func RunHello(mctx helpers.MetricsCtx, lc fx.Lifecycle, h host.Host, svc *hello. ctx := helpers.LifecycleCtx(mctx, lc) go func() { + // We want to get information on connected peers, we don't want to trigger new connections. + ctx := network.WithNoDial(ctx, "filecoin hello") for evt := range sub.Out() { pic := evt.(event.EvtPeerIdentificationCompleted) + // We just finished identifying the peer, that means we should know what + // protocols it speaks. Check if it speeks the Filecoin hello protocol + // before continuing. + if p, _ := h.Peerstore().FirstSupportedProtocol(pic.Peer, hello.ProtocolID); p != hello.ProtocolID { + continue + } + go func() { if err := svc.SayHello(ctx, pic.Peer); err != nil { protos, _ := h.Peerstore().GetProtocols(pic.Peer) agent, _ := h.Peerstore().Get(pic.Peer, "AgentVersion") - if protosContains(protos, hello.ProtocolID) { - log.Warnw("failed to say hello", "error", err, "peer", pic.Peer, "supported", protos, "agent", agent) - } else { - log.Debugw("failed to say hello", "error", err, "peer", pic.Peer, "supported", protos, "agent", agent) - } - return + log.Warnw("failed to say hello", "error", err, "peer", pic.Peer, "supported", protos, "agent", agent) } }() } @@ -85,15 +91,6 @@ func RunHello(mctx helpers.MetricsCtx, lc fx.Lifecycle, h host.Host, svc *hello. return nil } -func protosContains(protos []protocol.ID, search protocol.ID) bool { - for _, p := range protos { - if p == search { - return true - } - } - return false -} - func RunPeerMgr(mctx helpers.MetricsCtx, lc fx.Lifecycle, pmgr *peermgr.PeerMgr) { go pmgr.Run(helpers.LifecycleCtx(mctx, lc)) } From 89dede32ac6e9b6a806969405677468c90c57acd Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 11 Sep 2023 13:50:55 -0400 Subject: [PATCH 2/2] drop unused imports --- node/modules/services.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/node/modules/services.go b/node/modules/services.go index a76b92bde23..f3dd443d94d 100644 --- a/node/modules/services.go +++ b/node/modules/services.go @@ -2,7 +2,6 @@ package modules import ( "context" - "fmt" "os" "strconv" "time" @@ -14,7 +13,6 @@ import ( "github.com/libp2p/go-libp2p/core/host" "github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/peer" - "github.com/libp2p/go-libp2p/core/protocol" "github.com/libp2p/go-libp2p/p2p/host/eventbus" "go.uber.org/fx" "golang.org/x/xerrors"