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

Commit

Permalink
Ignore rather than disconnect peers who send us bogus data
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tnull committed Feb 29, 2024
1 parent 0bc7203 commit 8e06b0d
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ use crate::lsps1::service::{LSPS1ServiceConfig, LSPS1ServiceHandler};
use crate::lsps2::client::{LSPS2ClientConfig, LSPS2ClientHandler};
use crate::lsps2::msgs::LSPS2Message;
use crate::lsps2::service::{LSPS2ServiceConfig, LSPS2ServiceHandler};
use crate::prelude::{HashMap, ToString, Vec};
use crate::prelude::{HashMap, HashSet, ToString, Vec};
use crate::sync::{Arc, Mutex, RwLock};

use lightning::chain::{self, BestBlock, Confirm, Filter, Listen};
use lightning::ln::channelmanager::{AChannelManager, ChainParameters};
use lightning::ln::features::{InitFeatures, NodeFeatures};
use lightning::ln::msgs::{ErrorAction, ErrorMessage, LightningError};
use lightning::ln::msgs::{ErrorAction, LightningError};
use lightning::ln::peer_handler::CustomMessageHandler;
use lightning::ln::wire::CustomMessageReader;
use lightning::ln::ChannelId;
use lightning::sign::EntropySource;
use lightning::util::logger::Level;
use lightning::util::ser::Readable;
Expand Down Expand Up @@ -94,6 +93,8 @@ where
pending_messages: Arc<MessageQueue>,
pending_events: Arc<EventQueue>,
request_id_to_method_map: Mutex<HashMap<RequestId, LSPSMethod>>,
// We ignore peers if they send us bogus data.
ignored_peers: RwLock<HashSet<PublicKey>>,
lsps0_client_handler: LSPS0ClientHandler<ES>,
lsps0_service_handler: Option<LSPS0ServiceHandler>,
#[cfg(lsps1)]
Expand Down Expand Up @@ -126,6 +127,7 @@ where
where {
let pending_messages = Arc::new(MessageQueue::new());
let pending_events = Arc::new(EventQueue::new());
let ignored_peers = RwLock::new(HashSet::new());

let lsps0_client_handler = LSPS0ClientHandler::new(
entropy_source.clone(),
Expand Down Expand Up @@ -192,6 +194,7 @@ where {
pending_messages,
pending_events,
request_id_to_method_map: Mutex::new(HashMap::new()),
ignored_peers,
lsps0_client_handler,
lsps0_service_handler,
#[cfg(lsps1)]
Expand Down Expand Up @@ -480,6 +483,16 @@ where
fn handle_custom_message(
&self, msg: Self::CustomMessage, sender_node_id: &PublicKey,
) -> Result<(), lightning::ln::msgs::LightningError> {
{
if self.ignored_peers.read().unwrap().contains(&sender_node_id) {
let err = format!("Ignoring message from peer {}.", sender_node_id);
return Err(LightningError {
err,
action: ErrorAction::IgnoreAndLog(Level::Trace),
});
}
}

let message = {
{
let mut request_id_to_method_map = self.request_id_to_method_map.lock().unwrap();
Expand All @@ -493,10 +506,12 @@ where
};

self.pending_messages.enqueue(sender_node_id, LSPSMessage::Invalid(error));
let err = format!("Failed to deserialize invalid LSPS message.");
let err_msg =
Some(ErrorMessage { channel_id: ChannelId([0; 32]), data: err.clone() });
LightningError { err, action: ErrorAction::DisconnectPeer { msg: err_msg } }
self.ignored_peers.write().unwrap().insert(*sender_node_id);
let err = format!(
"Failed to deserialize invalid LSPS message. Ignoring peer {} from now on.",
sender_node_id
);
LightningError { err, action: ErrorAction::IgnoreAndLog(Level::Info) }
})?
};

Expand Down

0 comments on commit 8e06b0d

Please sign in to comment.