aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/wire-check
blob: f53dafb5e9eadf233aaba62352a4c1eb250dde73 (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
#!/bin/sh
# Runs a swarm of continuum as a release tag builds it and as this tree does, on this
# host over libp2p on loopback, and checks how they get on (docs/development.md:
# Releasing). Where their wire versions meet: every node reads what each wrote, and
# none logs a peer of another wire version. Where they don't: the tag's nodes log this
# tree's as of another wire version, so their operators know to upgrade them.
#
#   scripts/wire-check [TAG]
#
# TAG is the latest v* tag unless given. The nodes: a of the tag (the swarm's first), b
# of this tree, c of the tag, d of this tree, each seeded with the one before it.
# CONTINUUM_WIRE_CHECK_FEATURES are the features both are built with, libp2p among them
# (federation,libp2p unless given); the tag's build is kept in CONTINUUM_RELEASE_TARGET
# (~/.cache/continuum/release unless given), as scripts/release keeps it. The nodes
# listen from CONTINUUM_WIRE_CHECK_PORT on (47600 unless given), eight ports.

set -eu

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

tag=${1:-$(git tag --list 'v[0-9]*.[0-9]*.[0-9]*' --sort=-version:refname | head -n 1)}
if [ -z "$tag" ]; then
    echo "wire-check: no v* tag to check against" >&2
    exit 2
fi
features=${CONTINUUM_WIRE_CHECK_FEATURES:-federation,libp2p}
cache=${XDG_CACHE_HOME:-$HOME/.cache}
target=${CONTINUUM_RELEASE_TARGET:-$cache/continuum/release}
port=${CONTINUUM_WIRE_CHECK_PORT:-47600}
say() { echo "wire-check: $*" >&2; }

work=$(mktemp -d)
pids=""
failed=false
clean() {
    for pid in $pids; do
        kill "$pid" 2>/dev/null || true
    done
    wait 2>/dev/null || true
    if $failed; then
        for log in "$work"/*/log; do
            [ -f "$log" ] || continue
            say "the last of $(basename "$(dirname "$log")")'s log:"
            tail -n 20 "$log" >&2
        done
    fi
    git worktree remove --force "$work/tagged" >/dev/null 2>&1 || true
    rm -rf "$work"
}
trap clean EXIT
trap 'exit 130' INT TERM
fail() {
    failed=true
    say "$*"
    exit 1
}

say "building this tree and $tag (features $features)"
cargo build -q --features "$features" --bin continuum
here_dir=${CARGO_TARGET_DIR:-$(pwd)/target}
cp "$here_dir/debug/continuum" "$work/here"
git worktree add -q --detach "$work/tagged" "$tag"
(cd "$work/tagged" && CARGO_TARGET_DIR=$target cargo build -q --features "$features" --bin continuum)
cp "$target/debug/continuum" "$work/there"

wire() { "$1" --version | sed -n 's/^wire versions \([0-9]*\) to \([0-9]*\)$/\1 \2/p'; }
set -- $(wire "$work/here")
here_oldest=$1 here_newest=$2
there=$(wire "$work/there")
if [ -z "$there" ]; then
    # Before 0.0.1 a build named no wire versions, and spoke none but its own.
    fail "$tag doesn't say which wire versions it speaks"
fi
set -- $there
there_oldest=$1 there_newest=$2
if [ "$here_oldest" -le "$there_newest" ] && [ "$there_oldest" -le "$here_newest" ]; then
    meet=true
    say "$tag speaks $there_oldest to $there_newest, this tree $here_oldest to $here_newest: they should talk"
else
    meet=false
    say "$tag speaks $there_oldest to $there_newest, this tree $here_oldest to $here_newest: they should say they don't"
fi

http() { echo $((port + $1)); }
p2p() { echo $((port + 4 + $1)); }
seed() { echo "libp2p:/ip4/127.0.0.1/tcp/$(p2p "$1")"; }
start() { # NAME NUMBER BINARY [OPTION...]
    name=$1 number=$2 binary=$3
    shift 3
    mkdir "$work/$name"
    (cd "$work/$name" && exec "$binary" --bind-addr "127.0.0.1:$(http "$number")" \
        --user-header X-User --node-key-file node.key --node-key-create \
        --state-file state.json --libp2p-enable \
        --libp2p-listen "/ip4/127.0.0.1/tcp/$(p2p "$number")" "$@" >log 2>&1) &
    pids="$pids $!"
}
api() { # NUMBER PATH [CURL OPTION...]
    number=$1 path=$2
    shift 2
    curl -sf -H 'X-User: wire-check' -H 'Accept: text/plain' "$@" \
        "http://127.0.0.1:$(http "$number")$path"
}
# Whether the command $2... succeeds within $1 seconds, tried every second.
within() {
    seconds=$1
    shift
    while ! "$@" >/dev/null 2>&1; do
        seconds=$((seconds - 1))
        [ "$seconds" -gt 0 ] || return 1
        sleep 1
    done
}

start a 0 "$work/there" --genesis
within 60 api 0 /ready || fail "a ($tag) isn't ready"
start b 1 "$work/here" --seeds "$(seed 0)"
start c 2 "$work/there" --seeds "$(seed 1)"
start d 3 "$work/here" --seeds "$(seed 2)"

other_version() { grep -l 'another wire version' "$work"/*/log; }

if ! $meet; then
    # The tag's nodes say so. This tree's say so too where the tag's SPEAKS goes ahead of
    # its proofs, as from this tree on.
    logged() { grep -q 'another wire version' "$work/a/log" "$work/c/log"; }
    within 60 logged || fail "$tag's nodes don't say they share no wire version with this tree's"
    say "they share no wire version, and say so:"
    grep -h 'another wire version' "$work"/*/log | head -n 4 >&2
    exit 0
fi

for number in 1 2 3; do
    within 120 api "$number" /ready || fail "node $number isn't ready"
done
for number in 0 1 2 3; do
    api "$number" "/store/wire-check/$number" -X PUT -H 'Content-Type: text/plain' \
        -d "written on $number" || fail "node $number didn't take a write"
done
reads_all() {
    for number in 0 1 2 3; do
        for written in 0 1 2 3; do
            [ "$(api "$number" "/store/wire-check/$written")" = "written on $written" ] ||
                return 1
        done
    done
}
within 60 reads_all || fail "not every node reads what each wrote"
if other_version >/dev/null; then
    fail "a node logs a peer of another wire version: $(other_version | tr '\n' ' ')"
fi
say "$tag and this tree talk: every node reads what each wrote, and none logs another wire version"