Skip to content

Commit

Permalink
fix: throw error if transaction is already marked as sent
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Aug 20, 2024
1 parent 15e9bbf commit 9a64e86
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions transactions/payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

Expand Down
33 changes: 18 additions & 15 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 == "" {
Expand Down

0 comments on commit 9a64e86

Please sign in to comment.