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