From da288c481a7d28241db34b61fc54851d416de1f9 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Tue, 13 Sep 2022 14:46:00 +0300 Subject: [PATCH] refactor: Refactor GetOldestKeyPoolTime to align with bitcoin 10235 --- src/wallet/scriptpubkeyman.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) 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; }