6
6
7
7
use alloc:: boxed:: Box ;
8
8
use alloc:: string:: { String , ToString } ;
9
+ use core:: fmt;
9
10
10
11
use bitcoin:: secp256k1:: { Secp256k1 , Verification } ;
11
- #[ cfg( feature = "std" ) ]
12
12
use serde:: { Deserialize , Deserializer } ;
13
13
use serde:: { Serialize , Serializer } ;
14
14
use serde_json:: { json, Value } ;
@@ -18,6 +18,70 @@ use super::MessageHandleError;
18
18
use crate :: SECP256K1 ;
19
19
use crate :: { Event , EventId , SubscriptionId } ;
20
20
21
+ /// Negentropy error code
22
+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
23
+ pub enum NegentropyErrorCode {
24
+ /// Results too big
25
+ ResultsTooBig ,
26
+ /// Because the NEG-OPEN queries are stateful, relays may choose to time-out inactive queries to recover memory resources
27
+ Closed ,
28
+ /// If an event ID is used as the filter, this error will be returned if the relay does not have this event.
29
+ /// The client should retry with the full filter, or upload the event to the relay.
30
+ FilterNotFound ,
31
+ /// The event's content was not valid JSON, or the filter was invalid for some other reason.
32
+ FilterInvalid ,
33
+ /// Other
34
+ Other ( String ) ,
35
+ }
36
+
37
+ impl fmt:: Display for NegentropyErrorCode {
38
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
39
+ match self {
40
+ Self :: ResultsTooBig => write ! ( f, "RESULTS_TOO_BIG" ) ,
41
+ Self :: Closed => write ! ( f, "CLOSED" ) ,
42
+ Self :: FilterNotFound => write ! ( f, "FILTER_NOT_FOUND" ) ,
43
+ Self :: FilterInvalid => write ! ( f, "FILTER_INVALID" ) ,
44
+ Self :: Other ( e) => write ! ( f, "{e}" ) ,
45
+ }
46
+ }
47
+ }
48
+
49
+ impl < S > From < S > for NegentropyErrorCode
50
+ where
51
+ S : Into < String > ,
52
+ {
53
+ fn from ( code : S ) -> Self {
54
+ let code: String = code. into ( ) ;
55
+ match code. as_str ( ) {
56
+ "RESULTS_TOO_BIG" => Self :: ResultsTooBig ,
57
+ "CLOSED" => Self :: Closed ,
58
+ "FILTER_NOT_FOUND" => Self :: FilterNotFound ,
59
+ "FILTER_INVALID" => Self :: FilterInvalid ,
60
+ o => Self :: Other ( o. to_string ( ) ) ,
61
+ }
62
+ }
63
+ }
64
+
65
+ impl Serialize for NegentropyErrorCode {
66
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
67
+ where
68
+ S : Serializer ,
69
+ {
70
+ serializer. serialize_str ( & self . to_string ( ) )
71
+ }
72
+ }
73
+
74
+ impl < ' de > Deserialize < ' de > for NegentropyErrorCode {
75
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
76
+ where
77
+ D : Deserializer < ' de > ,
78
+ {
79
+ let value = Value :: deserialize ( deserializer) ?;
80
+ let alphaber: String = serde_json:: from_value ( value) . map_err ( serde:: de:: Error :: custom) ?;
81
+ Ok ( Self :: from ( alphaber) )
82
+ }
83
+ }
84
+
21
85
/// Messages sent by relays, received by clients
22
86
#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
23
87
pub enum RelayMessage {
@@ -63,6 +127,13 @@ pub enum RelayMessage {
63
127
/// Message
64
128
message : String ,
65
129
} ,
130
+ /// Negentropy Error
131
+ NegErr {
132
+ /// Subscription ID
133
+ subscription_id : SubscriptionId ,
134
+ /// Error code
135
+ code : NegentropyErrorCode ,
136
+ } ,
66
137
}
67
138
68
139
impl Serialize for RelayMessage {
@@ -164,6 +235,10 @@ impl RelayMessage {
164
235
subscription_id,
165
236
message,
166
237
} => json ! ( [ "NEG-MSG" , subscription_id, message] ) ,
238
+ Self :: NegErr {
239
+ subscription_id,
240
+ code,
241
+ } => json ! ( [ "NEG-ERR" , subscription_id, code] ) ,
167
242
}
168
243
}
169
244
@@ -291,6 +366,20 @@ impl RelayMessage {
291
366
} ) ;
292
367
}
293
368
369
+ // Negentropy Error
370
+ // ["NEG-ERR", <subscription ID string>, <reason-code>]
371
+ if v[ 0 ] == "NEG-ERR" {
372
+ if v_len != 3 {
373
+ return Err ( MessageHandleError :: InvalidMessageFormat ) ;
374
+ }
375
+ let subscription_id: SubscriptionId = SubscriptionId :: new ( v[ 1 ] . to_string ( ) ) ;
376
+ let code: NegentropyErrorCode = NegentropyErrorCode :: from ( v[ 2 ] . to_string ( ) ) ;
377
+ return Ok ( Self :: NegErr {
378
+ subscription_id,
379
+ code,
380
+ } ) ;
381
+ }
382
+
294
383
Err ( MessageHandleError :: InvalidMessageFormat )
295
384
}
296
385
0 commit comments