Skip to content

Commit 5e81762

Browse files
TheBlueMattadi2011
authored andcommitted
Add PathBuildingHop::best_path_from_hop_selected
When we process a path backwards from a node during pathfinding, we implicitly commit to the path up to that node. Any changes to the preferred path up to that node will make the newly processed path's state invalid. In the previous few commits we fixed cases for this in last-hop paths (both blinded and unblinded). Here we add assertions to enforce this, tracked in a new bool in `PathBuildingHop`.
1 parent b1fd8e1 commit 5e81762

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lightning/src/routing/router.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,12 @@ struct PathBuildingHop<'a> {
18161816
/// decrease as well. Thus, we have to explicitly track which nodes have been processed and
18171817
/// avoid processing them again.
18181818
was_processed: bool,
1819+
/// If we've already processed a channel backwards from a target node, we shouldn't update our
1820+
/// selected best path from that node to the destination. This should never happen, but with
1821+
/// multiple codepaths processing channels we've had issues here in the past, so in debug-mode
1822+
/// we track it and assert on it when processing a node.
1823+
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
1824+
best_path_from_hop_selected: bool,
18191825
/// When processing a node as the next best-score candidate, we want to quickly check if it is
18201826
/// a direct counterparty of ours, using our local channel information immediately if we can.
18211827
///
@@ -2459,6 +2465,19 @@ where L::Target: Logger {
24592465
// We "return" whether we updated the path at the end, and how much we can route via
24602466
// this channel, via this:
24612467
let mut hop_contribution_amt_msat = None;
2468+
2469+
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
2470+
if let Some(counter) = $candidate.target_node_counter() {
2471+
// Once we are adding paths backwards from a given target, we've selected the best
2472+
// path from that target to the destination and it should no longer change. We thus
2473+
// set the best-path selected flag and check that it doesn't change below.
2474+
if let Some(node) = &mut dist[counter as usize] {
2475+
node.best_path_from_hop_selected = true;
2476+
} else if counter != payee_node_counter {
2477+
panic!("No dist entry for target node counter {}", counter);
2478+
}
2479+
}
2480+
24622481
// Channels to self should not be used. This is more of belt-and-suspenders, because in
24632482
// practice these cases should be caught earlier:
24642483
// - for regular channels at channel announcement (TODO)
@@ -2623,6 +2642,8 @@ where L::Target: Logger {
26232642
was_processed: false,
26242643
is_first_hop_target: false,
26252644
is_last_hop_target: false,
2645+
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
2646+
best_path_from_hop_selected: false,
26262647
value_contribution_msat,
26272648
});
26282649
dist_entry.as_mut().unwrap()
@@ -2703,6 +2724,11 @@ where L::Target: Logger {
27032724
|| (new_cost == old_cost && old_entry.value_contribution_msat < value_contribution_msat);
27042725

27052726
if !old_entry.was_processed && should_replace {
2727+
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
2728+
{
2729+
assert!(!old_entry.best_path_from_hop_selected);
2730+
}
2731+
27062732
let new_graph_node = RouteGraphNode {
27072733
node_id: src_node_id,
27082734
node_counter: src_node_counter,
@@ -2916,6 +2942,8 @@ where L::Target: Logger {
29162942
is_first_hop_target: true,
29172943
is_last_hop_target: false,
29182944
value_contribution_msat: 0,
2945+
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
2946+
best_path_from_hop_selected: false,
29192947
});
29202948
}
29212949
for (target_node_counter, candidates) in last_hop_candidates.iter() {
@@ -2943,6 +2971,8 @@ where L::Target: Logger {
29432971
is_first_hop_target: false,
29442972
is_last_hop_target: true,
29452973
value_contribution_msat: 0,
2974+
#[cfg(all(not(ldk_bench), any(test, fuzzing)))]
2975+
best_path_from_hop_selected: false,
29462976
});
29472977
}
29482978
}

0 commit comments

Comments
 (0)