From f3a3de9ac4687a1768ffc1604e9409a75977c8a6 Mon Sep 17 00:00:00 2001 From: bitromortac Date: Fri, 26 Sep 2025 09:39:05 +0200 Subject: [PATCH] faraday: start chan events monitor --- chanevents/monitor.go | 12 +++++++++--- faraday.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/chanevents/monitor.go b/chanevents/monitor.go index 6ff52f0..c83a65c 100644 --- a/chanevents/monitor.go +++ b/chanevents/monitor.go @@ -94,15 +94,21 @@ func (m *Monitor) monitorLoop(ctx context.Context) { log.Info("Channel events monitor starting") + var synced bool + for { // Wait for lnd to be synced to chain, retrying on RPC errors. if !m.waitForReady(ctx) { return } - // Initial state sync. - if err := m.initialSync(ctx); err != nil { - log.Errorf("Error during initial sync: %v", err) + // Initial state sync, only performed once. + if !synced { + if err := m.initialSync(ctx); err != nil { + log.Errorf("Error during initial sync: %v", err) + } else { + synced = true + } } // Subscribe and consume events until the stream breaks or an diff --git a/faraday.go b/faraday.go index b302475..0f15d38 100644 --- a/faraday.go +++ b/faraday.go @@ -18,6 +18,7 @@ import ( proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/jessevdk/go-flags" "github.com/lightninglabs/faraday/chain" + "github.com/lightninglabs/faraday/chanevents" "github.com/lightninglabs/faraday/frdrpc" "github.com/lightninglabs/faraday/frdrpcserver" "github.com/lightninglabs/faraday/frdrpcserver/perms" @@ -90,9 +91,15 @@ type Faraday struct { // reuse of the struct, since internal fields are not reset. stopped atomic.Bool + // monitor is the channel events monitor. + monitor *chanevents.Monitor + // stores contains all the stores used by faraday. stores *stores + // ctxCancel is a function that can be used to cancel the main context. + ctxCancel context.CancelFunc + lnd *lndclient.GrpcLndServices // lndOwned indicates whether Faraday created the lnd connection @@ -446,6 +453,17 @@ func (f *Faraday) Stop() error { // can complete cleanly. f.wg.Wait() + if f.ctxCancel != nil { + f.ctxCancel() + } + + if f.monitor != nil { + if err := f.monitor.Stop(); err != nil { + log.Errorf("Error stopping channel event monitor: %v", + err) + } + } + if f.stores != nil { if err := f.stores.Close(); err != nil { log.Errorf("Error closing stores: %v", err) @@ -552,6 +570,21 @@ func (f *Faraday) initialize(withMacaroonService bool) error { return fmt.Errorf("could not create stores: %v", err) } + // Create the channel event monitor. + f.monitor = chanevents.NewMonitor( + f.lnd.Client, f.stores.ChanEventsStore, + ) + + ctx, cancel := context.WithCancel(context.Background()) + f.ctxCancel = cancel + + if err := f.monitor.Start(ctx); err != nil { + cancel() + + return fmt.Errorf("could not start channel event "+ + "monitor: %v", err) + } + return nil }