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
|
//! What the federation's tests build with (`docs/federation-tests.md`, The harness):
//! parties with keys made from seeds, signing events as the design says, and versions.
use ed25519_dalek::SigningKey;
use super::event;
use super::jcs::Json;
use super::store::Id;
use crate::access::sshsig::PublicKey;
/// A party of the tests: a name and a key, made from a seed.
#[derive(Clone)]
pub(crate) struct Party {
pub(crate) name: String,
pub(crate) key: SigningKey,
}
/// A key made from a seed, spelled as the federation spells one.
pub(crate) fn key_line(seed: u8) -> String {
PublicKey(
SigningKey::from_bytes(&[seed; 32])
.verifying_key()
.to_bytes(),
)
.line()
}
/// A node ID made from a seed.
pub(crate) fn node_id(seed: u8) -> String {
crate::identity::node_id_of(
&SigningKey::from_bytes(&[seed.wrapping_add(128); 32])
.verifying_key()
.to_bytes(),
)
}
pub(crate) fn str(text: &str) -> Json {
Json::Str(text.to_owned())
}
/// The JSON of IDs, as an event or a version names them.
pub(crate) fn ids(ids: &[Id]) -> Json {
let mut ids = ids.to_vec();
ids.sort();
ids.dedup();
Json::Array(ids.iter().map(|id| Json::Str(id.to_string())).collect())
}
pub(crate) fn object(members: Vec<(&str, Json)>) -> Json {
Json::object(
members
.into_iter()
.map(|(name, value)| (name.to_owned(), value))
.collect(),
)
.expect("names once each")
}
impl Party {
pub(crate) fn new(name: &str, seed: u8) -> Party {
Party {
name: name.to_owned(),
key: SigningKey::from_bytes(&[seed; 32]),
}
}
/// Its key, spelled as the federation spells one.
pub(crate) fn key_line(&self) -> String {
PublicKey(self.key.verifying_key().to_bytes()).line()
}
/// An event it signs, on `deps`, doing `does` (a field's name, and what it holds), with
/// `key` its key, signed with `signer`: the two differ only where a test wants them to.
pub(crate) fn signed_as(
&self,
signer: &SigningKey,
key: &str,
deps: &[Id],
does: (&str, Json),
) -> Vec<u8> {
event::signed(signer, &self.name, key, deps, does)
}
pub(crate) fn sign(&self, deps: &[Id], does: (&str, Json)) -> Vec<u8> {
self.signed_as(&self.key, &self.key_line(), deps, does)
}
pub(crate) fn write(&self, deps: &[Id], path: &str, value: Json) -> Vec<u8> {
self.sign(deps, event::write(path, value))
}
pub(crate) fn keys(&self, deps: &[Id], keys: &[String]) -> Vec<u8> {
self.sign(deps, event::keys(keys))
}
pub(crate) fn nodes(&self, deps: &[Id], nodes: &[String]) -> Vec<u8> {
self.sign(deps, event::nodes(nodes))
}
pub(crate) fn propose(&self, deps: &[Id], version: &Json) -> Vec<u8> {
self.sign(deps, event::proposal(version))
}
pub(crate) fn vote(&self, deps: &[Id], on: Id, proposal: Id, yes: bool) -> Vec<u8> {
self.sign(deps, event::vote(on, proposal, yes))
}
}
/// A version event, which nobody signs.
pub(crate) fn version_event(deps: &[Id], version: Id) -> Vec<u8> {
event::version_event(deps, version)
}
/// What a version gives a party, as a test spells it out.
#[derive(Clone)]
pub(crate) struct SpecParty {
pub(crate) name: String,
pub(crate) keys_since: u64,
pub(crate) keys: Vec<String>,
pub(crate) nodes_since: u64,
pub(crate) nodes: Vec<String>,
}
/// A version, as a test spells it out.
#[derive(Clone)]
pub(crate) struct Spec {
pub(crate) number: u64,
pub(crate) changes: Option<Id>,
pub(crate) parties: Vec<SpecParty>,
/// Each rule: its path and its writer.
pub(crate) rules: Vec<(String, Json)>,
pub(crate) checkpoint: Option<Vec<Id>>,
}
impl Spec {
/// A first version naming these parties, each with its key and no nodes but the
/// first's, with a namespace each (`home/{party}/**`) and the rules given after.
pub(crate) fn first(parties: &[&Party], rules: &[(&str, Json)]) -> Spec {
Spec {
number: 0,
changes: None,
parties: parties
.iter()
.enumerate()
.map(|(i, party)| {
let nodes = if i == 0 { vec![node_id(0)] } else { Vec::new() };
SpecParty {
name: party.name.clone(),
keys_since: 0,
keys: vec![party.key_line()],
nodes_since: 0,
nodes,
}
})
.collect(),
rules: std::iter::once(("home/{party}/**".to_owned(), str("{party}")))
.chain(
rules
.iter()
.map(|(path, writer)| ((*path).to_owned(), writer.clone())),
)
.collect(),
checkpoint: None,
}
}
/// The version after `before`, which this spells: one past its number, changing its
/// hash, nothing named anew.
pub(crate) fn after(&self, before: Id) -> Spec {
Spec {
number: self.number + 1,
changes: Some(before),
checkpoint: None,
..self.clone()
}
}
pub(crate) fn json(&self) -> Json {
let mut members = vec![
("federation", Json::Int(1)),
("number", Json::Int(self.number as i64)),
(
"changes",
self.changes
.map_or(Json::Null, |hash| Json::Str(hash.to_string())),
),
(
"keys",
object(
self.parties
.iter()
.map(|party| {
(
party.name.as_str(),
object(vec![
("since", Json::Int(party.keys_since as i64)),
(
"keys",
Json::Array(
party.keys.iter().map(|key| str(key)).collect(),
),
),
]),
)
})
.collect(),
),
),
(
"nodes",
object(
self.parties
.iter()
.map(|party| {
(
party.name.as_str(),
object(vec![
("since", Json::Int(party.nodes_since as i64)),
(
"nodes",
Json::Array(party.nodes.iter().map(|id| str(id)).collect()),
),
]),
)
})
.collect(),
),
),
(
"rules",
Json::Array(
self.rules
.iter()
.map(|(path, writer)| {
object(vec![("path", str(path)), ("writer", writer.clone())])
})
.collect(),
),
),
];
if let Some(heads) = &self.checkpoint {
members.push(("checkpoint", ids(heads)));
}
object(members)
}
/// The version's hash.
pub(crate) fn hash(&self) -> Id {
Id::of(&self.json().bytes())
}
/// The root of a federation whose first version this is.
pub(crate) fn root(&self) -> Vec<u8> {
event::Checkpoint {
at: Vec::new(),
keeps: Vec::new(),
version: super::version::Version::read(&self.json()).expect("a version"),
}
.bytes()
}
}
/// Checks the attack table of `doc` (its section Attacks): every row has its test, named
/// `{prefix}{row}`, among the sources of its kind, `unit` rows among `unit` and `sim`
/// rows among `sim`, but for the rows `later`, whose tests come with later stages; a row
/// marked `—` has none; and no test names a row that isn't there, nor two tests one row.
pub(crate) fn every_row_has_its_test(
doc: &str,
prefix: &str,
unit: &[&str],
sim: &[&str],
later: &[u32],
) {
let attacks = doc.split("\n## Attacks").nth(1).expect("an attack table");
let attacks = attacks.split("\n## ").next().unwrap_or(attacks);
let rows: std::collections::BTreeMap<u32, String> = attacks
.lines()
.filter_map(|line| {
let cells: Vec<&str> = line.split('|').map(str::trim).collect();
Some((cells.get(1)?.parse().ok()?, cells.get(4)?.to_string()))
})
.collect();
let tests = |sources: &[&str]| -> Vec<u32> {
sources
.iter()
.flat_map(|source| source.lines())
.filter_map(|line| {
let line = line.trim();
line.strip_prefix("async ")
.unwrap_or(line)
.strip_prefix("fn ")?
.strip_prefix(prefix)?
.split('(')
.next()?
.parse()
.ok()
})
.collect()
};
let (unit, sim) = (tests(unit), tests(sim));
let all: Vec<u32> = unit.iter().chain(&sim).copied().collect();
for n in &all {
assert!(rows.contains_key(n), "a test of row {n}, which isn't there");
assert_eq!(
all.iter().filter(|m| *m == n).count(),
1,
"row {n} tested twice"
);
}
let mut missing = Vec::new();
for (n, kind) in &rows {
let none = kind == "—";
let tested = if kind.starts_with("unit") {
unit.contains(n)
} else if kind == "sim" {
sim.contains(n)
} else {
false
};
if later.contains(n) {
assert!(!all.contains(n), "row {n} has its test now");
assert!(!none, "row {n} asks for no test");
} else if none {
assert!(!all.contains(n), "row {n} asks for no test, and has one");
} else if !tested {
missing.push(*n);
}
}
assert!(
missing.is_empty(),
"rows without their test of their kind: {missing:?}"
);
}
|