Skip to content

Conversation

ziggie1984
Copy link
Collaborator

@ziggie1984 ziggie1984 commented Aug 13, 2025

Depends on #9847

This introduces the paymentDB interfaces and moves all tests from the kv store implementation to the db agnostic place where they can run against both backends (kv and native sql)

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.

Summary of Changes

Hello @ziggie1984, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request undertakes a significant refactoring effort by introducing a new PaymentDB interface to abstract the underlying payment database operations. This change decouples various system components, such as the RPC server and the control tower, from the concrete Key-Value payment database implementation. The refactoring enhances modularity, improves testability by allowing different database implementations to be swapped in, and lays the groundwork for future architectural improvements.

Highlights

  • Introduction of Payment Database Interface: A new paymentsdb.PaymentDB interface, along with PaymentReader, PaymentWriter, and PaymentControl sub-interfaces, has been introduced to define standard payment database operations. This provides a clear contract for how payment data is accessed and modified.
  • Decoupling of Core System Components: Key components of the system, including config_builder, routing/control_tower, rpcserver, and the main server, have been updated to interact with the payment database solely through the new PaymentDB interface. This significantly reduces coupling and improves architectural flexibility.
  • Comprehensive Test Reorganization and Refinement: Payment-related tests have been thoroughly reorganized. Generic tests that operate on the PaymentDB interface have been moved to payments/db/payment_test.go, promoting interface-driven testing. Tests specific to the Key-Value implementation remain in payments/db/kv_store_test.go, and test database creation helpers were adjusted to support this separation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 introduces a PaymentDB interface to abstract the payment database implementation, which is a solid refactoring step. The changes are consistently applied across the codebase, and the test files have been reorganized logically. My feedback is focused on ensuring documentation comments adhere to the project's style guide.

@ziggie1984 ziggie1984 self-assigned this Aug 13, 2025
@ziggie1984 ziggie1984 added no-changelog payments Related to invoices/payments labels Aug 13, 2025
@ziggie1984 ziggie1984 added this to the v0.20.0 milestone Aug 13, 2025
@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-05 branch from 3e885a4 to ee81e16 Compare August 13, 2025 13:07
@ziggie1984 ziggie1984 marked this pull request as ready for review August 13, 2025 13:10
@ziggie1984 ziggie1984 moved this to In review in lnd v0.20 Aug 13, 2025
@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-05 branch from ee81e16 to 59a2c71 Compare August 13, 2025 20:31
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.

very nice PR 🙏

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.

Cool very clean PR🧹 I think the ControlTower interface can be removed given that we have the new interface?

@ziggie1984 ziggie1984 changed the base branch from elle-payment-sql-series to master August 15, 2025 08:42
@ziggie1984 ziggie1984 force-pushed the refactor-payments-code-05 branch 2 times, most recently from 02f655b to ed327b9 Compare August 19, 2025 15:26
@ziggie1984 ziggie1984 requested a review from yyforyongyu August 19, 2025 15:27
@ziggie1984
Copy link
Collaborator Author

@gemini-code-assist 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 introduces a significant and well-executed refactoring of the payment database logic. By introducing a paymentsdb.DB interface, it successfully decouples the payment system from the underlying kvdb implementation, paving the way for alternative backends like native SQL. Moving the tests from the kv_store to a backend-agnostic location is a great step towards ensuring correctness across different database implementations.

My review includes a couple of suggestions to improve test code clarity and documentation, but overall the changes are solid and align well with the stated goals.

Comment on lines +25 to +27
// NewKVTestDB is a helper function that creates an BBolt database for testing
// and there is no need to convert the interface to the KVStore because for
// some unit tests we still need access to the kvdb interface.

Choose a reason for hiding this comment

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

medium

The comment for NewKVTestDB has a minor typo ("an BBolt" should be "a bbolt") and could be rephrased for better clarity.

Suggested change
// NewKVTestDB is a helper function that creates an BBolt database for testing
// and there is no need to convert the interface to the KVStore because for
// some unit tests we still need access to the kvdb interface.
// NewKVTestDB is a helper function that creates a bbolt database for testing.
// It returns the concrete `KVStore` type, which is useful for tests that need
// access to the underlying `kvdb` interface.

@ziggie1984
Copy link
Collaborator Author

Cool very clean PR🧹 I think the ControlTower interface can be removed given that we have the new interface?

I don't think we can, because the ControlTower has a slightly different abstraction level. For most of the interface method the ControlTower returns the Attempt rather than the payment for example:

paymentsdb:

SettleAttempt(lntypes.Hash, uint64, *HTLCSettleInfo) (*MPPayment, error)

whereas the ControlTower:

SettleAttempt(lntypes.Hash, uint64, *paymentsdb.HTLCSettleInfo) ( *paymentsdb.HTLCAttempt, error)

plus the interface gives us good mocking/testing possibilities, wdyt ?

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.

plus the interface gives us good mocking/testing possibilities, wdyt ?

Given that the control tower holds the paymentsdb.DB, it already provides mocking as when testing, we can just create a mock db and attach it to the control tower. I think to make the layer clearer,

  • at the bottom we have the db layer
  • on top of it we have the control tower layer, which provides notifications
    I think the control tower interface can embed the payment control interface, but yeah the changes may be large so some future refactoring PR would do.

Only got one final question re the payment controller interface, otherwise this is good to go.

// its database operations. This interface represents the control flow of how
// a payment should be handled in the database. They are not just writing
// operations but they inherently represent the flow of a payment. The methods
// are called in the following order.
Copy link
Member

Choose a reason for hiding this comment

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

nice - based on the reasoning here, DeleteFailedAttempts should also be part of PaymentControl?

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 moved the function as well to the PaymentControl interface

This commit starts reusing test cases which are not dependant on
the kv db backend. So they can be later used with the native db
implementation as well.
we make the index assertion db independant so it is a noop for
a future native sql backend. This allows us to reuse even more
tests for the different db architectures.
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 8f489d0 into lightningnetwork:master Aug 20, 2025
37 of 39 checks passed
@github-project-automation github-project-automation bot moved this from In review to Done in lnd v0.20 Aug 20, 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
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants