diff --git a/.github/workflows/build_reconciliation.yaml b/.github/workflows/build_reconciliation.yaml new file mode 100644 index 00000000..04091e51 --- /dev/null +++ b/.github/workflows/build_reconciliation.yaml @@ -0,0 +1,21 @@ +name: Reconciliation Docker build & push +on: + push: +jobs: + build: + env: + REGISTRY: ghcr.io + IMAGENAME: lndhub-reconciliation + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + name: Check out code + - name: Docker build + uses: mr-smithers-excellent/docker-build-push@v5 + id: build + with: + dockerfile: reconciliation.Dockerfile + image: ${{ env.IMAGENAME }} + registry: ${{ env.REGISTRY }} + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/lnd/lnd.go b/lnd/lnd.go index c1640e95..1b3ea057 100644 --- a/lnd/lnd.go +++ b/lnd/lnd.go @@ -137,3 +137,7 @@ func (wrapper *LNDWrapper) IsIdentityPubkey(pubkey string) (isOurPubkey bool) { func (wrapper *LNDWrapper) GetMainPubkey() (pubkey string) { return wrapper.IdentityPubkey } + +func (wrapper *LNDWrapper) ListInvoices(ctx context.Context, req *lnrpc.ListInvoiceRequest, options ...grpc.CallOption) (*lnrpc.ListInvoiceResponse, error) { + return wrapper.client.ListInvoices(ctx, req, options...) +} diff --git a/reconciliation.Dockerfile b/reconciliation.Dockerfile new file mode 100644 index 00000000..0888b7c2 --- /dev/null +++ b/reconciliation.Dockerfile @@ -0,0 +1,24 @@ +FROM golang:1.20-alpine as builder + +# Move to working directory /build +WORKDIR /build + +# Copy and download dependency using go mod +COPY go.mod . +COPY go.sum . +RUN go mod download + +# Copy the code into the container +COPY . . + +# Build the application +WORKDIR /build/reconciliation_lost_invoices +RUN go build -o main + +# 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/reconciliation_lost_invoices/main /bin/ + +ENTRYPOINT [ "/bin/main" ] diff --git a/reconciliation_lost_invoices/main.go b/reconciliation_lost_invoices/main.go new file mode 100644 index 00000000..a2285f9b --- /dev/null +++ b/reconciliation_lost_invoices/main.go @@ -0,0 +1,79 @@ +package main + +import ( + "context" + "fmt" + "log" + + "github.com/getAlby/lndhub.go/db" + "github.com/getAlby/lndhub.go/lib" + "github.com/getAlby/lndhub.go/lib/service" + "github.com/getAlby/lndhub.go/lnd" + "github.com/getsentry/sentry-go" + "github.com/joho/godotenv" + "github.com/kelseyhightower/envconfig" + "github.com/labstack/echo/v4" +) + +// script to reconcile pending payments between the backup node and the database +func main() { + + c := &service.Config{} + + // Load configruation from environment variables + err := godotenv.Load(".env") + if err != nil { + fmt.Println("Failed to load .env file") + } + err = envconfig.Process("", c) + if err != nil { + log.Fatalf("Error loading environment variables: %v", err) + } + + // Setup logging to STDOUT or a configrued log file + logger := lib.Logger(c.LogFilePath) + + // 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) + } + + // Migrate the DB + //Todo: use timeout for startupcontext + startupCtx := context.Background() + + // New Echo app + e := echo.New() + + //// Init new LND client + 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 { + e.Logger.Fatalf("Error initializing the LND connection: %v", err) + } + logger.Infof("Connected to LND: %s ", lndClient.GetMainPubkey()) + + svc := &service.LndhubService{ + Config: c, + DB: dbConn, + LndClient: lndClient, + Logger: logger, + InvoicePubSub: service.NewPubsub(), + } + + pendingPayments, err := svc.GetAllPendingPayments(startupCtx) + if err != nil { + return + } + + err = svc.CheckPendingOutgoingPayments(startupCtx, pendingPayments) + if err != nil { + sentry.CaptureException(err) + svc.Logger.Error(err) + } +}