Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

walletdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_WALLET_WALLETDB_H
7 #define BITCOIN_WALLET_WALLETDB_H
8 
9 #include <amount.h>
10 #include <wallet/db.h>
11 #include <hdchain.h>
12 #include <key.h>
13 
14 #include <list>
15 #include <stdint.h>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
32 static const bool DEFAULT_FLUSHWALLET = true;
33 
34 class CAccount;
35 class CAccountingEntry;
36 struct CBlockLocator;
37 class CKeyPool;
38 class CMasterKey;
39 class CScript;
40 class CWallet;
41 class CWalletTx;
42 class uint160;
43 class uint256;
44 
47 
50 {
57 };
58 
60 {
61 public:
62  static const int CURRENT_VERSION=1;
63  int nVersion;
64  int64_t nCreateTime; // 0 means unknown
65 
67  {
68  SetNull();
69  }
70  explicit CKeyMetadata(int64_t nCreateTime_)
71  {
72  SetNull();
73  nCreateTime = nCreateTime_;
74  }
75 
77 
78  template <typename Stream, typename Operation>
79  inline void SerializationOp(Stream& s, Operation ser_action) {
80  READWRITE(this->nVersion);
82  }
83 
84  void SetNull()
85  {
87  nCreateTime = 0;
88  }
89 };
90 
97 {
98 private:
99  template <typename K, typename T>
100  bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
101  {
102  if (!m_batch.Write(key, value, fOverwrite)) {
103  return false;
104  }
106  return true;
107  }
108 
109  template <typename K>
110  bool EraseIC(const K& key)
111  {
112  if (!m_batch.Erase(key)) {
113  return false;
114  }
116  return true;
117  }
118 
119 public:
120  explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) :
121  m_batch(database, pszMode, _fFlushOnClose),
122  m_database(database)
123  {
124  }
125  WalletBatch(const WalletBatch&) = delete;
126  WalletBatch& operator=(const WalletBatch&) = delete;
127 
128  bool WriteName(const std::string& strAddress, const std::string& strName);
129  bool EraseName(const std::string& strAddress);
130 
131  bool WritePurpose(const std::string& strAddress, const std::string& purpose);
132  bool ErasePurpose(const std::string& strAddress);
133 
134  bool WriteTx(const CWalletTx& wtx);
135  bool EraseTx(uint256 hash);
136 
137  bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
138  bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
139  bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
140 
141  bool WriteCScript(const uint160& hash, const CScript& redeemScript);
142 
143  bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
144  bool EraseWatchOnly(const CScript &script);
145 
146  bool WriteBestBlock(const CBlockLocator& locator);
147  bool ReadBestBlock(CBlockLocator& locator);
148 
149  bool WriteOrderPosNext(int64_t nOrderPosNext);
150 
151  bool ReadPool(int64_t nPool, CKeyPool& keypool);
152  bool WritePool(int64_t nPool, const CKeyPool& keypool);
153  bool ErasePool(int64_t nPool);
154 
155  bool WriteMinVersion(int nVersion);
156 
159  bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
160  bool ReadAccount(const std::string& strAccount, CAccount& account);
161  bool WriteAccount(const std::string& strAccount, const CAccount& account);
162 
163  bool ReadPrivateSendSalt(uint256& salt);
164  bool WritePrivateSendSalt(const uint256& salt);
165 
167  bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
169  bool EraseDestData(const std::string &address, const std::string &key);
170 
171  CAmount GetAccountCreditDebit(const std::string& strAccount);
172  void ListAccountCreditDebit(const std::string& strAccount, std::list<CAccountingEntry>& acentries);
173 
174  DBErrors LoadWallet(CWallet* pwallet);
175  DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::vector<CWalletTx>& vWtx);
176  DBErrors ZapWalletTx(std::vector<CWalletTx>& vWtx);
177  DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
178  /* Try to (very carefully!) recover wallet database (with a possible key type filter) */
179  static bool Recover(const fs::path& wallet_path, void *callbackDataIn, bool (*recoverKVcallback)(void* callbackData, CDataStream ssKey, CDataStream ssValue), std::string& out_backup_filename);
180  /* Recover convenience-function to bypass the key filter callback, called when verify fails, recovers everything */
181  static bool Recover(const fs::path& wallet_path, std::string& out_backup_filename);
182  /* Recover filter (used as callback), will only let keys (cryptographical keys) as KV/key-type pass through */
183  static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue);
184  /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
185  static bool IsKeyType(const std::string& strType);
186  /* verifies the database environment */
187  static bool VerifyEnvironment(const fs::path& wallet_path, std::string& errorStr);
188  /* verifies the database file */
189  static bool VerifyDatabaseFile(const fs::path& wallet_path, std::string& warningStr, std::string& errorStr);
190 
192  bool WriteHDChain(const CHDChain& chain);
193  bool WriteCryptedHDChain(const CHDChain& chain);
194  bool WriteHDPubKey(const CHDPubKey& hdPubKey, const CKeyMetadata& keyMeta);
195 
197  bool TxnBegin();
199  bool TxnCommit();
201  bool TxnAbort();
203  bool ReadVersion(int& nVersion);
205  bool WriteVersion(int nVersion);
206 private:
209 };
210 
212 void MaybeCompactWalletDB();
213 
214 #endif // BITCOIN_WALLET_WALLETDB_H
bool TxnCommit()
Commit current transaction.
Definition: walletdb.cpp:905
CKeyMetadata(int64_t nCreateTime_)
Definition: walletdb.h:70
bool WriteName(const std::string &strAddress, const std::string &strName)
Definition: walletdb.cpp:29
bool EraseIC(const K &key)
Definition: walletdb.h:110
bool WriteVersion(int nVersion)
Write wallet version.
Definition: walletdb.cpp:920
bool EraseDestData(const std::string &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:872
Account information.
Definition: wallet.h:1308
bool ErasePurpose(const std::string &strAddress)
Definition: walletdb.cpp:46
bool ReadBestBlock(CBlockLocator &locator)
Definition: walletdb.cpp:124
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:127
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:76
#define READWRITE(obj)
Definition: serialize.h:165
bool WriteHDPubKey(const CHDPubKey &hdPubKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:892
bool WriteIC(const K &key, const T &value, bool fOverwrite=true)
Definition: walletdb.h:100
DBErrors FindWalletTx(std::vector< uint256 > &vTxHash, std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:675
DBErrors ZapSelectTx(std::vector< uint256 > &vHashIn, std::vector< uint256 > &vHashOut)
Definition: walletdb.cpp:734
bool WriteMinVersion(int nVersion)
Definition: walletdb.cpp:150
bool WriteHDChain(const CHDChain &chain)
write the hdchain model (external chain child index counter)
Definition: walletdb.cpp:877
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:34
bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta)
Definition: walletdb.cpp:102
bool Write(const K &key, const T &value, bool fOverwrite=true)
Definition: db.h:233
static bool VerifyDatabaseFile(const fs::path &wallet_path, std::string &warningStr, std::string &errorStr)
Definition: walletdb.cpp:862
An instance of this class represents one database.
Definition: db.h:95
CKeyMetadata()
Definition: walletdb.h:66
void ListAccountCreditDebit(const std::string &strAccount, std::list< CAccountingEntry > &acentries)
Definition: walletdb.cpp:193
bool TxnBegin()
Begin a new transaction.
Definition: walletdb.cpp:900
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
DBErrors ZapWalletTx(std::vector< CWalletTx > &vWtx)
Definition: walletdb.cpp:772
bool WriteTx(const CWalletTx &wtx)
Definition: walletdb.cpp:51
WalletBatch(WalletDatabase &database, const char *pszMode="r+", bool _fFlushOnClose=true)
Definition: walletdb.h:120
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:564
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:49
static bool Recover(const fs::path &wallet_path, void *callbackDataIn, bool(*recoverKVcallback)(void *callbackData, CDataStream ssKey, CDataStream ssValue), std::string &out_backup_filename)
Definition: walletdb.cpp:822
bool EraseWatchOnly(const CScript &script)
Definition: walletdb.cpp:110
Access to the wallet database.
Definition: walletdb.h:96
bool ReadAccount(const std::string &strAccount, CAccount &account)
Definition: walletdb.cpp:155
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool WriteBestBlock(const CBlockLocator &locator)
Definition: walletdb.cpp:118
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
Definition: key.h:24
bool TxnAbort()
Abort current transaction.
Definition: walletdb.cpp:910
bool Erase(const K &key)
Definition: db.h:262
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value)
Write destination data key,value tuple to database.
Definition: walletdb.cpp:867
static bool IsKeyType(const std::string &strType)
Definition: walletdb.cpp:557
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
Definition: walletdb.cpp:97
static const int CURRENT_VERSION
Definition: walletdb.h:62
static bool VerifyEnvironment(const fs::path &wallet_path, std::string &errorStr)
Definition: walletdb.cpp:857
WalletDatabase & m_database
Definition: walletdb.h:208
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:789
An encapsulated public key.
Definition: pubkey.h:30
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
Definition: walletdb.cpp:92
ADD_SERIALIZE_METHODS
Definition: walletdb.h:76
RAII class that provides access to a Berkeley database.
Definition: db.h:167
bool WriteOrderPosNext(int64_t nOrderPosNext)
Definition: walletdb.cpp:130
int64_t nCreateTime
Definition: walletdb.h:64
bool WritePrivateSendSalt(const uint256 &salt)
Definition: walletdb.cpp:176
CAmount GetAccountCreditDebit(const std::string &strAccount)
Definition: walletdb.cpp:181
bool WriteAccount(const std::string &strAccount, const CAccount &account)
Definition: walletdb.cpp:161
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:280
int nVersion
Definition: walletdb.h:63
bool ErasePool(int64_t nPool)
Definition: walletdb.cpp:145
bool WritePool(int64_t nPool, const CKeyPool &keypool)
Definition: walletdb.cpp:140
256-bit opaque blob.
Definition: uint256.h:123
BerkeleyBatch m_batch
Definition: walletdb.h:207
void IncrementUpdateCounter()
Definition: db.cpp:535
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:32
Internal transfers.
Definition: wallet.h:631
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:715
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
Definition: walletdb.cpp:41
160-bit opaque blob.
Definition: uint256.h:112
bool ReadPool(int64_t nPool, CKeyPool &keypool)
Definition: walletdb.cpp:135
void SerializationOp(Stream &s, Operation ser_action)
Definition: walletdb.h:79
bool ReadVersion(int &nVersion)
Read wallet version.
Definition: walletdb.cpp:915
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
Definition: walletdb.cpp:61
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry &acentry)
This writes directly to the database, and will not update the CWallet&#39;s cached accounting entries! Us...
Definition: walletdb.cpp:166
bool EraseTx(uint256 hash)
Definition: walletdb.cpp:56
bool ReadPrivateSendSalt(uint256 &salt)
Definition: walletdb.cpp:171
void SetNull()
Definition: walletdb.h:84
static bool RecoverKeysOnlyFilter(void *callbackData, CDataStream ssKey, CDataStream ssValue)
Definition: walletdb.cpp:834
WalletBatch & operator=(const WalletBatch &)=delete
A key pool entry.
Definition: wallet.h:119
bool EraseName(const std::string &strAddress)
Definition: walletdb.cpp:34
bool WriteCryptedHDChain(const CHDChain &chain)
Definition: walletdb.cpp:882
Released under the MIT license