From 37232332bd7253422ea92a8c9eeb36b12fc84b56 Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Fri, 2 Feb 2024 14:16:49 -0500 Subject: [PATCH] wallet: Cache scriptPubKeys for all DescriptorSPKMs Have CWallet maintain a cache of all known scriptPubKeys for its DescriptorSPKMs in order to improve performance of the functions that require searching for scriptPubKeys. --- src/wallet/wallet.cpp | 5 +++++ src/wallet/wallet.h | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index e98baeff54..a416276d05 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3891,6 +3891,8 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error) if (ExtractDestination(script, dest)) not_migrated_dests.emplace(dest); } + Assume(!m_cached_spks.empty()); + for (auto& desc_spkm : data.desc_spkms) { if (m_spk_managers.count(desc_spkm->GetID()) > 0) { error = _("Error: Duplicate descriptors created during migration. Your wallet may be corrupted."); @@ -4337,6 +4339,9 @@ util::Result MigrateLegacyToDescriptor(const std::string& walle void CWallet::CacheNewScriptPubKeys(const std::set& spks, ScriptPubKeyMan* spkm) { + for (const auto& script : spks) { + m_cached_spks[script].push_back(spkm); + } } void CWallet::TopUpCallback(const std::set& spks, ScriptPubKeyMan* spkm) diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index d13a9308c4..3360d2515a 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -422,6 +422,9 @@ private: // Same as 'AddActiveScriptPubKeyMan' but designed for use within a batch transaction context void AddActiveScriptPubKeyManWithDb(WalletBatch& batch, uint256 id, OutputType type, bool internal); + //! Cache of descriptor ScriptPubKeys used for IsMine. Maps ScriptPubKey to set of spkms + std::unordered_map, SaltedSipHasher> m_cached_spks; + /** * Catch wallet up to current chain, scanning new blocks, updating the best * block locator and m_last_block_processed, and registering for