communitas-core 0.1.22

Core business logic for Communitas - PQC collaboration with virtual disks
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
// Copyright (c) 2025 Saorsa Labs Limited
//
// Licensed under the AGPL-3.0 license

//! CRDT Operations Primitives
//!
//! Implements the fundamental CRDT operation types:
//! - Last-Write-Wins (LWW) for scalar values
//! - Counter for concurrent increments
//! - Set operations (add/remove) for collections
//! - Tombstone handling for soft deletes

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use yrs::{Map, MapRef, ReadTxn, TransactionMut};

/// Last-Write-Wins (LWW) timestamp for conflict resolution
///
/// When two peers concurrently update the same field, the update with
/// the higher timestamp wins. If timestamps are equal, use a tie-breaker
/// (e.g., peer ID) for deterministic resolution.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct LamportTimestamp {
    /// Logical timestamp (milliseconds since epoch or logical counter)
    pub timestamp: i64,
    /// Tie-breaker: peer ID hash or counter
    pub tie_breaker: u64,
}

impl LamportTimestamp {
    /// Create a new timestamp with current system time
    pub fn now(peer_id: &str) -> Self {
        use std::time::SystemTime;
        let timestamp = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .map(|d| d.as_millis() as i64)
            .unwrap_or(0);

        // Use deterministic hash of peer ID as tie-breaker to ensure convergence across replicas
        let hash = blake3::hash(peer_id.as_bytes());
        let mut bytes = [0_u8; 8];
        bytes.copy_from_slice(&hash.as_bytes()[..8]);
        let tie_breaker = u64::from_le_bytes(bytes);

        Self {
            timestamp,
            tie_breaker,
        }
    }

    /// Check if this timestamp wins over another
    pub fn wins_over(&self, other: &Self) -> bool {
        self > other
    }
}

/// Last-Write-Wins Register
///
/// Stores a value with its associated timestamp. Updates replace the value
/// only if the new timestamp is greater.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LWWRegister<T> {
    pub value: T,
    pub timestamp: LamportTimestamp,
}

impl<T> LWWRegister<T> {
    pub fn new(value: T, timestamp: LamportTimestamp) -> Self {
        Self { value, timestamp }
    }

    /// Update the register if the new timestamp is greater
    pub fn update(&mut self, new_value: T, new_timestamp: LamportTimestamp) -> bool {
        if new_timestamp.wins_over(&self.timestamp) {
            self.value = new_value;
            self.timestamp = new_timestamp;
            true
        } else {
            false
        }
    }

    /// Merge with another register, keeping the value with higher timestamp
    pub fn merge(&mut self, other: &Self)
    where
        T: Clone,
    {
        if other.timestamp.wins_over(&self.timestamp) {
            self.value = other.value.clone();
            self.timestamp = other.timestamp;
        }
    }
}

/// CRDT Counter (G-Counter - Grow-only Counter)
///
/// Each peer has its own counter. The total is the sum of all peer counters.
/// This ensures concurrent increments are never lost.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Counter {
    /// Map of peer ID to their counter value
    counts: HashMap<String, i64>,
}

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

impl Counter {
    pub fn new() -> Self {
        Self {
            counts: HashMap::new(),
        }
    }

    /// Increment the counter for a specific peer
    pub fn increment(&mut self, peer_id: &str, amount: i64) {
        *self.counts.entry(peer_id.to_string()).or_insert(0) += amount;
    }

    /// Get the total count across all peers
    pub fn value(&self) -> i64 {
        self.counts.values().sum()
    }

    /// Merge with another counter (take max for each peer)
    pub fn merge(&mut self, other: &Self) {
        for (peer, count) in &other.counts {
            let entry = self.counts.entry(peer.clone()).or_insert(0);
            *entry = (*entry).max(*count);
        }
    }

    /// Get the counter map (for serialization)
    pub fn counts(&self) -> &HashMap<String, i64> {
        &self.counts
    }
}

/// CRDT Set Operations (OR-Set - Observed-Remove Set)
///
/// Each element has a unique identifier (UUID). Elements can be added and removed.
/// An element exists if it was added and not removed with the same ID.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SetOperation {
    /// Element identifier (e.g., user ID, channel ID)
    pub element_id: String,
    /// Operation type
    pub op_type: SetOpType,
    /// Unique operation ID (UUID) to distinguish add/remove operations
    pub operation_id: String,
    /// Timestamp for ordering
    pub timestamp: LamportTimestamp,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum SetOpType {
    Add,
    Remove,
}

/// OR-Set (Observed-Remove Set)
///
/// Tracks add and remove operations. An element is in the set if it has
/// been added with an operation ID that hasn't been removed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ORSet {
    /// Map of element ID -> set of (add_operation_id, timestamp)
    adds: HashMap<String, Vec<(String, LamportTimestamp)>>,
    /// Map of element ID -> set of (remove_operation_id, timestamp)
    removes: HashMap<String, Vec<(String, LamportTimestamp)>>,
}

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

impl ORSet {
    pub fn new() -> Self {
        Self {
            adds: HashMap::new(),
            removes: HashMap::new(),
        }
    }

    /// Add an element with a unique operation ID
    pub fn add(&mut self, element_id: String, operation_id: String, timestamp: LamportTimestamp) {
        self.adds
            .entry(element_id)
            .or_default()
            .push((operation_id, timestamp));
    }

    /// Remove an element (removes all add operations for this element)
    pub fn remove(
        &mut self,
        element_id: String,
        _operation_id: String,
        timestamp: LamportTimestamp,
    ) {
        // Get all current add operation IDs for this element
        if let Some(adds) = self.adds.get(&element_id) {
            let add_ids: Vec<String> = adds.iter().map(|(id, _)| id.clone()).collect();
            for add_id in add_ids {
                self.removes
                    .entry(element_id.clone())
                    .or_default()
                    .push((add_id, timestamp));
            }
        }
    }

    /// Check if an element is in the set
    /// (has an add operation not matched by a remove)
    pub fn contains(&self, element_id: &str) -> bool {
        if let Some(adds) = self.adds.get(element_id) {
            if let Some(removes) = self.removes.get(element_id) {
                // Element is in set if there's an add operation not in removes
                adds.iter()
                    .any(|(add_id, _)| !removes.iter().any(|(rem_id, _)| rem_id == add_id))
            } else {
                // No removes, so element is in set if it has adds
                !adds.is_empty()
            }
        } else {
            false
        }
    }

    /// Get all elements in the set
    pub fn elements(&self) -> Vec<String> {
        self.adds
            .keys()
            .filter(|k| self.contains(k))
            .cloned()
            .collect()
    }

    /// Merge with another OR-Set
    pub fn merge(&mut self, other: &Self) {
        // Merge adds
        for (element, ops) in &other.adds {
            self.adds
                .entry(element.clone())
                .or_default()
                .extend(ops.clone());
        }

        // Merge removes
        for (element, ops) in &other.removes {
            self.removes
                .entry(element.clone())
                .or_default()
                .extend(ops.clone());
        }
    }
}

/// Tombstone for soft-delete semantics
///
/// Used to mark entities as deleted without removing them from the CRDT.
/// The tombstone timestamp is treated with LWW semantics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tombstone {
    pub deleted_at: Option<LamportTimestamp>,
}

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

impl Tombstone {
    pub fn new() -> Self {
        Self { deleted_at: None }
    }

    pub fn delete(&mut self, timestamp: LamportTimestamp) {
        if let Some(existing) = self.deleted_at {
            if timestamp.wins_over(&existing) {
                self.deleted_at = Some(timestamp);
            }
        } else {
            self.deleted_at = Some(timestamp);
        }
    }

    pub fn is_deleted(&self) -> bool {
        self.deleted_at.is_some()
    }

    pub fn merge(&mut self, other: &Self) {
        match (self.deleted_at, other.deleted_at) {
            (Some(a), Some(b)) => {
                self.deleted_at = Some(if b.wins_over(&a) { b } else { a });
            }
            (None, Some(b)) => {
                self.deleted_at = Some(b);
            }
            _ => {}
        }
    }
}

/// Helper functions for working with CRDT operations in Yrs documents
/// Set an LWW value in a Yrs map
pub fn set_lww_value(
    txn: &mut TransactionMut,
    map: &MapRef,
    key: &str,
    value: impl Into<String>,
    timestamp: LamportTimestamp,
) -> Result<()> {
    // Store value with timestamp as key suffix
    let value_key = format!("{}_value", key);
    let timestamp_key = format!("{}_timestamp", key);
    let tie_breaker_key = format!("{}_tie_breaker", key);

    map.insert(txn, value_key.as_str(), value.into());
    map.insert(txn, timestamp_key.as_str(), timestamp.timestamp);
    map.insert(txn, tie_breaker_key.as_str(), timestamp.tie_breaker as i64);

    Ok(())
}

/// Get an LWW value from a Yrs map
pub fn get_lww_value(
    txn: &impl ReadTxn,
    map: &MapRef,
    key: &str,
) -> Result<Option<(String, LamportTimestamp)>> {
    let value_key = format!("{}_value", key);
    let timestamp_key = format!("{}_timestamp", key);
    let tie_breaker_key = format!("{}_tie_breaker", key);

    if let Some(value) = map.get(txn, &value_key) {
        let timestamp = map
            .get(txn, &timestamp_key)
            .and_then(|v| i64::try_from(v).ok())
            .unwrap_or(0);
        let tie_breaker = map
            .get(txn, &tie_breaker_key)
            .and_then(|v| i64::try_from(v).ok())
            .unwrap_or(0) as u64;

        let value_str = String::try_from(value).unwrap_or_default();

        Ok(Some((
            value_str,
            LamportTimestamp {
                timestamp,
                tie_breaker,
            },
        )))
    } else {
        Ok(None)
    }
}

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

    #[test]
    fn test_lamport_timestamp_ordering() {
        let t1 = LamportTimestamp {
            timestamp: 100,
            tie_breaker: 1,
        };
        let t2 = LamportTimestamp {
            timestamp: 200,
            tie_breaker: 1,
        };
        let t3 = LamportTimestamp {
            timestamp: 100,
            tie_breaker: 2,
        };

        assert!(t2.wins_over(&t1));
        assert!(!t1.wins_over(&t2));
        assert!(t3.wins_over(&t1));
        assert!(!t1.wins_over(&t3));
    }

    #[test]
    fn test_lww_register() {
        let t1 = LamportTimestamp {
            timestamp: 100,
            tie_breaker: 1,
        };
        let t2 = LamportTimestamp {
            timestamp: 200,
            tie_breaker: 1,
        };

        let mut register = LWWRegister::new("value1".to_string(), t1);
        assert_eq!(register.value, "value1");

        // Update with newer timestamp succeeds
        assert!(register.update("value2".to_string(), t2));
        assert_eq!(register.value, "value2");

        // Update with older timestamp fails
        assert!(!register.update("value3".to_string(), t1));
        assert_eq!(register.value, "value2");
    }

    #[test]
    fn test_counter() {
        let mut counter = Counter::new();

        counter.increment("peer1", 1);
        assert_eq!(counter.value(), 1);

        counter.increment("peer2", 2);
        assert_eq!(counter.value(), 3);

        counter.increment("peer1", 1);
        assert_eq!(counter.value(), 4);

        // Test merge
        let mut counter2 = Counter::new();
        counter2.increment("peer1", 5);
        counter2.increment("peer3", 3);

        counter.merge(&counter2);
        assert_eq!(counter.value(), 10); // max(2, 5) + 2 + 3 = 5 + 2 + 3 = 10
    }

    #[test]
    fn test_or_set() {
        let mut set = ORSet::new();
        let t1 = LamportTimestamp {
            timestamp: 100,
            tie_breaker: 1,
        };

        // Add element
        set.add("user1".to_string(), "op1".to_string(), t1);
        assert!(set.contains("user1"));

        // Remove element
        set.remove("user1".to_string(), "op2".to_string(), t1);
        assert!(!set.contains("user1"));

        // Add again with different operation ID
        set.add("user1".to_string(), "op3".to_string(), t1);
        assert!(set.contains("user1"));
    }

    #[test]
    fn test_tombstone() {
        let mut tombstone = Tombstone::new();
        assert!(!tombstone.is_deleted());

        let t1 = LamportTimestamp {
            timestamp: 100,
            tie_breaker: 1,
        };
        tombstone.delete(t1);
        assert!(tombstone.is_deleted());

        // Older delete doesn't override
        let t0 = LamportTimestamp {
            timestamp: 50,
            tie_breaker: 1,
        };
        tombstone.delete(t0);
        assert_eq!(tombstone.deleted_at, Some(t1));

        // Newer delete overrides
        let t2 = LamportTimestamp {
            timestamp: 200,
            tie_breaker: 1,
        };
        tombstone.delete(t2);
        assert_eq!(tombstone.deleted_at, Some(t2));
    }
}