Skip to content

Commit

Permalink
wallet: remove nWalletMaxVersion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
achow101 committed Nov 4, 2020
1 parent bd7398c commit 8e32e1c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/wallet/scriptpubkeyman.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class WalletStorage
virtual bool IsWalletFlagSet(uint64_t) const = 0;
virtual void UnsetBlankWalletFlag(WalletBatch&) = 0;
virtual bool CanSupportFeature(enum WalletFeature) const = 0;
virtual void SetMinVersion(enum WalletFeature, WalletBatch* = nullptr, bool = false) = 0;
virtual void SetMinVersion(enum WalletFeature, WalletBatch* = nullptr) = 0;
virtual const CKeyingMaterial& GetEncryptionKey() const = 0;
virtual bool HasEncryptionKeys() const = 0;
virtual bool IsLocked() const = 0;
Expand Down
41 changes: 10 additions & 31 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,21 +435,13 @@ void CWallet::chainStateFlushed(const CBlockLocator& loc)
batch.WriteBestBlock(loc);
}

void CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch* batch_in, bool fExplicit)
void CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch* batch_in)
{
LOCK(cs_wallet);
if (nWalletVersion >= nVersion)
return;

// when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
if (fExplicit && nVersion > nWalletMaxVersion)
nVersion = FEATURE_LATEST;

nWalletVersion = nVersion;

if (nVersion > nWalletMaxVersion)
nWalletMaxVersion = nVersion;

{
WalletBatch* batch = batch_in ? batch_in : new WalletBatch(*database);
if (nWalletVersion > 40000)
Expand All @@ -459,18 +451,6 @@ void CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch* batch_in,
}
}

bool CWallet::SetMaxVersion(int nVersion)
{
LOCK(cs_wallet);
// cannot downgrade below current version
if (nWalletVersion > nVersion)
return false;

nWalletMaxVersion = nVersion;

return true;
}

std::set<uint256> CWallet::GetConflicts(const uint256& txid) const
{
std::set<uint256> result;
Expand Down Expand Up @@ -655,7 +635,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
}

// Encryption was introduced in version 0.4.0
SetMinVersion(FEATURE_WALLETCRYPT, encrypted_batch, true);
SetMinVersion(FEATURE_WALLETCRYPT, encrypted_batch);

if (!encrypted_batch->TxnCommit()) {
delete encrypted_batch;
Expand Down Expand Up @@ -4120,31 +4100,30 @@ const CAddressBookData* CWallet::FindAddressBookEntry(const CTxDestination& dest
bool CWallet::UpgradeWallet(int version, bilingual_str& error, std::vector<bilingual_str>& warnings)
{
int prev_version = GetVersion();
int& nMaxVersion = version;
if (nMaxVersion == 0) // the -upgradewallet without argument case
if (version == 0) // the -upgradewallet without argument case
{
WalletLogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
nMaxVersion = FEATURE_LATEST;
SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
version = FEATURE_LATEST;
} else {
WalletLogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
WalletLogPrintf("Allowing wallet upgrade up to %i\n", version);
}
if (nMaxVersion < GetVersion())
if (version < prev_version)
{
error = _("Cannot downgrade wallet");
return false;
}
SetMaxVersion(nMaxVersion);

LOCK(cs_wallet);

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

// Permanently upgrade to the version
SetMinVersion(GetClosestWalletFeature(version));

for (auto spk_man : GetActiveScriptPubKeyMans()) {
if (!spk_man->Upgrade(prev_version, version, error)) {
return false;
Expand Down
16 changes: 5 additions & 11 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,6 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
//! the current wallet version: clients below this version are not able to load the wallet
int nWalletVersion GUARDED_BY(cs_wallet){FEATURE_BASE};

//! the maximum wallet format version: memory-only variable that specifies to what version this wallet may be upgraded
int nWalletMaxVersion GUARDED_BY(cs_wallet) = FEATURE_BASE;

int64_t nNextResend = 0;
bool fBroadcastTransactions = false;
// Local time that the tip block was received. Used to schedule wallet rebroadcasts.
Expand Down Expand Up @@ -800,8 +797,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
const CWalletTx* GetWalletTx(const uint256& hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool IsTrusted(const CWalletTx& wtx, std::set<uint256>& trusted_parents) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

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

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

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

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

unsigned int GetKeyPoolSize() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

//! signify that a particular wallet feature is now used. this may change nWalletVersion and nWalletMaxVersion if those are lower
void SetMinVersion(enum WalletFeature, WalletBatch* batch_in = nullptr, bool fExplicit = false) override;

//! change which version we're allowed to upgrade to (note that this does not immediately imply upgrading to that format)
bool SetMaxVersion(int nVersion);
//! signify that a particular wallet feature is now used.
void SetMinVersion(enum WalletFeature, WalletBatch* batch_in = nullptr) override;

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

0 comments on commit 8e32e1c

Please sign in to comment.