ipfrs-network 0.2.0

Peer-to-peer networking layer with libp2p and QUIC for IPFRS
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
//! NetworkEventBus — synchronous in-process publish-subscribe bus for network events.
//!
//! Decouples protocol handlers from application logic by providing a simple,
//! zero-dependency pub/sub mechanism with per-subscriber event queues and
//! configurable event filtering.

use std::collections::{HashMap, VecDeque};

// ---------------------------------------------------------------------------
// NetworkEvent
// ---------------------------------------------------------------------------

/// All events that can be emitted by the network layer.
#[derive(Clone, Debug)]
pub enum NetworkEvent {
    /// A remote peer successfully connected.
    PeerConnected { peer_id: String, address: String },
    /// A remote peer disconnected.
    PeerDisconnected { peer_id: String, reason: String },
    /// A block was received from a remote peer.
    BlockReceived {
        from: String,
        cid: String,
        size_bytes: usize,
    },
    /// A remote peer requested a block from us.
    BlockRequested { from: String, cid: String },
    /// A DHT provider record was found for a given CID.
    DhtProviderFound { cid: String, provider: String },
    /// A gossip message was received on a topic.
    GossipMessage {
        topic: String,
        from: String,
        payload_len: usize,
    },
    /// A dial attempt to a peer failed.
    DialFailed {
        peer_id: String,
        address: String,
        error: String,
    },
}

// ---------------------------------------------------------------------------
// EventFilter
// ---------------------------------------------------------------------------

/// Determines which events a subscription will receive.
#[derive(Clone, Debug)]
pub enum EventFilter {
    /// Receive every event.
    All,
    /// Receive only peer-related events: `PeerConnected`, `PeerDisconnected`, `DialFailed`.
    PeerEvents,
    /// Receive only block-related events: `BlockReceived`, `BlockRequested`.
    BlockEvents,
    /// Receive only DHT events: `DhtProviderFound`.
    DhtEvents,
    /// Receive only gossip events: `GossipMessage`.
    GossipEvents,
}

// ---------------------------------------------------------------------------
// Subscription
// ---------------------------------------------------------------------------

/// Maximum number of events that can be buffered per subscription before
/// events are dropped.
const MAX_QUEUE_CAPACITY: usize = 200;

/// A single subscription to the `NetworkEventBus`.
#[derive(Debug)]
pub struct Subscription {
    /// Unique subscription identifier.
    pub id: u64,
    /// Filter controlling which events are delivered.
    pub filter: EventFilter,
    /// Buffered events waiting to be drained by the subscriber.
    pub queue: VecDeque<NetworkEvent>,
    /// Number of events dropped because the queue was full.
    pub dropped: u64,
}

impl Subscription {
    fn new(id: u64, filter: EventFilter) -> Self {
        Self {
            id,
            filter,
            queue: VecDeque::new(),
            dropped: 0,
        }
    }

    /// Returns `true` if this subscription should receive `event`.
    pub fn matches(&self, event: &NetworkEvent) -> bool {
        match &self.filter {
            EventFilter::All => true,
            EventFilter::PeerEvents => matches!(
                event,
                NetworkEvent::PeerConnected { .. }
                    | NetworkEvent::PeerDisconnected { .. }
                    | NetworkEvent::DialFailed { .. }
            ),
            EventFilter::BlockEvents => matches!(
                event,
                NetworkEvent::BlockReceived { .. } | NetworkEvent::BlockRequested { .. }
            ),
            EventFilter::DhtEvents => matches!(event, NetworkEvent::DhtProviderFound { .. }),
            EventFilter::GossipEvents => matches!(event, NetworkEvent::GossipMessage { .. }),
        }
    }
}

// ---------------------------------------------------------------------------
// BusStats
// ---------------------------------------------------------------------------

/// Aggregate statistics for the `NetworkEventBus`.
#[derive(Clone, Debug, Default)]
pub struct BusStats {
    /// Total number of `publish` calls made.
    pub total_published: u64,
    /// Total number of event copies successfully enqueued across all subscriptions.
    pub total_delivered: u64,
    /// Total number of event copies dropped because a subscription queue was full.
    pub total_dropped: u64,
    /// Current number of active subscriptions.
    pub subscriber_count: usize,
}

// ---------------------------------------------------------------------------
// NetworkEventBus
// ---------------------------------------------------------------------------

/// Synchronous in-process publish-subscribe bus for `NetworkEvent`s.
///
/// # Example
///
/// ```rust
/// use ipfrs_network::event_bus::{NetworkEventBus, NetworkEvent, EventFilter};
///
/// let mut bus = NetworkEventBus::new();
/// let id = bus.subscribe(EventFilter::PeerEvents);
///
/// bus.publish(NetworkEvent::PeerConnected {
///     peer_id: "peer1".into(),
///     address: "/ip4/1.2.3.4/tcp/4001".into(),
/// });
///
/// let events = bus.drain(id);
/// assert_eq!(events.len(), 1);
/// ```
#[derive(Debug, Default)]
pub struct NetworkEventBus {
    subscriptions: HashMap<u64, Subscription>,
    next_id: u64,
    stats: BusStats,
}

impl NetworkEventBus {
    /// Create a new, empty `NetworkEventBus`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Subscribe to events matching `filter`.
    ///
    /// Returns a unique subscription id that can be used to drain events or
    /// unsubscribe later.
    pub fn subscribe(&mut self, filter: EventFilter) -> u64 {
        let id = self.next_id;
        self.next_id += 1;
        self.subscriptions.insert(id, Subscription::new(id, filter));
        id
    }

    /// Remove the subscription identified by `id`.
    ///
    /// Returns `true` if the subscription existed, `false` otherwise.
    pub fn unsubscribe(&mut self, id: u64) -> bool {
        self.subscriptions.remove(&id).is_some()
    }

    /// Publish an event to all matching subscribers.
    ///
    /// Each matching subscriber receives its own clone of `event`.  If a
    /// subscriber's queue is full (`>= 200` entries) the event is dropped and
    /// both the per-subscription and global drop counters are incremented.
    pub fn publish(&mut self, event: NetworkEvent) {
        self.stats.total_published += 1;

        for sub in self.subscriptions.values_mut() {
            if sub.matches(&event) {
                if sub.queue.len() < MAX_QUEUE_CAPACITY {
                    sub.queue.push_back(event.clone());
                    self.stats.total_delivered += 1;
                } else {
                    sub.dropped += 1;
                    self.stats.total_dropped += 1;
                }
            }
        }
    }

    /// Drain all buffered events for the subscription identified by `id`.
    ///
    /// Returns an empty `Vec` if the id does not exist.
    pub fn drain(&mut self, id: u64) -> Vec<NetworkEvent> {
        match self.subscriptions.get_mut(&id) {
            Some(sub) => sub.queue.drain(..).collect(),
            None => Vec::new(),
        }
    }

    /// Return the number of events currently buffered for subscription `id`.
    ///
    /// Returns `0` for unknown ids.
    pub fn peek_count(&self, id: u64) -> usize {
        self.subscriptions
            .get(&id)
            .map(|s| s.queue.len())
            .unwrap_or(0)
    }

    /// Return a snapshot of the current bus statistics.
    pub fn stats(&self) -> BusStats {
        BusStats {
            subscriber_count: self.subscriptions.len(),
            ..self.stats.clone()
        }
    }

    /// Drain all queues for all active subscriptions, discarding pending events.
    pub fn clear_all_queues(&mut self) {
        for sub in self.subscriptions.values_mut() {
            sub.queue.clear();
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn peer_connected() -> NetworkEvent {
        NetworkEvent::PeerConnected {
            peer_id: "peer1".into(),
            address: "/ip4/127.0.0.1/tcp/4001".into(),
        }
    }

    fn peer_disconnected() -> NetworkEvent {
        NetworkEvent::PeerDisconnected {
            peer_id: "peer1".into(),
            reason: "timeout".into(),
        }
    }

    fn block_received() -> NetworkEvent {
        NetworkEvent::BlockReceived {
            from: "peer1".into(),
            cid: "bafy123".into(),
            size_bytes: 1024,
        }
    }

    fn block_requested() -> NetworkEvent {
        NetworkEvent::BlockRequested {
            from: "peer2".into(),
            cid: "bafy456".into(),
        }
    }

    fn dht_provider_found() -> NetworkEvent {
        NetworkEvent::DhtProviderFound {
            cid: "bafy789".into(),
            provider: "peer3".into(),
        }
    }

    fn gossip_message() -> NetworkEvent {
        NetworkEvent::GossipMessage {
            topic: "blocks".into(),
            from: "peer4".into(),
            payload_len: 256,
        }
    }

    fn dial_failed() -> NetworkEvent {
        NetworkEvent::DialFailed {
            peer_id: "peer5".into(),
            address: "/ip4/1.2.3.4/tcp/4001".into(),
            error: "connection refused".into(),
        }
    }

    // 1. new() empty state
    #[test]
    fn test_new_empty_state() {
        let bus = NetworkEventBus::new();
        let stats = bus.stats();
        assert_eq!(stats.total_published, 0);
        assert_eq!(stats.total_delivered, 0);
        assert_eq!(stats.total_dropped, 0);
        assert_eq!(stats.subscriber_count, 0);
    }

    // 2. subscribe() returns unique IDs
    #[test]
    fn test_subscribe_unique_ids() {
        let mut bus = NetworkEventBus::new();
        let id1 = bus.subscribe(EventFilter::All);
        let id2 = bus.subscribe(EventFilter::All);
        let id3 = bus.subscribe(EventFilter::PeerEvents);
        assert_ne!(id1, id2);
        assert_ne!(id2, id3);
        assert_ne!(id1, id3);
    }

    // 3. unsubscribe() returns true/false
    #[test]
    fn test_unsubscribe_true_false() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::All);
        assert!(bus.unsubscribe(id));
        assert!(!bus.unsubscribe(id)); // already removed
        assert!(!bus.unsubscribe(9999)); // never existed
    }

    // 4. publish() delivers to All filter
    #[test]
    fn test_publish_delivers_to_all_filter() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::All);
        bus.publish(peer_connected());
        bus.publish(block_received());
        bus.publish(dht_provider_found());
        assert_eq!(bus.peek_count(id), 3);
    }

    // 5. publish() PeerEvents filter matches PeerConnected
    #[test]
    fn test_peer_events_filter_matches_peer_connected() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::PeerEvents);
        bus.publish(peer_connected());
        assert_eq!(bus.peek_count(id), 1);
    }

    // 6. publish() PeerEvents filter ignores BlockReceived
    #[test]
    fn test_peer_events_filter_ignores_block_received() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::PeerEvents);
        bus.publish(block_received());
        assert_eq!(bus.peek_count(id), 0);
    }

    // 7. publish() BlockEvents filter matches BlockReceived and BlockRequested
    #[test]
    fn test_block_events_filter_matches_block_events() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::BlockEvents);
        bus.publish(block_received());
        bus.publish(block_requested());
        bus.publish(peer_connected()); // should be ignored
        assert_eq!(bus.peek_count(id), 2);
    }

    // 8. publish() DhtEvents filter matches DhtProviderFound only
    #[test]
    fn test_dht_events_filter_matches_dht_provider_found_only() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::DhtEvents);
        bus.publish(dht_provider_found());
        bus.publish(block_received());
        bus.publish(peer_connected());
        assert_eq!(bus.peek_count(id), 1);
    }

    // 9. publish() GossipEvents filter matches GossipMessage only
    #[test]
    fn test_gossip_events_filter_matches_gossip_message_only() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::GossipEvents);
        bus.publish(gossip_message());
        bus.publish(block_received());
        bus.publish(dht_provider_found());
        assert_eq!(bus.peek_count(id), 1);
    }

    // 10. drain() returns buffered events
    #[test]
    fn test_drain_returns_buffered_events() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::All);
        bus.publish(peer_connected());
        bus.publish(block_received());
        let events = bus.drain(id);
        assert_eq!(events.len(), 2);
        // Queue is now empty
        assert_eq!(bus.peek_count(id), 0);
    }

    // 11. drain() unknown id returns empty vec
    #[test]
    fn test_drain_unknown_id_returns_empty() {
        let mut bus = NetworkEventBus::new();
        let events = bus.drain(9999);
        assert!(events.is_empty());
    }

    // 12. peek_count() accurate
    #[test]
    fn test_peek_count_accurate() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::All);
        assert_eq!(bus.peek_count(id), 0);
        bus.publish(peer_connected());
        assert_eq!(bus.peek_count(id), 1);
        bus.publish(block_received());
        assert_eq!(bus.peek_count(id), 2);
        bus.drain(id);
        assert_eq!(bus.peek_count(id), 0);
    }

    // 13. queue bounded at 200, overflow increments dropped
    #[test]
    fn test_queue_bounded_at_200_overflow_drops() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::All);

        for _ in 0..205 {
            bus.publish(peer_connected());
        }

        assert_eq!(bus.peek_count(id), 200);

        let sub = bus.subscriptions.get(&id).expect("subscription must exist");
        assert_eq!(sub.dropped, 5);
        assert_eq!(bus.stats().total_dropped, 5);
    }

    // 14. stats totals updated correctly
    #[test]
    fn test_stats_totals_updated_correctly() {
        let mut bus = NetworkEventBus::new();
        let id1 = bus.subscribe(EventFilter::All);
        let id2 = bus.subscribe(EventFilter::PeerEvents);

        bus.publish(peer_connected()); // delivered to both → 2 delivered
        bus.publish(block_received()); // delivered to id1 only → 1 delivered

        let stats = bus.stats();
        assert_eq!(stats.total_published, 2);
        assert_eq!(stats.total_delivered, 3);
        assert_eq!(stats.total_dropped, 0);
        assert_eq!(stats.subscriber_count, 2);

        let _ = (id1, id2); // suppress unused warnings
    }

    // 15. clear_all_queues empties all queues
    #[test]
    fn test_clear_all_queues_empties_all() {
        let mut bus = NetworkEventBus::new();
        let id1 = bus.subscribe(EventFilter::All);
        let id2 = bus.subscribe(EventFilter::BlockEvents);
        bus.publish(peer_connected());
        bus.publish(block_received());
        assert!(bus.peek_count(id1) > 0);
        assert!(bus.peek_count(id2) > 0);

        bus.clear_all_queues();

        assert_eq!(bus.peek_count(id1), 0);
        assert_eq!(bus.peek_count(id2), 0);
    }

    // 16. multiple subscribers receive same event (each gets own copy)
    #[test]
    fn test_multiple_subscribers_each_get_copy() {
        let mut bus = NetworkEventBus::new();
        let ids: Vec<u64> = (0..5).map(|_| bus.subscribe(EventFilter::All)).collect();

        bus.publish(peer_connected());

        for id in &ids {
            assert_eq!(
                bus.peek_count(*id),
                1,
                "subscriber {id} should have 1 event"
            );
        }
    }

    // 17. unsubscribed id no longer receives events
    #[test]
    fn test_unsubscribed_id_no_longer_receives_events() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::All);
        bus.publish(peer_connected());
        assert_eq!(bus.peek_count(id), 1);

        assert!(bus.unsubscribe(id));
        bus.publish(block_received()); // should not be delivered

        // After unsubscribe peek_count returns 0 (id is gone)
        assert_eq!(bus.peek_count(id), 0);
    }

    // 18. PeerEvents filter also matches PeerDisconnected and DialFailed
    #[test]
    fn test_peer_events_filter_matches_all_peer_variants() {
        let mut bus = NetworkEventBus::new();
        let id = bus.subscribe(EventFilter::PeerEvents);
        bus.publish(peer_connected());
        bus.publish(peer_disconnected());
        bus.publish(dial_failed());
        bus.publish(gossip_message()); // should not match
        assert_eq!(bus.peek_count(id), 3);
    }

    // 19. subscriber_count reflects current subscriptions
    #[test]
    fn test_subscriber_count_reflects_current() {
        let mut bus = NetworkEventBus::new();
        assert_eq!(bus.stats().subscriber_count, 0);
        let id1 = bus.subscribe(EventFilter::All);
        assert_eq!(bus.stats().subscriber_count, 1);
        let id2 = bus.subscribe(EventFilter::BlockEvents);
        assert_eq!(bus.stats().subscriber_count, 2);
        bus.unsubscribe(id1);
        assert_eq!(bus.stats().subscriber_count, 1);
        bus.unsubscribe(id2);
        assert_eq!(bus.stats().subscriber_count, 0);
    }
}