Skip to content

Commit 7166877

Browse files
ellemoutonpositiveblue
authored andcommitted
terminal: modularise the Run function
A pure refactor commit. It just splits various parts of the main Run function out into helper functions in order to more clearly show the steps taken to set up LiT.
1 parent 37b24d5 commit 7166877

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

terminal.go

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,39 @@ func (g *LightningTerminal) Run() error {
482482
g.cfg.lndAdminMacaroon = <-macChan
483483
}
484484

485-
err = g.startSubservers()
485+
// Set up all the LND clients required by LiT.
486+
err = g.setUpLNDClients()
486487
if err != nil {
487-
log.Errorf("Could not start subservers: %v", err)
488+
log.Errorf("Could not set up LND clients: %w", err)
488489
return err
489490
}
490491

492+
// If we're in integrated and stateless init mode, we won't create
493+
// macaroon files in any of the subserver daemons.
494+
createDefaultMacaroons := true
495+
if g.cfg.LndMode == ModeIntegrated && g.lndInterceptorChain != nil &&
496+
g.lndInterceptorChain.MacaroonService() != nil {
497+
498+
// If the wallet was initialized in stateless mode, we don't
499+
// want any macaroons lying around on the filesystem. In that
500+
// case only the UI will be able to access any of the integrated
501+
// daemons. In all other cases we want default macaroons so we
502+
// can use the CLI tools to interact with loop/pool/faraday.
503+
macService := g.lndInterceptorChain.MacaroonService()
504+
createDefaultMacaroons = !macService.StatelessInit
505+
}
506+
507+
err = g.startIntegratedDaemons(createDefaultMacaroons)
508+
if err != nil {
509+
log.Errorf("Could not start integrated daemons: %v", err)
510+
return err
511+
}
512+
513+
err = g.startInternalSubServers(createDefaultMacaroons)
514+
if err != nil {
515+
return fmt.Errorf("could not start litd sub-servers: %v", err)
516+
}
517+
491518
// Now block until we receive an error or the main shutdown signal.
492519
select {
493520
case err := <-g.loopServer.ErrChan:
@@ -513,10 +540,8 @@ func (g *LightningTerminal) Run() error {
513540
return nil
514541
}
515542

516-
// startSubservers creates an internal connection to lnd and then starts all
517-
// embedded daemons as external subservers that hook into the same gRPC and REST
518-
// servers that lnd started.
519-
func (g *LightningTerminal) startSubservers() error {
543+
// setUpLNDClients sets up the various LND clients required by LiT.
544+
func (g *LightningTerminal) setUpLNDClients() error {
520545
var (
521546
insecure bool
522547
clientOptions []lndclient.BasicClientOption
@@ -557,7 +582,7 @@ func (g *LightningTerminal) startSubservers() error {
557582
return err
558583
}, defaultStartupTimeout)
559584
if err != nil {
560-
return err
585+
return fmt.Errorf("could not create basic LND Client: %v", err)
561586
}
562587

563588
// Now we know that the connection itself is ready. But we also need to
@@ -600,7 +625,8 @@ func (g *LightningTerminal) startSubservers() error {
600625
},
601626
)
602627
if err != nil {
603-
return err
628+
return fmt.Errorf("could not create LND Services client: %v",
629+
err)
604630
}
605631

606632
// Pass LND's build tags to the permission manager so that it can
@@ -628,26 +654,19 @@ func (g *LightningTerminal) startSubservers() error {
628654
g.rpcProxy.superMacaroon = superMacaroon
629655
}
630656

631-
// If we're in integrated and stateless init mode, we won't create
632-
// macaroon files in any of the subserver daemons.
633-
createDefaultMacaroons := true
634-
if g.cfg.LndMode == ModeIntegrated && g.lndInterceptorChain != nil &&
635-
g.lndInterceptorChain.MacaroonService() != nil {
657+
return nil
658+
}
636659

637-
// If the wallet was initialized in stateless mode, we don't
638-
// want any macaroons lying around on the filesystem. In that
639-
// case only the UI will be able to access any of the integrated
640-
// daemons. In all other cases we want default macaroons so we
641-
// can use the CLI tools to interact with loop/pool/faraday.
642-
macService := g.lndInterceptorChain.MacaroonService()
643-
createDefaultMacaroons = !macService.StatelessInit
644-
}
660+
// startIntegratedDaemons starts all embedded daemons as external sub-servers
661+
// that hook into the same gRPC and REST servers that lnd started.
662+
func (g *LightningTerminal) startIntegratedDaemons(
663+
createDefaultMacaroons bool) error {
645664

646-
// Both connection types are ready now, let's start our subservers if
665+
// Both connection types are ready now, let's start our sub-servers if
647666
// they should be started locally as an integrated service.
648667
if !g.cfg.faradayRemote {
649668
log.Infof("Starting integrated faraday daemon")
650-
err = g.faradayServer.StartAsSubserver(
669+
err := g.faradayServer.StartAsSubserver(
651670
g.lndClient.LndServices, createDefaultMacaroons,
652671
)
653672
if err != nil {
@@ -658,7 +677,7 @@ func (g *LightningTerminal) startSubservers() error {
658677

659678
if !g.cfg.loopRemote {
660679
log.Infof("Starting integrated loop daemon")
661-
err = g.loopServer.StartAsSubserver(
680+
err := g.loopServer.StartAsSubserver(
662681
g.lndClient, createDefaultMacaroons,
663682
)
664683
if err != nil {
@@ -669,7 +688,7 @@ func (g *LightningTerminal) startSubservers() error {
669688

670689
if !g.cfg.poolRemote {
671690
log.Infof("Starting integrated pool daemon")
672-
err = g.poolServer.StartAsSubserver(
691+
err := g.poolServer.StartAsSubserver(
673692
g.basicClient, g.lndClient, createDefaultMacaroons,
674693
)
675694
if err != nil {
@@ -678,6 +697,13 @@ func (g *LightningTerminal) startSubservers() error {
678697
g.poolStarted = true
679698
}
680699

700+
return nil
701+
}
702+
703+
// startInternalSubServers starts all Litd specific sub-servers.
704+
func (g *LightningTerminal) startInternalSubServers(
705+
createDefaultMacaroons bool) error {
706+
681707
log.Infof("Starting LiT macaroon service")
682708

683709
// Set up the macaroon service.
@@ -770,15 +796,10 @@ func (g *LightningTerminal) startSubservers() error {
770796
}
771797

772798
if !g.cfg.Autopilot.Disable {
773-
info, err := g.lndClient.Client.GetInfo(ctxc)
774-
if err != nil {
775-
return fmt.Errorf("GetInfo call failed: %v", err)
776-
}
777-
778799
ruleEnforcer := firewall.NewRuleEnforcer(
779800
g.firewallDB, g.firewallDB,
780801
g.autopilotClient.ListFeaturePerms,
781-
g.permsMgr, info.IdentityPubkey,
802+
g.permsMgr, g.lndClient.NodePubkey,
782803
g.lndClient.Router,
783804
g.lndClient.Client, g.ruleMgrs,
784805
func(reqID uint64, reason string) error {

0 commit comments

Comments
 (0)