ipfrs-interface 0.1.0

HTTP, gRPC, GraphQL and Python interfaces for IPFRS distributed storage
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
//! WebSocket Support for Real-Time IPFRS Communication
//!
//! Provides:
//! - WebSocket upgrade handler
//! - Message routing and handling
//! - Pub/sub pattern for subscriptions
//! - Real-time event notifications (block additions, peer connections, etc.)

use axum::{
    extract::{
        ws::{Message, WebSocket, WebSocketUpgrade},
        State,
    },
    response::Response,
};
use futures::{sink::SinkExt, stream::StreamExt};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::{broadcast, RwLock};
use tracing::{debug, error, info, warn};
use uuid::Uuid;

// ============================================================================
// WebSocket Message Types
// ============================================================================

/// WebSocket message envelope
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum WsMessage {
    /// Subscribe to a topic
    Subscribe {
        topic: String,
        filter: Option<String>,
    },
    /// Unsubscribe from a topic
    Unsubscribe { topic: String },
    /// Event notification
    Event {
        topic: String,
        data: serde_json::Value,
    },
    /// Ping message for keepalive
    Ping,
    /// Pong response
    Pong,
    /// Error message
    Error { code: u16, message: String },
}

// ============================================================================
// Event Types
// ============================================================================

/// Real-time event types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event_type", rename_all = "snake_case")]
pub enum RealtimeEvent {
    /// Block was added to the store
    BlockAdded {
        cid: String,
        size: usize,
        timestamp: u64,
    },
    /// Block was removed from the store
    BlockRemoved { cid: String, timestamp: u64 },
    /// Peer connected
    PeerConnected {
        peer_id: String,
        address: String,
        timestamp: u64,
    },
    /// Peer disconnected
    PeerDisconnected { peer_id: String, timestamp: u64 },
    /// DHT query started
    DhtQueryStarted { query_id: String, key: String },
    /// DHT query progress
    DhtQueryProgress {
        query_id: String,
        peers_queried: usize,
        results_found: usize,
    },
    /// DHT query completed
    DhtQueryCompleted {
        query_id: String,
        success: bool,
        results: usize,
    },
}

impl RealtimeEvent {
    /// Get the topic for this event
    pub fn topic(&self) -> &str {
        match self {
            RealtimeEvent::BlockAdded { .. } | RealtimeEvent::BlockRemoved { .. } => "blocks",
            RealtimeEvent::PeerConnected { .. } | RealtimeEvent::PeerDisconnected { .. } => "peers",
            RealtimeEvent::DhtQueryStarted { .. }
            | RealtimeEvent::DhtQueryProgress { .. }
            | RealtimeEvent::DhtQueryCompleted { .. } => "dht",
        }
    }
}

// ============================================================================
// Subscription Manager
// ============================================================================

/// Manages WebSocket subscriptions and pub/sub
#[derive(Clone)]
pub struct SubscriptionManager {
    /// Topic-based broadcast channels
    topics: Arc<RwLock<HashMap<String, broadcast::Sender<RealtimeEvent>>>>,
    /// Active subscriptions per connection
    subscriptions: Arc<RwLock<HashMap<Uuid, Vec<String>>>>,
}

impl SubscriptionManager {
    /// Create a new subscription manager
    pub fn new() -> Self {
        Self {
            topics: Arc::new(RwLock::new(HashMap::new())),
            subscriptions: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Subscribe a connection to a topic
    pub async fn subscribe(
        &self,
        connection_id: Uuid,
        topic: String,
    ) -> Result<broadcast::Receiver<RealtimeEvent>, WsError> {
        let mut topics = self.topics.write().await;

        // Get or create topic channel
        let sender = topics
            .entry(topic.clone())
            .or_insert_with(|| {
                let (tx, _rx) = broadcast::channel(100);
                info!("Created new topic channel: {}", topic);
                tx
            })
            .clone();

        // Track subscription
        let mut subs = self.subscriptions.write().await;
        subs.entry(connection_id).or_default().push(topic.clone());

        info!(
            "Connection {} subscribed to topic: {}",
            connection_id, topic
        );

        Ok(sender.subscribe())
    }

    /// Unsubscribe a connection from a topic
    pub async fn unsubscribe(&self, connection_id: Uuid, topic: &str) {
        let mut subs = self.subscriptions.write().await;
        if let Some(topics) = subs.get_mut(&connection_id) {
            topics.retain(|t| t != topic);
            info!(
                "Connection {} unsubscribed from topic: {}",
                connection_id, topic
            );
        }
    }

    /// Remove all subscriptions for a connection
    pub async fn remove_connection(&self, connection_id: Uuid) {
        let mut subs = self.subscriptions.write().await;
        subs.remove(&connection_id);
        info!(
            "Removed all subscriptions for connection: {}",
            connection_id
        );
    }

    /// Publish an event to a topic
    pub async fn publish(&self, event: RealtimeEvent) -> Result<usize, WsError> {
        let topic = event.topic().to_string();
        let topics = self.topics.read().await;

        if let Some(sender) = topics.get(&topic) {
            match sender.send(event.clone()) {
                Ok(count) => {
                    debug!(
                        "Published event to {} subscribers on topic: {}",
                        count, topic
                    );
                    Ok(count)
                }
                Err(_) => {
                    warn!("No active subscribers for topic: {}", topic);
                    Ok(0)
                }
            }
        } else {
            debug!("Topic not found: {}", topic);
            Ok(0)
        }
    }

    /// Get active subscription count
    pub async fn subscription_count(&self) -> usize {
        let subs = self.subscriptions.read().await;
        subs.len()
    }

    /// Get topic count
    pub async fn topic_count(&self) -> usize {
        let topics = self.topics.read().await;
        topics.len()
    }
}

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

// ============================================================================
// WebSocket Handler
// ============================================================================

/// WebSocket handler state
#[derive(Clone)]
pub struct WsState {
    pub subscription_manager: SubscriptionManager,
}

impl WsState {
    /// Create new WebSocket state
    pub fn new() -> Self {
        Self {
            subscription_manager: SubscriptionManager::new(),
        }
    }
}

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

/// WebSocket upgrade handler
///
/// GET /ws
pub async fn ws_handler(ws: WebSocketUpgrade, State(state): State<WsState>) -> Response {
    ws.on_upgrade(|socket| handle_socket(socket, state))
}

/// Handle individual WebSocket connection
#[allow(clippy::too_many_arguments)]
async fn handle_socket(socket: WebSocket, state: WsState) {
    let connection_id = Uuid::new_v4();
    info!("New WebSocket connection: {}", connection_id);

    let (sender, receiver) = socket.split();
    let sender = Arc::new(tokio::sync::Mutex::new(sender));

    // Subscriptions for this connection
    let active_subscriptions: Arc<
        tokio::sync::Mutex<HashMap<String, broadcast::Receiver<RealtimeEvent>>>,
    > = Arc::new(tokio::sync::Mutex::new(HashMap::new()));

    // Spawn task to handle outgoing events
    let sender_clone = sender.clone();
    let subs_clone = active_subscriptions.clone();
    let event_task = tokio::spawn(async move {
        loop {
            // Check all active subscriptions for events
            let mut subs = subs_clone.lock().await;
            let topics: Vec<String> = subs.keys().cloned().collect();

            for topic in topics {
                if let Some(rx) = subs.get_mut(&topic) {
                    match rx.try_recv() {
                        Ok(event) => {
                            let msg = WsMessage::Event {
                                topic: topic.clone(),
                                data: serde_json::to_value(&event).unwrap_or_default(),
                            };

                            if let Ok(json) = serde_json::to_string(&msg) {
                                let mut tx = sender_clone.lock().await;
                                if tx.send(Message::Text(json.into())).await.is_err() {
                                    return;
                                }
                            }
                        }
                        Err(broadcast::error::TryRecvError::Empty) => {}
                        Err(_) => {}
                    }
                }
            }

            drop(subs);
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        }
    });

    // Handle incoming messages
    let mut receiver = receiver;
    while let Some(msg) = receiver.next().await {
        match msg {
            Ok(Message::Text(text)) => match serde_json::from_str::<WsMessage>(&text) {
                Ok(ws_msg) => match ws_msg {
                    WsMessage::Subscribe { topic, filter } => {
                        debug!(
                            "Connection {} subscribing to topic: {} (filter: {:?})",
                            connection_id, topic, filter
                        );

                        match state
                            .subscription_manager
                            .subscribe(connection_id, topic.clone())
                            .await
                        {
                            Ok(rx) => {
                                let mut subs = active_subscriptions.lock().await;
                                subs.insert(topic, rx);
                            }
                            Err(e) => {
                                error!("Failed to subscribe: {}", e);
                                let error_msg = WsMessage::Error {
                                    code: 500,
                                    message: format!("Subscription failed: {}", e),
                                };
                                if let Ok(json) = serde_json::to_string(&error_msg) {
                                    let mut tx = sender.lock().await;
                                    let _ = tx.send(Message::Text(json.into())).await;
                                }
                            }
                        }
                    }
                    WsMessage::Unsubscribe { topic } => {
                        debug!(
                            "Connection {} unsubscribing from topic: {}",
                            connection_id, topic
                        );
                        state
                            .subscription_manager
                            .unsubscribe(connection_id, &topic)
                            .await;
                        let mut subs = active_subscriptions.lock().await;
                        subs.remove(&topic);
                    }
                    WsMessage::Ping => {
                        let pong = WsMessage::Pong;
                        if let Ok(json) = serde_json::to_string(&pong) {
                            let mut tx = sender.lock().await;
                            let _ = tx.send(Message::Text(json.into())).await;
                        }
                    }
                    _ => {
                        warn!("Unexpected message type from client");
                    }
                },
                Err(e) => {
                    error!("Failed to parse message: {}", e);
                    let error_msg = WsMessage::Error {
                        code: 400,
                        message: format!("Invalid message format: {}", e),
                    };
                    if let Ok(json) = serde_json::to_string(&error_msg) {
                        let mut tx = sender.lock().await;
                        let _ = tx.send(Message::Text(json.into())).await;
                    }
                }
            },
            Ok(Message::Close(_)) => {
                info!("Connection {} closed by client", connection_id);
                break;
            }
            Err(e) => {
                error!("WebSocket error: {}", e);
                break;
            }
            _ => {}
        }
    }

    // Cleanup
    event_task.abort();
    state
        .subscription_manager
        .remove_connection(connection_id)
        .await;
    info!("Connection {} disconnected", connection_id);
}

// ============================================================================
// Error Types
// ============================================================================

/// WebSocket errors
#[derive(Debug, Error)]
pub enum WsError {
    #[error("Subscription error: {0}")]
    SubscriptionError(String),

    #[error("Invalid topic: {0}")]
    InvalidTopic(String),

    #[error("Send error: {0}")]
    SendError(String),
}

// ============================================================================
// Tests
// ============================================================================

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

    #[tokio::test]
    async fn test_subscription_manager_new() {
        let manager = SubscriptionManager::new();
        assert_eq!(manager.subscription_count().await, 0);
        assert_eq!(manager.topic_count().await, 0);
    }

    #[tokio::test]
    async fn test_subscribe_and_publish() {
        let manager = SubscriptionManager::new();
        let conn_id = Uuid::new_v4();

        // Subscribe to blocks topic
        let mut rx = manager
            .subscribe(conn_id, "blocks".to_string())
            .await
            .unwrap();

        // Publish an event
        let event = RealtimeEvent::BlockAdded {
            cid: "QmTest".to_string(),
            size: 1024,
            timestamp: 12345,
        };

        let count = manager.publish(event.clone()).await.unwrap();
        assert_eq!(count, 1);

        // Receive the event
        let received = rx.recv().await.unwrap();
        match received {
            RealtimeEvent::BlockAdded { cid, size, .. } => {
                assert_eq!(cid, "QmTest");
                assert_eq!(size, 1024);
            }
            _ => panic!("Wrong event type"),
        }
    }

    #[tokio::test]
    async fn test_unsubscribe() {
        let manager = SubscriptionManager::new();
        let conn_id = Uuid::new_v4();

        // Subscribe
        let _rx = manager
            .subscribe(conn_id, "blocks".to_string())
            .await
            .unwrap();
        assert_eq!(manager.subscription_count().await, 1);

        // Unsubscribe
        manager.unsubscribe(conn_id, "blocks").await;
        assert_eq!(manager.subscription_count().await, 1); // Connection still tracked

        // Remove connection
        manager.remove_connection(conn_id).await;
        assert_eq!(manager.subscription_count().await, 0);
    }

    #[tokio::test]
    async fn test_multiple_subscribers() {
        let manager = SubscriptionManager::new();
        let conn1 = Uuid::new_v4();
        let conn2 = Uuid::new_v4();

        // Subscribe both connections
        let mut rx1 = manager
            .subscribe(conn1, "blocks".to_string())
            .await
            .unwrap();
        let mut rx2 = manager
            .subscribe(conn2, "blocks".to_string())
            .await
            .unwrap();

        // Publish event
        let event = RealtimeEvent::BlockAdded {
            cid: "QmTest".to_string(),
            size: 2048,
            timestamp: 12345,
        };

        let count = manager.publish(event).await.unwrap();
        assert_eq!(count, 2); // Both subscribers receive it

        // Both should receive
        assert!(rx1.recv().await.is_ok());
        assert!(rx2.recv().await.is_ok());
    }

    #[test]
    fn test_realtime_event_topic() {
        let block_event = RealtimeEvent::BlockAdded {
            cid: "test".to_string(),
            size: 100,
            timestamp: 123,
        };
        assert_eq!(block_event.topic(), "blocks");

        let peer_event = RealtimeEvent::PeerConnected {
            peer_id: "peer1".to_string(),
            address: "addr1".to_string(),
            timestamp: 123,
        };
        assert_eq!(peer_event.topic(), "peers");

        let dht_event = RealtimeEvent::DhtQueryStarted {
            query_id: "q1".to_string(),
            key: "key1".to_string(),
        };
        assert_eq!(dht_event.topic(), "dht");
    }

    #[test]
    fn test_ws_message_serialization() {
        let subscribe = WsMessage::Subscribe {
            topic: "blocks".to_string(),
            filter: Some("cid=Qm*".to_string()),
        };

        let json = serde_json::to_string(&subscribe).unwrap();
        assert!(json.contains("subscribe"));
        assert!(json.contains("blocks"));

        let deserialized: WsMessage = serde_json::from_str(&json).unwrap();
        match deserialized {
            WsMessage::Subscribe { topic, .. } => {
                assert_eq!(topic, "blocks");
            }
            _ => panic!("Wrong message type"),
        }
    }
}