Skip to content

Commit 0ba5a6e

Browse files
committed
paymentsdb: implement SettleAttempt for sql backend
1 parent 0dffdea commit 0ba5a6e

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

payments/db/sql_store.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,3 +1290,66 @@ func (s *SQLStore) RegisterAttempt(paymentHash lntypes.Hash,
12901290

12911291
return mpPayment, nil
12921292
}
1293+
1294+
// SettleAttempt marks the specified HTLC attempt as successfully settled,
1295+
// recording the payment preimage and settlement time. The preimage serves as
1296+
// cryptographic proof of payment and is atomically saved to the database.
1297+
//
1298+
// This method is part of the PaymentControl interface, which is embedded in
1299+
// the PaymentWriter interface and ultimately the DB interface. It represents
1300+
// step 3a in the payment lifecycle control flow (step 3b is FailAttempt),
1301+
// called after RegisterAttempt when an HTLC successfully completes.
1302+
func (s *SQLStore) SettleAttempt(paymentHash lntypes.Hash,
1303+
attemptID uint64, settleInfo *HTLCSettleInfo) (*MPPayment, error) {
1304+
1305+
ctx := context.TODO()
1306+
1307+
var mpPayment *MPPayment
1308+
1309+
err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error {
1310+
dbPayment, err := db.FetchPayment(ctx, paymentHash[:])
1311+
if err != nil {
1312+
return fmt.Errorf("failed to fetch payment: %w", err)
1313+
}
1314+
1315+
paymentStatus, err := computePaymentStatusFromDB(
1316+
ctx, db, dbPayment,
1317+
)
1318+
if err != nil {
1319+
return fmt.Errorf("failed to compute payment "+
1320+
"status: %w", err)
1321+
}
1322+
1323+
if err := paymentStatus.updatable(); err != nil {
1324+
return fmt.Errorf("payment is not updatable: %w", err)
1325+
}
1326+
1327+
err = db.SettleAttempt(ctx, sqlc.SettleAttemptParams{
1328+
AttemptIndex: int64(attemptID),
1329+
ResolutionTime: time.Now(),
1330+
ResolutionType: int32(HTLCAttemptResolutionSettled),
1331+
SettlePreimage: settleInfo.Preimage[:],
1332+
})
1333+
if err != nil {
1334+
return fmt.Errorf("failed to settle attempt: %w", err)
1335+
}
1336+
1337+
// Fetch the complete payment after we settled the attempt.
1338+
mpPayment, err = s.fetchPaymentWithCompleteData(
1339+
ctx, db, dbPayment,
1340+
)
1341+
if err != nil {
1342+
return fmt.Errorf("failed to fetch payment with "+
1343+
"complete data: %w", err)
1344+
}
1345+
1346+
return nil
1347+
}, func() {
1348+
mpPayment = nil
1349+
})
1350+
if err != nil {
1351+
return nil, fmt.Errorf("failed to settle attempt: %w", err)
1352+
}
1353+
1354+
return mpPayment, nil
1355+
}

0 commit comments

Comments
 (0)