wallet: support for external wallet DB

This commit is contained in:
Andras Banki-Horvath 2021-04-27 22:18:50 +02:00
parent 50978fcf79
commit a795db6b12
No known key found for this signature in database
GPG key ID: 80E5375C094198D8

View file

@ -6,6 +6,7 @@ package wallet
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
@ -55,6 +56,8 @@ type Loader struct {
timeout time.Duration timeout time.Duration
recoveryWindow uint32 recoveryWindow uint32
wallet *Wallet wallet *Wallet
localDB bool
walletExists func() (bool, error)
db walletdb.DB db walletdb.DB
mu sync.Mutex mu sync.Mutex
} }
@ -72,18 +75,42 @@ func NewLoader(chainParams *chaincfg.Params, dbDirPath string,
noFreelistSync: noFreelistSync, noFreelistSync: noFreelistSync,
timeout: timeout, timeout: timeout,
recoveryWindow: recoveryWindow, recoveryWindow: recoveryWindow,
localDB: true,
} }
} }
// NewLoaderWithDB constructs a Loader with an externally provided DB. This way
// users are free to use their own walletdb implementation (eg. leveldb, etcd)
// to store the wallet. Given that the external DB may be shared an additional
// function is also passed which will override Loader.WalletExists().
func NewLoaderWithDB(chainParams *chaincfg.Params, recoveryWindow uint32,
db walletdb.DB, walletExists func() (bool, error)) (*Loader, error) {
if db == nil {
return nil, fmt.Errorf("no DB provided")
}
if walletExists == nil {
return nil, fmt.Errorf("unable to check if wallet exists")
}
return &Loader{
chainParams: chainParams,
recoveryWindow: recoveryWindow,
localDB: false,
walletExists: walletExists,
db: db,
}, nil
}
// onLoaded executes each added callback and prevents loader from loading any // onLoaded executes each added callback and prevents loader from loading any
// additional wallets. Requires mutex to be locked. // additional wallets. Requires mutex to be locked.
func (l *Loader) onLoaded(w *Wallet, db walletdb.DB) { func (l *Loader) onLoaded(w *Wallet) {
for _, fn := range l.callbacks { for _, fn := range l.callbacks {
fn(w) fn(w)
} }
l.wallet = w l.wallet = w
l.db = db
l.callbacks = nil // not needed anymore l.callbacks = nil // not needed anymore
} }
@ -134,8 +161,7 @@ func (l *Loader) createNewWallet(pubPassphrase, privPassphrase,
return nil, ErrLoaded return nil, ErrLoaded
} }
dbPath := filepath.Join(l.dbDirPath, WalletDBName) exists, err := l.WalletExists()
exists, err := fileExists(dbPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -143,25 +169,34 @@ func (l *Loader) createNewWallet(pubPassphrase, privPassphrase,
return nil, ErrExists return nil, ErrExists
} }
// Create the wallet database backed by bolt db. if l.localDB {
err = os.MkdirAll(l.dbDirPath, 0700) dbPath := filepath.Join(l.dbDirPath, WalletDBName)
if err != nil {
return nil, err // Create the wallet database backed by bolt db.
} err = os.MkdirAll(l.dbDirPath, 0700)
db, err := walletdb.Create("bdb", dbPath, l.noFreelistSync, l.timeout) if err != nil {
if err != nil { return nil, err
return nil, err }
l.db, err = walletdb.Create(
"bdb", dbPath, l.noFreelistSync, l.timeout,
)
if err != nil {
return nil, err
}
} }
// Initialize the newly created database for the wallet before opening. // Initialize the newly created database for the wallet before opening.
if isWatchingOnly { if isWatchingOnly {
err = CreateWatchingOnly(db, pubPassphrase, l.chainParams, bday) err := CreateWatchingOnly(
l.db, pubPassphrase, l.chainParams, bday,
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else { } else {
err = Create( err := Create(
db, pubPassphrase, privPassphrase, seed, l.chainParams, bday, l.db, pubPassphrase, privPassphrase, seed,
l.chainParams, bday,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -169,13 +204,13 @@ func (l *Loader) createNewWallet(pubPassphrase, privPassphrase,
} }
// Open the newly-created wallet. // Open the newly-created wallet.
w, err := Open(db, pubPassphrase, nil, l.chainParams, l.recoveryWindow) w, err := Open(l.db, pubPassphrase, nil, l.chainParams, l.recoveryWindow)
if err != nil { if err != nil {
return nil, err return nil, err
} }
w.Start() w.Start()
l.onLoaded(w, db) l.onLoaded(w)
return w, nil return w, nil
} }
@ -197,17 +232,22 @@ func (l *Loader) OpenExistingWallet(pubPassphrase []byte, canConsolePrompt bool)
return nil, ErrLoaded return nil, ErrLoaded
} }
// Ensure that the network directory exists. if l.localDB {
if err := checkCreateDir(l.dbDirPath); err != nil { var err error
return nil, err // Ensure that the network directory exists.
} if err = checkCreateDir(l.dbDirPath); err != nil {
return nil, err
}
// Open the database using the boltdb backend. // Open the database using the boltdb backend.
dbPath := filepath.Join(l.dbDirPath, WalletDBName) dbPath := filepath.Join(l.dbDirPath, WalletDBName)
db, err := walletdb.Open("bdb", dbPath, l.noFreelistSync, l.timeout) l.db, err = walletdb.Open(
if err != nil { "bdb", dbPath, l.noFreelistSync, l.timeout,
log.Errorf("Failed to open database: %v", err) )
return nil, err if err != nil {
log.Errorf("Failed to open database: %v", err)
return nil, err
}
} }
var cbs *waddrmgr.OpenCallbacks var cbs *waddrmgr.OpenCallbacks
@ -222,28 +262,35 @@ func (l *Loader) OpenExistingWallet(pubPassphrase []byte, canConsolePrompt bool)
ObtainPrivatePass: noConsole, ObtainPrivatePass: noConsole,
} }
} }
w, err := Open(db, pubPassphrase, cbs, l.chainParams, l.recoveryWindow) w, err := Open(l.db, pubPassphrase, cbs, l.chainParams, l.recoveryWindow)
if err != nil { if err != nil {
// If opening the wallet fails (e.g. because of wrong // If opening the wallet fails (e.g. because of wrong
// passphrase), we must close the backing database to // passphrase), we must close the backing database to
// allow future calls to walletdb.Open(). // allow future calls to walletdb.Open().
e := db.Close() if l.localDB {
if e != nil { e := l.db.Close()
log.Warnf("Error closing database: %v", e) if e != nil {
log.Warnf("Error closing database: %v", e)
}
} }
return nil, err return nil, err
} }
w.Start() w.Start()
l.onLoaded(w, db) l.onLoaded(w)
return w, nil return w, nil
} }
// WalletExists returns whether a file exists at the loader's database path. // WalletExists returns whether a file exists at the loader's database path.
// This may return an error for unexpected I/O failures. // This may return an error for unexpected I/O failures.
func (l *Loader) WalletExists() (bool, error) { func (l *Loader) WalletExists() (bool, error) {
dbPath := filepath.Join(l.dbDirPath, WalletDBName) if l.localDB {
return fileExists(dbPath) dbPath := filepath.Join(l.dbDirPath, WalletDBName)
return fileExists(dbPath)
}
return l.walletExists()
} }
// LoadedWallet returns the loaded wallet, if any, and a bool for whether the // LoadedWallet returns the loaded wallet, if any, and a bool for whether the
@ -270,9 +317,11 @@ func (l *Loader) UnloadWallet() error {
l.wallet.Stop() l.wallet.Stop()
l.wallet.WaitForShutdown() l.wallet.WaitForShutdown()
err := l.db.Close() if l.localDB {
if err != nil { err := l.db.Close()
return err if err != nil {
return err
}
} }
l.wallet = nil l.wallet = nil