Skip to content

Commit 53106e4

Browse files
committed
gossip: add GossipAllowedRelays to filter relays during selection
Closes nostr:nevent1qvzqqqqx25pzpepndn2jthmelfxn4umylktqp493ph8yy9d2fse76al2ppprgjcsqqsxjcxdfc5vt8hhqfqfgcynk32ls3fgduxzx4xlevldceuj4sk9kvsgx4h5j Pull-Request: TBD Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent f75e730 commit 53106e4

File tree

9 files changed

+226
-13
lines changed

9 files changed

+226
-13
lines changed

crates/nostr-sdk/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
2424
-->
2525

26+
## Unreleased
27+
28+
### Added
29+
30+
- Add `GossipAllowedRelays` to `GossipOptions` to filter relays during selection (https://github.com/rust-nostr/nostr/pull/1128)
31+
2632
## v0.44.1 - 2025/11/09
2733

2834
### Fixed

crates/nostr-sdk/src/client/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,12 @@ impl Client {
16341634

16351635
// Broken-down filters
16361636
let filters: HashMap<RelayUrl, Filter> = match gossip
1637-
.break_down_filter(filter, pattern, &self.opts.gossip.limits)
1637+
.break_down_filter(
1638+
filter,
1639+
pattern,
1640+
&self.opts.gossip.limits,
1641+
self.opts.gossip.allowed,
1642+
)
16381643
.await?
16391644
{
16401645
BrokenDownFilters::Filters(filters) => filters,
@@ -1710,6 +1715,7 @@ impl Client {
17101715
.get_relays(
17111716
event.tags.public_keys(),
17121717
BestRelaySelection::PrivateMessage { limit: 3 },
1718+
self.opts.gossip.allowed,
17131719
)
17141720
.await?;
17151721

@@ -1740,6 +1746,7 @@ impl Client {
17401746
hints: 1,
17411747
most_received: 1,
17421748
},
1749+
self.opts.gossip.allowed,
17431750
)
17441751
.await?;
17451752

@@ -1754,6 +1761,7 @@ impl Client {
17541761
hints: 1,
17551762
most_received: 1,
17561763
},
1764+
self.opts.gossip.allowed,
17571765
)
17581766
.await?;
17591767

crates/nostr-sdk/src/client/options.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::net::SocketAddr;
1010
use std::path::Path;
1111
use std::time::Duration;
1212

13+
use nostr_gossip::GossipAllowedRelays;
1314
use nostr_relay_pool::prelude::*;
1415

1516
/// Max number of relays to use for gossip
@@ -44,6 +45,8 @@ impl Default for GossipRelayLimits {
4445
pub struct GossipOptions {
4546
/// Max number of relays to use
4647
pub limits: GossipRelayLimits,
48+
/// Allowed relay during selection
49+
pub allowed: GossipAllowedRelays,
4750
}
4851

4952
impl GossipOptions {
@@ -53,6 +56,13 @@ impl GossipOptions {
5356
self.limits = limits;
5457
self
5558
}
59+
60+
/// Set allowed
61+
#[inline]
62+
pub fn allowed(mut self, allowed: GossipAllowedRelays) -> Self {
63+
self.allowed = allowed;
64+
self
65+
}
5666
}
5767

5868
/// Options

crates/nostr-sdk/src/gossip/mod.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::ops::Deref;
77
use std::sync::Arc;
88

99
use nostr::prelude::*;
10-
use nostr_gossip::{BestRelaySelection, NostrGossip};
10+
use nostr_gossip::{BestRelaySelection, GossipAllowedRelays, NostrGossip};
1111

1212
use crate::client::options::GossipRelayLimits;
1313
use crate::client::Error;
@@ -48,15 +48,18 @@ impl GossipWrapper {
4848
&self,
4949
public_keys: I,
5050
selection: BestRelaySelection,
51+
allowed: GossipAllowedRelays,
5152
) -> Result<HashSet<RelayUrl>, Error>
5253
where
5354
I: IntoIterator<Item = &'a PublicKey>,
5455
{
5556
let mut urls: HashSet<RelayUrl> = HashSet::new();
5657

5758
for public_key in public_keys.into_iter() {
58-
let relays: HashSet<RelayUrl> =
59-
self.gossip.get_best_relays(public_key, selection).await?;
59+
let relays: HashSet<RelayUrl> = self
60+
.gossip
61+
.get_best_relays(public_key, selection, allowed)
62+
.await?;
6063
urls.extend(relays);
6164
}
6265

@@ -67,15 +70,18 @@ impl GossipWrapper {
6770
&self,
6871
public_keys: I,
6972
selection: BestRelaySelection,
73+
allowed: GossipAllowedRelays,
7074
) -> Result<HashMap<RelayUrl, BTreeSet<PublicKey>>, Error>
7175
where
7276
I: IntoIterator<Item = &'a PublicKey>,
7377
{
7478
let mut urls: HashMap<RelayUrl, BTreeSet<PublicKey>> = HashMap::new();
7579

7680
for public_key in public_keys.into_iter() {
77-
let relays: HashSet<RelayUrl> =
78-
self.gossip.get_best_relays(public_key, selection).await?;
81+
let relays: HashSet<RelayUrl> = self
82+
.gossip
83+
.get_best_relays(public_key, selection, allowed)
84+
.await?;
7985

8086
for url in relays.into_iter() {
8187
urls.entry(url)
@@ -95,6 +101,7 @@ impl GossipWrapper {
95101
filter: Filter,
96102
pattern: GossipFilterPattern,
97103
limits: &GossipRelayLimits,
104+
allowed: GossipAllowedRelays,
98105
) -> Result<BrokenDownFilters, Error> {
99106
// Extract `p` tag from generic tags and parse public key hex
100107
let p_tag: Option<BTreeSet<PublicKey>> = filter.generic_tags.get(&P_TAG).map(|s| {
@@ -113,6 +120,7 @@ impl GossipWrapper {
113120
BestRelaySelection::Write {
114121
limit: limits.write_relays_per_user,
115122
},
123+
allowed,
116124
)
117125
.await?;
118126

@@ -123,6 +131,7 @@ impl GossipWrapper {
123131
BestRelaySelection::Hints {
124132
limit: limits.hint_relays_per_user,
125133
},
134+
allowed,
126135
)
127136
.await?;
128137

@@ -133,6 +142,7 @@ impl GossipWrapper {
133142
BestRelaySelection::MostReceived {
134143
limit: limits.most_used_relays_per_user,
135144
},
145+
allowed,
136146
)
137147
.await?;
138148

@@ -148,6 +158,7 @@ impl GossipWrapper {
148158
BestRelaySelection::PrivateMessage {
149159
limit: limits.nip17_relays,
150160
},
161+
allowed,
151162
)
152163
.await?;
153164

@@ -181,6 +192,7 @@ impl GossipWrapper {
181192
BestRelaySelection::Read {
182193
limit: limits.read_relays_per_user,
183194
},
195+
allowed,
184196
)
185197
.await?;
186198

@@ -191,6 +203,7 @@ impl GossipWrapper {
191203
BestRelaySelection::Hints {
192204
limit: limits.hint_relays_per_user,
193205
},
206+
allowed,
194207
)
195208
.await?;
196209

@@ -201,6 +214,7 @@ impl GossipWrapper {
201214
BestRelaySelection::MostReceived {
202215
limit: limits.most_used_relays_per_user,
203216
},
217+
allowed,
204218
)
205219
.await?;
206220

@@ -217,6 +231,7 @@ impl GossipWrapper {
217231
BestRelaySelection::PrivateMessage {
218232
limit: limits.nip17_relays,
219233
},
234+
allowed,
220235
)
221236
.await?;
222237

@@ -257,6 +272,7 @@ impl GossipWrapper {
257272
hints: limits.hint_relays_per_user,
258273
most_received: limits.most_used_relays_per_user,
259274
},
275+
allowed,
260276
)
261277
.await?;
262278

@@ -269,6 +285,7 @@ impl GossipWrapper {
269285
BestRelaySelection::PrivateMessage {
270286
limit: limits.nip17_relays,
271287
},
288+
allowed,
272289
)
273290
.await?;
274291

@@ -400,6 +417,7 @@ mod tests {
400417
filter.clone(),
401418
GossipFilterPattern::Nip65,
402419
&GossipRelayLimits::default(),
420+
GossipAllowedRelays::default(),
403421
)
404422
.await
405423
.unwrap()
@@ -420,6 +438,7 @@ mod tests {
420438
authors_filter.clone(),
421439
GossipFilterPattern::Nip65,
422440
&GossipRelayLimits::default(),
441+
GossipAllowedRelays::default(),
423442
)
424443
.await
425444
.unwrap()
@@ -455,6 +474,7 @@ mod tests {
455474
search_filter.clone(),
456475
GossipFilterPattern::Nip65,
457476
&GossipRelayLimits::default(),
477+
GossipAllowedRelays::default(),
458478
)
459479
.await
460480
.unwrap()
@@ -472,6 +492,7 @@ mod tests {
472492
p_tag_filter.clone(),
473493
GossipFilterPattern::Nip65,
474494
&GossipRelayLimits::default(),
495+
GossipAllowedRelays::default(),
475496
)
476497
.await
477498
.unwrap()
@@ -497,6 +518,7 @@ mod tests {
497518
filter.clone(),
498519
GossipFilterPattern::Nip65,
499520
&GossipRelayLimits::default(),
521+
GossipAllowedRelays::default(),
500522
)
501523
.await
502524
.unwrap()
@@ -521,6 +543,7 @@ mod tests {
521543
filter.clone(),
522544
GossipFilterPattern::Nip65,
523545
&GossipRelayLimits::default(),
546+
GossipAllowedRelays::default(),
524547
)
525548
.await
526549
.unwrap()

gossip/nostr-gossip-memory/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929

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

32+
### Added
33+
34+
- Add support for `GossipAllowedRelays` filtering during relay selection (https://github.com/rust-nostr/nostr/pull/1128)
35+
3236
## v0.44.0 - 2025/11/06
3337

3438
First release.

0 commit comments

Comments
 (0)