Skip to main content

fips_core/cache/
coord_cache.rs

1//! Coordinate cache for routing decisions.
2//!
3//! Maps node addresses to their tree coordinates, enabling data packets
4//! to be routed without carrying coordinates in every packet. Populated
5//! by SessionSetup packets.
6
7use std::collections::HashMap;
8
9use super::CacheStats;
10use super::entry::CacheEntry;
11use crate::NodeAddr;
12use crate::tree::TreeCoordinate;
13
14/// Default maximum entries in coordinate cache.
15pub const DEFAULT_COORD_CACHE_SIZE: usize = 50_000;
16
17/// Default TTL for coordinate cache entries (5 minutes in milliseconds).
18pub const DEFAULT_COORD_CACHE_TTL_MS: u64 = 300_000;
19
20/// Coordinate cache for routing decisions.
21///
22/// Maps node addresses to their tree coordinates, enabling data packets
23/// to be routed without carrying coordinates in every packet. Populated
24/// by SessionSetup packets.
25#[derive(Clone, Debug)]
26pub struct CoordCache {
27    /// NodeAddr -> coordinates mapping.
28    entries: HashMap<NodeAddr, CacheEntry>,
29    /// Maximum number of entries.
30    max_entries: usize,
31    /// Default TTL for entries (milliseconds).
32    default_ttl_ms: u64,
33}
34
35impl CoordCache {
36    /// Create a new coordinate cache.
37    pub fn new(max_entries: usize, default_ttl_ms: u64) -> Self {
38        Self {
39            entries: HashMap::with_capacity(max_entries.min(1000)),
40            max_entries,
41            default_ttl_ms,
42        }
43    }
44
45    /// Create a cache with default parameters.
46    pub fn with_defaults() -> Self {
47        Self::new(DEFAULT_COORD_CACHE_SIZE, DEFAULT_COORD_CACHE_TTL_MS)
48    }
49
50    /// Get the maximum capacity.
51    pub fn max_entries(&self) -> usize {
52        self.max_entries
53    }
54
55    /// Get the default TTL.
56    pub fn default_ttl_ms(&self) -> u64 {
57        self.default_ttl_ms
58    }
59
60    /// Set the default TTL.
61    pub fn set_default_ttl_ms(&mut self, ttl_ms: u64) {
62        self.default_ttl_ms = ttl_ms;
63    }
64
65    /// Insert or update a cache entry.
66    pub fn insert(&mut self, addr: NodeAddr, coords: TreeCoordinate, current_time_ms: u64) {
67        // Update existing entry if present
68        if let Some(entry) = self.entries.get_mut(&addr) {
69            entry.update(coords, current_time_ms, self.default_ttl_ms);
70            return;
71        }
72
73        // Evict if at capacity
74        if self.entries.len() >= self.max_entries {
75            self.evict_one(current_time_ms);
76        }
77
78        let entry = CacheEntry::new(coords, current_time_ms, self.default_ttl_ms);
79        self.entries.insert(addr, entry);
80    }
81
82    /// Insert or update a cache entry with path MTU information.
83    ///
84    /// Used by discovery response handling to store the discovered path MTU
85    /// alongside the target's coordinates.
86    pub fn insert_with_path_mtu(
87        &mut self,
88        addr: NodeAddr,
89        coords: TreeCoordinate,
90        current_time_ms: u64,
91        path_mtu: u16,
92    ) {
93        if let Some(entry) = self.entries.get_mut(&addr) {
94            entry.update(coords, current_time_ms, self.default_ttl_ms);
95            entry.set_path_mtu(path_mtu);
96            return;
97        }
98
99        if self.entries.len() >= self.max_entries {
100            self.evict_one(current_time_ms);
101        }
102
103        let mut entry = CacheEntry::new(coords, current_time_ms, self.default_ttl_ms);
104        entry.set_path_mtu(path_mtu);
105        self.entries.insert(addr, entry);
106    }
107
108    /// Insert with a custom TTL.
109    pub fn insert_with_ttl(
110        &mut self,
111        addr: NodeAddr,
112        coords: TreeCoordinate,
113        current_time_ms: u64,
114        ttl_ms: u64,
115    ) {
116        if let Some(entry) = self.entries.get_mut(&addr) {
117            entry.update(coords, current_time_ms, ttl_ms);
118            return;
119        }
120
121        if self.entries.len() >= self.max_entries {
122            self.evict_one(current_time_ms);
123        }
124
125        let entry = CacheEntry::new(coords, current_time_ms, ttl_ms);
126        self.entries.insert(addr, entry);
127    }
128
129    /// Look up coordinates for an address (without touching).
130    pub fn get(&self, addr: &NodeAddr, current_time_ms: u64) -> Option<&TreeCoordinate> {
131        self.entries.get(addr).and_then(|entry| {
132            if entry.is_expired(current_time_ms) {
133                None
134            } else {
135                Some(entry.coords())
136            }
137        })
138    }
139
140    /// Look up coordinates and refresh (update last_used and extend TTL).
141    pub fn get_and_touch(
142        &mut self,
143        addr: &NodeAddr,
144        current_time_ms: u64,
145    ) -> Option<&TreeCoordinate> {
146        // Check and remove if expired
147        if let Some(entry) = self.entries.get(addr)
148            && entry.is_expired(current_time_ms)
149        {
150            self.entries.remove(addr);
151            return None;
152        }
153
154        // Refresh TTL and return
155        if let Some(entry) = self.entries.get_mut(addr) {
156            entry.refresh(current_time_ms, self.default_ttl_ms);
157            Some(entry.coords())
158        } else {
159            None
160        }
161    }
162
163    /// Get the full cache entry.
164    pub fn get_entry(&self, addr: &NodeAddr) -> Option<&CacheEntry> {
165        self.entries.get(addr)
166    }
167
168    /// Remove an entry.
169    pub fn remove(&mut self, addr: &NodeAddr) -> Option<CacheEntry> {
170        self.entries.remove(addr)
171    }
172
173    /// Check if an address is cached (and not expired).
174    pub fn contains(&self, addr: &NodeAddr, current_time_ms: u64) -> bool {
175        self.get(addr, current_time_ms).is_some()
176    }
177
178    /// Number of entries (including expired).
179    pub fn len(&self) -> usize {
180        self.entries.len()
181    }
182
183    /// Check if empty.
184    pub fn is_empty(&self) -> bool {
185        self.entries.is_empty()
186    }
187
188    /// Iterate over non-expired entries.
189    pub fn iter(&self, current_time_ms: u64) -> impl Iterator<Item = (&NodeAddr, &CacheEntry)> {
190        self.entries
191            .iter()
192            .filter(move |(_, entry)| !entry.is_expired(current_time_ms))
193    }
194
195    /// Remove all expired entries.
196    pub fn purge_expired(&mut self, current_time_ms: u64) -> usize {
197        let before = self.entries.len();
198        self.entries
199            .retain(|_, entry| !entry.is_expired(current_time_ms));
200        before - self.entries.len()
201    }
202
203    /// Clear all entries.
204    pub fn clear(&mut self) {
205        self.entries.clear();
206    }
207
208    /// Evict one entry (expired first, then LRU).
209    fn evict_one(&mut self, current_time_ms: u64) {
210        // First try to evict an expired entry
211        let expired_key = self
212            .entries
213            .iter()
214            .find(|(_, e)| e.is_expired(current_time_ms))
215            .map(|(k, _)| *k);
216
217        if let Some(key) = expired_key {
218            self.entries.remove(&key);
219            return;
220        }
221
222        // Otherwise evict LRU (oldest last_used)
223        let lru_key = self
224            .entries
225            .iter()
226            .max_by_key(|(_, e)| e.idle_time(current_time_ms))
227            .map(|(k, _)| *k);
228
229        if let Some(key) = lru_key {
230            self.entries.remove(&key);
231        }
232    }
233
234    /// Get cache statistics.
235    pub fn stats(&self, current_time_ms: u64) -> CacheStats {
236        let mut expired = 0;
237        let mut total_age = 0u64;
238
239        for entry in self.entries.values() {
240            if entry.is_expired(current_time_ms) {
241                expired += 1;
242            }
243            total_age += entry.age(current_time_ms);
244        }
245
246        CacheStats {
247            entries: self.entries.len(),
248            max_entries: self.max_entries,
249            expired,
250            avg_age_ms: if self.entries.is_empty() {
251                0
252            } else {
253                total_age / self.entries.len() as u64
254            },
255        }
256    }
257}
258
259impl Default for CoordCache {
260    fn default() -> Self {
261        Self::with_defaults()
262    }
263}
264
265#[cfg(test)]
266mod tests {
267    use super::*;
268
269    fn make_node_addr(val: u8) -> NodeAddr {
270        let mut bytes = [0u8; 16];
271        bytes[0] = val;
272        NodeAddr::from_bytes(bytes)
273    }
274
275    fn make_coords(ids: &[u8]) -> TreeCoordinate {
276        TreeCoordinate::from_addrs(ids.iter().map(|&v| make_node_addr(v)).collect()).unwrap()
277    }
278
279    #[test]
280    fn test_coord_cache_basic() {
281        let mut cache = CoordCache::new(100, 1000);
282        let addr = make_node_addr(1);
283        let coords = make_coords(&[1, 0]);
284
285        cache.insert(addr, coords.clone(), 0);
286
287        assert!(cache.contains(&addr, 0));
288        assert_eq!(cache.get(&addr, 0), Some(&coords));
289        assert_eq!(cache.len(), 1);
290    }
291
292    #[test]
293    fn test_coord_cache_expiry() {
294        let mut cache = CoordCache::new(100, 1000);
295        let addr = make_node_addr(1);
296        let coords = make_coords(&[1, 0]);
297
298        cache.insert(addr, coords, 0);
299
300        assert!(cache.contains(&addr, 500));
301        assert!(!cache.contains(&addr, 1500));
302    }
303
304    #[test]
305    fn test_coord_cache_update() {
306        let mut cache = CoordCache::new(100, 1000);
307        let addr = make_node_addr(1);
308
309        cache.insert(addr, make_coords(&[1, 0]), 0);
310        cache.insert(addr, make_coords(&[1, 2, 0]), 500);
311
312        assert_eq!(cache.len(), 1);
313        let coords = cache.get(&addr, 500).unwrap();
314        assert_eq!(coords.depth(), 2);
315    }
316
317    #[test]
318    fn test_coord_cache_eviction() {
319        let mut cache = CoordCache::new(2, 10000);
320
321        let addr1 = make_node_addr(1);
322        let addr2 = make_node_addr(2);
323        let addr3 = make_node_addr(3);
324
325        cache.insert(addr1, make_coords(&[1, 0]), 0);
326        cache.insert(addr2, make_coords(&[2, 0]), 100);
327
328        // Touch addr2 to make it more recent
329        let _ = cache.get_and_touch(&addr2, 200);
330
331        // Insert addr3, should evict addr1 (LRU)
332        cache.insert(addr3, make_coords(&[3, 0]), 300);
333
334        assert!(!cache.contains(&addr1, 300));
335        assert!(cache.contains(&addr2, 300));
336        assert!(cache.contains(&addr3, 300));
337    }
338
339    #[test]
340    fn test_coord_cache_evict_expired_first() {
341        let mut cache = CoordCache::new(2, 100);
342
343        cache.insert(make_node_addr(1), make_coords(&[1, 0]), 0);
344        cache.insert(make_node_addr(2), make_coords(&[2, 0]), 50);
345
346        // At time 150, addr1 is expired, addr2 is not
347        cache.insert(make_node_addr(3), make_coords(&[3, 0]), 150);
348
349        // addr1 should be evicted (expired), not addr2 (LRU but not expired)
350        assert!(!cache.contains(&make_node_addr(1), 150));
351        assert!(cache.contains(&make_node_addr(2), 150));
352        assert!(cache.contains(&make_node_addr(3), 150));
353    }
354
355    #[test]
356    fn test_coord_cache_purge_expired() {
357        let mut cache = CoordCache::new(100, 100);
358
359        cache.insert(make_node_addr(1), make_coords(&[1, 0]), 0); // expires at 100
360        cache.insert(make_node_addr(2), make_coords(&[2, 0]), 50); // expires at 150
361        cache.insert(make_node_addr(3), make_coords(&[3, 0]), 200); // expires at 300
362
363        assert_eq!(cache.len(), 3);
364
365        let purged = cache.purge_expired(151); // both addr1 and addr2 expired
366
367        // Entry 1 and 2 expired, entry 3 still valid
368        assert_eq!(purged, 2);
369        assert_eq!(cache.len(), 1);
370        assert!(cache.contains(&make_node_addr(3), 151));
371    }
372
373    #[test]
374    fn test_coord_cache_stats() {
375        let mut cache = CoordCache::new(100, 100);
376
377        cache.insert(make_node_addr(1), make_coords(&[1, 0]), 0);
378        cache.insert(make_node_addr(2), make_coords(&[2, 0]), 50);
379
380        let stats = cache.stats(150);
381
382        assert_eq!(stats.entries, 2);
383        assert_eq!(stats.max_entries, 100);
384        assert_eq!(stats.expired, 1); // addr1 expired
385        assert!(stats.avg_age_ms > 0);
386    }
387
388    #[test]
389    fn test_coord_cache_insert_with_ttl() {
390        let mut cache = CoordCache::new(100, 1000);
391        let addr = make_node_addr(1);
392
393        cache.insert_with_ttl(addr, make_coords(&[1, 0]), 0, 200);
394
395        // Should expire at 200, not the default 1000
396        assert!(cache.contains(&addr, 100));
397        assert!(!cache.contains(&addr, 201));
398    }
399
400    #[test]
401    fn test_coord_cache_insert_with_ttl_update() {
402        let mut cache = CoordCache::new(100, 1000);
403        let addr = make_node_addr(1);
404
405        cache.insert_with_ttl(addr, make_coords(&[1, 0]), 0, 200);
406        cache.insert_with_ttl(addr, make_coords(&[1, 2, 0]), 100, 300);
407
408        assert_eq!(cache.len(), 1);
409        let coords = cache.get(&addr, 100).unwrap();
410        assert_eq!(coords.depth(), 2);
411        // New TTL: 100 + 300 = 400
412        assert!(cache.contains(&addr, 399));
413        assert!(!cache.contains(&addr, 401));
414    }
415
416    #[test]
417    fn test_coord_cache_get_and_touch_removes_expired() {
418        let mut cache = CoordCache::new(100, 100);
419        let addr = make_node_addr(1);
420
421        cache.insert(addr, make_coords(&[1, 0]), 0);
422        assert_eq!(cache.len(), 1);
423
424        // Entry expired at time 200
425        let result = cache.get_and_touch(&addr, 200);
426        assert!(result.is_none());
427        // Entry should be removed from the map
428        assert_eq!(cache.len(), 0);
429    }
430
431    #[test]
432    fn test_coord_cache_get_entry() {
433        let mut cache = CoordCache::new(100, 1000);
434        let addr = make_node_addr(1);
435
436        cache.insert(addr, make_coords(&[1, 0]), 500);
437
438        let entry = cache.get_entry(&addr).unwrap();
439        assert_eq!(entry.created_at(), 500);
440        assert_eq!(entry.expires_at(), 1500);
441
442        assert!(cache.get_entry(&make_node_addr(99)).is_none());
443    }
444
445    #[test]
446    fn test_coord_cache_remove() {
447        let mut cache = CoordCache::new(100, 1000);
448        let addr = make_node_addr(1);
449
450        cache.insert(addr, make_coords(&[1, 0]), 0);
451        assert_eq!(cache.len(), 1);
452
453        let removed = cache.remove(&addr);
454        assert!(removed.is_some());
455        assert_eq!(cache.len(), 0);
456
457        // Removing again returns None
458        assert!(cache.remove(&addr).is_none());
459    }
460
461    #[test]
462    fn test_coord_cache_clear_and_is_empty() {
463        let mut cache = CoordCache::new(100, 1000);
464
465        assert!(cache.is_empty());
466
467        cache.insert(make_node_addr(1), make_coords(&[1, 0]), 0);
468        cache.insert(make_node_addr(2), make_coords(&[2, 0]), 0);
469
470        assert!(!cache.is_empty());
471
472        cache.clear();
473        assert!(cache.is_empty());
474        assert_eq!(cache.len(), 0);
475    }
476
477    #[test]
478    fn test_coord_cache_default() {
479        let cache = CoordCache::default();
480
481        assert_eq!(cache.max_entries(), DEFAULT_COORD_CACHE_SIZE);
482        assert_eq!(cache.default_ttl_ms(), DEFAULT_COORD_CACHE_TTL_MS);
483        assert!(cache.is_empty());
484    }
485
486    #[test]
487    fn test_coord_cache_set_default_ttl() {
488        let mut cache = CoordCache::new(100, 1000);
489        let addr = make_node_addr(1);
490
491        cache.set_default_ttl_ms(200);
492        assert_eq!(cache.default_ttl_ms(), 200);
493
494        cache.insert(addr, make_coords(&[1, 0]), 0);
495        // New TTL applies: expires at 200
496        assert!(cache.contains(&addr, 100));
497        assert!(!cache.contains(&addr, 201));
498    }
499
500    #[test]
501    fn test_coord_cache_stats_empty() {
502        let cache = CoordCache::new(100, 1000);
503        let stats = cache.stats(0);
504
505        assert_eq!(stats.entries, 0);
506        assert_eq!(stats.max_entries, 100);
507        assert_eq!(stats.expired, 0);
508        assert_eq!(stats.avg_age_ms, 0);
509    }
510}