saorsa_core/dht/
mod.rs

1//! Distributed Hash Table implementations
2//!
3//! This module provides various DHT implementations including:
4//! - Trust-weighted Kademlia DHT with EigenTrust integration
5//! - Core DHT engine with replication and fault tolerance
6//! - Geographic routing and content addressing
7//! - Capacity signaling and telemetry
8
9pub mod capacity_signaling;
10pub mod core_engine;
11pub mod telemetry;
12pub mod trust_weighted_dht;
13pub mod trust_weighted_kademlia;
14
15// Re-export the main DHT trait and types
16pub use trust_weighted_dht::{
17    Contact, Dht, Key, Outcome, PutPolicy, PutReceipt, eigen_trust_epoch, record_interaction,
18};
19
20// Re-export PeerId from trust_weighted_dht
21pub use trust_weighted_dht::PeerId;
22
23// Re-export the trust-weighted implementation
24pub use trust_weighted_kademlia::TrustWeightedKademlia;
25
26// Re-export capacity signaling
27pub use capacity_signaling::{CapacityGossip, CapacityHistogram, CapacityManager, CapacityStats};
28
29// Re-export telemetry
30pub use telemetry::{DhtTelemetry, OperationStats, OperationType, TelemetryStats};
31
32// Re-export existing DHT components
33pub use core_engine::{DhtCoreEngine, DhtKey, NodeCapacity, NodeId as DhtNodeId, NodeInfo};
34
35// Legacy type aliases for backward compatibility
36pub type DHT = DhtCoreEngine;
37pub type DHTNode = NodeInfo;
38pub type SerializableDHTNode = NodeInfo;
39
40// Re-export types from trust_weighted_dht
41pub use trust_weighted_dht::Key as DHT_Key;
42
43// Import additional types for compatibility
44use serde::{Deserialize, Serialize};
45use std::time::Duration;
46
47/// DHT configuration parameters
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct DHTConfig {
50    /// Replication parameter (k) - number of nodes to store each record
51    pub replication_factor: usize,
52    /// Maximum nodes per k-bucket
53    pub bucket_size: usize,
54    /// Concurrency parameter for parallel lookups
55    pub alpha: usize,
56    /// Record expiration time
57    pub record_ttl: Duration,
58    /// Refresh interval for buckets
59    pub bucket_refresh_interval: Duration,
60    /// Republish interval for stored records
61    pub republish_interval: Duration,
62    /// Maximum distance for considering nodes "close"
63    pub max_distance: u8,
64}
65
66impl Default for DHTConfig {
67    fn default() -> Self {
68        Self {
69            replication_factor: 8,
70            bucket_size: 20,
71            alpha: 3,
72            record_ttl: Duration::from_secs(3600),
73            bucket_refresh_interval: Duration::from_secs(3600),
74            republish_interval: Duration::from_secs(3600),
75            max_distance: 160,
76        }
77    }
78}
79
80/// DHT record containing key-value data with metadata
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct Record {
83    /// Record key
84    pub key: Key,
85    /// Record value
86    pub value: Vec<u8>,
87    /// Publisher peer ID
88    pub publisher: PeerId,
89    /// Record creation time
90    pub created_at: std::time::SystemTime,
91    /// Record expiration time
92    pub expires_at: std::time::SystemTime,
93    /// Signature for verification (optional)
94    pub signature: Option<Vec<u8>>,
95}
96
97impl Record {
98    /// Create a new record
99    pub fn new(key: Key, value: Vec<u8>, publisher: PeerId) -> Self {
100        let now = std::time::SystemTime::now();
101        Self {
102            key,
103            value,
104            publisher,
105            created_at: now,
106            expires_at: now + std::time::Duration::from_secs(3600), // 1 hour default TTL
107            signature: None,
108        }
109    }
110
111    /// Check if the record has expired
112    pub fn is_expired(&self) -> bool {
113        std::time::SystemTime::now() > self.expires_at
114    }
115}
116
117// Re-export other DHT modules
118pub mod client;
119pub mod content_addressing;
120pub mod enhanced_storage;
121pub mod geographic_network_integration;
122pub mod geographic_routing;
123pub mod geographic_routing_table;
124pub mod latency_aware_selection;
125pub mod network_integration;
126pub mod optimized_storage;
127pub mod reed_solomon;
128pub mod rsps_integration;
129pub mod skademlia;
130pub mod witness;