Skip to content

Commit

Permalink
Add -check and move index check to upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxcanfly committed May 11, 2015
1 parent 9de5070 commit e72e51a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
28 changes: 28 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 23 additions & 12 deletions waddrmgr/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit e72e51a

Please sign in to comment.