Skip to content

Commit

Permalink
Merge branch 'main' into remove-getinfo-lnd-cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiidb committed Oct 2, 2023
2 parents 7969e74 + 5273f45 commit c87ded7
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 43 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/integration_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,4 @@ jobs:
- name: Run tests
run: go test -p 1 -v -covermode=atomic -coverprofile=coverage.out -cover -coverpkg=./... ./...
env:
RABBITMQ_URI: amqp://root:password@localhost:5672
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
RABBITMQ_URI: amqp://root:password@localhost:5672
6 changes: 3 additions & 3 deletions controllers/keysend.ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (controller *KeySendController) KeySend(c echo.Context) error {
})
}

ok, err := controller.svc.BalanceCheck(c.Request().Context(), lnPayReq, userID)
resp, err := controller.svc.CheckPaymentAllowed(c.Request().Context(), lnPayReq, userID)
if err != nil {
c.Logger().Errorj(
log.JSON{
Expand All @@ -93,9 +93,9 @@ func (controller *KeySendController) KeySend(c echo.Context) error {
)
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}
if !ok {
if resp != nil {
c.Logger().Errorf("User does not have enough balance user_id:%v amount:%v", userID, lnPayReq.PayReq.NumSatoshis)
return c.JSON(http.StatusBadRequest, responses.NotEnoughBalanceError)
return c.JSON(http.StatusBadRequest, resp)
}
invoice, err := controller.svc.AddOutgoingInvoice(c.Request().Context(), userID, "", lnPayReq)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions controllers/payinvoice.ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
}
}

ok, err := controller.svc.BalanceCheck(c.Request().Context(), lnPayReq, userID)
resp, err := controller.svc.CheckPaymentAllowed(c.Request().Context(), lnPayReq, userID)
if err != nil {
c.Logger().Errorj(
log.JSON{
Expand All @@ -108,9 +108,9 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
)
return c.JSON(http.StatusBadRequest, responses.GeneralServerError)
}
if !ok {
if resp != nil {
c.Logger().Errorf("User does not have enough balance user_id:%v amount:%v", userID, lnPayReq.PayReq.NumSatoshis)
return c.JSON(http.StatusBadRequest, responses.NotEnoughBalanceError)
return c.JSON(http.StatusBadRequest, resp)
}

invoice, err := controller.svc.AddOutgoingInvoice(c.Request().Context(), userID, paymentRequest, lnPayReq)
Expand Down
25 changes: 13 additions & 12 deletions controllers_v2/keysend.ctrl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v2controllers

import (
"context"
"encoding/hex"
"fmt"
"net/http"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (controller *KeySendController) KeySend(c echo.Context) error {
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
}

result, errResp := controller.SingleKeySend(c, &reqBody, userID)
result, errResp := controller.SingleKeySend(c.Request().Context(), &reqBody, userID)
if errResp != nil {
c.Logger().Errorf("Failed to send keysend: %s", errResp.Message)
return c.JSON(errResp.HttpStatusCode, errResp)
Expand Down Expand Up @@ -124,7 +125,7 @@ func (controller *KeySendController) MultiKeySend(c echo.Context) error {
singleSuccesfulPayment := false
for _, keysend := range reqBody.Keysends {
keysend := keysend
res, err := controller.SingleKeySend(c, &keysend, userID)
res, err := controller.SingleKeySend(context.Background(), &keysend, userID)
if err != nil {
controller.svc.Logger.Errorf("Error making keysend split payment %v %s", keysend, err.Message)
result.Keysends = append(result.Keysends, KeySendResult{
Expand All @@ -148,7 +149,7 @@ func (controller *KeySendController) MultiKeySend(c echo.Context) error {
return c.JSON(status, result)
}

func (controller *KeySendController) SingleKeySend(c echo.Context, reqBody *KeySendRequestBody, userID int64) (result *KeySendResponseBody, resp *responses.ErrorResponse) {
func (controller *KeySendController) SingleKeySend(ctx context.Context, reqBody *KeySendRequestBody, userID int64) (result *KeySendResponseBody, resp *responses.ErrorResponse) {
lnPayReq := &lnd.LNPayReq{
PayReq: &lnrpc.PayReq{
Destination: reqBody.Destination,
Expand All @@ -171,30 +172,30 @@ func (controller *KeySendController) SingleKeySend(c echo.Context, reqBody *KeyS
HttpStatusCode: 400,
}
}
ok, err := controller.svc.BalanceCheck(c.Request().Context(), lnPayReq, userID)
resp, err := controller.svc.CheckPaymentAllowed(ctx, lnPayReq, userID)
if err != nil {
controller.svc.Logger.Error(err)
return nil, &responses.GeneralServerError
}
if !ok {
c.Logger().Errorf("User does not have enough balance user_id:%v amount:%v", userID, lnPayReq.PayReq.NumSatoshis)
return nil, &responses.NotEnoughBalanceError
if resp != nil {
controller.svc.Logger.Errorf("User does not have enough balance user_id:%v amount:%v", userID, lnPayReq.PayReq.NumSatoshis)
return nil, resp
}
invoice, err := controller.svc.AddOutgoingInvoice(c.Request().Context(), userID, "", lnPayReq)
invoice, err := controller.svc.AddOutgoingInvoice(ctx, userID, "", lnPayReq)
if err != nil {
controller.svc.Logger.Error(err)
return nil, &responses.GeneralServerError
}
if _, err := hex.DecodeString(invoice.DestinationPubkeyHex); err != nil || len(invoice.DestinationPubkeyHex) != common.DestinationPubkeyHexSize {
c.Logger().Errorf("Invalid destination pubkey hex user_id:%v pubkey:%v", userID, len(invoice.DestinationPubkeyHex))
controller.svc.Logger.Errorf("Invalid destination pubkey hex user_id:%v pubkey:%v", userID, len(invoice.DestinationPubkeyHex))
return nil, &responses.InvalidDestinationError
}
invoice.DestinationCustomRecords = map[uint64][]byte{}

for key, value := range customRecords {
intKey, err := strconv.Atoi(key)
if err != nil {
c.Logger().Errorj(
controller.svc.Logger.Errorj(
log.JSON{
"message": "invalid custom records",
"error": err,
Expand All @@ -205,9 +206,9 @@ func (controller *KeySendController) SingleKeySend(c echo.Context, reqBody *KeyS
}
invoice.DestinationCustomRecords[uint64(intKey)] = []byte(value)
}
sendPaymentResponse, err := controller.svc.PayInvoice(c.Request().Context(), invoice)
sendPaymentResponse, err := controller.svc.PayInvoice(ctx, invoice)
if err != nil {
c.Logger().Errorf("Payment failed: user_id:%v error: %v", userID, err)
controller.svc.Logger.Errorf("Payment failed: user_id:%v error: %v", userID, err)
sentry.CaptureException(err)
return nil, &responses.ErrorResponse{
Error: true,
Expand Down
6 changes: 3 additions & 3 deletions controllers_v2/payinvoice.ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
}
lnPayReq.PayReq.NumSatoshis = amt
}
ok, err := controller.svc.BalanceCheck(c.Request().Context(), lnPayReq, userID)
resp, err := controller.svc.CheckPaymentAllowed(c.Request().Context(), lnPayReq, userID)
if err != nil {
c.Logger().Errorj(
log.JSON{
Expand All @@ -109,9 +109,9 @@ func (controller *PayInvoiceController) PayInvoice(c echo.Context) error {
)
return err
}
if !ok {
if resp != nil {
c.Logger().Errorf("User does not have enough balance user_id:%v amount:%v", userID, lnPayReq.PayReq.NumSatoshis)
return c.JSON(http.StatusInternalServerError, responses.NotEnoughBalanceError)
return c.JSON(http.StatusInternalServerError, resp)
}
invoice, err := controller.svc.AddOutgoingInvoice(c.Request().Context(), userID, paymentRequest, lnPayReq)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions db/migrations/20230928130000_add_invoice_settled_index.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE INDEX CONCURRENTLY IF NOT EXISTS index_invoices_on_user_id_settled_at
ON invoices(user_id, settled_at)
INCLUDE(amount);
61 changes: 61 additions & 0 deletions integration_tests/internal_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,67 @@ func (suite *PaymentTestSuite) TestPaymentFeeReserve() {
//reset fee reserve so it's not used in other tests
suite.service.Config.FeeReserve = false
}
func (suite *PaymentTestSuite) TestVolumeExceeded() {
//this will cause the payment to fail as the account was already funded
//with 1000 sats
suite.service.Config.MaxVolume = 999
suite.service.Config.MaxVolumePeriod = 2592000
aliceFundingSats := 1000
//fund alice account
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test internal payment alice", suite.aliceToken)
err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
assert.NoError(suite.T(), err)

//wait a bit for the payment to be processed
time.Sleep(10 * time.Millisecond)

//try to make external payment
//which should fail
//create external invoice
externalSatRequested := 1000
externalInvoice := lnrpc.Invoice{
Memo: "integration tests: external pay from user",
Value: int64(externalSatRequested),
}
invoice, err := suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
assert.NoError(suite.T(), err)
//pay external invoice
rec := httptest.NewRecorder()
var buf bytes.Buffer
assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&ExpectedPayInvoiceRequestBody{
Invoice: invoice.PaymentRequest,
}))
req := httptest.NewRequest(http.MethodPost, "/payinvoice", &buf)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.aliceToken))
suite.echo.ServeHTTP(rec, req)
//should fail because max volume check
assert.Equal(suite.T(), http.StatusBadRequest, rec.Code)
resp := &responses.ErrorResponse{}
err = json.NewDecoder(rec.Body).Decode(resp)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), responses.TooMuchVolumeError.Message, resp.Message)

//change the period to be 1 second, sleep for 2 seconds, try to make another payment, this should work
suite.service.Config.MaxVolumePeriod = 1
time.Sleep(2 * time.Second)
rec = httptest.NewRecorder()
externalInvoice = lnrpc.Invoice{
Memo: "integration tests: external pay from user",
Value: int64(externalSatRequested),
}
invoice, err = suite.externalLND.AddInvoice(context.Background(), &externalInvoice)
assert.NoError(suite.T(), err)
assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(&ExpectedPayInvoiceRequestBody{
Invoice: invoice.PaymentRequest,
}))
suite.echo.ServeHTTP(rec, req)
assert.Equal(suite.T(), http.StatusOK, rec.Code)

//change the config back
suite.service.Config.MaxVolumePeriod = 0
suite.service.Config.MaxVolume = 1e6
}
func (suite *PaymentTestSuite) TestInternalPayment() {
aliceFundingSats := 1000
bobSatRequested := 500
Expand Down
118 changes: 118 additions & 0 deletions integration_tests/keysend_failure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package integration_tests

import (
"context"
"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/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type KeySendFailureTestSuite struct {
TestSuite
service *service.LndhubService
mlnd *MockLND
aliceLogin ExpectedCreateUserResponseBody
aliceToken string
invoiceUpdateSubCancelFn context.CancelFunc
serviceClient *LNDMockWrapperAsync
}

func (suite *KeySendFailureTestSuite) TearDownTest() {
clearTable(suite.service, "transaction_entries")
clearTable(suite.service, "invoices")
}

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

func (suite *KeySendFailureTestSuite) SetupSuite() {
fee := int64(1)
mlnd := newDefaultMockLND()
mlnd.fee = fee
suite.mlnd = mlnd
// inject fake lnd client with failing send payment sync into service
lndClient, err := NewLNDMockWrapperAsync(mlnd)
suite.serviceClient = 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.aliceLogin = users[0]
suite.aliceToken = userTokens[0]
suite.echo.Use(tokens.Middleware([]byte(suite.service.Config.JWTSecret)))
suite.echo.POST("/addinvoice", controllers.NewAddInvoiceController(suite.service).AddInvoice)
suite.echo.POST("/keysend", controllers.NewKeySendController(suite.service).KeySend)
}


func (suite *KeySendFailureTestSuite) TestKeysendPayment() {
aliceFundingSats := 1000
externalSatRequested := 500
//fund alice account
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken)
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)

go suite.createKeySendReqError(int64(externalSatRequested), "key send test", "123456789012345678901234567890123456789012345678901234567890abcdef", suite.aliceToken)

suite.serviceClient.FailPayment(SendPaymentMockError)
time.Sleep(2 * time.Second)

// check that balance was reverted
userId := getUserIdFromToken(suite.aliceToken)
aliceBalance, 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(aliceFundingSats), aliceBalance)

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)
assert.Equal(suite.T(), SendPaymentMockError, invoices[0].ErrorMessage)
assert.NotEmpty(suite.T(), invoices[0].RHash)
assert.NotEmpty(suite.T(), invoices[0].Preimage)
}

func TestKeySendFailureTestSuite(t *testing.T) {
suite.Run(t, new(KeySendFailureTestSuite))
}
1 change: 1 addition & 0 deletions integration_tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *servi
DatabaseMaxConns: 1,
DatabaseMaxIdleConns: 1,
DatabaseConnMaxLifetime: 10,
MaxFeeAmount: 1e6,
JWTSecret: []byte("SECRET"),
JWTAccessTokenExpiry: 3600,
JWTRefreshTokenExpiry: 3600,
Expand Down
7 changes: 7 additions & 0 deletions lib/responses/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ var NotEnoughBalanceError = ErrorResponse{
HttpStatusCode: 400,
}

var TooMuchVolumeError = ErrorResponse{
Error: true,
Code: 2,
Message: "transaction volume too high. please contact support for further assistance.",
HttpStatusCode: 400,
}

var AccountDeactivatedError = ErrorResponse{
Error: true,
Code: 1,
Expand Down
2 changes: 2 additions & 0 deletions lib/service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Config struct {
MaxSendAmount int64 `envconfig:"MAX_SEND_AMOUNT" default:"0"`
MaxAccountBalance int64 `envconfig:"MAX_ACCOUNT_BALANCE" default:"0"`
MaxFeeAmount int64 `envconfig:"MAX_FEE_AMOUNT" default:"5000"`
MaxVolume int64 `envconfig:"MAX_VOLUME" default:"0"` //0 means the volume check is disabled by default
MaxVolumePeriod int64 `envconfig:"MAX_VOLUME_PERIOD" default:"2592000"` //in seconds, default 1 month
RabbitMQUri string `envconfig:"RABBITMQ_URI"`
RabbitMQLndhubInvoiceExchange string `envconfig:"RABBITMQ_INVOICE_EXCHANGE" default:"lndhub_invoice"`
RabbitMQLndInvoiceExchange string `envconfig:"RABBITMQ_LND_INVOICE_EXCHANGE" default:"lnd_invoice"`
Expand Down
Loading

0 comments on commit c87ded7

Please sign in to comment.