Skip to content

Commit

Permalink
Fix SearchKeyPool and ReserveKeyFromKeyPool
Browse files Browse the repository at this point in the history
Avoid the potential error that is caused by unable to erase a key that
does not exist in the key pool
  • Loading branch information
hihiben committed Jun 19, 2017
1 parent e02e979 commit 82da47e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2611,7 +2611,7 @@ void CWallet::ViewKeyPool(std::vector<CPubKey>& keys)
}
}

int64_t CWallet::SearchKeyPool(const CBitcoinAddress& address) const
bool CWallet::SearchKeyPool(int64_t& nIndex, const CBitcoinAddress& address) const
{
LOCK(cs_wallet);
CWalletDB walletdb(strWalletFile);
Expand All @@ -2620,10 +2620,12 @@ int64_t CWallet::SearchKeyPool(const CBitcoinAddress& address) const
CKeyPool keypool;
if (!walletdb.ReadPool(*it, keypool))
throw runtime_error(_(__func__) + "() : read failed");
if (address == CBitcoinAddress(keypool.vchPubKey.GetID()))
return (*it);
if (address == CBitcoinAddress(keypool.vchPubKey.GetID())) {
nIndex = *it;
return true;
}
}
return -1;
return false;
}

void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool)
Expand Down Expand Up @@ -2657,12 +2659,13 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, const CB
{
LOCK(cs_wallet);

// Get the oldest key
// Return if the key pool is empty
if (setKeyPool.empty())
return;

CWalletDB walletdb(strWalletFile);
nIndex = SearchKeyPool(address);
if (!SearchKeyPool(nIndex, address))
return;
setKeyPool.erase(nIndex);
if (!walletdb.ReadPool(nIndex, keypool))
throw runtime_error(_(__func__) + "() : read failed");
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ class CWallet : public CHDKeyStore, public CValidationInterface
bool AddKeyPool(CPubKey& key);
bool EraseKeyPool();
void ViewKeyPool(std::vector<CPubKey>& keys);
int64_t SearchKeyPool(const CBitcoinAddress& address) const;
bool SearchKeyPool(int64_t& nIndex, const CBitcoinAddress& address) const;
void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, const CBitcoinAddress& address);
void KeepKey(int64_t nIndex);
Expand Down

0 comments on commit 82da47e

Please sign in to comment.