Skip to content

Commit 8b0d82b

Browse files
committed
Refactor: Move Upgrade code out of CWallet::CreateWalletFromFile
This commit does not change behavior.
1 parent 46865ec commit 8b0d82b

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,41 @@ bool LegacyScriptPubKeyMan::CanGetAddresses(bool internal)
361361
return keypool_has_keys;
362362
}
363363

364+
bool LegacyScriptPubKeyMan::Upgrade(int prev_version, std::string& error)
365+
{
366+
AssertLockHeld(cs_wallet);
367+
error = "";
368+
bool hd_upgrade = false;
369+
bool split_upgrade = false;
370+
if (m_storage.CanSupportFeature(FEATURE_HD) && !IsHDEnabled()) {
371+
WalletLogPrintf("Upgrading wallet to HD\n");
372+
m_storage.SetMinVersion(FEATURE_HD);
373+
374+
// generate a new master key
375+
CPubKey masterPubKey = GenerateNewSeed();
376+
SetHDSeed(masterPubKey);
377+
hd_upgrade = true;
378+
}
379+
// Upgrade to HD chain split if necessary
380+
if (m_storage.CanSupportFeature(FEATURE_HD_SPLIT)) {
381+
WalletLogPrintf("Upgrading wallet to use HD chain split\n");
382+
m_storage.SetMinVersion(FEATURE_PRE_SPLIT_KEYPOOL);
383+
split_upgrade = FEATURE_HD_SPLIT > prev_version;
384+
}
385+
// Mark all keys currently in the keypool as pre-split
386+
if (split_upgrade) {
387+
MarkPreSplitKeys();
388+
}
389+
// Regenerate the keypool if upgraded to HD
390+
if (hd_upgrade) {
391+
if (!TopUpKeyPool()) {
392+
error = _("Unable to generate keys").translated;
393+
return false;
394+
}
395+
}
396+
return true;
397+
}
398+
364399
static int64_t GetOldestKeyTimeInPool(const std::set<int64_t>& setKeyPool, WalletBatch& batch) {
365400
if (setKeyPool.empty()) {
366401
return GetTime();

src/wallet/scriptpubkeyman.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ class ScriptPubKeyMan
165165
/* Returns true if the wallet can give out new addresses. This means it has keys in the keypool or can generate new keys */
166166
virtual bool CanGetAddresses(bool internal = false) { return false; }
167167

168+
/** Upgrades the wallet to the specified version */
169+
virtual bool Upgrade(int prev_version, std::string& error) { return false; }
170+
168171
virtual int64_t GetOldestKeyPoolTime() { return GetTime(); }
169172

170173
virtual size_t KeypoolCountExternalKeys() { return 0; }
@@ -271,6 +274,8 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
271274

272275
bool IsHDEnabled() const override;
273276

277+
bool Upgrade(int prev_version, std::string& error) override;
278+
274279
int64_t GetOldestKeyPoolTime() override;
275280
size_t KeypoolCountExternalKeys() override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
276281

src/wallet/wallet.cpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,31 +3614,10 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
36143614
return nullptr;
36153615
}
36163616

3617-
bool hd_upgrade = false;
3618-
bool split_upgrade = false;
3619-
if (walletInstance->CanSupportFeature(FEATURE_HD) && !walletInstance->m_spk_man->IsHDEnabled()) {
3620-
walletInstance->WalletLogPrintf("Upgrading wallet to HD\n");
3621-
walletInstance->SetMinVersion(FEATURE_HD);
3622-
3623-
// generate a new master key
3624-
CPubKey masterPubKey = walletInstance->m_spk_man->GenerateNewSeed();
3625-
walletInstance->m_spk_man->SetHDSeed(masterPubKey);
3626-
hd_upgrade = true;
3627-
}
3628-
// Upgrade to HD chain split if necessary
3629-
if (walletInstance->CanSupportFeature(FEATURE_HD_SPLIT)) {
3630-
walletInstance->WalletLogPrintf("Upgrading wallet to use HD chain split\n");
3631-
walletInstance->SetMinVersion(FEATURE_PRE_SPLIT_KEYPOOL);
3632-
split_upgrade = FEATURE_HD_SPLIT > prev_version;
3633-
}
3634-
// Mark all keys currently in the keypool as pre-split
3635-
if (split_upgrade) {
3636-
walletInstance->MarkPreSplitKeys();
3637-
}
3638-
// Regenerate the keypool if upgraded to HD
3639-
if (hd_upgrade) {
3640-
if (!walletInstance->m_spk_man->TopUp()) {
3641-
error = _("Unable to generate keys").translated;
3617+
if (auto spk_man = walletInstance->m_spk_man.get()) {
3618+
std::string error;
3619+
if (!spk_man->Upgrade(prev_version, error)) {
3620+
chain.initError(error);
36423621
return nullptr;
36433622
}
36443623
}

src/wallet/wallet.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,6 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
11341134
std::set<int64_t>& setInternalKeyPool GUARDED_BY(cs_wallet) = m_spk_man->setInternalKeyPool;
11351135
std::set<int64_t>& setExternalKeyPool GUARDED_BY(cs_wallet) = m_spk_man->setExternalKeyPool;
11361136
int64_t& nTimeFirstKey GUARDED_BY(cs_wallet) = m_spk_man->nTimeFirstKey;
1137-
void MarkPreSplitKeys() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(m_spk_man->cs_wallet); m_spk_man->MarkPreSplitKeys(); }
11381137
using CryptedKeyMap = LegacyScriptPubKeyMan::CryptedKeyMap;
11391138
};
11401139

0 commit comments

Comments
 (0)