Skip to main content

vector_core/community/
transport.rs

1//! Transport abstraction for Community events.
2//!
3//! The protocol's send/sync logic is written against this trait, not against the
4//! live Nostr client, so it can be exercised end-to-end by multiple emulated
5//! clients sharing an in-memory relay (no network, fully deterministic). Production
6//! provides an adapter over `NOSTR_CLIENT`; tests use [`MemoryRelay`].
7
8use nostr_sdk::prelude::*;
9
10/// The slice of a relay query the Community protocol needs: event kinds, the `z`
11/// pseudonym tag values, and an optional `since` floor. Production translates
12/// this into a Nostr `Filter`; the in-memory relay matches it directly.
13#[derive(Clone, Debug, Default)]
14pub struct Query {
15    pub kinds: Vec<u16>,
16    /// `z` tag values to match (OR). Empty = match any (no `z` constraint).
17    pub z_tags: Vec<String>,
18    /// `d` tag (identifier) values to match (OR). Empty = no `d` constraint. Used to
19    /// locate addressable events (e.g. a public-invite bundle by its token locator).
20    pub d_tags: Vec<String>,
21    /// `p` tag (recipient pubkey hex) values to match (OR). Empty = no `p` constraint.
22    /// Used to fetch person-addressed giftwraps (direct invites) by recipient.
23    pub p_tags: Vec<String>,
24    /// `k` tag (wrapped-kind) values to match (OR). Empty = no `k` constraint. Narrows
25    /// giftwrap fetches to the inner kind advertised on the wrap.
26    pub k_tags: Vec<String>,
27    /// Author pubkeys (hex) to match (OR). Empty = any author.
28    pub authors: Vec<String>,
29    /// Lower bound on `created_at` (seconds), inclusive.
30    pub since: Option<u64>,
31    /// Upper bound on `created_at` (seconds), inclusive — pages OLDER history (events
32    /// strictly/inclusively before a scroll cursor).
33    pub until: Option<u64>,
34    /// Max events to return (newest-first), the relay-side page cap. `None` = no limit.
35    pub limit: Option<usize>,
36}
37
38impl Query {
39    /// Does `event` satisfy this query?
40    pub fn matches(&self, event: &Event) -> bool {
41        // Compare via `Kind` (not raw u16) so this matches `to_filter`'s
42        // `Kind::Custom` normalization exactly — the live and in-memory paths must
43        // agree even at kind values that nostr maps to named variants.
44        if !self.kinds.is_empty() && !self.kinds.iter().any(|k| Kind::Custom(*k) == event.kind) {
45            return false;
46        }
47        if let Some(since) = self.since {
48            if event.created_at.as_secs() < since {
49                return false;
50            }
51        }
52        if let Some(until) = self.until {
53            if event.created_at.as_secs() > until {
54                return false;
55            }
56        }
57        if !self.authors.is_empty() && !self.authors.iter().any(|a| *a == event.pubkey.to_hex()) {
58            return false;
59        }
60        if !self.z_tags.is_empty() && !self.matches_single_letter("z", &self.z_tags, event) {
61            return false;
62        }
63        if !self.d_tags.is_empty() && !self.matches_single_letter("d", &self.d_tags, event) {
64            return false;
65        }
66        if !self.p_tags.is_empty() && !self.matches_single_letter("p", &self.p_tags, event) {
67            return false;
68        }
69        if !self.k_tags.is_empty() && !self.matches_single_letter("k", &self.k_tags, event) {
70            return false;
71        }
72        true
73    }
74
75    fn matches_single_letter(&self, name: &str, wanted: &[String], event: &Event) -> bool {
76        // ANY occurrence may satisfy the OR-set (an event can carry several `p` tags);
77        // relays match every tag instance, and matches() must agree with to_filter.
78        event.tags.iter().any(|t| {
79            let s = t.as_slice();
80            s.len() >= 2 && s[0] == name && wanted.iter().any(|w| *w == s[1])
81        })
82    }
83
84    /// Translate to a Nostr relay `Filter` for the live client.
85    pub fn to_filter(&self) -> Filter {
86        let mut filter = Filter::new();
87        if !self.kinds.is_empty() {
88            filter = filter.kinds(self.kinds.iter().map(|k| Kind::Custom(*k)));
89        }
90        if !self.z_tags.is_empty() {
91            filter = filter
92                .custom_tags(SingleLetterTag::lowercase(Alphabet::Z), self.z_tags.clone());
93        }
94        if !self.d_tags.is_empty() {
95            filter = filter.identifiers(self.d_tags.clone());
96        }
97        if !self.p_tags.is_empty() {
98            filter = filter
99                .custom_tags(SingleLetterTag::lowercase(Alphabet::P), self.p_tags.clone());
100        }
101        if !self.k_tags.is_empty() {
102            filter = filter
103                .custom_tags(SingleLetterTag::lowercase(Alphabet::K), self.k_tags.clone());
104        }
105        if !self.authors.is_empty() {
106            let authors: Vec<PublicKey> =
107                self.authors.iter().filter_map(|a| PublicKey::from_hex(a).ok()).collect();
108            if !authors.is_empty() {
109                filter = filter.authors(authors);
110            }
111        }
112        if let Some(since) = self.since {
113            filter = filter.since(Timestamp::from_secs(since));
114        }
115        if let Some(until) = self.until {
116            filter = filter.until(Timestamp::from_secs(until));
117        }
118        if let Some(limit) = self.limit {
119            filter = filter.limit(limit);
120        }
121        filter
122    }
123}
124
125/// Publish + fetch over a set of relays. Async to match the live Nostr client (the
126/// whole app is tokio-based and network I/O is async); `async-trait` boxes the
127/// futures as `Send` so impls work inside spawned tasks.
128#[async_trait::async_trait]
129pub trait Transport {
130    /// Publish `event` to every relay in `relays`. Single-attempt: Ok if ≥1 relay ACKs.
131    async fn publish(&self, event: &Event, relays: &[String]) -> Result<(), String>;
132    /// Fetch events matching `query` across `relays`, unioned and deduped by id.
133    async fn fetch(&self, query: &Query, relays: &[String]) -> Result<Vec<Event>, String>;
134
135    /// DURABLE publish for security-critical control events (rekeys, bans, the invite registry, deletes):
136    /// retry **each relay independently** until it ACKs, up to [`MAX_PUBLISH_ATTEMPTS`] times, re-sending
137    /// only the relays that have NOT yet accepted. The already-signed `event` is broadcast as-is — the
138    /// crypto (e.g. a rekey's fresh root) is minted ONCE by the caller; this only hardens the broadcast,
139    /// so a relay blip or a brief local connectivity drop can't leave a rekey/ban under-propagated.
140    /// (Required, not defaulted — a default async-trait method would force a `Sync` bound on every
141    /// generic `T: Transport` caller. The in-memory test relay implements it as a single publish.)
142    async fn publish_durable(&self, event: &Event, relays: &[String]) -> Result<(), String>;
143}
144
145/// Per-relay broadcast cap: retry each relay up to this many times before giving up on it (matching the
146/// NIP-17 deletable-DM durability). High enough to ride out a transient relay/local-network blip.
147pub const MAX_PUBLISH_ATTEMPTS: usize = 30;
148
149/// Union grace window: how long past the FIRST response the live transport keeps unioning
150/// slower relays before returning. Long enough for a healthy-but-slower relay on a
151/// high-latency link; short enough that a dead relay doesn't stall every sync.
152pub const FETCH_UNION_GRACE_MS: u64 = 3500;
153
154/// Hard ceiling on the initial confirmation: at least one relay must ACK within this window or the publish
155/// is a failure (we throw rather than spin on a dead/unreachable relay set forever). Once ONE relay accepts,
156/// the slow/ratelimited stragglers are threaded in the background (capped at MAX_PUBLISH_ATTEMPTS).
157pub const CONFIRM_WINDOW: std::time::Duration = std::time::Duration::from_secs(30);
158
159/// The retry engine behind a durable broadcast, factored out so it is unit-testable without a live
160/// client. `send_round(pending)` performs ONE broadcast attempt to the given still-pending relays and
161/// returns the subset that ACKed; this retries the rest (with `backoff` between rounds) until every relay
162/// has ACKed or `max_attempts` is reached. Returns `Ok` if at least one relay ever accepted (the event is
163/// durably out there; the fetch-union self-heals the stragglers), `Err` only if ZERO relays accepted
164/// after exhausting the retries. Dedups `relays` first so a duplicated url isn't double-counted.
165pub async fn durable_broadcast<'a, F>(
166    relays: &[String],
167    max_attempts: usize,
168    backoff: std::time::Duration,
169    mut send_round: F,
170) -> Result<(), String>
171where
172    F: FnMut(Vec<String>) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<String>> + Send + 'a>>,
173{
174    let mut pending: Vec<String> = Vec::new();
175    for r in relays {
176        if !pending.contains(r) {
177            pending.push(r.clone());
178        }
179    }
180    let total = pending.len();
181    if total == 0 {
182        return Err("no relays to broadcast to".to_string());
183    }
184    for attempt in 0..max_attempts {
185        if pending.is_empty() {
186            break;
187        }
188        let acked = send_round(pending.clone()).await;
189        pending.retain(|r| !acked.contains(r));
190        if pending.is_empty() || attempt + 1 == max_attempts {
191            break;
192        }
193        if !backoff.is_zero() {
194            tokio::time::sleep(backoff).await;
195        }
196    }
197    if pending.len() < total {
198        Ok(()) // at least one relay accepted; the rest were retried to the cap
199    } else {
200        Err(format!("no relay accepted the event after {max_attempts} attempts each"))
201    }
202}
203
204/// Sink for "straggler" events — ones a SLOWER relay returns after a racing [`LiveTransport::fetch`]
205/// has already handed the caller the first relay's batch. The integrator (src-tauri) registers a
206/// handler that feeds them back through the normal Concord ingest path (`process_incoming` for
207/// content, control/rekey re-fold for authority). The transport stays DUMB: it dedups only by event
208/// id (identical bytes) and forwards everything else. Two relays disagreeing on the latest control
209/// commit are two editions with DIFFERENT ids, so BOTH reach the ingester, where the deterministic
210/// convergence engine (version floors + same-version tiebreakers) decides the winner. The transport
211/// never resolves conflicts itself.
212pub trait CommunityIngestSink: Send + Sync + 'static {
213    fn ingest_stragglers(&self, events: Vec<Event>);
214}
215
216static INGEST_SINK: std::sync::OnceLock<Box<dyn CommunityIngestSink>> = std::sync::OnceLock::new();
217
218/// Register the straggler ingest sink. Call once during app startup (mirrors `set_event_emitter`).
219pub fn set_community_ingest_sink(sink: Box<dyn CommunityIngestSink>) {
220    let _ = INGEST_SINK.set(sink);
221}
222
223fn submit_stragglers(events: Vec<Event>) {
224    if events.is_empty() {
225        return;
226    }
227    if let Some(sink) = INGEST_SINK.get() {
228        sink.ingest_stragglers(events);
229    }
230}
231
232/// Relay urls already added + connected into the shared pool this session, so `warm_client` can
233/// skip the per-call `add_relay` bookkeeping + whole-pool `connect()` sweep once a community's
234/// relays are established (relays auto-reconnect on drop, so the re-kick is redundant once warm).
235/// Keyed by session generation: an account swap bumps the generation, invalidating every entry
236/// (the pool is rebuilt for the new account).
237static WARMED_RELAYS: std::sync::LazyLock<std::sync::Mutex<(u64, std::collections::HashSet<String>)>> =
238    std::sync::LazyLock::new(|| std::sync::Mutex::new((0, std::collections::HashSet::new())));
239
240/// Forget a relay from the warm set — call when a relay is REMOVED from the pool (e.g. pruned after
241/// leaving a community), so a later `warm_client` for another community that shares it doesn't
242/// fast-path-skip the re-add and target a relay the pool no longer holds. Poison-tolerant: the set
243/// is pure optimization state, so a poisoned lock is recovered rather than propagated.
244pub fn forget_warmed_relay(url: &str) {
245    WARMED_RELAYS.lock().unwrap_or_else(|e| e.into_inner()).1.remove(url);
246}
247
248/// Shed pooled Community relays from `candidates` that no JOINED community still needs. Used by both
249/// the leave path (relays of a community we left) and the invite-preload TTL cleanup (relays an
250/// unsolicited/declined invite warmed but never became a join, #297). Keep rules: a relay is kept if
251/// a remaining joined community lists it, OR it carries READ/WRITE (the user's own chat relays —
252/// Community relays are GOSSIP-only, so never READ/WRITE). A pruned relay re-warms automatically if
253/// its invite is later accepted (the join's subscription re-adds it), so pruning a still-pending
254/// invite's relay is safe.
255pub async fn prune_unneeded_community_relays(candidates: &[String]) {
256    if candidates.is_empty() {
257        return;
258    }
259    let Some(client) = crate::state::nostr_client() else { return };
260
261    let mut still_needed: std::collections::HashSet<String> = std::collections::HashSet::new();
262    if let Ok(ids) = crate::db::community::list_community_ids() {
263        for id in ids {
264            if let Ok(Some(c)) = crate::db::community::load_community(&id) {
265                for r in &c.relays {
266                    still_needed.insert(r.clone());
267                }
268            }
269        }
270    }
271
272    let pool = client.pool();
273    // all_relays(): community relays carry GOSSIP, so they're absent from `relays()` (READ/WRITE only).
274    let pooled = pool.all_relays().await;
275    for url in candidates {
276        if still_needed.contains(url) {
277            continue;
278        }
279        if let Ok(parsed) = nostr_sdk::RelayUrl::parse(url) {
280            if let Some(relay) = pooled.get(&parsed) {
281                if relay.flags().has_read() || relay.flags().has_write() {
282                    continue; // a real chat relay (or an overlap) — never sever
283                }
284            }
285            let _ = pool.force_remove_relay(parsed).await; // plain remove_relay refuses GOSSIP
286            forget_warmed_relay(url);
287        }
288    }
289}
290
291/// Production [`Transport`] over the live Nostr network.
292///
293/// Reuses the app's persistent client (`state::nostr_client`) — already connected to the user's relays,
294/// which ARE the Community's relays — and targets sends/fetches at the Community relay set explicitly. No
295/// per-call cold handshake (a throwaway client paid ~4s of TLS + relay handshake on every op). Any Community
296/// relay the pool doesn't already hold is added idempotently, mirroring the realtime subscription.
297pub struct LiveTransport {
298    timeout: std::time::Duration,
299}
300
301impl Default for LiveTransport {
302    fn default() -> Self {
303        Self { timeout: std::time::Duration::from_secs(10) }
304    }
305}
306
307impl LiveTransport {
308    pub fn new() -> Self {
309        Self::default()
310    }
311
312    pub fn with_timeout(timeout: std::time::Duration) -> Self {
313        Self { timeout }
314    }
315
316    /// Grab the app's persistent client and make sure it's connected to `relays` — the Community's relays
317    /// are the user's own relays, so this is almost always a pure no-op (already in the warm pool). A relay
318    /// the pool doesn't hold yet is added idempotently (mirrors what the realtime subscription does), then
319    /// `connect()` kicks it without disturbing the already-connected majority. Never shut this client down:
320    /// it is shared. Errors only if there is no client yet or every relay url was invalid.
321    async fn warm_client(relays: &[String], connect_timeout: std::time::Duration) -> Result<Client, String> {
322        if relays.is_empty() {
323            return Err("community has no relays configured".to_string());
324        }
325        let client = crate::state::nostr_client().ok_or_else(|| "nostr client not initialized".to_string())?;
326
327        // Fast path: every one of these relays was already warmed this session → the pool holds and
328        // (auto-)maintains them, so skip the redundant add_relay + connect churn that otherwise runs on
329        // EVERY fetch/publish. Account swaps bump the generation, dropping the cache.
330        let generation = crate::state::current_session_generation();
331        {
332            let warmed = WARMED_RELAYS.lock().unwrap_or_else(|e| e.into_inner());
333            if warmed.0 == generation && relays.iter().all(|r| warmed.1.contains(r)) {
334                return Ok(client);
335            }
336        }
337
338        // `add_relay` returns Ok(true) if NEWLY added, Ok(false) if the pool already held it.
339        // Community relays join GOSSIP|PING (see `community_relay_options`) so they stay 24/7 warm
340        // without pulling the user's DM/profile traffic onto relays they don't own. An overlap
341        // relay already in the pool as a user relay keeps its READ+WRITE flags (add_relay no-ops).
342        let mut added_new = false;
343        let mut succeeded: Vec<&String> = Vec::new();
344        for url in relays {
345            let opts = crate::community_relay_options();
346            match client.pool().add_relay(url.as_str(), opts).await {
347                Ok(true) => { added_new = true; succeeded.push(url); }
348                Ok(false) => { succeeded.push(url); }
349                Err(_) => {}
350            }
351        }
352        if succeeded.is_empty() {
353            return Err("no valid community relays could be added".to_string());
354        }
355        if added_new {
356            // A relay we weren't already connected to (a Community on non-default relays). `connect()`
357            // returns before sockets are up, so WAIT for it — otherwise the immediate fetch/send reaches
358            // zero relays. Already-connected relays return instantly in `success`, so the warm majority
359            // adds no latency; only the genuinely-new relay's handshake is awaited (bounded).
360            let _ = client.try_connect(connect_timeout).await;
361        } else {
362            // Every relay already warm in the pool — cheap re-kick of any dropped connection, no wait.
363            client.connect().await;
364        }
365
366        // Record the now-connected relays as warmed for this generation so subsequent calls fast-path
367        // (reset the set if the generation advanced under us — a swap mid-warm).
368        {
369            let mut warmed = WARMED_RELAYS.lock().unwrap_or_else(|e| e.into_inner());
370            if warmed.0 != generation {
371                warmed.0 = generation;
372                warmed.1.clear();
373            }
374            for url in succeeded {
375                warmed.1.insert(url.clone());
376            }
377        }
378        Ok(client)
379    }
380}
381
382#[async_trait::async_trait]
383impl Transport for LiveTransport {
384    async fn publish(&self, event: &Event, relays: &[String]) -> Result<(), String> {
385        let client = Self::warm_client(relays, self.timeout).await?;
386        let timeout = self.timeout;
387        let mut targets: Vec<String> = Vec::new();
388        for r in relays { if !targets.contains(r) { targets.push(r.clone()); } }
389        // Fan out one send per relay and RETURN on the first ACK — never wait for the slowest relay (a
390        // distant/ratelimited one must not gate a reaction/edit/message). Each send is SPAWNED, so the rest
391        // keep delivering to every relay after we return (dropping a JoinHandle detaches, it doesn't abort).
392        // Single attempt — durable retry is publish_durable's job. The sends only touch relays, no per-account
393        // state, so no SessionGuard is needed.
394        use futures_util::stream::{FuturesUnordered, StreamExt};
395        let mut sends: FuturesUnordered<_> = targets
396            .into_iter()
397            .map(|r| {
398                let client = client.clone();
399                let event = event.clone();
400                tokio::spawn(async move {
401                    matches!(
402                        tokio::time::timeout(timeout, client.send_event_to(vec![r.clone()], &event)).await,
403                        Ok(Ok(out)) if RelayUrl::parse(&r).map(|u| out.success.contains(&u)).unwrap_or(false)
404                    )
405                })
406            })
407            .collect();
408        while let Some(joined) = sends.next().await {
409            if matches!(joined, Ok(true)) {
410                return Ok(()); // first relay ACKed; the others keep delivering in the background
411            }
412        }
413        Err("no relay accepted the event".to_string())
414    }
415
416    async fn fetch(&self, query: &Query, relays: &[String]) -> Result<Vec<Event>, String> {
417        let client = Self::warm_client(relays, self.timeout).await?;
418        let timeout = self.timeout;
419        let filter = query.to_filter();
420
421        let mut targets: Vec<String> = Vec::new();
422        for r in relays {
423            if !targets.contains(r) {
424                targets.push(r.clone());
425            }
426        }
427
428        // One relay → nothing to race.
429        if targets.len() <= 1 {
430            return client
431                .fetch_events_from(targets, filter, timeout)
432                .await
433                .map(|evs| evs.into_iter().collect())
434                .map_err(|e| e.to_string());
435        }
436
437        // RACE per-relay (mirrors the publish first-ACK race): hand the caller the FIRST relay's
438        // batch the instant it completes, then keep the slower relays running in the BACKGROUND and
439        // feed any events they alone hold back through the ingester. Never wait for the slowest relay.
440        use futures_util::stream::{FuturesUnordered, StreamExt};
441        let mut fetches: FuturesUnordered<_> = targets
442            .into_iter()
443            .map(|r| {
444                let client = client.clone();
445                let filter = filter.clone();
446                tokio::spawn(async move {
447                    client
448                        .fetch_events_from(vec![r], filter, timeout)
449                        .await
450                        .map(|evs| evs.into_iter().collect::<Vec<Event>>())
451                        .unwrap_or_default()
452                })
453            })
454            .collect();
455
456        // UNION every relay that answers — ALWAYS. A single fast-but-shallow relay must never be
457        // the sole input to what comes back: fail-closed folds gap-quarantine on a partial plane
458        // (seats wedge on stale names/roles), the re-founding coverage gate aborts on a missing
459        // head, and the history-start verdict latches scroll-back dead. All three happened live
460        // off the same first-relay-wins race. Back-paging (`until`) drains to completion (its
461        // verdict must be authoritative; bounded by the per-relay timeout); everything else
462        // drains up to a grace window past the FIRST response — so one dead relay costs the
463        // grace, not the full timeout. Relays slower than the grace still background-merge below.
464        let mut result: Vec<Event> = Vec::new();
465        loop {
466            match fetches.next().await {
467                Some(Ok(evs)) => {
468                    result = evs;
469                    break;
470                }
471                Some(Err(_)) => continue, // task join error — try the next relay
472                None => break,            // every relay failed
473            }
474        }
475        let mut union_ids: std::collections::HashSet<EventId> = result.iter().map(|e| e.id).collect();
476        if query.until.is_some() {
477            while let Some(joined) = fetches.next().await {
478                if let Ok(evs) = joined {
479                    for e in evs {
480                        if union_ids.insert(e.id) {
481                            result.push(e);
482                        }
483                    }
484                }
485            }
486        } else if !fetches.is_empty() {
487            let grace = tokio::time::sleep(std::time::Duration::from_millis(FETCH_UNION_GRACE_MS));
488            tokio::pin!(grace);
489            loop {
490                tokio::select! {
491                    _ = &mut grace => break,
492                    next = fetches.next() => match next {
493                        Some(Ok(evs)) => {
494                            for e in evs {
495                                if union_ids.insert(e.id) {
496                                    result.push(e);
497                                }
498                            }
499                        }
500                        Some(Err(_)) => continue,
501                        None => break,
502                    }
503                }
504            }
505        }
506
507        // Background-merge the relays that haven't finished: dedup by event id ONLY (identical bytes)
508        // against what we returned, then hand the rest to the ingester. Conflicting editions carry
509        // distinct ids, so the protocol's convergence engine resolves them — not the transport.
510        if !fetches.is_empty() {
511            let seen: std::collections::HashSet<EventId> = result.iter().map(|e| e.id).collect();
512            // Captured BEFORE the drain spawn: the drain can outlive an account swap, and
513            // stragglers fetched under the prior session must not feed the new one's ingest.
514            let session = crate::state::SessionGuard::capture();
515            tokio::spawn(async move {
516                let mut extra: Vec<Event> = Vec::new();
517                let mut extra_ids: std::collections::HashSet<EventId> = std::collections::HashSet::new();
518                while let Some(joined) = fetches.next().await {
519                    if let Ok(evs) = joined {
520                        for e in evs {
521                            if !seen.contains(&e.id) && extra_ids.insert(e.id) {
522                                extra.push(e);
523                            }
524                        }
525                    }
526                }
527                if !session.is_valid() {
528                    return;
529                }
530                submit_stragglers(extra);
531            });
532        }
533
534        Ok(result)
535    }
536
537    async fn publish_durable(&self, event: &Event, relays: &[String]) -> Result<(), String> {
538        // "Durable" = confirm the network has it (≥1 relay ACKs within CONFIRM_WINDOW), then keep bugging the
539        // SLOW/ratelimited stragglers (Damus-style 1-event/min) in the BACKGROUND. If NOTHING ACKs in the
540        // window we throw — a dead relay set is a failure, not an endless wait. Uses the shared warm client
541        // (NEVER shut down here) and targets the community relay set across rounds.
542        let client = Self::warm_client(relays, self.timeout).await?;
543        let timeout = self.timeout;
544        let event = event.clone();
545        let backoff = std::time::Duration::from_millis(750);
546        let mut pending: Vec<String> = Vec::new();
547        for r in relays { if !pending.contains(r) { pending.push(r.clone()); } }
548        if pending.is_empty() {
549            return Err("no relays to broadcast to".to_string());
550        }
551
552        // Phase 1 — CONFIRM: RACE the relays and return the instant ANY one ACKs — never wait for the
553        // slowest. `send_event_to(all)` blocks on the slowest relay (a distant/ratelimited one dominates the
554        // latency), so we fan out one send per relay and take the first winner; the losers are cancelled and
555        // re-sent in the background. Retry rounds within CONFIRM_WINDOW; zero ACKs in the window = failure.
556        let mut acked_any = false;
557        let _ = tokio::time::timeout(CONFIRM_WINDOW, async {
558            loop {
559                let sends = pending.iter().cloned().map(|r| {
560                    let client = &client;
561                    let event = &event;
562                    Box::pin(async move {
563                        match tokio::time::timeout(timeout, client.send_event_to(vec![r.clone()], event)).await {
564                            Ok(Ok(out)) if RelayUrl::parse(&r).map(|u| out.success.contains(&u)).unwrap_or(false) => Ok(r),
565                            _ => Err(()),
566                        }
567                    })
568                });
569                if let Ok((winner, _losers)) = futures_util::future::select_ok(sends).await {
570                    acked_any = true;
571                    pending.retain(|r| r != &winner);
572                    break;
573                }
574                tokio::time::sleep(backoff).await;
575            }
576        })
577        .await;
578
579        if !acked_any {
580            return Err(format!("no relay accepted the event within {}s", CONFIRM_WINDOW.as_secs()));
581        }
582        if pending.is_empty() {
583            return Ok(()); // every relay ACKed during the confirm phase
584        }
585
586        // Phase 2 — BACKGROUND: thread the laggards through a durable publisher (retries each up to
587        // MAX_PUBLISH_ATTEMPTS at a 750ms backoff, so it can't run forever). The caller returns NOW with its
588        // confirmed ACK; 's fetch-union heals anything that never lands. The client is shared — not torn
589        // down — so the spawned task just drops its handle when finished.
590        tokio::spawn(async move {
591            let client_ref = &client;
592            let event_ref = &event;
593            let _ = durable_broadcast(&pending, MAX_PUBLISH_ATTEMPTS, backoff, move |round| {
594                Box::pin(async move {
595                    match tokio::time::timeout(timeout, client_ref.send_event_to(round.clone(), event_ref)).await {
596                        Ok(Ok(output)) => round.into_iter().filter(|p| RelayUrl::parse(p).map(|u| output.success.contains(&u)).unwrap_or(false)).collect(),
597                        _ => Vec::new(),
598                    }
599                })
600            })
601            .await;
602        });
603        Ok(())
604    }
605}
606
607#[cfg(test)]
608pub(crate) mod memory {
609    use super::*;
610    use std::collections::{HashMap, HashSet};
611    use std::sync::Mutex;
612
613    /// An in-memory stand-in for the Community's relay set. Stores events per relay
614    /// url so tests can model partial propagation (a relay that missed an event) and
615    /// verify the redundancy/self-heal property: a fetch across the set unions +
616    /// dedups, so a gap on one relay is covered by its siblings.
617    pub struct MemoryRelay {
618        per_relay: Mutex<HashMap<String, Vec<Event>>>,
619        subscribers: Mutex<Vec<(Query, tokio::sync::mpsc::UnboundedSender<Event>)>>,
620    }
621
622    /// NIP-01 ephemeral range: relays stream these to live subscriptions but never store
623    /// them, so a fetch on a real relay can never return one.
624    fn is_ephemeral(kind: u16) -> bool {
625        (20000..30000).contains(&kind)
626    }
627
628    impl MemoryRelay {
629        pub fn new() -> Self {
630            MemoryRelay {
631                per_relay: Mutex::new(HashMap::new()),
632                subscribers: Mutex::new(Vec::new()),
633            }
634        }
635
636        /// Open a live subscription: every subsequent publish/inject matching `query` is
637        /// delivered — ephemerals included, which stream but are never stored.
638        pub fn subscribe(&self, query: Query) -> tokio::sync::mpsc::UnboundedReceiver<Event> {
639            let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
640            self.subscribers.lock().unwrap().push((query, tx));
641            rx
642        }
643
644        /// Push `event` to every live matching subscriber, pruning closed ones.
645        fn deliver(&self, event: &Event) {
646            self.subscribers.lock().unwrap().retain(|(q, tx)| {
647                if q.matches(event) {
648                    tx.send(event.clone()).is_ok()
649                } else {
650                    !tx.is_closed()
651                }
652            });
653        }
654
655        /// Publish to ONLY a subset of relays — used to simulate a relay missing an
656        /// event (e.g. a dropped rekey) for redundancy tests.
657        pub fn inject(&self, event: &Event, relays: &[String]) {
658            self.deliver(event);
659            if is_ephemeral(event.kind.as_u16()) {
660                return; // live-delivered above, never stored
661            }
662            // Replaceable kinds: parameterized (30000-39999, keyed by (kind, pubkey, d-tag)) AND
663            // standard (10000-19999, plus 0/3, keyed by (kind, pubkey) — the d-tag is "") — a relay keeps
664            // only the latest at that coordinate, so a new event REPLACES the old (NIP-01). This is what
665            // makes a revocation tombstone overwrite a bundle, and a fresh 13302 supersede the last one,
666            // even on relays that ignore deletions — model it so tests match real relay behavior.
667            let d_tag = |e: &Event| e.tags.iter().find_map(|t| {
668                let s = t.as_slice();
669                (s.len() >= 2 && s[0] == "d").then(|| s[1].clone())
670            }).unwrap_or_default();
671            let k = event.kind.as_u16();
672            let replaceable = (30000..40000).contains(&k) || (10000..20000).contains(&k) || k == 0 || k == 3;
673            let coord = (event.kind.as_u16(), event.pubkey, d_tag(event));
674            let mut map = self.per_relay.lock().unwrap();
675            for r in relays {
676                let v = map.entry(r.clone()).or_default();
677                if replaceable {
678                    v.retain(|e| (e.kind.as_u16(), e.pubkey, d_tag(e)) != coord);
679                }
680                v.push(event.clone());
681            }
682        }
683
684        /// How many events a given relay holds (test introspection).
685        pub fn count_on(&self, relay: &str) -> usize {
686            self.per_relay.lock().unwrap().get(relay).map_or(0, |v| v.len())
687        }
688
689        /// Apply a NIP-09 deletion: drop any stored event matched by the deletion's `e`
690        /// tags (by id) OR `a` tags (addressable coordinate `kind:pubkey:d`), AND whose
691        /// author matches the deletion's author (a deleter can only delete their own
692        /// events — same rule strfry enforces).
693        fn apply_deletion(&self, deletion: &Event, relays: &[String]) {
694            let mut id_targets: HashSet<String> = HashSet::new();
695            let mut coord_targets: HashSet<String> = HashSet::new();
696            for t in deletion.tags.iter() {
697                let s = t.as_slice();
698                if s.len() >= 2 && s[0] == "e" {
699                    id_targets.insert(s[1].clone());
700                } else if s.len() >= 2 && s[0] == "a" {
701                    coord_targets.insert(s[1].clone());
702                }
703            }
704            let mut map = self.per_relay.lock().unwrap();
705            for r in relays {
706                if let Some(events) = map.get_mut(r) {
707                    events.retain(|e| {
708                        if e.pubkey != deletion.pubkey {
709                            return true;
710                        }
711                        if id_targets.contains(&e.id.to_hex()) {
712                            return false;
713                        }
714                        // Addressable coordinate "kind:pubkey:d-identifier".
715                        let d = e.tags.iter().find_map(|t| {
716                            let s = t.as_slice();
717                            (s.len() >= 2 && s[0] == "d").then(|| s[1].clone())
718                        }).unwrap_or_default();
719                        let coord = format!("{}:{}:{}", e.kind.as_u16(), e.pubkey.to_hex(), d);
720                        !coord_targets.contains(&coord)
721                    });
722                }
723            }
724        }
725    }
726
727    #[async_trait::async_trait]
728    impl Transport for MemoryRelay {
729        async fn publish(&self, event: &Event, relays: &[String]) -> Result<(), String> {
730            // Honor NIP-09 so the delete→gone cycle is testable offline.
731            if event.kind == Kind::EventDeletion {
732                self.apply_deletion(event, relays);
733                self.deliver(event);
734            } else {
735                self.inject(event, relays);
736            }
737            Ok(())
738        }
739
740        // The in-memory relay always accepts, so a "durable" publish is just a publish (the retry
741        // engine itself is unit-tested separately via `durable_broadcast`).
742        async fn publish_durable(&self, event: &Event, relays: &[String]) -> Result<(), String> {
743            self.publish(event, relays).await
744        }
745
746        async fn fetch(&self, query: &Query, relays: &[String]) -> Result<Vec<Event>, String> {
747            let map = self.per_relay.lock().unwrap();
748            let mut seen = HashSet::new();
749            let mut out = Vec::new();
750            for r in relays {
751                if let Some(events) = map.get(r) {
752                    for ev in events {
753                        // Never stored, but guard the read path too: a real relay never
754                        // serves an ephemeral from a fetch, whatever got in.
755                        if is_ephemeral(ev.kind.as_u16()) {
756                            continue;
757                        }
758                        if query.matches(ev) && seen.insert(ev.id) {
759                            out.push(ev.clone());
760                        }
761                    }
762                }
763            }
764            // Apply the relay-side page cap newest-first (matches how relays honor `limit`):
765            // sort by created_at desc, keep the newest `limit`. Mirrors production paging.
766            if let Some(limit) = query.limit {
767                out.sort_by(|a, b| b.created_at.cmp(&a.created_at).then_with(|| b.id.cmp(&a.id)));
768                out.truncate(limit);
769            }
770            Ok(out)
771        }
772    }
773}
774
775#[cfg(test)]
776mod tests {
777    use super::*;
778
779    fn evt(kind: u16, z: &str) -> Event {
780        EventBuilder::new(Kind::Custom(kind), "x")
781            .tags([Tag::custom(
782                TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::Z)),
783                [z.to_string()],
784            )])
785            .sign_with_keys(&Keys::generate())
786            .unwrap()
787    }
788
789    #[test]
790    fn query_matches_kind_and_z() {
791        let e = evt(3300, "abc");
792        assert!(Query { kinds: vec![3300], z_tags: vec!["abc".into()], since: None, ..Default::default() }.matches(&e));
793        assert!(!Query { kinds: vec![3301], ..Default::default() }.matches(&e));
794        assert!(!Query { kinds: vec![], z_tags: vec!["xyz".into()], since: None, ..Default::default() }.matches(&e));
795        assert!(Query::default().matches(&e), "empty query matches anything");
796    }
797
798    fn evt_at(kind: u16, secs: u64) -> Event {
799        EventBuilder::new(Kind::Custom(kind), "x")
800            .custom_created_at(Timestamp::from(secs))
801            .sign_with_keys(&Keys::generate())
802            .unwrap()
803    }
804
805    /// Build a z-tagged event at a controlled created_at (for deterministic paging tests —
806    /// the real outer events carry wall-clock send time).
807    fn evt_z_at(kind: u16, z: &str, secs: u64) -> Event {
808        EventBuilder::new(Kind::Custom(kind), "x")
809            .custom_created_at(Timestamp::from(secs))
810            .tags([Tag::custom(
811                TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::Z)),
812                [z.to_string()],
813            )])
814            .sign_with_keys(&Keys::generate())
815            .unwrap()
816    }
817
818    #[tokio::test]
819    async fn fetch_pages_with_until_and_limit_newest_first() {
820        // The Discord-style paging mechanism: `limit` caps newest-first; `until` walks older.
821        let relay = super::memory::MemoryRelay::new();
822        let relays = vec!["r1".to_string()];
823        for s in 1..=5u64 {
824            relay.inject(&evt_z_at(3300, "pg", s), &relays);
825        }
826        let secs = |evs: &[Event]| evs.iter().map(|e| e.created_at.as_secs()).collect::<Vec<_>>();
827
828        // Latest page: the two newest (secs 5, 4), newest-first.
829        let latest = relay
830            .fetch(&Query { kinds: vec![3300], z_tags: vec!["pg".into()], limit: Some(2), ..Default::default() }, &relays)
831            .await
832            .unwrap();
833        assert_eq!(secs(&latest), vec![5, 4]);
834
835        // Older page before the cursor (until=3, inclusive): secs 3, 2.
836        let older = relay
837            .fetch(&Query { kinds: vec![3300], z_tags: vec!["pg".into()], until: Some(3), limit: Some(2), ..Default::default() }, &relays)
838            .await
839            .unwrap();
840        assert_eq!(secs(&older), vec![3, 2]);
841
842        // Start of history: until=1 returns only the single oldest event — the signal the
843        // caller uses to mark "no more older" and stop hitting the network.
844        let start = relay
845            .fetch(&Query { kinds: vec![3300], z_tags: vec!["pg".into()], until: Some(1), limit: Some(2), ..Default::default() }, &relays)
846            .await
847            .unwrap();
848        assert_eq!(secs(&start), vec![1]);
849    }
850
851    #[test]
852    fn to_filter_translates_kinds_z_and_since() {
853        // The live/test-parity claim rests on to_filter matching matches(); assert
854        // the Filter directly. An event the Query matches must also pass the Filter.
855        let q = Query { kinds: vec![3300], z_tags: vec!["abc".into()], since: Some(100), ..Default::default() };
856        let filter = q.to_filter();
857        let matching = EventBuilder::new(Kind::Custom(3300), "x")
858            .custom_created_at(Timestamp::from(150))
859            .tags([Tag::custom(
860                TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::Z)),
861                ["abc".to_string()],
862            )])
863            .sign_with_keys(&Keys::generate())
864            .unwrap();
865        assert!(filter.match_event(&matching, MatchEventOptions::new()), "to_filter must accept what matches() accepts");
866        assert!(q.matches(&matching));
867
868        // Wrong kind, wrong z, and too-early all rejected by the same Filter.
869        let wrong_kind = EventBuilder::new(Kind::Custom(3301), "x")
870            .custom_created_at(Timestamp::from(150))
871            .tags([Tag::custom(
872                TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::Z)),
873                ["abc".to_string()],
874            )])
875            .sign_with_keys(&Keys::generate())
876            .unwrap();
877        assert!(!filter.match_event(&wrong_kind, MatchEventOptions::new()));
878    }
879
880    /// Build an event carrying one arbitrary single-letter tag (recipient `p`, wrapped-kind `k`).
881    fn evt_sl(kind: u16, letter: Alphabet, value: &str) -> Event {
882        EventBuilder::new(Kind::Custom(kind), "x")
883            .tags([Tag::custom(
884                TagKind::SingleLetter(SingleLetterTag::lowercase(letter)),
885                [value.to_string()],
886            )])
887            .sign_with_keys(&Keys::generate())
888            .unwrap()
889    }
890
891    #[test]
892    fn to_filter_and_matches_agree_on_authors() {
893        let keys = Keys::generate();
894        let e = EventBuilder::new(Kind::Custom(1059), "x").sign_with_keys(&keys).unwrap();
895        let q = Query { kinds: vec![1059], authors: vec![keys.public_key().to_hex()], ..Default::default() };
896        assert!(q.matches(&e));
897        assert!(q.to_filter().match_event(&e, MatchEventOptions::new()));
898        let miss = Query { kinds: vec![1059], authors: vec![Keys::generate().public_key().to_hex()], ..Default::default() };
899        assert!(!miss.matches(&e));
900        assert!(!miss.to_filter().match_event(&e, MatchEventOptions::new()));
901    }
902
903    #[test]
904    fn to_filter_and_matches_agree_on_p_tags() {
905        let recipient = Keys::generate().public_key().to_hex();
906        let e = evt_sl(1059, Alphabet::P, &recipient);
907        let q = Query { kinds: vec![1059], p_tags: vec![recipient], ..Default::default() };
908        assert!(q.matches(&e));
909        assert!(q.to_filter().match_event(&e, MatchEventOptions::new()));
910        let miss = Query { kinds: vec![1059], p_tags: vec![Keys::generate().public_key().to_hex()], ..Default::default() };
911        assert!(!miss.matches(&e));
912        assert!(!miss.to_filter().match_event(&e, MatchEventOptions::new()));
913    }
914
915    #[test]
916    fn to_filter_and_matches_agree_on_k_tags() {
917        let e = evt_sl(1059, Alphabet::K, "3311");
918        let q = Query { kinds: vec![1059], k_tags: vec!["3311".into()], ..Default::default() };
919        assert!(q.matches(&e));
920        assert!(q.to_filter().match_event(&e, MatchEventOptions::new()));
921        let miss = Query { kinds: vec![1059], k_tags: vec!["3300".into()], ..Default::default() };
922        assert!(!miss.matches(&e));
923        assert!(!miss.to_filter().match_event(&e, MatchEventOptions::new()));
924    }
925
926    #[test]
927    fn to_filter_empty_kinds_only_constrains_z_and_since() {
928        // Empty kinds = no kind constraint, matching matches()' behavior.
929        let q = Query { kinds: vec![], z_tags: vec!["p".into()], since: None, ..Default::default() };
930        let filter = q.to_filter();
931        let e = evt(3300, "p");
932        assert!(filter.match_event(&e, MatchEventOptions::new()));
933        assert!(q.matches(&e));
934    }
935
936    #[test]
937    fn since_is_an_inclusive_lower_bound() {
938        let below = evt_at(3300, 99);
939        let exact = evt_at(3300, 100);
940        let above = evt_at(3300, 101);
941        let q = Query { kinds: vec![3300], z_tags: vec![], since: Some(100), ..Default::default() };
942        assert!(!q.matches(&below), "below the floor is excluded");
943        assert!(q.matches(&exact), "exactly the floor is included");
944        assert!(q.matches(&above), "above the floor is included");
945    }
946
947    #[test]
948    fn z_tags_match_as_or_set() {
949        // The whole-channel fetch lists multiple epoch pseudonyms; an event
950        // tagged with any one of them must match.
951        let e = evt(3300, "p2");
952        let q = Query { kinds: vec![3300], z_tags: vec!["p1".into(), "p2".into()], since: None, ..Default::default() };
953        assert!(q.matches(&e));
954        let miss = Query { kinds: vec![3300], z_tags: vec!["p1".into(), "p3".into()], since: None, ..Default::default() };
955        assert!(!miss.matches(&e));
956    }
957
958    #[tokio::test]
959    async fn fetch_unions_and_dedups_across_relays() {
960        use super::memory::MemoryRelay;
961        let relay = MemoryRelay::new();
962        let relays = vec!["r1".to_string(), "r2".to_string(), "r3".to_string()];
963        let e = evt(3300, "p");
964        relay.publish(&e, &relays).await.unwrap();
965        let got = relay
966            .fetch(&Query { kinds: vec![3300], z_tags: vec!["p".into()], since: None, ..Default::default() }, &relays)
967            .await
968            .unwrap();
969        assert_eq!(got.len(), 1, "same event on 3 relays dedups to 1");
970    }
971
972    #[tokio::test]
973    async fn durable_broadcast_retries_only_the_failing_relays_until_they_ack() {
974        // r1/r3 ACK on round 1; r2 fails the first 4 rounds then ACKs. The engine must keep re-sending
975        // ONLY r2 (not the already-ACKed r1/r3) until it lands, and succeed.
976        use std::cell::Cell;
977        let relays = vec!["r1".to_string(), "r2".to_string(), "r3".to_string()];
978        let round = Cell::new(0usize);
979        let r2_round_seen = Cell::new(0usize);
980        let res = durable_broadcast(&relays, 30, std::time::Duration::ZERO, |pending| {
981            let n = round.get();
982            round.set(n + 1);
983            // r2 is only ever retried alone after round 1 — assert we never re-send a relay that ACKed.
984            if n >= 1 {
985                assert_eq!(pending, vec!["r2".to_string()], "only the failing relay is retried");
986                r2_round_seen.set(r2_round_seen.get() + 1);
987            }
988            Box::pin(async move {
989                pending.into_iter().filter(|r| r != "r2" || n >= 4).collect()
990            })
991        })
992        .await;
993        assert!(res.is_ok(), "all relays eventually ACK → Ok");
994        assert!(round.get() >= 5, "kept retrying r2 across rounds");
995    }
996
997    #[tokio::test]
998    async fn durable_broadcast_is_ok_if_some_ack_even_when_one_never_does() {
999        // r1 ACKs; r2 never does. After exhausting r2's retries, the event is still durably out (r1 has
1000        // it, fetch-union covers r2), so the result is Ok — durability is best-effort per relay.
1001        let relays = vec!["r1".to_string(), "r2".to_string()];
1002        let res = durable_broadcast(&relays, 5, std::time::Duration::ZERO, |pending| {
1003            Box::pin(async move { pending.into_iter().filter(|r| r == "r1").collect() })
1004        })
1005        .await;
1006        assert!(res.is_ok(), "≥1 relay accepted → Ok despite a permanently-failing relay");
1007    }
1008
1009    #[tokio::test]
1010    async fn durable_broadcast_errs_only_if_zero_relays_ever_accept() {
1011        let relays = vec!["r1".to_string(), "r2".to_string()];
1012        let res = durable_broadcast(&relays, 5, std::time::Duration::ZERO, |_pending| {
1013            Box::pin(async move { Vec::new() }) // nobody ever ACKs
1014        })
1015        .await;
1016        assert!(res.is_err(), "zero acceptances after the retry cap → Err");
1017    }
1018
1019    #[tokio::test]
1020    async fn redundancy_self_heals_a_missing_relay() {
1021        use super::memory::MemoryRelay;
1022        let relay = MemoryRelay::new();
1023        let all = vec!["r1".to_string(), "r2".to_string(), "r3".to_string()];
1024        let e = evt(3300, "p");
1025        // Event lands on ONLY r2 (the others "missed" it).
1026        relay.inject(&e, &["r2".to_string()]);
1027        assert_eq!(relay.count_on("r1"), 0);
1028        assert_eq!(relay.count_on("r2"), 1);
1029        // A fetch across the full set still finds it (redundancy).
1030        let got = relay.fetch(&Query { kinds: vec![3300], ..Default::default() }, &all).await.unwrap();
1031        assert_eq!(got.len(), 1);
1032    }
1033
1034    #[tokio::test]
1035    async fn ephemeral_kind_streams_live_but_is_never_stored_or_fetched() {
1036        use super::memory::MemoryRelay;
1037        let relay = MemoryRelay::new();
1038        let relays = vec!["r1".to_string()];
1039        let mut sub = relay.subscribe(Query { kinds: vec![21059], ..Default::default() });
1040        let e = evt(21059, "p");
1041        relay.publish(&e, &relays).await.unwrap();
1042        assert_eq!(relay.count_on("r1"), 0, "ephemeral is never stored");
1043        let got = relay
1044            .fetch(&Query { kinds: vec![21059], ..Default::default() }, &relays)
1045            .await
1046            .unwrap();
1047        assert!(got.is_empty(), "a real relay never serves an ephemeral from a fetch");
1048        assert_eq!(sub.try_recv().unwrap().id, e.id, "but a live subscriber receives it");
1049    }
1050
1051    #[tokio::test]
1052    async fn stored_kind_is_fetchable_and_delivered_live() {
1053        use super::memory::MemoryRelay;
1054        let relay = MemoryRelay::new();
1055        let relays = vec!["r1".to_string()];
1056        let mut sub = relay.subscribe(Query { kinds: vec![1059], ..Default::default() });
1057        let e = evt(1059, "p");
1058        relay.publish(&e, &relays).await.unwrap();
1059        let got = relay
1060            .fetch(&Query { kinds: vec![1059], ..Default::default() }, &relays)
1061            .await
1062            .unwrap();
1063        assert_eq!(got.len(), 1, "stored kind is fetchable");
1064        assert_eq!(sub.try_recv().unwrap().id, e.id, "and delivered to the live subscriber");
1065    }
1066
1067    #[tokio::test]
1068    async fn p_tags_route_a_giftwrap_to_the_matching_subscriber() {
1069        use super::memory::MemoryRelay;
1070        let relay = MemoryRelay::new();
1071        let relays = vec!["r1".to_string()];
1072        let alice = Keys::generate().public_key().to_hex();
1073        let bob = Keys::generate().public_key().to_hex();
1074        let mut sub_alice =
1075            relay.subscribe(Query { kinds: vec![1059], p_tags: vec![alice.clone()], ..Default::default() });
1076        let mut sub_bob =
1077            relay.subscribe(Query { kinds: vec![1059], p_tags: vec![bob.clone()], ..Default::default() });
1078        let wrap = evt_sl(1059, Alphabet::P, &alice);
1079        relay.publish(&wrap, &relays).await.unwrap();
1080        assert_eq!(sub_alice.try_recv().unwrap().id, wrap.id, "addressed recipient gets it live");
1081        assert!(sub_bob.try_recv().is_err(), "a differently-addressed subscriber does not");
1082        // Fetch agrees with the live routing.
1083        let for_alice = relay
1084            .fetch(&Query { kinds: vec![1059], p_tags: vec![alice], ..Default::default() }, &relays)
1085            .await
1086            .unwrap();
1087        assert_eq!(for_alice.len(), 1);
1088        let for_bob = relay
1089            .fetch(&Query { kinds: vec![1059], p_tags: vec![bob], ..Default::default() }, &relays)
1090            .await
1091            .unwrap();
1092        assert!(for_bob.is_empty());
1093    }
1094}