X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fonion_message%2Fmessenger.rs;h=37de550453283e4a383051ace821cabb44f95ff1;hb=9bac73b4f3c922b790d1ec844413c4567149f4ca;hp=2dc5a89a37b5fae41071d8c4589d813b39916a3e;hpb=0995de7fa85afcbe78bb4f7345f4dd5198dfe94c;p=rust-lightning diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 2dc5a89a..37de5504 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -358,16 +358,28 @@ where const MIN_PEER_CHANNELS: usize = 3; let network_graph = self.network_graph.deref().read_only(); - let paths = peers.iter() + 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(|pubkey| + .filter_map(|pubkey| network_graph .node(&NodeId::from_pubkey(pubkey)) - .map(|info| &info.channels[..]) - .map(|channels| channels.len() >= MIN_PEER_CHANNELS) - .unwrap_or(false) + .filter(|info| info.channels.len() >= MIN_PEER_CHANNELS) + .map(|info| (*pubkey, info.is_tor_only(), info.channels.len())) ) - .map(|pubkey| vec![*pubkey, recipient]) + // Exclude Tor-only nodes when the recipient is announced. + .filter(|(_, is_tor_only, _)| !(*is_tor_only && is_recipient_announced)) + .collect::>(); + + // Prefer using non-Tor nodes with the most channels as the introduction node. + peer_info.sort_unstable_by(|(_, a_tor_only, a_channels), (_, b_tor_only, b_channels)| { + a_tor_only.cmp(b_tor_only).then(a_channels.cmp(b_channels).reverse()) + }); + + let paths = peer_info.into_iter() + .map(|(pubkey, _, _)| vec![pubkey, recipient]) .map(|node_pks| BlindedPath::new_for_message(&node_pks, &*self.entropy_source, secp_ctx)) .take(MAX_PATHS) .collect::, _>>(); @@ -375,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 {