1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Content Routing Optimizer
//!
//! Optimizes content routing decisions by maintaining a cost model for fetching
//! blocks from different peers, caching routing decisions, and detecting when
//! cached routes have become stale.
use std::collections::HashMap;
/// Cost model for fetching content from a specific peer.
#[derive(Debug, Clone)]
pub struct RouteCost {
/// Peer identifier
pub peer_id: String,
/// Round-trip latency in milliseconds
pub latency_ms: f64,
/// Available bandwidth in bytes per second
pub bandwidth_bps: u64,
/// Peer reliability score in range [0.0, 1.0]
pub reliability: f64,
}
impl RouteCost {
/// Estimated fetch cost in milliseconds for the given payload size.
///
/// Formula: `(size_bytes / bandwidth_bps) * 1000 + latency_ms`
pub fn fetch_cost(&self, size_bytes: u64) -> f64 {
let transfer_ms = size_bytes as f64 / (self.bandwidth_bps.max(1) as f64) * 1000.0;
transfer_ms + self.latency_ms
}
/// Weighted quality score — higher is better.
///
/// Formula: `reliability / fetch_cost(65536).max(1e-9)`
pub fn weighted_score(&self) -> f64 {
self.reliability / self.fetch_cost(65536).max(1e-9)
}
}
/// A cached routing entry for a single CID.
#[derive(Debug, Clone)]
pub struct RouteEntry {
/// Content identifier this entry is for
pub cid: String,
/// Candidate peers sorted by `weighted_score` descending
pub candidates: Vec<RouteCost>,
/// Unix timestamp (seconds) at which this entry was last updated
pub cached_at_secs: u64,
/// Number of times this entry has been served from cache
pub hit_count: u64,
}
impl RouteEntry {
/// Returns a reference to the best (lowest-cost, highest-reliability) peer,
/// or `None` if there are no candidates.
pub fn best_peer(&self) -> Option<&RouteCost> {
self.candidates.first()
}
/// Returns `true` when the entry has exceeded its time-to-live.
pub fn is_stale(&self, ttl_secs: u64, now_secs: u64) -> bool {
now_secs.saturating_sub(self.cached_at_secs) >= ttl_secs
}
}
/// Aggregate statistics for the routing optimizer.
#[derive(Debug, Clone, Default)]
pub struct RoutingStats {
/// Number of lookups that were served from cache
pub cache_hits: u64,
/// Number of lookups that were not in cache (or were stale)
pub cache_misses: u64,
/// Cumulative number of stale entries that have been evicted
pub stale_routes: u64,
/// Current number of entries held in the cache
pub total_routes: usize,
}
impl RoutingStats {
/// Fraction of total lookups that were cache hits.
///
/// Returns `0.0` when no lookups have been performed yet.
pub fn hit_rate(&self) -> f64 {
let total = self.cache_hits + self.cache_misses;
if total == 0 {
0.0
} else {
self.cache_hits as f64 / total as f64
}
}
}
/// Configuration knobs for the [`ContentRoutingOptimizer`].
#[derive(Debug, Clone)]
pub struct OptimizerConfig {
/// Maximum age of a cached route before it is considered stale (seconds)
pub route_ttl_secs: u64,
/// Maximum number of candidate peers stored per CID
pub max_candidates_per_cid: usize,
/// Minimum reliability score for a peer to be admitted as a candidate
pub min_reliability: f64,
}
impl Default for OptimizerConfig {
fn default() -> Self {
Self {
route_ttl_secs: 300,
max_candidates_per_cid: 5,
min_reliability: 0.3,
}
}
}
/// Content routing optimizer that caches and manages peer routing decisions.
pub struct ContentRoutingOptimizer {
/// Routing table keyed by CID string
routes: HashMap<String, RouteEntry>,
/// Optimizer configuration
config: OptimizerConfig,
/// Running statistics
stats: RoutingStats,
}
impl ContentRoutingOptimizer {
/// Creates a new optimizer with the supplied configuration.
pub fn new(config: OptimizerConfig) -> Self {
Self {
routes: HashMap::new(),
config,
stats: RoutingStats::default(),
}
}
/// Adds (or replaces) a routing entry for `cid`.
///
/// Candidates that do not meet the `min_reliability` threshold are
/// discarded before insertion. The survivors are sorted by
/// [`RouteCost::weighted_score`] descending and truncated to
/// `max_candidates_per_cid`.
pub fn add_route(&mut self, cid: String, candidates: Vec<RouteCost>, now_secs: u64) {
let mut filtered: Vec<RouteCost> = candidates
.into_iter()
.filter(|c| c.reliability >= self.config.min_reliability)
.collect();
filtered.sort_by(|a, b| {
b.weighted_score()
.partial_cmp(&a.weighted_score())
.unwrap_or(std::cmp::Ordering::Equal)
});
filtered.truncate(self.config.max_candidates_per_cid);
let entry = RouteEntry {
cid: cid.clone(),
candidates: filtered,
cached_at_secs: now_secs,
hit_count: 0,
};
self.routes.insert(cid, entry);
self.stats.total_routes = self.routes.len();
}
/// Returns the best peer for `cid`, updating cache statistics.
///
/// Returns `None` when:
/// - The entry does not exist (cache miss).
/// - The entry is stale — the entry is removed and counted as a stale
/// eviction plus a cache miss.
pub fn best_route(&mut self, cid: &str, now_secs: u64) -> Option<&RouteCost> {
let ttl = self.config.route_ttl_secs;
if let Some(entry) = self.routes.get(cid) {
if entry.is_stale(ttl, now_secs) {
self.routes.remove(cid);
self.stats.stale_routes += 1;
self.stats.cache_misses += 1;
self.stats.total_routes = self.routes.len();
return None;
}
} else {
self.stats.cache_misses += 1;
return None;
}
// Entry is present and fresh — record hit.
let entry = self.routes.get_mut(cid)?;
self.stats.cache_hits += 1;
entry.hit_count += 1;
// Re-borrow immutably to return a reference with the right lifetime.
self.routes.get(cid).and_then(|e| e.best_peer())
}
/// Evicts all stale entries from the cache.
///
/// Returns the number of entries that were removed.
pub fn evict_stale(&mut self, now_secs: u64) -> usize {
let ttl = self.config.route_ttl_secs;
let before = self.routes.len();
self.routes
.retain(|_, entry| !entry.is_stale(ttl, now_secs));
let removed = before - self.routes.len();
self.stats.stale_routes += removed as u64;
self.stats.total_routes = self.routes.len();
removed
}
/// Updates the latency and reliability for `peer_id` across **all**
/// cached routing entries, then re-sorts each affected entry's candidates.
pub fn update_peer_cost(&mut self, peer_id: &str, new_latency_ms: f64, new_reliability: f64) {
for entry in self.routes.values_mut() {
let mut changed = false;
for candidate in &mut entry.candidates {
if candidate.peer_id == peer_id {
candidate.latency_ms = new_latency_ms;
candidate.reliability = new_reliability;
changed = true;
}
}
if changed {
entry.candidates.sort_by(|a, b| {
b.weighted_score()
.partial_cmp(&a.weighted_score())
.unwrap_or(std::cmp::Ordering::Equal)
});
}
}
}
/// Returns a reference to the current routing statistics.
pub fn stats(&self) -> &RoutingStats {
&self.stats
}
/// Returns the number of entries currently held in the route cache.
pub fn route_count(&self) -> usize {
self.routes.len()
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
fn make_peer(
peer_id: &str,
latency_ms: f64,
bandwidth_bps: u64,
reliability: f64,
) -> RouteCost {
RouteCost {
peer_id: peer_id.to_string(),
latency_ms,
bandwidth_bps,
reliability,
}
}
fn default_optimizer() -> ContentRoutingOptimizer {
ContentRoutingOptimizer::new(OptimizerConfig::default())
}
// ── 1. new() starts empty ─────────────────────────────────────────────────
#[test]
fn test_new_empty() {
let opt = default_optimizer();
assert_eq!(opt.route_count(), 0);
assert_eq!(opt.stats().cache_hits, 0);
assert_eq!(opt.stats().cache_misses, 0);
}
// ── 2. fetch_cost calculation ─────────────────────────────────────────────
#[test]
fn test_fetch_cost_calculation() {
let peer = make_peer("p1", 10.0, 1_000_000, 1.0); // 1 MB/s
// 65536 bytes @ 1 MB/s = 65.536 ms + 10 ms = 75.536 ms
let expected = 65536.0 / 1_000_000.0 * 1000.0 + 10.0;
assert!((peer.fetch_cost(65536) - expected).abs() < 1e-9);
}
// ── 3. weighted_score higher for reliable/fast peer ──────────────────────
#[test]
fn test_weighted_score_fast_beats_slow() {
let fast = make_peer("fast", 5.0, 10_000_000, 0.9);
let slow = make_peer("slow", 200.0, 100_000, 0.9);
assert!(fast.weighted_score() > slow.weighted_score());
}
// ── 4. add_route: filters by min_reliability ─────────────────────────────
#[test]
fn test_add_route_filters_by_min_reliability() {
let mut opt = ContentRoutingOptimizer::new(OptimizerConfig {
min_reliability: 0.5,
..Default::default()
});
let candidates = vec![
make_peer("good", 10.0, 1_000_000, 0.8),
make_peer("bad", 10.0, 1_000_000, 0.2),
];
opt.add_route("cid1".to_string(), candidates, 1000);
let entry = opt.routes.get("cid1").expect("entry must exist");
assert_eq!(entry.candidates.len(), 1);
assert_eq!(entry.candidates[0].peer_id, "good");
}
// ── 5. add_route: sorts by weighted_score descending ─────────────────────
#[test]
fn test_add_route_sorts_by_weighted_score() {
let mut opt = default_optimizer();
let candidates = vec![
make_peer("slow", 500.0, 100_000, 0.9),
make_peer("fast", 5.0, 10_000_000, 0.9),
];
opt.add_route("cid1".to_string(), candidates, 1000);
let entry = opt.routes.get("cid1").expect("entry must exist");
assert_eq!(entry.candidates[0].peer_id, "fast");
}
// ── 6. add_route: truncates to max_candidates ─────────────────────────────
#[test]
fn test_add_route_truncates_to_max_candidates() {
let mut opt = ContentRoutingOptimizer::new(OptimizerConfig {
max_candidates_per_cid: 3,
..Default::default()
});
let candidates: Vec<RouteCost> = (0..10)
.map(|i| make_peer(&format!("p{i}"), 10.0, 1_000_000, 0.9))
.collect();
opt.add_route("cid1".to_string(), candidates, 1000);
assert_eq!(opt.routes["cid1"].candidates.len(), 3);
}
// ── 7. best_route: cache hit increments stats ─────────────────────────────
#[test]
fn test_best_route_cache_hit_increments_stats() {
let mut opt = default_optimizer();
opt.add_route(
"cid1".to_string(),
vec![make_peer("p1", 10.0, 1_000_000, 0.9)],
1000,
);
let result = opt.best_route("cid1", 1000);
assert!(result.is_some());
assert_eq!(opt.stats().cache_hits, 1);
assert_eq!(opt.stats().cache_misses, 0);
assert_eq!(opt.routes["cid1"].hit_count, 1);
}
// ── 8. best_route: stale entry is removed ────────────────────────────────
#[test]
fn test_best_route_stale_entry_removed() {
let mut opt = ContentRoutingOptimizer::new(OptimizerConfig {
route_ttl_secs: 60,
..Default::default()
});
opt.add_route(
"cid1".to_string(),
vec![make_peer("p1", 10.0, 1_000_000, 0.9)],
0,
);
// Query 120 seconds later — beyond TTL
let result = opt.best_route("cid1", 120);
assert!(result.is_none());
assert_eq!(opt.stats().stale_routes, 1);
assert_eq!(opt.stats().cache_misses, 1);
assert_eq!(opt.route_count(), 0);
}
// ── 9. best_route: not found increments misses ───────────────────────────
#[test]
fn test_best_route_not_found_increments_misses() {
let mut opt = default_optimizer();
let result = opt.best_route("nonexistent", 1000);
assert!(result.is_none());
assert_eq!(opt.stats().cache_misses, 1);
assert_eq!(opt.stats().cache_hits, 0);
}
// ── 10. evict_stale removes expired entries ───────────────────────────────
#[test]
fn test_evict_stale_removes_expired_entries() {
let mut opt = ContentRoutingOptimizer::new(OptimizerConfig {
route_ttl_secs: 60,
..Default::default()
});
opt.add_route(
"fresh".to_string(),
vec![make_peer("p1", 5.0, 1_000_000, 0.9)],
100,
);
opt.add_route(
"stale".to_string(),
vec![make_peer("p2", 5.0, 1_000_000, 0.9)],
0,
);
let removed = opt.evict_stale(120);
assert_eq!(removed, 1);
assert!(opt.routes.contains_key("fresh"));
assert!(!opt.routes.contains_key("stale"));
}
// ── 11. evict_stale returns correct count ─────────────────────────────────
#[test]
fn test_evict_stale_returns_count() {
let mut opt = ContentRoutingOptimizer::new(OptimizerConfig {
route_ttl_secs: 60,
..Default::default()
});
for i in 0..5_u64 {
opt.add_route(
format!("cid{i}"),
vec![make_peer("p", 5.0, 1_000_000, 0.9)],
0,
);
}
let removed = opt.evict_stale(120);
assert_eq!(removed, 5);
assert_eq!(opt.route_count(), 0);
}
// ── 12. update_peer_cost updates all affected routes ─────────────────────
#[test]
fn test_update_peer_cost_updates_all_routes() {
let mut opt = default_optimizer();
opt.add_route(
"cid1".to_string(),
vec![make_peer("target", 100.0, 500_000, 0.8)],
0,
);
opt.add_route(
"cid2".to_string(),
vec![make_peer("target", 100.0, 500_000, 0.8)],
0,
);
opt.update_peer_cost("target", 10.0, 0.95);
for entry in opt.routes.values() {
let c = entry
.candidates
.iter()
.find(|c| c.peer_id == "target")
.expect("should exist");
assert!((c.latency_ms - 10.0).abs() < 1e-9);
assert!((c.reliability - 0.95).abs() < 1e-9);
}
}
// ── 13. update_peer_cost re-sorts candidates ──────────────────────────────
#[test]
fn test_update_peer_cost_resorts_candidates() {
let mut opt = default_optimizer();
// Same bandwidth so latency/reliability dominate the score.
// p_poor starts with high latency (above min_reliability floor); p_good is currently first.
let candidates = vec![
make_peer("p_poor", 100.0, 10_000_000, 0.4),
make_peer("p_good", 5.0, 10_000_000, 0.9),
];
opt.add_route("cid1".to_string(), candidates, 0);
// p_good is first; now dramatically boost p_poor: latency=1ms, reliability=1.0
opt.update_peer_cost("p_poor", 1.0, 1.0);
// After update the re-sort should place p_poor first
let entry = opt.routes.get("cid1").expect("entry must exist");
assert_eq!(entry.candidates[0].peer_id, "p_poor");
}
// ── 14. is_stale: fresh entry ─────────────────────────────────────────────
#[test]
fn test_is_stale_fresh() {
let entry = RouteEntry {
cid: "c".to_string(),
candidates: vec![],
cached_at_secs: 1000,
hit_count: 0,
};
assert!(!entry.is_stale(300, 1200)); // 200s elapsed < 300s TTL
}
// ── 15. is_stale: stale entry ─────────────────────────────────────────────
#[test]
fn test_is_stale_stale() {
let entry = RouteEntry {
cid: "c".to_string(),
candidates: vec![],
cached_at_secs: 1000,
hit_count: 0,
};
assert!(entry.is_stale(300, 1400)); // 400s elapsed >= 300s TTL
}
// ── 16. hit_rate calculation ──────────────────────────────────────────────
#[test]
fn test_hit_rate_calculation() {
let stats = RoutingStats {
cache_hits: 3,
cache_misses: 1,
..Default::default()
};
assert!((stats.hit_rate() - 0.75).abs() < 1e-9);
}
// ── 16b. hit_rate zero when no lookups ───────────────────────────────────
#[test]
fn test_hit_rate_zero_no_lookups() {
let stats = RoutingStats::default();
assert_eq!(stats.hit_rate(), 0.0);
}
// ── 17. best_peer returns first candidate ─────────────────────────────────
#[test]
fn test_best_peer_first_candidate() {
let entry = RouteEntry {
cid: "c".to_string(),
candidates: vec![
make_peer("p_best", 5.0, 10_000_000, 0.9),
make_peer("p_second", 50.0, 1_000_000, 0.8),
],
cached_at_secs: 0,
hit_count: 0,
};
assert_eq!(
entry.best_peer().map(|p| p.peer_id.as_str()),
Some("p_best")
);
}
// ── 18. route_count is correct ────────────────────────────────────────────
#[test]
fn test_route_count_correct() {
let mut opt = default_optimizer();
assert_eq!(opt.route_count(), 0);
opt.add_route(
"c1".to_string(),
vec![make_peer("p", 5.0, 1_000_000, 0.9)],
0,
);
assert_eq!(opt.route_count(), 1);
opt.add_route(
"c2".to_string(),
vec![make_peer("p", 5.0, 1_000_000, 0.9)],
0,
);
assert_eq!(opt.route_count(), 2);
}
}