Skip to content

Commit 0b4c8ef

Browse files
committed
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.
1 parent 976b53b commit 0b4c8ef

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

src/script/descriptor.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,36 @@ bool DescriptorCache::GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t d
14181418
return true;
14191419
}
14201420

1421+
DescriptorCache DescriptorCache::MergeAndDiff(const DescriptorCache& other)
1422+
{
1423+
DescriptorCache diff;
1424+
for (const auto& parent_xpub_pair : other.GetCachedParentExtPubKeys()) {
1425+
CExtPubKey xpub;
1426+
if (GetCachedParentExtPubKey(parent_xpub_pair.first, xpub)) {
1427+
if (xpub != parent_xpub_pair.second) {
1428+
throw std::runtime_error(std::string(__func__) + ": New cached parent xpub does not match already cached parent xpub");
1429+
}
1430+
continue;
1431+
}
1432+
CacheParentExtPubKey(parent_xpub_pair.first, parent_xpub_pair.second);
1433+
diff.CacheParentExtPubKey(parent_xpub_pair.first, parent_xpub_pair.second);
1434+
}
1435+
for (const auto& derived_xpub_map_pair : other.GetCachedDerivedExtPubKeys()) {
1436+
for (const auto& derived_xpub_pair : derived_xpub_map_pair.second) {
1437+
CExtPubKey xpub;
1438+
if (GetCachedDerivedExtPubKey(derived_xpub_map_pair.first, derived_xpub_pair.first, xpub)) {
1439+
if (xpub != derived_xpub_pair.second) {
1440+
throw std::runtime_error(std::string(__func__) + ": New cached derived xpub does not match already cached derived xpub");
1441+
}
1442+
continue;
1443+
}
1444+
CacheDerivedExtPubKey(derived_xpub_map_pair.first, derived_xpub_pair.first, derived_xpub_pair.second);
1445+
diff.CacheDerivedExtPubKey(derived_xpub_map_pair.first, derived_xpub_pair.first, derived_xpub_pair.second);
1446+
}
1447+
}
1448+
return diff;
1449+
}
1450+
14211451
const ExtPubKeyMap DescriptorCache::GetCachedParentExtPubKeys() const
14221452
{
14231453
return m_parent_xpubs;

src/script/descriptor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class DescriptorCache {
5555
const ExtPubKeyMap GetCachedParentExtPubKeys() const;
5656
/** Retrieve all cached derived xpubs */
5757
const std::unordered_map<uint32_t, ExtPubKeyMap> GetCachedDerivedExtPubKeys() const;
58+
59+
/** Combine another DescriptorCache into this one.
60+
* Returns a cache containing the items from the other cache unknown to current cache
61+
*/
62+
DescriptorCache MergeAndDiff(const DescriptorCache& other);
5863
};
5964

6065
/** \brief Interface for parsed descriptor objects.

src/wallet/scriptpubkeyman.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,33 +1805,18 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
18051805
}
18061806
m_map_pubkeys[pubkey] = i;
18071807
}
1808-
// Write the cache
1809-
for (const auto& parent_xpub_pair : temp_cache.GetCachedParentExtPubKeys()) {
1810-
CExtPubKey xpub;
1811-
if (m_wallet_descriptor.cache.GetCachedParentExtPubKey(parent_xpub_pair.first, xpub)) {
1812-
if (xpub != parent_xpub_pair.second) {
1813-
throw std::runtime_error(std::string(__func__) + ": New cached parent xpub does not match already cached parent xpub");
1814-
}
1815-
continue;
1816-
}
1808+
// Merge and write the cache
1809+
DescriptorCache new_items = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
1810+
for (const auto& parent_xpub_pair : new_items.GetCachedParentExtPubKeys()) {
18171811
if (!batch.WriteDescriptorParentCache(parent_xpub_pair.second, id, parent_xpub_pair.first)) {
18181812
throw std::runtime_error(std::string(__func__) + ": writing cache item failed");
18191813
}
1820-
m_wallet_descriptor.cache.CacheParentExtPubKey(parent_xpub_pair.first, parent_xpub_pair.second);
18211814
}
1822-
for (const auto& derived_xpub_map_pair : temp_cache.GetCachedDerivedExtPubKeys()) {
1815+
for (const auto& derived_xpub_map_pair : new_items.GetCachedDerivedExtPubKeys()) {
18231816
for (const auto& derived_xpub_pair : derived_xpub_map_pair.second) {
1824-
CExtPubKey xpub;
1825-
if (m_wallet_descriptor.cache.GetCachedDerivedExtPubKey(derived_xpub_map_pair.first, derived_xpub_pair.first, xpub)) {
1826-
if (xpub != derived_xpub_pair.second) {
1827-
throw std::runtime_error(std::string(__func__) + ": New cached derived xpub does not match already cached derived xpub");
1828-
}
1829-
continue;
1830-
}
18311817
if (!batch.WriteDescriptorDerivedCache(derived_xpub_pair.second, id, derived_xpub_map_pair.first, derived_xpub_pair.first)) {
18321818
throw std::runtime_error(std::string(__func__) + ": writing cache item failed");
18331819
}
1834-
m_wallet_descriptor.cache.CacheDerivedExtPubKey(derived_xpub_map_pair.first, derived_xpub_pair.first, derived_xpub_pair.second);
18351820
}
18361821
}
18371822
m_max_cached_index++;

0 commit comments

Comments
 (0)