Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions payments/db/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,3 +936,51 @@ func (s *SQLStore) RegisterAttempt(paymentHash lntypes.Hash,

return mpPayment, nil
}

// SettleAttempt marks the given attempt settled with the preimage.
func (s *SQLStore) SettleAttempt(paymentHash lntypes.Hash,
attemptID uint64, settleInfo *HTLCSettleInfo) (*MPPayment, error) {

ctx := context.TODO()

var mpPayment *MPPayment

err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
// Before updating the attempt, we fetch the payment to get the
// payment ID.
payment, 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
}

Comment on lines +951 to +958
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why dont we check Status.updatable() as is done for KVStore?

err = db.SettleAttempt(ctx, sqlc.SettleAttemptParams{
AttemptIndex: int64(attemptID),
ResolutionTime: time.Now(),
ResolutionType: int32(HTLCAttemptResolutionSettled),
SettlePreimage: settleInfo.Preimage[:],
})
if err != nil {
return fmt.Errorf("failed to settle attempt: %w", err)
}

mpPayment, err = s.fetchPaymentWithCompleteData(
ctx, db, payment,
)
if err != nil {
return fmt.Errorf("failed to fetch payment with "+
"complete data: %w", err)
}

return nil
}, func() {
mpPayment = nil
})
if err != nil {
return nil, fmt.Errorf("failed to settle attempt: %w", err)
}

return mpPayment, nil
}