summaryrefslogtreecommitdiffstats
path: root/scripts/federation-check
blob: e31e00273917893f0f4abb542ec2f3cbfe5442a8 (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
#!/bin/sh
# Judges the same histories with continuum as a federation tag names it and with this
# tree, and says where they differ (docs/federation-tests.md: Against released
# revisions). Each release that makes a revision of the federation is tagged
# federation-<F>.<R>, the federation's number and the revision's (federation-1.1 for
# revision 1 of federation 1); a later one judges every history under the revisions that
# release knows as it did. Run before a release, from a checkout with its tags.
#
#   scripts/federation-check [TAG]
#
# TAG is the latest federation-<F>.<R> unless given. The histories: of each
# revision and kind, generated from CONTINUUM_TEST_SEED on (a random one, said, unless
# given), CONTINUUM_TEST_CASES times as many as `write_histories` writes; and those the
# attack tests offer. CONTINUUM_FEATURES are the features both are built with, a
# transport among them (federation,mycelium unless given); the tag's build is kept in
# CONTINUUM_CHECK_TARGET (~/.cache/continuum/federation-check unless given) for the
# next run.

set -eu

cd "$(git rev-parse --show-toplevel)"

tag=${1:-$(git tag --list 'federation-[0-9]*.[0-9]*' --sort=-version:refname | head -n 1)}
if [ -z "$tag" ]; then
    echo "federation-check: no federation-<F>.<R> tag to check against" >&2
    exit 2
fi
features=${CONTINUUM_FEATURES:-federation,mycelium}
seed=${CONTINUUM_TEST_SEED:-$(od -An -N4 -tu4 /dev/urandom | tr -d ' ')}
cache=${XDG_CACHE_HOME:-$HOME/.cache}
target=${CONTINUUM_CHECK_TARGET:-$cache/continuum/federation-check}

work=$(mktemp -d)
clean() {
    git worktree remove --force "$work/tagged" >/dev/null 2>&1 || true
    rm -rf "$work"
}
trap clean EXIT
trap 'exit 130' INT TERM

echo "federation-check: $tag against this tree, histories from seed $seed" >&2
echo "federation-check: CONTINUUM_TEST_SEED=$seed reruns them" >&2

CONTINUUM_HISTORIES="$work/histories" CONTINUUM_TEST_SEED=$seed \
    cargo test -q --features "$features" --lib write_histories -- --ignored >&2
cargo run -q --features "$features" --bin continuum-party -- judge \
    <"$work/histories" >"$work/here"

git worktree add -q --detach "$work/tagged" "$tag"
(cd "$work/tagged" && CARGO_TARGET_DIR=$target \
    cargo run -q --features "$features" --bin continuum-party -- judge) \
    <"$work/histories" >"$work/there"

# Histories whose root the tag's continuum reads as none, of a revision after it, it
# doesn't judge: left out on both sides.
awk '/^case /{label=substr($0, 6)} $0 == "root none"{print label}' "$work/there" \
    >"$work/unread"
keep() {
    awk -v unread="$work/unread" '
        BEGIN { while ((getline line < unread) > 0) skip[line] = 1 }
        /^case / { label = substr($0, 6) }
        !(label in skip)
    ' "$1"
}
keep "$work/there" >"$work/there.kept"
keep "$work/here" >"$work/here.kept"

judged=$(grep -c '^case ' "$work/there.kept" || true)
left=$(wc -l <"$work/unread" | tr -d ' ')
if diff -u --label "$tag" --label "this tree" "$work/there.kept" "$work/here.kept" \
    >"$work/differ"; then
    echo "federation-check: $judged histories judged alike" \
        "($left of revisions after $tag left out)"
else
    head -n 200 "$work/differ"
    echo "federation-check: this tree judges otherwise than $tag (seed $seed): above" >&2
    exit 1
fi