mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 22:18:54 +01:00
Merge bitcoin/bitcoin#26008: wallet: cache IsMine scriptPubKeys to improve performance of descriptor wallets
e041ed9b75wallet: Retrieve ID from loaded DescSPKM directly (Ava Chow)39640dd34ewallet: Use scriptPubKeyCache in GetSolvingProvider (Ava Chow)b410f68791wallet: Use scriptPubKey cache in GetScriptPubKeyMans (Ava Chow)edf4e73a16wallet: Use scriptPubKey cache in IsMine (Ava Chow)37232332bdwallet: Cache scriptPubKeys for all DescriptorSPKMs (Ava Chow)99a0cddbc0wallet: Introduce a callback called after TopUp completes (Ava Chow)b276825932bench: Add a benchmark for ismine (Ava Chow) Pull request description: Wallets that have a ton of non-ranged descriptors (such as a migrated non-HD wallet) perform fairly poorly due to looping through all of the wallet's `ScriptPubKeyMan`s. This is done in various places, such as `IsMine`, and helper functions for fetching a `ScriptPubKeyMan` and a `SolvingProvider`. This also has a bit of a performance impact on standard descriptor wallets, although less noticeable due to the small number of SPKMs. As these functions are based on doing `IsMine` for each `ScriptPubKeyMan`, we can improve this performance by caching `IsMine` scriptPubKeys for all descriptors and use that to determine which `ScriptPubKeyMan` to actually use for those things. This cache is used exclusively and we no longer iterate the SPKMs. Also added a benchmark for `IsMine`. ACKs for top commit: ryanofsky: Code review ACKe041ed9b75. Just suggested changes since last review josibake: ACKe041ed9b75furszy: Code review ACKe041ed9bTree-SHA512: 8e7081991a025e682e9dea838b4543b0d179832d1c47397fb9fe7a97fa01eb699c15a5d5a785634926844fc83a46e6ac07ef753119f39d84423220ef8a548894
This commit is contained in:
@@ -1288,6 +1288,9 @@ bool LegacyScriptPubKeyMan::TopUp(unsigned int kpSize)
|
||||
}
|
||||
if (!batch.TxnCommit()) throw std::runtime_error(strprintf("Error during keypool top up. Cannot commit changes for wallet %s", m_storage.GetDisplayName()));
|
||||
NotifyCanGetAddressesChanged();
|
||||
// Note: Unlike with DescriptorSPKM, LegacySPKM does not need to call
|
||||
// m_storage.TopUpCallback() as we do not know what new scripts the LegacySPKM is
|
||||
// watching for. CWallet's scriptPubKey cache is not used for LegacySPKMs.
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2152,6 +2155,7 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
|
||||
bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int size)
|
||||
{
|
||||
LOCK(cs_desc_man);
|
||||
std::set<CScript> new_spks;
|
||||
unsigned int target_size;
|
||||
if (size > 0) {
|
||||
target_size = size;
|
||||
@@ -2182,6 +2186,7 @@ bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int siz
|
||||
if (!m_wallet_descriptor.descriptor->Expand(i, provider, scripts_temp, out_keys, &temp_cache)) return false;
|
||||
}
|
||||
// Add all of the scriptPubKeys to the scriptPubKey set
|
||||
new_spks.insert(scripts_temp.begin(), scripts_temp.end());
|
||||
for (const CScript& script : scripts_temp) {
|
||||
m_map_script_pub_keys[script] = i;
|
||||
}
|
||||
@@ -2207,6 +2212,7 @@ bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int siz
|
||||
// By this point, the cache size should be the size of the entire range
|
||||
assert(m_wallet_descriptor.range_end - 1 == m_max_cached_index);
|
||||
|
||||
m_storage.TopUpCallback(new_spks, this);
|
||||
NotifyCanGetAddressesChanged();
|
||||
return true;
|
||||
}
|
||||
@@ -2620,6 +2626,7 @@ uint256 DescriptorScriptPubKeyMan::GetID() const
|
||||
void DescriptorScriptPubKeyMan::SetCache(const DescriptorCache& cache)
|
||||
{
|
||||
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;
|
||||
@@ -2628,6 +2635,7 @@ void DescriptorScriptPubKeyMan::SetCache(const DescriptorCache& cache)
|
||||
throw std::runtime_error("Error: Unable to expand wallet descriptor from cache");
|
||||
}
|
||||
// Add all of the scriptPubKeys to the scriptPubKey set
|
||||
new_spks.insert(scripts_temp.begin(), scripts_temp.end());
|
||||
for (const CScript& script : scripts_temp) {
|
||||
if (m_map_script_pub_keys.count(script) != 0) {
|
||||
throw std::runtime_error(strprintf("Error: Already loaded script at index %d as being at index %d", i, m_map_script_pub_keys[script]));
|
||||
@@ -2645,6 +2653,8 @@ void DescriptorScriptPubKeyMan::SetCache(const DescriptorCache& cache)
|
||||
}
|
||||
m_max_cached_index++;
|
||||
}
|
||||
// Make sure the wallet knows about our new spks
|
||||
m_storage.TopUpCallback(new_spks, this);
|
||||
}
|
||||
|
||||
bool DescriptorScriptPubKeyMan::AddKey(const CKeyID& key_id, const CKey& key)
|
||||
|
||||
Reference in New Issue
Block a user