chaincraft_rust/
network.rs

1//! Networking module for peer-to-peer communication
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5use std::net::SocketAddr;
6use uuid::Uuid;
7
8/// Unique identifier for a peer
9#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
10pub struct PeerId(Uuid);
11
12impl PeerId {
13    pub fn new() -> Self {
14        Self(Uuid::new_v4())
15    }
16
17    pub fn from_uuid(uuid: Uuid) -> Self {
18        Self(uuid)
19    }
20}
21
22impl fmt::Display for PeerId {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        write!(f, "{}", self.0)
25    }
26}
27
28impl Default for PeerId {
29    fn default() -> Self {
30        Self::new()
31    }
32}
33
34/// Information about a peer
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct PeerInfo {
37    pub id: PeerId,
38    pub address: SocketAddr,
39    pub last_seen: chrono::DateTime<chrono::Utc>,
40}
41
42impl PeerInfo {
43    pub fn new(id: PeerId, address: SocketAddr) -> Self {
44        Self {
45            id,
46            address,
47            last_seen: chrono::Utc::now(),
48        }
49    }
50}