op_alloy_rpc_types/
net.rs

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#![allow(missing_docs)]
//! Network RPC types

use alloc::{string::String, vec::Vec};
use alloy_primitives::{map::HashMap, ChainId};
use core::net::IpAddr;
use serde::{
    de::{self, Unexpected},
    Deserialize, Deserializer, Serialize, Serializer,
};

// https://github.com/ethereum-optimism/optimism/blob/8dd17a7b114a7c25505cd2e15ce4e3d0f7e3f7c1/op-node/p2p/store/iface.go#L13
#[derive(Clone, Debug, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TopicScores {
    pub time_in_mesh: f64,
    pub first_message_deliveries: f64,
    pub mesh_message_deliveries: f64,
    pub invalid_message_deliveries: f64,
}

// https://github.com/ethereum-optimism/optimism/blob/8dd17a7b114a7c25505cd2e15ce4e3d0f7e3f7c1/op-node/p2p/store/iface.go#L20C6-L20C18
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GossipScores {
    pub total: f64,
    pub blocks: TopicScores,
    #[serde(rename = "IPColocationFactor")]
    pub ip_colocation_factor: f64,
    pub behavioral_penalty: f64,
}

// https://github.com/ethereum-optimism/optimism/blob/8dd17a7b114a7c25505cd2e15ce4e3d0f7e3f7c1/op-node/p2p/store/iface.go#L31C1-L35C2
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReqRespScores {
    pub valid_responses: f64,
    pub error_responses: f64,
    pub rejected_payloads: f64,
}

// https://github.com/ethereum-optimism/optimism/blob/8dd17a7b114a7c25505cd2e15ce4e3d0f7e3f7c1/op-node/p2p/store/iface.go#L81
#[derive(Clone, Debug, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PeerScores {
    pub gossip: GossipScores,
    pub req_resp: ReqRespScores,
}

// https://github.com/ethereum-optimism/optimism/blob/develop/op-node/p2p/rpc_api.go#L15
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PeerInfo {
    #[serde(rename = "peerID")]
    pub peer_id: String,
    #[serde(rename = "nodeID")]
    pub node_id: String,
    pub user_agent: String,
    pub protocol_version: String,
    #[serde(rename = "ENR")]
    pub enr: String,
    pub addresses: Vec<String>,
    pub protocols: Option<Vec<String>>,
    /// 0: "`NotConnected`", 1: "Connected",
    /// 2: "`CanConnect`" (gracefully disconnected)
    /// 3: "`CannotConnect`" (tried but failed)
    pub connectedness: Connectedness,
    /// 0: "Unknown", 1: "Inbound" (if the peer contacted us)
    /// 2: "Outbound" (if we connected to them)
    pub direction: Direction,
    pub protected: bool,
    #[serde(rename = "chainID")]
    pub chain_id: ChainId,
    /// nanosecond
    pub latency: u64,
    pub gossip_blocks: bool,
    #[serde(rename = "scores")]
    pub peer_scores: PeerScores,
}
// https://github.com/ethereum-optimism/optimism/blob/40750a58e7a4a6f06370d18dfe6c6eab309012d9/op-node/p2p/rpc_api.go#L36
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PeerDump {
    pub total_connected: u32,
    pub peers: HashMap<String, PeerInfo>,
    pub banned_peers: Vec<String>,
    #[serde(rename = "bannedIPS")]
    pub banned_ips: Vec<IpAddr>,
    // todo: should be IPNet
    pub banned_subnets: Vec<IpAddr>,
}

// https://github.com/ethereum-optimism/optimism/blob/develop/op-node/p2p/rpc_server.go#L203
#[derive(Clone, Debug, Copy, Serialize, Deserialize)]
pub struct PeerStats {
    pub connected: u32,
    pub table: u32,
    #[serde(rename = "blocksTopic")]
    pub blocks_topic: u32,
    #[serde(rename = "blocksTopicV2")]
    pub blocks_topic_v2: u32,
    #[serde(rename = "blocksTopicV3")]
    pub blocks_topic_v3: u32,
    pub banned: u32,
    pub known: u32,
}

/// Represents the connectivity state of a peer in a network, indicating the reachability and
/// interaction status of a node with its peers.
#[derive(Clone, Debug, PartialEq, Copy, Default, Serialize, Deserialize)]
#[repr(u8)]
pub enum Connectedness {
    /// No current connection to the peer, and no recent history of a successful connection.
    #[default]
    NotConnected = 0,

    /// An active, open connection to the peer exists.
    Connected = 1,

    /// Connection to the peer is possible but not currently established; usually implies a past
    /// successful connection.
    CanConnect = 2,

    /// Recent attempts to connect to the peer failed, indicating potential issues in reachability
    /// or peer status.
    CannotConnect = 3,

    /// Connection to the peer is limited; may not have full capabilities.
    Limited = 4,
}

impl core::fmt::Display for Connectedness {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::NotConnected => write!(f, "Not Connected"),
            Self::Connected => write!(f, "Connected"),
            Self::CanConnect => write!(f, "Can Connect"),
            Self::CannotConnect => write!(f, "Cannot Connect"),
            Self::Limited => write!(f, "Limited"),
        }
    }
}

impl From<u8> for Connectedness {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::NotConnected,
            1 => Self::Connected,
            2 => Self::CanConnect,
            3 => Self::CannotConnect,
            4 => Self::Limited,
            _ => Self::NotConnected,
        }
    }
}
/// Direction represents the direction of a connection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Direction {
    /// Unknown is the default direction when the direction is not specified.
    #[default]
    Unknown = 0,
    /// Inbound is for when the remote peer initiated the connection.
    Inbound = 1,
    /// Outbound is for when the local peer initiated the connection.
    Outbound = 2,
}

impl Serialize for Direction {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_u8(*self as u8)
    }
}

impl<'de> Deserialize<'de> for Direction {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = u8::deserialize(deserializer)?;
        match value {
            0 => Ok(Self::Unknown),
            1 => Ok(Self::Inbound),
            2 => Ok(Self::Outbound),
            _ => Err(de::Error::invalid_value(
                Unexpected::Unsigned(value as u64),
                &"a value between 0 and 2",
            )),
        }
    }
}
impl core::fmt::Display for Direction {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Unknown => "Unknown",
                Self::Inbound => "Inbound",
                Self::Outbound => "Outbound",
            }
        )
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{self};

    #[test]
    fn test_direction_serialization() {
        assert_eq!(
            serde_json::to_string(&Direction::Unknown).unwrap(),
            "0",
            "Serialization failed for Direction::Unknown"
        );
        assert_eq!(
            serde_json::to_string(&Direction::Inbound).unwrap(),
            "1",
            "Serialization failed for Direction::Inbound"
        );
        assert_eq!(
            serde_json::to_string(&Direction::Outbound).unwrap(),
            "2",
            "Serialization failed for Direction::Outbound"
        );
    }

    #[test]
    fn test_direction_deserialization() {
        let unknown: Direction = serde_json::from_str("0").unwrap();
        let inbound: Direction = serde_json::from_str("1").unwrap();
        let outbound: Direction = serde_json::from_str("2").unwrap();

        assert_eq!(unknown, Direction::Unknown, "Deserialization mismatch for Direction::Unknown");
        assert_eq!(inbound, Direction::Inbound, "Deserialization mismatch for Direction::Inbound");
        assert_eq!(
            outbound,
            Direction::Outbound,
            "Deserialization mismatch for Direction::Outbound"
        );
    }
    #[test]
    fn test_peer_info_connectedness_serialization() {
        let peer_info = PeerInfo {
            peer_id: String::from("peer123"),
            node_id: String::from("node123"),
            user_agent: String::from("MyUserAgent"),
            protocol_version: String::from("v1"),
            enr: String::from("enr123"),
            addresses: vec![String::from("127.0.0.1")],
            protocols: Some(vec![String::from("eth"), String::from("p2p")]),
            connectedness: Connectedness::Connected,
            direction: Direction::Outbound,
            protected: true,
            chain_id: 1,
            latency: 100,
            gossip_blocks: true,
            peer_scores: PeerScores {
                gossip: GossipScores {
                    total: 1.0,
                    blocks: TopicScores {
                        time_in_mesh: 10.0,
                        first_message_deliveries: 5.0,
                        mesh_message_deliveries: 2.0,
                        invalid_message_deliveries: 0.0,
                    },
                    ip_colocation_factor: 0.5,
                    behavioral_penalty: 0.1,
                },
                req_resp: ReqRespScores {
                    valid_responses: 10.0,
                    error_responses: 1.0,
                    rejected_payloads: 0.0,
                },
            },
        };

        let serialized = serde_json::to_string(&peer_info).expect("Serialization failed");

        let deserialized: PeerInfo =
            serde_json::from_str(&serialized).expect("Deserialization failed");

        assert_eq!(peer_info.peer_id, deserialized.peer_id);
        assert_eq!(peer_info.node_id, deserialized.node_id);
        assert_eq!(peer_info.user_agent, deserialized.user_agent);
        assert_eq!(peer_info.protocol_version, deserialized.protocol_version);
        assert_eq!(peer_info.enr, deserialized.enr);
        assert_eq!(peer_info.addresses, deserialized.addresses);
        assert_eq!(peer_info.protocols, deserialized.protocols);
        assert_eq!(peer_info.connectedness, deserialized.connectedness);
        assert_eq!(peer_info.direction, deserialized.direction);
        assert_eq!(peer_info.protected, deserialized.protected);
        assert_eq!(peer_info.chain_id, deserialized.chain_id);
        assert_eq!(peer_info.latency, deserialized.latency);
        assert_eq!(peer_info.gossip_blocks, deserialized.gossip_blocks);
        assert_eq!(peer_info.peer_scores.gossip.total, deserialized.peer_scores.gossip.total);
        assert_eq!(
            peer_info.peer_scores.req_resp.valid_responses,
            deserialized.peer_scores.req_resp.valid_responses
        );
    }
}