Skip to content

routing: add RouteOrigin interface for multi-source pathfinding#10764

Open
calvinrzachman wants to merge 4 commits intolightningnetwork:elle-base-branch-payment-servicefrom
calvinrzachman:route-origin-interface
Open

routing: add RouteOrigin interface for multi-source pathfinding#10764
calvinrzachman wants to merge 4 commits intolightningnetwork:elle-base-branch-payment-servicefrom
calvinrzachman:route-origin-interface

Conversation

@calvinrzachman
Copy link
Copy Markdown
Collaborator

Change Description

Path finding currently terminates at a single concrete source vertex, which assumes the node doing the routing is the node dispatching HTLCs. An external router controller that pathfinds centrally and dispatches from multiple lnd "gateway" backends needs routes that can originate from any of those backends.

This PR introduces a RouteOrigin interface that generalizes where routes can originate from. RouteOrigin replaces the concrete source parameter with an interface method (IsOrigin(v) bool) so the path-finding algorithm (a modified version of Dijkstra running from target to source) can terminate at any vertex in a caller-provided set, naturally selecting whichever provides the cheapest path.

This is the source-end counterpart to what AdditionalEdge does at the destination end. Route hints inject extra edges near the target so the pathfinder can reach nodes the graph doesn't know about. RouteOrigin works similarly at the source: it's equivalent to adding a virtual super-source s* with zero-weight edges to each origin and running single-source Dijkstra.

s* ---0--- gw1 -------- A -------- target
s* ---0--- gw2 -------- B -------/
s* ---0--- gw3 -------- C ------/

RouteOrigin achieves the same result without s* or extra edges in the graph by widening the termination check. Subpaths of shortest paths are shortest paths, so stripping the virtual edge leaves the optimal path from the selected origin. And the min-heap ensures the first origin popped has the smallest distance among all origins in the set (no cheaper one can remain in the queue).

The default singleOrigin wraps a single vertex and produces identical behavior for all existing callers so should present no functional change for standard lnd.

  • Add RouteOrigin interface and singleOrigin implementation. Replace the concrete source parameter on findPath with origin RouteOrigin.
  • findPath returns the origin vertex it selected as its first return value. RequestRoute passes this to newRoute so route.SourcePubKey is set correctly regardless of origin type. SessionSource gains an optional Origin field, threaded to the payment session via functional options.
  • RouteRequest accepts an optional Origin field so FindRoute (used by QueryRoutes) can use multi-origin, same as the payment session and the SendPaymentV2 flow.
  • Restrict routeToSelf to single-origin callers. When a multi-origin set includes the target vertex, circular routing is not the intent. Rather we want a direct route from another origin.

The findPath function previously accepted a concrete source vertex that
the path-finding loop terminated at. This introduces a RouteOrigin
interface with a single method, IsOrigin(v) bool, and replaces the
source parameter on findPath with it. The default singleOrigin
implementation wraps a single vertex and produces identical behavior
for all existing callers. RouteOrigin is the source-end counterpart to
AdditionalEdge, which extends the graph at the destination end via
route hints.

After the path-finding loop, we verify the settled node is a valid
origin before unraveling the forward path. When the heap empties without
reaching an origin, we return errNoPathFound.
findPath now returns the origin vertex it settled on as its first return
value, alongside the path and probability. For singleOrigin callers this
is always the source they passed in; for multi-origin callers it tells
them which gateway the pathfinder selected, so newRoute can set
SourcePubKey correctly. SessionSource gains an optional Origin field
which the payment session threads through as a functional option to path
finding; when unset, behavior is identical to before.
FindRoute constructs singleOrigin{req.Source} when calling findPath,
which means it always terminates at a single source vertex. An external
controller using the RouteOrigin interface for multi-source pathfinding
in the payment session has no way to apply the same behavior to
FindRoute (used by QueryRoutes).

RouteRequest now accepts an optional Origin field. When set, FindRoute
uses it instead of singleOrigin{req.Source}. When nil, behavior is
identical to before.
When a multi-origin set includes the target vertex, routeToSelf was
incorrectly set to true, disabling the guard that prevents the target
from re-entering the distance map. This allowed the target to compete
with real origins at equal cost, with the heap tiebreak producing
platform-dependent results. Restrict routeToSelf to singleOrigin
callers where circular self-payment is the actual intent.
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a flexible mechanism for multi-source pathfinding in lnd. By replacing the concrete source vertex with a RouteOrigin interface, the pathfinder can now efficiently determine the optimal route from a set of potential gateway nodes. This change is particularly beneficial for external payment controllers that manage multiple dispatch backends, allowing them to route payments through the most cost-effective gateway without requiring complex graph modifications.

Highlights

  • RouteOrigin Interface: Introduced the RouteOrigin interface to generalize route starting points, allowing the pathfinder to terminate at any vertex within a caller-provided set.
  • Multi-Source Pathfinding: Updated the pathfinding algorithm to support multi-source routing, enabling payment controllers to dispatch from multiple gateway nodes by selecting the cheapest origin.
  • API Updates: Refactored findPath, RequestRoute, and SessionSource to accept the new RouteOrigin interface, ensuring backward compatibility with existing single-source logic.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added the severity-high Requires knowledgeable engineer review label Apr 20, 2026
@github-actions
Copy link
Copy Markdown

🟠 PR Severity: HIGH

Automated classification | 4 files | 196 lines changed

🟠 High (4 files)
  • routing/pathfind.go - Payment pathfinding algorithm (routing/*)
  • routing/payment_session.go - Payment session management (routing/*)
  • routing/payment_session_source.go - Payment session source factory (routing/*)
  • routing/router.go - Core router logic (routing/*)

Analysis

All changed files are in the routing/* package, which handles payment pathfinding algorithms and is classified as HIGH severity. The changes touch core pathfinding logic (pathfind.go), payment session management (payment_session.go, payment_session_source.go), and the router itself (router.go).

Test files (pathfind_test.go, payment_session_test.go) were excluded from file/line counts per policy. The 4 non-test files change 196 lines — below the 500-line bump threshold, with only 4 files (below the 20-file threshold), and touching only one package (no multi-critical-package bump applies).


To override, add a severity-override-{critical,high,medium,low} label.
<!-- pr-severity-bot -->

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors pathfinding to support multiple origins via a new RouteOrigin interface, allowing Dijkstra's algorithm to terminate at any node in a set. This change enables multi-backend payment services to find the cheapest path from several gateway nodes. A review comment correctly identifies a potential nil pointer dereference in findPath when accessing the distance map for the selected source, suggesting a more robust check for origin presence in the map.

Comment thread routing/pathfind.go
Comment on lines 1250 to +1253
distance[source].probability, len(pathEdges),
distance[source].netAmountReceived-amt)

return pathEdges, distance[source].probability, nil
return source, pathEdges, distance[source].probability, nil
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There is a potential nil pointer dereference here if source is not present in the distance map. While the unraveling loop at line 1213 checks for presence in distance, it returns errNoPathFound if the node is missing. However, if source == target and routeToSelf is false (which can happen with a multiOrigin implementation where the target is in the origin set), the target node is never added to the distance map. In this specific case, the unraveling loop will return errNoPathFound at line 1219, so the code won't reach line 1250. Nevertheless, relying on this side effect for safety is fragile. Consider adding an explicit check or ensuring source is always in distance if it's a valid origin.

@saubyk saubyk added this to lnd v0.22 Apr 20, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in lnd v0.22 Apr 20, 2026
@saubyk saubyk moved this from Backlog to In review in lnd v0.22 Apr 20, 2026
@saubyk saubyk moved this from In review to In progress in lnd v0.22 Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

severity-high Requires knowledgeable engineer review

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

2 participants