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
|
//! The store's properties over generated histories (`docs/federation-tests.md`, Properties):
//! writers that see some of what the others wrote build on it with the test fold's events,
//! and what a store holds is checked against the whole history, read from the root with
//! nothing forgotten. A failing history is cut down, events taken out with what builds on
//! them for as long as it still fails, and printed.
use std::collections::{BTreeMap, BTreeSet, HashMap};
use super::memory::{Refusal, Store};
use super::testfold::{self, State, TestFold};
use super::{Fold, Id};
use crate::testrand::{Rng, for_seeds};
/// The most events a history has.
const MOST: usize = 20;
/// Histories this small are offered in every order that puts each event after what it
/// builds on; larger ones in some orders, seeded.
const EVERY_ORDER: usize = 6;
const KEYS: [&str; 3] = ["a", "b", "c"];
/// A root and events, in the order written: each after what it builds on.
#[derive(Clone)]
struct History {
root: Vec<u8>,
events: Vec<Vec<u8>>,
ids: Vec<Id>,
deps: HashMap<Id, Vec<Id>>,
}
impl History {
fn new(root: Vec<u8>) -> Self {
Self {
root,
events: Vec::new(),
ids: Vec::new(),
deps: HashMap::new(),
}
}
fn push(&mut self, event: Vec<u8>) {
let id = Id::of(&event);
self.deps
.insert(id, TestFold::dependencies(&event).unwrap_or_default());
self.ids.push(id);
self.events.push(event);
}
fn root_id(&self) -> Id {
Id::of(&self.root)
}
/// The events of `ids` and all they build on, the root aside.
fn history(&self, ids: &[Id]) -> BTreeSet<Id> {
let mut history = BTreeSet::new();
let mut todo = ids.to_vec();
while let Some(id) = todo.pop() {
if let Some(deps) = self.deps.get(&id)
&& history.insert(id)
{
todo.extend(deps);
}
}
history
}
/// What the events of `ids` and all they build on read as, from the root, with
/// nothing forgotten.
fn read(&self, ids: &[Id]) -> State {
let history = self.history(ids);
let mut state = TestFold::start(&self.root, &[]).expect("a root reads");
for (id, event) in self.ids.iter().zip(&self.events) {
if history.contains(id) {
TestFold::add(&mut state, id, event);
}
}
state
}
/// A store offered the events in `order`, by their places in the history.
fn store(&self, order: &[usize]) -> Store<TestFold> {
let mut store = Store::new(self.root.clone(), Vec::new()).expect("a root starts a store");
for &i in order {
store.offer(self.events[i].clone());
}
store
}
/// The order written.
fn written(&self) -> Vec<usize> {
(0..self.events.len()).collect()
}
/// Orders to offer the events in: every one that puts each after what it builds on,
/// where there are few; else the order written and some shuffled, seeded. Backwards
/// too, where every event waits until the first comes.
fn orders(&self) -> Vec<Vec<usize>> {
let n = self.events.len();
let mut orders = if n <= EVERY_ORDER {
let mut all = Vec::new();
self.after_dependencies(&mut Vec::new(), &mut all);
all
} else {
let mut rng = Rng::new(n as u64);
let mut orders = vec![self.written()];
for _ in 0..4 {
let mut order = self.written();
rng.shuffle(&mut order);
orders.push(order);
}
orders
};
orders.push(self.written().into_iter().rev().collect());
orders
}
/// Every order beginning with `order` that puts each event after what it builds on.
fn after_dependencies(&self, order: &mut Vec<usize>, all: &mut Vec<Vec<usize>>) {
if order.len() == self.events.len() {
all.push(order.clone());
return;
}
let placed: BTreeSet<Id> = order.iter().map(|&i| self.ids[i]).collect();
for (i, id) in self.ids.iter().enumerate() {
let ready = self.deps[id]
.iter()
.all(|dep| placed.contains(dep) || !self.deps.contains_key(dep));
if !placed.contains(id) && ready {
order.push(i);
self.after_dependencies(order, all);
order.pop();
}
}
}
/// The history without the event at `i`, and what builds on it.
fn without(&self, i: usize) -> Self {
let mut gone = BTreeSet::from([self.ids[i]]);
let mut history = Self::new(self.root.clone());
for (id, event) in self.ids.iter().zip(&self.events) {
if gone.contains(id) || self.deps[id].iter().any(|dep| gone.contains(dep)) {
gone.insert(*id);
} else {
history.push(event.clone());
}
}
history
}
/// The root's ID, then each event's with its bytes, in the order written.
fn show(&self) -> String {
let events = self
.ids
.iter()
.zip(&self.events)
.map(|(id, event)| format!("{id} {}", String::from_utf8_lossy(event)));
std::iter::once(format!("root {}", self.root_id()))
.chain(events)
.collect::<Vec<_>>()
.join("\n")
}
}
/// One of `ids`, of the latest five more often than not.
fn pick(rng: &mut Rng, ids: &[Id]) -> Id {
if rng.chance(0.6) {
*rng.pick(&ids[ids.len().saturating_sub(5)..])
} else {
*rng.pick(ids)
}
}
/// A history of up to `MOST` events by two to four writers. Each builds on one event it
/// knows or two: the root, what it wrote, and what was written when it last caught up with
/// the others. Eras and cuts are numbered by their history; unless `conflicts`, no two
/// have one number.
fn generate(rng: &mut Rng, conflicts: bool) -> History {
let mut history = History::new(testfold::root("properties"));
let writers = rng.between(2, 4);
let mut known = vec![vec![history.root_id()]; writers];
let mut eras = BTreeSet::new();
let mut cuts = BTreeSet::new();
for i in 0..rng.between(1, MOST) {
let writer = rng.below(writers);
if rng.chance(0.2) {
known[writer] = std::iter::once(history.root_id())
.chain(history.ids.iter().copied())
.collect();
}
let mut deps = vec![pick(rng, &known[writer])];
let other = pick(rng, &known[writer]);
if rng.chance(0.3) && !deps.contains(&other) {
deps.push(other);
}
let at = history.read(&deps);
let key = *rng.pick(&KEYS);
let value = i.to_string();
let put = testfold::put(&deps, key, &value);
let mut event = match rng.below(20) {
0 => testfold::lock(&deps, key),
1..=3 => {
let n = at.era().0 + 1;
if eras.insert(n) || conflicts {
testfold::era(&deps, n, &value)
} else {
put.clone()
}
}
4 | 5 => {
let n = at.cut() + 1;
// What it builds on, or one event of its history.
let past: Vec<Id> = history.history(&deps).into_iter().collect();
let heads = if past.is_empty() || rng.chance(0.7) {
deps.clone()
} else {
vec![*rng.pick(&past)]
};
if cuts.insert(n) || conflicts {
testfold::cut(&deps, n, &heads)
} else {
put.clone()
}
}
6 => testfold::junk(&deps),
_ => put.clone(),
};
if history.deps.contains_key(&Id::of(&event)) {
event = put;
}
known[writer].push(Id::of(&event));
history.push(event);
}
history
}
/// Checks `property` on histories from `cases` seeds, with eras and cuts of one number or
/// without; a failing history is cut down and printed.
fn check(cases: u64, conflicts: bool, property: impl Fn(&History) -> Result<(), String>) {
for_seeds(cases, |rng| {
let history = generate(rng, conflicts);
if let Err(why) = property(&history) {
let (history, why) = cut_down(history, why, &property);
panic!("{why}, in the history\n{}", history.show());
}
});
}
/// The history with every event taken out, with what builds on it, that it fails without.
fn cut_down(
mut history: History,
mut why: String,
property: &impl Fn(&History) -> Result<(), String>,
) -> (History, String) {
'shorter: loop {
for i in (0..history.events.len()).rev() {
let shorter = history.without(i);
if let Err(still) = property(&shorter) {
(history, why) = (shorter, still);
continue 'shorter;
}
}
return (history, why);
}
}
/// What a store holds, to compare stores by: its checkpoint, what it took after it, and
/// what it reads as.
fn holding(store: &Store<TestFold>) -> (Vec<u8>, BTreeSet<Id>, State) {
(
store.checkpoint().to_vec(),
store.taken().iter().copied().collect(),
store.state().clone(),
)
}
/// What a state says, to compare a store's with its whole history's: what counts, what is
/// locked, the era and its tag, and the cut.
type Says = (
BTreeMap<String, String>,
BTreeSet<String>,
u64,
Option<String>,
u64,
);
fn says(state: &State) -> Says {
let (era, tag) = state.era();
(
state.counts(),
state.locks().clone(),
era,
tag.cloned(),
state.cut(),
)
}
#[test]
fn a_store_holds_the_same_whatever_order_events_come_in() {
check(100, false, |history| {
let held = holding(&history.store(&history.written()));
for order in history.orders() {
if holding(&history.store(&order)) != held {
return Err(format!(
"offered in the order {order:?}, it holds otherwise"
));
}
}
Ok(())
});
}
#[test]
fn a_store_judges_every_event_as_its_whole_history_does() {
for conflicts in [false, true] {
check(200, conflicts, |history| {
let store = history.store(&history.written());
for (id, event) in history.ids.iter().zip(&history.events) {
let judged = TestFold::judge(&history.read(&history.deps[id]), id, event);
let refusal = store.refusal(id);
if refusal.is_none() && judged.is_err() {
return Err(format!("{id} is taken, its whole history refuses it"));
}
// A cut numbered as one taken, at a history without that one, a store judges
// by the checkpoint it moved to, after the history's.
if !conflicts && matches!(refusal, Some(Refusal::Judged(_))) && judged.is_ok() {
return Err(format!("{id} is refused, its whole history takes it"));
}
}
Ok(())
});
}
}
#[test]
fn what_builds_on_a_refused_event_is_refused() {
for conflicts in [false, true] {
check(200, conflicts, |history| {
let backwards: Vec<usize> = history.written().into_iter().rev().collect();
for order in [history.written(), backwards] {
let store = history.store(&order);
for id in &history.ids {
let refused = history.deps[id]
.iter()
.find(|dep| store.refusal(dep).is_some());
if let Some(dep) = refused
&& store.refusal(id).is_none()
{
return Err(format!("{id} is taken, built on {dep}, which is refused"));
}
}
}
Ok(())
});
}
}
#[test]
fn a_store_reads_as_its_whole_history_does() {
for conflicts in [false, true] {
check(200, conflicts, |history| {
let store = history.store(&history.written());
let (at, _) = TestFold::parts(store.checkpoint()).expect("a checkpoint");
let heads: BTreeSet<Id> = if at.is_empty() {
BTreeSet::from([store.id()])
} else {
at.iter().copied().collect()
};
// What it took after its checkpoint is above its heads.
let taken: BTreeSet<Id> = store.taken().iter().copied().collect();
for id in store.taken() {
let deps = &history.deps[id];
let on = deps
.iter()
.all(|dep| heads.contains(dep) || taken.contains(dep));
let above = deps.iter().any(|dep| taken.contains(dep))
|| heads.iter().all(|head| deps.contains(head));
if !on || !above {
return Err(format!("{id} is taken, not above the checkpoint's heads"));
}
}
// Moving to its checkpoint refused nothing that named it.
if !at.is_empty() && store.state().named() != Some(&at[..]) {
return Err("the cut naming its checkpoint isn't taken".to_owned());
}
// It reads as its checkpoint and what it took after it.
let kept: Vec<(Id, Vec<u8>)> = store
.kept()
.into_iter()
.map(|bytes| (Id::of(&bytes), bytes))
.collect();
let mut state = TestFold::start(store.checkpoint(), &kept).expect("it reads");
for id in store.taken() {
TestFold::add(&mut state, id, store.get(id).expect("taken"));
}
if state != *store.state() {
return Err("it reads otherwise than its checkpoint and what it took".to_owned());
}
// And as its whole history.
let whole = history.read(&store.heads());
if says(&whole) != says(&state) {
return Err(format!(
"it reads {:?}, its whole history {:?}",
says(&state),
says(&whole)
));
}
Ok(())
});
}
}
#[test]
fn a_node_starting_from_the_checkpoint_holds_what_the_others_do() {
check(200, false, |history| {
let store = history.store(&history.written());
if TestFold::parts(store.checkpoint()).is_some_and(|(at, _)| at.is_empty()) {
return Ok(());
}
let backwards: Vec<usize> = history.written().into_iter().rev().collect();
for order in [history.written(), backwards] {
let mut pinned: Store<TestFold> =
Store::pinned(store.id(), store.checkpoint().to_vec(), store.kept())
.map_err(|why| format!("its checkpoint isn't one to start from: {why:?}"))?;
for &i in &order {
pinned.offer(history.events[i].clone());
}
if holding(&pinned) != holding(&store) {
return Err(format!(
"offered in the order {order:?}, one starting from the checkpoint holds \
otherwise"
));
}
}
Ok(())
});
}
|