@@ -199,6 +199,7 @@ object Graph {
199
199
val bestEdges = mutable.HashMap .newBuilder[PublicKey , GraphEdge ](initialCapacity, mutable.HashMap .defaultLoadFactor).result()
200
200
// NB: we want the elements with smallest weight first, hence the `reverse`.
201
201
val toExplore = mutable.PriorityQueue .empty[WeightedNode ](NodeComparator .reverse)
202
+ val visitedNodes = mutable.HashSet [PublicKey ]()
202
203
203
204
// initialize the queue and cost array with the initial weight
204
205
bestWeights.put(targetNode, initialWeight)
@@ -208,8 +209,9 @@ object Graph {
208
209
while (toExplore.nonEmpty && ! targetFound) {
209
210
// node with the smallest distance from the target
210
211
val current = toExplore.dequeue() // O(log(n))
211
- if (current.key != sourceNode) {
212
- val currentWeight = bestWeights(current.key) // NB: there is always an entry for the current in the 'bestWeights' map
212
+ targetFound = current.key == sourceNode
213
+ if (! targetFound && ! visitedNodes.contains(current.key)) {
214
+ visitedNodes += current.key
213
215
// build the neighbors with optional extra edges
214
216
val neighborEdges = {
215
217
val extraNeighbors = extraEdges.filter(_.desc.b == current.key)
@@ -220,11 +222,11 @@ object Graph {
220
222
val neighbor = edge.desc.a
221
223
// NB: this contains the amount (including fees) that will need to be sent to `neighbor`, but the amount that
222
224
// will be relayed through that edge is the one in `currentWeight`.
223
- val neighborWeight = addEdgeWeight(sender, edge, currentWeight , currentBlockHeight, wr)
224
- val canRelayAmount = currentWeight .cost <= edge.capacity &&
225
- edge.balance_opt.forall(currentWeight .cost <= _) &&
226
- edge.update.htlcMaximumMsat.forall(currentWeight .cost <= _) &&
227
- currentWeight .cost >= edge.update.htlcMinimumMsat
225
+ val neighborWeight = addEdgeWeight(sender, edge, current.weight , currentBlockHeight, wr)
226
+ val canRelayAmount = current.weight .cost <= edge.capacity &&
227
+ edge.balance_opt.forall(current.weight .cost <= _) &&
228
+ edge.update.htlcMaximumMsat.forall(current.weight .cost <= _) &&
229
+ current.weight .cost >= edge.update.htlcMinimumMsat
228
230
if (canRelayAmount && boundaries(neighborWeight) && ! ignoredEdges.contains(edge.desc) && ! ignoredVertices.contains(neighbor)) {
229
231
val previousNeighborWeight = bestWeights.getOrElse(neighbor, RichWeight (MilliSatoshi (Long .MaxValue ), Int .MaxValue , CltvExpiryDelta (Int .MaxValue ), Double .MaxValue ))
230
232
// if this path between neighbor and the target has a shorter distance than previously known, we select it
@@ -238,21 +240,19 @@ object Graph {
238
240
}
239
241
}
240
242
}
241
- } else {
242
- targetFound = true
243
243
}
244
244
}
245
245
246
- targetFound match {
247
- case false => Seq .empty [GraphEdge ]
248
- case true =>
249
- val edgePath = new mutable. ArrayBuffer [ GraphEdge ]( RouteCalculation . ROUTE_MAX_LENGTH )
250
- var current = bestEdges .get(sourceNode)
251
- while (current.isDefined) {
252
- edgePath += current.get
253
- current = bestEdges.get(current.get.desc.b)
254
- }
255
- edgePath.toSeq
246
+ if (targetFound) {
247
+ val edgePath = new mutable. ArrayBuffer [GraphEdge ]( RouteCalculation . ROUTE_MAX_LENGTH )
248
+ var current = bestEdges.get(sourceNode)
249
+ while (current.isDefined) {
250
+ edgePath += current .get
251
+ current = bestEdges.get (current.get.desc.b)
252
+ }
253
+ edgePath.toSeq
254
+ } else {
255
+ Seq .empty[ GraphEdge ]
256
256
}
257
257
}
258
258
@@ -421,9 +421,10 @@ object Graph {
421
421
* @return a new graph without this edge
422
422
*/
423
423
def removeEdge (desc : ChannelDesc ): DirectedGraph = {
424
- containsEdge(desc) match {
425
- case true => DirectedGraph (vertices.updated(desc.b, vertices(desc.b).filterNot(_.desc == desc)))
426
- case false => this
424
+ if (containsEdge(desc)) {
425
+ DirectedGraph (vertices.updated(desc.b, vertices(desc.b).filterNot(_.desc == desc)))
426
+ } else {
427
+ this
427
428
}
428
429
}
429
430
@@ -555,9 +556,8 @@ object Graph {
555
556
556
557
def addDescToMap (desc : ChannelDesc , u : ChannelUpdate , capacity : Satoshi , balance_opt : Option [MilliSatoshi ]): Unit = {
557
558
mutableMap.put(desc.b, GraphEdge (desc, u, getCapacity(capacity, u), balance_opt) +: mutableMap.getOrElse(desc.b, List .empty[GraphEdge ]))
558
- mutableMap.get(desc.a) match {
559
- case None => mutableMap += desc.a -> List .empty[GraphEdge ]
560
- case _ =>
559
+ if (! mutableMap.contains(desc.a)) {
560
+ mutableMap += desc.a -> List .empty[GraphEdge ]
561
561
}
562
562
}
563
563
0 commit comments