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.
This commit is contained in:
Viktor Tigerström 2025-08-06 14:11:55 +02:00
parent a79187f84e
commit d52b4565f2
No known key found for this signature in database
GPG key ID: B984570980684DCC
2 changed files with 39 additions and 9 deletions

View file

@ -3,6 +3,7 @@ package accounts
import (
"context"
"encoding/hex"
"errors"
"fmt"
"time"
@ -16,6 +17,13 @@ import (
"gopkg.in/macaroon.v2"
)
var (
// ErrServerNotActive indicates that the server has started but hasn't
// fully finished the startup process.
ErrServerNotActive = errors.New("accounts server is still in the " +
"process of starting")
)
// RPCServer is the main server that implements the Accounts gRPC service.
type RPCServer struct {
litrpc.UnimplementedAccountsServer
@ -26,13 +34,17 @@ type RPCServer struct {
}
// NewRPCServer returns a new RPC server for the given service.
func NewRPCServer(service *InterceptorService,
superMacBaker litmac.Baker) *RPCServer {
func NewRPCServer() *RPCServer {
return &RPCServer{}
}
return &RPCServer{
service: service,
superMacBaker: superMacBaker,
}
// Start adds the necessary dependencies for the RPCServer to be able to process
// requests, and starts the RPCServer.
func (s *RPCServer) Start(service *InterceptorService,
superMacBaker litmac.Baker) {
s.service = service
s.superMacBaker = superMacBaker
}
// CreateAccount adds an entry to the account database. This entry represents

View file

@ -471,9 +471,15 @@ func (g *LightningTerminal) start(ctx context.Context) error {
)
}
g.accountRpcServer = accounts.NewRPCServer(
g.accountService, superMacBaker,
)
// We create a reference to the `accountRpcServer` here before starting
// it and prior to setting up the LND connection. This is because when
// the LND connection is set up for an integrated LND instance, LND will
// call litd's `RegisterGrpcSubserver` function during the setup of the
// connection.
// That function calls `registerSubDaemonGrpcServers` which requires
// that the `accountRpcServer` pointer exist, to not nil pointer panic
// when requests get passed to the server.
g.accountRpcServer = accounts.NewRPCServer()
g.ruleMgrs = rules.NewRuleManagerSet()
@ -1028,6 +1034,18 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context,
}
g.macaroonServiceStarted = true
superMacBaker := func(ctx context.Context, rootKeyID uint64,
perms []bakery.Op, caveats []macaroon.Caveat) (string, error) {
return litmac.BakeSuperMacaroon(
ctx, g.basicClient, rootKeyID, perms, caveats,
)
}
log.Infof("Starting LiT accounts server")
g.accountRpcServer.Start(g.accountService, superMacBaker)
if !g.cfg.Autopilot.Disable {
withLndVersion := func(cfg *autopilotserver.Config) {
cfg.LndVersion = autopilotserver.Version{