Browse Source

CWalletDB: Store the update counter per wallet

0.15
Luke Dashjr 8 years ago
parent
commit
19b3648bb5
  1. 10
      src/wallet/db.cpp
  2. 4
      src/wallet/db.h
  3. 2
      src/wallet/wallet.cpp
  4. 28
      src/wallet/walletdb.cpp
  5. 11
      src/wallet/walletdb.h

10
src/wallet/db.cpp

@ -434,6 +434,16 @@ void CDB::Flush() @@ -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)

4
src/wallet/db.h

@ -119,10 +119,14 @@ public: @@ -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

2
src/wallet/wallet.cpp

@ -3884,7 +3884,7 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile) @@ -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")

28
src/wallet/walletdb.cpp

@ -22,8 +22,6 @@ @@ -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() @@ -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) @@ -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();

11
src/wallet/walletdb.h

@ -147,7 +147,7 @@ private: @@ -147,7 +147,7 @@ private:
if (!batch.Write(key, value, fOverwrite)) {
return false;
}
IncrementUpdateCounter();
m_dbw.IncrementUpdateCounter();
return true;
}
@ -157,13 +157,14 @@ private: @@ -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: @@ -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: @@ -247,6 +245,7 @@ public:
bool WriteVersion(int nVersion);
private:
CDB batch;
CWalletDBWrapper& m_dbw;
CWalletDB(const CWalletDB&);
void operator=(const CWalletDB&);

Loading…
Cancel
Save