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
use num_bigint::BigInt;
use serde::{Deserialize, Serialize};
use crate::traits::{Causal, CmRDT, CvRDT};
use crate::{Actor, Dot, GCounter, VClock};
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
pub struct PNCounter<A: Actor> {
p: GCounter<A>,
n: GCounter<A>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Dir {
Pos,
Neg,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Op<A: Actor> {
pub dot: Dot<A>,
pub dir: Dir,
}
impl<A: Actor> Default for PNCounter<A> {
fn default() -> Self {
Self::new()
}
}
impl<A: Actor> CmRDT for PNCounter<A> {
type Op = Op<A>;
fn apply(&mut self, op: Self::Op) {
match op {
Op { dot, dir: Dir::Pos } => self.p.apply(dot),
Op { dot, dir: Dir::Neg } => self.n.apply(dot),
}
}
}
impl<A: Actor> CvRDT for PNCounter<A> {
fn merge(&mut self, other: Self) {
self.p.merge(other.p);
self.n.merge(other.n);
}
}
impl<A: Actor> Causal<A> for PNCounter<A> {
fn forget(&mut self, clock: &VClock<A>) {
self.p.forget(&clock);
self.n.forget(&clock);
}
}
impl<A: Actor> PNCounter<A> {
pub fn new() -> Self {
Self {
p: GCounter::new(),
n: GCounter::new(),
}
}
pub fn inc(&self, actor: A) -> Op<A> {
Op {
dot: self.p.inc(actor),
dir: Dir::Pos,
}
}
pub fn dec(&self, actor: A) -> Op<A> {
Op {
dot: self.n.inc(actor),
dir: Dir::Neg,
}
}
pub fn read(&self) -> BigInt {
let p: BigInt = self.p.read().into();
let n: BigInt = self.n.read().into();
p - n
}
}
#[cfg(test)]
mod test {
use super::*;
use std::collections::BTreeSet;
use quickcheck::quickcheck;
const ACTOR_MAX: u8 = 11;
fn build_op(prims: (u8, u64, bool)) -> Op<u8> {
let (actor, counter, dir_choice) = prims;
Op {
dot: Dot { actor, counter },
dir: if dir_choice { Dir::Pos } else { Dir::Neg },
}
}
quickcheck! {
fn prop_merge_converges(op_prims: Vec<(u8, u64, bool)>) -> bool {
let ops: Vec<Op<u8>> = op_prims.into_iter().map(build_op).collect();
let mut results = BTreeSet::new();
for i in 2..ACTOR_MAX {
let mut witnesses: Vec<PNCounter<u8>> =
(0..i).map(|_| PNCounter::new()).collect();
for op in ops.iter() {
let index = op.dot.actor as usize % i as usize;
let witness = &mut witnesses[index];
witness.apply(op.clone());
}
let mut merged = PNCounter::new();
for witness in witnesses.iter() {
merged.merge(witness.clone());
}
results.insert(merged.read());
if results.len() > 1 {
println!("opvec: {:?}", ops);
println!("results: {:?}", results);
println!("witnesses: {:?}", &witnesses);
println!("merged: {:?}", merged);
}
}
results.len() == 1
}
}
#[test]
fn test_basic() {
let mut a = PNCounter::new();
assert_eq!(a.read(), 0.into());
a.apply(a.inc("A"));
assert_eq!(a.read(), 1.into());
a.apply(a.inc("A"));
assert_eq!(a.read(), 2.into());
a.apply(a.dec("A"));
assert_eq!(a.read(), 1.into());
a.apply(a.inc("A"));
assert_eq!(a.read(), 2.into());
}
}