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); 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() void CDB::Close()
{ {
if (!pdb) if (!pdb)

View file

@ -119,10 +119,14 @@ public:
*/ */
void Flush(bool shutdown); void Flush(bool shutdown);
void IncrementUpdateCounter();
unsigned int GetUpdateCounter();
private: private:
/** BerkeleyDB specific */ /** BerkeleyDB specific */
CDBEnv *env; CDBEnv *env;
std::string strFile; std::string strFile;
std::atomic<unsigned int> nUpdateCounter;
/** Return whether this database handle is a dummy for testing. /** Return whether this database handle is a dummy for testing.
* Only to be used at a low level, application should ideally not care * 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); walletInstance->ScanForWalletTransactions(pindexRescan, true);
LogPrintf(" rescan %15dms\n", GetTimeMillis() - nStart); LogPrintf(" rescan %15dms\n", GetTimeMillis() - nStart);
walletInstance->SetBestChain(chainActive.GetLocator()); walletInstance->SetBestChain(chainActive.GetLocator());
CWalletDB::IncrementUpdateCounter(); walletInstance->dbw->IncrementUpdateCounter();
// Restore wallet transaction metadata after -zapwallettxes=1 // Restore wallet transaction metadata after -zapwallettxes=1
if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2") if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2")

View file

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

View file

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