|
| 1 | +package paymentsdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/lightningnetwork/lnd/lntypes" |
| 7 | +) |
| 8 | + |
| 9 | +// DB represents the interface to the underlying payments database. |
| 10 | +type DB interface { |
| 11 | + PaymentReader |
| 12 | + PaymentWriter |
| 13 | +} |
| 14 | + |
| 15 | +// PaymentReader represents the interface to read operations from the payments |
| 16 | +// database. |
| 17 | +type PaymentReader interface { |
| 18 | + // QueryPayments queries the payments database and should support |
| 19 | + // pagination. |
| 20 | + QueryPayments(ctx context.Context, query Query) (Response, error) |
| 21 | + |
| 22 | + // FetchPayment fetches the payment corresponding to the given payment |
| 23 | + // hash. |
| 24 | + FetchPayment(paymentHash lntypes.Hash) (*MPPayment, error) |
| 25 | + |
| 26 | + // FetchInFlightPayments returns all payments with status InFlight. |
| 27 | + FetchInFlightPayments() ([]*MPPayment, error) |
| 28 | +} |
| 29 | + |
| 30 | +// PaymentWriter represents the interface to write operations to the payments |
| 31 | +// database. |
| 32 | +type PaymentWriter interface { |
| 33 | + // DeletePayment deletes a payment from the DB given its payment hash. |
| 34 | + DeletePayment(paymentHash lntypes.Hash, failedAttemptsOnly bool) error |
| 35 | + |
| 36 | + // DeletePayments deletes all payments from the DB given the specified |
| 37 | + // flags. |
| 38 | + DeletePayments(failedOnly, failedAttemptsOnly bool) (int, error) |
| 39 | + |
| 40 | + PaymentControl |
| 41 | +} |
| 42 | + |
| 43 | +// PaymentControl represents the interface to control the payment lifecycle and |
| 44 | +// its database operations. This interface represents the control flow of how |
| 45 | +// a payment should be handled in the database. They are not just writing |
| 46 | +// operations but they inherently represent the flow of a payment. The methods |
| 47 | +// are called in the following order. |
| 48 | +// |
| 49 | +// 1. InitPayment. |
| 50 | +// 2. RegisterAttempt (a payment can have multiple attempts). |
| 51 | +// 3. SettleAttempt or FailAttempt (attempts can also fail as long as the |
| 52 | +// sending amount will be eventually settled). |
| 53 | +// 4. Payment succeeds or "Fail" is called. |
| 54 | +// 5. DeleteFailedAttempts is called which will delete all failed attempts |
| 55 | +// for a payment to clean up the database. |
| 56 | +type PaymentControl interface { |
| 57 | + // InitPayment checks that no other payment with the same payment hash |
| 58 | + // exists in the database before creating a new payment. However, it |
| 59 | + // should allow the user making a subsequent payment if the payment is |
| 60 | + // in a Failed state. |
| 61 | + InitPayment(lntypes.Hash, *PaymentCreationInfo) error |
| 62 | + |
| 63 | + // RegisterAttempt atomically records the provided HTLCAttemptInfo. |
| 64 | + RegisterAttempt(lntypes.Hash, *HTLCAttemptInfo) (*MPPayment, error) |
| 65 | + |
| 66 | + // SettleAttempt marks the given attempt settled with the preimage. If |
| 67 | + // this is a multi shard payment, this might implicitly mean the |
| 68 | + // full payment succeeded. |
| 69 | + // |
| 70 | + // After invoking this method, InitPayment should always return an |
| 71 | + // error to prevent us from making duplicate payments to the same |
| 72 | + // payment hash. The provided preimage is atomically saved to the DB |
| 73 | + // for record keeping. |
| 74 | + SettleAttempt(lntypes.Hash, uint64, *HTLCSettleInfo) (*MPPayment, error) |
| 75 | + |
| 76 | + // FailAttempt marks the given payment attempt failed. |
| 77 | + FailAttempt(lntypes.Hash, uint64, *HTLCFailInfo) (*MPPayment, error) |
| 78 | + |
| 79 | + // Fail transitions a payment into the Failed state, and records |
| 80 | + // the ultimate reason the payment failed. Note that this should only |
| 81 | + // be called when all active attempts are already failed. After |
| 82 | + // invoking this method, InitPayment should return nil on its next call |
| 83 | + // for this payment hash, allowing the user to make a subsequent |
| 84 | + // payment. |
| 85 | + Fail(lntypes.Hash, FailureReason) (*MPPayment, error) |
| 86 | + |
| 87 | + // DeleteFailedAttempts removes all failed HTLCs from the db. It should |
| 88 | + // be called for a given payment whenever all inflight htlcs are |
| 89 | + // completed, and the payment has reached a final terminal state. |
| 90 | + DeleteFailedAttempts(lntypes.Hash) error |
| 91 | +} |
| 92 | + |
| 93 | +// DBMPPayment is an interface that represents the payment state during a |
| 94 | +// payment lifecycle. |
| 95 | +type DBMPPayment interface { |
| 96 | + // GetState returns the current state of the payment. |
| 97 | + GetState() *MPPaymentState |
| 98 | + |
| 99 | + // Terminated returns true if the payment is in a final state. |
| 100 | + Terminated() bool |
| 101 | + |
| 102 | + // GetStatus returns the current status of the payment. |
| 103 | + GetStatus() PaymentStatus |
| 104 | + |
| 105 | + // NeedWaitAttempts specifies whether the payment needs to wait for the |
| 106 | + // outcome of an attempt. |
| 107 | + NeedWaitAttempts() (bool, error) |
| 108 | + |
| 109 | + // GetHTLCs returns all HTLCs of this payment. |
| 110 | + GetHTLCs() []HTLCAttempt |
| 111 | + |
| 112 | + // InFlightHTLCs returns all HTLCs that are in flight. |
| 113 | + InFlightHTLCs() []HTLCAttempt |
| 114 | + |
| 115 | + // AllowMoreAttempts is used to decide whether we can safely attempt |
| 116 | + // more HTLCs for a given payment state. Return an error if the payment |
| 117 | + // is in an unexpected state. |
| 118 | + AllowMoreAttempts() (bool, error) |
| 119 | + |
| 120 | + // TerminalInfo returns the settled HTLC attempt or the payment's |
| 121 | + // failure reason. |
| 122 | + TerminalInfo() (*HTLCAttempt, *FailureReason) |
| 123 | +} |
0 commit comments