From 7f41070da070eadd3312efce1cc821aaf3e23771 Mon Sep 17 00:00:00 2001 From: Mehmet Gurevin Date: Thu, 18 Oct 2018 18:52:51 +0300 Subject: [PATCH] p2p: re-check after sleeps --- p2p/netaddress.go | 5 +++++ p2p/pex/pex_reactor.go | 4 +++- p2p/switch.go | 13 +++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/p2p/netaddress.go b/p2p/netaddress.go index ec9a0ea7c..4bc37ddfe 100644 --- a/p2p/netaddress.go +++ b/p2p/netaddress.go @@ -34,6 +34,11 @@ type NetAddress struct { // IDAddressString returns id@hostPort. func IDAddressString(id ID, hostPort string) string { + // we respect the protocol definition in here. + if p := strings.Index(hostPort, "://"); p > -1 { + return fmt.Sprintf("%s://%s@%s", hostPort[:p], id, hostPort[p+3:]) + } + return fmt.Sprintf("%s@%s", id, hostPort) } diff --git a/p2p/pex/pex_reactor.go b/p2p/pex/pex_reactor.go index c919794ab..52069ddef 100644 --- a/p2p/pex/pex_reactor.go +++ b/p2p/pex/pex_reactor.go @@ -8,7 +8,6 @@ import ( "time" "github.com/pkg/errors" - amino "github.com/tendermint/go-amino" cmn "github.com/tendermint/tendermint/libs/common" "github.com/tendermint/tendermint/p2p" @@ -395,6 +394,9 @@ func (r *PEXReactor) ensurePeers() { if r.Switch.IsDialingOrExistingAddress(try) { continue } + if r.Switch.NodeInfo().ID == try.ID { + continue // we don't want to dial ourselves, usually. + } // TODO: consider moving some checks from toDial into here // so we don't even consider dialing peers that we want to wait // before dialling again, or have dialed too many times already diff --git a/p2p/switch.go b/p2p/switch.go index 64e248fc3..e1c8a9007 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -325,6 +325,11 @@ func (sw *Switch) reconnectToPeer(addr *NetAddress) { return } + if sw.IsDialingOrExistingAddress(addr) { + sw.Logger.Info("Peer connection has been established or dialed while we waiting next try", "addr", addr) + return + } + err := sw.DialPeerWithAddress(addr, true) if err == nil { return // success @@ -412,12 +417,16 @@ func (sw *Switch) DialPeersAsync(addrBook AddrBook, peers []string, persistent b if addr.Same(ourAddr) { sw.Logger.Debug("Ignore attempt to connect to ourselves", "addr", addr, "ourAddr", ourAddr) return - } else if sw.IsDialingOrExistingAddress(addr) { + } + + sw.randomSleep(0) + + // check the destination address in established/dialed peers after sleep. + if sw.IsDialingOrExistingAddress(addr) { sw.Logger.Debug("Ignore attempt to connect to an existing peer", "addr", addr) return } - sw.randomSleep(0) err := sw.DialPeerWithAddress(addr, persistent) if err != nil { switch err.(type) {