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

Commit e879e30

Browse files
committed
Properly parse Invalid error rather than erroring
... and sending an `Invalid` error in return...
1 parent 210a6e6 commit e879e30

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

src/lsps0/msgs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl TryFrom<LSPSMessage> for LSPS0Message {
7474

7575
fn try_from(message: LSPSMessage) -> Result<Self, Self::Error> {
7676
match message {
77-
LSPSMessage::Invalid => Err(()),
77+
LSPSMessage::Invalid(_) => Err(()),
7878
LSPSMessage::LSPS0(message) => Ok(message),
7979
#[cfg(lsps1)]
8080
LSPSMessage::LSPS1(_) => Err(()),

src/lsps0/ser.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub struct ResponseError {
194194
#[derive(Clone, Debug, PartialEq, Eq)]
195195
pub enum LSPSMessage {
196196
/// An invalid variant.
197-
Invalid,
197+
Invalid(ResponseError),
198198
/// An LSPS0 message.
199199
LSPS0(LSPS0Message),
200200
/// An LSPS1 message.
@@ -344,13 +344,7 @@ impl Serialize for LSPSMessage {
344344
}
345345
}
346346
}
347-
LSPSMessage::Invalid => {
348-
let error = ResponseError {
349-
code: JSONRPC_INVALID_MESSAGE_ERROR_CODE,
350-
message: JSONRPC_INVALID_MESSAGE_ERROR_MESSAGE.to_string(),
351-
data: None,
352-
};
353-
347+
LSPSMessage::Invalid(error) => {
354348
jsonrpc_object.serialize_field(JSONRPC_ID_FIELD_KEY, &serde_json::Value::Null)?;
355349
jsonrpc_object.serialize_field(JSONRPC_ERROR_FIELD_KEY, &error)?;
356350
}
@@ -404,13 +398,25 @@ impl<'de, 'a> Visitor<'de> for LSPSMessageVisitor<'a> {
404398
}
405399
}
406400

407-
let id = id.ok_or_else(|| {
408-
if let Some(method) = method {
409-
de::Error::custom(format!("Received unknown notification: {}", method))
410-
} else {
411-
de::Error::custom("Received invalid JSON-RPC object: one of method or id required")
401+
let id = match id {
402+
Some(id) => id,
403+
None => {
404+
if let Some(method) = method {
405+
return Err(de::Error::custom(format!(
406+
"Received unknown notification: {}",
407+
method
408+
)));
409+
} else {
410+
if let Some(error) = error {
411+
if error.code == JSONRPC_INVALID_MESSAGE_ERROR_CODE {
412+
return Ok(LSPSMessage::Invalid(error));
413+
}
414+
}
415+
416+
return Err(de::Error::custom("Received unknown error message"));
417+
}
412418
}
413-
})?;
419+
};
414420

415421
match method {
416422
Some(method) => match method {

src/manager.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::events::{Event, EventQueue};
22
use crate::lsps0::client::LSPS0ClientHandler;
33
use crate::lsps0::msgs::LSPS0Message;
44
use crate::lsps0::ser::{
5-
LSPSMessage, LSPSMethod, ProtocolMessageHandler, RawLSPSMessage, RequestId,
5+
LSPSMessage, LSPSMethod, ProtocolMessageHandler, RawLSPSMessage, RequestId, ResponseError,
6+
JSONRPC_INVALID_MESSAGE_ERROR_CODE, JSONRPC_INVALID_MESSAGE_ERROR_MESSAGE,
67
LSPS_MESSAGE_TYPE_ID,
78
};
89
use crate::lsps0::service::LSPS0ServiceHandler;
@@ -18,7 +19,7 @@ use crate::lsps1::service::{LSPS1ServiceConfig, LSPS1ServiceHandler};
1819
use crate::lsps2::client::{LSPS2ClientConfig, LSPS2ClientHandler};
1920
use crate::lsps2::msgs::LSPS2Message;
2021
use crate::lsps2::service::{LSPS2ServiceConfig, LSPS2ServiceHandler};
21-
use crate::prelude::{HashMap, Vec};
22+
use crate::prelude::{HashMap, ToString, Vec};
2223
use crate::sync::{Arc, Mutex, RwLock};
2324

2425
use lightning::chain::{self, BestBlock, Confirm, Filter, Listen};
@@ -330,7 +331,7 @@ where {
330331
&self, msg: LSPSMessage, sender_node_id: &PublicKey,
331332
) -> Result<(), lightning::ln::msgs::LightningError> {
332333
match msg {
333-
LSPSMessage::Invalid => {
334+
LSPSMessage::Invalid(_error) => {
334335
return Err(LightningError { err: format!("{} did not understand a message we previously sent, maybe they don't support a protocol we are trying to use?", sender_node_id), action: ErrorAction::IgnoreAndLog(Level::Error)});
335336
}
336337
LSPSMessage::LSPS0(msg @ LSPS0Message::Response(..)) => {
@@ -422,7 +423,13 @@ where
422423
let mut request_id_to_method_map = self.request_id_to_method_map.lock().unwrap();
423424
LSPSMessage::from_str_with_id_map(&msg.payload, &mut request_id_to_method_map).map_err(
424425
|_| {
425-
self.pending_messages.enqueue(sender_node_id, LSPSMessage::Invalid);
426+
let error = ResponseError {
427+
code: JSONRPC_INVALID_MESSAGE_ERROR_CODE,
428+
message: JSONRPC_INVALID_MESSAGE_ERROR_MESSAGE.to_string(),
429+
data: None,
430+
};
431+
432+
self.pending_messages.enqueue(sender_node_id, LSPSMessage::Invalid(error));
426433
let err = format!("Failed to deserialize invalid LSPS message.");
427434
let err_msg =
428435
Some(ErrorMessage { channel_id: ChannelId([0; 32]), data: err.clone() });

0 commit comments

Comments
 (0)