]> git.bitcoin.ninja Git - rust-lightning/commitdiff
1/3 Use `MessageSendInstructions` instead of `PendingOnionMessage`
authorMatt Corallo <git@bluematt.me>
Thu, 22 Aug 2024 01:41:27 +0000 (01:41 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 22 Aug 2024 22:27:44 +0000 (22:27 +0000)
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.

fuzz/src/onion_message.rs
lightning/src/ln/peer_handler.rs
lightning/src/onion_message/functional_tests.rs
lightning/src/onion_message/messenger.rs

index 97e8ae2430638db4978954ce5fdcbd25ef738b7d..33458436c66f593e36dcf635f8f0936e365e9b4e 100644 (file)
@@ -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<PendingOnionMessage<Self::CustomMessage>> {
+       fn release_pending_custom_messages(&self) -> Vec<(TestCustomMessage, MessageSendInstructions)> {
                vec![]
        }
 }
index bde5454b49c8d02ae2acd5b745ed6df41dda3dda..b4fc9252a3fbeab24760030e837451b3d771c59e 100644 (file)
@@ -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<R: io::Read>(&self, _msg_type: u64, _buffer: &mut R) -> Result<Option<Infallible>, msgs::DecodeError> where Self: Sized {
                Ok(None)
        }
-       fn release_pending_custom_messages(&self) -> Vec<PendingOnionMessage<Infallible>> {
+       fn release_pending_custom_messages(&self) -> Vec<(Infallible, MessageSendInstructions)> {
                vec![]
        }
 }
index 01b90314ba111816b1f4f9be1344a546384dfe59..8dcbb809996b89361c7bf128864cf662848e2824 100644 (file)
@@ -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<PendingOnionMessage<Self::CustomMessage>> {
+       fn release_pending_custom_messages(&self) -> Vec<(Self::CustomMessage, MessageSendInstructions)> {
                vec![]
        }
 }
index 2bcdbffba55e8a7f3e8d35c7db5f99e0de9acf84..cf885aaee63c474f015913fd88031fecb818e232 100644 (file)
@@ -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<PendingOnionMessage<Self::CustomMessage>>;
-
-       /// 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<BlindedMessagePath>)>;
+       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<T: OnionMessageContents>(
+               &self, message: T, instructions: MessageSendInstructions, log_suffix: fmt::Arguments,
+       ) -> Result<Option<SendSuccess>, 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<T: OnionMessageContents>(
                &self, contents: T, destination: Destination, reply_path: Option<BlindedMessagePath>,
                log_suffix: fmt::Arguments
@@ -1342,33 +1361,14 @@ where
        pub fn handle_onion_message_response<T: OnionMessageContents>(
                &self, response: T, instructions: ResponseInstruction,
        ) -> Result<Option<SendSuccess>, 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")
                        );
                }