chie-core 0.2.0

Core protocol logic for CHIE Protocol
Documentation
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! Intelligent peer selection module for optimizing content delivery.
//!
//! This module provides smart peer selection algorithms that combine multiple
//! factors including reputation scores, network quality, load balancing, and
//! geographical proximity to select the best peers for content requests.
//!
//! # Example
//!
//! ```
//! use chie_core::{PeerSelector, SelectionStrategy, PeerCandidate};
//!
//! # async fn example() {
//! let mut selector = PeerSelector::new();
//!
//! // Add peer candidates with various metrics
//! selector.add_candidate(PeerCandidate {
//!     peer_id: "peer1".to_string(),
//!     reputation_score: 0.95,
//!     network_health: 0.90,
//!     current_load: 0.3,
//!     latency_ms: 50.0,
//!     bandwidth_mbps: 100.0,
//!     distance_km: Some(100.0),
//!     last_seen: std::time::SystemTime::now(),
//! });
//!
//! // Select the best peer using weighted scoring
//! if let Some(best_peer) = selector.select_best() {
//!     println!("Selected peer: {}", best_peer.peer_id);
//! }
//!
//! // Get top N peers for redundancy
//! let top_peers = selector.select_top_n(3);
//! # }
//! ```

use std::collections::HashMap;
use std::time::{Duration, SystemTime};

/// Represents a peer candidate for content delivery.
#[derive(Debug, Clone)]
pub struct PeerCandidate {
    /// Unique peer identifier
    pub peer_id: String,
    /// Reputation score (0.0 to 1.0)
    pub reputation_score: f64,
    /// Network health score (0.0 to 1.0)
    pub network_health: f64,
    /// Current load percentage (0.0 to 1.0)
    pub current_load: f64,
    /// Average latency in milliseconds
    pub latency_ms: f64,
    /// Available bandwidth in Mbps
    pub bandwidth_mbps: f64,
    /// Geographic distance in kilometers (if known)
    pub distance_km: Option<f64>,
    /// Last seen timestamp
    pub last_seen: SystemTime,
}

/// Peer selection strategy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SelectionStrategy {
    /// Select the single best peer based on weighted score
    Best,
    /// Weighted random selection (higher scores more likely)
    WeightedRandom,
    /// Round-robin among top peers
    RoundRobin,
    /// Least loaded peer
    LeastLoaded,
    /// Lowest latency peer
    LowestLatency,
}

/// Weights for different factors in peer selection.
#[derive(Debug, Clone)]
pub struct SelectionWeights {
    /// Weight for reputation score (default: 0.3)
    pub reputation: f64,
    /// Weight for network health (default: 0.25)
    pub network_health: f64,
    /// Weight for load (inverted - lower is better) (default: 0.2)
    pub load: f64,
    /// Weight for latency (inverted - lower is better) (default: 0.15)
    pub latency: f64,
    /// Weight for bandwidth (default: 0.1)
    pub bandwidth: f64,
    /// Weight for distance (inverted - closer is better) (default: 0.0)
    pub distance: f64,
}

impl Default for SelectionWeights {
    fn default() -> Self {
        Self {
            reputation: 0.3,
            network_health: 0.25,
            load: 0.2,
            latency: 0.15,
            bandwidth: 0.1,
            distance: 0.0,
        }
    }
}

/// Peer selector for intelligent peer ranking and selection.
pub struct PeerSelector {
    candidates: Vec<PeerCandidate>,
    weights: SelectionWeights,
    strategy: SelectionStrategy,
    round_robin_index: usize,
    peer_request_counts: HashMap<String, u64>,
    stale_threshold: Duration,
}

impl PeerSelector {
    /// Create a new peer selector with default settings.
    #[must_use]
    #[inline]
    pub fn new() -> Self {
        Self {
            candidates: Vec::new(),
            weights: SelectionWeights::default(),
            strategy: SelectionStrategy::Best,
            round_robin_index: 0,
            peer_request_counts: HashMap::new(),
            stale_threshold: Duration::from_secs(300), // 5 minutes
        }
    }

    /// Create a peer selector with custom weights.
    #[must_use]
    #[inline]
    pub fn with_weights(weights: SelectionWeights) -> Self {
        Self {
            weights,
            ..Self::new()
        }
    }

    /// Set the selection strategy.
    #[inline]
    pub fn set_strategy(&mut self, strategy: SelectionStrategy) {
        self.strategy = strategy;
    }

    /// Set the stale threshold for removing old peers.
    #[inline]
    pub fn set_stale_threshold(&mut self, threshold: Duration) {
        self.stale_threshold = threshold;
    }

    /// Add a peer candidate.
    pub fn add_candidate(&mut self, candidate: PeerCandidate) {
        // Remove existing entry for this peer if present
        self.candidates.retain(|c| c.peer_id != candidate.peer_id);
        self.candidates.push(candidate);
    }

    /// Remove a peer candidate.
    #[inline]
    pub fn remove_candidate(&mut self, peer_id: &str) {
        self.candidates.retain(|c| c.peer_id != peer_id);
        self.peer_request_counts.remove(peer_id);
    }

    /// Remove stale peers based on last_seen timestamp.
    pub fn remove_stale_peers(&mut self) -> usize {
        let now = SystemTime::now();
        let initial_count = self.candidates.len();

        self.candidates.retain(|c| {
            if let Ok(duration) = now.duration_since(c.last_seen) {
                duration < self.stale_threshold
            } else {
                true // Keep if we can't determine age
            }
        });

        initial_count - self.candidates.len()
    }

    /// Calculate weighted score for a peer.
    #[inline]
    fn calculate_score(&self, peer: &PeerCandidate) -> f64 {
        let mut score = 0.0;

        // Add reputation component
        score += peer.reputation_score * self.weights.reputation;

        // Add network health component
        score += peer.network_health * self.weights.network_health;

        // Add load component (inverted - lower load is better)
        score += (1.0 - peer.current_load) * self.weights.load;

        // Add latency component (inverted - lower latency is better)
        // Normalize latency: assume 0-500ms range
        let normalized_latency = 1.0 - (peer.latency_ms.min(500.0) / 500.0);
        score += normalized_latency * self.weights.latency;

        // Add bandwidth component
        // Normalize bandwidth: assume 0-1000 Mbps range
        let normalized_bandwidth = peer.bandwidth_mbps.min(1000.0) / 1000.0;
        score += normalized_bandwidth * self.weights.bandwidth;

        // Add distance component if available (inverted - closer is better)
        if let Some(distance) = peer.distance_km {
            // Normalize distance: assume 0-10000km range
            let normalized_distance = 1.0 - (distance.min(10000.0) / 10000.0);
            score += normalized_distance * self.weights.distance;
        }

        score
    }

    /// Select the best peer based on the current strategy.
    #[must_use]
    pub fn select_best(&mut self) -> Option<PeerCandidate> {
        if self.candidates.is_empty() {
            return None;
        }

        match self.strategy {
            SelectionStrategy::Best => self.select_highest_score(),
            SelectionStrategy::WeightedRandom => self.select_weighted_random(),
            SelectionStrategy::RoundRobin => self.select_round_robin(),
            SelectionStrategy::LeastLoaded => self.select_least_loaded(),
            SelectionStrategy::LowestLatency => self.select_lowest_latency(),
        }
    }

    /// Select the peer with the highest score.
    fn select_highest_score(&mut self) -> Option<PeerCandidate> {
        let mut scored: Vec<_> = self
            .candidates
            .iter()
            .map(|c| (c.clone(), self.calculate_score(c)))
            .collect();

        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        scored.first().map(|(peer, _)| {
            *self
                .peer_request_counts
                .entry(peer.peer_id.clone())
                .or_insert(0) += 1;
            peer.clone()
        })
    }

    /// Select a peer using weighted random selection.
    fn select_weighted_random(&mut self) -> Option<PeerCandidate> {
        use rand::RngExt as _;

        let scores: Vec<_> = self
            .candidates
            .iter()
            .map(|c| self.calculate_score(c))
            .collect();

        let total_score: f64 = scores.iter().sum();
        if total_score == 0.0 {
            return self.candidates.first().cloned();
        }

        let mut rng = rand::rng();
        let mut random_value = rng.random_range(0.0..total_score);

        for (i, score) in scores.iter().enumerate() {
            random_value -= score;
            if random_value <= 0.0 {
                let peer = self.candidates[i].clone();
                *self
                    .peer_request_counts
                    .entry(peer.peer_id.clone())
                    .or_insert(0) += 1;
                return Some(peer);
            }
        }

        // Fallback to last candidate
        self.candidates.last().cloned()
    }

    /// Select the next peer in round-robin order.
    fn select_round_robin(&mut self) -> Option<PeerCandidate> {
        if self.candidates.is_empty() {
            return None;
        }

        let peer = self.candidates[self.round_robin_index % self.candidates.len()].clone();
        self.round_robin_index = (self.round_robin_index + 1) % self.candidates.len();
        *self
            .peer_request_counts
            .entry(peer.peer_id.clone())
            .or_insert(0) += 1;
        Some(peer)
    }

    /// Select the peer with the lowest current load.
    fn select_least_loaded(&mut self) -> Option<PeerCandidate> {
        let mut sorted = self.candidates.clone();
        sorted.sort_by(|a, b| {
            a.current_load
                .partial_cmp(&b.current_load)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        sorted.first().map(|peer| {
            *self
                .peer_request_counts
                .entry(peer.peer_id.clone())
                .or_insert(0) += 1;
            peer.clone()
        })
    }

    /// Select the peer with the lowest latency.
    fn select_lowest_latency(&mut self) -> Option<PeerCandidate> {
        let mut sorted = self.candidates.clone();
        sorted.sort_by(|a, b| {
            a.latency_ms
                .partial_cmp(&b.latency_ms)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        sorted.first().map(|peer| {
            *self
                .peer_request_counts
                .entry(peer.peer_id.clone())
                .or_insert(0) += 1;
            peer.clone()
        })
    }

    /// Select the top N peers based on score.
    #[must_use]
    #[inline]
    pub fn select_top_n(&self, n: usize) -> Vec<PeerCandidate> {
        let mut scored: Vec<_> = self
            .candidates
            .iter()
            .map(|c| (c.clone(), self.calculate_score(c)))
            .collect();

        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        scored.into_iter().take(n).map(|(peer, _)| peer).collect()
    }

    /// Get peers with score above a threshold.
    #[must_use]
    #[inline]
    pub fn get_qualified_peers(&self, min_score: f64) -> Vec<PeerCandidate> {
        self.candidates
            .iter()
            .filter(|c| self.calculate_score(c) >= min_score)
            .cloned()
            .collect()
    }

    /// Get the number of candidates.
    #[must_use]
    #[inline]
    pub fn candidate_count(&self) -> usize {
        self.candidates.len()
    }

    /// Get all candidates.
    #[must_use]
    #[inline]
    pub fn candidates(&self) -> &[PeerCandidate] {
        &self.candidates
    }

    /// Clear all candidates.
    #[inline]
    pub fn clear(&mut self) {
        self.candidates.clear();
        self.peer_request_counts.clear();
        self.round_robin_index = 0;
    }

    /// Get request count for a peer.
    #[must_use]
    #[inline]
    pub fn get_request_count(&self, peer_id: &str) -> u64 {
        self.peer_request_counts.get(peer_id).copied().unwrap_or(0)
    }

    /// Get statistics about peer selection.
    #[must_use]
    #[inline]
    pub fn get_statistics(&self) -> PeerSelectionStats {
        if self.candidates.is_empty() {
            return PeerSelectionStats::default();
        }

        let scores: Vec<f64> = self
            .candidates
            .iter()
            .map(|c| self.calculate_score(c))
            .collect();

        let total_score: f64 = scores.iter().sum();
        let avg_score = total_score / scores.len() as f64;
        let max_score = scores.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
        let min_score = scores.iter().cloned().fold(f64::INFINITY, f64::min);

        let total_requests: u64 = self.peer_request_counts.values().sum();

        PeerSelectionStats {
            total_candidates: self.candidates.len(),
            average_score: avg_score,
            max_score,
            min_score,
            total_requests,
        }
    }
}

impl Default for PeerSelector {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics about peer selection.
#[derive(Debug, Clone, Default)]
pub struct PeerSelectionStats {
    /// Total number of peer candidates
    pub total_candidates: usize,
    /// Average score across all candidates
    pub average_score: f64,
    /// Maximum score among candidates
    pub max_score: f64,
    /// Minimum score among candidates
    pub min_score: f64,
    /// Total number of selection requests
    pub total_requests: u64,
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_peer(peer_id: &str, reputation: f64, health: f64, load: f64) -> PeerCandidate {
        PeerCandidate {
            peer_id: peer_id.to_string(),
            reputation_score: reputation,
            network_health: health,
            current_load: load,
            latency_ms: 100.0,
            bandwidth_mbps: 100.0,
            distance_km: None,
            last_seen: SystemTime::now(),
        }
    }

    #[test]
    fn test_peer_selection_best_strategy() {
        let mut selector = PeerSelector::new();

        selector.add_candidate(create_test_peer("peer1", 0.5, 0.5, 0.8));
        selector.add_candidate(create_test_peer("peer2", 0.9, 0.9, 0.2));
        selector.add_candidate(create_test_peer("peer3", 0.7, 0.7, 0.5));

        selector.set_strategy(SelectionStrategy::Best);
        let best = selector.select_best().unwrap();
        assert_eq!(best.peer_id, "peer2");
    }

    #[test]
    fn test_peer_selection_least_loaded() {
        let mut selector = PeerSelector::new();

        selector.add_candidate(create_test_peer("peer1", 0.9, 0.9, 0.8));
        selector.add_candidate(create_test_peer("peer2", 0.5, 0.5, 0.1));
        selector.add_candidate(create_test_peer("peer3", 0.7, 0.7, 0.5));

        selector.set_strategy(SelectionStrategy::LeastLoaded);
        let best = selector.select_best().unwrap();
        assert_eq!(best.peer_id, "peer2");
    }

    #[test]
    fn test_peer_selection_top_n() {
        let mut selector = PeerSelector::new();

        selector.add_candidate(create_test_peer("peer1", 0.5, 0.5, 0.8));
        selector.add_candidate(create_test_peer("peer2", 0.9, 0.9, 0.2));
        selector.add_candidate(create_test_peer("peer3", 0.7, 0.7, 0.5));

        let top_2 = selector.select_top_n(2);
        assert_eq!(top_2.len(), 2);
        assert_eq!(top_2[0].peer_id, "peer2");
        assert_eq!(top_2[1].peer_id, "peer3");
    }

    #[test]
    fn test_peer_selection_round_robin() {
        let mut selector = PeerSelector::new();

        selector.add_candidate(create_test_peer("peer1", 0.5, 0.5, 0.5));
        selector.add_candidate(create_test_peer("peer2", 0.5, 0.5, 0.5));
        selector.add_candidate(create_test_peer("peer3", 0.5, 0.5, 0.5));

        selector.set_strategy(SelectionStrategy::RoundRobin);

        assert_eq!(selector.select_best().unwrap().peer_id, "peer1");
        assert_eq!(selector.select_best().unwrap().peer_id, "peer2");
        assert_eq!(selector.select_best().unwrap().peer_id, "peer3");
        assert_eq!(selector.select_best().unwrap().peer_id, "peer1");
    }

    #[test]
    fn test_remove_candidate() {
        let mut selector = PeerSelector::new();

        selector.add_candidate(create_test_peer("peer1", 0.5, 0.5, 0.5));
        selector.add_candidate(create_test_peer("peer2", 0.5, 0.5, 0.5));

        assert_eq!(selector.candidate_count(), 2);

        selector.remove_candidate("peer1");
        assert_eq!(selector.candidate_count(), 1);
        assert_eq!(selector.candidates()[0].peer_id, "peer2");
    }

    #[test]
    fn test_custom_weights() {
        let weights = SelectionWeights {
            reputation: 1.0,
            network_health: 0.0,
            load: 0.0,
            latency: 0.0,
            bandwidth: 0.0,
            distance: 0.0,
        };

        let mut selector = PeerSelector::with_weights(weights);

        selector.add_candidate(create_test_peer("peer1", 0.5, 1.0, 0.0));
        selector.add_candidate(create_test_peer("peer2", 1.0, 0.0, 1.0));

        selector.set_strategy(SelectionStrategy::Best);
        let best = selector.select_best().unwrap();
        assert_eq!(best.peer_id, "peer2"); // Higher reputation
    }

    #[test]
    fn test_qualified_peers() {
        let mut selector = PeerSelector::new();

        selector.add_candidate(create_test_peer("peer1", 0.3, 0.3, 0.9));
        selector.add_candidate(create_test_peer("peer2", 0.9, 0.9, 0.1));
        selector.add_candidate(create_test_peer("peer3", 0.7, 0.7, 0.5));

        let qualified = selector.get_qualified_peers(0.5);
        assert!(qualified.len() >= 2);
    }

    #[test]
    fn test_statistics() {
        let mut selector = PeerSelector::new();

        selector.add_candidate(create_test_peer("peer1", 0.5, 0.5, 0.5));
        selector.add_candidate(create_test_peer("peer2", 0.9, 0.9, 0.2));
        selector.add_candidate(create_test_peer("peer3", 0.7, 0.7, 0.5));

        let _ = selector.select_best();
        let _ = selector.select_best();

        let stats = selector.get_statistics();
        assert_eq!(stats.total_candidates, 3);
        assert!(stats.average_score > 0.0);
        assert_eq!(stats.total_requests, 2);
    }

    #[test]
    fn test_stale_peer_removal() {
        let mut selector = PeerSelector::new();
        selector.set_stale_threshold(Duration::from_secs(1));

        let mut old_peer = create_test_peer("peer1", 0.5, 0.5, 0.5);
        old_peer.last_seen = SystemTime::now() - Duration::from_secs(5);

        selector.add_candidate(old_peer);
        selector.add_candidate(create_test_peer("peer2", 0.5, 0.5, 0.5));

        assert_eq!(selector.candidate_count(), 2);

        let removed = selector.remove_stale_peers();
        assert_eq!(removed, 1);
        assert_eq!(selector.candidate_count(), 1);
    }

    #[test]
    fn test_request_counting() {
        let mut selector = PeerSelector::new();

        selector.add_candidate(create_test_peer("peer1", 0.9, 0.9, 0.1));

        selector.set_strategy(SelectionStrategy::Best);
        let _ = selector.select_best();
        let _ = selector.select_best();
        let _ = selector.select_best();

        assert_eq!(selector.get_request_count("peer1"), 3);
        assert_eq!(selector.get_request_count("peer2"), 0);
    }
}