44
55//! Relay limits
66
7+ use std:: collections:: HashMap ;
8+
9+ use nostr:: Kind ;
10+
11+ const MAX_EVENT_SIZE : u32 = 70_000 ; // bytes
12+ const MAX_CONTACT_LIST_EVENT_SIZE : u32 = 840_000 ; // bytes
13+
714/// Relay limits
8- #[ derive( Debug , Clone , Copy , Default , PartialEq , Eq ) ]
15+ #[ derive( Debug , Clone , Default , PartialEq , Eq ) ]
916pub struct RelayLimits {
1017 /// Message limits
1118 pub messages : RelayMessageLimits ,
@@ -48,19 +55,31 @@ impl RelayMessageLimits {
4855}
4956
5057/// Events limits
51- #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
58+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
5259pub struct RelayEventLimits {
5360 /// Maximum size of normalised JSON, in bytes (default: 70_000)
5461 pub max_size : Option < u32 > ,
62+ /// Maximum size of normalized JSON per [Kind], in bytes.
63+ pub max_size_per_kind : HashMap < Kind , Option < u32 > > ,
5564 /// Maximum number of tags allowed (default: 2_000)
5665 pub max_num_tags : Option < u16 > ,
66+ /// Maximum number of tags allowed per [Kind].
67+ pub max_num_tags_per_kind : HashMap < Kind , Option < u16 > > ,
5768}
5869
5970impl Default for RelayEventLimits {
6071 fn default ( ) -> Self {
72+ let mut max_size_per_kind: HashMap < Kind , Option < u32 > > = HashMap :: with_capacity ( 1 ) ;
73+ max_size_per_kind. insert ( Kind :: ContactList , Some ( MAX_CONTACT_LIST_EVENT_SIZE ) ) ;
74+
75+ let mut max_num_tags_per_kind: HashMap < Kind , Option < u16 > > = HashMap :: with_capacity ( 1 ) ;
76+ max_num_tags_per_kind. insert ( Kind :: ContactList , Some ( 10_000 ) ) ;
77+
6178 Self {
62- max_size : Some ( 70_000 ) ,
79+ max_size : Some ( MAX_EVENT_SIZE ) ,
80+ max_size_per_kind,
6381 max_num_tags : Some ( 2_000 ) ,
82+ max_num_tags_per_kind,
6483 }
6584 }
6685}
@@ -71,7 +90,60 @@ impl RelayEventLimits {
7190 pub fn disable ( ) -> Self {
7291 Self {
7392 max_size : None ,
93+ max_size_per_kind : HashMap :: new ( ) ,
7494 max_num_tags : None ,
95+ max_num_tags_per_kind : HashMap :: new ( ) ,
7596 }
7697 }
98+
99+ /// Add/Edit max size per [Kind]
100+ pub fn set_max_size_per_kind ( mut self , kind : Kind , max_size : Option < u32 > ) -> Self {
101+ self . max_size_per_kind . insert ( kind, max_size) ;
102+ self
103+ }
104+
105+ /// Add/Edit max number of tags per [Kind]
106+ pub fn set_max_num_tags_per_kind ( mut self , kind : Kind , max_num_tags : Option < u16 > ) -> Self {
107+ self . max_num_tags_per_kind . insert ( kind, max_num_tags) ;
108+ self
109+ }
110+
111+ /// Get max size for [Kind]
112+ ///
113+ /// Fallback to `max_size` if no limit is specified for [Kind]
114+ pub fn get_max_size ( & self , kind : & Kind ) -> Option < u32 > {
115+ match self . max_size_per_kind . get ( kind) . copied ( ) {
116+ Some ( limit) => limit,
117+ None => self . max_size ,
118+ }
119+ }
120+
121+ /// Get max number of tags allowed for [Kind]
122+ ///
123+ /// Fallback to `max_num_tags` if no limit is specified for [Kind]
124+ pub fn get_max_num_tags ( & self , kind : & Kind ) -> Option < u16 > {
125+ match self . max_num_tags_per_kind . get ( kind) . copied ( ) {
126+ Some ( limit) => limit,
127+ None => self . max_num_tags ,
128+ }
129+ }
130+ }
131+
132+ #[ cfg( test) ]
133+ mod tests {
134+ use super :: * ;
135+
136+ #[ test]
137+ fn test_event_limits_get_max_size ( ) {
138+ let limits = RelayLimits :: default ( ) ;
139+
140+ assert_eq ! (
141+ limits. events. get_max_size( & Kind :: TextNote ) ,
142+ Some ( MAX_EVENT_SIZE )
143+ ) ;
144+ assert_eq ! (
145+ limits. events. get_max_size( & Kind :: ContactList ) ,
146+ Some ( MAX_CONTACT_LIST_EVENT_SIZE )
147+ ) ;
148+ }
77149}
0 commit comments