Skip to content

Conversation

@ziggie1984
Copy link
Collaborator

@ziggie1984 ziggie1984 commented Nov 13, 2025

Implements the Third and Final Part of Payments SQL backend functions. Follows after
#10291

@ziggie1984 ziggie1984 changed the base branch from master to elle-payment-sql-series-new November 13, 2025 16:59
@ziggie1984 ziggie1984 added this to the v0.21.0 milestone Nov 13, 2025
@ziggie1984 ziggie1984 self-assigned this Nov 13, 2025
@ziggie1984 ziggie1984 added this to v0.21 Nov 13, 2025
@ziggie1984 ziggie1984 added payments Related to invoices/payments sql labels Nov 13, 2025
@ziggie1984 ziggie1984 moved this to In progress in v0.21 Nov 13, 2025
@ziggie1984 ziggie1984 marked this pull request as ready for review November 13, 2025 17:09
@ziggie1984 ziggie1984 force-pushed the introduce-sql-schema-payments-part-3b branch from cb9cde9 to 072544e Compare November 13, 2025 19:26
@ziggie1984 ziggie1984 moved this from In progress to In review in v0.21 Nov 13, 2025
@ziggie1984 ziggie1984 added P0 very high priority issue/PR, blocker on all others size/kilo medium, proper context needed, less than 1000 lines labels Nov 13, 2025
We wrap the fetchPayment db call and catch the case where no errors
are found in the db, where we now return the ErrPaymentNotInitiated
error.
@ziggie1984 ziggie1984 force-pushed the introduce-sql-schema-payments-part-3b branch from 072544e to 31e548f Compare November 13, 2025 20: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 implements the third and final part of the payments SQL backend functions, focusing on payment control and write operations. The changes are extensive and well-executed, including the implementation of functions for initializing, registering, settling, failing, and deleting payments. The database schema has been improved to better model the relationship between payments and payment intents, and performance is enhanced through efficient batching and lightweight queries.

I have two minor suggestions: one to fix a broken link and improve wording in the release notes, and another to address a style guide violation regarding spacing in a switch statement. Overall, this is a solid contribution to the SQL backend migration.

Comment on lines +71 to +72
* Implement third(final) Part of SQL backend [payment functions]
(https://github.com/lightningnetwork/lnd/pull/10368)

Choose a reason for hiding this comment

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

medium

The release note entry has a broken link due to a newline, and the wording could be more descriptive and consistent with other entries in this section.

Suggested change
* Implement third(final) Part of SQL backend [payment functions]
(https://github.com/lightningnetwork/lnd/pull/10368)
* Implement payment control functions for the [payments db SQL Backend](https://github.com/lightningnetwork/lnd/pull/10368)

Comment on lines +1195 to +1229
switch {
case err == nil:
paymentStatus, err := s.computePaymentStatusFromDB(
ctx, db, existingPayment,
)
if err != nil {
return fmt.Errorf("failed to compute payment "+
"status: %w", err)
}

// Check if the payment is initializable otherwise
// we'll return early.
if err := paymentStatus.initializable(); err != nil {
return fmt.Errorf("payment is not "+
"initializable: %w", err)
}

// If the initializable check above passes, then the
// existing payment has failed. So we delete it and
// all of its previous artifacts. We rely on
// cascading deletes to clean up the rest.
err = db.DeletePayment(ctx, existingPayment.Payment.ID)
if err != nil {
return fmt.Errorf("failed to delete "+
"payment: %w", err)
}

case !errors.Is(err, sql.ErrNoRows):
// Some other error occurred
return fmt.Errorf("failed to check existing "+
"payment: %w", err)

// The payment does not yet exist.
default:
}

Choose a reason for hiding this comment

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

medium

The switch statement in the InitPayment function is missing blank lines between case blocks, which is required by the repository style guide for code spacing. Adding blank lines and descriptive comments for each case will improve readability and adhere to the style guide.1

switch {
		// A payment with this hash already exists. We need to check its
		// status to see if we can re-initialize.
		case err == nil:
			paymentStatus, err := s.computePaymentStatusFromDB(
				ctx, db, existingPayment,
			)
			if err != nil {
				return fmt.Errorf("failed to compute payment "+
					"status: %w", err)
			}

			// Check if the payment is initializable otherwise
			// we'll return early.
			if err := paymentStatus.initializable(); err != nil {
				return fmt.Errorf("payment is not "+
					"initializable: %w", err)
			}

			// If the initializable check above passes, then the
			// existing payment has failed. So we delete it and
			// all of its previous artifacts. We rely on
			// cascading deletes to clean up the rest.
			err = db.DeletePayment(ctx, existingPayment.Payment.ID)
			if err != nil {
				return fmt.Errorf("failed to delete "+
					"payment: %w", err)
			}

		// An unexpected error occurred while fetching the payment.
		case !errors.Is(err, sql.ErrNoRows):
			return fmt.Errorf("failed to check existing "+
				"payment: %w", err)

		// The payment does not yet exist, so we can proceed.
		default:
		}

Style Guide References

Footnotes

  1. The style guide requires spacing between case stanzas in a switch statement for better readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P0 very high priority issue/PR, blocker on all others payments Related to invoices/payments size/kilo medium, proper context needed, less than 1000 lines sql

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

1 participant