diff --git a/Dockerfile b/Dockerfile index 42c3993c..da68ef37 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,12 +12,18 @@ RUN go mod download COPY . . # Build the application -RUN go build -o main +RUN go build -o main ./cmd/server + +# Build the utility scripts +RUN go build ./cmd/invoice-republishing +RUN go build ./cmd/payment-reconciliation # Start a new, final image to reduce size. FROM alpine as final # Copy the binaries and entrypoint from the builder image. COPY --from=builder /build/main /bin/ +COPY --from=builder /build/invoice-republishing /bin/ +COPY --from=builder /build/payment-reconciliation /bin/ ENTRYPOINT [ "/bin/main" ] diff --git a/background_routines.go b/background_routines.go deleted file mode 100644 index b01ad073..00000000 --- a/background_routines.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "context" - - "github.com/getAlby/lndhub.go/lib/service" -) - -func StartInvoiceRoutine(svc *service.LndhubService, backGroundCtx context.Context) (err error) { - if svc.RabbitMQClient != nil { - err = svc.RabbitMQClient.SubscribeToLndInvoices(backGroundCtx, svc.ProcessInvoiceUpdate) - if err != nil && err != context.Canceled { - return err - } - - return nil - } else { - err = svc.InvoiceUpdateSubscription(backGroundCtx) - if err != nil && err != context.Canceled { - // in case of an error in this routine, we want to restart LNDhub - return err - } - - return nil - } -} - -func StartPendingPaymentRoutine(svc *service.LndhubService, backGroundCtx context.Context) (err error) { - if svc.RabbitMQClient != nil { - return svc.RabbitMQClient.FinalizeInitializedPayments(backGroundCtx, svc) - } else { - return svc.CheckAllPendingOutgoingPayments(backGroundCtx) - } -} diff --git a/cmd/invoice-republishing/main.go b/cmd/invoice-republishing/main.go new file mode 100644 index 00000000..d3569c37 --- /dev/null +++ b/cmd/invoice-republishing/main.go @@ -0,0 +1,102 @@ +package main + +import ( + "context" + "fmt" + "os" + "time" + + "github.com/getAlby/lndhub.go/db" + "github.com/getAlby/lndhub.go/db/models" + "github.com/getAlby/lndhub.go/lib" + "github.com/getAlby/lndhub.go/lib/service" + "github.com/getAlby/lndhub.go/rabbitmq" + "github.com/joho/godotenv" + "github.com/kelseyhightower/envconfig" + "github.com/sirupsen/logrus" +) + +func main() { + + c := &service.Config{} + // Load configruation from environment variables + err := godotenv.Load(".env") + if err != nil { + fmt.Println("Failed to load .env file") + } + logger := lib.Logger(c.LogFilePath) + startDate, endDate, err := loadStartAndEndIdFromEnv() + if err != nil { + logger.Fatalf("Could not load start and end id from env %v", err) + } + err = envconfig.Process("", c) + if err != nil { + logger.Fatalf("Error loading environment variables: %v", err) + } + // Open a DB connection based on the configured DATABASE_URI + dbConn, err := db.Open(c) + if err != nil { + logger.Fatalf("Error initializing db connection: %v", err) + } + amqpClient, err := rabbitmq.DialAMQP(c.RabbitMQUri, rabbitmq.WithAmqpLogger(logger)) + if err != nil { + logger.Fatal(err) + } + + defer amqpClient.Close() + + rabbitmqClient, err := rabbitmq.NewClient(amqpClient, + rabbitmq.WithLogger(logger), + rabbitmq.WithLndInvoiceExchange(c.RabbitMQLndInvoiceExchange), + rabbitmq.WithLndHubInvoiceExchange(c.RabbitMQLndhubInvoiceExchange), + rabbitmq.WithLndInvoiceConsumerQueueName(c.RabbitMQInvoiceConsumerQueueName), + rabbitmq.WithLndPaymentExchange(c.RabbitMQLndPaymentExchange), + rabbitmq.WithLndPaymentConsumerQueueName(c.RabbitMQPaymentConsumerQueueName), + ) + if err != nil { + logger.Fatal(err) + } + //small hack to get the script to work decently + defaultClient := rabbitmqClient.(*rabbitmq.DefaultClient) + + // close the connection gently at the end of the runtime + defer rabbitmqClient.Close() + + result := []models.Invoice{} + err = dbConn.NewSelect().Model(&result).Where("settled_at > ?", startDate).Where("settled_at < ?", endDate).Scan(context.Background()) + if err != nil { + logger.Fatal(err) + } + logrus.Infof("Found %d invoices", len(result)) + svc := &service.LndhubService{ + Config: c, + DB: dbConn, + Logger: logger, + RabbitMQClient: rabbitmqClient, + InvoicePubSub: service.NewPubsub(), + } + ctx := context.Background() + dryRun := os.Getenv("DRY_RUN") == "true" + errCount := 0 + for _, inv := range result { + logger.Infof("Publishing invoice with hash %s", inv.RHash) + if dryRun { + continue + } + err = defaultClient.PublishToLndhubExchange(ctx, inv, svc.EncodeInvoiceWithUserLogin) + if err != nil { + logrus.WithError(err).Error("errror publishing to lndhub exchange") + } + } + logger.Infof("Published %d invoices, # errors %d", len(result), errCount) + +} + +func loadStartAndEndIdFromEnv() (start, end time.Time, err error) { + start, err = time.Parse(time.RFC3339, os.Getenv("START_DATE")) + if err != nil { + return + } + end, err = time.Parse(time.RFC3339, os.Getenv("END_DATE")) + return +} diff --git a/reconciliation_lost_invoices/main.go b/cmd/payment-reconciliation/main.go similarity index 85% rename from reconciliation_lost_invoices/main.go rename to cmd/payment-reconciliation/main.go index bdd41ec2..b13b05b7 100644 --- a/reconciliation_lost_invoices/main.go +++ b/cmd/payment-reconciliation/main.go @@ -47,12 +47,16 @@ func main() { e := echo.New() // Init new LND client + lnCfg, err := lnd.LoadConfig() + if err != nil { + logger.Fatalf("Failed to load lnd config %v", err) + } lndClient, err := lnd.NewLNDclient(lnd.LNDoptions{ - Address: c.LNDAddress, - MacaroonFile: c.LNDMacaroonFile, - MacaroonHex: c.LNDMacaroonHex, - CertFile: c.LNDCertFile, - CertHex: c.LNDCertHex, + Address: lnCfg.LNDAddress, + MacaroonFile: lnCfg.LNDMacaroonFile, + MacaroonHex: lnCfg.LNDMacaroonHex, + CertFile: lnCfg.LNDCertFile, + CertHex: lnCfg.LNDCertHex, }, startupCtx) if err != nil { e.Logger.Fatalf("Error initializing the LND connection: %v", err) diff --git a/main.go b/cmd/server/main.go similarity index 85% rename from main.go rename to cmd/server/main.go index bf00b9d3..fa5a61ab 100644 --- a/main.go +++ b/cmd/server/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "embed" "fmt" "log" "net/http" @@ -11,6 +10,7 @@ import ( "sync" "time" + "github.com/getAlby/lndhub.go/lnd" "github.com/getAlby/lndhub.go/rabbitmq" ddEcho "gopkg.in/DataDog/dd-trace-go.v1/contrib/labstack/echo.v4" "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" @@ -21,6 +21,7 @@ import ( "github.com/getAlby/lndhub.go/lib" "github.com/getAlby/lndhub.go/lib/service" "github.com/getAlby/lndhub.go/lib/tokens" + "github.com/getAlby/lndhub.go/lib/transport" "github.com/getsentry/sentry-go" "github.com/joho/godotenv" "github.com/kelseyhightower/envconfig" @@ -29,12 +30,6 @@ import ( "github.com/uptrace/bun/migrate" ) -//go:embed templates/index.html -var indexHtml string - -//go:embed static/* -var staticContent embed.FS - // @title LndHub.go // @version 0.9.0 // @description Accounting wrapper for the Lightning Network providing separate accounts for end-users. @@ -99,12 +94,16 @@ func main() { } } // Init new LND client - lndClient, err := InitLNClient(c, logger, startupCtx) + lnCfg, err := lnd.LoadConfig() + if err != nil { + logger.Fatalf("Error loading LN config: %v", err) + } + lndClient, err := lnd.InitLNClient(lnCfg, logger, startupCtx) if err != nil { - logger.Fatalf("Error initializing the %s connection: %v", c.LNClientType, err) + logger.Fatalf("Error initializing the %s connection: %v", lnCfg.LNClientType, err) } - logger.Infof("Connected to %s: %s", c.LNClientType, lndClient.GetMainPubkey()) + logger.Infof("Connected to %s: %s", lnCfg.LNClientType, lndClient.GetMainPubkey()) // If no RABBITMQ_URI was provided we will not attempt to create a client // No rabbitmq features will be available in this case. @@ -143,7 +142,7 @@ func main() { } //init echo server - e := initEcho(c, logger) + e := transport.InitEcho(c, logger) //if Datadog is configured, add datadog middleware if c.DatadogAgentUrl != "" { tracer.Start(tracer.WithAgentAddr(c.DatadogAgentUrl)) @@ -151,14 +150,14 @@ func main() { e.Use(ddEcho.Middleware(ddEcho.WithServiceName("lndhub.go"))) } - logMw := createLoggingMiddleware(logger) + logMw := transport.CreateLoggingMiddleware(logger) // strict rate limit for requests for sending payments - strictRateLimitMiddleware := createRateLimitMiddleware(c.StrictRateLimit, c.BurstRateLimit) + strictRateLimitMiddleware := transport.CreateRateLimitMiddleware(c.StrictRateLimit, c.BurstRateLimit) secured := e.Group("", tokens.Middleware(c.JWTSecret), logMw) securedWithStrictRateLimit := e.Group("", tokens.Middleware(c.JWTSecret), strictRateLimitMiddleware, logMw) - RegisterLegacyEndpoints(svc, e, secured, securedWithStrictRateLimit, strictRateLimitMiddleware, tokens.AdminTokenMiddleware(c.AdminToken), logMw) - RegisterV2Endpoints(svc, e, secured, securedWithStrictRateLimit, strictRateLimitMiddleware, tokens.AdminTokenMiddleware(c.AdminToken), logMw) + transport.RegisterLegacyEndpoints(svc, e, secured, securedWithStrictRateLimit, strictRateLimitMiddleware, tokens.AdminTokenMiddleware(c.AdminToken), logMw) + transport.RegisterV2Endpoints(svc, e, secured, securedWithStrictRateLimit, strictRateLimitMiddleware, tokens.AdminTokenMiddleware(c.AdminToken), logMw) //Swagger API spec docs.SwaggerInfo.Host = c.Host @@ -169,7 +168,7 @@ func main() { // Subscribe to LND invoice updates in the background backgroundWg.Add(1) go func() { - err = StartInvoiceRoutine(svc, backGroundCtx) + err = svc.StartInvoiceRoutine(backGroundCtx) if err != nil { sentry.CaptureException(err) //we want to restart in case of an error here @@ -182,7 +181,7 @@ func main() { // Check the status of all pending outgoing payments backgroundWg.Add(1) go func() { - err = StartPendingPaymentRoutine(svc, backGroundCtx) + err = svc.StartPendingPaymentRoutine(backGroundCtx) if err != nil { sentry.CaptureException(err) //in case of an error here no restart is necessary @@ -223,7 +222,7 @@ func main() { //Start Prometheus server if necessary var echoPrometheus *echo.Echo if svc.Config.EnablePrometheus { - go startPrometheusEcho(logger, svc, e) + go transport.StartPrometheusEcho(logger, svc, e) } // Start server diff --git a/integration_tests/util.go b/integration_tests/util.go index 3297ada9..1d7812dd 100644 --- a/integration_tests/util.go +++ b/integration_tests/util.go @@ -54,8 +54,6 @@ func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *servi JWTSecret: []byte("SECRET"), JWTAccessTokenExpiry: 3600, JWTRefreshTokenExpiry: 3600, - LNDAddress: mockLNDAddress, - LNDMacaroonHex: mockLNDMacaroonHex, } rabbitmqUri, ok := os.LookupEnv("RABBITMQ_URI") diff --git a/lib/service/background_routines.go b/lib/service/background_routines.go new file mode 100644 index 00000000..11cc2704 --- /dev/null +++ b/lib/service/background_routines.go @@ -0,0 +1,32 @@ +package service + +import ( + "context" +) + +func (svc *LndhubService) StartInvoiceRoutine(ctx context.Context) (err error) { + if svc.RabbitMQClient != nil { + err = svc.RabbitMQClient.SubscribeToLndInvoices(ctx, svc.ProcessInvoiceUpdate) + if err != nil && err != context.Canceled { + return err + } + + return nil + } else { + err = svc.InvoiceUpdateSubscription(ctx) + if err != nil && err != context.Canceled { + // in case of an error in this routine, we want to restart LNDhub + return err + } + + return nil + } +} + +func (svc *LndhubService) StartPendingPaymentRoutine(ctx context.Context) (err error) { + if svc.RabbitMQClient != nil { + return svc.RabbitMQClient.FinalizeInitializedPayments(ctx, svc) + } else { + return svc.CheckAllPendingOutgoingPayments(ctx) + } +} diff --git a/lib/service/config.go b/lib/service/config.go index ee1f9988..6ddb0cfb 100644 --- a/lib/service/config.go +++ b/lib/service/config.go @@ -5,12 +5,6 @@ import ( "strings" ) -const ( - LND_CLIENT_TYPE = "lnd" - LND_CLUSTER_CLIENT_TYPE = "lnd_cluster" - ECLAIR_CLIENT_TYPE = "eclair" -) - type Config struct { DatabaseUri string `envconfig:"DATABASE_URI" required:"true"` DatabaseMaxConns int `envconfig:"DATABASE_MAX_CONNS" default:"10"` @@ -25,14 +19,6 @@ type Config struct { AdminToken string `envconfig:"ADMIN_TOKEN"` JWTRefreshTokenExpiry int `envconfig:"JWT_REFRESH_EXPIRY" default:"604800"` // in seconds, default 7 days JWTAccessTokenExpiry int `envconfig:"JWT_ACCESS_EXPIRY" default:"172800"` // in seconds, default 2 days - LNClientType string `envconfig:"LN_CLIENT_TYPE" default:"lnd"` //lnd, lnd_cluster, eclair - LNDAddress string `envconfig:"LND_ADDRESS" required:"true"` - LNDMacaroonFile string `envconfig:"LND_MACAROON_FILE"` - LNDCertFile string `envconfig:"LND_CERT_FILE"` - LNDMacaroonHex string `envconfig:"LND_MACAROON_HEX"` - LNDCertHex string `envconfig:"LND_CERT_HEX"` - LNDClusterLivenessPeriod int `envconfig:"LND_CLUSTER_LIVENESS_PERIOD" default:"10"` - LNDClusterActiveChannelRatio float64 `envconfig:"LND_CLUSTER_ACTIVE_CHANNEL_RATIO" default:"0.5"` CustomName string `envconfig:"CUSTOM_NAME"` Host string `envconfig:"HOST" default:"localhost:3000"` Port int `envconfig:"PORT" default:"3000"` diff --git a/init_echo.go b/lib/transport/echo.go similarity index 88% rename from init_echo.go rename to lib/transport/echo.go index 42f4d7d1..53e94040 100644 --- a/init_echo.go +++ b/lib/transport/echo.go @@ -1,6 +1,7 @@ -package main +package transport import ( + "embed" "fmt" "log" "strconv" @@ -21,7 +22,13 @@ import ( "golang.org/x/time/rate" ) -func initEcho(c *service.Config, logger *lecho.Logger) (e *echo.Echo) { +//go:embed templates/index.html +var indexHtml string + +//go:embed static/* +var staticContent embed.FS + +func InitEcho(c *service.Config, logger *lecho.Logger) (e *echo.Echo) { // New Echo app e = echo.New() @@ -45,7 +52,8 @@ func initEcho(c *service.Config, logger *lecho.Logger) (e *echo.Echo) { } return e } -func createLoggingMiddleware(logger *lecho.Logger) echo.MiddlewareFunc { + +func CreateLoggingMiddleware(logger *lecho.Logger) echo.MiddlewareFunc { return lecho.Middleware(lecho.Config{ Logger: logger, Enricher: func(c echo.Context, logger zerolog.Context) zerolog.Context { @@ -54,7 +62,7 @@ func createLoggingMiddleware(logger *lecho.Logger) echo.MiddlewareFunc { }) } -func createRateLimitMiddleware(requestsPerSecond int, burst int) echo.MiddlewareFunc { +func CreateRateLimitMiddleware(requestsPerSecond int, burst int) echo.MiddlewareFunc { config := middleware.RateLimiterConfig{ Store: middleware.NewRateLimiterMemoryStoreWithConfig( middleware.RateLimiterMemoryStoreConfig{Rate: rate.Limit(requestsPerSecond), Burst: burst}, @@ -96,7 +104,7 @@ func createCacheClient() *cache.Client { return cacheClient } -func startPrometheusEcho(logger *lecho.Logger, svc *service.LndhubService, e *echo.Echo) { +func StartPrometheusEcho(logger *lecho.Logger, svc *service.LndhubService, e *echo.Echo) { // Create Prometheus server and Middleware echoPrometheus := echo.New() echoPrometheus.HideBanner = true diff --git a/legacy_endpoints.go b/lib/transport/legacy_endpoints.go similarity index 99% rename from legacy_endpoints.go rename to lib/transport/legacy_endpoints.go index f3f82ad1..7ac17cd9 100644 --- a/legacy_endpoints.go +++ b/lib/transport/legacy_endpoints.go @@ -1,4 +1,4 @@ -package main +package transport import ( "net/http" diff --git a/static/css/style.css b/lib/transport/static/css/style.css similarity index 100% rename from static/css/style.css rename to lib/transport/static/css/style.css diff --git a/static/img/alby.svg b/lib/transport/static/img/alby.svg similarity index 100% rename from static/img/alby.svg rename to lib/transport/static/img/alby.svg diff --git a/static/img/favicon.png b/lib/transport/static/img/favicon.png similarity index 100% rename from static/img/favicon.png rename to lib/transport/static/img/favicon.png diff --git a/static/img/logo.png b/lib/transport/static/img/logo.png similarity index 100% rename from static/img/logo.png rename to lib/transport/static/img/logo.png diff --git a/templates/index.html b/lib/transport/templates/index.html similarity index 100% rename from templates/index.html rename to lib/transport/templates/index.html diff --git a/v2_endpoints.go b/lib/transport/v2_endpoints.go similarity index 98% rename from v2_endpoints.go rename to lib/transport/v2_endpoints.go index 68daa86f..170cea32 100644 --- a/v2_endpoints.go +++ b/lib/transport/v2_endpoints.go @@ -1,4 +1,4 @@ -package main +package transport import ( v2controllers "github.com/getAlby/lndhub.go/controllers_v2" diff --git a/lnd/config.go b/lnd/config.go new file mode 100644 index 00000000..381ba361 --- /dev/null +++ b/lnd/config.go @@ -0,0 +1,31 @@ +package lnd + +import ( + "github.com/kelseyhightower/envconfig" +) + +const ( + LND_CLIENT_TYPE = "lnd" + LND_CLUSTER_CLIENT_TYPE = "lnd_cluster" + ECLAIR_CLIENT_TYPE = "eclair" +) + +type Config struct { + LNClientType string `envconfig:"LN_CLIENT_TYPE" default:"lnd"` //lnd, lnd_cluster, eclair + LNDAddress string `envconfig:"LND_ADDRESS" required:"true"` + LNDMacaroonFile string `envconfig:"LND_MACAROON_FILE"` + LNDCertFile string `envconfig:"LND_CERT_FILE"` + LNDMacaroonHex string `envconfig:"LND_MACAROON_HEX"` + LNDCertHex string `envconfig:"LND_CERT_HEX"` + LNDClusterLivenessPeriod int `envconfig:"LND_CLUSTER_LIVENESS_PERIOD" default:"10"` + LNDClusterActiveChannelRatio float64 `envconfig:"LND_CLUSTER_ACTIVE_CHANNEL_RATIO" default:"0.5"` +} + +func LoadConfig() (c *Config, err error) { + c = &Config{} + err = envconfig.Process("", c) + if err != nil { + return nil, err + } + return c, nil +} diff --git a/lnd/interface.go b/lnd/interface.go deleted file mode 100644 index 15fdf5a6..00000000 --- a/lnd/interface.go +++ /dev/null @@ -1,28 +0,0 @@ -package lnd - -import ( - "context" - - "github.com/lightningnetwork/lnd/lnrpc" - "github.com/lightningnetwork/lnd/lnrpc/routerrpc" - "google.golang.org/grpc" -) - -type LightningClientWrapper interface { - ListChannels(ctx context.Context, req *lnrpc.ListChannelsRequest, options ...grpc.CallOption) (*lnrpc.ListChannelsResponse, error) - SendPaymentSync(ctx context.Context, req *lnrpc.SendRequest, options ...grpc.CallOption) (*lnrpc.SendResponse, error) - AddInvoice(ctx context.Context, req *lnrpc.Invoice, options ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) - SubscribeInvoices(ctx context.Context, req *lnrpc.InvoiceSubscription, options ...grpc.CallOption) (SubscribeInvoicesWrapper, error) - SubscribePayment(ctx context.Context, req *routerrpc.TrackPaymentRequest, options ...grpc.CallOption) (SubscribePaymentWrapper, error) - GetInfo(ctx context.Context, req *lnrpc.GetInfoRequest, options ...grpc.CallOption) (*lnrpc.GetInfoResponse, error) - DecodeBolt11(ctx context.Context, bolt11 string, options ...grpc.CallOption) (*lnrpc.PayReq, error) - IsIdentityPubkey(pubkey string) (isOurPubkey bool) - GetMainPubkey() (pubkey string) -} - -type SubscribeInvoicesWrapper interface { - Recv() (*lnrpc.Invoice, error) -} -type SubscribePaymentWrapper interface { - Recv() (*lnrpc.Payment, error) -} diff --git a/init_lnd.go b/lnd/ln_client.go similarity index 51% rename from init_lnd.go rename to lnd/ln_client.go index 44d64405..41b3853b 100644 --- a/init_lnd.go +++ b/lnd/ln_client.go @@ -1,29 +1,48 @@ -package main +package lnd import ( "context" "fmt" "strings" - "github.com/getAlby/lndhub.go/lib/service" - "github.com/getAlby/lndhub.go/lnd" "github.com/lightningnetwork/lnd/lnrpc" + "github.com/lightningnetwork/lnd/lnrpc/routerrpc" "github.com/ziflex/lecho/v3" + "google.golang.org/grpc" ) -func InitLNClient(c *service.Config, logger *lecho.Logger, ctx context.Context) (result lnd.LightningClientWrapper, err error) { +type LightningClientWrapper interface { + ListChannels(ctx context.Context, req *lnrpc.ListChannelsRequest, options ...grpc.CallOption) (*lnrpc.ListChannelsResponse, error) + SendPaymentSync(ctx context.Context, req *lnrpc.SendRequest, options ...grpc.CallOption) (*lnrpc.SendResponse, error) + AddInvoice(ctx context.Context, req *lnrpc.Invoice, options ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) + SubscribeInvoices(ctx context.Context, req *lnrpc.InvoiceSubscription, options ...grpc.CallOption) (SubscribeInvoicesWrapper, error) + SubscribePayment(ctx context.Context, req *routerrpc.TrackPaymentRequest, options ...grpc.CallOption) (SubscribePaymentWrapper, error) + GetInfo(ctx context.Context, req *lnrpc.GetInfoRequest, options ...grpc.CallOption) (*lnrpc.GetInfoResponse, error) + DecodeBolt11(ctx context.Context, bolt11 string, options ...grpc.CallOption) (*lnrpc.PayReq, error) + IsIdentityPubkey(pubkey string) (isOurPubkey bool) + GetMainPubkey() (pubkey string) +} + +type SubscribeInvoicesWrapper interface { + Recv() (*lnrpc.Invoice, error) +} +type SubscribePaymentWrapper interface { + Recv() (*lnrpc.Payment, error) +} + +func InitLNClient(c *Config, logger *lecho.Logger, ctx context.Context) (result LightningClientWrapper, err error) { switch c.LNClientType { - case service.LND_CLIENT_TYPE: + case LND_CLIENT_TYPE: return InitSingleLNDClient(c, ctx) - case service.LND_CLUSTER_CLIENT_TYPE: + case LND_CLUSTER_CLIENT_TYPE: return InitLNDCluster(c, logger, ctx) default: return nil, fmt.Errorf("Did not recognize LN client type %s", c.LNClientType) } } -func InitSingleLNDClient(c *service.Config, ctx context.Context) (result lnd.LightningClientWrapper, err error) { - client, err := lnd.NewLNDclient(lnd.LNDoptions{ +func InitSingleLNDClient(c *Config, ctx context.Context) (result LightningClientWrapper, err error) { + client, err := NewLNDclient(LNDoptions{ Address: c.LNDAddress, MacaroonFile: c.LNDMacaroonFile, MacaroonHex: c.LNDMacaroonHex, @@ -40,8 +59,8 @@ func InitSingleLNDClient(c *service.Config, ctx context.Context) (result lnd.Lig client.IdentityPubkey = getInfo.IdentityPubkey return client, nil } -func InitLNDCluster(c *service.Config, logger *lecho.Logger, ctx context.Context) (result lnd.LightningClientWrapper, err error) { - nodes := []lnd.LightningClientWrapper{} +func InitLNDCluster(c *Config, logger *lecho.Logger, ctx context.Context) (result LightningClientWrapper, err error) { + nodes := []LightningClientWrapper{} //interpret lnd address, macaroon file & cert file as comma seperated values addresses := strings.Split(c.LNDAddress, ",") macaroons := strings.Split(c.LNDMacaroonFile, ",") @@ -50,7 +69,7 @@ func InitLNDCluster(c *service.Config, logger *lecho.Logger, ctx context.Context return nil, fmt.Errorf("Error parsing LND cluster config: addresses, macaroons or certs array length mismatch") } for i := 0; i < len(addresses); i++ { - n, err := lnd.NewLNDclient(lnd.LNDoptions{ + n, err := NewLNDclient(LNDoptions{ Address: addresses[i], MacaroonFile: macaroons[i], CertFile: certs[i], @@ -66,7 +85,7 @@ func InitLNDCluster(c *service.Config, logger *lecho.Logger, ctx context.Context nodes = append(nodes, n) } logger.Infof("Initialized LND cluster with %d nodes", len(nodes)) - cluster := &lnd.LNDCluster{ + cluster := &LNDCluster{ Nodes: nodes, ActiveChannelRatio: c.LNDClusterActiveChannelRatio, ActiveNode: nodes[0], diff --git a/rabbitmq/rabbitmq.go b/rabbitmq/rabbitmq.go index d4a48f8b..9308f41c 100644 --- a/rabbitmq/rabbitmq.go +++ b/rabbitmq/rabbitmq.go @@ -377,7 +377,7 @@ func (client *DefaultClient) StartPublishInvoices(ctx context.Context, invoicesS case <-ctx.Done(): return context.Canceled case incomingInvoice := <-in: - err = client.publishToLndhubExchange(ctx, incomingInvoice, payloadFunc) + err = client.PublishToLndhubExchange(ctx, incomingInvoice, payloadFunc) if err != nil { captureErr(client.logger, err, log.JSON{ @@ -387,7 +387,7 @@ func (client *DefaultClient) StartPublishInvoices(ctx context.Context, invoicesS }) } case outgoing := <-out: - err = client.publishToLndhubExchange(ctx, outgoing, payloadFunc) + err = client.PublishToLndhubExchange(ctx, outgoing, payloadFunc) if err != nil { captureErr(client.logger, err, log.JSON{ @@ -400,7 +400,7 @@ func (client *DefaultClient) StartPublishInvoices(ctx context.Context, invoicesS } } -func (client *DefaultClient) publishToLndhubExchange(ctx context.Context, invoice models.Invoice, payloadFunc EncodeOutgoingInvoiceFunc) error { +func (client *DefaultClient) PublishToLndhubExchange(ctx context.Context, invoice models.Invoice, payloadFunc EncodeOutgoingInvoiceFunc) error { payload := bufPool.Get().(*bytes.Buffer) err := payloadFunc(ctx, payload, invoice) if err != nil {