Skip to content

Commit d3dc753

Browse files
committed
rpc_proxy: give the proxy access to basicLNDClient
1 parent 2fc07ff commit d3dc753

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

rpc_proxy.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
litstatus "github.com/lightninglabs/lightning-terminal/status"
1919
"github.com/lightninglabs/lightning-terminal/subservers"
2020
"github.com/lightningnetwork/lnd/lncfg"
21+
"github.com/lightningnetwork/lnd/lnrpc"
2122
"github.com/lightningnetwork/lnd/macaroons"
2223
grpcProxy "github.com/mwitkow/grpc-proxy/proxy"
2324
"google.golang.org/grpc"
@@ -69,7 +70,7 @@ func (e *proxyErr) Unwrap() error {
6970
func newRpcProxy(cfg *Config, validator macaroons.MacaroonValidator,
7071
superMacValidator session.SuperMacaroonValidator,
7172
permsMgr *perms.Manager, subServerMgr *subservers.Manager,
72-
statusMgr *litstatus.Manager) *rpcProxy {
73+
statusMgr *litstatus.Manager, getLNDClient lndBasicClientFn) *rpcProxy {
7374

7475
// The gRPC web calls are protected by HTTP basic auth which is defined
7576
// by base64(username:password). Because we only have a password, we
@@ -91,6 +92,7 @@ func newRpcProxy(cfg *Config, validator macaroons.MacaroonValidator,
9192
superMacValidator: superMacValidator,
9293
subServerMgr: subServerMgr,
9394
statusMgr: statusMgr,
95+
getBasicLNDClient: getLNDClient,
9496
}
9597
p.grpcServer = grpc.NewServer(
9698
// From the grpxProxy doc: This codec is *crucial* to the
@@ -161,11 +163,12 @@ type rpcProxy struct {
161163
// must only ever be used atomically.
162164
started int32
163165

164-
cfg *Config
165-
basicAuth string
166-
permsMgr *perms.Manager
167-
subServerMgr *subservers.Manager
168-
statusMgr *litstatus.Manager
166+
cfg *Config
167+
basicAuth string
168+
permsMgr *perms.Manager
169+
subServerMgr *subservers.Manager
170+
statusMgr *litstatus.Manager
171+
getBasicLNDClient lndBasicClientFn
169172

170173
bakeSuperMac bakeSuperMac
171174

@@ -183,6 +186,10 @@ type rpcProxy struct {
183186
// bakeSuperMac can be used to bake a new super macaroon.
184187
type bakeSuperMac func(ctx context.Context, rootKeyID uint32) (string, error)
185188

189+
// lndBasicClientFn can be used to obtain access to an lnrpc.LightningClient if
190+
// it is available.
191+
type lndBasicClientFn func() (lnrpc.LightningClient, error)
192+
186193
// Start creates initial connection to lnd.
187194
func (p *rpcProxy) Start(lndConn *grpc.ClientConn,
188195
bakeSuperMac bakeSuperMac) error {

terminal.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"regexp"
1717
"strings"
1818
"sync"
19+
"sync/atomic"
1920
"time"
2021

2122
restProxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
@@ -174,10 +175,14 @@ type LightningTerminal struct {
174175
wg sync.WaitGroup
175176
errQueue *queue.ConcurrentQueue[error]
176177

177-
lndConnID string
178-
lndConn *grpc.ClientConn
179-
lndClient *lndclient.GrpcLndServices
180-
basicClient lnrpc.LightningClient
178+
lndConnID string
179+
lndConn *grpc.ClientConn
180+
lndClient *lndclient.GrpcLndServices
181+
182+
// basicClient may be accessed by other sub-systems but this access
183+
// should be provided via the basicLNDClient method.
184+
basicClient lnrpc.LightningClient
185+
basicClientSet atomic.Bool
181186

182187
subServerMgr *subservers.Manager
183188
statusMgr *status.Manager
@@ -297,7 +302,7 @@ func (g *LightningTerminal) Run() error {
297302
// server is started.
298303
g.rpcProxy = newRpcProxy(
299304
g.cfg, g, g.validateSuperMacaroon, g.permsMgr, g.subServerMgr,
300-
g.statusMgr,
305+
g.statusMgr, g.basicLNDClient,
301306
)
302307

303308
// Register any gRPC services that should be served using LiT's
@@ -728,6 +733,15 @@ func (g *LightningTerminal) start() error {
728733
return nil
729734
}
730735

736+
// basicLNDClient provides access to LiT's basicClient if it has been set.
737+
func (g *LightningTerminal) basicLNDClient() (lnrpc.LightningClient, error) {
738+
if !g.basicClientSet.Load() {
739+
return nil, fmt.Errorf("basic LND client has not yet been set")
740+
}
741+
742+
return g.basicClient, nil
743+
}
744+
731745
// setUpLNDClients sets up the various LND clients required by LiT.
732746
func (g *LightningTerminal) setUpLNDClients(lndQuit chan struct{}) error {
733747
var (
@@ -805,6 +819,7 @@ func (g *LightningTerminal) setUpLNDClients(lndQuit chan struct{}) error {
805819

806820
log.Infof("Retrying to connect basic lnd client")
807821
}
822+
g.basicClientSet.Store(true)
808823

809824
// Now we know that the connection itself is ready. But we also need to
810825
// wait for two things: The chain notifier to be ready and the lnd

0 commit comments

Comments
 (0)