Skip to content

Commit d04e485

Browse files
committed
terminal: separate basic and full LND client setup
In the upcoming actions migration, we will need to fetch LND's macaroons prior creating litd's stores, and therefore we need to connect to LND prior to creating the stores. To avoid having to wait for LND to fully sync before creating the stores (and, by extension, Litd's RPC servers), we separate the basic LND client setup from the full LND client setup. Only the full setup requires a fully synced LND.
1 parent 49aa365 commit d04e485

File tree

1 file changed

+76
-38
lines changed

1 file changed

+76
-38
lines changed

terminal.go

Lines changed: 76 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,16 @@ func (g *LightningTerminal) start(ctx context.Context) error {
695695
}
696696
}
697697

698-
// Set up all the LND clients required by LiT.
699-
err = g.setUpLNDClients(ctx, lndQuit)
698+
// Since we are now connected to LND, we can now set up a basic LND
699+
// client. Note this doesn't require LND to be synced, but can still be
700+
// used to fetch info from LND such as its macaroons. Therefore, it's ok
701+
// set it up prior to setting up the stores and starting the other RPC
702+
// servers, as the setup will be fast.
703+
err = g.setupBasicLNDClient(ctx, lndQuit)
700704
if err != nil {
701705
g.statusMgr.SetErrored(
702-
subservers.LND, "could not set up LND clients: %v", err,
706+
subservers.LND,
707+
"could not to set up a basic LND client: %v", err,
703708
)
704709

705710
return fmt.Errorf("could not start LND")
@@ -776,6 +781,18 @@ func (g *LightningTerminal) start(ctx context.Context) error {
776781
return fmt.Errorf("could not register RPC servers: %v", err)
777782
}
778783

784+
// Set up a full LND client. With this, we now have all LND clients
785+
// needed for LiT to be fully started.
786+
err = g.setupFullNDClient(ctx, lndQuit)
787+
if err != nil {
788+
g.statusMgr.SetErrored(
789+
subservers.LND,
790+
"could not to set up a full LND client: %v", err,
791+
)
792+
793+
return fmt.Errorf("could not start LND")
794+
}
795+
779796
// Mark that lnd is now completely running after connecting the
780797
// lnd clients.
781798
g.statusMgr.SetRunning(subservers.LND)
@@ -826,13 +843,35 @@ func (g *LightningTerminal) basicLNDClient() (lnrpc.LightningClient, error) {
826843
return g.basicClient, nil
827844
}
828845

829-
// setUpLNDClients sets up the various LND clients required by LiT.
830-
func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
846+
// checkRunning checks if we should continue running for the duration of the
847+
// defaultStartupTimeout, or else returns an error indicating why a shut-down is
848+
// needed.
849+
func (g *LightningTerminal) checkRunning(ctx context.Context,
850+
lndQuit chan struct{}) error {
851+
852+
select {
853+
case err := <-g.errQueue.ChanOut():
854+
return fmt.Errorf("error from subsystem: %v", err)
855+
856+
case <-lndQuit:
857+
return fmt.Errorf("LND has stopped")
858+
859+
case <-ctx.Done():
860+
return ctx.Err()
861+
862+
case <-time.After(g.cfg.LndConnectInterval):
863+
return nil
864+
}
865+
}
866+
867+
// setupBasicLNDClient sets up a basic LND client that can be used to connect to
868+
// LND without requiring LND to be fully synced. Since this client is only a
869+
// basic client, not all of LNDs functionality is available through it.
870+
func (g *LightningTerminal) setupBasicLNDClient(ctx context.Context,
831871
lndQuit chan struct{}) error {
832872

833873
var (
834874
err error
835-
insecure bool
836875
clientOptions []lndclient.BasicClientOption
837876
)
838877

@@ -847,36 +886,13 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
847886
// If we're in integrated mode, we can retrieve the macaroon string
848887
// from lnd directly, rather than grabbing it from disk.
849888
if g.cfg.LndMode == ModeIntegrated {
850-
// Set to true in integrated mode, since we will not require tls
851-
// when communicating with lnd via a bufconn.
852-
insecure = true
853889
clientOptions = append(clientOptions, lndclient.Insecure())
854890
}
855891

856-
// checkRunning checks if we should continue running for the duration of
857-
// the defaultStartupTimeout, or else returns an error indicating why
858-
// a shut-down is needed.
859-
checkRunning := func() error {
860-
select {
861-
case err := <-g.errQueue.ChanOut():
862-
return fmt.Errorf("error from subsystem: %v", err)
863-
864-
case <-lndQuit:
865-
return fmt.Errorf("LND has stopped")
866-
867-
case <-ctx.Done():
868-
return ctx.Err()
869-
870-
case <-time.After(g.cfg.LndConnectInterval):
871-
return nil
872-
}
873-
}
874-
875892
// The main RPC listener of lnd might need some time to start, it could
876893
// be that we run into a connection refused a few times. We use the
877894
// basic client connection to find out if the RPC server is started yet
878-
// because that doesn't do anything else than just connect. We'll check
879-
// if lnd is also ready to be used in the next step.
895+
// because that doesn't do anything else than just connect.
880896
log.Infof("Connecting basic lnd client")
881897

882898
for {
@@ -898,7 +914,7 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
898914
"Error when setting up basic LND Client: %v", err,
899915
)
900916

901-
err = checkRunning()
917+
err = g.checkRunning(ctx, lndQuit)
902918
if err != nil {
903919
return err
904920
}
@@ -921,12 +937,34 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
921937
g.cfg.statelessInitMode = macService.StatelessInit
922938
}
923939

924-
// Now we know that the connection itself is ready. But we also need to
925-
// wait for two things: The chain notifier to be ready and the lnd
926-
// wallet being fully synced to its chain backend. The chain notifier
927-
// will always be ready first so if we instruct the lndclient to wait
928-
// for the wallet sync, we should be fully ready to start all our
929-
// subservers. This will just block until lnd signals readiness.
940+
return nil
941+
}
942+
943+
// setupFullNDClient connects a up a full LND client to LND. Note that the setup
944+
// of this client will block until LND is fully synced and unlocked.
945+
func (g *LightningTerminal) setupFullNDClient(ctx context.Context,
946+
lndQuit chan struct{}) error {
947+
948+
var (
949+
err error
950+
insecure bool
951+
)
952+
953+
host, network, tlsPath, macPath, macData := g.cfg.lndConnectParams()
954+
955+
if g.cfg.LndMode == ModeIntegrated {
956+
// Ssince we will not require tls when communicating with lnd
957+
// via a bufconn in integrated mode, we set the insecure flag
958+
// to true.
959+
insecure = true
960+
}
961+
962+
// When setting up a full LND client, we we need to wait for two things:
963+
// The chain notifier to be ready and the lnd wallet being fully synced
964+
// to its chain backend. The chain notifier will always be ready first
965+
// so if we instruct the lndclient to wait for the wallet sync, we
966+
// should be fully ready to start all our subservers. This will just
967+
// block until lnd signals readiness.
930968
log.Infof("Connecting full lnd client")
931969
for {
932970
g.lndClient, err = lndclient.NewLndServices(
@@ -959,7 +997,7 @@ func (g *LightningTerminal) setUpLNDClients(ctx context.Context,
959997
err,
960998
)
961999

962-
err = checkRunning()
1000+
err = g.checkRunning(ctx, lndQuit)
9631001
if err != nil {
9641002
return err
9651003
}

0 commit comments

Comments
 (0)