Skip to content

Commit 6dbf880

Browse files
committed
multi: plug in sessions SQL DB
Let LiTd support a SQL db for the sessions store under the `dev` build tag. This can be used via itests.
1 parent 60da67d commit 6dbf880

File tree

3 files changed

+110
-40
lines changed

3 files changed

+110
-40
lines changed

config_dev.go

+50-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/lightninglabs/lightning-terminal/accounts"
99
"github.com/lightninglabs/lightning-terminal/db"
10+
"github.com/lightninglabs/lightning-terminal/session"
1011
"github.com/lightningnetwork/lnd/clock"
1112
)
1213

@@ -80,14 +81,19 @@ func defaultDevConfig() *DevConfig {
8081
}
8182
}
8283

83-
// NewAccountStore creates a new account store based on the chosen database
84-
// backend.
85-
func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) {
84+
// NewStores creates a new stores instance based on the chosen database backend.
85+
func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
86+
var (
87+
networkDir = filepath.Join(cfg.LitDir, cfg.Network)
88+
acctStore accounts.Store
89+
sessStore session.Store
90+
closeFn func() error
91+
)
92+
8693
switch cfg.DatabaseBackend {
8794
case DatabaseBackendSqlite:
8895
// Before we initialize the SQLite store, we'll make sure that
8996
// the directory where we will store the database file exists.
90-
networkDir := filepath.Join(cfg.LitDir, cfg.Network)
9197
err := makeDirectories(networkDir)
9298
if err != nil {
9399
return nil, err
@@ -98,20 +104,57 @@ func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) {
98104
return nil, err
99105
}
100106

101-
return accounts.NewSQLStore(sqlStore.BaseDB, clock), nil
107+
acctStore = accounts.NewSQLStore(sqlStore.BaseDB, clock)
108+
sessStore = session.NewSQLStore(sqlStore.BaseDB, clock)
109+
closeFn = sqlStore.BaseDB.Close
102110

103111
case DatabaseBackendPostgres:
104112
sqlStore, err := db.NewPostgresStore(cfg.Postgres)
105113
if err != nil {
106114
return nil, err
107115
}
108116

109-
return accounts.NewSQLStore(sqlStore.BaseDB, clock), nil
117+
acctStore = accounts.NewSQLStore(sqlStore.BaseDB, clock)
118+
sessStore = session.NewSQLStore(sqlStore.BaseDB, clock)
119+
closeFn = sqlStore.BaseDB.Close
110120

111121
default:
112-
return accounts.NewBoltStore(
122+
accountStore, err := accounts.NewBoltStore(
113123
filepath.Dir(cfg.MacaroonPath), accounts.DBFilename,
114124
clock,
115125
)
126+
if err != nil {
127+
return nil, err
128+
}
129+
130+
sessionStore, err := session.NewDB(
131+
networkDir, session.DBFilename, clock, accountStore,
132+
)
133+
if err != nil {
134+
return nil, err
135+
}
136+
137+
acctStore = accountStore
138+
sessStore = sessionStore
139+
closeFn = func() error {
140+
var returnErr error
141+
err = accountStore.Close()
142+
if err != nil {
143+
returnErr = err
144+
}
145+
146+
err = sessionStore.Close()
147+
if err != nil {
148+
returnErr = err
149+
}
150+
151+
return returnErr
152+
}
116153
}
154+
155+
return &stores{
156+
accounts: acctStore,
157+
sessions: sessStore,
158+
close: closeFn,
159+
}, nil
117160
}

config_prod.go

+37-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
package terminal
44

55
import (
6+
"fmt"
67
"path/filepath"
78

89
"github.com/lightninglabs/lightning-terminal/accounts"
10+
"github.com/lightninglabs/lightning-terminal/session"
911
"github.com/lightningnetwork/lnd/clock"
1012
)
1113

@@ -24,10 +26,41 @@ func (c *DevConfig) Validate(_, _ string) error {
2426
return nil
2527
}
2628

27-
// NewAccountStore creates a new account store using the default Bolt backend
28-
// since in production, this is the only backend supported currently.
29-
func NewAccountStore(cfg *Config, clock clock.Clock) (accounts.Store, error) {
30-
return accounts.NewBoltStore(
29+
// NewStores creates a new instance of the stores struct using the default Bolt
30+
// backend since in production, this is currently the only backend supported.
31+
func NewStores(cfg *Config, clock clock.Clock) (*stores, error) {
32+
networkDir := filepath.Join(cfg.LitDir, cfg.Network)
33+
34+
acctStore, err := accounts.NewBoltStore(
3135
filepath.Dir(cfg.MacaroonPath), accounts.DBFilename, clock,
3236
)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
sessStore, err := session.NewDB(
42+
networkDir, session.DBFilename, clock, acctStore,
43+
)
44+
if err != nil {
45+
return nil, fmt.Errorf("error creating session BoltStore: %v",
46+
err)
47+
}
48+
49+
return &stores{
50+
accounts: acctStore,
51+
sessions: sessStore,
52+
close: func() error {
53+
var returnErr error
54+
if err := acctStore.Close(); err != nil {
55+
returnErr = fmt.Errorf("error closing "+
56+
"account store: %v", err)
57+
}
58+
if err := sessStore.Close(); err != nil {
59+
returnErr = fmt.Errorf("error closing "+
60+
"session store: %v", err)
61+
}
62+
63+
return returnErr
64+
},
65+
}, nil
3366
}

terminal.go

+23-29
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ type LightningTerminal struct {
216216
middleware *mid.Manager
217217
middlewareStarted bool
218218

219-
accountsStore accounts.Store
220219
accountService *accounts.InterceptorService
221220
accountServiceStarted bool
222221

223222
accountRpcServer *accounts.RPCServer
224223

224+
stores *stores
225+
225226
firewallDB *firewalldb.DB
226-
sessionDB *session.BoltStore
227227

228228
restHandler http.Handler
229229
restCancel func()
@@ -236,6 +236,16 @@ func New() *LightningTerminal {
236236
}
237237
}
238238

239+
// stores holds a collection of the DB stores that are used by LiT.
240+
type stores struct {
241+
accounts accounts.Store
242+
sessions session.Store
243+
244+
// close is a callback that can be used to close all the stores in the
245+
// stores struct.
246+
close func() error
247+
}
248+
239249
// Run starts everything and then blocks until either the application is shut
240250
// down or a critical error happens.
241251
func (g *LightningTerminal) Run(ctx context.Context) error {
@@ -421,14 +431,13 @@ func (g *LightningTerminal) start(ctx context.Context) error {
421431
return fmt.Errorf("could not create network directory: %v", err)
422432
}
423433

424-
clock := clock.NewDefaultClock()
425-
g.accountsStore, err = NewAccountStore(g.cfg, clock)
434+
g.stores, err = NewStores(g.cfg, clock.NewDefaultClock())
426435
if err != nil {
427-
return fmt.Errorf("error creating accounts store: %w", err)
436+
return fmt.Errorf("could not create stores: %v", err)
428437
}
429438

430439
g.accountService, err = accounts.NewService(
431-
g.accountsStore, accountServiceErrCallback,
440+
g.stores.accounts, accountServiceErrCallback,
432441
)
433442
if err != nil {
434443
return fmt.Errorf("error creating account service: %v", err)
@@ -448,16 +457,8 @@ func (g *LightningTerminal) start(ctx context.Context) error {
448457

449458
g.ruleMgrs = rules.NewRuleManagerSet()
450459

451-
// Create an instance of the local Terminal Connect session store DB.
452-
g.sessionDB, err = session.NewDB(
453-
networkDir, session.DBFilename, clock, g.accountsStore,
454-
)
455-
if err != nil {
456-
return fmt.Errorf("error creating session DB: %v", err)
457-
}
458-
459460
g.firewallDB, err = firewalldb.NewDB(
460-
networkDir, firewalldb.DBFilename, g.sessionDB,
461+
networkDir, firewalldb.DBFilename, g.stores.sessions,
461462
)
462463
if err != nil {
463464
return fmt.Errorf("error creating firewall DB: %v", err)
@@ -493,7 +494,7 @@ func (g *LightningTerminal) start(ctx context.Context) error {
493494
}
494495

495496
g.sessionRpcServer, err = newSessionRPCServer(&sessionRpcServerConfig{
496-
db: g.sessionDB,
497+
db: g.stores.sessions,
497498
basicAuth: g.rpcProxy.basicAuth,
498499
grpcOptions: []grpc.ServerOption{
499500
grpc.CustomCodec(grpcProxy.Codec()), // nolint: staticcheck,
@@ -1086,7 +1087,7 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context,
10861087

10871088
privacyMapper := firewall.NewPrivacyMapper(
10881089
g.firewallDB.PrivacyDB, firewall.CryptoRandIntn,
1089-
g.sessionDB,
1090+
g.stores.sessions,
10901091
)
10911092

10921093
mw := []mid.RequestInterceptor{
@@ -1097,7 +1098,7 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context,
10971098

10981099
if !g.cfg.Autopilot.Disable {
10991100
ruleEnforcer := firewall.NewRuleEnforcer(
1100-
g.firewallDB, g.firewallDB, g.sessionDB,
1101+
g.firewallDB, g.firewallDB, g.stores.sessions,
11011102
g.autopilotClient.ListFeaturePerms,
11021103
g.permsMgr, g.lndClient.NodePubkey,
11031104
g.lndClient.Router,
@@ -1436,14 +1437,6 @@ func (g *LightningTerminal) shutdownSubServers() error {
14361437
}
14371438
}
14381439

1439-
if g.accountsStore != nil {
1440-
err = g.accountsStore.Close()
1441-
if err != nil {
1442-
log.Errorf("Error closing accounts store: %v", err)
1443-
returnErr = err
1444-
}
1445-
}
1446-
14471440
if g.middlewareStarted {
14481441
g.middleware.Stop()
14491442
}
@@ -1462,9 +1455,10 @@ func (g *LightningTerminal) shutdownSubServers() error {
14621455
}
14631456
}
14641457

1465-
if g.sessionDB != nil {
1466-
if err := g.sessionDB.Close(); err != nil {
1467-
log.Errorf("Error closing session DB: %v", err)
1458+
if g.stores != nil {
1459+
err = g.stores.close()
1460+
if err != nil {
1461+
log.Errorf("Error closing stores: %v", err)
14681462
returnErr = err
14691463
}
14701464
}

0 commit comments

Comments
 (0)