Skip to content

Commit

Permalink
Merge pull request #231 from getAlby/chore/update-initialized-payments
Browse files Browse the repository at this point in the history
Look up status of outgoing payments at startup
  • Loading branch information
kiwiidb authored Dec 6, 2022
2 parents d869cf6 + 47690db commit 0676fd0
Show file tree
Hide file tree
Showing 10 changed files with 421 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ vendor/

# env, use .env_example for reference
.env

initialized_payments_dryrun/*
220 changes: 220 additions & 0 deletions integration_tests/hodl_invoice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package integration_tests

import (
"context"
"encoding/hex"
"fmt"
"log"
"testing"
"time"

"github.com/getAlby/lndhub.go/common"
"github.com/getAlby/lndhub.go/controllers"
"github.com/getAlby/lndhub.go/lib"
"github.com/getAlby/lndhub.go/lib/responses"
"github.com/getAlby/lndhub.go/lib/service"
"github.com/getAlby/lndhub.go/lib/tokens"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type HodlInvoiceSuite struct {
TestSuite
mlnd *MockLND
externalLND *MockLND
service *service.LndhubService
userLogin ExpectedCreateUserResponseBody
userToken string
invoiceUpdateSubCancelFn context.CancelFunc
hodlLND *LNDMockHodlWrapperAsync
}

func (suite *HodlInvoiceSuite) SetupSuite() {
mlnd := newDefaultMockLND()
externalLND, err := NewMockLND("1234567890abcdefabcd", 0, make(chan (*lnrpc.Invoice)))
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
suite.externalLND = externalLND
suite.mlnd = mlnd
// inject hodl lnd client
lndClient, err := NewLNDMockHodlWrapperAsync(mlnd)
suite.hodlLND = lndClient
if err != nil {
log.Fatalf("Error setting up test client: %v", err)
}

svc, err := LndHubTestServiceInit(lndClient)
if err != nil {
log.Fatalf("Error initializing test service: %v", err)
}
users, userTokens, err := createUsers(svc, 1)
if err != nil {
log.Fatalf("Error creating test users: %v", err)
}
// Subscribe to LND invoice updates in the background
// store cancel func to be called in tear down suite
ctx, cancel := context.WithCancel(context.Background())
suite.invoiceUpdateSubCancelFn = cancel
go svc.InvoiceUpdateSubscription(ctx)
suite.service = svc
e := echo.New()

e.HTTPErrorHandler = responses.HTTPErrorHandler
e.Validator = &lib.CustomValidator{Validator: validator.New()}
suite.echo = e
assert.Equal(suite.T(), 1, len(users))
assert.Equal(suite.T(), 1, len(userTokens))
suite.userLogin = users[0]
suite.userToken = userTokens[0]
suite.echo.Use(tokens.Middleware([]byte(suite.service.Config.JWTSecret)))
suite.echo.GET("/balance", controllers.NewBalanceController(suite.service).Balance)
suite.echo.POST("/addinvoice", controllers.NewAddInvoiceController(suite.service).AddInvoice)
suite.echo.POST("/payinvoice", controllers.NewPayInvoiceController(suite.service).PayInvoice)
}

func (suite *HodlInvoiceSuite) TestHodlInvoice() {
userFundingSats := 1000
externalSatRequested := 500
// fund user account
invoiceResponse := suite.createAddInvoiceReq(userFundingSats, "integration test external payment user", suite.userToken)
err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
assert.NoError(suite.T(), err)

// wait a bit for the callback event to hit
time.Sleep(10 * time.Millisecond)

// create external invoice
externalInvoice := lnrpc.Invoice{
Memo: "integration tests: external pay from user",
Value: int64(externalSatRequested),
RPreimage: []byte("preimage1"),
}
invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
assert.NoError(suite.T(), err)
// pay external from user, req will be canceled after 2 sec
go suite.createPayInvoiceReqWithCancel(invoice.PaymentRequest, suite.userToken)

// wait for payment to be updated as pending in database
time.Sleep(5 * time.Second)

// check to see that balance was reduced
userId := getUserIdFromToken(suite.userToken)
userBalance, err := suite.service.CurrentUserBalance(context.Background(), userId)
if err != nil {
fmt.Printf("Error when getting balance %v\n", err.Error())
}
assert.Equal(suite.T(), int64(userFundingSats-externalSatRequested), userBalance)

// check payment is pending
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)

//start payment checking loop
err = suite.service.CheckAllPendingOutgoingPayments(context.Background())
assert.NoError(suite.T(), err)
//send cancel invoice with lnrpc.payment
suite.hodlLND.SettlePayment(lnrpc.Payment{
PaymentHash: hex.EncodeToString(invoice.RHash),
Value: externalInvoice.Value,
CreationDate: 0,
Fee: 0,
PaymentPreimage: "",
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)
if err != nil {
fmt.Printf("Error when getting balance %v\n", err.Error())
}
assert.Equal(suite.T(), int64(userFundingSats), userBalance)

invoices, err := suite.service.InvoicesFor(context.Background(), userId, common.InvoiceTypeOutgoing)
if err != nil {
fmt.Printf("Error when getting invoices %v\n", err.Error())
}
assert.Equal(suite.T(), 1, len(invoices))
assert.Equal(suite.T(), common.InvoiceStateError, invoices[0].State)
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 {
fmt.Printf("Error when getting transaction entries %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)
assert.Equal(suite.T(), transactonEntries[1].Amount, int64(externalSatRequested))
assert.Equal(suite.T(), transactonEntries[2].Amount, int64(externalSatRequested))

// create external invoice
externalInvoice = lnrpc.Invoice{
Memo: "integration tests: external pay from user",
Value: int64(externalSatRequested),
RPreimage: []byte("preimage2"),
}
invoice, err = suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
assert.NoError(suite.T(), err)
// pay external from user, req will be canceled after 2 sec
go suite.createPayInvoiceReqWithCancel(invoice.PaymentRequest, suite.userToken)
// wait for payment to be updated as pending in database
time.Sleep(3 * time.Second)
// check payment is pending
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)
//start payment checking loop
err = suite.service.CheckAllPendingOutgoingPayments(context.Background())
assert.NoError(suite.T(), err)
//send settle invoice with lnrpc.payment
suite.hodlLND.SettlePayment(lnrpc.Payment{
PaymentHash: hex.EncodeToString(invoice.RHash),
Value: externalInvoice.Value,
CreationDate: 0,
Fee: 0,
PaymentPreimage: "preimage2",
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.InvoiceStateSettled, inv.State)
clearTable(suite.service, "invoices")
clearTable(suite.service, "transaction_entries")
clearTable(suite.service, "accounts")
}

func (suite *HodlInvoiceSuite) TearDownSuite() {
suite.invoiceUpdateSubCancelFn()
}

func TestHodlInvoiceSuite(t *testing.T) {
suite.Run(t, new(HodlInvoiceSuite))
}
10 changes: 9 additions & 1 deletion integration_tests/lnd_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/getAlby/lndhub.go/lnd"
"github.com/labstack/gommon/random"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/zpay32"
"google.golang.org/grpc"
Expand Down Expand Up @@ -58,6 +59,9 @@ func (mockSub *MockSubscribeInvoices) Recv() (*lnrpc.Invoice, error) {
inv := <-mockSub.invoiceChan
return inv, nil
}
func (mlnd *MockLND) SubscribePayment(ctx context.Context, req *routerrpc.TrackPaymentRequest, options ...grpc.CallOption) (lnd.SubscribePaymentWrapper, error) {
return nil, nil
}

func (mlnd *MockLND) ListChannels(ctx context.Context, req *lnrpc.ListChannelsRequest, options ...grpc.CallOption) (*lnrpc.ListChannelsResponse, error) {
return &lnrpc.ListChannelsResponse{
Expand Down Expand Up @@ -216,14 +220,18 @@ func (mlnd *MockLND) GetInfo(ctx context.Context, req *lnrpc.GetInfoRequest, opt
}, nil
}

func (mlnd *MockLND) TrackPayment(ctx context.Context, hash []byte, options ...grpc.CallOption) (*lnrpc.Payment, error) {
return nil, nil
}

func (mlnd *MockLND) DecodeBolt11(ctx context.Context, bolt11 string, options ...grpc.CallOption) (*lnrpc.PayReq, error) {
inv, err := zpay32.Decode(bolt11, &chaincfg.RegressionNetParams)
if err != nil {
return nil, err
}
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()),
Expand Down
58 changes: 58 additions & 0 deletions integration_tests/lnd_mock_hodl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package integration_tests

import (
"context"

"github.com/getAlby/lndhub.go/lnd"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"google.golang.org/grpc"
)

func NewLNDMockHodlWrapper(lnd lnd.LightningClientWrapper) (result *LNDMockWrapper, err error) {
return &LNDMockWrapper{
lnd,
}, nil
}

type LNDMockHodlWrapperAsync struct {
hps *HodlPaymentSubscriber
lnd.LightningClientWrapper
}

type HodlPaymentSubscriber struct {
ch chan (lnrpc.Payment)
}

// wait for channel, then return
func (hps *HodlPaymentSubscriber) Recv() (*lnrpc.Payment, error) {
result := <-hps.ch
return &result, nil
}

func NewLNDMockHodlWrapperAsync(lnd lnd.LightningClientWrapper) (result *LNDMockHodlWrapperAsync, err error) {
return &LNDMockHodlWrapperAsync{
hps: &HodlPaymentSubscriber{
ch: make(chan lnrpc.Payment),
},
LightningClientWrapper: lnd,
}, nil
}

func (wrapper *LNDMockHodlWrapperAsync) SubscribePayment(ctx context.Context, req *routerrpc.TrackPaymentRequest, options ...grpc.CallOption) (lnd.SubscribePaymentWrapper, error) {
return wrapper.hps, nil
}

func (wrapper *LNDMockHodlWrapperAsync) SendPaymentSync(ctx context.Context, req *lnrpc.SendRequest, options ...grpc.CallOption) (*lnrpc.SendResponse, error) {
//block indefinetely
//because we don't want this function to ever return something here
//the payments should be processed asynchronously by the payment tracker
select {}
}

func (wrapper *LNDMockHodlWrapperAsync) SettlePayment(payment lnrpc.Payment) {
wrapper.hps.ch <- payment
}

//write test that completes payment
//write test that fails payment
8 changes: 8 additions & 0 deletions integration_tests/subscription_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/uptrace/bun"
Expand Down Expand Up @@ -128,6 +129,9 @@ func (mock *lndSubscriptionStartMockClient) SubscribeInvoices(ctx context.Contex
func (mock *lndSubscriptionStartMockClient) Recv() (*lnrpc.Invoice, error) {
select {}
}
func (mock *lndSubscriptionStartMockClient) SubscribePayment(ctx context.Context, req *routerrpc.TrackPaymentRequest, options ...grpc.CallOption) (lnd.SubscribePaymentWrapper, error) {
return nil, nil
}

func (mock *lndSubscriptionStartMockClient) GetInfo(ctx context.Context, req *lnrpc.GetInfoRequest, options ...grpc.CallOption) (*lnrpc.GetInfoResponse, error) {
panic("not implemented") // TODO: Implement
Expand All @@ -136,3 +140,7 @@ func (mock *lndSubscriptionStartMockClient) GetInfo(ctx context.Context, req *ln
func (mock *lndSubscriptionStartMockClient) DecodeBolt11(ctx context.Context, bolt11 string, options ...grpc.CallOption) (*lnrpc.PayReq, error) {
panic("not implemented") // TODO: Implement
}

func (mlnd *lndSubscriptionStartMockClient) TrackPayment(ctx context.Context, hash []byte, options ...grpc.CallOption) (*lnrpc.Payment, error) {
return nil, nil
}
Loading

0 comments on commit 0676fd0

Please sign in to comment.