Skip to content

Conversation

@ellemouton
Copy link
Collaborator

@ellemouton ellemouton commented Jun 4, 2025

In this PR we add explicit fields for the InboundFee record on both the lnwire.ChannelUpdate message as well as on our models.ChannelEdgePolicy internal type.

Previously, we would extract the inbound fee record from the extra TLV bytes at any call-site where we needed it. But now we add explicit fields for it which will also open the way for our SQL implementation of the graph store to have an explicit column for it in the channel_policies table. It will also mean that for our SQL impl, we dont need the extra round trip of fetching TLV data in order to get the inbound fee for an update.

PR structure

We start bottom up:

  1. first, we move the extraction of inbound fee data from the extra bytes to our DB layer at deserialisation time. For any errors encountered, we just treat the edge as nil (see commit message for more details).
  2. At any call-site that obtains the edge policy from the DB, we can now just use the new explicit field instead of parsing the tlv data blob.
  3. Then, we update the lnwire.ChannelUpdate type with the new TLV record
  4. Any construction of a ChannelEdgePolicy type from an lnwire.ChannelUpdate type can now use the InboundFee field directly instead of parsing the tlv data blob.

Once this PR is merged, we can tag the TLV package and remove the replace added here in the go.mod file

Part of #9795

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 4, 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.

@ellemouton ellemouton force-pushed the inboundFeeTLV branch 3 times, most recently from 8c460c1 to 61c798b Compare June 4, 2025 12:29
@ellemouton ellemouton requested review from bhandras and guggero June 4, 2025 12:55
@saubyk saubyk added this to lnd v0.20 Jun 4, 2025
@saubyk saubyk moved this to In review in lnd v0.20 Jun 4, 2025
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Very nice! LGTM 🎉

}

if len(tlvRecords) != 0 {
a.ExtraOpaqueData = tlvRecords
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will still include the inbound type. Is that on purpose? Or should we remove the inbound fee type as we should never consume/read that from the extra opaque data from now on?
Not sure on the implications, just thinking aloud.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah this is on purpose. For v1 messages at least, we just continue to store the entire TLV blob. The same is done in channel_ready for the new NextLocalNonce record.

ellemouton added 11 commits June 9, 2025 08:26
In this commit, we start validating the extra opaque data of a channel
edge policy before persisting it. We just check that the data is valid
TLV.

NOTE: we recently [started
validating](lightningnetwork@1410a09)
this at the lnwire level. So really, no new update will reach the DB
layer without this already being checked. But we check it again here so
that the DB API behaves correctly as its own unit.
Here we add an explicit InboundFee field to the ChannelEdgePolicy
struct. Then, in the graph KVStore, at deserialisation time, we extract
the InboundFee from the ExtraOpaqueData. Currently we do this at higher
levels but we are going to move it to the DB layer so that when we add
the SQL implementation of the graph store, we can have explicit columns
for inbound fees. We need to account for the fact that we may have
invalid TLV already persisted though and we dont want to fail if we
deserialise those necessarily. So we return ErrParsingExtraTLVBytes now
if we fail to parse the extra bytes as TLV and then we let the callers
handle it similarly to how ErrParsingExtraTLVBytes is handled in that we
dont necessarily fail if we receive one of these errors.

As of this commit, we can now expect the InboundFee field of a
ChannelEdgePolicy to be set (if inbound fees are set on the policy) for
any update that we read from disk.
For any call-site where we extract inbound fees from a
models.ChannelEdgePolicy object that was deserialised from disk, we can
now just use the new InboundFee field on the object since we know that
it would have been populated at deserialisation time.

Note that for all these call-sites, if a failure previously happened on
decoding of the TLV stream, the error would be ignored and the edge
would just be skipped. This behaviour is now still the same given how
ErrParsingExtraTLVBytes is handled on the DB layer.
Like the previous commit, here we can start directly using the
InboundFee on the models.ChannelEdgePolicy object since we know we read
it from disk and so the InboundFee field will be populated accordingly.

NOTE: unlike the previous commit, behaviour is slightly different here
since previously we would error out here if TLV parsing failed whereas
now, the DB call will just skip the error and return a nil policy. This
should be ok since this is explicitly only dealing with our own updates
and so our TLV should always be valid.
In this commit, we make sure to set the new field wherever appropriate.
This will be any place where the ChannelEdgePolicy is constructed other
than its disk deserialisation.
Now that we know that the InboundFee on the ChannelEdgePolicy is always
set appropriately, we can update the GraphCache UpdatePolicy method to
take the InboundFee directly from the ChannelEdgePolicy object.
And then use that new field instead of parsing from extra opaque data.
Also add a temporariy replace to the tlv package which can be removed as
soon as the PR that includes this commit is merged and a new tag for the
tlv package has been created.
Remove the previously added TODOs which would extract InboundFee info
from the ExtraOpaqueData of a ChannelUpdate at the time of
ChannelEdgePolicy construction. These can now be replaced by using the
newly added InboundFee record on the ChannelUpdate message.
Copy link
Collaborator

@bhandras bhandras left a comment

Choose a reason for hiding this comment

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

LGTM 🌤️

var inboundFee lnwire.Fee
typeMap, err := edge.ExtraOpaqueData.ExtractRecords(&inboundFee)
if err != nil {
return edge, fmt.Errorf("%w: %w", ErrParsingExtraTLVBytes, err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Q: do we plan to do anything with this error? Right now we return it and then match everywhere if it's ErrParsingExtraTLVBytes to ignore it. Maybe we could use the same approach we already do in rpcserver.go's extractInboundFeeSafe(data lnwire.ExtraOpaqueData) lnwire.Fee and ignore it right here at parsing time? wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the thing that is nice about returning it is then we skip the whole edge rather than just leaving it as &lnwire.Fee{} and still making use of the edge.

leaving it as is lets us not load these invalid policies.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(gonna request override merge in the mean time - happy to address action items from this discussion in a follow up)

@Roasbeef Roasbeef merged commit 32592db into lightningnetwork:master Jun 10, 2025
32 of 37 checks passed
@github-project-automation github-project-automation bot moved this from In review to Done in lnd v0.20 Jun 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants