From: Matt Corallo Date: Thu, 22 Aug 2024 01:41:27 +0000 (+0000) Subject: 1/3 Use `MessageSendInstructions` instead of `PendingOnionMessage` X-Git-Tag: v0.0.124-rc1~15^2~4 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=90361c18bf709fa2d2f148ff375351fda7528c98;p=rust-lightning 1/3 Use `MessageSendInstructions` instead of `PendingOnionMessage` Now that the `MessageRouter` can `create_blinded_paths` forcing callers of the `OnionMessenger` to provide it with a reply path up front is unnecessary complexity, doubly so in message handlers. Here we take the first step towards untangling that, moving from `PendingOnionMessage` to `MessageSendInstructions` for the outbound message queue in `CustomMessageHandler`. Better, we can also drop the `c_bindings`-specific message queue variant, unifying the APIs. --- diff --git a/fuzz/src/onion_message.rs b/fuzz/src/onion_message.rs index 97e8ae243..33458436c 100644 --- a/fuzz/src/onion_message.rs +++ b/fuzz/src/onion_message.rs @@ -16,8 +16,8 @@ use lightning::onion_message::async_payments::{ AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc, }; use lightning::onion_message::messenger::{ - CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger, - PendingOnionMessage, Responder, ResponseInstruction, + CustomOnionMessageHandler, Destination, MessageRouter, MessageSendInstructions, + OnionMessagePath, OnionMessenger, Responder, ResponseInstruction, }; use lightning::onion_message::offers::{OffersMessage, OffersMessageHandler}; use lightning::onion_message::packet::OnionMessageContents; @@ -173,7 +173,7 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler { buffer.read_to_limit(&mut buf, u64::MAX)?; return Ok(Some(TestCustomMessage {})); } - fn release_pending_custom_messages(&self) -> Vec> { + fn release_pending_custom_messages(&self) -> Vec<(TestCustomMessage, MessageSendInstructions)> { vec![] } } diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index bde5454b4..b4fc9252a 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -30,7 +30,7 @@ use crate::ln::peer_channel_encryptor::{PeerChannelEncryptor, NextNoiseStep, Mes use crate::ln::wire; use crate::ln::wire::{Encode, Type}; use crate::onion_message::async_payments::{AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc}; -use crate::onion_message::messenger::{CustomOnionMessageHandler, PendingOnionMessage, Responder, ResponseInstruction}; +use crate::onion_message::messenger::{CustomOnionMessageHandler, Responder, ResponseInstruction, MessageSendInstructions}; use crate::onion_message::offers::{OffersMessage, OffersMessageHandler}; use crate::onion_message::packet::OnionMessageContents; use crate::routing::gossip::{NodeId, NodeAlias}; @@ -165,7 +165,7 @@ impl CustomOnionMessageHandler for IgnoringMessageHandler { fn read_custom_message(&self, _msg_type: u64, _buffer: &mut R) -> Result, msgs::DecodeError> where Self: Sized { Ok(None) } - fn release_pending_custom_messages(&self) -> Vec> { + fn release_pending_custom_messages(&self) -> Vec<(Infallible, MessageSendInstructions)> { vec![] } } diff --git a/lightning/src/onion_message/functional_tests.rs b/lightning/src/onion_message/functional_tests.rs index 01b90314b..8dcbb8099 100644 --- a/lightning/src/onion_message/functional_tests.rs +++ b/lightning/src/onion_message/functional_tests.rs @@ -20,7 +20,7 @@ use crate::sign::{NodeSigner, Recipient}; use crate::util::ser::{FixedLengthReader, LengthReadable, Writeable, Writer}; use crate::util::test_utils; use super::async_payments::{AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc}; -use super::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, PendingOnionMessage, Responder, ResponseInstruction, SendError, SendSuccess}; +use super::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, Responder, ResponseInstruction, MessageSendInstructions, SendError, SendSuccess}; use super::offers::{OffersMessage, OffersMessageHandler}; use super::packet::{OnionMessageContents, Packet}; @@ -211,7 +211,7 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler { _ => Ok(None), } } - fn release_pending_custom_messages(&self) -> Vec> { + fn release_pending_custom_messages(&self) -> Vec<(Self::CustomMessage, MessageSendInstructions)> { vec![] } } diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 2bcdbffba..cf885aaee 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -830,15 +830,7 @@ pub trait CustomOnionMessageHandler { /// /// Typically, this is used for messages initiating a message flow rather than in response to /// another message. The latter should use the return value of [`Self::handle_custom_message`]. - #[cfg(not(c_bindings))] - fn release_pending_custom_messages(&self) -> Vec>; - - /// Releases any [`Self::CustomMessage`]s that need to be sent. - /// - /// Typically, this is used for messages initiating a message flow rather than in response to - /// another message. The latter should use the return value of [`Self::handle_custom_message`]. - #[cfg(c_bindings)] - fn release_pending_custom_messages(&self) -> Vec<(Self::CustomMessage, Destination, Option)>; + fn release_pending_custom_messages(&self) -> Vec<(Self::CustomMessage, MessageSendInstructions)>; } /// A processed incoming onion message, containing either a Forward (another onion message) @@ -1188,6 +1180,33 @@ where ) } + fn send_onion_message_internal( + &self, message: T, instructions: MessageSendInstructions, log_suffix: fmt::Arguments, + ) -> Result, SendError> { + let (destination, context) = match instructions { + MessageSendInstructions::WithReplyPath { destination, context } => (destination, Some(context)), + MessageSendInstructions::WithoutReplyPath { destination } => (destination, None), + }; + + let reply_path = if let Some(context) = context { + match self.create_blinded_path(context) { + Ok(reply_path) => Some(reply_path), + Err(err) => { + log_trace!( + self.logger, + "Failed to create reply path {}: {:?}", + log_suffix, err + ); + return Err(err); + } + } + } else { None }; + + self.find_path_and_enqueue_onion_message( + message, destination, reply_path, log_suffix, + ).map(|result| Some(result)) + } + fn find_path_and_enqueue_onion_message( &self, contents: T, destination: Destination, reply_path: Option, log_suffix: fmt::Arguments @@ -1342,33 +1361,14 @@ where pub fn handle_onion_message_response( &self, response: T, instructions: ResponseInstruction, ) -> Result, SendError> { - let (destination, context) = match instructions.into_instructions() { - MessageSendInstructions::WithReplyPath { destination, context } => (destination, Some(context)), - MessageSendInstructions::WithoutReplyPath { destination } => (destination, None), - }; - let message_type = response.msg_type(); - let reply_path = if let Some(context) = context { - match self.create_blinded_path(context) { - Ok(reply_path) => Some(reply_path), - Err(err) => { - log_trace!( - self.logger, - "Failed to create reply path when responding with {} to an onion message: {:?}", - message_type, err - ); - return Err(err); - } - } - } else { None }; - - self.find_path_and_enqueue_onion_message( - response, destination, reply_path, + self.send_onion_message_internal( + response, instructions.into_instructions(), format_args!( "when responding with {} to an onion message", message_type, ) - ).map(|result| Some(result)) + ) } #[cfg(test)] @@ -1748,13 +1748,9 @@ where } // Enqueue any initiating `CustomMessage`s to send. - for message in self.custom_handler.release_pending_custom_messages() { - #[cfg(not(c_bindings))] - let PendingOnionMessage { contents, destination, reply_path } = message; - #[cfg(c_bindings)] - let (contents, destination, reply_path) = message; - let _ = self.find_path_and_enqueue_onion_message( - contents, destination, reply_path, format_args!("when sending CustomMessage") + for (message, instructions) in self.custom_handler.release_pending_custom_messages() { + let _ = self.send_onion_message_internal( + message, instructions, format_args!("when sending CustomMessage") ); }