Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
3 changes: 0 additions & 3 deletions src/wallet/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@



unsigned int nWalletDBUpdated;


//
// CDB
//
Expand Down
5 changes: 0 additions & 5 deletions src/wallet/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ class COutPoint;

struct CBlockLocator;

extern unsigned int nWalletDBUpdated;

void ThreadFlushWalletDB(const std::string& strWalletFile);


class CDBEnv
{
private:
Expand Down
91 changes: 52 additions & 39 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,58 +19,61 @@
#include "wallet/wallet.h"
#include <zpiv/deterministicmint.h>

#include <atomic>
#include <boost/scoped_ptr.hpp>
#include <boost/thread.hpp>
#include <fstream>


static uint64_t nAccountingEntryNumber = 0;

static std::atomic<unsigned int> 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);
}

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))
Expand All @@ -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))
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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))
Expand All @@ -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);
}
Expand All @@ -198,33 +201,33 @@ 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);
}

bool CWalletDB::WriteMultiSend(std::vector<std::pair<std::string, int> > vMultiSend)
{
return false;
/* disable multisend
nWalletDBUpdated++;
nWalletDBUpdateCounter++;
bool ret = true;
for (unsigned int i = 0; i < vMultiSend.size(); i++) {
std::pair<std::string, int> pMultiSend;
Expand All @@ -240,7 +243,7 @@ bool CWalletDB::EraseMultiSend(std::vector<std::pair<std::string, int> > vMultiS
{
return false;
/* disable multisend
nWalletDBUpdated++;
nWalletDBUpdateCounter++;
bool ret = true;
for (unsigned int i = 0; i < vMultiSend.size(); i++) {
std::pair<std::string, int> pMultiSend;
Expand All @@ -256,7 +259,7 @@ bool CWalletDB::WriteMSettings(bool fMultiSendStake, bool fMultiSendMasternode,
{
return false;
/* disable multisend
nWalletDBUpdated++;
nWalletDBUpdateCounter++;
std::pair<bool, bool> enabledMS(fMultiSendStake, fMultiSendMasternode);
std::pair<std::pair<bool, bool>, int> pSettings(enabledMS, nLastMultiSendHeight);

Expand All @@ -268,7 +271,7 @@ bool CWalletDB::WriteMSDisabledAddresses(std::vector<std::string> 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]))
Expand All @@ -282,7 +285,7 @@ bool CWalletDB::EraseMSDisabledAddresses(std::vector<std::string> 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)))
Expand All @@ -294,7 +297,7 @@ bool CWalletDB::EraseMSDisabledAddresses(std::vector<std::string> vDisabledAddre

bool CWalletDB::WriteAutoCombineSettings(bool fEnable, CAmount nCombineThreshold)
{
nWalletDBUpdated++;
nWalletDBUpdateCounter++;
std::pair<bool, CAmount> pSettings;
pSettings.first = fEnable;
pSettings.second = nCombineThreshold;
Expand All @@ -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));
}

Expand All @@ -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");
Expand Down Expand Up @@ -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
Expand All @@ -1016,7 +1019,7 @@ void ThreadFlushWalletDB(const std::string& strFile)
std::map<std::string, int>::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
Expand Down Expand Up @@ -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)));
}

Expand Down Expand Up @@ -1737,3 +1740,13 @@ std::list<CDeterministicMint> CWalletDB::ListArchivedDeterministicMints()
pcursor->close();
return listMints;
}

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

unsigned int CWalletDB::GetUpdateCounter()
{
return nWalletDBUpdateCounter;
}
4 changes: 3 additions & 1 deletion src/wallet/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ class CWalletDB : public CDB
std::map<uint256, std::vector<std::pair<uint256, uint32_t> > > 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&);
Expand All @@ -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