Skip to content

Commit

Permalink
CWalletDB: Store the update counter per wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr authored and random-zebra committed May 17, 2021
1 parent 8edb74f commit 9cfb711
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
10 changes: 10 additions & 0 deletions src/wallet/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,16 @@ void CDB::Flush()
env->dbenv->txn_checkpoint(nMinutes ? gArgs.GetArg("-dblogsize", 100) * 1024 : 0, nMinutes, 0);
}

void CWalletDBWrapper::IncrementUpdateCounter()
{
++nUpdateCounter;
}

unsigned int CWalletDBWrapper::GetUpdateCounter()
{
return nUpdateCounter.load();
}

void CDB::Close()
{
if (!pdb)
Expand Down
5 changes: 5 additions & 0 deletions src/wallet/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "sync.h"
#include "version.h"

#include <atomic>
#include <map>
#include <string>
#include <vector>
Expand Down Expand Up @@ -121,10 +122,14 @@ class CWalletDBWrapper
*/
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
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4309,7 +4309,7 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
}
LogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - nWalletRescanTime);
walletInstance->SetBestChain(chainActive.GetLocator());
CWalletDB::IncrementUpdateCounter();
walletInstance->dbw->IncrementUpdateCounter();

// Restore wallet transaction metadata after -zapwallettxes=1
if (gArgs.GetBoolArg("-zapwallettxes", false) && gArgs.GetArg("-zapwallettxes", "1") != "2") {
Expand Down
27 changes: 9 additions & 18 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ namespace DBKeys {

} // namespace DBKeys

static std::atomic<unsigned int> nWalletDBUpdateCounter;

//
// CWalletDB
Expand Down Expand Up @@ -798,18 +797,20 @@ 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()) {
nLastSeen = CWalletDB::GetUpdateCounter();
if (nLastSeen != dbh.GetUpdateCounter()) {
nLastSeen = dbh.GetUpdateCounter();
nLastWalletUpdate = GetTime();
}

if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2) {
if (CDB::PeriodicFlush(pwalletMain->GetDBHandle())) {
nLastFlushed = CWalletDB::GetUpdateCounter();
if (nLastFlushed != dbh.GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2) {
if (CDB::PeriodicFlush(dbh)) {
nLastFlushed = dbh.GetUpdateCounter();
}
}
fOneThread = false;
Expand Down Expand Up @@ -1117,16 +1118,6 @@ bool CWalletDB::EraseDestData(const std::string& address, const std::string& key
return EraseIC(std::make_pair(std::string(DBKeys::DESTDATA), std::make_pair(address, key)));
}

void CWalletDB::IncrementUpdateCounter()
{
nWalletDBUpdateCounter++;
}

unsigned int CWalletDB::GetUpdateCounter()
{
return nWalletDBUpdateCounter;
}

bool CWalletDB::TxnBegin()
{
return batch.TxnBegin();
Expand Down
11 changes: 5 additions & 6 deletions src/wallet/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class CWalletDB
if (!batch.Write(key, value, fOverwrite)) {
return false;
}
IncrementUpdateCounter();
m_dbw.IncrementUpdateCounter();
return true;
}

Expand All @@ -131,13 +131,14 @@ class CWalletDB
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)
{
}

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

static void IncrementUpdateCounter();
static unsigned int GetUpdateCounter();

//! Begin a new transaction
bool TxnBegin();
//! Commit current transaction
Expand All @@ -233,6 +231,7 @@ class CWalletDB
bool WriteVersion(int nVersion);
private:
CDB batch;
CWalletDBWrapper& m_dbw;

CWalletDB(const CWalletDB&);
void operator=(const CWalletDB&);
Expand Down

0 comments on commit 9cfb711

Please sign in to comment.