Skip to content

Commit 3e4f069

Browse files
committed
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.
1 parent 7efc628 commit 3e4f069

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,16 +2513,16 @@ static RPCHelpMan getwalletinfo()
25132513

25142514
size_t kpExternalSize = pwallet->KeypoolCountExternalKeys();
25152515
const auto bal = GetBalance(*pwallet);
2516-
int64_t kp_oldest = pwallet->GetOldestKeyPoolTime();
25172516
obj.pushKV("walletname", pwallet->GetName());
25182517
obj.pushKV("walletversion", pwallet->GetVersion());
25192518
obj.pushKV("format", pwallet->GetDatabase().Format());
25202519
obj.pushKV("balance", ValueFromAmount(bal.m_mine_trusted));
25212520
obj.pushKV("unconfirmed_balance", ValueFromAmount(bal.m_mine_untrusted_pending));
25222521
obj.pushKV("immature_balance", ValueFromAmount(bal.m_mine_immature));
25232522
obj.pushKV("txcount", (int)pwallet->mapWallet.size());
2524-
if (kp_oldest > 0) {
2525-
obj.pushKV("keypoololdest", kp_oldest);
2523+
const auto kp_oldest = pwallet->GetOldestKeyPoolTime();
2524+
if (kp_oldest.has_value()) {
2525+
obj.pushKV("keypoololdest", kp_oldest.value());
25262526
}
25272527
obj.pushKV("keypoolsize", (int64_t)kpExternalSize);
25282528

src/wallet/scriptpubkeyman.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ static int64_t GetOldestKeyTimeInPool(const std::set<int64_t>& setKeyPool, Walle
528528
return keypool.nTime;
529529
}
530530

531-
int64_t LegacyScriptPubKeyMan::GetOldestKeyPoolTime() const
531+
std::optional<int64_t> LegacyScriptPubKeyMan::GetOldestKeyPoolTime() const
532532
{
533533
LOCK(cs_KeyStore);
534534

@@ -1970,11 +1970,10 @@ bool DescriptorScriptPubKeyMan::HavePrivateKeys() const
19701970
return m_map_keys.size() > 0 || m_map_crypted_keys.size() > 0;
19711971
}
19721972

1973-
int64_t DescriptorScriptPubKeyMan::GetOldestKeyPoolTime() const
1973+
std::optional<int64_t> DescriptorScriptPubKeyMan::GetOldestKeyPoolTime() const
19741974
{
19751975
// This is only used for getwalletinfo output and isn't relevant to descriptor wallets.
1976-
// The magic number 0 indicates that it shouldn't be displayed so that's what we return.
1977-
return 0;
1976+
return std::nullopt;
19781977
}
19791978

19801979

src/wallet/scriptpubkeyman.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <boost/signals2/signal.hpp>
2121

22+
#include <optional>
2223
#include <unordered_map>
2324

2425
enum class OutputType;
@@ -203,7 +204,7 @@ class ScriptPubKeyMan
203204
//! The action to do when the DB needs rewrite
204205
virtual void RewriteDB() {}
205206

206-
virtual int64_t GetOldestKeyPoolTime() const { return GetTime(); }
207+
virtual std::optional<int64_t> GetOldestKeyPoolTime() const { return GetTime(); }
207208

208209
virtual unsigned int GetKeyPoolSize() const { return 0; }
209210

@@ -371,7 +372,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
371372

372373
void RewriteDB() override;
373374

374-
int64_t GetOldestKeyPoolTime() const override;
375+
std::optional<int64_t> GetOldestKeyPoolTime() const override;
375376
size_t KeypoolCountExternalKeys() const;
376377
unsigned int GetKeyPoolSize() const override;
377378

@@ -577,7 +578,7 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
577578

578579
bool HavePrivateKeys() const override;
579580

580-
int64_t GetOldestKeyPoolTime() const override;
581+
std::optional<int64_t> GetOldestKeyPoolTime() const override;
581582
unsigned int GetKeyPoolSize() const override;
582583

583584
int64_t GetTimeFirstKey() const override;

src/wallet/wallet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,14 +2169,14 @@ bool CWallet::GetNewChangeDestination(const OutputType type, CTxDestination& des
21692169
return true;
21702170
}
21712171

2172-
int64_t CWallet::GetOldestKeyPoolTime() const
2172+
std::optional<int64_t> CWallet::GetOldestKeyPoolTime() const
21732173
{
21742174
LOCK(cs_wallet);
2175-
int64_t oldestKey = std::numeric_limits<int64_t>::max();
2175+
std::optional<int64_t> oldest_key{std::numeric_limits<int64_t>::max()};
21762176
for (const auto& spk_man_pair : m_spk_managers) {
2177-
oldestKey = std::min(oldestKey, spk_man_pair.second->GetOldestKeyPoolTime());
2177+
oldest_key = std::min(oldest_key, spk_man_pair.second->GetOldestKeyPoolTime());
21782178
}
2179-
return oldestKey;
2179+
return oldest_key;
21802180
}
21812181

21822182
void CWallet::MarkDestinationsDirty(const std::set<CTxDestination>& destinations) {

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
628628
size_t KeypoolCountExternalKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
629629
bool TopUpKeyPool(unsigned int kpSize = 0);
630630

631-
int64_t GetOldestKeyPoolTime() const;
631+
std::optional<int64_t> GetOldestKeyPoolTime() const;
632632

633633
std::set<CTxDestination> GetLabelAddresses(const std::string& label) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
634634

0 commit comments

Comments
 (0)