CWalletDB: Store the update counter per wallet

This commit is contained in:
Luke Dashjr 2017-03-09 20:56:58 +00:00
parent 74e8738961
commit 19b3648bb5
5 changed files with 29 additions and 26 deletions

View file

@ -434,6 +434,16 @@ void CDB::Flush()
env->dbenv->txn_checkpoint(nMinutes ? GetArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0, nMinutes, 0);
}
void CWalletDBWrapper::IncrementUpdateCounter()
{
++nUpdateCounter;
}
unsigned int CWalletDBWrapper::GetUpdateCounter()
{
return nUpdateCounter.load();
}
void CDB::Close()
{
if (!pdb)

View file

@ -119,10 +119,14 @@ public:
*/
void Flush(bool shutdown);
void IncrementUpdateCounter();
unsigned int GetUpdateCounter();
private:
/** BerkeleyDB specific */
CDBEnv *env;
std::string strFile;
std::atomic<unsigned int> nUpdateCounter;
/** Return whether this database handle is a dummy for testing.
* Only to be used at a low level, application should ideally not care

View file

@ -3884,7 +3884,7 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
walletInstance->ScanForWalletTransactions(pindexRescan, true);
LogPrintf(" rescan %15dms\n", GetTimeMillis() - nStart);
walletInstance->SetBestChain(chainActive.GetLocator());
CWalletDB::IncrementUpdateCounter();
walletInstance->dbw->IncrementUpdateCounter();
// Restore wallet transaction metadata after -zapwallettxes=1
if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2")

View file

@ -22,8 +22,6 @@
#include <boost/foreach.hpp>
#include <boost/thread.hpp>
static std::atomic<unsigned int> nWalletDBUpdateCounter;
//
// CWalletDB
//
@ -762,20 +760,22 @@ void MaybeCompactWalletDB()
return;
}
static unsigned int nLastSeen = CWalletDB::GetUpdateCounter();
static unsigned int nLastFlushed = CWalletDB::GetUpdateCounter();
CWalletDBWrapper& dbh = pwalletMain->GetDBHandle();
static unsigned int nLastSeen = dbh.GetUpdateCounter();
static unsigned int nLastFlushed = dbh.GetUpdateCounter();
static int64_t nLastWalletUpdate = GetTime();
if (nLastSeen != CWalletDB::GetUpdateCounter())
if (nLastSeen != dbh.GetUpdateCounter())
{
nLastSeen = CWalletDB::GetUpdateCounter();
nLastSeen = dbh.GetUpdateCounter();
nLastWalletUpdate = GetTime();
}
if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2)
if (nLastFlushed != dbh.GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2)
{
if (CDB::PeriodicFlush(pwalletMain->GetDBHandle())) {
nLastFlushed = CWalletDB::GetUpdateCounter();
if (CDB::PeriodicFlush(dbh)) {
nLastFlushed = dbh.GetUpdateCounter();
}
}
fOneThread = false;
@ -845,16 +845,6 @@ bool CWalletDB::WriteHDChain(const CHDChain& chain)
return WriteIC(std::string("hdchain"), chain);
}
void CWalletDB::IncrementUpdateCounter()
{
nWalletDBUpdateCounter++;
}
unsigned int CWalletDB::GetUpdateCounter()
{
return nWalletDBUpdateCounter;
}
bool CWalletDB::TxnBegin()
{
return batch.TxnBegin();

View file

@ -147,7 +147,7 @@ private:
if (!batch.Write(key, value, fOverwrite)) {
return false;
}
IncrementUpdateCounter();
m_dbw.IncrementUpdateCounter();
return true;
}
@ -157,13 +157,14 @@ private:
if (!batch.Erase(key)) {
return false;
}
IncrementUpdateCounter();
m_dbw.IncrementUpdateCounter();
return true;
}
public:
CWalletDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool _fFlushOnClose = true) :
batch(dbw, pszMode, _fFlushOnClose)
batch(dbw, pszMode, _fFlushOnClose),
m_dbw(dbw)
{
}
@ -232,9 +233,6 @@ public:
//! write the hdchain model (external chain child index counter)
bool WriteHDChain(const CHDChain& chain);
static void IncrementUpdateCounter();
static unsigned int GetUpdateCounter();
//! Begin a new transaction
bool TxnBegin();
//! Commit current transaction
@ -247,6 +245,7 @@ public:
bool WriteVersion(int nVersion);
private:
CDB batch;
CWalletDBWrapper& m_dbw;
CWalletDB(const CWalletDB&);
void operator=(const CWalletDB&);