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.
This commit is contained in:
Ava Chow 2024-02-02 14:16:49 -05:00
parent 99a0cddbc0
commit 37232332bd
2 changed files with 8 additions and 0 deletions

View file

@ -3891,6 +3891,8 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
if (ExtractDestination(script, dest)) not_migrated_dests.emplace(dest); if (ExtractDestination(script, dest)) not_migrated_dests.emplace(dest);
} }
Assume(!m_cached_spks.empty());
for (auto& desc_spkm : data.desc_spkms) { for (auto& desc_spkm : data.desc_spkms) {
if (m_spk_managers.count(desc_spkm->GetID()) > 0) { if (m_spk_managers.count(desc_spkm->GetID()) > 0) {
error = _("Error: Duplicate descriptors created during migration. Your wallet may be corrupted."); error = _("Error: Duplicate descriptors created during migration. Your wallet may be corrupted.");
@ -4337,6 +4339,9 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
void CWallet::CacheNewScriptPubKeys(const std::set<CScript>& spks, ScriptPubKeyMan* spkm) void CWallet::CacheNewScriptPubKeys(const std::set<CScript>& spks, ScriptPubKeyMan* spkm)
{ {
for (const auto& script : spks) {
m_cached_spks[script].push_back(spkm);
}
} }
void CWallet::TopUpCallback(const std::set<CScript>& spks, ScriptPubKeyMan* spkm) void CWallet::TopUpCallback(const std::set<CScript>& spks, ScriptPubKeyMan* spkm)

View file

@ -422,6 +422,9 @@ private:
// Same as 'AddActiveScriptPubKeyMan' but designed for use within a batch transaction context // Same as 'AddActiveScriptPubKeyMan' but designed for use within a batch transaction context
void AddActiveScriptPubKeyManWithDb(WalletBatch& batch, uint256 id, OutputType type, bool internal); 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<CScript, std::vector<ScriptPubKeyMan*>, SaltedSipHasher> m_cached_spks;
/** /**
* Catch wallet up to current chain, scanning new blocks, updating the best * Catch wallet up to current chain, scanning new blocks, updating the best
* block locator and m_last_block_processed, and registering for * block locator and m_last_block_processed, and registering for