Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiidb committed Jul 5, 2023
1 parent a22ab04 commit c3bd3bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
19 changes: 13 additions & 6 deletions integration_tests/payment_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func (suite *PaymentTestErrorsSuite) TestExternalFailingInvoice() {

//test an expired invoice
externalInvoice := lnrpc.Invoice{
Memo: "integration tests: external pay from alice",
Value: int64(externalSatRequested),
Memo: "integration tests: external pay from alice",
Value: int64(externalSatRequested),
Expiry: 1,
}
invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
Expand Down Expand Up @@ -148,10 +148,17 @@ func (suite *PaymentTestErrorsSuite) TestExternalFailingInvoice() {
fmt.Printf("Error when getting balance %v\n", err.Error())
}

// check if there are 3 transaction entries, with reversed credit and debit account ids
assert.Equal(suite.T(), 3, len(transactonEntries))
assert.Equal(suite.T(), transactonEntries[1].CreditAccountID, transactonEntries[2].DebitAccountID)
assert.Equal(suite.T(), transactonEntries[1].DebitAccountID, transactonEntries[2].CreditAccountID)
// check if there are 5 transaction entries:
// - the incoming payment
// - the outgoing payment
// - the fee reserve + the fee reserve reversal
// - the outgoing payment reversal
// with reversed credit and debit account ids for payment 2/5 & payment 3/4
assert.Equal(suite.T(), 5, len(transactonEntries))
assert.Equal(suite.T(), transactonEntries[1].CreditAccountID, transactonEntries[4].DebitAccountID)
assert.Equal(suite.T(), transactonEntries[1].DebitAccountID, transactonEntries[4].CreditAccountID)
assert.Equal(suite.T(), transactonEntries[2].CreditAccountID, transactonEntries[3].DebitAccountID)
assert.Equal(suite.T(), transactonEntries[2].DebitAccountID, transactonEntries[3].CreditAccountID)
assert.Equal(suite.T(), transactonEntries[1].Amount, int64(externalSatRequested))
assert.Equal(suite.T(), transactonEntries[2].Amount, int64(externalSatRequested))
// assert that balance is the same
Expand Down
24 changes: 13 additions & 11 deletions lib/service/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,16 @@ func (svc *LndhubService) HandleFailedPayment(ctx context.Context, invoice *mode
svc.Logger.Errorf("Could not open tx entry for updating failed payment:r_hash:%s %v", invoice.RHash, err)
return err
}
// add transaction entry with reverted credit/debit account id

//revert the fee reserve if necessary
err = svc.RevertFeeReserve(ctx, &entryToRevert, invoice, tx)
if err != nil {
sentry.CaptureException(err)
svc.Logger.Errorf("Could not revert fee reserve entry entry user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error())
return err
}

//revert the payment if necessary
entry := models.TransactionEntry{
UserID: invoice.UserID,
InvoiceID: invoice.ID,
Expand All @@ -264,13 +273,6 @@ func (svc *LndhubService) HandleFailedPayment(ctx context.Context, invoice *mode
return err
}

err = svc.RevertFeeReserve(ctx, &entryToRevert, tx)
if err != nil {
sentry.CaptureException(err)
svc.Logger.Errorf("Could not revert fee reserve entry entry user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error())
return err
}

invoice.State = common.InvoiceStateError
if failedPaymentError != nil {
invoice.ErrorMessage = failedPaymentError.Error()
Expand Down Expand Up @@ -337,12 +339,12 @@ func (svc *LndhubService) InsertTransactionEntry(ctx context.Context, invoice *m
return entry, err
}

func (svc *LndhubService) RevertFeeReserve(ctx context.Context, entry *models.TransactionEntry, tx bun.Tx) (err error) {
func (svc *LndhubService) RevertFeeReserve(ctx context.Context, entry *models.TransactionEntry, invoice *models.Invoice, tx bun.Tx) (err error) {
if entry.FeeReserve != nil {
entryToRevert := entry.FeeReserve
feeReserveRevert := models.TransactionEntry{
UserID: entryToRevert.UserID,
InvoiceID: entryToRevert.ID,
InvoiceID: invoice.ID,
CreditAccountID: entryToRevert.DebitAccountID,
DebitAccountID: entryToRevert.CreditAccountID,
Amount: entryToRevert.Amount,
Expand Down Expand Up @@ -394,7 +396,7 @@ func (svc *LndhubService) HandleSuccessfulPayment(ctx context.Context, invoice *
}

//revert the fee reserve entry
err = svc.RevertFeeReserve(ctx, &parentEntry, tx)
err = svc.RevertFeeReserve(ctx, &parentEntry, invoice, tx)
if err != nil {
tx.Rollback()
sentry.CaptureException(err)
Expand Down

0 comments on commit c3bd3bc

Please sign in to comment.