Skip to content

lnrpc: add HtlcIndex to ForwardingEvents #9813

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

Merged
merged 3 commits into from
Jun 6, 2025

Conversation

Abdulkbk
Copy link
Contributor

@Abdulkbk Abdulkbk commented May 15, 2025

Change Description

Fixes #9656

This PR adds incoming and outgoing HTLC indices fields to the ForwardingEvent to improve HTLC tracking and reconciliation.

When creating forwarding events, the HTLC indices fields are populated using the incoming and outgoing HTLCId from the payment circuit.

Steps to Test

  • Start LND node
  • Route payments through the node
  • Verify that the incoming and outgoing htlc indices of the payments' HTLC are present in the response from lncli fwdinghistory

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

Copy link
Contributor

coderabbitai bot commented May 15, 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.

@Abdulkbk Abdulkbk force-pushed the htlcidx-to-fwdingevent branch from f4c5fd9 to 009584f Compare May 15, 2025 22:29
@guggero
Copy link
Collaborator

guggero commented May 16, 2025

Thanks for the PR.
Just a heads up, this won't work like that. For everything that touches the database, you need to assume there's already data in there with the old format. So we either have to write a migration script or deal with the fact that you might get an EOF error when reading a new field.

@Abdulkbk
Copy link
Contributor Author

Thanks for the PR. Just a heads up, this won't work like that. For everything that touches the database, you need to assume there's already data in there with the old format. So we either have to write a migration script or deal with the fact that you might get an EOF error when reading a new field.

Thanks for the heads up. I will check how this was handled before in previous PRs. For now, I will change this to a draft.

@Abdulkbk Abdulkbk marked this pull request as draft May 16, 2025 08:20
@Abdulkbk Abdulkbk force-pushed the htlcidx-to-fwdingevent branch from 009584f to cb09e8c Compare May 19, 2025 12:48
@Abdulkbk Abdulkbk marked this pull request as ready for review May 19, 2025 12:54
@Abdulkbk Abdulkbk force-pushed the htlcidx-to-fwdingevent branch from cb09e8c to a775db2 Compare May 19, 2025 12:55
@Abdulkbk
Copy link
Contributor Author

Thanks for the PR. Just a heads up, this won't work like that. For everything that touches the database, you need to assume there's already data in there with the old format. So we either have to write a migration script or deal with the fact that you might get an EOF error when reading a new field.

Thanks again for the early feedback, @guggero. I thought about writing a migration script or handling the EOF error, but I decided to handle the EOF error when decoding the events (Let me know if a migration is preferred instead).

Also, does it make sense to set a default value of "0" when returning older event logs?. This means if you execute fwdinghistory and you have events before this change, you'd a get a bunch returned with HtlcIndex set to 0. But I'm thinking maybe updating the command's docs to explicitly state this would help?

@guggero
Copy link
Collaborator

guggero commented May 20, 2025

Definitely looks better now, thanks for the update. I think because HtlcIndex: 0 is a valid value, we shouldn't use that. We should probably use a fn.Option to signify whether it's set or not (and then on the RPC level we can use the optional keyword, which makes the value nilable).

Copy link
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

For forwarding events we need to have both incoming and outgoing htlc index.

A migration would be nice, tho I don't think it's possible since we cannot get the htlc index info. Alternative to fn.Option, we can use a tlv record instead, sth like tlv.BigSizeT to save the space since for a busy node the logs can be huge.

We should also update the RPC docs, and release docs to mention this behavior.

rpcserver.go Outdated
@@ -8060,6 +8060,7 @@ func (r *rpcServer) ForwardingHistory(ctx context.Context,
FeeMsat: uint64(feeMsat),
AmtInMsat: uint64(amtInMsat),
AmtOutMsat: uint64(amtOutMsat),
HtlcIndex: event.HtlcIndex,
Copy link
Member

Choose a reason for hiding this comment

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

let's also update the test here to check the change,

fwdingHistory := dave.RPC.ForwardingHistory(nil)
require.Len(ht, fwdingHistory.ForwardingEvents, numPayments)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to check for the change 👍

@Abdulkbk Abdulkbk force-pushed the htlcidx-to-fwdingevent branch 2 times, most recently from a6343fe to 5b070cb Compare May 21, 2025 13:08
@Abdulkbk
Copy link
Contributor Author

Thanks for the PR. Just a heads up, this won't work like that. For everything that touches the database, you need to assume there's already data in there with the old format. So we either have to write a migration script or deal with the fact that you might get an EOF error when reading a new field.

Now using fn.Option 👍

@Abdulkbk
Copy link
Contributor Author

Thanks for the PR!

For forwarding events we need to have both incoming and outgoing htlc index.

A migration would be nice, tho I don't think it's possible since we cannot get the htlc index info. Alternative to fn.Option, we can use a tlv record instead, sth like tlv.BigSizeT to save the space since for a busy node the logs can be huge.

We should also update the RPC docs, and release docs to mention this behavior.

Thanks for the review @yyforyongyu.

The tlv is quite new to me, but I've done some research on it, and I want to clarify something. By using tlv.BigSizeT, do you mean doing something like below here:

....
       // IncomingHtlcID is the ID of the incoming HTLC in the payment circuit.
	// If the HTLC ID is not set, the value will be nil.
	IncomingHtlcID tlv.OptionalRecordT[tlv.TlvType0, tlv.BigSizeT[uint64]]

	// OutgoingHtlcID is the ID of the outgoing HTLC in the payment circuit.
	// If the HTLC ID is not set, the value will be nil.
	OutgoingHtlcID tlv.OptionalRecordT[tlv.TlvType0, tlv.BigSizeT[uint64]]
...

and then checking if the indices are set in the encode and decode functions?

@Abdulkbk Abdulkbk requested a review from yyforyongyu May 27, 2025 06:20
Copy link
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

Changes looking good! My main comment is to provide more detailed docs, and minor suggestion on the tests.

The tlv is quite new to me, but I've done some research on it, and I want to clarify something.

Yeah sth like that, I've played a bit locally without the optional record, here's the diff. Previously I was concerned about the size of the records, but since each TLV record requires extra 2 bytes to store the type and length info, plus one byte to store the total length of the TLV records, in this specific case, it would require extra 5 bytes. If the value stored is less than 0xffffffff (roughly 4.3 billion), then each htlc index takes 5 bytes, so a total of 5*2 + 5 = 15 bytes, which takes one byte less than two uint64.

However I don't think it's necessary to use TLV here now, as I'm looking forward to sunset the forwarding log bucket and relying on revocation log only one day - the latter plus the channel commitments gives us enough info to construct the forwarding events on the fly, maybe a simple query would be sufficient once we've done SQLizing the channeldb.

Also not requesting for changes, but just give you my thoughts on the most proper way of handling adding new fields, for this specific case,

  • add new fields, without the fn.Option.
  • define a flag, sth like NoHtlcIndexValue = math.MaxUint64.
  • create a migration to migrate all forwarding events using this flag.
  • at the RPC level, check the index value, and only set the optional field there if the value is not the above flag value.

This way we can avoid all the if-else branches and catching EOF in our serializer/deserializer, tho there's a bit more work to create the migration.

return err
}

// Then we write the optional HTLC IDs if they are set.
Copy link
Member

Choose a reason for hiding this comment

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

To be more defensive - I would return an error here if f.IncomingHtlcID.IsNone() to make it clear that they are mandatory.

Copy link
Contributor Author

@Abdulkbk Abdulkbk May 28, 2025

Choose a reason for hiding this comment

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

Makes sense since new events now should always have them. Now returning an error if the fields are not set.

@@ -213,6 +214,17 @@ func testMultiHopPayments(ht *lntest.HarnessTest) {
require.Equal(ht, aliceAlias, event.PeerAliasOut)
}

// Verify HTLC IDs are not nil and unique across all forwarding events.
Copy link
Member

Choose a reason for hiding this comment

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

Nice addition! I think we can make this test more rigorous by asserting the htlc ids are exactly [0, 1, 2, 3, 4].

Copy link
Contributor Author

@Abdulkbk Abdulkbk May 28, 2025

Choose a reason for hiding this comment

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

I tried using require.Equal to compare the htlc indices strictly, the itest passed locally but all failed in the CI, seems it's a bit hard to track the exact indices.

Copy link
Member

Choose a reason for hiding this comment

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

yeah the events order is not guaranteed, so we'll need to collect the indices in a map and compare them after the iteration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now comparing the indices

@saubyk saubyk added this to the v0.20.0 milestone May 27, 2025
@saubyk saubyk added this to lnd v0.20 May 27, 2025
@saubyk saubyk moved this to In progress in lnd v0.20 May 27, 2025
@Abdulkbk Abdulkbk force-pushed the htlcidx-to-fwdingevent branch 5 times, most recently from 90b2f24 to f06a99c Compare May 28, 2025 13:05
@Abdulkbk
Copy link
Contributor Author

Thanks again for the review @yyforyongyu 👍.

Changes looking good! My main comment is to provide more detailed docs, and minor suggestion on the tests.

Added more details to the doc, giving more context to this change.

Also not requesting for changes, but just give you my thoughts on the most proper way of handling adding new fields, for this specific case,

add new fields, without the fn.Option.
define a flag, sth like NoHtlcIndexValue = math.MaxUint64.
create a migration to migrate all forwarding events using this flag.
at the RPC level, check the index value, and only set the optional field there if the value is not the above flag value.
This way we can avoid all the if-else branches and catching EOF in our serializer/deserializer, tho there's a bit more work to create the migration.

Thanks for the information. I will certainly keep this in mind!

@Abdulkbk Abdulkbk requested a review from yyforyongyu June 2, 2025 04:35
Copy link
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

LGTM🌹 (Need to squash that fixup commit before merging ofc)

@@ -213,6 +214,17 @@ func testMultiHopPayments(ht *lntest.HarnessTest) {
require.Equal(ht, aliceAlias, event.PeerAliasOut)
}

// Verify HTLC IDs are not nil and unique across all forwarding events.
Copy link
Member

Choose a reason for hiding this comment

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

yeah the events order is not guaranteed, so we'll need to collect the indices in a map and compare them after the iteration.

@Abdulkbk
Copy link
Contributor Author

Abdulkbk commented Jun 4, 2025

LGTM🌹 (Need to squash that fixup commit before merging ofc)

It will be squashed (used fixup because the contribution guide says it's preferable)

@guggero
Copy link
Collaborator

guggero commented Jun 5, 2025

The doc mentions "auto squash the fix up commits on rebase". Which is something the PR author needs to do before merge, upon request by the reviewers, which is what @yyforyongyu meant.
We could use "rebase and merge", but that would mean all the commits have a different committer because they are changed by GitHub (which we don't prefer). So yeah, maybe we could make the docs a bit more clear.

@guggero guggero self-requested a review June 5, 2025 06:53
@Abdulkbk Abdulkbk force-pushed the htlcidx-to-fwdingevent branch 3 times, most recently from f105265 to 1a7298d Compare June 5, 2025 12:57
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! Looks good, just two ideas/requests.

Abdulkbk added 3 commits June 6, 2025 12:21
In this commit we add htlcindex field to the forwardingevent
struct, which is persisted alongside the other event fields.
In this commit we update the returned message for fwdinghistory
to include the htlcindex for all forwarded htlcs.
@Abdulkbk Abdulkbk force-pushed the htlcidx-to-fwdingevent branch from 1a7298d to ccf3a28 Compare June 6, 2025 11:26
@guggero guggero merged commit e1c9825 into lightningnetwork:master Jun 6, 2025
34 of 37 checks passed
@github-project-automation github-project-automation bot moved this from In progress to Done in lnd v0.20 Jun 6, 2025
@@ -78,14 +81,44 @@ type ForwardingEvent struct {
// AmtOut is the amount of the outgoing HTLC. Subtracting the incoming
// amount from this gives the total fees for this payment circuit.
AmtOut lnwire.MilliSatoshi

// IncomingHtlcID is the ID of the incoming HTLC in the payment circuit.
// If this is not set, the value will be nil. This field is added in
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not understand this comment, this value cannot be nil, it is of type fn.Option.

Copy link
Collaborator

Choose a reason for hiding this comment

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

probably you meant in lnrpc.ForwardingEvent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do not understand this comment, this value cannot be nil, it is of type fn.Option.

Since fn.Option represents a value that may or may not be present, I thought I could refer to it as nil when the value isn't present?. But I think if you find it confusing then many would too, which means I should've framed this better.

IncomingHtlcID fn.Option[uint64]

// OutgoingHtlcID is the ID of the outgoing HTLC in the payment circuit.
// If this is not set, the value will be nil. This field is added in
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here

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.

[feature]: Add htlc_id to lnrpc.HTLCAttempt and lnrpc.ForwardingEvent
5 participants