Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 0c46dc8

Browse files
committed
Introduce LSPSMethod and track things by RequestId
1 parent 16ebaff commit 0c46dc8

File tree

5 files changed

+175
-87
lines changed

5 files changed

+175
-87
lines changed

src/lsps0/msgs.rs

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
//!
33
//! Please refer to the [LSPS0 specification](https://github.com/BitcoinAndLightningLayerSpecs/lsp/tree/main/LSPS0) for more information.
44
5+
use crate::lsps0::ser::LSPSMethod;
6+
57
#[cfg(lsps1)]
6-
use crate::lsps1::msgs::{
7-
LSPS1Message, LSPS1Request, LSPS1Response, LSPS1_CREATE_ORDER_METHOD_NAME,
8-
LSPS1_GET_INFO_METHOD_NAME, LSPS1_GET_ORDER_METHOD_NAME,
9-
};
10-
use crate::lsps2::msgs::{
11-
LSPS2Message, LSPS2Request, LSPS2Response, LSPS2_BUY_METHOD_NAME, LSPS2_GET_INFO_METHOD_NAME,
12-
};
8+
use crate::lsps1::msgs::{LSPS1Message, LSPS1Request, LSPS1Response};
9+
use crate::lsps2::msgs::{LSPS2Message, LSPS2Request, LSPS2Response};
1310
use crate::prelude::{HashMap, String, ToString, Vec};
1411

1512
use lightning::impl_writeable_msg;
@@ -37,7 +34,7 @@ const JSONRPC_RESULT_FIELD_KEY: &str = "result";
3734
const JSONRPC_ERROR_FIELD_KEY: &str = "error";
3835
const JSONRPC_INVALID_MESSAGE_ERROR_CODE: i32 = -32700;
3936
const JSONRPC_INVALID_MESSAGE_ERROR_MESSAGE: &str = "parse error";
40-
const LSPS0_LISTPROTOCOLS_METHOD_NAME: &str = "lsps0.list_protocols";
37+
pub(crate) const LSPS0_LISTPROTOCOLS_METHOD_NAME: &str = "lsps0.list_protocols";
4138

4239
/// The Lightning message type id for LSPS messages.
4340
pub const LSPS_MESSAGE_TYPE_ID: u16 = 37913;
@@ -74,7 +71,8 @@ impl wire::Type for RawLSPSMessage {
7471
///
7572
/// Please refer to the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#request_object) for
7673
/// more information.
77-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
74+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
75+
#[serde(transparent)]
7876
pub struct RequestId(pub String);
7977

8078
/// An error returned in response to an JSON-RPC request.
@@ -190,26 +188,26 @@ impl LSPSMessage {
190188
///
191189
/// The given `request_id_to_method` associates request ids with method names, as response objects
192190
/// don't carry the latter.
193-
pub fn from_str_with_id_map(
194-
json_str: &str, request_id_to_method_map: &mut HashMap<String, String>,
191+
pub(crate) fn from_str_with_id_map(
192+
json_str: &str, request_id_to_method_map: &mut HashMap<RequestId, LSPSMethod>,
195193
) -> Result<Self, serde_json::Error> {
196194
let deserializer = &mut serde_json::Deserializer::from_str(json_str);
197195
let visitor = LSPSMessageVisitor { request_id_to_method_map };
198196
deserializer.deserialize_any(visitor)
199197
}
200198

201199
/// Returns the request id and the method.
202-
pub fn get_request_id_and_method(&self) -> Option<(String, String)> {
200+
pub(crate) fn get_request_id_and_method(&self) -> Option<(RequestId, LSPSMethod)> {
203201
match self {
204202
LSPSMessage::LSPS0(LSPS0Message::Request(request_id, request)) => {
205-
Some((request_id.0.clone(), request.method().to_string()))
203+
Some((RequestId(request_id.0.clone()), request.into()))
206204
}
207205
#[cfg(lsps1)]
208206
LSPSMessage::LSPS1(LSPS1Message::Request(request_id, request)) => {
209-
Some((request_id.0.clone(), request.method().to_string()))
207+
Some((RequestId(request_id.0.clone()), request.into()))
210208
}
211209
LSPSMessage::LSPS2(LSPS2Message::Request(request_id, request)) => {
212-
Some((request_id.0.clone(), request.method().to_string()))
210+
Some((RequestId(request_id.0.clone()), request.into()))
213211
}
214212
_ => None,
215213
}
@@ -228,8 +226,9 @@ impl Serialize for LSPSMessage {
228226

229227
match self {
230228
LSPSMessage::LSPS0(LSPS0Message::Request(request_id, request)) => {
231-
jsonrpc_object.serialize_field(JSONRPC_METHOD_FIELD_KEY, request.method())?;
232229
jsonrpc_object.serialize_field(JSONRPC_ID_FIELD_KEY, &request_id.0)?;
230+
jsonrpc_object
231+
.serialize_field(JSONRPC_METHOD_FIELD_KEY, &LSPSMethod::from(request))?;
233232

234233
match request {
235234
LSPS0Request::ListProtocols(params) => {
@@ -252,7 +251,8 @@ impl Serialize for LSPSMessage {
252251
#[cfg(lsps1)]
253252
LSPSMessage::LSPS1(LSPS1Message::Request(request_id, request)) => {
254253
jsonrpc_object.serialize_field(JSONRPC_ID_FIELD_KEY, &request_id.0)?;
255-
jsonrpc_object.serialize_field(JSONRPC_METHOD_FIELD_KEY, request.method())?;
254+
jsonrpc_object
255+
.serialize_field(JSONRPC_METHOD_FIELD_KEY, &LSPSMethod::from(request))?;
256256

257257
match request {
258258
LSPS1Request::GetInfo(params) => {
@@ -290,7 +290,8 @@ impl Serialize for LSPSMessage {
290290
}
291291
LSPSMessage::LSPS2(LSPS2Message::Request(request_id, request)) => {
292292
jsonrpc_object.serialize_field(JSONRPC_ID_FIELD_KEY, &request_id.0)?;
293-
jsonrpc_object.serialize_field(JSONRPC_METHOD_FIELD_KEY, request.method())?;
293+
jsonrpc_object
294+
.serialize_field(JSONRPC_METHOD_FIELD_KEY, &LSPSMethod::from(request))?;
294295

295296
match request {
296297
LSPS2Request::GetInfo(params) => {
@@ -336,7 +337,7 @@ impl Serialize for LSPSMessage {
336337
}
337338

338339
struct LSPSMessageVisitor<'a> {
339-
request_id_to_method_map: &'a mut HashMap<String, String>,
340+
request_id_to_method_map: &'a mut HashMap<RequestId, LSPSMethod>,
340341
}
341342

342343
impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
@@ -350,8 +351,8 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
350351
where
351352
A: MapAccess<'de>,
352353
{
353-
let mut id: Option<String> = None;
354-
let mut method: Option<&str> = None;
354+
let mut id: Option<RequestId> = None;
355+
let mut method: Option<LSPSMethod> = None;
355356
let mut params = None;
356357
let mut result = None;
357358
let mut error: Option<ResponseError> = None;
@@ -389,141 +390,134 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
389390

390391
match method {
391392
Some(method) => match method {
392-
LSPS0_LISTPROTOCOLS_METHOD_NAME => Ok(LSPSMessage::LSPS0(LSPS0Message::Request(
393-
RequestId(id),
393+
LSPSMethod::LSPS0ListProtocols => Ok(LSPSMessage::LSPS0(LSPS0Message::Request(
394+
id,
394395
LSPS0Request::ListProtocols(ListProtocolsRequest {}),
395396
))),
396397
#[cfg(lsps1)]
397-
LSPS1_GET_INFO_METHOD_NAME => {
398+
LSPSMethod::LSPS1GetInfo => {
398399
let request = serde_json::from_value(params.unwrap_or(json!({})))
399400
.map_err(de::Error::custom)?;
400401
Ok(LSPSMessage::LSPS1(LSPS1Message::Request(
401-
RequestId(id),
402+
id,
402403
LSPS1Request::GetInfo(request),
403404
)))
404405
}
405406
#[cfg(lsps1)]
406-
LSPS1_CREATE_ORDER_METHOD_NAME => {
407+
LSPSMethod::LSPS1CreateOrder => {
407408
let request = serde_json::from_value(params.unwrap_or(json!({})))
408409
.map_err(de::Error::custom)?;
409410
Ok(LSPSMessage::LSPS1(LSPS1Message::Request(
410-
RequestId(id),
411+
id,
411412
LSPS1Request::CreateOrder(request),
412413
)))
413414
}
414415
#[cfg(lsps1)]
415-
LSPS1_GET_ORDER_METHOD_NAME => {
416+
LSPSMethod::LSPS1GetOrder => {
416417
let request = serde_json::from_value(params.unwrap_or(json!({})))
417418
.map_err(de::Error::custom)?;
418419
Ok(LSPSMessage::LSPS1(LSPS1Message::Request(
419-
RequestId(id),
420+
id,
420421
LSPS1Request::GetOrder(request),
421422
)))
422423
}
423-
LSPS2_GET_INFO_METHOD_NAME => {
424+
LSPSMethod::LSPS2GetInfo => {
424425
let request = serde_json::from_value(params.unwrap_or(json!({})))
425426
.map_err(de::Error::custom)?;
426427
Ok(LSPSMessage::LSPS2(LSPS2Message::Request(
427-
RequestId(id),
428+
id,
428429
LSPS2Request::GetInfo(request),
429430
)))
430431
}
431-
LSPS2_BUY_METHOD_NAME => {
432+
LSPSMethod::LSPS2Buy => {
432433
let request = serde_json::from_value(params.unwrap_or(json!({})))
433434
.map_err(de::Error::custom)?;
434-
Ok(LSPSMessage::LSPS2(LSPS2Message::Request(
435-
RequestId(id),
436-
LSPS2Request::Buy(request),
437-
)))
435+
Ok(LSPSMessage::LSPS2(LSPS2Message::Request(id, LSPS2Request::Buy(request))))
438436
}
439-
_ => Err(de::Error::custom(format!(
440-
"Received request with unknown method: {}",
441-
method
442-
))),
443437
},
444438
None => match self.request_id_to_method_map.remove(&id) {
445-
Some(method) => match method.as_str() {
446-
LSPS0_LISTPROTOCOLS_METHOD_NAME => {
439+
Some(method) => match method {
440+
LSPSMethod::LSPS0ListProtocols => {
447441
if let Some(error) = error {
448442
Ok(LSPSMessage::LSPS0(LSPS0Message::Response(
449-
RequestId(id),
443+
id,
450444
LSPS0Response::ListProtocolsError(error),
451445
)))
452446
} else if let Some(result) = result {
453447
let list_protocols_response =
454448
serde_json::from_value(result).map_err(de::Error::custom)?;
455449
Ok(LSPSMessage::LSPS0(LSPS0Message::Response(
456-
RequestId(id),
450+
id,
457451
LSPS0Response::ListProtocols(list_protocols_response),
458452
)))
459453
} else {
460454
Err(de::Error::custom("Received invalid JSON-RPC object: one of method, result, or error required"))
461455
}
462456
}
463457
#[cfg(lsps1)]
464-
LSPS1_CREATE_ORDER_METHOD_NAME => {
458+
LSPSMethod::LSPS1CreateOrder => {
465459
if let Some(error) = error {
466460
Ok(LSPSMessage::LSPS1(LSPS1Message::Response(
467-
RequestId(id),
461+
id,
468462
LSPS1Response::CreateOrderError(error),
469463
)))
470464
} else if let Some(result) = result {
471465
let response =
472466
serde_json::from_value(result).map_err(de::Error::custom)?;
473467
Ok(LSPSMessage::LSPS1(LSPS1Message::Response(
474-
RequestId(id),
468+
id,
475469
LSPS1Response::CreateOrder(response),
476470
)))
477471
} else {
478472
Err(de::Error::custom("Received invalid JSON-RPC object: one of method, result, or error required"))
479473
}
480474
}
481475
#[cfg(lsps1)]
482-
LSPS1_GET_ORDER_METHOD_NAME => {
476+
LSPSMethod::LSPS1GetOrder => {
483477
if let Some(error) = error {
484478
Ok(LSPSMessage::LSPS1(LSPS1Message::Response(
485-
RequestId(id),
479+
id,
486480
LSPS1Response::GetOrderError(error),
487481
)))
488482
} else if let Some(result) = result {
489483
let response =
490484
serde_json::from_value(result).map_err(de::Error::custom)?;
491485
Ok(LSPSMessage::LSPS1(LSPS1Message::Response(
492-
RequestId(id),
486+
id,
493487
LSPS1Response::GetOrder(response),
494488
)))
495489
} else {
496490
Err(de::Error::custom("Received invalid JSON-RPC object: one of method, result, or error required"))
497491
}
498492
}
499-
LSPS2_GET_INFO_METHOD_NAME => {
493+
LSPSMethod::LSPS2GetInfo => {
500494
if let Some(error) = error {
501495
Ok(LSPSMessage::LSPS2(LSPS2Message::Response(
502-
RequestId(id),
496+
id,
503497
LSPS2Response::GetInfoError(error),
504498
)))
505499
} else if let Some(result) = result {
506500
let response =
507501
serde_json::from_value(result).map_err(de::Error::custom)?;
508502
Ok(LSPSMessage::LSPS2(LSPS2Message::Response(
509-
RequestId(id),
503+
id,
510504
LSPS2Response::GetInfo(response),
511505
)))
512506
} else {
513507
Err(de::Error::custom("Received invalid JSON-RPC object: one of method, result, or error required"))
514508
}
515509
}
516-
LSPS2_BUY_METHOD_NAME => {
510+
LSPSMethod::LSPS2Buy => {
517511
if let Some(error) = error {
518512
Ok(LSPSMessage::LSPS2(LSPS2Message::Response(
519-
RequestId(id),
513+
id,
520514
LSPS2Response::BuyError(error),
521515
)))
522516
} else if let Some(result) = result {
523517
let response =
524518
serde_json::from_value(result).map_err(de::Error::custom)?;
525519
Ok(LSPSMessage::LSPS2(LSPS2Message::Response(
526-
RequestId(id),
520+
id,
527521
LSPS2Response::Buy(response),
528522
)))
529523
} else {
@@ -537,7 +531,7 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
537531
},
538532
None => Err(de::Error::custom(format!(
539533
"Received response for unknown request id: {}",
540-
id
534+
id.0
541535
))),
542536
},
543537
}
@@ -579,7 +573,7 @@ mod tests {
579573
let json = serde_json::to_string(&request).unwrap();
580574
assert_eq!(
581575
json,
582-
r#"{"jsonrpc":"2.0","method":"lsps0.list_protocols","id":"request:id:xyz123","params":{}}"#
576+
r#"{"jsonrpc":"2.0","id":"request:id:xyz123","method":"lsps0.list_protocols","params":{}}"#
583577
);
584578
}
585579

@@ -594,7 +588,7 @@ mod tests {
594588
}"#;
595589
let mut request_id_to_method_map = HashMap::new();
596590
request_id_to_method_map
597-
.insert("request:id:xyz123".to_string(), "lsps0.list_protocols".to_string());
591+
.insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
598592

599593
let response =
600594
LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map).unwrap();
@@ -620,7 +614,7 @@ mod tests {
620614
}"#;
621615
let mut request_id_to_method_map = HashMap::new();
622616
request_id_to_method_map
623-
.insert("request:id:xyz123".to_string(), "lsps0.list_protocols".to_string());
617+
.insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
624618

625619
let response =
626620
LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map).unwrap();
@@ -649,7 +643,7 @@ mod tests {
649643
}"#;
650644
let mut request_id_to_method_map = HashMap::new();
651645
request_id_to_method_map
652-
.insert("request:id:xyz123".to_string(), "lsps0.list_protocols".to_string());
646+
.insert(RequestId("request:id:xyz123".to_string()), LSPSMethod::LSPS0ListProtocols);
653647

654648
let response = LSPSMessage::from_str_with_id_map(json, &mut request_id_to_method_map);
655649
assert!(response.is_err());

0 commit comments

Comments
 (0)