From c88e87c3b2be3f97b712107e04285d06dfef3878 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Wed, 13 Mar 2019 00:37:08 -0400 Subject: [PATCH 1/3] Remove nFileVersion from CWalletScanState nFileVersion is not the actual file version and is not used except in one place. So it is removed from CWalletScanState and changed so that it is just read at the place it is needed. Furthermore, the "version" record now only indicates the version of the highest versioned client that has opened a wallet file so the variable name is changed accordingly --- src/wallet/walletdb.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 2783f83fd..6499204c8 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -165,7 +165,6 @@ public: unsigned int m_unknown_records{0}; bool fIsEncrypted{false}; bool fAnyUnordered{false}; - int nFileVersion{0}; std::vector vWalletUpgrade; CWalletScanState() { @@ -376,12 +375,6 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, pwallet->LoadKeyPool(nIndex, keypool); } - else if (strType == "version") - { - ssValue >> wss.nFileVersion; - if (wss.nFileVersion == 10300) - wss.nFileVersion = 300; - } else if (strType == "cscript") { uint160 hash; @@ -419,7 +412,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, return false; } } else if (strType != "bestblock" && strType != "bestblock_nomerkle" && - strType != "minversion" && strType != "acentry") { + strType != "minversion" && strType != "acentry" && strType != "version") { wss.m_unknown_records++; } } catch (...) @@ -512,7 +505,11 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet) if (result != DBErrors::LOAD_OK) return result; - pwallet->WalletLogPrintf("nFileVersion = %d\n", wss.nFileVersion); + // Last client version to open this wallet, was previously the file version number + int last_client = CLIENT_VERSION; + ReadVersion(last_client); + + pwallet->WalletLogPrintf("nFileVersion = %d\n", last_client); pwallet->WalletLogPrintf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total. Unknown wallet records: %u\n", wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys, wss.m_unknown_records); @@ -525,10 +522,10 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet) WriteTx(pwallet->mapWallet.at(hash)); // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc: - if (wss.fIsEncrypted && (wss.nFileVersion == 40000 || wss.nFileVersion == 50000)) + if (wss.fIsEncrypted && (last_client == 40000 || last_client == 50000)) return DBErrors::NEED_REWRITE; - if (wss.nFileVersion < CLIENT_VERSION) // Update + if (last_client < CLIENT_VERSION) // Update WriteVersion(CLIENT_VERSION); if (wss.fAnyUnordered) From b3d4f6c9619142948ab3d53551b4f3c0d7d73bde Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Wed, 13 Mar 2019 00:38:24 -0400 Subject: [PATCH 2/3] Log the actual wallet file version The actual wallet file version is the minversion record, not the version record. --- src/wallet/walletdb.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 6499204c8..2db18bec2 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -509,7 +509,8 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet) int last_client = CLIENT_VERSION; ReadVersion(last_client); - pwallet->WalletLogPrintf("nFileVersion = %d\n", last_client); + int wallet_version = pwallet->GetVersion(); + pwallet->WalletLogPrintf("Wallet File Version = %d\n", wallet_version > 0 ? wallet_version : last_client); pwallet->WalletLogPrintf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total. Unknown wallet records: %u\n", wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys, wss.m_unknown_records); From 35e60e790f2cd602d1bdd0be835d27f0ba37efa9 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Wed, 13 Mar 2019 00:44:52 -0400 Subject: [PATCH 3/3] Remove ReadVersion and WriteVersion The "version" record that these functions read and write are not used anywhere in the code except for one place. There is no reason to expose these functions publicly. Furthermore, this avoids potential confusion as developers may mistake these functions for actually reading and writing the wallet version when they do not. --- src/wallet/db.cpp | 2 +- src/wallet/db.h | 11 ----------- src/wallet/walletdb.cpp | 14 ++------------ src/wallet/walletdb.h | 4 ---- 4 files changed, 3 insertions(+), 28 deletions(-) diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index 99d880daa..41c157c5c 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -587,7 +587,7 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo if (fCreate && !Exists(std::string("version"))) { bool fTmp = fReadOnly; fReadOnly = false; - WriteVersion(CLIENT_VERSION); + Write(std::string("version"), CLIENT_VERSION); fReadOnly = fTmp; } } diff --git a/src/wallet/db.h b/src/wallet/db.h index 9df965305..671bb7e6c 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -396,17 +396,6 @@ public: return (ret == 0); } - bool ReadVersion(int& nVersion) - { - nVersion = 0; - return Read(std::string("version"), nVersion); - } - - bool WriteVersion(int nVersion) - { - return Write(std::string("version"), nVersion); - } - bool static Rewrite(BerkeleyDatabase& database, const char* pszSkip = nullptr); }; diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 2db18bec2..9e992b266 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -507,7 +507,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet) // Last client version to open this wallet, was previously the file version number int last_client = CLIENT_VERSION; - ReadVersion(last_client); + m_batch.Read(std::string("version"), last_client); int wallet_version = pwallet->GetVersion(); pwallet->WalletLogPrintf("Wallet File Version = %d\n", wallet_version > 0 ? wallet_version : last_client); @@ -527,7 +527,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet) return DBErrors::NEED_REWRITE; if (last_client < CLIENT_VERSION) // Update - WriteVersion(CLIENT_VERSION); + m_batch.Write(std::string("version"), CLIENT_VERSION); if (wss.fAnyUnordered) result = pwallet->ReorderTransactions(); @@ -770,13 +770,3 @@ bool WalletBatch::TxnAbort() { return m_batch.TxnAbort(); } - -bool WalletBatch::ReadVersion(int& nVersion) -{ - return m_batch.ReadVersion(nVersion); -} - -bool WalletBatch::WriteVersion(int nVersion) -{ - return m_batch.WriteVersion(nVersion); -} diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 0532a55ff..90163528a 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -241,10 +241,6 @@ public: bool TxnCommit(); //! Abort current transaction bool TxnAbort(); - //! Read wallet version - bool ReadVersion(int& nVersion); - //! Write wallet version - bool WriteVersion(int nVersion); private: BerkeleyBatch m_batch; WalletDatabase& m_database;