Auto merge of #1486 - str4d:1456-writewitnesscache-exception-safety, r=bitcartel

WriteWitnessCache: Catch errors and abort transaction

Closes #1452
This commit is contained in:
zkbot
2016-10-14 17:37:53 -04:00
3 changed files with 118 additions and 31 deletions

View File

@@ -15,7 +15,6 @@
#include "script/script.h"
#include "script/sign.h"
#include "timedata.h"
#include "util.h"
#include "utilmoneystr.h"
#include "zcash/Note.hpp"
#include "crypter.h"
@@ -697,7 +696,8 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex,
nWitnessCacheSize += 1;
}
if (fFileBacked) {
WriteWitnessCache();
CWalletDB walletdb(strWalletFile);
WriteWitnessCache(walletdb);
}
}
}
@@ -718,37 +718,12 @@ void CWallet::DecrementNoteWitnesses()
// TODO: If nWitnessCache is zero, we need to regenerate the caches (#1302)
assert(nWitnessCacheSize > 0);
if (fFileBacked) {
WriteWitnessCache();
CWalletDB walletdb(strWalletFile);
WriteWitnessCache(walletdb);
}
}
}
void CWallet::WriteWitnessCache() {
CWalletDB walletdb(strWalletFile);
if (!walletdb.TxnBegin()) {
// This needs to be done atomically, so don't do it at all
LogPrintf("WriteWitnessCache(): Couldn't start atomic write\n");
return;
}
for (std::pair<const uint256, CWalletTx>& wtxItem : mapWallet) {
if (!walletdb.WriteTx(wtxItem.first, wtxItem.second)) {
LogPrintf("WriteWitnessCache(): Failed to write CWalletTx, aborting atomic write\n");
walletdb.TxnAbort();
return;
}
}
if (!walletdb.WriteWitnessCacheSize(nWitnessCacheSize)) {
LogPrintf("WriteWitnessCache(): Failed to write nWitnessCacheSize, aborting atomic write\n");
walletdb.TxnAbort();
return;
}
if (!walletdb.TxnCommit()) {
// Couldn't commit all to db, but in-memory state is fine
LogPrintf("WriteWitnessCache(): Couldn't commit atomic write\n");
return;
}
}
bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
{
if (IsCrypted())