diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 279d670878..7cfb4b4d6a 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4333,21 +4333,18 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize) if (i < missingInternal) { fInternal = true; } - if (!setInternalKeyPool.empty()) { - nEnd = *(--setInternalKeyPool.end()) + 1; - } - if (!setExternalKeyPool.empty()) { - nEnd = std::max(nEnd, *(--setExternalKeyPool.end()) + 1); - } - // TODO: implement keypools for all accounts? - if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey(walletdb, 0, fInternal), fInternal))) { + + assert(m_max_keypool_index < std::numeric_limits::max()); // How in the hell did you use so many keys? + int64_t index = ++m_max_keypool_index; + + if (!walletdb.WritePool(index, CKeyPool(GenerateNewKey(walletdb, internal), internal))) { throw std::runtime_error(std::string(__func__) + ": writing generated key failed"); } if (fInternal) { - setInternalKeyPool.insert(nEnd); + setInternalKeyPool.insert(index); } else { - setExternalKeyPool.insert(nEnd); + setExternalKeyPool.insert(index); } if (missingInternal + missingExternal > 0) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index df33fc64d8..97e758e6c4 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -753,6 +753,7 @@ private: std::set setInternalKeyPool; std::set setExternalKeyPool; + int64_t m_max_keypool_index; int64_t nTimeFirstKey; @@ -799,13 +800,14 @@ public: } } - void LoadKeyPool(int nIndex, const CKeyPool &keypool) + void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool) { if (keypool.fInternal) { setInternalKeyPool.insert(nIndex); } else { setExternalKeyPool.insert(nIndex); } + m_max_keypool_index = std::max(m_max_keypool_index, nIndex); // If no metadata exists yet, create a default with the pool key's // creation time. Note that this may be overwritten by actually @@ -851,6 +853,7 @@ public: nAccountingEntryNumber = 0; nNextResend = 0; nLastResend = 0; + m_max_keypool_index = 0; nTimeFirstKey = 0; fBroadcastTransactions = false; nRelockTime = 0;