Skip to content

Commit 9bac73b

Browse files
authored
Merge pull request #2912 from jkczyz/2024-02-prefer-more-connections
Order blinded paths by reliability criteria
2 parents 7a35bf8 + e105833 commit 9bac73b

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

lightning/src/ln/offers_tests.rs

Lines changed: 66 additions & 1 deletion
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,73 @@ 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+
}
279+
}
280+
281+
/// Checks that blinded paths prefer an introduction node that is the most connected.
282+
#[test]
283+
fn prefers_more_connected_nodes_in_blinded_paths() {
284+
let mut accept_forward_cfg = test_default_channel_config();
285+
accept_forward_cfg.accept_forwards_to_priv_channels = true;
286+
287+
let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
288+
features.set_onion_messages_optional();
289+
features.set_route_blinding_optional();
290+
291+
let chanmon_cfgs = create_chanmon_cfgs(6);
292+
let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
293+
294+
*node_cfgs[1].override_init_features.borrow_mut() = Some(features);
295+
296+
let node_chanmgrs = create_node_chanmgrs(
297+
6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
298+
);
299+
let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
300+
301+
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
302+
create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
303+
create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
304+
create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
305+
create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
306+
create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
307+
create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
308+
309+
// Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
310+
// more than the others.
311+
create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
312+
create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
313+
314+
let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
315+
let bob_id = bob.node.get_our_node_id();
316+
317+
disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
318+
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
319+
320+
let offer = bob.node
321+
.create_offer_builder("coffee".to_string()).unwrap()
322+
.amount_msats(10_000_000)
323+
.build().unwrap();
324+
assert_ne!(offer.signing_pubkey(), bob_id);
325+
assert!(!offer.paths().is_empty());
326+
for path in offer.paths() {
327+
assert_eq!(path.introduction_node_id, nodes[4].node.get_our_node_id());
328+
}
264329
}
265330

266331
/// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used

lightning/src/onion_message/messenger.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,27 +358,36 @@ 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|
364367
network_graph
365368
.node(&NodeId::from_pubkey(pubkey))
366369
.filter(|info| info.channels.len() >= MIN_PEER_CHANNELS)
367-
.map(|info| (*pubkey, info.is_tor_only()))
370+
.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<_>>();
370-
peer_info.sort_unstable_by(|(_, a_tor_only), (_, b_tor_only)| a_tor_only.cmp(b_tor_only));
375+
376+
// Prefer using non-Tor nodes with the most channels as the introduction node.
377+
peer_info.sort_unstable_by(|(_, a_tor_only, a_channels), (_, b_tor_only, b_channels)| {
378+
a_tor_only.cmp(b_tor_only).then(a_channels.cmp(b_channels).reverse())
379+
});
371380

372381
let paths = peer_info.into_iter()
373-
.map(|(pubkey, _)| vec![pubkey, recipient])
382+
.map(|(pubkey, _, _)| vec![pubkey, recipient])
374383
.map(|node_pks| BlindedPath::new_for_message(&node_pks, &*self.entropy_source, secp_ctx))
375384
.take(MAX_PATHS)
376385
.collect::<Result<Vec<_>, _>>();
377386

378387
match paths {
379388
Ok(paths) if !paths.is_empty() => Ok(paths),
380389
_ => {
381-
if network_graph.nodes().contains_key(&NodeId::from_pubkey(&recipient)) {
390+
if is_recipient_announced {
382391
BlindedPath::one_hop_for_message(recipient, &*self.entropy_source, secp_ctx)
383392
.map(|path| vec![path])
384393
} else {

0 commit comments

Comments
 (0)