p2p: add CompareNodeNetworkTime() comparator struct

to compare and sort peer eviction candidates by the
passed-in is_local (localhost status) and network
arguments, and by longest uptime.
This commit is contained in:
Jon Atack 2021-05-17 11:09:08 +02:00
parent 4ee7aec47e
commit 38a81a8e20
No known key found for this signature in database
GPG Key ID: 4F5721B3D0E3921D

View File

@ -883,6 +883,26 @@ static bool CompareNodeBlockRelayOnlyTime(const NodeEvictionCandidate &a, const
return a.nTimeConnected > b.nTimeConnected; return a.nTimeConnected > b.nTimeConnected;
} }
/**
* Sort eviction candidates by network/localhost and connection uptime.
* Candidates near the beginning are more likely to be evicted, and those
* near the end are more likely to be protected, e.g. less likely to be evicted.
* - First, nodes that are not `is_local` and that do not belong to `network`,
* sorted by increasing uptime (from most recently connected to connected longer).
* - Then, nodes that are `is_local` or belong to `network`, sorted by increasing uptime.
*/
struct CompareNodeNetworkTime {
const bool m_is_local;
const Network m_network;
CompareNodeNetworkTime(bool is_local, Network network) : m_is_local(is_local), m_network(network) {}
bool operator()(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b) const
{
if (m_is_local && a.m_is_local != b.m_is_local) return b.m_is_local;
if ((a.m_network == m_network) != (b.m_network == m_network)) return b.m_network == m_network;
return a.nTimeConnected > b.nTimeConnected;
};
};
//! Sort an array by the specified comparator, then erase the last K elements where predicate is true. //! Sort an array by the specified comparator, then erase the last K elements where predicate is true.
template <typename T, typename Comparator> template <typename T, typename Comparator>
static void EraseLastKElements( static void EraseLastKElements(