Skip to content

Commit 82da47e

Browse files
committed
Fix SearchKeyPool and ReserveKeyFromKeyPool
Avoid the potential error that is caused by unable to erase a key that does not exist in the key pool
1 parent e02e979 commit 82da47e

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/wallet/wallet.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,7 +2611,7 @@ void CWallet::ViewKeyPool(std::vector<CPubKey>& keys)
26112611
}
26122612
}
26132613

2614-
int64_t CWallet::SearchKeyPool(const CBitcoinAddress& address) const
2614+
bool CWallet::SearchKeyPool(int64_t& nIndex, const CBitcoinAddress& address) const
26152615
{
26162616
LOCK(cs_wallet);
26172617
CWalletDB walletdb(strWalletFile);
@@ -2620,10 +2620,12 @@ int64_t CWallet::SearchKeyPool(const CBitcoinAddress& address) const
26202620
CKeyPool keypool;
26212621
if (!walletdb.ReadPool(*it, keypool))
26222622
throw runtime_error(_(__func__) + "() : read failed");
2623-
if (address == CBitcoinAddress(keypool.vchPubKey.GetID()))
2624-
return (*it);
2623+
if (address == CBitcoinAddress(keypool.vchPubKey.GetID())) {
2624+
nIndex = *it;
2625+
return true;
2626+
}
26252627
}
2626-
return -1;
2628+
return false;
26272629
}
26282630

26292631
void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool)
@@ -2657,12 +2659,13 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, const CB
26572659
{
26582660
LOCK(cs_wallet);
26592661

2660-
// Get the oldest key
2662+
// Return if the key pool is empty
26612663
if (setKeyPool.empty())
26622664
return;
26632665

26642666
CWalletDB walletdb(strWalletFile);
2665-
nIndex = SearchKeyPool(address);
2667+
if (!SearchKeyPool(nIndex, address))
2668+
return;
26662669
setKeyPool.erase(nIndex);
26672670
if (!walletdb.ReadPool(nIndex, keypool))
26682671
throw runtime_error(_(__func__) + "() : read failed");

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ class CWallet : public CHDKeyStore, public CValidationInterface
680680
bool AddKeyPool(CPubKey& key);
681681
bool EraseKeyPool();
682682
void ViewKeyPool(std::vector<CPubKey>& keys);
683-
int64_t SearchKeyPool(const CBitcoinAddress& address) const;
683+
bool SearchKeyPool(int64_t& nIndex, const CBitcoinAddress& address) const;
684684
void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool);
685685
void ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, const CBitcoinAddress& address);
686686
void KeepKey(int64_t nIndex);

0 commit comments

Comments
 (0)