From 683282fa24e93a72d934fb19e0119a7c14c521ba Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Mon, 18 Nov 2019 09:24:09 +0100 Subject: [PATCH] routing: check loop conditions at end This prepares for routing to self. When checking the condition at the start, the loop would terminate immediately because the source is equal to the target. --- routing/pathfind.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/routing/pathfind.go b/routing/pathfind.go index e2f1b447..65f84043 100644 --- a/routing/pathfind.go +++ b/routing/pathfind.go @@ -534,7 +534,7 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig, // heap. heap.Push(&nodeHeap, distance[target]) - for nodeHeap.Len() != 0 { + for { nodesVisited++ // Fetch the node within the smallest distance from our source @@ -585,13 +585,17 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig, // already have. processEdge(fromNode, policy, partialPath) } + + if nodeHeap.Len() == 0 { + break + } } // Use the distance map to unravel the forward path from source to // target. var pathEdges []*channeldb.ChannelEdgePolicy currentNode := source - for currentNode != target { // TODO(roasbeef): assumes no cycles + for { // Determine the next hop forward using the next map. currentNodeWithDist, ok := distance[currentNode] if !ok { @@ -605,6 +609,10 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig, // Advance current node. currentNode = currentNodeWithDist.nextHop.Node.PubKeyBytes + + if currentNode == target { + break + } } // The route is invalid if it spans more than 20 hops. The current