Skip to content

Commit 9476585

Browse files
committed
paymentsdb: implement FetchPayment for sql backend
1 parent 0079123 commit 9476585

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

payments/db/sql_store.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package paymentsdb
22

33
import (
44
"context"
5+
"database/sql"
56
"errors"
67
"fmt"
78
"math"
89
"time"
910

11+
"github.com/lightningnetwork/lnd/lntypes"
1012
"github.com/lightningnetwork/lnd/lnwire"
1113
"github.com/lightningnetwork/lnd/sqldb"
1214
"github.com/lightningnetwork/lnd/sqldb/sqlc"
@@ -652,3 +654,44 @@ func (s *SQLStore) QueryPayments(ctx context.Context, query Query) (Response,
652654
TotalCount: uint64(totalCount),
653655
}, nil
654656
}
657+
658+
// FetchPayment retrieves a complete payment record from the database by its
659+
// payment hash. The returned MPPayment includes all payment metadata such as
660+
// creation info, payment status, current state, all HTLC attempts (both
661+
// successful and failed), and the failure reason if the payment has been
662+
// marked as failed.
663+
//
664+
// Returns ErrPaymentNotInitiated if no payment with the given hash exists.
665+
//
666+
// This is part of the DB interface.
667+
func (s *SQLStore) FetchPayment(paymentHash lntypes.Hash) (*MPPayment, error) {
668+
ctx := context.TODO()
669+
670+
var mpPayment *MPPayment
671+
672+
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
673+
dbPayment, err := db.FetchPayment(ctx, paymentHash[:])
674+
if err != nil && !errors.Is(err, sql.ErrNoRows) {
675+
return fmt.Errorf("failed to fetch payment: %w", err)
676+
}
677+
678+
if errors.Is(err, sql.ErrNoRows) {
679+
return ErrPaymentNotInitiated
680+
}
681+
682+
mpPayment, err = s.fetchPaymentWithCompleteData(
683+
ctx, db, dbPayment,
684+
)
685+
if err != nil {
686+
return fmt.Errorf("failed to fetch payment with "+
687+
"complete data: %w", err)
688+
}
689+
690+
return nil
691+
}, sqldb.NoOpReset)
692+
if err != nil {
693+
return nil, err
694+
}
695+
696+
return mpPayment, nil
697+
}

0 commit comments

Comments
 (0)