1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! `/net_info` endpoint JSON-RPC wrapper

use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
use std::net::IpAddr;
use std::time::Duration;

use tendermint::{channel::Channel, node, serializers, Time};

/// Request network information from a node
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Request;

impl crate::Request for Request {
    type Response = Response;

    fn method(&self) -> crate::Method {
        crate::Method::NetInfo
    }
}

impl crate::SimpleRequest for Request {}

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

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

    /// Number of connected peers
    #[serde(with = "serializers::from_str")]
    pub n_peers: u64,

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

impl crate::Response for Response {}

/// 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", with = "serializers::time_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: Time,

    /// Duration of this monitor
    #[serde(rename = "Duration", with = "serializers::time_duration")]
    pub duration: Duration,

    /// Idle duration for this monitor
    #[serde(rename = "Idle", with = "serializers::time_duration")]
    pub idle: Duration,

    /// Bytes
    #[serde(rename = "Bytes", with = "serializers::from_str")]
    pub bytes: u64,

    /// Samples
    #[serde(rename = "Samples", with = "serializers::from_str")]
    pub samples: u64,

    /// Instant rate
    #[serde(rename = "InstRate", with = "serializers::from_str")]
    pub inst_rate: u64,

    /// Current rate
    #[serde(rename = "CurRate", with = "serializers::from_str")]
    pub cur_rate: u64,

    /// Average rate
    #[serde(rename = "AvgRate", with = "serializers::from_str")]
    pub avg_rate: u64,

    /// Peak rate
    #[serde(rename = "PeakRate", with = "serializers::from_str")]
    pub peak_rate: u64,

    /// Bytes remaining
    #[serde(rename = "BytesRem", with = "serializers::from_str")]
    pub bytes_rem: u64,

    /// Time remaining
    #[serde(rename = "TimeRem", with = "serializers::from_str")]
    pub time_rem: u64,

    /// Progress
    #[serde(rename = "Progress")]
    pub progress: u64,
}