Skip to content

Commit 6e96c61

Browse files
multi: separate account RPC server init & start
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. In preparation of doing so, we cannot reference the stores when initializing/creating the accounts RPC server object, as we do so prior to setting up the LND connection. This commit therefore refactors the accounts RPC server so that we separate the initializing from the starting of the RPC server, and only require the store reference during the actual startup of the RPC server. Note that we still keep the init of the accounts RPC server reference prior to setting up the LND connection, as not doing so would require that we'd refactor the registering of `GrpcSubserver`s in a more complex and in a less elegant way.
1 parent a79187f commit 6e96c61

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

accounts/rpcserver.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package accounts
33
import (
44
"context"
55
"encoding/hex"
6+
"errors"
67
"fmt"
78
"time"
89

@@ -16,6 +17,13 @@ import (
1617
"gopkg.in/macaroon.v2"
1718
)
1819

20+
var (
21+
// ErrServerNotActive indicates that the server has started but hasn't
22+
// fully finished the startup process.
23+
ErrServerNotActive = errors.New("accounts server is still in the " +
24+
"process of starting")
25+
)
26+
1927
// RPCServer is the main server that implements the Accounts gRPC service.
2028
type RPCServer struct {
2129
litrpc.UnimplementedAccountsServer
@@ -26,13 +34,17 @@ type RPCServer struct {
2634
}
2735

2836
// NewRPCServer returns a new RPC server for the given service.
29-
func NewRPCServer(service *InterceptorService,
30-
superMacBaker litmac.Baker) *RPCServer {
37+
func NewRPCServer() *RPCServer {
38+
return &RPCServer{}
39+
}
3140

32-
return &RPCServer{
33-
service: service,
34-
superMacBaker: superMacBaker,
35-
}
41+
// Start adds the necessary dependencies for the RPCServer to be able to process
42+
// requests, and starts the RPCServer.
43+
func (s *RPCServer) Start(service *InterceptorService,
44+
superMacBaker litmac.Baker) {
45+
46+
s.service = service
47+
s.superMacBaker = superMacBaker
3648
}
3749

3850
// CreateAccount adds an entry to the account database. This entry represents

terminal.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,15 @@ func (g *LightningTerminal) start(ctx context.Context) error {
471471
)
472472
}
473473

474-
g.accountRpcServer = accounts.NewRPCServer(
475-
g.accountService, superMacBaker,
476-
)
474+
// We create a reference to the `accountRpcServer` here before starting
475+
// it and prior to setting up the LND connection. This is because when
476+
// the LND connection is set up for an integrated LND instance, LND will
477+
// call litd's `RegisterGrpcSubserver` function during the setup of the
478+
// connection.
479+
// That function calls `registerSubDaemonGrpcServers` which requires
480+
// that the `accountRpcServer` pointer exist, to not nil pointer panic
481+
// when requests get passed to the server.
482+
g.accountRpcServer = accounts.NewRPCServer()
477483

478484
g.ruleMgrs = rules.NewRuleManagerSet()
479485

@@ -1028,6 +1034,21 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context,
10281034
}
10291035
g.macaroonServiceStarted = true
10301036

1037+
superMacBaker := func(ctx context.Context, rootKeyID uint64,
1038+
perms []bakery.Op, caveats []macaroon.Caveat) (string, error) {
1039+
1040+
return litmac.BakeSuperMacaroon(
1041+
ctx, g.basicClient, rootKeyID, perms, caveats,
1042+
)
1043+
}
1044+
1045+
log.Infof("Starting LiT accounts server")
1046+
1047+
err = g.accountRpcServer.Start(g.accountService, superMacBaker)
1048+
if err != nil {
1049+
return err
1050+
}
1051+
10311052
if !g.cfg.Autopilot.Disable {
10321053
withLndVersion := func(cfg *autopilotserver.Config) {
10331054
cfg.LndVersion = autopilotserver.Version{

0 commit comments

Comments
 (0)