-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathmsgs.rs
More file actions
228 lines (199 loc) · 6.41 KB
/
msgs.rs
File metadata and controls
228 lines (199 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! Message, request, and other primitive types used to implement LSPS0.
use alloc::vec::Vec;
use core::convert::TryFrom;
use crate::lsps0::ser::{LSPSMessage, LSPSRequestId, LSPSResponseError};
use serde::{Deserialize, Serialize};
pub(crate) const LSPS0_LISTPROTOCOLS_METHOD_NAME: &str = "lsps0.list_protocols";
/// A `list_protocols` request.
///
/// Please refer to the [bLIP-50 / LSPS0
/// specification](https://github.com/lightning/blips/blob/master/blip-0050.md#lsps-specification-support-query)
/// for more information.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
pub struct LSPS0ListProtocolsRequest {}
/// A response to a `list_protocols` request.
///
/// Please refer to the [bLIP-50 / LSPS0
/// specification](https://github.com/lightning/blips/blob/master/blip-0050.md#lsps-specification-support-query)
/// for more information.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct LSPS0ListProtocolsResponse {
/// A list of supported protocols.
pub protocols: Vec<u16>,
}
/// An bLIP-50 / LSPS0 protocol request.
///
/// Please refer to the [bLIP-50 / LSPS0
/// specification](https://github.com/lightning/blips/blob/master/blip-0050.md#lsps-specification-support-query)
/// for more information.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LSPS0Request {
/// A request calling `list_protocols`.
ListProtocols(LSPS0ListProtocolsRequest),
}
impl LSPS0Request {
/// Returns the method name associated with the given request variant.
pub fn method(&self) -> &'static str {
match self {
LSPS0Request::ListProtocols(_) => LSPS0_LISTPROTOCOLS_METHOD_NAME,
}
}
}
/// An bLIP-50 / LSPS0 protocol request.
///
/// Please refer to the [bLIP-50 / LSPS0
/// specification](https://github.com/lightning/blips/blob/master/blip-0050.md#lsps-specification-support-query)
/// for more information.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LSPS0Response {
/// A response to a `list_protocols` request.
ListProtocols(LSPS0ListProtocolsResponse),
/// An error response to a `list_protocols` request.
ListProtocolsError(LSPSResponseError),
}
/// An bLIP-50 / LSPS0 protocol message.
///
/// Please refer to the [bLIP-50 / LSPS0
/// specification](https://github.com/lightning/blips/blob/master/blip-0050.md#lsps-specification-support-query)
/// for more information.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LSPS0Message {
/// A request variant.
Request(LSPSRequestId, LSPS0Request),
/// A response variant.
Response(LSPSRequestId, LSPS0Response),
}
impl TryFrom<LSPSMessage> for LSPS0Message {
type Error = ();
fn try_from(message: LSPSMessage) -> Result<Self, Self::Error> {
match message {
LSPSMessage::Invalid(_) => Err(()),
LSPSMessage::LSPS0(message) => Ok(message),
LSPSMessage::LSPS1(_) => Err(()),
LSPSMessage::LSPS2(_) => Err(()),
LSPSMessage::LSPS5(_) => Err(()),
LSPSMessage::LSPS7(_) => Err(()),
}
}
}
impl From<LSPS0Message> for LSPSMessage {
fn from(message: LSPS0Message) -> Self {
LSPSMessage::LSPS0(message)
}
}
#[cfg(test)]
mod tests {
use lightning::util::hash_tables::new_hash_map;
use super::*;
use crate::lsps0::ser::LSPSMethod;
use alloc::string::ToString;
#[test]
fn deserializes_request() {
let json = r#"{
"jsonrpc": "2.0",
"id": "request:id:xyz123",
"method": "lsps0.list_protocols"
}"#;
let mut request_id_method_map = new_hash_map();
let msg = LSPSMessage::from_str_with_id_map(json, &mut request_id_method_map);
assert!(msg.is_ok());
let msg = msg.unwrap();
assert_eq!(
msg,
LSPSMessage::LSPS0(LSPS0Message::Request(
LSPSRequestId("request:id:xyz123".to_string()),
LSPS0Request::ListProtocols(LSPS0ListProtocolsRequest {})
))
);
}
#[test]
fn serializes_request() {
let request = LSPSMessage::LSPS0(LSPS0Message::Request(
LSPSRequestId("request:id:xyz123".to_string()),
LSPS0Request::ListProtocols(LSPS0ListProtocolsRequest {}),
));
let json = serde_json::to_string(&request).unwrap();
assert_eq!(
json,
r#"{"jsonrpc":"2.0","id":"request:id:xyz123","method":"lsps0.list_protocols","params":{}}"#
);
}
#[test]
fn deserializes_success_response() {
let json = r#"{
"jsonrpc": "2.0",
"id": "request:id:xyz123",
"result": {
"protocols": [1,2,3]
}
}"#;
let mut request_id_to_method_map = new_hash_map();
request_id_to_method_map
.insert(LSPSRequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
let response =
LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map).unwrap();
assert_eq!(
response,
LSPSMessage::LSPS0(LSPS0Message::Response(
LSPSRequestId("request:id:xyz123".to_string()),
LSPS0Response::ListProtocols(LSPS0ListProtocolsResponse {
protocols: vec![1, 2, 3]
})
))
);
}
#[test]
fn deserializes_error_response() {
let json = r#"{
"jsonrpc": "2.0",
"id": "request:id:xyz123",
"error": {
"code": -32617,
"message": "Unknown Error"
}
}"#;
let mut request_id_to_method_map = new_hash_map();
request_id_to_method_map
.insert(LSPSRequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
let response =
LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map).unwrap();
assert_eq!(
response,
LSPSMessage::LSPS0(LSPS0Message::Response(
LSPSRequestId("request:id:xyz123".to_string()),
LSPS0Response::ListProtocolsError(LSPSResponseError {
code: -32617,
message: "Unknown Error".to_string(),
data: None
})
))
);
}
#[test]
fn deserialize_fails_with_unknown_request_id() {
let json = r#"{
"jsonrpc": "2.0",
"id": "request:id:xyz124",
"result": {
"protocols": [1,2,3]
}
}"#;
let mut request_id_to_method_map = new_hash_map();
request_id_to_method_map
.insert(LSPSRequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
let response = LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map);
assert!(response.is_err());
}
#[test]
fn serializes_response() {
let response = LSPSMessage::LSPS0(LSPS0Message::Response(
LSPSRequestId("request:id:xyz123".to_string()),
LSPS0Response::ListProtocols(LSPS0ListProtocolsResponse { protocols: vec![1, 2, 3] }),
));
let json = serde_json::to_string(&response).unwrap();
assert_eq!(
json,
r#"{"jsonrpc":"2.0","id":"request:id:xyz123","result":{"protocols":[1,2,3]}}"#
);
}
}