Skip to content

Commit 49aa365

Browse files
committed
terminal: setup LND prior to stores
In the upcoming actions migration, we need fetch LNDs macaroons prior to initializing the stores, as the macaroons will be required during the migration. This requires that we setup the connection to LND before we initialize the stores, as we can only fetch the macaroons after the LND connection is established. This commit refactors the litd startup process, so that the stores are initialized after the LND connection is established. This also requires that we refactor the RPC server setup, as litd's RPC servers require the stores to be initialized prior to being created.
1 parent ef87c7a commit 49aa365

File tree

1 file changed

+120
-78
lines changed

1 file changed

+120
-78
lines changed

terminal.go

Lines changed: 120 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -447,36 +447,6 @@ func (g *LightningTerminal) start(ctx context.Context) error {
447447
return fmt.Errorf("could not create network directory: %v", err)
448448
}
449449

450-
g.stores, err = NewStores(g.cfg, clock.NewDefaultClock())
451-
if err != nil {
452-
return fmt.Errorf("could not create stores: %v", err)
453-
}
454-
455-
if err := g.stores.firewall.Start(ctx); err != nil {
456-
return fmt.Errorf("could not start firewall DB: %v", err)
457-
}
458-
459-
g.accountService, err = accounts.NewService(
460-
g.stores.accounts, accountServiceErrCallback,
461-
)
462-
if err != nil {
463-
return fmt.Errorf("error creating account service: %v", err)
464-
}
465-
466-
superMacBaker := func(ctx context.Context, rootKeyID uint64,
467-
perms []bakery.Op, caveats []macaroon.Caveat) (string, error) {
468-
469-
return litmac.BakeSuperMacaroon(
470-
ctx, g.basicClient, rootKeyID, perms, caveats,
471-
)
472-
}
473-
474-
g.accountRpcServer = accounts.NewRPCServer(
475-
g.accountService, superMacBaker,
476-
)
477-
478-
g.ruleMgrs = rules.NewRuleManagerSet()
479-
480450
if !g.cfg.Autopilot.Disable {
481451
if g.cfg.Autopilot.Address == "" &&
482452
len(g.cfg.Autopilot.DialOpts) == 0 {
@@ -506,40 +476,6 @@ func (g *LightningTerminal) start(ctx context.Context) error {
506476
}
507477
}
508478

509-
g.sessionRpcServer, err = newSessionRPCServer(&sessionRpcServerConfig{
510-
db: g.stores.sessions,
511-
basicAuth: g.rpcProxy.basicAuth,
512-
grpcOptions: []grpc.ServerOption{
513-
grpc.CustomCodec(grpcProxy.Codec()), // nolint: staticcheck,
514-
grpc.ChainStreamInterceptor(
515-
g.rpcProxy.StreamServerInterceptor,
516-
),
517-
grpc.ChainUnaryInterceptor(
518-
g.rpcProxy.UnaryServerInterceptor,
519-
),
520-
grpc.UnknownServiceHandler(
521-
grpcProxy.TransparentHandler(
522-
// Don't allow calls to litrpc.
523-
g.rpcProxy.makeDirector(false),
524-
),
525-
),
526-
},
527-
registerGrpcServers: func(server *grpc.Server) {
528-
g.registerSubDaemonGrpcServers(server, true)
529-
},
530-
superMacBaker: superMacBaker,
531-
firstConnectionDeadline: g.cfg.FirstLNCConnDeadline,
532-
permMgr: g.permsMgr,
533-
actionsDB: g.stores.firewall,
534-
autopilot: g.autopilotClient,
535-
ruleMgrs: g.ruleMgrs,
536-
privMap: g.stores.firewall,
537-
})
538-
if err != nil {
539-
return fmt.Errorf("could not create new session rpc "+
540-
"server: %v", err)
541-
}
542-
543479
// Call the "real" main in a nested manner so the defers will properly
544480
// be executed in the case of a graceful shutdown.
545481
var (
@@ -769,6 +705,77 @@ func (g *LightningTerminal) start(ctx context.Context) error {
769705
return fmt.Errorf("could not start LND")
770706
}
771707

708+
g.stores, err = NewStores(g.cfg, clock.NewDefaultClock())
709+
if err != nil {
710+
return fmt.Errorf("could not create stores: %v", err)
711+
}
712+
713+
if err := g.stores.firewall.Start(ctx); err != nil {
714+
return fmt.Errorf("could not start firewall DB: %v", err)
715+
}
716+
717+
g.accountService, err = accounts.NewService(
718+
g.stores.accounts, accountServiceErrCallback,
719+
)
720+
if err != nil {
721+
return fmt.Errorf("error creating account service: %v", err)
722+
}
723+
724+
superMacBaker := func(ctx context.Context, rootKeyID uint64,
725+
perms []bakery.Op, caveats []macaroon.Caveat) (string, error) {
726+
727+
return litmac.BakeSuperMacaroon(
728+
ctx, g.basicClient, rootKeyID, perms, caveats,
729+
)
730+
}
731+
732+
g.accountRpcServer = accounts.NewRPCServer(
733+
g.accountService, superMacBaker,
734+
)
735+
736+
g.sessionRpcServer, err = newSessionRPCServer(&sessionRpcServerConfig{
737+
db: g.stores.sessions,
738+
basicAuth: g.rpcProxy.basicAuth,
739+
grpcOptions: []grpc.ServerOption{
740+
grpc.CustomCodec(grpcProxy.Codec()), // nolint: staticcheck,
741+
grpc.ChainStreamInterceptor(
742+
g.rpcProxy.StreamServerInterceptor,
743+
),
744+
grpc.ChainUnaryInterceptor(
745+
g.rpcProxy.UnaryServerInterceptor,
746+
),
747+
grpc.UnknownServiceHandler(
748+
grpcProxy.TransparentHandler(
749+
// Don't allow calls to litrpc.
750+
g.rpcProxy.makeDirector(false),
751+
),
752+
),
753+
},
754+
registerGrpcServers: func(server *grpc.Server) {
755+
g.registerSubDaemonGrpcServers(server, true)
756+
},
757+
superMacBaker: superMacBaker,
758+
firstConnectionDeadline: g.cfg.FirstLNCConnDeadline,
759+
permMgr: g.permsMgr,
760+
actionsDB: g.stores.firewall,
761+
autopilot: g.autopilotClient,
762+
ruleMgrs: g.ruleMgrs,
763+
privMap: g.stores.firewall,
764+
})
765+
if err != nil {
766+
return fmt.Errorf("could not create new session rpc "+
767+
"server: %v", err)
768+
}
769+
770+
g.ruleMgrs = rules.NewRuleManagerSet()
771+
772+
// Once the accounts and sessions RPC servers have been created, we
773+
// can register them with the RPC proxy gRPC server.
774+
err = g.registerSubDaemonGrpcServersToProxy()
775+
if err != nil {
776+
return fmt.Errorf("could not register RPC servers: %v", err)
777+
}
778+
772779
// Mark that lnd is now completely running after connecting the
773780
// lnd clients.
774781
g.statusMgr.SetRunning(subservers.LND)
@@ -1167,34 +1174,69 @@ func (g *LightningTerminal) RegisterGrpcSubserver(server *grpc.Server) error {
11671174

11681175
// registerSubDaemonGrpcServers registers the sub daemon (Faraday, Loop, Pool
11691176
// and LiT session) servers to a given gRPC server, given they are running in
1170-
// the local process. Some of LiT's own sub-servers should be registered with
1171-
// LNC sessions and some should not - the forLNCSession boolean can be used to
1172-
// control this.
1177+
// the local process.
1178+
// For LNC sessions, this functions registers some of LiT's own sub-servers with
1179+
// the session. This is toggled by the forLNCSession boolean.
11731180
func (g *LightningTerminal) registerSubDaemonGrpcServers(server *grpc.Server,
11741181
forLNCSession bool) {
11751182

11761183
g.subServerMgr.RegisterRPCServices(server)
11771184

1178-
if forLNCSession {
1179-
litrpc.RegisterStatusServer(server, g.statusMgr)
1180-
litrpc.RegisterProxyServer(server, g.rpcProxy)
1181-
} else {
1182-
litrpc.RegisterSessionsServer(server, g.sessionRpcServer)
1183-
1184-
if !g.cfg.Accounts.Disable {
1185-
litrpc.RegisterAccountsServer(
1186-
server, g.accountRpcServer,
1187-
)
1188-
}
1185+
// For non-LNC sessions, we will proceed to register the other
1186+
// necessary sub-daemon servers manually with the
1187+
// registerSubDaemonGrpcServersToProxy function after they have been
1188+
// created. They will be registered after we've connected to LND, hence
1189+
// after this function has been called by LND.
1190+
if !forLNCSession {
1191+
return
11891192
}
11901193

1194+
litrpc.RegisterStatusServer(server, g.statusMgr)
1195+
litrpc.RegisterProxyServer(server, g.rpcProxy)
11911196
litrpc.RegisterFirewallServer(server, g.sessionRpcServer)
11921197

11931198
if !g.cfg.Autopilot.Disable {
11941199
litrpc.RegisterAutopilotServer(server, g.sessionRpcServer)
11951200
}
11961201
}
11971202

1203+
// registerSubDaemonGrpcServersToProxy registers the session and accounts
1204+
// gRPC servers to the RPC proxy gRPC Server.
1205+
//
1206+
// NOTE: This function can only be called once the accounts and session
1207+
// RPC servers have been created.
1208+
func (g *LightningTerminal) registerSubDaemonGrpcServersToProxy() error {
1209+
if g.rpcProxy == nil || g.rpcProxy.grpcServer == nil {
1210+
return errors.New("RPC proxy gRPC server is not initialized")
1211+
}
1212+
1213+
if g.sessionRpcServer == nil {
1214+
return errors.New("session RPC server is not initialized")
1215+
}
1216+
1217+
if !g.cfg.Accounts.Disable && g.accountRpcServer == nil {
1218+
return errors.New("account RPC server is not initialized")
1219+
}
1220+
1221+
litrpc.RegisterSessionsServer(g.rpcProxy.grpcServer, g.sessionRpcServer)
1222+
1223+
litrpc.RegisterFirewallServer(g.rpcProxy.grpcServer, g.sessionRpcServer)
1224+
1225+
if !g.cfg.Autopilot.Disable {
1226+
litrpc.RegisterAutopilotServer(
1227+
g.rpcProxy.grpcServer, g.sessionRpcServer,
1228+
)
1229+
}
1230+
1231+
if !g.cfg.Accounts.Disable {
1232+
litrpc.RegisterAccountsServer(
1233+
g.rpcProxy.grpcServer, g.accountRpcServer,
1234+
)
1235+
}
1236+
1237+
return nil
1238+
}
1239+
11981240
// RegisterRestSubserver is a callback on the lnd.SubserverConfig struct that is
11991241
// called once lnd has initialized its main REST server instance. It gives the
12001242
// daemons (or external subservers) the possibility to register themselves to

0 commit comments

Comments
 (0)