Skip to content

Commit 1114c3c

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 17af8d5 commit 1114c3c

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
@@ -79,6 +79,7 @@ impl MessageRouter for TestMessageRouter {
7979
Ok(OnionMessagePath {
8080
intermediate_nodes: vec![],
8181
destination,
82+
addresses: None,
8283
})
8384
}
8485
}

lightning/src/onion_message/functional_tests.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl MessageRouter for TestMessageRouter {
5555
Ok(OnionMessagePath {
5656
intermediate_nodes: vec![],
5757
destination,
58+
addresses: None,
5859
})
5960
}
6061
}
@@ -205,6 +206,7 @@ fn one_unblinded_hop() {
205206
let path = OnionMessagePath {
206207
intermediate_nodes: vec![],
207208
destination: Destination::Node(nodes[1].get_node_pk()),
209+
addresses: None,
208210
};
209211
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
210212
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
@@ -219,6 +221,7 @@ fn two_unblinded_hops() {
219221
let path = OnionMessagePath {
220222
intermediate_nodes: vec![nodes[1].get_node_pk()],
221223
destination: Destination::Node(nodes[2].get_node_pk()),
224+
addresses: None,
222225
};
223226
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
224227
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
@@ -235,6 +238,7 @@ fn one_blinded_hop() {
235238
let path = OnionMessagePath {
236239
intermediate_nodes: vec![],
237240
destination: Destination::BlindedPath(blinded_path),
241+
addresses: None,
238242
};
239243
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
240244
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
@@ -251,6 +255,7 @@ fn two_unblinded_two_blinded() {
251255
let path = OnionMessagePath {
252256
intermediate_nodes: vec![nodes[1].get_node_pk(), nodes[2].get_node_pk()],
253257
destination: Destination::BlindedPath(blinded_path),
258+
addresses: None,
254259
};
255260

256261
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
@@ -268,6 +273,7 @@ fn three_blinded_hops() {
268273
let path = OnionMessagePath {
269274
intermediate_nodes: vec![],
270275
destination: Destination::BlindedPath(blinded_path),
276+
addresses: None,
271277
};
272278

273279
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
@@ -286,6 +292,7 @@ fn too_big_packet_error() {
286292
let path = OnionMessagePath {
287293
intermediate_nodes: hops,
288294
destination: Destination::Node(hop_node_id),
295+
addresses: None,
289296
};
290297
let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap_err();
291298
assert_eq!(err, SendError::TooBigPacket);
@@ -303,6 +310,7 @@ fn we_are_intro_node() {
303310
let path = OnionMessagePath {
304311
intermediate_nodes: vec![],
305312
destination: Destination::BlindedPath(blinded_path),
313+
addresses: None,
306314
};
307315

308316
nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), None).unwrap();
@@ -314,6 +322,7 @@ fn we_are_intro_node() {
314322
let path = OnionMessagePath {
315323
intermediate_nodes: vec![],
316324
destination: Destination::BlindedPath(blinded_path),
325+
addresses: None,
317326
};
318327
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
319328
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
@@ -334,6 +343,7 @@ fn invalid_blinded_path_error() {
334343
let path = OnionMessagePath {
335344
intermediate_nodes: vec![],
336345
destination: Destination::BlindedPath(blinded_path),
346+
addresses: None,
337347
};
338348
let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), None).unwrap_err();
339349
assert_eq!(err, SendError::TooFewBlindedHops);
@@ -349,6 +359,7 @@ fn reply_path() {
349359
let path = OnionMessagePath {
350360
intermediate_nodes: vec![nodes[1].get_node_pk(), nodes[2].get_node_pk()],
351361
destination: Destination::Node(nodes[3].get_node_pk()),
362+
addresses: None,
352363
};
353364
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();
354365
nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), Some(reply_path)).unwrap();
@@ -364,6 +375,7 @@ fn reply_path() {
364375
let path = OnionMessagePath {
365376
intermediate_nodes: vec![],
366377
destination: Destination::BlindedPath(blinded_path),
378+
addresses: None,
367379
};
368380
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();
369381

@@ -398,6 +410,7 @@ fn invalid_custom_message_type() {
398410
let path = OnionMessagePath {
399411
intermediate_nodes: vec![],
400412
destination: Destination::Node(nodes[1].get_node_pk()),
413+
addresses: None,
401414
};
402415
let err = nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap_err();
403416
assert_eq!(err, SendError::InvalidMessage);
@@ -410,6 +423,7 @@ fn peer_buffer_full() {
410423
let path = OnionMessagePath {
411424
intermediate_nodes: vec![],
412425
destination: Destination::Node(nodes[1].get_node_pk()),
426+
addresses: None,
413427
};
414428
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
415429
nodes[0].messenger.send_onion_message_using_path(path.clone(), test_msg.clone(), None).unwrap();
@@ -434,6 +448,7 @@ fn many_hops() {
434448
let path = OnionMessagePath {
435449
intermediate_nodes,
436450
destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
451+
addresses: None,
437452
};
438453
nodes[0].messenger.send_onion_message_using_path(path, test_msg, None).unwrap();
439454
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;
@@ -84,6 +84,7 @@ use crate::prelude::*;
8484
/// # Ok(OnionMessagePath {
8585
/// # intermediate_nodes: vec![hop_node_id1, hop_node_id2],
8686
/// # destination,
87+
/// # addresses: None,
8788
/// # })
8889
/// # }
8990
/// # }
@@ -282,7 +283,7 @@ where
282283
&self, _sender: PublicKey, peers: Vec<PublicKey>, destination: Destination
283284
) -> Result<OnionMessagePath, ()> {
284285
if peers.contains(&destination.first_node()) {
285-
Ok(OnionMessagePath { intermediate_nodes: vec![], destination })
286+
Ok(OnionMessagePath { intermediate_nodes: vec![], destination, addresses: None })
286287
} else {
287288
Err(())
288289
}
@@ -297,6 +298,22 @@ pub struct OnionMessagePath {
297298

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

302319
/// The destination of an onion message.
@@ -427,7 +444,7 @@ where
427444
ES::Target: EntropySource,
428445
NS::Target: NodeSigner,
429446
{
430-
let OnionMessagePath { intermediate_nodes, mut destination } = path;
447+
let OnionMessagePath { intermediate_nodes, mut destination, .. } = path;
431448
if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
432449
if blinded_hops.is_empty() {
433450
return Err(SendError::TooFewBlindedHops);

0 commit comments

Comments
 (0)