From d8cd3903074f766865aa092c7ce44866a9e7f2d2 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Mon, 24 Jun 2024 14:48:32 -0600 Subject: [PATCH] check inbound region for sameRegion --- p2p/switch.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/p2p/switch.go b/p2p/switch.go index ff26cf0200..c95501f11c 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -790,22 +790,38 @@ func (sw *Switch) acceptRoutine() { // Note if the new peer is in the same region as us isSameRegion := sw.addrBook.GetAddressRegion(p.SocketAddr()) == sw.MyRegion - if !isSameRegion { - // If this peer is not in our same region and we have no room to dial peers outside of our region, return error - // TODO check this formula + // Calculate the maximum allowed peers for both same region and other regions + maxOutboundPeersInSameRegion := int(sw.config.MaxPercentPeersInSameRegion * float64(sw.config.MaxNumOutboundPeers)) + maxInboundPeersInSameRegion := int(sw.config.MaxPercentPeersInSameRegion * float64(sw.config.MaxNumInboundPeers)) + maxOutboundPeersInOtherRegion := sw.config.MaxNumOutboundPeers - maxOutboundPeersInSameRegion + maxInboundPeersInOtherRegion := sw.config.MaxNumInboundPeers - maxInboundPeersInSameRegion + + if isSameRegion { + out, in, _ := sw.NumPeers() + if p.IsOutbound() { - maxOutboundPeersInOtherRegion := sw.config.MaxNumOutboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumOutboundPeers)) - if sw.CurrentNumOutboundPeersInOtherRegion+1 > maxOutboundPeersInOtherRegion { + if (out-sw.CurrentNumOutboundPeersInOtherRegion)+1 > maxOutboundPeersInSameRegion { sw.Logger.Error("exceeds max percent peers in same region") continue } } else { - maxInboundPeersInOtherRegion := sw.config.MaxNumInboundPeers - int(sw.config.MaxPercentPeersInSameRegion*float64(sw.config.MaxNumInboundPeers)) - if sw.CurrentNumInboundPeersInOtherRegion+1 > maxInboundPeersInOtherRegion { + if (in-sw.CurrentNumInboundPeersInOtherRegion)+1 > maxInboundPeersInSameRegion { sw.Logger.Error("exceeds max percent peers in same region") continue } } + } else { + if p.IsOutbound() { + if sw.CurrentNumOutboundPeersInOtherRegion+1 > maxOutboundPeersInOtherRegion { + sw.Logger.Error("exceeds max percent peers in other regions") + continue + } + } else { + if sw.CurrentNumInboundPeersInOtherRegion+1 > maxInboundPeersInOtherRegion { + sw.Logger.Error("exceeds max percent peers in other regions") + continue + } + } } }