mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-18 21:01:22 +02:00
Refactor Cache merging and writing
Instead of having a large blob of cache merging code in TopUp, refactor this into DescriptorCache so that it can merge and provide a diff (another DescriptorCache containing just the items that were added). Then TopUp can just write everything that was in the diff.
This commit is contained in:
parent
976b53b085
commit
0b4c8ef75c
@ -1418,6 +1418,36 @@ bool DescriptorCache::GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t d
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DescriptorCache DescriptorCache::MergeAndDiff(const DescriptorCache& other)
|
||||||
|
{
|
||||||
|
DescriptorCache diff;
|
||||||
|
for (const auto& parent_xpub_pair : other.GetCachedParentExtPubKeys()) {
|
||||||
|
CExtPubKey xpub;
|
||||||
|
if (GetCachedParentExtPubKey(parent_xpub_pair.first, xpub)) {
|
||||||
|
if (xpub != parent_xpub_pair.second) {
|
||||||
|
throw std::runtime_error(std::string(__func__) + ": New cached parent xpub does not match already cached parent xpub");
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
CacheParentExtPubKey(parent_xpub_pair.first, parent_xpub_pair.second);
|
||||||
|
diff.CacheParentExtPubKey(parent_xpub_pair.first, parent_xpub_pair.second);
|
||||||
|
}
|
||||||
|
for (const auto& derived_xpub_map_pair : other.GetCachedDerivedExtPubKeys()) {
|
||||||
|
for (const auto& derived_xpub_pair : derived_xpub_map_pair.second) {
|
||||||
|
CExtPubKey xpub;
|
||||||
|
if (GetCachedDerivedExtPubKey(derived_xpub_map_pair.first, derived_xpub_pair.first, xpub)) {
|
||||||
|
if (xpub != derived_xpub_pair.second) {
|
||||||
|
throw std::runtime_error(std::string(__func__) + ": New cached derived xpub does not match already cached derived xpub");
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
CacheDerivedExtPubKey(derived_xpub_map_pair.first, derived_xpub_pair.first, derived_xpub_pair.second);
|
||||||
|
diff.CacheDerivedExtPubKey(derived_xpub_map_pair.first, derived_xpub_pair.first, derived_xpub_pair.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return diff;
|
||||||
|
}
|
||||||
|
|
||||||
const ExtPubKeyMap DescriptorCache::GetCachedParentExtPubKeys() const
|
const ExtPubKeyMap DescriptorCache::GetCachedParentExtPubKeys() const
|
||||||
{
|
{
|
||||||
return m_parent_xpubs;
|
return m_parent_xpubs;
|
||||||
|
@ -55,6 +55,11 @@ public:
|
|||||||
const ExtPubKeyMap GetCachedParentExtPubKeys() const;
|
const ExtPubKeyMap GetCachedParentExtPubKeys() const;
|
||||||
/** Retrieve all cached derived xpubs */
|
/** Retrieve all cached derived xpubs */
|
||||||
const std::unordered_map<uint32_t, ExtPubKeyMap> GetCachedDerivedExtPubKeys() const;
|
const std::unordered_map<uint32_t, ExtPubKeyMap> GetCachedDerivedExtPubKeys() const;
|
||||||
|
|
||||||
|
/** Combine another DescriptorCache into this one.
|
||||||
|
* Returns a cache containing the items from the other cache unknown to current cache
|
||||||
|
*/
|
||||||
|
DescriptorCache MergeAndDiff(const DescriptorCache& other);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** \brief Interface for parsed descriptor objects.
|
/** \brief Interface for parsed descriptor objects.
|
||||||
|
@ -1805,33 +1805,18 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
|
|||||||
}
|
}
|
||||||
m_map_pubkeys[pubkey] = i;
|
m_map_pubkeys[pubkey] = i;
|
||||||
}
|
}
|
||||||
// Write the cache
|
// Merge and write the cache
|
||||||
for (const auto& parent_xpub_pair : temp_cache.GetCachedParentExtPubKeys()) {
|
DescriptorCache new_items = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
|
||||||
CExtPubKey xpub;
|
for (const auto& parent_xpub_pair : new_items.GetCachedParentExtPubKeys()) {
|
||||||
if (m_wallet_descriptor.cache.GetCachedParentExtPubKey(parent_xpub_pair.first, xpub)) {
|
|
||||||
if (xpub != parent_xpub_pair.second) {
|
|
||||||
throw std::runtime_error(std::string(__func__) + ": New cached parent xpub does not match already cached parent xpub");
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!batch.WriteDescriptorParentCache(parent_xpub_pair.second, id, parent_xpub_pair.first)) {
|
if (!batch.WriteDescriptorParentCache(parent_xpub_pair.second, id, parent_xpub_pair.first)) {
|
||||||
throw std::runtime_error(std::string(__func__) + ": writing cache item failed");
|
throw std::runtime_error(std::string(__func__) + ": writing cache item failed");
|
||||||
}
|
}
|
||||||
m_wallet_descriptor.cache.CacheParentExtPubKey(parent_xpub_pair.first, parent_xpub_pair.second);
|
|
||||||
}
|
}
|
||||||
for (const auto& derived_xpub_map_pair : temp_cache.GetCachedDerivedExtPubKeys()) {
|
for (const auto& derived_xpub_map_pair : new_items.GetCachedDerivedExtPubKeys()) {
|
||||||
for (const auto& derived_xpub_pair : derived_xpub_map_pair.second) {
|
for (const auto& derived_xpub_pair : derived_xpub_map_pair.second) {
|
||||||
CExtPubKey xpub;
|
|
||||||
if (m_wallet_descriptor.cache.GetCachedDerivedExtPubKey(derived_xpub_map_pair.first, derived_xpub_pair.first, xpub)) {
|
|
||||||
if (xpub != derived_xpub_pair.second) {
|
|
||||||
throw std::runtime_error(std::string(__func__) + ": New cached derived xpub does not match already cached derived xpub");
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!batch.WriteDescriptorDerivedCache(derived_xpub_pair.second, id, derived_xpub_map_pair.first, derived_xpub_pair.first)) {
|
if (!batch.WriteDescriptorDerivedCache(derived_xpub_pair.second, id, derived_xpub_map_pair.first, derived_xpub_pair.first)) {
|
||||||
throw std::runtime_error(std::string(__func__) + ": writing cache item failed");
|
throw std::runtime_error(std::string(__func__) + ": writing cache item failed");
|
||||||
}
|
}
|
||||||
m_wallet_descriptor.cache.CacheDerivedExtPubKey(derived_xpub_map_pair.first, derived_xpub_pair.first, derived_xpub_pair.second);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_max_cached_index++;
|
m_max_cached_index++;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user