lnd+lncfg: move wallet DB options into backend method

To have all the database backend related code in one place, we finally
also move the initialization of the wallet DB loader option into the
GetBackends() method.
This commit is contained in:
Oliver Gugger 2021-08-03 09:57:35 +02:00
parent 75531455da
commit 57c7862eeb
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
2 changed files with 27 additions and 26 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/kvdb/etcd"
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
)
const (
@ -135,6 +136,10 @@ type DatabaseBackends struct {
// server data. This might be nil if the watchtower server is disabled.
TowerServerDB kvdb.Backend
// WalletDB is an option that instructs the wallet loader where to load
// the underlying wallet database from.
WalletDB btcwallet.LoaderOption
// Remote indicates whether the database backends are remote, possibly
// replicated instances or local bbolt backed databases.
Remote bool
@ -185,8 +190,16 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
DecayedLogDB: etcdBackend,
TowerClientDB: etcdBackend,
TowerServerDB: etcdBackend,
Remote: true,
CloseFuncs: closeFuncs,
// The wallet loader will attempt to use/create the
// wallet in the replicated remote DB if we're running
// in a clustered environment. This will ensure that all
// members of the cluster have access to the same wallet
// state.
WalletDB: btcwallet.LoaderWithExternalWalletDB(
etcdBackend,
),
Remote: true,
CloseFuncs: closeFuncs,
}, nil
}
@ -281,7 +294,13 @@ func (db *DB) GetBackends(ctx context.Context, chanDBPath,
DecayedLogDB: decayedLogBackend,
TowerClientDB: towerClientBackend,
TowerServerDB: towerServerBackend,
CloseFuncs: closeFuncs,
// When "running locally", LND will use the bbolt wallet.db to
// store the wallet located in the chain data dir, parametrized
// by the active network.
WalletDB: btcwallet.LoaderWithLocalWalletDB(
walletDBPath, !db.Bolt.SyncFreelist, db.Bolt.DBTimeout,
),
CloseFuncs: closeFuncs,
}, nil
}

28
lnd.go
View File

@ -462,25 +462,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error
defer cleanUp()
var loaderOpt btcwallet.LoaderOption
if cfg.Cluster.EnableLeaderElection {
// The wallet loader will attempt to use/create the wallet in
// the replicated remote DB if we're running in a clustered
// environment. This will ensure that all members of the cluster
// have access to the same wallet state.
loaderOpt = btcwallet.LoaderWithExternalWalletDB(
dbs.chanStateDB.Backend,
)
} else {
// When "running locally", LND will use the bbolt wallet.db to
// store the wallet located in the chain data dir, parametrized
// by the active network.
loaderOpt = btcwallet.LoaderWithLocalWalletDB(
cfg.networkDir, !cfg.SyncFreelist, cfg.DB.Bolt.DBTimeout,
)
}
pwService.SetLoaderOpts([]btcwallet.LoaderOption{loaderOpt})
pwService.SetLoaderOpts([]btcwallet.LoaderOption{dbs.walletDB})
pwService.SetMacaroonDB(dbs.macaroonDB)
walletExists, err := pwService.WalletExists()
if err != nil {
@ -556,7 +538,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error
// over RPC.
default:
params, err := waitForWalletPassword(
cfg, pwService, []btcwallet.LoaderOption{loaderOpt},
cfg, pwService, []btcwallet.LoaderOption{dbs.walletDB},
interceptor.ShutdownChannel(),
)
if err != nil {
@ -715,9 +697,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error
return cfg.net.Dial("tcp", addr, cfg.ConnectionTimeout)
},
BlockCacheSize: cfg.BlockCacheSize,
LoaderOptions: []btcwallet.LoaderOption{
loaderOpt,
},
LoaderOptions: []btcwallet.LoaderOption{dbs.walletDB},
}
// Parse coin selection strategy.
@ -1584,6 +1564,7 @@ type databaseInstances struct {
decayedLogDB kvdb.Backend
towerClientDB wtclient.DB
towerServerDB watchtower.DB
walletDB btcwallet.LoaderOption
}
// initializeDatabases extracts the current databases that we'll use for normal
@ -1622,6 +1603,7 @@ func initializeDatabases(ctx context.Context,
heightHintDB: databaseBackends.HeightHintDB,
macaroonDB: databaseBackends.MacaroonDB,
decayedLogDB: databaseBackends.DecayedLogDB,
walletDB: databaseBackends.WalletDB,
}
cleanUp := func() {
// We can just close the returned close functions directly. Even