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.
This commit is contained in:
Matt Corallo 2024-08-22 01:41:27 +00:00
parent b396f0afd1
commit 90361c18bf
4 changed files with 41 additions and 45 deletions

View 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![]
}
}

View 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![]
}
}

View 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![]
}
}

View 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")
);
}