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.
This commit is contained in:
Hennadii Stepanov 2021-10-30 22:19:22 +03:00
parent 7efc628539
commit 3e4f069d23
No known key found for this signature in database
GPG key ID: 410108112E7EA81F
5 changed files with 15 additions and 15 deletions

View file

@ -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);

View file

@ -528,7 +528,7 @@ static int64_t GetOldestKeyTimeInPool(const std::set<int64_t>& setKeyPool, Walle
return keypool.nTime;
}
int64_t LegacyScriptPubKeyMan::GetOldestKeyPoolTime() const
std::optional<int64_t> 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<int64_t> 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;
}

View file

@ -19,6 +19,7 @@
#include <boost/signals2/signal.hpp>
#include <optional>
#include <unordered_map>
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<int64_t> 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<int64_t> 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<int64_t> GetOldestKeyPoolTime() const override;
unsigned int GetKeyPoolSize() const override;
int64_t GetTimeFirstKey() const override;

View file

@ -2169,14 +2169,14 @@ bool CWallet::GetNewChangeDestination(const OutputType type, CTxDestination& des
return true;
}
int64_t CWallet::GetOldestKeyPoolTime() const
std::optional<int64_t> CWallet::GetOldestKeyPoolTime() const
{
LOCK(cs_wallet);
int64_t oldestKey = std::numeric_limits<int64_t>::max();
std::optional<int64_t> oldest_key{std::numeric_limits<int64_t>::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<CTxDestination>& destinations) {

View file

@ -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<int64_t> GetOldestKeyPoolTime() const;
std::set<CTxDestination> GetLabelAddresses(const std::string& label) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);