Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use DB Transactions when encrypting wallet.
This speeds up the encryption process significantly.
  • Loading branch information
Matt Corallo committed Jul 13, 2011
commit 96f34cd5c4d76459917b29b15aa9f4b7e2a6cec1
31 changes: 25 additions & 6 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ bool CWallet::AddCryptedKey(const vector<unsigned char> &vchPubKey, const vector
return false;
if (!fFileBacked)
return true;
return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret);
CRITICAL_BLOCK(cs_pwalletdbEncryption)
{
if (pwalletdbEncryption)
return pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret);
else
return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret);
}
}

bool CWallet::Unlock(const string& strWalletPassphrase)
Expand Down Expand Up @@ -104,10 +110,10 @@ bool CWallet::ChangeWalletPassphrase(const string& strOldWalletPassphrase, const

bool CWallet::EncryptWallet(const string& strWalletPassphrase)
{
//TODO: use db commits
CRITICAL_BLOCK(cs_mapPubKeys)
CRITICAL_BLOCK(cs_KeyStore)
CRITICAL_BLOCK(cs_vMasterKey)
CRITICAL_BLOCK(cs_pwalletdbEncryption)
{
if (IsCrypted())
return false;
Expand Down Expand Up @@ -146,13 +152,26 @@ bool CWallet::EncryptWallet(const string& strWalletPassphrase)
mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
if (fFileBacked)
{
DBFlush(false);
CWalletDB(strWalletFile).WriteMasterKey(nMasterKeyMaxID, kMasterKey);
DBFlush(false);
pwalletdbEncryption = new CWalletDB(strWalletFile);
pwalletdbEncryption->TxnBegin();
pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
}

if (!EncryptKeys(vMasterKey))
exit(1); //We now probably have half of our keys encrypted, and half not...die and let the user ask someone with experience to recover their wallet.
{
if (fFileBacked)
pwalletdbEncryption->TxnAbort();
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.
}

if (fFileBacked)
{
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.

pwalletdbEncryption->Close();
pwalletdbEncryption = NULL;
}

Lock();
}
Expand Down
4 changes: 4 additions & 0 deletions src/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class CWallet : public CCryptoKeyStore
bool SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;
bool SelectCoins(int64 nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const;

CWalletDB *pwalletdbEncryption;
CCriticalSection cs_pwalletdbEncryption;

public:
bool fFileBacked;
Expand All @@ -34,12 +36,14 @@ class CWallet : public CCryptoKeyStore
{
fFileBacked = false;
nMasterKeyMaxID = 0;
pwalletdbEncryption = NULL;
}
CWallet(std::string strWalletFileIn)
{
strWalletFile = strWalletFileIn;
fFileBacked = true;
nMasterKeyMaxID = 0;
pwalletdbEncryption = NULL;
}

mutable CCriticalSection cs_mapWallet;
Expand Down