Skip to content

Commit

Permalink
wallet, refactor: Make GetOldestKeyPoolTime return type std::optional
Browse files Browse the repository at this point in the history
This change gets rid of the magic number 0 in the
DescriptorScriptPubKeyMan::GetOldestKeyPoolTime() function.

No behavior change.
  • Loading branch information
hebasto committed Nov 3, 2021
1 parent 7efc628 commit 3e4f069
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2513,16 +2513,16 @@ 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());
obj.pushKV("balance", ValueFromAmount(bal.m_mine_trusted));
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);

Expand Down
7 changes: 3 additions & 4 deletions src/wallet/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}


Expand Down
7 changes: 4 additions & 3 deletions src/wallet/scriptpubkeyman.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <boost/signals2/signal.hpp>

#include <optional>
#include <unordered_map>

enum class OutputType;
Expand Down Expand Up @@ -203,7 +204,7 @@ class ScriptPubKeyMan
//! 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; }

Expand Down Expand Up @@ -371,7 +372,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv

void RewriteDB() override;

int64_t GetOldestKeyPoolTime() const override;
std::optional<int64_t> GetOldestKeyPoolTime() const override;
size_t KeypoolCountExternalKeys() const;
unsigned int GetKeyPoolSize() const override;

Expand Down Expand Up @@ -577,7 +578,7 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan

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;
Expand Down
8 changes: 4 additions & 4 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
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);

Expand Down

0 comments on commit 3e4f069

Please sign in to comment.