Skip to content

Commit

Permalink
OM pathfinding: ensure first_hops overrides network graph
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinewallace committed Sep 28, 2022
1 parent aba9e61 commit 846dd0f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lightning/src/routing/onion_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ pub fn find_path<L: Deref, GL: Deref>(

// Add our start and first-hops to `frontier`.
let start = NodeId::from_pubkey(&our_node_pubkey);
let mut valid_first_hops = HashSet::new();
let mut frontier = BinaryHeap::new();
frontier.push(PathBuildingHop { cost: 0, node_id: start, parent_node_id: start });
if let Some(first_hops) = first_hops {
for hop in first_hops {
if !hop.counterparty.features.supports_onion_messages() { continue; }
let node_id = NodeId::from_pubkey(&hop.counterparty.node_id);
frontier.push(PathBuildingHop { cost: 1, node_id, parent_node_id: start });
valid_first_hops.insert(node_id);
}
}

Expand All @@ -75,7 +77,7 @@ pub fn find_path<L: Deref, GL: Deref>(
return Ok(path)
}
if let Some(node_info) = network_nodes.get(&node_id) {
if node_id == our_node_id {
if valid_first_hops.contains(&node_id) || node_id == our_node_id {
} else if let Some(node_ann) = &node_info.announcement_info {
if !node_ann.features.supports_onion_messages() || node_ann.features.requires_unknown_bits()
{ continue; }
Expand Down Expand Up @@ -173,7 +175,7 @@ fn reverse_path(
#[cfg(test)]
mod tests {
use ln::features::{InitFeatures, NodeFeatures};
use routing::test_utils::{add_or_update_node, build_graph_with_features, build_line_graph, get_nodes};
use routing::test_utils::{add_or_update_node, build_graph_with_features, build_line_graph, get_channel_details, get_nodes};

use sync::Arc;

Expand Down Expand Up @@ -246,6 +248,13 @@ mod tests {
// If all nodes require some features we don't understand, route should fail
let err = super::find_path(&our_id, &node_pks[2], &network_graph, None, Arc::clone(&logger)).unwrap_err();
assert_eq!(err, super::Error::PathNotFound);

// If we specify a channel to node7, that overrides our local channel view and that gets used
let our_chans = vec![get_channel_details(Some(42), node_pks[7].clone(), features, 250_000_000)];
let path = super::find_path(&our_id, &node_pks[2], &network_graph, Some(&our_chans.iter().collect::<Vec<_>>()), Arc::clone(&logger)).unwrap();
assert_eq!(path.len(), 2);
assert_eq!(path[0], node_pks[7]);
assert_eq!(path[1], node_pks[2]);
}

#[test]
Expand All @@ -263,5 +272,11 @@ mod tests {
assert_eq!(path[0], node_pks[1]);
assert_eq!(path[1], node_pks[2]);
assert_eq!(path[2], node_pks[0]);

// If we specify a channel to node1, that overrides our local channel view and that gets used
let our_chans = vec![get_channel_details(Some(42), node_pks[0].clone(), features, 250_000_000)];
let path = super::find_path(&our_id, &node_pks[0], &network_graph, Some(&our_chans.iter().collect::<Vec<_>>()), Arc::clone(&logger)).unwrap();
assert_eq!(path.len(), 1);
assert_eq!(path[0], node_pks[0]);
}
}

0 comments on commit 846dd0f

Please sign in to comment.