Skip to content

Commit 4440710

Browse files
committed
Replace relevant services logic with a function suite.
Adds HasAllRelevantServices and GetRelevantServices, which check for NETWORK|WITNESS. This changes the following: * Removes nRelevantServices from CConnman, disconnecting it a bit more from protocol-level logic. * Replaces our sometimes-connect-to-!WITNESS-nodes logic with simply always requiring WITNESS|NETWORK for outbound non-feeler connections (feelers still only require NETWORK). * This has the added benefit of removing nServicesExpected from CNode - instead letting net_processing's VERSION message handling simply check HasAllRelevantServices. * This implies we believe WITNESS nodes to continue to be a significant majority of nodes on the network, but also because we cannot sync properly from !WITNESS nodes, it is strange to continue using our valuable outbound slots on them. * In order to prevent this change from preventing connection to -connect= nodes which have !WITNESS, -connect nodes are now given the "addnode" flag. This also allows outbound connections to !NODE_NETWORK nodes for -connect nodes (which was already true of addnodes). * Has the (somewhat unintended) consequence of changing one of the eviction metrics from the same sometimes-connect-to-!WITNESS-nodes metric to requiring HasRelevantServices. This should make NODE_NETWORK_LIMITED much simpler to implement.
1 parent 167cef8 commit 4440710

File tree

6 files changed

+55
-48
lines changed

6 files changed

+55
-48
lines changed

src/init.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,6 @@ void InitLogging()
815815

816816
namespace { // Variables internal to initialization process only
817817

818-
ServiceFlags nRelevantServices = NODE_NETWORK;
819818
int nMaxConnections;
820819
int nUserMaxConnections;
821820
int nFD;
@@ -1604,9 +1603,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16041603
// Note that setting NODE_WITNESS is never required: the only downside from not
16051604
// doing so is that after activation, no upgraded nodes will fetch from you.
16061605
nLocalServices = ServiceFlags(nLocalServices | NODE_WITNESS);
1607-
// Only care about others providing witness capabilities if there is a softfork
1608-
// defined.
1609-
nRelevantServices = ServiceFlags(nRelevantServices | NODE_WITNESS);
16101606
}
16111607

16121608
// ********************************************************* Step 10: import blocks
@@ -1656,7 +1652,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16561652

16571653
CConnman::Options connOptions;
16581654
connOptions.nLocalServices = nLocalServices;
1659-
connOptions.nRelevantServices = nRelevantServices;
16601655
connOptions.nMaxConnections = nMaxConnections;
16611656
connOptions.nMaxOutbound = std::min(MAX_OUTBOUND_CONNECTIONS, connOptions.nMaxConnections);
16621657
connOptions.nMaxAddnode = MAX_ADDNODE_CONNECTIONS;

src/net.cpp

+10-30
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,6 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
444444
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
445445
CAddress addr_bind = GetBindAddress(hSocket);
446446
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, addr_bind, pszDest ? pszDest : "", false);
447-
pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices);
448447
pnode->AddRef();
449448

450449
return pnode;
@@ -985,7 +984,7 @@ bool CConnman::AttemptToEvictConnection()
985984
continue;
986985
NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->nMinPingUsecTime,
987986
node->nLastBlockTime, node->nLastTXTime,
988-
(node->nServices & nRelevantServices) == nRelevantServices,
987+
HasAllDesirableServiceFlags(node->nServices),
989988
node->fRelayTxes, node->pfilter != nullptr, node->addr, node->nKeyedNetGroup};
990989
vEvictionCandidates.push_back(candidate);
991990
}
@@ -1602,7 +1601,7 @@ void CConnman::ThreadDNSAddressSeed()
16021601
LOCK(cs_vNodes);
16031602
int nRelevant = 0;
16041603
for (auto pnode : vNodes) {
1605-
nRelevant += pnode->fSuccessfullyConnected && ((pnode->nServices & nRelevantServices) == nRelevantServices);
1604+
nRelevant += pnode->fSuccessfullyConnected && HasAllDesirableServiceFlags(pnode->nServices);
16061605
}
16071606
if (nRelevant >= 2) {
16081607
LogPrintf("P2P peers available. Skipped DNS seeding.\n");
@@ -1624,7 +1623,7 @@ void CConnman::ThreadDNSAddressSeed()
16241623
} else {
16251624
std::vector<CNetAddr> vIPs;
16261625
std::vector<CAddress> vAdd;
1627-
ServiceFlags requiredServiceBits = nRelevantServices;
1626+
ServiceFlags requiredServiceBits = GetDesirableServiceFlags(NODE_NONE);
16281627
std::string host = GetDNSHost(seed, &requiredServiceBits);
16291628
CNetAddr resolveSource;
16301629
if (!resolveSource.SetInternal(host)) {
@@ -1705,7 +1704,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17051704
for (const std::string& strAddr : connect)
17061705
{
17071706
CAddress addr(CService(), NODE_NONE);
1708-
OpenNetworkConnection(addr, false, nullptr, strAddr.c_str());
1707+
OpenNetworkConnection(addr, false, nullptr, strAddr.c_str(), false, false, true);
17091708
for (int i = 0; i < 10 && i < nLoop; i++)
17101709
{
17111710
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
@@ -1753,17 +1752,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17531752
// Only connect out to one peer per network group (/16 for IPv4).
17541753
// Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
17551754
int nOutbound = 0;
1756-
int nOutboundRelevant = 0;
17571755
std::set<std::vector<unsigned char> > setConnected;
17581756
{
17591757
LOCK(cs_vNodes);
17601758
for (CNode* pnode : vNodes) {
17611759
if (!pnode->fInbound && !pnode->fAddnode) {
1762-
1763-
// Count the peers that have all relevant services
1764-
if (pnode->fSuccessfullyConnected && !pnode->fFeeler && ((pnode->nServices & nRelevantServices) == nRelevantServices)) {
1765-
nOutboundRelevant++;
1766-
}
17671760
// Netgroups for inbound and addnode peers are not excluded because our goal here
17681761
// is to not use multiple of our limited outbound slots on a single netgroup
17691762
// but inbound and addnode peers do not use our outbound slots. Inbound peers
@@ -1818,21 +1811,16 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18181811
if (IsLimited(addr))
18191812
continue;
18201813

1821-
// only connect to full nodes
1822-
if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
1823-
continue;
1824-
18251814
// only consider very recently tried nodes after 30 failed attempts
18261815
if (nANow - addr.nLastTry < 600 && nTries < 30)
18271816
continue;
18281817

1829-
// only consider nodes missing relevant services after 40 failed attempts and only if less than half the outbound are up.
1830-
ServiceFlags nRequiredServices = nRelevantServices;
1831-
if (nTries >= 40 && nOutbound < (nMaxOutbound >> 1)) {
1832-
nRequiredServices = REQUIRED_SERVICES;
1833-
}
1834-
1835-
if ((addr.nServices & nRequiredServices) != nRequiredServices) {
1818+
// for non-feelers, require all the services we'll want,
1819+
// for feelers, only require they be a full node (only because most
1820+
// SPV clients don't have a good address DB available)
1821+
if (!fFeeler && !HasAllDesirableServiceFlags(addr.nServices)) {
1822+
continue;
1823+
} else if (fFeeler && !MayHaveUsefulAddressDB(addr.nServices)) {
18361824
continue;
18371825
}
18381826

@@ -1841,13 +1829,6 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18411829
continue;
18421830

18431831
addrConnect = addr;
1844-
1845-
// regardless of the services assumed to be available, only require the minimum if half or more outbound have relevant services
1846-
if (nOutboundRelevant >= (nMaxOutbound >> 1)) {
1847-
addrConnect.nServices = REQUIRED_SERVICES;
1848-
} else {
1849-
addrConnect.nServices = nRequiredServices;
1850-
}
18511832
break;
18521833
}
18531834

@@ -2712,7 +2693,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
27122693
nSendVersion(0)
27132694
{
27142695
nServices = NODE_NONE;
2715-
nServicesExpected = NODE_NONE;
27162696
hSocket = hSocketIn;
27172697
nRecvVersion = INIT_PROTO_VERSION;
27182698
nLastSend = 0;

src/net.h

-8
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ static const bool DEFAULT_FORCEDNSSEED = false;
8484
static const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000;
8585
static const size_t DEFAULT_MAXSENDBUFFER = 1 * 1000;
8686

87-
static const ServiceFlags REQUIRED_SERVICES = NODE_NETWORK;
88-
8987
// NOTE: When adjusting this, update rpcnet:setban's help ("24h")
9088
static const unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24; // Default 24-hour ban
9189

@@ -130,7 +128,6 @@ class CConnman
130128
struct Options
131129
{
132130
ServiceFlags nLocalServices = NODE_NONE;
133-
ServiceFlags nRelevantServices = NODE_NONE;
134131
int nMaxConnections = 0;
135132
int nMaxOutbound = 0;
136133
int nMaxAddnode = 0;
@@ -152,7 +149,6 @@ class CConnman
152149

153150
void Init(const Options& connOptions) {
154151
nLocalServices = connOptions.nLocalServices;
155-
nRelevantServices = connOptions.nRelevantServices;
156152
nMaxConnections = connOptions.nMaxConnections;
157153
nMaxOutbound = std::min(connOptions.nMaxOutbound, connOptions.nMaxConnections);
158154
nMaxAddnode = connOptions.nMaxAddnode;
@@ -390,9 +386,6 @@ class CConnman
390386
/** Services this instance offers */
391387
ServiceFlags nLocalServices;
392388

393-
/** Services this instance cares about */
394-
ServiceFlags nRelevantServices;
395-
396389
CSemaphore *semOutbound;
397390
CSemaphore *semAddnode;
398391
int nMaxConnections;
@@ -585,7 +578,6 @@ class CNode
585578
public:
586579
// socket
587580
std::atomic<ServiceFlags> nServices;
588-
ServiceFlags nServicesExpected;
589581
SOCKET hSocket;
590582
size_t nSendSize; // total size of all vSendMsg entries
591583
size_t nSendOffset; // offset inside the first vSendMsg already sent

src/net_processing.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -1232,11 +1232,11 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
12321232
{
12331233
connman->SetServices(pfrom->addr, nServices);
12341234
}
1235-
if (pfrom->nServicesExpected & ~nServices)
1235+
if (!pfrom->fInbound && !pfrom->fFeeler && !pfrom->fAddnode && !HasAllDesirableServiceFlags(nServices))
12361236
{
1237-
LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->GetId(), nServices, pfrom->nServicesExpected);
1237+
LogPrint(BCLog::NET, "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->GetId(), nServices, GetDesirableServiceFlags(nServices));
12381238
connman->PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::REJECT, strCommand, REJECT_NONSTANDARD,
1239-
strprintf("Expected to offer services %08x", pfrom->nServicesExpected)));
1239+
strprintf("Expected to offer services %08x", GetDesirableServiceFlags(nServices))));
12401240
pfrom->fDisconnect = true;
12411241
return false;
12421242
}
@@ -1455,7 +1455,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
14551455
if (interruptMsgProc)
14561456
return true;
14571457

1458-
if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
1458+
// We only bother storing full nodes, though this may include
1459+
// things which we would not make an outbound connection to, in
1460+
// part because we may make feeler connections to them.
1461+
if (!MayHaveUsefulAddressDB(addr.nServices))
14591462
continue;
14601463

14611464
if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)

src/protocol.h

+37
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,43 @@ enum ServiceFlags : uint64_t {
277277
// BIP process.
278278
};
279279

280+
/**
281+
* Gets the set of service flags which are "desirable" for a given peer.
282+
*
283+
* These are the flags which are required for a peer to support for them
284+
* to be "interesting" to us, ie for us to wish to use one of our few
285+
* outbound connection slots for or for us to wish to prioritize keeping
286+
* their connection around.
287+
*
288+
* Relevant service flags may be peer- and state-specific in that the
289+
* version of the peer may determine which flags are required (eg in the
290+
* case of NODE_NETWORK_LIMITED where we seek out NODE_NETWORK peers
291+
* unless they set NODE_NETWORK_LIMITED and we are out of IBD, in which
292+
* case NODE_NETWORK_LIMITED suffices).
293+
*
294+
* Thus, generally, avoid calling with peerServices == NODE_NONE.
295+
*/
296+
static ServiceFlags GetDesirableServiceFlags(ServiceFlags services) {
297+
return ServiceFlags(NODE_NETWORK | NODE_WITNESS);
298+
}
299+
300+
/**
301+
* A shortcut for (services & GetDesirableServiceFlags(services))
302+
* == GetDesirableServiceFlags(services), ie determines whether the given
303+
* set of service flags are sufficient for a peer to be "relevant".
304+
*/
305+
static inline bool HasAllDesirableServiceFlags(ServiceFlags services) {
306+
return !(GetDesirableServiceFlags(services) & (~services));
307+
}
308+
309+
/**
310+
* Checks if a peer with the given service flags may be capable of having a
311+
* robust address-storage DB. Currently an alias for checking NODE_NETWORK.
312+
*/
313+
static inline bool MayHaveUsefulAddressDB(ServiceFlags services) {
314+
return services & NODE_NETWORK;
315+
}
316+
280317
/** A CService with information about it as peer */
281318
class CAddress : public CService
282319
{

src/rpc/net.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ UniValue addnode(const JSONRPCRequest& request)
217217
if (strCommand == "onetry")
218218
{
219219
CAddress addr;
220-
g_connman->OpenNetworkConnection(addr, false, nullptr, strNode.c_str());
220+
g_connman->OpenNetworkConnection(addr, false, nullptr, strNode.c_str(), false, false, true);
221221
return NullUniValue;
222222
}
223223

0 commit comments

Comments
 (0)