Skip to content

Commit

Permalink
chore: add fee reserve tests for transactions service
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jul 19, 2024
1 parent 6a2ce72 commit 30c1799
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
4 changes: 2 additions & 2 deletions tests/mock_ln_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var MockLNClientTransactions = []lnclient.Transaction{
Description: "mock invoice 1",
DescriptionHash: "hash1",
Preimage: "preimage1",
PaymentHash: "payment_hash_1",
PaymentHash: MockPaymentHash,
Amount: 1000,
FeesPaid: 50,
SettledAt: &MockTimeUnix,
Expand All @@ -48,7 +48,7 @@ var MockLNClientTransactions = []lnclient.Transaction{
Description: "mock invoice 2",
DescriptionHash: "hash2",
Preimage: "preimage2",
PaymentHash: "payment_hash_2",
PaymentHash: MockPaymentHash,
Amount: 2000,
FeesPaid: 75,
SettledAt: &MockTimeUnix,
Expand Down
56 changes: 56 additions & 0 deletions transactions/payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package transactions

import (
"context"
"errors"
"testing"

"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/lnclient"
"github.com/getAlby/hub/tests"
"github.com/stretchr/testify/assert"
)
Expand All @@ -22,5 +24,59 @@ func TestSendPaymentSync_NoApp(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, uint64(123000), transaction.AmountMsat)
assert.Equal(t, constants.TRANSACTION_STATE_SETTLED, transaction.State)
assert.Zero(t, *transaction.FeeReserveMsat)
assert.Equal(t, "123preimage", *transaction.Preimage)
}

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

defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)

svc.LNClient.(*tests.MockLn).PayInvoiceErrors = append(svc.LNClient.(*tests.MockLn).PayInvoiceErrors, errors.New("Some error"))
svc.LNClient.(*tests.MockLn).PayInvoiceResponses = append(svc.LNClient.(*tests.MockLn).PayInvoiceResponses, nil)

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

assert.Error(t, err)
assert.Nil(t, transaction)

transactionType := constants.TRANSACTION_TYPE_OUTGOING
transaction, err = transactionsService.LookupTransaction(ctx, tests.MockLNClientTransaction.PaymentHash, &transactionType, svc.LNClient, nil)
assert.Nil(t, err)

assert.Equal(t, uint64(123000), transaction.AmountMsat)
assert.Equal(t, constants.TRANSACTION_STATE_FAILED, transaction.State)
assert.Zero(t, *transaction.FeeReserveMsat)
assert.Nil(t, transaction.Preimage)
}

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

defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)

// timeout will leave the payment as pending
svc.LNClient.(*tests.MockLn).PayInvoiceErrors = append(svc.LNClient.(*tests.MockLn).PayInvoiceErrors, lnclient.NewTimeoutError())
svc.LNClient.(*tests.MockLn).PayInvoiceResponses = append(svc.LNClient.(*tests.MockLn).PayInvoiceResponses, nil)

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

assert.Error(t, err)
assert.Nil(t, transaction)

transactionType := constants.TRANSACTION_TYPE_OUTGOING
transaction, err = transactionsService.LookupTransaction(ctx, tests.MockLNClientTransaction.PaymentHash, &transactionType, svc.LNClient, nil)
assert.Nil(t, err)

assert.Equal(t, uint64(123000), transaction.AmountMsat)
assert.Equal(t, constants.TRANSACTION_STATE_PENDING, transaction.State)
assert.Equal(t, uint64(10000), *transaction.FeeReserveMsat)
assert.Nil(t, transaction.Preimage)
}
4 changes: 0 additions & 4 deletions transactions/todo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ package transactions
// TODO: keysend
// TODO: lookup invoice
// TODO: list transactions

// TODO: notifications
// TODO: fee reserve removed - successful payment
// TODO: fee reserve removed - failed payment
4 changes: 3 additions & 1 deletion transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ func (svc *transactionsService) SendPaymentSync(ctx context.Context, payReq stri
}

// As the LNClient did not return a timeout error, we assume the payment definitely failed
feeReserve := uint64(0)
dbErr := svc.db.Model(&dbTransaction).Updates(&db.Transaction{
State: constants.TRANSACTION_STATE_FAILED,
State: constants.TRANSACTION_STATE_FAILED,
FeeReserveMsat: &feeReserve,
}).Error
if dbErr != nil {
logger.Logger.WithFields(logrus.Fields{
Expand Down

0 comments on commit 30c1799

Please sign in to comment.