Skip to main content

firecloud_core/
node.rs

1//! Node-related types for P2P network
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5
6/// Unique identifier for a node (derived from its public key)
7#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8pub struct NodeId(pub [u8; 32]);
9
10impl NodeId {
11    /// Create from raw bytes
12    pub fn from_bytes(bytes: [u8; 32]) -> Self {
13        Self(bytes)
14    }
15
16    /// Get as hex string
17    pub fn to_hex(&self) -> String {
18        hex::encode(self.0)
19    }
20
21    /// Short display (first 8 chars)
22    pub fn short(&self) -> String {
23        self.to_hex()[..8].to_string()
24    }
25}
26
27impl fmt::Debug for NodeId {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(f, "NodeId({})", self.short())
30    }
31}
32
33impl fmt::Display for NodeId {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(f, "{}", self.short())
36    }
37}
38
39/// Information about a node in the network
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct NodeInfo {
42    /// Node's unique identifier
43    pub id: NodeId,
44    /// Human-readable name (optional)
45    pub name: Option<String>,
46    /// Available storage in bytes
47    pub available_storage: u64,
48    /// Total storage capacity in bytes
49    pub total_storage: u64,
50    /// Node's public addresses
51    pub addresses: Vec<String>,
52    /// Software version
53    pub version: String,
54    /// Last seen timestamp (Unix millis)
55    pub last_seen: i64,
56}
57
58impl NodeInfo {
59    /// Calculate storage utilization as percentage
60    pub fn storage_utilization(&self) -> f64 {
61        if self.total_storage == 0 {
62            return 0.0;
63        }
64        let used = self.total_storage - self.available_storage;
65        (used as f64 / self.total_storage as f64) * 100.0
66    }
67}