diff --git a/transactions/payments_test.go b/transactions/payments_test.go index 308f5766..f0d74572 100644 --- a/transactions/payments_test.go +++ b/transactions/payments_test.go @@ -131,8 +131,8 @@ func TestDoNotMarkSettledTwice(t *testing.T) { return transactionsService.markTransactionSettled(tx, &dbTransaction, "test", 0, false) }) - assert.Nil(t, err) - assert.Equal(t, settledAt, *dbTransaction.SettledAt) + assert.Error(t, err) + assert.Equal(t, "payment already marked as sent", err.Error()) assert.Zero(t, len(mockEventConsumer.GetConsumeEvents())) } diff --git a/transactions/transactions_service.go b/transactions/transactions_service.go index 0ca0af97..2ae0bb4f 100644 --- a/transactions/transactions_service.go +++ b/transactions/transactions_service.go @@ -249,13 +249,14 @@ func (svc *transactionsService) SendPaymentSync(ctx context.Context, payReq stri return nil, err } - // the payment definitely succeeded - err = svc.db.Transaction(func(tx *gorm.DB) error { - return svc.markTransactionSettled(tx, &dbTransaction, response.Preimage, response.Fee, selfPayment) - }) - - if err != nil { - return nil, err + // the payment definitely succeeded, but only update it if wasn't already settled via lnclient notification + if dbTransaction.State != constants.TRANSACTION_STATE_SETTLED { + err = svc.db.Transaction(func(tx *gorm.DB) error { + return svc.markTransactionSettled(tx, &dbTransaction, response.Preimage, response.Fee, selfPayment) + }) + if err != nil { + return nil, err + } } return &dbTransaction, nil @@ -410,13 +411,15 @@ func (svc *transactionsService) SendKeysend(ctx context.Context, amount uint64, return nil, err } - // the payment definitely succeeded - err = svc.db.Transaction(func(tx *gorm.DB) error { - return svc.markTransactionSettled(tx, &dbTransaction, preimage, payKeysendResponse.Fee, selfPayment) - }) + // the payment definitely succeeded, but only update it if wasn't already settled via lnclient notification + if dbTransaction.State != constants.TRANSACTION_STATE_SETTLED { + err = svc.db.Transaction(func(tx *gorm.DB) error { + return svc.markTransactionSettled(tx, &dbTransaction, preimage, payKeysendResponse.Fee, selfPayment) + }) - if err != nil { - return nil, err + if err != nil { + return nil, err + } } return &dbTransaction, nil @@ -885,8 +888,8 @@ func (svc *transactionsService) markTransactionSettled(tx *gorm.DB, dbTransactio PaymentHash: dbTransaction.PaymentHash, State: constants.TRANSACTION_STATE_SETTLED, }).RowsAffected > 0 { - logger.Logger.WithField("payment_hash", dbTransaction.PaymentHash).Info("payment already marked as sent") - return nil + logger.Logger.WithField("payment_hash", dbTransaction.PaymentHash).Error("payment already marked as sent") + return errors.New("payment already marked as sent") } if preimage == "" {