multi: move FailureReason to payment package

This commit is contained in:
ziggie
2025-08-12 13:57:08 +02:00
parent d77b2f9c26
commit d138e23919
19 changed files with 134 additions and 140 deletions

View File

@@ -157,10 +157,10 @@ func fetchDuplicatePayment(bucket kvdb.RBucket) (*MPPayment, error) {
}
// Get failure reason if available.
var failureReason *channeldb.FailureReason
var failureReason *FailureReason
b = bucket.Get(duplicatePaymentFailInfoKey)
if b != nil {
reason := channeldb.FailureReason(b[0])
reason := FailureReason(b[0])
failureReason = &reason
}

View File

@@ -605,7 +605,7 @@ func (p *KVPaymentsDB) updateHtlcKey(paymentHash lntypes.Hash,
// its next call for this payment hash, allowing the switch to make a
// subsequent payment.
func (p *KVPaymentsDB) Fail(paymentHash lntypes.Hash,
reason channeldb.FailureReason) (*MPPayment, error) {
reason FailureReason) (*MPPayment, error) {
var (
updateErr error
@@ -979,10 +979,10 @@ func fetchPayment(bucket kvdb.RBucket) (*MPPayment, error) {
}
// Get failure reason if available.
var failureReason *channeldb.FailureReason
var failureReason *FailureReason
b := bucket.Get(paymentFailInfoKey)
if b != nil {
reason := channeldb.FailureReason(b[0])
reason := FailureReason(b[0])
failureReason = &reason
}

View File

@@ -82,7 +82,7 @@ func TestKVPaymentsDBSwitchFail(t *testing.T) {
)
// Fail the payment, which should moved it to Failed.
failReason := channeldb.FailureReasonNoRoute
failReason := FailureReasonNoRoute
_, err = paymentDB.Fail(info.PaymentIdentifier, failReason)
require.NoError(t, err, "unable to fail payment hash")
@@ -301,7 +301,7 @@ func TestKVPaymentsDBFailsWithoutInFlight(t *testing.T) {
// Calling Fail should return an error.
_, err = paymentDB.Fail(
info.PaymentIdentifier, channeldb.FailureReasonNoRoute,
info.PaymentIdentifier, FailureReasonNoRoute,
)
require.ErrorIs(t, err, ErrPaymentNotInitiated)
}
@@ -383,7 +383,7 @@ func TestKVPaymentsDBDeleteNonInFlight(t *testing.T) {
}
// Fail the payment, which should moved it to Failed.
failReason := channeldb.FailureReasonNoRoute
failReason := FailureReasonNoRoute
_, err = paymentDB.Fail(
info.PaymentIdentifier, failReason,
)
@@ -787,7 +787,7 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
HTLCAttemptInfo: a,
}
var firstFailReason *channeldb.FailureReason
var firstFailReason *FailureReason
if test.settleFirst {
_, err := paymentDB.SettleAttempt(
info.PaymentIdentifier, a.AttemptID,
@@ -827,7 +827,7 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
// We also record a payment level fail, to move it into
// a terminal state.
failReason := channeldb.FailureReasonNoRoute
failReason := FailureReasonNoRoute
_, err = paymentDB.Fail(
info.PaymentIdentifier, failReason,
)
@@ -910,7 +910,7 @@ func TestKVPaymentsDBMultiShard(t *testing.T) {
// failure. This is to allow multiple concurrent shard
// write a terminal failure to the database without
// syncing.
failReason := channeldb.FailureReasonPaymentDetails
failReason := FailureReasonPaymentDetails
_, err = paymentDB.Fail(
info.PaymentIdentifier, failReason,
)
@@ -1156,7 +1156,7 @@ type htlcStatus struct {
// assertPaymentInfo retrieves the payment referred to by hash and verifies the
// expected values.
func assertPaymentInfo(t *testing.T, p *KVPaymentsDB, hash lntypes.Hash,
c *channeldb.PaymentCreationInfo, f *channeldb.FailureReason,
c *channeldb.PaymentCreationInfo, f *FailureReason,
a *htlcStatus) {
t.Helper()
@@ -1339,7 +1339,7 @@ func createTestPayments(t *testing.T, p *KVPaymentsDB, payments []*payment) {
)
require.NoError(t, err, "unable to fail htlc")
failReason := channeldb.FailureReasonNoRoute
failReason := FailureReasonNoRoute
_, err = p.Fail(info.PaymentIdentifier,
failReason)
require.NoError(t, err, "unable to fail payment hash")

View File

@@ -19,6 +19,63 @@ import (
"github.com/lightningnetwork/lnd/routing/route"
)
// FailureReason encodes the reason a payment ultimately failed.
type FailureReason byte
const (
// FailureReasonTimeout indicates that the payment did timeout before a
// successful payment attempt was made.
FailureReasonTimeout FailureReason = 0
// FailureReasonNoRoute indicates no successful route to the
// destination was found during path finding.
FailureReasonNoRoute FailureReason = 1
// FailureReasonError indicates that an unexpected error happened during
// payment.
FailureReasonError FailureReason = 2
// FailureReasonPaymentDetails indicates that either the hash is unknown
// or the final cltv delta or amount is incorrect.
FailureReasonPaymentDetails FailureReason = 3
// FailureReasonInsufficientBalance indicates that we didn't have enough
// balance to complete the payment.
FailureReasonInsufficientBalance FailureReason = 4
// FailureReasonCanceled indicates that the payment was canceled by the
// user.
FailureReasonCanceled FailureReason = 5
// TODO(joostjager): Add failure reasons for:
// LocalLiquidityInsufficient, RemoteCapacityInsufficient.
)
// Error returns a human-readable error string for the FailureReason.
func (r FailureReason) Error() string {
return r.String()
}
// String returns a human-readable FailureReason.
func (r FailureReason) String() string {
switch r {
case FailureReasonTimeout:
return "timeout"
case FailureReasonNoRoute:
return "no_route"
case FailureReasonError:
return "error"
case FailureReasonPaymentDetails:
return "incorrect_payment_details"
case FailureReasonInsufficientBalance:
return "insufficient_balance"
case FailureReasonCanceled:
return "canceled"
}
return "unknown"
}
// HTLCAttemptInfo contains static information about a specific HTLC attempt
// for a payment. This information is used by the router to handle any errors
// coming back after an attempt is made, and to query the switch about the
@@ -252,7 +309,7 @@ type MPPayment struct {
//
// NOTE: Will only be set once the daemon has given up on the payment
// altogether.
FailureReason *channeldb.FailureReason
FailureReason *FailureReason
// Status is the current PaymentStatus of this payment.
Status PaymentStatus
@@ -273,7 +330,7 @@ func (m *MPPayment) Terminated() bool {
// TerminalInfo returns any HTLC settle info recorded. If no settle info is
// recorded, any payment level failure will be returned. If neither a settle
// nor a failure is recorded, both return values will be nil.
func (m *MPPayment) TerminalInfo() (*HTLCAttempt, *channeldb.FailureReason) {
func (m *MPPayment) TerminalInfo() (*HTLCAttempt, *FailureReason) {
for _, h := range m.HTLCs {
if h.Settle != nil {
return &h, nil

View File

@@ -2,8 +2,6 @@ package paymentsdb
import (
"fmt"
"github.com/lightningnetwork/lnd/channeldb"
)
// PaymentStatus represent current status of payment.
@@ -173,7 +171,7 @@ func (ps PaymentStatus) updatable() error {
// failed` is false, this indicates all the payment's HTLCs have occurred a
// temporarily failure and the payment is still in-flight.
func decidePaymentStatus(htlcs []HTLCAttempt,
reason *channeldb.FailureReason) (PaymentStatus, error) {
reason *FailureReason) (PaymentStatus, error) {
var (
inflight bool

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"testing"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/stretchr/testify/require"
)
@@ -24,13 +23,13 @@ func TestDecidePaymentStatus(t *testing.T) {
}
// Create a test failure reason and get the pointer.
reason := channeldb.FailureReasonNoRoute
reason := FailureReasonNoRoute
failure := &reason
testCases := []struct {
name string
htlcs []HTLCAttempt
reason *channeldb.FailureReason
reason *FailureReason
expectedStatus PaymentStatus
expectedErr error
}{

View File

@@ -598,7 +598,7 @@ func TestPaymentSetState(t *testing.T) {
// Create a test preimage and failure reason.
preimage := lntypes.Preimage{1}
failureReasonError := channeldb.FailureReasonError
failureReasonError := FailureReasonError
testCases := []struct {
name string