Skip to content

Commit e105833

Browse files
committed
Prefer one-hop blinded path to Tor intro nodes
If a node is announced, prefer using a one-hop blinded path with it as the introduction node to using a two-hop blinded path with a Tor-only introduction node. The one-hop blinded path is more reliable, thus only use Tor-only nodes if the recipient is unannounced. And then, prefer non-Tor-only nodes.
1 parent 5e9a998 commit e105833

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lightning/src/ln/offers_tests.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
250250
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
251251

252252
let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
253-
announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor);
253+
announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
254254

255255
let offer = bob.node
256256
.create_offer_builder("coffee".to_string()).unwrap()
@@ -259,8 +259,23 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
259259
assert_ne!(offer.signing_pubkey(), bob_id);
260260
assert!(!offer.paths().is_empty());
261261
for path in offer.paths() {
262+
assert_ne!(path.introduction_node_id, bob_id);
262263
assert_ne!(path.introduction_node_id, charlie_id);
263264
}
265+
266+
// Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
267+
announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
268+
announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
269+
270+
let offer = bob.node
271+
.create_offer_builder("coffee".to_string()).unwrap()
272+
.amount_msats(10_000_000)
273+
.build().unwrap();
274+
assert_ne!(offer.signing_pubkey(), bob_id);
275+
assert!(!offer.paths().is_empty());
276+
for path in offer.paths() {
277+
assert_eq!(path.introduction_node_id, bob_id);
278+
}
264279
}
265280

266281
/// Checks that blinded paths prefer an introduction node that is the most connected.

lightning/src/onion_message/messenger.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ where
358358
const MIN_PEER_CHANNELS: usize = 3;
359359

360360
let network_graph = self.network_graph.deref().read_only();
361+
let is_recipient_announced =
362+
network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient));
363+
361364
let mut peer_info = peers.iter()
362365
// Limit to peers with announced channels
363366
.filter_map(|pubkey|
@@ -366,6 +369,8 @@ where
366369
.filter(|info| info.channels.len() >= MIN_PEER_CHANNELS)
367370
.map(|info| (*pubkey, info.is_tor_only(), info.channels.len()))
368371
)
372+
// Exclude Tor-only nodes when the recipient is announced.
373+
.filter(|(_, is_tor_only, _)| !(*is_tor_only && is_recipient_announced))
369374
.collect::<Vec<_>>();
370375

371376
// Prefer using non-Tor nodes with the most channels as the introduction node.
@@ -382,7 +387,7 @@ where
382387
match paths {
383388
Ok(paths) if !paths.is_empty() => Ok(paths),
384389
_ => {
385-
if network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient)) {
390+
if is_recipient_announced {
386391
BlindedPath::one_hop_for_message(recipient, &*self.entropy_source, secp_ctx)
387392
.map(|path| vec![path])
388393
} else {

0 commit comments

Comments
 (0)