diff --git a/.changeset/vanish-read-reads-first.md b/.changeset/vanish-read-reads-first.md new file mode 100644 index 000000000..bd15fb582 --- /dev/null +++ b/.changeset/vanish-read-reads-first.md @@ -0,0 +1,10 @@ +--- +--- + +fix(storage): answer the vanish read from a read transaction + +`LmdbBackend::is_vanished` always took the single LMDB write transaction, so every event +from a non-relay pubkey queued behind the writer for a flag that was usually already +stored. It now reads the row through the read pool and takes the write only when no row +exists, where `users::is_vanished` re-loads and hydrates as before. Rust-only, so this +changeset is empty on purpose: no published package changes. diff --git a/crates/nostream-storage/src/lmdb/mod.rs b/crates/nostream-storage/src/lmdb/mod.rs index 6ec606151..d7e96b2bb 100644 --- a/crates/nostream-storage/src/lmdb/mod.rs +++ b/crates/nostream-storage/src/lmdb/mod.rs @@ -886,6 +886,12 @@ impl UserRepository for LmdbBackend { fn is_vanished<'a>(&'a self, pubkey: &'a Pubkey) -> StorageFuture<'a, bool> { Box::pin(async move { let pubkey = *pubkey; + let stored = self + .read(move |txn, databases| users::find_by_pubkey(txn, databases, &pubkey)) + .await?; + if let Some(user) = stored { + return Ok(user.is_vanished); + } let now = encoding::micros(Utc::now()); self.write(move |txn, databases| users::is_vanished(txn, databases, &pubkey, now)) .await diff --git a/crates/nostream-storage/src/lmdb/users.rs b/crates/nostream-storage/src/lmdb/users.rs index b9eb2f6c5..e44c1c5fc 100644 --- a/crates/nostream-storage/src/lmdb/users.rs +++ b/crates/nostream-storage/src/lmdb/users.rs @@ -104,6 +104,10 @@ pub(crate) fn upsert_vanish_state( } /// The capture's `isVanished`: the stored flag, or one hydrated from a live vanish request. +/// +/// Re-loads inside the caller's write transaction before it scans or writes, so a caller whose +/// earlier read found no row cannot overwrite a flag another writer committed in between: a row +/// that appeared is returned as stored rather than written over by the scan's answer. pub(crate) fn is_vanished( txn: &mut RwTxn<'_>, databases: &Databases, @@ -156,9 +160,14 @@ pub(crate) fn revoke_admission( mod tests { #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + use std::time::Duration; + + use nostream_core::{Event, EventId, Kind, Signature}; + use tokio::time::timeout; + use super::*; use crate::lmdb::LmdbBackend; - use crate::repositories::UserRepository; + use crate::repositories::{EventRecord, UserRepository}; fn patch( pubkey: Pubkey, @@ -227,4 +236,204 @@ mod tests { assert!(user.is_admitted); assert!(user.is_vanished); } + + /// A store on a temporary directory, kept alive by the returned handle. + async fn open_store() -> (tempfile::TempDir, LmdbBackend) { + let directory = tempfile::tempdir().expect("tempdir"); + let backend = LmdbBackend::open(directory.path()) + .await + .expect("the store opens"); + (directory, backend) + } + + /// A signed event of `kind` by the key `secret`, at `created_at`. + fn signed(secret: u8, kind: Kind, created_at: i64) -> EventRecord { + let mut event = Event { + id: EventId::from_byte_array([0; 32]), + pubkey: Pubkey::from_byte_array([0; 32]), + created_at, + kind, + tags: Vec::new(), + content: format!("{secret}-{created_at}"), + sig: Signature::from_byte_array([0; 64]), + }; + nostream_core::crypto::sign_event(&[secret; 32], &mut event).expect("the event signs"); + EventRecord::from_event(&event) + } + + /// The vanish read answers from a read transaction, so it never queues behind LMDB's one + /// writer; a write still does, and completes once the writer is released. + #[tokio::test] + async fn a_held_writer_blocks_event_admission_but_not_the_vanish_read() { + let (_directory, backend) = open_store().await; + let pubkey = Pubkey::from_byte_array([3; 32]); + backend + .upsert(&patch(pubkey, FieldUpdate::Absent, FieldUpdate::Set(false))) + .await + .expect("the upsert"); + + let writer = backend.env().write_txn().expect("a write transaction"); + let vanished = timeout(Duration::from_secs(10), backend.is_vanished(&pubkey)) + .await + .expect("the vanish read answers while a writer is held") + .expect("the read"); + assert!(!vanished, "the stored flag is returned"); + + let record = signed(4, Kind(1), 1_700_000_000); + let mut create = crate::repositories::EventRepository::create(&backend, &record); + assert!( + timeout(Duration::from_millis(750), &mut create) + .await + .is_err(), + "event admission still queues behind the held writer" + ); + + drop(writer); + let created = timeout(Duration::from_secs(10), create) + .await + .expect("the create finishes once the writer is released") + .expect("the create ran"); + assert_eq!(created, 1, "the event is stored once"); + } + + /// An absent pubkey still hydrates in one write transaction, and the scan's answer is what + /// the row keeps; the second call answers from the stored row without committing. + #[tokio::test] + async fn an_absent_pubkey_hydrates_once_from_the_scan() { + let (_directory, backend) = open_store().await; + let vanish = signed(5, Kind::REQUEST_TO_VANISH, 1_700_000_100); + let note = signed(6, Kind(1), 1_700_000_200); + assert_eq!( + crate::repositories::EventRepository::create(&backend, &vanish) + .await + .expect("the create"), + 1 + ); + assert_eq!( + crate::repositories::EventRepository::create(&backend, ¬e) + .await + .expect("the create"), + 1 + ); + let vanishing = vanish.event.pubkey; + let noting = note.event.pubkey; + + let before = backend.env().info().last_txn_id; + assert!( + backend + .is_vanished(&vanishing) + .await + .expect("the vanish read"), + "a live kind-62 hydrates true" + ); + assert_eq!( + backend.env().info().last_txn_id, + before + 1, + "the hydration commits" + ); + let user = backend + .find_by_pubkey(&vanishing) + .await + .expect("the row") + .expect("a row"); + assert!(user.is_vanished); + assert!(!user.is_admitted, "a new row takes the column default"); + assert_eq!(user.balance, 0, "a new row takes the column default"); + assert_eq!(user.created_at, user.updated_at, "one instant writes both"); + + assert!( + backend + .is_vanished(&vanishing) + .await + .expect("the vanish read"), + "the stored flag answers the second call" + ); + assert_eq!( + backend.env().info().last_txn_id, + before + 1, + "the stored row is read, not rewritten" + ); + + let before_note = backend.env().info().last_txn_id; + assert!( + !backend.is_vanished(¬ing).await.expect("the vanish read"), + "a kind-1 row is not a vanish request" + ); + assert_eq!( + backend.env().info().last_txn_id, + before_note + 1, + "the hydration commits" + ); + let user = backend + .find_by_pubkey(¬ing) + .await + .expect("the row") + .expect("a row"); + assert!(!user.is_vanished); + assert!(!user.is_admitted, "a new row takes the column default"); + assert_eq!(user.balance, 0, "a new row takes the column default"); + assert_eq!(user.created_at, user.updated_at, "one instant writes both"); + + assert!( + !backend.is_vanished(¬ing).await.expect("the vanish read"), + "the stored flag answers the second call" + ); + assert_eq!( + backend.env().info().last_txn_id, + before_note + 1, + "the stored row is read, not rewritten" + ); + } + + /// The hydration re-loads before it scans or writes, so a flag committed after the + /// implementation's read found no row is kept rather than written over. + #[tokio::test] + async fn a_committed_vanish_write_between_the_read_and_the_hydration_is_kept() { + let (_directory, backend) = open_store().await; + let pubkey = Pubkey::from_byte_array([7; 32]); + + let read = backend + .read(move |txn, databases| find_by_pubkey(txn, databases, &pubkey)) + .await + .expect("the read half"); + assert!(read.is_none(), "the read half finds no row"); + + assert_eq!( + backend + .set_vanished(&pubkey, true) + .await + .expect("the commit"), + 1, + "the concurrent write stores the flag" + ); + let stored = backend + .find_by_pubkey(&pubkey) + .await + .expect("the row") + .expect("a row"); + let updated_at = stored.updated_at; + let before = backend.env().info().last_txn_id; + + let now = updated_at + chrono::TimeDelta::milliseconds(1); + let vanished = backend + .write(move |txn, databases| is_vanished(txn, databases, &pubkey, now)) + .await + .expect("the write half"); + assert!(vanished, "the committed flag is returned, not the scan's"); + + let kept = backend + .find_by_pubkey(&pubkey) + .await + .expect("the row") + .expect("a row"); + assert!(kept.is_vanished); + assert_eq!(kept.updated_at, updated_at, "the hydration wrote nothing"); + assert!(!kept.is_admitted, "the flag write kept the other columns"); + assert_eq!(kept.balance, 0, "the flag write kept the other columns"); + assert_eq!( + backend.env().info().last_txn_id, + before, + "the write half committed nothing" + ); + } }