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,6 +15,7 @@
#include "primitives/transaction.h"
#include "tinyformat.h"
#include "ui_interface.h"
#include "util.h"
#include "utilstrencodings.h"
#include "validationinterface.h"
#include "wallet/crypter.h"
@@ -605,7 +606,40 @@ protected:
const CBlock* pblock,
ZCIncrementalMerkleTree tree);
void DecrementNoteWitnesses();
void WriteWitnessCache();
template <typename WalletDB>
void WriteWitnessCache(WalletDB& walletdb) {
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;
}
}
private:
template <class T>