diff options
author | Jon Atack <jon@atack.com> | 2021-05-17 11:09:08 +0200 |
---|---|---|
committer | Jon Atack <jon@atack.com> | 2021-06-13 20:15:49 +0200 |
commit | 38a81a8e20b0e5ad9fef0eae8abd914619f05b25 (patch) | |
tree | 95e3fa63555f284ba5c035561019df4c48c44e9e | |
parent | 4ee7aec47ebf6b59b4d930e6e4025e91352c05b4 (diff) | |
download | bitcoin-38a81a8e20b0e5ad9fef0eae8abd914619f05b25.tar.xz |
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.
-rw-r--r-- | src/net.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/net.cpp b/src/net.cpp index 71e7754ad4..9318d25359 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -883,6 +883,26 @@ static bool CompareNodeBlockRelayOnlyTime(const NodeEvictionCandidate &a, const 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. template <typename T, typename Comparator> static void EraseLastKElements( |