Skip to content

Commit 5ed86a9

Browse files
committed
paymentsdb: implement FetchPayment for sql backend
1 parent e65b735 commit 5ed86a9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

payments/db/sql_store.go

Lines changed: 38 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"
@@ -627,3 +629,39 @@ func (s *SQLStore) QueryPayments(ctx context.Context,
627629
TotalCount: uint64(totalCount),
628630
}, nil
629631
}
632+
633+
// FetchPayment fetches the payment corresponding to the given payment
634+
// hash.
635+
//
636+
// This is part of the DB interface.
637+
func (s *SQLStore) FetchPayment(paymentHash lntypes.Hash) (*MPPayment, error) {
638+
ctx := context.TODO()
639+
640+
var mpPayment *MPPayment
641+
642+
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
643+
dbPayment, err := db.FetchPayment(ctx, paymentHash[:])
644+
if err != nil && !errors.Is(err, sql.ErrNoRows) {
645+
return fmt.Errorf("failed to fetch payment: %w", err)
646+
}
647+
648+
if errors.Is(err, sql.ErrNoRows) {
649+
return ErrPaymentNotInitiated
650+
}
651+
652+
mpPayment, err = s.fetchPaymentWithCompleteData(
653+
ctx, db, dbPayment,
654+
)
655+
if err != nil {
656+
return fmt.Errorf("failed to fetch payment with "+
657+
"complete data: %w", err)
658+
}
659+
660+
return nil
661+
}, sqldb.NoOpReset)
662+
if err != nil {
663+
return nil, err
664+
}
665+
666+
return mpPayment, nil
667+
}

0 commit comments

Comments
 (0)