Skip to content

Commit 13aef70

Browse files
committed
paymentsdb: implement SettleAttempt for sql backend
1 parent 2cfd4e7 commit 13aef70

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

payments/db/sql_store.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,3 +924,51 @@ func (s *SQLStore) RegisterAttempt(paymentHash lntypes.Hash,
924924

925925
return mpPayment, nil
926926
}
927+
928+
// SettleAttempt marks the given attempt settled with the preimage.
929+
func (s *SQLStore) SettleAttempt(paymentHash lntypes.Hash,
930+
attemptID uint64, settleInfo *HTLCSettleInfo) (*MPPayment, error) {
931+
932+
ctx := context.TODO()
933+
934+
var mpPayment *MPPayment
935+
936+
err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
937+
// Before updating the attempt, we fetch the payment to get the
938+
// payment ID.
939+
payment, err := db.FetchPayment(ctx, paymentHash[:])
940+
if err != nil && !errors.Is(err, sql.ErrNoRows) {
941+
return fmt.Errorf("failed to fetch payment: %w", err)
942+
}
943+
if errors.Is(err, sql.ErrNoRows) {
944+
return ErrPaymentNotInitiated
945+
}
946+
947+
err = db.SettleAttempt(ctx, sqlc.SettleAttemptParams{
948+
AttemptIndex: int64(attemptID),
949+
ResolutionTime: time.Now(),
950+
ResolutionType: int32(HTLCAttemptResolutionSettled),
951+
SettlePreimage: settleInfo.Preimage[:],
952+
})
953+
if err != nil {
954+
return fmt.Errorf("failed to settle attempt: %w", err)
955+
}
956+
957+
mpPayment, err = s.fetchPaymentWithCompleteData(
958+
ctx, db, payment,
959+
)
960+
if err != nil {
961+
return fmt.Errorf("failed to fetch payment with "+
962+
"complete data: %w", err)
963+
}
964+
965+
return nil
966+
}, func() {
967+
mpPayment = nil
968+
})
969+
if err != nil {
970+
return nil, fmt.Errorf("failed to settle attempt: %w", err)
971+
}
972+
973+
return mpPayment, nil
974+
}

0 commit comments

Comments
 (0)