Skip to content

Commit 90361c1

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.
1 parent b396f0a commit 90361c1

File tree

4 files changed

+41
-45
lines changed

4 files changed

+41
-45
lines changed

fuzz/src/onion_message.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use lightning::onion_message::async_payments::{
1616
AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc,
1717
};
1818
use lightning::onion_message::messenger::{
19-
CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger,
20-
PendingOnionMessage, Responder, ResponseInstruction,
19+
CustomOnionMessageHandler, Destination, MessageRouter, MessageSendInstructions,
20+
OnionMessagePath, OnionMessenger, Responder, ResponseInstruction,
2121
};
2222
use lightning::onion_message::offers::{OffersMessage, OffersMessageHandler};
2323
use lightning::onion_message::packet::OnionMessageContents;
@@ -173,7 +173,7 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler {
173173
buffer.read_to_limit(&mut buf, u64::MAX)?;
174174
return Ok(Some(TestCustomMessage {}));
175175
}
176-
fn release_pending_custom_messages(&self) -> Vec<PendingOnionMessage<Self::CustomMessage>> {
176+
fn release_pending_custom_messages(&self) -> Vec<(TestCustomMessage, MessageSendInstructions)> {
177177
vec![]
178178
}
179179
}

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, ResponseInstruction};
33+
use crate::onion_message::messenger::{CustomOnionMessageHandler, Responder, ResponseInstruction, 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, ResponseInstruction, SendError, SendSuccess};
23+
use super::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, OnionMessagePath, OnionMessenger, Responder, ResponseInstruction, 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: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -830,15 +830,7 @@ pub trait CustomOnionMessageHandler {
830830
///
831831
/// Typically, this is used for messages initiating a message flow rather than in response to
832832
/// another message. The latter should use the return value of [`Self::handle_custom_message`].
833-
#[cfg(not(c_bindings))]
834-
fn release_pending_custom_messages(&self) -> Vec<PendingOnionMessage<Self::CustomMessage>>;
835-
836-
/// Releases any [`Self::CustomMessage`]s that need to be sent.
837-
///
838-
/// Typically, this is used for messages initiating a message flow rather than in response to
839-
/// another message. The latter should use the return value of [`Self::handle_custom_message`].
840-
#[cfg(c_bindings)]
841-
fn release_pending_custom_messages(&self) -> Vec<(Self::CustomMessage, Destination, Option<BlindedMessagePath>)>;
833+
fn release_pending_custom_messages(&self) -> Vec<(Self::CustomMessage, MessageSendInstructions)>;
842834
}
843835

844836
/// A processed incoming onion message, containing either a Forward (another onion message)
@@ -1188,6 +1180,33 @@ where
11881180
)
11891181
}
11901182

1183+
fn send_onion_message_internal<T: OnionMessageContents>(
1184+
&self, message: T, instructions: MessageSendInstructions, log_suffix: fmt::Arguments,
1185+
) -> Result<Option<SendSuccess>, SendError> {
1186+
let (destination, context) = match instructions {
1187+
MessageSendInstructions::WithReplyPath { destination, context } => (destination, Some(context)),
1188+
MessageSendInstructions::WithoutReplyPath { destination } => (destination, None),
1189+
};
1190+
1191+
let reply_path = if let Some(context) = context {
1192+
match self.create_blinded_path(context) {
1193+
Ok(reply_path) => Some(reply_path),
1194+
Err(err) => {
1195+
log_trace!(
1196+
self.logger,
1197+
"Failed to create reply path {}: {:?}",
1198+
log_suffix, err
1199+
);
1200+
return Err(err);
1201+
}
1202+
}
1203+
} else { None };
1204+
1205+
self.find_path_and_enqueue_onion_message(
1206+
message, destination, reply_path, log_suffix,
1207+
).map(|result| Some(result))
1208+
}
1209+
11911210
fn find_path_and_enqueue_onion_message<T: OnionMessageContents>(
11921211
&self, contents: T, destination: Destination, reply_path: Option<BlindedMessagePath>,
11931212
log_suffix: fmt::Arguments
@@ -1342,33 +1361,14 @@ where
13421361
pub fn handle_onion_message_response<T: OnionMessageContents>(
13431362
&self, response: T, instructions: ResponseInstruction,
13441363
) -> Result<Option<SendSuccess>, SendError> {
1345-
let (destination, context) = match instructions.into_instructions() {
1346-
MessageSendInstructions::WithReplyPath { destination, context } => (destination, Some(context)),
1347-
MessageSendInstructions::WithoutReplyPath { destination } => (destination, None),
1348-
};
1349-
13501364
let message_type = response.msg_type();
1351-
let reply_path = if let Some(context) = context {
1352-
match self.create_blinded_path(context) {
1353-
Ok(reply_path) => Some(reply_path),
1354-
Err(err) => {
1355-
log_trace!(
1356-
self.logger,
1357-
"Failed to create reply path when responding with {} to an onion message: {:?}",
1358-
message_type, err
1359-
);
1360-
return Err(err);
1361-
}
1362-
}
1363-
} else { None };
1364-
1365-
self.find_path_and_enqueue_onion_message(
1366-
response, destination, reply_path,
1365+
self.send_onion_message_internal(
1366+
response, instructions.into_instructions(),
13671367
format_args!(
13681368
"when responding with {} to an onion message",
13691369
message_type,
13701370
)
1371-
).map(|result| Some(result))
1371+
)
13721372
}
13731373

13741374
#[cfg(test)]
@@ -1748,13 +1748,9 @@ where
17481748
}
17491749

17501750
// Enqueue any initiating `CustomMessage`s to send.
1751-
for message in self.custom_handler.release_pending_custom_messages() {
1752-
#[cfg(not(c_bindings))]
1753-
let PendingOnionMessage { contents, destination, reply_path } = message;
1754-
#[cfg(c_bindings)]
1755-
let (contents, destination, reply_path) = message;
1756-
let _ = self.find_path_and_enqueue_onion_message(
1757-
contents, destination, reply_path, format_args!("when sending CustomMessage")
1751+
for (message, instructions) in self.custom_handler.release_pending_custom_messages() {
1752+
let _ = self.send_onion_message_internal(
1753+
message, instructions, format_args!("when sending CustomMessage")
17581754
);
17591755
}
17601756

0 commit comments

Comments
 (0)