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

Commit 8e06b0d

Browse files
committed
Ignore rather than disconnect peers who send us bogus data
Disconnecting might be unfortunate if we have open channels with the counterparty. If they send us bogus data, we now just add them to a (currently unpersisted) ignore list. We should in the future figure out when to drop them from this list, so that peers have the chance to recover.
1 parent 0bc7203 commit 8e06b0d

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/manager.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ use crate::lsps1::service::{LSPS1ServiceConfig, LSPS1ServiceHandler};
1919
use crate::lsps2::client::{LSPS2ClientConfig, LSPS2ClientHandler};
2020
use crate::lsps2::msgs::LSPS2Message;
2121
use crate::lsps2::service::{LSPS2ServiceConfig, LSPS2ServiceHandler};
22-
use crate::prelude::{HashMap, ToString, Vec};
22+
use crate::prelude::{HashMap, HashSet, ToString, Vec};
2323
use crate::sync::{Arc, Mutex, RwLock};
2424

2525
use lightning::chain::{self, BestBlock, Confirm, Filter, Listen};
2626
use lightning::ln::channelmanager::{AChannelManager, ChainParameters};
2727
use lightning::ln::features::{InitFeatures, NodeFeatures};
28-
use lightning::ln::msgs::{ErrorAction, ErrorMessage, LightningError};
28+
use lightning::ln::msgs::{ErrorAction, LightningError};
2929
use lightning::ln::peer_handler::CustomMessageHandler;
3030
use lightning::ln::wire::CustomMessageReader;
31-
use lightning::ln::ChannelId;
3231
use lightning::sign::EntropySource;
3332
use lightning::util::logger::Level;
3433
use lightning::util::ser::Readable;
@@ -94,6 +93,8 @@ where
9493
pending_messages: Arc<MessageQueue>,
9594
pending_events: Arc<EventQueue>,
9695
request_id_to_method_map: Mutex<HashMap<RequestId, LSPSMethod>>,
96+
// We ignore peers if they send us bogus data.
97+
ignored_peers: RwLock<HashSet<PublicKey>>,
9798
lsps0_client_handler: LSPS0ClientHandler<ES>,
9899
lsps0_service_handler: Option<LSPS0ServiceHandler>,
99100
#[cfg(lsps1)]
@@ -126,6 +127,7 @@ where
126127
where {
127128
let pending_messages = Arc::new(MessageQueue::new());
128129
let pending_events = Arc::new(EventQueue::new());
130+
let ignored_peers = RwLock::new(HashSet::new());
129131

130132
let lsps0_client_handler = LSPS0ClientHandler::new(
131133
entropy_source.clone(),
@@ -192,6 +194,7 @@ where {
192194
pending_messages,
193195
pending_events,
194196
request_id_to_method_map: Mutex::new(HashMap::new()),
197+
ignored_peers,
195198
lsps0_client_handler,
196199
lsps0_service_handler,
197200
#[cfg(lsps1)]
@@ -480,6 +483,16 @@ where
480483
fn handle_custom_message(
481484
&self, msg: Self::CustomMessage, sender_node_id: &PublicKey,
482485
) -> Result<(), lightning::ln::msgs::LightningError> {
486+
{
487+
if self.ignored_peers.read().unwrap().contains(&sender_node_id) {
488+
let err = format!("Ignoring message from peer {}.", sender_node_id);
489+
return Err(LightningError {
490+
err,
491+
action: ErrorAction::IgnoreAndLog(Level::Trace),
492+
});
493+
}
494+
}
495+
483496
let message = {
484497
{
485498
let mut request_id_to_method_map = self.request_id_to_method_map.lock().unwrap();
@@ -493,10 +506,12 @@ where
493506
};
494507

495508
self.pending_messages.enqueue(sender_node_id, LSPSMessage::Invalid(error));
496-
let err = format!("Failed to deserialize invalid LSPS message.");
497-
let err_msg =
498-
Some(ErrorMessage { channel_id: ChannelId([0; 32]), data: err.clone() });
499-
LightningError { err, action: ErrorAction::DisconnectPeer { msg: err_msg } }
509+
self.ignored_peers.write().unwrap().insert(*sender_node_id);
510+
let err = format!(
511+
"Failed to deserialize invalid LSPS message. Ignoring peer {} from now on.",
512+
sender_node_id
513+
);
514+
LightningError { err, action: ErrorAction::IgnoreAndLog(Level::Info) }
500515
})?
501516
};
502517

0 commit comments

Comments
 (0)