Skip to content

Commit de8b2d5

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 6418672 commit de8b2d5

File tree

9 files changed

+228
-13
lines changed

9 files changed

+228
-13
lines changed

crates/nostr-sdk/CHANGELOG.md

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

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

32+
### Added
33+
34+
- Add `GossipAllowedRelays` to `GossipOptions` to filter relays during selection (https://github.com/rust-nostr/nostr/pull/1128)
35+
3236
## v0.44.1 - 2025/11/09
3337

3438
### Fixed

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

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

16531653
// Broken-down filters
16541654
let filters: HashMap<RelayUrl, Filter> = match gossip
1655-
.break_down_filter(filter, pattern, &self.opts.gossip.limits)
1655+
.break_down_filter(
1656+
filter,
1657+
pattern,
1658+
&self.opts.gossip.limits,
1659+
self.opts.gossip.allowed,
1660+
)
16561661
.await?
16571662
{
16581663
BrokenDownFilters::Filters(filters) => filters,
@@ -1728,6 +1733,7 @@ impl Client {
17281733
.get_relays(
17291734
event.tags.public_keys(),
17301735
BestRelaySelection::PrivateMessage { limit: 3 },
1736+
self.opts.gossip.allowed,
17311737
)
17321738
.await?;
17331739

@@ -1758,6 +1764,7 @@ impl Client {
17581764
hints: 1,
17591765
most_received: 1,
17601766
},
1767+
self.opts.gossip.allowed,
17611768
)
17621769
.await?;
17631770

@@ -1772,6 +1779,7 @@ impl Client {
17721779
hints: 1,
17731780
most_received: 1,
17741781
},
1782+
self.opts.gossip.allowed,
17751783
)
17761784
.await?;
17771785

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
@@ -8,7 +8,7 @@ use std::sync::atomic::{AtomicU64, Ordering};
88
use std::sync::Arc;
99

1010
use nostr::prelude::*;
11-
use nostr_gossip::{BestRelaySelection, NostrGossip};
11+
use nostr_gossip::{BestRelaySelection, GossipAllowedRelays, NostrGossip};
1212

1313
use crate::client::options::GossipRelayLimits;
1414
use crate::client::Error;
@@ -58,15 +58,18 @@ impl GossipWrapper {
5858
&self,
5959
public_keys: I,
6060
selection: BestRelaySelection,
61+
allowed: GossipAllowedRelays,
6162
) -> Result<HashSet<RelayUrl>, Error>
6263
where
6364
I: IntoIterator<Item = &'a PublicKey>,
6465
{
6566
let mut urls: HashSet<RelayUrl> = HashSet::new();
6667

6768
for public_key in public_keys.into_iter() {
68-
let relays: HashSet<RelayUrl> =
69-
self.gossip.get_best_relays(public_key, selection).await?;
69+
let relays: HashSet<RelayUrl> = self
70+
.gossip
71+
.get_best_relays(public_key, selection, allowed)
72+
.await?;
7073
urls.extend(relays);
7174
}
7275

@@ -77,15 +80,18 @@ impl GossipWrapper {
7780
&self,
7881
public_keys: I,
7982
selection: BestRelaySelection,
83+
allowed: GossipAllowedRelays,
8084
) -> Result<HashMap<RelayUrl, BTreeSet<PublicKey>>, Error>
8185
where
8286
I: IntoIterator<Item = &'a PublicKey>,
8387
{
8488
let mut urls: HashMap<RelayUrl, BTreeSet<PublicKey>> = HashMap::new();
8589

8690
for public_key in public_keys.into_iter() {
87-
let relays: HashSet<RelayUrl> =
88-
self.gossip.get_best_relays(public_key, selection).await?;
91+
let relays: HashSet<RelayUrl> = self
92+
.gossip
93+
.get_best_relays(public_key, selection, allowed)
94+
.await?;
8995

9096
for url in relays.into_iter() {
9197
urls.entry(url)
@@ -105,6 +111,7 @@ impl GossipWrapper {
105111
filter: Filter,
106112
pattern: GossipFilterPattern,
107113
limits: &GossipRelayLimits,
114+
allowed: GossipAllowedRelays,
108115
) -> Result<BrokenDownFilters, Error> {
109116
// Extract `p` tag from generic tags and parse public key hex
110117
let p_tag: Option<BTreeSet<PublicKey>> = filter.generic_tags.get(&P_TAG).map(|s| {
@@ -123,6 +130,7 @@ impl GossipWrapper {
123130
BestRelaySelection::Write {
124131
limit: limits.write_relays_per_user,
125132
},
133+
allowed,
126134
)
127135
.await?;
128136

@@ -133,6 +141,7 @@ impl GossipWrapper {
133141
BestRelaySelection::Hints {
134142
limit: limits.hint_relays_per_user,
135143
},
144+
allowed,
136145
)
137146
.await?;
138147

@@ -143,6 +152,7 @@ impl GossipWrapper {
143152
BestRelaySelection::MostReceived {
144153
limit: limits.most_used_relays_per_user,
145154
},
155+
allowed,
146156
)
147157
.await?;
148158

@@ -158,6 +168,7 @@ impl GossipWrapper {
158168
BestRelaySelection::PrivateMessage {
159169
limit: limits.nip17_relays,
160170
},
171+
allowed,
161172
)
162173
.await?;
163174

@@ -191,6 +202,7 @@ impl GossipWrapper {
191202
BestRelaySelection::Read {
192203
limit: limits.read_relays_per_user,
193204
},
205+
allowed,
194206
)
195207
.await?;
196208

@@ -201,6 +213,7 @@ impl GossipWrapper {
201213
BestRelaySelection::Hints {
202214
limit: limits.hint_relays_per_user,
203215
},
216+
allowed,
204217
)
205218
.await?;
206219

@@ -211,6 +224,7 @@ impl GossipWrapper {
211224
BestRelaySelection::MostReceived {
212225
limit: limits.most_used_relays_per_user,
213226
},
227+
allowed,
214228
)
215229
.await?;
216230

@@ -227,6 +241,7 @@ impl GossipWrapper {
227241
BestRelaySelection::PrivateMessage {
228242
limit: limits.nip17_relays,
229243
},
244+
allowed,
230245
)
231246
.await?;
232247

@@ -267,6 +282,7 @@ impl GossipWrapper {
267282
hints: limits.hint_relays_per_user,
268283
most_received: limits.most_used_relays_per_user,
269284
},
285+
allowed,
270286
)
271287
.await?;
272288

@@ -279,6 +295,7 @@ impl GossipWrapper {
279295
BestRelaySelection::PrivateMessage {
280296
limit: limits.nip17_relays,
281297
},
298+
allowed,
282299
)
283300
.await?;
284301

@@ -410,6 +427,7 @@ mod tests {
410427
filter.clone(),
411428
GossipFilterPattern::Nip65,
412429
&GossipRelayLimits::default(),
430+
GossipAllowedRelays::default(),
413431
)
414432
.await
415433
.unwrap()
@@ -430,6 +448,7 @@ mod tests {
430448
authors_filter.clone(),
431449
GossipFilterPattern::Nip65,
432450
&GossipRelayLimits::default(),
451+
GossipAllowedRelays::default(),
433452
)
434453
.await
435454
.unwrap()
@@ -465,6 +484,7 @@ mod tests {
465484
search_filter.clone(),
466485
GossipFilterPattern::Nip65,
467486
&GossipRelayLimits::default(),
487+
GossipAllowedRelays::default(),
468488
)
469489
.await
470490
.unwrap()
@@ -482,6 +502,7 @@ mod tests {
482502
p_tag_filter.clone(),
483503
GossipFilterPattern::Nip65,
484504
&GossipRelayLimits::default(),
505+
GossipAllowedRelays::default(),
485506
)
486507
.await
487508
.unwrap()
@@ -507,6 +528,7 @@ mod tests {
507528
filter.clone(),
508529
GossipFilterPattern::Nip65,
509530
&GossipRelayLimits::default(),
531+
GossipAllowedRelays::default(),
510532
)
511533
.await
512534
.unwrap()
@@ -531,6 +553,7 @@ mod tests {
531553
filter.clone(),
532554
GossipFilterPattern::Nip65,
533555
&GossipRelayLimits::default(),
556+
GossipAllowedRelays::default(),
534557
)
535558
.await
536559
.unwrap()

gossip/nostr-gossip-memory/CHANGELOG.md

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

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

36+
### Added
37+
38+
- Add support for `GossipAllowedRelays` filtering during relay selection (https://github.com/rust-nostr/nostr/pull/1128)
39+
3640
## v0.44.0 - 2025/11/06
3741

3842
First release.

0 commit comments

Comments
 (0)