From 3e4f069d23cd2ea5de8fa3c4b1a761ab097ad56f Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sat, 30 Oct 2021 22:19:22 +0300 Subject: [PATCH 1/2] wallet, refactor: Make GetOldestKeyPoolTime return type std::optional This change gets rid of the magic number 0 in the DescriptorScriptPubKeyMan::GetOldestKeyPoolTime() function. No behavior change. --- src/wallet/rpcwallet.cpp | 6 +++--- src/wallet/scriptpubkeyman.cpp | 7 +++---- src/wallet/scriptpubkeyman.h | 7 ++++--- src/wallet/wallet.cpp | 8 ++++---- src/wallet/wallet.h | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 86bfa10d888..5c3231c5b6e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2513,7 +2513,6 @@ static RPCHelpMan getwalletinfo() size_t kpExternalSize = pwallet->KeypoolCountExternalKeys(); const auto bal = GetBalance(*pwallet); - int64_t kp_oldest = pwallet->GetOldestKeyPoolTime(); obj.pushKV("walletname", pwallet->GetName()); obj.pushKV("walletversion", pwallet->GetVersion()); obj.pushKV("format", pwallet->GetDatabase().Format()); @@ -2521,8 +2520,9 @@ static RPCHelpMan getwalletinfo() obj.pushKV("unconfirmed_balance", ValueFromAmount(bal.m_mine_untrusted_pending)); obj.pushKV("immature_balance", ValueFromAmount(bal.m_mine_immature)); obj.pushKV("txcount", (int)pwallet->mapWallet.size()); - if (kp_oldest > 0) { - obj.pushKV("keypoololdest", kp_oldest); + const auto kp_oldest = pwallet->GetOldestKeyPoolTime(); + if (kp_oldest.has_value()) { + obj.pushKV("keypoololdest", kp_oldest.value()); } obj.pushKV("keypoolsize", (int64_t)kpExternalSize); diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 619ebc8b4f6..f8ba6da20ae 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -528,7 +528,7 @@ static int64_t GetOldestKeyTimeInPool(const std::set& setKeyPool, Walle return keypool.nTime; } -int64_t LegacyScriptPubKeyMan::GetOldestKeyPoolTime() const +std::optional LegacyScriptPubKeyMan::GetOldestKeyPoolTime() const { LOCK(cs_KeyStore); @@ -1970,11 +1970,10 @@ bool DescriptorScriptPubKeyMan::HavePrivateKeys() const return m_map_keys.size() > 0 || m_map_crypted_keys.size() > 0; } -int64_t DescriptorScriptPubKeyMan::GetOldestKeyPoolTime() const +std::optional DescriptorScriptPubKeyMan::GetOldestKeyPoolTime() const { // This is only used for getwalletinfo output and isn't relevant to descriptor wallets. - // The magic number 0 indicates that it shouldn't be displayed so that's what we return. - return 0; + return std::nullopt; } diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h index ef746387513..2d447f1d674 100644 --- a/src/wallet/scriptpubkeyman.h +++ b/src/wallet/scriptpubkeyman.h @@ -19,6 +19,7 @@ #include +#include #include enum class OutputType; @@ -203,7 +204,7 @@ public: //! The action to do when the DB needs rewrite virtual void RewriteDB() {} - virtual int64_t GetOldestKeyPoolTime() const { return GetTime(); } + virtual std::optional GetOldestKeyPoolTime() const { return GetTime(); } virtual unsigned int GetKeyPoolSize() const { return 0; } @@ -371,7 +372,7 @@ public: void RewriteDB() override; - int64_t GetOldestKeyPoolTime() const override; + std::optional GetOldestKeyPoolTime() const override; size_t KeypoolCountExternalKeys() const; unsigned int GetKeyPoolSize() const override; @@ -577,7 +578,7 @@ public: bool HavePrivateKeys() const override; - int64_t GetOldestKeyPoolTime() const override; + std::optional GetOldestKeyPoolTime() const override; unsigned int GetKeyPoolSize() const override; int64_t GetTimeFirstKey() const override; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 4eb9d5560d2..904154c91da 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2169,14 +2169,14 @@ bool CWallet::GetNewChangeDestination(const OutputType type, CTxDestination& des return true; } -int64_t CWallet::GetOldestKeyPoolTime() const +std::optional CWallet::GetOldestKeyPoolTime() const { LOCK(cs_wallet); - int64_t oldestKey = std::numeric_limits::max(); + std::optional oldest_key{std::numeric_limits::max()}; for (const auto& spk_man_pair : m_spk_managers) { - oldestKey = std::min(oldestKey, spk_man_pair.second->GetOldestKeyPoolTime()); + oldest_key = std::min(oldest_key, spk_man_pair.second->GetOldestKeyPoolTime()); } - return oldestKey; + return oldest_key; } void CWallet::MarkDestinationsDirty(const std::set& destinations) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index c911eb461ce..96fb84a2cf3 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -628,7 +628,7 @@ public: size_t KeypoolCountExternalKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); bool TopUpKeyPool(unsigned int kpSize = 0); - int64_t GetOldestKeyPoolTime() const; + std::optional GetOldestKeyPoolTime() const; std::set GetLabelAddresses(const std::string& label) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); From ee03c782ba61993d9e95fa499546cd14cee35445 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sat, 30 Oct 2021 22:19:22 +0300 Subject: [PATCH 2/2] wallet: Make GetOldestKeyPoolTime return nullopt for blank wallets This change suppress the "keypoololdest" field in the getwalletinfo RPC response for blank descriptor wallets. --- src/wallet/wallet.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 904154c91da..e535a080f28 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2172,6 +2172,10 @@ bool CWallet::GetNewChangeDestination(const OutputType type, CTxDestination& des std::optional CWallet::GetOldestKeyPoolTime() const { LOCK(cs_wallet); + if (m_spk_managers.empty()) { + return std::nullopt; + } + std::optional oldest_key{std::numeric_limits::max()}; for (const auto& spk_man_pair : m_spk_managers) { oldest_key = std::min(oldest_key, spk_man_pair.second->GetOldestKeyPoolTime());