diff --git a/src/addrman.h b/src/addrman.h index dd863aa9ac1aa..251260e248bcd 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -291,7 +291,7 @@ friend class CAddrManTest; * @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, Optional network = nullopt) EXCLUSIVE_LOCKS_REQUIRED(cs); + void GetAddr_(std::vector& vAddr, size_t max_addresses, size_t max_pct, Optional network) EXCLUSIVE_LOCKS_REQUIRED(cs); //! Mark an entry as currently-connected-to. void Connected_(const CService& addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs); @@ -704,14 +704,20 @@ friend class CAddrManTest; return addrRet; } - //! Return a bunch of addresses, selected at random. - std::vector GetAddr(size_t max_addresses, size_t max_pct) + /** + * Return all or many randomly selected addresses, optionally by network. + * + * @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). + */ + std::vector GetAddr(size_t max_addresses, size_t max_pct, Optional network) { Check(); std::vector vAddr; { LOCK(cs); - GetAddr_(vAddr, max_addresses, max_pct); + GetAddr_(vAddr, max_addresses, max_pct, network); } Check(); return vAddr; diff --git a/src/net.cpp b/src/net.cpp index bc3bc809961be..09d0e72ef1384 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -18,6 +18,7 @@ #include "guiinterface.h" #include "netbase.h" #include "netmessagemaker.h" +#include "optional.h" #include "primitives/transaction.h" #include "scheduler.h" #include "validation.h" @@ -2144,7 +2145,7 @@ bool CConnman::AddNewAddresses(const std::vector& vAddr, const CAddres std::vector CConnman::GetAddresses(size_t max_addresses, size_t max_pct) { - return addrman.GetAddr(max_addresses, max_pct); + return addrman.GetAddr(max_addresses, max_pct, /* network */ nullopt); } bool CConnman::AddNode(const std::string& strNode) diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index 56badacc29bab..aa520473ed353 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -2,18 +2,22 @@ // Copyright (c) 2019 The PIVX developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "addrman.h" + #include "test/test_pivx.h" -#include -#include -#include // for ReadLE64 -#include "util/asmap.h" #include "test/data/asmap.raw.h" +#include "addrman.h" +#include "crypto/common.h" // for ReadLE64 #include "hash.h" #include "netbase.h" +#include "optional.h" +#include "util/asmap.h" #include "random.h" +#include + +#include + class CAddrManTest : public CAddrMan { private: @@ -385,7 +389,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr) // Test: Sanity check, GetAddr should never return anything if addrman // is empty. BOOST_CHECK(addrman.size() == 0); - std::vector vAddr1 = addrman.GetAddr(/* max_addresses */ 0, /* max_pct */0); + std::vector vAddr1 = addrman.GetAddr(/* max_addresses */ 0, /* max_pct */0, /* network */ nullopt); BOOST_CHECK(vAddr1.size() == 0); CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE); @@ -408,15 +412,15 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr) addrman.Add(addr4, source2); addrman.Add(addr5, source1); - BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 0, /* max_pct */ 0).size(), 5U); + BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 0, /* max_pct */ 0, /* network */ nullopt).size(), 5U); // Net processing asks for 23% of addresses. 23% of 5 is 1 rounded down. - BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23).size(), 1U); + BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23, /* network */ nullopt).size(), 1U); // Test 24: Ensure GetAddr works with new and tried addresses. addrman.Good(CAddress(addr1, NODE_NONE)); addrman.Good(CAddress(addr2, NODE_NONE)); - BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 0, /* max_pct */ 0).size(), 5U); - BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23).size(), 1U); + BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 0, /* max_pct */ 0, /* network */ nullopt).size(), 5U); + BOOST_CHECK_EQUAL(addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23, /* network */ nullopt).size(), 1U); // Test 25: Ensure GetAddr still returns 23% when addrman has many addrs. for (unsigned int i = 1; i < (8 * 256); i++) { @@ -432,7 +436,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr) if (i % 8 == 0) addrman.Good(addr); } - std::vector vAddr = addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23); + std::vector vAddr = addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23, /* network */ nullopt); size_t percent23 = (addrman.size() * 23) / 100; BOOST_CHECK(vAddr.size() == percent23);