Skip to content

Commit d35ddca

Browse files
committed
p2p: enable CAddrMan::GetAddr_() by network, add doxygen
1 parent 4da26fb commit d35ddca

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/addrman.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
#include <hash.h>
99
#include <logging.h>
10+
#include <netaddress.h>
1011
#include <serialize.h>
1112

1213
#include <cmath>
14+
#include <optional>
1315

1416
int CAddrInfo::GetTriedBucket(const uint256& nKey, const std::vector<bool> &asmap) const
1517
{
@@ -481,7 +483,7 @@ int CAddrMan::Check_()
481483
}
482484
#endif
483485

484-
void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct)
486+
void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network)
485487
{
486488
size_t nNodes = vRandom.size();
487489
if (max_pct != 0) {
@@ -501,8 +503,14 @@ void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size
501503
assert(mapInfo.count(vRandom[n]) == 1);
502504

503505
const CAddrInfo& ai = mapInfo[vRandom[n]];
504-
if (!ai.IsTerrible())
505-
vAddr.push_back(ai);
506+
507+
// Filter by network (optional)
508+
if (network != std::nullopt && ai.GetNetClass() != network) continue;
509+
510+
// Filter for quality
511+
if (ai.IsTerrible()) continue;
512+
513+
vAddr.push_back(ai);
506514
}
507515
}
508516

src/addrman.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <hash.h>
2121
#include <iostream>
2222
#include <map>
23+
#include <optional>
2324
#include <set>
2425
#include <stdint.h>
2526
#include <streams.h>
@@ -278,8 +279,15 @@ friend class CAddrManTest;
278279
int Check_() EXCLUSIVE_LOCKS_REQUIRED(cs);
279280
#endif
280281

281-
//! Select several addresses at once.
282-
void GetAddr_(std::vector<CAddress> &vAddr, size_t max_addresses, size_t max_pct) EXCLUSIVE_LOCKS_REQUIRED(cs);
282+
/**
283+
* Return all or many randomly selected addresses, optionally by network.
284+
*
285+
* @param[out] vAddr Vector of randomly selected addresses from vRandom.
286+
* @param[in] max_addresses Maximum number of addresses to return (0 = all).
287+
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
288+
* @param[in] network Select only addresses of this network (nullopt = all).
289+
*/
290+
void GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network = std::nullopt) EXCLUSIVE_LOCKS_REQUIRED(cs);
283291

284292
/** We have successfully connected to this peer. Calling this function
285293
* updates the CAddress's nTime, which is used in our IsTerrible()

0 commit comments

Comments
 (0)