Skip to content

Commit 3eb769f

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

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ bool LegacyScriptPubKeyMan::TopUpInactiveHDChain(const CKeyID seed_id, int64_t i
333333
chain.m_next_external_index = std::max(chain.m_next_external_index, index + 1);
334334
}
335335

336-
TopUpChain(chain, 0);
336+
WalletBatch batch(m_storage.GetDatabase());
337+
TopUpChain(batch, chain, 0);
337338

338339
return true;
339340
}
@@ -1274,19 +1275,22 @@ bool LegacyScriptPubKeyMan::TopUp(unsigned int kpSize)
12741275
return false;
12751276
}
12761277

1277-
if (!TopUpChain(m_hd_chain, kpSize)) {
1278+
WalletBatch batch(m_storage.GetDatabase());
1279+
if (!batch.TxnBegin()) return false;
1280+
if (!TopUpChain(batch, m_hd_chain, kpSize)) {
12781281
return false;
12791282
}
12801283
for (auto& [chain_id, chain] : m_inactive_hd_chains) {
1281-
if (!TopUpChain(chain, kpSize)) {
1284+
if (!TopUpChain(batch, chain, kpSize)) {
12821285
return false;
12831286
}
12841287
}
1288+
if (!batch.TxnCommit()) throw std::runtime_error(strprintf("Error during keypool top up. Cannot commit changes for wallet %s", m_storage.GetDisplayName()));
12851289
NotifyCanGetAddressesChanged();
12861290
return true;
12871291
}
12881292

1289-
bool LegacyScriptPubKeyMan::TopUpChain(CHDChain& chain, unsigned int kpSize)
1293+
bool LegacyScriptPubKeyMan::TopUpChain(WalletBatch& batch, CHDChain& chain, unsigned int kpSize)
12901294
{
12911295
LOCK(cs_KeyStore);
12921296

@@ -1318,7 +1322,6 @@ bool LegacyScriptPubKeyMan::TopUpChain(CHDChain& chain, unsigned int kpSize)
13181322
missingInternal = 0;
13191323
}
13201324
bool internal = false;
1321-
WalletBatch batch(m_storage.GetDatabase());
13221325
for (int64_t i = missingInternal + missingExternal; i--;) {
13231326
if (i < missingInternal) {
13241327
internal = true;

src/wallet/scriptpubkeyman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
366366
*/
367367
bool TopUpInactiveHDChain(const CKeyID seed_id, int64_t index, bool internal);
368368

369-
bool TopUpChain(CHDChain& chain, unsigned int size);
369+
bool TopUpChain(WalletBatch& batch, CHDChain& chain, unsigned int size);
370370
public:
371371
LegacyScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size) : ScriptPubKeyMan(storage), m_keypool_size(keypool_size) {}
372372

0 commit comments

Comments
 (0)