diff --git a/chainntnfs/test_utils.go b/chainntnfs/test_utils.go index c4350ec52..a60439684 100644 --- a/chainntnfs/test_utils.go +++ b/chainntnfs/test_utils.go @@ -24,6 +24,7 @@ import ( "github.com/btcsuite/btcwallet/chain" "github.com/btcsuite/btcwallet/walletdb" "github.com/lightninglabs/neutrino" + "github.com/lightningnetwork/lnd/channeldb/kvdb" ) var ( @@ -268,7 +269,9 @@ func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService, } dbName := filepath.Join(spvDir, "neutrino.db") - spvDatabase, err := walletdb.Create("bdb", dbName, true) + spvDatabase, err := walletdb.Create( + "bdb", dbName, true, kvdb.DefaultDBTimeout, + ) if err != nil { os.RemoveAll(spvDir) t.Fatalf("unable to create walletdb: %v", err) diff --git a/chainreg/chainregistry.go b/chainreg/chainregistry.go index bf0bb3d14..02d556128 100644 --- a/chainreg/chainregistry.go +++ b/chainreg/chainregistry.go @@ -101,6 +101,10 @@ type Config struct { // FeeURL defines the URL for fee estimation we will use. This field is // optional. FeeURL string + + // DBTimeOut specifies the timeout value to use when opening the wallet + // database. + DBTimeOut time.Duration } const ( @@ -273,6 +277,7 @@ func NewChainControl(cfg *Config) (*ChainControl, error) { NetParams: cfg.ActiveNetParams.Params, CoinType: cfg.ActiveNetParams.CoinType, Wallet: cfg.Wallet, + DBTimeOut: cfg.DBTimeOut, } var err error diff --git a/channeldb/db.go b/channeldb/db.go index 5c7d90b06..6cdebd5f4 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -244,6 +244,7 @@ func Open(dbPath string, modifiers ...OptionModifier) (*DB, error) { NoFreelistSync: opts.NoFreelistSync, AutoCompact: opts.AutoCompact, AutoCompactMinAge: opts.AutoCompactMinAge, + DBTimeout: opts.DBTimeout, }) if err != nil { return nil, err diff --git a/channeldb/forwarding_package_test.go b/channeldb/forwarding_package_test.go index a85c7420f..e76915d11 100644 --- a/channeldb/forwarding_package_test.go +++ b/channeldb/forwarding_package_test.go @@ -808,7 +808,9 @@ func makeFwdPkgDB(t *testing.T, path string) kvdb.Backend { // nolint:unparam path = filepath.Join(path, "fwdpkg.db") } - bdb, err := kvdb.Create(kvdb.BoltBackendName, path, true) + bdb, err := kvdb.Create( + kvdb.BoltBackendName, path, true, kvdb.DefaultDBTimeout, + ) if err != nil { t.Fatalf("unable to open boltdb: %v", err) } diff --git a/channeldb/kvdb/backend.go b/channeldb/kvdb/backend.go index e2ac7ae64..851f430e7 100644 --- a/channeldb/kvdb/backend.go +++ b/channeldb/kvdb/backend.go @@ -64,6 +64,10 @@ type BoltBackendConfig struct { // since a bolt database file was last compacted for the compaction to // be considered again. AutoCompactMinAge time.Duration + + // DBTimeout specifies the timeout value to use when opening the wallet + // database. + DBTimeout time.Duration } // GetBoltBackend opens (or creates if doesn't exits) a bbolt backed database @@ -79,7 +83,10 @@ func GetBoltBackend(cfg *BoltBackendConfig) (Backend, error) { } } - return Create(BoltBackendName, dbFilePath, cfg.NoFreelistSync) + return Create( + BoltBackendName, dbFilePath, + cfg.NoFreelistSync, cfg.DBTimeout, + ) } // This is an existing database. We might want to compact it on startup @@ -90,7 +97,10 @@ func GetBoltBackend(cfg *BoltBackendConfig) (Backend, error) { } } - return Open(BoltBackendName, dbFilePath, cfg.NoFreelistSync) + return Open( + BoltBackendName, dbFilePath, + cfg.NoFreelistSync, cfg.DBTimeout, + ) } // compactAndSwap will attempt to write a new temporary DB file to disk with @@ -156,8 +166,9 @@ func compactAndSwap(cfg *BoltBackendConfig) error { _ = os.Remove(tempDestFilePath) }() c := &compacter{ - srcPath: sourceFilePath, - dstPath: tempDestFilePath, + srcPath: sourceFilePath, + dstPath: tempDestFilePath, + dbTimeout: cfg.DBTimeout, } initialSize, newSize, err := c.execute() if err != nil { @@ -235,6 +246,7 @@ func GetTestBackend(path, name string) (Backend, func(), error) { DBPath: path, DBFileName: name, NoFreelistSync: true, + DBTimeout: DefaultDBTimeout, }) if err != nil { return nil, nil, err diff --git a/channeldb/kvdb/bolt_compact.go b/channeldb/kvdb/bolt_compact.go index da8f27982..9d2676d75 100644 --- a/channeldb/kvdb/bolt_compact.go +++ b/channeldb/kvdb/bolt_compact.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "path" + "time" "github.com/lightningnetwork/lnd/healthcheck" "go.etcd.io/bbolt" @@ -38,6 +39,9 @@ type compacter struct { srcPath string dstPath string txMaxSize int64 + + // dbTimeout specifies the timeout value used when opening the db. + dbTimeout time.Duration } // execute opens the source and destination databases and then compacts the @@ -79,6 +83,7 @@ func (cmd *compacter) execute() (int64, int64, error) { // possible freelist sync problems. src, err := bbolt.Open(cmd.srcPath, 0444, &bbolt.Options{ ReadOnly: true, + Timeout: cmd.dbTimeout, }) if err != nil { return 0, 0, fmt.Errorf("error opening source database: %v", @@ -91,7 +96,9 @@ func (cmd *compacter) execute() (int64, int64, error) { }() // Open destination database. - dst, err := bbolt.Open(cmd.dstPath, fi.Mode(), nil) + dst, err := bbolt.Open(cmd.dstPath, fi.Mode(), &bbolt.Options{ + Timeout: cmd.dbTimeout, + }) if err != nil { return 0, 0, fmt.Errorf("error opening destination database: "+ "%v", err) diff --git a/channeldb/kvdb/config.go b/channeldb/kvdb/config.go index 9ea50adc0..019e34af0 100644 --- a/channeldb/kvdb/config.go +++ b/channeldb/kvdb/config.go @@ -17,6 +17,10 @@ const ( // have passed since a bolt database file was last compacted for the // compaction to be considered again. DefaultBoltAutoCompactMinAge = time.Hour * 24 * 7 + + // DefaultDBTimeout specifies the default timeout value when opening + // the bbolt database. + DefaultDBTimeout = time.Second * 60 ) // BoltConfig holds bolt configuration. @@ -26,6 +30,8 @@ type BoltConfig struct { AutoCompact bool `long:"auto-compact" description:"Whether the databases used within lnd should automatically be compacted on every startup (and if the database has the configured minimum age). This is disabled by default because it requires additional disk space to be available during the compaction that is freed afterwards. In general compaction leads to smaller database files."` AutoCompactMinAge time.Duration `long:"auto-compact-min-age" description:"How long ago the last compaction of a database file must be for it to be considered for auto compaction again. Can be set to 0 to compact on every startup."` + + DBTimeout time.Duration `long:"dbtimeout" description:"Specify the timeout value used when opening the database."` } // EtcdConfig holds etcd configuration. diff --git a/channeldb/migration_01_to_11/db.go b/channeldb/migration_01_to_11/db.go index be690a987..353e7aed8 100644 --- a/channeldb/migration_01_to_11/db.go +++ b/channeldb/migration_01_to_11/db.go @@ -55,7 +55,10 @@ func Open(dbPath string, modifiers ...OptionModifier) (*DB, error) { // Specify bbolt freelist options to reduce heap pressure in case the // freelist grows to be very large. - bdb, err := kvdb.Open(kvdb.BoltBackendName, path, opts.NoFreelistSync) + bdb, err := kvdb.Open( + kvdb.BoltBackendName, path, + opts.NoFreelistSync, opts.DBTimeout, + ) if err != nil { return nil, err } @@ -84,7 +87,9 @@ func createChannelDB(dbPath string) error { } path := filepath.Join(dbPath, dbName) - bdb, err := kvdb.Create(kvdb.BoltBackendName, path, false) + bdb, err := kvdb.Create( + kvdb.BoltBackendName, path, false, kvdb.DefaultDBTimeout, + ) if err != nil { return err } diff --git a/channeldb/migration_01_to_11/options.go b/channeldb/migration_01_to_11/options.go index 03b287e04..8b2edad64 100644 --- a/channeldb/migration_01_to_11/options.go +++ b/channeldb/migration_01_to_11/options.go @@ -1,5 +1,7 @@ package migration_01_to_11 +import "time" + const ( // DefaultRejectCacheSize is the default number of rejectCacheEntries to // cache for use in the rejection cache of incoming gossip traffic. This @@ -10,6 +12,10 @@ const ( // in order to reply to gossip queries. This produces a cache size of // around 40MB. DefaultChannelCacheSize = 20000 + + // DefaultDBTimeout specifies the default timeout value when opening + // the bbolt database. + DefaultDBTimeout = time.Second * 60 ) // Options holds parameters for tuning and customizing a channeldb.DB. @@ -26,6 +32,10 @@ type Options struct { // freelist to disk, resulting in improved performance at the expense of // increased startup time. NoFreelistSync bool + + // DBTimeout specifies the timeout value to use when opening the wallet + // database. + DBTimeout time.Duration } // DefaultOptions returns an Options populated with default values. @@ -34,6 +44,7 @@ func DefaultOptions() Options { RejectCacheSize: DefaultRejectCacheSize, ChannelCacheSize: DefaultChannelCacheSize, NoFreelistSync: true, + DBTimeout: DefaultDBTimeout, } } diff --git a/channeldb/migtest/migtest.go b/channeldb/migtest/migtest.go index 9555d093c..d970769ee 100644 --- a/channeldb/migtest/migtest.go +++ b/channeldb/migtest/migtest.go @@ -20,7 +20,9 @@ func MakeDB() (kvdb.Backend, func(), error) { } dbPath := file.Name() - db, err := kvdb.Open(kvdb.BoltBackendName, dbPath, true) + db, err := kvdb.Open( + kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout, + ) if err != nil { return nil, nil, err } diff --git a/channeldb/options.go b/channeldb/options.go index 285da5704..2c15c42f2 100644 --- a/channeldb/options.go +++ b/channeldb/options.go @@ -50,6 +50,7 @@ func DefaultOptions() Options { NoFreelistSync: true, AutoCompact: false, AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge, + DBTimeout: kvdb.DefaultDBTimeout, }, RejectCacheSize: DefaultRejectCacheSize, ChannelCacheSize: DefaultChannelCacheSize, diff --git a/channeldb/options_test.go b/channeldb/options_test.go new file mode 100644 index 000000000..b45e886c9 --- /dev/null +++ b/channeldb/options_test.go @@ -0,0 +1,28 @@ +package channeldb_test + +import ( + "testing" + + "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/channeldb/kvdb" + "github.com/stretchr/testify/require" +) + +// TestDefaultOptions tests the default options are created as intended. +func TestDefaultOptions(t *testing.T) { + + opts := channeldb.DefaultOptions() + + require.True(t, opts.NoFreelistSync) + require.False(t, opts.AutoCompact) + require.Equal( + t, kvdb.DefaultBoltAutoCompactMinAge, opts.AutoCompactMinAge, + ) + require.Equal(t, kvdb.DefaultDBTimeout, opts.DBTimeout) + require.Equal( + t, channeldb.DefaultRejectCacheSize, opts.RejectCacheSize, + ) + require.Equal( + t, channeldb.DefaultChannelCacheSize, opts.ChannelCacheSize, + ) +} diff --git a/contractcourt/briefcase_test.go b/contractcourt/briefcase_test.go index 6c2936b4e..b9671239d 100644 --- a/contractcourt/briefcase_test.go +++ b/contractcourt/briefcase_test.go @@ -112,7 +112,10 @@ func makeTestDB() (kvdb.Backend, func(), error) { return nil, nil, err } - db, err := kvdb.Create(kvdb.BoltBackendName, tempDirName+"/test.db", true) + db, err := kvdb.Create( + kvdb.BoltBackendName, tempDirName+"/test.db", true, + kvdb.DefaultDBTimeout, + ) if err != nil { return nil, nil, err } diff --git a/contractcourt/channel_arbitrator_test.go b/contractcourt/channel_arbitrator_test.go index a4e9d0034..cea6f3f4f 100644 --- a/contractcourt/channel_arbitrator_test.go +++ b/contractcourt/channel_arbitrator_test.go @@ -392,7 +392,10 @@ func createTestChannelArbitrator(t *testing.T, log ArbitratorLog, return nil, err } dbPath := filepath.Join(dbDir, "testdb") - db, err := kvdb.Create(kvdb.BoltBackendName, dbPath, true) + db, err := kvdb.Create( + kvdb.BoltBackendName, dbPath, true, + kvdb.DefaultDBTimeout, + ) if err != nil { return nil, err } diff --git a/go.mod b/go.mod index f244d135a..18b2c6f05 100644 --- a/go.mod +++ b/go.mod @@ -9,10 +9,10 @@ require ( github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f github.com/btcsuite/btcutil v1.0.2 github.com/btcsuite/btcutil/psbt v1.0.3-0.20200826194809-5f93e33af2b0 - github.com/btcsuite/btcwallet v0.11.1-0.20201104022105-a6f3888450e4 + github.com/btcsuite/btcwallet v0.11.1-0.20201119022810-664f77ded195 github.com/btcsuite/btcwallet/wallet/txauthor v1.0.0 github.com/btcsuite/btcwallet/wallet/txrules v1.0.0 - github.com/btcsuite/btcwallet/walletdb v1.3.3 + github.com/btcsuite/btcwallet/walletdb v1.3.4-0.20201106155705-08308c81eda0 github.com/btcsuite/btcwallet/wtxmgr v1.2.0 github.com/coreos/etcd v3.3.22+incompatible github.com/coreos/go-semver v0.3.0 // indirect diff --git a/go.sum b/go.sum index 1b0312ca0..d58ecbabe 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/btcsuite/btcutil/psbt v1.0.3-0.20200826194809-5f93e33af2b0 h1:3Zumkyl github.com/btcsuite/btcutil/psbt v1.0.3-0.20200826194809-5f93e33af2b0/go.mod h1:LVveMu4VaNSkIRTZu2+ut0HDBRuYjqGocxDMNS1KuGQ= github.com/btcsuite/btcwallet v0.11.1-0.20201104022105-a6f3888450e4 h1:iipg6ldTL0y69KLicrd0gLhY/yErB0rG4a84uQbu7ao= github.com/btcsuite/btcwallet v0.11.1-0.20201104022105-a6f3888450e4/go.mod h1:owv9oZqM0HnUW+ByF7VqOgfs2eb0ooiePW/+Tl/i/Nk= +github.com/btcsuite/btcwallet v0.11.1-0.20201119022810-664f77ded195 h1:QjMLhLVZlpMjgWH5FK0dvwcXAhO2/80g4opgUDa9mAQ= +github.com/btcsuite/btcwallet v0.11.1-0.20201119022810-664f77ded195/go.mod h1:owv9oZqM0HnUW+ByF7VqOgfs2eb0ooiePW/+Tl/i/Nk= github.com/btcsuite/btcwallet/wallet/txauthor v1.0.0 h1:KGHMW5sd7yDdDMkCZ/JpP0KltolFsQcB973brBnfj4c= github.com/btcsuite/btcwallet/wallet/txauthor v1.0.0/go.mod h1:VufDts7bd/zs3GV13f/lXc/0lXrPnvxD/NvmpG/FEKU= github.com/btcsuite/btcwallet/wallet/txrules v1.0.0 h1:2VsfS0sBedcM5KmDzRMT3+b6xobqWveZGvjb+jFez5w= @@ -51,6 +53,8 @@ github.com/btcsuite/btcwallet/walletdb v1.3.2 h1:nFnMBVgkqoVOx08Z756oDwNc9sdVgYR github.com/btcsuite/btcwallet/walletdb v1.3.2/go.mod h1:GZCMPNpUu5KE3ASoVd+k06p/1OW8OwNGCCaNWRto2cQ= github.com/btcsuite/btcwallet/walletdb v1.3.3 h1:u6e7vRIKBF++cJy+hOHaMGg+88ZTwvpaY27AFvtB668= github.com/btcsuite/btcwallet/walletdb v1.3.3/go.mod h1:oJDxAEUHVtnmIIBaa22wSBPTVcs6hUp5NKWmI8xDwwU= +github.com/btcsuite/btcwallet/walletdb v1.3.4-0.20201106155705-08308c81eda0 h1:1ErPv22GAP5CKfQ1cSv5ROu+mSlDvzUOHpOuSnjVU9g= +github.com/btcsuite/btcwallet/walletdb v1.3.4-0.20201106155705-08308c81eda0/go.mod h1:oJDxAEUHVtnmIIBaa22wSBPTVcs6hUp5NKWmI8xDwwU= github.com/btcsuite/btcwallet/wtxmgr v1.0.0/go.mod h1:vc4gBprll6BP0UJ+AIGDaySoc7MdAmZf8kelfNb8CFY= github.com/btcsuite/btcwallet/wtxmgr v1.2.0 h1:ZUYPsSv8GjF9KK7lboB2OVHF0uYEcHxgrCfFWqPd9NA= github.com/btcsuite/btcwallet/wtxmgr v1.2.0/go.mod h1:h8hkcKUE3X7lMPzTUoGnNiw5g7VhGrKEW3KpR2r0VnY= diff --git a/htlcswitch/decayedlog.go b/htlcswitch/decayedlog.go index e9248aa04..93c2fd1b9 100644 --- a/htlcswitch/decayedlog.go +++ b/htlcswitch/decayedlog.go @@ -73,6 +73,7 @@ func NewDecayedLog(dbPath, dbFileName string, boltCfg *kvdb.BoltConfig, NoFreelistSync: true, AutoCompact: boltCfg.AutoCompact, AutoCompactMinAge: boltCfg.AutoCompactMinAge, + DBTimeout: boltCfg.DBTimeout, } // Use default path for log database diff --git a/keychain/interface_test.go b/keychain/interface_test.go index 2f3945779..654ad22ce 100644 --- a/keychain/interface_test.go +++ b/keychain/interface_test.go @@ -42,6 +42,9 @@ var ( 0x4f, 0x2f, 0x6f, 0x25, 0x98, 0xa3, 0xef, 0xb9, 0x69, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53, } + + // testDBTimeout is the wallet db timeout value used in this test. + testDBTimeout = time.Second * 10 ) func createTestBtcWallet(coinType uint32) (func(), *wallet.Wallet, error) { @@ -62,7 +65,9 @@ func createTestBtcWallet(coinType uint32) (func(), *wallet.Wallet, error) { if err != nil { return nil, nil, err } - loader := wallet.NewLoader(&chaincfg.SimNetParams, tempDir, true, 0) + loader := wallet.NewLoader( + &chaincfg.SimNetParams, tempDir, true, testDBTimeout, 0, + ) pass := []byte("test") diff --git a/lncfg/db.go b/lncfg/db.go index 8c9203145..e639ece1f 100644 --- a/lncfg/db.go +++ b/lncfg/db.go @@ -33,6 +33,7 @@ func DefaultDB() *DB { BatchCommitInterval: DefaultBatchCommitInterval, Bolt: &kvdb.BoltConfig{ AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge, + DBTimeout: kvdb.DefaultDBTimeout, }, } } @@ -95,6 +96,7 @@ func (db *DB) GetBackends(ctx context.Context, dbPath string, localDB, err = kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{ DBPath: dbPath, DBFileName: dbName, + DBTimeout: db.Bolt.DBTimeout, NoFreelistSync: !db.Bolt.SyncFreelist, AutoCompact: db.Bolt.AutoCompact, AutoCompactMinAge: db.Bolt.AutoCompactMinAge, diff --git a/lncfg/db_test.go b/lncfg/db_test.go new file mode 100644 index 000000000..5485ced1a --- /dev/null +++ b/lncfg/db_test.go @@ -0,0 +1,24 @@ +package lncfg_test + +import ( + "testing" + + "github.com/lightningnetwork/lnd/channeldb/kvdb" + "github.com/lightningnetwork/lnd/lncfg" + "github.com/stretchr/testify/require" +) + +// TestDBDefaultConfig tests that the default DB config is created as expected. +func TestDBDefaultConfig(t *testing.T) { + defaultConfig := lncfg.DefaultDB() + + require.Equal(t, lncfg.BoltBackend, defaultConfig.Backend) + require.Equal( + t, kvdb.DefaultBoltAutoCompactMinAge, + defaultConfig.Bolt.AutoCompactMinAge, + ) + require.Equal(t, kvdb.DefaultDBTimeout, defaultConfig.Bolt.DBTimeout) + // Implicitly, the following fields are default to false. + require.False(t, defaultConfig.Bolt.AutoCompact) + require.False(t, defaultConfig.Bolt.SyncFreelist) +} diff --git a/lnd.go b/lnd.go index a4580e5bf..4b91d8251 100644 --- a/lnd.go +++ b/lnd.go @@ -412,7 +412,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error { // Create the macaroon authentication/authorization service. macaroonService, err = macaroons.NewService( cfg.networkDir, "lnd", walletInitParams.StatelessInit, - macaroons.IPLockChecker, + cfg.DB.Bolt.DBTimeout, macaroons.IPLockChecker, ) if err != nil { err := fmt.Errorf("unable to set up macaroon "+ @@ -522,6 +522,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error { Birthday: walletInitParams.Birthday, RecoveryWindow: walletInitParams.RecoveryWindow, Wallet: walletInitParams.Wallet, + DBTimeOut: cfg.DB.Bolt.DBTimeout, NeutrinoCS: neutrinoCS, ActiveNetParams: cfg.ActiveNetParams, FeeURL: cfg.FeeURL, @@ -564,7 +565,9 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error { var towerClientDB *wtdb.ClientDB if cfg.WtClient.Active { var err error - towerClientDB, err = wtdb.OpenClientDB(cfg.localDatabaseDir()) + towerClientDB, err = wtdb.OpenClientDB( + cfg.localDatabaseDir(), cfg.DB.Bolt.DBTimeout, + ) if err != nil { err := fmt.Errorf("unable to open watchtower client "+ "database: %v", err) @@ -605,7 +608,9 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error { lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name), ) - towerDB, err := wtdb.OpenTowerDB(towerDBDir) + towerDB, err := wtdb.OpenTowerDB( + towerDBDir, cfg.DB.Bolt.DBTimeout, + ) if err != nil { err := fmt.Errorf("unable to open watchtower "+ "database: %v", err) @@ -1140,7 +1145,7 @@ func waitForWalletPassword(cfg *Config, restEndpoints []net.Addr, } pwService := walletunlocker.New( chainConfig.ChainDir, cfg.ActiveNetParams.Params, - !cfg.SyncFreelist, macaroonFiles, + !cfg.SyncFreelist, macaroonFiles, cfg.DB.Bolt.DBTimeout, ) // Set up a new PasswordService, which will listen for passwords @@ -1264,7 +1269,7 @@ func waitForWalletPassword(cfg *Config, restEndpoints []net.Addr, ) loader := wallet.NewLoader( cfg.ActiveNetParams.Params, netDir, !cfg.SyncFreelist, - recoveryWindow, + cfg.DB.Bolt.DBTimeout, recoveryWindow, ) // With the seed, we can now use the wallet loader to create @@ -1484,7 +1489,9 @@ func initNeutrinoBackend(cfg *Config, chainDir string) (*neutrino.ChainService, } dbName := filepath.Join(dbPath, "neutrino.db") - db, err := walletdb.Create("bdb", dbName, !cfg.SyncFreelist) + db, err := walletdb.Create( + "bdb", dbName, !cfg.SyncFreelist, cfg.DB.Bolt.DBTimeout, + ) if err != nil { return nil, nil, fmt.Errorf("unable to create neutrino "+ "database: %v", err) diff --git a/lnwallet/btcwallet/btcwallet.go b/lnwallet/btcwallet/btcwallet.go index 6adc7ebff..1ff5a7e7f 100644 --- a/lnwallet/btcwallet/btcwallet.go +++ b/lnwallet/btcwallet/btcwallet.go @@ -98,7 +98,7 @@ func New(cfg Config) (*BtcWallet, error) { } loader := base.NewLoader( cfg.NetParams, netDir, cfg.NoFreelistSync, - cfg.RecoveryWindow, + cfg.DBTimeOut, cfg.RecoveryWindow, ) walletExists, err := loader.WalletExists() if err != nil { diff --git a/lnwallet/btcwallet/config.go b/lnwallet/btcwallet/config.go index 2dedf4e3d..e3731e17c 100644 --- a/lnwallet/btcwallet/config.go +++ b/lnwallet/btcwallet/config.go @@ -80,6 +80,10 @@ type Config struct { // freelist to disk, resulting in improved performance at the expense of // increased startup time. NoFreelistSync bool + + // DBTimeOut specifies the timeout value to use when opening the wallet + // database. + DBTimeOut time.Duration } // NetworkDir returns the directory name of a network directory to hold wallet diff --git a/lnwallet/interface_test.go b/lnwallet/interface_test.go index 09cd9c30c..c37464ce5 100644 --- a/lnwallet/interface_test.go +++ b/lnwallet/interface_test.go @@ -3212,6 +3212,7 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver, // instance, and initialize a btcwallet driver for it. aliceDB, err := walletdb.Create( "bdb", tempTestDirAlice+"/neutrino.db", true, + kvdb.DefaultDBTimeout, ) if err != nil { t.Fatalf("unable to create DB: %v", err) @@ -3240,6 +3241,7 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver, // instance, and initialize a btcwallet driver for it. bobDB, err := walletdb.Create( "bdb", tempTestDirBob+"/neutrino.db", true, + kvdb.DefaultDBTimeout, ) if err != nil { t.Fatalf("unable to create DB: %v", err) diff --git a/macaroons/service.go b/macaroons/service.go index 9f2c01c9e..e8d9c164a 100644 --- a/macaroons/service.go +++ b/macaroons/service.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path" + "time" "github.com/lightningnetwork/lnd/channeldb/kvdb" "google.golang.org/grpc" @@ -76,7 +77,7 @@ type Service struct { // such as those for `allow`, `time-before`, `declared`, and `error` caveats // are registered automatically and don't need to be added. func NewService(dir, location string, statelessInit bool, - checks ...Checker) (*Service, error) { + dbTimeout time.Duration, checks ...Checker) (*Service, error) { // Ensure that the path to the directory exists. if _, err := os.Stat(dir); os.IsNotExist(err) { @@ -89,6 +90,7 @@ func NewService(dir, location string, statelessInit bool, // and all generated macaroons+caveats. macaroonDB, err := kvdb.Create( kvdb.BoltBackendName, path.Join(dir, DBFilename), true, + dbTimeout, ) if err != nil { return nil, err diff --git a/macaroons/service_test.go b/macaroons/service_test.go index 409d0614c..65199ed22 100644 --- a/macaroons/service_test.go +++ b/macaroons/service_test.go @@ -40,6 +40,7 @@ func setupTestRootKeyStorage(t *testing.T) string { } db, err := kvdb.Create( kvdb.BoltBackendName, path.Join(tempDir, "macaroons.db"), true, + kvdb.DefaultDBTimeout, ) if err != nil { t.Fatalf("Error opening store DB: %v", err) @@ -67,7 +68,8 @@ func TestNewService(t *testing.T) { // Second, create the new service instance, unlock it and pass in a // checker that we expect it to add to the bakery. service, err := macaroons.NewService( - tempDir, "lnd", false, macaroons.IPLockChecker, + tempDir, "lnd", false, kvdb.DefaultDBTimeout, + macaroons.IPLockChecker, ) if err != nil { t.Fatalf("Error creating new service: %v", err) @@ -118,7 +120,8 @@ func TestValidateMacaroon(t *testing.T) { tempDir := setupTestRootKeyStorage(t) defer os.RemoveAll(tempDir) service, err := macaroons.NewService( - tempDir, "lnd", false, macaroons.IPLockChecker, + tempDir, "lnd", false, kvdb.DefaultDBTimeout, + macaroons.IPLockChecker, ) if err != nil { t.Fatalf("Error creating new service: %v", err) @@ -178,7 +181,8 @@ func TestListMacaroonIDs(t *testing.T) { // Second, create the new service instance, unlock it and pass in a // checker that we expect it to add to the bakery. service, err := macaroons.NewService( - tempDir, "lnd", false, macaroons.IPLockChecker, + tempDir, "lnd", false, kvdb.DefaultDBTimeout, + macaroons.IPLockChecker, ) require.NoError(t, err, "Error creating new service") defer service.Close() @@ -210,7 +214,8 @@ func TestDeleteMacaroonID(t *testing.T) { // Second, create the new service instance, unlock it and pass in a // checker that we expect it to add to the bakery. service, err := macaroons.NewService( - tempDir, "lnd", false, macaroons.IPLockChecker, + tempDir, "lnd", false, kvdb.DefaultDBTimeout, + macaroons.IPLockChecker, ) require.NoError(t, err, "Error creating new service") defer service.Close() diff --git a/macaroons/store_test.go b/macaroons/store_test.go index b4427eb11..e8443c61e 100644 --- a/macaroons/store_test.go +++ b/macaroons/store_test.go @@ -42,6 +42,7 @@ func openTestStore(t *testing.T, tempDir string) (func(), db, err := kvdb.Create( kvdb.BoltBackendName, path.Join(tempDir, "weks.db"), true, + kvdb.DefaultDBTimeout, ) require.NoError(t, err) diff --git a/routing/chainview/interface_test.go b/routing/chainview/interface_test.go index e790cdaaa..56a09ecb8 100644 --- a/routing/chainview/interface_test.go +++ b/routing/chainview/interface_test.go @@ -27,6 +27,7 @@ import ( "github.com/lightninglabs/neutrino" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/channeldb/kvdb" ) var ( @@ -846,7 +847,9 @@ var interfaceImpls = []struct { } dbName := filepath.Join(spvDir, "neutrino.db") - spvDatabase, err := walletdb.Create("bdb", dbName, true) + spvDatabase, err := walletdb.Create( + "bdb", dbName, true, kvdb.DefaultDBTimeout, + ) if err != nil { return nil, nil, err } diff --git a/routing/integrated_routing_context_test.go b/routing/integrated_routing_context_test.go index 8cbeea539..3bad60857 100644 --- a/routing/integrated_routing_context_test.go +++ b/routing/integrated_routing_context_test.go @@ -105,7 +105,9 @@ func (c *integratedRoutingContext) testPayment(maxParts uint32) ([]htlcAttempt, dbPath := file.Name() defer os.Remove(dbPath) - db, err := kvdb.Open(kvdb.BoltBackendName, dbPath, true) + db, err := kvdb.Open( + kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout, + ) if err != nil { c.t.Fatal(err) } diff --git a/routing/missioncontrol_store_test.go b/routing/missioncontrol_store_test.go index 497affd52..c226a78e8 100644 --- a/routing/missioncontrol_store_test.go +++ b/routing/missioncontrol_store_test.go @@ -31,7 +31,9 @@ func TestMissionControlStore(t *testing.T) { dbPath := file.Name() - db, err := kvdb.Create(kvdb.BoltBackendName, dbPath, true) + db, err := kvdb.Create( + kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout, + ) if err != nil { t.Fatal(err) } diff --git a/routing/missioncontrol_test.go b/routing/missioncontrol_test.go index 3a9c93a75..001e7b036 100644 --- a/routing/missioncontrol_test.go +++ b/routing/missioncontrol_test.go @@ -63,7 +63,9 @@ func createMcTestContext(t *testing.T) *mcTestContext { ctx.dbPath = file.Name() - ctx.db, err = kvdb.Open(kvdb.BoltBackendName, ctx.dbPath, true) + ctx.db, err = kvdb.Open( + kvdb.BoltBackendName, ctx.dbPath, true, kvdb.DefaultDBTimeout, + ) if err != nil { t.Fatal(err) } diff --git a/sample-lnd.conf b/sample-lnd.conf index 93f96061a..a633a4951 100644 --- a/sample-lnd.conf +++ b/sample-lnd.conf @@ -991,3 +991,6 @@ litecoin.node=ltcd ; considered for auto compaction again. Can be set to 0 to compact on every ; startup. (default: 168h) ; db.bolt.auto-compact-min-age=0 + +; Specify the timeout to be used when opening the database. +; db.bolt.dbtimeout=60s diff --git a/walletunlocker/service.go b/walletunlocker/service.go index bea2e758b..1744a8f9d 100644 --- a/walletunlocker/service.go +++ b/walletunlocker/service.go @@ -129,11 +129,15 @@ type UnlockerService struct { // different access permissions. These might not exist in a stateless // initialization of lnd. macaroonFiles []string + + // dbTimeout specifies the timeout value to use when opening the wallet + // database. + dbTimeout time.Duration } // New creates and returns a new UnlockerService. func New(chainDir string, params *chaincfg.Params, noFreelistSync bool, - macaroonFiles []string) *UnlockerService { + macaroonFiles []string, dbTimeout time.Duration) *UnlockerService { return &UnlockerService{ InitMsgs: make(chan *WalletInitMsg, 1), @@ -145,6 +149,7 @@ func New(chainDir string, params *chaincfg.Params, noFreelistSync bool, chainDir: chainDir, netParams: params, macaroonFiles: macaroonFiles, + dbTimeout: dbTimeout, } } @@ -162,7 +167,9 @@ func (u *UnlockerService) GenSeed(_ context.Context, // Before we start, we'll ensure that the wallet hasn't already created // so we don't show a *new* seed to the user if one already exists. netDir := btcwallet.NetworkDir(u.chainDir, u.netParams) - loader := wallet.NewLoader(u.netParams, netDir, u.noFreelistSync, 0) + loader := wallet.NewLoader( + u.netParams, netDir, u.noFreelistSync, u.dbTimeout, 0, + ) walletExists, err := loader.WalletExists() if err != nil { return nil, err @@ -292,7 +299,8 @@ func (u *UnlockerService) InitWallet(ctx context.Context, // wallet's files so we can check if the wallet already exists. netDir := btcwallet.NetworkDir(u.chainDir, u.netParams) loader := wallet.NewLoader( - u.netParams, netDir, u.noFreelistSync, uint32(recoveryWindow), + u.netParams, netDir, u.noFreelistSync, + u.dbTimeout, uint32(recoveryWindow), ) walletExists, err := loader.WalletExists() @@ -368,7 +376,8 @@ func (u *UnlockerService) UnlockWallet(ctx context.Context, netDir := btcwallet.NetworkDir(u.chainDir, u.netParams) loader := wallet.NewLoader( - u.netParams, netDir, u.noFreelistSync, recoveryWindow, + u.netParams, netDir, u.noFreelistSync, + u.dbTimeout, recoveryWindow, ) // Check if wallet already exists. @@ -435,7 +444,9 @@ func (u *UnlockerService) ChangePassword(ctx context.Context, in *lnrpc.ChangePasswordRequest) (*lnrpc.ChangePasswordResponse, error) { netDir := btcwallet.NetworkDir(u.chainDir, u.netParams) - loader := wallet.NewLoader(u.netParams, netDir, u.noFreelistSync, 0) + loader := wallet.NewLoader( + u.netParams, netDir, u.noFreelistSync, u.dbTimeout, 0, + ) // First, we'll make sure the wallet exists for the specific chain and // network. @@ -513,7 +524,7 @@ func (u *UnlockerService) ChangePassword(ctx context.Context, // Attempt to open the macaroon DB, unlock it and then change // the passphrase. macaroonService, err := macaroons.NewService( - netDir, "lnd", in.StatelessInit, + netDir, "lnd", in.StatelessInit, u.dbTimeout, ) if err != nil { return nil, err diff --git a/walletunlocker/service_test.go b/walletunlocker/service_test.go index b32f9af85..f84df8572 100644 --- a/walletunlocker/service_test.go +++ b/walletunlocker/service_test.go @@ -69,7 +69,9 @@ func createTestWalletWithPw(t *testing.T, pubPw, privPw []byte, dir string, // Create a new test wallet that uses fast scrypt as KDF. netDir := btcwallet.NetworkDir(dir, netParams) - loader := wallet.NewLoader(netParams, netDir, true, 0) + loader := wallet.NewLoader( + netParams, netDir, true, kvdb.DefaultDBTimeout, 0, + ) _, err := loader.CreateNewWallet( pubPw, privPw, testSeed, time.Time{}, ) @@ -105,7 +107,7 @@ func openOrCreateTestMacStore(tempDir string, pw *[]byte, } db, err := kvdb.Create( kvdb.BoltBackendName, path.Join(netDir, macaroons.DBFilename), - true, + true, kvdb.DefaultDBTimeout, ) if err != nil { return nil, err @@ -144,7 +146,9 @@ func TestGenSeed(t *testing.T) { _ = os.RemoveAll(testDir) }() - service := walletunlocker.New(testDir, testNetParams, true, nil) + service := walletunlocker.New( + testDir, testNetParams, true, nil, kvdb.DefaultDBTimeout, + ) // Now that the service has been created, we'll ask it to generate a // new seed for us given a test passphrase. @@ -179,7 +183,9 @@ func TestGenSeedGenerateEntropy(t *testing.T) { defer func() { _ = os.RemoveAll(testDir) }() - service := walletunlocker.New(testDir, testNetParams, true, nil) + service := walletunlocker.New( + testDir, testNetParams, true, nil, kvdb.DefaultDBTimeout, + ) // Now that the service has been created, we'll ask it to generate a // new seed for us given a test passphrase. Note that we don't actually @@ -213,7 +219,9 @@ func TestGenSeedInvalidEntropy(t *testing.T) { defer func() { _ = os.RemoveAll(testDir) }() - service := walletunlocker.New(testDir, testNetParams, true, nil) + service := walletunlocker.New( + testDir, testNetParams, true, nil, kvdb.DefaultDBTimeout, + ) // Now that the service has been created, we'll ask it to generate a // new seed for us given a test passphrase. However, we'll be using an @@ -244,7 +252,9 @@ func TestInitWallet(t *testing.T) { }() // Create new UnlockerService. - service := walletunlocker.New(testDir, testNetParams, true, nil) + service := walletunlocker.New( + testDir, testNetParams, true, nil, kvdb.DefaultDBTimeout, + ) // Once we have the unlocker service created, we'll now instantiate a // new cipher seed and its mnemonic. @@ -330,7 +340,9 @@ func TestCreateWalletInvalidEntropy(t *testing.T) { }() // Create new UnlockerService. - service := walletunlocker.New(testDir, testNetParams, true, nil) + service := walletunlocker.New( + testDir, testNetParams, true, nil, kvdb.DefaultDBTimeout, + ) // We'll attempt to init the wallet with an invalid cipher seed and // passphrase. @@ -359,7 +371,9 @@ func TestUnlockWallet(t *testing.T) { }() // Create new UnlockerService. - service := walletunlocker.New(testDir, testNetParams, true, nil) + service := walletunlocker.New( + testDir, testNetParams, true, nil, kvdb.DefaultDBTimeout, + ) ctx := context.Background() req := &lnrpc.UnlockWalletRequest{ @@ -448,7 +462,9 @@ func TestChangeWalletPasswordNewRootkey(t *testing.T) { } // Create a new UnlockerService with our temp files. - service := walletunlocker.New(testDir, testNetParams, true, tempFiles) + service := walletunlocker.New( + testDir, testNetParams, true, tempFiles, kvdb.DefaultDBTimeout, + ) ctx := context.Background() newPassword := []byte("hunter2???") @@ -558,7 +574,7 @@ func TestChangeWalletPasswordStateless(t *testing.T) { // Create a new UnlockerService with our temp files. service := walletunlocker.New(testDir, testNetParams, true, []string{ tempMacFile, nonExistingFile, - }) + }, kvdb.DefaultDBTimeout) // Create a wallet we can try to unlock. We use the default password // so we can check that the unlocker service defaults to this when diff --git a/watchtower/wtdb/client_db.go b/watchtower/wtdb/client_db.go index bf150b109..60dd040f0 100644 --- a/watchtower/wtdb/client_db.go +++ b/watchtower/wtdb/client_db.go @@ -6,6 +6,7 @@ import ( "fmt" "math" "net" + "time" "github.com/btcsuite/btcd/btcec" "github.com/lightningnetwork/lnd/channeldb/kvdb" @@ -129,8 +130,10 @@ type ClientDB struct { // migrations will be applied before returning. Any attempt to open a database // with a version number higher that the latest version will fail to prevent // accidental reversion. -func OpenClientDB(dbPath string) (*ClientDB, error) { - bdb, firstInit, err := createDBIfNotExist(dbPath, clientDBName) +func OpenClientDB(dbPath string, dbTimeout time.Duration) (*ClientDB, error) { + bdb, firstInit, err := createDBIfNotExist( + dbPath, clientDBName, dbTimeout, + ) if err != nil { return nil, err } diff --git a/watchtower/wtdb/client_db_test.go b/watchtower/wtdb/client_db_test.go index 81a1b684f..07d2d53c4 100644 --- a/watchtower/wtdb/client_db_test.go +++ b/watchtower/wtdb/client_db_test.go @@ -11,6 +11,7 @@ import ( "testing" "github.com/btcsuite/btcd/btcec" + "github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/watchtower/blob" "github.com/lightningnetwork/lnd/watchtower/wtclient" @@ -783,7 +784,9 @@ func TestClientDB(t *testing.T) { err) } - db, err := wtdb.OpenClientDB(path) + db, err := wtdb.OpenClientDB( + path, kvdb.DefaultDBTimeout, + ) if err != nil { os.RemoveAll(path) t.Fatalf("unable to open db: %v", err) @@ -806,14 +809,18 @@ func TestClientDB(t *testing.T) { err) } - db, err := wtdb.OpenClientDB(path) + db, err := wtdb.OpenClientDB( + path, kvdb.DefaultDBTimeout, + ) if err != nil { os.RemoveAll(path) t.Fatalf("unable to open db: %v", err) } db.Close() - db, err = wtdb.OpenClientDB(path) + db, err = wtdb.OpenClientDB( + path, kvdb.DefaultDBTimeout, + ) if err != nil { os.RemoveAll(path) t.Fatalf("unable to reopen db: %v", err) diff --git a/watchtower/wtdb/db_common.go b/watchtower/wtdb/db_common.go index c4f8bb17b..7f4e981aa 100644 --- a/watchtower/wtdb/db_common.go +++ b/watchtower/wtdb/db_common.go @@ -5,6 +5,7 @@ import ( "errors" "os" "path/filepath" + "time" "github.com/lightningnetwork/lnd/channeldb/kvdb" ) @@ -49,7 +50,9 @@ func fileExists(path string) bool { // one doesn't exist. The boolean returned indicates if the database did not // exist before, or if it has been created but no version metadata exists within // it. -func createDBIfNotExist(dbPath, name string) (kvdb.Backend, bool, error) { +func createDBIfNotExist(dbPath, name string, + dbTimeout time.Duration) (kvdb.Backend, bool, error) { + path := filepath.Join(dbPath, name) // If the database file doesn't exist, this indicates we much initialize @@ -65,7 +68,9 @@ func createDBIfNotExist(dbPath, name string) (kvdb.Backend, bool, error) { // Specify bbolt freelist options to reduce heap pressure in case the // freelist grows to be very large. - bdb, err := kvdb.Create(kvdb.BoltBackendName, path, true) + bdb, err := kvdb.Create( + kvdb.BoltBackendName, path, true, dbTimeout, + ) if err != nil { return nil, false, err } diff --git a/watchtower/wtdb/tower_db.go b/watchtower/wtdb/tower_db.go index f3ba6ef53..8fc3925f0 100644 --- a/watchtower/wtdb/tower_db.go +++ b/watchtower/wtdb/tower_db.go @@ -3,6 +3,7 @@ package wtdb import ( "bytes" "errors" + "time" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/lightningnetwork/lnd/chainntnfs" @@ -66,8 +67,10 @@ type TowerDB struct { // migrations will be applied before returning. Any attempt to open a database // with a version number higher that the latest version will fail to prevent // accidental reversion. -func OpenTowerDB(dbPath string) (*TowerDB, error) { - bdb, firstInit, err := createDBIfNotExist(dbPath, towerDBName) +func OpenTowerDB(dbPath string, dbTimeout time.Duration) (*TowerDB, error) { + bdb, firstInit, err := createDBIfNotExist( + dbPath, towerDBName, dbTimeout, + ) if err != nil { return nil, err } diff --git a/watchtower/wtdb/tower_db_test.go b/watchtower/wtdb/tower_db_test.go index c408e7ef9..e02d2fd43 100644 --- a/watchtower/wtdb/tower_db_test.go +++ b/watchtower/wtdb/tower_db_test.go @@ -10,6 +10,7 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/lightningnetwork/lnd/chainntnfs" + "github.com/lightningnetwork/lnd/channeldb/kvdb" "github.com/lightningnetwork/lnd/watchtower" "github.com/lightningnetwork/lnd/watchtower/blob" "github.com/lightningnetwork/lnd/watchtower/wtdb" @@ -642,7 +643,9 @@ func TestTowerDB(t *testing.T) { err) } - db, err := wtdb.OpenTowerDB(path) + db, err := wtdb.OpenTowerDB( + path, kvdb.DefaultDBTimeout, + ) if err != nil { os.RemoveAll(path) t.Fatalf("unable to open db: %v", err) @@ -665,7 +668,9 @@ func TestTowerDB(t *testing.T) { err) } - db, err := wtdb.OpenTowerDB(path) + db, err := wtdb.OpenTowerDB( + path, kvdb.DefaultDBTimeout, + ) if err != nil { os.RemoveAll(path) t.Fatalf("unable to open db: %v", err) @@ -675,7 +680,9 @@ func TestTowerDB(t *testing.T) { // Open the db again, ensuring we test a // different path during open and that all // buckets remain initialized. - db, err = wtdb.OpenTowerDB(path) + db, err = wtdb.OpenTowerDB( + path, kvdb.DefaultDBTimeout, + ) if err != nil { os.RemoveAll(path) t.Fatalf("unable to open db: %v", err)