aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/sets-check219
1 files changed, 219 insertions, 0 deletions
diff --git a/scripts/sets-check b/scripts/sets-check
new file mode 100755
index 0000000..8deb7fe
--- /dev/null
+++ b/scripts/sets-check
@@ -0,0 +1,219 @@
+#!/bin/sh
+# Runs a federation of this tree's continuum on this host, over libp2p on loopback, and
+# moves it from Automerge documents to sets of events node by node, as its operators
+# would (docs/operations.md: Moving to sets of events), through the nodes' API and the
+# party's client all along:
+#
+# * a and b of tv, c of lass, each holding the store in its Automerge document;
+# * each started again of both kinds (--store-document, --store-both);
+# * each started again on its set alone;
+# * all stopped, and started from their sets' files;
+# * d, which tv lists then, joining with nothing but the store's ID.
+#
+# After each start, a value written at one node is read at every other, and what was
+# written before is still read. No node logs an error.
+#
+# scripts/sets-check
+#
+# Built with CONTINUUM_FEATURES (`most` unless given; libp2p and federation among
+# them). The nodes listen from CONTINUUM_SETS_CHECK_PORT on (47700 unless given), eight
+# ports.
+
+set -eu
+
+cd "$(git rev-parse --show-toplevel)"
+
+features=${CONTINUUM_FEATURES:-most}
+port=${CONTINUUM_SETS_CHECK_PORT:-47700}
+say() { echo "sets-check: $*" >&2; }
+
+work=$(mktemp -d)
+failed=false
+clean() {
+ for pid in "$work"/*/pid; do
+ [ -f "$pid" ] && kill "$(cat "$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 25 "$log" >&2
+ done
+ fi
+ rm -rf "$work"
+}
+trap clean EXIT
+trap 'exit 130' INT TERM
+fail() {
+ failed=true
+ say "$*"
+ exit 1
+}
+
+say "building this tree (features $features)"
+cargo build -q --features "$features" --bin continuum --bin continuum-party
+bin=${CARGO_TARGET_DIR:-$(pwd)/target}/debug
+node=$bin/continuum
+party=$bin/continuum-party
+
+number() { # NAME: a 0, b 1, c 2, d 3
+ case $1 in a) echo 0 ;; b) echo 1 ;; c) echo 2 ;; d) echo 3 ;; esac
+}
+http() { echo $((port + $(number "$1"))); }
+p2p() { echo $((port + 4 + $(number "$1"))); }
+seed() { echo "libp2p:/ip4/127.0.0.1/tcp/$(p2p "$1")"; }
+party_of() {
+ case $1 in c) echo lass ;; *) echo tv ;; esac
+}
+
+# Keys of the parties, and of the nodes, whose IDs the first version names.
+for name in tv lass; do
+ ssh-keygen -q -t ed25519 -N '' -f "$work/$name.key"
+done
+for name in a b c d; do
+ mkdir "$work/$name"
+ "$node" node-id --create "$work/$name/node.key" >"$work/$name/id" 2>/dev/null
+done
+id_of() { tail -n 1 "$work/$1/id"; }
+"$party" found --name sets-check "tv=$work/tv.key.pub" "lass=$work/lass.key.pub" \
+ --nodes "tv=$(id_of a),$(id_of b)" --nodes "lass=$(id_of c)" >"$work/first.json"
+"$party" root "$work/first.json" >"$work/root"
+# The store's ID: the SHA-256 of the root, the file's first line.
+store=$(printf '%s' "$(head -n 1 "$work/root")" | sha256sum | cut -d' ' -f1)
+# Each party's record of its votes, which its client writes by.
+for name in tv lass; do
+ "$party" --record "$work/$name.record" init "$name" --root "$work/root" >/dev/null
+done
+say "the federation $store: a and b of tv, c of lass"
+
+start() { # NAME [OPTION...]: the node, with what every start of it has
+ name=$1
+ shift
+ (cd "$work/$name" && exec "$node" --bind-addr "127.0.0.1:$(http "$name")" \
+ --user-header X-User --node-key-file node.key --state-file state.json \
+ --libp2p-enable --libp2p-listen "/ip4/127.0.0.1/tcp/$(p2p "$name")" \
+ --store-party "$(party_of "$name")" "$@" >>log 2>&1) &
+ echo $! >"$work/$name/pid"
+}
+stop() { # NAME
+ kill "$(cat "$work/$1/pid")" 2>/dev/null || true
+ wait "$(cat "$work/$1/pid")" 2>/dev/null || true
+ rm -f "$work/$1/pid"
+}
+# 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
+}
+ready() { curl -sf "http://127.0.0.1:$(http "$1")/ready"; }
+# The party's client at a node, as that node's party: it logs in by the party's key.
+client() { # NAME COMMAND...
+ name=$1
+ shift
+ "$party" --node "http://127.0.0.1:$(http "$name")" \
+ --key "$work/$(party_of "$name").key" \
+ --record "$work/$(party_of "$name").record" "$@"
+}
+reads() { # NAME PATH VALUE: whether the node reads the value there
+ [ "$(client "$1" read "$2" 2>/dev/null)" = "$3" ]
+}
+written=""
+# Writes a value at NAME, as its party, and waits until every node of $nodes reads it,
+# and everything written before.
+write_and_read() { # NAME KEY
+ path="home/$(party_of "$1")/$2"
+ within 30 client "$1" write "$path" "\"$2\"" || {
+ client "$1" write "$path" "\"$2\"" >&2 || true
+ fail "$1 takes no write at $path"
+ }
+ written="$written $path=$2"
+ for at in $nodes; do
+ for pair in $written; do
+ within 60 reads "$at" "${pair%%=*}" "\"${pair##*=}\"" ||
+ fail "$at doesn't read ${pair%%=*} (written at $1)"
+ done
+ done
+ say "written at $1, read at$(printf ' %s' $nodes): $path"
+}
+kind() { # NAME: the kinds the node says it holds, as its log has them
+ grep -c "as a set of events\|Going on with the store's document\|Begun the store's set" \
+ "$work/$1/log" || true
+}
+
+# On the Automerge document: a from the root, b and c by the store's ID.
+automerge() { # NAME
+ case $1 in
+ a) start a --store-root "$work/root" --store-file store.json --document-file document ;;
+ *) start "$1" --store-id "$store" --store-file store.json --document-file document \
+ --seeds "$(seed a)" ;;
+ esac
+}
+nodes="a b c"
+for name in $nodes; do
+ automerge "$name"
+ within 60 ready "$name" || fail "$name isn't ready on its Automerge document"
+done
+write_and_read a first
+write_and_read c second
+
+# Of both kinds, one at a time, everything else as it was.
+for name in $nodes; do
+ stop "$name"
+ case $name in
+ a) start a --store-root "$work/root" --store-file store.json --document-file document \
+ --store-document set.events --store-both ;;
+ *) start "$name" --store-id "$store" --store-file store.json --document-file document \
+ --seeds "$(seed a)" --store-document set.events --store-both ;;
+ esac
+ within 60 ready "$name" || fail "$name isn't ready of both kinds"
+ write_and_read "$name" "both-$name"
+done
+for name in $nodes; do
+ within 30 test -s "$work/$name/set.events" || fail "$name wrote no file of its set"
+done
+
+# On its set alone, one at a time, from the set's file.
+alone() { # NAME
+ case $1 in
+ a) start a --store-id "$store" --store-document set.events --seeds "$(seed b)" ;;
+ *) start "$1" --store-id "$store" --store-document set.events --seeds "$(seed a)" ;;
+ esac
+}
+for name in $nodes; do
+ stop "$name"
+ alone "$name"
+ within 60 ready "$name" || fail "$name isn't ready on its set alone"
+ write_and_read "$name" "set-$name"
+done
+
+# All down, and up again from their sets' files.
+for name in $nodes; do
+ stop "$name"
+done
+for name in $nodes; do
+ alone "$name"
+done
+for name in $nodes; do
+ within 60 ready "$name" || fail "$name isn't ready again"
+done
+write_and_read b again
+
+# A node tv lists then, with nothing but the store's ID.
+client a nodes --add "$(id_of d)" >/dev/null 2>&1 || fail "tv doesn't list d"
+start d --store-id "$store" --store-document set.events --seeds "$(seed a)"
+within 60 ready d || fail "d isn't ready: it didn't begin its set from what members gave"
+nodes="a b c d"
+write_and_read d joined
+
+if grep -l ' ERROR \|panicked' "$work"/*/log >/dev/null 2>&1; then
+ failed=true
+ grep -h ' ERROR \|panicked' "$work"/*/log | head -n 10 >&2
+ fail "a node logged an error"
+fi
+say "moved to sets of events, node by node: every write read everywhere, at every step"