Skip to content

Commit 0f6eebc

Browse files
committed
paymentsdb: implement FetchPayment for sql backend
1 parent 9de046b commit 0f6eebc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

payments/db/sql_store.go

Lines changed: 39 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"
@@ -474,3 +476,40 @@ func (s *SQLStore) QueryPayments(ctx context.Context,
474476
TotalCount: uint64(totalCount),
475477
}, nil
476478
}
479+
480+
// FetchPayment fetches the payment corresponding to the given payment
481+
// hash.
482+
//
483+
// This is part of the DB interface.
484+
func (s *SQLStore) FetchPayment(paymentHash lntypes.Hash) (*MPPayment, error) {
485+
ctx := context.TODO()
486+
487+
var mpPayment *MPPayment
488+
489+
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
490+
dbPayment, err := db.FetchPayment(ctx, paymentHash[:])
491+
if err != nil && !errors.Is(err, sql.ErrNoRows) {
492+
return fmt.Errorf("failed to fetch payment: %w", err)
493+
}
494+
495+
if errors.Is(err, sql.ErrNoRows) {
496+
return ErrPaymentNotInitiated
497+
}
498+
499+
mpPayment, err = s.fetchPaymentWithCompleteData(
500+
ctx, db, dbPayment,
501+
)
502+
if err != nil {
503+
return fmt.Errorf("failed to fetch payment with "+
504+
"complete data: %w", err)
505+
}
506+
507+
return nil
508+
}, func() {
509+
})
510+
if err != nil {
511+
return nil, fmt.Errorf("failed to fetch payment: %w", err)
512+
}
513+
514+
return mpPayment, nil
515+
}

0 commit comments

Comments
 (0)