Skip to content

Commit

Permalink
Prefer one-hop blinded path to Tor intro nodes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkczyz committed Feb 28, 2024
1 parent 5e9a998 commit e105833
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 16 additions & 1 deletion lightning/src/ln/offers_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);

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

let offer = bob.node
.create_offer_builder("coffee".to_string()).unwrap()
Expand All @@ -259,8 +259,23 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
assert_ne!(offer.signing_pubkey(), bob_id);
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_ne!(path.introduction_node_id, bob_id);
assert_ne!(path.introduction_node_id, charlie_id);
}

// Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());

let offer = bob.node
.create_offer_builder("coffee".to_string()).unwrap()
.amount_msats(10_000_000)
.build().unwrap();
assert_ne!(offer.signing_pubkey(), bob_id);
assert!(!offer.paths().is_empty());
for path in offer.paths() {
assert_eq!(path.introduction_node_id, bob_id);
}
}

/// Checks that blinded paths prefer an introduction node that is the most connected.
Expand Down
7 changes: 6 additions & 1 deletion lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ where
const MIN_PEER_CHANNELS: usize = 3;

let network_graph = self.network_graph.deref().read_only();
let is_recipient_announced =
network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient));

let mut peer_info = peers.iter()
// Limit to peers with announced channels
.filter_map(|pubkey|
Expand All @@ -366,6 +369,8 @@ where
.filter(|info| info.channels.len() >= MIN_PEER_CHANNELS)
.map(|info| (*pubkey, info.is_tor_only(), info.channels.len()))
)
// Exclude Tor-only nodes when the recipient is announced.
.filter(|(_, is_tor_only, _)| !(*is_tor_only && is_recipient_announced))
.collect::<Vec<_>>();

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

0 comments on commit e105833

Please sign in to comment.