-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Refactor Payment PR 5 #10153
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
yyforyongyu
merged 16 commits into
lightningnetwork:master
from
ziggie1984:refactor-payments-code-05
Aug 20, 2025
Merged
Refactor Payment PR 5 #10153
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9f824fe
multi: introduce interface for payment database
ziggie1984 46500f9
multi: fix comment of InitPayment method
ziggie1984 39b7417
paymentsdb: use querypayments method to make test db agnostic
ziggie1984 e22b898
paymentsdb: move db interface dependant tests to different file
ziggie1984 8726ba3
paymentsdb: move more tests
ziggie1984 68a8cf1
paymentsdb: rename assertPayments
ziggie1984 a1fc8a3
paymentsdb: rename db agnostic tests to highlight their behaviour
ziggie1984 7423bfe
paymentsdb: rename assertPaymentstatus
ziggie1984 8245e35
paymentsdb: move serialization methods to kv_store file
ziggie1984 6abd539
paymentsdb: add missing function comments
ziggie1984 82242f5
multi: rename KVPaymentDB to KVStore
ziggie1984 f87841d
paymentsdb: declare helper functions and add comments
ziggie1984 9ac93e7
paymentsdb: fix linter
ziggie1984 b16782c
multi: move DBMPPayment to paymentsdb package
ziggie1984 039b599
routing: add more comments to the ControlTower interface
ziggie1984 bf6131b
routing: Add comment to DeleteFailedAttempts func call
ziggie1984 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package paymentsdb | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/lightningnetwork/lnd/lntypes" | ||
) | ||
|
||
// DB represents the interface to the underlying payments database. | ||
type DB interface { | ||
PaymentReader | ||
PaymentWriter | ||
} | ||
|
||
// PaymentReader represents the interface to read operations from the payments | ||
// database. | ||
type PaymentReader interface { | ||
// QueryPayments queries the payments database and should support | ||
// pagination. | ||
QueryPayments(ctx context.Context, query Query) (Response, error) | ||
|
||
// FetchPayment fetches the payment corresponding to the given payment | ||
// hash. | ||
FetchPayment(paymentHash lntypes.Hash) (*MPPayment, error) | ||
|
||
// FetchInFlightPayments returns all payments with status InFlight. | ||
FetchInFlightPayments() ([]*MPPayment, error) | ||
} | ||
|
||
// PaymentWriter represents the interface to write operations to the payments | ||
// database. | ||
type PaymentWriter interface { | ||
// DeletePayment deletes a payment from the DB given its payment hash. | ||
DeletePayment(paymentHash lntypes.Hash, failedAttemptsOnly bool) error | ||
|
||
// DeletePayments deletes all payments from the DB given the specified | ||
// flags. | ||
DeletePayments(failedOnly, failedAttemptsOnly bool) (int, error) | ||
|
||
PaymentControl | ||
} | ||
|
||
// PaymentControl represents the interface to control the payment lifecycle and | ||
// 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. | ||
// | ||
// 1. InitPayment. | ||
// 2. RegisterAttempt (a payment can have multiple attempts). | ||
// 3. SettleAttempt or FailAttempt (attempts can also fail as long as the | ||
// sending amount will be eventually settled). | ||
// 4. Payment succeeds or "Fail" is called. | ||
// 5. DeleteFailedAttempts is called which will delete all failed attempts | ||
// for a payment to clean up the database. | ||
type PaymentControl interface { | ||
// InitPayment checks that no other payment with the same payment hash | ||
// exists in the database before creating a new payment. However, it | ||
// should allow the user making a subsequent payment if the payment is | ||
// in a Failed state. | ||
InitPayment(lntypes.Hash, *PaymentCreationInfo) error | ||
|
||
// RegisterAttempt atomically records the provided HTLCAttemptInfo. | ||
RegisterAttempt(lntypes.Hash, *HTLCAttemptInfo) (*MPPayment, error) | ||
|
||
// SettleAttempt marks the given attempt settled with the preimage. If | ||
// this is a multi shard payment, this might implicitly mean the | ||
// full payment succeeded. | ||
// | ||
// After invoking this method, InitPayment should always return an | ||
// error to prevent us from making duplicate payments to the same | ||
// payment hash. The provided preimage is atomically saved to the DB | ||
// for record keeping. | ||
SettleAttempt(lntypes.Hash, uint64, *HTLCSettleInfo) (*MPPayment, error) | ||
|
||
// FailAttempt marks the given payment attempt failed. | ||
FailAttempt(lntypes.Hash, uint64, *HTLCFailInfo) (*MPPayment, error) | ||
|
||
// Fail transitions a payment into the Failed state, and records | ||
// the ultimate reason the payment failed. Note that this should only | ||
// be called when all active attempts are already failed. After | ||
// invoking this method, InitPayment should return nil on its next call | ||
// for this payment hash, allowing the user to make a subsequent | ||
// payment. | ||
Fail(lntypes.Hash, FailureReason) (*MPPayment, error) | ||
|
||
// DeleteFailedAttempts removes all failed HTLCs from the db. It should | ||
// be called for a given payment whenever all inflight htlcs are | ||
// completed, and the payment has reached a final terminal state. | ||
DeleteFailedAttempts(lntypes.Hash) error | ||
} | ||
|
||
// DBMPPayment is an interface that represents the payment state during a | ||
// payment lifecycle. | ||
type DBMPPayment interface { | ||
// GetState returns the current state of the payment. | ||
GetState() *MPPaymentState | ||
|
||
// Terminated returns true if the payment is in a final state. | ||
Terminated() bool | ||
|
||
// GetStatus returns the current status of the payment. | ||
GetStatus() PaymentStatus | ||
|
||
// NeedWaitAttempts specifies whether the payment needs to wait for the | ||
// outcome of an attempt. | ||
NeedWaitAttempts() (bool, error) | ||
|
||
// GetHTLCs returns all HTLCs of this payment. | ||
GetHTLCs() []HTLCAttempt | ||
|
||
// InFlightHTLCs returns all HTLCs that are in flight. | ||
InFlightHTLCs() []HTLCAttempt | ||
|
||
// AllowMoreAttempts is used to decide whether we can safely attempt | ||
// more HTLCs for a given payment state. Return an error if the payment | ||
// is in an unexpected state. | ||
AllowMoreAttempts() (bool, error) | ||
|
||
// TerminalInfo returns the settled HTLC attempt or the payment's | ||
// failure reason. | ||
TerminalInfo() (*HTLCAttempt, *FailureReason) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofPaymentControl
?There was a problem hiding this comment.
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