Maybe to ~enforce that callers are actually setting up the cache add the cache as an argument to the constructor?
<details>
<summary>
diff
</summary>
diff --git a/src/wallet/external_signer_scriptpubkeyman.h b/src/wallet/external_signer_scriptpubkeyman.h
index c0bdb037d8..6c181500d3 100644
--- a/src/wallet/external_signer_scriptpubkeyman.h
+++ b/src/wallet/external_signer_scriptpubkeyman.h
@@ -16,8 +16,8 @@ namespace wallet {
class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan
{
public:
- ExternalSignerScriptPubKeyMan(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys)
- : DescriptorScriptPubKeyMan(storage, id, descriptor, keypool_size, keys, ckeys)
+ ExternalSignerScriptPubKeyMan(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys, const DescriptorCache& cache)
+ : DescriptorScriptPubKeyMan(storage, id, descriptor, keypool_size, keys, ckeys, cache)
{}
ExternalSignerScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size)
: DescriptorScriptPubKeyMan(storage, keypool_size)
diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp
index cf0d0033e5..a49cc99f1d 100644
--- a/src/wallet/scriptpubkeyman.cpp
+++ b/src/wallet/scriptpubkeyman.cpp
@@ -821,7 +821,7 @@ bool LegacyDataSPKM::DeleteRecordsWithDB(WalletBatch& batch)
return batch.EraseRecords(DBKeys::LEGACY_TYPES);
}
-DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys)
+DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys, const DescriptorCache& cache)
: ScriptPubKeyMan(storage),
m_map_keys(keys),
m_map_crypted_keys(ckeys),
diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h
index 096e4a57b4..42be4d8f39 100644
--- a/src/wallet/scriptpubkeyman.h
+++ b/src/wallet/scriptpubkeyman.h
@@ -330,7 +330,7 @@ public:
m_wallet_descriptor(descriptor)
{}
//! Create a DescriptorScriptPubKeyMan from existing data (i.e. during loading)
- DescriptorScriptPubKeyMan(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys);
+ DescriptorScriptPubKeyMan(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys, const DescriptorCache& cache);
DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size)
: ScriptPubKeyMan(storage),
m_keypool_size(keypool_size)
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 2eb3242835..3b02856cbb 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -3546,13 +3546,13 @@ void CWallet::ConnectScriptPubKeyManNotifiers()
}
}
-void CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc, const KeyMap& keys, const CryptedKeyMap& ckeys)
+void CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc, const KeyMap& keys, const CryptedKeyMap& ckeys, const DescriptorCache& cache)
{
DescriptorScriptPubKeyMan* spk_manager;
if (IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
- spk_manager = new ExternalSignerScriptPubKeyMan(*this, id, desc, m_keypool_size, keys, ckeys);
+ spk_manager = new ExternalSignerScriptPubKeyMan(*this, id, desc, m_keypool_size, keys, ckeys, cache);
} else {
- spk_manager = new DescriptorScriptPubKeyMan(*this, id, desc, m_keypool_size, keys, ckeys);
+ spk_manager = new DescriptorScriptPubKeyMan(*this, id, desc, m_keypool_size, keys, ckeys, cache);
}
AddScriptPubKeyMan(id, std::unique_ptr<ScriptPubKeyMan>(spk_manager));
}
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
index df2659739e..7dec6d72dd 100644
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -1004,7 +1004,7 @@ public:
void ConnectScriptPubKeyManNotifiers();
//! Instantiate a descriptor ScriptPubKeyMan from the WalletDescriptor and load it
- void LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc, const KeyMap& keys, const CryptedKeyMap& ckeys);
+ void LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc, const KeyMap& keys, const CryptedKeyMap& ckeys, const DescriptorCache& cache);
//! Adds the active ScriptPubKeyMan for the specified type and internal. Writes it to the wallet file
//! [@param](/bitcoin-bitcoin/contributor/param/)[in] id The unique id for the ScriptPubKeyMan
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index b9ca9214e2..ca15df0043 100644
--- a/src/wallet/walletdb.cpp
+++ b/src/wallet/walletdb.cpp
@@ -831,9 +831,6 @@ static DBErrors LoadDescriptorWalletRecords(CWallet* pwallet, DatabaseBatch& bat
});
result = std::max(result, lh_cache_res.m_result);
- // Set the cache to the WalletDescriptor
- desc.cache = cache;
-
// Get unencrypted keys
KeyMap keys;
prefix = PrefixStream(DBKeys::WALLETDESCRIPTORKEY, id);
@@ -901,7 +898,7 @@ static DBErrors LoadDescriptorWalletRecords(CWallet* pwallet, DatabaseBatch& bat
num_ckeys = ckey_res.m_records;
try {
- pwallet->LoadDescriptorScriptPubKeyMan(id, desc, keys, ckeys);
+ pwallet->LoadDescriptorScriptPubKeyMan(id, desc, keys, ckeys, cache);
} catch (std::runtime_error& e) {
strErr = e.what();
return DBErrors::CORRUPT;
</details>
Alternatively... drop the argument from SetCache() since this is it's only caller and it uses DSPKM state anyways:
<details>
<summary>
diff
</summary>
diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp
index cf0d0033e5..67bdb5597d 100644
--- a/src/wallet/scriptpubkeyman.cpp
+++ b/src/wallet/scriptpubkeyman.cpp
@@ -831,7 +831,7 @@ DescriptorScriptPubKeyMan::DescriptorScriptPubKeyMan(WalletStorage& storage, con
if (!m_map_keys.empty() && !m_map_crypted_keys.empty()) {
throw std::runtime_error("Error: Wallet contains both unencrypted and encrypted keys");
}
- SetCache(m_wallet_descriptor.cache);
+ SetCache();
}
util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type)
@@ -1439,11 +1439,10 @@ uint256 DescriptorScriptPubKeyMan::GetID() const
return m_wallet_descriptor.id;
}
-void DescriptorScriptPubKeyMan::SetCache(const DescriptorCache& cache)
+void DescriptorScriptPubKeyMan::SetCache()
{
LOCK(cs_desc_man);
std::set<CScript> new_spks;
- m_wallet_descriptor.cache = cache;
for (int32_t i = m_wallet_descriptor.range_start; i < m_wallet_descriptor.range_end; ++i) {
FlatSigningProvider out_keys;
std::vector<CScript> scripts_temp;
diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h
index 096e4a57b4..8cc99310bf 100644
--- a/src/wallet/scriptpubkeyman.h
+++ b/src/wallet/scriptpubkeyman.h
@@ -314,7 +314,7 @@ private:
// Fetch the SigningProvider for a given index and optionally include private keys. Called by the above functions.
std::unique_ptr<FlatSigningProvider> GetSigningProvider(int32_t index, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
- void SetCache(const DescriptorCache& cache);
+ void SetCache();
protected:
WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);
</details>