diff --git a/config.go b/config.go index e035a132b9..45c5b32573 100644 --- a/config.go +++ b/config.go @@ -26,6 +26,8 @@ import ( "github.com/btcsuite/btcutil" "github.com/btcsuite/btcwallet/legacy/keystore" + "github.com/btcsuite/btcwallet/waddrmgr" + "github.com/btcsuite/btcwallet/walletdb" flags "github.com/btcsuite/go-flags" ) @@ -66,6 +68,7 @@ var ( type config struct { ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` + Check bool `long:"check" description:"Check and attempt to fix databases"` Create bool `long:"create" description:"Create the wallet if it does not exist"` CreateTemp bool `long:"createtemp" description:"Create a temporary simulation wallet (pass=password) in the data directory indicated; must call with --datadir"` CAFile string `long:"cafile" description:"File containing root certificates to authenticate a TLS connections with btcd"` @@ -376,6 +379,31 @@ func loadConfig() (*config, []string, error) { return nil, nil, err } + // Check existing database and attempt fixes if required + if cfg.Check { + netDir := networkDir(cfg.DataDir, activeNet.Params) + dbPath := filepath.Join(netDir, walletDbName) + db, err := walletdb.Open("bdb", dbPath) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return nil, nil, err + } + defer db.Close() + namespace, err := db.Namespace(waddrmgrNamespaceKey) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return nil, nil, err + } + err = waddrmgr.CheckIndexes(namespace) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return nil, nil, err + } + str := "Wallet check completed." + fmt.Fprintln(os.Stdout, str) + os.Exit(0) + } + // Exit if you try to use a simulation wallet with a standard // data directory. if cfg.DataDir == defaultDataDir && cfg.CreateTemp { diff --git a/waddrmgr/db.go b/waddrmgr/db.go index 84bb3fe04d..7c092b03b5 100644 --- a/waddrmgr/db.go +++ b/waddrmgr/db.go @@ -1746,9 +1746,13 @@ func upgradeManager(namespace walletdb.Namespace, pubPassPhrase []byte, chainPar version = 4 } - // Ensure that the account indexes are up-to-date and consistent - if err := updateAccountIndexes(namespace); err != nil { - return err + if version < 5 { + if err := upgradeToVersion5(namespace); err != nil { + return err + } + + // The manager is now at version 5. + version = 5 } // Ensure the manager is upraded to the latest version. This check is @@ -1976,14 +1980,25 @@ func upgradeToVersion4(namespace walletdb.Namespace, pubPassPhrase []byte) error return nil } -// updateAccountIndexes checks for any missing entries in the account id +func upgradeToVersion5(namespace walletdb.Namespace) error { + err := namespace.Update(func(tx walletdb.Tx) error { + // Write new manager version. + return putManagerVersion(tx, 5) + }) + if err != nil { + return maybeConvertDbError(err) + } + return CheckIndexes(namespace) +} + +// CheckIndexes checks for any missing entries in the account id // and name indexes and rebuils them, if required, to make sure that the // indexes are consistent -func updateAccountIndexes(namespace walletdb.Namespace) error { +func CheckIndexes(namespace walletdb.Namespace) error { err := namespace.Update(func(tx walletdb.Tx) error { bucket := tx.RootBucket().Bucket(acctBucketName) - err := bucket.ForEach(func(k, v []byte) error { + bucket.ForEach(func(k, v []byte) error { account := binary.LittleEndian.Uint32(k) if v == nil { fmt.Printf("account #%d: missing account data", account) @@ -2028,13 +2043,9 @@ func updateAccountIndexes(namespace walletdb.Namespace) error { "#%d -> #%d\n", name, oldAccount, account) } } - return nil }) - return err + return nil }) - if err != nil { - return maybeConvertDbError(err) - } - return nil + return err }