Skip to content

Refactor Payment PR 4#9847

Merged
yyforyongyu merged 8 commits intolightningnetwork:masterfrom
ziggie1984:refactor-payments-code-04
Aug 15, 2025
Merged

Refactor Payment PR 4#9847
yyforyongyu merged 8 commits intolightningnetwork:masterfrom
ziggie1984:refactor-payments-code-04

Conversation

@ziggie1984
Copy link
Collaborator

@ziggie1984 ziggie1984 commented May 21, 2025

Depends on
#9844

This moves all data payment related data into the payment package.

The first commit is quite huge because otherwise function more changes would have been needed. Notice that we still maintain the dependence of the channeldb in the payment package. See also the codec.go file where we enhance the kv related parsing methods by the payment index.

@coderabbitai
Copy link
Contributor

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

@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-04 branch 2 times, most recently from 2538b0e to 2880b9a Compare May 21, 2025 10:56
@ziggie1984 ziggie1984 self-assigned this May 21, 2025
@ziggie1984 ziggie1984 added no-changelog payments Related to invoices/payments refactoring labels May 21, 2025
@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-04 branch 2 times, most recently from 7e1853b to 10ef9e5 Compare May 25, 2025 10:50
@ziggie1984 ziggie1984 added this to the v0.20.0 milestone May 25, 2025
@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-04 branch 2 times, most recently from c30e5fa to 3680d42 Compare May 25, 2025 15:04
@saubyk saubyk moved this to In progress in lnd v0.20 Aug 5, 2025
@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-04 branch from 3680d42 to 184a58b Compare August 12, 2025 11:36
@ziggie1984 ziggie1984 changed the base branch from master to elle-payment-sql-series August 12, 2025 11:44
@ziggie1984 ziggie1984 marked this pull request as ready for review August 12, 2025 12:06
@ziggie1984 ziggie1984 moved this from In progress to In review in lnd v0.20 Aug 12, 2025
@ziggie1984
Copy link
Collaborator Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a significant refactoring that moves payment-related logic from the channeldb package to a new payments package. The changes are largely consistent with this goal. However, I've identified a few critical issues related to incorrect function calls between packages, which will likely cause compilation errors. Specifically, the new paymentsdb package attempts to call unexported functions from channeldb and also uses the channeldb codec for types that are now handled by the paymentsdb codec. Additionally, there's a minor formatting issue in an error message that could be improved for conciseness.

@ziggie1984
Copy link
Collaborator Author

The above unit test fail because we haven't yet introduced the native sql backend code for payments which will be done in PR 6 onward.

@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-04 branch 4 times, most recently from 6bbf37b to ac2726d Compare August 13, 2025 08:28
@ziggie1984 ziggie1984 changed the base branch from elle-payment-sql-series to master August 13, 2025 10:16
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.

Looking good - think we just need to fix the CI and one more question re db initialization and removal.

invoiceBucket,
payAddrIndexBucket,
setIDIndexBucket,
paymentsIndexBucket,
Copy link
Member

Choose a reason for hiding this comment

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

does it mean we no longer init the payments db in initChannelDB or remove it in Wipe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

exactly we have a separate init method now in the kv store => initKVStore

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And the wipe method was only used in tests, which did not affect the payment related data and I don't think we currently need a wipe test on the paymentsdb package side, because the test seems not really necessary imo.

type UnknownElementType = channeldb.UnknownElementType

// ReadElement deserializes a single element from the provided io.Reader.
func ReadElement(r io.Reader, element interface{}) error {
Copy link
Member

Choose a reason for hiding this comment

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

So this is new method right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, we need it because we move the paymentIndexType definition into the payment pkg, and don't want to import the paymentsdb into the channeldb to create a circular dependancy.

@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-04 branch from ac2726d to feda7ed Compare August 13, 2025 11:00
@ziggie1984
Copy link
Collaborator Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a large refactoring that moves payment-related logic from the channeldb package into a new payments/db package. This is a significant architectural improvement that increases modularity and separation of concerns. The changes are mostly mechanical, involving moving files, updating import paths, and renaming some functions for better clarity.

My review focuses on ensuring the refactoring is clean and doesn't introduce new issues. I've identified a case of code duplication, a potential issue in a test file related to concurrent execution, and a line length violation. Overall, the refactoring is well-executed and a positive change for the codebase.

Comment on lines +461 to +493
// deserializeTime deserializes time as unix nanoseconds.
func deserializeTime(r io.Reader) (time.Time, error) {
var scratch [8]byte
if _, err := io.ReadFull(r, scratch[:]); err != nil {
return time.Time{}, err
}

// Convert to time.Time. Interpret unix nano time zero as a zero
// time.Time value.
unixNano := byteOrder.Uint64(scratch[:])
if unixNano == 0 {
return time.Time{}, nil
}

return time.Unix(0, int64(unixNano)), nil
}

// serializeTime serializes time as unix nanoseconds.
func serializeTime(w io.Writer, t time.Time) error {
var scratch [8]byte

// Convert to unix nano seconds, but only if time is non-zero. Calling
// UnixNano() on a zero time yields an undefined result.
var unixNano int64
if !t.IsZero() {
unixNano = t.UnixNano()
}

byteOrder.PutUint64(scratch[:], uint64(unixNano))
_, err := w.Write(scratch[:])

return err
}

Choose a reason for hiding this comment

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

medium

The functions deserializeTime and serializeTime are added here, but they also exist in the new payments/db/codec.go file. This appears to be code duplication introduced during the refactoring. Since these functions are related to payment serialization and are used within the new payments package, they should likely only reside in payments/db/codec.go. Please remove the duplicated functions from this file.


case channeldb.FailureReasonPaymentDetails:
case paymentsdb.FailureReasonPaymentDetails:
return lnrpc.PaymentFailureReason_FAILURE_REASON_INCORRECT_PAYMENT_DETAILS, nil

Choose a reason for hiding this comment

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

medium

This line exceeds the 80-character limit specified in the style guide.1 Please refactor it for better readability and to adhere to the coding standards.

Suggested change
return lnrpc.PaymentFailureReason_FAILURE_REASON_INCORRECT_PAYMENT_DETAILS, nil
reason := lnrpc.PaymentFailureReason_FAILURE_REASON_INCORRECT_PAYMENT_DETAILS
return reason, nil

Style Guide References

Footnotes

  1. Lines must not exceed 80 characters. Tabs should be counted as 8 spaces. (link)

}

for _, test := range tests {
test := test

Choose a reason for hiding this comment

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

medium

The line test := test has been removed. This line was likely there to prevent a race condition by creating a new variable for the closure in the subtest. If the Go version used by this project is older than 1.22, removing this line can lead to tests failing intermittently because the test variable will be captured by reference and may change before the subtest goroutine executes. Please restore this line to ensure test correctness across different Go versions.

We export some methods related to the pagination logic be the
kv store implemenation of the payment data will live in another
package.
This method is not only used by the payment logic so we need to
move it to a generalized place because in the following commits
we move payment related code into its own package.
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.

lgtm!

@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-04 branch 2 times, most recently from 19145a9 to b4419dd Compare August 14, 2025 14:39
This commit moves most of the code into its own package. It is
the smallest code move possible without moving import cycles and
keeping the changes to the code base as small as possible during
refactor.
We also rename the struct since it is now in its own package
there is no need to prefix it.
@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-04 branch from b4419dd to 2b856f0 Compare August 14, 2025 17:53
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👍

@yyforyongyu yyforyongyu merged commit dfc29b3 into lightningnetwork:master Aug 15, 2025
36 of 39 checks passed
@github-project-automation github-project-automation bot moved this from In review to Done in lnd v0.20 Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog payments Related to invoices/payments refactoring

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants