diff --git a/integration_tests/hodl_invoice_test.go b/integration_tests/hodl_invoice_test.go index ae85241..0e6cba2 100644 --- a/integration_tests/hodl_invoice_test.go +++ b/integration_tests/hodl_invoice_test.go @@ -114,22 +114,27 @@ func (suite *HodlInvoiceSuite) TestHodlInvoiceSuccess() { Value: externalInvoice.Value, CreationDate: 0, Fee: 0, - PaymentPreimage: "", - ValueSat: 0, + PaymentPreimage: "123preimage", + ValueSat: externalInvoice.Value, ValueMsat: 0, PaymentRequest: invoice.PaymentRequest, Status: lnrpc.Payment_SUCCEEDED, FailureReason: 0, }) + //wait a bit for db update to happen + time.Sleep(time.Second) if err != nil { fmt.Printf("Error when getting balance %v\n", err.Error()) } + //fetch user balance again + userBalance, err = suite.service.CurrentUserBalance(context.Background(), userId) + assert.NoError(suite.T(), err) assert.Equal(suite.T(), int64(userFundingSats-externalSatRequested), userBalance) // check payment is updated as succesful inv, err = suite.service.FindInvoiceByPaymentHash(context.Background(), userId, hex.EncodeToString(invoice.RHash)) assert.NoError(suite.T(), err) - assert.Equal(suite.T(), common.InvoiceStateInitialized, inv.State) + assert.Equal(suite.T(), common.InvoiceStateSettled, inv.State) } func (suite *HodlInvoiceSuite) TestHodlInvoiceFailure() { @@ -178,13 +183,15 @@ func (suite *HodlInvoiceSuite) TestHodlInvoiceFailure() { Value: externalInvoice.Value, CreationDate: 0, Fee: 0, - PaymentPreimage: "", - ValueSat: 0, + PaymentPreimage: "123preimage", + ValueSat: externalInvoice.Value, ValueMsat: 0, PaymentRequest: invoice.PaymentRequest, Status: lnrpc.Payment_FAILED, FailureReason: lnrpc.PaymentFailureReason_FAILURE_REASON_INCORRECT_PAYMENT_DETAILS, }) + //wait a bit for db update to happen + time.Sleep(time.Second) // check that balance was reverted and invoice is in error state userBalance, err = suite.service.CurrentUserBalance(context.Background(), userId) @@ -199,7 +206,8 @@ func (suite *HodlInvoiceSuite) TestHodlInvoiceFailure() { } assert.Equal(suite.T(), 1, len(invoices)) assert.Equal(suite.T(), common.InvoiceStateError, invoices[0].State) - assert.Equal(suite.T(), SendPaymentMockError, invoices[0].ErrorMessage) + errorString := "FAILURE_REASON_INCORRECT_PAYMENT_DETAILS" + assert.Equal(suite.T(), errorString, invoices[0].ErrorMessage) transactonEntries, err := suite.service.TransactionEntriesFor(context.Background(), userId) if err != nil { diff --git a/integration_tests/lnd_mock.go b/integration_tests/lnd_mock.go index 86c51ff..9f01a35 100644 --- a/integration_tests/lnd_mock.go +++ b/integration_tests/lnd_mock.go @@ -231,7 +231,7 @@ func (mlnd *MockLND) DecodeBolt11(ctx context.Context, bolt11 string, options .. } result := &lnrpc.PayReq{ Destination: hex.EncodeToString(inv.Destination.SerializeCompressed()), - PaymentHash: string(inv.PaymentHash[:]), + PaymentHash: hex.EncodeToString(inv.PaymentHash[:]), NumSatoshis: int64(*inv.MilliSat) / 1000, Timestamp: inv.Timestamp.Unix(), Expiry: int64(inv.Expiry()), diff --git a/integration_tests/lnd_mock_hodl.go b/integration_tests/lnd_mock_hodl.go index c68313a..01f7c5a 100644 --- a/integration_tests/lnd_mock_hodl.go +++ b/integration_tests/lnd_mock_hodl.go @@ -25,8 +25,9 @@ type HodlPaymentSubscriber struct { } // wait for channel, then return -func (hps *HodlPaymentSubscriber) Recv() (lnrpc.Payment, error) { - return <-hps.ch, nil +func (hps *HodlPaymentSubscriber) Recv() (*lnrpc.Payment, error) { + result := <-hps.ch + return &result, nil } func NewLNDMockHodlWrapperAsync(lnd lnd.LightningClientWrapper) (result *LNDMockHodlWrapperAsync, err error) { @@ -39,7 +40,7 @@ func NewLNDMockHodlWrapperAsync(lnd lnd.LightningClientWrapper) (result *LNDMock } func (wrapper *LNDMockHodlWrapperAsync) SubscribePayment(ctx context.Context, req *routerrpc.TrackPaymentRequest, options ...grpc.CallOption) (lnd.SubscribePaymentWrapper, error) { - return nil, nil + return wrapper.hps, nil } func (wrapper *LNDMockHodlWrapperAsync) SendPaymentSync(ctx context.Context, req *lnrpc.SendRequest, options ...grpc.CallOption) (*lnrpc.SendResponse, error) { diff --git a/lib/service/checkpayments.go b/lib/service/checkpayments.go index 5f844d2..891951f 100644 --- a/lib/service/checkpayments.go +++ b/lib/service/checkpayments.go @@ -24,6 +24,7 @@ func (svc *LndhubService) CheckAllPendingOutgoingPayments(ctx context.Context) ( //spawn goroutines //https://go.dev/doc/faq#closures_and_goroutines inv := inv + svc.Logger.Infof("Spawning tracker for payment with hash %s", inv.RHash) go svc.TrackOutgoingPaymentstatus(ctx, &inv) } return nil @@ -31,7 +32,6 @@ func (svc *LndhubService) CheckAllPendingOutgoingPayments(ctx context.Context) ( // Should be called in a goroutine as the tracking can potentially take a long time func (svc *LndhubService) TrackOutgoingPaymentstatus(ctx context.Context, invoice *models.Invoice) { - fmt.Println(invoice.RHash) //ask lnd using TrackPaymentV2 by hash of payment rawHash, err := hex.DecodeString(invoice.RHash) if err != nil { diff --git a/lib/service/invoices.go b/lib/service/invoices.go index 601fd23..f62acc4 100644 --- a/lib/service/invoices.go +++ b/lib/service/invoices.go @@ -251,7 +251,7 @@ func (svc *LndhubService) HandleFailedPayment(ctx context.Context, invoice *mode _, err := svc.DB.NewInsert().Model(&entry).Exec(ctx) if err != nil { sentry.CaptureException(err) - svc.Logger.Errorf("Could not insert transaction entry user_id:%v invoice_id:%v", invoice.UserID, invoice.ID) + svc.Logger.Errorf("Could not insert transaction entry user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error()) return err } @@ -263,7 +263,7 @@ func (svc *LndhubService) HandleFailedPayment(ctx context.Context, invoice *mode _, err = svc.DB.NewUpdate().Model(invoice).WherePK().Exec(ctx) if err != nil { sentry.CaptureException(err) - svc.Logger.Errorf("Could not update failed payment invoice user_id:%v invoice_id:%v", invoice.UserID, invoice.ID) + svc.Logger.Errorf("Could not update failed payment invoice user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error()) } return err } @@ -275,7 +275,7 @@ func (svc *LndhubService) HandleSuccessfulPayment(ctx context.Context, invoice * _, err := svc.DB.NewUpdate().Model(invoice).WherePK().Exec(ctx) if err != nil { sentry.CaptureException(err) - svc.Logger.Errorf("Could not update sucessful payment invoice user_id:%v invoice_id:%v", invoice.UserID, invoice.ID) + svc.Logger.Errorf("Could not update sucessful payment invoice user_id:%v invoice_id:%v, error %s", invoice.UserID, invoice.ID, err.Error()) } // Get the user's fee account for the transaction entry, current account is already there in parent entry @@ -297,14 +297,14 @@ func (svc *LndhubService) HandleSuccessfulPayment(ctx context.Context, invoice * _, err = svc.DB.NewInsert().Model(&entry).Exec(ctx) if err != nil { sentry.CaptureException(err) - svc.Logger.Errorf("Could not insert fee transaction entry user_id:%v invoice_id:%v", invoice.UserID, invoice.ID) + svc.Logger.Errorf("Could not insert fee transaction entry user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error()) return err } userBalance, err := svc.CurrentUserBalance(ctx, entry.UserID) if err != nil { sentry.CaptureException(err) - svc.Logger.Errorf("Could not fetch user balance user_id:%v invoice_id:%v", invoice.UserID, invoice.ID) + svc.Logger.Errorf("Could not fetch user balance user_id:%v invoice_id:%v error %s", invoice.UserID, invoice.ID, err.Error()) return err }