Skip to content

Commit ec09b5b

Browse files
authored
Merge pull request #649 from hieblmi/refactor-db-opening
db: refactor db opening
2 parents abecfd3 + 79063d7 commit ec09b5b

File tree

4 files changed

+53
-47
lines changed

4 files changed

+53
-47
lines changed

loopd/daemon.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,22 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
392392
log.Infof("Successfully migrated boltdb")
393393
}
394394

395+
// Now that we know where the database will live, we'll go ahead and
396+
// open up the default implementation of it.
397+
chainParams, err := lndclient.Network(d.cfg.Network).ChainParams()
398+
if err != nil {
399+
return err
400+
}
401+
402+
swapDb, _, err := openDatabase(d.cfg, chainParams)
403+
if err != nil {
404+
return err
405+
}
406+
395407
// Create an instance of the loop client library.
396-
swapclient, clientCleanup, err := getClient(d.cfg, &d.lnd.LndServices)
408+
swapClient, clientCleanup, err := getClient(
409+
d.cfg, swapDb, &d.lnd.LndServices,
410+
)
397411
if err != nil {
398412
return err
399413
}
@@ -456,8 +470,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
456470
d.swapClientServer = swapClientServer{
457471
config: d.cfg,
458472
network: lndclient.Network(d.cfg.Network),
459-
impl: swapclient,
460-
liquidityMgr: getLiquidityManager(swapclient),
473+
impl: swapClient,
474+
liquidityMgr: getLiquidityManager(swapClient),
461475
lnd: &d.lnd.LndServices,
462476
swaps: make(map[lntypes.Hash]loop.SwapInfo),
463477
subscribers: make(map[int]chan<- interface{}),

loopd/migration.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package loopd
22

33
import (
44
"context"
5-
"fmt"
65
"os"
76
"path/filepath"
87

@@ -26,34 +25,14 @@ func migrateBoltdb(ctx context.Context, cfg *Config) error {
2625
}
2726
defer boltdb.Close()
2827

29-
var db loopdb.SwapStore
30-
switch cfg.DatabaseBackend {
31-
case DatabaseBackendSqlite:
32-
log.Infof("Opening sqlite3 database at: %v",
33-
cfg.Sqlite.DatabaseFileName)
34-
db, err = loopdb.NewSqliteStore(
35-
cfg.Sqlite, chainParams,
36-
)
37-
38-
case DatabaseBackendPostgres:
39-
log.Infof("Opening postgres database at: %v",
40-
cfg.Postgres.DSN(true))
41-
db, err = loopdb.NewPostgresStore(
42-
cfg.Postgres, chainParams,
43-
)
44-
45-
default:
46-
return fmt.Errorf("unknown database backend: %s",
47-
cfg.DatabaseBackend)
48-
}
28+
swapDb, _, err := openDatabase(cfg, chainParams)
4929
if err != nil {
50-
return fmt.Errorf("unable to open database: %v", err)
30+
return err
5131
}
52-
53-
defer db.Close()
32+
defer swapDb.Close()
5433

5534
// Create a new migrator manager.
56-
migrator := loopdb.NewMigratorManager(boltdb, db)
35+
migrator := loopdb.NewMigratorManager(boltdb, swapDb)
5736

5837
// Run the migration.
5938
err = migrator.RunMigrations(ctx)

loopd/utils.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/btcsuite/btcd/btcutil"
8+
"github.com/btcsuite/btcd/chaincfg"
89
"github.com/lightninglabs/lndclient"
910
"github.com/lightninglabs/loop"
1011
"github.com/lightninglabs/loop/liquidity"
@@ -15,8 +16,8 @@ import (
1516
)
1617

1718
// getClient returns an instance of the swap client.
18-
func getClient(cfg *Config, lnd *lndclient.LndServices) (*loop.Client,
19-
func(), error) {
19+
func getClient(cfg *Config, swapDb loopdb.SwapStore,
20+
lnd *lndclient.LndServices) (*loop.Client, func(), error) {
2021

2122
clientConfig := &loop.ClientConfig{
2223
ServerAddress: cfg.Server.Host,
@@ -31,26 +32,40 @@ func getClient(cfg *Config, lnd *lndclient.LndServices) (*loop.Client,
3132
MaxPaymentRetries: cfg.MaxPaymentRetries,
3233
}
3334

34-
// Now that we know where the database will live, we'll go ahead and
35-
// open up the default implementation of it.
35+
swapClient, cleanUp, err := loop.NewClient(
36+
cfg.DataDir, swapDb, clientConfig,
37+
)
38+
if err != nil {
39+
return nil, nil, err
40+
}
41+
42+
return swapClient, cleanUp, nil
43+
}
44+
45+
func openDatabase(cfg *Config, chainParams *chaincfg.Params) (loopdb.SwapStore,
46+
*loopdb.BaseDB, error) { //nolint:unparam
47+
3648
var (
37-
db loopdb.SwapStore
38-
err error
49+
db loopdb.SwapStore
50+
err error
51+
baseDb loopdb.BaseDB
3952
)
4053
switch cfg.DatabaseBackend {
4154
case DatabaseBackendSqlite:
4255
log.Infof("Opening sqlite3 database at: %v",
4356
cfg.Sqlite.DatabaseFileName)
4457
db, err = loopdb.NewSqliteStore(
45-
cfg.Sqlite, clientConfig.Lnd.ChainParams,
58+
cfg.Sqlite, chainParams,
4659
)
60+
baseDb = *db.(*loopdb.SqliteSwapStore).BaseDB
4761

4862
case DatabaseBackendPostgres:
4963
log.Infof("Opening postgres database at: %v",
5064
cfg.Postgres.DSN(true))
5165
db, err = loopdb.NewPostgresStore(
52-
cfg.Postgres, clientConfig.Lnd.ChainParams,
66+
cfg.Postgres, chainParams,
5367
)
68+
baseDb = *db.(*loopdb.PostgresStore).BaseDB
5469

5570
default:
5671
return nil, nil, fmt.Errorf("unknown database backend: %s",
@@ -60,14 +75,7 @@ func getClient(cfg *Config, lnd *lndclient.LndServices) (*loop.Client,
6075
return nil, nil, fmt.Errorf("unable to open database: %v", err)
6176
}
6277

63-
swapClient, cleanUp, err := loop.NewClient(
64-
cfg.DataDir, db, clientConfig,
65-
)
66-
if err != nil {
67-
return nil, nil, err
68-
}
69-
70-
return swapClient, cleanUp, nil
78+
return db, &baseDb, nil
7179
}
7280

7381
func getLiquidityManager(client *loop.Client) *liquidity.Manager {

loopd/view.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,21 @@ func view(config *Config, lisCfg *ListenerCfg) error {
2020
}
2121
defer lnd.Close()
2222

23-
swapClient, cleanup, err := getClient(config, &lnd.LndServices)
23+
chainParams, err := network.ChainParams()
2424
if err != nil {
2525
return err
2626
}
27-
defer cleanup()
2827

29-
chainParams, err := network.ChainParams()
28+
swapDb, _, err := openDatabase(config, chainParams)
29+
if err != nil {
30+
return err
31+
}
32+
33+
swapClient, cleanup, err := getClient(config, swapDb, &lnd.LndServices)
3034
if err != nil {
3135
return err
3236
}
37+
defer cleanup()
3338

3439
if err := viewOut(swapClient, chainParams); err != nil {
3540
return err

0 commit comments

Comments
 (0)