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

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

Conversation

Abdulkbk
Copy link
Contributor

@Abdulkbk Abdulkbk commented May 15, 2025

Change Description

Fixes #9656

This PR adds a new HtlcIndex field to the ForwardingEvent to improve HTLC tracking and reconciliation.

The HtlcIndex field is populated using the incoming HtlcId from the circuit when creating forwarding events. This allows for matching HTLCs between channel state and forwarding log.

To ensure everything works well together, we handle EOF errors by setting the HtlcIndex field to a default value of "0" for older ForwardingEvents that do not have this field.

Steps to Test

  • Start LND node
  • Route payments through the node
  • Verify that the HtlcIndex 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.

@@ -4710,6 +4710,10 @@ message ForwardingEvent {
// The peer alias of the outgoing channel.
string peer_alias_out = 13;

// The index of the HTLC in the channel. This allows for better tracking
Copy link
Member

Choose a reason for hiding this comment

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

nit: typo in the commit fwdinghiatory -> fwdinghistory

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed

@@ -3074,6 +3074,7 @@ func (s *Switch) handlePacketSettle(packet *htlcPacket) error {
OutgoingChanID: circuit.Outgoing.ChanID,
AmtIn: circuit.IncomingAmount,
AmtOut: circuit.OutgoingAmount,
HtlcIndex: circuit.Incoming.HtlcID,
Copy link
Member

Choose a reason for hiding this comment

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

For forwarding events I think we need to have both indices? like what we have in the htlcPacket, which has both incomingHTLCID and outgoingHTLCID. Or like the RPC routerrpc.HtlcEvent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

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 👍


// TestForwardingLogDecodeForwardingEvent tests that we're able to decode
// forwarding events that don't have the new HtlcIndex field.
func TestForwardingLogDecodeForwardingEvent(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

👍

In this commit we add htlcindex field to the forwardingevent
struct, which is persisted alongside the other event fields.
@Abdulkbk Abdulkbk force-pushed the htlcidx-to-fwdingevent branch from a775db2 to a6343fe Compare May 21, 2025 11:48
Abdulkbk added 2 commits May 21, 2025 14:08
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 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?

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
3 participants