Skip to content

Commit

Permalink
Prefer to disconnect peers in favor of whitelisted peers
Browse files Browse the repository at this point in the history
  • Loading branch information
pstratem authored and Fuzzbawls committed Jun 23, 2020
1 parent a1c4aaf commit 0ca7ce3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ class CompareNetGroupKeyed
}
};

static bool AttemptToEvictConnection() {
static bool AttemptToEvictConnection(bool fPreferNewConnection) {
std::vector<CNode*> vEvictionCandidates;
{
LOCK(cs_vNodes);
Expand Down Expand Up @@ -979,8 +979,11 @@ static bool AttemptToEvictConnection() {
// Reduce to the CNetAddr with the most connections
vEvictionCandidates = mapAddrCounts[naMostConnections];

// Do not disconnect peers who have only 1 evictable connection
if (vEvictionCandidates.size() <= 1)
return false;
// unless we prefer the new connection (for whitelisted peers)
if (!fPreferNewConnection)
return false;

// Disconnect the most recent connection from the CNetAddr with the most connections
std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeTimeConnected);
Expand Down Expand Up @@ -1028,7 +1031,7 @@ static void AcceptConnection(const ListenSocket& hListenSocket) {
}

if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS) {
if (!AttemptToEvictConnection()) {
if (!AttemptToEvictConnection(whitelisted)) {
// No connection to evict, disconnect the new connection
LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
CloseSocket(hSocket);
Expand Down

0 comments on commit 0ca7ce3

Please sign in to comment.