ipfrs-transport 0.2.0

Transport protocols and zero-copy data exchange for IPFRS distributed system
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Content Routing Integration
//!
//! Provides DHT-based provider discovery, content advertising, and cache-aware routing
//! for global content discovery in the IPFS network.
//!
//! # Example
//!
//! ```
//! use ipfrs_transport::content_routing::{ContentRouter, ContentRoutingConfig};
//! use multihash::Multihash;
//! use cid::Cid;
//!
//! # #[tokio::main]
//! # async fn main() {
//! use libp2p::PeerId;
//!
//! // Create a content router with default config
//! let router = ContentRouter::new();
//!
//! // Create example CID
//! let hash = Multihash::wrap(0x12, &[1u8; 32]).unwrap();
//! let cid = Cid::new_v1(0x55, hash);
//!
//! // Create a dummy peer ID for testing
//! let peer_id = PeerId::random();
//!
//! // Advertise that we provide this content
//! router.advertise_content(cid, peer_id).await;
//!
//! // Find providers for content
//! let providers = router.find_providers(&cid).await;
//! assert!(!providers.is_empty());
//!
//! // Check how many providers we have
//! let count = router.provider_count(&cid);
//! println!("Number of providers: {}", count);
//! # }
//! ```

use cid::Cid;
use dashmap::DashMap;
use libp2p::PeerId;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Configuration for content routing
#[derive(Debug, Clone)]
pub struct ContentRoutingConfig {
    /// Maximum number of providers to track per CID
    pub max_providers_per_cid: usize,
    /// Provider record TTL
    pub provider_record_ttl: Duration,
    /// Cache entry TTL
    pub cache_entry_ttl: Duration,
    /// Maximum cache size (number of entries)
    pub max_cache_size: usize,
    /// Number of parallel DHT queries
    pub dht_query_parallelism: usize,
    /// DHT query timeout
    pub dht_query_timeout: Duration,
    /// Whether to enable cache-aware routing
    pub enable_cache_aware_routing: bool,
}

impl Default for ContentRoutingConfig {
    fn default() -> Self {
        Self {
            max_providers_per_cid: 20,
            provider_record_ttl: Duration::from_secs(24 * 3600), // 24 hours
            cache_entry_ttl: Duration::from_secs(3600),          // 1 hour
            max_cache_size: 10_000,
            dht_query_parallelism: 3,
            dht_query_timeout: Duration::from_secs(30),
            enable_cache_aware_routing: true,
        }
    }
}

/// Provider record in the DHT
#[derive(Debug, Clone)]
pub struct ProviderRecord {
    /// Peer ID of the provider
    pub peer_id: PeerId,
    /// Timestamp when this record was added
    pub added_at: Instant,
    /// Time-to-live for this record
    pub ttl: Duration,
    /// Provider score (for ranking)
    pub score: f64,
    /// Number of successful retrievals from this provider
    pub successful_retrievals: u64,
    /// Number of failed retrievals from this provider
    pub failed_retrievals: u64,
}

impl ProviderRecord {
    fn new(peer_id: PeerId, ttl: Duration) -> Self {
        Self {
            peer_id,
            added_at: Instant::now(),
            ttl,
            score: 1.0,
            successful_retrievals: 0,
            failed_retrievals: 0,
        }
    }

    fn is_expired(&self) -> bool {
        self.added_at.elapsed() > self.ttl
    }

    fn update_score(&mut self) {
        let total = self.successful_retrievals + self.failed_retrievals;
        if total > 0 {
            self.score = self.successful_retrievals as f64 / total as f64;
        }
    }
}

/// Cache entry for content location
#[derive(Debug, Clone)]
struct CacheEntry {
    /// CID of the content
    #[allow(dead_code)]
    cid: Cid,
    /// List of providers
    providers: Vec<PeerId>,
    /// Timestamp when cached
    cached_at: Instant,
    /// Number of cache hits
    hits: u64,
}

impl CacheEntry {
    fn is_expired(&self, ttl: Duration) -> bool {
        self.cached_at.elapsed() > ttl
    }
}

/// Content routing statistics
#[derive(Debug, Default, Clone)]
pub struct ContentRoutingStats {
    /// Total DHT queries performed
    pub dht_queries: u64,
    /// Successful DHT queries
    pub dht_queries_success: u64,
    /// Failed DHT queries
    pub dht_queries_failed: u64,
    /// Cache hits
    pub cache_hits: u64,
    /// Cache misses
    pub cache_misses: u64,
    /// Total content advertisements
    pub content_advertisements: u64,
    /// Total provider records added
    pub provider_records_added: u64,
    /// Total provider records removed (expired)
    pub provider_records_removed: u64,
}

/// Content routing manager for DHT-based provider discovery
pub struct ContentRouter {
    /// Configuration
    config: ContentRoutingConfig,
    /// Provider records: CID -> Set of providers
    providers: Arc<DashMap<Cid, Vec<ProviderRecord>>>,
    /// Provider cache for fast lookups
    cache: Arc<RwLock<HashMap<Cid, CacheEntry>>>,
    /// Statistics
    stats: Arc<RwLock<ContentRoutingStats>>,
    /// Set of CIDs we're providing
    provided_content: Arc<DashMap<Cid, Instant>>,
}

impl ContentRouter {
    /// Create a new content router with default configuration
    pub fn new() -> Self {
        Self::with_config(ContentRoutingConfig::default())
    }

    /// Create a new content router with custom configuration
    pub fn with_config(config: ContentRoutingConfig) -> Self {
        Self {
            config,
            providers: Arc::new(DashMap::new()),
            cache: Arc::new(RwLock::new(HashMap::new())),
            stats: Arc::new(RwLock::new(ContentRoutingStats::default())),
            provided_content: Arc::new(DashMap::new()),
        }
    }

    /// Advertise that we have content
    ///
    /// This adds ourselves as a provider for the given CID in the DHT
    pub async fn advertise_content(&self, cid: Cid, peer_id: PeerId) {
        // Record that we're providing this content
        self.provided_content.insert(cid, Instant::now());

        // Add ourselves as a provider
        self.add_provider(cid, peer_id, self.config.provider_record_ttl)
            .await;

        // Update stats
        let mut stats = self.stats.write().await;
        stats.content_advertisements += 1;
    }

    /// Add a provider for a CID
    async fn add_provider(&self, cid: Cid, peer_id: PeerId, ttl: Duration) {
        let record = ProviderRecord::new(peer_id, ttl);

        // Update or insert provider record
        self.providers
            .entry(cid)
            .and_modify(|providers| {
                // Remove expired providers
                providers.retain(|p| !p.is_expired());

                // Update existing provider or add new one
                if let Some(existing) = providers.iter_mut().find(|p| p.peer_id == peer_id) {
                    existing.added_at = Instant::now();
                    existing.ttl = ttl;
                } else if providers.len() < self.config.max_providers_per_cid {
                    providers.push(record.clone());
                } else {
                    // Replace lowest-scored provider
                    if let Some(worst) = providers.iter_mut().min_by(|a, b| {
                        a.score
                            .partial_cmp(&b.score)
                            .unwrap_or(std::cmp::Ordering::Equal)
                    }) {
                        *worst = record.clone();
                    }
                }
            })
            .or_insert_with(|| vec![record]);

        // Invalidate cache for this CID
        self.cache.write().await.remove(&cid);

        // Update stats
        let mut stats = self.stats.write().await;
        stats.provider_records_added += 1;
    }

    /// Find providers for a CID
    ///
    /// Returns a list of peer IDs that can provide the content, ordered by score
    pub async fn find_providers(&self, cid: &Cid) -> Vec<PeerId> {
        // Check cache first
        if self.config.enable_cache_aware_routing {
            let mut cache = self.cache.write().await;
            if let Some(entry) = cache.get_mut(cid) {
                if !entry.is_expired(self.config.cache_entry_ttl) {
                    entry.hits += 1;
                    let mut stats = self.stats.write().await;
                    stats.cache_hits += 1;
                    return entry.providers.clone();
                } else {
                    cache.remove(cid);
                }
            }

            let mut stats = self.stats.write().await;
            stats.cache_misses += 1;
        }

        // Query DHT for providers
        let providers = self.query_dht_providers(cid).await;

        // Update cache
        if self.config.enable_cache_aware_routing && !providers.is_empty() {
            let mut cache = self.cache.write().await;
            if cache.len() >= self.config.max_cache_size {
                // Evict least recently used entry
                if let Some(lru_cid) = cache
                    .iter()
                    .min_by_key(|(_, entry)| entry.hits)
                    .map(|(cid, _)| *cid)
                {
                    cache.remove(&lru_cid);
                }
            }

            cache.insert(
                *cid,
                CacheEntry {
                    cid: *cid,
                    providers: providers.clone(),
                    cached_at: Instant::now(),
                    hits: 1,
                },
            );
        }

        providers
    }

    /// Query DHT for providers (simulated)
    async fn query_dht_providers(&self, cid: &Cid) -> Vec<PeerId> {
        let mut stats = self.stats.write().await;
        stats.dht_queries += 1;

        // Get providers from local store
        if let Some(providers) = self.providers.get(cid) {
            // Remove expired providers
            let valid_providers: Vec<_> = providers.iter().filter(|p| !p.is_expired()).collect();

            if !valid_providers.is_empty() {
                stats.dht_queries_success += 1;

                // Sort by score (highest first)
                let mut sorted_providers = valid_providers.clone();
                sorted_providers.sort_by(|a, b| {
                    b.score
                        .partial_cmp(&a.score)
                        .unwrap_or(std::cmp::Ordering::Equal)
                });

                return sorted_providers.iter().map(|p| p.peer_id).collect();
            }
        }

        stats.dht_queries_failed += 1;
        Vec::new()
    }

    /// Record successful retrieval from a provider
    pub async fn record_success(&self, cid: &Cid, peer_id: &PeerId) {
        if let Some(mut providers) = self.providers.get_mut(cid) {
            if let Some(provider) = providers.iter_mut().find(|p| p.peer_id == *peer_id) {
                provider.successful_retrievals += 1;
                provider.update_score();
            }
        }
    }

    /// Record failed retrieval from a provider
    pub async fn record_failure(&self, cid: &Cid, peer_id: &PeerId) {
        if let Some(mut providers) = self.providers.get_mut(cid) {
            if let Some(provider) = providers.iter_mut().find(|p| p.peer_id == *peer_id) {
                provider.failed_retrievals += 1;
                provider.update_score();
            }
        }
    }

    /// Clean up expired provider records
    pub async fn cleanup_expired(&self) {
        let mut removed_count = 0;

        // Clean up provider records
        let cids_to_remove: Vec<Cid> = self
            .providers
            .iter()
            .filter_map(|entry| {
                let cid = *entry.key();
                let mut providers = entry.value().clone();
                providers.retain(|p| !p.is_expired());

                if providers.is_empty() {
                    Some(cid)
                } else {
                    removed_count += entry.value().len() - providers.len();
                    None
                }
            })
            .collect();

        for cid in cids_to_remove {
            removed_count += self.providers.get(&cid).map(|p| p.len()).unwrap_or(0);
            self.providers.remove(&cid);
        }

        // Clean up cache
        if self.config.enable_cache_aware_routing {
            let mut cache = self.cache.write().await;
            cache.retain(|_, entry| !entry.is_expired(self.config.cache_entry_ttl));
        }

        // Update stats
        let mut stats = self.stats.write().await;
        stats.provider_records_removed += removed_count as u64;
    }

    /// Get statistics
    pub async fn stats(&self) -> ContentRoutingStats {
        self.stats.read().await.clone()
    }

    /// Get cache-aware route recommendation
    ///
    /// Returns the best peer to retrieve content from based on cache locality
    pub async fn get_cache_aware_route(
        &self,
        cid: &Cid,
        _local_cache: &HashSet<Cid>,
    ) -> Option<PeerId> {
        let providers = self.find_providers(cid).await;

        if providers.is_empty() {
            return None;
        }

        // Prefer providers that have content we also need (cache locality)
        // This is a simplified heuristic - in practice, we'd use more sophisticated metrics

        // For now, just return the highest-scored provider
        providers.first().copied()
    }

    /// Get number of providers for a CID
    pub fn provider_count(&self, cid: &Cid) -> usize {
        self.providers
            .get(cid)
            .map(|providers| providers.iter().filter(|p| !p.is_expired()).count())
            .unwrap_or(0)
    }

    /// List all content we're providing
    pub fn list_provided_content(&self) -> Vec<Cid> {
        self.provided_content
            .iter()
            .map(|entry| *entry.key())
            .collect()
    }
}

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

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

    fn create_test_cid(data: &[u8]) -> Cid {
        use ipfrs_core::CidBuilder;
        CidBuilder::new()
            .build(data)
            .expect("test: build CID from data")
    }

    #[tokio::test]
    async fn test_advertise_and_find_content() {
        let router = ContentRouter::new();
        let cid = create_test_cid(b"test content");
        let peer_id = PeerId::random();

        // Advertise content
        router.advertise_content(cid, peer_id).await;

        // Find providers
        let providers = router.find_providers(&cid).await;
        assert_eq!(providers.len(), 1);
        assert_eq!(providers[0], peer_id);
    }

    #[tokio::test]
    async fn test_multiple_providers() {
        let router = ContentRouter::new();
        let cid = create_test_cid(b"shared content");

        let peer1 = PeerId::random();
        let peer2 = PeerId::random();
        let peer3 = PeerId::random();

        // Multiple peers advertise the same content
        router.advertise_content(cid, peer1).await;
        router.advertise_content(cid, peer2).await;
        router.advertise_content(cid, peer3).await;

        let providers = router.find_providers(&cid).await;
        assert_eq!(providers.len(), 3);
        assert!(providers.contains(&peer1));
        assert!(providers.contains(&peer2));
        assert!(providers.contains(&peer3));
    }

    #[tokio::test]
    async fn test_provider_scoring() {
        let router = ContentRouter::new();
        let cid = create_test_cid(b"scored content");
        let peer = PeerId::random();

        router.advertise_content(cid, peer).await;

        // Record some successes
        router.record_success(&cid, &peer).await;
        router.record_success(&cid, &peer).await;
        router.record_success(&cid, &peer).await;

        // Record one failure
        router.record_failure(&cid, &peer).await;

        // Check provider score
        if let Some(providers) = router.providers.get(&cid) {
            let provider = providers
                .iter()
                .find(|p| p.peer_id == peer)
                .expect("test: find provider matching peer");
            assert_eq!(provider.successful_retrievals, 3);
            assert_eq!(provider.failed_retrievals, 1);
            assert!((provider.score - 0.75).abs() < 0.01); // 3/4 = 0.75
        };
    }

    #[tokio::test]
    async fn test_cache_functionality() {
        let config = ContentRoutingConfig {
            enable_cache_aware_routing: true,
            ..Default::default()
        };
        let router = ContentRouter::with_config(config);
        let cid = create_test_cid(b"cached content");
        let peer = PeerId::random();

        router.advertise_content(cid, peer).await;

        // First query - cache miss
        let providers1 = router.find_providers(&cid).await;
        let stats1 = router.stats().await;
        assert_eq!(stats1.cache_misses, 1);
        assert_eq!(stats1.cache_hits, 0);

        // Second query - cache hit
        let providers2 = router.find_providers(&cid).await;
        let stats2 = router.stats().await;
        assert_eq!(stats2.cache_misses, 1);
        assert_eq!(stats2.cache_hits, 1);

        assert_eq!(providers1, providers2);
    }

    #[tokio::test]
    async fn test_provider_expiration() {
        let config = ContentRoutingConfig {
            provider_record_ttl: Duration::from_millis(100),
            cache_entry_ttl: Duration::from_millis(50), // Short cache TTL
            enable_cache_aware_routing: false,          // Disable caching for this test
            ..Default::default()
        };
        let router = ContentRouter::with_config(config);
        let cid = create_test_cid(b"expiring content");
        let peer = PeerId::random();

        router.advertise_content(cid, peer).await;

        // Should find provider immediately
        let providers = router.find_providers(&cid).await;
        assert_eq!(providers.len(), 1);

        // Wait for expiration
        tokio::time::sleep(Duration::from_millis(150)).await;

        // Cleanup expired records
        router.cleanup_expired().await;

        // Should not find provider after expiration
        let providers = router.find_providers(&cid).await;
        assert_eq!(providers.len(), 0);
    }

    #[tokio::test]
    async fn test_max_providers_limit() {
        let config = ContentRoutingConfig {
            max_providers_per_cid: 3,
            ..Default::default()
        };
        let router = ContentRouter::with_config(config);
        let cid = create_test_cid(b"popular content");

        // Add 5 providers (exceeds limit of 3)
        for _ in 0..5 {
            let peer = PeerId::random();
            router.advertise_content(cid, peer).await;
        }

        let providers = router.find_providers(&cid).await;
        assert_eq!(providers.len(), 3);
    }

    #[tokio::test]
    async fn test_stats_tracking() {
        let router = ContentRouter::new();
        let cid = create_test_cid(b"stats content");
        let peer = PeerId::random();

        router.advertise_content(cid, peer).await;
        router.find_providers(&cid).await;

        let stats = router.stats().await;
        assert_eq!(stats.content_advertisements, 1);
        assert_eq!(stats.provider_records_added, 1);
        assert!(stats.dht_queries > 0);
    }

    #[tokio::test]
    async fn test_list_provided_content() {
        let router = ContentRouter::new();
        let peer = PeerId::random();

        let cid1 = create_test_cid(b"content 1");
        let cid2 = create_test_cid(b"content 2");

        router.advertise_content(cid1, peer).await;
        router.advertise_content(cid2, peer).await;

        let provided = router.list_provided_content();
        assert_eq!(provided.len(), 2);
        assert!(provided.contains(&cid1));
        assert!(provided.contains(&cid2));
    }

    #[tokio::test]
    async fn test_provider_count() {
        let router = ContentRouter::new();
        let cid = create_test_cid(b"counted content");

        assert_eq!(router.provider_count(&cid), 0);

        let peer1 = PeerId::random();
        router.advertise_content(cid, peer1).await;
        assert_eq!(router.provider_count(&cid), 1);

        let peer2 = PeerId::random();
        router.advertise_content(cid, peer2).await;
        assert_eq!(router.provider_count(&cid), 2);
    }
}