Skip to content

Commit

Permalink
feat: add and use detailed inbound message count stats
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed May 5, 2021
1 parent 86a749c commit 0d7148e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
24 changes: 24 additions & 0 deletions network/src/inbound/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,22 @@ impl<S: Storage + Send + Sync + 'static> Node<S> {

match payload {
Payload::Transaction(transaction) => {
self.stats.recv_transactions.fetch_add(1, Ordering::Relaxed);

if let Some(ref sync) = self.sync() {
sync.received_memory_pool_transaction(source, transaction).await?;
}
}
Payload::Block(block) => {
self.stats.recv_blocks.fetch_add(1, Ordering::Relaxed);

if let Some(ref sync) = self.sync() {
sync.received_block(source, block, true).await?;
}
}
Payload::SyncBlock(block) => {
self.stats.recv_syncblocks.fetch_add(1, Ordering::Relaxed);

if let Some(ref sync) = self.sync() {
sync.received_block(source, block, false).await?;

Expand All @@ -256,26 +262,36 @@ impl<S: Storage + Send + Sync + 'static> Node<S> {
}
}
Payload::GetBlocks(hashes) => {
self.stats.recv_getblocks.fetch_add(1, Ordering::Relaxed);

if let Some(ref sync) = self.sync() {
sync.received_get_blocks(source, hashes).await?;
}
}
Payload::GetMemoryPool => {
self.stats.recv_getmemorypool.fetch_add(1, Ordering::Relaxed);

if let Some(ref sync) = self.sync() {
sync.received_get_memory_pool(source).await?;
}
}
Payload::MemoryPool(mempool) => {
self.stats.recv_memorypool.fetch_add(1, Ordering::Relaxed);

if let Some(ref sync) = self.sync() {
sync.received_memory_pool(mempool)?;
}
}
Payload::GetSync(getsync) => {
self.stats.recv_getsync.fetch_add(1, Ordering::Relaxed);

if let Some(ref sync) = self.sync() {
sync.received_get_sync(source, getsync).await?;
}
}
Payload::Sync(sync) => {
self.stats.recv_syncs.fetch_add(1, Ordering::Relaxed);

if let Some(ref sync_handler) = self.sync() {
if sync.is_empty() {
trace!("{} doesn't have sync blocks to share", source);
Expand All @@ -286,12 +302,18 @@ impl<S: Storage + Send + Sync + 'static> Node<S> {
}
}
Payload::GetPeers => {
self.stats.recv_getpeers.fetch_add(1, Ordering::Relaxed);

self.send_peers(source).await;
}
Payload::Peers(peers) => {
self.stats.recv_peers.fetch_add(1, Ordering::Relaxed);

self.process_inbound_peers(peers);
}
Payload::Ping(block_height) => {
self.stats.recv_pings.fetch_add(1, Ordering::Relaxed);

self.peer_book.received_ping(source, block_height);

// TODO (howardwu): Delete me after stabilizing new sync logic for blocks.
Expand All @@ -311,9 +333,11 @@ impl<S: Storage + Send + Sync + 'static> Node<S> {
// }
}
Payload::Pong => {
self.stats.recv_pongs.fetch_add(1, Ordering::Relaxed);
// Skip as this case is already handled with priority in Inbound::listen_for_messages
}
Payload::Unknown => {
self.stats.recv_unknown.fetch_add(1, Ordering::Relaxed);
warn!("Unknown payload received; this could indicate that the client you're using is out-of-date");
}
}
Expand Down
29 changes: 29 additions & 0 deletions network/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use std::sync::atomic::{AtomicU32, AtomicU64};

// TODO: make members private and make gathering of stats feature-gated and possibly
// interchangeable with prometheus metrics.
#[derive(Default)]
pub struct Stats {
/// The monotonic counter for the number of send requests that succeeded.
Expand All @@ -34,4 +36,31 @@ pub struct Stats {
pub outbound_connection_requests: AtomicU64,
/// The number of mined blocks.
pub blocks_mined: AtomicU32,

/// The number of all received `Block` messages.
pub recv_blocks: AtomicU64,
/// The number of all received `GetBlocks` messages.
pub recv_getblocks: AtomicU64,
/// The number of all received `GetMemoryPool` messages.
pub recv_getmemorypool: AtomicU64,
/// The number of all received `GetPeers` messages.
pub recv_getpeers: AtomicU64,
/// The number of all received `GetSync` messages.
pub recv_getsync: AtomicU64,
/// The number of all received `MemoryPool` messages.
pub recv_memorypool: AtomicU64,
/// The number of all received `Peers` messages.
pub recv_peers: AtomicU64,
/// The number of all received `Ping` messages.
pub recv_pings: AtomicU64,
/// The number of all received `Pong` messages.
pub recv_pongs: AtomicU64,
/// The number of all received `Sync` messages.
pub recv_syncs: AtomicU64,
/// The number of all received `SyncBlock` messages.
pub recv_syncblocks: AtomicU64,
/// The number of all received `Transaction` messages.
pub recv_transactions: AtomicU64,
/// The number of all received `Unknown` messages.
pub recv_unknown: AtomicU64,
}
12 changes: 12 additions & 0 deletions rpc/documentation/public_endpoints/getnodestats.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ None
| `number_of_connecting_peers` | u16 | The number of currently connecting peers |
| `blocks_mined` | u32 | The number of blocks the node has mined |
| `block_height` | u32 | The current block height of the node |
| `recv_blocks` | u64 | The number of all received Block messages |
| `recv_getmemorypool` | u64 | The number of all received GetMemoryPool messages |
| `recv_getpeers` | u64 | The number of all received GetPeers messages |
| `recv_getsync` | u64 | The number of all received GetSync messages |
| `recv_memorypool` | u64 | The number of all received MemoryPool messages |
| `recv_peers` | u64 | The number of all received Peers messages |
| `recv_pings` | u64 | The number of all received Ping messages |
| `recv_pongs` | u64 | The number of all received Pong messages |
| `recv_syncs` | u64 | The number of all received Sync messages |
| `recv_syncblocks` | u64 | The number of all received SyncBlock messages |
| `recv_transactions` | u64 | The number of all received Transaction messages |
| `recv_unknown` | u64 | The number of all received Unknown messages |

### Example
```ignore
Expand Down
14 changes: 14 additions & 0 deletions rpc/src/rpc_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,20 @@ impl<S: Storage + Send + core::marker::Sync + 'static> RpcFunctions for RpcImpl<
number_of_connecting_peers: self.node.peer_book.number_of_connecting_peers(),
blocks_mined: self.node.stats.blocks_mined.load(Ordering::Relaxed),
block_height: self.node.sync().map(|sync| sync.current_block_height()).unwrap_or(0),

recv_blocks: self.node.stats.recv_blocks.load(Ordering::Relaxed),
recv_getblocks: self.node.stats.recv_getblocks.load(Ordering::Relaxed),
recv_getmemorypool: self.node.stats.recv_getmemorypool.load(Ordering::Relaxed),
recv_getpeers: self.node.stats.recv_getpeers.load(Ordering::Relaxed),
recv_getsync: self.node.stats.recv_getsync.load(Ordering::Relaxed),
recv_memorypool: self.node.stats.recv_memorypool.load(Ordering::Relaxed),
recv_peers: self.node.stats.recv_peers.load(Ordering::Relaxed),
recv_pings: self.node.stats.recv_pings.load(Ordering::Relaxed),
recv_pongs: self.node.stats.recv_pongs.load(Ordering::Relaxed),
recv_syncs: self.node.stats.recv_syncs.load(Ordering::Relaxed),
recv_syncblocks: self.node.stats.recv_syncblocks.load(Ordering::Relaxed),
recv_transactions: self.node.stats.recv_transactions.load(Ordering::Relaxed),
recv_unknown: self.node.stats.recv_unknown.load(Ordering::Relaxed),
})
}

Expand Down
39 changes: 39 additions & 0 deletions rpc/src/rpc_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,45 @@ pub struct NodeStats {

/// The current block height of the node.
pub block_height: u32,

/// The number of all received `Block` messages.
pub recv_blocks: u64,

/// The number of all received `GetBlocks` messages.
pub recv_getblocks: u64,

/// The number of all received `GetMemoryPool` messages.
pub recv_getmemorypool: u64,

/// The number of all received `GetPeers` messages.
pub recv_getpeers: u64,

/// The number of all received `GetSync` messages.
pub recv_getsync: u64,

/// The number of all received `MemoryPool` messages.
pub recv_memorypool: u64,

/// The number of all received `Peers` messages.
pub recv_peers: u64,

/// The number of all received `Ping` messages.
pub recv_pings: u64,

/// The number of all received `Pong` messages.
pub recv_pongs: u64,

/// The number of all received `Sync` messages.
pub recv_syncs: u64,

/// The number of all received `SyncBlock` messages.
pub recv_syncblocks: u64,

/// The number of all received `Transaction` messages.
pub recv_transactions: u64,

/// The number of all received `Unknown` messages.
pub recv_unknown: u64,
}

/// Returned value for the `getpeerinfo` rpc call
Expand Down

0 comments on commit 0d7148e

Please sign in to comment.