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.
This commit is contained in:
Joost Jager 2019-11-18 09:24:09 +01:00
parent 2b332893b7
commit 683282fa24
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -534,7 +534,7 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
// heap. // heap.
heap.Push(&nodeHeap, distance[target]) heap.Push(&nodeHeap, distance[target])
for nodeHeap.Len() != 0 { for {
nodesVisited++ nodesVisited++
// Fetch the node within the smallest distance from our source // Fetch the node within the smallest distance from our source
@ -585,13 +585,17 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
// already have. // already have.
processEdge(fromNode, policy, partialPath) processEdge(fromNode, policy, partialPath)
} }
if nodeHeap.Len() == 0 {
break
}
} }
// Use the distance map to unravel the forward path from source to // Use the distance map to unravel the forward path from source to
// target. // target.
var pathEdges []*channeldb.ChannelEdgePolicy var pathEdges []*channeldb.ChannelEdgePolicy
currentNode := source currentNode := source
for currentNode != target { // TODO(roasbeef): assumes no cycles for {
// Determine the next hop forward using the next map. // Determine the next hop forward using the next map.
currentNodeWithDist, ok := distance[currentNode] currentNodeWithDist, ok := distance[currentNode]
if !ok { if !ok {
@ -605,6 +609,10 @@ func findPath(g *graphParams, r *RestrictParams, cfg *PathFindingConfig,
// Advance current node. // Advance current node.
currentNode = currentNodeWithDist.nextHop.Node.PubKeyBytes currentNode = currentNodeWithDist.nextHop.Node.PubKeyBytes
if currentNode == target {
break
}
} }
// The route is invalid if it spans more than 20 hops. The current // The route is invalid if it spans more than 20 hops. The current