Skip to content

Commit

Permalink
net: Add most functions needed for vNodes to CConnman
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni authored and Fuzzbawls committed Aug 25, 2020
1 parent 5667e61 commit 2e02467
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 50 deletions.
66 changes: 66 additions & 0 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,72 @@ bool CConnman::RemoveAddedNode(const std::string& strNode)
return false;
}

size_t CConnman::GetNodeCount(NumConnections flags)
{
LOCK(cs_vNodes);
if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
return vNodes.size();

int nNum = 0;
for(std::vector<CNode*>::const_iterator it = vNodes.begin(); it != vNodes.end(); ++it)
if (flags & ((*it)->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
nNum++;

return nNum;
}

void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
{
vstats.clear();
LOCK(cs_vNodes);
vstats.reserve(vNodes.size());
for(std::vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) {
CNode* pnode = *it;
CNodeStats stats;
pnode->copyStats(stats);
vstats.push_back(stats);
}
}

bool CConnman::DisconnectAddress(const CNetAddr& netAddr)
{
if (CNode* pnode = FindNode(netAddr)) {
pnode->fDisconnect = true;
return true;
}
return false;
}

bool CConnman::DisconnectSubnet(const CSubNet& subNet)
{
if (CNode* pnode = FindNode(subNet)) {
pnode->fDisconnect = true;
return true;
}
return false;
}

bool CConnman::DisconnectNode(const std::string& strNode)
{
if (CNode* pnode = FindNode(strNode)) {
pnode->fDisconnect = true;
return true;
}
return false;
}

bool CConnman::DisconnectNode(NodeId id)
{
LOCK(cs_vNodes);
for(CNode* pnode : vNodes) {
if (id == pnode->id) {
pnode->fDisconnect = true;
return true;
}
}
return false;
}

void RelayTransaction(const CTransaction& tx)
{
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
Expand Down
20 changes: 18 additions & 2 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ unsigned int SendBufferSize();

bool RecvLine(SOCKET hSocket, std::string& strLine);

typedef int NodeId;

struct AddedNodeInfo
{
std::string strAddedNode;
Expand All @@ -102,9 +104,18 @@ CNode* FindNode(const CSubNet& subNet);
CNode* FindNode(const std::string& addrName);
CNode* FindNode(const CService& ip);

class CNodeStats;
class CConnman
{
public:

enum NumConnections {
CONNECTIONS_NONE = 0,
CONNECTIONS_IN = (1U << 0),
CONNECTIONS_OUT = (1U << 1),
CONNECTIONS_ALL = (CONNECTIONS_IN | CONNECTIONS_OUT),
};

CConnman();
~CConnman();
bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError);
Expand Down Expand Up @@ -151,6 +162,13 @@ class CConnman
bool RemoveAddedNode(const std::string& node);
std::vector<AddedNodeInfo> GetAddedNodeInfo();

size_t GetNodeCount(NumConnections num);
void GetNodeStats(std::vector<CNodeStats>& vstats);
bool DisconnectAddress(const CNetAddr& addr);
bool DisconnectNode(const std::string& node);
bool DisconnectNode(NodeId id);
bool DisconnectSubnet(const CSubNet& subnet);

private:
struct ListenSocket {
SOCKET socket;
Expand Down Expand Up @@ -199,8 +217,6 @@ bool StopNode(CConnman& connman);
void SocketSendData(CNode* pnode);
void CheckOffsetDisconnectedPeers(const CNetAddr& ip);

typedef int NodeId;

struct CombinerAll {
typedef bool result_type;

Expand Down
22 changes: 12 additions & 10 deletions src/qt/clientmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ ClientModel::~ClientModel()

int ClientModel::getNumConnections(unsigned int flags) const
{
LOCK(cs_vNodes);
if (flags == CONNECTIONS_ALL) // Shortcut if we want total
return vNodes.size();

int nNum = 0;
for (CNode* pnode : vNodes)
if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
nNum++;

return nNum;
CConnman::NumConnections connections = CConnman::CONNECTIONS_NONE;

if(flags == CONNECTIONS_IN)
connections = CConnman::CONNECTIONS_IN;
else if (flags == CONNECTIONS_OUT)
connections = CConnman::CONNECTIONS_OUT;
else if (flags == CONNECTIONS_ALL)
connections = CConnman::CONNECTIONS_ALL;

if (g_connman)
return g_connman->GetNodeCount(connections);
return 0;
}

QString ClientModel::getMasternodeCountString() const
Expand Down
16 changes: 7 additions & 9 deletions src/qt/peertablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,19 @@ class PeerTablePriv
void refreshPeers()
{
{
TRY_LOCK(cs_vNodes, lockNodes);
if (!lockNodes) {
// skip the refresh if we can't immediately get the lock
return;
}
cachedNodeStats.clear();

cachedNodeStats.reserve(vNodes.size());
Q_FOREACH (CNode* pnode, vNodes) {
std::vector<CNodeStats> vstats;
if(g_connman)
g_connman->GetNodeStats(vstats);
cachedNodeStats.reserve(vstats.size());
Q_FOREACH (const CNodeStats& nodestats, vstats)
{
CNodeCombinedStats stats;
stats.nodeStateStats.nMisbehavior = 0;
stats.nodeStateStats.nSyncHeight = -1;
stats.nodeStateStats.nCommonHeight = -1;
stats.fNodeStateStatsAvailable = false;
pnode->copyStats(stats.nodeStats);
stats.nodeStats = nodestats;
cachedNodeStats.append(stats);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,13 +984,13 @@ void RPCConsole::showBanTableContextMenu(const QPoint& point)

void RPCConsole::disconnectSelectedNode()
{
if(!g_connman)
return;
// Get currently selected peer address
QString strNode = GUIUtil::getEntryData(ui->peerWidget, 0, PeerTableModel::Address).toString();
NodeId id = GUIUtil::getEntryData(ui->peerWidget, 0, PeerTableModel::NetNodeId).toInt();
// Find the node, disconnect it and clear the selected node
if (CNode *bannedNode = FindNode(strNode.toStdString())) {
bannedNode->CloseSocketDisconnect();
if(g_connman->DisconnectNode(id))
clearSelectedNode();
}
}

void RPCConsole::banSelectedNode(int bantime)
Expand Down
5 changes: 4 additions & 1 deletion src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,10 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
if (strMode != "template")
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");

if (vNodes.empty())
if(!g_connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");

if (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0)
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "PIVX is not connected!");

if (IsInitialBlockDownload())
Expand Down
3 changes: 2 additions & 1 deletion src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ UniValue getinfo(const JSONRPCRequest& request)
#endif
obj.push_back(Pair("blocks", (int)chainActive.Height()));
obj.push_back(Pair("timeoffset", GetTimeOffset()));
obj.push_back(Pair("connections", (int)vNodes.size()));
if(g_connman)
obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL)));
obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.proxy.ToStringIPPort() : std::string())));
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
obj.push_back(Pair("testnet", Params().NetworkID() == CBaseChainParams::TESTNET));
Expand Down
36 changes: 13 additions & 23 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ UniValue getconnectioncount(const JSONRPCRequest& request)
"\nExamples:\n" +
HelpExampleCli("getconnectioncount", "") + HelpExampleRpc("getconnectioncount", ""));

LOCK2(cs_main, cs_vNodes);
if(!g_connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");

return (int)vNodes.size();
return (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL);
}

UniValue ping(const JSONRPCRequest& request)
Expand All @@ -61,19 +62,6 @@ UniValue ping(const JSONRPCRequest& request)
return NullUniValue;
}

static void CopyNodeStats(std::vector<CNodeStats>& vstats)
{
vstats.clear();

LOCK(cs_vNodes);
vstats.reserve(vNodes.size());
for (CNode* pnode : vNodes) {
CNodeStats stats;
pnode->copyStats(stats);
vstats.push_back(stats);
}
}

UniValue getpeerinfo(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 0)
Expand Down Expand Up @@ -114,10 +102,11 @@ UniValue getpeerinfo(const JSONRPCRequest& request)
"\nExamples:\n" +
HelpExampleCli("getpeerinfo", "") + HelpExampleRpc("getpeerinfo", ""));

LOCK(cs_main);
if(!g_connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");

std::vector<CNodeStats> vstats;
CopyNodeStats(vstats);
g_connman->GetNodeStats(vstats);

UniValue ret(UniValue::VARR);

Expand Down Expand Up @@ -221,11 +210,12 @@ UniValue disconnectnode(const JSONRPCRequest& request)
+ HelpExampleRpc("disconnectnode", "\"192.168.0.6:8333\"")
);

CNode* pNode = FindNode(request.params[0].get_str());
if (pNode == NULL)
throw JSONRPCError(RPC_CLIENT_NODE_NOT_CONNECTED, "Node not found in connected nodes");
if(!g_connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");

pNode->CloseSocketDisconnect();
bool ret = g_connman->DisconnectNode(request.params[0].get_str());
if (!ret)
throw JSONRPCError(RPC_CLIENT_NODE_NOT_CONNECTED, "Node not found in connected nodes");

return NullUniValue;
}
Expand Down Expand Up @@ -383,14 +373,14 @@ UniValue getnetworkinfo(const JSONRPCRequest& request)
HelpExampleCli("getnetworkinfo", "") + HelpExampleRpc("getnetworkinfo", ""));

LOCK(cs_main);

UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("version", CLIENT_VERSION));
obj.push_back(Pair("subversion", strSubVersion));
obj.push_back(Pair("protocolversion", PROTOCOL_VERSION));
obj.push_back(Pair("localservices", strprintf("%016x", nLocalServices)));
obj.push_back(Pair("timeoffset", GetTimeOffset()));
obj.push_back(Pair("connections", (int)vNodes.size()));
if(g_connman)
obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL)));
obj.push_back(Pair("networks", GetNetworksInfo()));
obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())));
UniValue localAddresses(UniValue::VARR);
Expand Down

0 comments on commit 2e02467

Please sign in to comment.