aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGregory Maxwell <greg@xiph.org>2015-11-23 03:48:54 +0000
committerMarcoFalke <falke.marco@gmail.com>2016-02-01 15:31:21 +0100
commit1e05727072a58d3538dc654c5a3de83ed58874b8 (patch)
tree9ae090fbc4635e029786b2ee98564121bea4f610 /src
parent1e9613ac090ee82f52e1d02a622358b2a1085249 (diff)
downloadbitcoin-1e05727072a58d3538dc654c5a3de83ed58874b8.tar.xz
Decide eviction group ties based on time.
This corrects a bug the case of tying group size where the code may fail to select the group with the newest member. Since newest time is the final selection criteria, failing to break ties on it on the step before can undermine the final selection. Tied netgroups are very common. (cherry picked from commit 8e09f914f8ec66301257358b250e9a61befadd95)
Diffstat (limited to 'src')
-rw-r--r--src/net.cpp16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 84c5644ccc..14e22f6cb3 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -929,15 +929,20 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) {
if (vEvictionCandidates.empty()) return false;
- // Identify the network group with the most connections
+ // Identify the network group with the most connections and youngest member.
+ // (vEvictionCandidates is already sorted by reverse connect time)
std::vector<unsigned char> naMostConnections;
unsigned int nMostConnections = 0;
+ int64_t nMostConnectionsTime = 0;
std::map<std::vector<unsigned char>, std::vector<CNodeRef> > mapAddrCounts;
BOOST_FOREACH(const CNodeRef &node, vEvictionCandidates) {
mapAddrCounts[node->addr.GetGroup()].push_back(node);
+ int64_t grouptime = mapAddrCounts[node->addr.GetGroup()][0]->nTimeConnected;
+ size_t groupsize = mapAddrCounts[node->addr.GetGroup()].size();
- if (mapAddrCounts[node->addr.GetGroup()].size() > nMostConnections) {
- nMostConnections = mapAddrCounts[node->addr.GetGroup()].size();
+ if (groupsize > nMostConnections || (groupsize == nMostConnections && grouptime > nMostConnectionsTime)) {
+ nMostConnections = groupsize;
+ nMostConnectionsTime = grouptime;
naMostConnections = node->addr.GetGroup();
}
}
@@ -945,14 +950,13 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) {
// Reduce to the network group with the most connections
vEvictionCandidates = mapAddrCounts[naMostConnections];
- // Do not disconnect peers if there is only 1 connection from their network group
+ // Do not disconnect peers if there is only one unprotected connection from their network group.
if (vEvictionCandidates.size() <= 1)
// unless we prefer the new connection (for whitelisted peers)
if (!fPreferNewConnection)
return false;
- // Disconnect the most recent connection from the network group with the most connections
- std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeTimeConnected);
+ // Disconnect from the network group with the most connections
vEvictionCandidates[0]->fDisconnect = true;
return true;