diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -640,6 +640,7 @@ std::set setInternalKeyPool; std::set setExternalKeyPool; + int64_t m_max_keypool_index; int64_t nTimeFirstKey; @@ -684,12 +685,13 @@ } } - 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 stored @@ -729,6 +731,7 @@ nAccountingEntryNumber = 0; nNextResend = 0; nLastResend = 0; + m_max_keypool_index = 0; nTimeFirstKey = 0; fBroadcastTransactions = false; } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3293,28 +3293,25 @@ bool internal = false; CWalletDB walletdb(*dbw); for (int64_t i = missingInternal + missingExternal; i--;) { - int64_t nEnd = 1; if (i < missingInternal) { internal = true; } - if (!setInternalKeyPool.empty()) { - nEnd = *(setInternalKeyPool.rbegin()) + 1; - } - if (!setExternalKeyPool.empty()) { - nEnd = std::max(nEnd, *(setExternalKeyPool.rbegin()) + 1); - } + // How in the hell did you use so many keys? + assert(m_max_keypool_index < std::numeric_limits::max()); + int64_t index = ++m_max_keypool_index; if (!walletdb.WritePool( - nEnd, CKeyPool(GenerateNewKey(walletdb, internal), internal))) { + index, + CKeyPool(GenerateNewKey(walletdb, internal), internal))) { throw std::runtime_error(std::string(__func__) + ": writing generated key failed"); } if (internal) { - setInternalKeyPool.insert(nEnd); + setInternalKeyPool.insert(index); } else { - setExternalKeyPool.insert(nEnd); + setExternalKeyPool.insert(index); } } if (missingInternal + missingExternal > 0) {