X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fonion_message%2Fmessenger.rs;h=a3613605cdebbd1ff71014d827b70782e043a160;hb=d94227cc13d2dee0ce1a4f24629d6e58a8a8416d;hp=07cb0bc1e9896df5241c05a0b80a2b03e89eb995;hpb=a71000f35dd23949d26fffd421d6f6478f0cd6b5;p=rust-lightning diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 07cb0bc1..a3613605 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -152,6 +152,17 @@ pub trait MessageRouter { ) -> Result; } +/// A [`MessageRouter`] that always fails. +pub struct DefaultMessageRouter; + +impl MessageRouter for DefaultMessageRouter { + fn find_path( + &self, _sender: PublicKey, _peers: Vec, _destination: Destination + ) -> Result { + Err(()) + } +} + /// A path for sending an [`msgs::OnionMessage`]. #[derive(Clone)] pub struct OnionMessagePath { @@ -224,8 +235,10 @@ pub trait CustomOnionMessageHandler { /// The message known to the handler. To support multiple message types, you may want to make this /// an enum with a variant for each supported message. type CustomMessage: CustomOnionMessageContents; - /// Called with the custom message that was received. - fn handle_custom_message(&self, msg: Self::CustomMessage); + + /// Called with the custom message that was received, returning a response to send, if any. + fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option; + /// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the /// message type is unknown. fn read_custom_message(&self, message_type: u64, buffer: &mut R) -> Result, msgs::DecodeError>; @@ -320,6 +333,56 @@ where } } + fn respond_with_onion_message( + &self, response: OnionMessageContents, path_id: Option<[u8; 32]>, + reply_path: Option + ) { + let sender = match self.node_signer.get_node_id(Recipient::Node) { + Ok(node_id) => node_id, + Err(_) => { + log_warn!( + self.logger, "Unable to retrieve node id when responding to onion message with \ + path_id {:02x?}", path_id + ); + return; + } + }; + + let peers = self.pending_messages.lock().unwrap().keys().copied().collect(); + + let destination = match reply_path { + Some(reply_path) => Destination::BlindedPath(reply_path), + None => { + log_trace!( + self.logger, "Missing reply path when responding to onion message with path_id \ + {:02x?}", path_id + ); + return; + }, + }; + + let path = match self.message_router.find_path(sender, peers, destination) { + Ok(path) => path, + Err(()) => { + log_trace!( + self.logger, "Failed to find path when responding to onion message with \ + path_id {:02x?}", path_id + ); + return; + }, + }; + + log_trace!(self.logger, "Responding to onion message with path_id {:02x?}", path_id); + + if let Err(e) = self.send_onion_message(path, response, None) { + log_trace!( + self.logger, "Failed responding to onion message with path_id {:02x?}: {:?}", + path_id, e + ); + return; + } + } + #[cfg(test)] pub(super) fn release_pending_msgs(&self) -> HashMap> { let mut pending_msgs = self.pending_messages.lock().unwrap(); @@ -364,7 +427,7 @@ where L::Target: Logger, MR::Target: MessageRouter, OMH::Target: OffersMessageHandler, - CMH::Target: CustomOnionMessageHandler + Sized, + CMH::Target: CustomOnionMessageHandler, { /// Handle an incoming onion message. Currently, if a message was destined for us we will log, but /// soon we'll delegate the onion message to a handler that can generate invoices or send @@ -400,12 +463,23 @@ where Ok((Payload::Receive::<<::Target as CustomOnionMessageHandler>::CustomMessage> { message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id }), reply_path, }, None)) => { - log_info!(self.logger, + log_trace!(self.logger, "Received an onion message with path_id {:02x?} and {} reply_path", path_id, if reply_path.is_some() { "a" } else { "no" }); - match message { - OnionMessageContents::Offers(msg) => self.offers_handler.handle_message(msg), - OnionMessageContents::Custom(msg) => self.custom_handler.handle_custom_message(msg), + + let response = match message { + OnionMessageContents::Offers(msg) => { + self.offers_handler.handle_message(msg) + .map(|msg| OnionMessageContents::Offers(msg)) + }, + OnionMessageContents::Custom(msg) => { + self.custom_handler.handle_custom_message(msg) + .map(|msg| OnionMessageContents::Custom(msg)) + }, + }; + + if let Some(response) = response { + self.respond_with_onion_message(response, path_id, reply_path); } }, Ok((Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs { @@ -535,11 +609,11 @@ where /// /// [`SimpleArcChannelManager`]: crate::ln::channelmanager::SimpleArcChannelManager /// [`SimpleArcPeerManager`]: crate::ln::peer_handler::SimpleArcPeerManager -pub type SimpleArcOnionMessenger = OnionMessenger< +pub type SimpleArcOnionMessenger = OnionMessenger< Arc, Arc, Arc, - Arc, + Arc, IgnoringMessageHandler, IgnoringMessageHandler >; @@ -551,11 +625,11 @@ pub type SimpleArcOnionMessenger = OnionMessenger< /// /// [`SimpleRefChannelManager`]: crate::ln::channelmanager::SimpleRefChannelManager /// [`SimpleRefPeerManager`]: crate::ln::peer_handler::SimpleRefPeerManager -pub type SimpleRefOnionMessenger<'a, 'b, 'c, L, R> = OnionMessenger< +pub type SimpleRefOnionMessenger<'a, 'b, 'c, L> = OnionMessenger< &'a KeysManager, &'a KeysManager, &'b L, - &'c R, + &'c DefaultMessageRouter, IgnoringMessageHandler, IgnoringMessageHandler >;