Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/nostr-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

- Replace `usize` with `u8` for gossip relay limits

### Added

- Add `GossipAllowedRelays` to `GossipOptions` to filter relays during selection (https://github.com/rust-nostr/nostr/pull/1128)

## v0.44.1 - 2025/11/09

### Fixed
Expand Down
10 changes: 9 additions & 1 deletion crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,12 @@ impl Client {

// Broken-down filters
let filters: HashMap<RelayUrl, Filter> = match gossip
.break_down_filter(filter, pattern, &self.opts.gossip.limits)
.break_down_filter(
filter,
pattern,
&self.opts.gossip.limits,
self.opts.gossip.allowed,
)
.await?
{
BrokenDownFilters::Filters(filters) => filters,
Expand Down Expand Up @@ -1728,6 +1733,7 @@ impl Client {
.get_relays(
event.tags.public_keys(),
BestRelaySelection::PrivateMessage { limit: 3 },
self.opts.gossip.allowed,
)
.await?;

Expand Down Expand Up @@ -1758,6 +1764,7 @@ impl Client {
hints: 1,
most_received: 1,
},
self.opts.gossip.allowed,
)
.await?;

Expand All @@ -1772,6 +1779,7 @@ impl Client {
hints: 1,
most_received: 1,
},
self.opts.gossip.allowed,
)
.await?;

Expand Down
10 changes: 10 additions & 0 deletions crates/nostr-sdk/src/client/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::net::SocketAddr;
use std::path::Path;
use std::time::Duration;

use nostr_gossip::GossipAllowedRelays;
use nostr_relay_pool::prelude::*;

/// Max number of relays to use for gossip
Expand Down Expand Up @@ -44,6 +45,8 @@ impl Default for GossipRelayLimits {
pub struct GossipOptions {
/// Max number of relays to use
pub limits: GossipRelayLimits,
/// Allowed relay during selection
pub allowed: GossipAllowedRelays,
}

impl GossipOptions {
Expand All @@ -53,6 +56,13 @@ impl GossipOptions {
self.limits = limits;
self
}

/// Set allowed
#[inline]
pub fn allowed(mut self, allowed: GossipAllowedRelays) -> Self {
self.allowed = allowed;
self
}
}

/// Options
Expand Down
33 changes: 28 additions & 5 deletions crates/nostr-sdk/src/gossip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

use nostr::prelude::*;
use nostr_gossip::{BestRelaySelection, NostrGossip};
use nostr_gossip::{BestRelaySelection, GossipAllowedRelays, NostrGossip};

use crate::client::options::GossipRelayLimits;
use crate::client::Error;
Expand Down Expand Up @@ -58,15 +58,18 @@ impl GossipWrapper {
&self,
public_keys: I,
selection: BestRelaySelection,
allowed: GossipAllowedRelays,
) -> Result<HashSet<RelayUrl>, Error>
where
I: IntoIterator<Item = &'a PublicKey>,
{
let mut urls: HashSet<RelayUrl> = HashSet::new();

for public_key in public_keys.into_iter() {
let relays: HashSet<RelayUrl> =
self.gossip.get_best_relays(public_key, selection).await?;
let relays: HashSet<RelayUrl> = self
.gossip
.get_best_relays(public_key, selection, allowed)
.await?;
urls.extend(relays);
}

Expand All @@ -77,15 +80,18 @@ impl GossipWrapper {
&self,
public_keys: I,
selection: BestRelaySelection,
allowed: GossipAllowedRelays,
) -> Result<HashMap<RelayUrl, BTreeSet<PublicKey>>, Error>
where
I: IntoIterator<Item = &'a PublicKey>,
{
let mut urls: HashMap<RelayUrl, BTreeSet<PublicKey>> = HashMap::new();

for public_key in public_keys.into_iter() {
let relays: HashSet<RelayUrl> =
self.gossip.get_best_relays(public_key, selection).await?;
let relays: HashSet<RelayUrl> = self
.gossip
.get_best_relays(public_key, selection, allowed)
.await?;

for url in relays.into_iter() {
urls.entry(url)
Expand All @@ -105,6 +111,7 @@ impl GossipWrapper {
filter: Filter,
pattern: GossipFilterPattern,
limits: &GossipRelayLimits,
allowed: GossipAllowedRelays,
) -> Result<BrokenDownFilters, Error> {
// Extract `p` tag from generic tags and parse public key hex
let p_tag: Option<BTreeSet<PublicKey>> = filter.generic_tags.get(&P_TAG).map(|s| {
Expand All @@ -123,6 +130,7 @@ impl GossipWrapper {
BestRelaySelection::Write {
limit: limits.write_relays_per_user,
},
allowed,
)
.await?;

Expand All @@ -133,6 +141,7 @@ impl GossipWrapper {
BestRelaySelection::Hints {
limit: limits.hint_relays_per_user,
},
allowed,
)
.await?;

Expand All @@ -143,6 +152,7 @@ impl GossipWrapper {
BestRelaySelection::MostReceived {
limit: limits.most_used_relays_per_user,
},
allowed,
)
.await?;

Expand All @@ -158,6 +168,7 @@ impl GossipWrapper {
BestRelaySelection::PrivateMessage {
limit: limits.nip17_relays,
},
allowed,
)
.await?;

Expand Down Expand Up @@ -191,6 +202,7 @@ impl GossipWrapper {
BestRelaySelection::Read {
limit: limits.read_relays_per_user,
},
allowed,
)
.await?;

Expand All @@ -201,6 +213,7 @@ impl GossipWrapper {
BestRelaySelection::Hints {
limit: limits.hint_relays_per_user,
},
allowed,
)
.await?;

Expand All @@ -211,6 +224,7 @@ impl GossipWrapper {
BestRelaySelection::MostReceived {
limit: limits.most_used_relays_per_user,
},
allowed,
)
.await?;

Expand All @@ -227,6 +241,7 @@ impl GossipWrapper {
BestRelaySelection::PrivateMessage {
limit: limits.nip17_relays,
},
allowed,
)
.await?;

Expand Down Expand Up @@ -267,6 +282,7 @@ impl GossipWrapper {
hints: limits.hint_relays_per_user,
most_received: limits.most_used_relays_per_user,
},
allowed,
)
.await?;

Expand All @@ -279,6 +295,7 @@ impl GossipWrapper {
BestRelaySelection::PrivateMessage {
limit: limits.nip17_relays,
},
allowed,
)
.await?;

Expand Down Expand Up @@ -410,6 +427,7 @@ mod tests {
filter.clone(),
GossipFilterPattern::Nip65,
&GossipRelayLimits::default(),
GossipAllowedRelays::default(),
)
.await
.unwrap()
Expand All @@ -430,6 +448,7 @@ mod tests {
authors_filter.clone(),
GossipFilterPattern::Nip65,
&GossipRelayLimits::default(),
GossipAllowedRelays::default(),
)
.await
.unwrap()
Expand Down Expand Up @@ -465,6 +484,7 @@ mod tests {
search_filter.clone(),
GossipFilterPattern::Nip65,
&GossipRelayLimits::default(),
GossipAllowedRelays::default(),
)
.await
.unwrap()
Expand All @@ -482,6 +502,7 @@ mod tests {
p_tag_filter.clone(),
GossipFilterPattern::Nip65,
&GossipRelayLimits::default(),
GossipAllowedRelays::default(),
)
.await
.unwrap()
Expand All @@ -507,6 +528,7 @@ mod tests {
filter.clone(),
GossipFilterPattern::Nip65,
&GossipRelayLimits::default(),
GossipAllowedRelays::default(),
)
.await
.unwrap()
Expand All @@ -531,6 +553,7 @@ mod tests {
filter.clone(),
GossipFilterPattern::Nip65,
&GossipRelayLimits::default(),
GossipAllowedRelays::default(),
)
.await
.unwrap()
Expand Down
4 changes: 4 additions & 0 deletions gossip/nostr-gossip-memory/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

- Replace `Mutex` with `RwLock` for better concurrency (https://github.com/rust-nostr/nostr/pull/1126)

### Added

- Add support for `GossipAllowedRelays` filtering during relay selection (https://github.com/rust-nostr/nostr/pull/1128)

## v0.44.0 - 2025/11/06

First release.
Loading
Loading