Skip to content

Commit

Permalink
p2p: allow CAddrMan::GetAddr() by network, add doxygen
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzbawls committed Aug 12, 2021
1 parent a4d2733 commit fa78233
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
14 changes: 10 additions & 4 deletions src/addrman.h
Expand Up @@ -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<CAddress>& vAddr, size_t max_addresses, size_t max_pct, Optional<Network> network = nullopt) EXCLUSIVE_LOCKS_REQUIRED(cs);
void GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, Optional<Network> network) EXCLUSIVE_LOCKS_REQUIRED(cs);

//! Mark an entry as currently-connected-to.
void Connected_(const CService& addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
Expand Down Expand Up @@ -704,14 +704,20 @@ friend class CAddrManTest;
return addrRet;
}

//! Return a bunch of addresses, selected at random.
std::vector<CAddress> 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<CAddress> GetAddr(size_t max_addresses, size_t max_pct, Optional<Network> network)
{
Check();
std::vector<CAddress> vAddr;
{
LOCK(cs);
GetAddr_(vAddr, max_addresses, max_pct);
GetAddr_(vAddr, max_addresses, max_pct, network);
}
Check();
return vAddr;
Expand Down
3 changes: 2 additions & 1 deletion src/net.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -2144,7 +2145,7 @@ bool CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddres

std::vector<CAddress> 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)
Expand Down
26 changes: 15 additions & 11 deletions src/test/addrman_tests.cpp
Expand Up @@ -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 <string>
#include <boost/test/unit_test.hpp>
#include <crypto/common.h> // 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 <string>

#include <boost/test/unit_test.hpp>

class CAddrManTest : public CAddrMan
{
private:
Expand Down Expand Up @@ -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<CAddress> vAddr1 = addrman.GetAddr(/* max_addresses */ 0, /* max_pct */0);
std::vector<CAddress> 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);
Expand All @@ -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++) {
Expand All @@ -432,7 +436,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
if (i % 8 == 0)
addrman.Good(addr);
}
std::vector<CAddress> vAddr = addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23);
std::vector<CAddress> vAddr = addrman.GetAddr(/* max_addresses */ 2500, /* max_pct */ 23, /* network */ nullopt);

size_t percent23 = (addrman.size() * 23) / 100;
BOOST_CHECK(vAddr.size() == percent23);
Expand Down

0 comments on commit fa78233

Please sign in to comment.