aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/corpus.rs
blob: f19e2b17bc3811d3054df239137ba63b722ed38d (plain)
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
//! What each revision of federation 1 takes, refuses and reads, as fixed bytes
//! (`docs/federation-tests.md`, The corpus): for each revision this continuum knows, a file
//! in `corpus/` of histories generated at that revision, each with what became of every
//! event and what the store reads after, written once when the revision is made
//! (`write_the_corpus`) and never edited after. Every later continuum judges them alike: a
//! change to judging that alters what an earlier revision's corpus says comes as a later
//! revision.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write as _;
use std::path::PathBuf;

use super::event::{Does, Event};
use super::fold::Federation;
use super::properties::{self, History, Kind};
use super::revision::{Feature, Revision};
use super::store::Id;
use super::store::memory::{Refusal, Store};
use crate::testrand::Rng;

/// Of each kind, the seeds histories are generated from, the first so many kept
/// whatever they show, and at most how many kept.
const SCANNED: u64 = 400;
const FIRST: u64 = 2;
const MOST: usize = 8;

/// The kinds of history generated: with an adversary or not, versions naming checkpoints
/// or not, and versions this continuum doesn't read or not.
fn kinds() -> Vec<Kind> {
    let mut kinds = Vec::new();
    for adversary in [false, true] {
        for checkpoints in [false, true] {
            for unread in [false, true] {
                kinds.push(Kind {
                    adversary,
                    checkpoints,
                    unread,
                });
            }
        }
    }
    kinds
}

fn dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/federation/corpus")
}

/// The file of `revision`'s corpus: by its number and the feature it adds, or `base`.
fn file(revision: Revision) -> PathBuf {
    let name = match revision.features().last() {
        None => "0-base".to_owned(),
        Some(feature) => format!("{}-{}", revision.0, feature.name()),
    };
    dir().join(format!("{name}.txt"))
}

/// The first line of `revision`'s corpus, naming it by its ID, so that a renamed feature
/// fails as a changed judgment does.
fn header(revision: Revision) -> String {
    format!(
        "continuum federation 1, {revision}, ID {}: never edited once released",
        revision.id()
    )
}

/// What became of `id` in `store`, by kind: never a refusal's words, which may change.
fn became(store: &Store<Federation>, id: &Id) -> String {
    if store.stopped().is_some_and(|(at, ..)| at == id) {
        return "stopped at".to_owned();
    }
    if store.held_back().iter().any(|(held, _)| held == id) {
        return "held back".to_owned();
    }
    if store.holds(id) {
        return "taken".to_owned();
    }
    if store.get(id).is_some() {
        return "kept".to_owned();
    }
    match store.refusal(id) {
        Some(Refusal::None) => "refused: none".to_owned(),
        Some(Refusal::Judged(_)) => "refused: judged".to_owned(),
        Some(Refusal::BuiltOn(_)) => "refused: built on a refused one".to_owned(),
        Some(Refusal::NotAbove) => "refused: not above the checkpoint".to_owned(),
        Some(Refusal::Own) => "refused: its own".to_owned(),
        Some(Refusal::Dropped) => "refused: dropped".to_owned(),
        None if store.is_waiting(id) => "waiting".to_owned(),
        None => "gone with a move".to_owned(),
    }
}

/// What `store` reads: its checkpoint, the current version, the values at places, the
/// members, and the event it stopped at; a line of JSON.
fn reads(store: &Store<Federation>) -> String {
    let state = store.state();
    let values: BTreeMap<String, String> = state
        .values()
        .into_iter()
        .map(|(path, value)| (path, String::from_utf8_lossy(&value.bytes()).into_owned()))
        .collect();
    serde_json::json!({
        "checkpoint": Id::of(store.checkpoint()).to_string(),
        "current": state.current().hash.to_string(),
        "values": values,
        "members": state.members(),
        "stopped": store.stopped().map(|(id, ..)| id.to_string()),
    })
    .to_string()
}

/// A store from `root`, offered `events` in their order.
fn offered(root: &[u8], events: &[Vec<u8>]) -> Store<Federation> {
    let mut store = Store::new(root.to_vec(), Vec::new()).expect("a root");
    for event in events {
        store.offer(event.clone());
    }
    store
}

/// What a history shows of what a corpus is to hold: of each kind of event, what became
/// of one; and whether a version was written, the store moved to a checkpoint, or stopped.
fn shows(history: &History) -> BTreeSet<String> {
    let store = offered(&history.root, &history.events);
    let mut shown = BTreeSet::new();
    if store.state().current().number > 0 {
        shown.insert("a version written".to_owned());
    }
    if Id::of(store.checkpoint()) != store.id() {
        shown.insert("a move to a checkpoint".to_owned());
    }
    if store.stopped().is_some() {
        shown.insert("a stop".to_owned());
    }
    for event in &history.events {
        let kind = match Event::read_taken(event) {
            Some(Event::Signed(signed)) => match signed.does {
                Does::Write { .. } => "a write",
                Does::Keys(_) => "a key set",
                Does::Nodes(_) => "a node list",
                Does::Proposal { .. } => "a proposal",
                Does::Vote { .. } => "a vote",
                Does::Merge => "a merge event",
            },
            Some(Event::Version { .. }) => "a version event",
            None => "no event",
        };
        shown.insert(format!("{kind}: {}", became(&store, &Id::of(event))));
    }
    shown
}

/// A case: a root, and its events offered in the order written; what each became, and
/// what the store reads after, as the corpus spells them.
fn case(label: &str, root: &[u8], events: &[Vec<u8>]) -> String {
    let store = offered(root, events);
    let mut text = format!("case {label}\nroot {}\n", String::from_utf8_lossy(root));
    for event in events {
        let _ = writeln!(text, "event {}", String::from_utf8_lossy(event));
        let _ = writeln!(text, "became {}", became(&store, &Id::of(event)));
    }
    let _ = writeln!(text, "reads {}", reads(&store));
    text
}

/// `revision`'s corpus, generated: of each kind, histories whose first version follows
/// `revision`, those of the first seeds, and of the rest those showing what the ones kept
/// don't (`shows`), up to `MOST`.
fn generated(revision: Revision) -> String {
    let mut text = header(revision) + "\n";
    for kind in kinds() {
        let mut held = BTreeSet::new();
        let mut kept = 0;
        for seed in 0..SCANNED {
            if kept == MOST {
                break;
            }
            let history = properties::generate_at(&mut Rng::new(seed), kind, Some(revision));
            let shown = shows(&history);
            if seed >= FIRST && shown.is_subset(&held) {
                continue;
            }
            held.extend(shown);
            kept += 1;
            let label = format!("seed {seed}, {kind:?}");
            text += &case(&label, &history.root, &history.events);
        }
    }
    text
}

/// A corpus's cases, read: each its label, its root, and its events with what each
/// became, and what the store reads after.
struct Case {
    label: String,
    root: Vec<u8>,
    events: Vec<(Vec<u8>, String)>,
    reads: String,
}

fn parse(text: &str) -> Result<(String, Vec<Case>), String> {
    let mut lines = text.lines();
    let header = lines.next().ok_or("an empty corpus")?.to_owned();
    let mut cases: Vec<Case> = Vec::new();
    let mut event: Option<Vec<u8>> = None;
    for line in lines {
        let (word, rest) = line.split_once(' ').ok_or(format!("no line: {line:?}"))?;
        let current = cases.last_mut();
        match (word, current) {
            ("case", _) => cases.push(Case {
                label: rest.to_owned(),
                root: Vec::new(),
                events: Vec::new(),
                reads: String::new(),
            }),
            ("root", Some(case)) => case.root = rest.as_bytes().to_vec(),
            ("event", Some(_)) => event = Some(rest.as_bytes().to_vec()),
            ("became", Some(case)) => {
                let bytes = event.take().ok_or("`became` without its event")?;
                case.events.push((bytes, rest.to_owned()));
            }
            ("reads", Some(case)) => case.reads = rest.to_owned(),
            _ => return Err(format!("no line of a corpus: {line:?}")),
        }
    }
    Ok((header, cases))
}

/// Writes the corpus of every revision this continuum knows that has none: once, when a
/// revision is made, its file then committed and never edited. Run by hand:
/// `cargo test --features federation write_the_corpus -- --ignored`.
#[test]
#[ignore = "writes into the source tree: run by hand when a revision is made"]
fn write_the_corpus() {
    std::fs::create_dir_all(dir()).expect("the corpus's directory");
    for revision in Revision::known() {
        let path = file(revision);
        if !path.exists() {
            std::fs::write(&path, generated(revision)).expect("the corpus written");
            println!("wrote {}", path.display());
        }
    }
}

/// Every revision this continuum knows has its corpus, which it judges and reads as the
/// corpus says; and which shows a version written, a move to a checkpoint and a stop, and
/// what became of a merge event at that revision: taken where it has merge events, else
/// refused.
#[test]
fn every_revision_judges_as_its_corpus_says() {
    for revision in Revision::known() {
        let path = file(revision);
        let text = std::fs::read_to_string(&path).unwrap_or_else(|_| {
            panic!(
                "no corpus of {revision} ({}): write it once, as the revision is made, with \
                 `cargo test --features federation write_the_corpus -- --ignored`, and \
                 commit it",
                path.display()
            )
        });
        let (named, cases) = parse(&text).unwrap_or_else(|why| panic!("{}: {why}", path.display()));
        assert_eq!(
            named,
            header(revision),
            "{}: another revision",
            path.display()
        );
        assert!(!cases.is_empty(), "{}: no case", path.display());
        let mut shown = BTreeSet::new();
        for case in cases {
            let events: Vec<Vec<u8>> = case.events.iter().map(|(bytes, _)| bytes.clone()).collect();
            let judged = self::case(&case.label, &case.root, &events);
            shown.extend(shows(&History {
                root: case.root.clone(),
                events,
            }));
            let (_, again) = parse(&format!("{named}\n{judged}")).expect("as written");
            let again = &again[0];
            for ((bytes, was), (_, is)) in case.events.iter().zip(&again.events) {
                assert_eq!(
                    was,
                    is,
                    "{}, {}: the event {} became otherwise",
                    path.display(),
                    case.label,
                    Id::of(bytes)
                );
            }
            assert_eq!(
                case.reads,
                again.reads,
                "{}, {}: the store reads otherwise",
                path.display(),
                case.label
            );
        }
        let merged = if revision.has(Feature::MergeEvents) {
            "a merge event: taken"
        } else {
            "a merge event: refused: judged"
        };
        for wanted in [
            "a version written",
            "a move to a checkpoint",
            "a stop",
            merged,
        ] {
            assert!(
                shown.contains(wanted),
                "{}: no case shows {wanted}",
                path.display()
            );
        }
    }
}