Skip to content

Commit a42923c

Browse files
committed
Merge bitcoin/bitcoin#23348: rpc, wallet: Do not return "keypoololdest" for blank descriptor wallets
ee03c78 wallet: Make GetOldestKeyPoolTime return nullopt for blank wallets (Hennadii Stepanov) 3e4f069 wallet, refactor: Make GetOldestKeyPoolTime return type std::optional (Hennadii Stepanov) Pull request description: The "keypoololdest" field in the `getwalletinfo` RPC response should be used for legacy wallets only. Th current implementation (04437ee) assumes that `CWallet::GetOldestKeyPoolTime()` always return `0` for descriptor wallets. This assumption is wrong for _blank_ descriptor wallets, when `m_spk_managers` is empty. As a result: ``` $ src/bitcoin-cli -signet -rpcwallet=211024-d-DPK getwalletinfo { "walletname": "211024-d-DPK", "walletversion": 169900, "format": "sqlite", "balance": 0.00000000, "unconfirmed_balance": 0.00000000, "immature_balance": 0.00000000, "txcount": 0, "keypoololdest": 9223372036854775807, "keypoolsize": 0, "keypoolsize_hd_internal": 0, "paytxfee": 0.00000000, "private_keys_enabled": false, "avoid_reuse": false, "scanning": false, "descriptors": true } ``` This PR fixes this issue with direct checking of the `WALLET_FLAG_DESCRIPTORS` flag. ACKs for top commit: lsilva01: re-ACK ee03c78 stratospher: ACK ee03c78. meshcollider: Code review ACK ee03c78 Tree-SHA512: 9852f9f8ed5c08c07507274d7714f039bbfda66da6df65cf98f67bf11a600167d0f7f872680c95775399477f4df9ba9fce80ec0cbe0adb7f2bb33c3bd65b15df
2 parents 79e64a0 + ee03c78 commit a42923c

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

src/wallet/rpcwallet.cpp

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

25232523
size_t kpExternalSize = pwallet->KeypoolCountExternalKeys();
25242524
const auto bal = GetBalance(*pwallet);
2525-
int64_t kp_oldest = pwallet->GetOldestKeyPoolTime();
25262525
obj.pushKV("walletname", pwallet->GetName());
25272526
obj.pushKV("walletversion", pwallet->GetVersion());
25282527
obj.pushKV("format", pwallet->GetDatabase().Format());
25292528
obj.pushKV("balance", ValueFromAmount(bal.m_mine_trusted));
25302529
obj.pushKV("unconfirmed_balance", ValueFromAmount(bal.m_mine_untrusted_pending));
25312530
obj.pushKV("immature_balance", ValueFromAmount(bal.m_mine_immature));
25322531
obj.pushKV("txcount", (int)pwallet->mapWallet.size());
2533-
if (kp_oldest > 0) {
2534-
obj.pushKV("keypoololdest", kp_oldest);
2532+
const auto kp_oldest = pwallet->GetOldestKeyPoolTime();
2533+
if (kp_oldest.has_value()) {
2534+
obj.pushKV("keypoololdest", kp_oldest.value());
25352535
}
25362536
obj.pushKV("keypoolsize", (int64_t)kpExternalSize);
25372537

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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,14 +2169,18 @@ 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+
if (m_spk_managers.empty()) {
2176+
return std::nullopt;
2177+
}
2178+
2179+
std::optional<int64_t> oldest_key{std::numeric_limits<int64_t>::max()};
21762180
for (const auto& spk_man_pair : m_spk_managers) {
2177-
oldestKey = std::min(oldestKey, spk_man_pair.second->GetOldestKeyPoolTime());
2181+
oldest_key = std::min(oldest_key, spk_man_pair.second->GetOldestKeyPoolTime());
21782182
}
2179-
return oldestKey;
2183+
return oldest_key;
21802184
}
21812185

21822186
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
@@ -632,7 +632,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
632632
size_t KeypoolCountExternalKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
633633
bool TopUpKeyPool(unsigned int kpSize = 0);
634634

635-
int64_t GetOldestKeyPoolTime() const;
635+
std::optional<int64_t> GetOldestKeyPoolTime() const;
636636

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

0 commit comments

Comments
 (0)