Skip to content

Commit

Permalink
Remove database if wallet.Loader.CreateNewWallet errors. (btcsuite#419)
Browse files Browse the repository at this point in the history
This prevents future calls to CreateNewWallet from erroring due to the
database file existing.

Fixes btcsuite#418.
  • Loading branch information
jrick authored and alexlyp committed Dec 2, 2016
1 parent d4b2737 commit 73bc20e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rpc/documentation/api.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# RPC API Specification

Version: 4.0.1
Version: 4.0.2

**Note:** This document assumes the reader is familiar with gRPC concepts.
Refer to the [gRPC Concepts documentation](http://www.grpc.io/docs/guides/concepts.html)
Expand Down
4 changes: 2 additions & 2 deletions rpc/rpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ import (

// Public API version constants
const (
semverString = "4.0.1"
semverString = "4.0.2"
semverMajor = 4
semverMinor = 0
semverPatch = 1
semverPatch = 2
)

// translateError creates a new gRPC error with an appropiate error code for
Expand Down
10 changes: 8 additions & 2 deletions wallet/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (l *Loader) RunAfterLoad(fn func(*Wallet)) {
// CreateNewWallet creates a new wallet using the provided public and private
// passphrases. The seed is optional. If non-nil, addresses are derived from
// this seed. If nil, a secure random seed is generated.
func (l *Loader) CreateNewWallet(pubPassphrase, privPassphrase, seed []byte) (*Wallet, error) {
func (l *Loader) CreateNewWallet(pubPassphrase, privPassphrase, seed []byte) (w *Wallet, err error) {
defer l.mu.Unlock()
l.mu.Lock()

Expand All @@ -147,6 +147,12 @@ func (l *Loader) CreateNewWallet(pubPassphrase, privPassphrase, seed []byte) (*W
if err != nil {
return nil, err
}
// Attempt to remove database file if this function errors.
defer func() {
if err != nil {
_ = os.Remove(dbPath)
}
}()

// Initialize the newly created database for the wallet before opening.
err = Create(db, pubPassphrase, privPassphrase, seed, l.chainParams,
Expand All @@ -157,7 +163,7 @@ func (l *Loader) CreateNewWallet(pubPassphrase, privPassphrase, seed []byte) (*W

// Open the newly-created wallet.
so := l.stakeOptions
w, err := Open(db, pubPassphrase, nil, so.VoteBits, so.VoteBitsExtended,
w, err = Open(db, pubPassphrase, nil, so.VoteBits, so.VoteBitsExtended,
so.StakeMiningEnabled, so.BalanceToMaintain, so.AddressReuse,
so.PruneTickets, so.TicketAddress, so.TicketMaxPrice,
so.TicketBuyFreq, so.PoolAddress, so.PoolFees, so.TicketFee,
Expand Down

0 comments on commit 73bc20e

Please sign in to comment.