Add unit tests for WriteWitnessCache

Requires moving implementation into header
This commit is contained in:
Jack Grigg
2016-10-10 23:13:36 -05:00
parent 6216b4b2dc
commit 82c2d97c60
3 changed files with 102 additions and 39 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,45 +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;
}
try {
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;
}
} catch (const std::exception &exc) {
// Unexpected failure
LogPrintf("WriteWitnessCache(): Unexpected error during atomic write:\n");
LogPrintf("%s\n", exc.what());
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())