Additional mutex fixes.

This commit is contained in:
Josh Rickmar 2014-01-28 14:43:55 -05:00
parent dd3d7467c3
commit f4c5cc1b7e

View file

@ -277,8 +277,6 @@ func (a *Account) CurrentAddress() (btcutil.Address, error) {
// replies. // replies.
func (a *Account) ListSinceBlock(since, curBlockHeight int32, minconf int) ([]map[string]interface{}, error) { func (a *Account) ListSinceBlock(since, curBlockHeight int32, minconf int) ([]map[string]interface{}, error) {
var txInfoList []map[string]interface{} var txInfoList []map[string]interface{}
a.mtx.RLock()
defer a.mtx.RUnlock()
a.TxStore.RLock() a.TxStore.RLock()
defer a.TxStore.RUnlock() defer a.TxStore.RUnlock()
@ -307,7 +305,6 @@ func (a *Account) ListTransactions(from, count int) ([]map[string]interface{}, e
} }
var txInfoList []map[string]interface{} var txInfoList []map[string]interface{}
a.mtx.RLock()
a.TxStore.RLock() a.TxStore.RLock()
lastLookupIdx := len(a.TxStore.s) - count lastLookupIdx := len(a.TxStore.s) - count
@ -316,7 +313,6 @@ func (a *Account) ListTransactions(from, count int) ([]map[string]interface{}, e
txInfoList = append(txInfoList, txInfoList = append(txInfoList,
a.TxStore.s[i].TxInfo(a.name, bs.Height, a.Net())...) a.TxStore.s[i].TxInfo(a.name, bs.Height, a.Net())...)
} }
a.mtx.RUnlock()
a.TxStore.RUnlock() a.TxStore.RUnlock()
return txInfoList, nil return txInfoList, nil
@ -336,7 +332,6 @@ func (a *Account) ListAddressTransactions(pkHashes map[string]struct{}) (
} }
var txInfoList []map[string]interface{} var txInfoList []map[string]interface{}
a.mtx.RLock()
a.TxStore.RLock() a.TxStore.RLock()
for i := range a.TxStore.s { for i := range a.TxStore.s {
@ -349,7 +344,6 @@ func (a *Account) ListAddressTransactions(pkHashes map[string]struct{}) (
txInfoList = append(txInfoList, info...) txInfoList = append(txInfoList, info...)
} }
} }
a.mtx.RUnlock()
a.TxStore.RUnlock() a.TxStore.RUnlock()
return txInfoList, nil return txInfoList, nil
@ -367,7 +361,6 @@ func (a *Account) ListAllTransactions() ([]map[string]interface{}, error) {
} }
var txInfoList []map[string]interface{} var txInfoList []map[string]interface{}
a.mtx.RLock()
a.TxStore.RLock() a.TxStore.RLock()
// Search in reverse order: lookup most recently-added first. // Search in reverse order: lookup most recently-added first.
@ -375,7 +368,6 @@ func (a *Account) ListAllTransactions() ([]map[string]interface{}, error) {
txInfoList = append(txInfoList, txInfoList = append(txInfoList,
a.TxStore.s[i].TxInfo(a.name, bs.Height, a.Net())...) a.TxStore.s[i].TxInfo(a.name, bs.Height, a.Net())...)
} }
a.mtx.RUnlock()
a.TxStore.RUnlock() a.TxStore.RUnlock()
return txInfoList, nil return txInfoList, nil
@ -390,13 +382,13 @@ func (a *Account) DumpPrivKeys() ([]string, error) {
// Iterate over each active address, appending the private // Iterate over each active address, appending the private
// key to privkeys. // key to privkeys.
var privkeys []string var privkeys []string
for addr, info := range a.ActiveAddresses() { for addr, info := range a.Wallet.ActiveAddresses() {
key, err := a.AddressKey(addr) key, err := a.Wallet.AddressKey(addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
encKey, err := btcutil.EncodePrivateKey(key.D.Bytes(), encKey, err := btcutil.EncodePrivateKey(key.D.Bytes(),
a.Net(), info.Compressed) a.Wallet.Net(), info.Compressed)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -413,14 +405,14 @@ func (a *Account) DumpWIFPrivateKey(addr btcutil.Address) (string, error) {
defer a.mtx.RUnlock() defer a.mtx.RUnlock()
// Get private key from wallet if it exists. // Get private key from wallet if it exists.
key, err := a.AddressKey(addr) key, err := a.Wallet.AddressKey(addr)
if err != nil { if err != nil {
return "", err return "", err
} }
// Get address info. This is needed to determine whether // Get address info. This is needed to determine whether
// the pubkey is compressed or not. // the pubkey is compressed or not.
info, err := a.AddressInfo(addr) info, err := a.Wallet.AddressInfo(addr)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -506,9 +498,8 @@ func (a *Account) ImportWIFPrivateKey(wif string, bs *wallet.BlockStamp) (string
// dropped from scope. // dropped from scope.
func (a *Account) ExportWatchingWallet() (*Account, error) { func (a *Account) ExportWatchingWallet() (*Account, error) {
a.mtx.RLock() a.mtx.RLock()
defer a.mtx.RUnlock()
ww, err := a.Wallet.ExportWatchingWallet() ww, err := a.Wallet.ExportWatchingWallet()
a.mtx.RUnlock()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -525,24 +516,27 @@ func (a *Account) exportBase64() (map[string]string, error) {
m := make(map[string]string) m := make(map[string]string)
a.mtx.RLock() a.mtx.RLock()
defer a.mtx.RUnlock() _, err := a.Wallet.WriteTo(buf)
if _, err := a.Wallet.WriteTo(buf); err != nil { a.mtx.RUnlock()
if err != nil {
return nil, err return nil, err
} }
m["wallet"] = base64.StdEncoding.EncodeToString(buf.Bytes()) m["wallet"] = base64.StdEncoding.EncodeToString(buf.Bytes())
buf.Reset() buf.Reset()
a.TxStore.RLock() a.TxStore.RLock()
defer a.TxStore.RUnlock() _, err = a.TxStore.s.WriteTo(buf)
if _, err := a.TxStore.s.WriteTo(buf); err != nil { a.TxStore.RUnlock()
if err != nil {
return nil, err return nil, err
} }
m["tx"] = base64.StdEncoding.EncodeToString(buf.Bytes()) m["tx"] = base64.StdEncoding.EncodeToString(buf.Bytes())
buf.Reset() buf.Reset()
a.UtxoStore.RLock() a.UtxoStore.RLock()
defer a.UtxoStore.RUnlock() _, err = a.UtxoStore.s.WriteTo(buf)
if _, err := a.UtxoStore.s.WriteTo(buf); err != nil { a.UtxoStore.RUnlock()
if err != nil {
return nil, err return nil, err
} }
m["utxo"] = base64.StdEncoding.EncodeToString(buf.Bytes()) m["utxo"] = base64.StdEncoding.EncodeToString(buf.Bytes())
@ -613,11 +607,10 @@ func (a *Account) RescanActiveAddresses() {
// addresses in an account. // addresses in an account.
func (a *Account) SortedActivePaymentAddresses() []string { func (a *Account) SortedActivePaymentAddresses() []string {
a.mtx.RLock() a.mtx.RLock()
defer a.mtx.RUnlock() infos := a.Wallet.SortedActiveAddresses()
a.mtx.RUnlock()
infos := a.SortedActiveAddresses()
addrs := make([]string, len(infos)) addrs := make([]string, len(infos))
for i, info := range infos { for i, info := range infos {
addrs[i] = info.Address.EncodeAddress() addrs[i] = info.Address.EncodeAddress()
} }
@ -629,11 +622,10 @@ func (a *Account) SortedActivePaymentAddresses() []string {
// in an account. // in an account.
func (a *Account) ActivePaymentAddresses() map[string]struct{} { func (a *Account) ActivePaymentAddresses() map[string]struct{} {
a.mtx.RLock() a.mtx.RLock()
defer a.mtx.RUnlock()
infos := a.ActiveAddresses() infos := a.ActiveAddresses()
addrs := make(map[string]struct{}, len(infos)) a.mtx.RUnlock()
addrs := make(map[string]struct{}, len(infos))
for _, info := range infos { for _, info := range infos {
addrs[info.Address.EncodeAddress()] = struct{}{} addrs[info.Address.EncodeAddress()] = struct{}{}
} }
@ -643,17 +635,16 @@ func (a *Account) ActivePaymentAddresses() map[string]struct{} {
// NewAddress returns a new payment address for an account. // NewAddress returns a new payment address for an account.
func (a *Account) NewAddress() (btcutil.Address, error) { func (a *Account) NewAddress() (btcutil.Address, error) {
a.mtx.Lock()
// Get current block's height and hash. // Get current block's height and hash.
bs, err := GetCurBlock() bs, err := GetCurBlock()
if err != nil { if err != nil {
a.mtx.Unlock()
return nil, err return nil, err
} }
a.mtx.Lock()
// Get next address from wallet. // Get next address from wallet.
addr, err := a.NextChainedAddress(&bs, cfg.KeypoolSize) addr, err := a.Wallet.NextChainedAddress(&bs, cfg.KeypoolSize)
if err != nil { if err != nil {
a.mtx.Unlock() a.mtx.Unlock()
return nil, err return nil, err
@ -677,17 +668,17 @@ func (a *Account) NewAddress() (btcutil.Address, error) {
// RecoverAddresses recovers the next n chained addresses of a wallet. // RecoverAddresses recovers the next n chained addresses of a wallet.
func (a *Account) RecoverAddresses(n int) error { func (a *Account) RecoverAddresses(n int) error {
a.mtx.Lock()
// Get info on the last chained address. The rescan starts at the // Get info on the last chained address. The rescan starts at the
// earliest block height the last chained address might appear at. // earliest block height the last chained address might appear at.
a.mtx.RLock()
last := a.Wallet.LastChainedAddress() last := a.Wallet.LastChainedAddress()
lastInfo, err := a.Wallet.AddressInfo(last) lastInfo, err := a.Wallet.AddressInfo(last)
a.mtx.RUnlock()
if err != nil { if err != nil {
a.mtx.Unlock()
return err return err
} }
a.mtx.Lock()
addrs, err := a.Wallet.ExtendActiveAddresses(n, cfg.KeypoolSize) addrs, err := a.Wallet.ExtendActiveAddresses(n, cfg.KeypoolSize)
a.mtx.Unlock() a.mtx.Unlock()
if err != nil { if err != nil {