Browse Source

Add SetMinVersion to CWallet

miguelfreitas
Pieter Wuille 13 years ago
parent
commit
0b807a417f
  1. 1
      src/db.cpp
  2. 5
      src/db.h
  3. 31
      src/wallet.cpp
  4. 9
      src/wallet.h

1
src/db.cpp

@ -940,6 +940,7 @@ int CWalletDB::LoadWallet(CWallet* pwallet)
ssValue >> nMinVersion; ssValue >> nMinVersion;
if (nMinVersion > CLIENT_VERSION) if (nMinVersion > CLIENT_VERSION)
return DB_TOO_NEW; return DB_TOO_NEW;
pwallet->LoadMinVersion(nMinVersion);
} }
else if (strType == "cscript") else if (strType == "cscript")
{ {

5
src/db.h

@ -486,6 +486,11 @@ public:
return Write(std::make_pair(std::string("setting"), strKey), value); return Write(std::make_pair(std::string("setting"), strKey), value);
} }
bool WriteMinVersion(int nVersion)
{
return Write(std::string("minversion"), nVersion);
}
bool ReadAccount(const std::string& strAccount, CAccount& account); bool ReadAccount(const std::string& strAccount, CAccount& account);
bool WriteAccount(const std::string& strAccount, const CAccount& account); bool WriteAccount(const std::string& strAccount, const CAccount& account);
bool WriteAccountingEntry(const CAccountingEntry& acentry); bool WriteAccountingEntry(const CAccountingEntry& acentry);

31
src/wallet.cpp

@ -131,6 +131,32 @@ public:
) )
}; };
bool CWallet::SetMinVersion(int nVersion, CWalletDB* pwalletdbIn)
{
if (nWalletVersion >= nVersion)
return true;
nWalletVersion = nVersion;
if (fFileBacked)
{
CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
if (nWalletVersion >= 40000)
{
// Versions prior to 0.4.0 did not support the "minversion" record.
// Use a CCorruptAddress to make them crash instead.
CCorruptAddress corruptAddress;
pwalletdb->WriteSetting("addrIncoming", corruptAddress);
}
if (nWalletVersion > 40000)
pwalletdb->WriteMinVersion(nWalletVersion);
if (!pwalletdbIn)
delete pwalletdb;
}
return true;
}
bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase) bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
{ {
if (IsCrypted()) if (IsCrypted())
@ -184,10 +210,11 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
exit(1); //We now probably have half of our keys encrypted in memory, and half not...die and let the user reload their unencrypted wallet. exit(1); //We now probably have half of our keys encrypted in memory, and half not...die and let the user reload their unencrypted wallet.
} }
// Encryption was introduced in version 0.4.0
SetMinVersion(40000, pwalletdbEncryption);
if (fFileBacked) if (fFileBacked)
{ {
CCorruptAddress corruptAddress;
pwalletdbEncryption->WriteSetting("addrIncoming", corruptAddress);
if (!pwalletdbEncryption->TxnCommit()) if (!pwalletdbEncryption->TxnCommit())
exit(1); //We now have keys encrypted in memory, but no on disk...die to avoid confusion and let the user reload their unencrypted wallet. exit(1); //We now have keys encrypted in memory, but no on disk...die to avoid confusion and let the user reload their unencrypted wallet.

9
src/wallet.h

@ -25,6 +25,8 @@ private:
CWalletDB *pwalletdbEncryption; CWalletDB *pwalletdbEncryption;
int nWalletVersion;
public: public:
mutable CCriticalSection cs_wallet; mutable CCriticalSection cs_wallet;
@ -33,18 +35,21 @@ public:
std::set<int64> setKeyPool; std::set<int64> setKeyPool;
typedef std::map<unsigned int, CMasterKey> MasterKeyMap; typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
MasterKeyMap mapMasterKeys; MasterKeyMap mapMasterKeys;
unsigned int nMasterKeyMaxID; unsigned int nMasterKeyMaxID;
CWallet() CWallet()
{ {
nWalletVersion = 0;
fFileBacked = false; fFileBacked = false;
nMasterKeyMaxID = 0; nMasterKeyMaxID = 0;
pwalletdbEncryption = NULL; pwalletdbEncryption = NULL;
} }
CWallet(std::string strWalletFileIn) CWallet(std::string strWalletFileIn)
{ {
nWalletVersion = 0;
strWalletFile = strWalletFileIn; strWalletFile = strWalletFileIn;
fFileBacked = true; fFileBacked = true;
nMasterKeyMaxID = 0; nMasterKeyMaxID = 0;
@ -66,6 +71,8 @@ public:
// Adds a key to the store, without saving it to disk (used by LoadWallet) // Adds a key to the store, without saving it to disk (used by LoadWallet)
bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); } bool LoadKey(const CKey& key) { return CCryptoKeyStore::AddKey(key); }
bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; return true; }
// Adds an encrypted key to the store, and saves it to disk. // Adds an encrypted key to the store, and saves it to disk.
bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret); bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
// Adds an encrypted key to the store, without saving it to disk (used by LoadWallet) // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet)
@ -206,6 +213,8 @@ public:
bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx); bool GetTransaction(const uint256 &hashTx, CWalletTx& wtx);
bool SetDefaultKey(const std::vector<unsigned char> &vchPubKey); bool SetDefaultKey(const std::vector<unsigned char> &vchPubKey);
bool SetMinVersion(int nVersion, CWalletDB* pwalletdbIn = NULL);
}; };

Loading…
Cancel
Save