aboutsummaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp36
1 files changed, 30 insertions, 6 deletions
diff --git a/src/net.cpp b/src/net.cpp
index e8a27c3530..468358a94d 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -354,6 +354,11 @@ CNode* CConnman::FindNode(const CService& addr)
return nullptr;
}
+bool CConnman::AlreadyConnectedToAddress(const CAddress& addr)
+{
+ return FindNode(static_cast<CNetAddr>(addr)) || FindNode(addr.ToStringIPPort());
+}
+
bool CConnman::CheckIncomingNonce(uint64_t nonce)
{
LOCK(cs_vNodes);
@@ -1994,11 +1999,30 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
if (nTries > 100)
break;
- CAddrInfo addr = addrman.SelectTriedCollision();
+ CAddrInfo addr;
- // SelectTriedCollision returns an invalid address if it is empty.
- if (!fFeeler || !addr.IsValid()) {
- addr = addrman.Select(fFeeler);
+ if (fFeeler) {
+ // First, try to get a tried table collision address. This returns
+ // an empty (invalid) address if there are no collisions to try.
+ addr = addrman.SelectTriedCollision();
+
+ if (!addr.IsValid()) {
+ // No tried table collisions. Select a new table address
+ // for our feeler.
+ addr = addrman.Select(true);
+ } else if (AlreadyConnectedToAddress(addr)) {
+ // If test-before-evict logic would have us connect to a
+ // peer that we're already connected to, just mark that
+ // address as Good(). We won't be able to initiate the
+ // connection anyway, so this avoids inadvertently evicting
+ // a currently-connected peer.
+ addrman.Good(addr);
+ // Select a new table address for our feeler instead.
+ addr = addrman.Select(true);
+ }
+ } else {
+ // Not a feeler
+ addr = addrman.Select();
}
// Require outbound connections, other than feelers, to be to distinct network groups
@@ -2160,7 +2184,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
}
if (!pszDest) {
bool banned_or_discouraged = m_banman && (m_banman->IsDiscouraged(addrConnect) || m_banman->IsBanned(addrConnect));
- if (IsLocal(addrConnect) || FindNode(static_cast<CNetAddr>(addrConnect)) || banned_or_discouraged || FindNode(addrConnect.ToStringIPPort())) {
+ if (IsLocal(addrConnect) || banned_or_discouraged || AlreadyConnectedToAddress(addrConnect)) {
return;
}
} else if (FindNode(std::string(pszDest)))
@@ -2624,7 +2648,7 @@ void CConnman::DeleteNode(CNode* pnode)
{
assert(pnode);
bool fUpdateConnectionTime = false;
- m_msgproc->FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
+ m_msgproc->FinalizeNode(*pnode, fUpdateConnectionTime);
if (fUpdateConnectionTime) {
addrman.Connected(pnode->addr);
}