Skip to content

Commit 1f798fe

Browse files
committed
wallet: Cache SigningProviders
In order to avoid constantly re-deriving the same keys in DescriptorScriptPubKeyMan, cache the SigningProviders generated inside of GetSigningProvider.
1 parent 8a105ec commit 1f798fe

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,10 +2075,21 @@ std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvid
20752075
std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(int32_t index, bool include_private) const
20762076
{
20772077
AssertLockHeld(cs_desc_man);
2078-
// Get the scripts, keys, and key origins for this script
2078+
20792079
std::unique_ptr<FlatSigningProvider> out_keys = std::make_unique<FlatSigningProvider>();
2080-
std::vector<CScript> scripts_temp;
2081-
if (!m_wallet_descriptor.descriptor->ExpandFromCache(index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) return nullptr;
2080+
2081+
// Fetch SigningProvider from cache to avoid re-deriving
2082+
auto it = m_map_signing_providers.find(index);
2083+
if (it != m_map_signing_providers.end()) {
2084+
*out_keys = Merge(*out_keys, it->second);
2085+
} else {
2086+
// Get the scripts, keys, and key origins for this script
2087+
std::vector<CScript> scripts_temp;
2088+
if (!m_wallet_descriptor.descriptor->ExpandFromCache(index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) return nullptr;
2089+
2090+
// Cache SigningProvider so we don't need to re-derive if we need this SigningProvider again
2091+
m_map_signing_providers[index] = *out_keys;
2092+
}
20822093

20832094
if (HavePrivateKeys() && include_private) {
20842095
FlatSigningProvider master_provider;

src/wallet/scriptpubkeyman.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,8 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
547547

548548
KeyMap GetKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
549549

550+
// Cached FlatSigningProviders to avoid regenerating them each time they are needed.
551+
mutable std::map<int32_t, FlatSigningProvider> m_map_signing_providers;
550552
// Fetch the SigningProvider for the given script and optionally include private keys
551553
std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CScript& script, bool include_private = false) const;
552554
// Fetch the SigningProvider for the given pubkey and always include private keys. This should only be called by signing code.

0 commit comments

Comments
 (0)