Skip to content

Commit 7ce59d3

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

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

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

0 commit comments

Comments
 (0)