Skip to content

Commit 71cdc5a

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 23fbb10 commit 71cdc5a

File tree

3 files changed

+109
-40
lines changed

3 files changed

+109
-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

+22-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,15 @@ 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+
close func() error
246+
}
247+
239248
// Run starts everything and then blocks until either the application is shut
240249
// down or a critical error happens.
241250
func (g *LightningTerminal) Run(ctx context.Context) error {
@@ -421,14 +430,13 @@ func (g *LightningTerminal) start(ctx context.Context) error {
421430
return fmt.Errorf("could not create network directory: %v", err)
422431
}
423432

424-
clock := clock.NewDefaultClock()
425-
g.accountsStore, err = NewAccountStore(g.cfg, clock)
433+
g.stores, err = NewStores(g.cfg, clock.NewDefaultClock())
426434
if err != nil {
427-
return fmt.Errorf("error creating accounts store: %w", err)
435+
return fmt.Errorf("could not create stores: %v", err)
428436
}
429437

430438
g.accountService, err = accounts.NewService(
431-
g.accountsStore, accountServiceErrCallback,
439+
g.stores.accounts, accountServiceErrCallback,
432440
)
433441
if err != nil {
434442
return fmt.Errorf("error creating account service: %v", err)
@@ -448,16 +456,8 @@ func (g *LightningTerminal) start(ctx context.Context) error {
448456

449457
g.ruleMgrs = rules.NewRuleManagerSet()
450458

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-
459459
g.firewallDB, err = firewalldb.NewDB(
460-
networkDir, firewalldb.DBFilename, g.sessionDB,
460+
networkDir, firewalldb.DBFilename, g.stores.sessions,
461461
)
462462
if err != nil {
463463
return fmt.Errorf("error creating firewall DB: %v", err)
@@ -493,7 +493,7 @@ func (g *LightningTerminal) start(ctx context.Context) error {
493493
}
494494

495495
g.sessionRpcServer, err = newSessionRPCServer(&sessionRpcServerConfig{
496-
db: g.sessionDB,
496+
db: g.stores.sessions,
497497
basicAuth: g.rpcProxy.basicAuth,
498498
grpcOptions: []grpc.ServerOption{
499499
grpc.CustomCodec(grpcProxy.Codec()), // nolint: staticcheck,
@@ -1086,7 +1086,7 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context,
10861086

10871087
privacyMapper := firewall.NewPrivacyMapper(
10881088
g.firewallDB.PrivacyDB, firewall.CryptoRandIntn,
1089-
g.sessionDB,
1089+
g.stores.sessions,
10901090
)
10911091

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

10981098
if !g.cfg.Autopilot.Disable {
10991099
ruleEnforcer := firewall.NewRuleEnforcer(
1100-
g.firewallDB, g.firewallDB, g.sessionDB,
1100+
g.firewallDB, g.firewallDB, g.stores.sessions,
11011101
g.autopilotClient.ListFeaturePerms,
11021102
g.permsMgr, g.lndClient.NodePubkey,
11031103
g.lndClient.Router,
@@ -1436,14 +1436,6 @@ func (g *LightningTerminal) shutdownSubServers() error {
14361436
}
14371437
}
14381438

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-
14471439
if g.middlewareStarted {
14481440
g.middleware.Stop()
14491441
}
@@ -1462,9 +1454,10 @@ func (g *LightningTerminal) shutdownSubServers() error {
14621454
}
14631455
}
14641456

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

0 commit comments

Comments
 (0)