From f6054f3c37c0c669dca7572d423998c59e8306a5 Mon Sep 17 00:00:00 2001 From: mleku Date: Tue, 30 Sep 2025 18:39:53 +0100 Subject: [PATCH] Add `run-relay-and-seed.sh` script, remove redundant JS library mappings, and improve logging consistency. - Introduced `scripts/run-relay-and-seed.sh` to simplify relay testing and Market seeding. - Removed `.idea/jsLibraryMappings.xml` as it is no longer required. - Enhanced consistency by reintroducing relevant debug logs and removing redundant comments. --- .idea/jsLibraryMappings.xml | 7 --- app/handle-message.go | 15 ++--- app/handle-req.go | 97 +++++++++++++++---------------- app/handle-websocket.go | 15 +---- scripts/run-relay-and-seed.sh | 104 ++++++++++++++++++++++++++++++++++ 5 files changed, 163 insertions(+), 75 deletions(-) delete mode 100644 .idea/jsLibraryMappings.xml create mode 100755 scripts/run-relay-and-seed.sh diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml deleted file mode 100644 index b4b1e9d..0000000 --- a/.idea/jsLibraryMappings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/app/handle-message.go b/app/handle-message.go index aecfec9..52d8043 100644 --- a/app/handle-message.go +++ b/app/handle-message.go @@ -4,6 +4,7 @@ import ( "fmt" "lol.mleku.dev/chk" + "lol.mleku.dev/log" "next.orly.dev/pkg/encoders/envelopes" "next.orly.dev/pkg/encoders/envelopes/authenvelope" "next.orly.dev/pkg/encoders/envelopes/closeenvelope" @@ -36,13 +37,13 @@ func (l *Listener) HandleMessage(msg []byte, remote string) { } } if err != nil { - // log.D.C( - // func() string { - // return fmt.Sprintf( - // "notice->%s %s", remote, err, - // ) - // }, - // ) + log.D.C( + func() string { + return fmt.Sprintf( + "notice->%s %s", remote, err, + ) + }, + ) if err = noticeenvelope.NewFrom(err.Error()).Write(l); err != nil { return } diff --git a/app/handle-req.go b/app/handle-req.go index d605b15..52a6ee9 100644 --- a/app/handle-req.go +++ b/app/handle-req.go @@ -129,15 +129,16 @@ privCheck: continue } - if kind.IsPrivileged(ev.Kind) && - accessLevel != "admin" { // admins can see all events - // log.T.C( - // func() string { - // return fmt.Sprintf( - // "checking privileged event %0x", ev.ID, - // ) - // }, - // ) + if l.Config.ACLMode != "none" && + (kind.IsPrivileged(ev.Kind) && accessLevel != "admin") && + l.authedPubkey.Load() != nil { // admins can see all events + log.T.C( + func() string { + return fmt.Sprintf( + "checking privileged event %0x", ev.ID, + ) + }, + ) pk := l.authedPubkey.Load() if pk == nil { continue @@ -161,26 +162,26 @@ privCheck: continue } if utils.FastEqual(pt, pk) { - // log.T.C( - // func() string { - // return fmt.Sprintf( - // "privileged event %s is for logged in pubkey %0x", - // ev.ID, pk, - // ) - // }, - // ) + log.T.C( + func() string { + return fmt.Sprintf( + "privileged event %s is for logged in pubkey %0x", + ev.ID, pk, + ) + }, + ) tmp = append(tmp, ev) continue privCheck } } - // log.T.C( - // func() string { - // return fmt.Sprintf( - // "privileged event %s does not contain the logged in pubkey %0x", - // ev.ID, pk, - // ) - // }, - // ) + log.T.C( + func() string { + return fmt.Sprintf( + "privileged event %s does not contain the logged in pubkey %0x", + ev.ID, pk, + ) + }, + ) } else { tmp = append(tmp, ev) } @@ -188,19 +189,19 @@ privCheck: events = tmp seen := make(map[string]struct{}) for _, ev := range events { - // log.D.C( - // func() string { - // return fmt.Sprintf( - // "REQ %s: sending EVENT id=%s kind=%d", env.Subscription, - // hex.Enc(ev.ID), ev.Kind, - // ) - // }, - // ) - // log.T.C( - // func() string { - // return fmt.Sprintf("event:\n%s\n", ev.Serialize()) - // }, - // ) + log.D.C( + func() string { + return fmt.Sprintf( + "REQ %s: sending EVENT id=%s kind=%d", env.Subscription, + hex.Enc(ev.ID), ev.Kind, + ) + }, + ) + log.T.C( + func() string { + return fmt.Sprintf("event:\n%s\n", ev.Serialize()) + }, + ) var res *eventenvelope.Result if res, err = eventenvelope.NewResultWith( env.Subscription, ev, @@ -215,7 +216,7 @@ privCheck: } // write the EOSE to signal to the client that all events found have been // sent. - // log.T.F("sending EOSE to %s", l.remote) + log.T.F("sending EOSE to %s", l.remote) if err = eoseenvelope.NewFrom(env.Subscription). Write(l); chk.E(err) { return @@ -223,10 +224,10 @@ privCheck: // if the query was for just Ids, we know there can't be any more results, // so cancel the subscription. cancel := true - // log.T.F( - // "REQ %s: computing cancel/subscription; events_sent=%d", - // env.Subscription, len(events), - // ) + log.T.F( + "REQ %s: computing cancel/subscription; events_sent=%d", + env.Subscription, len(events), + ) var subbedFilters filter.S for _, f := range *env.Filters { if f.Ids.Len() < 1 { @@ -241,10 +242,10 @@ privCheck: } notFounds = append(notFounds, id) } - // log.T.F( - // "REQ %s: ids outstanding=%d of %d", env.Subscription, - // len(notFounds), f.Ids.Len(), - // ) + log.T.F( + "REQ %s: ids outstanding=%d of %d", env.Subscription, + len(notFounds), f.Ids.Len(), + ) // if all were found, don't add to subbedFilters if len(notFounds) == 0 { continue @@ -281,6 +282,6 @@ privCheck: return } } - // log.T.F("HandleReq: COMPLETED processing from %s", l.remote) + log.T.F("HandleReq: COMPLETED processing from %s", l.remote) return } diff --git a/app/handle-websocket.go b/app/handle-websocket.go index f0a092e..e6cc2b0 100644 --- a/app/handle-websocket.go +++ b/app/handle-websocket.go @@ -19,7 +19,6 @@ const ( DefaultWriteWait = 10 * time.Second DefaultPongWait = 60 * time.Second DefaultPingWait = DefaultPongWait / 2 - DefaultReadTimeout = 7 * time.Second // Read timeout to detect stalled connections DefaultWriteTimeout = 3 * time.Second DefaultMaxMessageSize = 1 * units.Mb @@ -99,10 +98,8 @@ whitelist: var msg []byte // log.T.F("waiting for message from %s", remote) - // Create a read context with timeout to prevent indefinite blocking - readCtx, readCancel := context.WithTimeout(ctx, DefaultReadTimeout) - typ, msg, err = conn.Read(readCtx) - readCancel() + // Block waiting for message; rely on pings and context cancellation to detect dead peers + typ, msg, err = conn.Read(ctx) if err != nil { if strings.Contains( @@ -110,14 +107,6 @@ whitelist: ) { return } - // Handle timeout errors - occurs when client becomes unresponsive - if strings.Contains(err.Error(), "context deadline exceeded") { - log.T.F( - "connection from %s timed out after %v", remote, - DefaultReadTimeout, - ) - return - } // Handle EOF errors gracefully - these occur when client closes connection // or sends incomplete/malformed WebSocket frames if strings.Contains(err.Error(), "EOF") || diff --git a/scripts/run-relay-and-seed.sh b/scripts/run-relay-and-seed.sh new file mode 100755 index 0000000..72550d4 --- /dev/null +++ b/scripts/run-relay-and-seed.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env bash +set -euo pipefail + +# run-relay-and-seed.sh +# Starts the ORLY relay with specified settings, then runs `bun dev:seed` in a +# provided Market repository to observe how the app interacts with the relay. +# +# Usage: +# scripts/run-relay-and-seed.sh /path/to/market +# MARKET_DIR=/path/to/market scripts/run-relay-and-seed.sh +# +# Notes: +# - This script removes /tmp/plebeian before starting the relay. +# - The relay listens on 0.0.0.0:3334 +# - ORLY_ADMINS is intentionally empty and ACL is set to 'none'. +# - Requires: go, bun, curl + +# ---------- Config ---------- +RELAY_HOST="127.0.0.1" +RELAY_PORT="10547" +RELAY_DATA_DIR="/tmp/plebeian" +LOG_PREFIX="[relay]" +WAIT_TIMEOUT="45" # seconds + +# ---------- Resolve repo root ---------- +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)" +cd "${REPO_ROOT}" + +# ---------- Resolve Market directory ---------- +MARKET_DIR="${1:-${MARKET_DIR:-}}" +if [[ -z "${MARKET_DIR}" ]]; then + echo "ERROR: Market repository directory not provided. Set MARKET_DIR env or pass as first arg." >&2 + echo "Example: MARKET_DIR=$HOME/src/market scripts/run-relay-and-seed.sh" >&2 + exit 1 +fi +if [[ ! -d "${MARKET_DIR}" ]]; then + echo "ERROR: MARKET_DIR does not exist: ${MARKET_DIR}" >&2 + exit 1 +fi + +# ---------- Prerequisites ---------- +command -v go >/dev/null 2>&1 || { echo "ERROR: 'go' not found in PATH" >&2; exit 1; } +command -v bun >/dev/null 2>&1 || { echo "ERROR: 'bun' not found in PATH. Install Bun: https://bun.sh" >&2; exit 1; } +command -v curl >/dev/null 2>&1 || { echo "ERROR: 'curl' not found in PATH" >&2; exit 1; } + +# ---------- Cleanup handler ---------- +RELAY_PID="" +cleanup() { + set +e + if [[ -n "${RELAY_PID}" ]]; then + echo "${LOG_PREFIX} stopping relay (pid=${RELAY_PID})" >&2 + kill "${RELAY_PID}" 2>/dev/null || true + wait "${RELAY_PID}" 2>/dev/null || true + fi +} +trap cleanup EXIT INT TERM + +# ---------- Start relay ---------- +reset || true +rm -rf "${RELAY_DATA_DIR}" + +# Run go relay in background with required environment variables +( + export ORLY_LOG_LEVEL="trace" + export ORLY_LISTEN="0.0.0.0" + export ORLY_PORT="${RELAY_PORT}" + export ORLY_ADMINS="" + export ORLY_ACL_MODE="none" + export ORLY_DATA_DIR="${RELAY_DATA_DIR}" + # Important: run from repo root + cd "${REPO_ROOT}" + # Prefix relay logs so they are distinguishable + stdbuf -oL -eL go run . 2>&1 | sed -u "s/^/${LOG_PREFIX} /" +) & +RELAY_PID=$! +echo "${LOG_PREFIX} started (pid=${RELAY_PID}), waiting for readiness on ${RELAY_HOST}:${RELAY_PORT} …" + +# ---------- Wait for readiness ---------- +start_ts=$(date +%s) +while true; do + if curl -fsS "http://${RELAY_HOST}:${RELAY_PORT}/" >/dev/null 2>&1; then + break + fi + now=$(date +%s) + if (( now - start_ts > WAIT_TIMEOUT )); then + echo "ERROR: relay did not become ready within ${WAIT_TIMEOUT}s" >&2 + exit 1 + fi + sleep 1 +done +echo "${LOG_PREFIX} ready. Running Market seeding…" + +# ---------- Run market seeding ---------- +( + cd "${MARKET_DIR}" + # Stream bun output with clear prefix + stdbuf -oL -eL bun dev:seed 2>&1 | sed -u 's/^/[market] /' +) + +# After seeding completes, keep the relay up briefly for inspection +echo "${LOG_PREFIX} seeding finished. Relay is still running for inspection. Press Ctrl+C to stop." +# Wait indefinitely until interrupted, to allow observing relay logs/behavior +while true; do sleep 3600; done