Skip to content

Commit

Permalink
Merge pull request btcsuite#714 from yyforyongyu/add-bdb-timeout
Browse files Browse the repository at this point in the history
bdb: Add timeout options for bbolt
  • Loading branch information
Roasbeef committed Nov 19, 2020
2 parents 5ee84d8 + e34b43b commit 664f77d
Show file tree
Hide file tree
Showing 18 changed files with 204 additions and 70 deletions.
10 changes: 7 additions & 3 deletions btcwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func walletMain() error {
}

dbDir := networkDir(cfg.AppDataDir.Value, activeNet.Params)
loader := wallet.NewLoader(activeNet.Params, dbDir, true, 250)
loader := wallet.NewLoader(
activeNet.Params, dbDir, true, cfg.DBTimeout, 250,
)

// Create and start HTTP server to serve wallet client connections.
// This will be updated with the wallet and chain server RPC client
Expand Down Expand Up @@ -160,8 +162,10 @@ func rpcClientConnectLoop(legacyRPCServer *legacyrpc.Server, loader *wallet.Load
spvdb walletdb.DB
)
netDir := networkDir(cfg.AppDataDir.Value, activeNet.Params)
spvdb, err = walletdb.Create("bdb",
filepath.Join(netDir, "neutrino.db"), true)
spvdb, err = walletdb.Create(
"bdb", filepath.Join(netDir, "neutrino.db"),
true, cfg.DBTimeout,
)
defer spvdb.Close()
if err != nil {
log.Errorf("Unable to create Neutrino DB: %s", err)
Expand Down
19 changes: 12 additions & 7 deletions cmd/dropwtxmgr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/wallet"
Expand All @@ -19,16 +20,20 @@ import (

const defaultNet = "mainnet"

var datadir = btcutil.AppDataDir("btcwallet", false)
var (
datadir = btcutil.AppDataDir("btcwallet", false)
)

// Flags.
var opts = struct {
Force bool `short:"f" description:"Force removal without prompt"`
DbPath string `long:"db" description:"Path to wallet database"`
DropLabels bool `long:"droplabels" description:"Drop transaction labels"`
Force bool `short:"f" description:"Force removal without prompt"`
DbPath string `long:"db" description:"Path to wallet database"`
DropLabels bool `long:"droplabels" description:"Drop transaction labels"`
Timeout time.Duration `long:"timeout" description:"Timeout value when opening the wallet database"`
}{
Force: false,
DbPath: filepath.Join(datadir, defaultNet, "wallet.db"),
Force: false,
DbPath: filepath.Join(datadir, defaultNet, wallet.WalletDBName),
Timeout: wallet.DefaultDBTimeout,
}

func init() {
Expand Down Expand Up @@ -93,7 +98,7 @@ func mainInt() int {
fmt.Println("Enter yes or no.")
}

db, err := walletdb.Open("bdb", opts.DbPath, true)
db, err := walletdb.Open("bdb", opts.DbPath, true, opts.Timeout)
if err != nil {
fmt.Println("Failed to open database:", err)
return 1
Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ const (
defaultLogFilename = "btcwallet.log"
defaultRPCMaxClients = 10
defaultRPCMaxWebsockets = 25

walletDbName = "wallet.db"
)

var (
Expand All @@ -58,6 +56,7 @@ type config struct {
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"`
LogDir string `long:"logdir" description:"Directory to log output."`
Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"`
DBTimeout time.Duration `long:"dbtimeout" description:"The timeout value to use when opening the wallet database."`

// Wallet options
WalletPass string `long:"walletpass" default-mask:"-" description:"The public wallet password -- Only required if the wallet was created with one"`
Expand Down Expand Up @@ -273,6 +272,7 @@ func loadConfig() (*config, []string, error) {
MaxPeers: neutrino.MaxPeers,
BanDuration: neutrino.BanDuration,
BanThreshold: neutrino.BanThreshold,
DBTimeout: wallet.DefaultDBTimeout,
}

// Pre-parse the command line options to see if an alternative config
Expand Down Expand Up @@ -415,7 +415,7 @@ func loadConfig() (*config, []string, error) {

// Ensure the wallet exists or create it when the create flag is set.
netDir := networkDir(cfg.AppDataDir.Value, activeNet.Params)
dbPath := filepath.Join(netDir, walletDbName)
dbPath := filepath.Join(netDir, wallet.WalletDBName)

if cfg.CreateTemp && cfg.Create {
err := fmt.Errorf("The flags --create and --createtemp can not " +
Expand Down
8 changes: 6 additions & 2 deletions waddrmgr/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ var (
// expectedInternalAddrs is the list of expected internal addresses
// generated from the seed
expectedInternalAddrs = expectedAddrs[5:]

// defaultDBTimeout specifies the timeout value when opening the wallet
// database.
defaultDBTimeout = 10 * time.Second
)

// checkManagerError ensures the passed error is a ManagerError with an error
Expand Down Expand Up @@ -238,7 +242,7 @@ func emptyDB(t *testing.T) (tearDownFunc func(), db walletdb.DB) {
t.Fatalf("Failed to create db temp dir: %v", err)
}
dbPath := filepath.Join(dirName, "mgrtest.db")
db, err = walletdb.Create("bdb", dbPath, true)
db, err = walletdb.Create("bdb", dbPath, true, defaultDBTimeout)
if err != nil {
_ = os.RemoveAll(dirName)
t.Fatalf("createDbNamespace: unexpected error: %v", err)
Expand All @@ -259,7 +263,7 @@ func setupManager(t *testing.T) (tearDownFunc func(), db walletdb.DB, mgr *Manag
t.Fatalf("Failed to create db temp dir: %v", err)
}
dbPath := filepath.Join(dirName, "mgrtest.db")
db, err = walletdb.Create("bdb", dbPath, true)
db, err = walletdb.Create("bdb", dbPath, true, defaultDBTimeout)
if err != nil {
_ = os.RemoveAll(dirName)
t.Fatalf("createDbNamespace: unexpected error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion waddrmgr/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ func testConvertWatchingOnly(tc *testContext) bool {
defer os.Remove(woMgrName)

// Open the new database copy and get the address manager namespace.
db, err := walletdb.Open("bdb", woMgrName, true)
db, err := walletdb.Open("bdb", woMgrName, true, defaultDBTimeout)
if err != nil {
tc.t.Errorf("openDbNamespace: unexpected error: %v", err)
return false
Expand Down
8 changes: 7 additions & 1 deletion wallet/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/btcsuite/btcutil/hdkeychain"
)

// defaultDBTimeout specifies the timeout value when opening the wallet
// database.
var defaultDBTimeout = 10 * time.Second

// testWallet creates a test wallet and unlocks it.
func testWallet(t *testing.T) (*Wallet, func()) {
// Set up a wallet.
Expand All @@ -32,7 +36,9 @@ func testWallet(t *testing.T) (*Wallet, func()) {
pubPass := []byte("hello")
privPass := []byte("world")

loader := NewLoader(&chaincfg.TestNet3Params, dir, true, 250)
loader := NewLoader(
&chaincfg.TestNet3Params, dir, true, defaultDBTimeout, 250,
)
w, err := loader.CreateNewWallet(pubPass, privPass, seed, time.Now())
if err != nil {
t.Fatalf("unable to create wallet: %v", err)
Expand Down
22 changes: 15 additions & 7 deletions wallet/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import (
)

const (
walletDbName = "wallet.db"
// WalletDBName specified the database filename for the wallet.
WalletDBName = "wallet.db"

// DefaultDBTimeout is the default timeout value when opening the wallet
// database.
DefaultDBTimeout = 60 * time.Second
)

var (
Expand Down Expand Up @@ -47,6 +52,7 @@ type Loader struct {
chainParams *chaincfg.Params
dbDirPath string
noFreelistSync bool
timeout time.Duration
recoveryWindow uint32
wallet *Wallet
db walletdb.DB
Expand All @@ -57,12 +63,14 @@ type Loader struct {
// recovery window is non-zero, the wallet will attempt to recovery addresses
// starting from the last SyncedTo height.
func NewLoader(chainParams *chaincfg.Params, dbDirPath string,
noFreelistSync bool, recoveryWindow uint32) *Loader {
noFreelistSync bool, timeout time.Duration,
recoveryWindow uint32) *Loader {

return &Loader{
chainParams: chainParams,
dbDirPath: dbDirPath,
noFreelistSync: noFreelistSync,
timeout: timeout,
recoveryWindow: recoveryWindow,
}
}
Expand Down Expand Up @@ -126,7 +134,7 @@ func (l *Loader) createNewWallet(pubPassphrase, privPassphrase,
return nil, ErrLoaded
}

dbPath := filepath.Join(l.dbDirPath, walletDbName)
dbPath := filepath.Join(l.dbDirPath, WalletDBName)
exists, err := fileExists(dbPath)
if err != nil {
return nil, err
Expand All @@ -140,7 +148,7 @@ func (l *Loader) createNewWallet(pubPassphrase, privPassphrase,
if err != nil {
return nil, err
}
db, err := walletdb.Create("bdb", dbPath, l.noFreelistSync)
db, err := walletdb.Create("bdb", dbPath, l.noFreelistSync, l.timeout)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -195,8 +203,8 @@ func (l *Loader) OpenExistingWallet(pubPassphrase []byte, canConsolePrompt bool)
}

// Open the database using the boltdb backend.
dbPath := filepath.Join(l.dbDirPath, walletDbName)
db, err := walletdb.Open("bdb", dbPath, l.noFreelistSync)
dbPath := filepath.Join(l.dbDirPath, WalletDBName)
db, err := walletdb.Open("bdb", dbPath, l.noFreelistSync, l.timeout)
if err != nil {
log.Errorf("Failed to open database: %v", err)
return nil, err
Expand Down Expand Up @@ -234,7 +242,7 @@ func (l *Loader) OpenExistingWallet(pubPassphrase []byte, canConsolePrompt bool)
// WalletExists returns whether a file exists at the loader's database path.
// This may return an error for unexpected I/O failures.
func (l *Loader) WalletExists() (bool, error) {
dbPath := filepath.Join(l.dbDirPath, walletDbName)
dbPath := filepath.Join(l.dbDirPath, WalletDBName)
return fileExists(dbPath)
}

Expand Down
4 changes: 3 additions & 1 deletion wallet/watchingonly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ func TestCreateWatchingOnly(t *testing.T) {

pubPass := []byte("hello")

loader := NewLoader(&chaincfg.TestNet3Params, dir, true, 250)
loader := NewLoader(
&chaincfg.TestNet3Params, dir, true, defaultDBTimeout, 250,
)
_, err = loader.CreateNewWatchingOnlyWallet(pubPass, time.Now())
if err != nil {
t.Fatalf("unable to create wallet: %v", err)
Expand Down
11 changes: 6 additions & 5 deletions walletdb/bdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ datastore. Package bdb is licensed under the copyfree ISC license.
## Usage

This package is only a driver to the walletdb package and provides the database
type of "bdb". The only parameters the Open and Create functions take is the
database path as a string, and an option for the database to not sync its
freelist to disk as a bool:
type of "bdb". The only parameters the Open and Create functions take are the
database path as a string, an option for the database to not sync its freelist
to disk as a bool, and a timeout value for opening the database as a
time.Duration:

```Go
db, err := walletdb.Open("bdb", "path/to/database.db", true)
db, err := walletdb.Open("bdb", "path/to/database.db", true, 60*time.Second)
if err != nil {
// Handle error
}
```

```Go
db, err := walletdb.Create("bdb", "path/to/database.db", true)
db, err := walletdb.Create("bdb", "path/to/database.db", true, 60*time.Second)
if err != nil {
// Handle error
}
Expand Down
6 changes: 5 additions & 1 deletion walletdb/bdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package bdb
import (
"io"
"os"
"time"

"github.com/btcsuite/btcwallet/walletdb"
"go.etcd.io/bbolt"
Expand Down Expand Up @@ -367,7 +368,9 @@ func fileExists(name string) bool {

// openDB opens the database at the provided path. walletdb.ErrDbDoesNotExist
// is returned if the database doesn't exist and the create flag is not set.
func openDB(dbPath string, noFreelistSync bool, create bool) (walletdb.DB, error) {
func openDB(dbPath string, noFreelistSync bool,
create bool, timeout time.Duration) (walletdb.DB, error) {

if !create && !fileExists(dbPath) {
return nil, walletdb.ErrDbDoesNotExist
}
Expand All @@ -377,6 +380,7 @@ func openDB(dbPath string, noFreelistSync bool, create bool) (walletdb.DB, error
options := &bbolt.Options{
NoFreelistSync: noFreelistSync,
FreelistType: bbolt.FreelistMapType,
Timeout: timeout,
}

boltDB, err := bbolt.Open(dbPath, 0600, options)
Expand Down
11 changes: 6 additions & 5 deletions walletdb/bdb/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ datastore.
Usage
This package is only a driver to the walletdb package and provides the database
type of "bdb". The only parameters the Open and Create functions take is the
database path as a string, and an option for the database to not sync its
freelist to disk as a bool:
type of "bdb". The only parameters the Open and Create functions take are the
database path as a string, an option for the database to not sync its freelist
to disk as a bool, and a timeout value for opening the database as a
time.Duration:
db, err := walletdb.Open("bdb", "path/to/database.db", true)
db, err := walletdb.Open("bdb", "path/to/database.db", true, 60*time.Second)
if err != nil {
// Handle error
}
db, err := walletdb.Create("bdb", "path/to/database.db", true)
db, err := walletdb.Create("bdb", "path/to/database.db", true, 60*time.Second)
if err != nil {
// Handle error
}
Expand Down

0 comments on commit 664f77d

Please sign in to comment.