From 5830e61a1da08185bc31466e7cbeec1e0f95220a Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Tue, 10 Nov 2026 09:37:13 -0500 Subject: [PATCH 1/2] test(relay): pin an event that expires while earlier records are read --- crates/nostream-relay/tests/connection.rs | 178 ++++++++++++++++++++++ 1 file changed, 178 insertions(+) diff --git a/crates/nostream-relay/tests/connection.rs b/crates/nostream-relay/tests/connection.rs index ad0893f6..d238a81b 100644 --- a/crates/nostream-relay/tests/connection.rs +++ b/crates/nostream-relay/tests/connection.rs @@ -611,6 +611,184 @@ async fn a_restricted_read_is_judged_against_the_authenticated_set_at_each_event assert_eq!(authenticated[2], r#"["EOSE","restricted"]"#); } +// --------------------------------------------------------------------------------------- +// Expiry on the stored read (NIP-40) +// --------------------------------------------------------------------------------------- + +/// How far the clock moves between the two records: past the second one's expiration +/// (`NOW_SECS + 5`) and not onto the inclusive instant, which the boundary test pins. +const EXPIRY_ADVANCE_MS: u64 = 10_000; + +/// The gate releases one record at a time, so the clock moves after the first record has been +/// judged and before the second one is — the same instrument the mid-read `AUTH` test uses. +#[tokio::test] +async fn an_event_that_expires_while_earlier_records_are_still_being_read_is_not_delivered() { + let fixture = Fixture::new(settings()); + let gate = fixture.events.gate_reads_per_item(); + + // The first record's expiration is an hour ahead, so it is unexpired at both instants this + // test reaches: a read of milliseconds where seconds belong would withhold it too. + let first_expiration = (NOW_SECS + 3_600).to_string(); + let second_expiration = (NOW_SECS + 5).to_string(); + let first = signed_event( + &CLIENT_SECRET, + 1, + vec![vec!["expiration", first_expiration.as_str()]], + "first", + NOW_SECS, + ); + let second = signed_event( + &CLIENT_SECRET, + 1, + vec![vec!["expiration", second_expiration.as_str()]], + "second", + NOW_SECS, + ); + // Seeding takes the event by value, so each frame is attributed by the id read here. + let first_id = first.id.to_string(); + let second_id = second.id.to_string(); + + fixture.seed_event(first).await; + fixture.seed_event(second).await; + + let (sink, mut frames) = FrameSink::channel(1024 * 1024, None); + let connection = connection(); + let clock = Arc::new(TestClock::new(NOW_MS)); + let context = + Arc::new(fixture.context(clock.clone(), relay_keys(), Arc::clone(&connection), sink)); + + let reading = tokio::spawn({ + let context = Arc::clone(&context); + + async move { + let continuation = nostream_relay::handlers::subscribe::begin( + SubscribeMessage { + subscription_id: "expiry".to_owned(), + filters: vec![Filter::default()], + }, + &context, + ) + .expect("the REQ registers its subscription"); + + continuation.await; + } + }); + + for _ in 0..100 { + if connection.read_in_flight("expiry") { + break; + } + + tokio::task::yield_now().await; + } + + assert!(connection.read_in_flight("expiry"), "the read is in flight"); + + // The first record passes while the clock is still at `NOW_SECS`, below both expirations. + gate.add_permits(1); + + let mut collected = Vec::new(); + for _ in 0..100 { + if let Ok(frame) = frames.try_recv() { + collected.push(frame); + break; + } + + tokio::task::yield_now().await; + } + + assert_eq!( + collected.len(), + 1, + "the first record arrived: {collected:?}" + ); + assert!( + collected[0].starts_with(r#"["EVENT","expiry","#) && collected[0].contains(&first_id), + "the first record's frame: {}", + collected[0] + ); + + // The clock moves past the second record's expiration before the only permit that lets that + // record reach the expiry check, so the check runs after the move by construction. + clock.advance(EXPIRY_ADVANCE_MS); + gate.add_permits(1); + reading.await.expect("the read finishes"); + + while let Ok(frame) = frames.try_recv() { + collected.push(frame); + } + + assert_eq!(collected.len(), 2, "{collected:?}"); + assert!( + collected[0].starts_with(r#"["EVENT","expiry","#) && collected[0].contains(&first_id), + "the first record's frame: {}", + collected[0] + ); + assert_eq!(collected[1], r#"["EOSE","expiry"]"#); + assert!( + !collected.iter().any(|frame| frame.contains(&second_id)), + "the record that expired during the read was delivered: {collected:?}" + ); +} + +#[tokio::test] +async fn an_event_expired_when_the_read_starts_is_not_delivered() { + // No gate: the clock never moves here, so the subject is the comparison, not the interleave. + // An expiration exactly at the instant the read starts is expired (`expiration <= now`, + // `expiration.rs:52`), which a per-record read comparing strictly would deliver. + let fixture = Fixture::new(settings()); + let expired_at = NOW_SECS.to_string(); + let kept = signed_event(&CLIENT_SECRET, 1, vec![], "kept", NOW_SECS); + let expired = signed_event( + &CLIENT_SECRET, + 1, + vec![vec!["expiration", expired_at.as_str()]], + "expired", + NOW_SECS, + ); + let kept_id = kept.id.to_string(); + let expired_id = expired.id.to_string(); + + fixture.seed_event(kept).await; + fixture.seed_event(expired).await; + + let (sink, mut frames) = FrameSink::channel(1024 * 1024, None); + let connection = connection(); + let context = Arc::new(fixture.context( + Arc::new(TestClock::new(NOW_MS)), + relay_keys(), + Arc::clone(&connection), + sink, + )); + + nostream_relay::handlers::subscribe::begin( + SubscribeMessage { + subscription_id: "boundary".to_owned(), + filters: vec![Filter::default()], + }, + &context, + ) + .expect("the REQ registers its subscription") + .await; + + let mut collected = Vec::new(); + while let Ok(frame) = frames.try_recv() { + collected.push(frame); + } + + assert_eq!(collected.len(), 2, "{collected:?}"); + assert!( + collected[0].starts_with(r#"["EVENT","boundary","#) && collected[0].contains(&kept_id), + "the unexpired record's frame: {}", + collected[0] + ); + assert_eq!(collected[1], r#"["EOSE","boundary"]"#); + assert!( + !collected.iter().any(|frame| frame.contains(&expired_id)), + "the record expired at the read's start was delivered: {collected:?}" + ); +} + // --------------------------------------------------------------------------------------- // Handler dispatch order // --------------------------------------------------------------------------------------- From 3245656cc25ddd867e84211c0060fd311bfd0d7c Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Tue, 10 Nov 2026 09:37:13 -0500 Subject: [PATCH 2/2] fix(relay): read the clock at the expiry check, not before the stored read --- .changeset/expiry-read-clock.md | 10 ++++++++++ crates/nostream-relay/src/handlers/subscribe.rs | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 .changeset/expiry-read-clock.md diff --git a/.changeset/expiry-read-clock.md b/.changeset/expiry-read-clock.md new file mode 100644 index 00000000..b1e64afd --- /dev/null +++ b/.changeset/expiry-read-clock.md @@ -0,0 +1,10 @@ +--- +--- + +fix(relay): ask the clock at the expiry check, not before the stored read (#953) + +`fetch_and_send` captured `now` once, between `find_by_filters` and the loop that streams +the stored records, and judged every record against that one instant, so a record that +expired while earlier records were still being read was delivered anyway. The expiry check +now reads `context.now_secs()` as it runs, one record at a time, which is what the capture's +`isTagUnexpired` does inside `isExpiredEvent`. No other behaviour changes. diff --git a/crates/nostream-relay/src/handlers/subscribe.rs b/crates/nostream-relay/src/handlers/subscribe.rs index c09f8a29..fb8f27ac 100644 --- a/crates/nostream-relay/src/handlers/subscribe.rs +++ b/crates/nostream-relay/src/handlers/subscribe.rs @@ -303,7 +303,6 @@ async fn fetch_and_send(mut read: InFlightRead, filters: &[Filter], context: &Ha futures_util::pin_mut!(stream); - let now = context.now_secs(); let matches_any = |event: &nostream_core::Event| filters.iter().any(|filter| filter.matches(event)); @@ -378,7 +377,9 @@ async fn fetch_and_send(mut read: InFlightRead, filters: &[Filter], context: &Ha } let event = record.event; - if expiration::is_expired_event(&event, now) { + // The clock is asked when the record is judged, not once before the loop, so a + // record that expires while earlier ones are still being read is not delivered. + if expiration::is_expired_event(&event, context.now_secs()) { continue; }