elara-diffusion 0.2.0

ELARA Protocol - Swarm diffusion engine for state propagation, livestream distribution, and group communication
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
//! Swarm - Complete diffusion system for livestream/group
//!
//! Combines authority, interest, topology, and propagation.

use elara_core::{NodeId, StateTime};
use std::collections::HashMap;

use crate::{
    InterestDeclaration, InterestLevel, InterestMap, LivestreamAuthority, LivestreamInterest,
    PropagationTopology, StarTopology, StateUpdate, TreeTopology,
};

/// Swarm configuration
#[derive(Debug, Clone)]
pub struct SwarmConfig {
    /// Maximum viewers before switching to tree topology
    pub star_to_tree_threshold: usize,
    /// Maximum fan-out for tree topology
    pub tree_fanout: usize,
    /// Bandwidth budget per viewer (bytes/second)
    pub bandwidth_per_viewer: u32,
    /// Keyframe interval in milliseconds
    pub keyframe_interval_ms: u32,
}

impl Default for SwarmConfig {
    fn default() -> Self {
        Self {
            star_to_tree_threshold: 50,
            tree_fanout: 5,
            bandwidth_per_viewer: 500_000, // 500 KB/s
            keyframe_interval_ms: 2000,
        }
    }
}

/// Swarm state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SwarmState {
    /// Initializing
    Initializing,
    /// Active and streaming
    Active,
    /// Paused (broadcaster paused)
    Paused,
    /// Ended
    Ended,
}

/// Livestream swarm - complete system for a single livestream
#[derive(Debug)]
pub struct LivestreamSwarm {
    /// Stream ID
    pub stream_id: u64,
    /// Configuration
    pub config: SwarmConfig,
    /// Current state
    pub state: SwarmState,
    /// Authority model
    pub authority: LivestreamAuthority,
    /// Interest tracking
    pub interest: LivestreamInterest,
    /// Current topology (star or tree)
    topology: SwarmTopology,
    /// Last keyframe time
    last_keyframe: StateTime,
    /// Sequence counter
    sequence: u64,
    /// Statistics
    pub stats: SwarmStats,
}

/// Topology type
#[derive(Debug)]
enum SwarmTopology {
    Star(StarTopology),
    Tree(TreeTopology),
}

impl LivestreamSwarm {
    /// Create a new livestream swarm
    pub fn new(stream_id: u64, broadcaster: NodeId, config: SwarmConfig) -> Self {
        Self {
            stream_id,
            config: config.clone(),
            state: SwarmState::Initializing,
            authority: LivestreamAuthority::new(broadcaster, stream_id),
            interest: LivestreamInterest::new(stream_id),
            topology: SwarmTopology::Star(StarTopology::new(broadcaster)),
            last_keyframe: StateTime::from_millis(0),
            sequence: 0,
            stats: SwarmStats::new(),
        }
    }

    /// Start the stream
    pub fn start(&mut self) {
        self.state = SwarmState::Active;
    }

    /// Pause the stream
    pub fn pause(&mut self) {
        self.state = SwarmState::Paused;
    }

    /// Resume the stream
    pub fn resume(&mut self) {
        self.state = SwarmState::Active;
    }

    /// End the stream
    pub fn end(&mut self) {
        self.state = SwarmState::Ended;
    }

    /// Add a viewer
    pub fn add_viewer(&mut self, viewer: NodeId) {
        self.interest.add_viewer(viewer);

        match &mut self.topology {
            SwarmTopology::Star(star) => {
                star.add_leaf(viewer);

                // Check if we need to switch to tree
                if star.leaf_count() > self.config.star_to_tree_threshold {
                    self.switch_to_tree();
                }
            }
            SwarmTopology::Tree(tree) => {
                tree.add_node(viewer);
            }
        }

        self.stats.peak_viewers = self.stats.peak_viewers.max(self.viewer_count() as u32);
    }

    /// Remove a viewer
    pub fn remove_viewer(&mut self, viewer: NodeId) {
        self.interest.remove_viewer(viewer);

        match &mut self.topology {
            SwarmTopology::Star(star) => {
                star.remove_leaf(viewer);
            }
            SwarmTopology::Tree(tree) => {
                tree.remove_node(viewer);
            }
        }
    }

    /// Switch from star to tree topology
    fn switch_to_tree(&mut self) {
        if let SwarmTopology::Star(star) = &self.topology {
            let mut tree = TreeTopology::new(star.center, self.config.tree_fanout);

            // Add all existing leaves
            for &leaf in &star.leaves {
                tree.add_node(leaf);
            }

            self.topology = SwarmTopology::Tree(tree);
        }
    }

    /// Get viewer count
    pub fn viewer_count(&self) -> usize {
        self.interest.viewer_count()
    }

    /// Get broadcaster
    pub fn broadcaster(&self) -> NodeId {
        self.authority.broadcaster
    }

    /// Check if a node can broadcast
    pub fn can_broadcast(&self, node: NodeId) -> bool {
        self.authority.can_mutate_visual(node)
    }

    /// Create a state update for broadcasting
    pub fn create_update(
        &mut self,
        timestamp: StateTime,
        size: usize,
        is_keyframe: bool,
    ) -> StateUpdate {
        self.sequence += 1;

        let mut update =
            StateUpdate::new(self.stream_id, self.broadcaster(), self.sequence, timestamp)
                .with_size(size);

        if is_keyframe {
            update = update.keyframe();
            self.last_keyframe = timestamp;
        }

        update
    }

    /// Check if we need a keyframe
    pub fn needs_keyframe(&self, current_time: StateTime) -> bool {
        let elapsed = current_time.as_millis() - self.last_keyframe.as_millis();
        elapsed >= self.config.keyframe_interval_ms as i64
    }

    /// Get propagation targets for an update
    pub fn get_targets(&self) -> Vec<NodeId> {
        match &self.topology {
            SwarmTopology::Star(star) => star.leaves.iter().copied().collect(),
            SwarmTopology::Tree(tree) => tree.topology.downstream(self.broadcaster()),
        }
    }
}

/// Swarm statistics
#[derive(Debug, Clone, Default)]
pub struct SwarmStats {
    /// Total updates sent
    pub updates_sent: u64,
    /// Total bytes sent
    pub bytes_sent: u64,
    /// Peak viewer count
    pub peak_viewers: u32,
    /// Total unique viewers
    pub total_viewers: u32,
    /// Stream duration in seconds
    pub duration_seconds: u32,
}

impl SwarmStats {
    pub fn new() -> Self {
        Self::default()
    }
}

/// Group swarm - for video calls (symmetric authority)
#[derive(Debug)]
pub struct GroupSwarm {
    /// Group ID
    pub group_id: u64,
    /// All participants
    participants: HashMap<NodeId, ParticipantState>,
    /// Interest map
    interests: InterestMap,
    /// Topology (mesh for small groups)
    topology: PropagationTopology,
    /// Maximum participants
    pub max_participants: usize,
}

/// Participant state in a group
#[derive(Debug, Clone)]
pub struct ParticipantState {
    /// Node ID
    pub node: NodeId,
    /// Is video enabled?
    pub video_enabled: bool,
    /// Is audio enabled?
    pub audio_enabled: bool,
    /// Is screen sharing?
    pub screen_sharing: bool,
    /// Join time
    pub joined_at: StateTime,
}

impl GroupSwarm {
    /// Create a new group swarm
    pub fn new(group_id: u64, max_participants: usize) -> Self {
        Self {
            group_id,
            participants: HashMap::new(),
            interests: InterestMap::new(),
            topology: PropagationTopology::new(),
            max_participants,
        }
    }

    /// Add a participant
    pub fn add_participant(&mut self, node: NodeId, joined_at: StateTime) -> bool {
        if self.participants.len() >= self.max_participants {
            return false;
        }

        let state = ParticipantState {
            node,
            video_enabled: true,
            audio_enabled: true,
            screen_sharing: false,
            joined_at,
        };

        // Add edges to/from all existing participants (mesh)
        for &existing in self.participants.keys() {
            self.topology
                .add_edge(crate::PropagationEdge::new(existing, node));
            self.topology
                .add_edge(crate::PropagationEdge::new(node, existing));
        }

        // Register interest in all other participants' states
        for &existing in self.participants.keys() {
            self.interests.register(InterestDeclaration::new(
                node,
                existing.0,
                InterestLevel::High,
            ));
            self.interests.register(InterestDeclaration::new(
                existing,
                node.0,
                InterestLevel::High,
            ));
        }

        self.participants.insert(node, state);
        self.topology.add_node(node);

        true
    }

    /// Remove a participant
    pub fn remove_participant(&mut self, node: NodeId) {
        self.participants.remove(&node);
        self.topology.remove_node(node);
        self.interests.remove_node(node);
    }

    /// Get participant count
    pub fn participant_count(&self) -> usize {
        self.participants.len()
    }

    /// Toggle video for a participant
    pub fn toggle_video(&mut self, node: NodeId, enabled: bool) {
        if let Some(p) = self.participants.get_mut(&node) {
            p.video_enabled = enabled;
        }
    }

    /// Toggle audio for a participant
    pub fn toggle_audio(&mut self, node: NodeId, enabled: bool) {
        if let Some(p) = self.participants.get_mut(&node) {
            p.audio_enabled = enabled;
        }
    }

    /// Start screen sharing
    pub fn start_screen_share(&mut self, node: NodeId) -> bool {
        // Only one person can screen share at a time
        if self.participants.values().any(|p| p.screen_sharing) {
            return false;
        }

        if let Some(p) = self.participants.get_mut(&node) {
            p.screen_sharing = true;
            return true;
        }

        false
    }

    /// Stop screen sharing
    pub fn stop_screen_share(&mut self, node: NodeId) {
        if let Some(p) = self.participants.get_mut(&node) {
            p.screen_sharing = false;
        }
    }

    /// Get all participants
    pub fn participants(&self) -> Vec<&ParticipantState> {
        self.participants.values().collect()
    }
}

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

    #[test]
    fn test_livestream_swarm() {
        let broadcaster = NodeId::new(1);
        let mut swarm = LivestreamSwarm::new(1000, broadcaster, SwarmConfig::default());

        swarm.start();
        assert_eq!(swarm.state, SwarmState::Active);

        // Add viewers
        for i in 2..=10 {
            swarm.add_viewer(NodeId::new(i));
        }

        assert_eq!(swarm.viewer_count(), 9);
        assert!(swarm.can_broadcast(broadcaster));
        assert!(!swarm.can_broadcast(NodeId::new(2)));
    }

    #[test]
    fn test_livestream_topology_switch() {
        let broadcaster = NodeId::new(1);
        let config = SwarmConfig {
            star_to_tree_threshold: 5,
            ..Default::default()
        };
        let mut swarm = LivestreamSwarm::new(1000, broadcaster, config);

        // Add viewers until we switch to tree
        for i in 2..=10 {
            swarm.add_viewer(NodeId::new(i));
        }

        // Should have switched to tree
        assert!(matches!(swarm.topology, SwarmTopology::Tree(_)));
    }

    #[test]
    fn test_group_swarm() {
        let mut group = GroupSwarm::new(2000, 10);

        let time = StateTime::from_millis(0);

        assert!(group.add_participant(NodeId::new(1), time));
        assert!(group.add_participant(NodeId::new(2), time));
        assert!(group.add_participant(NodeId::new(3), time));

        assert_eq!(group.participant_count(), 3);

        // Test screen sharing
        assert!(group.start_screen_share(NodeId::new(1)));
        assert!(!group.start_screen_share(NodeId::new(2))); // Already sharing

        group.stop_screen_share(NodeId::new(1));
        assert!(group.start_screen_share(NodeId::new(2))); // Now allowed
    }
}