forked from hetu-project/chronos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
233 lines (202 loc) · 6.12 KB
/
lib.rs
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
//! Verifiable logical clock.
//!
//! This crate implements a verifiable logical clock construct. The clock
//! can be used in a peer-to-peer network to order events. Any node in the
//! network can verify the correctness of the clock. And HashMap as its core
//! data structure.
pub mod ordinary_clock;
use serde::{Deserialize, Serialize};
use std::cmp;
use std::collections::HashMap;
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct Clock {
values: HashMap<u128, u128>,
}
impl PartialOrd for Clock {
fn partial_cmp(&self, other: &Clock) -> Option<cmp::Ordering> {
let mut less = false;
let mut greater = false;
for (id, value) in &self.values {
let other_value = other.values.get(id);
if other_value.is_none() || value > other_value.unwrap() {
greater = true;
} else if value < other_value.unwrap() {
less = true;
}
}
for (id, _) in &other.values {
if self.values.get(id).is_none() {
less = true;
}
}
if less && greater {
None
} else if less {
Some(cmp::Ordering::Less)
} else if greater {
Some(cmp::Ordering::Greater)
} else {
Some(cmp::Ordering::Equal)
}
}
}
impl Clock {
/// Create a new clock.
pub fn new() -> Self {
Self {
values: HashMap::new(),
}
}
/// Increment the clock
pub fn inc(&mut self, id: u128) {
let value = self.values.entry(id).or_insert(0);
*value += 1;
}
/// Get the clock count by id
pub fn get(&mut self, id: u128) -> u128 {
let value = self.values.entry(id).or_insert(0);
*value
}
/// Reset the clock.
pub fn clear(&mut self) {
self.values.clear();
}
/// Merge the clock with other clocks.
pub fn merge(&mut self, others: &Vec<&Clock>) {
for &clock in others {
for (id, value) in &clock.values {
let v = self.values.entry(*id).or_insert(0);
*v = std::cmp::max(*v, *value);
}
}
}
/// Diff is local clock minus another clock
pub fn diff(&self, other: &Clock) -> Clock {
let mut ret = Clock::new();
for (id, v1) in &self.values {
let v2 = other.values.get(id).unwrap_or(&0);
if v1 > v2 {
ret.values.insert(*id, v1-v2);
} else {
ret.values.insert(*id, 0);
}
}
ret
}
/// return index key of clock
pub fn index_key(&self) -> String {
let mut key: String = String::new();
for (index, value) in &self.values {
key = format!("{}{}-{}-", key, index, value);
}
key
}
/// return common base clock of two clock
pub fn base_common(&self, other: &Clock) -> Clock {
let mut ret = Clock::new();
for (id, v1) in &self.values {
let v2 = other.values.get(id).unwrap_or(&0);
if v1 <= v2 {
ret.values.insert(*id, *v1);
} else {
ret.values.insert(*id, *v2);
}
}
ret
}
/// return true when all value is zero in clock dimensions
pub fn is_genesis(&self) -> bool {
let sum: u128 = self.values.values().sum();
sum == 0
}
}
#[cfg(test)]
mod tests {
use super::*;
use bincode::Options;
use sha2::Sha256;
use sha2::Digest;
#[test]
fn clock_inc() {
let mut c = Clock::new();
c.inc(0);
c.inc(0);
assert_eq!(c.values.get(&0), Some(&2));
}
#[test]
fn clock_cmp() {
let mut c1 = Clock::new();
c1.inc(0);
let c2 = c1.clone();
let mut c3 = Clock::new();
c3.inc(1);
assert_eq!(c1, c2);
assert_eq!(c1.partial_cmp(&c3), None);
assert_eq!(c2.partial_cmp(&c3), None);
c1.inc(0);
assert_eq!(c2.partial_cmp(&c1), Some(cmp::Ordering::Less));
assert_eq!(c3.partial_cmp(&c1), None);
}
#[test]
fn clock_merge() {
let mut c1 = Clock::new();
c1.inc(0);
let mut c2 = Clock::new();
c2.inc(1);
let mut c3 = Clock::new();
c3.inc(2);
assert_eq!(c1.partial_cmp(&c2), None);
assert_eq!(c1.partial_cmp(&c3), None);
assert_eq!(c2.partial_cmp(&c3), None);
c1.merge(&vec![&c2, &c3]);
assert_eq!(c2.partial_cmp(&c1), Some(cmp::Ordering::Less));
assert_eq!(c1.partial_cmp(&c2), Some(cmp::Ordering::Greater));
assert_eq!(c3.partial_cmp(&c1), Some(cmp::Ordering::Less));
assert_eq!(c1.partial_cmp(&c3), Some(cmp::Ordering::Greater));
}
#[test]
#[ignore]
fn clock_serialize() {
let mut c1 = Clock::new();
c1.inc(0);
c1.inc(1);
c1.inc(1);
c1.inc(2);
c1.inc(3);
let ser1 = bincode::options().serialize(&c1).unwrap();
let mut c2 = Clock::new();
c2.inc(0);
c2.inc(1);
c2.inc(1);
c2.inc(2);
c2.inc(3);
let ser2 = bincode::options().serialize(&c2).unwrap();
println!("{:?}, {:?}", c1, c2);
assert_eq!(c1, c2); // ignore diff order, random
// not equal, no order
assert_ne!(ser1, ser2);
}
#[test]
#[ignore]
fn clock_sha256() {
let mut c1 = Clock::new();
c1.inc(0);
c1.inc(1);
c1.inc(1);
c1.inc(2);
let ser1 = bincode::options().serialize(&c1).unwrap();
let mut f_hasher_1 = Sha256::new();
f_hasher_1.update(ser1.clone());
let hash_1 = f_hasher_1.finalize();
let unser1 = bincode::options().deserialize::<Clock>(&ser1).unwrap();
assert_eq!(c1, unser1); // ignore diff order
// not equal
let ser2 = bincode::options().serialize(&unser1).unwrap();
assert_ne!(ser1, ser2);
// not equal
let mut f_hasher_2 = Sha256::new();
f_hasher_2.update(ser2);
let hash_2 = f_hasher_2.finalize();
assert_ne!(hash_1, hash_2);
}
}