Skip to content

PoC Onion messaging using msgmux #9868

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gijswijs
Copy link
Collaborator

@gijswijs gijswijs commented May 26, 2025

In the spirit of share early, share often and also in the spirit of relatively small and incremental PRs, I'm sharing my first stab at onion messaging using the new message multiplexer package msgmux. It's derived from the custom message handling, but adjusted as needed to conform to the msgmux constraint.

Scope of this PR:

  • Receive onion messages by registering a msgmux endpoint in brontide
  • 'Handle' onion messages by dispatching the message to subscribers
  • Sending onion messages to a specified peer (The peer is passed through the endpoint; not derived from the onion message itself)
  • itest testing the above behavior

Out-of-scope (for now)

  • Onion message payload
  • Path finding
  • Blinded paths
  • Registering endpoint in brontide based on
    • onion messaging support from peers
    • active channel with peers
  • announcing onion message support through featurebit 38/39
  • rate limiting/backpressure to mitigate DoS (see queue: add new BackpressureQueue[T] variant #9838)
  • BOLT12 offer and invoice request

Copy link
Contributor

coderabbitai bot commented May 26, 2025

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.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

@gijswijs gijswijs requested review from ziggie1984 and ellemouton and removed request for ziggie1984 May 26, 2025 14:18
Copy link
Collaborator

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

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

Nice start of the onion message saga 🎉, I think the msgmux is the way to go here. I am not sure if we should use one server for all peers but if we use it we should buffer the update channel of the onionserver.

Let's now decrypt the onion message in the next PR ?

}

onionLen := len(o.OnionBlob)
if err := WriteElement(w, uint16(onionLen)); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not use varint ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think WriteUint16 is maybe even a better choice. WriteElement is about to be decommissioned so I changed the other references as well.

// onion message.
string blinding_point = 2;

// Onion is the raw serialized mix header used to relay messages in a
Copy link
Collaborator

Choose a reason for hiding this comment

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

Q: what do you mean by mix header I mean this is the body of the message why do you call it header ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

"mix header" is the term used in the original Sphinx paper. The "mix header" is the part that contains the per-hop encrypted instructions for each node along the route. I find the usage here confusing as well. (It's copy-pasta from Carla's code, but I'll take responsibility for any confusion 😄).

I'll update it to this:

// Onion is the raw serialized Sphinx onion packet containing the
// per-hop encrypted routing information used to relay this message
// through the network in a privacy-preserving manner.


// SendMessage processes the incoming onion message.
// It returns true if the message was successfully processed.
func (o *OnionEndpoint) SendMessage(ctx context.Context, msg PeerMsg) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I find the name confusing it is called SendMessage but handles Incoming onion messages maybe call it forwardToListener or something ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

server.go Outdated
@@ -5314,7 +5314,7 @@ func (s *server) applyChannelUpdate(update *lnwire.ChannelUpdate1,
func (s *server) SendCustomMessage(peerPub [33]byte, msgType lnwire.MessageType,
data []byte) error {

peer, err := s.FindPeerByPubStr(hex.EncodeToString(peerPub[:]))
peer, err := s.FindPeerByPubStr(string(peerPub[:]))
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should fix this in the FindPeerByPubStr method, we should use a proper hex-encoded string.

@@ -886,6 +890,21 @@ func (p *Brontide) Start() error {
return fmt.Errorf("unable to load channels: %w", err)
}

onionMessageEndpoint := msgmux.NewOnionEndpoint(
p.cfg.OnionMessageServer,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not sure if this is the right approach, we use 1 server-level Server with an update channel which is not buffered to funnel in all onion message from all peers, seem not really efficient ?

this is the update channel of the server:

lnd/subscribe/subscribe.go

Lines 154 to 161 in c6d6d4c

func (s *Server) SendUpdate(update interface{}) error {
select {
case s.updates <- update:
return nil
case <-s.quit:
return ErrServerShuttingDown
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure if I completely follow you critique.

We have a single subscription endpoint for onion messages (similar to custom messages) which allows for "listening in" on onion messages received by this node. We pass along the subscription server here to the msgmux endpoint so that when we handle the onion message, we can notify a subscriber of an incoming message.

Buffering or rate limiting the actual onion message itself should happen upstream of the notification server, right?

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 talked about this via other means and agreed upon the fact that we need to differentiate incoming messages so that onion-messages will not hinder channel msges to be received in time.

@gijswijs gijswijs force-pushed the onion-messaging branch 2 times, most recently from 5762808 to 9f23971 Compare June 10, 2025 15:49
@saubyk saubyk added this to the v0.20.0 milestone Jun 11, 2025
@saubyk saubyk added this to lnd v0.20 Jun 11, 2025
@saubyk saubyk moved this to In progress in lnd v0.20 Jun 11, 2025
Copy link
Collaborator

@ellemouton ellemouton left a comment

Choose a reason for hiding this comment

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

removing request for review in the mean time :) feel free to re-request when ready!

gijswijs added 3 commits July 17, 2025 21:01
This message type is a message that carries an onion-encrypted payload
used for BOLT12 messages.
This commit creates the necessary endpoints for onion messages.
Specifically, it adds the following:

- `SendOnionMessage` endpoint to send onion messages.
- `SubscribeOnionMessages` endpoint to subscribe to incoming onion
  messages.

It uses the `msgmux` package to handle the onion messages.
Copy link
Contributor

@mohamedawnallah mohamedawnallah left a comment

Choose a reason for hiding this comment

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

Thanks, @gijswijs, for putting this together! I've added some comments. Overall, I like the design and how it's seamlessly integrated with the LND system/subsystems. I can form a clear mental picture of the changes. Nice work!

return err
}

o.OnionBlob = make([]byte, onionLen)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Perhaps adding a basic sanity check about onion message length (e.g. empty one) and the onion blob size (we can utilize lnwire/lnwire.MaxMsgBody) makes the size boundary more clear


// A compile-time check to ensure OnionEndpoint implements the Endpoint
// interface.
var _ msgmux.Endpoint = (*OnionEndpoint)(nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: The interface check var _ msgmux.Endpoint = (*OnionEndpoint)(nil) is separated from the OnionEndpoint struct making it harder to see what interfaces are implemented, considering move it immediately after the OnionEndpoint definition (and perhaps considering moving OnionMessageUpdate before OnionEndpoint for better organization). With this in place perhaps this comment wouldn't have posted #9868 (comment)

type OnionEndpoint struct {
// subscribe.Server is used for subscriptions to onion messages.
onionMessageServer *subscribe.Server
}
Copy link
Contributor

@mohamedawnallah mohamedawnallah Aug 4, 2025

Choose a reason for hiding this comment

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

Q: This is for my education. It seems there is no rate limiting exists on incoming onion messages per peer which allows peers to spam unlimited onion messages potentially overwhelming node resources and message questions ?

I've already saw your comment #9868 (comment):

Buffering or rate limiting the actual onion message itself should happen upstream of the notification server, right?

Does that mean we currently get out of the box rate limiting (assuming same priority) for each endpoint we register or it is a TODO?


// Send the message as low-priority. For now we assume that all
// application-defined message are low priority.
return peer.SendMessageLazy(true, msg)
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 Nice!


var peerArr [33]byte
copy(peerArr[:], peer)
err := o.onionMessageServer.SendUpdate(&OnionMessageUpdate{
Copy link
Contributor

@mohamedawnallah mohamedawnallah Aug 4, 2025

Choose a reason for hiding this comment

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

It appears this sends updates to the subscribers without any priority control which could block subscriber notifications when processing high-volume onion messages but when we send the onion message we assign it low priority as in SendOnionMessage using SendMessageLazy. Should we enforce same/similar priority mechanism constraint here so it wouldn't interfere with probably higher priority updates?

This may overlap with this comment #9868 (comment).

@@ -5404,6 +5420,33 @@ func (s *server) SendCustomMessage(peerPub [33]byte, msgType lnwire.MessageType,
return peer.SendMessageLazy(true, msg)
}

// SendOnionMessage sends a custom message to the peer with the specified
// pubkey.
// TODO(gijs): change this message to include path finding.
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps this is the reason why the PR in draft mode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

5 participants