Skip to content

Commit a529fd3

Browse files
committed
[net processing] Move GetNodeStateStats into PeerManager
1 parent 257cf05 commit a529fd3

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
887887
LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
888888
}
889889

890-
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
890+
bool PeerManager::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
891891
{
892892
LOCK(cs_main);
893893
CNodeState* state = State(nodeid);

src/net_processing.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ static const bool DEFAULT_PEERBLOCKFILTERS = false;
3232
/** Threshold for marking a node to be discouraged, e.g. disconnected and added to the discouragement filter. */
3333
static const int DISCOURAGEMENT_THRESHOLD{100};
3434

35+
struct CNodeStateStats {
36+
int m_misbehavior_score = 0;
37+
int nSyncHeight = -1;
38+
int nCommonHeight = -1;
39+
std::vector<int> vHeightInFlight;
40+
};
41+
3542
class PeerManager final : public CValidationInterface, public NetEventsInterface {
3643
public:
3744
PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
@@ -94,6 +101,9 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
94101
*/
95102
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message);
96103

104+
/** Get statistics from node state */
105+
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats);
106+
97107
private:
98108
/**
99109
* Potentially mark a node discouraged based on the contents of a BlockValidationState object
@@ -145,16 +155,6 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
145155
int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
146156
};
147157

148-
struct CNodeStateStats {
149-
int m_misbehavior_score = 0;
150-
int nSyncHeight = -1;
151-
int nCommonHeight = -1;
152-
std::vector<int> vHeightInFlight;
153-
};
154-
155-
/** Get statistics from node state */
156-
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
157-
158158
/** Relay transaction to every node */
159159
void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
160160

src/node/interfaces.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ class NodeImpl : public Node
121121
}
122122

123123
// Try to retrieve the CNodeStateStats for each node.
124-
TRY_LOCK(::cs_main, lockMain);
125-
if (lockMain) {
126-
for (auto& node_stats : stats) {
127-
std::get<1>(node_stats) =
128-
GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats));
124+
if (m_context->peerman) {
125+
TRY_LOCK(::cs_main, lockMain);
126+
if (lockMain) {
127+
for (auto& node_stats : stats) {
128+
std::get<1>(node_stats) =
129+
m_context->peerman->GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats));
130+
}
129131
}
130132
}
131133
return true;

src/rpc/net.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ static RPCHelpMan getpeerinfo()
165165
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
166166
{
167167
NodeContext& node = EnsureNodeContext(request.context);
168-
if(!node.connman)
168+
if(!node.connman || !node.peerman) {
169169
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
170+
}
170171

171172
std::vector<CNodeStats> vstats;
172173
node.connman->GetNodeStats(vstats);
@@ -176,7 +177,7 @@ static RPCHelpMan getpeerinfo()
176177
for (const CNodeStats& stats : vstats) {
177178
UniValue obj(UniValue::VOBJ);
178179
CNodeStateStats statestats;
179-
bool fStateStats = GetNodeStateStats(stats.nodeid, statestats);
180+
bool fStateStats = node.peerman->GetNodeStateStats(stats.nodeid, statestats);
180181
obj.pushKV("id", stats.nodeid);
181182
obj.pushKV("addr", stats.addrName);
182183
if (stats.addrBind.IsValid()) {

0 commit comments

Comments
 (0)