storage-bindings 0.4.4

Rust bindings for Storage, the Decentralized Durability Engine
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Types for P2P operations

use serde::{Deserialize, Serialize};

/// Information about a peer in the network
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerInfo {
    /// Peer ID
    pub id: String,
    /// Multiaddresses of the peer
    pub addresses: Vec<String>,
    /// Whether the peer is connected
    pub connected: bool,
    /// Connection direction (inbound/outbound)
    pub direction: Option<String>,
    /// Latency to the peer (in milliseconds)
    pub latency_ms: Option<u64>,
}

impl PeerInfo {
    /// Create a new peer info
    pub fn new(id: String) -> Self {
        Self {
            id,
            addresses: Vec::new(),
            connected: false,
            direction: None,
            latency_ms: None,
        }
    }

    /// Set the addresses
    pub fn addresses(mut self, addresses: Vec<String>) -> Self {
        self.addresses = addresses;
        self
    }

    /// Set the connection status
    pub fn connected(mut self, connected: bool) -> Self {
        self.connected = connected;
        self
    }

    /// Set the connection direction
    pub fn direction(mut self, direction: String) -> Self {
        self.direction = Some(direction);
        self
    }

    /// Set the latency
    pub fn latency(mut self, latency_ms: u64) -> Self {
        self.latency_ms = Some(latency_ms);
        self
    }

    /// Check if the peer is reachable (has addresses and is connected)
    pub fn is_reachable(&self) -> bool {
        self.connected && !self.addresses.is_empty()
    }

    /// Get the primary address (first one in the list)
    pub fn primary_address(&self) -> Option<&String> {
        self.addresses.first()
    }

    /// Check if the connection is inbound
    pub fn is_inbound(&self) -> bool {
        self.direction.as_ref().is_some_and(|d| d == "inbound")
    }

    /// Check if the connection is outbound
    pub fn is_outbound(&self) -> bool {
        self.direction.as_ref().is_some_and(|d| d == "outbound")
    }

    /// Get a human-readable latency string
    pub fn latency_string(&self) -> String {
        match self.latency_ms {
            Some(latency) => format!("{}ms", latency),
            None => "Unknown".to_string(),
        }
    }
}

/// Detailed peer record for debugging
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerRecord {
    /// Peer ID
    pub id: String,
    /// Multiaddresses of the peer
    pub addresses: Vec<String>,
    /// Connection state
    pub connected: bool,
    /// Connection direction
    pub direction: Option<String>,
    /// Latency information
    pub latency_ms: Option<u64>,
    /// Protocols supported by the peer
    pub protocols: Vec<String>,
    /// User agent string
    pub user_agent: Option<String>,
    /// Last seen timestamp
    pub last_seen: Option<String>,
    /// Connection duration in seconds
    pub connection_duration_seconds: Option<u64>,
    /// Bytes sent to this peer
    pub bytes_sent: Option<u64>,
    /// Bytes received from this peer
    pub bytes_received: Option<u64>,
    /// Additional peer metadata
    pub metadata: Option<serde_json::Value>,
}

impl PeerRecord {
    /// Create a new peer record
    pub fn new(id: String) -> Self {
        Self {
            id,
            addresses: Vec::new(),
            connected: false,
            direction: None,
            latency_ms: None,
            protocols: Vec::new(),
            user_agent: None,
            last_seen: None,
            connection_duration_seconds: None,
            bytes_sent: None,
            bytes_received: None,
            metadata: None,
        }
    }

    /// Set the addresses
    pub fn addresses(mut self, addresses: Vec<String>) -> Self {
        self.addresses = addresses;
        self
    }

    /// Set the connection status
    pub fn connected(mut self, connected: bool) -> Self {
        self.connected = connected;
        self
    }

    /// Set the connection direction
    pub fn direction(mut self, direction: String) -> Self {
        self.direction = Some(direction);
        self
    }

    /// Set the latency
    pub fn latency(mut self, latency_ms: u64) -> Self {
        self.latency_ms = Some(latency_ms);
        self
    }

    /// Set the protocols
    pub fn protocols(mut self, protocols: Vec<String>) -> Self {
        self.protocols = protocols;
        self
    }

    /// Set the user agent
    pub fn user_agent(mut self, user_agent: String) -> Self {
        self.user_agent = Some(user_agent);
        self
    }

    /// Set the last seen time
    pub fn last_seen(mut self, last_seen: String) -> Self {
        self.last_seen = Some(last_seen);
        self
    }

    /// Set the connection duration
    pub fn connection_duration(mut self, duration_seconds: u64) -> Self {
        self.connection_duration_seconds = Some(duration_seconds);
        self
    }

    /// Set the bytes sent
    pub fn bytes_sent(mut self, bytes_sent: u64) -> Self {
        self.bytes_sent = Some(bytes_sent);
        self
    }

    /// Set the bytes received
    pub fn bytes_received(mut self, bytes_received: u64) -> Self {
        self.bytes_received = Some(bytes_received);
        self
    }

    /// Set the metadata
    pub fn metadata(mut self, metadata: serde_json::Value) -> Self {
        self.metadata = Some(metadata);
        self
    }

    /// Get the total bytes transferred
    pub fn total_bytes(&self) -> u64 {
        self.bytes_sent.unwrap_or(0) + self.bytes_received.unwrap_or(0)
    }

    /// Check if the peer supports a specific protocol
    pub fn supports_protocol(&self, protocol: &str) -> bool {
        self.protocols.contains(&protocol.to_string())
    }

    /// Get the connection duration as a human-readable string
    pub fn duration_string(&self) -> String {
        match self.connection_duration_seconds {
            Some(seconds) => {
                if seconds < 60 {
                    format!("{}s", seconds)
                } else if seconds < 3600 {
                    format!("{}m {}s", seconds / 60, seconds % 60)
                } else {
                    format!(
                        "{}h {}m {}s",
                        seconds / 3600,
                        (seconds % 3600) / 60,
                        seconds % 60
                    )
                }
            }
            None => "Unknown".to_string(),
        }
    }

    /// Get a human-readable size string for bytes transferred
    pub fn bytes_string(&self) -> String {
        let total = self.total_bytes();
        if total < 1024 {
            format!("{}B", total)
        } else if total < 1024 * 1024 {
            format!("{:.1}KB", total as f64 / 1024.0)
        } else if total < 1024 * 1024 * 1024 {
            format!("{:.1}MB", total as f64 / (1024.0 * 1024.0))
        } else {
            format!("{:.1}GB", total as f64 / (1024.0 * 1024.0 * 1024.0))
        }
    }

    /// Convert to a PeerInfo (simplified version)
    pub fn to_peer_info(&self) -> PeerInfo {
        PeerInfo {
            id: self.id.clone(),
            addresses: self.addresses.clone(),
            connected: self.connected,
            direction: self.direction.clone(),
            latency_ms: self.latency_ms,
        }
    }

    /// Get a human-readable latency string
    pub fn latency_string(&self) -> String {
        match self.latency_ms {
            Some(latency) => format!("{}ms", latency),
            None => "Unknown".to_string(),
        }
    }

    /// Get connection quality based on latency and duration
    pub fn connection_quality(&self) -> ConnectionQuality {
        let latency = self.latency_ms.unwrap_or(u64::MAX);
        let duration = self.connection_duration_seconds.unwrap_or(0);

        match (latency, duration) {
            (0..=100, 300..) => ConnectionQuality::Excellent,
            (0..=100, _) => ConnectionQuality::Good,
            (101..=500, 300..) => ConnectionQuality::Good,
            (101..=500, _) => ConnectionQuality::Fair,
            (501..=1000, 300..) => ConnectionQuality::Fair,
            (501..=1000, _) => ConnectionQuality::Poor,
            _ => ConnectionQuality::VeryPoor,
        }
    }

    /// Check if the connection is inbound
    pub fn is_inbound(&self) -> bool {
        self.direction.as_ref().is_some_and(|d| d == "inbound")
    }

    /// Check if the connection is outbound
    pub fn is_outbound(&self) -> bool {
        self.direction.as_ref().is_some_and(|d| d == "outbound")
    }
}

/// Connection quality assessment
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConnectionQuality {
    Excellent,
    Good,
    Fair,
    Poor,
    VeryPoor,
}

impl ConnectionQuality {
    /// Get a string representation
    pub fn as_str(&self) -> &'static str {
        match self {
            ConnectionQuality::Excellent => "Excellent",
            ConnectionQuality::Good => "Good",
            ConnectionQuality::Fair => "Fair",
            ConnectionQuality::Poor => "Poor",
            ConnectionQuality::VeryPoor => "VeryPoor",
        }
    }

    /// Get a numeric score (0-4)
    pub fn score(&self) -> u8 {
        match self {
            ConnectionQuality::Excellent => 4,
            ConnectionQuality::Good => 3,
            ConnectionQuality::Fair => 2,
            ConnectionQuality::Poor => 1,
            ConnectionQuality::VeryPoor => 0,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_peer_info_creation() {
        let peer_info = PeerInfo::new("12D3KooWExamplePeer".to_string())
            .addresses(vec!["/ip4/192.168.1.100/tcp/8080".to_string()])
            .connected(true)
            .direction("outbound".to_string())
            .latency(50);

        assert_eq!(peer_info.id, "12D3KooWExamplePeer");
        assert_eq!(peer_info.addresses.len(), 1);
        assert!(peer_info.connected);
        assert_eq!(peer_info.direction, Some("outbound".to_string()));
        assert_eq!(peer_info.latency_ms, Some(50));
    }

    #[test]
    fn test_peer_info_methods() {
        let peer_info = PeerInfo::new("12D3KooWExamplePeer".to_string())
            .addresses(vec!["/ip4/192.168.1.100/tcp/8080".to_string()])
            .connected(true)
            .direction("outbound".to_string())
            .latency(50);

        assert!(peer_info.is_reachable());
        assert_eq!(
            peer_info.primary_address(),
            Some(&"/ip4/192.168.1.100/tcp/8080".to_string())
        );
        assert!(peer_info.is_outbound());
        assert!(!peer_info.is_inbound());
        assert_eq!(peer_info.latency_string(), "50ms");
    }

    #[test]
    fn test_peer_record_creation() {
        let peer_record = PeerRecord::new("12D3KooWExamplePeer".to_string())
            .addresses(vec!["/ip4/192.168.1.100/tcp/8080".to_string()])
            .connected(true)
            .direction("outbound".to_string())
            .latency(50)
            .protocols(vec!["/storage/1.0.0".to_string()])
            .user_agent("storage-rust-bindings/0.1.0".to_string())
            .last_seen("2023-01-01T12:00:00Z".to_string())
            .connection_duration(1800)
            .bytes_sent(1024 * 1024)
            .bytes_received(2 * 1024 * 1024)
            .metadata(serde_json::json!({"version": "1.0.0"}));

        assert_eq!(peer_record.id, "12D3KooWExamplePeer");
        assert_eq!(peer_record.addresses.len(), 1);
        assert!(peer_record.connected);
        assert_eq!(peer_record.latency_ms, Some(50));
        assert_eq!(peer_record.protocols.len(), 1);
        assert!(peer_record.supports_protocol("/storage/1.0.0"));
        assert_eq!(peer_record.total_bytes(), 3 * 1024 * 1024);
        assert_eq!(peer_record.duration_string(), "30m 0s");
        assert_eq!(peer_record.bytes_string(), "3.0MB");
    }

    #[test]
    fn test_serialization() {
        let peer_info = PeerInfo::new("12D3KooWExamplePeer".to_string())
            .addresses(vec!["/ip4/192.168.1.100/tcp/8080".to_string()])
            .connected(true)
            .direction("outbound".to_string())
            .latency(50);

        let json = serde_json::to_string(&peer_info).unwrap();
        let deserialized: PeerInfo = serde_json::from_str(&json).unwrap();

        assert_eq!(peer_info.id, deserialized.id);
        assert_eq!(peer_info.addresses, deserialized.addresses);
        assert_eq!(peer_info.connected, deserialized.connected);
        assert_eq!(peer_info.direction, deserialized.direction);
        assert_eq!(peer_info.latency_ms, deserialized.latency_ms);
    }
}