Skip to content

Commit 9cfb711

Browse files
luke-jrrandom-zebra
authored andcommitted
CWalletDB: Store the update counter per wallet
1 parent 8edb74f commit 9cfb711

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

src/wallet/db.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,16 @@ void CDB::Flush()
428428
env->dbenv->txn_checkpoint(nMinutes ? gArgs.GetArg("-dblogsize", 100) * 1024 : 0, nMinutes, 0);
429429
}
430430

431+
void CWalletDBWrapper::IncrementUpdateCounter()
432+
{
433+
++nUpdateCounter;
434+
}
435+
436+
unsigned int CWalletDBWrapper::GetUpdateCounter()
437+
{
438+
return nUpdateCounter.load();
439+
}
440+
431441
void CDB::Close()
432442
{
433443
if (!pdb)

src/wallet/db.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "sync.h"
1515
#include "version.h"
1616

17+
#include <atomic>
1718
#include <map>
1819
#include <string>
1920
#include <vector>
@@ -121,10 +122,14 @@ class CWalletDBWrapper
121122
*/
122123
void Flush(bool shutdown);
123124

125+
void IncrementUpdateCounter();
126+
unsigned int GetUpdateCounter();
127+
124128
private:
125129
/** BerkeleyDB specific */
126130
CDBEnv *env;
127131
std::string strFile;
132+
std::atomic<unsigned int> nUpdateCounter;
128133

129134
/** Return whether this database handle is a dummy for testing.
130135
* 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
@@ -4309,7 +4309,7 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
43094309
}
43104310
LogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - nWalletRescanTime);
43114311
walletInstance->SetBestChain(chainActive.GetLocator());
4312-
CWalletDB::IncrementUpdateCounter();
4312+
walletInstance->dbw->IncrementUpdateCounter();
43134313

43144314
// Restore wallet transaction metadata after -zapwallettxes=1
43154315
if (gArgs.GetBoolArg("-zapwallettxes", false) && gArgs.GetArg("-zapwallettxes", "1") != "2") {

src/wallet/walletdb.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ namespace DBKeys {
6161

6262
} // namespace DBKeys
6363

64-
static std::atomic<unsigned int> nWalletDBUpdateCounter;
6564

6665
//
6766
// CWalletDB
@@ -798,18 +797,20 @@ void MaybeCompactWalletDB()
798797
return;
799798
}
800799

801-
static unsigned int nLastSeen = CWalletDB::GetUpdateCounter();
802-
static unsigned int nLastFlushed = CWalletDB::GetUpdateCounter();
800+
CWalletDBWrapper& dbh = pwalletMain->GetDBHandle();
801+
802+
static unsigned int nLastSeen = dbh.GetUpdateCounter();
803+
static unsigned int nLastFlushed = dbh.GetUpdateCounter();
803804
static int64_t nLastWalletUpdate = GetTime();
804805

805-
if (nLastSeen != CWalletDB::GetUpdateCounter()) {
806-
nLastSeen = CWalletDB::GetUpdateCounter();
806+
if (nLastSeen != dbh.GetUpdateCounter()) {
807+
nLastSeen = dbh.GetUpdateCounter();
807808
nLastWalletUpdate = GetTime();
808809
}
809810

810-
if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2) {
811-
if (CDB::PeriodicFlush(pwalletMain->GetDBHandle())) {
812-
nLastFlushed = CWalletDB::GetUpdateCounter();
811+
if (nLastFlushed != dbh.GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2) {
812+
if (CDB::PeriodicFlush(dbh)) {
813+
nLastFlushed = dbh.GetUpdateCounter();
813814
}
814815
}
815816
fOneThread = false;
@@ -1117,16 +1118,6 @@ bool CWalletDB::EraseDestData(const std::string& address, const std::string& key
11171118
return EraseIC(std::make_pair(std::string(DBKeys::DESTDATA), std::make_pair(address, key)));
11181119
}
11191120

1120-
void CWalletDB::IncrementUpdateCounter()
1121-
{
1122-
nWalletDBUpdateCounter++;
1123-
}
1124-
1125-
unsigned int CWalletDB::GetUpdateCounter()
1126-
{
1127-
return nWalletDBUpdateCounter;
1128-
}
1129-
11301121
bool CWalletDB::TxnBegin()
11311122
{
11321123
return batch.TxnBegin();

src/wallet/walletdb.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class CWalletDB
121121
if (!batch.Write(key, value, fOverwrite)) {
122122
return false;
123123
}
124-
IncrementUpdateCounter();
124+
m_dbw.IncrementUpdateCounter();
125125
return true;
126126
}
127127

@@ -131,13 +131,14 @@ class CWalletDB
131131
if (!batch.Erase(key)) {
132132
return false;
133133
}
134-
IncrementUpdateCounter();
134+
m_dbw.IncrementUpdateCounter();
135135
return true;
136136
}
137137

138138
public:
139139
CWalletDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool _fFlushOnClose = true) :
140-
batch(dbw, pszMode, _fFlushOnClose)
140+
batch(dbw, pszMode, _fFlushOnClose),
141+
m_dbw(dbw)
141142
{
142143
}
143144

@@ -218,9 +219,6 @@ class CWalletDB
218219
/* verifies the database file */
219220
static bool VerifyDatabaseFile(const std::string& walletFile, const fs::path& dataDir, std::string& warningStr, std::string& errorStr);
220221

221-
static void IncrementUpdateCounter();
222-
static unsigned int GetUpdateCounter();
223-
224222
//! Begin a new transaction
225223
bool TxnBegin();
226224
//! Commit current transaction
@@ -233,6 +231,7 @@ class CWalletDB
233231
bool WriteVersion(int nVersion);
234232
private:
235233
CDB batch;
234+
CWalletDBWrapper& m_dbw;
236235

237236
CWalletDB(const CWalletDB&);
238237
void operator=(const CWalletDB&);

0 commit comments

Comments
 (0)