Skip to content

Commit

Permalink
Add defenses against looping paths (#2109)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomash-acinq authored Dec 17, 2021
1 parent 8afb02a commit bf0969a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions eclair-core/src/main/scala/fr/acinq/eclair/router/Graph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ object Graph {
override def compare(x: WeightedPath, y: WeightedPath): Int = y.weight.compare(x.weight)
}

case class InfiniteLoop(path: Seq[GraphEdge]) extends Exception

/**
* Yen's algorithm to find the k-shortest (loop-less) paths in a graph, uses dijkstra as search algo. Is guaranteed to
* terminate finding at most @pathsToFind paths sorted by cost (the cheapest is in position 0).
Expand Down Expand Up @@ -271,6 +273,9 @@ object Graph {
while (current.isDefined) {
edgePath += current.get
current = bestEdges.get(current.get.desc.b)
if(edgePath.length > RouteCalculation.ROUTE_MAX_LENGTH){
throw InfiniteLoop(edgePath.toSeq)
}
}
edgePath.toSeq
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import fr.acinq.eclair._
import fr.acinq.eclair.payment.PaymentRequest.ExtraHop
import fr.acinq.eclair.router.Graph.GraphStructure.DirectedGraph.graphEdgeToHop
import fr.acinq.eclair.router.Graph.GraphStructure.{DirectedGraph, GraphEdge}
import fr.acinq.eclair.router.Graph.{RichWeight, RoutingHeuristics}
import fr.acinq.eclair.router.Graph.{InfiniteLoop, RichWeight, RoutingHeuristics}
import fr.acinq.eclair.router.Monitoring.{Metrics, Tags}
import fr.acinq.eclair.router.Router._
import fr.acinq.eclair.wire.protocol.ChannelUpdate
Expand Down Expand Up @@ -117,7 +117,7 @@ object RouteCalculation {
val routesToFind = if (params.randomize) DEFAULT_ROUTES_COUNT else 1

log.info(s"finding routes ${r.source}->${r.target} with assistedChannels={} ignoreNodes={} ignoreChannels={} excludedChannels={}", assistedChannels.keys.mkString(","), r.ignore.nodes.map(_.value).mkString(","), r.ignore.channels.mkString(","), d.excludedChannels.mkString(","))
log.info("finding routes with randomize={} params={}", params.randomize, params)
log.info("finding routes with params={}, multiPart={}", params, r.allowMultiPart)
val tags = TagSet.Empty.withTag(Tags.MultiPart, r.allowMultiPart).withTag(Tags.Amount, Tags.amountBucket(r.amount))
KamonExt.time(Metrics.FindRouteDuration.withTags(tags.withTag(Tags.NumberOfRoutes, routesToFind.toLong))) {
val result = if (r.allowMultiPart) {
Expand All @@ -130,6 +130,10 @@ object RouteCalculation {
Metrics.RouteResults.withTags(tags).record(routes.length)
routes.foreach(route => Metrics.RouteLength.withTags(tags).record(route.length))
ctx.sender() ! RouteResponse(routes)
case Failure(InfiniteLoop(loop)) =>
log.error(s"found infinite loop ${loop.map(edge => edge.desc).mkString(" -> ")}")
Metrics.FindRouteErrors.withTags(tags.withTag(Tags.Error, "InfiniteLoop")).increment()
ctx.sender() ! Status.Failure(InfiniteLoop(loop))
case Failure(t) =>
val failure = if (isNeighborBalanceTooLow(d.graph, r)) BalanceTooLow else t
Metrics.FindRouteErrors.withTags(tags.withTag(Tags.Error, failure.getClass.getSimpleName)).increment()
Expand Down

0 comments on commit bf0969a

Please sign in to comment.