db: refactor db opening

pull/649/head
Slyghtning 7 months ago
parent abecfd3b65
commit 79063d788a
No known key found for this signature in database
GPG Key ID: F82D456EA023C9BF

@ -392,8 +392,22 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
log.Infof("Successfully migrated boltdb") log.Infof("Successfully migrated boltdb")
} }
// Now that we know where the database will live, we'll go ahead and
// open up the default implementation of it.
chainParams, err := lndclient.Network(d.cfg.Network).ChainParams()
if err != nil {
return err
}
swapDb, _, err := openDatabase(d.cfg, chainParams)
if err != nil {
return err
}
// Create an instance of the loop client library. // Create an instance of the loop client library.
swapclient, clientCleanup, err := getClient(d.cfg, &d.lnd.LndServices) swapClient, clientCleanup, err := getClient(
d.cfg, swapDb, &d.lnd.LndServices,
)
if err != nil { if err != nil {
return err return err
} }
@ -456,8 +470,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
d.swapClientServer = swapClientServer{ d.swapClientServer = swapClientServer{
config: d.cfg, config: d.cfg,
network: lndclient.Network(d.cfg.Network), network: lndclient.Network(d.cfg.Network),
impl: swapclient, impl: swapClient,
liquidityMgr: getLiquidityManager(swapclient), liquidityMgr: getLiquidityManager(swapClient),
lnd: &d.lnd.LndServices, lnd: &d.lnd.LndServices,
swaps: make(map[lntypes.Hash]loop.SwapInfo), swaps: make(map[lntypes.Hash]loop.SwapInfo),
subscribers: make(map[int]chan<- interface{}), subscribers: make(map[int]chan<- interface{}),

@ -2,7 +2,6 @@ package loopd
import ( import (
"context" "context"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -26,34 +25,14 @@ func migrateBoltdb(ctx context.Context, cfg *Config) error {
} }
defer boltdb.Close() defer boltdb.Close()
var db loopdb.SwapStore swapDb, _, err := openDatabase(cfg, chainParams)
switch cfg.DatabaseBackend {
case DatabaseBackendSqlite:
log.Infof("Opening sqlite3 database at: %v",
cfg.Sqlite.DatabaseFileName)
db, err = loopdb.NewSqliteStore(
cfg.Sqlite, chainParams,
)
case DatabaseBackendPostgres:
log.Infof("Opening postgres database at: %v",
cfg.Postgres.DSN(true))
db, err = loopdb.NewPostgresStore(
cfg.Postgres, chainParams,
)
default:
return fmt.Errorf("unknown database backend: %s",
cfg.DatabaseBackend)
}
if err != nil { if err != nil {
return fmt.Errorf("unable to open database: %v", err) return err
} }
defer swapDb.Close()
defer db.Close()
// Create a new migrator manager. // Create a new migrator manager.
migrator := loopdb.NewMigratorManager(boltdb, db) migrator := loopdb.NewMigratorManager(boltdb, swapDb)
// Run the migration. // Run the migration.
err = migrator.RunMigrations(ctx) err = migrator.RunMigrations(ctx)

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightninglabs/lndclient" "github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop" "github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/liquidity" "github.com/lightninglabs/loop/liquidity"
@ -15,8 +16,8 @@ import (
) )
// getClient returns an instance of the swap client. // getClient returns an instance of the swap client.
func getClient(cfg *Config, lnd *lndclient.LndServices) (*loop.Client, func getClient(cfg *Config, swapDb loopdb.SwapStore,
func(), error) { lnd *lndclient.LndServices) (*loop.Client, func(), error) {
clientConfig := &loop.ClientConfig{ clientConfig := &loop.ClientConfig{
ServerAddress: cfg.Server.Host, ServerAddress: cfg.Server.Host,
@ -31,26 +32,40 @@ func getClient(cfg *Config, lnd *lndclient.LndServices) (*loop.Client,
MaxPaymentRetries: cfg.MaxPaymentRetries, MaxPaymentRetries: cfg.MaxPaymentRetries,
} }
// Now that we know where the database will live, we'll go ahead and swapClient, cleanUp, err := loop.NewClient(
// open up the default implementation of it. cfg.DataDir, swapDb, clientConfig,
)
if err != nil {
return nil, nil, err
}
return swapClient, cleanUp, nil
}
func openDatabase(cfg *Config, chainParams *chaincfg.Params) (loopdb.SwapStore,
*loopdb.BaseDB, error) { //nolint:unparam
var ( var (
db loopdb.SwapStore db loopdb.SwapStore
err error err error
baseDb loopdb.BaseDB
) )
switch cfg.DatabaseBackend { switch cfg.DatabaseBackend {
case DatabaseBackendSqlite: case DatabaseBackendSqlite:
log.Infof("Opening sqlite3 database at: %v", log.Infof("Opening sqlite3 database at: %v",
cfg.Sqlite.DatabaseFileName) cfg.Sqlite.DatabaseFileName)
db, err = loopdb.NewSqliteStore( db, err = loopdb.NewSqliteStore(
cfg.Sqlite, clientConfig.Lnd.ChainParams, cfg.Sqlite, chainParams,
) )
baseDb = *db.(*loopdb.SqliteSwapStore).BaseDB
case DatabaseBackendPostgres: case DatabaseBackendPostgres:
log.Infof("Opening postgres database at: %v", log.Infof("Opening postgres database at: %v",
cfg.Postgres.DSN(true)) cfg.Postgres.DSN(true))
db, err = loopdb.NewPostgresStore( db, err = loopdb.NewPostgresStore(
cfg.Postgres, clientConfig.Lnd.ChainParams, cfg.Postgres, chainParams,
) )
baseDb = *db.(*loopdb.PostgresStore).BaseDB
default: default:
return nil, nil, fmt.Errorf("unknown database backend: %s", return nil, nil, fmt.Errorf("unknown database backend: %s",
@ -60,14 +75,7 @@ func getClient(cfg *Config, lnd *lndclient.LndServices) (*loop.Client,
return nil, nil, fmt.Errorf("unable to open database: %v", err) return nil, nil, fmt.Errorf("unable to open database: %v", err)
} }
swapClient, cleanUp, err := loop.NewClient( return db, &baseDb, nil
cfg.DataDir, db, clientConfig,
)
if err != nil {
return nil, nil, err
}
return swapClient, cleanUp, nil
} }
func getLiquidityManager(client *loop.Client) *liquidity.Manager { func getLiquidityManager(client *loop.Client) *liquidity.Manager {

@ -20,16 +20,21 @@ func view(config *Config, lisCfg *ListenerCfg) error {
} }
defer lnd.Close() defer lnd.Close()
swapClient, cleanup, err := getClient(config, &lnd.LndServices) chainParams, err := network.ChainParams()
if err != nil { if err != nil {
return err return err
} }
defer cleanup()
chainParams, err := network.ChainParams() swapDb, _, err := openDatabase(config, chainParams)
if err != nil {
return err
}
swapClient, cleanup, err := getClient(config, swapDb, &lnd.LndServices)
if err != nil { if err != nil {
return err return err
} }
defer cleanup()
if err := viewOut(swapClient, chainParams); err != nil { if err := viewOut(swapClient, chainParams); err != nil {
return err return err

Loading…
Cancel
Save