Skip to content

Commit d2cc904

Browse files
committed
Change send_onion_message to take a MessageSendInstructions
This lets callers include a `reply_path` without doing the path-finding at the callsite, utilizing the built-in path-finding logic in `OnionMessenger`
1 parent a24063a commit d2cc904

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

lightning/src/onion_message/functional_tests.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ fn one_unblinded_hop() {
345345
let test_msg = TestCustomMessage::Pong;
346346

347347
let destination = Destination::Node(nodes[1].node_id);
348-
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
348+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
349+
nodes[0].messenger.send_onion_message(test_msg, instructions).unwrap();
349350
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Pong);
350351
pass_along_path(&nodes);
351352
}
@@ -375,7 +376,8 @@ fn one_blinded_hop() {
375376
let context = MessageContext::Custom(Vec::new());
376377
let blinded_path = BlindedMessagePath::new(&[], nodes[1].node_id, context, &*nodes[1].entropy_source, &secp_ctx).unwrap();
377378
let destination = Destination::BlindedPath(blinded_path);
378-
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
379+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
380+
nodes[0].messenger.send_onion_message(test_msg, instructions).unwrap();
379381
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Pong);
380382
pass_along_path(&nodes);
381383
}
@@ -413,8 +415,9 @@ fn three_blinded_hops() {
413415
let context = MessageContext::Custom(Vec::new());
414416
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[3].node_id, context, &*nodes[3].entropy_source, &secp_ctx).unwrap();
415417
let destination = Destination::BlindedPath(blinded_path);
418+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
416419

417-
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
420+
nodes[0].messenger.send_onion_message(test_msg, instructions).unwrap();
418421
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Pong);
419422
pass_along_path(&nodes);
420423
}
@@ -447,7 +450,7 @@ fn async_response_over_one_blinded_hop() {
447450
let (msg, instructions) = response_instruction.unwrap();
448451
assert_eq!(
449452
nodes[0].messenger.handle_onion_message_response(msg, instructions),
450-
Ok(Some(SendSuccess::Buffered)),
453+
Ok(SendSuccess::Buffered),
451454
);
452455

453456
bob.custom_message_handler.expect_message(TestCustomMessage::Pong);
@@ -481,7 +484,7 @@ fn async_response_with_reply_path_succeeds() {
481484
let (msg, instructions) = response_instruction.unwrap();
482485
assert_eq!(
483486
alice.messenger.handle_onion_message_response(msg, instructions),
484-
Ok(Some(SendSuccess::Buffered)),
487+
Ok(SendSuccess::Buffered),
485488
);
486489

487490
// Set Bob's expectation and pass the Onion Message along the path.
@@ -557,8 +560,9 @@ fn we_are_intro_node() {
557560
let context = MessageContext::Custom(Vec::new());
558561
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[2].node_id, context, &*nodes[2].entropy_source, &secp_ctx).unwrap();
559562
let destination = Destination::BlindedPath(blinded_path);
563+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
560564

561-
nodes[0].messenger.send_onion_message(test_msg.clone(), destination, None).unwrap();
565+
nodes[0].messenger.send_onion_message(test_msg.clone(), instructions).unwrap();
562566
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Pong);
563567
pass_along_path(&nodes);
564568

@@ -567,7 +571,9 @@ fn we_are_intro_node() {
567571
let context = MessageContext::Custom(Vec::new());
568572
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[1].node_id, context, &*nodes[1].entropy_source, &secp_ctx).unwrap();
569573
let destination = Destination::BlindedPath(blinded_path);
570-
nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap();
574+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
575+
576+
nodes[0].messenger.send_onion_message(test_msg, instructions).unwrap();
571577
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Pong);
572578
nodes.remove(2);
573579
pass_along_path(&nodes);
@@ -585,7 +591,9 @@ fn invalid_blinded_path_error() {
585591
let mut blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[2].node_id, context, &*nodes[2].entropy_source, &secp_ctx).unwrap();
586592
blinded_path.clear_blinded_hops();
587593
let destination = Destination::BlindedPath(blinded_path);
588-
let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err();
594+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
595+
596+
let err = nodes[0].messenger.send_onion_message(test_msg, instructions).unwrap_err();
589597
assert_eq!(err, SendError::TooFewBlindedHops);
590598
}
591599

@@ -629,8 +637,9 @@ fn reply_path() {
629637
];
630638
let context = MessageContext::Custom(Vec::new());
631639
let reply_path = BlindedMessagePath::new(&intermediate_nodes, nodes[0].node_id, context, &*nodes[0].entropy_source, &secp_ctx).unwrap();
640+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath { destination, reply_path };
632641

633-
nodes[0].messenger.send_onion_message(test_msg, destination, Some(reply_path)).unwrap();
642+
nodes[0].messenger.send_onion_message(test_msg, instructions).unwrap();
634643
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Ping);
635644
pass_along_path(&nodes);
636645

@@ -662,7 +671,9 @@ fn invalid_custom_message_type() {
662671

663672
let test_msg = InvalidCustomMessage {};
664673
let destination = Destination::Node(nodes[1].node_id);
665-
let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err();
674+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
675+
676+
let err = nodes[0].messenger.send_onion_message(test_msg, instructions).unwrap_err();
666677
assert_eq!(err, SendError::InvalidMessage);
667678
}
668679

@@ -671,10 +682,12 @@ fn peer_buffer_full() {
671682
let nodes = create_nodes(2);
672683
let test_msg = TestCustomMessage::Ping;
673684
let destination = Destination::Node(nodes[1].node_id);
685+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
686+
674687
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
675-
nodes[0].messenger.send_onion_message(test_msg.clone(), destination.clone(), None).unwrap();
688+
nodes[0].messenger.send_onion_message(test_msg.clone(), instructions.clone()).unwrap();
676689
}
677-
let err = nodes[0].messenger.send_onion_message(test_msg, destination, None).unwrap_err();
690+
let err = nodes[0].messenger.send_onion_message(test_msg, instructions.clone()).unwrap_err();
678691
assert_eq!(err, SendError::BufferFull);
679692
}
680693

@@ -714,17 +727,18 @@ fn requests_peer_connection_for_buffered_messages() {
714727
&intermediate_nodes, nodes[2].node_id, context, &*nodes[0].entropy_source, &secp_ctx
715728
).unwrap();
716729
let destination = Destination::BlindedPath(blinded_path);
730+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
717731

718732
// Buffer an onion message for a connected peer
719-
nodes[0].messenger.send_onion_message(message.clone(), destination.clone(), None).unwrap();
733+
nodes[0].messenger.send_onion_message(message.clone(), instructions.clone()).unwrap();
720734
assert!(release_events(&nodes[0]).is_empty());
721735
assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_some());
722736
assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none());
723737

724738
// Buffer an onion message for a disconnected peer
725739
disconnect_peers(&nodes[0], &nodes[1]);
726740
assert!(nodes[0].messenger.next_onion_message_for_peer(nodes[1].node_id).is_none());
727-
nodes[0].messenger.send_onion_message(message, destination, None).unwrap();
741+
nodes[0].messenger.send_onion_message(message, instructions).unwrap();
728742

729743
// Check that a ConnectionNeeded event for the peer is provided
730744
let events = release_events(&nodes[0]);
@@ -753,10 +767,11 @@ fn drops_buffered_messages_waiting_for_peer_connection() {
753767
&intermediate_nodes, nodes[2].node_id, context, &*nodes[0].entropy_source, &secp_ctx
754768
).unwrap();
755769
let destination = Destination::BlindedPath(blinded_path);
770+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
756771

757772
// Buffer an onion message for a disconnected peer
758773
disconnect_peers(&nodes[0], &nodes[1]);
759-
nodes[0].messenger.send_onion_message(message, destination, None).unwrap();
774+
nodes[0].messenger.send_onion_message(message, instructions).unwrap();
760775

761776
// Release the event so the timer can start ticking
762777
let events = release_events(&nodes[0]);
@@ -804,10 +819,11 @@ fn intercept_offline_peer_oms() {
804819
&intermediate_nodes, nodes[2].node_id, context, &*nodes[2].entropy_source, &secp_ctx
805820
).unwrap();
806821
let destination = Destination::BlindedPath(blinded_path);
822+
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
807823

808824
// Disconnect the peers to ensure we intercept the OM.
809825
disconnect_peers(&nodes[1], &nodes[2]);
810-
nodes[0].messenger.send_onion_message(message, destination, None).unwrap();
826+
nodes[0].messenger.send_onion_message(message, instructions).unwrap();
811827
let mut final_node_vec = nodes.split_off(2);
812828
pass_along_path(&nodes);
813829

lightning/src/onion_message/messenger.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, CMH> where
151151
/// # use lightning::blinded_path::message::{BlindedMessagePath, ForwardNode, MessageContext};
152152
/// # use lightning::sign::{EntropySource, KeysManager};
153153
/// # use lightning::ln::peer_handler::IgnoringMessageHandler;
154-
/// # use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath, OnionMessenger};
154+
/// # use lightning::onion_message::messenger::{Destination, MessageRouter, MessageSendInstructions, OnionMessagePath, OnionMessenger};
155155
/// # use lightning::onion_message::packet::OnionMessageContents;
156156
/// # use lightning::util::logger::{Logger, Record};
157157
/// # use lightning::util::ser::{Writeable, Writer};
@@ -218,9 +218,9 @@ for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, CMH> where
218218
/// }
219219
/// // Send a custom onion message to a node id.
220220
/// let destination = Destination::Node(destination_node_id);
221-
/// let reply_path = None;
221+
/// let instructions = MessageSendInstructions::WithoutReplyPath { destination };
222222
/// # let message = YourCustomMessage {};
223-
/// onion_messenger.send_onion_message(message, destination, reply_path);
223+
/// onion_messenger.send_onion_message(message, instructions);
224224
///
225225
/// // Create a blinded path to yourself, for someone to send an onion message to.
226226
/// # let your_node_id = hop_node_id1;
@@ -233,9 +233,9 @@ for OnionMessenger<ES, NS, L, NL, MR, OMH, APH, CMH> where
233233
///
234234
/// // Send a custom onion message to a blinded path.
235235
/// let destination = Destination::BlindedPath(blinded_path);
236-
/// let reply_path = None;
236+
/// let instructions = MessageSendInstructions::WithoutReplyPath { destination };
237237
/// # let message = YourCustomMessage {};
238-
/// onion_messenger.send_onion_message(message, destination, reply_path);
238+
/// onion_messenger.send_onion_message(message, instructions);
239239
/// ```
240240
///
241241
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
@@ -1148,17 +1148,24 @@ where
11481148
/// Sends an [`OnionMessage`] with the given `contents` to `destination`.
11491149
///
11501150
/// See [`OnionMessenger`] for example usage.
1151-
pub fn send_onion_message<T: OnionMessageContents>(
1151+
pub fn send_onion_message_with_specified_reply_path<T: OnionMessageContents>(
11521152
&self, contents: T, destination: Destination, reply_path: Option<BlindedMessagePath>
11531153
) -> Result<SendSuccess, SendError> {
11541154
self.find_path_and_enqueue_onion_message(
11551155
contents, destination, reply_path, format_args!("")
11561156
)
11571157
}
11581158

1159+
/// Sends an [`OnionMessage`] based on its [`MessageSendInstructions`].
1160+
pub fn send_onion_message<T: OnionMessageContents>(
1161+
&self, contents: T, instructions: MessageSendInstructions,
1162+
) -> Result<SendSuccess, SendError> {
1163+
self.send_onion_message_internal(contents, instructions, format_args!(""))
1164+
}
1165+
11591166
fn send_onion_message_internal<T: OnionMessageContents>(
11601167
&self, message: T, instructions: MessageSendInstructions, log_suffix: fmt::Arguments,
1161-
) -> Result<Option<SendSuccess>, SendError> {
1168+
) -> Result<SendSuccess, SendError> {
11621169
let (destination, reply_path) = match instructions {
11631170
MessageSendInstructions::WithSpecifiedReplyPath { destination, reply_path } =>
11641171
(destination, Some(reply_path)),
@@ -1181,7 +1188,7 @@ where
11811188

11821189
self.find_path_and_enqueue_onion_message(
11831190
message, destination, reply_path, log_suffix,
1184-
).map(|result| Some(result))
1191+
)
11851192
}
11861193

11871194
fn find_path_and_enqueue_onion_message<T: OnionMessageContents>(
@@ -1337,7 +1344,7 @@ where
13371344
/// ready for sending, that task can invoke this method to enqueue the response for delivery.
13381345
pub fn handle_onion_message_response<T: OnionMessageContents>(
13391346
&self, response: T, instructions: ResponseInstruction,
1340-
) -> Result<Option<SendSuccess>, SendError> {
1347+
) -> Result<SendSuccess, SendError> {
13411348
let message_type = response.msg_type();
13421349
self.send_onion_message_internal(
13431350
response, instructions.into_instructions(),

0 commit comments

Comments
 (0)