wallet: use db timeout in loader

This commit is contained in:
yyforyongyu 2020-08-12 03:53:04 +08:00
parent 368301866e
commit 86bc349c6e
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868
3 changed files with 16 additions and 5 deletions

View file

@ -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.
@ -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)

View file

@ -47,6 +47,7 @@ type Loader struct {
chainParams *chaincfg.Params
dbDirPath string
noFreelistSync bool
timeout time.Duration
recoveryWindow uint32
wallet *Wallet
db walletdb.DB
@ -57,12 +58,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,
}
}
@ -140,7 +143,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
}
@ -196,7 +199,7 @@ 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)
db, err := walletdb.Open("bdb", dbPath, l.noFreelistSync, l.timeout)
if err != nil {
log.Errorf("Failed to open database: %v", err)
return nil, err

View file

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