diff --git a/src/init.cpp b/src/init.cpp index 0e9ea7ac92c4..2d894b277cf5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1882,7 +1882,7 @@ bool AppInit2() } LogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - nWalletRescanTime); pwalletMain->SetBestChain(chainActive.GetLocator()); - nWalletDBUpdated++; + CWalletDB::IncrementUpdateCounter(); // Restore wallet transaction metadata after -zapwallettxes=1 if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2") { diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index bb2742b79fe4..a7573ad0753e 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -23,9 +23,6 @@ -unsigned int nWalletDBUpdated; - - // // CDB // diff --git a/src/wallet/db.h b/src/wallet/db.h index 1f0782207bac..3ddcce4f553f 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -25,11 +25,6 @@ class COutPoint; struct CBlockLocator; -extern unsigned int nWalletDBUpdated; - -void ThreadFlushWalletDB(const std::string& strWalletFile); - - class CDBEnv { private: diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index e206db5dddbd..eae496a19b7d 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -19,6 +19,7 @@ #include "wallet/wallet.h" #include +#include #include #include #include @@ -26,13 +27,15 @@ static uint64_t nAccountingEntryNumber = 0; +static std::atomic nWalletDBUpdateCounter; + // // CWalletDB // bool CWalletDB::WriteName(const std::string& strAddress, const std::string& strName) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::make_pair(std::string("name"), strAddress), strName); } @@ -40,37 +43,37 @@ bool CWalletDB::EraseName(const std::string& strAddress) { // This should only be used for sending addresses, never for receiving addresses, // receiving addresses must always have an address book entry if they're not change return. - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Erase(std::make_pair(std::string("name"), strAddress)); } bool CWalletDB::WritePurpose(const std::string& strAddress, const std::string& strPurpose) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::make_pair(std::string("purpose"), strAddress), strPurpose); } bool CWalletDB::ErasePurpose(const std::string& strPurpose) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Erase(std::make_pair(std::string("purpose"), strPurpose)); } bool CWalletDB::WriteTx(const CWalletTx& wtx) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::make_pair(std::string("tx"), wtx.GetHash()), wtx); } bool CWalletDB::EraseTx(uint256 hash) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Erase(std::make_pair(std::string("tx"), hash)); } bool CWalletDB::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; if (!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta, false)) @@ -90,7 +93,7 @@ bool CWalletDB::WriteCryptedKey(const CPubKey& vchPubKey, const CKeyMetadata& keyMeta) { const bool fEraseUnencryptedKey = true; - nWalletDBUpdated++; + nWalletDBUpdateCounter++; if (!Write(std::make_pair(std::string("keymeta"), vchPubKey), keyMeta)) @@ -109,7 +112,7 @@ bool CWalletDB::WriteSaplingZKey(const libzcash::SaplingIncomingViewingKey &ivk, const libzcash::SaplingExtendedSpendingKey &key, const CKeyMetadata &keyMeta) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; if (!Write(std::make_pair(std::string("sapzkeymeta"), ivk), keyMeta)) return false; @@ -121,7 +124,7 @@ bool CWalletDB::WriteSaplingPaymentAddress( const libzcash::SaplingPaymentAddress &addr, const libzcash::SaplingIncomingViewingKey &ivk) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::make_pair(std::string("sapzaddr"), addr), ivk, false); } @@ -132,7 +135,7 @@ bool CWalletDB::WriteCryptedSaplingZKey( const CKeyMetadata &keyMeta) { const bool fEraseUnencryptedKey = true; - nWalletDBUpdated++; + nWalletDBUpdateCounter++; auto ivk = extfvk.fvk.in_viewing_key(); if (!Write(std::make_pair(std::string("sapzkeymeta"), ivk), keyMeta)) @@ -149,43 +152,43 @@ bool CWalletDB::WriteCryptedSaplingZKey( bool CWalletDB::WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true); } bool CWalletDB::WriteCScript(const uint160& hash, const CScript& redeemScript) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::make_pair(std::string("cscript"), hash), *(const CScriptBase*)(&redeemScript), false); } bool CWalletDB::WriteWatchOnly(const CScript& dest) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::make_pair(std::string("watchs"), *(const CScriptBase*)(&dest)), '1'); } bool CWalletDB::EraseWatchOnly(const CScript& dest) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Erase(std::make_pair(std::string("watchs"), *(const CScriptBase*)(&dest))); } bool CWalletDB::WriteMultiSig(const CScript& dest) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::make_pair(std::string("multisig"), *(const CScriptBase*)(&dest)), '1'); } bool CWalletDB::EraseMultiSig(const CScript& dest) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Erase(std::make_pair(std::string("multisig"), *(const CScriptBase*)(&dest))); } bool CWalletDB::WriteBestBlock(const CBlockLocator& locator) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; Write(std::string("bestblock"), CBlockLocator()); // Write empty block locator so versions that require a merkle branch automatically rescan return Write(std::string("bestblock_nomerkle"), locator); } @@ -198,25 +201,25 @@ bool CWalletDB::ReadBestBlock(CBlockLocator& locator) bool CWalletDB::WriteOrderPosNext(int64_t nOrderPosNext) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::string("orderposnext"), nOrderPosNext); } bool CWalletDB::WriteStakeSplitThreshold(const CAmount& nStakeSplitThreshold) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::string("stakeSplitThreshold"), nStakeSplitThreshold); } bool CWalletDB::WriteUseCustomFee(bool fUse) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::string("fUseCustomFee"), fUse); } bool CWalletDB::WriteCustomFeeValue(const CAmount& nFee) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::string("nCustomFee"), nFee); } @@ -224,7 +227,7 @@ bool CWalletDB::WriteMultiSend(std::vector > vMultiS { return false; /* disable multisend - nWalletDBUpdated++; + nWalletDBUpdateCounter++; bool ret = true; for (unsigned int i = 0; i < vMultiSend.size(); i++) { std::pair pMultiSend; @@ -240,7 +243,7 @@ bool CWalletDB::EraseMultiSend(std::vector > vMultiS { return false; /* disable multisend - nWalletDBUpdated++; + nWalletDBUpdateCounter++; bool ret = true; for (unsigned int i = 0; i < vMultiSend.size(); i++) { std::pair pMultiSend; @@ -256,7 +259,7 @@ bool CWalletDB::WriteMSettings(bool fMultiSendStake, bool fMultiSendMasternode, { return false; /* disable multisend - nWalletDBUpdated++; + nWalletDBUpdateCounter++; std::pair enabledMS(fMultiSendStake, fMultiSendMasternode); std::pair, int> pSettings(enabledMS, nLastMultiSendHeight); @@ -268,7 +271,7 @@ bool CWalletDB::WriteMSDisabledAddresses(std::vector vDisabledAddre { return false; /* disable multisend - nWalletDBUpdated++; + nWalletDBUpdateCounter++; bool ret = true; for (unsigned int i = 0; i < vDisabledAddresses.size(); i++) { if (!Write(std::make_pair(std::string("mdisabled"), i), vDisabledAddresses[i])) @@ -282,7 +285,7 @@ bool CWalletDB::EraseMSDisabledAddresses(std::vector vDisabledAddre { return false; /* disable multisend - nWalletDBUpdated++; + nWalletDBUpdateCounter++; bool ret = true; for (unsigned int i = 0; i < vDisabledAddresses.size(); i++) { if (!Erase(std::make_pair(std::string("mdisabled"), i))) @@ -294,7 +297,7 @@ bool CWalletDB::EraseMSDisabledAddresses(std::vector vDisabledAddre bool CWalletDB::WriteAutoCombineSettings(bool fEnable, CAmount nCombineThreshold) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; std::pair pSettings; pSettings.first = fEnable; pSettings.second = nCombineThreshold; @@ -308,13 +311,13 @@ bool CWalletDB::ReadPool(int64_t nPool, CKeyPool& keypool) bool CWalletDB::WritePool(int64_t nPool, const CKeyPool& keypool) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::make_pair(std::string("pool"), nPool), keypool); } bool CWalletDB::ErasePool(int64_t nPool) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Erase(std::make_pair(std::string("pool"), nPool)); } @@ -325,7 +328,7 @@ bool CWalletDB::WriteMinVersion(int nVersion) bool CWalletDB::WriteHDChain(const CHDChain& chain) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; std::string key = std::string("hdchain"); if (chain.chainType == HDChain::ChainCounterType::Sapling) key += std::string("_sap"); @@ -989,18 +992,18 @@ void ThreadFlushWalletDB(const std::string& strFile) if (!GetBoolArg("-flushwallet", true)) return; - unsigned int nLastSeen = nWalletDBUpdated; - unsigned int nLastFlushed = nWalletDBUpdated; + unsigned int nLastSeen = CWalletDB::GetUpdateCounter(); + unsigned int nLastFlushed = CWalletDB::GetUpdateCounter(); int64_t nLastWalletUpdate = GetTime(); while (true) { MilliSleep(500); - if (nLastSeen != nWalletDBUpdated) { - nLastSeen = nWalletDBUpdated; + if (nLastSeen != CWalletDB::GetUpdateCounter()) { + nLastSeen = CWalletDB::GetUpdateCounter(); nLastWalletUpdate = GetTime(); } - if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2) { + if (nLastFlushed != CWalletDB::GetUpdateCounter() && GetTime() - nLastWalletUpdate >= 2) { TRY_LOCK(bitdb.cs_db, lockDb); if (lockDb) { // Don't do this if any databases are in use @@ -1016,7 +1019,7 @@ void ThreadFlushWalletDB(const std::string& strFile) std::map::iterator mi = bitdb.mapFileUseCount.find(strFile); if (mi != bitdb.mapFileUseCount.end()) { LogPrint(BCLog::DB, "Flushing wallet.dat\n"); - nLastFlushed = nWalletDBUpdated; + nLastFlushed = CWalletDB::GetUpdateCounter(); int64_t nStart = GetTimeMillis(); // Flush wallet.dat so it's self contained @@ -1254,13 +1257,13 @@ bool CWalletDB::Recover(CDBEnv& dbenv, std::string filename) bool CWalletDB::WriteDestData(const std::string& address, const std::string& key, const std::string& value) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Write(std::make_pair(std::string("destdata"), std::make_pair(address, key)), value); } bool CWalletDB::EraseDestData(const std::string& address, const std::string& key) { - nWalletDBUpdated++; + nWalletDBUpdateCounter++; return Erase(std::make_pair(std::string("destdata"), std::make_pair(address, key))); } @@ -1737,3 +1740,13 @@ std::list CWalletDB::ListArchivedDeterministicMints() pcursor->close(); return listMints; } + +void CWalletDB::IncrementUpdateCounter() +{ + nWalletDBUpdateCounter++; +} + +unsigned int CWalletDB::GetUpdateCounter() +{ + return nWalletDBUpdateCounter; +} diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index b79147f5f77f..ef5da229ead6 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -220,6 +220,8 @@ class CWalletDB : public CDB std::map > > MapMintPool(); bool WriteMintPoolPair(const uint256& hashMasterSeed, const uint256& hashPubcoin, const uint32_t& nCount); + static void IncrementUpdateCounter(); + static unsigned int GetUpdateCounter(); private: CWalletDB(const CWalletDB&); void operator=(const CWalletDB&); @@ -230,6 +232,6 @@ class CWalletDB : public CDB void NotifyBacked(const CWallet& wallet, bool fSuccess, std::string strMessage); bool BackupWallet(const CWallet& wallet, const fs::path& strDest, bool fEnableCustom = true); bool AttemptBackupWallet(const CWallet& wallet, const fs::path& pathSrc, const fs::path& pathDest); - +void ThreadFlushWalletDB(const std::string& strFile); #endif // BITCOIN_WALLETDB_H