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 replication grace period types
33pub use replication_grace_period::{ReplicationGracePeriodConfig, FailedNodeInfo, NodeFailureReason, ReplicationError, EndpointRegistration};
34
35// Re-export node failure tracker
36pub use node_failure_tracker::{NodeFailureTracker, DefaultNodeFailureTracker, DhtClient};
37
38// Re-export existing DHT components
39pub use core_engine::{DhtCoreEngine, DhtKey, NodeCapacity, NodeId as DhtNodeId, NodeInfo};
40
41// Legacy type aliases for backward compatibility
42pub type DHT = DhtCoreEngine;
43pub type DHTNode = NodeInfo;
44pub type SerializableDHTNode = NodeInfo;
45
46// Re-export types from trust_weighted_dht
47pub use trust_weighted_dht::Key as DHT_Key;
48
49// Import additional types for compatibility
50use serde::{Deserialize, Serialize};
51use std::time::Duration;
52
53/// DHT configuration parameters
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct DHTConfig {
56    /// Replication parameter (k) - number of nodes to store each record
57    pub replication_factor: usize,
58    /// Maximum nodes per k-bucket
59    pub bucket_size: usize,
60    /// Concurrency parameter for parallel lookups
61    pub alpha: usize,
62    /// Record expiration time
63    pub record_ttl: Duration,
64    /// Refresh interval for buckets
65    pub bucket_refresh_interval: Duration,
66    /// Republish interval for stored records
67    pub republish_interval: Duration,
68    /// Maximum distance for considering nodes "close"
69    pub max_distance: u8,
70}
71
72impl Default for DHTConfig {
73    fn default() -> Self {
74        Self {
75            replication_factor: 8,
76            bucket_size: 20,
77            alpha: 3,
78            record_ttl: Duration::from_secs(3600),
79            bucket_refresh_interval: Duration::from_secs(3600),
80            republish_interval: Duration::from_secs(3600),
81            max_distance: 160,
82        }
83    }
84}
85
86/// DHT record containing key-value data with metadata
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub struct Record {
89    /// Record key
90    pub key: Key,
91    /// Record value
92    pub value: Vec<u8>,
93    /// Publisher peer ID
94    pub publisher: PeerId,
95    /// Record creation time
96    pub created_at: std::time::SystemTime,
97    /// Record expiration time
98    pub expires_at: std::time::SystemTime,
99    /// Signature for verification (optional)
100    pub signature: Option<Vec<u8>>,
101}
102
103impl Record {
104    /// Create a new record
105    pub fn new(key: Key, value: Vec<u8>, publisher: PeerId) -> Self {
106        let now = std::time::SystemTime::now();
107        Self {
108            key,
109            value,
110            publisher,
111            created_at: now,
112            expires_at: now + std::time::Duration::from_secs(3600), // 1 hour default TTL
113            signature: None,
114        }
115    }
116
117    /// Check if the record has expired
118    pub fn is_expired(&self) -> bool {
119        std::time::SystemTime::now() > self.expires_at
120    }
121}
122
123// Re-export other DHT modules
124pub mod client;
125pub mod content_addressing;
126pub mod enhanced_storage;
127pub mod geographic_network_integration;
128pub mod geographic_routing;
129pub mod geographic_routing_table;
130pub mod latency_aware_selection;
131pub mod network_integration;
132pub mod node_failure_tracker;
133pub mod optimized_storage;
134pub mod reed_solomon;
135pub mod replication_grace_period;
136pub mod rsps_integration;
137pub mod skademlia;
138pub mod witness;