diff --git a/p2p/peer.go b/p2p/peer.go index ae757acc890..55e63bb1696 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -45,7 +45,7 @@ type Peer interface { SetRemovalFailed() GetRemovalFailed() bool - GetRegion() string + // GetRegion() string } //---------------------------------------------------------- @@ -129,7 +129,7 @@ type peer struct { // When removal of a peer fails, we set this flag removalAttemptFailed bool - region string + // region string } type PeerOption func(*peer) @@ -143,22 +143,21 @@ func newPeer( chDescs []*cmtconn.ChannelDescriptor, onPeerError func(Peer, interface{}), mlc *metricsLabelCache, - sameRegion bool, options ...PeerOption, ) *peer { - var peerRegion string - if sameRegion { - // Note if the new peer is in the same region as us - peerIp, err := nodeInfo.NetAddress() - if err != nil { - fmt.Print("Failed to get IP from nodeInfo", "err", err) - } - peerRegionInternal, err := getRegionFromIP(peerIp.IP.String()) - if err != nil { - fmt.Print("Failed to get region from IP", "err", err) - } - peerRegion = peerRegionInternal - } + // var peerRegion string + // if sameRegion { + // // Note if the new peer is in the same region as us + // peerIp, err := nodeInfo.NetAddress() + // if err != nil { + // fmt.Print("Failed to get IP from nodeInfo", "err", err) + // } + // peerRegionInternal, err := getRegionFromIP(peerIp.IP.String()) + // if err != nil { + // fmt.Print("Failed to get region from IP", "err", err) + // } + // peerRegion = peerRegionInternal + // } p := &peer{ peerConn: pc, @@ -168,7 +167,7 @@ func newPeer( metricsTicker: time.NewTicker(metricsTickerDuration), metrics: NopMetrics(), mlc: mlc, - region: peerRegion, + // region: peerRegion, } p.mconn = createMConnection( @@ -457,6 +456,6 @@ func createMConnection( ) } -func (p *peer) GetRegion() string { - return p.region -} +// func (p *peer) GetRegion() string { +// return p.region +// } diff --git a/p2p/peer_test.go b/p2p/peer_test.go index 625d6f77854..9d73e0a6663 100644 --- a/p2p/peer_test.go +++ b/p2p/peer_test.go @@ -99,7 +99,7 @@ func createOutboundPeerAndPerformHandshake( return nil, err } - p := newPeer(pc, mConfig, peerNodeInfo, reactorsByCh, msgTypeByChID, chDescs, func(p Peer, r interface{}) {}, newMetricsLabelCache(), false) + p := newPeer(pc, mConfig, peerNodeInfo, reactorsByCh, msgTypeByChID, chDescs, func(p Peer, r interface{}) {}, newMetricsLabelCache()) p.SetLogger(log.TestingLogger().With("peer", addr)) return p, nil } diff --git a/p2p/pex/addrbook.go b/p2p/pex/addrbook.go index a8f80c5c2f7..385461fa4eb 100644 --- a/p2p/pex/addrbook.go +++ b/p2p/pex/addrbook.go @@ -50,11 +50,11 @@ type AddrBook interface { // Add and remove an address AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error - PickAddressNotInRegion(biasTowardsNewAddrs int, region string) *p2p.NetAddress RemoveAddress(*p2p.NetAddress) // Check if the address is in the book HasAddress(*p2p.NetAddress) bool + GetAddressRegion(addr *p2p.NetAddress) string // Do we need more peers? NeedMoreAddrs() bool @@ -65,6 +65,7 @@ type AddrBook interface { // Pick an address to dial PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress PickAddressWithRegion(biasTowardsNewAddrs int, region string) *p2p.NetAddress + PickAddressNotInRegion(biasTowardsNewAddrs int, region string) *p2p.NetAddress // Mark address MarkGood(p2p.ID) @@ -271,6 +272,17 @@ func (a *addrBook) HasAddress(addr *p2p.NetAddress) bool { return ka != nil } +func (a *addrBook) GetAddressRegion(addr *p2p.NetAddress) string { + a.mtx.Lock() + defer a.mtx.Unlock() + + ka, exists := a.addrLookup[addr.ID] + if !exists { + return "" + } + return ka.Region +} + // NeedMoreAddrs implements AddrBook - returns true if there are not have enough addresses in the book. func (a *addrBook) NeedMoreAddrs() bool { return a.Size() < needAddressThreshold @@ -340,6 +352,8 @@ func (a *addrBook) PickAddressWithRegion(biasTowardsNewAddrs int, region string) } ka.Region = region a.addrLookup[ka.ID()] = ka + } else { + fmt.Println("PickAddressWithRegion Region already set", ka.Addr, "region", ka.Region) } if ka.Region == region { fmt.Println("PickAddressWithRegion Adding address", ka.Addr, "region", ka.Region) @@ -424,6 +438,8 @@ func (a *addrBook) PickAddressNotInRegion(biasTowardsNewAddrs int, region string } ka.Region = region a.addrLookup[ka.ID()] = ka + } else { + fmt.Println("PickAddressNotInRegion Region already set", ka.Addr, "region", ka.Region) } if ka.Region != region { fmt.Println("PickAddressNotInRegion Adding address", ka.Addr, "region", ka.Region) diff --git a/p2p/switch.go b/p2p/switch.go index 09450c6f26e..df5eca1b774 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -56,6 +56,7 @@ type AddrBook interface { MarkGood(ID) RemoveAddress(*NetAddress) HasAddress(*NetAddress) bool + GetAddressRegion(*NetAddress) string Save() } @@ -414,13 +415,13 @@ func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) { sw.metrics.Peers.Add(float64(-1)) if peer.IsOutbound() { if sw.config.SameRegion { - if peer.GetRegion() != sw.config.MyRegion { + if sw.addrBook.GetAddressRegion(peer.SocketAddr()) != sw.config.MyRegion { sw.config.CurrentNumOutboundPeersInOtherRegion-- } } } else { if sw.config.SameRegion { - if peer.GetRegion() != sw.config.MyRegion { + if sw.addrBook.GetAddressRegion(peer.SocketAddr()) != sw.config.MyRegion { sw.config.CurrentNumInboundPeersInOtherRegion-- } } @@ -754,8 +755,8 @@ func (sw *Switch) acceptRoutine() { if sw.config.SameRegion { // Note if the new peer is in the same region as us - fmt.Println("Checking if peer is same region. My region: ", sw.config.MyRegion, " Peer region: ", p.GetRegion()) - isSameRegion := p.GetRegion() == sw.config.MyRegion + fmt.Println("Checking if peer is same region. My region: ", sw.config.MyRegion, " Peer region: ", sw.addrBook.GetAddressRegion(p.SocketAddr())) + isSameRegion := sw.addrBook.GetAddressRegion(p.SocketAddr()) == sw.config.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 @@ -981,9 +982,9 @@ func (sw *Switch) addPeer(p Peer) error { } sw.metrics.Peers.Add(float64(1)) if sw.config.SameRegion { - if p.IsOutbound() && p.GetRegion() != sw.config.MyRegion { + if p.IsOutbound() && sw.addrBook.GetAddressRegion(p.SocketAddr()) != sw.config.MyRegion { sw.config.CurrentNumOutboundPeersInOtherRegion++ - } else if !p.IsOutbound() && p.GetRegion() != sw.config.MyRegion { + } else if !p.IsOutbound() && sw.addrBook.GetAddressRegion(p.SocketAddr()) != sw.config.MyRegion { sw.config.CurrentNumInboundPeersInOtherRegion++ } } diff --git a/p2p/test_util.go b/p2p/test_util.go index 966b9028ce2..4567a797ce6 100644 --- a/p2p/test_util.go +++ b/p2p/test_util.go @@ -153,7 +153,6 @@ func (sw *Switch) addPeerWithConnection(conn net.Conn) error { sw.chDescs, sw.StopPeerForError, sw.mlc, - sw.config.SameRegion, ) if err = sw.addPeer(p); err != nil { @@ -306,6 +305,9 @@ func (book *AddrBookMock) HasAddress(addr *NetAddress) bool { _, ok := book.Addrs[addr.String()] return ok } +func (book *AddrBookMock) GetAddressRegion(addr *NetAddress) string { + return "" +} func (book *AddrBookMock) RemoveAddress(addr *NetAddress) { delete(book.Addrs, addr.String()) } diff --git a/p2p/transport.go b/p2p/transport.go index c4991c12a3c..617072ca0f2 100644 --- a/p2p/transport.go +++ b/p2p/transport.go @@ -527,7 +527,6 @@ func (mt *MultiplexTransport) wrapPeer( cfg.chDescs, cfg.onPeerError, cfg.mlc, - cfg.sameRegion, PeerMetrics(cfg.metrics), )