From d35ddca91ebbcf8d8b790c3b9f8cf218fafb7a53 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 2 May 2021 18:44:17 +0200 Subject: p2p: enable CAddrMan::GetAddr_() by network, add doxygen --- src/addrman.cpp | 14 +++++++++++--- src/addrman.h | 12 ++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/addrman.cpp b/src/addrman.cpp index f91121f156..ae2f1c23a2 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -7,9 +7,11 @@ #include #include +#include #include #include +#include int CAddrInfo::GetTriedBucket(const uint256& nKey, const std::vector &asmap) const { @@ -481,7 +483,7 @@ int CAddrMan::Check_() } #endif -void CAddrMan::GetAddr_(std::vector& vAddr, size_t max_addresses, size_t max_pct) +void CAddrMan::GetAddr_(std::vector& vAddr, size_t max_addresses, size_t max_pct, std::optional network) { size_t nNodes = vRandom.size(); if (max_pct != 0) { @@ -501,8 +503,14 @@ void CAddrMan::GetAddr_(std::vector& vAddr, size_t max_addresses, size assert(mapInfo.count(vRandom[n]) == 1); const CAddrInfo& ai = mapInfo[vRandom[n]]; - if (!ai.IsTerrible()) - vAddr.push_back(ai); + + // Filter by network (optional) + if (network != std::nullopt && ai.GetNetClass() != network) continue; + + // Filter for quality + if (ai.IsTerrible()) continue; + + vAddr.push_back(ai); } } diff --git a/src/addrman.h b/src/addrman.h index 92a5570953..cf059478a5 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -278,8 +279,15 @@ protected: int Check_() EXCLUSIVE_LOCKS_REQUIRED(cs); #endif - //! Select several addresses at once. - void GetAddr_(std::vector &vAddr, size_t max_addresses, size_t max_pct) EXCLUSIVE_LOCKS_REQUIRED(cs); + /** + * Return all or many randomly selected addresses, optionally by network. + * + * @param[out] vAddr Vector of randomly selected addresses from vRandom. + * @param[in] max_addresses Maximum number of addresses to return (0 = all). + * @param[in] max_pct Maximum percentage of addresses to return (0 = all). + * @param[in] network Select only addresses of this network (nullopt = all). + */ + void GetAddr_(std::vector& vAddr, size_t max_addresses, size_t max_pct, std::optional network = std::nullopt) EXCLUSIVE_LOCKS_REQUIRED(cs); /** We have successfully connected to this peer. Calling this function * updates the CAddress's nTime, which is used in our IsTerrible() -- cgit v1.2.3