Skip to content

Commit 089e17d

Browse files
committed
Refactor: Move RewriteDB code out of CWallet
This commit does not change behavior.
1 parent 0eac708 commit 089e17d

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,17 @@ bool LegacyScriptPubKeyMan::HavePrivateKeys() const
415415
return !mapKeys.empty() || !mapCryptedKeys.empty();
416416
}
417417

418+
void LegacyScriptPubKeyMan::RewriteDB()
419+
{
420+
AssertLockHeld(cs_wallet);
421+
setInternalKeyPool.clear();
422+
setExternalKeyPool.clear();
423+
m_pool_key_to_index.clear();
424+
// Note: can't top-up keypool here, because wallet is locked.
425+
// User will be prompted to unlock wallet the next operation
426+
// that requires a new key.
427+
}
428+
418429
static int64_t GetOldestKeyTimeInPool(const std::set<int64_t>& setKeyPool, WalletBatch& batch) {
419430
if (setKeyPool.empty()) {
420431
return GetTime();

src/wallet/scriptpubkeyman.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ class ScriptPubKeyMan
176176

177177
virtual bool HavePrivateKeys() const { return false; }
178178

179+
//! The action to do when the DB needs rewrite
180+
virtual void RewriteDB() {}
181+
179182
virtual int64_t GetOldestKeyPoolTime() { return GetTime(); }
180183

181184
virtual size_t KeypoolCountExternalKeys() { return 0; }
@@ -288,6 +291,8 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
288291

289292
bool HavePrivateKeys() const override;
290293

294+
void RewriteDB() override;
295+
291296
int64_t GetOldestKeyPoolTime() override;
292297
size_t KeypoolCountExternalKeys() override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
293298

src/wallet/wallet.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,12 +2903,9 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
29032903
{
29042904
if (database->Rewrite("\x04pool"))
29052905
{
2906-
setInternalKeyPool.clear();
2907-
setExternalKeyPool.clear();
2908-
m_spk_man->m_pool_key_to_index.clear();
2909-
// Note: can't top-up keypool here, because wallet is locked.
2910-
// User will be prompted to unlock wallet the next operation
2911-
// that requires a new key.
2906+
if (auto spk_man = m_spk_man.get()) {
2907+
spk_man->RewriteDB();
2908+
}
29122909
}
29132910
}
29142911

@@ -2940,12 +2937,9 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
29402937
{
29412938
if (database->Rewrite("\x04pool"))
29422939
{
2943-
setInternalKeyPool.clear();
2944-
setExternalKeyPool.clear();
2945-
m_spk_man->m_pool_key_to_index.clear();
2946-
// Note: can't top-up keypool here, because wallet is locked.
2947-
// User will be prompted to unlock wallet the next operation
2948-
// that requires a new key.
2940+
if (auto spk_man = m_spk_man.get()) {
2941+
spk_man->RewriteDB();
2942+
}
29492943
}
29502944
}
29512945

@@ -2964,13 +2958,9 @@ DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
29642958
{
29652959
if (database->Rewrite("\x04pool"))
29662960
{
2967-
LOCK(cs_wallet);
2968-
setInternalKeyPool.clear();
2969-
setExternalKeyPool.clear();
2970-
m_spk_man->m_pool_key_to_index.clear();
2971-
// Note: can't top-up keypool here, because wallet is locked.
2972-
// User will be prompted to unlock wallet the next operation
2973-
// that requires a new key.
2961+
if (auto spk_man = m_spk_man.get()) {
2962+
spk_man->RewriteDB();
2963+
}
29742964
}
29752965
}
29762966

0 commit comments

Comments
 (0)