From 30df44197afab20d0ac84a8ab52f967f54288641 Mon Sep 17 00:00:00 2001 From: npub13n66s06epmqf2kc3v373ez8hj65cuzyvxzjf93vwpervxqn2u7jq2qd9je <8cf5a83f590ec0955b11647d1c88f796a98e088c30a492c58e0e46c3026ae7a4@buzz.block.builderlab.xyz> Date: Sat, 1 Aug 2026 07:47:12 -0400 Subject: [PATCH] spike(relay): persist tracer events in SQLite Signed-off-by: npub13n66s06epmqf2kc3v373ez8hj65cuzyvxzjf93vwpervxqn2u7jq2qd9je <8cf5a83f590ec0955b11647d1c88f796a98e088c30a492c58e0e46c3026ae7a4@buzz.block.builderlab.xyz> --- Cargo.lock | 1 + Cargo.toml | 2 +- PHASE0_BREAKAGE_CATALOGUE.md | 16 ++++-- crates/buzz-relay/src/local_mode.rs | 56 +++++++++++++++---- .../src/bin/local_mode_smoke.rs | 14 +++++ 5 files changed, 73 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index da86c89b85..5962ec44e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4532,6 +4532,7 @@ version = "0.30.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" dependencies = [ + "cc", "pkg-config", "vcpkg", ] diff --git a/Cargo.toml b/Cargo.toml index cc1dd0f9df..9879a1666c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,7 @@ tower-http = { version = "0.6", features = ["trace", "cors", "compression-gzip" # Database sqlx = { version = "0.9", features = [ - "runtime-tokio", "tls-rustls", "postgres", "uuid", "chrono", "json" + "runtime-tokio", "tls-rustls", "postgres", "sqlite", "uuid", "chrono", "json" ] } # Redis diff --git a/PHASE0_BREAKAGE_CATALOGUE.md b/PHASE0_BREAKAGE_CATALOGUE.md index 5b1ae235c8..47cbe69b2f 100644 --- a/PHASE0_BREAKAGE_CATALOGUE.md +++ b/PHASE0_BREAKAGE_CATALOGUE.md @@ -4,7 +4,7 @@ Base: `origin/main` c104eecfb38620de2c35c7e20a716f8658b5a6b1. Spike branch: `spi ## Result -`BUZZ_LOCAL_MODE=1 BUZZ_BIND_ADDR=127.0.0.1:4317 BUZZ_RELAY_URL=ws://127.0.0.1:4317 target/debug/buzz-relay` boots without Postgres, Redis, or S3. The real `buzz-test-client` proves NIP-42, event insertion, historical REQ filtering, and two-client live fan-out. This is a deliberately separate tracer-bullet path, not the production `AppState` and not mergeable architecture. +`BUZZ_LOCAL_MODE=1 BUZZ_BIND_ADDR=127.0.0.1:4317 BUZZ_RELAY_URL=ws://127.0.0.1:4317 BUZZ_LOCAL_DB=sqlite:///tmp/buzz-phase0.sqlite target/debug/buzz-relay` boots without Postgres, Redis, or S3. The real `buzz-test-client` proves NIP-42, SQLite event insertion, historical REQ filtering (including across relay restart), and two-client live fan-out. This is a deliberately separate tracer-bullet path, not the production `AppState` and not mergeable architecture. ## Worked as-is @@ -19,7 +19,7 @@ Base: `origin/main` c104eecfb38620de2c35c7e20a716f8658b5a6b1. Spike branch: `spi | Module | Evidence | Fidelity / breakage | |---|---|---| -| Event store + REQ | `local_mode.rs:50-55,113-140` | `RwLock>`; correct basic filters/dedupe, volatile, O(n), no replacement/deletion/tenant/channel visibility semantics. This is in-memory rather than SQLite: it proves the storage boundary, not SQL portability. | +| Event store + REQ | `local_mode.rs:56-80,130-163,199-207` | SQLite via sqlx; a two-column `events(id,event_json)` table gives durable insert/dedupe and restart history. REQ deserializes all rows then applies `Filter::match_event` in Rust: O(n), no replacement/deletion/tenant/channel visibility semantics. | | Pubsub | `local_mode.rs:53,58-63,93-94,126,146-154` | `tokio::broadcast`; fan-out works in one process. Redis topic scoping, reconnect, cross-pod invalidation and connection control absent. Existing Redis-only shape is explicit at `crates/buzz-pubsub/src/lib.rs:99-139`. | | NIP-42 | `local_mode.rs:79-111,159-170` | Crypto challenge/relay/signature verification works. Moderation, allowlist, relay membership, and NIP-OA backfill are bypassed; production coupling starts at `handlers/auth.rs:94-260`. | | Health | `local_mode.rs:64-70` | Static OK; does not inspect backend health. | @@ -40,10 +40,14 @@ Base: `origin/main` c104eecfb38620de2c35c7e20a716f8658b5a6b1. Spike branch: `spi The production router cannot be retained while swapping only the core store. `AppState` is a concrete service aggregate, not a seam: concrete `Db`, Redis pool/manager, `SearchService`, and `MediaStorage` are mandatory fields (`state.rs:488-502,554-584`) and constructor parameters (`state.rs:637-648`). Constructor body also creates S3-backed git storage plus Redis replay/rate-limit services (`state.rs:692-713`). Startup eagerly connects Postgres (`main.rs:172-192`), Redis (`main.rs:369-399`), search Postgres (`main.rs:403-419`), and S3 (`main.rs:448-454`) before router construction. -Therefore a SQLite port of `event.rs` alone cannot boot the existing handler/router path. Phase 1 must first separate a local profile/router service aggregate or add backend traits/configurable optional services. The tracer bullet confirms protocol/client compatibility but **does not yet meet the desktop+agent/channel success criterion**. +Therefore a SQLite port of `event.rs` alone cannot boot the existing handler/router path. Phase 1 must first separate a local profile/router service aggregate or add backend traits/configurable optional services. **SECURITY WARNING — OPEN RELAY:** the tracer accepts any cryptographically valid NIP-42 identity and allows it to publish/read every stored event. This is spike-only behavior and must not survive into Phase 1 without an explicit unsafe flag. + +The tracer bullet confirms protocol/client compatibility and minimal SQLite durability but **does not yet meet the desktop+agent/channel success criterion**. ## Phase 2 estimate refinement +SQLite datum: enabling sqlx SQLite compiled without source changes outside the tracer. Basic durable event JSON uses portable table creation plus SQLite `INSERT OR IGNORE`; the production `event.rs` cannot be mechanically reused because its 39 queries assume the production schema, typed columns, community/channel fences, replacement rules, deletion, and PG-specific query composition. This cheap datum shows driver/toolchain viability, not a completed `event.rs` port. + Core SQL count is not the main blocker. `event.rs` has 39 `sqlx::query` call sites; unavoidable auth/chat widening immediately adds approximately channel (50), relay_members (23), moderation (15), user (11), reaction (8), thread (19), DM (12), and feed (2), before shared `lib.rs` queries. A useful real-client slice is thus roughly 179 module-level query sites, not 39, plus creating the AppState/service seam. Recommended next tracer bullet: land the service aggregate/profile seam first, then port event + tenant/community + auth moderation/membership + channel as one vertical slice. ## Commands and observed output @@ -57,8 +61,12 @@ cargo test -p buzz-relay --lib # Failures require external DB/media/mesh and are present in untouched production tests. BUZZ_LOCAL_MODE=1 ... target/debug/buzz-relay -# BUZZ_LOCAL_MODE active: in-memory store and fan-out; external services bypassed +# BUZZ_LOCAL_MODE active: SQLite store and in-process fan-out; external services bypassed target/debug/local_mode_smoke ws://127.0.0.1:4317 # PASS nip42,event_insert,req_history,live_fanout + +# after relay restart against same BUZZ_LOCAL_DB +target/debug/local_mode_smoke ws://127.0.0.1:4317 history-only +# PASS sqlite_restart_history ``` diff --git a/crates/buzz-relay/src/local_mode.rs b/crates/buzz-relay/src/local_mode.rs index 1447dbaee2..c7749fb1e8 100644 --- a/crates/buzz-relay/src/local_mode.rs +++ b/crates/buzz-relay/src/local_mode.rs @@ -4,6 +4,7 @@ //! AppState so the spike can measure the coupling before a durable storage seam exists. use std::net::SocketAddr; +use std::str::FromStr; use std::sync::Arc; use anyhow::Context; @@ -15,7 +16,9 @@ use axum::Router; use futures_util::{SinkExt, StreamExt}; use nostr::filter::MatchEventOptions; use nostr::{Event, Filter, Kind}; -use tokio::sync::{broadcast, RwLock}; +use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; +use sqlx::SqlitePool; +use tokio::sync::broadcast; use tracing::{info, warn}; use uuid::Uuid; @@ -24,6 +27,7 @@ use buzz_relay::protocol::{ClientMessage, RelayMessage}; pub(crate) struct Config { bind_addr: SocketAddr, relay_url: String, + database_url: String, } impl Config { @@ -40,24 +44,37 @@ impl Config { .context("invalid BUZZ_BIND_ADDR")?; let relay_url = std::env::var("BUZZ_RELAY_URL").unwrap_or_else(|_| format!("ws://{bind_addr}")); + let database_url = + std::env::var("BUZZ_LOCAL_DB").unwrap_or_else(|_| "sqlite://buzz-local-mode.db".into()); Ok(Some(Self { bind_addr, relay_url, + database_url, })) } } #[derive(Clone)] struct LocalState { - events: Arc>>, + db: SqlitePool, fanout: broadcast::Sender, relay_url: Arc, } pub(crate) async fn run(config: Config) -> anyhow::Result<()> { + let options = SqliteConnectOptions::from_str(&config.database_url)?.create_if_missing(true); + let db = SqlitePoolOptions::new() + .max_connections(1) + .connect_with(options) + .await?; + sqlx::query( + "CREATE TABLE IF NOT EXISTS events (id TEXT PRIMARY KEY, event_json TEXT NOT NULL)", + ) + .execute(&db) + .await?; let (fanout, _) = broadcast::channel(1024); let state = LocalState { - events: Arc::new(RwLock::new(Vec::new())), + db, fanout, relay_url: config.relay_url.into(), }; @@ -67,7 +84,7 @@ pub(crate) async fn run(config: Config) -> anyhow::Result<()> { .route("/_readiness", get(|| async { "ok" })) .with_state(state); let listener = tokio::net::TcpListener::bind(config.bind_addr).await?; - info!(bind_addr = %config.bind_addr, "BUZZ_LOCAL_MODE active: in-memory store and fan-out; external services bypassed"); + info!(bind_addr = %config.bind_addr, "BUZZ_LOCAL_MODE active: SQLite store and in-process fan-out; external services bypassed"); axum::serve(listener, app).await?; Ok(()) } @@ -116,18 +133,25 @@ async fn serve_socket(socket: WebSocket, state: LocalState) { let _ = tx.send(Message::Text(RelayMessage::ok(&id, false, "invalid: bad signature").into())).await; continue; } - let inserted = { - let mut events = state.events.write().await; - if events.iter().any(|stored| stored.id == event.id) { false } else { - events.push(event.clone()); - true + let event_json = match serde_json::to_string(&event) { + Ok(json) => json, + Err(error) => { + let _ = tx.send(Message::Text(RelayMessage::ok(&id, false, &format!("error: {error}")).into())).await; + continue; } }; + let inserted = sqlx::query("INSERT OR IGNORE INTO events (id, event_json) VALUES (?, ?)") + .bind(&id) + .bind(event_json) + .execute(&state.db) + .await + .map(|result| result.rows_affected() == 1) + .unwrap_or(false); if inserted { let _ = state.fanout.send(event); } let _ = tx.send(Message::Text(RelayMessage::ok(&id, true, if inserted { "" } else { "duplicate" }).into())).await; } ClientMessage::Req { sub_id, filters } if authenticated => { - let events = state.events.read().await.clone(); + let events = load_events(&state.db).await; for event in events.iter().filter(|event| matches_any(&filters, event)) { if tx.send(Message::Text(RelayMessage::event(&sub_id, event).into())).await.is_err() { return; } } @@ -136,7 +160,7 @@ async fn serve_socket(socket: WebSocket, state: LocalState) { subscriptions.push((sub_id, filters)); } ClientMessage::Count { sub_id, filters } if authenticated => { - let count = state.events.read().await.iter().filter(|event| matches_any(&filters, event)).count() as u64; + let count = load_events(&state.db).await.iter().filter(|event| matches_any(&filters, event)).count() as u64; let _ = tx.send(Message::Text(RelayMessage::count(&sub_id, count).into())).await; } ClientMessage::Close(sub_id) => subscriptions.retain(|(existing, _)| existing != &sub_id), @@ -175,3 +199,13 @@ fn matches_any(filters: &[Filter], event: &Event) -> bool { .iter() .any(|filter| filter.match_event(event, MatchEventOptions::default())) } + +async fn load_events(db: &SqlitePool) -> Vec { + sqlx::query_scalar::<_, String>("SELECT event_json FROM events ORDER BY rowid") + .fetch_all(db) + .await + .unwrap_or_default() + .into_iter() + .filter_map(|json| serde_json::from_str(&json).ok()) + .collect() +} diff --git a/crates/buzz-test-client/src/bin/local_mode_smoke.rs b/crates/buzz-test-client/src/bin/local_mode_smoke.rs index 709b1dcd2d..41337f153e 100644 --- a/crates/buzz-test-client/src/bin/local_mode_smoke.rs +++ b/crates/buzz-test-client/src/bin/local_mode_smoke.rs @@ -9,6 +9,20 @@ async fn main() -> anyhow::Result<()> { .nth(1) .unwrap_or_else(|| "ws://127.0.0.1:4317".into()); let keys = Keys::parse("0000000000000000000000000000000000000000000000000000000000000001")?; + if std::env::args().nth(2).as_deref() == Some("history-only") { + let mut history = BuzzTestClient::connect(&url, &keys).await?; + history + .subscribe("restart-history", vec![Filter::new()]) + .await?; + match history.recv_event(Duration::from_secs(2)).await? { + RelayMessage::Event { event, .. } => { + anyhow::ensure!(event.content == "phase0 live") + } + other => anyhow::bail!("expected persisted EVENT, got {other:?}"), + } + println!("PASS sqlite_restart_history"); + return Ok(()); + } let mut subscriber = BuzzTestClient::connect(&url, &keys).await?; subscriber.subscribe("live", vec![Filter::new()]).await?; let _ = subscriber.recv_event(Duration::from_secs(1)).await?; // EOSE