~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
220 lines · 8.7 KB Raw History
#!/usr/bin/env sh
# Interop lane: boot zds (and the reference PDS, and indigo's relay core)
# against a validating PLC directory, drive the same writes through each PDS,
# and judge the results with code other implementations trust:
#
#   firehose  every #commit verified strictly (signature, sync 1.1 prevData
#             and since chain, MST inversion, rebuilt MST root), live and
#             replayed from cursor 0, plus goat's own verifier
#   repo      each exported repo verified (hashes, signature, canonical MST)
#             and checked against the firehose and the read endpoints
#   relay     indigo's relay core consumes zds; everything it re-serves must
#             match, and it must log no warnings
#   diff      responses compared with the reference PDS's
#
# The reference PDS also runs the firehose and repo checks, which keeps the
# checkers honest: they must pass it.
#
# Environment:
#   INTEROP_LANES      lanes to run (default "zds reference relay diff")
#   INTEROP_RELAY_PDS  which PDS the relay consumes: zds (default) or reference
#   INTEROP_RELAY_CMD  run this instead of indigo's relay core; see
#                      docs/testing.md#interop for the contract (zlay uses it)
#   INTEROP_ZDS_BIN    a prebuilt zds binary; otherwise `zig build smoke-build`
#   INTEROP_BIN        where the Go tools are built (default zig-out/interop)
#   INTEROP_PORT       first of seven consecutive ports (default 2590)
#   INTEROP_KEEP       keep the work directory even when everything passed
set -eu

root=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd)
here="$root/tools/interop"
lanes=${INTEROP_LANES:-zds reference relay diff}
relay_pds=${INTEROP_RELAY_PDS:-zds}
bin=${INTEROP_BIN:-$root/zig-out/interop}
port=${INTEROP_PORT:-2590}
plc_port=$port
zds_port=$((port + 1))
ref_port=$((port + 2))
relay_port=$((port + 3))
plc="http://127.0.0.1:$plc_port"
work=$(mktemp -d "${TMPDIR:-/tmp}/zds-interop.XXXXXX")
pids=
failed=0
passed=0

has() { case " $lanes " in *" $1 "*) return 0 ;; esac; return 1; }
say() { printf '\n── %s\n' "$*"; }
ok() { passed=$((passed + 1)); echo "pass: $1"; }
bad() { failed=$((failed + 1)); echo "FAIL: $1" >&2; }
check() {
  check_name=$1
  shift
  if "$@"; then ok "$check_name"; else bad "$check_name"; fi
}
bg() {
  log=$1
  shift
  "$@" >"$log" 2>&1 &
  pids="$pids $!"
}
cleanup() {
  status=$?
  for pid in $pids; do kill "$pid" 2>/dev/null || true; done
  wait 2>/dev/null || true
  if [ "$status" -eq 0 ] && [ -z "${INTEROP_KEEP:-}" ]; then
    rm -rf "$work"
  else
    echo "interop: logs and transcripts kept in $work" >&2
  fi
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM

wait_http() {
  i=0
  until curl -fsS -o /dev/null "$1" 2>/dev/null; do
    i=$((i + 1))
    if [ "$i" -gt 300 ]; then
      echo "interop: $1 did not come up" >&2
      return 1
    fi
    sleep 0.1
  done
}

say "build"
mkdir -p "$bin"
(
  cd "$here/check"
  go build -o "$bin/interop-check" .
  go build -o "$bin/interop-relay" ./relay
  go build -o "$bin/goat" github.com/bluesky-social/goat
)
zds_bin=${INTEROP_ZDS_BIN:-}
if [ -z "$zds_bin" ] && { has zds || [ "$relay_pds" = zds ]; }; then
  (cd "$root" && zig build smoke-build)
  zds_bin="$root/zig-out/bin/zds"
fi
if has reference || has diff || [ "$relay_pds" = reference ]; then
  # quiet when it works, the whole log when it does not
  if ! (cd "$here/refpds" && pnpm install --frozen-lockfile >"$work/pnpm-install.log" 2>&1); then
    cat "$work/pnpm-install.log" >&2
    exit 1
  fi
fi

say "start"
bg "$work/plc.log" "$bin/interop-check" plc -listen "127.0.0.1:$plc_port"
wait_http "$plc/_health"

if has zds || has diff || [ "$relay_pds" = zds ]; then
  mkdir -p "$work/zds/blobs"
  # localhost, not 127.0.0.1: the relay checks that an account's DID
  # document names the host it is crawling
  bg "$work/zds/server.log" env \
    ZDS_PLC_ROTATION_KEY=1111111111111111111111111111111111111111111111111111111111111111 \
    ZDS_MAIL_PROVIDER=outbox ZDS_ALLOW_PRIVATE_FETCH=true \
    "$zds_bin" --host 127.0.0.1 --port "$zds_port" --jwt-secret interop-jwt \
    --db "$work/zds/zds.sqlite3" --blobstore-path "$work/zds/blobs" \
    --public-url "http://localhost:$zds_port" --server-did did:web:localhost \
    --handle-domains .test --admin-token interop-admin --plc-directory "$plc" --crawlers ""
  wait_http "http://127.0.0.1:$zds_port/xrpc/_health"
fi
if has reference || has diff || [ "$relay_pds" = reference ]; then
  mkdir -p "$work/reference/blobs"
  # the reference refuses a PLC URL with an IP address in it
  bg "$work/reference/server.log" env \
    PDS_HOSTNAME=localhost PDS_PORT="$ref_port" PDS_DEV_MODE=true \
    PDS_DATA_DIRECTORY="$work/reference" PDS_BLOBSTORE_DISK_LOCATION="$work/reference/blobs" \
    PDS_JWT_SECRET=interop-jwt PDS_ADMIN_PASSWORD=interop-admin \
    PDS_PLC_ROTATION_KEY_K256_PRIVATE_KEY_HEX=2222222222222222222222222222222222222222222222222222222222222222 \
    PDS_DID_PLC_URL="http://localhost:$plc_port" PDS_SERVICE_HANDLE_DOMAINS=.test \
    PDS_INVITE_REQUIRED=false LOG_ENABLED=false \
    node "$here/refpds/index.mjs"
  wait_http "http://127.0.0.1:$ref_port/xrpc/_health"
fi

if has relay; then
  case $relay_pds in
    zds) relay_target=$zds_port ;;
    reference) relay_target=$ref_port ;;
    *) echo "interop: INTEROP_RELAY_PDS must be zds or reference" >&2; exit 2 ;;
  esac
  mkdir -p "$work/relay"
  if [ -n "${INTEROP_RELAY_CMD:-}" ]; then
    INTEROP_PLC_URL=$plc INTEROP_PDS_HOST="localhost:$relay_target" INTEROP_RELAY_PORT=$relay_port
    INTEROP_RELAY_DIR="$work/relay"
    export INTEROP_PLC_URL INTEROP_PDS_HOST INTEROP_RELAY_PORT INTEROP_RELAY_DIR
    bg "$work/relay/relay.log" sh -c "$INTEROP_RELAY_CMD"
  else
    bg "$work/relay/relay.log" "$bin/interop-relay" -listen "127.0.0.1:$relay_port" -plc "$plc" \
      -host "localhost:$relay_target" -data "$work/relay" -warnings "$work/relay/warnings.jsonl"
  fi
  wait_http "http://127.0.0.1:$relay_port/_health"
fi

# lane NAME PORT: drive the scenario through one PDS while consumers watch
lane() {
  name=$1
  p=$2
  dir="$work/$name"
  say "$name: scenario with live consumers"
  bg "$dir/firehose.log" "$bin/interop-check" firehose -host "ws://127.0.0.1:$p" -plc "$plc" \
    -wait-for "$dir/done" -out "$dir/live.json" -events "$dir/live.jsonl"
  live=$!
  bg "$dir/goat.log" env ATP_PLC_HOST="$plc" GOAT_LOG_LEVEL=warn \
    "$bin/goat" firehose --relay-host "ws://127.0.0.1:$p" --verify-basic --verify-mst -q
  relay_live=
  if has relay && [ "$name" = "$relay_pds" ]; then
    bg "$work/relay/firehose.log" "$bin/interop-check" firehose -host "ws://127.0.0.1:$relay_port" \
      -plc "$plc" -wait-for "$dir/done" -idle 5s -out "$work/relay/live.json" -events "$work/relay/live.jsonl"
    relay_live=$!
  fi
  sleep 0.5
  check "$name scenario" python3 "$here/scenario.py" --pds "http://localhost:$p" --out "$dir/transcript.json"
  touch "$dir/done"
  check "$name firehose (live)" wait "$live"
  check "$name firehose (replay from 0)" "$bin/interop-check" firehose -host "ws://127.0.0.1:$p" -plc "$plc" \
    -cursor 0 -idle 1s -out "$dir/replay.json" -events "$dir/replay.jsonl"
  check "$name live and replay agree" "$bin/interop-check" compare "$dir/live.json" "$dir/replay.json"
  check "$name repos" "$bin/interop-check" repo -pds "http://127.0.0.1:$p" -plc "$plc" -summary "$dir/live.json"
  # goat only warns; the genesis commit has no prevData by design
  if grep '"level":"WARN"\|"level":"ERROR"' "$dir/goat.log" | grep -v 'prevData is nil' >"$dir/goat-warnings.log"; then
    bad "$name goat firehose verify"
    cut -c1-400 "$dir/goat-warnings.log" >&2
  else
    ok "$name goat firehose verify"
  fi
  if [ -n "$relay_live" ]; then
    check "relay re-served $name's firehose" wait "$relay_live"
    check "relay matches $name" "$bin/interop-check" compare "$dir/live.json" "$work/relay/live.json"
    if [ -s "$work/relay/warnings.jsonl" ]; then
      # the reference PDS also emits a #sync at the rev it just committed
      # on createAccount and activateAccount; relays drop it as stale
      if grep -v '"msg":"dropping sync event with old rev"' "$work/relay/warnings.jsonl" >"$work/relay/unexpected.jsonl"; then
        bad "relay logged no warnings"
        cut -c1-400 "$work/relay/unexpected.jsonl" >&2
      else
        ok "relay logged no warnings"
      fi
    else
      ok "relay logged no warnings"
    fi
  fi
}

if has zds || has diff; then lane zds "$zds_port"; fi
if has reference || has diff; then lane reference "$ref_port"; fi
if has diff; then
  say "diff zds against the reference"
  check "responses match the reference" python3 "$here/diff.py" --known "$here/known-differences.json" \
    "$work/zds/transcript.json" "$work/reference/transcript.json"
fi

say "summary"
echo "$passed passed, $failed failed"
if [ "$failed" -ne 0 ]; then
  echo "the failed checks are marked FAIL above" >&2
  exit 1
fi