Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
tendermint-rs: /net_info RPC endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-iqlusion committed Apr 20, 2019
1 parent 2753d24 commit 55f1404
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 7 deletions.
2 changes: 2 additions & 0 deletions tendermint-rs/src/rpc/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Tendermint JSONRPC endpoints

mod net_info;
mod status;

pub use net_info::{NetInfoRequest, NetInfoResponse};
pub use status::{StatusRequest, StatusResponse};
189 changes: 189 additions & 0 deletions tendermint-rs/src/rpc/endpoint/net_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
//! RPC wrapper for `/net_info` endpoint

use crate::{channel::Channel, node, rpc, Timestamp};
use serde::{Deserialize, Serialize};
use std::{
fmt::{self, Display},
net::IpAddr,
time::Duration,
};

/// Request the status of the node
#[derive(Debug, Default)]
pub struct NetInfoRequest;

impl rpc::Request for NetInfoRequest {
type Response = NetInfoResponse;

fn path(&self) -> rpc::request::Path {
"/net_info".parse().unwrap()
}
}

/// Status responses
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct NetInfoResponse {
/// Are we presently listening?
pub listening: bool,

/// Active listeners
pub listeners: Vec<Listener>,

/// Number of connected peers
#[serde(
serialize_with = "rpc::response::serialize_u64",
deserialize_with = "rpc::response::parse_u64"
)]
pub n_peers: u64,

/// Peer information
pub peers: Vec<PeerInfo>,
}

impl rpc::Response for NetInfoResponse {}

/// Listener information
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Listener(String);

impl Display for Listener {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

/// Peer information
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PeerInfo {
/// Node information
pub node_info: node::Info,

/// Is this an outbound connection?
pub is_outbound: bool,

/// Connection status
pub connection_status: ConnectionStatus,

/// Remote IP address
pub remote_ip: IpAddr,
}

/// Connection status information
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ConnectionStatus {
/// Duration of this connection
#[serde(
rename = "Duration",
serialize_with = "rpc::response::serialize_duration",
deserialize_with = "rpc::response::parse_duration"
)]
pub duration: Duration,

/// Send monitor
#[serde(rename = "SendMonitor")]
pub send_monitor: Monitor,

/// Receive monitor
#[serde(rename = "RecvMonitor")]
pub recv_monitor: Monitor,

/// Channels
#[serde(rename = "Channels")]
pub channels: Vec<Channel>,
}

/// Monitor
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Monitor {
/// Is this monitor active?
#[serde(rename = "Active")]
pub active: bool,

/// When the monitor started
#[serde(rename = "Start")]
pub start: Timestamp,

/// Duration of this monitor
#[serde(
rename = "Duration",
serialize_with = "rpc::response::serialize_duration",
deserialize_with = "rpc::response::parse_duration"
)]
pub duration: Duration,

/// Idle duration for this monitor
#[serde(
rename = "Idle",
serialize_with = "rpc::response::serialize_duration",
deserialize_with = "rpc::response::parse_duration"
)]
pub idle: Duration,

/// Bytes
#[serde(
rename = "Bytes",
serialize_with = "rpc::response::serialize_u64",
deserialize_with = "rpc::response::parse_u64"
)]
bytes: u64,

/// Samples
#[serde(
rename = "Samples",
serialize_with = "rpc::response::serialize_u64",
deserialize_with = "rpc::response::parse_u64"
)]
samples: u64,

/// Instant rate
#[serde(
rename = "InstRate",
serialize_with = "rpc::response::serialize_u64",
deserialize_with = "rpc::response::parse_u64"
)]
inst_rate: u64,

/// Current rate
#[serde(
rename = "CurRate",
serialize_with = "rpc::response::serialize_u64",
deserialize_with = "rpc::response::parse_u64"
)]
cur_rate: u64,

/// Average rate
#[serde(
rename = "AvgRate",
serialize_with = "rpc::response::serialize_u64",
deserialize_with = "rpc::response::parse_u64"
)]
avg_rate: u64,

/// Peak rate
#[serde(
rename = "PeakRate",
serialize_with = "rpc::response::serialize_u64",
deserialize_with = "rpc::response::parse_u64"
)]
peak_rate: u64,

/// Bytes remaining
#[serde(
rename = "BytesRem",
serialize_with = "rpc::response::serialize_u64",
deserialize_with = "rpc::response::parse_u64"
)]
bytes_rem: u64,

/// Time remaining
#[serde(
rename = "TimeRem",
serialize_with = "rpc::response::serialize_u64",
deserialize_with = "rpc::response::parse_u64"
)]
time_rem: u64,

/// Progress
#[serde(rename = "Progress")]
progress: u64,
}
2 changes: 1 addition & 1 deletion tendermint-rs/src/rpc/endpoint/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{account, block, node, rpc, Hash, PublicKey, Timestamp};
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize, Serializer};

/// Node status request
#[derive(Default)]
#[derive(Debug, Default)]
pub struct StatusRequest;

impl rpc::Request for StatusRequest {
Expand Down
7 changes: 2 additions & 5 deletions tendermint-rs/src/rpc/response.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! JSONRPC response types

// TODO(tarcieri): remove this when functions below are all used
#![allow(dead_code)]

use failure::{format_err, Error};
use serde::{
de::{DeserializeOwned, Error as DeError},
Expand Down Expand Up @@ -78,7 +75,7 @@ where
}

/// Parse `Duration` from a JSON string containing a nanosecond count
fn parse_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
pub(crate) fn parse_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
D: Deserializer<'de>,
{
Expand All @@ -91,7 +88,7 @@ where
}

/// Serialize `Duration` as a JSON string containing a nanosecond count
fn serialize_duration<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
pub(crate) fn serialize_duration<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Expand Down
19 changes: 18 additions & 1 deletion tendermint-rs/tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@
#[cfg(feature = "rpc")]
mod endpoints {
use std::{fs, path::PathBuf};
use tendermint::rpc::{endpoint::StatusResponse, Response};
use tendermint::rpc::{
endpoint::{NetInfoResponse, StatusResponse},
Response,
};

fn read_json_fixture(name: &str) -> String {
fs::read_to_string(PathBuf::from("./tests/support/").join(name.to_owned() + ".json"))
.unwrap()
}

#[test]
fn net_info() {
let net_info_json = read_json_fixture("net_info");
let net_info_response = NetInfoResponse::from_json(&net_info_json).unwrap();

println!("net_info_response: {:?}", net_info_response);

assert_eq!(net_info_response.n_peers, 2);
assert_eq!(
net_info_response.peers[0].node_info.network.as_str(),
"cosmoshub-1"
);
}

#[test]
fn status() {
let status_json = read_json_fixture("status");
Expand Down
Loading

0 comments on commit 55f1404

Please sign in to comment.