From 9a7f5ab8573c7395596ceab083336b043e71deee Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Mon, 28 Oct 2024 08:16:17 +0100 Subject: [PATCH] sdk: fix `filters empty` error when gossip option is enabled Fixes https://github.com/rust-nostr/nostr/issues/594 Signed-off-by: Yuki Kishimoto --- CHANGELOG.md | 1 + crates/nostr-sdk/src/client/mod.rs | 20 +++++++++++--------- crates/nostr-sdk/src/gossip/graph.rs | 12 ++++++------ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 047dbf478..c38f98a51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,7 @@ * lmdb: add missing commit method call in `Store::delete` ([Yuki Kishimoto]) * lmdb: fix unit tests ([Yuki Kishimoto]) * lmdb: fix `Store::save_event` issues ([Yuki Kishimoto]) +* sdk: fix `filters empty` error when gossip option is enabled ([Yuki Kishimoto]) ### Removed diff --git a/crates/nostr-sdk/src/client/mod.rs b/crates/nostr-sdk/src/client/mod.rs index 1cde691cf..be339d488 100644 --- a/crates/nostr-sdk/src/client/mod.rs +++ b/crates/nostr-sdk/src/client/mod.rs @@ -1855,15 +1855,17 @@ impl Client { .await; // Extend filters with read relays and "other" filters (the filters that aren't linked to public keys) - for url in read_relays.into_keys() { - broken_down - .filters - .entry(url) - .and_modify(|f| { - f.extend(broken_down.other.clone()); - }) - .or_default() - .extend(broken_down.other.clone()) + if let Some(other) = broken_down.other { + for url in read_relays.into_keys() { + broken_down + .filters + .entry(url) + .and_modify(|f| { + f.extend(other.clone()); + }) + .or_default() + .extend(other.clone()) + } } // Add outbox relays diff --git a/crates/nostr-sdk/src/gossip/graph.rs b/crates/nostr-sdk/src/gossip/graph.rs index dbb0e00b6..43ab5a9ec 100644 --- a/crates/nostr-sdk/src/gossip/graph.rs +++ b/crates/nostr-sdk/src/gossip/graph.rs @@ -19,7 +19,7 @@ pub struct BrokenDownFilters { /// Filters by url pub filters: HashMap>, /// Filters that can be sent to read relays (generic query, not related to public keys) - pub other: Vec, + pub other: Option>, pub outbox_urls: HashSet, pub inbox_urls: HashSet, } @@ -313,7 +313,7 @@ impl GossipGraph { .into_iter() .map(|(u, f)| (u, f.into_iter().collect::>())) .collect(), - other, + other: if other.is_empty() { None } else { Some(other) }, outbox_urls, inbox_urls, } @@ -387,7 +387,7 @@ mod tests { assert_eq!(broken_down.filters.get(&nostr_bg_url).unwrap(), &filters); assert_eq!(broken_down.filters.get(&nos_lol_url).unwrap(), &filters); assert!(!broken_down.filters.contains_key(&nostr_mom_url)); - assert!(broken_down.other.is_empty()); + assert!(broken_down.other.is_none()); // Multiple filters, multiple authors let authors_filter = Filter::new().authors([keys_a.public_key, keys_b.public_key]); @@ -417,7 +417,7 @@ mod tests { &vec![Filter::new().author(keys_b.public_key)] ); assert!(!broken_down.filters.contains_key(&snort_url)); - assert_eq!(broken_down.other, vec![search_filter]); + assert_eq!(broken_down.other, Some(vec![search_filter])); // Multiple filters, multiple authors and single p tags let authors_filter = Filter::new().authors([keys_a.public_key, keys_b.public_key]); @@ -458,7 +458,7 @@ mod tests { &vec![Filter::new().author(keys_b.public_key)] ); assert!(!broken_down.filters.contains_key(&snort_url)); - assert_eq!(broken_down.other, vec![search_filter]); + assert_eq!(broken_down.other, Some(vec![search_filter])); // Single filter, both author and p tag let filters = vec![Filter::new() @@ -473,6 +473,6 @@ mod tests { assert_eq!(broken_down.filters.get(&nostr_info_url).unwrap(), &filters); assert_eq!(broken_down.filters.get(&relay_rip_url).unwrap(), &filters); assert_eq!(broken_down.filters.get(&snort_url).unwrap(), &filters); - assert!(broken_down.other.is_empty()); + assert!(broken_down.other.is_none()); } }