Skip to content

Commit 9127c08

Browse files
committed
paymentsdb: implement FetchPayment for sql backend
1 parent fe35828 commit 9127c08

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

0 commit comments

Comments
 (0)