Skip to content

Commit 3723233

Browse files
committed
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.
1 parent 99a0cdd commit 3723233

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/wallet/wallet.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3891,6 +3891,8 @@ bool CWallet::ApplyMigrationData(MigrationData& data, bilingual_str& error)
38913891
if (ExtractDestination(script, dest)) not_migrated_dests.emplace(dest);
38923892
}
38933893

3894+
Assume(!m_cached_spks.empty());
3895+
38943896
for (auto& desc_spkm : data.desc_spkms) {
38953897
if (m_spk_managers.count(desc_spkm->GetID()) > 0) {
38963898
error = _("Error: Duplicate descriptors created during migration. Your wallet may be corrupted.");
@@ -4337,6 +4339,9 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& walle
43374339

43384340
void CWallet::CacheNewScriptPubKeys(const std::set<CScript>& spks, ScriptPubKeyMan* spkm)
43394341
{
4342+
for (const auto& script : spks) {
4343+
m_cached_spks[script].push_back(spkm);
4344+
}
43404345
}
43414346

43424347
void CWallet::TopUpCallback(const std::set<CScript>& spks, ScriptPubKeyMan* spkm)

src/wallet/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
422422
// Same as 'AddActiveScriptPubKeyMan' but designed for use within a batch transaction context
423423
void AddActiveScriptPubKeyManWithDb(WalletBatch& batch, uint256 id, OutputType type, bool internal);
424424

425+
//! Cache of descriptor ScriptPubKeys used for IsMine. Maps ScriptPubKey to set of spkms
426+
std::unordered_map<CScript, std::vector<ScriptPubKeyMan*>, SaltedSipHasher> m_cached_spks;
427+
425428
/**
426429
* Catch wallet up to current chain, scanning new blocks, updating the best
427430
* block locator and m_last_block_processed, and registering for

0 commit comments

Comments
 (0)