Skip to content

Commit

Permalink
implement republish invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
frnandu committed Dec 27, 2023
1 parent 93fd974 commit 2fc0a6e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ COPY . .
# Build the application
RUN go build -o main

# Build the utility scripts
RUN go build ./cmd/republish-invoices

# 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/republish-invoices /bin/

ENTRYPOINT [ "/bin/main" ]
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ Possible missed-while-offline outgoing payments are handled by looking up the ea
- Routing key: `invoice.incoming.settled`
# LND outgoing payments
- Payload [lnrpc.Payment](https://github.com/lightningnetwork/lnd/blob/master/lnrpc/lightning.pb.go#L12612)
- Routing keys `payment.outgoing.settled`, `payment.outgoing.error`
- Routing keys `payment.outgoing.settled`, `payment.outgoing.error`

# REPUBLISH INVOICES

If you need to republish settled invoices to update state in lndhub, you can use the cmd/republish-invoices by providing all payment hashes separated by commas:
- "REPUBLISH_INVOICE_HASHES" : `<hash_1>,<hash_2>....<hash_n>`

Use this in a job by setting:
```command:
- /bin/republish-invoices```
26 changes: 18 additions & 8 deletions cmd/publish-invoices/main.go → cmd/republish-invoices/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package main

import (
"context"
"encoding/hex"
"github.com/getAlby/ln-event-publisher/config"
"github.com/getAlby/ln-event-publisher/db"
"github.com/getAlby/ln-event-publisher/lnd"
"github.com/getAlby/ln-event-publisher/service"
"github.com/getsentry/sentry-go"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/sirupsen/logrus"
"os"
"os/signal"
)

func main() {
Expand Down Expand Up @@ -50,21 +52,29 @@ func main() {
logrus.Fatal(err)
}
logrus.Infof("Connected to LND: %s - %s", resp.Alias, resp.IdentityPubkey)
logrus.Info("Opening PG database")
db, err := db.OpenDB(c)
if err != nil {
sentry.CaptureException(err)
logrus.Fatal(err)
}
svc := &service.Service{
Cfg: c,
Lnd: client,
Db: db,
}
err = svc.InitRabbitMq()
if err != nil {
sentry.CaptureException(err)
logrus.Fatal(err)
}
backgroundCtx := context.Background()
ctx, _ := signal.NotifyContext(backgroundCtx, os.Interrupt)

for i := 0; i < len(c.RepublishInvoiceHashes); i++ {
hashBytes, err := hex.DecodeString(c.RepublishInvoiceHashes[i])
if err != nil {
logrus.Error("Invalid Hash ", c.RepublishInvoiceHashes[i], " ", err)
continue
}

// Create a PaymentHash struct
paymentHash := &lnrpc.PaymentHash{
RHash: hashBytes,
}
svc.RepublishInvoice(ctx, paymentHash)
}
}
21 changes: 11 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package config

type Config struct {
LNDAddress string `envconfig:"LND_ADDRESS" required:"true"`
LNDMacaroonFile string `envconfig:"LND_MACAROON_FILE"`
LNDCertFile string `envconfig:"LND_CERT_FILE"`
DatabaseUri string `envconfig:"DATABASE_URI" required:"true"`
DatabaseMaxConns int `envconfig:"DATABASE_MAX_CONNS" default:"10"`
DatabaseMaxIdleConns int `envconfig:"DATABASE_MAX_IDLE_CONNS" default:"5"`
DatabaseConnMaxLifetime int `envconfig:"DATABASE_CONN_MAX_LIFETIME" default:"1800"` // 30 minutes
RabbitMQUri string `envconfig:"RABBITMQ_URI" required:"true"`
RabbitMQTimeoutSeconds int `envconfig:"RABBITMQ_TIMEOUT_SECONDS" default:"10"`
SentryDSN string `envconfig:"SENTRY_DSN"`
LNDAddress string `envconfig:"LND_ADDRESS" required:"true"`
LNDMacaroonFile string `envconfig:"LND_MACAROON_FILE"`
LNDCertFile string `envconfig:"LND_CERT_FILE"`
DatabaseUri string `envconfig:"DATABASE_URI" required:"true"`
DatabaseMaxConns int `envconfig:"DATABASE_MAX_CONNS" default:"10"`
DatabaseMaxIdleConns int `envconfig:"DATABASE_MAX_IDLE_CONNS" default:"5"`
DatabaseConnMaxLifetime int `envconfig:"DATABASE_CONN_MAX_LIFETIME" default:"1800"` // 30 minutes
RabbitMQUri string `envconfig:"RABBITMQ_URI" required:"true"`
RabbitMQTimeoutSeconds int `envconfig:"RABBITMQ_TIMEOUT_SECONDS" default:"10"`
SentryDSN string `envconfig:"SENTRY_DSN"`
RepublishInvoiceHashes []string `envconfig:"REPUBLISH_INVOICE_HASHES"`
}
1 change: 1 addition & 0 deletions lnd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type LightningClientWrapper interface {
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)
ListPayments(ctx context.Context, req *lnrpc.ListPaymentsRequest, options ...grpc.CallOption) (*lnrpc.ListPaymentsResponse, error)
LookupInvoice(ctx context.Context, req *lnrpc.PaymentHash, options ...grpc.CallOption) (*lnrpc.Invoice, error)
}

type SubscribeInvoicesWrapper interface {
Expand Down
4 changes: 4 additions & 0 deletions lnd/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ func (wrapper *LNDWrapper) SubscribePayments(ctx context.Context, req *routerrpc
func (wrapper *LNDWrapper) ListPayments(ctx context.Context, req *lnrpc.ListPaymentsRequest, options ...grpc.CallOption) (*lnrpc.ListPaymentsResponse, error) {
return wrapper.client.ListPayments(ctx, req)
}

func (wrapper *LNDWrapper) LookupInvoice(ctx context.Context, req *lnrpc.PaymentHash, options ...grpc.CallOption) (*lnrpc.Invoice, error) {
return wrapper.client.LookupInvoice(ctx, req)
}
16 changes: 16 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,19 @@ func (svc *Service) PublishPayload(ctx context.Context, payload interface{}, exc

return err
}

func (svc *Service) RepublishInvoice(ctx context.Context, paymentHash *lnrpc.PaymentHash) {
invoice, err := svc.Lnd.LookupInvoice(ctx, paymentHash)
if err != nil {
sentry.CaptureException(err)
logrus.Error("Invoice NOT FOUND ", paymentHash, err)
return
}
err = svc.ProcessInvoice(ctx, invoice)
if err != nil {
sentry.CaptureException(err)
logrus.Error("ERROR while trying to republish invoice ", paymentHash, err)
} else {
logrus.Info("Invoice Republished ", paymentHash, err)
}
}

0 comments on commit 2fc0a6e

Please sign in to comment.