diff --git a/src/net.cpp b/src/net.cpp index 2aab1dfcd..6d8d5d551 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2951,6 +2951,16 @@ void CNode::PushAddrMessage(CSerializedNetMsg&& msg) } } +void GetNodeCounts(int& nAll, int& nTls) +{ + LOCK(cs_vNodes); + nAll = (int)vNodes.size(); + nTls = 0; + for (const CNode* n : vNodes) + if (n->ssl != NULL) + nTls++; +} + size_t GetNodeCount(NumConnections flags) { LOCK(cs_vNodes); diff --git a/src/net.h b/src/net.h index 2627e071a..d50c76268 100644 --- a/src/net.h +++ b/src/net.h @@ -165,6 +165,7 @@ enum NumConnections { }; size_t GetNodeCount(NumConnections num); +void GetNodeCounts(int& nAll, int& nTls); bool GetNetworkActive(); void SetNetworkActive(bool active); diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index b3a14c699..e8267a5c2 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -132,6 +132,10 @@ UniValue getinfo(const UniValue& params, bool fHelp, const CPubKey& mypk) + HelpExampleRpc("getinfo", "") ); + // C268: snapshot peer counts under cs_vNodes (sequentially, before the cs_main/cs_wallet lock) + int nAllConns = 0, nTlsConns = 0; + GetNodeCounts(nAllConns, nTlsConns); + #ifdef ENABLE_WALLET LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL); #else @@ -175,8 +179,8 @@ UniValue getinfo(const UniValue& params, bool fHelp, const CPubKey& mypk) } //obj.push_back(Pair("build_date", BUILD_DATE)); obj.push_back(Pair("timeoffset", 0)); - obj.push_back(Pair("connections", (int)vNodes.size())); - obj.push_back(Pair("tls_connections", (int)std::count_if(vNodes.begin(), vNodes.end(), [](CNode* n) {return n->ssl != NULL;}))); + obj.push_back(Pair("connections", nAllConns)); + obj.push_back(Pair("tls_connections", nTlsConns)); obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.proxy.ToStringIPPort() : string()))); obj.push_back(Pair("testnet", Params().TestnetToBeDeprecatedFieldRPC())); obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()))); diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 62a04f45b..68acefab6 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -533,6 +533,9 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp, const CPubKey& mypk) + HelpExampleRpc("getnetworkinfo", "") ); + int nAllConns = 0, nTlsConns = 0; + GetNodeCounts(nAllConns, nTlsConns); + LOCK(cs_main); UniValue obj(UniValue::VOBJ); @@ -541,8 +544,8 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp, const CPubKey& mypk) obj.push_back(Pair("protocolversion",PROTOCOL_VERSION)); obj.push_back(Pair("localservices", strprintf("%016x", nLocalServices))); obj.push_back(Pair("timeoffset", 0)); - obj.push_back(Pair("connections", (int)vNodes.size())); - obj.push_back(Pair("tls_connections", (int)std::count_if(vNodes.begin(), vNodes.end(), [](CNode* n) {return n->ssl != NULL;}))); + obj.push_back(Pair("connections", nAllConns)); + obj.push_back(Pair("tls_connections", nTlsConns)); obj.push_back(Pair("networks", GetNetworksInfo())); obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()))); UniValue localAddresses(UniValue::VARR);