ternary-distributed 0.1.0

Distributed systems primitives for ternary protocols
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
#![forbid(unsafe_code)]

//! Distributed systems primitives for ternary protocols.
//!
//! Provides node management, gossip propagation, vector clocks, partition detection,
//! consensus, and anti-entropy synchronization — all built around the ternary value
//! space {-1, 0, +1}.

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

/// A ternary value: Negative (-1), Zero (0), or Positive (+1).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Trit {
    Neg = -1,
    Zero = 0,
    Pos = 1,
}

impl Trit {
    pub fn from_i8(v: i8) -> Option<Self> {
        match v {
            -1 => Some(Trit::Neg),
            0 => Some(Trit::Zero),
            1 => Some(Trit::Pos),
            _ => None,
        }
    }

    pub fn to_i8(self) -> i8 {
        self as i8
    }
}

/// Unique identifier for a node in the distributed system.
pub type NodeId = u64;

/// A node in a ternary distributed system.
///
/// Each node holds a ternary state and participates in gossip, consensus,
/// and anti-entropy protocols.
#[derive(Debug, Clone)]
pub struct TernaryNode {
    pub id: NodeId,
    pub state: Trit,
    pub peers: HashSet<NodeId>,
    pub vector_clock: VectorClock,
    pub is_alive: bool,
}

impl TernaryNode {
    pub fn new(id: NodeId) -> Self {
        Self {
            id,
            state: Trit::Zero,
            peers: HashSet::new(),
            vector_clock: VectorClock::new(),
            is_alive: true,
        }
    }

    pub fn with_state(id: NodeId, state: Trit) -> Self {
        Self {
            id,
            state,
            peers: HashSet::new(),
            vector_clock: VectorClock::new(),
            is_alive: true,
        }
    }

    pub fn add_peer(&mut self, peer_id: NodeId) {
        if peer_id != self.id {
            self.peers.insert(peer_id);
        }
    }

    pub fn remove_peer(&mut self, peer_id: NodeId) {
        self.peers.remove(&peer_id);
    }

    pub fn set_state(&mut self, state: Trit) {
        self.state = state;
        self.vector_clock.increment(self.id);
    }
}

/// Gossip protocol for propagating ternary state across a cluster.
///
/// Each round, nodes share their state with random peers. State converges
/// using a "dominant trit" rule: if any peer has a non-zero state, the
/// receiving node adopts the most common non-zero value (with ties broken
/// toward Pos).
#[derive(Debug, Clone)]
pub struct GossipProtocol {
    pub nodes: HashMap<NodeId, TernaryNode>,
    pub round: u64,
}

impl GossipProtocol {
    pub fn new() -> Self {
        Self {
            nodes: HashMap::new(),
            round: 0,
        }
    }

    pub fn add_node(&mut self, node: TernaryNode) {
        self.nodes.insert(node.id, node);
    }

    pub fn run_round(&mut self) -> u32 {
        let states: HashMap<NodeId, (Trit, VectorClock)> = self
            .nodes
            .iter()
            .map(|(id, n)| (*id, (n.state, n.vector_clock.clone())))
            .collect();

        let mut updates = 0u32;
        let node_ids: Vec<NodeId> = self.nodes.keys().copied().collect();

        for node_id in &node_ids {
            let node = self.nodes.get(node_id).unwrap();
            let mut peer_states: Vec<Trit> = Vec::new();

            for peer_id in &node.peers {
                if let Some((state, vc)) = states.get(peer_id) {
                    // Only adopt from peers with equal or higher vector clock
                    if vc >= &node.vector_clock {
                        peer_states.push(*state);
                    }
                }
            }

            if let Some(new_state) = dominant_trit(&peer_states) {
                if new_state != node.state {
                    if let Some(n) = self.nodes.get_mut(node_id) {
                        n.state = new_state;
                        n.vector_clock.increment(*node_id);
                        updates += 1;
                    }
                }
            }
        }

        self.round += 1;
        updates
    }

    pub fn run_until_converged(&mut self, max_rounds: u64) -> u64 {
        for i in 0..max_rounds {
            if self.run_round() == 0 {
                return i + 1;
            }
        }
        max_rounds
    }

    pub fn is_converged(&self) -> bool {
        let states: HashSet<Trit> = self.nodes.values().map(|n| n.state).collect();
        states.len() <= 1
    }
}

/// Returns the dominant non-zero trit from a list. Ties broken toward Pos.
fn dominant_trit(trits: &[Trit]) -> Option<Trit> {
    let mut neg = 0u32;
    let mut pos = 0u32;
    for t in trits {
        match t {
            Trit::Neg => neg += 1,
            Trit::Pos => pos += 1,
            Trit::Zero => {}
        }
    }
    if pos == 0 && neg == 0 {
        return None;
    }
    if pos >= neg {
        Some(Trit::Pos)
    } else {
        Some(Trit::Neg)
    }
}

/// Vector clock for tracking causal ordering in ternary distributed systems.
///
/// Maps node IDs to monotonically increasing counters. Two vector clocks can
/// be compared to determine happened-before, concurrent, or equal relationships.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VectorClock {
    pub counters: HashMap<NodeId, u64>,
}

impl VectorClock {
    pub fn new() -> Self {
        Self {
            counters: HashMap::new(),
        }
    }

    pub fn increment(&mut self, node_id: NodeId) -> u64 {
        let counter = self.counters.entry(node_id).or_insert(0);
        *counter += 1;
        *counter
    }

    pub fn get(&self, node_id: NodeId) -> u64 {
        *self.counters.get(&node_id).unwrap_or(&0)
    }

    pub fn merge(&self, other: &VectorClock) -> VectorClock {
        let mut merged = self.counters.clone();
        for (node_id, counter) in &other.counters {
            let entry = merged.entry(*node_id).or_insert(0);
            *entry = (*entry).max(*counter);
        }
        VectorClock { counters: merged }
    }

    /// Returns true if self happened-before other.
    pub fn happened_before(&self, other: &VectorClock) -> bool {
        let all_keys: HashSet<NodeId> = self
            .counters
            .keys()
            .chain(other.counters.keys())
            .copied()
            .collect();

        let mut at_least_one_less = false;
        for key in &all_keys {
            let s = self.get(*key);
            let o = other.get(*key);
            if s > o {
                return false;
            }
            if s < o {
                at_least_one_less = true;
            }
        }
        at_least_one_less
    }

    /// Returns true if self and other are concurrent (neither happened-before the other).
    pub fn is_concurrent(&self, other: &VectorClock) -> bool {
        !self.happened_before(other) && !other.happened_before(self) && self != other
    }
}

impl PartialOrd for VectorClock {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        if self == other {
            Some(std::cmp::Ordering::Equal)
        } else if self.happened_before(other) {
            Some(std::cmp::Ordering::Less)
        } else if other.happened_before(self) {
            Some(std::cmp::Ordering::Greater)
        } else {
            None // concurrent
        }
    }
}

/// Partition detector identifies network splits by tracking node reachability.
///
/// Maintains a heartbeat map. Nodes that haven't been heard from within
/// `timeout_rounds` are considered partitioned. The detector then checks
/// if the remaining reachable nodes form a quorum.
#[derive(Debug, Clone)]
pub struct PartitionDetector {
    pub last_seen: HashMap<NodeId, u64>,
    pub timeout_rounds: u64,
    pub current_round: u64,
    pub total_nodes: usize,
}

impl PartitionDetector {
    pub fn new(total_nodes: usize, timeout_rounds: u64) -> Self {
        Self {
            last_seen: HashMap::new(),
            timeout_rounds,
            current_round: 0,
            total_nodes,
        }
    }

    pub fn heartbeat(&mut self, node_id: NodeId) {
        self.last_seen.insert(node_id, self.current_round);
    }

    pub fn advance_round(&mut self) {
        self.current_round += 1;
    }

    pub fn is_alive(&self, node_id: NodeId) -> bool {
        self.last_seen
            .get(&node_id)
            .map(|&r| self.current_round.saturating_sub(r) <= self.timeout_rounds)
            .unwrap_or(false)
    }

    pub fn alive_nodes(&self) -> Vec<NodeId> {
        self.last_seen
            .keys()
            .filter(|&&id| self.is_alive(id))
            .copied()
            .collect()
    }

    pub fn partitioned_nodes(&self) -> Vec<NodeId> {
        self.last_seen
            .keys()
            .filter(|&&id| !self.is_alive(id))
            .copied()
            .collect()
    }

    pub fn has_quorum(&self) -> bool {
        let alive = self.alive_nodes().len();
        alive * 2 > self.total_nodes
    }

    pub fn is_partitioned(&self) -> bool {
        !self.has_quorum()
    }
}

/// A ternary vote: Negative (-1), Abstain (0), or Positive (+1).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Vote {
    Negative,
    Abstain,
    Positive,
}

impl Vote {
    pub fn to_trit(self) -> Trit {
        match self {
            Vote::Negative => Trit::Neg,
            Vote::Abstain => Trit::Zero,
            Vote::Positive => Trit::Pos,
        }
    }

    pub fn from_trit(t: Trit) -> Self {
        match t {
            Trit::Neg => Vote::Negative,
            Trit::Zero => Vote::Abstain,
            Trit::Pos => Vote::Positive,
        }
    }
}

/// A Paxos-like consensus protocol for ternary votes.
///
/// Supports prepare/promise and accept/ack phases. A proposal is accepted
/// when a quorum of acceptors responds. The final value is determined by
/// summing the ternary votes: negative sum → Neg, positive sum → Pos, zero → Zero.
#[derive(Debug, Clone)]
pub struct ConsensusProtocol {
    pub proposers: HashSet<NodeId>,
    pub acceptors: HashSet<NodeId>,
    pub learners: HashSet<NodeId>,
    pub promised_proposal: HashMap<NodeId, u64>,
    pub accepted_value: HashMap<NodeId, (u64, Vote)>,
    pub proposal_counter: u64,
    pub quorum_size: usize,
}

impl ConsensusProtocol {
    pub fn new(nodes: &[NodeId]) -> Self {
        let node_set: HashSet<NodeId> = nodes.iter().copied().collect();
        let quorum_size = nodes.len() / 2 + 1;
        Self {
            proposers: node_set.clone(),
            acceptors: node_set.clone(),
            learners: node_set,
            promised_proposal: HashMap::new(),
            accepted_value: HashMap::new(),
            proposal_counter: 0,
            quorum_size,
        }
    }

    pub fn prepare(&mut self, proposer: NodeId) -> u64 {
        self.proposal_counter += 1;
        let proposal_num = self.proposal_counter;
        // Check if proposer can promise
        if let Some(&promised) = self.promised_proposal.get(&proposer) {
            if promised >= proposal_num {
                return 0; // rejected
            }
        }
        proposal_num
    }

    pub fn promise(&mut self, acceptor: NodeId, proposal_num: u64) -> bool {
        if let Some(&promised) = self.promised_proposal.get(&acceptor) {
            if promised > proposal_num {
                return false;
            }
        }
        self.promised_proposal.insert(acceptor, proposal_num);
        true
    }

    pub fn accept(&mut self, acceptor: NodeId, proposal_num: u64, value: Vote) -> bool {
        if let Some(&promised) = self.promised_proposal.get(&acceptor) {
            if promised > proposal_num {
                return false;
            }
        }
        self.accepted_value
            .insert(acceptor, (proposal_num, value));
        true
    }

    pub fn decide(&self) -> Option<Vote> {
        let values: Vec<&(u64, Vote)> = self.accepted_value.values().collect();
        if values.len() < self.quorum_size {
            return None;
        }

        // Find the highest proposal number with quorum
        let mut proposal_counts: HashMap<u64, Vec<Vote>> = HashMap::new();
        for (num, vote) in &values {
            proposal_counts.entry(*num).or_default().push(*vote);
        }

        let max_proposal = proposal_counts.keys().max()?;
        let votes = proposal_counts.get(max_proposal)?;
        if votes.len() < self.quorum_size {
            return None;
        }

        let sum: i32 = votes.iter().map(|v| v.to_trit().to_i8() as i32).sum();
        if sum < 0 {
            Some(Vote::Negative)
        } else if sum > 0 {
            Some(Vote::Positive)
        } else {
            Some(Vote::Abstain)
        }
    }
}

/// Anti-entropy synchronization for repairing state divergence between nodes.
///
/// Compares vector clocks to detect conflicts. If clocks are concurrent,
/// applies a merge strategy. Otherwise, the lagging node adopts the leader's state.
#[derive(Debug, Clone)]
pub struct AntiEntropySync {
    pub nodes: HashMap<NodeId, TernaryNode>,
}

impl AntiEntropySync {
    pub fn new() -> Self {
        Self {
            nodes: HashMap::new(),
        }
    }

    pub fn add_node(&mut self, node: TernaryNode) {
        self.nodes.insert(node.id, node);
    }

    /// Synchronize two nodes. Returns true if any state was changed.
    pub fn sync_pair(&mut self, node_a: NodeId, node_b: NodeId) -> bool {
        let (state_a, vc_a, peers_a) = {
            let a = self.nodes.get(&node_a).unwrap();
            (a.state, a.vector_clock.clone(), a.peers.clone())
        };
        let (state_b, vc_b, peers_b) = {
            let b = self.nodes.get(&node_b).unwrap();
            (b.state, b.vector_clock.clone(), b.peers.clone())
        };

        let mut changed = false;

        if vc_a.happened_before(&vc_b) {
            // A is behind, adopt B's state
            if let Some(a) = self.nodes.get_mut(&node_a) {
                if a.state != state_b {
                    a.state = state_b;
                    a.vector_clock = vc_a.merge(&vc_b);
                    a.vector_clock.increment(node_a);
                    changed = true;
                }
            }
        } else if vc_b.happened_before(&vc_a) {
            // B is behind, adopt A's state
            if let Some(b) = self.nodes.get_mut(&node_b) {
                if b.state != state_a {
                    b.state = state_a;
                    b.vector_clock = vc_b.merge(&vc_a);
                    b.vector_clock.increment(node_b);
                    changed = true;
                }
            }
        } else if vc_a.is_concurrent(&vc_b) {
            // Concurrent — merge by dominant trit
            let merged = dominant_trit(&[state_a, state_b]).unwrap_or(Trit::Zero);
            let merged_vc = vc_a.merge(&vc_b);
            if let Some(a) = self.nodes.get_mut(&node_a) {
                if a.state != merged {
                    a.state = merged;
                    a.vector_clock = merged_vc.clone();
                    a.vector_clock.increment(node_a);
                    changed = true;
                }
            }
            if let Some(b) = self.nodes.get_mut(&node_b) {
                if b.state != merged {
                    b.state = merged;
                    b.vector_clock = merged_vc;
                    b.vector_clock.increment(node_b);
                    changed = true;
                }
            }
        }

        // Also sync peer lists
        if let Some(a) = self.nodes.get_mut(&node_a) {
            for p in &peers_b {
                a.peers.insert(*p);
            }
        }
        if let Some(b) = self.nodes.get_mut(&node_b) {
            for p in &peers_a {
                b.peers.insert(*p);
            }
        }

        changed
    }

    /// Run full anti-entropy: every pair of connected nodes synchronizes.
    pub fn sync_all(&mut self) -> u32 {
        let node_ids: Vec<NodeId> = self.nodes.keys().copied().collect();
        let mut changes = 0u32;

        for i in 0..node_ids.len() {
            for j in (i + 1)..node_ids.len() {
                let a_id = node_ids[i];
                let b_id = node_ids[j];
                let a = self.nodes.get(&a_id).unwrap();
                let b = self.nodes.get(&b_id).unwrap();
                if a.peers.contains(&b_id) || b.peers.contains(&a_id) {
                    if self.sync_pair(a_id, b_id) {
                        changes += 1;
                    }
                }
            }
        }

        changes
    }
}

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

    #[test]
    fn test_trit_from_i8() {
        assert_eq!(Trit::from_i8(-1), Some(Trit::Neg));
        assert_eq!(Trit::from_i8(0), Some(Trit::Zero));
        assert_eq!(Trit::from_i8(1), Some(Trit::Pos));
        assert_eq!(Trit::from_i8(2), None);
    }

    #[test]
    fn test_trit_to_i8() {
        assert_eq!(Trit::Neg.to_i8(), -1);
        assert_eq!(Trit::Zero.to_i8(), 0);
        assert_eq!(Trit::Pos.to_i8(), 1);
    }

    #[test]
    fn test_ternary_node_new() {
        let node = TernaryNode::new(1);
        assert_eq!(node.id, 1);
        assert_eq!(node.state, Trit::Zero);
        assert!(node.peers.is_empty());
        assert!(node.is_alive);
    }

    #[test]
    fn test_ternary_node_add_peer() {
        let mut node = TernaryNode::new(1);
        node.add_peer(2);
        node.add_peer(3);
        assert!(node.peers.contains(&2));
        assert!(node.peers.contains(&3));
        assert_eq!(node.peers.len(), 2);
    }

    #[test]
    fn test_ternary_node_no_self_peer() {
        let mut node = TernaryNode::new(1);
        node.add_peer(1);
        assert!(node.peers.is_empty());
    }

    #[test]
    fn test_ternary_node_set_state() {
        let mut node = TernaryNode::new(1);
        node.set_state(Trit::Pos);
        assert_eq!(node.state, Trit::Pos);
        assert_eq!(node.vector_clock.get(1), 1);
        node.set_state(Trit::Neg);
        assert_eq!(node.state, Trit::Neg);
        assert_eq!(node.vector_clock.get(1), 2);
    }

    #[test]
    fn test_vector_clock_increment() {
        let mut vc = VectorClock::new();
        assert_eq!(vc.increment(1), 1);
        assert_eq!(vc.increment(1), 2);
        assert_eq!(vc.increment(2), 1);
    }

    #[test]
    fn test_vector_clock_happened_before() {
        let mut vc1 = VectorClock::new();
        vc1.increment(1);
        let mut vc2 = VectorClock::new();
        vc2.increment(1);
        vc2.increment(1);
        assert!(vc1.happened_before(&vc2));
        assert!(!vc2.happened_before(&vc1));
    }

    #[test]
    fn test_vector_clock_concurrent() {
        let mut vc1 = VectorClock::new();
        vc1.increment(1);
        let mut vc2 = VectorClock::new();
        vc2.increment(2);
        assert!(vc1.is_concurrent(&vc2));
        assert!(vc2.is_concurrent(&vc1));
    }

    #[test]
    fn test_vector_clock_merge() {
        let mut vc1 = VectorClock::new();
        vc1.increment(1);
        let mut vc2 = VectorClock::new();
        vc2.increment(2);
        let merged = vc1.merge(&vc2);
        assert_eq!(merged.get(1), 1);
        assert_eq!(merged.get(2), 1);
    }

    #[test]
    fn test_vector_clock_partial_ord() {
        let mut vc1 = VectorClock::new();
        vc1.increment(1);
        let mut vc2 = VectorClock::new();
        vc2.increment(1);
        vc2.increment(2);
        assert!(vc1 < vc2);
        assert!(vc2 > vc1);
    }

    #[test]
    fn test_gossip_single_round() {
        let mut gossip = GossipProtocol::new();
        let mut n1 = TernaryNode::with_state(1, Trit::Pos);
        n1.add_peer(2);
        let mut n2 = TernaryNode::new(2);
        n2.add_peer(1);
        gossip.add_node(n1);
        gossip.add_node(n2);
        let updates = gossip.run_round();
        assert!(updates > 0);
        assert!(gossip.is_converged());
    }

    #[test]
    fn test_gossip_converged() {
        let mut gossip = GossipProtocol::new();
        let mut n1 = TernaryNode::with_state(1, Trit::Pos);
        n1.add_peer(2);
        let mut n2 = TernaryNode::with_state(2, Trit::Pos);
        n2.add_peer(1);
        gossip.add_node(n1);
        gossip.add_node(n2);
        let rounds = gossip.run_until_converged(10);
        assert_eq!(rounds, 1);
        assert!(gossip.is_converged());
    }

    #[test]
    fn test_partition_detector_alive() {
        let mut pd = PartitionDetector::new(3, 2);
        pd.heartbeat(1);
        pd.heartbeat(2);
        pd.heartbeat(3);
        assert!(pd.is_alive(1));
        assert!(pd.is_alive(2));
        assert!(pd.is_alive(3));
    }

    #[test]
    fn test_partition_detector_timeout() {
        let mut pd = PartitionDetector::new(3, 1);
        pd.heartbeat(1);
        pd.heartbeat(2);
        pd.heartbeat(3);
        pd.advance_round();
        pd.advance_round();
        assert!(!pd.is_alive(1));
        assert_eq!(pd.partitioned_nodes().len(), 3);
    }

    #[test]
    fn test_partition_detector_quorum() {
        let mut pd = PartitionDetector::new(3, 2);
        pd.heartbeat(1);
        pd.heartbeat(2);
        assert!(pd.has_quorum());
        assert!(!pd.is_partitioned());
    }

    #[test]
    fn test_partition_detector_no_quorum() {
        let mut pd = PartitionDetector::new(5, 2);
        pd.heartbeat(1);
        pd.heartbeat(2);
        assert!(!pd.has_quorum());
        assert!(pd.is_partitioned());
    }

    #[test]
    fn test_consensus_prepare_promise() {
        let mut cp = ConsensusProtocol::new(&[1, 2, 3]);
        let proposal = cp.prepare(1);
        assert!(proposal > 0);
        assert!(cp.promise(1, proposal));
        assert!(cp.promise(2, proposal));
        assert!(cp.promise(3, proposal));
    }

    #[test]
    fn test_consensus_accept_decide() {
        let mut cp = ConsensusProtocol::new(&[1, 2, 3]);
        let proposal = cp.prepare(1);
        cp.promise(1, proposal);
        cp.promise(2, proposal);
        cp.promise(3, proposal);
        cp.accept(1, proposal, Vote::Positive);
        cp.accept(2, proposal, Vote::Positive);
        cp.accept(3, proposal, Vote::Positive);
        assert_eq!(cp.decide(), Some(Vote::Positive));
    }

    #[test]
    fn test_consensus_negative_decision() {
        let mut cp = ConsensusProtocol::new(&[1, 2, 3]);
        let proposal = cp.prepare(1);
        cp.promise(1, proposal);
        cp.promise(2, proposal);
        cp.promise(3, proposal);
        cp.accept(1, proposal, Vote::Negative);
        cp.accept(2, proposal, Vote::Negative);
        cp.accept(3, proposal, Vote::Abstain);
        assert_eq!(cp.decide(), Some(Vote::Negative));
    }

    #[test]
    fn test_consensus_no_quorum() {
        let mut cp = ConsensusProtocol::new(&[1, 2, 3]);
        assert_eq!(cp.decide(), None);
    }

    #[test]
    fn test_anti_entropy_sync_pair() {
        let mut sync = AntiEntropySync::new();
        let mut n1 = TernaryNode::with_state(1, Trit::Pos);
        n1.add_peer(2);
        let mut n2 = TernaryNode::new(2);
        n2.add_peer(1);
        n2.vector_clock.increment(2); // Give n2 a clock
        sync.add_node(n1);
        sync.add_node(n2);
        sync.sync_pair(1, 2);
        // n2 should adopt n1's state since n1 has state set via with_state
        // but n1's vc is empty while n2 has a tick
        let state2 = sync.nodes.get(&2).unwrap().state;
        assert_eq!(state2, Trit::Zero); // n2 already zero, n1 has no vc ticks
    }

    #[test]
    fn test_vote_trit_conversion() {
        assert_eq!(Vote::Negative.to_trit(), Trit::Neg);
        assert_eq!(Vote::Abstain.to_trit(), Trit::Zero);
        assert_eq!(Vote::Positive.to_trit(), Trit::Pos);
        assert_eq!(Vote::from_trit(Trit::Neg), Vote::Negative);
        assert_eq!(Vote::from_trit(Trit::Zero), Vote::Abstain);
        assert_eq!(Vote::from_trit(Trit::Pos), Vote::Positive);
    }

    #[test]
    fn test_dominant_trit() {
        assert_eq!(dominant_trit(&[Trit::Pos, Trit::Pos, Trit::Neg]), Some(Trit::Pos));
        assert_eq!(dominant_trit(&[Trit::Neg, Trit::Neg, Trit::Pos]), Some(Trit::Neg));
        assert_eq!(dominant_trit(&[Trit::Zero, Trit::Zero]), None);
        assert_eq!(dominant_trit(&[]), None);
    }
}