Skip to content

Commit 3609207

Browse files
committed
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. By unifying the response type with the outbound message queue type, this also fixes #3178.
1 parent 94e6b47 commit 3609207

File tree

3 files changed

+39
-43
lines changed

3 files changed

+39
-43
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::ln::peer_channel_encryptor::{PeerChannelEncryptor, NextNoiseStep, Mes
3030
use crate::ln::wire;
3131
use crate::ln::wire::{Encode, Type};
3232
use crate::onion_message::async_payments::{AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc};
33-
use crate::onion_message::messenger::{CustomOnionMessageHandler, PendingOnionMessage, Responder, MessageSendInstructions};
33+
use crate::onion_message::messenger::{CustomOnionMessageHandler, Responder, MessageSendInstructions};
3434
use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
3535
use crate::onion_message::packet::OnionMessageContents;
3636
use crate::routing::gossip::{NodeId, NodeAlias};
@@ -165,7 +165,7 @@ impl CustomOnionMessageHandler for IgnoringMessageHandler {
165165
fn read_custom_message<R: io::Read>(&self, _msg_type: u64, _buffer: &mut R) -> Result<Option<Infallible>, msgs::DecodeError> where Self: Sized {
166166
Ok(None)
167167
}
168-
fn release_pending_custom_messages(&self) -> Vec<PendingOnionMessage<Infallible>> {
168+
fn release_pending_custom_messages(&self) -> Vec<(Infallible, MessageSendInstructions)> {
169169
vec![]
170170
}
171171
}

lightning/src/onion_message/functional_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::sign::{NodeSigner, Recipient};
2020
use crate::util::ser::{FixedLengthReader, LengthReadable, Writeable, Writer};
2121
use crate::util::test_utils;
2222
use super::async_payments::{AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc};
23-
use super::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, PendingOnionMessage, Responder, MessageSendInstructions, SendError, SendSuccess};
23+
use super::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, Responder, MessageSendInstructions, SendError, SendSuccess};
2424
use super::offers::{OffersMessage, OffersMessageHandler};
2525
use super::packet::{OnionMessageContents, Packet};
2626

@@ -211,7 +211,7 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler {
211211
_ => Ok(None),
212212
}
213213
}
214-
fn release_pending_custom_messages(&self) -> Vec<PendingOnionMessage<Self::CustomMessage>> {
214+
fn release_pending_custom_messages(&self) -> Vec<(Self::CustomMessage, MessageSendInstructions)> {
215215
vec![]
216216
}
217217
}

lightning/src/onion_message/messenger.rs

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -812,15 +812,7 @@ pub trait CustomOnionMessageHandler {
812812
///
813813
/// Typically, this is used for messages initiating a message flow rather than in response to
814814
/// another message. The latter should use the return value of [`Self::handle_custom_message`].
815-
#[cfg(not(c_bindings))]
816-
fn release_pending_custom_messages(&self) -> Vec<PendingOnionMessage<Self::CustomMessage>>;
817-
818-
/// Releases any [`Self::CustomMessage`]s that need to be sent.
819-
///
820-
/// Typically, this is used for messages initiating a message flow rather than in response to
821-
/// another message. The latter should use the return value of [`Self::handle_custom_message`].
822-
#[cfg(c_bindings)]
823-
fn release_pending_custom_messages(&self) -> Vec<(Self::CustomMessage, Destination, Option<BlindedMessagePath>)>;
815+
fn release_pending_custom_messages(&self) -> Vec<(Self::CustomMessage, MessageSendInstructions)>;
824816
}
825817

826818
/// A processed incoming onion message, containing either a Forward (another onion message)
@@ -1170,6 +1162,33 @@ where
11701162
)
11711163
}
11721164

1165+
fn handle_onion_message_send<T: OnionMessageContents>(
1166+
&self, response_message: T, response: MessageSendInstructions, log_suffix: fmt::Arguments,
1167+
) -> Result<Option<SendSuccess>, SendError> {
1168+
let (destination, context) = match response {
1169+
MessageSendInstructions::WithReplyPath { destination, context } => (destination, Some(context)),
1170+
MessageSendInstructions::WithoutReplyPath { destination } => (destination, None),
1171+
};
1172+
1173+
let reply_path = if let Some(context) = context {
1174+
match self.create_blinded_path(context) {
1175+
Ok(reply_path) => Some(reply_path),
1176+
Err(err) => {
1177+
log_trace!(
1178+
self.logger,
1179+
"Failed to create reply path {}: {:?}",
1180+
log_suffix, err
1181+
);
1182+
return Err(err);
1183+
}
1184+
}
1185+
} else { None };
1186+
1187+
self.find_path_and_enqueue_onion_message(
1188+
response_message, destination, reply_path, log_suffix,
1189+
).map(|result| Some(result))
1190+
}
1191+
11731192
fn find_path_and_enqueue_onion_message<T: OnionMessageContents>(
11741193
&self, contents: T, destination: Destination, reply_path: Option<BlindedMessagePath>,
11751194
log_suffix: fmt::Arguments
@@ -1322,35 +1341,16 @@ where
13221341
/// generating the response asynchronously. Subsequently, when the response is prepared and
13231342
/// ready for sending, that task can invoke this method to enqueue the response for delivery.
13241343
pub fn handle_onion_message_response<T: OnionMessageContents>(
1325-
&self, response_message: T, response: MessageSendInstructions,
1344+
&self, response_message: T, instructions: MessageSendInstructions,
13261345
) -> Result<Option<SendSuccess>, SendError> {
1327-
let (destination, context) = match response {
1328-
MessageSendInstructions::WithReplyPath { destination, context } => (destination, Some(context)),
1329-
MessageSendInstructions::WithoutReplyPath { destination } => (destination, None),
1330-
};
1331-
13321346
let message_type = response_message.msg_type();
1333-
let reply_path = if let Some(context) = context {
1334-
match self.create_blinded_path(context) {
1335-
Ok(reply_path) => Some(reply_path),
1336-
Err(err) => {
1337-
log_trace!(
1338-
self.logger,
1339-
"Failed to create reply path when responding with {} to an onion message: {:?}",
1340-
message_type, err
1341-
);
1342-
return Err(err);
1343-
}
1344-
}
1345-
} else { None };
1346-
1347-
self.find_path_and_enqueue_onion_message(
1348-
response_message, destination, reply_path,
1347+
self.handle_onion_message_send(
1348+
response_message, instructions,
13491349
format_args!(
13501350
"when responding with {} to an onion message",
13511351
message_type,
13521352
)
1353-
).map(|result| Some(result))
1353+
)
13541354
}
13551355

13561356
#[cfg(test)]
@@ -1730,13 +1730,9 @@ where
17301730
}
17311731

17321732
// Enqueue any initiating `CustomMessage`s to send.
1733-
for message in self.custom_handler.release_pending_custom_messages() {
1734-
#[cfg(not(c_bindings))]
1735-
let PendingOnionMessage { contents, destination, reply_path } = message;
1736-
#[cfg(c_bindings)]
1737-
let (contents, destination, reply_path) = message;
1738-
let _ = self.find_path_and_enqueue_onion_message(
1739-
contents, destination, reply_path, format_args!("when sending CustomMessage")
1733+
for (message, instructions) in self.custom_handler.release_pending_custom_messages() {
1734+
let _ = self.handle_onion_message_send(
1735+
message, instructions, format_args!("when sending CustomMessage")
17401736
);
17411737
}
17421738

0 commit comments

Comments
 (0)