From 57c7862eeb4cb406a480d9e174dc7f16aed4269e Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Tue, 3 Aug 2021 09:57:35 +0200 Subject: [PATCH] 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. --- lncfg/db.go | 25 ++++++++++++++++++++++--- lnd.go | 28 +++++----------------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/lncfg/db.go b/lncfg/db.go index 01b0f51cd..f856396b9 100644 --- a/lncfg/db.go +++ b/lncfg/db.go @@ -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 } diff --git a/lnd.go b/lnd.go index 348473df2..d730fe0fe 100644 --- a/lnd.go +++ b/lnd.go @@ -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