Skip to content

Commit 9c16b17

Browse files
committed
Move wallet upgrading to its own function
1 parent 6ef45bc commit 9c16b17

File tree

2 files changed

+47
-36
lines changed

2 files changed

+47
-36
lines changed

src/wallet/wallet.cpp

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,42 +3830,8 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
38303830
}
38313831
}
38323832

3833-
int prev_version = walletInstance->GetVersion();
3834-
if (gArgs.GetBoolArg("-upgradewallet", fFirstRun))
3835-
{
3836-
int nMaxVersion = gArgs.GetArg("-upgradewallet", 0);
3837-
if (nMaxVersion == 0) // the -upgradewallet without argument case
3838-
{
3839-
walletInstance->WalletLogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
3840-
nMaxVersion = FEATURE_LATEST;
3841-
walletInstance->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
3842-
}
3843-
else
3844-
walletInstance->WalletLogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
3845-
if (nMaxVersion < walletInstance->GetVersion())
3846-
{
3847-
error = _("Cannot downgrade wallet").translated;
3848-
return nullptr;
3849-
}
3850-
walletInstance->SetMaxVersion(nMaxVersion);
3851-
}
3852-
3853-
// Upgrade to HD if explicit upgrade
3854-
if (gArgs.GetBoolArg("-upgradewallet", false)) {
3855-
LOCK(walletInstance->cs_wallet);
3856-
3857-
// Do not upgrade versions to any version between HD_SPLIT and FEATURE_PRE_SPLIT_KEYPOOL unless already supporting HD_SPLIT
3858-
int max_version = walletInstance->GetVersion();
3859-
if (!walletInstance->CanSupportFeature(FEATURE_HD_SPLIT) && max_version >= FEATURE_HD_SPLIT && max_version < FEATURE_PRE_SPLIT_KEYPOOL) {
3860-
error = _("Cannot upgrade a non HD split wallet without upgrading to support pre split keypool. Please use -upgradewallet=169900 or -upgradewallet with no version specified.").translated;
3861-
return nullptr;
3862-
}
3863-
3864-
for (auto spk_man : walletInstance->GetActiveScriptPubKeyMans()) {
3865-
if (!spk_man->Upgrade(prev_version, error)) {
3866-
return nullptr;
3867-
}
3868-
}
3833+
if (!UpgradeWallet(walletInstance, fFirstRun, error, warnings)) {
3834+
return nullptr;
38693835
}
38703836

38713837
if (fFirstRun)
@@ -4129,6 +4095,48 @@ const CAddressBookData* CWallet::FindAddressBookEntry(const CTxDestination& dest
41294095
return &address_book_it->second;
41304096
}
41314097

4098+
bool CWallet::UpgradeWallet(std::shared_ptr<CWallet> walletInstance, bool fFirstRun, std::string& error, std::vector<std::string>& warnings)
4099+
{
4100+
int prev_version = walletInstance->GetVersion();
4101+
if (gArgs.GetBoolArg("-upgradewallet", fFirstRun))
4102+
{
4103+
int nMaxVersion = gArgs.GetArg("-upgradewallet", 0);
4104+
if (nMaxVersion == 0) // the -upgradewallet without argument case
4105+
{
4106+
walletInstance->WalletLogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
4107+
nMaxVersion = FEATURE_LATEST;
4108+
walletInstance->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
4109+
}
4110+
else
4111+
walletInstance->WalletLogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
4112+
if (nMaxVersion < walletInstance->GetVersion())
4113+
{
4114+
error = _("Cannot downgrade wallet").translated;
4115+
return false;
4116+
}
4117+
walletInstance->SetMaxVersion(nMaxVersion);
4118+
}
4119+
4120+
// Upgrade to HD if explicit upgrade
4121+
if (gArgs.GetBoolArg("-upgradewallet", false)) {
4122+
LOCK(walletInstance->cs_wallet);
4123+
4124+
// Do not upgrade versions to any version between HD_SPLIT and FEATURE_PRE_SPLIT_KEYPOOL unless already supporting HD_SPLIT
4125+
int max_version = walletInstance->GetVersion();
4126+
if (!walletInstance->CanSupportFeature(FEATURE_HD_SPLIT) && max_version >= FEATURE_HD_SPLIT && max_version < FEATURE_PRE_SPLIT_KEYPOOL) {
4127+
error = _("Cannot upgrade a non HD split wallet without upgrading to support pre split keypool. Please use -upgradewallet=169900 or -upgradewallet with no version specified.").translated;
4128+
return false;
4129+
}
4130+
4131+
for (auto spk_man : walletInstance->GetActiveScriptPubKeyMans()) {
4132+
if (!spk_man->Upgrade(prev_version, error)) {
4133+
return false;
4134+
}
4135+
}
4136+
}
4137+
return true;
4138+
}
4139+
41324140
void CWallet::postInitProcess()
41334141
{
41344142
auto locked_chain = chain().lock();

src/wallet/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
11751175
LogPrintf(("%s " + fmt).c_str(), GetDisplayName(), parameters...);
11761176
};
11771177

1178+
/** Upgrade the wallet */
1179+
static bool UpgradeWallet(std::shared_ptr<CWallet> wallet, bool first_run, std::string& error, std::vector<std::string>& warnings);
1180+
11781181
//! Returns all unique ScriptPubKeyMans in m_internal_spk_managers and m_external_spk_managers
11791182
std::set<ScriptPubKeyMan*> GetActiveScriptPubKeyMans() const;
11801183

0 commit comments

Comments
 (0)