diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 2f4e21e45dcc..90e4bd149376 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -615,7 +615,13 @@ bool LegacyScriptPubKeyMan::CanGetAddresses(bool internal) return keypool_has_keys; } -static int64_t GetOldestKeyInPool(const std::set& setKeyPool, WalletBatch& batch) { +static int64_t GetOldestKeyTimeInPool(const std::set& setKeyPool, WalletBatch& batch) +{ + if (setKeyPool.empty()) { + // if the keypool is empty, return + return GetTime(); + } + CKeyPool keypool; int64_t nIndex = *(setKeyPool.begin()); if (!batch.ReadPool(nIndex, keypool)) { @@ -629,19 +635,10 @@ int64_t LegacyScriptPubKeyMan::GetOldestKeyPoolTime() { LOCK(cs_wallet); - // if the keypool is empty, return - if (setExternalKeyPool.empty() && setInternalKeyPool.empty()) - return GetTime(); - WalletBatch batch(m_storage.GetDatabase()); - int64_t oldestKey = -1; - - // load oldest key from keypool, get time and return - if (!setInternalKeyPool.empty()) { - oldestKey = std::max(GetOldestKeyInPool(setInternalKeyPool, batch), oldestKey); - } - if (!setExternalKeyPool.empty()) { - oldestKey = std::max(GetOldestKeyInPool(setExternalKeyPool, batch), oldestKey); + int64_t oldestKey = GetOldestKeyTimeInPool(setExternalKeyPool, batch); + if (IsHDEnabled()) { + oldestKey = std::max(GetOldestKeyTimeInPool(setInternalKeyPool, batch), oldestKey); } return oldestKey; }