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
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
//! Peer Sync Protocol — bidirectional state synchronisation using vector clocks and CRDTs.
//!
//! [`PeerSyncProtocol`] implements a production-grade, conflict-free replicated data type
//! (CRDT) engine.  Each node maintains a [`VectorClock`] that it increments on every local
//! write.  Remote operations are applied through [`PeerSyncProtocol::apply_remote_op`], which
//! resolves write–write conflicts according to a pluggable [`ConflictPolicy`].
//!
//! ## Design goals
//! - **Convergence**: any two nodes that have received the same set of operations end up in
//!   identical state regardless of delivery order.
//! - **No data loss by default**: `LastWriteWins` and `HighestClock` policies always keep
//!   one version; `MergeBytes` retains both.
//! - **Tombstoning**: deleted keys are permanently suppressed so late-arriving puts for the
//!   same key are silently discarded.
//! - **Delta sync**: [`PeerSyncProtocol::generate_delta`] returns only the entries that a
//!   remote peer is missing, batched into a single [`SyncOperation::Merge`].
//!
//! ## Example
//! ```rust
//! use ipfrs_network::peer_sync_protocol::{
//!     PeerSyncProtocol, ConflictPolicy, SyncOperation, VectorClock,
//! };
//!
//! let mut node_a = PeerSyncProtocol::new("node-a".to_string(), ConflictPolicy::LastWriteWins);
//! let entry = node_a.local_put("greeting".to_string(), b"hello".to_vec(), 1000);
//!
//! let mut node_b = PeerSyncProtocol::new("node-b".to_string(), ConflictPolicy::LastWriteWins);
//! let op = SyncOperation::Put { entry };
//! node_b.apply_remote_op("node-a", op, 1001).unwrap();
//!
//! assert_eq!(node_b.get("greeting").unwrap().value, b"hello");
//! ```

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

use thiserror::Error;

// ─── Error type ─────────────────────────────────────────────────────────────

/// Errors returned by [`PeerSyncProtocol`] operations.
#[derive(Clone, Debug, PartialEq, Eq, Error)]
pub enum SyncError {
    /// The key has already been deleted and its tombstone is permanent.
    #[error("key '{0}' is tombstoned and cannot be re-inserted")]
    Tombstoned(String),

    /// A conflict was detected but the active policy is [`ConflictPolicy::RejectConflict`].
    #[error("conflict rejected for key '{key}'")]
    ConflictRejected { key: String },

    /// The operation is structurally invalid (e.g. an empty Merge).
    #[error("invalid sync operation")]
    InvalidOperation,
}

// ─── VectorClock ─────────────────────────────────────────────────────────────

/// A logical clock that tracks causality per node.
///
/// Each node maintains a counter that is incremented whenever the node produces
/// a new event.  The happened-before relation (→) is defined by Lamport/Mattern
/// vector clock rules.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct VectorClock {
    /// Map from `node_id` to that node's logical counter.
    pub entries: HashMap<String, u64>,
}

impl VectorClock {
    /// Create a new, zeroed vector clock.
    #[must_use]
    pub fn new() -> Self {
        Self {
            entries: HashMap::new(),
        }
    }

    /// Increment the counter for `node_id` by one.
    pub fn increment(&mut self, node_id: &str) {
        let counter = self.entries.entry(node_id.to_string()).or_insert(0);
        *counter = counter.saturating_add(1);
    }

    /// Element-wise maximum of `self` and `other`.
    ///
    /// The result is the smallest vector clock that *dominates* both inputs,
    /// i.e. `self.merge(other).dominates(self) && self.merge(other).dominates(other)`.
    #[must_use]
    pub fn merge(&self, other: &VectorClock) -> VectorClock {
        let mut result = self.entries.clone();
        for (node, &count) in &other.entries {
            let entry = result.entry(node.clone()).or_insert(0);
            if count > *entry {
                *entry = count;
            }
        }
        VectorClock { entries: result }
    }

    /// `self → other`: every component of `self` is ≤ the corresponding component
    /// of `other`, and at least one is strictly less.
    #[must_use]
    pub fn happens_before(&self, other: &VectorClock) -> bool {
        let mut strictly_less = false;
        for (node, &self_count) in &self.entries {
            let other_count = other.entries.get(node).copied().unwrap_or(0);
            if self_count > other_count {
                return false;
            }
            if self_count < other_count {
                strictly_less = true;
            }
        }
        // Also check nodes that appear only in `other`.
        for (node, &other_count) in &other.entries {
            if !self.entries.contains_key(node) && other_count > 0 {
                strictly_less = true;
            }
        }
        strictly_less
    }

    /// Two clocks are concurrent when neither happens-before the other.
    #[must_use]
    pub fn concurrent_with(&self, other: &VectorClock) -> bool {
        !self.happens_before(other) && !other.happens_before(self)
    }

    /// `self` dominates `other`: every component of `self` is ≥ the corresponding
    /// component of `other` (i.e. `other → self` or `other == self`).
    ///
    /// Note: this is not strict; equal clocks both dominate each other.
    #[must_use]
    pub fn dominates(&self, other: &VectorClock) -> bool {
        for (node, &other_count) in &other.entries {
            let self_count = self.entries.get(node).copied().unwrap_or(0);
            if self_count < other_count {
                return false;
            }
        }
        true
    }

    /// The maximum counter value across all nodes, or `0` if the clock is empty.
    #[must_use]
    pub fn max_value(&self) -> u64 {
        self.entries.values().copied().max().unwrap_or(0)
    }

    /// Return the counter for `node_id`, or `0` if not present.
    #[must_use]
    pub fn get(&self, node_id: &str) -> u64 {
        self.entries.get(node_id).copied().unwrap_or(0)
    }
}

// ─── SyncEntry ───────────────────────────────────────────────────────────────

/// A key-value record annotated with causal metadata.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SyncEntry {
    /// The logical key for this entry.
    pub key: String,
    /// Arbitrary payload bytes.
    pub value: Vec<u8>,
    /// The vector clock at the time of the last write.
    pub clock: VectorClock,
    /// The node that produced this entry.
    pub node_id: String,
    /// Wall-clock timestamp (milliseconds since Unix epoch, supplied by the caller).
    pub timestamp: u64,
}

// ─── SyncOperation ───────────────────────────────────────────────────────────

/// An atomic operation that can be applied to a [`PeerSyncProtocol`].
#[derive(Clone, Debug)]
pub enum SyncOperation {
    /// Insert or update a single entry.
    Put { entry: SyncEntry },
    /// Delete a key, recording the causal context at which deletion occurred.
    Delete { key: String, clock: VectorClock },
    /// Apply a batch of entries (used for delta and full sync).
    Merge { entries: Vec<SyncEntry> },
}

// ─── SyncState ───────────────────────────────────────────────────────────────

/// The replicated state held by a single node.
#[derive(Clone, Debug)]
pub struct SyncState {
    /// Live entries indexed by key.
    pub entries: HashMap<String, SyncEntry>,
    /// Keys that have been deleted; these keys can never be re-inserted.
    pub tombstones: HashSet<String>,
    /// This node's current vector clock (incremented on every local write/delete).
    pub local_clock: VectorClock,
    /// Stable identifier for this node.
    pub node_id: String,
}

impl SyncState {
    fn new(node_id: String) -> Self {
        Self {
            entries: HashMap::new(),
            tombstones: HashSet::new(),
            local_clock: VectorClock::new(),
            node_id,
        }
    }
}

// ─── ConflictPolicy ──────────────────────────────────────────────────────────

/// Strategy used to resolve write–write conflicts (concurrent updates to the same key).
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ConflictPolicy {
    /// Keep the entry with the higher `timestamp`; on tie, prefer the incoming entry.
    LastWriteWins,
    /// Keep the entry whose clock *dominates* the other.  When both clocks are
    /// concurrent, prefer the incoming entry.
    HighestClock,
    /// Concatenate both values separated by `separator`.
    MergeBytes { separator: u8 },
    /// Reject the incoming entry and keep the existing one unchanged.
    RejectConflict,
}

// ─── PeerSyncProtocol ────────────────────────────────────────────────────────

/// Bidirectional state-synchronisation protocol backed by vector clocks and CRDTs.
pub struct PeerSyncProtocol {
    /// The local replicated state.
    pub state: SyncState,
    /// How concurrent writes to the same key are resolved.
    pub conflict_policy: ConflictPolicy,
    /// Operations that have been enqueued but not yet dispatched to remote peers.
    pub pending_ops: VecDeque<SyncOperation>,
    /// Audit log: `(peer_id, operation, wall_timestamp)`.
    pub sync_log: VecDeque<(String, SyncOperation, u64)>,
}

// ─── SyncStats ───────────────────────────────────────────────────────────────

/// A snapshot of key metrics for a [`PeerSyncProtocol`] instance.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PspSyncStats {
    /// Number of live (non-tombstoned) entries.
    pub total_entries: usize,
    /// Number of tombstoned (deleted) keys.
    pub tombstone_count: usize,
    /// Number of pending outbound operations.
    pub pending_ops: usize,
    /// Number of entries in the sync audit log.
    pub sync_log_size: usize,
    /// The highest counter value in the local vector clock.
    pub local_clock_max: u64,
}

// ─── Implementation ──────────────────────────────────────────────────────────

impl PeerSyncProtocol {
    /// Create a new protocol instance for the given node.
    ///
    /// # Arguments
    /// * `node_id` — a stable, globally-unique identifier for this node.
    /// * `conflict_policy` — the policy applied when concurrent writes collide.
    pub fn new(node_id: String, conflict_policy: ConflictPolicy) -> Self {
        Self {
            state: SyncState::new(node_id),
            conflict_policy,
            pending_ops: VecDeque::new(),
            sync_log: VecDeque::new(),
        }
    }

    // ── Local mutations ───────────────────────────────────────────────────

    /// Write `value` at `key` and return the resulting [`SyncEntry`].
    ///
    /// The local vector clock is incremented before the entry is stored so that
    /// the returned entry strictly succeeds every previously stored entry from
    /// this node.
    pub fn local_put(&mut self, key: String, value: Vec<u8>, now: u64) -> SyncEntry {
        self.state
            .local_clock
            .increment(&self.state.node_id.clone());
        let entry = SyncEntry {
            key: key.clone(),
            value,
            clock: self.state.local_clock.clone(),
            node_id: self.state.node_id.clone(),
            timestamp: now,
        };
        self.state.entries.insert(key, entry.clone());
        self.pending_ops.push_back(SyncOperation::Put {
            entry: entry.clone(),
        });
        entry
    }

    /// Mark `key` as deleted.
    ///
    /// The key is added to the tombstone set and any live entry for it is removed.
    /// The local clock is incremented so that the deletion causally succeeds any
    /// previous entry.
    pub fn local_delete(&mut self, key: String, _now: u64) {
        self.state
            .local_clock
            .increment(&self.state.node_id.clone());
        let clock = self.state.local_clock.clone();
        self.state.tombstones.insert(key.clone());
        self.state.entries.remove(&key);
        self.pending_ops.push_back(SyncOperation::Delete {
            key: key.clone(),
            clock,
        });
    }

    // ── Remote operations ─────────────────────────────────────────────────

    /// Apply an operation received from a remote peer.
    ///
    /// The sync audit log is updated regardless of whether the operation caused
    /// a state change.
    ///
    /// # Errors
    /// - [`SyncError::Tombstoned`] — a `Put` arrived for a tombstoned key.
    /// - [`SyncError::ConflictRejected`] — [`ConflictPolicy::RejectConflict`] rejected a
    ///   conflicting `Put`.
    /// - [`SyncError::InvalidOperation`] — a `Merge` with zero entries was supplied.
    pub fn apply_remote_op(
        &mut self,
        peer_id: &str,
        op: SyncOperation,
        now: u64,
    ) -> Result<(), SyncError> {
        self.sync_log
            .push_back((peer_id.to_string(), op.clone(), now));

        match op {
            SyncOperation::Put { ref entry } => {
                self.apply_put(entry)?;
            }
            SyncOperation::Delete { ref key, ref clock } => {
                self.apply_delete(key, clock);
            }
            SyncOperation::Merge { ref entries } => {
                if entries.is_empty() {
                    return Err(SyncError::InvalidOperation);
                }
                for entry in entries {
                    // Individual tombstone / conflict errors are silently skipped
                    // during a bulk merge to preserve convergence.
                    let _ = self.apply_put(entry);
                }
            }
        }
        Ok(())
    }

    // ── Internal apply helpers ────────────────────────────────────────────

    fn apply_put(&mut self, incoming: &SyncEntry) -> Result<(), SyncError> {
        let key = &incoming.key;

        // Tombstoned keys are permanently suppressed.
        if self.state.tombstones.contains(key) {
            return Err(SyncError::Tombstoned(key.clone()));
        }

        match self.state.entries.get(key) {
            None => {
                // Fresh key — insert directly and advance local clock.
                self.state.local_clock = self.state.local_clock.merge(&incoming.clock);
                self.state.entries.insert(key.clone(), incoming.clone());
            }
            Some(existing) => {
                // Identical clock → idempotent no-op.
                if existing.clock == incoming.clock {
                    return Ok(());
                }

                // Incoming is strictly older than existing → discard.
                if incoming.clock.happens_before(&existing.clock) {
                    return Ok(());
                }

                // Existing is strictly older → replace with incoming.
                if existing.clock.happens_before(&incoming.clock) {
                    self.state.local_clock = self.state.local_clock.merge(&incoming.clock);
                    self.state.entries.insert(key.clone(), incoming.clone());
                    return Ok(());
                }

                // Concurrent writes — apply conflict policy.
                let existing_clone = existing.clone();
                let resolved = self.resolve_conflict_inner(&existing_clone, incoming)?;
                self.state.local_clock = self.state.local_clock.merge(&resolved.clock);
                self.state.entries.insert(key.clone(), resolved);
            }
        }
        Ok(())
    }

    fn apply_delete(&mut self, key: &str, clock: &VectorClock) {
        self.state.tombstones.insert(key.to_string());
        self.state.entries.remove(key);
        self.state.local_clock = self.state.local_clock.merge(clock);
    }

    // ── Conflict resolution ───────────────────────────────────────────────

    /// Resolve a write–write conflict between `existing` and `incoming` according
    /// to the active [`ConflictPolicy`].
    ///
    /// # Errors
    /// Returns [`SyncError::ConflictRejected`] when the policy is
    /// [`ConflictPolicy::RejectConflict`].
    pub fn resolve_conflict(
        &self,
        existing: &SyncEntry,
        incoming: &SyncEntry,
    ) -> Result<SyncEntry, SyncError> {
        self.resolve_conflict_inner(existing, incoming)
    }

    fn resolve_conflict_inner(
        &self,
        existing: &SyncEntry,
        incoming: &SyncEntry,
    ) -> Result<SyncEntry, SyncError> {
        match &self.conflict_policy {
            ConflictPolicy::LastWriteWins => {
                // Higher timestamp wins; on tie prefer incoming.
                if existing.timestamp > incoming.timestamp {
                    Ok(existing.clone())
                } else {
                    Ok(incoming.clone())
                }
            }
            ConflictPolicy::HighestClock => {
                if existing.clock.dominates(&incoming.clock)
                    && !incoming.clock.dominates(&existing.clock)
                {
                    // existing strictly dominates
                    Ok(existing.clone())
                } else {
                    // incoming dominates or they are concurrent → prefer incoming
                    Ok(incoming.clone())
                }
            }
            ConflictPolicy::MergeBytes { separator } => {
                let sep = *separator;
                let mut merged_value = existing.value.clone();
                merged_value.push(sep);
                merged_value.extend_from_slice(&incoming.value);
                let merged_clock = existing.clock.merge(&incoming.clock);
                Ok(SyncEntry {
                    key: existing.key.clone(),
                    value: merged_value,
                    clock: merged_clock,
                    node_id: incoming.node_id.clone(),
                    timestamp: existing.timestamp.max(incoming.timestamp),
                })
            }
            ConflictPolicy::RejectConflict => Err(SyncError::ConflictRejected {
                key: existing.key.clone(),
            }),
        }
    }

    // ── Delta / full sync ─────────────────────────────────────────────────

    /// Generate the set of local entries that a peer is missing.
    ///
    /// An entry is considered missing from the peer when its clock does **not**
    /// happen-before `since_clock` (i.e. the peer has not yet observed it).
    /// All matching entries are returned as a single [`SyncOperation::Merge`].
    /// An empty `Vec` is returned when nothing needs to be sent.
    pub fn generate_delta(&self, since_clock: &VectorClock) -> Vec<SyncOperation> {
        let missing: Vec<SyncEntry> = self
            .state
            .entries
            .values()
            .filter(|e| !e.clock.happens_before(since_clock))
            .cloned()
            .collect();

        if missing.is_empty() {
            return Vec::new();
        }
        vec![SyncOperation::Merge { entries: missing }]
    }

    /// Return all live (non-tombstoned) entries as a single
    /// [`SyncOperation::Merge`] suitable for a full-state transfer.
    pub fn full_sync(&self) -> SyncOperation {
        SyncOperation::Merge {
            entries: self.state.entries.values().cloned().collect(),
        }
    }

    // ── Read accessors ────────────────────────────────────────────────────

    /// Look up a live entry by key.
    #[must_use]
    pub fn get(&self, key: &str) -> Option<&SyncEntry> {
        self.state.entries.get(key)
    }

    /// Returns `true` if `key` exists as a live entry.
    #[must_use]
    pub fn contains(&self, key: &str) -> bool {
        self.state.entries.contains_key(key)
    }

    /// Returns `true` if `key` has been deleted (tombstoned).
    #[must_use]
    pub fn is_tombstoned(&self, key: &str) -> bool {
        self.state.tombstones.contains(key)
    }

    /// Number of live (non-tombstoned) entries.
    #[must_use]
    pub fn entry_count(&self) -> usize {
        self.state.entries.len()
    }

    /// The current vector-clock counter for `node_id`, or `0` if unknown.
    #[must_use]
    pub fn clock_value(&self, node_id: &str) -> u64 {
        self.state.local_clock.get(node_id)
    }

    /// Return a statistics snapshot for this instance.
    #[must_use]
    pub fn stats(&self) -> PspSyncStats {
        PspSyncStats {
            total_entries: self.state.entries.len(),
            tombstone_count: self.state.tombstones.len(),
            pending_ops: self.pending_ops.len(),
            sync_log_size: self.sync_log.len(),
            local_clock_max: self.state.local_clock.max_value(),
        }
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use crate::peer_sync_protocol::{
        ConflictPolicy, PeerSyncProtocol, PspSyncStats, SyncEntry, SyncError, SyncOperation,
        VectorClock,
    };

    // ── VectorClock ──────────────────────────────────────────────────────

    #[test]
    fn vc_new_is_empty() {
        let vc = VectorClock::new();
        assert!(vc.entries.is_empty());
        assert_eq!(vc.max_value(), 0);
    }

    #[test]
    fn vc_increment_creates_entry() {
        let mut vc = VectorClock::new();
        vc.increment("a");
        assert_eq!(vc.get("a"), 1);
    }

    #[test]
    fn vc_increment_twice() {
        let mut vc = VectorClock::new();
        vc.increment("a");
        vc.increment("a");
        assert_eq!(vc.get("a"), 2);
    }

    #[test]
    fn vc_increment_different_nodes() {
        let mut vc = VectorClock::new();
        vc.increment("a");
        vc.increment("b");
        assert_eq!(vc.get("a"), 1);
        assert_eq!(vc.get("b"), 1);
    }

    #[test]
    fn vc_merge_element_wise_max() {
        let mut a = VectorClock::new();
        a.increment("x");
        a.increment("x"); // x=2
        a.increment("y"); // y=1

        let mut b = VectorClock::new();
        b.increment("x"); // x=1
        b.increment("z"); // z=1

        let merged = a.merge(&b);
        assert_eq!(merged.get("x"), 2);
        assert_eq!(merged.get("y"), 1);
        assert_eq!(merged.get("z"), 1);
    }

    #[test]
    fn vc_merge_is_commutative() {
        let mut a = VectorClock::new();
        a.increment("a");
        a.increment("a");
        let mut b = VectorClock::new();
        b.increment("a");
        b.increment("b");

        assert_eq!(a.merge(&b), b.merge(&a));
    }

    #[test]
    fn vc_happens_before_strict() {
        let mut old = VectorClock::new();
        old.increment("n");
        let mut new = VectorClock::new();
        new.increment("n");
        new.increment("n");

        assert!(old.happens_before(&new));
        assert!(!new.happens_before(&old));
    }

    #[test]
    fn vc_equal_clocks_not_happens_before() {
        let mut a = VectorClock::new();
        a.increment("n");
        let b = a.clone();
        assert!(!a.happens_before(&b));
        assert!(!b.happens_before(&a));
    }

    #[test]
    fn vc_concurrent_with() {
        let mut a = VectorClock::new();
        a.increment("a");
        let mut b = VectorClock::new();
        b.increment("b");

        assert!(a.concurrent_with(&b));
        assert!(b.concurrent_with(&a));
    }

    #[test]
    fn vc_not_concurrent_when_ordered() {
        let mut early = VectorClock::new();
        early.increment("n");
        let mut late = early.clone();
        late.increment("n");

        assert!(!early.concurrent_with(&late));
        assert!(!late.concurrent_with(&early));
    }

    #[test]
    fn vc_dominates_self() {
        let mut vc = VectorClock::new();
        vc.increment("n");
        assert!(vc.dominates(&vc.clone()));
    }

    #[test]
    fn vc_dominates_older() {
        let mut old = VectorClock::new();
        old.increment("n");
        let mut new = old.clone();
        new.increment("n");

        assert!(new.dominates(&old));
        assert!(!old.dominates(&new));
    }

    #[test]
    fn vc_get_missing_node_returns_zero() {
        let vc = VectorClock::new();
        assert_eq!(vc.get("nonexistent"), 0);
    }

    #[test]
    fn vc_max_value_multiple_nodes() {
        let mut vc = VectorClock::new();
        vc.increment("a");
        vc.increment("b");
        vc.increment("b");
        vc.increment("b");
        assert_eq!(vc.max_value(), 3);
    }

    // ── PeerSyncProtocol — local operations ──────────────────────────────

    #[test]
    fn local_put_stores_entry() {
        let mut proto = PeerSyncProtocol::new("node-a".to_string(), ConflictPolicy::LastWriteWins);
        proto.local_put("k1".to_string(), b"hello".to_vec(), 100);
        assert!(proto.contains("k1"));
        assert_eq!(
            proto.get("k1").expect("test: k1 entry must exist").value,
            b"hello"
        );
    }

    #[test]
    fn local_put_increments_clock() {
        let mut proto = PeerSyncProtocol::new("node-a".to_string(), ConflictPolicy::LastWriteWins);
        proto.local_put("k".to_string(), b"v".to_vec(), 1);
        assert_eq!(proto.clock_value("node-a"), 1);
        proto.local_put("k2".to_string(), b"v2".to_vec(), 2);
        assert_eq!(proto.clock_value("node-a"), 2);
    }

    #[test]
    fn local_put_returns_correct_entry() {
        let mut proto = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        let entry = proto.local_put("key".to_string(), b"val".to_vec(), 42);
        assert_eq!(entry.key, "key");
        assert_eq!(entry.value, b"val");
        assert_eq!(entry.timestamp, 42);
        assert_eq!(entry.node_id, "n");
    }

    #[test]
    fn local_put_enqueues_pending_op() {
        let mut proto = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        proto.local_put("k".to_string(), b"v".to_vec(), 1);
        assert_eq!(proto.pending_ops.len(), 1);
    }

    #[test]
    fn local_delete_tombstones_key() {
        let mut proto = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        proto.local_put("k".to_string(), b"v".to_vec(), 1);
        proto.local_delete("k".to_string(), 2);
        assert!(!proto.contains("k"));
        assert!(proto.is_tombstoned("k"));
    }

    #[test]
    fn local_delete_increments_clock() {
        let mut proto = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        proto.local_delete("k".to_string(), 5);
        assert_eq!(proto.clock_value("n"), 1);
    }

    // ── apply_remote_op — Put ────────────────────────────────────────────

    #[test]
    fn apply_remote_put_fresh_key() {
        let mut node_a = PeerSyncProtocol::new("a".to_string(), ConflictPolicy::LastWriteWins);
        let entry = node_a.local_put("x".to_string(), b"data".to_vec(), 10);

        let mut node_b = PeerSyncProtocol::new("b".to_string(), ConflictPolicy::LastWriteWins);
        node_b
            .apply_remote_op("a", SyncOperation::Put { entry }, 11)
            .expect("test: apply remote put for fresh key x should succeed");
        assert_eq!(
            node_b
                .get("x")
                .expect("test: x entry must exist after remote put")
                .value,
            b"data"
        );
    }

    #[test]
    fn apply_remote_put_tombstoned_returns_error() {
        let mut node = PeerSyncProtocol::new("a".to_string(), ConflictPolicy::LastWriteWins);
        node.local_delete("k".to_string(), 1);

        let mut vc = VectorClock::new();
        vc.increment("b");
        let entry = SyncEntry {
            key: "k".to_string(),
            value: b"late".to_vec(),
            clock: vc,
            node_id: "b".to_string(),
            timestamp: 5,
        };
        let result = node.apply_remote_op("b", SyncOperation::Put { entry }, 6);
        assert_eq!(result, Err(SyncError::Tombstoned("k".to_string())));
    }

    #[test]
    fn apply_remote_put_older_clock_discarded() {
        let mut node = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        // Insert a newer entry locally.
        node.local_put("k".to_string(), b"new".to_vec(), 100);

        // Simulate an older remote entry.
        let mut old_vc = VectorClock::new();
        old_vc.increment("remote");
        let old_entry = SyncEntry {
            key: "k".to_string(),
            value: b"old".to_vec(),
            clock: old_vc,
            node_id: "remote".to_string(),
            timestamp: 1,
        };
        node.apply_remote_op("remote", SyncOperation::Put { entry: old_entry }, 2)
            .expect("test: apply older remote put should succeed without error");
        // Value must not be overwritten by an older entry.
        assert_eq!(
            node.get("k")
                .expect("test: k entry must still exist after discarded old entry")
                .value,
            b"new"
        );
    }

    #[test]
    fn apply_remote_delete_removes_entry() {
        let mut node_a = PeerSyncProtocol::new("a".to_string(), ConflictPolicy::LastWriteWins);
        node_a.local_put("x".to_string(), b"hi".to_vec(), 1);

        let mut del_clock = VectorClock::new();
        del_clock.increment("b");
        node_a
            .apply_remote_op(
                "b",
                SyncOperation::Delete {
                    key: "x".to_string(),
                    clock: del_clock,
                },
                2,
            )
            .expect("test: apply remote delete for key x should succeed");
        assert!(!node_a.contains("x"));
        assert!(node_a.is_tombstoned("x"));
    }

    #[test]
    fn apply_remote_merge_applies_all_entries() {
        let mut node_a = PeerSyncProtocol::new("a".to_string(), ConflictPolicy::LastWriteWins);
        let e1 = node_a.local_put("k1".to_string(), b"v1".to_vec(), 10);
        let e2 = node_a.local_put("k2".to_string(), b"v2".to_vec(), 11);

        let mut node_b = PeerSyncProtocol::new("b".to_string(), ConflictPolicy::LastWriteWins);
        node_b
            .apply_remote_op(
                "a",
                SyncOperation::Merge {
                    entries: vec![e1, e2],
                },
                12,
            )
            .expect("test: apply remote merge with two entries should succeed");
        assert_eq!(node_b.entry_count(), 2);
        assert!(node_b.contains("k1"));
        assert!(node_b.contains("k2"));
    }

    #[test]
    fn apply_remote_merge_empty_returns_invalid() {
        let mut node = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        let result = node.apply_remote_op("peer", SyncOperation::Merge { entries: vec![] }, 1);
        assert_eq!(result, Err(SyncError::InvalidOperation));
    }

    // ── Conflict resolution policies ─────────────────────────────────────

    #[test]
    fn conflict_last_write_wins_picks_higher_timestamp() {
        let mut proto = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        // Insert existing entry.
        proto.local_put("k".to_string(), b"existing".to_vec(), 50);

        // Incoming has higher timestamp → should win.
        let mut incoming_vc = VectorClock::new();
        incoming_vc.increment("peer");
        let incoming = SyncEntry {
            key: "k".to_string(),
            value: b"incoming".to_vec(),
            clock: incoming_vc,
            node_id: "peer".to_string(),
            timestamp: 100,
        };
        proto
            .apply_remote_op("peer", SyncOperation::Put { entry: incoming }, 101)
            .expect("test: apply remote put with higher timestamp should succeed");
        assert_eq!(
            proto
                .get("k")
                .expect("test: k entry must exist after LastWriteWins conflict")
                .value,
            b"incoming"
        );
    }

    #[test]
    fn conflict_last_write_wins_keeps_existing_on_higher_ts() {
        let mut proto = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        proto.local_put("k".to_string(), b"existing".to_vec(), 200);

        let mut incoming_vc = VectorClock::new();
        incoming_vc.increment("peer");
        let incoming = SyncEntry {
            key: "k".to_string(),
            value: b"stale".to_vec(),
            clock: incoming_vc,
            node_id: "peer".to_string(),
            timestamp: 100,
        };
        proto
            .apply_remote_op("peer", SyncOperation::Put { entry: incoming }, 201)
            .expect("test: apply remote put with lower timestamp should succeed");
        assert_eq!(
            proto
                .get("k")
                .expect("test: k entry must exist; existing higher-ts entry retained")
                .value,
            b"existing"
        );
    }

    #[test]
    fn conflict_highest_clock_incoming_dominates() {
        let mut proto = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::HighestClock);
        proto.local_put("k".to_string(), b"old".to_vec(), 1);

        // Build an incoming clock that dominates local clock.
        let mut big_vc = proto.state.local_clock.clone();
        big_vc.increment("peer");
        big_vc.increment("peer");
        let incoming = SyncEntry {
            key: "k".to_string(),
            value: b"newer".to_vec(),
            clock: big_vc,
            node_id: "peer".to_string(),
            timestamp: 1,
        };
        proto
            .apply_remote_op("peer", SyncOperation::Put { entry: incoming }, 2)
            .expect("test: apply remote put with dominating clock should succeed");
        assert_eq!(
            proto
                .get("k")
                .expect("test: k entry must exist after HighestClock conflict")
                .value,
            b"newer"
        );
    }

    #[test]
    fn conflict_merge_bytes_concatenates() {
        let mut proto = PeerSyncProtocol::new(
            "n".to_string(),
            ConflictPolicy::MergeBytes { separator: b'|' },
        );
        proto.local_put("k".to_string(), b"left".to_vec(), 1);

        let mut peer_vc = VectorClock::new();
        peer_vc.increment("peer");
        let incoming = SyncEntry {
            key: "k".to_string(),
            value: b"right".to_vec(),
            clock: peer_vc,
            node_id: "peer".to_string(),
            timestamp: 1,
        };
        proto
            .apply_remote_op("peer", SyncOperation::Put { entry: incoming }, 2)
            .expect("test: apply remote put for MergeBytes conflict should succeed");
        let merged = &proto
            .get("k")
            .expect("test: k entry must exist after MergeBytes conflict")
            .value;
        assert!(merged.contains(&b'|'));
        assert!(merged.windows(4).any(|w| w == b"left"));
        assert!(merged.windows(5).any(|w| w == b"right"));
    }

    #[test]
    fn conflict_reject_returns_error() {
        let mut proto = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::RejectConflict);
        proto.local_put("k".to_string(), b"existing".to_vec(), 1);

        let mut peer_vc = VectorClock::new();
        peer_vc.increment("peer");
        let incoming = SyncEntry {
            key: "k".to_string(),
            value: b"conflict".to_vec(),
            clock: peer_vc,
            node_id: "peer".to_string(),
            timestamp: 2,
        };
        let result = proto.apply_remote_op("peer", SyncOperation::Put { entry: incoming }, 3);
        assert!(matches!(result, Err(SyncError::ConflictRejected { .. })));
        // Existing value is preserved.
        assert_eq!(
            proto
                .get("k")
                .expect("test: k entry must exist; RejectConflict keeps existing")
                .value,
            b"existing"
        );
    }

    // ── Delta / full sync ────────────────────────────────────────────────

    #[test]
    fn generate_delta_returns_missing_entries() {
        let mut node_a = PeerSyncProtocol::new("a".to_string(), ConflictPolicy::LastWriteWins);
        node_a.local_put("k1".to_string(), b"v1".to_vec(), 1);
        node_a.local_put("k2".to_string(), b"v2".to_vec(), 2);

        // Empty peer clock — peer has seen nothing.
        let peer_clock = VectorClock::new();
        let delta = node_a.generate_delta(&peer_clock);
        assert_eq!(delta.len(), 1);
        if let SyncOperation::Merge { entries } = &delta[0] {
            assert_eq!(entries.len(), 2);
        } else {
            panic!("expected Merge");
        }
    }

    #[test]
    fn generate_delta_empty_when_peer_up_to_date() {
        let mut node_a = PeerSyncProtocol::new("a".to_string(), ConflictPolicy::LastWriteWins);
        node_a.local_put("k".to_string(), b"v".to_vec(), 1);

        // Peer clock that dominates all entries.
        let current_clock = node_a.state.local_clock.clone();
        // A clock that is strictly greater than the entry's clock.
        let mut ahead_clock = current_clock.clone();
        ahead_clock.increment("a");

        let delta = node_a.generate_delta(&ahead_clock);
        assert!(delta.is_empty());
    }

    #[test]
    fn full_sync_returns_all_live_entries() {
        let mut node = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        node.local_put("a".to_string(), b"1".to_vec(), 1);
        node.local_put("b".to_string(), b"2".to_vec(), 2);
        node.local_delete("b".to_string(), 3);

        if let SyncOperation::Merge { entries } = node.full_sync() {
            // Only "a" is live; "b" was tombstoned.
            assert_eq!(entries.len(), 1);
            assert_eq!(entries[0].key, "a");
        } else {
            panic!("expected Merge");
        }
    }

    // ── Stats ────────────────────────────────────────────────────────────

    #[test]
    fn stats_initial_state() {
        let proto = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        let s = proto.stats();
        assert_eq!(
            s,
            PspSyncStats {
                total_entries: 0,
                tombstone_count: 0,
                pending_ops: 0,
                sync_log_size: 0,
                local_clock_max: 0,
            }
        );
    }

    #[test]
    fn stats_after_operations() {
        let mut proto = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        proto.local_put("k1".to_string(), b"v1".to_vec(), 1);
        proto.local_put("k2".to_string(), b"v2".to_vec(), 2);
        proto.local_delete("k1".to_string(), 3);
        let s = proto.stats();
        assert_eq!(s.total_entries, 1);
        assert_eq!(s.tombstone_count, 1);
        assert_eq!(s.pending_ops, 3); // put, put, delete
        assert_eq!(s.local_clock_max, 3);
    }

    #[test]
    fn stats_sync_log_grows_on_remote_ops() {
        let mut node_a = PeerSyncProtocol::new("a".to_string(), ConflictPolicy::LastWriteWins);
        let entry = node_a.local_put("k".to_string(), b"v".to_vec(), 1);

        let mut node_b = PeerSyncProtocol::new("b".to_string(), ConflictPolicy::LastWriteWins);
        node_b
            .apply_remote_op("a", SyncOperation::Put { entry }, 2)
            .expect("test: apply remote put for sync log growth test should succeed");
        assert_eq!(node_b.stats().sync_log_size, 1);
    }

    // ── Convergence / CRDT invariants ────────────────────────────────────

    #[test]
    fn convergence_two_nodes_put_same_key() {
        // Both nodes write concurrently; after exchanging ops they should converge.
        let mut a = PeerSyncProtocol::new("a".to_string(), ConflictPolicy::LastWriteWins);
        let mut b = PeerSyncProtocol::new("b".to_string(), ConflictPolicy::LastWriteWins);

        let ea = a.local_put("k".to_string(), b"from-a".to_vec(), 10);
        let eb = b.local_put("k".to_string(), b"from-b".to_vec(), 20);

        // Exchange
        let _ = a.apply_remote_op("b", SyncOperation::Put { entry: eb }, 21);
        let _ = b.apply_remote_op("a", SyncOperation::Put { entry: ea }, 21);

        // Both should agree on the same value (higher timestamp wins).
        assert_eq!(
            a.get("k")
                .expect("test: node a must have k after convergence exchange")
                .value,
            b.get("k")
                .expect("test: node b must have k after convergence exchange")
                .value
        );
    }

    #[test]
    fn idempotent_apply_put_same_entry_twice() {
        let mut a = PeerSyncProtocol::new("a".to_string(), ConflictPolicy::LastWriteWins);
        let entry = a.local_put("k".to_string(), b"v".to_vec(), 1);

        let mut b = PeerSyncProtocol::new("b".to_string(), ConflictPolicy::LastWriteWins);
        b.apply_remote_op(
            "a",
            SyncOperation::Put {
                entry: entry.clone(),
            },
            2,
        )
        .expect("test: first idempotent apply of entry should succeed");
        b.apply_remote_op("a", SyncOperation::Put { entry }, 3)
            .expect("test: second idempotent apply of same entry should succeed");
        // Entry count must be 1, not 2.
        assert_eq!(b.entry_count(), 1);
    }

    #[test]
    fn tombstone_blocks_late_arriving_put_in_merge() {
        let mut node = PeerSyncProtocol::new("n".to_string(), ConflictPolicy::LastWriteWins);
        node.local_delete("k".to_string(), 1);

        // A merge containing a put for the tombstoned key should be silently skipped.
        let mut vc = VectorClock::new();
        vc.increment("peer");
        let late = SyncEntry {
            key: "k".to_string(),
            value: b"late".to_vec(),
            clock: vc,
            node_id: "peer".to_string(),
            timestamp: 5,
        };
        // Merge silently skips tombstoned entries (does not return error for batch).
        let result = node.apply_remote_op(
            "peer",
            SyncOperation::Merge {
                entries: vec![late],
            },
            6,
        );
        assert!(result.is_ok());
        assert!(!node.contains("k"));
        assert!(node.is_tombstoned("k"));
    }
}