Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/walletdb.h
Show All 16 Lines | |||||
#include <list> | #include <list> | ||||
#include <string> | #include <string> | ||||
#include <utility> | #include <utility> | ||||
#include <vector> | #include <vector> | ||||
/** | /** | ||||
* Overview of wallet database classes: | * Overview of wallet database classes: | ||||
* | * | ||||
* - CDBEnv is an environment in which the database exists (has no analog in | * - WalletBatch is an abstract modifier object for the wallet database, and | ||||
* dbwrapper.h) | * encapsulates a database batch update as well as methods to act on the | ||||
* - CWalletDBWrapper represents a wallet database (similar to CDBWrapper in | * database. It should be agnostic to the database implementation. | ||||
* dbwrapper.h) | |||||
* - CDB is a low-level database transaction (similar to CDBBatch in | |||||
* dbwrapper.h) | |||||
* - CWalletDB is a modifier object for the wallet, and encapsulates a database | |||||
* transaction as well as methods to act on the database (no analog in | |||||
* dbwrapper.h) | |||||
* | * | ||||
* The latter two are named confusingly, in contrast to what the names CDB | * The following classes are implementation specific: | ||||
* and CWalletDB suggest they are transient transaction objects and don't | * - BerkeleyEnvironment is an environment in which the database exists. | ||||
* represent the database itself. | * - BerkeleyDatabase represents a wallet database. | ||||
* - BerkeleyBatch is a low-level database batch update. | |||||
*/ | */ | ||||
static const bool DEFAULT_FLUSHWALLET = true; | static const bool DEFAULT_FLUSHWALLET = true; | ||||
class CAccount; | class CAccount; | ||||
class CAccountingEntry; | class CAccountingEntry; | ||||
struct CBlockLocator; | struct CBlockLocator; | ||||
class CKeyPool; | class CKeyPool; | ||||
class CMasterKey; | class CMasterKey; | ||||
class CScript; | class CScript; | ||||
class CWallet; | class CWallet; | ||||
class CWalletTx; | class CWalletTx; | ||||
class uint160; | class uint160; | ||||
class uint256; | class uint256; | ||||
/** Backend-agnostic database type. */ | |||||
using WalletDatabase = BerkeleyDatabase; | |||||
/** Error statuses for the wallet database */ | /** Error statuses for the wallet database */ | ||||
enum class DBErrors { | enum class DBErrors { | ||||
LOAD_OK, | LOAD_OK, | ||||
CORRUPT, | CORRUPT, | ||||
NONCRITICAL_ERROR, | NONCRITICAL_ERROR, | ||||
TOO_NEW, | TOO_NEW, | ||||
LOAD_FAIL, | LOAD_FAIL, | ||||
NEED_REWRITE | NEED_REWRITE | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | void SetNull() { | ||||
nCreateTime = 0; | nCreateTime = 0; | ||||
hdKeypath.clear(); | hdKeypath.clear(); | ||||
hdMasterKeyID.SetNull(); | hdMasterKeyID.SetNull(); | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Access to the wallet database. | * Access to the wallet database. | ||||
* This should really be named CWalletDBBatch, as it represents a single | * This represents a single transaction at the database. It will be committed | ||||
* transaction at the database. It will be committed when the object goes out of | * when the object goes out of scope. Optionally (on by default) it will flush | ||||
* scope. | * to disk as well. | ||||
* Optionally (on by default) it will flush to disk as well. | |||||
*/ | */ | ||||
class CWalletDB { | class WalletBatch { | ||||
private: | private: | ||||
template <typename K, typename T> | template <typename K, typename T> | ||||
bool WriteIC(const K &key, const T &value, bool fOverwrite = true) { | bool WriteIC(const K &key, const T &value, bool fOverwrite = true) { | ||||
if (!batch.Write(key, value, fOverwrite)) { | if (!m_batch.Write(key, value, fOverwrite)) { | ||||
return false; | return false; | ||||
} | } | ||||
m_dbw.IncrementUpdateCounter(); | m_database.IncrementUpdateCounter(); | ||||
return true; | return true; | ||||
} | } | ||||
template <typename K> bool EraseIC(const K &key) { | template <typename K> bool EraseIC(const K &key) { | ||||
if (!batch.Erase(key)) { | if (!m_batch.Erase(key)) { | ||||
return false; | return false; | ||||
} | } | ||||
m_dbw.IncrementUpdateCounter(); | m_database.IncrementUpdateCounter(); | ||||
return true; | return true; | ||||
} | } | ||||
public: | public: | ||||
explicit CWalletDB(CWalletDBWrapper &dbw, const char *pszMode = "r+", | explicit WalletBatch(WalletDatabase &database, const char *pszMode = "r+", | ||||
bool _fFlushOnClose = true) | bool _fFlushOnClose = true) | ||||
: batch(dbw, pszMode, _fFlushOnClose), m_dbw(dbw) {} | : m_batch(database, pszMode, _fFlushOnClose), m_database(database) {} | ||||
CWalletDB(const CWalletDB &) = delete; | WalletBatch(const WalletBatch &) = delete; | ||||
CWalletDB &operator=(const CWalletDB &) = delete; | WalletBatch &operator=(const WalletBatch &) = delete; | ||||
bool WriteName(const CTxDestination &address, const std::string &strName); | bool WriteName(const CTxDestination &address, const std::string &strName); | ||||
bool EraseName(const CTxDestination &address); | bool EraseName(const CTxDestination &address); | ||||
bool WritePurpose(const CTxDestination &address, | bool WritePurpose(const CTxDestination &address, | ||||
const std::string &purpose); | const std::string &purpose); | ||||
bool ErasePurpose(const CTxDestination &address); | bool ErasePurpose(const CTxDestination &address); | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | public: | ||||
//! Abort current transaction | //! Abort current transaction | ||||
bool TxnAbort(); | bool TxnAbort(); | ||||
//! Read wallet version | //! Read wallet version | ||||
bool ReadVersion(int &nVersion); | bool ReadVersion(int &nVersion); | ||||
//! Write wallet version | //! Write wallet version | ||||
bool WriteVersion(int nVersion); | bool WriteVersion(int nVersion); | ||||
private: | private: | ||||
CDB batch; | BerkeleyBatch m_batch; | ||||
CWalletDBWrapper &m_dbw; | WalletDatabase &m_database; | ||||
}; | }; | ||||
//! Compacts BDB state so that wallet.dat is self-contained (if there are | //! Compacts BDB state so that wallet.dat is self-contained (if there are | ||||
//! changes) | //! changes) | ||||
void MaybeCompactWalletDB(); | void MaybeCompactWalletDB(); | ||||
#endif // BITCOIN_WALLET_WALLETDB_H | #endif // BITCOIN_WALLET_WALLETDB_H |