Skip to content

Commit a49f3dd

Browse files
committed
p2p: allow CAddrMan::GetAddr() by network, add doxygen
1 parent c38981e commit a49f3dd

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

src/addrman.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ friend class CAddrManTest;
287287
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
288288
* @param[in] network Select only addresses of this network (nullopt = all).
289289
*/
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);
290+
void GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) EXCLUSIVE_LOCKS_REQUIRED(cs);
291291

292292
/** We have successfully connected to this peer. Calling this function
293293
* updates the CAddress's nTime, which is used in our IsTerrible()
@@ -723,14 +723,20 @@ friend class CAddrManTest;
723723
return addrRet;
724724
}
725725

726-
//! Return a bunch of addresses, selected at random.
727-
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct)
726+
/**
727+
* Return all or many randomly selected addresses, optionally by network.
728+
*
729+
* @param[in] max_addresses Maximum number of addresses to return (0 = all).
730+
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
731+
* @param[in] network Select only addresses of this network (nullopt = all).
732+
*/
733+
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network)
728734
{
729735
Check();
730736
std::vector<CAddress> vAddr;
731737
{
732738
LOCK(cs);
733-
GetAddr_(vAddr, max_addresses, max_pct);
739+
GetAddr_(vAddr, max_addresses, max_pct, network);
734740
}
735741
Check();
736742
return vAddr;

src/bench/addrman.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <random.h>
88
#include <util/time.h>
99

10+
#include <optional>
1011
#include <vector>
1112

1213
/* A "source" is a source address from which we have received a bunch of other addresses. */
@@ -98,7 +99,7 @@ static void AddrManGetAddr(benchmark::Bench& bench)
9899
FillAddrMan(addrman);
99100

100101
bench.run([&] {
101-
const auto& addresses = addrman.GetAddr(2500, 23);
102+
const auto& addresses = addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23, /* network */ std::nullopt);
102103
assert(addresses.size() > 0);
103104
});
104105
}

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2671,7 +2671,7 @@ CConnman::~CConnman()
26712671

26722672
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct) const
26732673
{
2674-
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct);
2674+
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct, /* network */ std::nullopt);
26752675
if (m_banman) {
26762676
addresses.erase(std::remove_if(addresses.begin(), addresses.end(),
26772677
[this](const CAddress& addr){return m_banman->IsDiscouraged(addr) || m_banman->IsBanned(addr);}),

src/test/addrman_tests.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <boost/test/unit_test.hpp>
1414

15+
#include <optional>
1516
#include <string>
1617

1718
class CAddrManTest : public CAddrMan
@@ -392,7 +393,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
392393
// Test: Sanity check, GetAddr should never return anything if addrman
393394
// is empty.
394395
BOOST_CHECK_EQUAL(addrman.size(), 0U);
395-
std::vector<CAddress> vAddr1 = addrman.GetAddr(/* max_addresses */ 0, /* max_pct */0);
396+
std::vector<CAddress> vAddr1 = addrman.GetAddr(/* max_addresses */ 0, /* max_pct */ 0, /* network */ std::nullopt);
396397
BOOST_CHECK_EQUAL(vAddr1.size(), 0U);
397398

398399
CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE);
@@ -415,15 +416,15 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
415416
BOOST_CHECK(addrman.Add(addr4, source2));
416417
BOOST_CHECK(addrman.Add(addr5, source1));
417418

418-
BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 0, /* max_pct */ 0).size(), 5U);
419+
BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 0, /* max_pct */ 0, /* network */ std::nullopt).size(), 5U);
419420
// Net processing asks for 23% of addresses. 23% of 5 is 1 rounded down.
420-
BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23).size(), 1U);
421+
BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23, /* network */ std::nullopt).size(), 1U);
421422

422423
// Test: Ensure GetAddr works with new and tried addresses.
423424
addrman.Good(CAddress(addr1, NODE_NONE));
424425
addrman.Good(CAddress(addr2, NODE_NONE));
425-
BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 0, /* max_pct */ 0).size(), 5U);
426-
BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23).size(), 1U);
426+
BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 0, /* max_pct */ 0, /* network */ std::nullopt).size(), 5U);
427+
BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23, /* network */ std::nullopt).size(), 1U);
427428

428429
// Test: Ensure GetAddr still returns 23% when addrman has many addrs.
429430
for (unsigned int i = 1; i < (8 * 256); i++) {
@@ -438,7 +439,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
438439
if (i % 8 == 0)
439440
addrman.Good(addr);
440441
}
441-
std::vector<CAddress> vAddr = addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23);
442+
std::vector<CAddress> vAddr = addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23, /* network */ std::nullopt);
442443

443444
size_t percent23 = (addrman.size() * 23) / 100;
444445
BOOST_CHECK_EQUAL(vAddr.size(), percent23);

src/test/fuzz/addrman.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ FUZZ_TARGET_INIT(addrman, initialize_addrman)
6060
(void)addr_man.Select(fuzzed_data_provider.ConsumeBool());
6161
},
6262
[&] {
63-
(void)addr_man.GetAddr(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
63+
(void)addr_man.GetAddr(
64+
/* max_addresses */ fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096),
65+
/* max_pct */ fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096),
66+
/* network */ std::nullopt);
6467
},
6568
[&] {
6669
const std::optional<CAddress> opt_address = ConsumeDeserializable<CAddress>(fuzzed_data_provider);

0 commit comments

Comments
 (0)