mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 20:42:59 +01:00
wallet: CWalletDB CDB composition not inheritance
CWalletDB now contains a CDB instead of inheriting from it. This makes it easier to replace the internal transaction with a different database, without leaking through internals.
This commit is contained in:
parent
be9e1a968d
commit
33232810dc
@ -144,10 +144,10 @@ protected:
|
||||
bool fFlushOnClose;
|
||||
CDBEnv *env;
|
||||
|
||||
public:
|
||||
explicit CDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
|
||||
~CDB() { Close(); }
|
||||
|
||||
public:
|
||||
void Flush();
|
||||
void Close();
|
||||
static bool Recover(const std::string& filename, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue));
|
||||
@ -164,7 +164,7 @@ private:
|
||||
CDB(const CDB&);
|
||||
void operator=(const CDB&);
|
||||
|
||||
protected:
|
||||
public:
|
||||
template <typename K, typename T>
|
||||
bool Read(const K& key, T& value)
|
||||
{
|
||||
|
@ -33,7 +33,7 @@ static std::atomic<unsigned int> nWalletDBUpdateCounter;
|
||||
bool CWalletDB::WriteName(const std::string& strAddress, const std::string& strName)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Write(make_pair(std::string("name"), strAddress), strName);
|
||||
return batch.Write(std::make_pair(std::string("name"), strAddress), strName);
|
||||
}
|
||||
|
||||
bool CWalletDB::EraseName(const std::string& strAddress)
|
||||
@ -41,38 +41,38 @@ 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.
|
||||
nWalletDBUpdateCounter++;
|
||||
return Erase(make_pair(std::string("name"), strAddress));
|
||||
return batch.Erase(std::make_pair(std::string("name"), strAddress));
|
||||
}
|
||||
|
||||
bool CWalletDB::WritePurpose(const std::string& strAddress, const std::string& strPurpose)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Write(make_pair(std::string("purpose"), strAddress), strPurpose);
|
||||
return batch.Write(std::make_pair(std::string("purpose"), strAddress), strPurpose);
|
||||
}
|
||||
|
||||
bool CWalletDB::ErasePurpose(const std::string& strPurpose)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Erase(make_pair(std::string("purpose"), strPurpose));
|
||||
return batch.Erase(std::make_pair(std::string("purpose"), strPurpose));
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteTx(const CWalletTx& wtx)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Write(std::make_pair(std::string("tx"), wtx.GetHash()), wtx);
|
||||
return batch.Write(std::make_pair(std::string("tx"), wtx.GetHash()), wtx);
|
||||
}
|
||||
|
||||
bool CWalletDB::EraseTx(uint256 hash)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Erase(std::make_pair(std::string("tx"), hash));
|
||||
return batch.Erase(std::make_pair(std::string("tx"), hash));
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
|
||||
if (!Write(std::make_pair(std::string("keymeta"), vchPubKey),
|
||||
if (!batch.Write(std::make_pair(std::string("keymeta"), vchPubKey),
|
||||
keyMeta, false))
|
||||
return false;
|
||||
|
||||
@ -82,7 +82,7 @@ bool CWalletDB::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, c
|
||||
vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end());
|
||||
vchKey.insert(vchKey.end(), vchPrivKey.begin(), vchPrivKey.end());
|
||||
|
||||
return Write(std::make_pair(std::string("key"), vchPubKey), std::make_pair(vchPrivKey, Hash(vchKey.begin(), vchKey.end())), false);
|
||||
return batch.Write(std::make_pair(std::string("key"), vchPubKey), std::make_pair(vchPrivKey, Hash(vchKey.begin(), vchKey.end())), false);
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteCryptedKey(const CPubKey& vchPubKey,
|
||||
@ -92,16 +92,16 @@ bool CWalletDB::WriteCryptedKey(const CPubKey& vchPubKey,
|
||||
const bool fEraseUnencryptedKey = true;
|
||||
nWalletDBUpdateCounter++;
|
||||
|
||||
if (!Write(std::make_pair(std::string("keymeta"), vchPubKey),
|
||||
if (!batch.Write(std::make_pair(std::string("keymeta"), vchPubKey),
|
||||
keyMeta))
|
||||
return false;
|
||||
|
||||
if (!Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
|
||||
if (!batch.Write(std::make_pair(std::string("ckey"), vchPubKey), vchCryptedSecret, false))
|
||||
return false;
|
||||
if (fEraseUnencryptedKey)
|
||||
{
|
||||
Erase(std::make_pair(std::string("key"), vchPubKey));
|
||||
Erase(std::make_pair(std::string("wkey"), vchPubKey));
|
||||
batch.Erase(std::make_pair(std::string("key"), vchPubKey));
|
||||
batch.Erase(std::make_pair(std::string("wkey"), vchPubKey));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -109,92 +109,92 @@ bool CWalletDB::WriteCryptedKey(const CPubKey& vchPubKey,
|
||||
bool CWalletDB::WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
|
||||
return batch.Write(std::make_pair(std::string("mkey"), nID), kMasterKey, true);
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteCScript(const uint160& hash, const CScript& redeemScript)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Write(std::make_pair(std::string("cscript"), hash), *(const CScriptBase*)(&redeemScript), false);
|
||||
return batch.Write(std::make_pair(std::string("cscript"), hash), *(const CScriptBase*)(&redeemScript), false);
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMeta)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
if (!Write(std::make_pair(std::string("watchmeta"), *(const CScriptBase*)(&dest)), keyMeta))
|
||||
if (!batch.Write(std::make_pair(std::string("watchmeta"), *(const CScriptBase*)(&dest)), keyMeta))
|
||||
return false;
|
||||
return Write(std::make_pair(std::string("watchs"), *(const CScriptBase*)(&dest)), '1');
|
||||
return batch.Write(std::make_pair(std::string("watchs"), *(const CScriptBase*)(&dest)), '1');
|
||||
}
|
||||
|
||||
bool CWalletDB::EraseWatchOnly(const CScript &dest)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
if (!Erase(std::make_pair(std::string("watchmeta"), *(const CScriptBase*)(&dest))))
|
||||
if (!batch.Erase(std::make_pair(std::string("watchmeta"), *(const CScriptBase*)(&dest))))
|
||||
return false;
|
||||
return Erase(std::make_pair(std::string("watchs"), *(const CScriptBase*)(&dest)));
|
||||
return batch.Erase(std::make_pair(std::string("watchs"), *(const CScriptBase*)(&dest)));
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteBestBlock(const CBlockLocator& locator)
|
||||
{
|
||||
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);
|
||||
batch.Write(std::string("bestblock"), CBlockLocator()); // Write empty block locator so versions that require a merkle branch automatically rescan
|
||||
return batch.Write(std::string("bestblock_nomerkle"), locator);
|
||||
}
|
||||
|
||||
bool CWalletDB::ReadBestBlock(CBlockLocator& locator)
|
||||
{
|
||||
if (Read(std::string("bestblock"), locator) && !locator.vHave.empty()) return true;
|
||||
return Read(std::string("bestblock_nomerkle"), locator);
|
||||
if (batch.Read(std::string("bestblock"), locator) && !locator.vHave.empty()) return true;
|
||||
return batch.Read(std::string("bestblock_nomerkle"), locator);
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteOrderPosNext(int64_t nOrderPosNext)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Write(std::string("orderposnext"), nOrderPosNext);
|
||||
return batch.Write(std::string("orderposnext"), nOrderPosNext);
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteDefaultKey(const CPubKey& vchPubKey)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Write(std::string("defaultkey"), vchPubKey);
|
||||
return batch.Write(std::string("defaultkey"), vchPubKey);
|
||||
}
|
||||
|
||||
bool CWalletDB::ReadPool(int64_t nPool, CKeyPool& keypool)
|
||||
{
|
||||
return Read(std::make_pair(std::string("pool"), nPool), keypool);
|
||||
return batch.Read(std::make_pair(std::string("pool"), nPool), keypool);
|
||||
}
|
||||
|
||||
bool CWalletDB::WritePool(int64_t nPool, const CKeyPool& keypool)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Write(std::make_pair(std::string("pool"), nPool), keypool);
|
||||
return batch.Write(std::make_pair(std::string("pool"), nPool), keypool);
|
||||
}
|
||||
|
||||
bool CWalletDB::ErasePool(int64_t nPool)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Erase(std::make_pair(std::string("pool"), nPool));
|
||||
return batch.Erase(std::make_pair(std::string("pool"), nPool));
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteMinVersion(int nVersion)
|
||||
{
|
||||
return Write(std::string("minversion"), nVersion);
|
||||
return batch.Write(std::string("minversion"), nVersion);
|
||||
}
|
||||
|
||||
bool CWalletDB::ReadAccount(const std::string& strAccount, CAccount& account)
|
||||
{
|
||||
account.SetNull();
|
||||
return Read(make_pair(std::string("acc"), strAccount), account);
|
||||
return batch.Read(std::make_pair(std::string("acc"), strAccount), account);
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteAccount(const std::string& strAccount, const CAccount& account)
|
||||
{
|
||||
return Write(make_pair(std::string("acc"), strAccount), account);
|
||||
return batch.Write(std::make_pair(std::string("acc"), strAccount), account);
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry)
|
||||
{
|
||||
return Write(std::make_pair(std::string("acentry"), std::make_pair(acentry.strAccount, nAccEntryNum)), acentry);
|
||||
return batch.Write(std::make_pair(std::string("acentry"), std::make_pair(acentry.strAccount, nAccEntryNum)), acentry);
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteAccountingEntry_Backend(const CAccountingEntry& acentry)
|
||||
@ -218,7 +218,7 @@ void CWalletDB::ListAccountCreditDebit(const std::string& strAccount, std::list<
|
||||
{
|
||||
bool fAllAccounts = (strAccount == "*");
|
||||
|
||||
Dbc* pcursor = GetCursor();
|
||||
Dbc* pcursor = batch.GetCursor();
|
||||
if (!pcursor)
|
||||
throw std::runtime_error(std::string(__func__) + ": cannot create DB cursor");
|
||||
bool setRange = true;
|
||||
@ -229,7 +229,7 @@ void CWalletDB::ListAccountCreditDebit(const std::string& strAccount, std::list<
|
||||
if (setRange)
|
||||
ssKey << std::make_pair(std::string("acentry"), std::make_pair((fAllAccounts ? std::string("") : strAccount), uint64_t(0)));
|
||||
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
||||
int ret = ReadAtCursor(pcursor, ssKey, ssValue, setRange);
|
||||
int ret = batch.ReadAtCursor(pcursor, ssKey, ssValue, setRange);
|
||||
setRange = false;
|
||||
if (ret == DB_NOTFOUND)
|
||||
break;
|
||||
@ -560,7 +560,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
|
||||
LOCK(pwallet->cs_wallet);
|
||||
try {
|
||||
int nMinVersion = 0;
|
||||
if (Read((std::string)"minversion", nMinVersion))
|
||||
if (batch.Read((std::string)"minversion", nMinVersion))
|
||||
{
|
||||
if (nMinVersion > CLIENT_VERSION)
|
||||
return DB_TOO_NEW;
|
||||
@ -568,7 +568,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
|
||||
}
|
||||
|
||||
// Get cursor
|
||||
Dbc* pcursor = GetCursor();
|
||||
Dbc* pcursor = batch.GetCursor();
|
||||
if (!pcursor)
|
||||
{
|
||||
LogPrintf("Error getting wallet database cursor\n");
|
||||
@ -580,7 +580,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
|
||||
// Read next record
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
||||
int ret = ReadAtCursor(pcursor, ssKey, ssValue);
|
||||
int ret = batch.ReadAtCursor(pcursor, ssKey, ssValue);
|
||||
if (ret == DB_NOTFOUND)
|
||||
break;
|
||||
else if (ret != 0)
|
||||
@ -664,14 +664,14 @@ DBErrors CWalletDB::FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWal
|
||||
|
||||
try {
|
||||
int nMinVersion = 0;
|
||||
if (Read((std::string)"minversion", nMinVersion))
|
||||
if (batch.Read((std::string)"minversion", nMinVersion))
|
||||
{
|
||||
if (nMinVersion > CLIENT_VERSION)
|
||||
return DB_TOO_NEW;
|
||||
}
|
||||
|
||||
// Get cursor
|
||||
Dbc* pcursor = GetCursor();
|
||||
Dbc* pcursor = batch.GetCursor();
|
||||
if (!pcursor)
|
||||
{
|
||||
LogPrintf("Error getting wallet database cursor\n");
|
||||
@ -683,7 +683,7 @@ DBErrors CWalletDB::FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWal
|
||||
// Read next record
|
||||
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
||||
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
||||
int ret = ReadAtCursor(pcursor, ssKey, ssValue);
|
||||
int ret = batch.ReadAtCursor(pcursor, ssKey, ssValue);
|
||||
if (ret == DB_NOTFOUND)
|
||||
break;
|
||||
else if (ret != 0)
|
||||
@ -855,20 +855,20 @@ bool CWalletDB::VerifyDatabaseFile(const std::string& walletFile, const fs::path
|
||||
bool CWalletDB::WriteDestData(const std::string &address, const std::string &key, const std::string &value)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Write(std::make_pair(std::string("destdata"), std::make_pair(address, key)), value);
|
||||
return batch.Write(std::make_pair(std::string("destdata"), std::make_pair(address, key)), value);
|
||||
}
|
||||
|
||||
bool CWalletDB::EraseDestData(const std::string &address, const std::string &key)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Erase(std::make_pair(std::string("destdata"), std::make_pair(address, key)));
|
||||
return batch.Erase(std::make_pair(std::string("destdata"), std::make_pair(address, key)));
|
||||
}
|
||||
|
||||
|
||||
bool CWalletDB::WriteHDChain(const CHDChain& chain)
|
||||
{
|
||||
nWalletDBUpdateCounter++;
|
||||
return Write(std::string("hdchain"), chain);
|
||||
return batch.Write(std::string("hdchain"), chain);
|
||||
}
|
||||
|
||||
void CWalletDB::IncrementUpdateCounter()
|
||||
@ -881,3 +881,27 @@ unsigned int CWalletDB::GetUpdateCounter()
|
||||
return nWalletDBUpdateCounter;
|
||||
}
|
||||
|
||||
bool CWalletDB::TxnBegin()
|
||||
{
|
||||
return batch.TxnBegin();
|
||||
}
|
||||
|
||||
bool CWalletDB::TxnCommit()
|
||||
{
|
||||
return batch.TxnCommit();
|
||||
}
|
||||
|
||||
bool CWalletDB::TxnAbort()
|
||||
{
|
||||
return batch.TxnAbort();
|
||||
}
|
||||
|
||||
bool CWalletDB::ReadVersion(int& nVersion)
|
||||
{
|
||||
return batch.ReadVersion(nVersion);
|
||||
}
|
||||
|
||||
bool CWalletDB::WriteVersion(int nVersion)
|
||||
{
|
||||
return batch.WriteVersion(nVersion);
|
||||
}
|
||||
|
@ -123,10 +123,11 @@ public:
|
||||
* database. It will be committed when the object goes out of scope.
|
||||
* Optionally (on by default) it will flush to disk as well.
|
||||
*/
|
||||
class CWalletDB : public CDB
|
||||
class CWalletDB
|
||||
{
|
||||
public:
|
||||
CWalletDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool _fFlushOnClose = true) : CDB(dbw, pszMode, _fFlushOnClose)
|
||||
CWalletDB(CWalletDBWrapper& dbw, const char* pszMode = "r+", bool _fFlushOnClose = true) :
|
||||
batch(dbw, pszMode, _fFlushOnClose)
|
||||
{
|
||||
}
|
||||
|
||||
@ -198,7 +199,20 @@ public:
|
||||
|
||||
static void IncrementUpdateCounter();
|
||||
static unsigned int GetUpdateCounter();
|
||||
|
||||
//! Begin a new transaction
|
||||
bool TxnBegin();
|
||||
//! Commit current transaction
|
||||
bool TxnCommit();
|
||||
//! Abort current transaction
|
||||
bool TxnAbort();
|
||||
//! Read wallet version
|
||||
bool ReadVersion(int& nVersion);
|
||||
//! Write wallet version
|
||||
bool WriteVersion(int nVersion);
|
||||
private:
|
||||
CDB batch;
|
||||
|
||||
CWalletDB(const CWalletDB&);
|
||||
void operator=(const CWalletDB&);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user