Skip to content

Commit cacc391

Browse files
committed
Move DescriptorCache writing to WalletBatch
Instead of adhoc writing of the items in DescriptorCache, move it all into WalletBatch.
1 parent 0b4c8ef commit cacc391

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,17 +1807,8 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
18071807
}
18081808
// Merge and write the cache
18091809
DescriptorCache new_items = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
1810-
for (const auto& parent_xpub_pair : new_items.GetCachedParentExtPubKeys()) {
1811-
if (!batch.WriteDescriptorParentCache(parent_xpub_pair.second, id, parent_xpub_pair.first)) {
1812-
throw std::runtime_error(std::string(__func__) + ": writing cache item failed");
1813-
}
1814-
}
1815-
for (const auto& derived_xpub_map_pair : new_items.GetCachedDerivedExtPubKeys()) {
1816-
for (const auto& derived_xpub_pair : derived_xpub_map_pair.second) {
1817-
if (!batch.WriteDescriptorDerivedCache(derived_xpub_pair.second, id, derived_xpub_map_pair.first, derived_xpub_pair.first)) {
1818-
throw std::runtime_error(std::string(__func__) + ": writing cache item failed");
1819-
}
1820-
}
1810+
if (!batch.WriteDescriptorCacheItems(id, new_items)) {
1811+
throw std::runtime_error(std::string(__func__) + ": writing cache items failed");
18211812
}
18221813
m_max_cached_index++;
18231814
}

src/wallet/walletdb.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,23 @@ bool WalletBatch::WriteDescriptorParentCache(const CExtPubKey& xpub, const uint2
248248
return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORCACHE, desc_id), key_exp_index), ser_xpub);
249249
}
250250

251+
bool WalletBatch::WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache)
252+
{
253+
for (const auto& parent_xpub_pair : cache.GetCachedParentExtPubKeys()) {
254+
if (!WriteDescriptorParentCache(parent_xpub_pair.second, desc_id, parent_xpub_pair.first)) {
255+
return false;
256+
}
257+
}
258+
for (const auto& derived_xpub_map_pair : cache.GetCachedDerivedExtPubKeys()) {
259+
for (const auto& derived_xpub_pair : derived_xpub_map_pair.second) {
260+
if (!WriteDescriptorDerivedCache(derived_xpub_pair.second, desc_id, derived_xpub_map_pair.first, derived_xpub_pair.first)) {
261+
return false;
262+
}
263+
}
264+
}
265+
return true;
266+
}
267+
251268
class CWalletScanState {
252269
public:
253270
unsigned int nKeys{0};

src/wallet/walletdb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ class WalletBatch
246246
bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
247247
bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
248248
bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
249+
bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);
249250

250251
/// Write destination data key,value tuple to database
251252
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);

0 commit comments

Comments
 (0)