Skip to content

Commit ee06e04

Browse files
committed
Introduce enum ServiceFlags for service flags
1 parent 15bf863 commit ee06e04

File tree

11 files changed

+89
-83
lines changed

11 files changed

+89
-83
lines changed

src/addrman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
263263
pinfo->nTime = std::max((int64_t)0, addr.nTime - nTimePenalty);
264264

265265
// add services
266-
pinfo->nServices |= addr.nServices;
266+
pinfo->nServices = ServiceFlags(pinfo->nServices | addr.nServices);
267267

268268
// do not update if no new information is present
269269
if (!addr.nTime || (pinfo->nTime && addr.nTime <= pinfo->nTime))
@@ -502,7 +502,7 @@ void CAddrMan::Connected_(const CService& addr, int64_t nTime)
502502
info.nTime = nTime;
503503
}
504504

505-
void CAddrMan::SetServices_(const CService& addr, uint64_t nServices)
505+
void CAddrMan::SetServices_(const CService& addr, ServiceFlags nServices)
506506
{
507507
CAddrInfo* pinfo = Find(addr);
508508

src/addrman.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class CAddrMan
257257
void Connected_(const CService &addr, int64_t nTime);
258258

259259
//! Update an entry's service bits.
260-
void SetServices_(const CService &addr, uint64_t nServices);
260+
void SetServices_(const CService &addr, ServiceFlags nServices);
261261

262262
public:
263263
/**
@@ -592,7 +592,7 @@ class CAddrMan
592592
}
593593
}
594594

595-
void SetServices(const CService &addr, uint64_t nServices)
595+
void SetServices(const CService &addr, ServiceFlags nServices)
596596
{
597597
LOCK(cs);
598598
Check();

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
950950
SetMockTime(GetArg("-mocktime", 0)); // SetMockTime(0) is a no-op
951951

952952
if (GetBoolArg("-peerbloomfilters", DEFAULT_PEERBLOOMFILTERS))
953-
nLocalServices |= NODE_BLOOM;
953+
nLocalServices = ServiceFlags(nLocalServices | NODE_BLOOM);
954954

955955
nMaxTipAge = GetArg("-maxtipage", DEFAULT_MAX_TIP_AGE);
956956

@@ -1361,7 +1361,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
13611361
// after any wallet rescanning has taken place.
13621362
if (fPruneMode) {
13631363
LogPrintf("Unsetting NODE_NETWORK on prune mode\n");
1364-
nLocalServices &= ~NODE_NETWORK;
1364+
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK);
13651365
if (!fReindex) {
13661366
uiInterface.InitMessage(_("Pruning blockstore..."));
13671367
PruneAndFlush();

src/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4611,7 +4611,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
46114611
CAddress addrMe;
46124612
CAddress addrFrom;
46134613
uint64_t nNonce = 1;
4614-
vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
4614+
uint64_t nServiceInt;
4615+
vRecv >> pfrom->nVersion >> nServiceInt >> nTime >> addrMe;
4616+
pfrom->nServices = ServiceFlags(nServiceInt);
46154617
if (!pfrom->fInbound)
46164618
{
46174619
addrman.SetServices(pfrom->addr, pfrom->nServices);

src/net.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ namespace {
7272
const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
7373

7474
/** Services this node implementation cares about */
75-
static const uint64_t nRelevantServices = NODE_NETWORK;
75+
static const ServiceFlags nRelevantServices = NODE_NETWORK;
7676

7777
//
7878
// Global state variables
7979
//
8080
bool fDiscover = true;
8181
bool fListen = true;
82-
uint64_t nLocalServices = NODE_NETWORK;
82+
ServiceFlags nLocalServices = NODE_NETWORK;
8383
bool fRelayTxes = true;
8484
CCriticalSection cs_mapLocalHost;
8585
std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
@@ -175,7 +175,7 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
175175
// one by discovery.
176176
CAddress GetLocalAddress(const CNetAddr *paddrPeer)
177177
{
178-
CAddress ret(CService("0.0.0.0",GetListenPort()),0);
178+
CAddress ret(CService("0.0.0.0",GetListenPort()), NODE_NONE);
179179
CService addr;
180180
if (GetLocal(addr, paddrPeer))
181181
{
@@ -411,7 +411,7 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure
411411
vNodes.push_back(pnode);
412412
}
413413

414-
pnode->nServicesExpected = addrConnect.nServices & nRelevantServices;
414+
pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices);
415415
pnode->nTimeConnected = GetTime();
416416

417417
return pnode;
@@ -464,14 +464,14 @@ void CNode::PushVersion()
464464
int nBestHeight = GetNodeSignals().GetHeight().get_value_or(0);
465465

466466
int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
467-
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0",0), addr.nServices));
467+
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0", 0), addr.nServices));
468468
CAddress addrMe = GetLocalAddress(&addr);
469469
GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
470470
if (fLogIPs)
471471
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), addrYou.ToString(), id);
472472
else
473473
LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id);
474-
PushMessage(NetMsgType::VERSION, PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
474+
PushMessage(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalServices, nTime, addrYou, addrMe,
475475
nLocalHostNonce, strSubVersion, nBestHeight, ::fRelayTxes);
476476
}
477477

@@ -1440,7 +1440,7 @@ void ThreadDNSAddressSeed()
14401440
} else {
14411441
std::vector<CNetAddr> vIPs;
14421442
std::vector<CAddress> vAdd;
1443-
uint64_t requiredServiceBits = nRelevantServices;
1443+
ServiceFlags requiredServiceBits = nRelevantServices;
14441444
if (LookupHost(seed.getHost(requiredServiceBits).c_str(), vIPs, 0, true))
14451445
{
14461446
BOOST_FOREACH(const CNetAddr& ip, vIPs)
@@ -1523,7 +1523,7 @@ void ThreadOpenConnections()
15231523
ProcessOneShot();
15241524
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-connect"])
15251525
{
1526-
CAddress addr(CService(), 0);
1526+
CAddress addr(CService(), NODE_NONE);
15271527
OpenNetworkConnection(addr, false, NULL, strAddr.c_str());
15281528
for (int i = 0; i < 10 && i < nLoop; i++)
15291529
{
@@ -1674,8 +1674,8 @@ void ThreadOpenAddedConnections()
16741674
{
16751675
CSemaphoreGrant grant(*semOutbound);
16761676
/* We want -addnode to work even for nodes that don't provide all
1677-
* wanted services, so pass in nServices=0 to CAddress. */
1678-
OpenNetworkConnection(CAddress(vserv[i % vserv.size()], 0), false, &grant);
1677+
* wanted services, so pass in nServices=NODE_NONE to CAddress. */
1678+
OpenNetworkConnection(CAddress(vserv[i % vserv.size()], NODE_NONE), false, &grant);
16791679
MilliSleep(500);
16801680
}
16811681
MilliSleep(120000); // Retry every 2 minutes
@@ -2333,8 +2333,8 @@ CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNa
23332333
addrKnown(5000, 0.001),
23342334
filterInventoryKnown(50000, 0.000001)
23352335
{
2336-
nServices = 0;
2337-
nServicesExpected = 0;
2336+
nServices = NODE_NONE;
2337+
nServicesExpected = NODE_NONE;
23382338
hSocket = hSocketIn;
23392339
nRecvVersion = INIT_PROTO_VERSION;
23402340
nLastSend = 0;

src/net.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
152152

153153
extern bool fDiscover;
154154
extern bool fListen;
155-
extern uint64_t nLocalServices;
155+
extern ServiceFlags nLocalServices;
156156
extern bool fRelayTxes;
157157
extern uint64_t nLocalHostNonce;
158158
extern CAddrMan addrman;
@@ -186,7 +186,7 @@ class CNodeStats
186186
{
187187
public:
188188
NodeId nodeid;
189-
uint64_t nServices;
189+
ServiceFlags nServices;
190190
bool fRelayTxes;
191191
int64_t nLastSend;
192192
int64_t nLastRecv;
@@ -316,8 +316,8 @@ class CNode
316316
{
317317
public:
318318
// socket
319-
uint64_t nServices;
320-
uint64_t nServicesExpected;
319+
ServiceFlags nServices;
320+
ServiceFlags nServicesExpected;
321321
SOCKET hSocket;
322322
CDataStream ssSend;
323323
size_t nSendSize; // total size of all vSendMsg entries

src/protocol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ CAddress::CAddress() : CService()
133133
Init();
134134
}
135135

136-
CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
136+
CAddress::CAddress(CService ipIn, ServiceFlags nServicesIn) : CService(ipIn)
137137
{
138138
Init();
139139
nServices = nServicesIn;
140140
}
141141

142142
void CAddress::Init()
143143
{
144-
nServices = 0;
144+
nServices = NODE_NONE;
145145
nTime = 100000000;
146146
}
147147

src/protocol.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ extern const char *FEEFILTER;
223223
const std::vector<std::string> &getAllNetMessageTypes();
224224

225225
/** nServices flags */
226-
enum {
226+
enum ServiceFlags : uint64_t {
227+
// Nothing
228+
NODE_NONE = 0,
227229
// NODE_NETWORK means that the node is capable of serving the block chain. It is currently
228230
// set by all Bitcoin Core nodes, and is unset by SPV clients or other peers that just want
229231
// network services but don't provide them.
@@ -251,7 +253,7 @@ class CAddress : public CService
251253
{
252254
public:
253255
CAddress();
254-
explicit CAddress(CService ipIn, uint64_t nServicesIn);
256+
explicit CAddress(CService ipIn, ServiceFlags nServicesIn);
255257

256258
void Init();
257259

@@ -267,13 +269,15 @@ class CAddress : public CService
267269
if ((nType & SER_DISK) ||
268270
(nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
269271
READWRITE(nTime);
270-
READWRITE(nServices);
272+
uint64_t nServicesInt = nServices;
273+
READWRITE(nServicesInt);
274+
nServices = (ServiceFlags)nServicesInt;
271275
READWRITE(*(CService*)this);
272276
}
273277

274278
// TODO: make private (improves encapsulation)
275279
public:
276-
uint64_t nServices;
280+
ServiceFlags nServices;
277281

278282
// disk and network only
279283
unsigned int nTime;

src/test/DoS_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ BOOST_FIXTURE_TEST_SUITE(DoS_tests, TestingSetup)
4545
BOOST_AUTO_TEST_CASE(DoS_banning)
4646
{
4747
CNode::ClearBanned();
48-
CAddress addr1(ip(0xa0b0c001), 0);
48+
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
4949
CNode dummyNode1(INVALID_SOCKET, addr1, "", true);
5050
dummyNode1.nVersion = 1;
5151
Misbehaving(dummyNode1.GetId(), 100); // Should get banned
5252
SendMessages(&dummyNode1);
5353
BOOST_CHECK(CNode::IsBanned(addr1));
5454
BOOST_CHECK(!CNode::IsBanned(ip(0xa0b0c001|0x0000ff00))); // Different IP, not banned
5555

56-
CAddress addr2(ip(0xa0b0c002), 0);
56+
CAddress addr2(ip(0xa0b0c002), NODE_NONE);
5757
CNode dummyNode2(INVALID_SOCKET, addr2, "", true);
5858
dummyNode2.nVersion = 1;
5959
Misbehaving(dummyNode2.GetId(), 50);
@@ -69,7 +69,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
6969
{
7070
CNode::ClearBanned();
7171
mapArgs["-banscore"] = "111"; // because 11 is my favorite number
72-
CAddress addr1(ip(0xa0b0c001), 0);
72+
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
7373
CNode dummyNode1(INVALID_SOCKET, addr1, "", true);
7474
dummyNode1.nVersion = 1;
7575
Misbehaving(dummyNode1.GetId(), 100);
@@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
9090
int64_t nStartTime = GetTime();
9191
SetMockTime(nStartTime); // Overrides future calls to GetTime()
9292

93-
CAddress addr(ip(0xa0b0c001), 0);
93+
CAddress addr(ip(0xa0b0c001), NODE_NONE);
9494
CNode dummyNode(INVALID_SOCKET, addr, "", true);
9595
dummyNode.nVersion = 1;
9696

0 commit comments

Comments
 (0)