@@ -9,10 +9,10 @@ use std::sync::Arc;
99use nostr:: prelude:: * ;
1010use nostr_gossip:: { BestRelaySelection , NostrGossip } ;
1111
12+ use crate :: client:: options:: GossipRelayLimits ;
1213use crate :: client:: Error ;
1314
1415const P_TAG : SingleLetterTag = SingleLetterTag :: lowercase ( Alphabet :: P ) ;
15- const MAX_NIP17_RELAYS : usize = 3 ;
1616
1717#[ derive( Debug ) ]
1818pub enum BrokenDownFilters {
@@ -94,6 +94,7 @@ impl GossipWrapper {
9494 & self ,
9595 filter : Filter ,
9696 pattern : GossipFilterPattern ,
97+ limits : & GossipRelayLimits ,
9798 ) -> Result < BrokenDownFilters , Error > {
9899 // Extract `p` tag from generic tags and parse public key hex
99100 let p_tag: Option < BTreeSet < PublicKey > > = filter. generic_tags . get ( & P_TAG ) . map ( |s| {
@@ -107,17 +108,32 @@ impl GossipWrapper {
107108 ( Some ( authors) , None ) => {
108109 // Get map of write relays
109110 let mut outbox: HashMap < RelayUrl , BTreeSet < PublicKey > > = self
110- . map_relays ( authors, BestRelaySelection :: Write { limit : 2 } )
111+ . map_relays (
112+ authors,
113+ BestRelaySelection :: Write {
114+ limit : limits. write_relays_per_user ,
115+ } ,
116+ )
111117 . await ?;
112118
113119 // Get map of hints relays
114120 let hints: HashMap < RelayUrl , BTreeSet < PublicKey > > = self
115- . map_relays ( authors, BestRelaySelection :: Hints { limit : 1 } )
121+ . map_relays (
122+ authors,
123+ BestRelaySelection :: Hints {
124+ limit : limits. hint_relays_per_user ,
125+ } ,
126+ )
116127 . await ?;
117128
118129 // Get map of relays that received more events
119130 let most_received: HashMap < RelayUrl , BTreeSet < PublicKey > > = self
120- . map_relays ( authors, BestRelaySelection :: MostReceived { limit : 1 } )
131+ . map_relays (
132+ authors,
133+ BestRelaySelection :: MostReceived {
134+ limit : limits. most_used_relays_per_user ,
135+ } ,
136+ )
121137 . await ?;
122138
123139 // Extend with hints and most received
@@ -130,7 +146,7 @@ impl GossipWrapper {
130146 . map_relays (
131147 authors,
132148 BestRelaySelection :: PrivateMessage {
133- limit : MAX_NIP17_RELAYS ,
149+ limit : limits . nip17_relays ,
134150 } ,
135151 )
136152 . await ?;
@@ -160,17 +176,32 @@ impl GossipWrapper {
160176 ( None , Some ( p_public_keys) ) => {
161177 // Get map of inbox relays
162178 let mut inbox: HashMap < RelayUrl , BTreeSet < PublicKey > > = self
163- . map_relays ( p_public_keys, BestRelaySelection :: Read { limit : 2 } )
179+ . map_relays (
180+ p_public_keys,
181+ BestRelaySelection :: Read {
182+ limit : limits. read_relays_per_user ,
183+ } ,
184+ )
164185 . await ?;
165186
166187 // Get map of hints relays
167188 let hints: HashMap < RelayUrl , BTreeSet < PublicKey > > = self
168- . map_relays ( p_public_keys, BestRelaySelection :: Hints { limit : 1 } )
189+ . map_relays (
190+ p_public_keys,
191+ BestRelaySelection :: Hints {
192+ limit : limits. hint_relays_per_user ,
193+ } ,
194+ )
169195 . await ?;
170196
171197 // Get map of relays that received more events
172198 let most_received: HashMap < RelayUrl , BTreeSet < PublicKey > > = self
173- . map_relays ( p_public_keys, BestRelaySelection :: MostReceived { limit : 1 } )
199+ . map_relays (
200+ p_public_keys,
201+ BestRelaySelection :: MostReceived {
202+ limit : limits. most_used_relays_per_user ,
203+ } ,
204+ )
174205 . await ?;
175206
176207 // Extend with hints and most received
@@ -184,7 +215,7 @@ impl GossipWrapper {
184215 . map_relays (
185216 p_public_keys,
186217 BestRelaySelection :: PrivateMessage {
187- limit : MAX_NIP17_RELAYS ,
218+ limit : limits . nip17_relays ,
188219 } ,
189220 )
190221 . await ?;
@@ -221,10 +252,10 @@ impl GossipWrapper {
221252 . get_relays (
222253 union. iter ( ) ,
223254 BestRelaySelection :: All {
224- read : 2 ,
225- write : 2 ,
226- hints : 1 ,
227- most_received : 1 ,
255+ read : limits . read_relays_per_user ,
256+ write : limits . write_relays_per_user ,
257+ hints : limits . hint_relays_per_user ,
258+ most_received : limits . most_used_relays_per_user ,
228259 } ,
229260 )
230261 . await ?;
@@ -236,7 +267,7 @@ impl GossipWrapper {
236267 . get_relays (
237268 union. iter ( ) ,
238269 BestRelaySelection :: PrivateMessage {
239- limit : MAX_NIP17_RELAYS ,
270+ limit : limits . nip17_relays ,
240271 } ,
241272 )
242273 . await ?;
@@ -365,7 +396,11 @@ mod tests {
365396 // Single author
366397 let filter = Filter :: new ( ) . author ( keys_a. public_key ) ;
367398 match gossip
368- . break_down_filter ( filter. clone ( ) , GossipFilterPattern :: Nip65 )
399+ . break_down_filter (
400+ filter. clone ( ) ,
401+ GossipFilterPattern :: Nip65 ,
402+ & GossipRelayLimits :: default ( ) ,
403+ )
369404 . await
370405 . unwrap ( )
371406 {
@@ -381,7 +416,11 @@ mod tests {
381416 // Multiple authors
382417 let authors_filter = Filter :: new ( ) . authors ( [ keys_a. public_key , keys_b. public_key ] ) ;
383418 match gossip
384- . break_down_filter ( authors_filter. clone ( ) , GossipFilterPattern :: Nip65 )
419+ . break_down_filter (
420+ authors_filter. clone ( ) ,
421+ GossipFilterPattern :: Nip65 ,
422+ & GossipRelayLimits :: default ( ) ,
423+ )
385424 . await
386425 . unwrap ( )
387426 {
@@ -412,7 +451,11 @@ mod tests {
412451 // Other filter
413452 let search_filter = Filter :: new ( ) . search ( "Test" ) . limit ( 10 ) ;
414453 match gossip
415- . break_down_filter ( search_filter. clone ( ) , GossipFilterPattern :: Nip65 )
454+ . break_down_filter (
455+ search_filter. clone ( ) ,
456+ GossipFilterPattern :: Nip65 ,
457+ & GossipRelayLimits :: default ( ) ,
458+ )
416459 . await
417460 . unwrap ( )
418461 {
@@ -425,7 +468,11 @@ mod tests {
425468 // Single p tags
426469 let p_tag_filter = Filter :: new ( ) . pubkey ( keys_a. public_key ) ;
427470 match gossip
428- . break_down_filter ( p_tag_filter. clone ( ) , GossipFilterPattern :: Nip65 )
471+ . break_down_filter (
472+ p_tag_filter. clone ( ) ,
473+ GossipFilterPattern :: Nip65 ,
474+ & GossipRelayLimits :: default ( ) ,
475+ )
429476 . await
430477 . unwrap ( )
431478 {
@@ -446,7 +493,11 @@ mod tests {
446493 . author ( keys_a. public_key )
447494 . pubkey ( keys_b. public_key ) ;
448495 match gossip
449- . break_down_filter ( filter. clone ( ) , GossipFilterPattern :: Nip65 )
496+ . break_down_filter (
497+ filter. clone ( ) ,
498+ GossipFilterPattern :: Nip65 ,
499+ & GossipRelayLimits :: default ( ) ,
500+ )
450501 . await
451502 . unwrap ( )
452503 {
@@ -466,7 +517,11 @@ mod tests {
466517 let random_keys = Keys :: generate ( ) ;
467518 let filter = Filter :: new ( ) . author ( random_keys. public_key ) ;
468519 match gossip
469- . break_down_filter ( filter. clone ( ) , GossipFilterPattern :: Nip65 )
520+ . break_down_filter (
521+ filter. clone ( ) ,
522+ GossipFilterPattern :: Nip65 ,
523+ & GossipRelayLimits :: default ( ) ,
524+ )
470525 . await
471526 . unwrap ( )
472527 {
0 commit comments