Skip to content

Commit d39966f

Browse files
OM pathfinding: don't consider offline network successors
As judged by whether the channel is disabled, similar to payment routing
1 parent 3e7e411 commit d39966f

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

lightning/src/routing/onion_message.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,19 @@ pub fn find_path<L: Deref, GL: Deref>(
6767
}
6868
for scid in &node_info.channels {
6969
if let Some(chan_info) = network_channels.get(&scid) {
70-
if let Some((_, successor)) = chan_info.as_directed_from(&node_id) {
71-
// We may push a given successor multiple times, but the heap should sort its best entry
72-
// to the top. We do this because there is no way to adjust the priority of an existing
73-
// entry in `BinaryHeap`.
74-
frontier.push(PathBuildingHop {
75-
cost: cost + 1,
76-
node_id: *successor,
77-
parent_node_id: node_id,
78-
});
70+
if let Some((directed_channel, successor)) = chan_info.as_directed_from(&node_id) {
71+
if let Some(direction) = directed_channel.direction() {
72+
if direction.enabled {
73+
// We may push a given successor multiple times, but the heap should sort its best
74+
// entry to the top. We do this because there is no way to adjust the priority of an
75+
// existing entry in `BinaryHeap`.
76+
frontier.push(PathBuildingHop {
77+
cost: cost + 1,
78+
node_id: *successor,
79+
parent_node_id: node_id,
80+
});
81+
}
82+
}
7983
}
8084
}
8185
}
@@ -216,6 +220,23 @@ mod tests {
216220
let err = super::find_path(&our_id, &node_pks[2], &network_graph, None, Arc::clone(&logger)).unwrap_err();
217221
assert_eq!(err, super::Error::PathNotFound);
218222
}
223+
224+
#[test]
225+
fn disabled_channels_test() {
226+
// Check that we won't attempt to route over nodes where the channel is disabled from their
227+
// direction (implying the peer is offline).
228+
let mut features = InitFeatures::empty();
229+
features.set_onion_messages_optional();
230+
let (secp_ctx, network_graph, _, _, logger) = build_graph_with_features(features.to_context());
231+
let (_, our_id, _, node_pks) = get_nodes(&secp_ctx);
232+
233+
// Route to 1 via 2 and 3 because our channel to 1 is disabled
234+
let path = super::find_path(&our_id, &node_pks[0], &network_graph, None, Arc::clone(&logger)).unwrap();
235+
assert_eq!(path.len(), 3);
236+
assert_eq!(path[0], node_pks[1]);
237+
assert_eq!(path[1], node_pks[2]);
238+
assert_eq!(path[2], node_pks[0]);
239+
}
219240
}
220241

221242
#[cfg(all(test, feature = "_bench_unstable", not(feature = "no-std")))]

0 commit comments

Comments
 (0)