Skip to content

Commit 3d6c56c

Browse files
committed
Add Option<Vec<SocketAddress>> to OnionMessagePath
MessageRouter::find_path is given a Destination to reach via a set of peers. If a path cannot be found, it may return a partial path such that OnionMessenger can signal a direct connection to the first node in the path is needed. Include a list of socket addresses in the returned OnionMessagePath to allow OnionMessenger to know how to connect to the node. This allows DefaultMessageRouter to use its NetworkGraph to return socket addresses for gossiped nodes.
1 parent 534c55e commit 3d6c56c

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

fuzz/src/onion_message.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ impl MessageRouter for TestMessageRouter {
6767
Ok(OnionMessagePath {
6868
intermediate_nodes: vec![],
6969
destination,
70+
addresses: None,
7071
})
7172
}
7273
}

lightning/src/onion_message/functional_tests.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl MessageRouter for TestMessageRouter {
5454
Ok(OnionMessagePath {
5555
intermediate_nodes: vec![],
5656
destination,
57+
addresses: None,
5758
})
5859
}
5960
}
@@ -204,6 +205,7 @@ fn one_unblinded_hop() {
204205
let path = OnionMessagePath {
205206
intermediate_nodes: vec![],
206207
destination: Destination::Node(nodes[1].get_node_pk()),
208+
addresses: None,
207209
};
208210
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
209211
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
@@ -218,6 +220,7 @@ fn two_unblinded_hops() {
218220
let path = OnionMessagePath {
219221
intermediate_nodes: vec![nodes[1].get_node_pk()],
220222
destination: Destination::Node(nodes[2].get_node_pk()),
223+
addresses: None,
221224
};
222225
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
223226
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
@@ -234,6 +237,7 @@ fn one_blinded_hop() {
234237
let path = OnionMessagePath {
235238
intermediate_nodes: vec![],
236239
destination: Destination::BlindedPath(blinded_path),
240+
addresses: None,
237241
};
238242
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
239243
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
@@ -250,6 +254,7 @@ fn two_unblinded_two_blinded() {
250254
let path = OnionMessagePath {
251255
intermediate_nodes: vec![nodes[1].get_node_pk(), nodes[2].get_node_pk()],
252256
destination: Destination::BlindedPath(blinded_path),
257+
addresses: None,
253258
};
254259

255260
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
@@ -267,6 +272,7 @@ fn three_blinded_hops() {
267272
let path = OnionMessagePath {
268273
intermediate_nodes: vec![],
269274
destination: Destination::BlindedPath(blinded_path),
275+
addresses: None,
270276
};
271277

272278
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
@@ -285,6 +291,7 @@ fn too_big_packet_error() {
285291
let path = OnionMessagePath {
286292
intermediate_nodes: hops,
287293
destination: Destination::Node(hop_node_id),
294+
addresses: None,
288295
};
289296
let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap_err();
290297
assert_eq!(err, SendError::TooBigPacket);
@@ -302,6 +309,7 @@ fn we_are_intro_node() {
302309
let path = OnionMessagePath {
303310
intermediate_nodes: vec![],
304311
destination: Destination::BlindedPath(blinded_path),
312+
addresses: None,
305313
};
306314

307315
nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), None).unwrap();
@@ -313,6 +321,7 @@ fn we_are_intro_node() {
313321
let path = OnionMessagePath {
314322
intermediate_nodes: vec![],
315323
destination: Destination::BlindedPath(blinded_path),
324+
addresses: None,
316325
};
317326
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
318327
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
@@ -333,6 +342,7 @@ fn invalid_blinded_path_error() {
333342
let path = OnionMessagePath {
334343
intermediate_nodes: vec![],
335344
destination: Destination::BlindedPath(blinded_path),
345+
addresses: None,
336346
};
337347
let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), None).unwrap_err();
338348
assert_eq!(err, SendError::TooFewBlindedHops);
@@ -348,6 +358,7 @@ fn reply_path() {
348358
let path = OnionMessagePath {
349359
intermediate_nodes: vec![nodes[1].get_node_pk(), nodes[2].get_node_pk()],
350360
destination: Destination::Node(nodes[3].get_node_pk()),
361+
addresses: None,
351362
};
352363
let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
353364
nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), Some(reply_path)).unwrap();
@@ -363,6 +374,7 @@ fn reply_path() {
363374
let path = OnionMessagePath {
364375
intermediate_nodes: vec![],
365376
destination: Destination::BlindedPath(blinded_path),
377+
addresses: None,
366378
};
367379
let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
368380

@@ -396,6 +408,7 @@ fn invalid_custom_message_type() {
396408
let path = OnionMessagePath {
397409
intermediate_nodes: vec![],
398410
destination: Destination::Node(nodes[1].get_node_pk()),
411+
addresses: None,
399412
};
400413
let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap_err();
401414
assert_eq!(err, SendError::InvalidMessage);
@@ -408,6 +421,7 @@ fn peer_buffer_full() {
408421
let path = OnionMessagePath {
409422
intermediate_nodes: vec![],
410423
destination: Destination::Node(nodes[1].get_node_pk()),
424+
addresses: None,
411425
};
412426
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
413427
nodes[0].messenger.send_onion_message_using_path(path.clone(), test_msg.clone(), None).unwrap();
@@ -432,6 +446,7 @@ fn many_hops() {
432446
let path = OnionMessagePath {
433447
intermediate_nodes,
434448
destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
449+
addresses: None,
435450
};
436451
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
437452
nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Response);

lightning/src/onion_message/messenger.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient};
2222
#[cfg(not(c_bindings))]
2323
use crate::ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager};
2424
use crate::ln::features::{InitFeatures, NodeFeatures};
25-
use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler};
25+
use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler, SocketAddress};
2626
use crate::ln::onion_utils;
2727
use crate::ln::peer_handler::IgnoringMessageHandler;
2828
use crate::routing::gossip::NetworkGraph;
@@ -83,6 +83,7 @@ use crate::prelude::*;
8383
/// # Ok(OnionMessagePath {
8484
/// # intermediate_nodes: vec![hop_node_id1, hop_node_id2],
8585
/// # destination,
86+
/// # addresses: None,
8687
/// # })
8788
/// # }
8889
/// # }
@@ -277,7 +278,7 @@ where
277278
&self, _sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
278279
) -> Result<OnionMessagePath, ()> {
279280
if peers.contains(&destination.first_node()) {
280-
Ok(OnionMessagePath { intermediate_nodes: vec![], destination })
281+
Ok(OnionMessagePath { intermediate_nodes: vec![], destination, addresses: None })
281282
} else {
282283
Err(())
283284
}
@@ -292,6 +293,22 @@ pub struct OnionMessagePath {
292293

293294
/// The recipient of the message.
294295
pub destination: Destination,
296+
297+
/// Addresses that may be used to connect to [`OnionMessagePath::first_node`].
298+
///
299+
/// Only needs to be set if a connection to the node is required. [`OnionMessenger`] may use
300+
/// this to initiate such a connection.
301+
pub addresses: Option<Vec<SocketAddress>>,
302+
}
303+
304+
impl OnionMessagePath {
305+
/// Returns the first node in the path.
306+
pub fn first_node(&self) -> PublicKey {
307+
self.intermediate_nodes
308+
.first()
309+
.copied()
310+
.unwrap_or_else(|| self.destination.first_node())
311+
}
295312
}
296313

297314
/// The destination of an onion message.
@@ -422,7 +439,7 @@ where
422439
ES::Target: EntropySource,
423440
NS::Target: NodeSigner,
424441
{
425-
let OnionMessagePath { intermediate_nodes, mut destination } = path;
442+
let OnionMessagePath { intermediate_nodes, mut destination, .. } = path;
426443
if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
427444
if blinded_hops.is_empty() {
428445
return Err(SendError::TooFewBlindedHops);

0 commit comments

Comments
 (0)