@@ -372,8 +372,42 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
372
372
// 8. Choose the best route by the lowest total fee.
373
373
374
374
// As for the actual search algorithm,
375
- // we do a payee-to-payer Dijkstra's sorting by each node's distance from the payee
376
- // plus the minimum per-HTLC fee to get from it to another node (aka "shitty A*").
375
+ // we do a payee-to-payer pseudo-Dijkstra's sorting by each node's distance from the payee
376
+ // plus the minimum per-HTLC fee to get from it to another node (aka "shitty pseudo-A*").
377
+ //
378
+ // We are not a faithful Dijkstra's implementation because we can change values which impact
379
+ // earlier nodes while processing later nodes. Specifically, if we reach a channel with a lower
380
+ // liquidity limit (via htlc_maximum_msat, on-chain capacity or assumed liquidity limits) then
381
+ // the value we are currently attempting to send over a path, we simply reduce the value being
382
+ // sent along the path for any hops after that channel. This may imply that later fees (which
383
+ // we've already tabulated) are lower because a smaller value is passing through the channels
384
+ // (and the proportional fee is thus lower). There isn't a trivial way to recalculate the
385
+ // channels which were selected earlier (and which may still be used for other paths without a
386
+ // lower liquidity limit), so we simply accept that some liquidity-limited paths may be
387
+ // de-preferenced.
388
+ //
389
+ // One potentially problematic case for this algorithm would be if there are many
390
+ // liquidity-limited paths which are liquidity-limited near the destination (ie early in our
391
+ // graph walking), we may never find a liquidity-unlimited path which has lower proportional
392
+ // fee (and only lower absolute fee when considering the ultimate value sent). Because we only
393
+ // consider paths with at least 5% of the total value being sent, the damage from such a case
394
+ // should be limited, however this could be further reduced in the future by calculating fees
395
+ // on the amount we wish to route over a path, not the amount we are routing over a path.
396
+ //
397
+ // Alternatively, we could store more detailed path information in the heap (targets, below)
398
+ // and index the best-path map (dist, below) by node *and* HTLC limits, however that would blow
399
+ // up the runtime significantly both algorithmically (as we'd traverse nodes multiple times)
400
+ // and practically (as we would need to store dynamically-allocated path information in heap
401
+ // objects, increasing malloc traffic and indirect memory access significantly). Further, the
402
+ // results of such an algorithm would likely be biased towards lower-value paths.
403
+ //
404
+ // Further, we could return to a faithful Dijkstra's algorithm by rejecting paths with limits
405
+ // outside of our current search value, running a path search more times to gather candidate
406
+ // paths at different values. While this may be acceptable, further path searches may increase
407
+ // runtime for little gain. Specifically, the current algorithm rather efficiently explores the
408
+ // graph for candidate paths, calculating the maximum value which can realistically be sent at
409
+ // the same time, remaining generic across different payment values.
410
+ //
377
411
// TODO: There are a few tweaks we could do, including possibly pre-calculating more stuff
378
412
// to use as the A* heuristic beyond just the cost to get one node further than the current
379
413
// one.
0 commit comments