Skip to content

Commit ac36553

Browse files
authored
Merge pull request #1611 from michaeljfazio/peer-stats
Added p2p stats to node stat output
2 parents 6c2d688 + e9dde00 commit ac36553

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

jormungandr/src/network/p2p/topology.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,36 @@ use poldercast::{
1414
use slog::Logger;
1515
use std::sync::{Arc, RwLock};
1616

17+
// object holding a count of available, unreachable and quarantined nodes.
18+
#[derive(Clone)]
19+
pub struct NodeCount {
20+
all_available_nodes: usize,
21+
all_unreachable_nodes: usize,
22+
all_quarantined_nodes: usize,
23+
}
24+
25+
impl NodeCount {
26+
pub fn new(nodes: &poldercast::Nodes) -> Self {
27+
NodeCount {
28+
all_available_nodes: nodes.all_available_nodes().len(),
29+
all_unreachable_nodes: nodes.all_unreachable_nodes().len(),
30+
all_quarantined_nodes: nodes.all_quarantined_nodes().len(),
31+
}
32+
}
33+
34+
pub fn all_available_nodes_count(&self) -> usize {
35+
self.all_available_nodes
36+
}
37+
38+
pub fn all_unreachable_nodes_count(&self) -> usize {
39+
self.all_unreachable_nodes
40+
}
41+
42+
pub fn all_quarantined_nodes_count(&self) -> usize {
43+
self.all_quarantined_nodes
44+
}
45+
}
46+
1747
/// object holding the P2pTopology of the Node
1848
#[derive(Clone)]
1949
pub struct P2pTopology {
@@ -138,6 +168,10 @@ impl P2pTopology {
138168
.collect()
139169
}
140170

171+
pub fn nodes_count(&self) -> NodeCount {
172+
NodeCount::new(self.lock.read().unwrap().nodes())
173+
}
174+
141175
/// register a strike against the given node id
142176
///
143177
/// the function returns `None` if the node was not even in the

jormungandr/src/rest/v0/handlers.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ async fn create_stats(context: &FullContext) -> Result<serde_json::Value, Error>
156156
.map_err(|e| ErrorInternalServerError(format!("Block value calculation error: {}", e)))?;
157157
let stats = &context.stats_counter;
158158
let tip_header = tip.header();
159+
let stats = &context.stats_counter;
160+
let nodes_count = &context.p2p.nodes_count();
159161
Ok(json!({
160162
"txRecvCnt": stats.tx_recv_cnt(),
161163
"blockRecvCnt": stats.block_recv_cnt(),
@@ -169,6 +171,9 @@ async fn create_stats(context: &FullContext) -> Result<serde_json::Value, Error>
169171
"lastBlockContentSize": tip_header.block_content_size(),
170172
"lastBlockSum": block_input_sum.0,
171173
"lastBlockFees": block_fee_sum.0,
174+
"peerAvailableCnt": nodes_count.all_available_nodes_count().to_string(),
175+
"peerUnreachableCnt": nodes_count.all_unreachable_nodes_count().to_string(),
176+
"peerQuarantinedCnt": nodes_count.all_quarantined_nodes_count().to_string(),
172177
}))
173178
}
174179

0 commit comments

Comments
 (0)