Skip to content

Commit 075aa44

Browse files
committed
wallet: batch descriptor spkm TopUp
Instead of performing multiple atomic write operations per descriptor setup call, batch them all within a single atomic db txn.
1 parent bb4554c commit 075aa44

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,15 @@ std::map<CKeyID, CKey> DescriptorScriptPubKeyMan::GetKeys() const
21352135
}
21362136

21372137
bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
2138+
{
2139+
WalletBatch batch(m_storage.GetDatabase());
2140+
if (!batch.TxnBegin()) return false;
2141+
bool res = TopUpWithDB(batch, size);
2142+
if (!batch.TxnCommit()) throw std::runtime_error(strprintf("Error during descriptors keypool top up. Cannot commit changes for wallet %s", m_storage.GetDisplayName()));
2143+
return res;
2144+
}
2145+
2146+
bool DescriptorScriptPubKeyMan::TopUpWithDB(WalletBatch& batch, unsigned int size)
21382147
{
21392148
LOCK(cs_desc_man);
21402149
unsigned int target_size;
@@ -2157,7 +2166,6 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
21572166
FlatSigningProvider provider;
21582167
provider.keys = GetKeys();
21592168

2160-
WalletBatch batch(m_storage.GetDatabase());
21612169
uint256 id = GetID();
21622170
for (int32_t i = m_max_cached_index + 1; i < new_range_end; ++i) {
21632171
FlatSigningProvider out_keys;
@@ -2326,6 +2334,8 @@ bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(const CExtKey& master_
23262334

23272335
// Store the master private key, and descriptor
23282336
WalletBatch batch(m_storage.GetDatabase());
2337+
if (!batch.TxnBegin()) throw std::runtime_error(std::string(__func__) + ": cannot start db transaction");
2338+
23292339
if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {
23302340
throw std::runtime_error(std::string(__func__) + ": writing descriptor master private key failed");
23312341
}
@@ -2334,9 +2344,11 @@ bool DescriptorScriptPubKeyMan::SetupDescriptorGeneration(const CExtKey& master_
23342344
}
23352345

23362346
// TopUp
2337-
TopUp();
2347+
TopUpWithDB(batch);
23382348

23392349
m_storage.UnsetBlankWalletFlag(batch);
2350+
2351+
if (!batch.TxnCommit()) throw std::runtime_error(std::string(__func__) + ": error committing db transaction");
23402352
return true;
23412353
}
23422354

src/wallet/scriptpubkeyman.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,10 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
583583
std::unique_ptr<FlatSigningProvider> GetSigningProvider(int32_t index, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
584584

585585
protected:
586-
WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);
586+
WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);
587+
588+
//! Same as 'TopUp' but designed for use within a batch transaction context
589+
bool TopUpWithDB(WalletBatch& batch, unsigned int size = 0);
587590

588591
public:
589592
DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size)

0 commit comments

Comments
 (0)