Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not allow creating duplicate pending transactions #1046

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 43 additions & 1 deletion transactions/payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestSendPaymentSync_MetadataTooLarge(t *testing.T) {
assert.Nil(t, transaction)
}

func TestSendPaymentSync_Duplicate(t *testing.T) {
func TestSendPaymentSync_Duplicate_AlreadyPaid(t *testing.T) {
ctx := context.TODO()

svc, err := tests.CreateTestService(t)
Expand All @@ -110,6 +110,48 @@ func TestSendPaymentSync_Duplicate(t *testing.T) {
assert.Nil(t, transaction)
}

func TestSendPaymentSync_Duplicate_Pending(t *testing.T) {
ctx := context.TODO()

svc, err := tests.CreateTestService(t)
require.NoError(t, err)
defer svc.Remove()

svc.DB.Create(&db.Transaction{
State: constants.TRANSACTION_STATE_PENDING,
Type: constants.TRANSACTION_TYPE_OUTGOING,
PaymentHash: tests.MockLNClientTransaction.PaymentHash,
AmountMsat: 123000,
})

transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
transaction, err := transactionsService.SendPaymentSync(ctx, tests.MockLNClientTransaction.Invoice, nil, nil, svc.LNClient, nil, nil)

assert.Error(t, err)
assert.Equal(t, "there is already a payment pending for this invoice", err.Error())
assert.Nil(t, transaction)
}

func TestSendPaymentSync_Duplicate_Failed(t *testing.T) {
ctx := context.TODO()

svc, err := tests.CreateTestService(t)
require.NoError(t, err)
defer svc.Remove()

svc.DB.Create(&db.Transaction{
State: constants.TRANSACTION_STATE_FAILED,
Type: constants.TRANSACTION_TYPE_OUTGOING,
PaymentHash: tests.MockLNClientTransaction.PaymentHash,
AmountMsat: 123000,
})

transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
_, err = transactionsService.SendPaymentSync(ctx, tests.MockLNClientTransaction.Invoice, nil, nil, svc.LNClient, nil, nil)

assert.NoError(t, err)
}

func TestMarkSettled_Sent(t *testing.T) {
svc, err := tests.CreateTestService(t)
require.NoError(t, err)
Expand Down
12 changes: 10 additions & 2 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,17 @@ func (svc *transactionsService) SendPaymentSync(ctx context.Context, payReq stri
PaymentHash: paymentRequest.PaymentHash,
State: constants.TRANSACTION_STATE_SETTLED,
}).RowsAffected > 0 {
logger.Logger.WithField("payment_hash", dbTransaction.PaymentHash).Info("this invoice has already been paid")
logger.Logger.WithField("payment_hash", dbTransaction.PaymentHash).Debug("this invoice has already been paid")
return errors.New("this invoice has already been paid")
}
if tx.Limit(1).Find(&existingSettledTransaction, &db.Transaction{
Type: constants.TRANSACTION_TYPE_OUTGOING,
PaymentHash: paymentRequest.PaymentHash,
State: constants.TRANSACTION_STATE_PENDING,
}).RowsAffected > 0 {
logger.Logger.WithField("payment_hash", dbTransaction.PaymentHash).Debug("this invoice is already being paid")
return errors.New("there is already a payment pending for this invoice")
}

err := svc.validateCanPay(tx, appId, paymentAmount, paymentRequest.Description)
if err != nil {
Expand Down Expand Up @@ -953,7 +961,7 @@ 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).Error("payment already marked as sent")
logger.Logger.WithField("payment_hash", dbTransaction.PaymentHash).Debug("payment already marked as sent")
return &existingSettledTransaction, nil
}

Expand Down
Loading