Skip to content

Commit 19b3648

Browse files
committed
CWalletDB: Store the update counter per wallet
1 parent 74e8738 commit 19b3648

File tree

5 files changed

+29
-26
lines changed

5 files changed

+29
-26
lines changed

src/wallet/db.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,16 @@ void CDB::Flush()
434434
env->dbenv->txn_checkpoint(nMinutes ? GetArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0, nMinutes, 0);
435435
}
436436

437+
void CWalletDBWrapper::IncrementUpdateCounter()
438+
{
439+
++nUpdateCounter;
440+
}
441+
442+
unsigned int CWalletDBWrapper::GetUpdateCounter()
443+
{
444+
return nUpdateCounter.load();
445+
}
446+
437447
void CDB::Close()
438448
{
439449
if (!pdb)

src/wallet/db.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,14 @@ class CWalletDBWrapper
119119
*/
120120
void Flush(bool shutdown);
121121

122+
void IncrementUpdateCounter();
123+
unsigned int GetUpdateCounter();
124+
122125
private:
123126
/** BerkeleyDB specific */
124127
CDBEnv *env;
125128
std::string strFile;
129+
std::atomic<unsigned int> nUpdateCounter;
126130

127131
/** Return whether this database handle is a dummy for testing.
128132
* Only to be used at a low level, application should ideally not care

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3884,7 +3884,7 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
38843884
walletInstance->ScanForWalletTransactions(pindexRescan, true);
38853885
LogPrintf(" rescan %15dms\n", GetTimeMillis() - nStart);
38863886
walletInstance->SetBestChain(chainActive.GetLocator());
3887-
CWalletDB::IncrementUpdateCounter();
3887+
walletInstance->dbw->IncrementUpdateCounter();
38883888

38893889
// Restore wallet transaction metadata after -zapwallettxes=1
38903890
if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2")

src/wallet/walletdb.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
#include <boost/foreach.hpp>
2323
#include <boost/thread.hpp>
2424

25-
static std::atomic<unsigned int> nWalletDBUpdateCounter;
26-
2725
//
2826
// CWalletDB
2927
//
@@ -762,20 +760,22 @@ void MaybeCompactWalletDB()
762760
return;
763761
}
764762

765-
static unsigned int nLastSeen = CWalletDB::GetUpdateCounter();
766-
static unsigned int nLastFlushed = CWalletDB::GetUpdateCounter();
763+
CWalletDBWrapper& dbh = pwalletMain->GetDBHandle();
764+
765+
static unsigned int nLastSeen = dbh.GetUpdateCounter();
766+
static unsigned int nLastFlushed = dbh.GetUpdateCounter();
767767
static int64_t nLastWalletUpdate = GetTime();
768768

769-
if (nLastSeen != CWalletDB::GetUpdateCounter())
769+
if (nLastSeen != dbh.GetUpdateCounter())
770770
{
771-
nLastSeen = CWalletDB::GetUpdateCounter();
771+
nLastSeen = dbh.GetUpdateCounter();
772772
nLastWalletUpdate = GetTime();
773773
}
774774

775-
if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2)
775+
if (nLastFlushed != dbh.GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2)
776776
{
777-
if (CDB::PeriodicFlush(pwalletMain->GetDBHandle())) {
778-
nLastFlushed = CWalletDB::GetUpdateCounter();
777+
if (CDB::PeriodicFlush(dbh)) {
778+
nLastFlushed = dbh.GetUpdateCounter();
779779
}
780780
}
781781
fOneThread = false;
@@ -845,16 +845,6 @@ bool CWalletDB::WriteHDChain(const CHDChain& chain)
845845
return WriteIC(std::string("hdchain"), chain);
846846
}
847847

848-
void CWalletDB::IncrementUpdateCounter()
849-
{
850-
nWalletDBUpdateCounter++;
851-
}
852-
853-
unsigned int CWalletDB::GetUpdateCounter()
854-
{
855-
return nWalletDBUpdateCounter;
856-
}
857-
858848
bool CWalletDB::TxnBegin()
859849
{
860850
return batch.TxnBegin();

src/wallet/walletdb.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class CWalletDB
147147
if (!batch.Write(key, value, fOverwrite)) {
148148
return false;
149149
}
150-
IncrementUpdateCounter();
150+
m_dbw.IncrementUpdateCounter();
151151
return true;
152152
}
153153

@@ -157,13 +157,14 @@ class CWalletDB
157157
if (!batch.Erase(key)) {
158158
return false;
159159
}
160-
IncrementUpdateCounter();
160+
m_dbw.IncrementUpdateCounter();
161161
return true;
162162
}
163163

164164
public:
165165
CWalletDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool _fFlushOnClose = true) :
166-
batch(dbw, pszMode, _fFlushOnClose)
166+
batch(dbw, pszMode, _fFlushOnClose),
167+
m_dbw(dbw)
167168
{
168169
}
169170

@@ -232,9 +233,6 @@ class CWalletDB
232233
//! write the hdchain model (external chain child index counter)
233234
bool WriteHDChain(const CHDChain& chain);
234235

235-
static void IncrementUpdateCounter();
236-
static unsigned int GetUpdateCounter();
237-
238236
//! Begin a new transaction
239237
bool TxnBegin();
240238
//! Commit current transaction
@@ -247,6 +245,7 @@ class CWalletDB
247245
bool WriteVersion(int nVersion);
248246
private:
249247
CDB batch;
248+
CWalletDBWrapper& m_dbw;
250249

251250
CWalletDB(const CWalletDB&);
252251
void operator=(const CWalletDB&);

0 commit comments

Comments
 (0)