Skip to content

Commit 1833237

Browse files
committed
Only run UpgradeWallet if the wallet needs to be upgraded
1 parent 9c16b17 commit 1833237

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

src/wallet/wallet.cpp

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

3833-
if (!UpgradeWallet(walletInstance, fFirstRun, error, warnings)) {
3834-
return nullptr;
3833+
if (gArgs.GetBoolArg("-upgradewallet", false)) {
3834+
if (!UpgradeWallet(walletInstance, error, warnings)) {
3835+
return nullptr;
3836+
}
38353837
}
38363838

38373839
if (fFirstRun)
@@ -4095,38 +4097,33 @@ const CAddressBookData* CWallet::FindAddressBookEntry(const CTxDestination& dest
40954097
return &address_book_it->second;
40964098
}
40974099

4098-
bool CWallet::UpgradeWallet(std::shared_ptr<CWallet> walletInstance, bool fFirstRun, std::string& error, std::vector<std::string>& warnings)
4100+
bool CWallet::UpgradeWallet(std::shared_ptr<CWallet> walletInstance, std::string& error, std::vector<std::string>& warnings)
40994101
{
41004102
int prev_version = walletInstance->GetVersion();
4101-
if (gArgs.GetBoolArg("-upgradewallet", fFirstRun))
4103+
int nMaxVersion = gArgs.GetArg("-upgradewallet", 0);
4104+
if (nMaxVersion == 0) // the -upgradewallet without argument case
41024105
{
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);
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
41184109
}
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);
41194118

4120-
// Upgrade to HD if explicit upgrade
4121-
if (gArgs.GetBoolArg("-upgradewallet", false)) {
4122-
LOCK(walletInstance->cs_wallet);
4119+
LOCK(walletInstance->cs_wallet);
41234120

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-
}
4121+
// Do not upgrade versions to any version between HD_SPLIT and FEATURE_PRE_SPLIT_KEYPOOL unless already supporting HD_SPLIT
4122+
int max_version = walletInstance->GetVersion();
4123+
if (!walletInstance->CanSupportFeature(FEATURE_HD_SPLIT) && max_version >= FEATURE_HD_SPLIT && max_version < FEATURE_PRE_SPLIT_KEYPOOL) {
4124+
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;
4125+
return false;
4126+
}
41304127

41314128
for (auto spk_man : walletInstance->GetActiveScriptPubKeyMans()) {
41324129
if (!spk_man->Upgrade(prev_version, error)) {

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
11761176
};
11771177

11781178
/** Upgrade the wallet */
1179-
static bool UpgradeWallet(std::shared_ptr<CWallet> wallet, bool first_run, std::string& error, std::vector<std::string>& warnings);
1179+
static bool UpgradeWallet(std::shared_ptr<CWallet> wallet, std::string& error, std::vector<std::string>& warnings);
11801180

11811181
//! Returns all unique ScriptPubKeyMans in m_internal_spk_managers and m_external_spk_managers
11821182
std::set<ScriptPubKeyMan*> GetActiveScriptPubKeyMans() const;

0 commit comments

Comments
 (0)