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