[net processing] Move GetNodeStateStats into PeerManager

This commit is contained in:
John Newbery 2020-08-24 17:39:54 +01:00
parent 257cf05f9b
commit a529fd3e3f
4 changed files with 21 additions and 18 deletions

View File

@ -887,7 +887,7 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid); LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
} }
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) { bool PeerManager::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
{ {
LOCK(cs_main); LOCK(cs_main);
CNodeState* state = State(nodeid); CNodeState* state = State(nodeid);

View File

@ -32,6 +32,13 @@ static const bool DEFAULT_PEERBLOCKFILTERS = false;
/** Threshold for marking a node to be discouraged, e.g. disconnected and added to the discouragement filter. */ /** Threshold for marking a node to be discouraged, e.g. disconnected and added to the discouragement filter. */
static const int DISCOURAGEMENT_THRESHOLD{100}; static const int DISCOURAGEMENT_THRESHOLD{100};
struct CNodeStateStats {
int m_misbehavior_score = 0;
int nSyncHeight = -1;
int nCommonHeight = -1;
std::vector<int> vHeightInFlight;
};
class PeerManager final : public CValidationInterface, public NetEventsInterface { class PeerManager final : public CValidationInterface, public NetEventsInterface {
public: public:
PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman, PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
@ -94,6 +101,9 @@ public:
*/ */
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message); void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message);
/** Get statistics from node state */
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats);
private: private:
/** /**
* Potentially mark a node discouraged based on the contents of a BlockValidationState object * Potentially mark a node discouraged based on the contents of a BlockValidationState object
@ -145,16 +155,6 @@ private:
int64_t m_stale_tip_check_time; //!< Next time to check for stale tip int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
}; };
struct CNodeStateStats {
int m_misbehavior_score = 0;
int nSyncHeight = -1;
int nCommonHeight = -1;
std::vector<int> vHeightInFlight;
};
/** Get statistics from node state */
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
/** Relay transaction to every node */ /** Relay transaction to every node */
void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main); void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

View File

@ -121,11 +121,13 @@ public:
} }
// Try to retrieve the CNodeStateStats for each node. // Try to retrieve the CNodeStateStats for each node.
TRY_LOCK(::cs_main, lockMain); if (m_context->peerman) {
if (lockMain) { TRY_LOCK(::cs_main, lockMain);
for (auto& node_stats : stats) { if (lockMain) {
std::get<1>(node_stats) = for (auto& node_stats : stats) {
GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats)); std::get<1>(node_stats) =
m_context->peerman->GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats));
}
} }
} }
return true; return true;

View File

@ -165,8 +165,9 @@ static RPCHelpMan getpeerinfo()
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{ {
NodeContext& node = EnsureNodeContext(request.context); NodeContext& node = EnsureNodeContext(request.context);
if(!node.connman) if(!node.connman || !node.peerman) {
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
}
std::vector<CNodeStats> vstats; std::vector<CNodeStats> vstats;
node.connman->GetNodeStats(vstats); node.connman->GetNodeStats(vstats);
@ -176,7 +177,7 @@ static RPCHelpMan getpeerinfo()
for (const CNodeStats& stats : vstats) { for (const CNodeStats& stats : vstats) {
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
CNodeStateStats statestats; CNodeStateStats statestats;
bool fStateStats = GetNodeStateStats(stats.nodeid, statestats); bool fStateStats = node.peerman->GetNodeStateStats(stats.nodeid, statestats);
obj.pushKV("id", stats.nodeid); obj.pushKV("id", stats.nodeid);
obj.pushKV("addr", stats.addrName); obj.pushKV("addr", stats.addrName);
if (stats.addrBind.IsValid()) { if (stats.addrBind.IsValid()) {