Merge #7749: Enforce expected outbound services

ecd7fd3 Introduce REQUIRED_SERVICES constant (Pieter Wuille)
ee06e04 Introduce enum ServiceFlags for service flags (Pieter Wuille)
15bf863 Don't require services in -addnode (Pieter Wuille)
5e7ab16 Only store and connect to NODE_NETWORK nodes (Pieter Wuille)
fc83f18 Verify that outbound connections have expected services (Pieter Wuille)
3764dec Keep addrman's nService bits consistent with outbound observations (Pieter Wuille)
This commit is contained in:
Wladimir J. van der Laan 2016-06-13 19:30:04 +02:00
commit be9711e597
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
11 changed files with 142 additions and 78 deletions

View File

@ -263,7 +263,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
pinfo->nTime = std::max((int64_t)0, addr.nTime - nTimePenalty); pinfo->nTime = std::max((int64_t)0, addr.nTime - nTimePenalty);
// add services // add services
pinfo->nServices |= addr.nServices; pinfo->nServices = ServiceFlags(pinfo->nServices | addr.nServices);
// do not update if no new information is present // do not update if no new information is present
if (!addr.nTime || (pinfo->nTime && addr.nTime <= pinfo->nTime)) if (!addr.nTime || (pinfo->nTime && addr.nTime <= pinfo->nTime))
@ -502,6 +502,24 @@ void CAddrMan::Connected_(const CService& addr, int64_t nTime)
info.nTime = nTime; info.nTime = nTime;
} }
void CAddrMan::SetServices_(const CService& addr, ServiceFlags nServices)
{
CAddrInfo* pinfo = Find(addr);
// if not found, bail out
if (!pinfo)
return;
CAddrInfo& info = *pinfo;
// check whether we are talking about the exact same CService (including same port)
if (info != addr)
return;
// update info
info.nServices = nServices;
}
int CAddrMan::RandomInt(int nMax){ int CAddrMan::RandomInt(int nMax){
return GetRandInt(nMax); return GetRandInt(nMax);
} }

View File

@ -256,6 +256,9 @@ protected:
//! Mark an entry as currently-connected-to. //! Mark an entry as currently-connected-to.
void Connected_(const CService &addr, int64_t nTime); void Connected_(const CService &addr, int64_t nTime);
//! Update an entry's service bits.
void SetServices_(const CService &addr, ServiceFlags nServices);
public: public:
/** /**
* serialized format: * serialized format:
@ -589,6 +592,14 @@ public:
} }
} }
void SetServices(const CService &addr, ServiceFlags nServices)
{
LOCK(cs);
Check();
SetServices_(addr, nServices);
Check();
}
}; };
#endif // BITCOIN_ADDRMAN_H #endif // BITCOIN_ADDRMAN_H

View File

@ -950,7 +950,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
SetMockTime(GetArg("-mocktime", 0)); // SetMockTime(0) is a no-op SetMockTime(GetArg("-mocktime", 0)); // SetMockTime(0) is a no-op
if (GetBoolArg("-peerbloomfilters", DEFAULT_PEERBLOOMFILTERS)) if (GetBoolArg("-peerbloomfilters", DEFAULT_PEERBLOOMFILTERS))
nLocalServices |= NODE_BLOOM; nLocalServices = ServiceFlags(nLocalServices | NODE_BLOOM);
nMaxTipAge = GetArg("-maxtipage", DEFAULT_MAX_TIP_AGE); nMaxTipAge = GetArg("-maxtipage", DEFAULT_MAX_TIP_AGE);
@ -1361,7 +1361,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
// after any wallet rescanning has taken place. // after any wallet rescanning has taken place.
if (fPruneMode) { if (fPruneMode) {
LogPrintf("Unsetting NODE_NETWORK on prune mode\n"); LogPrintf("Unsetting NODE_NETWORK on prune mode\n");
nLocalServices &= ~NODE_NETWORK; nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK);
if (!fReindex) { if (!fReindex) {
uiInterface.InitMessage(_("Pruning blockstore...")); uiInterface.InitMessage(_("Pruning blockstore..."));
PruneAndFlush(); PruneAndFlush();

View File

@ -4611,7 +4611,22 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
CAddress addrMe; CAddress addrMe;
CAddress addrFrom; CAddress addrFrom;
uint64_t nNonce = 1; uint64_t nNonce = 1;
vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe; uint64_t nServiceInt;
vRecv >> pfrom->nVersion >> nServiceInt >> nTime >> addrMe;
pfrom->nServices = ServiceFlags(nServiceInt);
if (!pfrom->fInbound)
{
addrman.SetServices(pfrom->addr, pfrom->nServices);
}
if (pfrom->nServicesExpected & ~pfrom->nServices)
{
LogPrint("net", "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->id, pfrom->nServices, pfrom->nServicesExpected);
pfrom->PushMessage(NetMsgType::REJECT, strCommand, REJECT_NONSTANDARD,
strprintf("Expected to offer services %08x", pfrom->nServicesExpected));
pfrom->fDisconnect = true;
return false;
}
if (pfrom->nVersion < MIN_PEER_PROTO_VERSION) if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
{ {
// disconnect from peers older than this proto version // disconnect from peers older than this proto version
@ -4772,6 +4787,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
{ {
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
continue;
if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60) if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
addr.nTime = nNow - 5 * 24 * 60 * 60; addr.nTime = nNow - 5 * 24 * 60 * 60;
pfrom->AddAddressKnown(addr); pfrom->AddAddressKnown(addr);

View File

@ -71,12 +71,15 @@ namespace {
const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*"; const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
/** Services this node implementation cares about */
static const ServiceFlags nRelevantServices = NODE_NETWORK;
// //
// Global state variables // Global state variables
// //
bool fDiscover = true; bool fDiscover = true;
bool fListen = true; bool fListen = true;
uint64_t nLocalServices = NODE_NETWORK; ServiceFlags nLocalServices = NODE_NETWORK;
bool fRelayTxes = true; bool fRelayTxes = true;
CCriticalSection cs_mapLocalHost; CCriticalSection cs_mapLocalHost;
std::map<CNetAddr, LocalServiceInfo> mapLocalHost; std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
@ -159,7 +162,7 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
{ {
struct in6_addr ip; struct in6_addr ip;
memcpy(&ip, i->addr, sizeof(ip)); memcpy(&ip, i->addr, sizeof(ip));
CAddress addr(CService(ip, i->port)); CAddress addr(CService(ip, i->port), NODE_NETWORK);
addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek; addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
vSeedsOut.push_back(addr); vSeedsOut.push_back(addr);
} }
@ -172,13 +175,12 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
// one by discovery. // one by discovery.
CAddress GetLocalAddress(const CNetAddr *paddrPeer) CAddress GetLocalAddress(const CNetAddr *paddrPeer)
{ {
CAddress ret(CService("0.0.0.0",GetListenPort()),0); CAddress ret(CService("0.0.0.0",GetListenPort()), NODE_NONE);
CService addr; CService addr;
if (GetLocal(addr, paddrPeer)) if (GetLocal(addr, paddrPeer))
{ {
ret = CAddress(addr); ret = CAddress(addr, nLocalServices);
} }
ret.nServices = nLocalServices;
ret.nTime = GetAdjustedTime(); ret.nTime = GetAdjustedTime();
return ret; return ret;
} }
@ -409,6 +411,7 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure
vNodes.push_back(pnode); vNodes.push_back(pnode);
} }
pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices);
pnode->nTimeConnected = GetTime(); pnode->nTimeConnected = GetTime();
return pnode; return pnode;
@ -461,14 +464,14 @@ void CNode::PushVersion()
int nBestHeight = GetNodeSignals().GetHeight().get_value_or(0); int nBestHeight = GetNodeSignals().GetHeight().get_value_or(0);
int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime()); int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",0))); CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0", 0), addr.nServices));
CAddress addrMe = GetLocalAddress(&addr); CAddress addrMe = GetLocalAddress(&addr);
GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce)); GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
if (fLogIPs) if (fLogIPs)
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id); LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id);
else else
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id); LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id);
PushMessage(NetMsgType::VERSION, PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe, PushMessage(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalServices, nTime, addrYou, addrMe,
nLocalHostNonce, strSubVersion, nBestHeight, ::fRelayTxes); nLocalHostNonce, strSubVersion, nBestHeight, ::fRelayTxes);
} }
@ -1437,7 +1440,7 @@ void ThreadDNSAddressSeed()
} else { } else {
std::vector<CNetAddr> vIPs; std::vector<CNetAddr> vIPs;
std::vector<CAddress> vAdd; std::vector<CAddress> vAdd;
uint64_t requiredServiceBits = NODE_NETWORK; ServiceFlags requiredServiceBits = nRelevantServices;
if (LookupHost(seed.getHost(requiredServiceBits).c_str(), vIPs, 0, true)) if (LookupHost(seed.getHost(requiredServiceBits).c_str(), vIPs, 0, true))
{ {
BOOST_FOREACH(const CNetAddr& ip, vIPs) BOOST_FOREACH(const CNetAddr& ip, vIPs)
@ -1520,7 +1523,7 @@ void ThreadOpenConnections()
ProcessOneShot(); ProcessOneShot();
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-connect"]) BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-connect"])
{ {
CAddress addr; CAddress addr(CService(), NODE_NONE);
OpenNetworkConnection(addr, false, NULL, strAddr.c_str()); OpenNetworkConnection(addr, false, NULL, strAddr.c_str());
for (int i = 0; i < 10 && i < nLoop; i++) for (int i = 0; i < 10 && i < nLoop; i++)
{ {
@ -1592,6 +1595,10 @@ void ThreadOpenConnections()
if (IsLimited(addr)) if (IsLimited(addr))
continue; continue;
// only connect to full nodes
if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
continue;
// only consider very recently tried nodes after 30 failed attempts // only consider very recently tried nodes after 30 failed attempts
if (nANow - addr.nLastTry < 600 && nTries < 30) if (nANow - addr.nLastTry < 600 && nTries < 30)
continue; continue;
@ -1666,7 +1673,9 @@ void ThreadOpenAddedConnections()
BOOST_FOREACH(std::vector<CService>& vserv, lservAddressesToAdd) BOOST_FOREACH(std::vector<CService>& vserv, lservAddressesToAdd)
{ {
CSemaphoreGrant grant(*semOutbound); CSemaphoreGrant grant(*semOutbound);
OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), false, &grant); /* We want -addnode to work even for nodes that don't provide all
* wanted services, so pass in nServices=NODE_NONE to CAddress. */
OpenNetworkConnection(CAddress(vserv[i % vserv.size()], NODE_NONE), false, &grant);
MilliSleep(500); MilliSleep(500);
} }
MilliSleep(120000); // Retry every 2 minutes MilliSleep(120000); // Retry every 2 minutes
@ -2324,7 +2333,8 @@ CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNa
addrKnown(5000, 0.001), addrKnown(5000, 0.001),
filterInventoryKnown(50000, 0.000001) filterInventoryKnown(50000, 0.000001)
{ {
nServices = 0; nServices = NODE_NONE;
nServicesExpected = NODE_NONE;
hSocket = hSocketIn; hSocket = hSocketIn;
nRecvVersion = INIT_PROTO_VERSION; nRecvVersion = INIT_PROTO_VERSION;
nLastSend = 0; nLastSend = 0;

View File

@ -72,6 +72,8 @@ static const bool DEFAULT_FORCEDNSSEED = false;
static const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000; static const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000;
static const size_t DEFAULT_MAXSENDBUFFER = 1 * 1000; static const size_t DEFAULT_MAXSENDBUFFER = 1 * 1000;
static const ServiceFlags REQUIRED_SERVICES = NODE_NETWORK;
// NOTE: When adjusting this, update rpcnet:setban's help ("24h") // NOTE: When adjusting this, update rpcnet:setban's help ("24h")
static const unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24; // Default 24-hour ban static const unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24; // Default 24-hour ban
@ -152,7 +154,7 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
extern bool fDiscover; extern bool fDiscover;
extern bool fListen; extern bool fListen;
extern uint64_t nLocalServices; extern ServiceFlags nLocalServices;
extern bool fRelayTxes; extern bool fRelayTxes;
extern uint64_t nLocalHostNonce; extern uint64_t nLocalHostNonce;
extern CAddrMan addrman; extern CAddrMan addrman;
@ -186,7 +188,7 @@ class CNodeStats
{ {
public: public:
NodeId nodeid; NodeId nodeid;
uint64_t nServices; ServiceFlags nServices;
bool fRelayTxes; bool fRelayTxes;
int64_t nLastSend; int64_t nLastSend;
int64_t nLastRecv; int64_t nLastRecv;
@ -316,7 +318,8 @@ class CNode
{ {
public: public:
// socket // socket
uint64_t nServices; ServiceFlags nServices;
ServiceFlags nServicesExpected;
SOCKET hSocket; SOCKET hSocket;
CDataStream ssSend; CDataStream ssSend;
size_t nSendSize; // total size of all vSendMsg entries size_t nSendSize; // total size of all vSendMsg entries

View File

@ -133,7 +133,7 @@ CAddress::CAddress() : CService()
Init(); Init();
} }
CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn) CAddress::CAddress(CService ipIn, ServiceFlags nServicesIn) : CService(ipIn)
{ {
Init(); Init();
nServices = nServicesIn; nServices = nServicesIn;
@ -141,7 +141,7 @@ CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
void CAddress::Init() void CAddress::Init()
{ {
nServices = NODE_NETWORK; nServices = NODE_NONE;
nTime = 100000000; nTime = 100000000;
} }

View File

@ -223,7 +223,9 @@ extern const char *FEEFILTER;
const std::vector<std::string> &getAllNetMessageTypes(); const std::vector<std::string> &getAllNetMessageTypes();
/** nServices flags */ /** nServices flags */
enum { enum ServiceFlags : uint64_t {
// Nothing
NODE_NONE = 0,
// NODE_NETWORK means that the node is capable of serving the block chain. It is currently // NODE_NETWORK means that the node is capable of serving the block chain. It is currently
// set by all Bitcoin Core nodes, and is unset by SPV clients or other peers that just want // set by all Bitcoin Core nodes, and is unset by SPV clients or other peers that just want
// network services but don't provide them. // network services but don't provide them.
@ -251,7 +253,7 @@ class CAddress : public CService
{ {
public: public:
CAddress(); CAddress();
explicit CAddress(CService ipIn, uint64_t nServicesIn = NODE_NETWORK); explicit CAddress(CService ipIn, ServiceFlags nServicesIn);
void Init(); void Init();
@ -267,13 +269,15 @@ public:
if ((nType & SER_DISK) || if ((nType & SER_DISK) ||
(nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH))) (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
READWRITE(nTime); READWRITE(nTime);
READWRITE(nServices); uint64_t nServicesInt = nServices;
READWRITE(nServicesInt);
nServices = (ServiceFlags)nServicesInt;
READWRITE(*(CService*)this); READWRITE(*(CService*)this);
} }
// TODO: make private (improves encapsulation) // TODO: make private (improves encapsulation)
public: public:
uint64_t nServices; ServiceFlags nServices;
// disk and network only // disk and network only
unsigned int nTime; unsigned int nTime;

View File

@ -45,7 +45,7 @@ BOOST_FIXTURE_TEST_SUITE(DoS_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(DoS_banning) BOOST_AUTO_TEST_CASE(DoS_banning)
{ {
CNode::ClearBanned(); CNode::ClearBanned();
CAddress addr1(ip(0xa0b0c001)); CAddress addr1(ip(0xa0b0c001), NODE_NONE);
CNode dummyNode1(INVALID_SOCKET, addr1, "", true); CNode dummyNode1(INVALID_SOCKET, addr1, "", true);
dummyNode1.nVersion = 1; dummyNode1.nVersion = 1;
Misbehaving(dummyNode1.GetId(), 100); // Should get banned Misbehaving(dummyNode1.GetId(), 100); // Should get banned
@ -53,7 +53,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
BOOST_CHECK(CNode::IsBanned(addr1)); BOOST_CHECK(CNode::IsBanned(addr1));
BOOST_CHECK(!CNode::IsBanned(ip(0xa0b0c001|0x0000ff00))); // Different IP, not banned BOOST_CHECK(!CNode::IsBanned(ip(0xa0b0c001|0x0000ff00))); // Different IP, not banned
CAddress addr2(ip(0xa0b0c002)); CAddress addr2(ip(0xa0b0c002), NODE_NONE);
CNode dummyNode2(INVALID_SOCKET, addr2, "", true); CNode dummyNode2(INVALID_SOCKET, addr2, "", true);
dummyNode2.nVersion = 1; dummyNode2.nVersion = 1;
Misbehaving(dummyNode2.GetId(), 50); Misbehaving(dummyNode2.GetId(), 50);
@ -69,7 +69,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
{ {
CNode::ClearBanned(); CNode::ClearBanned();
mapArgs["-banscore"] = "111"; // because 11 is my favorite number mapArgs["-banscore"] = "111"; // because 11 is my favorite number
CAddress addr1(ip(0xa0b0c001)); CAddress addr1(ip(0xa0b0c001), NODE_NONE);
CNode dummyNode1(INVALID_SOCKET, addr1, "", true); CNode dummyNode1(INVALID_SOCKET, addr1, "", true);
dummyNode1.nVersion = 1; dummyNode1.nVersion = 1;
Misbehaving(dummyNode1.GetId(), 100); Misbehaving(dummyNode1.GetId(), 100);
@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
int64_t nStartTime = GetTime(); int64_t nStartTime = GetTime();
SetMockTime(nStartTime); // Overrides future calls to GetTime() SetMockTime(nStartTime); // Overrides future calls to GetTime()
CAddress addr(ip(0xa0b0c001)); CAddress addr(ip(0xa0b0c001), NODE_NONE);
CNode dummyNode(INVALID_SOCKET, addr, "", true); CNode dummyNode(INVALID_SOCKET, addr, "", true);
dummyNode.nVersion = 1; dummyNode.nVersion = 1;

View File

@ -68,7 +68,7 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
// Test 2: Does Addrman::Add work as expected. // Test 2: Does Addrman::Add work as expected.
CService addr1 = CService("250.1.1.1", 8333); CService addr1 = CService("250.1.1.1", 8333);
addrman.Add(CAddress(addr1), source); addrman.Add(CAddress(addr1, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
CAddrInfo addr_ret1 = addrman.Select(); CAddrInfo addr_ret1 = addrman.Select();
BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333"); BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333");
@ -76,14 +76,14 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
// Test 3: Does IP address deduplication work correctly. // Test 3: Does IP address deduplication work correctly.
// Expected dup IP should not be added. // Expected dup IP should not be added.
CService addr1_dup = CService("250.1.1.1", 8333); CService addr1_dup = CService("250.1.1.1", 8333);
addrman.Add(CAddress(addr1_dup), source); addrman.Add(CAddress(addr1_dup, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
// Test 5: New table has one addr and we add a diff addr we should // Test 5: New table has one addr and we add a diff addr we should
// have two addrs. // have two addrs.
CService addr2 = CService("250.1.1.2", 8333); CService addr2 = CService("250.1.1.2", 8333);
addrman.Add(CAddress(addr2), source); addrman.Add(CAddress(addr2, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 2); BOOST_CHECK(addrman.size() == 2);
// Test 6: AddrMan::Clear() should empty the new table. // Test 6: AddrMan::Clear() should empty the new table.
@ -106,18 +106,18 @@ BOOST_AUTO_TEST_CASE(addrman_ports)
// Test 7; Addr with same IP but diff port does not replace existing addr. // Test 7; Addr with same IP but diff port does not replace existing addr.
CService addr1 = CService("250.1.1.1", 8333); CService addr1 = CService("250.1.1.1", 8333);
addrman.Add(CAddress(addr1), source); addrman.Add(CAddress(addr1, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
CService addr1_port = CService("250.1.1.1", 8334); CService addr1_port = CService("250.1.1.1", 8334);
addrman.Add(CAddress(addr1_port), source); addrman.Add(CAddress(addr1_port, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
CAddrInfo addr_ret2 = addrman.Select(); CAddrInfo addr_ret2 = addrman.Select();
BOOST_CHECK(addr_ret2.ToString() == "250.1.1.1:8333"); BOOST_CHECK(addr_ret2.ToString() == "250.1.1.1:8333");
// Test 8: Add same IP but diff port to tried table, it doesn't get added. // Test 8: Add same IP but diff port to tried table, it doesn't get added.
// Perhaps this is not ideal behavior but it is the current behavior. // Perhaps this is not ideal behavior but it is the current behavior.
addrman.Good(CAddress(addr1_port)); addrman.Good(CAddress(addr1_port, NODE_NONE));
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
bool newOnly = true; bool newOnly = true;
CAddrInfo addr_ret3 = addrman.Select(newOnly); CAddrInfo addr_ret3 = addrman.Select(newOnly);
@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(addrman_select)
// Test 9: Select from new with 1 addr in new. // Test 9: Select from new with 1 addr in new.
CService addr1 = CService("250.1.1.1", 8333); CService addr1 = CService("250.1.1.1", 8333);
addrman.Add(CAddress(addr1), source); addrman.Add(CAddress(addr1, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
bool newOnly = true; bool newOnly = true;
@ -144,7 +144,7 @@ BOOST_AUTO_TEST_CASE(addrman_select)
BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333"); BOOST_CHECK(addr_ret1.ToString() == "250.1.1.1:8333");
// Test 10: move addr to tried, select from new expected nothing returned. // Test 10: move addr to tried, select from new expected nothing returned.
addrman.Good(CAddress(addr1)); addrman.Good(CAddress(addr1, NODE_NONE));
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
CAddrInfo addr_ret2 = addrman.Select(newOnly); CAddrInfo addr_ret2 = addrman.Select(newOnly);
BOOST_CHECK(addr_ret2.ToString() == "[::]:0"); BOOST_CHECK(addr_ret2.ToString() == "[::]:0");
@ -160,21 +160,21 @@ BOOST_AUTO_TEST_CASE(addrman_select)
CService addr3 = CService("250.3.2.2", 9999); CService addr3 = CService("250.3.2.2", 9999);
CService addr4 = CService("250.3.3.3", 9999); CService addr4 = CService("250.3.3.3", 9999);
addrman.Add(CAddress(addr2), CService("250.3.1.1", 8333)); addrman.Add(CAddress(addr2, NODE_NONE), CService("250.3.1.1", 8333));
addrman.Add(CAddress(addr3), CService("250.3.1.1", 8333)); addrman.Add(CAddress(addr3, NODE_NONE), CService("250.3.1.1", 8333));
addrman.Add(CAddress(addr4), CService("250.4.1.1", 8333)); addrman.Add(CAddress(addr4, NODE_NONE), CService("250.4.1.1", 8333));
// Add three addresses to tried table. // Add three addresses to tried table.
CService addr5 = CService("250.4.4.4", 8333); CService addr5 = CService("250.4.4.4", 8333);
CService addr6 = CService("250.4.5.5", 7777); CService addr6 = CService("250.4.5.5", 7777);
CService addr7 = CService("250.4.6.6", 8333); CService addr7 = CService("250.4.6.6", 8333);
addrman.Add(CAddress(addr5), CService("250.3.1.1", 8333)); addrman.Add(CAddress(addr5, NODE_NONE), CService("250.3.1.1", 8333));
addrman.Good(CAddress(addr5)); addrman.Good(CAddress(addr5, NODE_NONE));
addrman.Add(CAddress(addr6), CService("250.3.1.1", 8333)); addrman.Add(CAddress(addr6, NODE_NONE), CService("250.3.1.1", 8333));
addrman.Good(CAddress(addr6)); addrman.Good(CAddress(addr6, NODE_NONE));
addrman.Add(CAddress(addr7), CService("250.1.1.3", 8333)); addrman.Add(CAddress(addr7, NODE_NONE), CService("250.1.1.3", 8333));
addrman.Good(CAddress(addr7)); addrman.Good(CAddress(addr7, NODE_NONE));
// Test 11: 6 addrs + 1 addr from last test = 7. // Test 11: 6 addrs + 1 addr from last test = 7.
BOOST_CHECK(addrman.size() == 7); BOOST_CHECK(addrman.size() == 7);
@ -199,7 +199,7 @@ BOOST_AUTO_TEST_CASE(addrman_new_collisions)
for (unsigned int i = 1; i < 18; i++) { for (unsigned int i = 1; i < 18; i++) {
CService addr = CService("250.1.1." + boost::to_string(i)); CService addr = CService("250.1.1." + boost::to_string(i));
addrman.Add(CAddress(addr), source); addrman.Add(CAddress(addr, NODE_NONE), source);
//Test 13: No collision in new table yet. //Test 13: No collision in new table yet.
BOOST_CHECK(addrman.size() == i); BOOST_CHECK(addrman.size() == i);
@ -207,11 +207,11 @@ BOOST_AUTO_TEST_CASE(addrman_new_collisions)
//Test 14: new table collision! //Test 14: new table collision!
CService addr1 = CService("250.1.1.18"); CService addr1 = CService("250.1.1.18");
addrman.Add(CAddress(addr1), source); addrman.Add(CAddress(addr1, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 17); BOOST_CHECK(addrman.size() == 17);
CService addr2 = CService("250.1.1.19"); CService addr2 = CService("250.1.1.19");
addrman.Add(CAddress(addr2), source); addrman.Add(CAddress(addr2, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 18); BOOST_CHECK(addrman.size() == 18);
} }
@ -228,8 +228,8 @@ BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
for (unsigned int i = 1; i < 80; i++) { for (unsigned int i = 1; i < 80; i++) {
CService addr = CService("250.1.1." + boost::to_string(i)); CService addr = CService("250.1.1." + boost::to_string(i));
addrman.Add(CAddress(addr), source); addrman.Add(CAddress(addr, NODE_NONE), source);
addrman.Good(CAddress(addr)); addrman.Good(CAddress(addr, NODE_NONE));
//Test 15: No collision in tried table yet. //Test 15: No collision in tried table yet.
BOOST_TEST_MESSAGE(addrman.size()); BOOST_TEST_MESSAGE(addrman.size());
@ -238,11 +238,11 @@ BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
//Test 16: tried table collision! //Test 16: tried table collision!
CService addr1 = CService("250.1.1.80"); CService addr1 = CService("250.1.1.80");
addrman.Add(CAddress(addr1), source); addrman.Add(CAddress(addr1, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 79); BOOST_CHECK(addrman.size() == 79);
CService addr2 = CService("250.1.1.81"); CService addr2 = CService("250.1.1.81");
addrman.Add(CAddress(addr2), source); addrman.Add(CAddress(addr2, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 80); BOOST_CHECK(addrman.size() == 80);
} }
@ -255,9 +255,9 @@ BOOST_AUTO_TEST_CASE(addrman_find)
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
CAddress addr1 = CAddress(CService("250.1.2.1", 8333)); CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(CService("250.1.2.1", 9999)); CAddress addr2 = CAddress(CService("250.1.2.1", 9999), NODE_NONE);
CAddress addr3 = CAddress(CService("251.255.2.1", 8333)); CAddress addr3 = CAddress(CService("251.255.2.1", 8333), NODE_NONE);
CNetAddr source1 = CNetAddr("250.1.2.1"); CNetAddr source1 = CNetAddr("250.1.2.1");
CNetAddr source2 = CNetAddr("250.1.2.2"); CNetAddr source2 = CNetAddr("250.1.2.2");
@ -294,7 +294,7 @@ BOOST_AUTO_TEST_CASE(addrman_create)
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
CAddress addr1 = CAddress(CService("250.1.2.1", 8333)); CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE);
CNetAddr source1 = CNetAddr("250.1.2.1"); CNetAddr source1 = CNetAddr("250.1.2.1");
int nId; int nId;
@ -317,7 +317,7 @@ BOOST_AUTO_TEST_CASE(addrman_delete)
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
CAddress addr1 = CAddress(CService("250.1.2.1", 8333)); CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE);
CNetAddr source1 = CNetAddr("250.1.2.1"); CNetAddr source1 = CNetAddr("250.1.2.1");
int nId; int nId;
@ -344,15 +344,15 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
vector<CAddress> vAddr1 = addrman.GetAddr(); vector<CAddress> vAddr1 = addrman.GetAddr();
BOOST_CHECK(vAddr1.size() == 0); BOOST_CHECK(vAddr1.size() == 0);
CAddress addr1 = CAddress(CService("250.250.2.1", 8333)); CAddress addr1 = CAddress(CService("250.250.2.1", 8333), NODE_NONE);
addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false
CAddress addr2 = CAddress(CService("250.251.2.2", 9999)); CAddress addr2 = CAddress(CService("250.251.2.2", 9999), NODE_NONE);
addr2.nTime = GetAdjustedTime(); addr2.nTime = GetAdjustedTime();
CAddress addr3 = CAddress(CService("251.252.2.3", 8333)); CAddress addr3 = CAddress(CService("251.252.2.3", 8333), NODE_NONE);
addr3.nTime = GetAdjustedTime(); addr3.nTime = GetAdjustedTime();
CAddress addr4 = CAddress(CService("252.253.3.4", 8333)); CAddress addr4 = CAddress(CService("252.253.3.4", 8333), NODE_NONE);
addr4.nTime = GetAdjustedTime(); addr4.nTime = GetAdjustedTime();
CAddress addr5 = CAddress(CService("252.254.4.5", 8333)); CAddress addr5 = CAddress(CService("252.254.4.5", 8333), NODE_NONE);
addr5.nTime = GetAdjustedTime(); addr5.nTime = GetAdjustedTime();
CNetAddr source1 = CNetAddr("250.1.2.1"); CNetAddr source1 = CNetAddr("250.1.2.1");
CNetAddr source2 = CNetAddr("250.2.3.3"); CNetAddr source2 = CNetAddr("250.2.3.3");
@ -368,8 +368,8 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
BOOST_CHECK(addrman.GetAddr().size() == 1); BOOST_CHECK(addrman.GetAddr().size() == 1);
// Test 24: Ensure GetAddr works with new and tried addresses. // Test 24: Ensure GetAddr works with new and tried addresses.
addrman.Good(CAddress(addr1)); addrman.Good(CAddress(addr1, NODE_NONE));
addrman.Good(CAddress(addr2)); addrman.Good(CAddress(addr2, NODE_NONE));
BOOST_CHECK(addrman.GetAddr().size() == 1); BOOST_CHECK(addrman.GetAddr().size() == 1);
// Test 25: Ensure GetAddr still returns 23% when addrman has many addrs. // Test 25: Ensure GetAddr still returns 23% when addrman has many addrs.
@ -378,7 +378,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
int octet2 = (i / 256) % 256; int octet2 = (i / 256) % 256;
int octet3 = (i / (256 * 2)) % 256; int octet3 = (i / (256 * 2)) % 256;
string strAddr = boost::to_string(octet1) + "." + boost::to_string(octet2) + "." + boost::to_string(octet3) + ".23"; string strAddr = boost::to_string(octet1) + "." + boost::to_string(octet2) + "." + boost::to_string(octet3) + ".23";
CAddress addr = CAddress(CService(strAddr)); CAddress addr = CAddress(CService(strAddr), NODE_NONE);
// Ensure that for all addrs in addrman, isTerrible == false. // Ensure that for all addrs in addrman, isTerrible == false.
addr.nTime = GetAdjustedTime(); addr.nTime = GetAdjustedTime();
@ -403,8 +403,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
// Set addrman addr placement to be deterministic. // Set addrman addr placement to be deterministic.
addrman.MakeDeterministic(); addrman.MakeDeterministic();
CAddress addr1 = CAddress(CService("250.1.1.1", 8333)); CAddress addr1 = CAddress(CService("250.1.1.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(CService("250.1.1.1", 9999)); CAddress addr2 = CAddress(CService("250.1.1.1", 9999), NODE_NONE);
CNetAddr source1 = CNetAddr("250.1.1.1"); CNetAddr source1 = CNetAddr("250.1.1.1");
@ -431,7 +431,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
set<int> buckets; set<int> buckets;
for (int i = 0; i < 255; i++) { for (int i = 0; i < 255; i++) {
CAddrInfo infoi = CAddrInfo( CAddrInfo infoi = CAddrInfo(
CAddress(CService("250.1.1." + boost::to_string(i))), CAddress(CService("250.1.1." + boost::to_string(i)), NODE_NONE),
CNetAddr("250.1.1." + boost::to_string(i))); CNetAddr("250.1.1." + boost::to_string(i)));
int bucket = infoi.GetTriedBucket(nKey1); int bucket = infoi.GetTriedBucket(nKey1);
buckets.insert(bucket); buckets.insert(bucket);
@ -443,7 +443,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
buckets.clear(); buckets.clear();
for (int j = 0; j < 255; j++) { for (int j = 0; j < 255; j++) {
CAddrInfo infoj = CAddrInfo( CAddrInfo infoj = CAddrInfo(
CAddress(CService("250." + boost::to_string(j) + ".1.1")), CAddress(CService("250." + boost::to_string(j) + ".1.1"), NODE_NONE),
CNetAddr("250." + boost::to_string(j) + ".1.1")); CNetAddr("250." + boost::to_string(j) + ".1.1"));
int bucket = infoj.GetTriedBucket(nKey1); int bucket = infoj.GetTriedBucket(nKey1);
buckets.insert(bucket); buckets.insert(bucket);
@ -460,8 +460,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
// Set addrman addr placement to be deterministic. // Set addrman addr placement to be deterministic.
addrman.MakeDeterministic(); addrman.MakeDeterministic();
CAddress addr1 = CAddress(CService("250.1.2.1", 8333)); CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(CService("250.1.2.1", 9999)); CAddress addr2 = CAddress(CService("250.1.2.1", 9999), NODE_NONE);
CNetAddr source1 = CNetAddr("250.1.2.1"); CNetAddr source1 = CNetAddr("250.1.2.1");
@ -484,7 +484,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
set<int> buckets; set<int> buckets;
for (int i = 0; i < 255; i++) { for (int i = 0; i < 255; i++) {
CAddrInfo infoi = CAddrInfo( CAddrInfo infoi = CAddrInfo(
CAddress(CService("250.1.1." + boost::to_string(i))), CAddress(CService("250.1.1." + boost::to_string(i)), NODE_NONE),
CNetAddr("250.1.1." + boost::to_string(i))); CNetAddr("250.1.1." + boost::to_string(i)));
int bucket = infoi.GetNewBucket(nKey1); int bucket = infoi.GetNewBucket(nKey1);
buckets.insert(bucket); buckets.insert(bucket);
@ -497,7 +497,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
for (int j = 0; j < 4 * 255; j++) { for (int j = 0; j < 4 * 255; j++) {
CAddrInfo infoj = CAddrInfo(CAddress( CAddrInfo infoj = CAddrInfo(CAddress(
CService( CService(
boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1")), boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1"), NODE_NONE),
CNetAddr("251.4.1.1")); CNetAddr("251.4.1.1"));
int bucket = infoj.GetNewBucket(nKey1); int bucket = infoj.GetNewBucket(nKey1);
buckets.insert(bucket); buckets.insert(bucket);
@ -509,7 +509,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
buckets.clear(); buckets.clear();
for (int p = 0; p < 255; p++) { for (int p = 0; p < 255; p++) {
CAddrInfo infoj = CAddrInfo( CAddrInfo infoj = CAddrInfo(
CAddress(CService("250.1.1.1")), CAddress(CService("250.1.1.1"), NODE_NONE),
CNetAddr("250." + boost::to_string(p) + ".1.1")); CNetAddr("250." + boost::to_string(p) + ".1.1"));
int bucket = infoj.GetNewBucket(nKey1); int bucket = infoj.GetNewBucket(nKey1);
buckets.insert(bucket); buckets.insert(bucket);

View File

@ -51,7 +51,7 @@ public:
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30); int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
s << nUBuckets; s << nUBuckets;
CAddress addr = CAddress(CService("252.1.1.1", 7777)); CAddress addr = CAddress(CService("252.1.1.1", 7777), NODE_NONE);
CAddrInfo info = CAddrInfo(addr, CNetAddr("252.2.2.2")); CAddrInfo info = CAddrInfo(addr, CNetAddr("252.2.2.2"));
s << info; s << info;
} }
@ -79,9 +79,9 @@ BOOST_AUTO_TEST_CASE(caddrdb_read)
CService addr3 = CService("250.7.3.3", 9999); CService addr3 = CService("250.7.3.3", 9999);
// Add three addresses to new table. // Add three addresses to new table.
addrmanUncorrupted.Add(CAddress(addr1), CService("252.5.1.1", 8333)); addrmanUncorrupted.Add(CAddress(addr1, NODE_NONE), CService("252.5.1.1", 8333));
addrmanUncorrupted.Add(CAddress(addr2), CService("252.5.1.1", 8333)); addrmanUncorrupted.Add(CAddress(addr2, NODE_NONE), CService("252.5.1.1", 8333));
addrmanUncorrupted.Add(CAddress(addr3), CService("252.5.1.1", 8333)); addrmanUncorrupted.Add(CAddress(addr3, NODE_NONE), CService("252.5.1.1", 8333));
// Test that the de-serialization does not throw an exception. // Test that the de-serialization does not throw an exception.
CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted); CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted);