Skip to content

Commit 8e32e1c

Browse files
committed
wallet: remove nWalletMaxVersion
nWalletMaxVersion was used to allow an upgrade to a version only when the new feature was used. This makes sense for the old -upgradewallet startup option. But because upgradewallet is now a RPC, putting off the version bump like this does not make sense. Instead, immediately upgrading to the given version number makes sense.
1 parent bd7398c commit 8e32e1c

File tree

3 files changed

+16
-43
lines changed

3 files changed

+16
-43
lines changed

src/wallet/scriptpubkeyman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class WalletStorage
3737
virtual bool IsWalletFlagSet(uint64_t) const = 0;
3838
virtual void UnsetBlankWalletFlag(WalletBatch&) = 0;
3939
virtual bool CanSupportFeature(enum WalletFeature) const = 0;
40-
virtual void SetMinVersion(enum WalletFeature, WalletBatch* = nullptr, bool = false) = 0;
40+
virtual void SetMinVersion(enum WalletFeature, WalletBatch* = nullptr) = 0;
4141
virtual const CKeyingMaterial& GetEncryptionKey() const = 0;
4242
virtual bool HasEncryptionKeys() const = 0;
4343
virtual bool IsLocked() const = 0;

src/wallet/wallet.cpp

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -435,21 +435,13 @@ void CWallet::chainStateFlushed(const CBlockLocator& loc)
435435
batch.WriteBestBlock(loc);
436436
}
437437

438-
void CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch* batch_in, bool fExplicit)
438+
void CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch* batch_in)
439439
{
440440
LOCK(cs_wallet);
441441
if (nWalletVersion >= nVersion)
442442
return;
443-
444-
// when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
445-
if (fExplicit && nVersion > nWalletMaxVersion)
446-
nVersion = FEATURE_LATEST;
447-
448443
nWalletVersion = nVersion;
449444

450-
if (nVersion > nWalletMaxVersion)
451-
nWalletMaxVersion = nVersion;
452-
453445
{
454446
WalletBatch* batch = batch_in ? batch_in : new WalletBatch(*database);
455447
if (nWalletVersion > 40000)
@@ -459,18 +451,6 @@ void CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch* batch_in,
459451
}
460452
}
461453

462-
bool CWallet::SetMaxVersion(int nVersion)
463-
{
464-
LOCK(cs_wallet);
465-
// cannot downgrade below current version
466-
if (nWalletVersion > nVersion)
467-
return false;
468-
469-
nWalletMaxVersion = nVersion;
470-
471-
return true;
472-
}
473-
474454
std::set<uint256> CWallet::GetConflicts(const uint256& txid) const
475455
{
476456
std::set<uint256> result;
@@ -655,7 +635,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
655635
}
656636

657637
// Encryption was introduced in version 0.4.0
658-
SetMinVersion(FEATURE_WALLETCRYPT, encrypted_batch, true);
638+
SetMinVersion(FEATURE_WALLETCRYPT, encrypted_batch);
659639

660640
if (!encrypted_batch->TxnCommit()) {
661641
delete encrypted_batch;
@@ -4120,31 +4100,30 @@ const CAddressBookData* CWallet::FindAddressBookEntry(const CTxDestination& dest
41204100
bool CWallet::UpgradeWallet(int version, bilingual_str& error, std::vector<bilingual_str>& warnings)
41214101
{
41224102
int prev_version = GetVersion();
4123-
int& nMaxVersion = version;
4124-
if (nMaxVersion == 0) // the -upgradewallet without argument case
4103+
if (version == 0) // the -upgradewallet without argument case
41254104
{
41264105
WalletLogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
4127-
nMaxVersion = FEATURE_LATEST;
4128-
SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
4106+
version = FEATURE_LATEST;
41294107
} else {
4130-
WalletLogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
4108+
WalletLogPrintf("Allowing wallet upgrade up to %i\n", version);
41314109
}
4132-
if (nMaxVersion < GetVersion())
4110+
if (version < prev_version)
41334111
{
41344112
error = _("Cannot downgrade wallet");
41354113
return false;
41364114
}
4137-
SetMaxVersion(nMaxVersion);
41384115

41394116
LOCK(cs_wallet);
41404117

41414118
// Do not upgrade versions to any version between HD_SPLIT and FEATURE_PRE_SPLIT_KEYPOOL unless already supporting HD_SPLIT
4142-
int max_version = GetVersion();
4143-
if (!CanSupportFeature(FEATURE_HD_SPLIT) && max_version >= FEATURE_HD_SPLIT && max_version < FEATURE_PRE_SPLIT_KEYPOOL) {
4119+
if (!CanSupportFeature(FEATURE_HD_SPLIT) && version >= FEATURE_HD_SPLIT && version < FEATURE_PRE_SPLIT_KEYPOOL) {
41444120
error = _("Cannot upgrade a non HD split wallet without upgrading to support pre split keypool. Please use version 169900 or no version specified.");
41454121
return false;
41464122
}
41474123

4124+
// Permanently upgrade to the version
4125+
SetMinVersion(GetClosestWalletFeature(version));
4126+
41484127
for (auto spk_man : GetActiveScriptPubKeyMans()) {
41494128
if (!spk_man->Upgrade(prev_version, version, error)) {
41504129
return false;

src/wallet/wallet.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,6 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
636636
//! the current wallet version: clients below this version are not able to load the wallet
637637
int nWalletVersion GUARDED_BY(cs_wallet){FEATURE_BASE};
638638

639-
//! the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
640-
int nWalletMaxVersion GUARDED_BY(cs_wallet) = FEATURE_BASE;
641-
642639
int64_t nNextResend = 0;
643640
bool fBroadcastTransactions = false;
644641
// Local time that the tip block was received. Used to schedule wallet rebroadcasts.
@@ -800,8 +797,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
800797
const CWalletTx* GetWalletTx(const uint256& hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
801798
bool IsTrusted(const CWalletTx& wtx, std::set<uint256>& trusted_parents) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
802799

803-
//! check whether we are allowed to upgrade (or already support) to the named feature
804-
bool CanSupportFeature(enum WalletFeature wf) const override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); return IsFeatureSupported(nWalletMaxVersion, wf); }
800+
//! check whether we support the named feature
801+
bool CanSupportFeature(enum WalletFeature wf) const override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); return IsFeatureSupported(nWalletVersion, wf); }
805802

806803
/**
807804
* populate vCoins with vector of available COutputs.
@@ -853,7 +850,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
853850
//! Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo
854851
void UpgradeKeyMetadata() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
855852

856-
bool LoadMinVersion(int nVersion) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
853+
bool LoadMinVersion(int nVersion) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; return true; }
857854

858855
/**
859856
* Adds a destination data tuple to the store, and saves it to disk
@@ -1076,11 +1073,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10761073

10771074
unsigned int GetKeyPoolSize() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10781075

1079-
//! signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
1080-
void SetMinVersion(enum WalletFeature, WalletBatch* batch_in = nullptr, bool fExplicit = false) override;
1081-
1082-
//! change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
1083-
bool SetMaxVersion(int nVersion);
1076+
//! signify that a particular wallet feature is now used.
1077+
void SetMinVersion(enum WalletFeature, WalletBatch* batch_in = nullptr) override;
10841078

10851079
//! get the current wallet format (the oldest client version guaranteed to understand this wallet)
10861080
int GetVersion() const { LOCK(cs_wallet); return nWalletVersion; }

0 commit comments

Comments
 (0)