Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

calvinrzachman
Copy link
Contributor

@calvinrzachman calvinrzachman commented Oct 3, 2024

Background & Motivation

We'd like to allow a ChannelRouter running remotely from a set of lnd instances to find paths through the network to payment destinations. For this, I have been able to come up with 2 choices:

  1. Additional channel edges emanating from source representing fake/virtual channels: the remote ChannelRouter can have its own sense of self and think itself connected to all lnd backends via virtual channel edges. In this scenario, the channel edges emanating from the source node exist only to facilitate tricking the path finding of the ChannelRouter that its self node is actually connected to the broader network. Concerning how the ChannelRouter and pathfinding are to be informed of these edges:

    1. The additional edges remain payment session/life-cycle scoped. We could update the routerpc RPC pre-processing step to mix a dynamically maintained set of edges in with every request. OR
    2. Make the additional edges a dynamically configurable aspect of the channel graph that the ChannelRouter uses for pathfinding. NOTE: Currently trying this and it seems to work.
  2. Delegated pathfinding: the remote ChannelRouter has "multiple personalities", taking its sense of “self” to be all of the lnd backend nodes. Then, during pathfinding, it would select as the pathfinding source a given backend.

    1. This sounds like a heavier lift from an lnd code change perspective. It also seems to imply the need for load balancing across which backends are selected for the delegation in a way that is not required with existing ChannelRouter logic if the proxy's ChannelRouter believes itself to be connected to each lnd backend via virtual channel edge.

Change Description

If we use additional edges emanating from source node to trick the remote ChannelRouter into thinking that it’s connected to a set of lnd instances, then the routes/onions we build will include the lnd backend as a first hop. This presents us with a choice we can either:

  1. Build routes/onions which include the lnd instance as the first hop. Implement SendOnion such that it peels the first onion layer before forwarding to outbound peer via the Switch.
  2. Trim the first hop from the route/onion such that the first hop is the LN peer of the lnd instance. The SendOnion RPC does NOT need to peel the onion and can just forward it directly to the Switch/outbound peer.

The BuildRoute RPC provides a natural point to trim routes before sending them back to the client. 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. We may need route post-processing hooks or otherwise more first class treatment of the channel edges emanating from source so that they meet our desired criteria. This is where the RouteTransformationFunc idea comes in.

  • Allow configurable post-processing which transforms paths or routes built by the ChannelRouter. This allows a setup in which the ChannelRouter runs remotely from the HTLCSwitch to build routes as if they we're constructed from the perspective of the lnd instance itself by trimming hops. This means that the onions the ChannelRouter builds will be ready for direct delivery to the peer of an lnd backend via the SendOnion style RPC, without requiring it be peeled.
    • Could probably handle this on paymentLifecycle level rather than paymentSession as well 🤔

The RouterBackend currently assumes that the source public key for a route is always the node’s public key when calling UnmarshallRoute. This means that any reliance we have on SourcePubKey field is troubled as modifications (eg: trimming) by BuildRoute or QueryRoutes will be undone.

  • Allow for marshal/unmarshal of route objects with arbitrary source public keys. This allows for setting the correct source public key when trimming hops representing representing virtual channel edges. This allows the remote ChannelRouter to avoid maintaining any new state and instead rely on its existing ControlTower state to maintain a mapping from attempt_id --> public key of the lnd backend which we delivered the onion to via SendOnion. This allows the remote ChannelRouter to ultimately track the result of a given remote payment attempt via a TrackOnion style RPC.

The BuildRoute method relies on being given a continuous sequence of hop public keys from source to destination. The routerrpc.Server and ChannelRouter currently assume that the source public key for a route is always the node’s public key.

  • Allow BuildRoute to accept an optional override source public key. We will use the node's public key by default. This allows a remote ChannelRouter to construct routes as if they we're built from the perspective of another node.

Copy link
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.

Allow configurable post-processing which transforms paths or
routes built by the ChannelRouter. This allows a setup in which
the ChannelRouter runs remotely from the HTLCSwitch to build
routes as if they we're constructed from the perspective of the
lnd instance itself by trimming hops. This means that the onions
the ChannelRouter builds will be ready for direct delivery to the
peer of an lnd backend via the SendOnion style RPC, without
requiring it be peeled.
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.
@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
@lightninglabs-deploy
Copy link

@ellemouton: review reminder

@ellemouton
Copy link
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).


// A transformation which will be applied to every route created for
// this payment session.
routeTransform RouteTransformFunc
Copy link
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

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
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
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,

...

@@ -251,6 +251,104 @@ func TestRequestRoute(t *testing.T) {
}
}

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

Choose a reason for hiding this comment

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

missing godoc

@@ -194,14 +194,29 @@ type paymentSession struct {

// log is a payment session-specific logger.
log btclog.Logger

// A transformation which will be applied to every route created for
Copy link
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

Comment on lines +71 to +73
if m.RouteTransform != nil {
options = append(options, withRouteTransform(m.RouteTransform))
}
Copy link
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))
	})

@@ -228,6 +228,9 @@ type ChannelPolicy struct {
MinHTLC *lnwire.MilliSatoshi
}

// RouteTransformFunc defines a function type for transforming a route.
type RouteTransformFunc func(route *route.Route) *route.Route
Copy link
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?

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

Choose a reason for hiding this comment

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

can use optional

return nil, fmt.Errorf("invalid source pubkey: %w", err)
}
}

prevNodePubKey := r.SelfNode
Copy link
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
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
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

@@ -390,6 +390,12 @@ func (r *ChannelRouter) Stop() error {
return nil
}

// GetSelfNode queries the current node identity public key used by the
// ChannelRouter.
func (r *ChannelRouter) GetSelfNode() route.Vertex {
Copy link
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))

@@ -44,6 +44,8 @@

## 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
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants