use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DhtStateSnapshot {
pub nodes: Vec<DhtNodeInfo>,
pub token_secret: [u8; 20],
pub last_bootstrap_epoch_secs: Option<u64>,
pub total_nodes: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DhtNodeInfo {
pub id: [u8; 20],
pub addr: String,
pub last_seen_epoch_secs: u64,
}
impl DhtStateSnapshot {
pub fn empty() -> Self {
Self {
nodes: vec![],
token_secret: [0u8; 20],
last_bootstrap_epoch_secs: None,
total_nodes: 0,
}
}
pub fn new(
nodes: Vec<DhtNodeInfo>,
token_secret: [u8; 20],
last_bootstrap_epoch_secs: Option<u64>,
) -> Self {
let total_nodes = nodes.len();
Self {
nodes,
token_secret,
last_bootstrap_epoch_secs,
total_nodes,
}
}
pub fn to_json_string(&self) -> Result<String, String> {
serde_json::to_string(self).map_err(|e| e.to_string())
}
pub fn from_json_string(json: &str) -> Result<Self, String> {
serde_json::from_str(json).map_err(|e| e.to_string())
}
}