Skip to content

(routing+lnrpc): arbitrary route source + route transformation#9153

Open
calvinrzachman wants to merge 3 commits intolightningnetwork:elle-base-branch-payment-servicefrom
calvinrzachman:route-manipulation
Open

(routing+lnrpc): arbitrary route source + route transformation#9153
calvinrzachman wants to merge 3 commits intolightningnetwork:elle-base-branch-payment-servicefrom
calvinrzachman:route-manipulation

Conversation

@calvinrzachman
Copy link
Copy Markdown
Collaborator

@calvinrzachman calvinrzachman commented Oct 3, 2024

Change Description

BuildRoute and UnmarshallRoute assume the source public key for a route is always the node's own key. This makes it impossible to construct or deserialize routes from the perspective of a different node.

This PR adds a source_pub_key field to the Route proto message and updates BuildRoute to accept an optional source override. When set, the route is constructed from the perspective of the provided source node — the
hop sequence must be connected to that source via channels in the graph. When unset, the node's own public key is used as before. UnmarshallRoute respects the field so that routes round-trip correctly.

This is useful independently for any tooling that needs to reason about routes from an arbitrary vantage point. It also pairs with the RouteOrigin generalization in #10764 which handles multi-source pathfinding for the SendPaymentV2 flow.

  • Add source_pub_key field to Route proto message in both lightning.proto and router.proto.
  • Update UnmarshallRoute to use the provided source key when set, defaulting to SelfNode when empty.
  • Update BuildRoute to accept a source node parameter instead of always using SelfNode.
  • Add tests for UnmarshallRoute with arbitrary source keys.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Oct 3, 2024

Important

Review skipped

Auto reviews are limited to specific labels.

🏷️ Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@calvinrzachman calvinrzachman force-pushed the route-manipulation branch 2 times, most recently from ad747b3 to 0dd29b2 Compare October 4, 2024 13:39
@calvinrzachman calvinrzachman marked this pull request as ready for review October 4, 2024 13:40
@ellemouton ellemouton self-requested a review October 7, 2024 06:40
@ellemouton
Copy link
Copy Markdown
Collaborator

High level thoughts

thanks for the detailed outline. I guess I had initially thought we could have a new very abstract "virtual channel" type that the remote channel router could open with each of the LND nodes. Each LND node can be given an allow-list of who to accept such a channel from. If that abstraction could be possible, then the underlying method of sending should be able work as is today right? Ie channel router just builds the routes and sends directly to switch which uses UpdateAddHTLC to communicate the send. Then the LND node will see the incoming UpdateAddHTLC from an upstream virtual channel.

Anyways - that is just what I thought the plan was. But perhaps getting channels to be that abstract is a bit of a tall order.

Re the PR description

Supporting the SendPaymentV2 flow from the remote ChannelRouter is a bit more involved as it does NOT have the ability to transform the route prior to onion construction and payment dispatch

I think I lost you a bit at this point. are you saying that the remove router will call SendPayment on the LND nodes? I thought a new SendOnion endpoint was the idea? Or are you saying that the SendPayment is called on the channelrouter? If the former, then im not sure why the real LND node needs to transform the route?

Yeah I think I lost you after this point. It would help to have a step by step flow (ie, what is calling what when and what is doing what transformation when etc etc).

Comment thread routing/payment_session.go Outdated

// A transformation which will be applied to every route created for
// this payment session.
routeTransform RouteTransformFunc
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can use fn.Option instead so that we dont have to remember to do a nil check

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Updated routeTransform to fn.Option[RouteTransformFunc] on both sessionOptions and SessionSource

Comment thread routing/payment_session.go Outdated
Comment on lines +203 to +210
// option is a functional option for configuring the payment session.
type option func(*paymentSession)

// withRouteTransform sets a route transformation function.
func withRouteTransform(routeTransform RouteTransformFunc) option {
return func(ps *paymentSession) {
ps.routeTransform = routeTransform
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

best practice is to have a separate paySessOptions struct with the specific things we all a caller to "edit".

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

so I'd do:

// paySessOptions holds ...
type paySessOptions struct {
	// routeTransform is ...
	routeTransform fn.Option[RouteTransformFunc]
}

// newDefaultPaySessOptions constructs...
func newDefaultPaySessOptions() *paySessOptions {
	return &paySessOptions{}
}

// paySessOption is a functional option for configuring the payment session.
type paySessOption func(*paySessOptions)

// withRouteTransform sets a route transformation function.
func withRouteTransform(routeTransform RouteTransformFunc) paySessOption {
	return func(ps *paySessOptions) {
		ps.routeTransform = fn.Some(routeTransform)
	}
}

...
type paymentSession struct {
	opts *paySessOptions
...


...
options := newDefaultPaySessOptions()
for _, opt := range opts {
	opt(options)
}

return &paymentSession{
	opts:              options,

...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

added sessionOptions struct with defaultSessionOptions() constructor and withRouteTransform as a functional option.

Comment thread routing/payment_session_test.go Outdated
}
}

func TestRouteTransformFunc(t *testing.T) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

missing godoc

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

added a doc in here.

Comment thread routing/payment_session.go Outdated
// log is a payment session-specific logger.
log btclog.Logger

// A transformation which will be applied to every route created for
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

comment should start with name of variable

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

updated

Comment thread routing/payment_session_source.go Outdated
Comment on lines +71 to +73
if m.RouteTransform != nil {
options = append(options, withRouteTransform(m.RouteTransform))
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

        var options []paySessOption
	m.RouteTransform.WhenSome(func(fn RouteTransformFunc) {
		options = append(options, withRouteTransform(fn))
	})

Comment thread routing/router.go Outdated
}

// RouteTransformFunc defines a function type for transforming a route.
type RouteTransformFunc func(route *route.Route) *route.Route
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

do we definitely want the flexibility to transform the entire route? It's a bit of a red flag to me cause we are depending on this module to construct a route but then we also as a caller are able to just replace that entire route... so can we not restrict the options a bit? Like WithPeeledFirstNode and things like that?

Comment thread lnrpc/lightning.proto
The source node from whose perspective the route was built. If empty,
self is assumed.
*/
string source_pub_key = 9;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can use optional

Comment thread lnrpc/routerrpc/router_backend.go Outdated
}
}

prevNodePubKey := r.SelfNode
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

prevNodePubKey is not necessarily selfnode anymore

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we should add tests for this change to make sure that things like this would be caught.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

gotta make sure that everywhere where r.SelfNode is used is still correct given this commit

Comment thread routing/router.go Outdated

// GetSelfNode queries the current node identity public key used by the
// ChannelRouter.
func (r *ChannelRouter) GetSelfNode() route.Vertex {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can just call it SelfNode. Getter is implied (unless it clashes with a member in ChannelRouter (which I dont think it does))


## RPC Updates

* [Allow arbitrary source public key for BuildRoute RPC](https://github.com/lightningnetwork/lnd/pull/9153) so that lnd's _*ChannelRouter*_ and _*routerrpc.Server*_ can build routes from the perspective of different nodes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

wrap at 80 🙏
(while we are here, we can fix the wrapping of the entry above (for 9017) as well)

@lightninglabs-deploy
Copy link
Copy Markdown
Collaborator

@calvinrzachman, remember to re-request review from reviewers when ready

@yyforyongyu
Copy link
Copy Markdown
Member

!lightninglabs-deploy mute

@saubyk saubyk added this to v0.21 Dec 1, 2025
@saubyk saubyk moved this to In progress in v0.21 Dec 1, 2025
@calvinrzachman calvinrzachman changed the base branch from master to elle-base-branch-payment-service March 24, 2026 01:45
@calvinrzachman calvinrzachman force-pushed the route-manipulation branch 2 times, most recently from 23ff296 to f8cc659 Compare March 24, 2026 02:19
@bitromortac bitromortac self-requested a review March 25, 2026 20:31
@saubyk saubyk added this to lnd v0.22 Mar 30, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in lnd v0.22 Mar 30, 2026
@saubyk saubyk moved this from Backlog to In review in lnd v0.22 Mar 30, 2026
@saubyk saubyk removed this from v0.21 Mar 30, 2026
Copy link
Copy Markdown
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🍠

Comment thread routing/payment_session.go Outdated
return route, err
// Apply the route transformation, if provided.
var transformErr error
p.opts.routeTransform.WhenSome(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the point brought up below (why can't the caller transform it themselves?) may still be relevant. Still getting through the diff.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On second thought, the payment session has awareness of this as it's the one that's constructing the route in the first place. The caller typically makes the session, then exits (eg: one time payment send), so it would need some lower level hook somewhere in the process.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, for the QueryRoutes/BuildRoute --> SendToRouteV2 flow where an external RPC client obtains the route from lnd there is ample opportunity for the client to manipulate the route however they desire. The routerrpc.SendPaymentV2 handles the path-finding, onion construction, dispatch, etc. all internally and so and external RPC client has no opportunity to manipulate the route. The lower level hook helps out with this. Though notably, I think this design (#10764) obviates the need for any route transform hooks. If it's deemed suitable - it also simplifies the the client side implementation no longer requiring the addition of additional edges on the source side to the underlying channel graph used by the router.

The RouterBackend assumes that the source public
key for a route is always the node’s public key
when calling UnmarshallRoute.

Here we update to allow overriding of this field
to any arbitrary public key.
The routerrpc.Server and ChannelRouter assume that the source
public key for a route is always the node’s public key when calling
BuildRoute.

We update to allow overriding of this field to any arbitrary public
key. The list of hop public keys must be connected to the source
via channels in our graph view.
Update release notes for public facing RPC change.
@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 | 9 files (excl. tests/generated) | ~90 lines changed

🟠 High (8 files)
  • lnrpc/routerrpc/router_backend.go - RPC backend logic (lnrpc/*)
  • lnrpc/routerrpc/router_server.go - RPC server (lnrpc/*)
  • lnrpc/switchrpc/switch.swagger.json - Switch RPC API (lnrpc/*)
  • lnrpc/lightning.proto - Core Lightning RPC API (lnrpc/*)
  • lnrpc/lightning.swagger.json - Core Lightning swagger (lnrpc/*)
  • lnrpc/routerrpc/router.proto - Router RPC API (lnrpc/*)
  • lnrpc/routerrpc/router.swagger.json - Router swagger (lnrpc/*)
  • routing/router.go - Payment pathfinding (routing/*)
🟢 Low (1 file)
  • docs/release-notes/release-notes-0.21.0.md - Release notes
⚪ Excluded (4 files)
  • lnrpc/lightning.pb.go - auto-generated
  • lnrpc/routerrpc/router.pb.go - auto-generated
  • lnrpc/routerrpc/router_backend_test.go - test file
  • routing/router_test.go - test file

Analysis: This PR modifies lnrpc/* RPC/API definitions and routing/router.go, both HIGH severity. Changes include proto file additions (API surface changes), swagger updates, and backend/server implementation changes, indicating a new RPC feature. Only 9 non-excluded files and ~90 lines, so no severity bump. A knowledgeable engineer should review proto/API changes for backwards compatibility and routing logic for correctness.


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

payments-v2 severity-high Requires knowledgeable engineer review

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

6 participants