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
|
//! What a change of a swarm holding a store writes (`docs/store.md`, What a change writes),
//! judged by every node by the change and its history alone, in place of rules by path
//! (`rules::judge`). It writes nothing but:
//!
//! * events: at `_continuum/events/<ID>` the bytes whose ID (SHA-256) it is, no more than
//! the fold allows, at a key held nowhere at the change's history;
//! * its node's own protocol data: its entry (`_continuum/members/<node ID>`), its acks
//! (`_continuum/votes/<node ID>`), a remake's record and proposal, where there is none,
//! of a compaction it coordinates, and, as a remake's coordinator, its decision or its
//! abort, or the record of the remake that made the epoch, carried into it whole; and
//! the maps holding them, where there are none.
//!
//! An epoch's root writes what it starts with (`rules::judge_reading`). Everything else
//! is closed: whatever isn't under `_continuum`, the policy, the swarm's ID, users, bans,
//! the swarm's topology, an abort asked for by another than the coordinator. Nothing
//! deletes or
//! replaces an event. A change names its node, as signed (signatures are required), but
//! for the changes founding the swarm, which every node builds and nobody signs: the
//! genesis change, which the epoch's root is, and those of its writer putting the events
//! a fork's root keeps (`document::events`), taken only as built byte for byte. Which
//! events the store takes is the store's (`federation::held`); this judges none.
//!
//! In a swarm not holding a store, no change writes a policy naming one (`turns_store`):
//! a swarm holds a store from its genesis change, or not at all.
use sha2::{Digest, Sha256};
use super::rules::{Change, Refusal, coordinator, made_the_epoch};
use crate::document::at::At;
use crate::document::inspect::{Action, Op, Path};
use crate::document::replica::Epoch;
use crate::document::{events, meta, store};
use crate::federation::store::Id;
/// The most bytes an event of the fold `fold` may have.
fn largest(fold: &str) -> usize {
use crate::federation::store::Fold;
match fold {
crate::federation::fold::Federation::NAME => crate::federation::fold::Federation::LARGEST,
_ => 0,
}
}
/// The events the store's root `root` keeps, by the fold `fold`: those of a fork's.
fn keeps(fold: &str, root: &[u8]) -> Option<Vec<Id>> {
use crate::federation::store::Fold;
match fold {
crate::federation::fold::Federation::NAME => {
crate::federation::fold::Federation::parts(root).map(|(_, keeps)| keeps)
}
_ => None,
}
}
/// Whether `change`, naming no node, is one of those founding the swarm, which every node
/// builds from the store's root and the events it keeps (`events::is_founding`).
fn founding(at: &At<'_>, change: &Change<'_>, fold: &str) -> bool {
let Some(root) = events::swarm(at).and_then(|swarm| events::bytes(at, &swarm)) else {
return false;
};
let Some(keeps) = keeps(fold, &root) else {
return false;
};
let written: Vec<Vec<u8>> = change
.ops
.iter()
.filter_map(|op| match &op.action {
Action::PutBytes(bytes) => Some(bytes.clone()),
_ => None,
})
.collect();
events::is_founding(at, fold, (&root, &keeps), &change.hash, &written)
}
/// Judges `change`, of a swarm holding a store of the fold `fold`, at its history `at`, in
/// the document of `epoch`.
pub(crate) fn judge(
at: &At<'_>,
epoch: Epoch,
change: &Change<'_>,
fold: &str,
) -> Result<(), Refusal> {
let refuse = |why: String| Err(Refusal::Rule(why));
let Some(node) = change.committer else {
if founding(at, change, fold) {
return Ok(());
}
return refuse("it names no node".to_owned());
};
let events = meta::path(&[meta::EVENTS]);
let members = meta::path(&[meta::MEMBERS]);
let entry = meta::path(&[meta::MEMBERS, node]);
let votes = meta::path(&[meta::VOTES]);
let acks = meta::path(&[meta::VOTES, node]);
let remakes = meta::path(&[meta::REMAKES]);
for op in change.ops {
let allowed = if op.path.starts_with(&events) {
event(at, op, fold)
} else if op.path.starts_with(&entry) || op.path.starts_with(&acks) {
Ok(())
} else if op.path == members || op.path == votes || op.path == remakes {
made(at, op)
} else if op.path.starts_with(&remakes) {
remake(at, epoch, change, node, op)
} else {
Err(format!("it writes {}, closed", shown(&op.path)))
};
allowed.or_else(|why| refuse(format!("{node}: {why}")))?;
}
Ok(())
}
/// Whether `op` makes the map at its path, where there is none at `at`.
fn made(at: &At<'_>, op: &Op) -> Result<(), String> {
if op.action == Action::MakeMap && !store::held_anywhere(at, &op.path) {
Ok(())
} else {
Err(format!("it writes {}, made already", shown(&op.path)))
}
}
/// Whether `op`, under `_continuum/remakes`, is one of `node`'s, at `at`, in the document of
/// `epoch`: a record made where there is none, its proposal written where there is none,
/// of a compaction `node` coordinates, and its decision, written by its coordinator; or
/// the record of the remake that made the epoch, carried into it whole by its
/// coordinator, where there is none.
fn remake(
at: &At<'_>,
epoch: Epoch,
change: &Change<'_>,
node: &str,
op: &Op,
) -> Result<(), String> {
let [_, _, id, rest @ ..] = op.path.as_slice() else {
return Err(format!("it writes {}", shown(&op.path)));
};
let proposal = meta::path(&[meta::REMAKES, id, "proposal"]);
match rest.first().map(String::as_str) {
None => made(at, op),
Some("proposal") => {
if store::held_anywhere(at, &proposal) {
Err(format!(
"it writes the proposal of remake {id}, written already"
))
} else if !proposes(change, &proposal, node) {
Err(format!(
"it proposes remake {id}, no compaction that {node} coordinates"
))
} else {
Ok(())
}
}
Some("decision") => match coordinator(at, id) {
Some(coordinator) if coordinator == node => Ok(()),
Some(coordinator) => Err(format!(
"it decides remake {id}, which is {coordinator}'s to decide"
)),
None if change.paths.contains(&proposal)
&& !store::held_anywhere(at, &meta::path(&[meta::REMAKES, id]))
&& made_the_epoch(at, epoch, id, node) =>
{
Ok(())
}
None => Err(format!("it decides remake {id}, proposed nowhere")),
},
Some(_) => Err(format!(
"it writes {}: a remake's abort is its coordinator's alone",
shown(&op.path)
)),
}
}
/// Whether `change` writes at `proposal` a compaction's proposal that names `node` its
/// coordinator.
fn proposes(change: &Change<'_>, proposal: &Path, node: &str) -> bool {
let at = |key: &str| {
let mut path = proposal.clone();
path.push(key.to_owned());
path
};
let (coordinator, kind) = (at("coordinator"), at("kind"));
let put = |path: &Path| {
change.ops.iter().find_map(|op| match &op.action {
Action::PutString(text) if op.path == *path => Some(text.as_str()),
_ => None,
})
};
put(&coordinator) == Some(node)
&& put(&kind).is_none_or(|kind| kind == crate::remake::DEFAULT_KIND)
&& change
.ops
.iter()
.filter(|op| op.path == kind)
.all(|op| matches!(&op.action, Action::PutString(_)))
}
/// Whether `op`, under `_continuum/events`, puts an event at its ID, held nowhere at `at`,
/// no larger than the fold `fold` allows.
fn event(at: &At<'_>, op: &Op, fold: &str) -> Result<(), String> {
let [_, _, key] = op.path.as_slice() else {
return Err(format!("it writes {}, no event's place", shown(&op.path)));
};
let Action::PutBytes(bytes) = &op.action else {
return Err(format!("it writes {}, no event's bytes", shown(&op.path)));
};
if bytes.len() > largest(fold) {
return Err(format!(
"an event of {} bytes, more than {} allows",
bytes.len(),
fold
));
}
if hex(&Sha256::digest(bytes)) != *key {
return Err(format!("an event at {key}, which isn't its ID"));
}
if store::held_anywhere(at, &op.path) {
return Err(format!("an event at {key}, held there already"));
}
Ok(())
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}
fn shown(path: &Path) -> String {
format!("/{}", path.join("/"))
}
/// Whether `ops` write a policy naming a store: anything at its `store`, or an envelope
/// at its place naming one.
pub(crate) fn turns_store(ops: &[Op]) -> bool {
let policy = super::policy::path();
let mut store = policy.clone();
store.push("store".to_owned());
ops.iter().any(|op| match &op.action {
_ if op.path.starts_with(&store) => true,
Action::PutString(text) if op.path == policy => crate::document::signed::read(text)
.is_some_and(|payload| payload.value.get("store").is_some()),
_ => false,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::clock::UnixMillis;
use crate::document::replica::{Epoch, Writer, transact};
use crate::document::store::StoreError;
use crate::document::typed::{TimestampFormat, Val};
use crate::document::{Document, ScalarValue, Transaction, events};
use crate::federation::store::Id;
use serde_json::json;
const ROOT: &[u8] = b"{\"a root\":true}";
/// A swarm holding a store, from its genesis change.
fn swarm() -> Document {
events::started("federation", ROOT, &[]).unwrap()
}
/// The change `node` writes to `doc` with `f`, judged as every node judges it.
fn judged(
doc: &mut Document,
node: &str,
f: impl FnOnce(&mut Transaction<'_>) -> Result<(), StoreError>,
) -> Result<(), String> {
// Another node writes under a writer of its own: a writer's changes are its
// first's author's.
if doc
.get_author()
.is_some_and(|author| author.as_bytes() != node.as_bytes())
{
doc.set_actor(crate::document::ActorId::random());
}
let before = doc.get_heads();
transact(doc, UnixMillis(1), &Writer { node, user: None }, f).unwrap();
let after = doc.get_heads();
assert_ne!(after, before, "no change written");
super::super::rules::judge_held(doc, Epoch::GENESIS, &after[0]).unwrap()
}
fn put(doc: &mut Transaction<'_>, key: &str, bytes: &[u8]) -> Result<(), StoreError> {
store::write_val_path(
doc,
&meta::path(&[meta::EVENTS, key]),
&Val::Scalar(ScalarValue::Bytes(bytes.to_vec())),
store::Writing::sticky(TimestampFormat::default()),
)
.map(|_| ())
}
#[test]
fn a_change_puts_events_at_their_ids() {
let event = b"an event".to_vec();
let id = Id::of(&event);
let key = id.to_string();
let mut doc = swarm();
let mut beside = doc.fork();
assert_eq!(
judged(&mut doc, "node-a", |doc| put(doc, &key, &event)),
Ok(())
);
// The same, by another at once: one event.
assert_eq!(
judged(&mut beside, "node-b", |doc| put(doc, &key, &event)),
Ok(())
);
// Again, at a key held already: refused. Automerge writes no put of the value a
// key holds, so the change is one made by hand.
let heads = doc.get_heads();
let at = At::new(&doc, &heads).unwrap();
let path = meta::path(&[meta::EVENTS, &key]);
let again = Change {
hash: crate::document::ChangeHash([0; 32]),
deps: &heads,
seen: &heads,
committer: Some("node-b"),
user: None,
paths: &std::collections::BTreeSet::from([path.clone()]),
ops: &[Op {
path,
action: Action::PutBytes(event.clone()),
}],
};
assert!(judge(&at, Epoch::GENESIS, &again, "federation").is_err());
// A string, not bytes: refused.
let mut doc = swarm();
assert!(
judged(&mut doc, "node-a", |doc| {
store::write_path(doc, &meta::path(&[meta::EVENTS, &key]), &json!("an event"))
})
.is_err()
);
}
/// An event deleted, or replaced: refused.
#[test]
fn store_attack_4() {
let root = Id::of(ROOT).to_string();
let mut doc = swarm();
assert!(
judged(&mut doc, "node-a", |doc| {
store::delete_path(doc, &meta::path(&[meta::EVENTS, &root]), false)
})
.is_err()
);
let mut doc = swarm();
assert!(judged(&mut doc, "node-a", |doc| put(doc, &root, b"other bytes")).is_err());
}
/// A node writing another node's entry, or under `_continuum` where it may not:
/// refused; its own entry it writes.
#[test]
fn store_attack_18() {
let entry = |node: &str| meta::path(&[meta::MEMBERS, node]);
let mut doc = swarm();
assert_eq!(
judged(&mut doc, "node-a", |doc| {
store::write_path(doc, &entry("node-a"), &json!({"alias": "a"}))
}),
Ok(())
);
assert!(
judged(&mut doc, "node-a", |doc| {
store::write_path(doc, &entry("node-b"), &json!({}))
})
.is_err()
);
for closed in [
meta::path(&["else"]),
meta::path(&[meta::USERS, "alice"]),
meta::path(&[meta::SWARM]),
meta::path(&[meta::NEIGHBOURS]),
meta::path(&[meta::VOTES, "node-b"]),
crate::access::policy::path(),
] {
let mut doc = swarm();
assert!(
judged(&mut doc, "node-a", |doc| store::write_path(
doc,
&closed,
&json!(1)
))
.is_err(),
"{closed:?}"
);
}
}
#[test]
fn a_swarm_turns_into_no_store() {
let mut doc = Document::new();
transact(
&mut doc,
UnixMillis(1),
&Writer {
node: "node-a",
user: None,
},
|doc| store::write_path(doc, &meta::path(&[meta::SWARM]), &json!("s")),
)
.unwrap();
assert!(
judged(&mut doc, "node-a", |doc| {
store::write_path(
doc,
&crate::access::policy::path(),
&events::policy("federation"),
)
})
.is_err()
);
}
// The store's attack rows (`docs/store.md`, Attacks) judged here.
/// A change writing outside `_continuum/events` and the protocol data listed: refused.
#[test]
fn store_attack_1() {
let mut doc = swarm();
assert!(
judged(&mut doc, "node-a", |doc| store::write_path(
doc,
&["config".to_owned()],
&json!(1)
))
.is_err()
);
}
/// An event at a key its bytes don't hash to: refused.
#[test]
fn store_attack_2() {
let mut doc = swarm();
assert!(
judged(&mut doc, "node-a", |doc| put(
doc,
&Id::of(b"else").to_string(),
b"an event"
))
.is_err()
);
}
/// A change by a node that isn't a member, brought by one that is: taken, as nothing
/// here asks who is a member.
#[test]
fn store_attack_7() {
let event = b"an event".to_vec();
let mut doc = swarm();
assert_eq!(
judged(&mut doc, "a-node-no-party-lists", |doc| put(
doc,
&Id::of(&event).to_string(),
&event
)),
Ok(())
);
}
/// `node`'s proposal of remake `id`, of `kind`, naming `coordinator`.
fn propose(
doc: &mut Document,
node: &str,
id: &str,
kind: &str,
coordinator: &str,
) -> Result<(), String> {
judged(doc, node, |doc| {
store::write_path(
doc,
&meta::path(&[meta::REMAKES, id, "proposal"]),
&json!({"coordinator": coordinator, "kind": kind, "quorum": 1}),
)
})
}
fn decide(doc: &mut Document, node: &str, id: &str) -> Result<(), String> {
judged(doc, node, |doc| {
store::write_path(
doc,
&meta::path(&[meta::REMAKES, id, "decision"]),
&json!({"outcome": "committed"}),
)
})
}
/// A node's acks and a compaction it coordinates, proposed and decided by it: taken.
/// Another's acks, a second proposal of one remake, a proposal of another kind or
/// naming another coordinator: refused.
#[test]
fn a_node_proposes_acks_and_decides_compactions() {
let mut doc = swarm();
let acks = |node: &str| meta::path(&[meta::VOTES, node]);
assert_eq!(
judged(&mut doc, "node-a", |doc| {
store::write_path(doc, &acks("node-a"), &json!({"remake": "r"}))
}),
Ok(())
);
assert!(
judged(&mut doc, "node-a", |doc| {
store::write_path(doc, &acks("node-b"), &json!({"remake": "r"}))
})
.is_err()
);
assert_eq!(
propose(&mut doc, "node-a", "r", "compact", "node-a"),
Ok(())
);
assert!(
judged(&mut doc, "node-a", |doc| {
store::write_path(
doc,
&meta::path(&[meta::REMAKES, "r", "proposal"]),
&json!({"coordinator": "node-a", "kind": "compact", "quorum": 2}),
)
})
.is_err()
);
assert!(propose(&mut doc, "node-a", "s", "replace", "node-a").is_err());
assert!(propose(&mut doc, "node-a", "s", "compact", "node-b").is_err());
assert_eq!(decide(&mut doc, "node-a", "r"), Ok(()));
}
/// A decision or abort written by a node not its coordinator: refused.
#[test]
fn store_attack_14() {
let mut doc = swarm();
assert_eq!(
propose(&mut doc, "node-a", "r", "compact", "node-a"),
Ok(())
);
assert!(decide(&mut doc, "node-b", "r").is_err());
assert!(decide(&mut doc, "node-b", "unproposed").is_err());
assert!(
judged(&mut doc, "node-b", |doc| {
store::write_path(
doc,
&meta::path(&[meta::REMAKES, "r", "abortRequested"]),
&json!({}),
)
})
.is_err()
);
}
/// A node no longer a member decides a remake, its decision brought by a member: taken,
/// as any decision, as nothing here asks who is a member.
#[test]
fn store_attack_21() {
let mut doc = swarm();
let gone = "a-node-no-party-lists";
assert_eq!(propose(&mut doc, gone, "r", "compact", gone), Ok(()));
assert_eq!(decide(&mut doc, gone, "r"), Ok(()));
}
/// An event larger than the fold allows, put by a change: refused, by the change's own
/// bytes.
#[test]
fn store_attack_32() {
let large = vec![b' '; crate::federation::event::LARGEST + 1];
let mut doc = swarm();
assert!(
judged(&mut doc, "node-a", |doc| put(
doc,
&Id::of(&large).to_string(),
&large
))
.is_err()
);
}
/// One to three writes `node` makes at random: events at their IDs, larger than the
/// fold allows or not, again, or at other keys; strings and maps under the events;
/// deletions; its entry or another's; and what is closed. `written` holds the events
/// put so far, by any node.
fn written_at_random(
doc: &mut Transaction<'_>,
rng: &mut crate::testrand::Rng,
node: &str,
written: &mut Vec<Vec<u8>>,
) -> Result<(), StoreError> {
let largest = crate::federation::event::LARGEST;
for _ in 0..rng.between(1, 3) {
let fresh = |rng: &mut crate::testrand::Rng| {
let mut bytes = rng.bytes32().to_vec();
bytes.resize(
if rng.chance(0.1) {
largest + 1
} else {
rng.between(32, 200)
},
b' ',
);
bytes
};
let some = rng.pick(written.as_slice()).clone();
let key = Id::of(&some).to_string();
let entry = |node: &str| meta::path(&[meta::MEMBERS, node]);
match rng.below(10) {
0 | 1 => {
let bytes = fresh(rng);
put(doc, &Id::of(&bytes).to_string(), &bytes)?;
written.push(bytes);
}
2 => put(doc, &key, &some)?,
3 => put(doc, &key, &fresh(rng))?,
4 => store::write_path(doc, &meta::path(&[meta::EVENTS, &key]), &json!("text"))?,
5 => store::write_path(doc, &meta::path(&[meta::EVENTS, &key]), &json!({"x": 1}))?,
6 => store::delete_path(doc, &meta::path(&[meta::EVENTS, &key]), false)?,
7 => store::write_path(doc, &entry(node), &json!({"alias": node}))?,
8 => store::write_path(doc, &entry("node-elsewhere"), &json!({}))?,
_ => {
let closed: [Vec<String>; 3] = [
vec!["config".to_owned()],
meta::path(&[meta::SWARM]),
crate::access::policy::path(),
];
let path = rng.pick(&closed);
store::write_path(doc, path, &json!(1))?
}
}
}
Ok(())
}
/// Changes generated: three nodes write at random (`written_at_random`), each now and
/// then catching up with a node that takes their changes as it receives them
/// (`screen::apply_bytes`). What it takes leaves at every key under
/// `_continuum/events` the bytes whose ID it is, the root's among them; its document
/// saves and loads; and another node, given all it took, takes it all.
#[test]
fn what_a_node_takes_leaves_its_events_sound() {
use crate::access::screen::{Screening, apply_bytes};
use crate::document::Change;
use std::cell::Cell;
let id = Id::of(ROOT);
let (taken, refused) = (Cell::new(0), Cell::new(0));
let offer =
|doc: &mut Document, changes: &[Change], screening: &std::sync::Mutex<Screening>| {
let bytes: Vec<u8> = changes
.iter()
.flat_map(|change| change.raw_bytes().to_vec())
.collect();
apply_bytes(
doc,
Epoch::GENESIS,
0,
&bytes,
screening,
None,
&mut crate::document::PatchLog::inactive(),
)
.unwrap();
};
crate::testrand::for_seeds(24, |rng| {
let screening = std::sync::Mutex::new(Screening::default().holding(Some(id)));
let mut ours = Document::new();
offer(
&mut ours,
&[events::genesis("federation", ROOT)],
&screening,
);
let nodes = ["node-a", "node-b", "node-c"];
let mut writers: Vec<Document> = nodes.iter().map(|_| ours.fork()).collect();
let mut written = vec![ROOT.to_vec()];
for _ in 0..40 {
let at = rng.below(nodes.len());
let writer = &mut writers[at];
let before = writer.get_heads();
let _ = transact(
writer,
UnixMillis(1),
&Writer {
node: nodes[at],
user: None,
},
|doc| written_at_random(doc, rng, nodes[at], &mut written),
);
let changes = writer.get_changes(&before);
for change in &changes {
let heads = ours.get_heads();
offer(&mut ours, std::slice::from_ref(change), &screening);
let counted = if ours.get_heads() == heads {
&refused
} else {
&taken
};
counted.set(counted.get() + 1);
}
if rng.chance(0.3) {
*writer = ours.fork();
}
}
assert_eq!(events::bytes(&ours, &id).as_deref(), Some(ROOT));
for event in events::ids(&ours) {
let bytes = events::bytes(&ours, &event).expect("bytes at an event's key");
assert_eq!(Id::of(&bytes), event);
assert!(bytes.len() <= crate::federation::event::LARGEST);
}
let loaded = Document::load(&ours.save()).expect("it loads");
assert_eq!(loaded.get_heads(), ours.get_heads());
let mut other = Document::new();
let screening = std::sync::Mutex::new(Screening::default().holding(Some(id)));
offer(&mut other, &ours.get_changes(&[]), &screening);
assert_eq!(other.get_heads(), ours.get_heads());
});
assert!(
taken.get() > 0 && refused.get() > 0,
"{} taken, {} refused",
taken.get(),
refused.get()
);
}
}
|