From ff03e04e0a56ad4d4470e78de4a1633f5dce9f30 Mon Sep 17 00:00:00 2001 From: y0sher Date: Sun, 8 Dec 2024 21:00:06 +0200 Subject: [PATCH] added already connected filter --- network/discovery/dv5_filters.go | 10 ++++++++++ network/discovery/dv5_routing.go | 2 +- network/discovery/dv5_service.go | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/network/discovery/dv5_filters.go b/network/discovery/dv5_filters.go index d72fa814fb..a25ebe717f 100644 --- a/network/discovery/dv5_filters.go +++ b/network/discovery/dv5_filters.go @@ -49,6 +49,16 @@ func (dvs *DiscV5Service) ssvNodeFilter(logger *zap.Logger) func(node *enode.Nod } } +func (dvs *DiscV5Service) alreadyConnectedFilter() func(node *enode.Node) bool { + return func(node *enode.Node) bool { + pid, err := PeerID(node) + if err != nil { + return false + } + return dvs.conns.Connectedness(pid) != libp2pnetwork.Connected + } +} + func (dvs *DiscV5Service) recentlyTrimmedFilter() func(node *enode.Node) bool { return func(node *enode.Node) bool { pid, err := PeerID(node) diff --git a/network/discovery/dv5_routing.go b/network/discovery/dv5_routing.go index 01d768f63a..ce00b53706 100644 --- a/network/discovery/dv5_routing.go +++ b/network/discovery/dv5_routing.go @@ -56,7 +56,7 @@ func (dvs *DiscV5Service) FindPeers(ctx context.Context, ns string, opt ...disco dvs.discover(ctx, logger, func(e PeerEvent) { cn <- e.AddrInfo - }, time.Millisecond, dvs.ssvNodeFilter(logger), dvs.badNodeFilter(logger), dvs.subnetFilter(subnet), dvs.recentlyTrimmedFilter()) + }, time.Millisecond, dvs.ssvNodeFilter(logger), dvs.badNodeFilter(logger), dvs.subnetFilter(subnet), dvs.alreadyConnectedFilter(), dvs.recentlyTrimmedFilter()) return cn, nil } diff --git a/network/discovery/dv5_service.go b/network/discovery/dv5_service.go index 13dec9c9ab..41304fdb94 100644 --- a/network/discovery/dv5_service.go +++ b/network/discovery/dv5_service.go @@ -158,7 +158,7 @@ func (dvs *DiscV5Service) Bootstrap(logger *zap.Logger, handler HandleNewPeer) e return } handler(e) - }, defaultDiscoveryInterval, dvs.ssvNodeFilter(logger), dvs.sharedSubnetsFilter(1), dvs.badNodeFilter(logger), dvs.recentlyTrimmedFilter()) + }, defaultDiscoveryInterval, dvs.ssvNodeFilter(logger), dvs.sharedSubnetsFilter(1), dvs.badNodeFilter(logger), dvs.alreadyConnectedFilter(), dvs.recentlyTrimmedFilter()) return nil }