aboutsummaryrefslogtreecommitdiff
path: root/src/wallet.cpp
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2011-09-01 12:01:29 -0700
committerGavin Andresen <gavinandresen@gmail.com>2011-09-01 12:01:29 -0700
commit783c636c73182731ca12e6af67516767a989da66 (patch)
tree6e160958804c3f0b968a295b3bec7d2ed947b220 /src/wallet.cpp
parentdd7868364d9a14349ff82f74fc451db5e6b6b7b9 (diff)
parented02c95d505ce48451b600ff40720841a000fd50 (diff)
downloadbitcoin-783c636c73182731ca12e6af67516767a989da66.tar.xz
Merge pull request #467 from gavinandresen/keypoolzero
Logic running with -keypool=0 was wrong (empty keys were being returned).
Diffstat (limited to 'src/wallet.cpp')
-rw-r--r--src/wallet.cpp38
1 files changed, 29 insertions, 9 deletions
diff --git a/src/wallet.cpp b/src/wallet.cpp
index 1daec98d34..8bbb80cf25 100644
--- a/src/wallet.cpp
+++ b/src/wallet.cpp
@@ -268,8 +268,12 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn)
{
if (txout.scriptPubKey == scriptDefaultKey)
{
- SetDefaultKey(GetOrReuseKeyFromPool());
- SetAddressBookName(CBitcoinAddress(vchDefaultKey), "");
+ std::vector<unsigned char> newDefaultKey;
+ if (GetKeyFromPool(newDefaultKey, false))
+ {
+ SetDefaultKey(newDefaultKey);
+ SetAddressBookName(CBitcoinAddress(vchDefaultKey), "");
+ }
}
}
@@ -1126,7 +1130,10 @@ int CWallet::LoadWallet(bool& fFirstRunRet)
// Create new keyUser and set as default key
RandAddSeedPerfmon();
- SetDefaultKey(GetOrReuseKeyFromPool());
+ std::vector<unsigned char> newDefaultKey;
+ if (!GetKeyFromPool(newDefaultKey, false))
+ return DB_LOAD_FAIL;
+ SetDefaultKey(newDefaultKey);
if (!SetAddressBookName(CBitcoinAddress(vchDefaultKey), ""))
return DB_LOAD_FAIL;
}
@@ -1269,15 +1276,28 @@ void CWallet::ReturnKey(int64 nIndex)
printf("keypool return %"PRI64d"\n", nIndex);
}
-vector<unsigned char> CWallet::GetOrReuseKeyFromPool()
+bool CWallet::GetKeyFromPool(vector<unsigned char>& result, bool fAllowReuse)
{
int64 nIndex = 0;
CKeyPool keypool;
- ReserveKeyFromKeyPool(nIndex, keypool);
- if(nIndex == -1)
- return vchDefaultKey;
- KeepKey(nIndex);
- return keypool.vchPubKey;
+ CRITICAL_BLOCK(cs_wallet)
+ {
+ ReserveKeyFromKeyPool(nIndex, keypool);
+ if (nIndex == -1)
+ {
+ if (fAllowReuse && !vchDefaultKey.empty())
+ {
+ result = vchDefaultKey;
+ return true;
+ }
+ if (IsLocked()) return false;
+ result = GenerateNewKey();
+ return true;
+ }
+ KeepKey(nIndex);
+ result = keypool.vchPubKey;
+ }
+ return true;
}
int64 CWallet::GetOldestKeyPoolTime()