paymentsdb: implement FetchPayment for sql backend
This commit is contained in:
1 file changed
+43
@@ -2,11 +2,13 @@ package paymentsdb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/lightningnetwork/lnd/lntypes"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
"github.com/lightningnetwork/lnd/sqldb"
|
"github.com/lightningnetwork/lnd/sqldb"
|
||||||
"github.com/lightningnetwork/lnd/sqldb/sqlc"
|
"github.com/lightningnetwork/lnd/sqldb/sqlc"
|
||||||
@@ -652,3 +654,44 @@ func (s *SQLStore) QueryPayments(ctx context.Context, query Query) (Response,
|
|||||||
TotalCount: uint64(totalCount),
|
TotalCount: uint64(totalCount),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FetchPayment retrieves a complete payment record from the database by its
|
||||||
|
// payment hash. The returned MPPayment includes all payment metadata such as
|
||||||
|
// creation info, payment status, current state, all HTLC attempts (both
|
||||||
|
// successful and failed), and the failure reason if the payment has been
|
||||||
|
// marked as failed.
|
||||||
|
//
|
||||||
|
// Returns ErrPaymentNotInitiated if no payment with the given hash exists.
|
||||||
|
//
|
||||||
|
// This is part of the DB interface.
|
||||||
|
func (s *SQLStore) FetchPayment(paymentHash lntypes.Hash) (*MPPayment, error) {
|
||||||
|
ctx := context.TODO()
|
||||||
|
|
||||||
|
var mpPayment *MPPayment
|
||||||
|
|
||||||
|
err := s.db.ExecTx(ctx, sqldb.ReadTxOpt(), func(db SQLQueries) error {
|
||||||
|
dbPayment, err := db.FetchPayment(ctx, paymentHash[:])
|
||||||
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return fmt.Errorf("failed to fetch payment: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return ErrPaymentNotInitiated
|
||||||
|
}
|
||||||
|
|
||||||
|
mpPayment, err = s.fetchPaymentWithCompleteData(
|
||||||
|
ctx, db, dbPayment,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to fetch payment with "+
|
||||||
|
"complete data: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}, sqldb.NoOpReset)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mpPayment, nil
|
||||||
|
}
|
||||||
Reference in new issue
Block a user