Skip to content

Commit 270bc31

Browse files
committed
multi: add sql stores to loopd
This commit adds the ability to choose between different sql stores for loopd. The default is sqlite, but it can be changed to postgres.
1 parent a2abcd0 commit 270bc31

File tree

3 files changed

+84
-22
lines changed

3 files changed

+84
-22
lines changed

client.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,9 @@ type ClientConfig struct {
121121
}
122122

123123
// NewClient returns a new instance to initiate swaps with.
124-
func NewClient(dbDir string, cfg *ClientConfig) (*Client, func(), error) {
125-
store, err := loopdb.NewBoltSwapStore(dbDir, cfg.Lnd.ChainParams)
126-
if err != nil {
127-
return nil, nil, err
128-
}
124+
func NewClient(dbDir string, loopDB loopdb.SwapStore,
125+
cfg *ClientConfig) (*Client, func(), error) {
126+
129127
lsatStore, err := lsat.NewFileStore(dbDir)
130128
if err != nil {
131129
return nil, nil, err
@@ -139,7 +137,7 @@ func NewClient(dbDir string, cfg *ClientConfig) (*Client, func(), error) {
139137
config := &clientConfig{
140138
LndServices: cfg.Lnd,
141139
Server: swapServerClient,
142-
Store: store,
140+
Store: loopDB,
143141
LsatStore: lsatStore,
144142
CreateExpiryTimer: func(d time.Duration) <-chan time.Time {
145143
return time.NewTimer(d).C
@@ -153,7 +151,7 @@ func NewClient(dbDir string, cfg *ClientConfig) (*Client, func(), error) {
153151

154152
executor := newExecutor(&executorConfig{
155153
lnd: cfg.Lnd,
156-
store: store,
154+
store: loopDB,
157155
sweeper: sweeper,
158156
createExpiryTimer: config.CreateExpiryTimer,
159157
loopOutMaxParts: cfg.LoopOutMaxParts,
@@ -185,7 +183,7 @@ func NewClient(dbDir string, cfg *ClientConfig) (*Client, func(), error) {
185183

186184
cleanup := func() {
187185
swapServerClient.stop()
188-
store.Close()
186+
loopDB.Close()
189187
}
190188

191189
return client, cleanup, nil

loopd/config.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/btcsuite/btcd/btcutil"
1313
"github.com/lightninglabs/aperture/lsat"
14+
"github.com/lightninglabs/loop/loopdb"
1415
"github.com/lightningnetwork/lnd/cert"
1516
"github.com/lightningnetwork/lnd/lncfg"
1617
"github.com/lightningnetwork/lnd/lnrpc"
@@ -28,11 +29,19 @@ var (
2829
defaultLogDirname = "logs"
2930
defaultLogFilename = "loopd.log"
3031

32+
defaultSqliteDatabaseFileName = "loop_sqlite.db"
33+
3134
defaultLogDir = filepath.Join(LoopDirBase, defaultLogDirname)
3235
defaultConfigFile = filepath.Join(
3336
LoopDirBase, DefaultNetwork, defaultConfigFilename,
3437
)
3538

39+
// defaultSqliteDatabasePath is the default path under which we store
40+
// the SQLite database file.
41+
defaultSqliteDatabasePath = filepath.Join(
42+
LoopDirBase, DefaultNetwork, defaultSqliteDatabaseFileName,
43+
)
44+
3645
defaultMaxLogFiles = 3
3746
defaultMaxLogFileSize = 10
3847
defaultLoopOutMaxParts = uint32(5)
@@ -47,6 +56,12 @@ var (
4756
// TLS key.
4857
DefaultTLSKeyFilename = "tls.key"
4958

59+
// DatabaseBackendSqlite is the name of the SQLite database backend.
60+
DatabaseBackendSqlite = "sqlite"
61+
62+
// DatabaseBackendPostgres is the name of the Postgres database backend.
63+
DatabaseBackendPostgres = "postgres"
64+
5065
defaultSelfSignedOrganization = "loop autogenerated cert"
5166

5267
// defaultLndMacaroon is the default macaroon file we use if the old,
@@ -127,6 +142,10 @@ type Config struct {
127142
ConfigFile string `long:"configfile" description:"Path to configuration file."`
128143
DataDir string `long:"datadir" description:"Directory for loopdb."`
129144

145+
DatabaseBackend string `long:"databasebackend" description:"The database backend to use for storing all asset related data." choice:"sqlite" choice:"postgres"`
146+
Sqlite *loopdb.SqliteConfig `group:"sqlite" namespace:"sqlite"`
147+
Postgres *loopdb.PostgresConfig `group:"postgres" namespace:"postgres"`
148+
130149
TLSCertPath string `long:"tlscertpath" description:"Path to write the TLS certificate for loop's RPC and REST services."`
131150
TLSKeyPath string `long:"tlskeypath" description:"Path to write the TLS private key for loop's RPC and REST services."`
132151
TLSExtraIPs []string `long:"tlsextraip" description:"Adds an extra IP to the generated certificate."`
@@ -172,9 +191,13 @@ func DefaultConfig() Config {
172191
Server: &loopServerConfig{
173192
NoTLS: false,
174193
},
175-
LoopDir: LoopDirBase,
176-
ConfigFile: defaultConfigFile,
177-
DataDir: LoopDirBase,
194+
LoopDir: LoopDirBase,
195+
ConfigFile: defaultConfigFile,
196+
DataDir: LoopDirBase,
197+
DatabaseBackend: DatabaseBackendSqlite,
198+
Sqlite: &loopdb.SqliteConfig{
199+
DatabaseFileName: defaultSqliteDatabasePath,
200+
},
178201
LogDir: defaultLogDir,
179202
MaxLogFiles: defaultMaxLogFiles,
180203
MaxLogFileSize: defaultMaxLogFileSize,
@@ -276,6 +299,14 @@ func Validate(cfg *Config) error {
276299
)
277300
}
278301

302+
// We'll also update the database file location as well, if it wasn't
303+
// set.
304+
if cfg.Sqlite.DatabaseFileName == defaultSqliteDatabasePath {
305+
cfg.Sqlite.DatabaseFileName = filepath.Join(
306+
cfg.DataDir, defaultSqliteDatabaseFileName,
307+
)
308+
}
309+
279310
// If either of these directories do not exist, create them.
280311
if err := os.MkdirAll(cfg.DataDir, os.ModePerm); err != nil {
281312
return err

loopd/utils.go

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,67 @@ package loopd
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/btcsuite/btcd/btcutil"
78
"github.com/lightninglabs/lndclient"
89
"github.com/lightninglabs/loop"
910
"github.com/lightninglabs/loop/liquidity"
11+
"github.com/lightninglabs/loop/loopdb"
1012
"github.com/lightninglabs/loop/swap"
1113
"github.com/lightningnetwork/lnd/clock"
1214
"github.com/lightningnetwork/lnd/ticker"
1315
)
1416

1517
// getClient returns an instance of the swap client.
16-
func getClient(config *Config, lnd *lndclient.LndServices) (*loop.Client,
18+
func getClient(cfg *Config, lnd *lndclient.LndServices) (*loop.Client,
1719
func(), error) {
1820

1921
clientConfig := &loop.ClientConfig{
20-
ServerAddress: config.Server.Host,
21-
ProxyAddress: config.Server.Proxy,
22-
SwapServerNoTLS: config.Server.NoTLS,
23-
TLSPathServer: config.Server.TLSPath,
22+
ServerAddress: cfg.Server.Host,
23+
ProxyAddress: cfg.Server.Proxy,
24+
SwapServerNoTLS: cfg.Server.NoTLS,
25+
TLSPathServer: cfg.Server.TLSPath,
2426
Lnd: lnd,
25-
MaxLsatCost: btcutil.Amount(config.MaxLSATCost),
26-
MaxLsatFee: btcutil.Amount(config.MaxLSATFee),
27-
LoopOutMaxParts: config.LoopOutMaxParts,
28-
TotalPaymentTimeout: config.TotalPaymentTimeout,
29-
MaxPaymentRetries: config.MaxPaymentRetries,
27+
MaxLsatCost: btcutil.Amount(cfg.MaxLSATCost),
28+
MaxLsatFee: btcutil.Amount(cfg.MaxLSATFee),
29+
LoopOutMaxParts: cfg.LoopOutMaxParts,
30+
TotalPaymentTimeout: cfg.TotalPaymentTimeout,
31+
MaxPaymentRetries: cfg.MaxPaymentRetries,
3032
}
3133

32-
swapClient, cleanUp, err := loop.NewClient(config.DataDir, clientConfig)
34+
// Now that we know where the database will live, we'll go ahead and
35+
// open up the default implementation of it.
36+
var (
37+
db loopdb.SwapStore
38+
err error
39+
)
40+
switch cfg.DatabaseBackend {
41+
case DatabaseBackendSqlite:
42+
log.Infof("Opening sqlite3 database at: %v",
43+
cfg.Sqlite.DatabaseFileName)
44+
db, err = loopdb.NewSqliteStore(
45+
cfg.Sqlite, clientConfig.Lnd.ChainParams,
46+
)
47+
48+
case DatabaseBackendPostgres:
49+
log.Infof("Opening postgres database at: %v",
50+
cfg.Postgres.DSN(true))
51+
db, err = loopdb.NewPostgresStore(
52+
cfg.Postgres, clientConfig.Lnd.ChainParams,
53+
)
54+
55+
default:
56+
return nil, nil, fmt.Errorf("unknown database backend: %s",
57+
cfg.DatabaseBackend)
58+
}
59+
if err != nil {
60+
return nil, nil, fmt.Errorf("unable to open database: %v", err)
61+
}
62+
63+
swapClient, cleanUp, err := loop.NewClient(
64+
cfg.DataDir, db, clientConfig,
65+
)
3366
if err != nil {
3467
return nil, nil, err
3568
}

0 commit comments

Comments
 (0)