Browse Source

kvdb: add timeout options for bbolt (#4787)

* mod: bump btcwallet version to accept db timeout

* btcwallet: add DBTimeOut in config

* kvdb: add database timeout option for bbolt

This commit adds a DBTimeout option in bbolt config. The relevant
functions walletdb.Open/Create are updated to use this config. In
addition, the bolt compacter also applies the new timeout option.

* channeldb: add DBTimeout in db options

This commit adds the DBTimeout option for channeldb. A new unit
test file is created to test the default options. In addition,
the params used in kvdb.Create inside channeldb_test is updated
with a DefaultDBTimeout value.

* contractcourt+routing: use DBTimeout in kvdb

This commit touches multiple test files in contractcourt and routing.
The call of function kvdb.Create and kvdb.Open are now updated with
the new param DBTimeout, using the default value kvdb.DefaultDBTimeout.

* lncfg: add DBTimeout option in db config

The DBTimeout option is added to db config. A new unit test is
added to check the default DB config is created as expected.

* migration: add DBTimeout param in kvdb.Create/kvdb.Open

* keychain: update tests to use DBTimeout param

* htlcswitch+chainreg: add DBTimeout option

* macaroons: support DBTimeout config in creation

This commit adds the DBTimeout during the creation of macaroons.db.
The usage of kvdb.Create and kvdb.Open in its tests are updated with
a timeout value using kvdb.DefaultDBTimeout.

* walletunlocker: add dbTimeout option in UnlockerService

This commit adds a new param, dbTimeout, during the creation of
UnlockerService. This param is then passed to wallet.NewLoader
inside various service calls, specifying a timeout value to be
used when opening the bbolt. In addition, the macaroonService
is also called with this dbTimeout param.

* watchtower/wtdb: add dbTimeout param during creation

This commit adds the dbTimeout param for the creation of both
watchtower.db and wtclient.db.

* multi: add db timeout param for walletdb.Create

This commit adds the db timeout param for the function call
walletdb.Create. It touches only the test files found in chainntnfs,
lnwallet, and routing.

* lnd: pass DBTimeout config to relevant services

This commit enables lnd to pass the DBTimeout config to the following
services/config/functions,
  - chainControlConfig
  - walletunlocker
  - wallet.NewLoader
  - macaroons
  - watchtower
In addition, the usage of wallet.Create is updated too.

* sample-config: add dbtimeout option
master
Yong 3 years ago committed by GitHub
parent
commit
582b164c46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      chainntnfs/test_utils.go
  2. 5
      chainreg/chainregistry.go
  3. 1
      channeldb/db.go
  4. 4
      channeldb/forwarding_package_test.go
  5. 20
      channeldb/kvdb/backend.go
  6. 9
      channeldb/kvdb/bolt_compact.go
  7. 6
      channeldb/kvdb/config.go
  8. 9
      channeldb/migration_01_to_11/db.go
  9. 11
      channeldb/migration_01_to_11/options.go
  10. 4
      channeldb/migtest/migtest.go
  11. 1
      channeldb/options.go
  12. 28
      channeldb/options_test.go
  13. 5
      contractcourt/briefcase_test.go
  14. 5
      contractcourt/channel_arbitrator_test.go
  15. 4
      go.mod
  16. 4
      go.sum
  17. 1
      htlcswitch/decayedlog.go
  18. 7
      keychain/interface_test.go
  19. 2
      lncfg/db.go
  20. 24
      lncfg/db_test.go
  21. 19
      lnd.go
  22. 2
      lnwallet/btcwallet/btcwallet.go
  23. 4
      lnwallet/btcwallet/config.go
  24. 2
      lnwallet/interface_test.go
  25. 4
      macaroons/service.go
  26. 13
      macaroons/service_test.go
  27. 1
      macaroons/store_test.go
  28. 5
      routing/chainview/interface_test.go
  29. 4
      routing/integrated_routing_context_test.go
  30. 4
      routing/missioncontrol_store_test.go
  31. 4
      routing/missioncontrol_test.go
  32. 3
      sample-lnd.conf
  33. 23
      walletunlocker/service.go
  34. 36
      walletunlocker/service_test.go
  35. 7
      watchtower/wtdb/client_db.go
  36. 13
      watchtower/wtdb/client_db_test.go
  37. 9
      watchtower/wtdb/db_common.go
  38. 7
      watchtower/wtdb/tower_db.go
  39. 13
      watchtower/wtdb/tower_db_test.go

5
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)

5
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

1
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

4
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)
}

20
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

9
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)

6
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.

9
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
}

11
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,
}
}

4
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
}

1
channeldb/options.go

@ -50,6 +50,7 @@ func DefaultOptions() Options {
NoFreelistSync: true,
AutoCompact: false,
AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge,
DBTimeout: kvdb.DefaultDBTimeout,
},
RejectCacheSize: DefaultRejectCacheSize,
ChannelCacheSize: DefaultChannelCacheSize,

28
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,
)
}

5
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
}

5
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
}

4
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

4
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=

1
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

7
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")

2
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,

24
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)
}

19
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)

2
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 {

4
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

2
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)

4
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

13
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()

1
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)

5
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
}

4
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)
}

4
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)
}

4
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)
}

3
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

23
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

36
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

7
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
}

13
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)

9
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
}

7
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
}

13
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)

Loading…
Cancel
Save